Skip to content

Commit

Permalink
Updating the log level also impacts the worker
Browse files Browse the repository at this point in the history
  • Loading branch information
peaBerberian committed Sep 1, 2023
1 parent 7cf7a53 commit 1059a82
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/core/api/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,20 @@ class Player extends EventEmitter<IPublicAPIEvent> {
type: "init",
value: {
wasmUrl: workerOptions.dashWasmUrl,
logLevel: log.getLevel(),
},
});
log.addEventListener("onLogLevelChange", (logLevel) => {
if (this._priv_worker === null) {
return;
}
sendMessage(this._priv_worker, {
type: "log-level-update",
value: {
logLevel,
},
});
}, this._destroyCanceller.signal);
} else {
this._priv_worker = null;
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/send_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
IManifestFetcherSettings,
ISegmentFetcherCreatorBackoffOptions,
} from "../core/fetchers";
import { ILoggerLevel } from "../utils/logger";
import { SourceBufferType } from "../worker/media_source_interface";

export default function sendMessage(
Expand Down Expand Up @@ -40,6 +41,14 @@ export interface IInitMessage {
type : "init";
value : {
wasmUrl: string;
logLevel: ILoggerLevel;
};
}

export interface ILogLevelUpdateMessage {
type : "log-level-update";
value : {
logLevel: ILoggerLevel;
};
}

Expand Down Expand Up @@ -136,6 +145,7 @@ export type IReferenceUpdateMessage =

export type IMainThreadMessage =
IInitMessage |
ILogLevelUpdateMessage |
IPrepareContentMessage |
IDisposeContentMessage |
/** The last prepared content can now begin. */
Expand Down
14 changes: 13 additions & 1 deletion src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import EventEmitter from "./event_emitter";
import noop from "./noop";

export type ILoggerLevel = "NONE" |
Expand All @@ -28,11 +29,19 @@ type IConsoleFn = (

const DEFAULT_LOG_LEVEL : ILoggerLevel = "NONE";

/**
* Events sent by `Logger` where the keys are the events' name and the values
* are the corresponding payloads.
*/
interface ILoggerEvents {
onLogLevelChange: ILoggerLevel;
}

/**
* Logger implementation.
* @class Logger
*/
export default class Logger {
export default class Logger extends EventEmitter<ILoggerEvents> {
public error : IConsoleFn;
public warn : IConsoleFn;
public info : IConsoleFn;
Expand All @@ -41,6 +50,7 @@ export default class Logger {
private readonly _levels : Record<ILoggerLevel, number>;

constructor() {
super();
this.error = noop;
this.warn = noop;
this.info = noop;
Expand Down Expand Up @@ -79,6 +89,8 @@ export default class Logger {
noop;
/* eslint-enable no-console */
/* eslint-enable no-invalid-this */

this.trigger("onLogLevelChange", this._currentLevel);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,18 @@ onmessage = function (e: MessageEvent<IMainThreadMessage>) {
const msg = e.data;
switch (msg.type) {
case "init":
log.setLevel(msg.value.logLevel);
parser.initialize({ wasmUrl: msg.value.wasmUrl }).catch((err) => {
const error = err instanceof Error ?
err.toString() :
"Unknown Error";
log.error("Worker: Could not initialize DASH_WASM parser", error);
});
break;
case "log-level-update":
log.setLevel(msg.value.logLevel);
break;

case "prepare":
prepareNewContent(msg.value);
break;
Expand Down

0 comments on commit 1059a82

Please sign in to comment.