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

[Proposal] Re-export config in v4 and synchronize main thread config and worker config #1510

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
"require": "./dist/commonjs/experimental/index.js",
"default": "./dist/es2017/experimental/index.js"
},
"./experimental/config": {
"import": "./dist/es2017/config.js",
"require": "./dist/commonjs/config.js",
"default": "./dist/es2017/config.js"
},
"./experimental/features": {
"import": "./dist/es2017/experimental/features/index.js",
"require": "./dist/commonjs/experimental/features/index.js",
Expand Down
12 changes: 10 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@
import type { IDefaultConfig } from "./default_config";
import DEFAULT_CONFIG from "./default_config";
import deepMerge from "./utils/deep_merge";
import EventEmitter from "./utils/event_emitter";

class ConfigHandler {
_config = DEFAULT_CONFIG;
interface IConfigHandlerEvents {
update: Partial<IDefaultConfig>;
}

class ConfigHandler extends EventEmitter<IConfigHandlerEvents> {
public updated = false;
private _config = DEFAULT_CONFIG;

update(config: Partial<IDefaultConfig>) {
const newConfig = deepMerge(this._config, config);
this._config = newConfig;
this.updated = true;
this.trigger("update", config);
}

getCurrent(): IDefaultConfig {
Expand Down
5 changes: 5 additions & 0 deletions src/core/main/worker/worker_main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ export default function initializeWorkerMain() {
break;
}

case MainThreadMessageType.ConfigUpdate: {
config.update(msg.value);
break;
}

default:
assertUnreachable(msg);
}
Expand Down
21 changes: 21 additions & 0 deletions src/default_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1195,3 +1195,24 @@ const DEFAULT_CONFIG = {

export type IDefaultConfig = typeof DEFAULT_CONFIG;
export default DEFAULT_CONFIG;

// NOTE Because the config may have to be serialized and shared between several
// environments, we check here that some strict type is respected:
// - only a subset of types are authorized for now, just make it easier to
// reason about.
// - Needs to make sense in JSON: no `function`, no `Date`, no `undefined`...
interface IGenericConfigData {
[key: string]:
| string
| number
| boolean
| number[]
| string[]
| Partial<Record<string, string[]>>
| IGenericConfigData;
}

function checkIsSerializable(_conf: IGenericConfigData): void {
// noop
}
checkIsSerializable(DEFAULT_CONFIG);
17 changes: 17 additions & 0 deletions src/main_thread/api/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ import getStartDate from "../../compat/get_start_date";
import hasMseInWorker from "../../compat/has_mse_in_worker";
import hasWorkerApi from "../../compat/has_worker_api";
import isDebugModeEnabled from "../../compat/is_debug_mode_enabled";
import config from "../../config";
import type { ISegmentSinkMetrics } from "../../core/segment_sinks/segment_buffers_store";
import type {
IAdaptationChoice,
IInbandEvent,
IABRThrottlers,
IBufferType,
} from "../../core/types";
import type { IDefaultConfig } from "../../default_config";
import type { IErrorCode, IErrorType } from "../../errors";
import { ErrorCodes, ErrorTypes, formatError, MediaError } from "../../errors";
import WorkerInitializationError from "../../errors/worker_initialization_error";
Expand Down Expand Up @@ -573,6 +575,21 @@ class Player extends EventEmitter<IPublicAPIEvent> {
},
this._destroyCanceller.signal,
);

const sendConfigUpdates = (updates: Partial<IDefaultConfig>) => {
if (this._priv_worker === null) {
return;
}
log.debug("---> Sending To Worker:", MainThreadMessageType.ConfigUpdate);
this._priv_worker.postMessage({
type: MainThreadMessageType.ConfigUpdate,
value: updates,
});
};
if (config.updated) {
sendConfigUpdates(config.getCurrent());
}
config.addEventListener("update", sendConfigUpdates, this._destroyCanceller.signal);
});
}

Expand Down
9 changes: 9 additions & 0 deletions src/multithread_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
IRepresentationsChoice,
ITrackSwitchingMode,
} from "./core/types";
import type { IDefaultConfig } from "./default_config";
import type {
ISerializedMediaError,
ISerializedNetworkError,
Expand Down Expand Up @@ -158,6 +159,12 @@ export interface ILogLevelUpdateMessage {
};
}

/** Message sent by the main thread to update the Worker's global config. */
export interface IConfigUpdateMessage {
type: MainThreadMessageType.ConfigUpdate;
value: Partial<IDefaultConfig>;
}

/**
* Message sent by the main thread when a new content should be "prepared".
*
Expand Down Expand Up @@ -542,6 +549,7 @@ export const enum MainThreadMessageType {
RemoveTextDataError = "remove-text-error",
CodecSupportUpdate = "codec-support-update",
ContentUrlsUpdate = "urls-update",
ConfigUpdate = "config-update",
DecipherabilityStatusUpdate = "decipherability-update",
LogLevelUpdate = "log-level-update",
MediaSourceReadyStateChange = "media-source-ready-state-change",
Expand All @@ -560,6 +568,7 @@ export const enum MainThreadMessageType {
export type IMainThreadMessage =
| IInitMessage
| ILogLevelUpdateMessage
| IConfigUpdateMessage
| IPrepareContentMessage
| IStopContentMessage
| IStartPreparedContentMessage
Expand Down
Loading