Skip to main content

API & Config

P2P Configuration

See Android and iOS Configuration

P2pEngine

Instantiate P2pEngine,which is a singleton:

FlutterP2pEngine.init(
token, /// replace with your token
config: P2pConfig.byDefault()
);

Explanation:


paramtyperequireddescription
tokenStringYesToken assigned by SwarmCloud
configP2pConfigNoCustom 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,
])

/// 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

note

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);
note

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 :

P2pConfig(
httpHeadersForHls: {
"referer": "XXX",
"User-Agent": "XXX",
}
)