API & Config
P2P Configuration
See Android, iOS Configuration and Type Definition
P2pEngine
Instantiate P2pEngine,which is a singleton:
FlutterP2pEngine.init(
token, /// replace with your token
config: P2pConfig.byDefault()
);
Explanation:
param | type | required | description |
---|---|---|---|
token | String | Yes | Token assigned by SwarmCloud |
config | P2pConfig | Yes | 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 parseStreamURL :
String parsedUrl = await FlutterP2pEngine.parseStreamURL(url);
FlutterP2pEngine API
/// The version of SDK.
static Future<String> getSDKVersion()
/// Create a new instance with token and the specified config.
static Future<int> init(
token, {
P2pConfig config,
void Function(Map<String, dynamic>)? infoListener
})
/// Get parsed local stream url by passing the original stream url(m3u8) .
static Future<String> parseStreamURL(
String sourceUrl, [
String videoId,
])
/// Set HTTP Headers while requesting ts and m3u8 dynamically.
static Future setHttpHeadersForHls(Map<String, String>? headers)
/// Set HTTP Headers while requesting Dash files dynamically.
static Future setHttpHeadersForDash(Map<String, String>? headers)
/// 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.
Callback Player Stats
On HLS Live streaming, to improve performance, we recommend to tell p2p engine the duration from the playback time to the end of the buffered interval. In order to do so, you need to use callback bufferedDurationGenerator .
FlutterP2pEngine.init(
token, /// replace with your token
bufferedDurationGeneratorEnable: true,
);
String parsedUrl = await FlutterP2pEngine.parseStreamURL(
url,
bufferedDurationGenerator: () {
return vpController!.value.buffered.last.end - vpController!.value.position;
},
);
Dynamic m3u8 Path Support
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 videoId = extractVideoIdFromUrl(originalUrl); /// extractVideoIdFromUrl is a function defined by yourself, you just need to extract video id from url
String url = await FlutterP2pEngine.parseStreamURL(originalUrl, videoId);
Interconnect with other platform should ensure that both have the same token and channelId.
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 httpHeadersForHls :
FlutterP2pEngine.setHttpHeadersForHls({
"referer": "XXX",
"User-Agent": "XXX",
});