Skip to content

Commit

Permalink
Change peer announcement type and corresponding logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
i-zolotarenko committed Sep 25, 2023
1 parent 0c8af4f commit 1938c55
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/p2p-media-loader-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class Core<TStream extends Stream = Stream> {
private readonly streams = new Map<string, StreamWithSegments<TStream>>();
private readonly settings: Settings = {
simultaneousHttpDownloads: 3,
simultaneousP2PDownloads: 3,
highDemandTimeWindow: 25,
httpDownloadTimeWindow: 60,
p2pDownloadTimeWindow: 60,
Expand Down
56 changes: 49 additions & 7 deletions packages/p2p-media-loader-core/src/hybrid-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,40 @@ export class HybridLoader {
queueSegmentIds.has(segmentId)
);

const { simultaneousHttpDownloads } = this.settings;
const { simultaneousHttpDownloads, simultaneousP2PDownloads } =
this.settings;
for (const { segment, statuses } of queue) {
if (this.requests.isHttpRequested(segment.localId)) continue;
// const timeToPlayback = getTimeToSegmentPlayback(segment, this.playback);
if (statuses.isHighDemand) {
if (this.requests.countHttpRequests() < simultaneousHttpDownloads) {
void this.loadSegmentThroughHttp(segment);
if (this.requests.isHttpRequested(segment.localId)) continue;
// const request = this.requests.get(segment.localId);
// if (request?.loaderRequest?.type === "p2p") {
// const remainingDownloadTime = getPredictedRemainingDownloadTime(
// request.loaderRequest
// );
// if (
// remainingDownloadTime === undefined ||
// remainingDownloadTime > timeToPlayback
// ) {
// request.loaderRequest.abort();
// } else {
// continue;
// }
// }
if (this.requests.httpRequestsCount < simultaneousHttpDownloads) {
void this.loadThroughHttp(segment);
continue;
}

this.abortLastHttpLoadingAfter(queue, segment.localId);
if (this.requests.countHttpRequests() < simultaneousHttpDownloads) {
void this.loadSegmentThroughHttp(segment);
if (this.requests.httpRequestsCount < simultaneousHttpDownloads) {
void this.loadThroughHttp(segment);
continue;
}
}
if (statuses.isP2PDownloadable) {
if (this.requests.p2pRequestsCount < simultaneousP2PDownloads) {
void this.loadThroughP2P(segment);
}
}
break;
Expand All @@ -122,7 +145,7 @@ export class HybridLoader {
this.requests.abortEngineRequest(segmentId);
}

private async loadSegmentThroughHttp(segment: Segment) {
private async loadThroughHttp(segment: Segment) {
let data: ArrayBuffer | undefined;
try {
const httpRequest = getHttpSegmentRequest(segment);
Expand Down Expand Up @@ -225,6 +248,8 @@ class P2PLoadersContainer {
else this.createLoader(stream);
}

// TODO: add stale loaders destroying

get activeLoader() {
return this._activeLoader;
}
Expand All @@ -235,3 +260,20 @@ class P2PLoadersContainer {
}
}
}

// function getTimeToSegmentPlayback(segment: Segment, playback: Playback) {
// return Math.max(segment.startTime - playback.position, 0) / playback.rate;
// }
//
// function getPredictedRemainingDownloadTime(request: HybridLoaderRequest) {
// const { startTimestamp, progress } = request;
// if (!progress || progress.percent === 0) return undefined;
// const now = performance.now();
// const bandwidth =
// progress.percent / (progress.lastLoadedChunkTimestamp - startTimestamp);
// const remainingDownloadPercent = 100 - progress.percent;
// const predictedRemainingTimeFromLastDownload =
// remainingDownloadPercent / bandwidth;
// const timeFromLastDownload = now - progress.lastLoadedChunkTimestamp;
// return predictedRemainingTimeFromLastDownload - timeFromLastDownload;
// }
1 change: 1 addition & 0 deletions packages/p2p-media-loader-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type Settings = {
httpDownloadTimeWindow: number;
p2pDownloadTimeWindow: number;
simultaneousHttpDownloads: number;
simultaneousP2PDownloads: number;
cachedSegmentExpiration: number;
cachedSegmentsCount: number;
webRtcMaxMessageSize: number;
Expand Down

0 comments on commit 1938c55

Please sign in to comment.