From 1cd526c6846ad27c19f642a9a331105f7eaf1749 Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Tue, 19 Nov 2024 09:38:19 -0500 Subject: [PATCH] wip (#182) --- app.js | 4 ++++ lib/call-session.js | 49 +++++++++++++++++++++++++++++++++++++++------ lib/constants.json | 5 +++++ lib/utils.js | 10 ++++++++- 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/app.js b/app.js index 343962c..6af4038 100644 --- a/app.js +++ b/app.js @@ -67,6 +67,8 @@ const { }, logger); const { client: redisClient, + addKey, + retrieveKey, createSet, retrieveSet, addToSet, @@ -108,6 +110,8 @@ srf.locals = {...srf.locals, lookupSystemInformation }, realtimeDbHelpers: { + addKey, + retrieveKey, createSet, incrKey, decrKey, diff --git a/lib/call-session.js b/lib/call-session.js index b192c32..04b131e 100644 --- a/lib/call-session.js +++ b/lib/call-session.js @@ -7,8 +7,11 @@ const { nudgeCallCounts, roundTripTime, parseConnectionIp, - isPrivateVoipNetwork + isPrivateVoipNetwork, + makeFullMediaReleaseKey, + makePartnerFullMediaReleaseKey } = require('./utils'); +const { MediaPath } = require('./constants.json'); const {forwardInDialogRequests} = require('drachtio-fn-b2b-sugar'); const {parseUri, stringifyUri, SipError} = require('drachtio-srf'); @@ -61,7 +64,10 @@ class CallSession extends Emitter { this.activeCallIds = this.srf.locals.activeCallIds; this.decrKey = req.srf.locals.realtimeDbHelpers.decrKey; - this._mediaReleased = false; + this.addKey = req.srf.locals.realtimeDbHelpers.addKey; + this.retrieveKey = req.srf.locals.realtimeDbHelpers.retrieveKey; + + this._mediaPath = MediaPath.FullMedia; this.application_sid = req.locals.application_sid; this.account_sid = req.locals.account_sid; @@ -79,7 +85,7 @@ class CallSession extends Emitter { } get isMediaReleased() { - return this._mediaReleased; + return this._mediaPath !== MediaPath.FullMedia; } get isFive9VoiceStream() { @@ -392,6 +398,14 @@ class CallSession extends Emitter { trace_id: uac.res?.get('X-Trace-ID') || '00000000000000000000000000000000' }; } + + /* save far end SDP for later use if we do a full media release */ + if (process.env.JAMBONES_ENABLE_FULL_MEDIA_RELEASE) { + const key = makeFullMediaReleaseKey(this.req.get('Call-ID')); + const sdp = this.req.body; + this.logger.info({key, sdp}, 'saving far end sdp for full media release feature'); + this.addKey(key, sdp, 3600).catch((err) => this.logger.error(err, 'Error saving far end sdp')); + } this.uas = uas; this.uac = uac; [uas, uac].forEach((dlg) => { @@ -605,6 +619,7 @@ Duration=${payload.duration} ` const toTag = dlg.type === 'uas' ? this.rtpEngineOpts.uac.tag : this.rtpEngineOpts.uas.tag; const reason = req.get('X-Reason'); const isReleasingMedia = reason && dlg.type === 'uac' && ['release-media', 'anchor-media'].includes(reason); + const isFullMediaRelease = reason === 'release-media-entirely' && process.env.JAMBONES_ENABLE_FULL_MEDIA_RELEASE; const offerMedia = dlg.type === 'uas' ? this.rtpEngineOpts.uac.mediaOpts : this.rtpEngineOpts.uas.mediaOpts; const answerMedia = dlg.type === 'uas' ? this.rtpEngineOpts.uas.mediaOpts : this.rtpEngineOpts.uac.mediaOpts; const direction = dlg.type === 'uas' ? ['public', 'private'] : ['private', 'public']; @@ -666,6 +681,26 @@ Duration=${payload.duration} ` } return; } + + if (isFullMediaRelease) { + const b_sdp = await this.retrieveKey(makePartnerFullMediaReleaseKey(this.req.get('Call-ID'))); + this.logger.info({b_sdp}, 'reinvite ourselves out of the media path with this reinvite offer'); + const answerSdp = await dlg.other.modify(b_sdp); + this.logger.info({answerSdp}, 'far end response to full media release'); + res.send(200, { + body: dlg.local.sdp, + headers: { + 'Contact': this.contactHeader + } + }); + /* no media going through us now we can destroy the rtpengine resource */ + this.rtpEngineResource.destroy().catch((err) => { + this.logger.info({err}, 'Error destroying rtpengine resource after full media release'); + }); + this._mediaPath = MediaPath.NoMedia; + return; + } + const offeredSdp = Array.isArray(req.payload) && req.payload.length > 1 ? req.payload.find((p) => p.type === 'application/sdp').content : req.body; @@ -695,12 +730,14 @@ Duration=${payload.duration} ` let sdp; //HL 2024-11-13: previously forwarded re-invites to webrtc clients but further testing has shown to be unnecessary //if (isReleasingMedia && !this.callerIsUsingSrtp) { - if (isReleasingMedia) { - this.logger.info({response}, `got a reinvite from FS to ${reason}`); + + //DH 2024-11- 18: if we are going from no-media to either partial or full media, we need reinvite the far end + if (isReleasingMedia && this._mediaPath !== MediaPath.NoMedia) { sdp = dlg.other.remote.sdp; if (!answerMedia.flags.includes('asymmetric')) answerMedia.flags.push('asymmetric'); answerMedia.flags = answerMedia.flags.filter((f) => f !== 'media handover'); - this._mediaReleased = 'release-media' === reason; + this._mediaPath = 'release-media' === reason ? MediaPath.PartialMedia : MediaPath.FullMedia; + this.logger.debug(`media path is now ${this._mediaPath}`); } else { sdp = await dlg.other.modify(response.sdp); diff --git a/lib/constants.json b/lib/constants.json index f4a5173..08ca0af 100644 --- a/lib/constants.json +++ b/lib/constants.json @@ -3,5 +3,10 @@ "ScaleIn": "scale-in", "StandbyEnter": "standby-enter", "StandbyExit": "standby-exit" + }, + "MediaPath": { + "NoMedia": "no-media", + "PartialMedia": "partial-media", + "FullMedia": "full-media" } } diff --git a/lib/utils.js b/lib/utils.js index c35be32..d1e34d6 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -246,6 +246,12 @@ const parseHostPorts = (logger, hostports, srf) => { return obj; }; +const makeFullMediaReleaseKey = (callId) => { + return `a_sdp:${callId}`; +}; +const makePartnerFullMediaReleaseKey = (callId) => { + return `b_sdp:${callId}`; +}; module.exports = { isWSS, @@ -265,6 +271,8 @@ module.exports = { parseConnectionIp, isMSTeamsCIDR, isPrivateVoipNetwork, - parseHostPorts + parseHostPorts, + makeFullMediaReleaseKey, + makePartnerFullMediaReleaseKey };