From 2935574440ebc7c5a06cda784841c1e3daa563cc Mon Sep 17 00:00:00 2001 From: Hoan Luu Huu <110280845+xquanluu@users.noreply.github.com> Date: Thu, 31 Aug 2023 09:43:50 +0700 Subject: [PATCH] feat pause resume transcribe (#438) * feat pause resume transcribe * wip * fix jslint * update fsmrf --- lib/session/call-session.js | 25 +++++++++++++++++++++++++ lib/tasks/transcribe.js | 30 +++++++++++++++++++++++++++--- lib/utils/constants.json | 5 +++++ package-lock.json | 14 +++++++------- package.json | 2 +- 5 files changed, 65 insertions(+), 11 deletions(-) diff --git a/lib/session/call-session.js b/lib/session/call-session.js index 0debff40..82b86614 100644 --- a/lib/session/call-session.js +++ b/lib/session/call-session.js @@ -1075,6 +1075,24 @@ ${credential.label ? `, label: ${credential.label}` : ''} is chosen`); listenTask.updateListen(opts.listen_status); } + /** + * perform live call control -- change Transcribe status + * @param {object} opts + * @param {string} opts.transcribe_status - 'pause' or 'resume' + */ + async _lccTranscribeStatus(opts) { + const task = this.currentTask; + if (!task || ![TaskName.Dial, TaskName.Transcribe].includes(task.name)) { + return this.logger.info(`CallSession:_lccTranscribeStatus - invalid transcribe_status in task ${task.name}`); + } + const transcribeTask = task.name === TaskName.Transcribe ? task : task.transcribeTask; + if (!transcribeTask) { + return this.logger + .info('CallSession:_lccTranscribeStatus - invalid transcribe_status: Dial does not have a Transcribe'); + } + transcribeTask.updateTranscribe(opts.transcribe_status); + } + async _lccMuteStatus(callSid, mute) { // this whole thing requires us to be in a Dial or Conference verb const task = this.currentTask; @@ -1193,6 +1211,9 @@ ${credential.label ? `, label: ${credential.label}` : ''} is chosen`); if (opts.listen_status) { await this._lccListenStatus(opts); } + if (opts.transcribe_status) { + await this._lccTranscribeStatus(opts); + } else if (opts.mute_status) { await this._lccMuteStatus(callSid, opts.mute_status === 'mute'); } @@ -1369,6 +1390,10 @@ ${credential.label ? `, label: ${credential.label}` : ''} is chosen`); this._lccListenStatus(data); break; + case 'transcribe:status': + this._lccTranscribeStatus(data); + break; + case 'whisper': this._lccWhisper(data, call_sid); break; diff --git a/lib/tasks/transcribe.js b/lib/tasks/transcribe.js index 45316224..cff2f015 100644 --- a/lib/tasks/transcribe.js +++ b/lib/tasks/transcribe.js @@ -9,7 +9,8 @@ const { SonioxTranscriptionEvents, IbmTranscriptionEvents, NvidiaTranscriptionEvents, - JambonzTranscriptionEvents + JambonzTranscriptionEvents, + TranscribeStatus } = require('../utils/constants'); const { normalizeJambones } = require('@jambonz/verb-specifications'); const SttTask = require('./stt-task'); @@ -119,8 +120,7 @@ class TaskTranscribe extends SttTask { this.removeSpeechListeners(ep); } - async kill(cs) { - super.kill(cs); + async _stopTranscription() { let stopTranscription = false; if (this.ep?.connected) { stopTranscription = true; @@ -132,6 +132,13 @@ class TaskTranscribe extends SttTask { this.ep2.stopTranscription({vendor: this.vendor}) .catch((err) => this.logger.info(err, 'Error TaskTranscribe:kill')); } + + return stopTranscription; + } + + async kill(cs) { + super.kill(cs); + const stopTranscription = this._stopTranscription(); // hangup after 1 sec if we don't get a final transcription if (stopTranscription) this._timer = setTimeout(() => this.notifyTaskDone(), 1500); else this.notifyTaskDone(); @@ -139,6 +146,23 @@ class TaskTranscribe extends SttTask { await this.awaitTaskDone(); } + async updateTranscribe(status) { + if (!this.killed && this.ep && this.ep.connected) { + this.logger.info(`TaskTranscribe:updateTranscribe status ${status}`); + switch (status) { + case TranscribeStatus.Pause: + await this._stopTranscription(); + break; + case TranscribeStatus.Resume: + await this._startTranscribing(this.cs, this.ep, 1); + if (this.separateRecognitionPerChannel && this.ep2) { + await this._startTranscribing(this.cs, this.ep2, 2); + } + break; + } + } + } + async _startTranscribing(cs, ep, channel) { const opts = this.setChannelVarsForStt(this, this.sttCredentials, this.data.recognizer); switch (this.vendor) { diff --git a/lib/utils/constants.json b/lib/utils/constants.json index 7287ef02..6df82943 100644 --- a/lib/utils/constants.json +++ b/lib/utils/constants.json @@ -51,6 +51,11 @@ "Silence": "silence", "Resume": "resume" }, + "TranscribeStatus": { + "Pause": "pause", + "Silence": "silence", + "Resume": "resume" + }, "TaskPreconditions": { "None": "none", "Endpoint": "endpoint", diff --git a/package-lock.json b/package-lock.json index b414fa7c..a97bdc2d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -30,7 +30,7 @@ "bent": "^7.3.12", "debug": "^4.3.4", "deepcopy": "^2.1.0", - "drachtio-fsmrf": "^3.0.23", + "drachtio-fsmrf": "^3.0.24", "drachtio-srf": "^4.5.26", "express": "^4.18.2", "ip": "^1.1.8", @@ -5152,9 +5152,9 @@ } }, "node_modules/drachtio-fsmrf": { - "version": "3.0.23", - "resolved": "https://registry.npmjs.org/drachtio-fsmrf/-/drachtio-fsmrf-3.0.23.tgz", - "integrity": "sha512-ElruNKuPzFiMOUH06PUd3dR9tElEGRhbP/gXxai58qhrqRQNLJxzCRJkbgbjrZdYWFQPHOAzy4ZQb7+qq0AUPw==", + "version": "3.0.24", + "resolved": "https://registry.npmjs.org/drachtio-fsmrf/-/drachtio-fsmrf-3.0.24.tgz", + "integrity": "sha512-MsTwHDOOg5yrKCcQ2buNTVSEwoMZzTcVeU5/BZ2Km0TPxBI1lErW6IiyZxA5pyoLfSdBtbqSh9ZwXrtxWTp6kA==", "dependencies": { "camel-case": "^4.1.2", "debug": "^2.6.9", @@ -14610,9 +14610,9 @@ } }, "drachtio-fsmrf": { - "version": "3.0.23", - "resolved": "https://registry.npmjs.org/drachtio-fsmrf/-/drachtio-fsmrf-3.0.23.tgz", - "integrity": "sha512-ElruNKuPzFiMOUH06PUd3dR9tElEGRhbP/gXxai58qhrqRQNLJxzCRJkbgbjrZdYWFQPHOAzy4ZQb7+qq0AUPw==", + "version": "3.0.24", + "resolved": "https://registry.npmjs.org/drachtio-fsmrf/-/drachtio-fsmrf-3.0.24.tgz", + "integrity": "sha512-MsTwHDOOg5yrKCcQ2buNTVSEwoMZzTcVeU5/BZ2Km0TPxBI1lErW6IiyZxA5pyoLfSdBtbqSh9ZwXrtxWTp6kA==", "requires": { "camel-case": "^4.1.2", "debug": "^2.6.9", diff --git a/package.json b/package.json index dc937496..0a8db9c1 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "bent": "^7.3.12", "debug": "^4.3.4", "deepcopy": "^2.1.0", - "drachtio-fsmrf": "^3.0.23", + "drachtio-fsmrf": "^3.0.24", "drachtio-srf": "^4.5.26", "express": "^4.18.2", "ip": "^1.1.8",