Skip to content

Commit

Permalink
Synchronize config between main and worker
Browse files Browse the repository at this point in the history
  • Loading branch information
peaBerberian committed Sep 4, 2024
1 parent 96f8de7 commit 896f58e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
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

0 comments on commit 896f58e

Please sign in to comment.