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 #3191

Backported to v2.5.x

Change-Id: I4f2c6b3e2e67a6db1bfb71815a5ce80a3584e41e
  • Loading branch information
ismena authored and joeyparrish committed Mar 11, 2021
1 parent fb9fb49 commit 4e1df84
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
14 changes: 11 additions & 3 deletions lib/media/segment_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,18 @@ shaka.media.SegmentIndex.prototype.find = function(time) {
// 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) {
let r = this.references_[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 r.position;
}
}
Expand Down
25 changes: 19 additions & 6 deletions test/media/segment_index_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,26 @@ describe('SegmentIndex', /** @suppress {accessControls} */ function() {
expect(pos).toBeNull();
});

it('returns null if time is within a gap', function() {
let actual1 = makeReference(1, 10, 20, uri(10));
let actual2 = makeReference(2, 25, 30, uri(25));
let index = new shaka.media.SegmentIndex([actual1, actual2]);
it('works with two references if time == first end time', () => {
const actual1 = makeReference(1, 10, 20.12, uri(10));
const actual2 = makeReference(2, 20.13, 30, uri(20));
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);
});

let pos = index.find(23);
expect(pos).toBeNull();
it('works with time is between first endTime and second startTime', () => {
const actual1 = makeReference(1, 10, 20.12111, uri(10));
const actual2 = makeReference(2, 20.12113, 30, uri(20));
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);
});
});

Expand Down

0 comments on commit 4e1df84

Please sign in to comment.