Skip to content

Commit

Permalink
fix: fix reference searching when search time = segment.endTime
Browse files Browse the repository at this point in the history
Fixes shaka-project#3191

Change-Id: I4f2c6b3e2e67a6db1bfb71815a5ce80a3584e41e
  • Loading branch information
ismena committed Mar 10, 2021
1 parent 4161ec1 commit 01ce0f4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
12 changes: 10 additions & 2 deletions lib/media/segment_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,18 @@ shaka.media.SegmentIndex = class {
// For live streams, searching from the end is faster. For VOD, it balances
// out either way. In both cases, references.length is small enough that
// the difference isn't huge.
for (let i = this.references.length - 1; i >= 0; --i) {
const lastReferenceIndex = this.references.length - 1;
for (let i = lastReferenceIndex; i >= 0; --i) {
const r = this.references[i];
const start = r.startTime;
// A rounding error can cause /time/ to equal e.endTime or fall in between
// the references by a fraction of a second. To account for this, we use
// the start of the next segment as /end/, unless this is the last
// reference, in which case we use its end time as /end/.
const end = i < lastReferenceIndex ?
this.references[i + 1].startTime : r.endTime;
// Note that a segment ends immediately before the end time.
if ((time >= r.startTime) && (time < r.endTime)) {
if ((time >= start) && (time < end)) {
return i + this.numEvicted;
}
}
Expand Down
31 changes: 22 additions & 9 deletions test/media/segment_index_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ describe('SegmentIndex', /** @suppress {accessControls} */ () => {
expect(ref).toBe(actual2);
});

it('works with two references if time == first end time', () => {
const actual1 = makeReference(uri(10), 10, 20.12);
const actual2 = makeReference(uri(20), 20.13, 30);
const index = new shaka.media.SegmentIndex([actual1, actual2]);

const pos = index.find(20.12);
goog.asserts.assert(pos != null, 'Null position!');
const ref = index.get(pos);
expect(ref).toBe(actual1);
});

it('works with time is between first endTime and second startTime', () => {
const actual1 = makeReference(uri(10), 10, 20.12111);
const actual2 = makeReference(uri(20), 20.12113, 30);
const index = new shaka.media.SegmentIndex([actual1, actual2]);

const pos = index.find(20.12112);
goog.asserts.assert(pos != null, 'Null position!');
const ref = index.get(pos);
expect(ref).toBe(actual1);
});

it('returns the first segment if time < first start time', () => {
const actual = makeReference(uri(10), 10, 20);
const index = new shaka.media.SegmentIndex([actual]);
Expand All @@ -85,15 +107,6 @@ describe('SegmentIndex', /** @suppress {accessControls} */ () => {
const pos = index.find(21);
expect(pos).toBeNull();
});

it('returns null if time is within a gap', () => {
const actual1 = makeReference(uri(10), 10, 20);
const actual2 = makeReference(uri(25), 25, 30);
const index = new shaka.media.SegmentIndex([actual1, actual2]);

const pos = index.find(23);
expect(pos).toBeNull();
});
});

describe('get', () => {
Expand Down

0 comments on commit 01ce0f4

Please sign in to comment.