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

Feat: Config #347

Merged
merged 19 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion packages/p2p-media-loader-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
BandwidthCalculators,
StreamDetails,
CoreEventMap,
Config,
} from "./types";
import * as StreamUtils from "./utils/stream";
import { BandwidthCalculator } from "./bandwidth-calculator";
Expand Down Expand Up @@ -46,7 +47,9 @@ export class Core<TStream extends Stream = Stream> {
activeLevelBitrate: 0,
};

constructor() {}
constructor(config: Config) {
Object.assign(this.settings, config.coreSettings);
}

addEventListener<K extends keyof CoreEventMap>(
eventName: K,
Expand Down
2 changes: 2 additions & 0 deletions packages/p2p-media-loader-core/src/p2p/tracker-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ export class P2PTrackerClient {
"wss://tracker.webtorrent.dev",
"wss://tracker.files.fm:7073/announce",
"wss://tracker.openwebtorrent.com",
...(this.settings.announceTrackers ?? []),
DimaDemchenko marked this conversation as resolved.
Show resolved Hide resolved
],
rtcConfig: {
iceServers: [
{ urls: "stun:stun.l.google.com:19302" },
{ urls: "stun:global.stun.twilio.com:3478" },
],
...this.settings.rtcConfig,
},
});
this.client.on("peer", this.onReceivePeerConnection);
Expand Down
19 changes: 19 additions & 0 deletions packages/p2p-media-loader-core/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ export type SegmentResponse = {
bandwidth: number;
};

export type Config = {
coreSettings: Partial<
DimaDemchenko marked this conversation as resolved.
Show resolved Hide resolved
Pick<
DimaDemchenko marked this conversation as resolved.
Show resolved Hide resolved
Settings,
| "webRtcMaxMessageSize"
| "p2pNotReceivingBytesTimeoutMs"
| "httpNotReceivingBytesTimeoutMs"
| "httpErrorRetries"
| "p2pErrorRetries"
| "validateP2PSegment"
| "httpRequestSetup"
| "rtcConfig"
| "announceTrackers"
>
>;
};

export type Settings = {
highDemandTimeWindow: number;
httpDownloadTimeWindow: number;
Expand All @@ -53,6 +70,8 @@ export type Settings = {
httpNotReceivingBytesTimeoutMs: number;
httpErrorRetries: number;
p2pErrorRetries: number;
announceTrackers?: string[];
rtcConfig?: RTCConfiguration;
validateP2PSegment?: (url: string, byteRange?: ByteRange) => Promise<boolean>;
httpRequestSetup?: (
segmentUrl: string,
Expand Down
9 changes: 6 additions & 3 deletions packages/p2p-media-loader-hlsjs/src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ import type { HlsConfig, Events } from "hls.js";
import { FragmentLoaderBase } from "./fragment-loader";
import { PlaylistLoaderBase } from "./playlist-loader";
import { SegmentManager } from "./segment-mananger";
import { Core, CoreEventMap } from "p2p-media-loader-core";
import { Config, Core, CoreEventMap } from "p2p-media-loader-core";

export type HlsEngineConfig = {
hlsjsConfig: Partial<HlsConfig>;
} & Config;
export class Engine {
private readonly core: Core;
private readonly segmentManager: SegmentManager;
private hlsInstanceGetter?: () => Hls;
private currentHlsInstance?: Hls;

constructor() {
this.core = new Core();
constructor(config: HlsEngineConfig) {
this.core = new Core(config);
this.segmentManager = new SegmentManager(this.core);
}

DimaDemchenko marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
39 changes: 33 additions & 6 deletions packages/p2p-media-loader-shaka/src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,24 @@ import {
P2PMLShakaData,
} from "./types";
import { Loader } from "./loading-handler";
import { Core, CoreEventMap } from "p2p-media-loader-core";
import { Config, Core, CoreEventMap } from "p2p-media-loader-core";

const LIVE_EDGE_DELAY = 25;

export type ShakaEngineConfig = {
ShakaConfig: {
Streaming: {
useNativeHlsOnSafari: boolean;
};
Manifest: {
dash: {
ignoreSuggestedPresentationDelay: boolean;
};
defaultPresentationDelay: number;
};
};
} & Config;

export class Engine {
private player?: shaka.Player;
private readonly shaka: Shaka;
Expand All @@ -25,22 +39,35 @@ export class Engine {
private readonly segmentManager: SegmentManager;
private requestFilter?: shaka.extern.RequestFilter;

constructor(shaka?: unknown) {
constructor(
shaka: unknown,
private readonly config: ShakaEngineConfig,
) {
this.shaka = (shaka as Shaka | undefined) ?? window.shaka;
this.core = new Core();
this.core = new Core(this.config);
this.segmentManager = new SegmentManager(this.streamInfo, this.core);
}

configureAndInitShakaPlayer(player: shaka.Player) {
if (this.player === player) return;
if (this.player) this.destroy();

const shakaConfig = this.config.ShakaConfig;

this.player = player;
this.player.configure("manifest.defaultPresentationDelay", LIVE_EDGE_DELAY);
this.player.configure(
"manifest.defaultPresentationDelay",
shakaConfig.Manifest.defaultPresentationDelay ?? LIVE_EDGE_DELAY,
);
this.player.configure(
"manifest.dash.ignoreSuggestedPresentationDelay",
true,
shakaConfig.Manifest.dash.ignoreSuggestedPresentationDelay ?? true,
);
this.player.configure("streaming.useNativeHlsOnSafari", false);
this.player.configure(
"streaming.useNativeHlsOnSafari",
shakaConfig.Streaming.useNativeHlsOnSafari ?? true,
);

this.updatePlayerEventHandlers("register");
}

Expand Down
Loading