Skip to content

Commit

Permalink
fix(DASH): Evict empty indexes in MetaSegmentIndex (#7272)
Browse files Browse the repository at this point in the history
Issue #6239

---------

Co-authored-by: Álvaro Velad Galván <ladvan91@hotmail.com>
  • Loading branch information
theodab and avelad authored Sep 11, 2024
1 parent c541b1c commit c9998f9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/media/segment_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,17 @@ shaka.media.MetaSegmentIndex = class extends shaka.media.SegmentIndex {
this.indexes_ = [];
}

/** Evicts all old SegmentIndexes in this MetaSegmentIndex that are empty. */
evictEmpty() {
while (this.indexes_.length > 1 && this.indexes_[0].isEmpty()) {
const index = this.indexes_.shift();
// In the case of this class, this.numEvicted_ represents the evicted
// segments that were in indexes that were entirely evicted.
this.numEvicted_ += index.getNumEvicted();
index.release();
}
}

/**
* Append a SegmentIndex to this MetaSegmentIndex. This effectively stitches
* the underlying Stream onto the end of the multi-Period Stream represented
Expand All @@ -724,6 +735,7 @@ shaka.media.MetaSegmentIndex = class extends shaka.media.SegmentIndex {
// Be careful to clone the Array. We don't want to share the reference with
// our clone and affect each other accidentally.
clone.indexes_ = this.indexes_.slice();
clone.numEvicted_ = this.numEvicted_;
return clone;
}

Expand Down Expand Up @@ -754,7 +766,7 @@ shaka.media.MetaSegmentIndex = class extends shaka.media.SegmentIndex {
* @export
*/
find(time) {
let numPassedInEarlierIndexes = 0;
let numPassedInEarlierIndexes = this.numEvicted_;

for (const index of this.indexes_) {
const position = index.find(time);
Expand All @@ -775,7 +787,7 @@ shaka.media.MetaSegmentIndex = class extends shaka.media.SegmentIndex {
* @export
*/
get(position) {
let numPassedInEarlierIndexes = 0;
let numPassedInEarlierIndexes = this.numEvicted_;
let sawSegments = false;

for (const index of this.indexes_) {
Expand Down
6 changes: 6 additions & 0 deletions lib/media/streaming_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ goog.require('shaka.log');
goog.require('shaka.media.InitSegmentReference');
goog.require('shaka.media.ManifestParser');
goog.require('shaka.media.MediaSourceEngine');
goog.require('shaka.media.MetaSegmentIndex');
goog.require('shaka.media.SegmentIterator');
goog.require('shaka.media.SegmentReference');
goog.require('shaka.media.SegmentPrefetch');
Expand Down Expand Up @@ -2502,6 +2503,11 @@ shaka.media.StreamingEngine = class {
* @private
*/
async evict_(mediaState, presentationTime) {
const segmentIndex = mediaState.stream.segmentIndex;
if (segmentIndex instanceof shaka.media.MetaSegmentIndex) {
segmentIndex.evictEmpty();
}

const logPrefix = shaka.media.StreamingEngine.logPrefix_(mediaState);
shaka.log.v2(logPrefix, 'checking buffer length');

Expand Down
21 changes: 21 additions & 0 deletions test/media/segment_index_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,27 @@ describe('SegmentIndex', /** @suppress {accessControls} */ () => {
expect(Array.from(metaIndex)).toEqual(allRefs.slice(4));
});

it('evicts empty SegmentIndexes when calling evictEmpty', () => {
const release0 = spyOn(index0, 'release');
const release1 = spyOn(index1, 'release');
const release2 = spyOn(index2, 'release');
metaIndex.appendSegmentIndex(index0);
metaIndex.appendSegmentIndex(index1);
metaIndex.appendSegmentIndex(index2);
expect(Array.from(metaIndex)).toEqual(
inputRefs0.concat(inputRefs1, inputRefs2));
expect(metaIndex.getNumEvicted()).toBe(0);
index0.evict(75);
index1.evict(75);
index2.evict(75);
metaIndex.evictEmpty();
expect(Array.from(metaIndex)).toEqual(inputRefs2.slice(1));
expect(release0).toHaveBeenCalled();
expect(release1).toHaveBeenCalled();
expect(release2).not.toHaveBeenCalled();
expect(metaIndex.getNumEvicted()).toBe(6);
});

it('updates through updateEvery', async () => {
metaIndex.appendSegmentIndex(index0);
metaIndex.appendSegmentIndex(index1);
Expand Down

0 comments on commit c9998f9

Please sign in to comment.