From 673eebcb2fc8855013dddff939c9ee87bebac684 Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Mon, 9 Mar 2020 21:55:41 +0000 Subject: [PATCH] address general case of sending completed status when we hangup the call --- lib/session/call-session.js | 18 ++++++++++++++++++ lib/session/inbound-call-session.js | 2 +- lib/session/rest-call-session.js | 10 +--------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/session/call-session.js b/lib/session/call-session.js index fb7cf8eb..681fb43f 100644 --- a/lib/session/call-session.js +++ b/lib/session/call-session.js @@ -434,6 +434,7 @@ class CallSession extends Emitter { uas.callSid = this.callSid; uas.connectTime = moment(); this.dlg = uas; + this.wrapDialog(this.dlg); this.emit('callStatusChange', {sipStatus: 200, callStatus: CallStatus.InProgress}); this.logger.debug('CallSession:_evalEndpointPrecondition - answered call'); }; @@ -556,6 +557,23 @@ class CallSession extends Emitter { return {ms: this.ms, ep: this.ep}; } + /** + * Call this whenever we answer the A leg, creating a dialog + * It wraps the 'destroy' method such that if we hang up the A leg + * (e.g. via 'hangup' verb) we emit a callStatusChange event + * @param {SipDialog} dlg + */ + wrapDialog(dlg) { + dlg.connectTime = moment(); + const origDestroy = dlg.destroy.bind(dlg); + dlg.destroy = () => { + const duration = moment().diff(this.dlg.connectTime, 'seconds'); + this.emit('callStatusChange', {callStatus: CallStatus.Completed, duration}); + this.logger.debug('CallSession: call terminated by jambones'); + origDestroy(); + }; + } + /** * Called any time call status changes. This method both invokes the * call_status_hook callback as well as updates the realtime database diff --git a/lib/session/inbound-call-session.js b/lib/session/inbound-call-session.js index a063ffdf..8177c7be 100644 --- a/lib/session/inbound-call-session.js +++ b/lib/session/inbound-call-session.js @@ -46,7 +46,7 @@ class InboundCallSession extends CallSession { if (!this.dlg) { assert(this.ep); this.dlg = await this.srf.createUAS(this.req, this.res, {localSdp: this.ep.local.sdp}); - this.dlg.connectTime = moment(); + this.wrapDialog(this.dlg); this.dlg.on('destroy', this._callerHungup.bind(this)); this.emit('callStatusChange', {sipStatus: 200, callStatus: CallStatus.InProgress}); this.logger.debug(`CallSession:propagateAnswer - answered callSid ${this.callSid}`); diff --git a/lib/session/rest-call-session.js b/lib/session/rest-call-session.js index 113ed381..143bd7d2 100644 --- a/lib/session/rest-call-session.js +++ b/lib/session/rest-call-session.js @@ -31,15 +31,7 @@ class RestCallSession extends CallSession { setDialog(dlg) { this.dlg = dlg; dlg.on('destroy', this._callerHungup.bind(this)); - dlg.connectTime = moment(); - - const origDestroy = dlg.destroy.bind(dlg); - dlg.destroy = () => { - const duration = moment().diff(this.dlg.connectTime, 'seconds'); - this.emit('callStatusChange', {callStatus: CallStatus.Completed, duration}); - this.logger.debug('RestCallSession: call terminated by jambones'); - origDestroy(); - }; + this.wrapDialog(dlg); } /**