From 9f158a8cf72b4318b381aee66a27d3b0499c9bfc Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Fri, 8 Oct 2021 16:21:47 -0400 Subject: [PATCH] initial WIP to remove freeswitch from media path when not recording or transcribing dial calls --- lib/session/call-session.js | 11 +++++++++++ lib/tasks/dial.js | 28 ++++++++++++++++++++++++++-- lib/utils/place-outdial.js | 12 ++++++++++++ package-lock.json | 12 ++++++------ 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/lib/session/call-session.js b/lib/session/call-session.js index dd1a1df9..2f7c9410 100644 --- a/lib/session/call-session.js +++ b/lib/session/call-session.js @@ -926,6 +926,17 @@ class CallSession extends Emitter { }; } + async releaseMediaToSBC(remoteSdp) { + assert(this.dlg); + assert(this.dlg.connected); + assert(this.ep); + assert(typeof remoteSdp === 'string'); + await this.dlg.modify(remoteSdp); + this.ep.destroy() + .then(() => this.ep = null) + .catch((err) => this.logger.error({err}, 'releaseMediaToSBC: Error destroying endpoint')); + } + /** * 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/tasks/dial.js b/lib/tasks/dial.js index 5d8eff14..c6d6c8e6 100644 --- a/lib/tasks/dial.js +++ b/lib/tasks/dial.js @@ -130,6 +130,8 @@ class TaskDial extends Task { get name() { return TaskName.Dial; } + get canReleaseMedia() { return !this.dtmfHook && !this.listenTask && !this.transcribeTask; } + async exec(cs) { await super.exec(cs); try { @@ -255,7 +257,8 @@ class TaskDial extends Task { debug(`Dial:__initializeInbound allocated ep for incoming call: ${ep.uuid}`); /* send outbound legs back to the same SBC (to support static IP feature) */ - if (!this.proxy) this.proxy = `${cs.req.source_address}:${cs.req.source_port};transport=tcp`; + //if (!this.proxy) this.proxy = `${cs.req.source_address}:${cs.req.source_port};transport=tcp`; + if (!this.proxy) this.proxy = `${cs.req.source_address}:${cs.req.source_port}`; if (this.dialMusic) { // play dial music to caller while we outdial @@ -395,7 +398,7 @@ class TaskDial extends Task { } _connectSingleDial(cs, sd) { - if (!this.bridged) { + if (!this.bridged && !this.canReleaseMedia) { this.logger.debug('Dial:_connectSingleDial bridging endpoints'); if (this.epOther) { this.epOther.api('uuid_break', this.epOther.uuid); @@ -457,6 +460,9 @@ class TaskDial extends Task { if (this.transcribeTask) this.transcribeTask.exec(cs, this.ep); if (this.listenTask) this.listenTask.exec(cs, this.ep); + + /* if we can release the media back to the SBC, do so now */ + if (this.canReleaseMedia) this._releaseMedia(cs, sd); } _bridgeEarlyMedia(sd) { @@ -468,6 +474,24 @@ class TaskDial extends Task { } } + /** + * Release the media from freeswitch + * @param {*} cs + * @param {*} sd + */ + async _releaseMedia(cs, sd) { + assert(cs.ep && sd.ep); + + try { + this.logger.info('Dial:_releaseMedia - releasing media from freewitch since we can'); + const aLegSdp = cs.ep.remote.sdp; + const bLegSdp = sd.ep.remote.sdp; + await Promise.all[sd.releaseMediaToSBC(aLegSdp), cs.releaseMediaToSBC(bLegSdp)]; + this.epOther = null; + } catch (err) { + this.logger.info({err}, 'Dial:_releaseMedia error'); + } + } } module.exports = TaskDial; diff --git a/lib/utils/place-outdial.js b/lib/utils/place-outdial.js index 93939536..05020e9e 100644 --- a/lib/utils/place-outdial.js +++ b/lib/utils/place-outdial.js @@ -62,6 +62,7 @@ class SingleDialer extends Emitter { opts = opts || {}; opts.headers = opts.headers || {}; opts.headers = {...opts.headers, 'X-Call-Sid': this.callSid}; + this.ms = ms; let uri, to; try { switch (this.target.type) { @@ -313,6 +314,17 @@ class SingleDialer extends Emitter { return cs; } + async releaseMediaToSBC(remoteSdp) { + assert(this.dlg); + assert(this.dlg.connected); + assert(this.ep); + assert(typeof remoteSdp === 'string'); + await this.dlg.modify(remoteSdp); + this.ep.destroy() + .then(() => this.ep = null) + .catch((err) => this.logger.error({err}, 'releaseMediaToSBC: Error destroying endpoint')); + } + _notifyCallStatusChange({callStatus, sipStatus, duration}) { assert((typeof duration === 'number' && callStatus === CallStatus.Completed) || (!duration && callStatus !== CallStatus.Completed), diff --git a/package-lock.json b/package-lock.json index 5ed4a2b1..6ebb4721 100644 --- a/package-lock.json +++ b/package-lock.json @@ -592,9 +592,9 @@ } }, "node_modules/ansi-regex": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz", - "integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg==", + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", + "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", "dev": true, "engines": { "node": ">=8" @@ -5239,9 +5239,9 @@ "dev": true }, "ansi-regex": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz", - "integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg==", + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", + "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", "dev": true }, "ansi-styles": {