Skip to content

Commit

Permalink
Merge pull request #1570 from canalplus/misc/setupInitialMediaSourceA…
Browse files Browse the repository at this point in the history
…ndDecryption

Rename and factorize some methods of the MediaSourceContentInitializer
  • Loading branch information
peaBerberian authored Oct 22, 2024
2 parents dc08257 + c644523 commit f99b774
Showing 1 changed file with 75 additions and 67 deletions.
142 changes: 75 additions & 67 deletions src/main_thread/init/media_source_content_initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export default class MediaSourceContentInitializer extends ContentInitializer {
this._initCanceller.signal,
);

this._initializeMediaSourceAndDecryption(mediaElement)
this._setupInitialMediaSourceAndDecryption(mediaElement)
.then((initResult) =>
this._onInitialMediaSourceReady(
mediaElement,
Expand All @@ -230,6 +230,10 @@ export default class MediaSourceContentInitializer extends ContentInitializer {
this._manifestFetcher.updateContentUrls(urls, refreshNow);
}

/**
* Stop content and free all resources linked to this
* `MediaSourceContentInitializer`.
*/
public dispose(): void {
this._initCanceller.cancel();
}
Expand All @@ -252,7 +256,7 @@ export default class MediaSourceContentInitializer extends ContentInitializer {
* @param {HTMLMediaElement|null} mediaElement
* @returns {Promise.<Object>}
*/
private _initializeMediaSourceAndDecryption(mediaElement: IMediaElement): Promise<{
private _setupInitialMediaSourceAndDecryption(mediaElement: IMediaElement): Promise<{
mediaSource: MainMediaSourceInterface;
drmSystemId: string | undefined;
unlinkMediaSource: TaskCanceller;
Expand Down Expand Up @@ -452,80 +456,84 @@ export default class MediaSourceContentInitializer extends ContentInitializer {
return;
}

const bufferOnMediaSource = this._startBufferingOnMediaSource.bind(this);
const triggerEvent = this.trigger.bind(this);
const onFatalError = this._onFatalError.bind(this);

// handle initial load and reloads
recursivelyLoadOnMediaSource(
initialMediaSource,
initialTime,
autoPlay,
initialMediaSourceCanceller,
);

/**
* Load the content defined by the Manifest in the mediaSource given at the
* given position and playing status.
* This function recursively re-call itself when a MediaSource reload is
* wanted.
* @param {MediaSource} mediaSource
* @param {number} startingPos
* @param {Object} currentCanceller
* @param {boolean} shouldPlay
*/
function recursivelyLoadOnMediaSource(
mediaSource: MainMediaSourceInterface,
startingPos: number,
shouldPlay: boolean,
currentCanceller: TaskCanceller,
): void {
const opts = {
this._setupContentWithNewMediaSource(
{
mediaElement,
playbackObserver,
mediaSource,
initialTime: startingPos,
autoPlay: shouldPlay,
mediaSource: initialMediaSource,
initialTime,
autoPlay,
manifest,
representationEstimator,
segmentQueueCreator,
speed,
bufferOptions: subBufferOptions,
};
bufferOnMediaSource(opts, onReloadMediaSource, currentCanceller.signal);

function onReloadMediaSource(reloadOrder: {
position: number;
autoPlay: boolean;
}): void {
currentCanceller.cancel();
if (initCanceller.isUsed()) {
return;
}
triggerEvent("reloadingMediaSource", reloadOrder);
if (initCanceller.isUsed()) {
return;
}
},
initialMediaSourceCanceller,
);
}

const newCanceller = new TaskCanceller();
newCanceller.linkToSignal(initCanceller.signal);
createMediaSource(mediaElement, newCanceller.signal)
.then((newMediaSource) => {
recursivelyLoadOnMediaSource(
newMediaSource,
reloadOrder.position,
reloadOrder.autoPlay,
newCanceller,
);
})
.catch((err) => {
if (newCanceller.isUsed()) {
return;
}
onFatalError(err);
});
/**
* Load the content defined by the Manifest in the mediaSource given at the
* given position and playing status.
* This function recursively re-call itself when a MediaSource reload is
* wanted.
* @param {Object} args
* @param {Object} currentCanceller
*/
private _setupContentWithNewMediaSource(
args: IBufferingMediaSettings,
currentCanceller: TaskCanceller,
): void {
this._startLoadingContentOnMediaSource(
args,
this._createReloadMediaSourceCallback(args, currentCanceller),
currentCanceller.signal,
);
}

/**
* Create `IReloadMediaSourceCallback` allowing to handle reload orders.
* @param {Object} args
* @param {Object} currentCanceller
*/
private _createReloadMediaSourceCallback(
args: IBufferingMediaSettings,
currentCanceller: TaskCanceller,
): IReloadMediaSourceCallback {
const initCanceller = this._initCanceller;
return (reloadOrder: { position: number; autoPlay: boolean }): void => {
currentCanceller.cancel();
if (initCanceller.isUsed()) {
return;
}
}
this.trigger("reloadingMediaSource", reloadOrder);
if (initCanceller.isUsed()) {
return;
}

const newCanceller = new TaskCanceller();
newCanceller.linkToSignal(initCanceller.signal);
createMediaSource(args.mediaElement, newCanceller.signal)
.then((newMediaSource) => {
this._setupContentWithNewMediaSource(
{
...args,
mediaSource: newMediaSource,
initialTime: reloadOrder.position,
autoPlay: reloadOrder.autoPlay,
},
newCanceller,
);
})
.catch((err) => {
if (newCanceller.isUsed()) {
return;
}
this._onFatalError(err);
});
};
}

/**
Expand All @@ -534,7 +542,7 @@ export default class MediaSourceContentInitializer extends ContentInitializer {
* @param {function} onReloadOrder
* @param {Object} cancelSignal
*/
private _startBufferingOnMediaSource(
private _startLoadingContentOnMediaSource(
args: IBufferingMediaSettings,
onReloadOrder: IReloadMediaSourceCallback,
cancelSignal: CancellationSignal,
Expand Down

0 comments on commit f99b774

Please sign in to comment.