From 2b59ee264017502f4900b45a6ac707740787ccf1 Mon Sep 17 00:00:00 2001 From: Igor Zolotarenko Date: Mon, 2 Oct 2023 13:16:43 +0300 Subject: [PATCH] Fix issue with not clearing aborted engine request. --- p2p-media-loader-demo/src/App.tsx | 1 + packages/p2p-media-loader-core/src/core.ts | 7 +---- .../src/hybrid-loader.ts | 29 +++++++++---------- packages/p2p-media-loader-core/src/request.ts | 19 +++++++++++- .../src/fragment-loader.ts | 5 +--- 5 files changed, 34 insertions(+), 27 deletions(-) diff --git a/p2p-media-loader-demo/src/App.tsx b/p2p-media-loader-demo/src/App.tsx index da462e5d..a8cce14a 100644 --- a/p2p-media-loader-demo/src/App.tsx +++ b/p2p-media-loader-demo/src/App.tsx @@ -159,6 +159,7 @@ function App() { }, }, }); + player.play(); setPlayerToWindow(player); }; diff --git a/packages/p2p-media-loader-core/src/core.ts b/packages/p2p-media-loader-core/src/core.ts index ee7dd013..5f010994 100644 --- a/packages/p2p-media-loader-core/src/core.ts +++ b/packages/p2p-media-loader-core/src/core.ts @@ -84,17 +84,12 @@ export class Core { await this.segmentStorage.initialize(); } const segment = this.identifySegment(segmentLocalId); - console.log( - "segment: ", - `${segment.stream.index}-${segment.externalId}`, - segment.startTime, - segment.endTime - ); const loader = this.getStreamHybridLoader(segment); void loader.loadSegment(segment, callbacks); } abortSegmentLoading(segmentId: string): void { + const segment = this.identifySegment(segmentId); this.mainStreamLoader?.abortSegment(segmentId); this.secondaryStreamLoader?.abortSegment(segmentId); } diff --git a/packages/p2p-media-loader-core/src/hybrid-loader.ts b/packages/p2p-media-loader-core/src/hybrid-loader.ts index d22d117b..8cbfa0d6 100644 --- a/packages/p2p-media-loader-core/src/hybrid-loader.ts +++ b/packages/p2p-media-loader-core/src/hybrid-loader.ts @@ -1,4 +1,4 @@ -import { Segment, StreamWithSegments } from "./index"; +import { RequestAbortError, Segment, StreamWithSegments } from "./index"; import { getHttpSegmentRequest } from "./http-loader"; import { P2PLoader } from "./p2p-loader"; import { SegmentsMemoryStorage } from "./segments-storage"; @@ -95,6 +95,18 @@ export class HybridLoader { const { simultaneousHttpDownloads, simultaneousP2PDownloads } = this.settings; + + for (const request of this.requests.engineRequests()) { + const { segment, loaderRequest } = request; + if ( + !queueSegmentIds.has(segment.localId) && + !loaderRequest && + segment.startTime < this.lastRequestedSegment.startTime + ) { + request.engineCallbacks.onError(new RequestAbortError()); + } + } + for (const { segment, statuses } of queue) { // const timeToPlayback = getTimeToSegmentPlayback(segment, this.playback); if (statuses.isHighDemand) { @@ -141,17 +153,6 @@ export class HybridLoader { // } break; } - - console.log( - [...this.requests.values()].map((req) => { - const { loaderRequest, engineCallbacks, segment } = req; - const { stream } = segment; - - return `${stream.index}-${segment.externalId}-l${ - loaderRequest ? 1 : 0 - }-e${engineCallbacks ? 1 : 0}`; - }) - ); } // api method for engines @@ -225,10 +226,6 @@ export class HybridLoader { if (isPositionChanged) this.playback.position = position; if (isRateChanged) this.playback.rate = rate; - - if (isPositionSignificantlyChanged) { - console.log("\nposition: ", position); - } void this.processQueue(isPositionSignificantlyChanged); } diff --git a/packages/p2p-media-loader-core/src/request.ts b/packages/p2p-media-loader-core/src/request.ts index 5f2f4b1a..4e0dc3a5 100644 --- a/packages/p2p-media-loader-core/src/request.ts +++ b/packages/p2p-media-loader-core/src/request.ts @@ -3,6 +3,8 @@ import { RequestAbortError } from "./errors"; import { Subscriptions } from "./segments-storage"; import Debug from "debug"; +type SetRequired = Omit & Required>; + export type EngineCallbacks = { onSuccess: (response: SegmentResponse) => void; onError: (reason?: unknown) => void; @@ -38,6 +40,8 @@ type Request = { engineCallbacks?: Readonly; }; +type RequestWithEngineCallbacks = SetRequired; + function getRequestItemId(segment: Segment) { return segment.localId; } @@ -91,12 +95,19 @@ export class RequestContainer { const segmentId = getRequestItemId(segment); const requestItem = this.requests.get(segmentId); - const { onSuccess } = engineCallbacks; + const { onSuccess, onError } = engineCallbacks; engineCallbacks.onSuccess = (response) => { this.clearRequestItem(segmentId, "engine"); return onSuccess(response); }; + engineCallbacks.onError = (error) => { + if (error instanceof RequestAbortError) { + this.clearRequestItem(segmentId, "engine"); + } + return onError(error); + }; + if (requestItem) { requestItem.engineCallbacks = engineCallbacks; } else { @@ -124,6 +135,12 @@ export class RequestContainer { } } + *engineRequests(): Generator { + for (const request of this.requests.values()) { + if (request.engineCallbacks) yield request as RequestWithEngineCallbacks; + } + } + resolveEngineRequest(segment: Segment, response: SegmentResponse) { const id = getRequestItemId(segment); this.requests.get(id)?.engineCallbacks?.onSuccess(response); diff --git a/packages/p2p-media-loader-hlsjs/src/fragment-loader.ts b/packages/p2p-media-loader-hlsjs/src/fragment-loader.ts index 97f9b241..8ed41e65 100644 --- a/packages/p2p-media-loader-hlsjs/src/fragment-loader.ts +++ b/packages/p2p-media-loader-hlsjs/src/fragment-loader.ts @@ -89,10 +89,7 @@ export class FragmentLoaderBase implements Loader { }; const onError = (error: unknown) => { - if (error instanceof RequestAbortError) { - if (this.stats.aborted) return; - this.callbacks?.onAbort?.(this.stats, this.context, {}); - } + if (error instanceof RequestAbortError && this.stats.aborted) return; this.handleError(error); };