API & Config
Create instance
var engine = new P2PEngineDash(player, {p2pConfig: [opts]});
Creates a new P2PEngineDash instance; player is an instance of dashjs#MediaPlayer.
If opts is provided, it overrides the default options shown below.
| Field | Type | Default | Description |
|---|---|---|---|
| logLevel | string|boolean | 'error' | Log level to print (warn, error, none; false=none, true=warn). |
| token | string | undefined | Token used to aggregate and display multi-domain data on the console. Also required when customizing channelId. |
| trackerZone | string | 'eu' | Country code for the tracker server address ('eu', 'hk', 'us'). |
| memoryCacheLimit | Object | {"pc": 400 * 1024 * 1024, "mobile": 100 * 1024 * 1024} | Maximum size of binary data that can be stored in the in-memory cache. |
| useDiskCache | boolean | true | Uses IndexedDB to cache data for VOD streaming. |
| diskCacheLimit | Object | {"pc": 1500 * 1024 * 1024, "mobile": 1000 * 1024 * 1024} | Maximum size of binary data that can be stored in the disk cache. |
| p2pEnabled | boolean | true | Enables or disables the P2P engine. |
| webRTCConfig | Object | {} | A configuration dictionary for configuring WebRTC connections. |
| useHttpRange | boolean | true | Uses HTTP range requests where possible, allowing aborted P2P downloads to resume over HTTP instead of starting over. |
| startFromSegmentOffset | number | 3 | The segment offset at which the client starts connecting to the tracker server. |
P2PEngineDash API
P2PEngineDash.version (static)
Returns the current version of P2PEngineDash.
P2PEngineDash.protocolVersion (static)
Returns the P2P protocol version.
P2PEngineDash.isSupported() (static method)
Returns true if the browser supports WebRTC data channels.
engine.enableP2P()
Resumes P2P streaming after it has been stopped.
engine.disableP2P()
Disables the engine to stop P2P streaming and free up resources.
engine.destroy()
Stops P2P streaming and frees up resources.
P2PEngineDash Events
engine.on('peerId', function (peerId) {})
Emitted when this client's peer ID is received from the server.
engine.on('peers', function (peers) {})
Emitted when successfully connected to a new peer.
engine.on('stats', function (stats) {})
Emitted whenever data is downloaded or uploaded.
stats.totalHTTPDownloaded: total data downloaded via HTTP (KB).
stats.totalP2PDownloaded: total data downloaded via P2P (KB).
stats.totalP2PUploaded: total data uploaded via P2P (KB).
stats.p2pDownloadSpeed: P2P download speed (KB/s).
engine.on('serverConnected', function (connected) {})
Emitted when the WebSocket connection opens or closes.
engine.on('exception', function (e) {})
Emitted when an exception occurs.
e.code: Exception identifier(TRACKER_EXPT SIGNAL_EXPT DASHJS_EXPT)
e.message: Exception message.
e.stack: Exception stack trace.
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
},
onHttpDownloaded: function (traffic) {
// listen to http downloaded traffic
},
onP2pDownloaded: function (traffic, speed) {
// listen to p2p downloaded traffic
},
onP2pUploaded: function (traffic) {
// listen to p2p uploaded traffic
},
}
Download and upload volumes are measured in KB. Download speed is measured in KB/s.
Advanced Usage
Dynamic MPD Path Support
Some MPD URLs point to the same live/VOD content but have different paths. For example, example.com/clientId1/streamId.mpd and example.com/clientId2/streamId.mpd. In this case, you can define 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
Similar to dynamic mpd path support, you should define a common segmentId for the same segment file. You can override the segment ID as follows:
p2pConfig: {
/*
streamId: The id of stream
sn: The serial number of segment
segmentUrl: The url of segment
range: bytes range of segmentUrl
*/
segmentId: function (segmentUrl, range) {
const segId = extractSegmentIdFromUrl(segmentUrl);
return segId;
}
}
Allow Http Range Request
When HTTP range requests are enabled, chunks of data can be retrieved from peers and the remaining chunks fetched from the CDN to complete each segment, reducing your CDN bandwidth usage. The code below is also required:
p2pConfig: {
useHttpRange: true,
}
How to Check Segment Validity
Sometimes you need to prevent a peer from sending a fake segment (similar to how BitTorrent uses hash functions). CDNBye provides a validation callback with the buffer of the downloaded segment; you implement the actual validation logic. For example, you could generate hashes for each segment and store them in a file, or inject the hash information into the m3u8 playlist. If the callback returns false, the segment is considered invalid.
p2pConfig: {
validateSegment: function (segId, buffer) {
var hash = hashFile.getHash(segId);
return hash === md5(buffer);
}
}