changes for updateCall pause/resume listen audio

This commit is contained in:
Dave Horton
2020-02-08 14:16:05 -05:00
parent 3ca2d982cc
commit ff531e6964
12 changed files with 140 additions and 26 deletions

View File

@@ -1,13 +1,42 @@
const router = require('express').Router();
const sysError = require('./error');
const sessionTracker = require('../../session/session-tracker');
const {DbErrorBadRequest, DbErrorUnprocessableRequest} = require('../utils/errors');
const {CallStatus, CallDirection} = require('../../utils/constants');
/**
* validate the payload and retrieve the CallSession for the CallSid
*/
function retrieveCallSession(callSid, opts) {
if (opts.call_status_hook && !opts.call_hook) {
throw new DbErrorBadRequest('call_status_hook can be updated only when call_hook is also being updated');
}
const cs = sessionTracker.get(callSid);
if (opts.call_status === CallStatus.Completed && !cs.hasStableDialog) {
throw new DbErrorUnprocessableRequest('current call state is incompatible with requested action');
}
else if (opts.call_status === CallStatus.NoAnswer) {
if (cs.direction === CallDirection.Outbound) {
if (!cs.isOutboundCallRinging) {
throw new DbErrorUnprocessableRequest('current call state is incompatible with requested action');
}
}
else {
if (cs.isInboundCallAnswered) {
throw new DbErrorUnprocessableRequest('current call state is incompatible with requested action');
}
}
}
return cs;
}
router.post('/:callSid', async(req, res) => {
const logger = req.app.locals.logger;
const callSid = req.params.callSid;
logger.debug({body: req.body}, 'got upateCall request');
try {
const cs = sessionTracker.get(callSid);
const cs = retrieveCallSession(callSid, req.body);
if (!cs) {
logger.info(`updateCall: callSid not found ${callSid}`);
return res.sendStatus(404);

View File

@@ -6,7 +6,7 @@ const retrieveApp = require('./utils/retrieve-app');
const parseUrl = require('parse-url');
module.exports = function(srf, logger) {
const {lookupAppByPhoneNumber} = srf.locals.dbHelpers;
const {lookupAppByPhoneNumber, lookupApplicationBySid} = srf.locals.dbHelpers;
function initLocals(req, res, next) {
const callSid = uuidv4();
@@ -14,6 +14,11 @@ module.exports = function(srf, logger) {
callSid,
logger: logger.child({callId: req.get('Call-ID'), callSid})
};
if (req.has('X-Application-Sid')) {
const application_sid = req.get('X-Application-Sid');
req.locals.logger.debug(`got application from X-Application-Sid header: ${application_sid}`);
req.locals.application_sid = application_sid;
}
next();
}
@@ -44,7 +49,13 @@ module.exports = function(srf, logger) {
async function retrieveApplication(req, res, next) {
const logger = req.locals.logger;
try {
const app = await lookupAppByPhoneNumber(req.locals.calledNumber);
let app;
if (req.locals.application_sid) {
app = await lookupApplicationBySid(req.locals.application_sid);
}
else {
app = await lookupAppByPhoneNumber(req.locals.calledNumber);
}
if (!app || !app.call_hook || !app.call_hook.url) {
logger.info(`rejecting call to ${req.locals.calledNumber}: no application or webhook url`);
return res.send(480, {

View File

@@ -1,6 +1,6 @@
const Emitter = require('events');
const config = require('config');
const {CallDirection, TaskPreconditions, CallStatus} = require('../utils/constants');
const {CallDirection, TaskPreconditions, CallStatus, TaskName} = require('../utils/constants');
const hooks = require('../utils/notifiers');
const moment = require('moment');
const assert = require('assert');
@@ -63,6 +63,18 @@ class CallSession extends Emitter {
return this.application.speech_recognizer_language;
}
get hasStableDialog() {
return this.dlg && this.dlg.connected;
}
get isOutboundCallRinging() {
return this.direction === CallDirection.Outbound && this.req && !this.dlg;
}
get isInboundCallAnswered() {
return this.direction === CallDirection.Inbound && this.res.finalResponseSent;
}
async exec() {
this.logger.info(`CallSession:exec starting task list with ${this.tasks.length} tasks`);
while (this.tasks.length && !this.callGone) {
@@ -128,18 +140,45 @@ class CallSession extends Emitter {
async updateCall(opts) {
this.logger.debug(opts, 'CallSession:updateCall');
if (opts.call_status === 'completed' && this.dlg) {
if (opts.call_status === CallStatus.Completed && this.dlg) {
this.logger.info('CallSession:updateCall hanging up call due to request from api');
this._callerHungup();
}
else if (opts.call_status === CallStatus.NoAnswer) {
if (this.direction === CallDirection.Inbound) {
if (this.res && !this.res.finalResponseSent) {
this.res.send(503);
this._callReleased();
}
}
else {
if (this.req && !this.dlg) {
this.req.cancel();
this._callReleased();
}
}
}
else if (opts.call_hook && opts.call_hook.url) {
const hook = this.normalizeUrl(opts.call_hook.url, opts.call_hook.method, opts.call_hook.auth);
this.logger.info({hook}, 'CallSession:updateCall replacing application due to request from api');
const {actionHook} = hooks(this.logger, this.callInfo);
if (opts.call_status_hook) this.call_status_hook = opts.call_status_hook;
const tasks = await actionHook(hook);
this.logger.info({tasks}, 'CallSession:updateCall new task list');
this.replaceApplication(tasks);
}
else if (opts.listen_status) {
const task = this.currentTask;
if (!task || ![TaskName.Dial, TaskName.Listen].includes(task.name)) {
return this.logger.info(`CallSession:updateCall - disregarding listen_status in task ${task.name}`);
}
const listenTask = task.name === TaskName.Listen ? task : task.listenTask;
if (!listenTask) {
return this.logger.info('CallSession:updateCall - disregarding listen_status as Dial does not have a listen');
}
listenTask.updateListen(opts.listen_status);
}
}
/**
@@ -262,6 +301,7 @@ class CallSession extends Emitter {
}
return {ms: this.ms, ep: this.ep};
}
_notifyCallStatusChange({callStatus, sipStatus, duration}) {
assert((typeof duration === 'number' && callStatus === CallStatus.Completed) ||
(!duration && callStatus !== CallStatus.Completed),

View File

@@ -26,7 +26,7 @@ class InboundCallSession extends CallSession {
this.logger.info('InboundCallSession:_onTasksDone auto-generating non-success response to invite');
this.res.send(603);
}
else if (this.dlg.connected) {
else if (this.dlg && this.dlg.connected) {
assert(this.dlg.connectTime);
const duration = moment().diff(this.dlg.connectTime, 'seconds');
this.emit('callStatusChange', {callStatus: CallStatus.Completed, duration});

View File

@@ -1,5 +1,5 @@
const Task = require('./task');
const {TaskName, TaskPreconditions, ListenEvents} = require('../utils/constants');
const {TaskName, TaskPreconditions, ListenEvents, ListenStatus} = require('../utils/constants');
const makeTask = require('./make_task');
const moment = require('moment');
@@ -68,6 +68,23 @@ class TaskListen extends Task {
this.notifyTaskDone();
}
updateListen(status) {
if (!this.killed && this.ep && this.ep.connected) {
this.logger.info(`TaskListen:updateListen status ${status}`);
switch (status) {
case ListenStatus.Pause:
this.ep.forkAudioPause().catch((err) => this.logger.info(err, 'TaskListen: error pausing audio'));
break;
case ListenStatus.Silence:
this.ep.forkAudioPause().catch((err) => this.logger.info(err, 'TaskListen: error pausing audio'));
break;
case ListenStatus.Resume:
this.ep.forkAudioResume().catch((err) => this.logger.info(err, 'TaskListen: error resuming audio'));
break;
}
}
}
async _playBeep(ep) {
await ep.play('tone_stream://L=1;%(500, 0, 1500)')
.catch((err) => this.logger.info(err, 'TaskListen:_playBeep Error playing beep'));

View File

@@ -7,6 +7,7 @@ class TaskSay extends Task {
this.preconditions = TaskPreconditions.Endpoint;
this.text = this.data.text;
this.loop = this.data.loop || 1;
this.earlyMedia = this.data.earlyMedia === true || (parentTask && parentTask.earlyMedia);
if (this.data.synthesizer) {
this.voice = this.data.synthesizer.voice;
@@ -26,11 +27,14 @@ class TaskSay extends Task {
super.exec(cs);
this.ep = ep;
try {
await ep.speak({
ttsEngine: 'google_tts',
voice: this.voice || this.callSession.speechSynthesisVoice,
text: this.text
});
while (!this.killed && this.loop--) {
this.logger.debug(`TaskSay: remaining loops ${this.loop}`);
await ep.speak({
ttsEngine: 'google_tts',
voice: this.voice || this.callSession.speechSynthesisVoice,
text: this.text
});
}
} catch (err) {
this.logger.info(err, 'TaskSay:exec error');
}

View File

@@ -30,6 +30,11 @@
"Inbound": "inbound",
"Outbound": "outbound"
},
"ListenStatus": {
"Pause": "pause",
"Silence": "silence",
"Resume": "resume"
},
"TaskPreconditions": {
"None": "none",
"Endpoint": "endpoint",

View File

@@ -6,7 +6,10 @@ const PORT = process.env.HTTP_PORT || config.get('defaultHttpPort');
function installSrfLocals(srf, logger) {
if (srf.locals.dbHelpers) return;
const {lookupAppByPhoneNumber} = require('jambonz-db-helpers')(config.get('mysql'), logger);
const {
lookupAppByPhoneNumber,
lookupApplicationBySid
} = require('jambonz-db-helpers')(config.get('mysql'), logger);
const {
updateCallStatus,
retrieveCall,
@@ -17,6 +20,7 @@ function installSrfLocals(srf, logger) {
Object.assign(srf.locals, {
dbHelpers: {
lookupAppByPhoneNumber,
lookupApplicationBySid,
updateCallStatus,
retrieveCall,
listCalls,