diff --git a/lib/player.js b/lib/player.js index 5e526a1aac6..56b4fc1928f 100644 --- a/lib/player.js +++ b/lib/player.js @@ -1236,6 +1236,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget { */ shouldUseSrcEquals_(payload) { const Platform = shaka.util.Platform; + const MimeUtils = shaka.util.MimeUtils; // If we are using a platform that does not support media source, we will // fall back to src= to handle all playback. @@ -1290,7 +1291,8 @@ shaka.Player = class extends shaka.util.FakeEventTarget { // version there. // Native HLS can be preferred on any platform via this flag: - if (this.config_.streaming.preferNativeHls) { + if (MimeUtils.isHlsType(mimeType) && + this.config_.streaming.preferNativeHls) { return true; } diff --git a/lib/util/mime_utils.js b/lib/util/mime_utils.js index 1471142f841..84350e53198 100644 --- a/lib/util/mime_utils.js +++ b/lib/util/mime_utils.js @@ -196,6 +196,17 @@ shaka.util.MimeUtils = class { return value; } + /** + * Checks if the given MIME type is HLS MIME type. + * + * @param {string} mimeType + * @return {boolean} + */ + static isHlsType(mimeType) { + return mimeType === 'application/x-mpegurl' || + mimeType === 'application/vnd.apple.mpegurl'; + } + /** * Get the base and profile of a codec string. Where [0] will be the codec * base and [1] will be the profile. diff --git a/test/player_unit.js b/test/player_unit.js index e0ee903b213..673a79b1e30 100644 --- a/test/player_unit.js +++ b/test/player_unit.js @@ -554,6 +554,60 @@ describe('Player', () => { expect(streamingEngine.unloadTextStream).not.toHaveBeenCalled(); }); }); + + describe('when config.streaming.preferNativeHls is set to true', () => { + beforeEach(() => { + shaka.media.ManifestParser.registerParserByMime( + 'application/x-mpegurl', + () => new shaka.test.FakeManifestParser(manifest)); + }); + + afterEach(() => { + shaka.media.ManifestParser.unregisterParserByMime( + 'application/x-mpegurl'); + video.canPlayType.calls.reset(); + }); + + it('only applies to HLS streams', async () => { + video.canPlayType.and.returnValue('maybe'); + spyOn(shaka.util.Platform, 'anyMediaElement').and.returnValue(video); + spyOn(shaka.util.Platform, 'supportsMediaSource').and.returnValue(true); + spyOn(shaka.util.Platform, 'isApple').and.returnValue(false); + // Make sure player.load() resolves for src= + spyOn(shaka.util.MediaReadyState, 'waitForReadyState').and.callFake( + (mediaElement, readyState, eventManager, callback) => { + callback(); + }); + + player.configure({ + streaming: { + preferNativeHls: true, + useNativeHlsOnSafari: false, + }, + }); + + await player.load(fakeManifestUri, undefined, 'application/x-mpegurl'); + + expect(player.getLoadMode()).toBe(shaka.Player.LoadMode.SRC_EQUALS); + }); + + it('does not apply to non-HLS streams', async () => { + video.canPlayType.and.returnValue('maybe'); + spyOn(shaka.util.Platform, 'supportsMediaSource').and.returnValue(true); + spyOn(shaka.util.Platform, 'isApple').and.returnValue(false); + + player.configure({ + streaming: { + preferNativeHls: true, + useNativeHlsOnSafari: false, + }, + }); + + await player.load(fakeManifestUri, 0, fakeMimeType); + + expect(player.getLoadMode()).toBe(shaka.Player.LoadMode.MEDIA_SOURCE); + }); + }); }); // describe('load/unload') describe('getConfiguration', () => { diff --git a/test/test/util/fake_drm_engine.js b/test/test/util/fake_drm_engine.js index 79140c5d432..85097bb417b 100644 --- a/test/test/util/fake_drm_engine.js +++ b/test/test/util/fake_drm_engine.js @@ -81,6 +81,9 @@ shaka.test.FakeDrmEngine = class { /** @type {!jasmine.Spy} */ this.supportsVariant = jasmine.createSpy('supportsVariant'); this.supportsVariant.and.returnValue(true); + + /** @type {!jasmine.Spy} */ + this.setSrcEquals = jasmine.createSpy('setSrcEquals'); } /** diff --git a/test/test/util/simple_fakes.js b/test/test/util/simple_fakes.js index 073b54b4bd3..53d79142081 100644 --- a/test/test/util/simple_fakes.js +++ b/test/test/util/simple_fakes.js @@ -228,6 +228,9 @@ shaka.test.FakeVideo = class { /** @type {!jasmine.Spy} */ this.dispatchEvent = jasmine.createSpy('dispatchEvent'); + + /** @type {!jasmine.Spy} */ + this.canPlayType = jasmine.createSpy('canPlayType'); } }; diff --git a/test/util/mime_utils_unit.js b/test/util/mime_utils_unit.js index 0364314ac10..3fe9799a99e 100644 --- a/test/util/mime_utils_unit.js +++ b/test/util/mime_utils_unit.js @@ -51,4 +51,14 @@ describe('MimeUtils', () => { expect(getNormalizedCodec('dvh1.05')).toBe('dovi'); expect(getNormalizedCodec('dvhe.05')).toBe('dovi'); }); + + it('isHlsType', () => { + const isHlsType = (mimeType) => shaka.util.MimeUtils.isHlsType(mimeType); + + expect(isHlsType('application/x-mpegurl')).toBe(true); + expect(isHlsType('application/vnd.apple.mpegurl')).toBe(true); + expect(isHlsType('application/dash+xml')).toBe(false); + expect(isHlsType('application/vnd.ms-sstr+xml')).toBe(false); + expect(isHlsType('foo')).toBe(false); + }); });