Skip to content

Commit

Permalink
fix: Fix duplicate init segment download when using startAtSegmentBou…
Browse files Browse the repository at this point in the history
…ndary (#6479)

Fixes #5420
  • Loading branch information
avelad authored Apr 26, 2024
1 parent c6c39df commit da7863d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
46 changes: 32 additions & 14 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -2234,31 +2234,49 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
this.config_.playRangeStart,
this.config_.playRangeEnd);

this.playhead_ = this.createPlayhead(this.startTime_);
this.playheadObservers_ =
this.createPlayheadObserversForMSE_(startTimeOfLoad);

// We need to start the buffer management code near the end because it
// will set the initial buffering state and that depends on other
// components being initialized.
const rebufferThreshold = Math.max(
this.manifest_.minBufferTime, this.config_.streaming.rebufferingGoal);
this.startBufferManagement_(mediaElement, rebufferThreshold);
const setupPlayhead = (startTime) => {
this.playhead_ = this.createPlayhead(startTime);
this.playheadObservers_ =
this.createPlayheadObserversForMSE_(startTimeOfLoad);

// We need to start the buffer management code near the end because it
// will set the initial buffering state and that depends on other
// components being initialized.
const rebufferThreshold = Math.max(
this.manifest_.minBufferTime,
this.config_.streaming.rebufferingGoal);
this.startBufferManagement_(mediaElement, rebufferThreshold);
};

if (!this.config_.streaming.startAtSegmentBoundary) {
setupPlayhead(this.startTime_);
}

// Now we can switch to the initial variant.
if (!activeVariant) {
goog.asserts.assert(initialVariant,
'Must have choosen an initial variant!');

this.switchVariant_(initialVariant, /* fromAdaptation= */ true,
/* clearBuffer= */ false, /* safeMargin= */ 0);

// Now that we have initial streams, we may adjust the start time to
// align to a segment boundary.
if (this.config_.streaming.startAtSegmentBoundary) {
this.playhead_.setStartTime(await this.adjustStartTime_(
initialVariant, this.playhead_.getTime()));
const timeline = this.manifest_.presentationTimeline;
let initialTime = this.startTime_ || this.video_.currentTime;
const seekRangeStart = timeline.getSeekRangeStart();
const seekRangeEnd = timeline.getSeekRangeEnd();
if (initialTime < seekRangeStart) {
initialTime = seekRangeStart;
} else if (initialTime > seekRangeEnd) {
initialTime = seekRangeEnd;
}
const startTime = await this.adjustStartTime_(
initialVariant, initialTime);
setupPlayhead(startTime);
}

this.switchVariant_(initialVariant, /* fromAdaptation= */ true,
/* clearBuffer= */ false, /* safeMargin= */ 0);
}

this.playhead_.ready();
Expand Down
2 changes: 1 addition & 1 deletion test/player_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4477,7 +4477,7 @@ describe('Player', () => {

await player.load(fakeManifestUri, 12, fakeMimeType);

expect(playhead.setStartTime).toHaveBeenCalledWith(10);
expect(shaka.test.Util.spyFunc(playhead.getTime)()).toBe(10);
});

it('does not fail with no segments', async () => {
Expand Down

0 comments on commit da7863d

Please sign in to comment.