csai_sdk 0.2.0
csai_sdk: ^0.2.0 copied to clipboard
Client-Side Ad Insertion (CSAI) SDK for Flutter with VAST 4.0 support, HLS/MPEG-DASH streaming, and seamless ad insertion.
example/lib/main.dart
import 'package:csai_sdk/csai_sdk.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const CsaiSdkExampleApp());
}
class CsaiSdkExampleApp extends StatelessWidget {
const CsaiSdkExampleApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'CSAI SDK Example',
theme: ThemeData(primarySwatch: Colors.blue),
home: const CsaiSdkExampleHome(),
);
}
}
class CsaiSdkExampleHome extends StatefulWidget {
const CsaiSdkExampleHome({Key? key}) : super(key: key);
@override
State<CsaiSdkExampleHome> createState() => _CsaiSdkExampleHomeState();
}
class _CsaiSdkExampleHomeState extends State<CsaiSdkExampleHome> {
String _adRequestResult = 'Tap button to generate Ad Request';
String _vastResponseResult = 'Tap button to get VAST Response';
bool _isLoading = false;
@override
void initState() {
super.initState();
_setupListeners();
}
/// Setup SCTE-35 and Ad Event listeners
void _setupListeners() {
// Listen to SCTE-35 cues
CsaiSdk.setScte35Listener((cue) {
_showSnackBar(
'SCTE-35 Cue: eventId=${cue.eventId}, duration=${cue.duration}ms');
});
// Listen to Ad events
CsaiSdk.setAdEventListener((event) {
_showSnackBar('Ad Event: ${event.eventType} - ${event.adTitle}');
});
}
/// Generate Ad Request
Future<void> _generateAdRequest() async {
setState(() => _isLoading = true);
try {
final response = await CsaiSdk.generateAdRequest({
'deviceId': 'device_12345',
'operatorId': 'operator_001',
'channelId': 'ch_001',
'channelName': 'Channel 1',
'channelGenre': 'news',
'adBlockDuration': '60',
});
setState(() {
_adRequestResult = 'Ad Request Generated:\n'
'Device: ${response['deviceId']}\n'
'Operator: ${response['operatorId']}\n'
'Channel: ${response['channelId']}\n'
'Timestamp: ${response['timestamp']}';
});
_showSnackBar('Ad Request generated successfully');
} catch (e) {
setState(() => _adRequestResult = 'Error: $e');
_showSnackBar('Error: $e');
} finally {
setState(() => _isLoading = false);
}
}
/// Get VAST Response
Future<void> _getVastResponse() async {
setState(() => _isLoading = true);
try {
// Create Ad Request
final adRequest = AdRequest(
deviceId: 'device_12345',
operatorId: 'operator_001',
channelId: 'ch_001',
channelName: 'Channel 1',
channelGenre: 'news',
adBlockDuration: 60,
);
// Get VAST Response
final vastUrl = await CsaiSdk.getVastResponse(adRequest.toJsonString());
setState(() {
_vastResponseResult = 'VAST Response:\n'
'URL: $vastUrl';
});
_showSnackBar('VAST Response received');
} catch (e) {
setState(() => _vastResponseResult = 'Error: $e');
_showSnackBar('Error: $e');
} finally {
setState(() => _isLoading = false);
}
}
/// Send Ad Report
Future<void> _sendReport() async {
setState(() => _isLoading = true);
try {
// Create Ad Report
final report = AdReport(
adBlockId: 'adblock_001',
duration: 60,
status: AdPlaybackStatus.completed,
playbackInfo: [
AdPlaybackInfo(
adId: 'ad_001',
title: 'Sample Ad',
playbackDuration: 10.5,
status: AdPlaybackStatus.completed,
),
],
);
// Send Report
final success = await CsaiSdk.sendReport(report.toJsonString());
if (success) {
_showSnackBar('Report sent successfully');
} else {
_showSnackBar('Failed to send report');
}
} catch (e) {
_showSnackBar('Error: $e');
} finally {
setState(() => _isLoading = false);
}
}
void _showSnackBar(String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(message), duration: const Duration(seconds: 2)),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('CSAI SDK Example')),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Ad Request Section
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Ad Request',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 16),
Text(_adRequestResult),
const SizedBox(height: 16),
ElevatedButton.icon(
onPressed: _generateAdRequest,
icon: const Icon(Icons.send),
label: const Text('Generate Ad Request'),
),
],
),
),
),
const SizedBox(height: 16),
// VAST Response Section
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'VAST Response',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 16),
Text(_vastResponseResult),
const SizedBox(height: 16),
ElevatedButton.icon(
onPressed: _getVastResponse,
icon: const Icon(Icons.cloud_download),
label: const Text('Get VAST Response'),
),
],
),
),
),
const SizedBox(height: 16),
// Send Report Section
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Ad Report',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 16),
const Text('Send playback report to CSAI Platform'),
const SizedBox(height: 16),
ElevatedButton.icon(
onPressed: _sendReport,
icon: const Icon(Icons.upload),
label: const Text('Send Report'),
),
],
),
),
),
],
),
),
);
}
}