API & Config
P2P Configuration
See the Android and iOS configuration guides, and the Type Definition
P2pEngine
Instantiate the P2pEngine singleton:
FlutterP2pEngine.init(
token, /// replace with your token
config: P2pConfig.byDefault()
);
Parameter description:
| 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, pass the URL through parseStreamURL before passing it (m3u8) to the player:
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
See the example
Download and upload volumes are measured in KB.
Callback Player Stats
For HLS live streaming, to improve performance, we recommend informing the P2P engine of the duration between the current playback position and the end of the buffered interval. To do so, use the bufferedDurationGenerator callback.
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 watching the same content. It's an optional parameter — by default, channelId is generated from the content URL by stripping the protocol and any query parameters. Some m3u8 URLs point to the same live/VOD content but have different paths — for example, example.com/clientId1/streamId.m3u8 and example.com/clientId2/streamId.m3u8. In this case, you can define 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);
To interconnect with another platform, ensure both sides use the same token and channelId.
Setup HTTP headers
Some HTTP requests require additional header information, such as referer or User-Agent, for anti-leech protection or analytics purposes. This can be configured via httpHeadersForHls :
FlutterP2pEngine.setHttpHeadersForHls({
"referer": "XXX",
"User-Agent": "XXX",
});