Skip to content

Commit

Permalink
Dedicated recording thread for each detection to prevent mic instance…
Browse files Browse the repository at this point in the history
…s from being used in multiple recordings causing premature recording termination
  • Loading branch information
Kevin Dolan committed Jun 2, 2021
1 parent c158142 commit 54fbbd1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
9 changes: 6 additions & 3 deletions service/DetectionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class DetectionService extends EventEmitter{
this.areNotificationsEnabled = areNotificationsEnabled;

this.toneDetectors = [];
this._recordingThread = new RecordingThread();
this._recordingThread = new RecordingThread({threadId: 0});
}

__processData(decodedData){
Expand Down Expand Up @@ -70,6 +70,9 @@ class DetectionService extends EventEmitter{
log[logLevel](message);

tonesDetector.on('toneDetected', async (result) =>{
const recordingThread = this._recordingThread;
this._recordingThread = new RecordingThread({threadId: recordingThread.threadId + 1});

log.debug(`Processing toneDetected event for ${name}`);
const {matchAverages, message} = result;
const timestamp = new Date().getTime();
Expand All @@ -94,8 +97,8 @@ class DetectionService extends EventEmitter{

if(this.isRecordingEnabled) {
//Start recording in new thread. Post recording notifications sent from new thread
log.debug(`Starting recorder & post recording notification processing`);
this._recordingThread.sendMessage(notificationParams.toObj());
log.debug(`Starting recorder & post recording notification processing. Thread Id: ${recordingThread.threadId}`);
recordingThread.sendMessage(notificationParams.toObj());
}
}

Expand Down
15 changes: 8 additions & 7 deletions service/RecordingThread.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const FORK_OPTIONS = {


class RecordingThread{
constructor() {
constructor({threadId}) {
this.threadId = threadId;
this.__initThread();
}

Expand All @@ -29,22 +30,22 @@ class RecordingThread{

__startWorkerThread(){
this._recordingWorker = new Worker(RECORD_NOTIFY_PATH, {env: SHARE_ENV, workerData: {worker: true}});
this._recordingWorker.on("message", incoming => log.debug(`Message from Recording Worker: ${incoming}`));
this._recordingWorker.on("error", code => new Error(`Recording Worker error with exit code ${code}`));
this._recordingWorker.on("message", incoming => log.debug(`Message from Recording Worker ${this.threadId}: ${incoming}`));
this._recordingWorker.on("error", code => new Error(`Recording Worker ${this.threadId} error with exit code ${code}`));
this._recordingWorker.on("exit", code => {
log.debug(`Worker stopped with exit code ${code}. Restarting`);
log.debug(`Worker ${this.threadId} stopped with exit code ${code}. Restarting`);
this.__initThread();
}
);
}

__startedForkedThread(){
this._child = fork(RECORD_NOTIFY_PATH, [], FORK_OPTIONS);
this._child.on("message", incoming => log.debug(`Message from Recording Fork: ${incoming}`));
this._child.on("message", incoming => log.debug(`Message from Recording Fork ${this.threadId}: ${incoming}`));
this._child.stdout.on("data", data => console.log(data.toString().replace(/(\r\n|\n|\r)/gm, "")));
this._child.on("error", code => new Error(`Recording Worker error with exit code ${code}`));
this._child.on("error", code => new Error(`Recording Worker ${this.threadId} error with exit code ${code}`));
this._child.on("exit", code => {
log.debug(`Worker stopped with exit code ${code}. Restarting`);
log.debug(`Worker ${this.threadId} stopped with exit code ${code}. Restarting`);
this.__initThread();
}
);
Expand Down

0 comments on commit 54fbbd1

Please sign in to comment.