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

Log error and detach if MediaSource 'sourceopen' is interrupted #5206

Merged
merged 2 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
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
16 changes: 14 additions & 2 deletions src/controller/buffer-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export default class BufferController implements ComponentAPI {
media.src = self.URL.createObjectURL(ms);
// cache the locally generated object url
this._objectUrl = media.src;
media.addEventListener('emptied', this._onMediaEmptied);
}
}

Expand Down Expand Up @@ -188,6 +189,7 @@ export default class BufferController implements ComponentAPI {
// Detach properly the MediaSource from the HTMLMediaElement as
// suggested in https://github.com/w3c/media-source/issues/53.
if (media) {
media.removeEventListener('emptied', this._onMediaEmptied);
if (_objectUrl) {
self.URL.revokeObjectURL(_objectUrl);
}
Expand Down Expand Up @@ -785,11 +787,12 @@ export default class BufferController implements ComponentAPI {

// Keep as arrow functions so that we can directly reference these functions directly as event listeners
private _onMediaSourceOpen = () => {
const { hls, media, mediaSource } = this;
const { media, mediaSource } = this;
logger.log('[buffer-controller]: Media source opened');
if (media) {
media.removeEventListener('emptied', this._onMediaEmptied);
this.updateMediaElementDuration();
hls.trigger(Events.MEDIA_ATTACHED, { media });
this.hls.trigger(Events.MEDIA_ATTACHED, { media });
}

if (mediaSource) {
Expand All @@ -807,6 +810,15 @@ export default class BufferController implements ComponentAPI {
logger.log('[buffer-controller]: Media source ended');
};

private _onMediaEmptied = () => {
const { media, _objectUrl } = this;
if (media && media.src !== _objectUrl) {
logger.error(
`Media element src was set while attaching MediaSource (${_objectUrl} > ${media.src})`
);
}
};

private _onSBUpdateStart(type: SourceBufferName) {
const { operationQueue } = this;
const operation = operationQueue.current(type);
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/controller/buffer-controller-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class MockMediaElement {
public currentTime: number = 0;
public duration: number = Infinity;
public textTracks: any[] = [];
addEventListener() {}
removeEventListener() {}
}

const queueNames: Array<SourceBufferName> = ['audio', 'video'];
Expand Down