From 5d5865b3fa451a3c8ef4e1747abf6efaada336f7 Mon Sep 17 00:00:00 2001 From: Igor Zolotarenko Date: Thu, 16 Nov 2023 15:03:28 +0200 Subject: [PATCH] P2P announce segments on http requests state change. --- .../src/event-dispatcher.ts | 3 ++- .../src/hybrid-loader.ts | 23 +++++++++++++++---- .../p2p-media-loader-core/src/p2p/loader.ts | 7 +++--- .../src/p2p/loaders-container.ts | 1 - packages/p2p-media-loader-core/src/request.ts | 17 ++------------ 5 files changed, 25 insertions(+), 26 deletions(-) diff --git a/packages/p2p-media-loader-core/src/event-dispatcher.ts b/packages/p2p-media-loader-core/src/event-dispatcher.ts index a6583633..7396e0e9 100644 --- a/packages/p2p-media-loader-core/src/event-dispatcher.ts +++ b/packages/p2p-media-loader-core/src/event-dispatcher.ts @@ -1,5 +1,6 @@ export class EventDispatcher< - T extends { [key: string]: (...args: any) => any }, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + T extends { [key: string]: (...args: any) => void | Promise }, K extends keyof T = keyof T > { private readonly listeners = new Map>(); diff --git a/packages/p2p-media-loader-core/src/hybrid-loader.ts b/packages/p2p-media-loader-core/src/hybrid-loader.ts index fd3f69ba..2318b8a4 100644 --- a/packages/p2p-media-loader-core/src/hybrid-loader.ts +++ b/packages/p2p-media-loader-core/src/hybrid-loader.ts @@ -22,7 +22,10 @@ export class HybridLoader { private lastQueueProcessingTimeStamp?: number; private readonly segmentAvgDuration: number; private randomHttpDownloadInterval!: number; - private readonly logger: { engine: debug.Debugger; loader: debug.Debugger }; + private readonly logger: { + engine: debug.Debugger; + loader: debug.Debugger; + }; private readonly levelBandwidth = { value: 0, refreshCount: 0 }; private isProcessQueueMicrotaskCreated = false; @@ -212,6 +215,7 @@ export class HybridLoader { request.subscribe("onSuccess", this.onRequestSuccess); request.subscribe("onError", this.onRequestError); + this.p2pLoaders.currentLoader.broadcastAnnouncement(); void fulfillHttpSegmentRequest(request, this.settings); if (!isRandom) { this.logger.loader( @@ -230,13 +234,22 @@ export class HybridLoader { } private onRequestSuccess = (request: Request, data: ArrayBuffer) => { - const { segment } = request; - this.logger.loader(`http responses: ${segment.externalId}`); - this.eventHandlers?.onSegmentLoaded?.(data.byteLength, "http"); + const requestType = request.type; + if (!requestType) return; + + if (requestType === "http") { + this.logger.loader(`http responses: ${request.segment.externalId}`); + this.p2pLoaders.currentLoader.broadcastAnnouncement(); + } + + this.eventHandlers?.onSegmentLoaded?.(data.byteLength, requestType); this.createProcessQueueMicrotask(); }; - private onRequestError = () => { + private onRequestError = (request: Request) => { + if (request.type === "http") { + this.p2pLoaders.currentLoader.broadcastAnnouncement(); + } this.createProcessQueueMicrotask(); }; diff --git a/packages/p2p-media-loader-core/src/p2p/loader.ts b/packages/p2p-media-loader-core/src/p2p/loader.ts index 0b774618..6b4d174a 100644 --- a/packages/p2p-media-loader-core/src/p2p/loader.ts +++ b/packages/p2p-media-loader-core/src/p2p/loader.ts @@ -49,7 +49,6 @@ export class P2PLoader { this.stream, this.broadcastAnnouncement ); - // this.requests.subscribeOnHttpRequestsUpdate(this.broadcastAnnouncement); this.trackerClient.start(); } @@ -103,6 +102,7 @@ export class P2PLoader { get connectedPeersAmount() { let count = 0; + // eslint-disable-next-line @typescript-eslint/no-unused-vars for (const peer of this.trackerClient.peers()) count++; return count; } @@ -127,7 +127,7 @@ export class P2PLoader { peer.sendSegmentsAnnouncement(announcement); } - private broadcastAnnouncement = () => { + broadcastAnnouncement() { if (this.isAnnounceMicrotaskCreated) return; this.isAnnounceMicrotaskCreated = true; @@ -138,7 +138,7 @@ export class P2PLoader { } this.isAnnounceMicrotaskCreated = false; }); - }; + } private async onSegmentRequested(peer: Peer, segmentExternalId: string) { const segment = StreamUtils.getSegmentFromStreamByExternalId( @@ -159,7 +159,6 @@ export class P2PLoader { this.stream, this.broadcastAnnouncement ); - // this.requests.unsubscribeFromHttpRequestsUpdate(this.broadcastAnnouncement); this.trackerClient.destroy(); } } diff --git a/packages/p2p-media-loader-core/src/p2p/loaders-container.ts b/packages/p2p-media-loader-core/src/p2p/loaders-container.ts index 8a8d5640..623fb00f 100644 --- a/packages/p2p-media-loader-core/src/p2p/loaders-container.ts +++ b/packages/p2p-media-loader-core/src/p2p/loaders-container.ts @@ -73,7 +73,6 @@ export class P2PLoadersContainer { } private setLoaderDestroyTimeout(item: P2PLoaderContainerItem) { - // TODO: use Timeout class instead item.destroyTimeoutId = window.setTimeout( () => this.destroyAndRemoveLoader(item), this.settings.p2pLoaderDestroyTimeoutMs diff --git a/packages/p2p-media-loader-core/src/request.ts b/packages/p2p-media-loader-core/src/request.ts index 35baae2e..09783c31 100644 --- a/packages/p2p-media-loader-core/src/request.ts +++ b/packages/p2p-media-loader-core/src/request.ts @@ -183,7 +183,6 @@ export class Request extends EventDispatcher { const data = Utils.joinChunks(this.chunks); this._status = "succeed"; this.prevAttempts.push(this.currentAttempt); - this.currentAttempt = undefined; this._engineCallbacks?.onSuccess({ data, @@ -225,7 +224,6 @@ export class Request extends EventDispatcher { } this.currentAttempt.error = error; this.prevAttempts.push(this.currentAttempt); - this.currentAttempt = undefined; this.dispatch("onError", this, error); } @@ -283,20 +281,9 @@ export class RequestError< static isRequestInnerErrorType( error: RequestError ): error is RequestError { + // eslint-disable-next-line @typescript-eslint/no-explicit-any return requestInnerErrorTypes.includes(error.type as any); } - - static isPeerErrorType( - error: RequestError - ): error is RequestError { - return peerRequestErrorTypes.includes(error.type as any); - } - - static isHttpErrorType( - error: RequestError - ): error is RequestError { - return peerRequestErrorTypes.includes(error.type as any); - } } export class CoreRequestError extends Error { @@ -305,7 +292,7 @@ export class CoreRequestError extends Error { } } -class Timeout { +export class Timeout { private timeoutId?: number; constructor(private readonly action: () => void) {}