Represents a P2P (peer-to-peer) engine for HLS (HTTP Live Streaming) to enhance media streaming efficiency. This class integrates P2P technologies into Shaka Player, 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.

// Initializing the ShakaP2PEngine with custom configuration
const shakaP2PEngine = new ShakaP2PEngine({
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

  • Configures and initializes the Shaka Player instance with predefined settings for optimal P2P performance.

    Parameters

    • player: Player

      The Shaka Player instance to configure.

    Returns void

  • Applies dynamic configuration updates to the P2P engine.

    Parameters

    Returns void

    // Assuming `shakaP2PEngine` is an instance of ShakaP2PEngine

    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,
    };

    shakaP2PEngine.applyDynamicConfig(newDynamicConfig);
  • 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
    shakaP2PEngine.addEventListener('onSegmentLoaded', (details) => {
    console.log('Segment Loaded:', details);
    });
    // Handling segment load errors
    shakaP2PEngine.addEventListener('onSegmentError', (errorDetails) => {
    console.error('Error loading segment:', errorDetails);
    });
    // Tracking data downloaded from peers
    shakaP2PEngine.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

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

    Returns void

  • Registers plugins related to P2P functionality into the Shaka Player. Plugins must be registered before initializing the player to ensure proper integration.

    Parameters

    • shaka: typeof shaka = window.shaka

      The Shaka Player library. Defaults to the global Shaka Player instance if not provided.

    Returns void

  • Unregister plugins related to P2P functionality from the Shaka Player.

    Parameters

    • shaka: typeof shaka = window.shaka

      The Shaka Player library. Defaults to the global Shaka Player instance if not provided.

    Returns void