From 0929a353b51f448359fa02360f610927725b2f3b Mon Sep 17 00:00:00 2001 From: Chuong Date: Wed, 9 Jun 2021 16:55:36 +0700 Subject: [PATCH 1/4] fix: pick variant track from streaming event allowed user to pick variant track from streaming event --- lib/player.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/player.js b/lib/player.js index e76730c689..653a2f33ff 100644 --- a/lib/player.js +++ b/lib/player.js @@ -1868,8 +1868,22 @@ shaka.Player = class extends shaka.util.FakeEventTarget { // 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 activeVariantTrack = null; + + for (const variantTrack of this.getVariantTracks()) { + if (variantTrack.active) { + activeVariantTrack = variantTrack; + break; + } + } + + if (! activeVariantTrack) { + this.switchVariant_(initialVariant, /* fromAdaptation= */ true, + /* clearBuffer= */ false, /* safeMargin= */ 0); + } // Decide if text should be shown automatically. const initialTextStream = this.chooseTextStream_(); From 8ffc6f1c7dc12136c63f005b2fca8dfdf237337b Mon Sep 17 00:00:00 2001 From: kocoten1992 Date: Thu, 10 Jun 2021 10:04:04 +0700 Subject: [PATCH 2/4] fix: condense logic + fix style --- lib/player.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/player.js b/lib/player.js index 653a2f33ff..dccfc221ef 100644 --- a/lib/player.js +++ b/lib/player.js @@ -1871,16 +1871,9 @@ shaka.Player = class extends shaka.util.FakeEventTarget { // However, we would skip switch to initial variant // if user already pick variant track (via selectVariantTrack api) - let activeVariantTrack = null; + const activeVariantTrack = this.getVariantTracks().find((t) => t.active); - for (const variantTrack of this.getVariantTracks()) { - if (variantTrack.active) { - activeVariantTrack = variantTrack; - break; - } - } - - if (! activeVariantTrack) { + if (!activeVariantTrack) { this.switchVariant_(initialVariant, /* fromAdaptation= */ true, /* clearBuffer= */ false, /* safeMargin= */ 0); } From 596e51913baa407d85ffc7fb1a2a5adefc02b101 Mon Sep 17 00:00:00 2001 From: kocoten1992 Date: Thu, 10 Jun 2021 14:28:12 +0700 Subject: [PATCH 3/4] fix: pick variant text track from streaming event --- lib/player.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/player.js b/lib/player.js index dccfc221ef..6c12931a61 100644 --- a/lib/player.js +++ b/lib/player.js @@ -1880,7 +1880,12 @@ shaka.Player = class extends shaka.util.FakeEventTarget { // Decide if text should be shown automatically. const initialTextStream = this.chooseTextStream_(); - if (initialTextStream) { + + // 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); + + if (!activeTextTrack && initialTextStream) { this.addTextStreamToSwitchHistory_( initialTextStream, /* fromAdaptation= */ true); } From 7f9eea03c8e3933a2c0a309f75cab158d59ba45d Mon Sep 17 00:00:00 2001 From: kocoten1992 Date: Fri, 11 Jun 2021 17:48:07 +0700 Subject: [PATCH 4/4] fix: complete handle select text track via streaming event --- lib/player.js | 60 ++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/lib/player.js b/lib/player.js index 6c12931a61..aeb6378505 100644 --- a/lib/player.js +++ b/lib/player.js @@ -1866,46 +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!'); - - // However, we would skip switch to initial variant + // 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); + } + + // Since the first streams just became active, send an adaptation event. + this.onAdaptation_(null, + shaka.util.StreamUtils.variantToTrack(initialVariant)); } // Decide if text should be shown automatically. - const initialTextStream = this.chooseTextStream_(); - - // Similar to video/audio track, we would skip switch initial text track + // 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); - if (!activeTextTrack && initialTextStream) { - this.addTextStreamToSwitchHistory_( - initialTextStream, /* fromAdaptation= */ true); - } + if (!activeTextTrack) { + const initialTextStream = this.chooseTextStream_(); - 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); - } + if (initialTextStream) { + this.addTextStreamToSwitchHistory_( + initialTextStream, /* fromAdaptation= */ true); + } - // 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 (initialVariant) { + this.setInitialTextState_(initialVariant, initialTextStream); + } - this.playhead_.setStartTime(adjustedTime); + // 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(); @@ -1920,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