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

Rename and factorize some methods of the MediaSourceContentInitializer #1570

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Changes from 1 commit
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
134 changes: 67 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,76 @@ 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 {
const initCanceller = this._initCanceller;
const onReloadMediaSource: IReloadMediaSourceCallback = (reloadOrder: {
Florent-Bouisset marked this conversation as resolved.
Show resolved Hide resolved
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);
});
};
this._startLoadingContentOnMediaSource(
args,
onReloadMediaSource,
currentCanceller.signal,
);
}

/**
Expand All @@ -534,7 +534,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
Loading