From c7246250323c3c97a2d30f9f66880e914e5c2344 Mon Sep 17 00:00:00 2001 From: lindsayrr <31417164+lindsayrr@users.noreply.github.com> Date: Thu, 1 Dec 2022 13:16:19 -0800 Subject: [PATCH] feat: Support customizing clearBuffers and safeMargin when select variants by label (#4770) Currently, selectVariantsByLabel doesn't support customizing clearBuffers and safeMargin. This pr is to change the function signature, so that we'll able to decide if we want to keep some amount of buffer when we change track. --- AUTHORS | 1 + CONTRIBUTORS | 1 + lib/player.js | 20 ++++++++++++++++---- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/AUTHORS b/AUTHORS index 207912e783..9b701a38d1 100644 --- a/AUTHORS +++ b/AUTHORS @@ -88,6 +88,7 @@ ViacomCBS <*@viacomcbs.com> Vincent Valot V-Nova Limited <*@v-nova.com> Wayne Morgan +Wen Ren Raymond Cheng Blue Billywig <*@bluebillywig.com> João Nabais diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 1b272e8878..aa611ec22e 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -127,6 +127,7 @@ Vignesh Venkatasubramanian Vincent Valot Vinod Balakrishnan Wayne Morgan +Wen Ren Yohann Connell Raymond Cheng Janroel Koppen diff --git a/lib/player.js b/lib/player.js index a6626abb9a..4d6e20e5c3 100644 --- a/lib/player.js +++ b/lib/player.js @@ -4242,9 +4242,15 @@ shaka.Player = class extends shaka.util.FakeEventTarget { * are expected to have the same variant.audio. * * @param {string} label + * @param {boolean=} clearBuffer Optional clear buffer or not when + * switch to new variant + * Defaults to true if not provided + * @param {number=} safeMargin Optional amount of buffer (in seconds) to + * retain when clearing the buffer. + * Defaults to 0 if not provided. Ignored if clearBuffer is false. * @export */ - selectVariantsByLabel(label) { + selectVariantsByLabel(label, clearBuffer = true, safeMargin = 0) { if (this.manifest_ && this.playhead_) { let firstVariantWithLabel = null; for (const variant of this.manifest_.variants) { @@ -4268,7 +4274,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget { new shaka.media.PreferenceBasedCriteria( firstVariantWithLabel.language, '', 0, label); - this.chooseVariantAndSwitch_(); + this.chooseVariantAndSwitch_(clearBuffer, safeMargin); } } @@ -5713,9 +5719,15 @@ shaka.Player = class extends shaka.util.FakeEventTarget { * Called after a config change, a key status event, or an explicit language * change. * + * @param {boolean=} clearBuffer Optional clear buffer or not when + * switch to new variant + * Defaults to true if not provided + * @param {number=} safeMargin Optional amount of buffer (in seconds) to + * retain when clearing the buffer. + * Defaults to 0 if not provided. Ignored if clearBuffer is false. * @private */ - chooseVariantAndSwitch_() { + chooseVariantAndSwitch_(clearBuffer = true, safeMargin = 0) { goog.asserts.assert(this.config_, 'Must not be destroyed'); // Because we're running this after a config change (manual language @@ -5724,7 +5736,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget { const chosenVariant = this.chooseVariant_(); if (chosenVariant) { this.switchVariant_(chosenVariant, /* fromAdaptation= */ true, - /* clearBuffers= */ true, /* safeMargin= */ 0); + clearBuffer, safeMargin); } }