Skip to content

Commit

Permalink
Pass multiple bandwidth calculators of different types to hybrid load…
Browse files Browse the repository at this point in the history
…er and requests.
  • Loading branch information
i-zolotarenko committed Jan 4, 2024
1 parent 9d679e5 commit 15517a6
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 36 deletions.
10 changes: 7 additions & 3 deletions packages/p2p-media-loader-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Settings,
SegmentBase,
CoreEventHandlers,
BandwidthCalculators,
} from "./types";
import * as StreamUtils from "./utils/stream";
import { LinkedMap } from "./linked-map";
Expand All @@ -31,7 +32,10 @@ export class Core<TStream extends Stream = Stream> {
httpErrorRetries: 3,
p2pErrorRetries: 3,
};
private readonly bandwidthCalculator = new BandwidthCalculator();
private readonly bandwidthCalculators: BandwidthCalculators = {
all: new BandwidthCalculator(),
http: new BandwidthCalculator(),
};
private segmentStorage?: SegmentsMemoryStorage;
private mainStreamLoader?: HybridLoader;
private secondaryStreamLoader?: HybridLoader;
Expand Down Expand Up @@ -95,7 +99,7 @@ export class Core<TStream extends Stream = Stream> {
const segment = this.identifySegment(segmentLocalId);
const loader = this.getStreamHybridLoader(segment);
void loader.loadSegment(segment, callbacks);
console.log(this.isLive, this.activeLevelBitrate);
// console.log(this.isLive, this.activeLevelBitrate);
}

abortSegmentLoading(segmentLocalId: string): void {
Expand Down Expand Up @@ -155,7 +159,7 @@ export class Core<TStream extends Stream = Stream> {
manifestResponseUrl,
segment,
this.settings,
this.bandwidthCalculator,
this.bandwidthCalculators,
this.segmentStorage,
this.eventHandlers
);
Expand Down
26 changes: 15 additions & 11 deletions packages/p2p-media-loader-core/src/hybrid-loader.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Segment, StreamWithSegments } from "./index";
import { HttpRequestExecutor } from "./http-loader";
import { SegmentsMemoryStorage } from "./segments-storage";
import { Settings, CoreEventHandlers, Playback } from "./types";
import { BandwidthCalculator } from "./bandwidth-calculator";
import {
Settings,
CoreEventHandlers,
Playback,
BandwidthCalculators,
} from "./types";
import { P2PLoadersContainer } from "./p2p/loaders-container";
import { RequestsContainer } from "./requests/request-container";
import { EngineRequest, EngineCallbacks } from "./requests/engine-request";
Expand All @@ -16,22 +20,22 @@ const FAILED_ATTEMPTS_CLEAR_INTERVAL = 60000;

export class HybridLoader {
private readonly requests: RequestsContainer;
private readonly engineRequests = new Map<Segment, EngineRequest>();
private readonly p2pLoaders: P2PLoadersContainer;
private storageCleanUpIntervalId?: number;
private lastRequestedSegment: Readonly<Segment>;
private readonly playback: Playback;
private lastQueueProcessingTimeStamp?: number;
private readonly segmentAvgDuration: number;
private randomHttpDownloadInterval!: number;
private readonly logger: debug.Debugger;
private lastRequestedSegment: Readonly<Segment>;
private storageCleanUpIntervalId?: number;
private lastQueueProcessingTimeStamp?: number;
private randomHttpDownloadInterval!: number;
private isProcessQueueMicrotaskCreated = false;
private readonly engineRequests = new Map<Segment, EngineRequest>();

constructor(
private streamManifestUrl: string,
requestedSegment: Segment,
private readonly settings: Settings,
private readonly bandwidthCalculator: BandwidthCalculator,
private readonly bandwidthCalculators: BandwidthCalculators,
private readonly segmentStorage: SegmentsMemoryStorage,
private readonly eventHandlers?: Pick<CoreEventHandlers, "onSegmentLoaded">
) {
Expand All @@ -41,7 +45,7 @@ export class HybridLoader {
this.segmentAvgDuration = StreamUtils.getSegmentAvgDuration(activeStream);
this.requests = new RequestsContainer(
this.requestProcessQueueMicrotask,
this.bandwidthCalculator,
this.bandwidthCalculators,
this.playback,
this.settings
);
Expand Down Expand Up @@ -96,7 +100,7 @@ export class HybridLoader {
if (data) {
engineRequest.resolve(
data,
this.bandwidthCalculator.getBandwidthForLastNSplicedSeconds(3),
this.bandwidthCalculators.all.getBandwidthForLastNSplicedSeconds(3)
);
}
} else {
Expand Down Expand Up @@ -150,7 +154,7 @@ export class HybridLoader {
}
engineRequest?.resolve(
request.data,
this.bandwidthCalculator.getBandwidthForLastNSeconds(3)
this.bandwidthCalculators.all.getBandwidthForLastNSeconds(3)
);
this.engineRequests.delete(segment);
this.requests.remove(request);
Expand Down
10 changes: 4 additions & 6 deletions packages/p2p-media-loader-core/src/p2p/tracker-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@ export class P2PTrackerClient {
announce: [
// "wss://tracker.novage.com.ua",
"wss://tracker.openwebtorrent.com",
"wss://tracker.webtorrent.dev",
"wss://tracker.files.fm:7073/announce",
],
rtcConfig: {
iceServers: [
{
urls: [
"stun:stun.l.google.com:19302",
"stun:global.stun.twilio.com:3478",
],
},
{ urls: "stun:stun.l.google.com:19302" },
{ urls: "stun:global.stun.twilio.com:3478" },
],
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Segment, Settings, Playback } from "../types";
import { BandwidthCalculator } from "../bandwidth-calculator";
import { Segment, Settings, Playback, BandwidthCalculators } from "../types";
import { Request } from "./request";

export class RequestsContainer {
private readonly requests = new Map<Segment, Request>();

constructor(
private readonly requestProcessQueueCallback: () => void,
private readonly bandwidthCalculator: BandwidthCalculator,
private readonly bandwidthCalculators: BandwidthCalculators,
private readonly playback: Playback,
private readonly settings: Settings
) {}
Expand Down Expand Up @@ -38,7 +37,7 @@ export class RequestsContainer {
request = new Request(
segment,
this.requestProcessQueueCallback,
this.bandwidthCalculator,
this.bandwidthCalculators,
this.playback,
this.settings
);
Expand Down
34 changes: 22 additions & 12 deletions packages/p2p-media-loader-core/src/requests/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Segment, Playback } from "../types";
import { BandwidthCalculator } from "../bandwidth-calculator";
import { Segment, Playback, BandwidthCalculators } from "../types";
import * as StreamUtils from "../utils/stream";
import * as Utils from "../utils/utils";
import * as LoggerUtils from "../utils/logger";
Expand Down Expand Up @@ -67,7 +66,7 @@ export class Request {
constructor(
readonly segment: Segment,
private readonly requestProcessQueueCallback: () => void,
private readonly bandwidthCalculator: BandwidthCalculator,
private readonly bandwidthCalculators: BandwidthCalculators,
private readonly playback: Playback,
private readonly settings: StreamUtils.PlaybackTimeWindowsSettings
) {
Expand Down Expand Up @@ -160,7 +159,8 @@ export class Request {
loadedBytes: 0,
startTimestamp: performance.now(),
};
this.bandwidthCalculator.startLoading();
this.manageBandwidthCalculatorsState("start");

const { notReceivingBytesTimeoutMs, abort } = controls;
this._abortRequestCallback = abort;

Expand Down Expand Up @@ -194,9 +194,8 @@ export class Request {
);
this._abortRequestCallback?.(new RequestError("abort"));
this._abortRequestCallback = undefined;
this.currentAttempt = undefined;
this.manageBandwidthCalculatorsState("stop");
this.notReceivingBytesTimeout.clear();
this.bandwidthCalculator.stopLoading();
}

private abortOnTimeout = () => {
Expand All @@ -213,7 +212,7 @@ export class Request {
error,
});
this.notReceivingBytesTimeout.clear();
this.bandwidthCalculator.stopLoading();
this.manageBandwidthCalculatorsState("stop");
this.requestProcessQueueCallback();
};

Expand All @@ -228,15 +227,15 @@ export class Request {
error,
});
this.notReceivingBytesTimeout.clear();
this.bandwidthCalculator.stopLoading();
this.manageBandwidthCalculatorsState("stop");
this.requestProcessQueueCallback();
};

private completeOnSuccess = () => {
this.throwErrorIfNotLoadingStatus();
if (!this.currentAttempt) return;

this.bandwidthCalculator.stopLoading();
this.manageBandwidthCalculatorsState("stop");
this.notReceivingBytesTimeout.clear();
this.finalData = Utils.joinChunks(this.bytes);
this.setStatus("succeed");
Expand All @@ -253,11 +252,15 @@ export class Request {
if (!this.currentAttempt || !this.progress) return;
this.notReceivingBytesTimeout.restart();

this.bandwidthCalculator.addBytes(chunk.length);
const byteLength = chunk.byteLength;
const { all: allBC, http: httpBC } = this.bandwidthCalculators;
allBC.addBytes(byteLength);
if (this.currentAttempt.type === "http") httpBC.addBytes(byteLength);

this.bytes.push(chunk);
this.progress.lastLoadedChunkTimestamp = performance.now();
this.progress.loadedBytes += chunk.length;
this._loadedBytes += chunk.length;
this.progress.loadedBytes += byteLength;
this._loadedBytes += byteLength;
};

private firstBytesReceived = () => {
Expand All @@ -277,6 +280,13 @@ export class Request {
this._logger.color = "";
}

private manageBandwidthCalculatorsState(state: "start" | "stop") {
const { all, http } = this.bandwidthCalculators;
const method = state === "start" ? "startLoading" : "stopLoading";
if (this.currentAttempt?.type === "http") http[method]();
all[method]();
}

static getRequestItemId(segment: Segment) {
return segment.localId;
}
Expand Down
6 changes: 6 additions & 0 deletions packages/p2p-media-loader-core/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LinkedMap } from "./linked-map";
import { RequestAttempt } from "./requests/request";
import { BandwidthCalculator } from "./bandwidth-calculator";

export type StreamType = "main" | "secondary";

Expand Down Expand Up @@ -71,3 +72,8 @@ export type Playback = {
position: number;
rate: number;
};

export type BandwidthCalculators = Readonly<{
all: BandwidthCalculator;
http: BandwidthCalculator;
}>;

0 comments on commit 15517a6

Please sign in to comment.