Skip to content

Commit

Permalink
refactor: segment index handling with eviction count
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaDemchenko committed Jan 17, 2025
1 parent 02af012 commit 7d42b14
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/p2p-media-loader-shaka/src/manifest-parser-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,15 @@ export class ManifestParserDecorator implements shaka.extern.ManifestParser {
segmentIndex.get = originalGet;
try {
const referencesCount = segmentIndex.getNumReferences();
const firstItemReference = segmentIndex.get(0);
const lastItemReference = segmentIndex.get(referencesCount - 1);
const evictedReferencesCount = segmentIndex.getNumEvicted();
const lastReferenceIndex =
referencesCount - 1 + evictedReferencesCount;

const firstItemReference = segmentIndex.get(evictedReferencesCount);
const lastItemReference = segmentIndex.get(lastReferenceIndex);

if (!firstItemReference || !lastItemReference) {
return reference;
throw new Error("Segment index is empty");
}

if (
Expand All @@ -159,7 +163,6 @@ export class ManifestParserDecorator implements shaka.extern.ManifestParser {
this.debug(`Stream ${stream.id} is updated`);
} catch {
// This catch is intentionally left blank.
// [...segmentIndex] throws an error when segmentIndex inner array is empty
} finally {
// do not set custom get again is segment index is already read and stream is VOD
if (
Expand Down
1 change: 1 addition & 0 deletions packages/p2p-media-loader-shaka/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ declare global {
interface SegmentIndex {
get(position: number): shaka.media.SegmentReference | null;
getNumReferences(): number;
getNumEvicted(): number;
}
}
}

0 comments on commit 7d42b14

Please sign in to comment.