Skip to content

Commit cc930a8

Browse files
authored
fix: Avoid useless MSE reset when transmuxing muxed content (#7818)
1 parent 77c4251 commit cc930a8

File tree

2 files changed

+75
-37
lines changed

2 files changed

+75
-37
lines changed

lib/media/media_source_engine.js

+74-31
Original file line numberDiff line numberDiff line change
@@ -2247,32 +2247,32 @@ shaka.media.MediaSourceEngine = class {
22472247
}
22482248

22492249
/**
2250-
* Codec switch if necessary, this will not resolve until the codec
2251-
* switch is over.
22522250
* @param {shaka.util.ManifestParserUtils.ContentType} contentType
22532251
* @param {string} mimeType
22542252
* @param {string} codecs
2255-
* @param {!Map.<shaka.util.ManifestParserUtils.ContentType,
2256-
* shaka.extern.Stream>} streamsByType
2257-
* @return {!Promise.<boolean>} true if there was a codec switch,
2258-
* false otherwise.
2253+
* @return {{transmuxer: ?shaka.extern.Transmuxer,
2254+
* transmuxerMuxed: boolean, basicType: string, codec: string,
2255+
* mimeType: string}}
22592256
* @private
22602257
*/
2261-
async codecSwitchIfNecessary_(contentType, mimeType, codecs, streamsByType) {
2258+
getRealInfo_(contentType, mimeType, codecs) {
22622259
const ContentType = shaka.util.ManifestParserUtils.ContentType;
2263-
if (contentType == ContentType.TEXT) {
2264-
return false;
2265-
}
22662260
const MimeUtils = shaka.util.MimeUtils;
2267-
const currentCodec = MimeUtils.getNormalizedCodec(
2268-
MimeUtils.getCodecs(this.sourceBufferTypes_[contentType]));
2269-
const currentBasicType = MimeUtils.getBasicType(
2270-
this.sourceBufferTypes_[contentType]);
2271-
22722261
/** @type {?shaka.extern.Transmuxer} */
22732262
let transmuxer;
22742263
let transmuxerMuxed = false;
2275-
let newMimeType = shaka.util.MimeUtils.getFullType(mimeType, codecs);
2264+
const audioCodec = shaka.util.ManifestParserUtils.guessCodecsSafe(
2265+
ContentType.AUDIO, (codecs || '').split(','));
2266+
const videoCodec = shaka.util.ManifestParserUtils.guessCodecsSafe(
2267+
ContentType.VIDEO, (codecs || '').split(','));
2268+
let codec = videoCodec;
2269+
if (contentType == ContentType.AUDIO) {
2270+
codec = audioCodec;
2271+
}
2272+
if (!codec) {
2273+
codec = codecs;
2274+
}
2275+
let newMimeType = shaka.util.MimeUtils.getFullType(mimeType, codec);
22762276
let needTransmux = this.config_.forceTransmux;
22772277
if (!shaka.media.Capabilities.isTypeSupported(newMimeType) ||
22782278
(!this.sequenceMode_ &&
@@ -2282,36 +2282,79 @@ shaka.media.MediaSourceEngine = class {
22822282
const TransmuxerEngine = shaka.transmuxer.TransmuxerEngine;
22832283
if (needTransmux) {
22842284
const newMimeTypeWithAllCodecs =
2285-
shaka.util.MimeUtils.getFullTypeWithAllCodecs(mimeType, codecs);
2285+
shaka.util.MimeUtils.getFullTypeWithAllCodecs(mimeType, codec);
22862286
const transmuxerPlugin =
22872287
TransmuxerEngine.findTransmuxer(newMimeTypeWithAllCodecs);
22882288
if (transmuxerPlugin) {
22892289
transmuxer = transmuxerPlugin();
2290-
const audioCodec = shaka.util.ManifestParserUtils.guessCodecsSafe(
2291-
ContentType.AUDIO, (codecs || '').split(','));
2292-
const videoCodec = shaka.util.ManifestParserUtils.guessCodecsSafe(
2293-
ContentType.VIDEO, (codecs || '').split(','));
22942290
if (audioCodec && videoCodec) {
22952291
transmuxerMuxed = true;
2296-
let codec = videoCodec;
2297-
if (contentType == ContentType.AUDIO) {
2298-
codec = audioCodec;
2299-
}
2300-
newMimeType = transmuxer.convertCodecs(contentType,
2301-
shaka.util.MimeUtils.getFullTypeWithAllCodecs(mimeType, codec));
2302-
} else {
2303-
newMimeType =
2304-
transmuxer.convertCodecs(contentType, newMimeTypeWithAllCodecs);
23052292
}
2293+
newMimeType =
2294+
transmuxer.convertCodecs(contentType, newMimeTypeWithAllCodecs);
23062295
}
23072296
}
23082297

23092298
const newCodec = MimeUtils.getNormalizedCodec(
23102299
MimeUtils.getCodecs(newMimeType));
23112300
const newBasicType = MimeUtils.getBasicType(newMimeType);
2301+
return {
2302+
transmuxer,
2303+
transmuxerMuxed,
2304+
basicType: newBasicType,
2305+
codec: newCodec,
2306+
mimeType: newMimeType,
2307+
};
2308+
}
2309+
2310+
/**
2311+
* Codec switch if necessary, this will not resolve until the codec
2312+
* switch is over.
2313+
* @param {shaka.util.ManifestParserUtils.ContentType} contentType
2314+
* @param {string} mimeType
2315+
* @param {string} codecs
2316+
* @param {!Map.<shaka.util.ManifestParserUtils.ContentType,
2317+
* shaka.extern.Stream>} streamsByType
2318+
* @return {!Promise.<boolean>} true if there was a codec switch,
2319+
* false otherwise.
2320+
* @private
2321+
*/
2322+
async codecSwitchIfNecessary_(contentType, mimeType, codecs, streamsByType) {
2323+
const ContentType = shaka.util.ManifestParserUtils.ContentType;
2324+
if (contentType == ContentType.TEXT) {
2325+
return false;
2326+
}
2327+
const MimeUtils = shaka.util.MimeUtils;
2328+
const currentCodec = MimeUtils.getNormalizedCodec(
2329+
MimeUtils.getCodecs(this.sourceBufferTypes_[contentType]));
2330+
const currentBasicType = MimeUtils.getBasicType(
2331+
this.sourceBufferTypes_[contentType]);
2332+
2333+
const realInfo = this.getRealInfo_(contentType, mimeType, codecs);
2334+
const transmuxer = realInfo.transmuxer;
2335+
const transmuxerMuxed = realInfo.transmuxerMuxed;
2336+
const newBasicType = realInfo.basicType;
2337+
const newCodec = realInfo.codec;
2338+
const newMimeType = realInfo.mimeType;
2339+
2340+
let muxedContentCheck = true;
2341+
if (transmuxerMuxed) {
2342+
const muxedRealInfo =
2343+
this.getRealInfo_(ContentType.AUDIO, mimeType, codecs);
2344+
const muxedCurrentCodec = MimeUtils.getNormalizedCodec(
2345+
MimeUtils.getCodecs(this.sourceBufferTypes_[ContentType.AUDIO]));
2346+
const muxedCurrentBasicType = MimeUtils.getBasicType(
2347+
this.sourceBufferTypes_[ContentType.AUDIO]);
2348+
muxedContentCheck = muxedCurrentCodec == muxedRealInfo.codec &&
2349+
muxedCurrentBasicType == muxedRealInfo.basicType;
2350+
if (muxedRealInfo.transmuxer) {
2351+
muxedRealInfo.transmuxer.destroy();
2352+
}
2353+
}
23122354

23132355
// Current/new codecs base and basic type match then no need to switch
2314-
if (currentCodec === newCodec && currentBasicType === newBasicType) {
2356+
if (currentCodec === newCodec && currentBasicType === newBasicType &&
2357+
muxedContentCheck) {
23152358
if (this.transmuxers_[contentType] && !transmuxer) {
23162359
this.transmuxers_[contentType].destroy();
23172360
delete this.transmuxers_[contentType];

test/transmuxer/transmuxer_integration.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -383,12 +383,7 @@ describe('Transmuxer Player', () => {
383383

384384
// eslint-disable-next-line max-len
385385
await player.load('/base/test/test/assets/hls-ts-muxed-mp3-h264/index.m3u8');
386-
try {
387-
await video.play();
388-
// eslint-disable-next-line no-restricted-syntax
389-
} catch (e) {
390-
// Ignore play errors
391-
}
386+
await video.play();
392387
expect(player.isLive()).toBe(false);
393388

394389
// Wait for the video to start playback. If it takes longer than 10

0 commit comments

Comments
 (0)