Skip to content

Commit

Permalink
Fix transfer media failing when MediaSource readyState is "ended"
Browse files Browse the repository at this point in the history
  • Loading branch information
robwalch committed Dec 11, 2024
1 parent 927ffc0 commit 3ab014a
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/controller/buffer-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,8 @@ export default class BufferController extends Logger implements ComponentAPI {
: null;
const trackCount = trackNames ? trackNames.length : 0;
const mediaSourceOpenCallback = () => {
if (this.media) {
const readyState = this.mediaSource?.readyState;
if (readyState === 'open' || readyState === 'ended') {
this._onMediaSourceOpen();
}
if (this.media && this.mediaSourceOpenOrEnded) {
this._onMediaSourceOpen();
}
};
if (transferredTracks && trackNames && trackCount) {
Expand Down Expand Up @@ -429,6 +426,11 @@ transfer tracks: ${JSON.stringify(transferredTracks, (key, value) => (key === 'i
}
}

private get mediaSourceOpenOrEnded(): boolean {
const readyState = this.mediaSource?.readyState;
return readyState === 'open' || readyState === 'ended';
}

private _onEndStreaming = (event) => {
if (!this.hls) {
return;
Expand Down Expand Up @@ -466,18 +468,23 @@ transfer tracks: ${JSON.stringify(transferredTracks, (key, value) => (key === 'i
});
this.resetQueue();
} else {
if (mediaSource.readyState === 'open') {
if (this.mediaSourceOpenOrEnded) {
const open = mediaSource.readyState === 'open';
try {
const sourceBuffers = mediaSource.sourceBuffers;
for (let i = sourceBuffers.length; i--; ) {
sourceBuffers[i].abort();
if (open) {
sourceBuffers[i].abort();
}
mediaSource.removeSourceBuffer(sourceBuffers[i]);
}
// endOfStream could trigger exception if any sourcebuffer is in updating state
// we don't really care about checking sourcebuffer state here,
// as we are anyway detaching the MediaSource
// let's just avoid this exception to propagate
mediaSource.endOfStream();
if (open) {
// endOfStream could trigger exception if any sourcebuffer is in updating state
// we don't really care about checking sourcebuffer state here,
// as we are anyway detaching the MediaSource
// let's just avoid this exception to propagate
mediaSource.endOfStream();
}
} catch (err) {
this.warn(
`onMediaDetaching: ${err.message} while calling endOfStream`,
Expand Down Expand Up @@ -651,7 +658,7 @@ transfer tracks: ${JSON.stringify(transferredTracks, (key, value) => (key === 'i
if (this.sourceBufferCount) {
return;
}
if (this.mediaSource !== null && this.mediaSource.readyState === 'open') {
if (this.mediaSourceOpenOrEnded) {
this.checkPendingTracks();
}
}
Expand Down Expand Up @@ -892,7 +899,7 @@ transfer tracks: ${JSON.stringify(transferredTracks, (key, value) => (key === 'i
event.details = ErrorDetails.BUFFER_FULL_ERROR;
} else if (
(error as DOMException).code === DOMException.INVALID_STATE_ERR &&
this.mediaSource?.readyState === 'open' &&
this.mediaSourceOpenOrEnded &&
!this.media?.error
) {
// Allow retry for "Failed to execute 'appendBuffer' on 'SourceBuffer': This SourceBuffer is still processing" errors
Expand Down

0 comments on commit 3ab014a

Please sign in to comment.