Skip to content

Commit

Permalink
fix: cache source buffer params on codec switch reload (#7630)
Browse files Browse the repository at this point in the history
Caches source buffer parameters on codec switch with the RELOAD
configuration

Fixes #7555
  • Loading branch information
dpfister2 authored and joeyparrish committed Nov 19, 2024
1 parent 2a51f0c commit 856f13f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Cristian Atehortua <[email protected]>
Damien Deis <[email protected]>
Dany L'Hébreux <[email protected]>
Dave Nicholas <[email protected]>
David Pfister <[email protected]>
Davide Zordan <[email protected]>
Dl Dador <[email protected]>
Edgeware AB <*@edgeware.tv>
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Cristian Atehortua <[email protected]>
Damien Deis <[email protected]>
Dany L'Hébreux <[email protected]>
Dave Nicholas <[email protected]>
David Pfister <[email protected]>
Davide Zordan <[email protected]>
Dl Dador <[email protected]>
Donato Borrello <[email protected]>
Expand Down
55 changes: 54 additions & 1 deletion lib/media/media_source_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -2100,6 +2100,48 @@ shaka.media.MediaSourceEngine = class {
null);
}

/**
* Returns the source buffer parameters
* @param {shaka.util.ManifestParserUtils.ContentType} contentType
* @return {?shaka.media.MediaSourceEngine.SourceBufferParams}
* @private
*/
getSourceBufferParams_(contentType) {
if (!this.sourceBuffers_[contentType]) {
return null;
}
return {
timestampOffset: this.sourceBuffers_[contentType].timestampOffset,
appendWindowStart: this.sourceBuffers_[contentType].appendWindowStart,
appendWindowEnd: this.sourceBuffers_[contentType].appendWindowEnd,
};
}

/**
* Restore source buffer parameters
* @param {shaka.util.ManifestParserUtils.ContentType} contentType
* @param {?shaka.media.MediaSourceEngine.SourceBufferParams} params
* @private
*/
restoreSourceBufferParams_(contentType, params) {
if (!params) {
return;
}

if (!this.sourceBuffers_[contentType]) {
shaka.log.warning('Attempted to restore a non-existent source buffer');
return;
}

this.sourceBuffers_[contentType].timestampOffset =
params.timestampOffset;
// `end` needs to be set before `start`
this.sourceBuffers_[contentType].appendWindowEnd =
params.appendWindowEnd;
this.sourceBuffers_[contentType].appendWindowStart =
params.appendWindowStart;
}

/**
* Resets the MediaSource and re-adds source buffers due to codec mismatch
*
Expand Down Expand Up @@ -2160,7 +2202,6 @@ shaka.media.MediaSourceEngine = class {
}
await Promise.all(cleanup);
this.transmuxers_ = {};
this.sourceBuffers_ = {};

const previousDuration = this.mediaSource_.duration;
this.mediaSourceOpen_ = new shaka.util.PublicPromise();
Expand Down Expand Up @@ -2191,6 +2232,7 @@ shaka.media.MediaSourceEngine = class {
onSourceBufferAdded);

for (const contentType of streamsByType.keys()) {
const previousParams = this.getSourceBufferParams_(contentType);
const stream = streamsByType.get(contentType);
// eslint-disable-next-line no-await-in-loop
await this.initSourceBuffer_(contentType, stream, stream.codecs);
Expand All @@ -2200,6 +2242,8 @@ shaka.media.MediaSourceEngine = class {
} else {
this.queues_[contentType] = [];
}

this.restoreSourceBufferParams_(contentType, previousParams);
}
const audio = streamsByType.get(ContentType.AUDIO);
if (audio && audio.isAudioMuxedInVideo) {
Expand Down Expand Up @@ -2497,3 +2541,12 @@ shaka.media.MediaSourceEngine.SourceBufferMode_ = {
* Called when an embedded 'emsg' box should trigger a manifest update.
*/
shaka.media.MediaSourceEngine.PlayerInterface;

/**
* @typedef {{
* timestampOffset: number,
* appendWindowStart: number,
* appendWindowEnd: number
* }}
*/
shaka.media.MediaSourceEngine.SourceBufferParams;

0 comments on commit 856f13f

Please sign in to comment.