Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transfer media (source) failing when MediaSource readyState is "ended" #6904

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading