From 6c012aa4678b6f4be0b3c04e9c16b99543886b53 Mon Sep 17 00:00:00 2001 From: Joey Parrish Date: Thu, 7 Jan 2021 11:01:40 -0800 Subject: [PATCH] feat(hls): Add config to prefer native HLS playback Closes #3077 Change-Id: Ib49270c17656e50cd68806836d7b71ee8e0bc148 --- demo/common/message_ids.js | 1 + demo/config.js | 4 +++- demo/locales/en.json | 1 + demo/locales/source.json | 4 ++++ externs/shaka/player.js | 5 ++++- lib/player.js | 18 +++++++++++++++--- lib/util/player_configuration.js | 1 + 7 files changed, 29 insertions(+), 5 deletions(-) diff --git a/demo/common/message_ids.js b/demo/common/message_ids.js index e3788c5321..435346c250 100644 --- a/demo/common/message_ids.js +++ b/demo/common/message_ids.js @@ -199,6 +199,7 @@ shakaDemo.MessageIds = { NUMBER_NONZERO_INTEGER_WARNING: 'DEMO_NUMBER_NONZERO_INTEGER_WARNING', OFFLINE_SECTION_HEADER: 'DEMO_OFFLINE_SECTION_HEADER', PREFER_FORCED_SUBS: 'DEMO_PREFER_FORCED_SUBS', + PREFER_NATIVE_HLS: 'DEMO_PREFER_NATIVE_HLS', REBUFFERING_GOAL: 'DEMO_REBUFFERING_GOAL', RESTRICTIONS_SECTION_HEADER: 'DEMO_RESTRICTIONS_SECTION_HEADER', SAFE_SEEK_OFFSET: 'DEMO_SAFE_SEEK_OFFSET', diff --git a/demo/config.js b/demo/config.js index aba4cbdf7e..5016a05ea0 100644 --- a/demo/config.js +++ b/demo/config.js @@ -323,7 +323,9 @@ shakaDemo.Config = class { .addBoolInput_(MessageIds.AUTO_LOW_LATENCY, 'streaming.autoLowLatencyMode') .addBoolInput_(MessageIds.FORCE_HTTPS, - 'streaming.forceHTTPS'); + 'streaming.forceHTTPS') + .addBoolInput_(MessageIds.PREFER_NATIVE_HLS, + 'streaming.preferNativeHls'); if (!shakaDemoMain.getNativeControlsEnabled()) { this.addBoolInput_(MessageIds.ALWAYS_STREAM_TEXT, diff --git a/demo/locales/en.json b/demo/locales/en.json index fe28080570..dc7c3f1aed 100644 --- a/demo/locales/en.json +++ b/demo/locales/en.json @@ -144,6 +144,7 @@ "DEMO_PLAY": "Play", "DEMO_PLAYREADY": "PlayReady DRM", "DEMO_PREFER_FORCED_SUBS": "Prefer Forced Subs", + "DEMO_PREFER_NATIVE_HLS": "Prefer native HLS playback when available", "DEMO_PROJECT_LINKS_HEADER": "PROJECT LINKS", "DEMO_PROMPT_NO": "No", "DEMO_PROMPT_YES": "Yes", diff --git a/demo/locales/source.json b/demo/locales/source.json index 712ff04b0d..983b87556b 100644 --- a/demo/locales/source.json +++ b/demo/locales/source.json @@ -579,6 +579,10 @@ "description": "The name of a configuration value.", "message": "Prefer Forced Subs" }, + "DEMO_PREFER_NATIVE_HLS": { + "description": "The name of a configuration value.", + "message": "Prefer native HLS playback when available" + }, "DEMO_PROJECT_LINKS_HEADER": { "description": "A header for a section of the footer, that contains links to various project-related things.", "message": "PROJECT LINKS" diff --git a/externs/shaka/player.js b/externs/shaka/player.js index 6f4dc77743..d8cc9c248f 100644 --- a/externs/shaka/player.js +++ b/externs/shaka/player.js @@ -730,7 +730,8 @@ shaka.extern.ManifestConfiguration; * inaccurateManifestTolerance: number, * lowLatencyMode: boolean, * autoLowLatencyMode: boolean, - * forceHTTPS: boolean + * forceHTTPS: boolean, + * preferNativeHls: boolean * }} * * @description @@ -831,6 +832,8 @@ shaka.extern.ManifestConfiguration; * activate the lowLatencyMode. Defaults to false. * @property {boolean} forceHTTPS * If true, if the protocol is HTTP change it to HTTPs. + * @property {boolean} preferNativeHls + * If true, prefer native HLS playback when possible, regardless of platform. * * @exportDoc */ diff --git a/lib/player.js b/lib/player.js index 5dea6f872f..1fdc95a7bc 100644 --- a/lib/player.js +++ b/lib/player.js @@ -1175,9 +1175,21 @@ shaka.Player = class extends shaka.util.FakeEventTarget { // We would prefer MediaSource in some cases, and src= in others. For // example, Android has native HLS, but we'd prefer our own MediaSource - // version there. For Safari, the choice is governed by the - // useNativeHlsOnSafari setting of the streaming config. - return Platform.isApple() && this.config_.streaming.useNativeHlsOnSafari; + // version there. + + // Native HLS can be preferred on any platform via this flag: + if (this.config_.streaming.preferNativeHls) { + return true; + } + + // For Safari, we have an older flag which only applies to this one + // browser: + if (Platform.isApple()) { + return this.config_.streaming.useNativeHlsOnSafari; + } + + // In all other cases, we prefer MediaSource. + return false; } // Unless there are good reasons to use src= (single-file playback or native diff --git a/lib/util/player_configuration.js b/lib/util/player_configuration.js index e5641b777e..e7c5b1fcdc 100644 --- a/lib/util/player_configuration.js +++ b/lib/util/player_configuration.js @@ -137,6 +137,7 @@ shaka.util.PlayerConfiguration = class { lowLatencyMode: false, autoLowLatencyMode: false, forceHTTPS: false, + preferNativeHls: false, }; // WebOS, Tizen, and Chromecast have long hardware pipelines that respond