Skip to content

Commit

Permalink
Add ISourceBufferList. Remove unused activeSourceBuffers
Browse files Browse the repository at this point in the history
  • Loading branch information
peaBerberian committed Jun 28, 2024
1 parent c712b78 commit 7512dba
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ module.exports = {
message:
"Avoid relying on `SourceBuffer` directly unless it is API-facing. Prefer our more restricted `ISourceBuffer` type",
},
SourceBufferList: {
message:
"Avoid relying on `SourceBufferList` directly unless it is API-facing. Prefer our more restricted `ISourceBufferList` type",
},
},
},
],
Expand Down
37 changes: 35 additions & 2 deletions src/compat/browser_compatibility_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,33 @@ export interface IEventTarget<TEventMap> {
): void;
}

/** Events potentially dispatched by an `ISourceBufferList` */
export interface ISourceBufferListEventMap {
addsourcebuffer: Event;
removesourcebuffer: Event;
}

/**
* More restrictive and type-compatible with the `SourceBufferList` type (i.e. a
* `SourceBufferList` is a valid `ISourceBufferList`), the `ISourceBufferList`
* type allows to:
*
* - re-define some attributes or methods in cases where we detected that some
* platforms have a different implementation.
*
* - list all `SourceBufferList` attributes, methods and events that are
* relied on by the RxPlayer.
*
* - Allow an easier re-definition of that API for tests or for platforms
* which do not implement it.
*/
export interface ISourceBufferList extends IEventTarget<ISourceBufferListEventMap> {
readonly length: number;
onaddsourcebuffer: ((evt: Event) => void) | null;
onremovesourcebuffer: ((evt: Event) => void) | null;
[index: number]: ISourceBuffer;
}

/** Events potentially dispatched by an `IMediaSource` */
export interface IMediaSourceEventMap {
sourceopen: Event;
Expand All @@ -106,11 +133,10 @@ export interface IMediaSourceEventMap {
* which do not implement it.
*/
export interface IMediaSource extends IEventTarget<IMediaSourceEventMap> {
activeSourceBuffers: SourceBufferList;
duration: number;
handle?: MediaProvider | IMediaSource | undefined;
readyState: "closed" | "open" | "ended";
sourceBuffers: SourceBufferList;
sourceBuffers: ISourceBufferList;

addSourceBuffer(type: string): ISourceBuffer;
clearLiveSeekableRange(): void;
Expand Down Expand Up @@ -302,6 +328,13 @@ function testSourceBuffer(x: SourceBuffer) {
function assetCompatibleISourceBuffer(_x: ISourceBuffer) {
// Noop
}
// @ts-expect-error unused function, just used for compile-time typechecking
function testSourceBufferList(x: SourceBufferList) {
assetCompatibleISourceBufferList(x);
}
function assetCompatibleISourceBufferList(_x: ISourceBufferList) {
// Noop
}
/* eslint-enable @typescript-eslint/no-unused-vars, @typescript-eslint/ban-types */

/**
Expand Down
5 changes: 2 additions & 3 deletions src/mse/utils/end_of_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import type {
IMediaSource,
ISourceBuffer,
ISourceBufferList,
} from "../../compat/browser_compatibility_types";
import {
onRemoveSourceBuffers,
Expand All @@ -32,9 +33,7 @@ import TaskCanceller from "../../utils/task_canceller";
* @param {SourceBufferList} sourceBuffers
* @returns {Array.<SourceBuffer>}
*/
function getUpdatingSourceBuffers(
sourceBuffers: SourceBufferList | ISourceBuffer[],
): ISourceBuffer[] {
function getUpdatingSourceBuffers(sourceBuffers: ISourceBufferList): ISourceBuffer[] {
const updatingSourceBuffers: ISourceBuffer[] = [];
for (let i = 0; i < sourceBuffers.length; i++) {
const SourceBuffer = sourceBuffers[i];
Expand Down
4 changes: 2 additions & 2 deletions src/mse/utils/media_source_duration_updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import type {
IMediaSource,
ISourceBuffer,
ISourceBufferList,
} from "../../compat/browser_compatibility_types";
import { onSourceOpen, onSourceEnded, onSourceClose } from "../../compat/event_listeners";
import hasIssuesWithHighMediaSourceDuration from "../../compat/has_issues_with_high_media_source_duration";
Expand Down Expand Up @@ -249,7 +249,7 @@ const enum MediaSourceDurationUpdateStatus {
* @returns {Object}
*/
function createSourceBuffersUpdatingReference(
sourceBuffers: SourceBufferList | ISourceBuffer[],
sourceBuffers: ISourceBufferList,
cancelSignal: CancellationSignal,
): IReadOnlySharedReference<boolean> {
if (sourceBuffers.length === 0) {
Expand Down

0 comments on commit 7512dba

Please sign in to comment.