API & Config
Create P2PEngineMedia instance
var engine = new P2PEngineMedia(p2pConfig);
Create a new P2PEngineMedia instance.
If p2pConfig is specified, then the default options (shown below) will be overridden.
Field | Type | Default | Description |
---|---|---|---|
logLevel | string|boolean | 'error' | Print log level(warn, error, none,false=none, true=warn). |
token | string | undefined | Token is used to summarize and display multi domain name data on the console. In addition, token is required while customizing channelId. |
trackerZone | string | 'eu' | The country code name of the tracker server address('eu', 'hk', 'us'). |
pieceLength | number | 512 * 1024 | Length in bytes of every piece but the last one. |
mediaElem | HTMLMediaElement|string | undefined | Specify the ID or element object of the video/audio tag, default value is the first video/audio element in the document tree. |
p2pEnabled | boolean | true | Enable or disable p2p engine. |
webRTCConfig | Object | {} | A Configuration dictionary providing options to configure WebRTC connections. |
tag | string | '' | User customized tag. |
swFile | string | './sw.js' | The file name and path of ServiceWorker. |
swScope | string | './' | The default scope is the location of the ServiceWorker file, and extends to all directories below. So if sw.js is located in the root directory, the ServiceWorker will control requests from all files at this domain. |
showSlogan | boolean | false | Display slogan of cdnbye on console. |
diskCacheLimit | Object | {"pc": 1500 1024 1024, "mobile": 1000 1024 1024} | The max size of binary data that can be stored in the disk cache. |
minBufferCount | number | 0 | Minimum buffer count of pieces. If buffer pieces is/become less than this value, a new piece will be loaded. |
P2PEngineMedia API
P2PEngineMedia.version (static)
Get the version of P2PEngineMedia.
P2PEngineMedia.protocolVersion (static)
Get the version of P2P protocol.
P2PEngineMedia.isSupported() (static)
Returns true if both WebRTC datachannel and ServiceWorker are supported by the browser.
P2PEngineMedia.isWebRTCSupported() (static)
Returns true if WebRTC datachannel is supported by the browser.
P2PEngineMedia.isSeviceWorkerSupported() (static)
Returns true if ServiceWorker is supported by the browser.
engine.getProxiedUrl(originalURL)
Get the promise of proxied playback url.
engine.enableP2P()
Start p2p.
engine.disableP2P()
Stop p2p, free used resources.
engine.restartP2p()
Restart p2p 。
P2PEngineMedia 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
},
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
},
}
The unit of download and upload is KB. The unit of download speed is KB/s.
MediaProxy.version (static)
Get the version of MediaProxy.
Advanced Usage
Dynamic MP4/MP3 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 mp4/mp3 urls play the same live/vod but have different paths on them. For example, example.com/clientId1/streamId.mp4 and example.com/clientId2/streamId.mp4. In this case, you can format a common channelId for them.
// Set token in p2pConfig before setting channelId! Connectivity with other platform should have the same token.
p2pConfig: {
token: YOUR_TOKEN,
channelId: function (url) {
const videoId = extractVideoIdFromUrl(url); // make a channelId by removing the different part which is defined by yourself
return videoId;
}
// channelId: VIDEO_ID // for fixed channel id
}
Interconnect with other platform should ensure that both have the same token and channelId.