From 21f5a88093bcb06ecf41ae9cd2903160d116008a Mon Sep 17 00:00:00 2001 From: Igor Zolotarenko Date: Sun, 24 Dec 2023 18:52:50 +0200 Subject: [PATCH] Substitute segment index get method immediately if it already exists. --- .../src/manifest-parser-decorator.ts | 20 ++++++++++++++----- .../src/segment-manager.ts | 1 - 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/p2p-media-loader-shaka/src/manifest-parser-decorator.ts b/packages/p2p-media-loader-shaka/src/manifest-parser-decorator.ts index d31e1a70..09d08c40 100644 --- a/packages/p2p-media-loader-shaka/src/manifest-parser-decorator.ts +++ b/packages/p2p-media-loader-shaka/src/manifest-parser-decorator.ts @@ -99,14 +99,12 @@ export class ManifestParserDecorator implements shaka.extern.ManifestParser { const { segmentManager } = this; if (!segmentManager) return; - const createSegmentIndexOriginal = stream.createSegmentIndex; - stream.createSegmentIndex = async () => { - const result = await createSegmentIndexOriginal.call(stream); - const { segmentIndex } = stream; + const substituteSegmentIndexGet = ( + segmentIndex: shaka.media.SegmentIndex + ) => { let prevReference: shaka.media.SegmentReference | null = null; let prevFirstItemReference: shaka.media.SegmentReference; let prevLastItemReference: shaka.media.SegmentReference; - if (!segmentIndex) return result; const originalGet = segmentIndex.get; const customGet = (position: number) => { @@ -142,6 +140,18 @@ export class ManifestParserDecorator implements shaka.extern.ManifestParser { }; segmentIndex.get = customGet; + }; + + if (stream.segmentIndex) { + substituteSegmentIndexGet(stream.segmentIndex); + return; + } + + const createSegmentIndexOriginal = stream.createSegmentIndex; + stream.createSegmentIndex = async () => { + const result = await createSegmentIndexOriginal.call(stream); + if (!stream.segmentIndex) return result; + substituteSegmentIndexGet(stream.segmentIndex); return result; }; } diff --git a/packages/p2p-media-loader-shaka/src/segment-manager.ts b/packages/p2p-media-loader-shaka/src/segment-manager.ts index 686b1bc2..08f52be8 100644 --- a/packages/p2p-media-loader-shaka/src/segment-manager.ts +++ b/packages/p2p-media-loader-shaka/src/segment-manager.ts @@ -26,7 +26,6 @@ export class SegmentManager { index, shakaStream, }); - if (shakaStream.segmentIndex) this.updateStreamSegments(shakaStream); }