Skip to content

Commit

Permalink
feat: reduce no. of worker creation for vb
Browse files Browse the repository at this point in the history
  • Loading branch information
raviteja83 authored Feb 24, 2024
1 parent d1fc164 commit 3a1dde7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { HMSAction } from '../../error/HMSAction';
import { EventBus } from '../../events/EventBus';
import { HMSLocalVideoTrack } from '../../media/tracks';
import HMSLogger from '../../utils/logger';
import { workerSleep } from '../../utils/timer-utils';
import { reusableWorker, workerSleep } from '../../utils/timer-utils';
import { HMSPluginUnsupportedTypes } from '../audio';

const DEFAULT_FRAME_RATE = 24;
Expand Down Expand Up @@ -57,6 +57,7 @@ export class HMSVideoPluginsManager {
private pluginNumFramesToSkip: Record<string, number>;
private pluginNumFramesSkipped: Record<string, number>;
private canvases: Array<CanvasElement>; //array of canvases to store intermediate result
private reusableWorker = reusableWorker();

constructor(track: HMSLocalVideoTrack, eventBus: EventBus) {
this.hmsTrack = track;
Expand Down Expand Up @@ -288,7 +289,7 @@ export class HMSVideoPluginsManager {
this.resetCanvases();
}
this.pluginsLoopState = 'paused';
await workerSleep(sleepTimeMs);
await this.reusableWorker.sleep(sleepTimeMs);
continue;
}
let processingTime = 0;
Expand All @@ -306,7 +307,7 @@ export class HMSVideoPluginsManager {
}
this.pluginsLoopState = 'running';
// take into account processing time to decide time to wait for the next loop
await workerSleep(sleepTimeMs - processingTime);
await this.reusableWorker.sleep(sleepTimeMs - processingTime);
}
}

Expand Down
23 changes: 22 additions & 1 deletion packages/hms-video-store/src/utils/timer-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const worker = `(function metronomeWorkerSetup() {
export const worker = `(function workerSetup() {
function ticker() {
self.postMessage('tick');
}
Expand Down Expand Up @@ -47,6 +47,27 @@ export function workerSleep(ms: number): Promise<void> {
});
}

export function reusableWorker() {
if (typeof Worker === 'undefined') {
return {
sleep: (ms: number) => sleep(ms),
};
}
const WorkerThread = new Worker(URL.createObjectURL(new Blob([worker], { type: 'application/javascript' })));
return {
sleep: (ms: number) => {
WorkerThread.postMessage(['start', ms]);
return new Promise<void>(resolve => {
WorkerThread.onmessage = event => {
if (event.data === 'tick') {
resolve();
}
};
});
},
};
}

/**
* Debounce Fn - Function to limit the number of executions of the passed in
* function in a given time duration
Expand Down

0 comments on commit 3a1dde7

Please sign in to comment.