From 5bb8c1cab09131033d813bb764dd276978fdc9e2 Mon Sep 17 00:00:00 2001 From: Michelle Zhuo Date: Fri, 29 Jan 2021 22:35:11 -0800 Subject: [PATCH] feat(MediaCap): make StreamUtils.filterManifest async Player.filterManifest() does two things: 1. Filter the manifest with Drm, codecs and currentVariant's compatibility in StreamUtil. 2. Filter the manifest with PlayerConfiguration.restrictions. In applyConfig_(), only the restrictions config affects filtering the manifests. We don't need to call StreamUtils.filterManifest(). 2. Make the filterManifest function async. Issue #1391 Change-Id: I2fba9cabb3f3a3e89f5d990c4eeb19cc74924de9 --- lib/player.js | 37 +++++++++++++++++++++++++++++-------- lib/util/stream_utils.js | 5 ++++- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/lib/player.js b/lib/player.js index 696c46bcb8..4865e95543 100644 --- a/lib/player.js +++ b/lib/player.js @@ -1745,7 +1745,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget { // Now that we have drm information, filter the manifest (again) so that we // can ensure we only use variants with the selected key system. - this.filterManifest_(this.manifest_); + await this.filterManifest_(this.manifest_); } /** @@ -1908,7 +1908,8 @@ shaka.Player = class extends shaka.util.FakeEventTarget { } // Re-filter the manifest after streams have been chosen. - this.filterManifest_(this.manifest_); + await this.filterManifest_(this.manifest_); + // Dispatch a 'trackschanged' event now that all initial filtering is done. this.onTracksChanged_(); // Since the first streams just became active, send an adaptation event. @@ -2727,8 +2728,8 @@ shaka.Player = class extends shaka.util.FakeEventTarget { // Need to apply the restrictions. try { - // this.filterManifest_() may throw. - this.filterManifest_(this.manifest_); + // this.filterManifestWithRestrictions_() may throw. + this.filterManifestWithRestrictions_(this.manifest_); } catch (error) { this.onError_(error); } @@ -4493,19 +4494,29 @@ shaka.Player = class extends shaka.util.FakeEventTarget { * @param {?shaka.extern.Manifest} manifest * @private */ - filterManifest_(manifest) { + async filterManifest_(manifest) { + await this.filterManifestWithStreamUtils_(manifest); + this.filterManifestWithRestrictions_(manifest); + } + + /** + * Filters a manifest, removing unplayable streams/variants. + * + * @param {?shaka.extern.Manifest} manifest + * @private + */ + async filterManifestWithStreamUtils_(manifest) { goog.asserts.assert(manifest, 'Manifest should exist!'); goog.asserts.assert(this.video_, 'Must not be destroyed'); - const StreamUtils = shaka.util.StreamUtils; /** @type {?shaka.extern.Variant} */ const currentVariant = this.streamingEngine_ ? this.streamingEngine_.getCurrentVariant() : null; - StreamUtils.filterManifest( + await shaka.util.StreamUtils.filterManifest( this.drmEngine_, currentVariant, manifest); - const valid = manifest.variants.some(StreamUtils.isPlayable); + const valid = manifest.variants.some(shaka.util.StreamUtils.isPlayable); // If none of the variants are playable, throw // CONTENT_UNSUPPORTED_BY_BROWSER. @@ -4515,7 +4526,17 @@ shaka.Player = class extends shaka.util.FakeEventTarget { shaka.util.Error.Category.MANIFEST, shaka.util.Error.Code.CONTENT_UNSUPPORTED_BY_BROWSER); } + } + + /** + * Apply the restrictions configuration to the manifest, and check if there's + * a variant that meets the restrictions. + * + * @param {?shaka.extern.Manifest} manifest + * @private + */ + filterManifestWithRestrictions_(manifest) { const tracksChanged = shaka.util.StreamUtils.applyRestrictions( manifest.variants, this.config_.restrictions, this.maxHwRes_); if (tracksChanged && this.streamingEngine_) { diff --git a/lib/util/stream_utils.js b/lib/util/stream_utils.js index e1e2b78cc0..e60aa937a2 100644 --- a/lib/util/stream_utils.js +++ b/lib/util/stream_utils.js @@ -194,7 +194,7 @@ shaka.util.StreamUtils = class { * @param {?shaka.extern.Variant} currentVariant * @param {shaka.extern.Manifest} manifest */ - static filterManifest(drmEngine, currentVariant, manifest) { + static async filterManifest(drmEngine, currentVariant, manifest) { const StreamUtils = shaka.util.StreamUtils; // Filter variants. @@ -260,6 +260,9 @@ shaka.util.StreamUtils = class { return keep; }); + + // TODO: remove this after adopting MediaCapabilities. + await Promise.resolve(); }