Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: dynamic liveSyncDurationCount based on fragment duration #450

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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