API & Config
# P2P Configuration
The parameters below is the default values:
P2pConfig(
logLevel: P2pLogLevel.warn, // Print log level
wsSignalerAddr: 'wss://signal.cdnbye.com', // The address of signal server
announce: 'https://tracker.cdnbye.com/v1', // The address of tracker server
diskCacheLimit: 1024 * 1024 * 1024, // The max size of binary data that can be stored in the disk cache for VOD(Set to 0 will disable disk cache)
memoryCacheLimit: 60 * 1024 * 1024, // The max size of binary data that can be stored in the memory cache, only for iOS
memoryCacheCountLimit: 30, // The max count of ts files that can be stored in the memory cache, only for Android
p2pEnabled: true, // Enable or disable p2p engine
downloadTimeout: const Duration(seconds: 15), // TS file download timeout by HTTP
dcDownloadTimeout: const Duration(seconds: 6), // TS file download timeout by P2P
tag: "flutter", // User defined tag which is presented in console
localPort: 52019, // The port for local http server(default is random port)
maxPeerConnections: 20, // Max peer connections at the same time
useHttpRange: true, // Use HTTP ranges requests where it is possible. Allows to continue (and not start over) aborted P2P downloads over HTTP
wifiOnly: false, // Only allow uploading on Wi-Fi and Ethernet
httpHeaders: {}, // Set HTTP Headers while requesting ts and m3u8
channelIdPrefix: "cdnbye", // Required while using customized channelId(5 < length < 15), recommended to set it as the unique identifier of your organization
isSetTopBox: false, // Set it as true if SDK is running on set-top box
),
# P2pEngine
Instantiate P2pEngine,which is a singleton:
Cdnbye.init(
token, // replace with your token
config: P2pConfig.byDefault()
);
Explanation:
param | type | required | description |
---|---|---|---|
token | String | Yes | Token assigned by CDNBye. |
config | P2pConfig | No | Custom configuration. |
# Switch Stream URL
When switching to a new stream URL, before passing new stream url(m3u8) to the player, pass that URL through Cdnbye :
String parsedUrl = await Cdnbye.parseStreamURL(url);
# Cdnbye API
/// The version of SDK.
static Future<String> get platformVersion
/// Create a new instance with token and the specified config.
static Future<int> init(
token, {
P2pConfig config,
CdnByeInfoListener infoListener,
String Function(int level, int sn, String url) segmentIdGenerator,
})
/// Get parsed local stream url by passing the original stream url(m3u8) to Cdnbye .
static Future<String> parseStreamURL(
String sourceUrl, [
String videoId,
])
/// Get the connection state of p2p engine.
static Future<bool> isConnected()
/// Restart p2p engine.
static Future restartP2p()
/// Stop p2p and free used resources.
static Future stopP2p()
/// Get the peer ID of p2p engine.
static Future<String> getPeerId()
# P2P Statistics
Please see example
The unit of download and upload is KB.
# Dynamic m3u8 Path Issue
# Dynamic m3u8 Path Issue
The channelId is an identifier used by our backend to match peers that are watching the same content. It is an optional parameter, and by default, we generate channelId from the content URL by removing any query parameters and protocol from it. Some m3u8 urls play the same live/vod but have different paths on them. For example, example.com/clientId1/streamId.m3u8 and example.com/clientId2/streamId.m3u8. In this case, you can format a common channelId for them.
String channelId = originalUrl.split('/').last;
String url = await Cdnbye.parseStreamURL(originalUrl, channelId);
Interconnect with other platform should ensure that both have the same channelIdPrefix and channelId.
# Dynamic ts path issue
Like dynamic m3u8 path issue, you should format a common segmentId for the same ts file. You can override the segment ID like this:
Cdnbye.init(
toekn, // replace with your token
segmentIdGenerator: (level, sn, url) {
// TODO: return your segmentId
return url;
},
);
# Setup HTTP headers
Some HTTP requests need to add header information such as referer or User-Agent for Anti-Leech or statistical requirements. It can be set via setHttpHeaders :
P2pConfig(
httpHeaders: {
"referer": "XXX",
"User-Agent": "XXX",
}
)