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 a5184fd commit aac5906
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 @@ -1844,6 +1844,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 @@ -1904,7 +1946,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 @@ -1935,6 +1976,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 @@ -1944,6 +1986,8 @@ shaka.media.MediaSourceEngine = class {
} else {
this.queues_[contentType] = [];
}

this.restoreSourceBufferParams_(contentType, previousParams);
}

// Fake a seek to catchup the playhead.
Expand Down Expand Up @@ -2231,3 +2275,12 @@ shaka.media.MediaSourceEngine.SourceBufferMode_ = {
* Callback to use when metadata arrives.
*/
shaka.media.MediaSourceEngine.PlayerInterface;

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

0 comments on commit aac5906

Please sign in to comment.