Skip to content

Commit

Permalink
fix: Fix choosing tracks from streaming event (shaka-project#3459)
Browse files Browse the repository at this point in the history
In v2.5, we could choose a track explicitly from the "streaming" event.  In v3, this was broken.  Now the player will not choose any tracks automatically if explicit tracks were chosen by the app during the "streaming" event.

Closes shaka-project#3448
  • Loading branch information
kocoten1992 authored Jul 10, 2021
1 parent 2c26321 commit 760161f
Showing 1 changed file with 43 additions and 25 deletions.
68 changes: 43 additions & 25 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1866,34 +1866,56 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
this.dispatchEvent(this.makeEvent_(shaka.Player.EventName.Streaming));

// Pick the initial streams to play.
const initialVariant = this.chooseVariant_();
goog.asserts.assert(initialVariant, 'Must choose an initial variant!');
this.switchVariant_(initialVariant, /* fromAdaptation= */ true,
/* clearBuffer= */ false, /* safeMargin= */ 0);
// however, we would skip switch to initial variant
// if user already pick variant track (via selectVariantTrack api)
let initialVariant = null;
const activeVariantTrack = this.getVariantTracks().find((t) => t.active);

if (!activeVariantTrack) {
initialVariant = this.chooseVariant_();
goog.asserts.assert(initialVariant, 'Must choose 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) {
const startTime = this.playhead_.getTime();
const adjustedTime =
await this.adjustStartTime_(initialVariant, startTime);

this.playhead_.setStartTime(adjustedTime);
}

// Decide if text should be shown automatically.
const initialTextStream = this.chooseTextStream_();
if (initialTextStream) {
this.addTextStreamToSwitchHistory_(
initialTextStream, /* fromAdaptation= */ true);
// Since the first streams just became active, send an adaptation event.
this.onAdaptation_(null,
shaka.util.StreamUtils.variantToTrack(initialVariant));
}

this.setInitialTextState_(initialVariant, initialTextStream);
// Don't initialize with a text stream unless we should be streaming text.
if (initialTextStream && this.shouldStreamText_()) {
this.streamingEngine_.switchTextStream(initialTextStream);
}
// Decide if text should be shown automatically.
// similar to video/audio track, we would skip switch initial text track
// if user already pick text track (via selectTextTrack api)
const activeTextTrack = this.getTextTracks().find((t) => t.active);

// Now that we have initial streams, we may adjust the start time to align
// to a segment boundary.
if (this.config_.streaming.startAtSegmentBoundary) {
const startTime = this.playhead_.getTime();
const adjustedTime =
await this.adjustStartTime_(initialVariant, startTime);
if (!activeTextTrack) {
const initialTextStream = this.chooseTextStream_();

this.playhead_.setStartTime(adjustedTime);
if (initialTextStream) {
this.addTextStreamToSwitchHistory_(
initialTextStream, /* fromAdaptation= */ true);
}

if (initialVariant) {
this.setInitialTextState_(initialVariant, initialTextStream);
}

// Don't initialize with a text stream unless we should be streaming text.
if (initialTextStream && this.shouldStreamText_()) {
this.streamingEngine_.switchTextStream(initialTextStream);
}
}


// Start streaming content. This will start the flow of content down to
// media source.
await this.streamingEngine_.start();
Expand All @@ -1908,10 +1930,6 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
// Dispatch a 'trackschanged' event now that all initial filtering is done.
this.onTracksChanged_();

// Since the first streams just became active, send an adaptation event.
this.onAdaptation_(null,
shaka.util.StreamUtils.variantToTrack(initialVariant));

// Now that we've filtered out variants that aren't compatible with the
// active one, update abr manager with filtered variants.
// NOTE: This may be unnecessary. We've already chosen one codec in
Expand Down

0 comments on commit 760161f

Please sign in to comment.