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

Make MainSourceBufferInterface code more readable isolating inner functions #1569

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
95 changes: 49 additions & 46 deletions src/mse/main_media_source_interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,52 +212,8 @@ export class MainSourceBufferInterface implements ISourceBufferInterface {
this._operationQueue = [];
this._currentOperations = [];

const onError = (evt: Event) => {
let error: Error;
if ((evt as unknown as Error) instanceof Error) {
error = evt as unknown as Error;
} else if ((evt as unknown as { error: Error }).error instanceof Error) {
error = (evt as unknown as { error: Error }).error;
} else {
error = new Error("Unknown SourceBuffer Error");
}
const currentOps = this._currentOperations;
this._currentOperations = [];
if (currentOps.length === 0) {
log.error("SBI: error for an unknown operation", error);
} else {
const rejected = new SourceBufferError(
error.name,
error.message,
error.name === "QuotaExceededError",
);
for (const op of currentOps) {
op.reject(rejected);
}
}
};
const onUpdateEnd = () => {
const currentOps = this._currentOperations;
this._currentOperations = [];
try {
for (const op of currentOps) {
op.resolve(convertToRanges(this._sourceBuffer.buffered));
}
} catch (err) {
for (const op of currentOps) {
if (err instanceof Error && err.name === "InvalidStateError") {
// Most likely the SourceBuffer just has been removed from the
// `MediaSource`.
// Just return an empty buffered range.
op.resolve([]);
} else {
op.reject(err);
}
}
}
this._performNextOperation();
};

const onError = this._onError.bind(this);
const onUpdateEnd = this._onUpdateEnd.bind(this);
sourceBuffer.addEventListener("updateend", onUpdateEnd);
sourceBuffer.addEventListener("error", onError);
this._canceller.signal.register(() => {
Expand Down Expand Up @@ -325,6 +281,53 @@ export class MainSourceBufferInterface implements ISourceBufferInterface {
this._emptyCurrentQueue();
}

private _onError(evt: Event) {
let error: Error;
if ((evt as unknown as Error) instanceof Error) {
error = evt as unknown as Error;
} else if ((evt as unknown as { error: Error }).error instanceof Error) {
error = (evt as unknown as { error: Error }).error;
} else {
error = new Error("Unknown SourceBuffer Error");
}
const currentOps = this._currentOperations;
this._currentOperations = [];
if (currentOps.length === 0) {
log.error("SBI: error for an unknown operation", error);
} else {
const rejected = new SourceBufferError(
error.name,
error.message,
error.name === "QuotaExceededError",
);
for (const op of currentOps) {
op.reject(rejected);
}
}
}

private _onUpdateEnd() {
const currentOps = this._currentOperations;
this._currentOperations = [];
try {
for (const op of currentOps) {
op.resolve(convertToRanges(this._sourceBuffer.buffered));
}
} catch (err) {
for (const op of currentOps) {
if (err instanceof Error && err.name === "InvalidStateError") {
// Most likely the SourceBuffer just has been removed from the
// `MediaSource`.
// Just return an empty buffered range.
op.resolve([]);
} else {
op.reject(err);
}
}
}
this._performNextOperation();
}

private _emptyCurrentQueue(): void {
const error = new CancellationError();
if (this._currentOperations.length > 0) {
Expand Down
Loading