Skip to content

Commit

Permalink
fix: config.streaming.preferNativeHls only applies to HLS streams (#…
Browse files Browse the repository at this point in the history
…5167)

Closes #5166
  • Loading branch information
zangue authored and joeyparrish committed Apr 26, 2023
1 parent bc4bd1e commit dd7a2dc
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,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.
Expand Down Expand Up @@ -1400,7 +1401,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;
}

Expand Down
11 changes: 11 additions & 0 deletions lib/util/mime_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,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.
Expand Down
54 changes: 54 additions & 0 deletions test/player_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,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', () => {
Expand Down
3 changes: 3 additions & 0 deletions test/test/util/fake_drm_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
3 changes: 3 additions & 0 deletions test/test/util/simple_fakes.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ shaka.test.FakeVideo = class {

/** @type {!jasmine.Spy} */
this.dispatchEvent = jasmine.createSpy('dispatchEvent');

/** @type {!jasmine.Spy} */
this.canPlayType = jasmine.createSpy('canPlayType');
}
};

Expand Down
10 changes: 10 additions & 0 deletions test/util/mime_utils_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit dd7a2dc

Please sign in to comment.