API & Config
Create P2PEngineMedia instance
var engine = new P2PEngineMedia(p2pConfig);
Creates a new P2PEngineMedia instance.
If p2pConfig 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' | 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. |
| 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. |
| startFromSegmentOffset | number | 3 | The segment offset that start to connect to tracker server. |
P2PEngineMedia API
P2PEngineMedia.version (static)
Returns the current version of P2PEngineMedia.
P2PEngineMedia.protocolVersion (static)
Returns the P2P protocol version.
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)
Returns the promise of the proxied playback URL.
engine.enableP2P()
Starts P2P.
engine.disableP2P()
Stops P2P and frees up resources.
engine.restartP2p()
Restarts 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 an exception occurs.
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
},
}
Download and upload volumes are measured in KB. Download speed is measured in KB/s.
MediaProxy.version (static)
Returns the current 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
}
To interconnect with another platform, ensure both sides use the same token and channelId.