Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip #182

Merged
merged 11 commits into from
Nov 19, 2024
Merged

wip #182

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ const {
}, logger);
const {
client: redisClient,
addKey,
retrieveKey,
createSet,
retrieveSet,
addToSet,
Expand Down Expand Up @@ -108,6 +110,8 @@ srf.locals = {...srf.locals,
lookupSystemInformation
},
realtimeDbHelpers: {
addKey,
retrieveKey,
createSet,
incrKey,
decrKey,
Expand Down
49 changes: 43 additions & 6 deletions lib/call-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand All @@ -79,7 +85,7 @@ class CallSession extends Emitter {
}

get isMediaReleased() {
return this._mediaReleased;
return this._mediaPath !== MediaPath.FullMedia;
}

get isFive9VoiceStream() {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions lib/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
"ScaleIn": "scale-in",
"StandbyEnter": "standby-enter",
"StandbyExit": "standby-exit"
},
"MediaPath": {
"NoMedia": "no-media",
"PartialMedia": "partial-media",
"FullMedia": "full-media"
}
}
10 changes: 9 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -265,6 +271,8 @@ module.exports = {
parseConnectionIp,
isMSTeamsCIDR,
isPrivateVoipNetwork,
parseHostPorts
parseHostPorts,
makeFullMediaReleaseKey,
makePartnerFullMediaReleaseKey
};