From 27d6d323590197bd0bc93d028afd076dee19cff6 Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Wed, 26 Jan 2022 07:37:09 -0500 Subject: [PATCH] bugfix: rtpengine needs to transcode when different codecs are used on A and B legs --- lib/tasks/dial.js | 3 +-- lib/utils/place-outdial.js | 6 ++++-- lib/utils/strip-ancillary-codecs.js | 30 +++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 lib/utils/strip-ancillary-codecs.js diff --git a/lib/tasks/dial.js b/lib/tasks/dial.js index 514824ee..e89810fc 100644 --- a/lib/tasks/dial.js +++ b/lib/tasks/dial.js @@ -554,8 +554,7 @@ class TaskDial extends Task { try { const aLegSdp = cs.ep.remote.sdp; const bLegSdp = sd.dlg.remote.sdp; - this.logger.debug({aLegSdp, bLegSdp}, 'Dial:_releaseMedia - releasing media from freewitch'); - await Promise.all[sd.releaseMediaToSBC(aLegSdp), cs.releaseMediaToSBC(bLegSdp)]; + await Promise.all[sd.releaseMediaToSBC(aLegSdp, cs.ep.local.sdp), cs.releaseMediaToSBC(bLegSdp)]; this.epOther = null; this.logger.info('Dial:_releaseMedia - successfully released media from freewitch'); } catch (err) { diff --git a/lib/utils/place-outdial.js b/lib/utils/place-outdial.js index 7fd1be70..7f415f58 100644 --- a/lib/utils/place-outdial.js +++ b/lib/utils/place-outdial.js @@ -8,6 +8,7 @@ const ConfirmCallSession = require('../session/confirm-call-session'); const AdultingCallSession = require('../session/adulting-call-session'); const deepcopy = require('deepcopy'); const moment = require('moment'); +const stripCodecs = require('./strip-ancillary-codecs'); const { v4: uuidv4 } = require('uuid'); class SingleDialer extends Emitter { @@ -316,9 +317,10 @@ class SingleDialer extends Emitter { return cs; } - async releaseMediaToSBC(remoteSdp) { + async releaseMediaToSBC(remoteSdp, localSdp) { assert(this.dlg && this.dlg.connected && this.ep && typeof remoteSdp === 'string'); - await this.dlg.modify(remoteSdp, { + const sdp = stripCodecs(this.logger, remoteSdp, localSdp) || remoteSdp; + await this.dlg.modify(sdp, { headers: { 'X-Reason': 'release-media' } diff --git a/lib/utils/strip-ancillary-codecs.js b/lib/utils/strip-ancillary-codecs.js new file mode 100644 index 00000000..5442b873 --- /dev/null +++ b/lib/utils/strip-ancillary-codecs.js @@ -0,0 +1,30 @@ +const sdpTransform = require('sdp-transform'); + +const stripCodecs = (logger, remoteSdp, localSdp) => { + try { + const sdp = sdpTransform.parse(remoteSdp); + const local = sdpTransform.parse(localSdp); + const m = local.media + .find((m) => 'audio' === m.type); + const pt = m.rtp[0].payload; + + /* manipulate on the audio section */ + const audio = sdp.media.find((m) => 'audio' === m.type); + + /* discard all of the codecs except the first in our 200 OK, and telephony-events */ + const ptSaves = audio.rtp + .filter((r) => r.codec === 'telephone-event' || r.payload === pt) + .map((r) => r.payload); + const rtp = audio.rtp.filter((r) => ptSaves.includes(r.payload)); + + /* reattach the new rtp sections and stripped payload list */ + audio.rtp = rtp; + audio.payloads = rtp.map((r) => r.payload).join(' '); + return sdpTransform.write(sdp); + } catch (err) { + logger.error({err, remoteSdp, localSdp}, 'strip-ancillary-codecs error'); + } +}; + +module.exports = stripCodecs; +