Skip to content

Commit

Permalink
Fix compatibility of ManagedMediaSource implementation with Edge 18
Browse files Browse the repository at this point in the history
Fixes #6243
  • Loading branch information
robwalch committed Feb 27, 2024
1 parent 4cd4e1c commit cc346f7
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions src/controller/buffer-controller.ts
Expand Up @@ -89,7 +89,10 @@ export default class BufferController implements ComponentAPI {
constructor(hls: Hls) {
this.hls = hls;
const logPrefix = '[buffer-controller]';
this.appendSource = hls.config.preferManagedMediaSource;
this.appendSource =
hls.config.preferManagedMediaSource &&
typeof self !== 'undefined' &&
(self as any).ManagedMediaSource;
this.log = logger.log.bind(logger, logPrefix);
this.warn = logger.warn.bind(logger, logPrefix);
this.error = logger.error.bind(logger, logPrefix);
Expand Down Expand Up @@ -194,8 +197,10 @@ export default class BufferController implements ComponentAPI {
ms.addEventListener('sourceopen', this._onMediaSourceOpen);
ms.addEventListener('sourceended', this._onMediaSourceEnded);
ms.addEventListener('sourceclose', this._onMediaSourceClose);
ms.addEventListener('startstreaming', this._onStartStreaming);
ms.addEventListener('endstreaming', this._onEndStreaming);
if (this.appendSource) {
ms.addEventListener('startstreaming', this._onStartStreaming);
ms.addEventListener('endstreaming', this._onEndStreaming);
}

// cache the locally generated object url
const objectUrl = (this._objectUrl = self.URL.createObjectURL(ms));
Expand Down Expand Up @@ -254,8 +259,13 @@ export default class BufferController implements ComponentAPI {
mediaSource.removeEventListener('sourceopen', this._onMediaSourceOpen);
mediaSource.removeEventListener('sourceended', this._onMediaSourceEnded);
mediaSource.removeEventListener('sourceclose', this._onMediaSourceClose);
mediaSource.removeEventListener('startstreaming', this._onStartStreaming);
mediaSource.removeEventListener('endstreaming', this._onEndStreaming);
if (this.appendSource) {
mediaSource.removeEventListener(
'startstreaming',
this._onStartStreaming,
);
mediaSource.removeEventListener('endstreaming', this._onEndStreaming);
}

// Detach properly the MediaSource from the HTMLMediaElement as
// suggested in https://github.com/w3c/media-source/issues/53.
Expand Down Expand Up @@ -345,7 +355,7 @@ export default class BufferController implements ComponentAPI {
if (trackName.slice(0, 5) === 'audio') {
trackCodec = getCodecCompatibleName(
trackCodec,
this.hls.config.preferManagedMediaSource,
this.appendSource,
);
}
const mimeType = `${container};codecs=${trackCodec}`;
Expand Down Expand Up @@ -920,10 +930,7 @@ export default class BufferController implements ComponentAPI {
let codec = track.levelCodec || track.codec;
if (codec) {
if (trackName.slice(0, 5) === 'audio') {
codec = getCodecCompatibleName(
codec,
this.hls.config.preferManagedMediaSource,
);
codec = getCodecCompatibleName(codec, this.appendSource);
}
}
const mimeType = `${track.container};codecs=${codec}`;
Expand All @@ -936,19 +943,21 @@ export default class BufferController implements ComponentAPI {
this.addBufferListener(sbName, 'updateend', this._onSBUpdateEnd);
this.addBufferListener(sbName, 'error', this._onSBUpdateError);
// ManagedSourceBuffer bufferedchange event
this.addBufferListener(
sbName,
'bufferedchange',
(type: SourceBufferName, event: BufferedChangeEvent) => {
// If media was ejected check for a change. Added ranges are redundant with changes on 'updateend' event.
const removedRanges = event.removedRanges;
if (removedRanges?.length) {
this.hls.trigger(Events.BUFFER_FLUSHED, {
type: trackName as SourceBufferName,
});
}
},
);
if (this.appendSource) {
this.addBufferListener(
sbName,
'bufferedchange',
(type: SourceBufferName, event: BufferedChangeEvent) => {
// If media was ejected check for a change. Added ranges are redundant with changes on 'updateend' event.
const removedRanges = event.removedRanges;
if (removedRanges?.length) {
this.hls.trigger(Events.BUFFER_FLUSHED, {
type: trackName as SourceBufferName,
});
}
},
);
}

this.tracks[trackName] = {
buffer: sb,
Expand Down

0 comments on commit cc346f7

Please sign in to comment.