From cd5421120f6d9ce7917640777a6d01c29e8712d5 Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Tue, 28 May 2024 12:45:29 -0400 Subject: [PATCH] fix race condition with filler noise and also play filler noise when idle and waiting for commands (#763) --- lib/session/call-session.js | 19 +++++++++++++++++++ lib/tasks/gather.js | 20 ++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/session/call-session.js b/lib/session/call-session.js index 77cbd807..85955c3e 100644 --- a/lib/session/call-session.js +++ b/lib/session/call-session.js @@ -990,6 +990,11 @@ class CallSession extends Emitter { ) { try { await this._awaitCommandsOrHangup(); + if (this._isPlayingFillerNoise) { + this._isPlayingFillerNoise = false; + this.ep.api('uuid_break', this.ep.uuid) + .catch((err) => this.logger.info(err, 'Error killing filler noise')); + } if (this.callGone) break; } catch (err) { this.logger.info(err, 'CallSession:exec - error waiting for new commands'); @@ -2427,6 +2432,20 @@ Duration=${duration} ` return new Promise((resolve, reject) => { this.logger.info('_awaitCommandsOrHangup - waiting...'); this.wakeupResolver = resolve; + + /* start filler noise if configured while we wait for new commands */ + if (this.fillerNoise?.url && this.ep?.connected && !this.ep2) { + this.logger.debug('CallSession:_awaitCommandsOrHangup - playing filler noise'); + this._isPlayingFillerNoise = true; + this.ep.play(this.fillerNoise.url); + this.ep.once('playback-start', (evt) => { + if (evt.file === this.fillerNoise.url && !this._isPlayingFillerNoise) { + this.logger.info('CallSession:_awaitCommandsOrHangup - filler noise started'); + this.ep.api('uuid_break', this.ep.uuid) + .catch((err) => this.logger.info(err, 'Error killing filler noise')); + } + }); + } }); } diff --git a/lib/tasks/gather.js b/lib/tasks/gather.js index 3964b0a1..3ae23326 100644 --- a/lib/tasks/gather.js +++ b/lib/tasks/gather.js @@ -274,6 +274,7 @@ class TaskGather extends SttTask { } await this.awaitTaskDone(); + this._killAudio(cs); } catch (err) { this.logger.error(err, 'TaskGather:exec error'); } @@ -703,11 +704,25 @@ class TaskGather extends SttTask { this._finalAsrTimer = null; } + _startFillerNoise() { + this.logger.debug('Gather:_startFillerNoise - playing filler noise'); + this.ep?.play(this.fillerNoise.url); + this._fillerNoiseOn = true; + this.ep.once('playback-start', (evt) => { + if (evt.file === this.fillerNoise.url && !this._fillerNoiseOn) { + this.logger.info({evt}, 'Gather:_startFillerNoise - race condition - kill filler noise here'); + this.ep.api('uuid_break', this.ep.uuid) + .catch((err) => this.logger.info(err, 'Error killing filler noise')); + return; + } else this.logger.debug({evt}, 'Gather:_startFillerNoise - playback started'); + }); + } + _startFillerNoiseTimer() { this._clearFillerNoiseTimer(); this._fillerNoiseTimer = setTimeout(() => { this.logger.debug('Gather:_startFillerNoiseTimer - playing filler noise'); - this.ep?.play(this.fillerNoise.url); + this._startFillerNoise(); }, this.fillerNoise.startDelaySecs * 1000); } @@ -728,6 +743,7 @@ class TaskGather extends SttTask { if (this.ep?.connected && (!this.playComplete || this.hasFillerNoise)) { this.logger.debug('Gather:_killAudio: killing playback of any audio'); this.playComplete = true; + this._fillerNoiseOn = false; // in a race, if we just started audio it may sneak through here this.ep.api('uuid_break', this.ep.uuid) .catch((err) => this.logger.info(err, 'Error killing audio')); } @@ -1090,7 +1106,7 @@ class TaskGather extends SttTask { } else { this.logger.debug(`TaskGather:_resolve - playing filler noise: ${this.fillerNoiseUrl}`); - this.ep.play(this.fillerNoiseUrl); + this._startFillerNoise(); } }