P2P Media Loader Documentation
    Preparing search index...

    Type Alias StreamConfig

    Configuration options for the Core functionality, including network and processing parameters.

    type StreamConfig = {
        isP2PUploadDisabled: boolean;
        isP2PDisabled: boolean;
        highDemandTimeWindow: number;
        httpDownloadTimeWindow: number;
        httpDownloadInitialTimeoutMs: number;
        p2pDownloadTimeWindow: number;
        simultaneousHttpDownloads: number;
        simultaneousP2PDownloads: number;
        webRtcMaxMessageSize: number;
        p2pNotReceivingBytesTimeoutMs: number;
        p2pInactiveLoaderDestroyTimeoutMs: number;
        httpNotReceivingBytesTimeoutMs: number;
        httpErrorRetries: number;
        p2pErrorRetries: number;
        announceTrackers: string[];
        rtcConfig: RTCConfiguration;
        swarmId?: string;
        validateP2PSegment?: (
            url: string,
            byteRange: ByteRange | undefined,
            data: ArrayBuffer,
        ) => Promise<boolean>;
        validateHTTPSegment?: (
            url: string,
            byteRange: ByteRange | undefined,
            data: ArrayBuffer,
        ) => Promise<boolean>;
        httpRequestSetup?: (
            segmentUrl: string,
            segmentByteRange: ByteRange | undefined,
            requestAbortSignal: AbortSignal | undefined,
            requestByteRange: { start: number; end?: number } | undefined,
        ) => Promise<Request | undefined | null>;
        p2pMaxPeers: number;
        p2pChurnMaxPeersMultiplier: number;
        p2pChurnCleanupIntervalMs: number;
        p2pChurnGracePeriodMs: number;
        webRtcOffersCount: number;
        webRtcOfferTimeoutMs: number;
        webRtcIceGatheringTimeoutMs: number;
        webRtcConnectionTimeoutMs: number;
    }
    Index

    Properties

    isP2PUploadDisabled: boolean

    Controls whether peer-to-peer uploading is disabled for the stream. If true, the stream will only download segments and will not upload to other peers.

    isP2PUploadDisabled: false
    
    isP2PDisabled: boolean

    Controls whether all peer-to-peer functionality is disabled for the stream.

    isP2PDisabled: false
    
    highDemandTimeWindow: number

    Defines the duration of the time window (in seconds) during which segments are preemptively loaded to ensure smooth playback. This window prioritizes the fetching of media segments that will be played imminently.

    highDemandTimeWindow: 15
    
    httpDownloadTimeWindow: number

    Defines the time window (in seconds) for HTTP segment downloads. This property specifies the duration over which media segments are preemptively fetched using HTTP requests.

    To achieve a higher P2P ratio, it is recommended to set httpDownloadTimeWindow lower than p2pDownloadTimeWindow.

    NOTE: This setting only takes effect if there is at least one peer connection, and the connected peer does not have the requested segments available to share via P2P.

    httpDownloadTimeWindow: 3000
    
    httpDownloadInitialTimeoutMs: number

    The delay (in milliseconds) before falling back to HTTP for the very first segments. This allows the tracker time to discover peers when playback first begins. A higher value provides a better opportunity to download initial segments via P2P, thereby improving the overall P2P ratio. However, setting this value too high can increase playback startup time or stall playback if peers are not immediately available. If set to 0, the HTTP fallback will occur immediately.

    httpDownloadInitialTimeoutMs: 0
    
    p2pDownloadTimeWindow: number

    Defines the time window (in seconds) dedicated to preemptively fetching media segments via Peer-to-Peer (P2P) downloads. This duration determines how much content is downloaded in advance via P2P connections to ensure smooth playback and reduce reliance on HTTP downloads.

    To achieve a higher P2P ratio, it is recommended to set this time window higher than httpDownloadTimeWindow to maximize P2P usage.

    p2pDownloadTimeWindow: 6000
    
    simultaneousHttpDownloads: number

    The maximum number of simultaneous HTTP downloads allowed.

    simultaneousHttpDownloads: 2
    
    simultaneousP2PDownloads: number

    The maximum number of simultaneous P2P downloads allowed.

    simultaneousP2PDownloads: 3
    
    webRtcMaxMessageSize: number

    The maximum message size for WebRTC communications, in bytes.

    webRtcMaxMessageSize: 64 * 1024 - 1
    
    p2pNotReceivingBytesTimeoutMs: number

    The timeout duration (in milliseconds) for not receiving bytes from a P2P connection.

    p2pNotReceivingBytesTimeoutMs: 2000
    
    p2pInactiveLoaderDestroyTimeoutMs: number

    The timeout duration (in milliseconds) before destroying the P2P loader if it remains inactive.

    p2pInactiveLoaderDestroyTimeoutMs: 30 * 1000
    
    httpNotReceivingBytesTimeoutMs: number

    The timeout duration (in milliseconds) for not receiving bytes from an HTTP download.

    httpNotReceivingBytesTimeoutMs: 3000
    
    httpErrorRetries: number

    The number of retries allowed following an HTTP error.

    httpErrorRetries: 3
    
    p2pErrorRetries: number

    The number of retries allowed following a P2P error.

    p2pErrorRetries: 3
    
    announceTrackers: string[]

    A list of URLs to the WebTorrent trackers used for announcing and discovering peers (i.e., WebRTC signaling).

    WARNING: In the Safari browser, only the first tracker will be utilized. Safari has known issues with multiple trackers, which can lead to problems sending SDP messages during WebRTC signaling.

    The default trackers used are:

    [
    "wss://tracker.novage.com.ua",
    "wss://tracker.openwebtorrent.com",
    ]
    rtcConfig: RTCConfiguration

    The configuration for the RTC layer, utilized during WebRTC communication. This configuration specifies the STUN/TURN servers used by WebRTC to establish connections through NATs and firewalls.

    {
    "rtcConfig": {
    "iceServers": [
    { "urls": "stun:stun.l.google.com:19302" },
    { "urls": "stun:global.stun.twilio.com:3478" }
    ]
    }
    }
    swarmId?: string

    An optional unique identifier for the swarm, used to isolate peer pools by media stream. If left undefined, the manifest URL will be used as the swarm ID.

    swarmId: undefined
    
    validateP2PSegment?: (
        url: string,
        byteRange: ByteRange | undefined,
        data: ArrayBuffer,
    ) => Promise<boolean>

    An optional function to validate a P2P segment before fully integrating it into the playback buffer.

    Type Declaration

      • (
            url: string,
            byteRange: ByteRange | undefined,
            data: ArrayBuffer,
        ): Promise<boolean>
      • Parameters

        • url: string

          The URL of the segment to validate.

        • byteRange: ByteRange | undefined

          The optional byte range of the segment.

        • data: ArrayBuffer

          The downloaded segment data.

        Returns Promise<boolean>

        A promise that resolves to a boolean indicating whether the segment is valid.

    validateP2PSegment: undefined
    
    validateHTTPSegment?: (
        url: string,
        byteRange: ByteRange | undefined,
        data: ArrayBuffer,
    ) => Promise<boolean>

    An optional function to validate an HTTP segment before fully integrating it into the playback buffer.

    Type Declaration

      • (
            url: string,
            byteRange: ByteRange | undefined,
            data: ArrayBuffer,
        ): Promise<boolean>
      • Parameters

        • url: string

          The URL of the segment to validate.

        • byteRange: ByteRange | undefined

          The optional byte range of the segment.

        • data: ArrayBuffer

          The downloaded segment data.

        Returns Promise<boolean>

        A promise that resolves to a boolean indicating whether the segment is valid.

    validateHTTPSegment: undefined
    
    httpRequestSetup?: (
        segmentUrl: string,
        segmentByteRange: ByteRange | undefined,
        requestAbortSignal: AbortSignal | undefined,
        requestByteRange: { start: number; end?: number } | undefined,
    ) => Promise<Request | undefined | null>

    An optional function to customize the setup of HTTP requests for segment downloads.

    Type Declaration

      • (
            segmentUrl: string,
            segmentByteRange: ByteRange | undefined,
            requestAbortSignal: AbortSignal | undefined,
            requestByteRange: { start: number; end?: number } | undefined,
        ): Promise<Request | undefined | null>
      • Parameters

        • segmentUrl: string

          The URL of the segment.

        • segmentByteRange: ByteRange | undefined

          The range of bytes requested for the segment.

        • requestAbortSignal: AbortSignal | undefined

          An abort signal to cancel the request if needed (will be undefined if AbortController is not supported by the browser).

        • requestByteRange: { start: number; end?: number } | undefined

          An additional byte range for partial requests, if required.

        Returns Promise<Request | undefined | null>

        A promise that resolves to the configured request, or undefined if no customization is necessary.

    httpRequestSetup: undefined
    
    p2pMaxPeers: number

    The maximum number of active peer-to-peer connections for the stream. If this limit is reached, the client will stop accepting new incoming offers and cease requesting new peers from the tracker until the connection count drops.

    p2pMaxPeers: 50
    
    p2pChurnMaxPeersMultiplier: number

    The multiplier applied to p2pMaxPeers to determine the hard limit for accepting incoming connections. To keep the P2P swarm healthy and well-mixed, incoming connection offers are accepted up to this hard limit, and a background cleanup process will periodically prune the worst-performing peers if the total count exceeds p2pMaxPeers.

    p2pChurnMaxPeersMultiplier: 1.5
    
    p2pChurnCleanupIntervalMs: number

    The interval (in milliseconds) at which the background churning process evaluates and prunes the worst-performing peers if the total connection count exceeds p2pMaxPeers.

    p2pChurnCleanupIntervalMs: 30000
    
    p2pChurnGracePeriodMs: number

    The duration (in milliseconds) of the grace period granted to new peers. During this time, newly connected peers are protected from being dropped by the churning logic, allowing them time to establish connections and prove their bandwidth.

    p2pChurnGracePeriodMs: 15000
    
    webRtcOffersCount: number

    The number of WebRTC offers to generate and send to the tracker per announce request. This controls how aggressively the client attempts to discover new peers.

    webRtcOffersCount: 5
    
    webRtcOfferTimeoutMs: number

    The duration (in milliseconds) to keep a pending WebRTC offer alive while waiting for an answer from a remote peer.

    webRtcOfferTimeoutMs: 50000
    
    webRtcIceGatheringTimeoutMs: number

    The maximum duration (in milliseconds) to wait for ICE candidates to gather before sending an offer.

    webRtcIceGatheringTimeoutMs: 5000
    
    webRtcConnectionTimeoutMs: number

    The maximum duration (in milliseconds) to wait for the actual RTCDataChannel to open after signaling has completed.

    webRtcConnectionTimeoutMs: 15000