Skip to content

Commit

Permalink
Fix remaining CoreInterface issues
Browse files Browse the repository at this point in the history
  • Loading branch information
peaBerberian committed Jan 21, 2025
1 parent 57f4edd commit 1eca46b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 115 deletions.
19 changes: 11 additions & 8 deletions src/core/main/worker/content_preparer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import type {
import TaskCanceller from "../../../utils/task_canceller";
import type { IRepresentationEstimator } from "../../adaptive";
import createAdaptiveRepresentationSelector from "../../adaptive";
import type { IRepresentationEstimatorThrottlers } from "../../adaptive/adaptive_representation_selector";
import CmcdDataBuilder from "../../cmcd";
import type { IManifestRefreshSettings } from "../../fetchers";
import { ManifestFetcher, SegmentQueueCreator } from "../../fetchers";
import SegmentSinksStore from "../../segment_sinks";
import type { INeedsMediaSourceReloadPayload } from "../../stream";
import DecipherabilityFreezeDetector from "../common/DecipherabilityFreezeDetector";
import { limitVideoResolution, throttleVideoBitrate } from "./globals";
import TrackChoiceSetter from "./track_choice_setter";
import { formatErrorForSender } from "./utils";
import WorkerTextDisplayerInterface from "./worker_text_displayer_interface";
Expand Down Expand Up @@ -75,6 +75,8 @@ export default class ContentPreparer {
public initializeNewContent(
sendMessage: (msg: IWorkerMessage, transferables?: Transferable[]) => void,
context: IContentInitializationData,
/** Allows to filter which Representations can be choosen. */
throttlers: IRepresentationEstimatorThrottlers,
): Promise<IManifest> {
return new Promise((res, rej) => {
this.disposeCurrentContent();
Expand All @@ -89,9 +91,13 @@ export default class ContentPreparer {

const transportFn = features.transports[transport];
if (typeof transportFn !== "function") {
// Stop previous content and reset its state
// XXX TODO: send fatal error
throw new Error(`transport "${transport}" not supported`);
rej(
new Error(
`transport "${transport}" not supported. ` +
"Did you add the corresponding feature?",
),
);
return;
}
const representationFilter =
typeof transportOptions.representationFilter === "string"
Expand All @@ -118,10 +124,7 @@ export default class ContentPreparer {
video: context.initialVideoBitrate ?? 0,
},
lowLatencyMode: transportOptions.lowLatencyMode,
throttlers: {
limitResolution: { video: limitVideoResolution },
throttleBitrate: { video: throttleVideoBitrate },
},
throttlers,
});

const unbindRejectOnCancellation = currentMediaSourceCanceller.signal.register(
Expand Down
38 changes: 0 additions & 38 deletions src/core/main/worker/globals.ts

This file was deleted.

74 changes: 53 additions & 21 deletions src/core/main/worker/worker_main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,30 @@ export type IMessageReceiverCallback = (evt: { data: IMainThreadMessage }) => vo
* receive messages coming from the "main thread" part of the RxPlayer logic.
* @param {Function} sendMessage - Function allowing to send messages to the
* "main thread" part of the RxPlayer logic.
* @param {Object} refs - Collection of so-called "references": values
* configuring playback that may be updated at any time and that the WorkerMain
* should react on.
*/
export default function initializeWorkerMain(
setMessageReceiver: (cb: IMessageReceiverCallback) => void,
sendMessage: (msg: IWorkerMessage, transferables?: Transferable[]) => void,
refs: ICoreReferences,
): void {
const {
DEFAULT_WANTED_BUFFER_AHEAD,
DEFAULT_MAX_VIDEO_BUFFER_SIZE,
DEFAULT_MAX_BUFFER_AHEAD,
DEFAULT_MAX_BUFFER_BEHIND,
} = config.getCurrent();
const refs: ICoreReferences = {
wantedBufferAhead: new SharedReference(DEFAULT_WANTED_BUFFER_AHEAD),
maxVideoBufferSize: new SharedReference(DEFAULT_MAX_VIDEO_BUFFER_SIZE),
maxBufferAhead: new SharedReference(DEFAULT_MAX_BUFFER_AHEAD),
maxBufferBehind: new SharedReference(DEFAULT_MAX_BUFFER_BEHIND),
limitVideoResolution: new SharedReference<IResolutionInfo>({
height: undefined,
width: undefined,
pixelRatio: 1,
}),
throttleVideoBitrate: new SharedReference(Infinity),
};

/**
* `true` once the worker has been initialized.
* Allow to enforce the fact that it is only initialized once.
Expand Down Expand Up @@ -130,7 +145,7 @@ export default function initializeWorkerMain(
break;

case MainThreadMessageType.PrepareContent:
prepareNewContent(sendMessage, contentPreparer, msg.value);
prepareNewContent(sendMessage, contentPreparer, msg.value, refs);
break;

case MainThreadMessageType.StartPreparedContent: {
Expand Down Expand Up @@ -434,28 +449,37 @@ export default function initializeWorkerMain(
* @param {ContentPreparer} contentPreparer
* @param {Object} contentInitData - Configuration wanted for the content to
* load.
* @param {Object} refs - Collection of so-called "references": values
* configuring playback that may be updated at any time and that the
* WorkerMain should react on.
*/
function prepareNewContent(
sendMessage: (msg: IWorkerMessage, transferables?: Transferable[]) => void,
contentPreparer: ContentPreparer,
contentInitData: IContentInitializationData,
refs: ICoreReferences,
): void {
contentPreparer.initializeNewContent(sendMessage, contentInitData).then(
(manifest) => {
sendMessage({
type: WorkerMessageType.ManifestReady,
contentId: contentInitData.contentId,
value: { manifest },
});
},
(err: unknown) => {
sendMessage({
type: WorkerMessageType.Error,
contentId: contentInitData.contentId,
value: formatErrorForSender(err),
});
},
);
contentPreparer
.initializeNewContent(sendMessage, contentInitData, {
limitResolution: { video: refs.limitVideoResolution },
throttleBitrate: { video: refs.throttleVideoBitrate },
})
.then(
(manifest) => {
sendMessage({
type: WorkerMessageType.ManifestReady,
contentId: contentInitData.contentId,
value: { manifest },
});
},
(err: unknown) => {
sendMessage({
type: WorkerMessageType.Error,
contentId: contentInitData.contentId,
value: formatErrorForSender(err),
});
},
);
}

function updateCoreReference(msg: IReferenceUpdateMessage, refs: ICoreReferences): void {
Expand Down Expand Up @@ -987,11 +1011,19 @@ function sendSegmentSinksStoreInfos(
});
}

/**
* Collection of so-called "references": values configuring playback that may
* be updated at any time and that the WorkerMain should react on.
*/
export interface ICoreReferences {
limitVideoResolution: SharedReference<IResolutionInfo>;
/** Max buffer size after the current position, in seconds (we GC further up). */
maxBufferAhead: SharedReference<number>;
/** Max buffer size before the current position, in seconds (we GC further down). */
maxBufferBehind: SharedReference<number>;
/** Buffer maximum size in kiloBytes at which we stop downloading */
maxVideoBufferSize: SharedReference<number>;
throttleVideoBitrate: SharedReference<number>;
/** Buffer "goal" at which we stop downloading new segments. */
wantedBufferAhead: SharedReference<number>;
}
33 changes: 7 additions & 26 deletions src/main_thread/api/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import type {
IInbandEvent,
IABRThrottlers,
IBufferType,
IResolutionInfo,
} from "../../core/types";
import { MonoThreadCoreInterface, WorkerCoreInterface } from "../../core_interface";
import type { IDefaultConfig } from "../../default_config";
Expand Down Expand Up @@ -823,27 +822,22 @@ class Player extends EventEmitter<IPublicAPIEvent> {
limitResolution: {},
};

let throttleVideoBitrate: IReadOnlySharedReference<number> = new SharedReference(
Infinity,
);

if (this._priv_throttleVideoBitrateWhenHidden) {
if (!relyOnVideoVisibilityAndSize) {
log.warn(
"API: Can't apply throttleVideoBitrateWhenHidden because " +
"browser can't be trusted for visibility.",
);
} else {
throttleVideoBitrate = createMappedReference(
getVideoVisibilityRef(
this._priv_pictureInPictureRef,
throttlers.throttleBitrate = {
video: createMappedReference(
getVideoVisibilityRef(
this._priv_pictureInPictureRef,
currentContentCanceller.signal,
),
(isActive) => (isActive ? Infinity : 0),
currentContentCanceller.signal,
),
(isActive) => (isActive ? Infinity : 0),
currentContentCanceller.signal,
);
throttlers.throttleBitrate = {
video: throttleVideoBitrate,
};
}
}
Expand Down Expand Up @@ -930,19 +924,6 @@ class Player extends EventEmitter<IPublicAPIEvent> {
initializeWorkerMain(
coreInterfaceCallbacks.setCoreMessageReceiver,
coreInterfaceCallbacks.sendCoreMessage,
{
// XXX TODO:
limitVideoResolution: new SharedReference<IResolutionInfo>({
height: undefined,
width: undefined,
pixelRatio: 1,
}),
maxBufferAhead: bufferOptions.maxBufferAhead,
maxBufferBehind: bufferOptions.maxBufferBehind,
maxVideoBufferSize: bufferOptions.maxVideoBufferSize,
throttleVideoBitrate: new SharedReference(Infinity),
wantedBufferAhead: bufferOptions.wantedBufferAhead,
},
);
coreInterface.sendMessage({
type: MainThreadMessageType.Init,
Expand Down
25 changes: 3 additions & 22 deletions src/worker_entry_point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@
*/

import initializeWorkerMain from "./core/main/worker";
import {
limitVideoResolution,
maxBufferAhead,
maxBufferBehind,
maxVideoBufferSize,
throttleVideoBitrate,
wantedBufferAhead,
} from "./core/main/worker/globals";
import log from "./experimental/tools/mediaCapabilitiesProber/log";
import features from "./features";
import Manifest from "./manifest/classes";
Expand All @@ -31,20 +23,9 @@ features.transports.dash = createDashPipelines;
globalScope.onmessageerror = (_msg: MessageEvent) => {
log.error("Worker: Error when receiving message from main thread.");
};
initializeWorkerMain(
(handler) => {
onmessage = handler;
},
sendMessage,
{
limitVideoResolution,
maxBufferAhead,
maxBufferBehind,
maxVideoBufferSize,
throttleVideoBitrate,
wantedBufferAhead,
},
);
initializeWorkerMain((handler) => {
onmessage = handler;
}, sendMessage);

/**
* Perform a `postMessage` to main thread with the given message.
Expand Down

0 comments on commit 1eca46b

Please sign in to comment.