Skip to content

Commit

Permalink
Merge pull request #1569 from canalplus/misc/main-mse_on-error-refacto
Browse files Browse the repository at this point in the history
Make MainSourceBufferInterface code more readable isolating inner functions
  • Loading branch information
peaBerberian authored Oct 17, 2024
2 parents 419d66a + e264f35 commit 1699573
Showing 1 changed file with 49 additions and 46 deletions.
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

0 comments on commit 1699573

Please sign in to comment.