Skip to content

Commit

Permalink
Log error and detach if MediaSource 'sourceopen' is interrupted (#5206)
Browse files Browse the repository at this point in the history
  • Loading branch information
robwalch committed Feb 7, 2023
1 parent 9e33ff0 commit bf49dd2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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

0 comments on commit bf49dd2

Please sign in to comment.