Represents a P2P (peer-to-peer) engine for HLS (HTTP Live Streaming) to enhance media streaming efficiency. This class integrates P2P technologies into HLS.js, enabling the distribution of media segments via a peer network alongside traditional HTTP fetching. It reduces server bandwidth costs and improves scalability by sharing the load across multiple clients.

The engine manages core functionalities such as segment fetching, segment management, peer connection management, and event handling related to the P2P and HLS processes.

// Creating an instance of HlsJsP2PEngine with custom configuration
const hlsP2PEngine = new HlsJsP2PEngine({
core: {
highDemandTimeWindow: 30, // 30 seconds
simultaneousHttpDownloads: 3,
webRtcMaxMessageSize: 64 * 1024, // 64 KB
p2pNotReceivingBytesTimeoutMs: 10000, // 10 seconds
p2pInactiveLoaderDestroyTimeoutMs: 15000, // 15 seconds
httpNotReceivingBytesTimeoutMs: 8000, // 8 seconds
httpErrorRetries: 2,
p2pErrorRetries: 2,
announceTrackers: ["wss://personal.tracker.com"],
rtcConfig: {
iceServers: [{ urls: "stun:personal.stun.com" }]
},
swarmId: "example-swarm-id"
}
});

Constructors

Methods

  • Enhances a given Hls.js class by injecting additional P2P (peer-to-peer) functionalities.

    Parameters

    • hls: typeof Hls

    Returns (new (config?: Partial<HlsConfig> & {
        p2p?: Partial<Omit<HlsJsP2PEngineConfig, "core">> & {
            core?: Partial<CoreConfig>;
        } & {
            onHlsJsCreated?: ((hls: HlsWithP2PInstance<typeof Hls>) => void);
        };
    }) => HlsWithP2PInstance<Hls>)

    • The enhanced Hls.js class with P2P functionalities.
    const HlsWithP2P = HlsJsP2PEngine.injectMixin(Hls);

    const hls = new HlsWithP2P({
    // Hls.js configuration
    startLevel: 0, // Example of Hls.js config parameter
    p2p: {
    core: {
    // P2P core configuration
    },
    onHlsJsCreated(hls) {
    // Do something with the Hls.js instance
    },
    },
    });
  • Adds an event listener for the specified event.

    Type Parameters

    Parameters

    • eventName: K

      The name of the event to listen for.

    • listener: CoreEventMap[K]

      The callback function to be invoked when the event is triggered.

    Returns void

    // Listening for a segment being successfully loaded
    p2pEngine.addEventListener('onSegmentLoaded', (details) => {
    console.log('Segment Loaded:', details);
    });
    // Handling segment load errors
    p2pEngine.addEventListener('onSegmentError', (errorDetails) => {
    console.error('Error loading segment:', errorDetails);
    });
    // Tracking data downloaded from peers
    p2pEngine.addEventListener('onChunkDownloaded', (bytesLength, downloadSource, peerId) => {
    console.log(`Downloaded ${bytesLength} bytes from ${downloadSource} ${peerId ? 'from peer ' + peerId : 'from server'}`);
    });
  • Removes an event listener for the specified event.

    Type Parameters

    Parameters

    • eventName: K

      The name of the event.

    • listener: CoreEventMap[K]

      The callback function that was previously added.

    Returns void

  • provides the Hls.js P2P specific configuration for Hls.js loaders.

    Returns {
        fLoader: unknown;
        pLoader: unknown;
    }

    An object with fragment loader (fLoader) and playlist loader (pLoader).

    • fLoader: unknown
    • pLoader: unknown
  • Applies dynamic configuration updates to the P2P engine.

    Parameters

    Returns void

    // Assuming `hlsP2PEngine` is an instance of HlsJsP2PEngine

    const newDynamicConfig = {
    core: {
    // Increase the number of cached segments to 1000
    cachedSegmentsCount: 1000,
    // 50 minutes of segments will be downloaded further through HTTP connections if P2P fails
    httpDownloadTimeWindow: 3000,
    // 100 minutes of segments will be downloaded further through P2P connections
    p2pDownloadTimeWindow: 6000,
    };

    hlsP2PEngine.applyDynamicConfig(newDynamicConfig);
  • Sets the HLS instance for handling media.

    Type Parameters

    • T = unknown

    Parameters

    • hls: T | (() => T)

      The HLS instance or a function that returns an HLS instance.

    Returns void

  • Clean up and release all resources. Unregister all event handlers.

    Returns void