Skip to content

Commit

Permalink
Substitute segment index get method immediately if it already exists.
Browse files Browse the repository at this point in the history
  • Loading branch information
i-zolotarenko committed Dec 24, 2023
1 parent 081557d commit 21f5a88
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
20 changes: 15 additions & 5 deletions packages/p2p-media-loader-shaka/src/manifest-parser-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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;
};
}
Expand Down
1 change: 0 additions & 1 deletion packages/p2p-media-loader-shaka/src/segment-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export class SegmentManager {
index,
shakaStream,
});

if (shakaStream.segmentIndex) this.updateStreamSegments(shakaStream);
}

Expand Down

0 comments on commit 21f5a88

Please sign in to comment.