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 17 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
6 changes: 3 additions & 3 deletions demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function App() {
if (!videoRef.current || !hlsEngine.current) return;
const engine = hlsEngine.current;
const hls = new window.Hls({
...engine.getConfig(),
...engine.getHlsConfig(),
});

engine.setHls(hls);
Expand All @@ -178,7 +178,7 @@ function App() {
type: "customHls",
customType: {
customHls: (video: HTMLVideoElement) => {
const hls = new window.Hls(engine.getConfig());
const hls = new window.Hls(engine.getHlsConfig());
engine.setHls(hls);
hls.loadSource(url);
hls.attachMedia(video);
Expand All @@ -202,7 +202,7 @@ function App() {
source: url,
playback: {
hlsjsConfig: {
...engine.getConfig(),
...engine.getHlsConfig(),
},
},
plugins: [window.LevelSelector],
Expand Down
23 changes: 18 additions & 5 deletions packages/p2p-media-loader-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ import {
Stream,
StreamWithSegments,
Segment,
Settings,
CoreConfig,
SegmentBase,
BandwidthCalculators,
StreamDetails,
CoreEventMap,
DynamicConfig,
} from "./types";
import * as StreamUtils from "./utils/stream";
import { BandwidthCalculator } from "./bandwidth-calculator";
import { EngineCallbacks } from "./requests/engine-request";
import { SegmentsMemoryStorage } from "./segments-storage";
import { EventEmitter } from "./utils/event-emitter";
import { deepConfigMerge } from "./utils/utils";

export class Core<TStream extends Stream = Stream> {
private readonly eventEmitter = new EventEmitter<CoreEventMap>();
private manifestResponseUrl?: string;
private readonly streams = new Map<string, StreamWithSegments<TStream>>();
private readonly settings: Settings = {
private config: CoreConfig = {
simultaneousHttpDownloads: 3,
simultaneousP2PDownloads: 3,
highDemandTimeWindow: 15,
Expand All @@ -46,7 +48,18 @@ export class Core<TStream extends Stream = Stream> {
activeLevelBitrate: 0,
};

constructor() {}
constructor(config?: CoreConfig) {
this.applyConfig(config);
}

private applyConfig(config?: CoreConfig) {
if (!config) return;
this.config = deepConfigMerge(this.config, config);
}

applyDynamicConfig(dynamicConfig: DynamicConfig) {
this.config = deepConfigMerge(this.config, dynamicConfig);
}

addEventListener<K extends keyof CoreEventMap>(
eventName: K,
Expand Down Expand Up @@ -110,7 +123,7 @@ export class Core<TStream extends Stream = Stream> {
if (!this.segmentStorage) {
this.segmentStorage = new SegmentsMemoryStorage(
this.manifestResponseUrl,
this.settings,
this.config,
);
await this.segmentStorage.initialize();
}
Expand Down Expand Up @@ -183,7 +196,7 @@ export class Core<TStream extends Stream = Stream> {
manifestResponseUrl,
segment,
this.streamDetails,
this.settings,
this.config,
this.bandwidthCalculators,
this.segmentStorage,
this.eventEmitter,
Expand Down
12 changes: 6 additions & 6 deletions packages/p2p-media-loader-core/src/http-loader.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CoreEventMap, Settings } from "./types";
import { CoreEventMap, CoreConfig } from "./types";
import { Request as SegmentRequest, RequestControls } from "./requests/request";
import { RequestError, HttpRequestErrorType } from "./types";
import { EventEmitter } from "./utils/event-emitter";

type HttpSettings = Pick<
Settings,
type HttpConfig = Pick<
CoreConfig,
"httpNotReceivingBytesTimeoutMs" | "httpRequestSetup"
>;

Expand All @@ -17,7 +17,7 @@ export class HttpRequestExecutor {

constructor(
private readonly request: SegmentRequest,
private readonly settings: HttpSettings,
private readonly httpConfig: HttpConfig,
eventEmitter: EventEmitter<CoreEventMap>,
) {
this.onChunkDownloaded =
Expand All @@ -36,7 +36,7 @@ export class HttpRequestExecutor {
this.request.totalBytes - this.request.loadedBytes;
}

const { httpNotReceivingBytesTimeoutMs } = this.settings;
const { httpNotReceivingBytesTimeoutMs } = this.httpConfig;
this.requestControls = this.request.start(
{ downloadSource: "http" },
{
Expand All @@ -50,7 +50,7 @@ export class HttpRequestExecutor {
private async fetch() {
const { segment } = this.request;
try {
let request = await this.settings.httpRequestSetup?.(
let request = await this.httpConfig.httpRequestSetup?.(
segment.url,
segment.byteRange,
this.abortController.signal,
Expand Down
24 changes: 12 additions & 12 deletions packages/p2p-media-loader-core/src/hybrid-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Segment, StreamWithSegments } from "./index";
import { HttpRequestExecutor } from "./http-loader";
import { SegmentsMemoryStorage } from "./segments-storage";
import {
Settings,
CoreConfig,
Playback,
BandwidthCalculators,
StreamDetails,
Expand Down Expand Up @@ -38,7 +38,7 @@ export class HybridLoader {
private streamManifestUrl: string,
private lastRequestedSegment: Readonly<Segment>,
private readonly streamDetails: Required<Readonly<StreamDetails>>,
private readonly settings: Settings,
private readonly config: CoreConfig,
private readonly bandwidthCalculators: BandwidthCalculators,
private readonly segmentStorage: SegmentsMemoryStorage,
private readonly eventEmitter: EventEmitter<CoreEventMap>,
Expand All @@ -50,7 +50,7 @@ export class HybridLoader {
this.requestProcessQueueMicrotask,
this.bandwidthCalculators,
this.playback,
this.settings,
this.config,
this.eventEmitter,
);

Expand All @@ -62,15 +62,15 @@ export class HybridLoader {
return StreamUtils.isSegmentActualInPlayback(
segment,
this.playback,
this.settings,
this.config,
);
});
this.p2pLoaders = new P2PLoadersContainer(
this.streamManifestUrl,
this.lastRequestedSegment.stream,
this.requests,
this.segmentStorage,
this.settings,
this.config,
this.eventEmitter,
);

Expand Down Expand Up @@ -139,7 +139,7 @@ export class HybridLoader {
queueDownloadRatio: number,
) {
const { stream } = this.lastRequestedSegment;
const { httpErrorRetries } = this.settings;
const { httpErrorRetries } = this.config;
const now = performance.now();
for (const request of this.requests.items()) {
const {
Expand Down Expand Up @@ -221,7 +221,7 @@ export class HybridLoader {
simultaneousHttpDownloads,
simultaneousP2PDownloads,
httpErrorRetries,
} = this.settings;
} = this.config;

if (
this.engineRequest?.shouldBeStartedImmediately &&
Expand All @@ -236,7 +236,7 @@ export class HybridLoader {
request.status === "not-started" ||
(request.status === "failed" &&
request.failedAttempts.httpAttemptsCount <
this.settings.httpErrorRetries)
this.config.httpErrorRetries)
) {
void this.loadThroughHttp(segment);
}
Expand Down Expand Up @@ -325,7 +325,7 @@ export class HybridLoader {

private loadThroughHttp(segment: Segment) {
const request = this.requests.getOrCreateRequest(segment);
new HttpRequestExecutor(request, this.settings, this.eventEmitter);
new HttpRequestExecutor(request, this.config, this.eventEmitter);
this.p2pLoaders.currentLoader.broadcastAnnouncement();
}

Expand All @@ -334,7 +334,7 @@ export class HybridLoader {
}

private loadRandomThroughHttp() {
const { simultaneousHttpDownloads, httpErrorRetries } = this.settings;
const { simultaneousHttpDownloads, httpErrorRetries } = this.config;
const p2pLoader = this.p2pLoaders.currentLoader;

if (
Expand All @@ -348,7 +348,7 @@ export class HybridLoader {
for (const { segment, statuses } of QueueUtils.generateQueue(
this.lastRequestedSegment,
this.playback,
this.settings,
this.config,
)) {
if (
!statuses.isHttpDownloadable ||
Expand Down Expand Up @@ -417,7 +417,7 @@ export class HybridLoader {
for (const item of QueueUtils.generateQueue(
this.lastRequestedSegment,
this.playback,
this.settings,
this.config,
)) {
maxPossibleLength++;
const { segment } = item;
Expand Down
11 changes: 8 additions & 3 deletions packages/p2p-media-loader-core/src/p2p/loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Peer } from "./peer";
import { CoreEventMap, Segment, Settings, StreamWithSegments } from "../types";
import {
CoreEventMap,
Segment,
CoreConfig,
StreamWithSegments,
} from "../types";
import { SegmentsMemoryStorage } from "../segments-storage";
import { RequestsContainer } from "../requests/request-container";
import { Request } from "../requests/request";
Expand All @@ -17,7 +22,7 @@ export class P2PLoader {
private readonly stream: StreamWithSegments,
private readonly requests: RequestsContainer,
private readonly segmentStorage: SegmentsMemoryStorage,
private readonly settings: Settings,
private readonly config: CoreConfig,
eventEmmiter: EventEmitter<CoreEventMap>,
) {
const streamExternalId = StreamUtils.getStreamExternalId(
Expand All @@ -33,7 +38,7 @@ export class P2PLoader {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
onSegmentRequested: this.onSegmentRequested,
},
this.settings,
this.config,
eventEmmiter,
);

Expand Down
8 changes: 4 additions & 4 deletions packages/p2p-media-loader-core/src/p2p/loaders-container.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { P2PLoader } from "./loader";
import debug from "debug";
import { CoreEventMap, Settings, Stream, StreamWithSegments } from "../index";
import { CoreEventMap, CoreConfig, Stream, StreamWithSegments } from "../index";
import { RequestsContainer } from "../requests/request-container";
import { SegmentsMemoryStorage } from "../segments-storage";
import * as LoggerUtils from "../utils/logger";
Expand All @@ -23,7 +23,7 @@ export class P2PLoadersContainer {
stream: StreamWithSegments,
private readonly requests: RequestsContainer,
private readonly segmentStorage: SegmentsMemoryStorage,
private readonly settings: Settings,
private readonly config: CoreConfig,
private readonly eventEmmiter: EventEmitter<CoreEventMap>,
) {
this.changeCurrentLoader(stream);
Expand All @@ -38,7 +38,7 @@ export class P2PLoadersContainer {
stream,
this.requests,
this.segmentStorage,
this.settings,
this.config,
this.eventEmmiter,
);
const loggerInfo = LoggerUtils.getStreamString(stream);
Expand Down Expand Up @@ -76,7 +76,7 @@ export class P2PLoadersContainer {
private setLoaderDestroyTimeout(item: P2PLoaderContainerItem) {
item.destroyTimeoutId = window.setTimeout(
() => this.destroyAndRemoveLoader(item),
this.settings.p2pLoaderDestroyTimeoutMs,
this.config.p2pLoaderDestroyTimeoutMs,
);
}

Expand Down
12 changes: 6 additions & 6 deletions packages/p2p-media-loader-core/src/p2p/peer-protocol.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { PeerConnection } from "bittorrent-tracker";
import { CoreEventMap, Settings } from "../types";
import { CoreEventMap, CoreConfig } from "../types";
import * as Utils from "../utils/utils";
import * as Command from "./commands";
import { EventEmitter } from "../utils/event-emitter";

export type PeerSettings = Pick<
Settings,
export type PeerConfig = Pick<
CoreConfig,
| "p2pNotReceivingBytesTimeoutMs"
| "webRtcMaxMessageSize"
| "p2pErrorRetries"
Expand All @@ -20,7 +20,7 @@ export class PeerProtocol {

constructor(
private readonly connection: PeerConnection,
private readonly settings: PeerSettings,
private readonly peerConfig: PeerConfig,
private readonly eventHandlers: {
onCommandReceived: (command: Command.PeerCommand) => void;
onSegmentChunkReceived: (data: Uint8Array) => void;
Expand All @@ -46,7 +46,7 @@ export class PeerProtocol {
sendCommand(command: Command.PeerCommand) {
const binaryCommandBuffers = Command.serializePeerCommand(
command,
this.settings.webRtcMaxMessageSize,
this.peerConfig.webRtcMaxMessageSize,
);
for (const buffer of binaryCommandBuffers) {
this.connection.send(buffer);
Expand All @@ -62,7 +62,7 @@ export class PeerProtocol {
if (this.uploadingContext) {
throw new Error(`Some segment data is already uploading.`);
}
const chunks = getBufferChunks(data, this.settings.webRtcMaxMessageSize);
const chunks = getBufferChunks(data, this.peerConfig.webRtcMaxMessageSize);
const channel = this.connection._channel;
const { promise, resolve, reject } = Utils.getControlledPromise<void>();

Expand Down
Loading