Skip to content

Commit

Permalink
Hls.js live eadge delay autodetection (#334)
Browse files Browse the repository at this point in the history
* userConfig

* added type check

* Fix types

---------

Co-authored-by: Andriy Lysnevych <[email protected]>
  • Loading branch information
DimaDemchenko and mrlika authored Feb 21, 2024
1 parent 38a0339 commit ba1a16f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 2 additions & 0 deletions demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const players = [
type Player = (typeof players)[number];

const streamUrls = {
radioStream:
"https://streamvideo.luxnet.ua/maximum/smil:maximum.stream.smil/playlist.m3u8",
hlsBigBunnyBuck: "https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8",
hlsByteRangeVideo:
"https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_16x9/bipbop_16x9_variant.m3u8",
Expand Down
21 changes: 14 additions & 7 deletions packages/p2p-media-loader-hlsjs/src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import type {
LevelUpdatedData,
ManifestLoadedData,
LevelSwitchingData,
PlaylistLevelType,
} from "hls.js";
import type { HlsConfig, Events } from "hls.js";
import { FragmentLoaderBase } from "./fragment-loader";
import { PlaylistLoaderBase } from "./playlist-loader";
import { SegmentManager } from "./segment-mananger";
import { Core, CoreEventHandlers } from "p2p-media-loader-core";

const LIVE_EDGE_DELAY = 25;

export class Engine {
private readonly core: Core;
private readonly segmentManager: SegmentManager;
Expand All @@ -24,14 +23,10 @@ export class Engine {
this.segmentManager = new SegmentManager(this.core);
}

public getConfig(): Pick<
HlsConfig,
"fLoader" | "pLoader" | "liveSyncDuration"
> {
public getConfig(): Partial<HlsConfig> {
return {
fLoader: this.createFragmentLoaderClass(),
pLoader: this.createPlaylistLoaderClass(),
liveSyncDuration: LIVE_EDGE_DELAY,
};
}

Expand Down Expand Up @@ -118,6 +113,18 @@ export class Engine {
event: string,
data: LevelUpdatedData | AudioTrackLoadedData,
) => {
if (
this.currentHlsInstance &&
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.currentHlsInstance.config.liveSyncDurationCount =
data.details.fragments.length - 1;
}

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

0 comments on commit ba1a16f

Please sign in to comment.