Core functionality for P2P sharing of segmented media streams (i.e. HLS, MPEG-DASH) using WebRTC.
Useful links:
The library uses window.p2pml.core
as a root namespace in Web browser for:
HybridLoader
- HTTP and P2P loaderEvents
- Events emitted byHybridLoader
Segment
- Media stream segmentversion
- API version
HTTP and P2P loader.
Returns true
if WebRTC data channels API is supported by the browser. Read more here.
Creates a new HybridLoader
instance.
If settings
is specified, then the default settings (shown below) will be overridden.
Name | Type | Default Value | Description |
---|---|---|---|
cachedSegmentExpiration |
Integer | 300000 | Segment lifetime in cache. The segment is deleted from the cache if the last access time is greater than this value (in milliseconds) |
cachedSegmentsCount |
Integer | 30 | Max number of segments that can be stored in the cache |
requiredSegmentsPriority |
Integer | 1 | The maximum priority of the segments to be downloaded (if not available) as quickly as possible (i.e. via HTTP method) |
useP2P |
Boolean | true | Enable/Disable peers interaction |
simultaneousP2PDownloads |
Integer | 3 | Max number of simultaneous downloads from peers |
httpDownloadProbability |
Float | 0.06 | Probability of downloading remaining not downloaded segment in the segments queue via HTTP |
httpDownloadProbabilityInterval |
Integer | 500 | Interval of the httpDownloadProbability check (in milliseconds) |
bufferedSegmentsCount |
Integer | 20 | Max number of the segments to be downloaded via HTTP or P2P methods |
trackerAnnounce |
String[] | [ "wss://tracker.btorrent.xyz/", "wss://tracker.openwebtorrent.com/" ] | Torrent trackers (announcers) to use |
webRtcMaxMessageSize |
Integer | 64 * 1024 - 1 | Max WebRTC message size. 64KiB - 1B should work with most of recent browsers. Set it to 16KiB for older browsers support. |
p2pSegmentDownloadTimeout |
Integer | 60000 | Timeout to download a segment from a peer. If exceeded the peer is dropped. |
rtcConfig |
RTCConfiguration | Object | An RTCConfiguration dictionary providing options to configure WebRTC connections. |
Creates new queue of segments to download. Aborts all http and peer connections for segments that are not in the new load and emits Events.SegmentAbort
event for each aborted event.
Function args:
segments
- array ofSegment
class instances with populatedurl
andpriority
field;swarmId
- used for gathering peers in pool;
Emitted when segment have been downloaded.
Listener args:
segment
- instance ofSegment
class with populatedurl
anddata
fields;
Emitted when an error occurred while loading the segment.
Listener args:
segment
- url of the segment;error
- error details;
Emitted for each segment that does not hit into a new segments queue when the load
method is called.
Listener args:
segment
- aborted segment;
Emitted when a peer is connected.
Listener args:
peer
- peer object with populatedid
andremoteAddress
fields;
Emitted when a peer is disconnected.
Listener args:
peerId
- Id of the disconnected peer;
Emitted when a segment piece downloaded.
Listener args:
method
- downloading method, possible values:http
,p2p
;bytes
- amount of bytes downloaded;
Emitted when a segment piece uploaded.
Listener args:
method
- uploading method, possible values:p2p
;bytes
- amount of bytes downloaded;
Returns loader instance settings.
Returns a segment from loader cache or undefined if the segment is not available.
Function args:
id
- Id of the segment;
Destroys loader: abort all connections (http, tcp, peer), clears cached segments.
Events that are emitted by HybridLoader
.
- SegmentLoaded
- SegmentError
- SegmentAbort
- PeerConnect
- PeerClose
- PieceBytesDownloaded
- PieceBytesUploaded
Media stream segment.
Instance contains:
id
- a
String
- unique identifier of the segment across peers
- can be equal to URL if it is the same for all peers
- a
url
- a
String
- URL of the segment
- a
range
- a
String
- must be valid HTTP Range header value or
undefined
- a
priority
- a non-negative integer
Number
- the lower value - the higher priority
- default is
0
- a non-negative integer
data
- an
ArrayBuffer
- available only when segment is fully loaded; subscribe to
SegmentLoaded
event for this very moment
- an
downloadSpeed
- a non-negative integer
Number
- download speed in bytes per millisecond or 0
- a non-negative integer
var loader = new p2pml.core.HybridLoader();
loader.on(p2pml.core.Events.SegmentLoaded, function (segment) {
console.log("Loading finished, bytes:", segment.data.byteLength);
});
loader.on(p2pml.core.Events.SegmentError, function (segment, error) {
console.log("Loading failed", segment, error);
});
loader.load([
new p2pml.core.Segment("segment-1", "//url/to/segment/1", undefined, 0),
new p2pml.core.Segment("segment-2", "//url/to/segment/2", undefined, 1),
new p2pml.core.Segment("segment-3", "//url/to/segment/3", undefined, 2)
], "swarm-1");