API & Config
# Create instance
# var engine = new P2PEngineMp4(video, mediaConfig);
Create a new P2PEngineMp4 instance, video is an html5 video tag.
If mediaConfig is specified, then the default options (shown below) will be overridden.
Field | Type | Default | Description |
---|---|---|---|
autoplay | boolean | false | Whether auto play video once ready. |
controls | boolean | false | Whether display control. |
muted | boolean | false | Whether to play video silently. |
maxBufferLength | number | 60 | Maximum buffer length in seconds. Player will never exceed this value. |
minBufferLength | number | 40 | Minimum buffer length in seconds. If buffer length is/become less than this value, a new fragment will be loaded. |
p2pConfig | Object | - | P2P configuration(shown below). |
If mediaConfig.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). |
wsSignalerAddr | string | 'wss://signal.cdnbye.com' | The address of signal server. |
announce | string | 'https://tracker.cdnbye.com/v1' | The address of tracker server. |
memoryCacheLimit | Object | {"pc": 1024 * 1024 * 512, "mobile": 1024 * 1024 * 256} | The max size of binary data that can be stored in the cache. |
p2pEnabled | boolean | true | Enable or disable p2p engine. |
wifiOnly | boolean | false | Only allow uploading on Wi-Fi and Ethernet. |
dcDownloadTimeout | number | 25 | Max download timeout for WebRTC datachannel. |
webRTCConfig | Object | {} | A Configuration dictionary providing options to configure WebRTC connections. |
channelIdPrefix | string | '' | Required while using customized channelId(5 <= length <= 15), recommended to set it as the unique identifier of your organization. |
pieceLength | number | 512 * 1024 | Length in bytes of every piece but the last one. |
httpMaxRetrys | number | 3 | Maximum retry times of file download by HTTP. |
# P2PEngineMp4 API
# P2PEngineMp4.version (static)
Get the version of P2PEngineMp4.
# P2PEngineMp4.protocolVersion (static)
Get the version of P2P protocol.
# P2PEngineMp4.isSupported() (static method)
Returns true if both WebRTC data channel and MSE(Media Source Extension) are supported by the browser.
# engine.loadSource(url)
load source of mp4。
# 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.
# P2PEngineMp4 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 MEDIA_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
# Switch to new source
Call destroy to free used resources, then create P2PEngineMp4 instance again.
engine.destroy();
engine = new P2PEngineMp4(video);
engine.loadSource('another.mp4');
# Dynamic MP4 path issue
Some MP4 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 channelIdPrefix in p2pConfig before setting channelId!
p2pConfig: {
channelIdPrefix: YOUR_UNIQUE_ID,
channelId: function (mp4Url) {
const videoId = extractVideoIdFromUrl(mp4Url); // make a channelId by removing the different part which is defined by yourself
return videoId;
}
}
# Config STUN Servers
p2pConfig: {
webRTCConfig: {
config: { // custom webrtc configuration (used by RTCPeerConnection constructor)
iceServers: [
{ urls: 'stun:stun.l.google.com:19302' },
{ urls: 'stun:global.stun.twilio.com:3478?transport=udp' }
]
}
}
}