Skip to content

Commit

Permalink
Refactor: dynamic liveSyncDurationCount based on fragment duration in…
Browse files Browse the repository at this point in the history
… hls.js package
  • Loading branch information
DimaDemchenko committed Nov 29, 2024
1 parent 922276d commit e1a79e9
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions packages/p2p-media-loader-hlsjs/src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export type HlsWithP2PConfig<HlsType extends abstract new () => unknown> =
};
};

const MAX_LIVE_SYNC_DURATION = 120;

/**
* 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
Expand Down Expand Up @@ -307,25 +309,45 @@ export class HlsJsP2PEngine {
) => {
if (
this.currentHlsInstance &&
this.currentHlsInstance.config.liveSyncDurationCount !==
data.details.fragments.length - 1 &&
data.details.live &&
data.details.fragments[0].type === ("main" as PlaylistLevelType) &&
!this.currentHlsInstance.userConfig.liveSyncDuration &&
!this.currentHlsInstance.userConfig.liveSyncDurationCount &&
data.details.fragments.length > 4
) {
this.debug(
`set liveSyncDurationCount ${data.details.fragments.length - 1}`,
);
this.currentHlsInstance.config.liveSyncDurationCount =
data.details.fragments.length - 1;
this.updateLiveSyncDurationCount(data);
}

this.core.setIsLive(data.details.live);
this.segmentManager.updatePlaylist(data);
};

private updateLiveSyncDurationCount(
data: LevelUpdatedData | AudioTrackLoadedData,
) {
const fragmentDuration = data.details.targetduration;

const maxLiveSyncCount = Math.floor(
MAX_LIVE_SYNC_DURATION / fragmentDuration,
);
const newLiveSyncDurationCount = Math.min(
data.details.fragments.length - 1,
maxLiveSyncCount,
);

if (
this.currentHlsInstance &&
this.currentHlsInstance.config.liveSyncDurationCount !==
newLiveSyncDurationCount
) {
this.debug(
`Setting liveSyncDurationCount to ${newLiveSyncDurationCount}`,
);
this.currentHlsInstance.config.liveSyncDurationCount =
newLiveSyncDurationCount;
}
}

private handleMediaAttached = () => {
this.updateMediaElementEventHandlers("register");
};
Expand Down

0 comments on commit e1a79e9

Please sign in to comment.