Skip to main content

API & Config

Create instance

var engine = new P2pEngineVHS(player, {p2pConfig: [opts]});

Create a new P2pEngineVHS instance, player is an instance of video.js .

If opts is specified, then the default options (shown below) will be overridden.

FieldTypeDefaultDescription
logLevelstring|boolean'error'Print log level(warn, error, none,false=none, true=warn).
tokenstringundefinedToken is used to summarize and display multi domain name data on the console. In addition, token is required while customizing channelId.
trackerZonestring'eu'The country code name of the tracker server address('eu', 'hk', 'us').
memoryCacheLimitObject{"pc": 400 1024 1024, "mobile": 100 1024 1024}The max size of binary data that can be stored in the cache.
useDiskCachebooleantrueUse IndexedDB to cache data for VOD streaming.
diskCacheLimitObject{"pc": 2500 1024 1024, "mobile": 1500 1024 1024}The max size of binary data that can be stored in the disk cache.
p2pEnabledbooleantrueEnable or disable p2p engine.
webRTCConfigObject{}A Configuration dictionary providing options to configure WebRTC connections.
useHttpRangebooleantrueUse HTTP ranges requests where it is possible. Allows to continue (and not start over) aborted P2P downloads over HTTP.
httpLoadTimenumber3.0Time for HTTP download if p2p download timeout.
sharePlaylistbooleanfalseAllow the P2P transmission of m3u8 file.
showSloganbooleanfalseDisplay slogan of SwarmCloud on console.
strictSegmentIdbooleanfalseUse segment url based segment id instead of sequence number based one.

P2pEngineVHS API

P2pEngineVHS.version (static method)

Get the version of P2pEngineVHS.

P2pEngineVHS.protocolVersion (static method)

Get the version of P2P protocol.

P2pEngineVHS.isSupported() (static method)

Returns true if WebRTC data channel is supported by the browser.

engine.enableP2P()

Resume P2P if it has been stopped.

engine.disableP2P()

Disable engine to stop p2p and free used resources.

engine.destroy()

Stop p2p and free used resources.

P2pEngineVHS Events

engine.on('peerId', function (peerId) {})

Emitted when the peer Id of this client is obtained from server.

engine.on('peers', function (peers) {})

Emitted when successfully connected with new peer.

engine.on('stats', function (stats) {})

Emitted when data is downloaded/uploaded.
stats.totalHTTPDownloaded: total data downloaded by HTTP(KB).
stats.totalP2PDownloaded: total data downloaded by P2P(KB).
stats.totalP2PUploaded: total data uploaded by P2P(KB).
stats.p2pDownloadSpeed: p2p download speed(KB/s).

engine.on('serverConnected', function (connected) {})

Emitted when websocket is opened/closed.

engine.on('exception', function (e) {})

Emitted when exception occured.
e.code: Exception identifier(TRACKER_EXPT SIGNAL_EXPT)
e.message: Exception message
e.stack: Exception stack

Get p2p information from p2pConfig

p2pConfig: {
getStats: function (totalP2PDownloaded, totalP2PUploaded, totalHTTPDownloaded, p2pDownloadSpeed) {
// get the downloading statistics
},
getPeerId: function (peerId) {
// get peer Id
},
getPeersInfo: function (peers) {
// get peers information
}
}

Advanced Usage

Dynamic M3u8/mpd Path Support

Some m3u8/mpd urls play the same live/vod but have different paths on them. For example, example.com/clientId1/streamId.mpd and example.com/clientId2/streamId.mpd. In this case, you can format a common channelId for them.

// Set token in p2pConfig before setting channelId!
p2pConfig: {
token: YOUR_TOKEN,
channelId: function (mpdUrl) {
const videoId = extractVideoIdFromUrl(mpdUrl); // make a channelId by removing the different part which is defined by yourself
return videoId;
}
// channelId: VIDEO_ID // for fixed channel id
}

Dynamic Segment Path Support

Like dynamic mpd path, you should format a common segmentId for the same segment file. You can override the segment ID like this:

p2pConfig: {
/*
segmentUrl: The url of segment
range: The byte range of segment
*/
segmentId: function (segmentUrl, range) {
const segId = extractSegmentIdFromUrl(segmentUrl, range);
return segId;
}
}