Skip to content

Commit

Permalink
feat: add mediaSource.addExtraFeaturesToSourceBuffer (#6362)
Browse files Browse the repository at this point in the history
Closes #6358
  • Loading branch information
tykus160 committed Mar 26, 2024
1 parent 73d888e commit d0aa697
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
2 changes: 0 additions & 2 deletions demo/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,6 @@ shakaDemo.Config = class {

const docLink = this.resolveExternLink_('.MediaSourceConfiguration');
this.addSection_('Media source', docLink)
.addTextInput_('Source buffer extra features',
'mediaSource.sourceBufferExtraFeatures')
.addBoolInput_('Force Transmux', 'mediaSource.forceTransmux')
.addBoolInput_('Insert fake encryption in init segments when needed ' +
'by the platform.', 'mediaSource.insertFakeEncryptionInInit')
Expand Down
1 change: 1 addition & 0 deletions docs/tutorials/upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ application:
`getAttribute()` and `textContent` results must now be decoded if they might contain
escape sequences. You can use `shaka.util.StringUtils.htmlUnescape` for this purpose.
- `streaming.useNativeHlsOnSafari` has removed. Now we have another config to do the same for FairPlay `streaming.useNativeHlsForFairPlay` or for HLS (any browser) `streaming.preferNativeHls`.
- `mediaSource.sourceBufferExtraFeatures` has been replaced with `mediaSource.addExtraFeaturesToSourceBuffer` callback.

- Plugin changes:
- `Transmuxer` plugins now has three new parameters in `transmux()` method.
Expand Down
8 changes: 5 additions & 3 deletions externs/shaka/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1383,7 +1383,7 @@ shaka.extern.StreamingConfiguration;
/**
* @typedef {{
* codecSwitchingStrategy: shaka.config.CodecSwitchingStrategy,
* sourceBufferExtraFeatures: string,
* addExtraFeaturesToSourceBuffer: function(string): string,
* forceTransmux: boolean,
* insertFakeEncryptionInInit: boolean,
* modifyCueCallback: shaka.extern.TextParser.ModifyCueCallback
Expand All @@ -1397,10 +1397,12 @@ shaka.extern.StreamingConfiguration;
* SourceBuffer.changeType. RELOAD uses cycling of MediaSource.
* Defaults to SMOOTH if SMOOTH codec switching is supported, RELOAD
* overwise.
* @property {string} sourceBufferExtraFeatures
* @property {function(string): string} addExtraFeaturesToSourceBuffer
* Callback to generate extra features striug based on used MIME type.
* Some platforms may need to pass features when initializing the
* sourceBuffer.
* This string is ultimately appended to MIME types in addSourceBuffer().
* This string is ultimately appended to a MIME type in addSourceBuffer() &
* changeType().
* @property {boolean} forceTransmux
* If this is <code>true</code>, we will transmux AAC and TS content even if
* not strictly necessary for the assets to be played.
Expand Down
17 changes: 15 additions & 2 deletions lib/media/media_source_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ shaka.media.MediaSourceEngine = class {
transmuxer.convertCodecs(contentType, mimeTypeWithAllCodecs);
}
}
const type = mimeType + this.config_.sourceBufferExtraFeatures;
const type = this.addExtraFeaturesToMimeType_(mimeType);

this.destroyer_.ensureNotDestroyed();

Expand Down Expand Up @@ -1680,7 +1680,7 @@ shaka.media.MediaSourceEngine = class {
if (transmuxer) {
this.transmuxers_[contentType] = transmuxer;
}
const type = mimeType + this.config_.sourceBufferExtraFeatures;
const type = this.addExtraFeaturesToMimeType_(mimeType);
this.sourceBuffers_[contentType].changeType(type);
this.sourceBufferTypes_[contentType] = mimeType;
} else {
Expand Down Expand Up @@ -1997,6 +1997,19 @@ shaka.media.MediaSourceEngine = class {
updateLcevcDec(lcevcDec) {
this.lcevcDec_ = lcevcDec;
}

/**
* @param {string} mimeType
* @return {string}
* @private
*/
addExtraFeaturesToMimeType_(mimeType) {
const extraFeatures = this.config_.addExtraFeaturesToSourceBuffer(mimeType);
const extendedType = mimeType + extraFeatures;
shaka.log.debug('Using full mime type', extendedType);

return extendedType;
}
};


Expand Down
6 changes: 5 additions & 1 deletion lib/util/player_configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,11 @@ shaka.util.PlayerConfiguration = class {

const mediaSource = {
codecSwitchingStrategy: codecSwitchingStrategy,
sourceBufferExtraFeatures: '',
addExtraFeaturesToSourceBuffer: (mimeType) => {
return shaka.util.ConfigUtils.referenceParametersAndReturn(
[mimeType],
'');
},
forceTransmux: false,
insertFakeEncryptionInInit: true,
modifyCueCallback: (cue, uri) => {
Expand Down
23 changes: 23 additions & 0 deletions test/media/media_source_engine_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,29 @@ describe('MediaSourceEngine', () => {
expect(shaka.text.TextEngine).not.toHaveBeenCalled();
});

it('creates SourceBuffers with extra features', async () => {
const config = shaka.util.PlayerConfiguration.createDefault().mediaSource;
config.addExtraFeaturesToSourceBuffer = (mimeType) => {
if (mimeType.includes('audio')) {
return '; extra_audio_param';
}
if (mimeType.includes('video')) {
return '; extra_video_param';
}
return '';
};
mediaSourceEngine.configure(config);
const initObject = new Map();
initObject.set(ContentType.AUDIO, fakeAudioStream);
initObject.set(ContentType.VIDEO, fakeVideoStream);
await mediaSourceEngine.init(initObject, false);
expect(mockMediaSource.addSourceBuffer).toHaveBeenCalledWith(
'audio/mp4; extra_audio_param');
expect(mockMediaSource.addSourceBuffer).toHaveBeenCalledWith(
'video/mp4; extra_video_param');
expect(shaka.text.TextEngine).not.toHaveBeenCalled();
});

it('creates TextEngines for text types', async () => {
const initObject = new Map();
initObject.set(ContentType.TEXT, fakeTextStream);
Expand Down

0 comments on commit d0aa697

Please sign in to comment.