Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix potential duplicate segments, AV sync issues #4884

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/media/segment_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,11 @@ shaka.media.SegmentIndex = class {
// Partial segments are used for live edge, and should be removed when they
// get older. Remove the old SegmentReferences after the first new
// reference's start time.
// Use times rounded to the millisecond for filtering purposes, so that
// tiny rounding errors will not result in duplicate segments in the index.
const firstStartTime = Math.round(references[0].startTime * 1000) / 1000;
this.references = this.references.filter((r) => {
return r.startTime < references[0].startTime;
return (Math.round(r.startTime * 1000) / 1000) < firstStartTime;
});

this.references.push(...references);
Expand Down
28 changes: 28 additions & 0 deletions test/media/segment_index_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,34 @@ describe('SegmentIndex', /** @suppress {accessControls} */ () => {
goog.asserts.assert(position1 != null, 'Position should not be null!');
expect(index1.get(position1)).toBe(references2[1]);
});

it('does not duplicate references with rounding errors', () => {
/** @type {!Array.<!shaka.media.SegmentReference>} */
const references1 = [
makeReference(uri(10), 10, 20),
makeReference(uri(20), 20, 30),
];
const index1 = new shaka.media.SegmentIndex(references1);

// 0.24 microseconds: an insignificant rounding error.
const tinyError = 0.24e-6;

/** @type {!Array.<!shaka.media.SegmentReference>} */
const references2 = [
makeReference(uri(10), 10, 20),
// The difference between this and the equivalent old reference is an
// insignificant rounding error.
makeReference(uri(20), 20 + tinyError, 30 + tinyError),
makeReference(uri(30), 30 + tinyError, 40),
];

index1.merge(references2);
expect(index1.references.length).toBe(3);
expect(index1.references[0]).toEqual(references1[0]);
// The new references replaced the old one.
expect(index1.references[1]).toEqual(references2[1]);
expect(index1.references[2]).toEqual(references2[2]);
});
});

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