mirror of
https://github.com/jambonz/sbc-outbound.git
synced 2026-10-04 02:04:19 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
121ae75b39 | ||
|
|
57bc073ea3 | ||
|
|
84bce56766 | ||
|
|
d053b94e71 | ||
|
|
a50ed56b36 | ||
|
|
8fdcc8cbc4 | ||
|
|
b882c0de2d | ||
|
|
b3fee43c7f |
@@ -34,6 +34,7 @@ build/Release
|
||||
node_modules
|
||||
|
||||
.DS_Store
|
||||
.vscode
|
||||
|
||||
examples/*
|
||||
CLAUDE.md
|
||||
+22
-29
@@ -1,5 +1,4 @@
|
||||
const Emitter = require('events');
|
||||
const sdpTransform = require('sdp-transform');
|
||||
const SrsClient = require('@jambonz/siprec-client-utils');
|
||||
const {
|
||||
makeRtpEngineOpts,
|
||||
@@ -10,7 +9,6 @@ const {
|
||||
makePartnerFullMediaReleaseKey,
|
||||
isValidDomainOrIP,
|
||||
removeVideoSdp,
|
||||
determineAnswerCodec
|
||||
} = require('./utils');
|
||||
const { MediaPath } = require('./constants.json');
|
||||
const {forwardInDialogRequests} = require('drachtio-fn-b2b-sugar');
|
||||
@@ -96,19 +94,6 @@ const initCdr = (req, invite) => {
|
||||
};
|
||||
};
|
||||
|
||||
const updateRtpEngineFlags = (sdp, opts) => {
|
||||
try {
|
||||
const parsed = sdpTransform.parse(sdp);
|
||||
const codec = parsed.media[0].rtp[0].codec;
|
||||
// Only add telephone-event support, don't restrict to specific G.711 codec yet
|
||||
// This allows far end to choose between PCMU/PCMA freely
|
||||
if (['PCMU', 'PCMA'].includes(codec)) {
|
||||
opts.flags.push('codec-accept-telephone-event');
|
||||
}
|
||||
} catch {}
|
||||
return opts;
|
||||
};
|
||||
|
||||
class CallSession extends Emitter {
|
||||
constructor(logger, req, res) {
|
||||
super();
|
||||
@@ -125,6 +110,7 @@ class CallSession extends Emitter {
|
||||
this.decrKey = req.srf.locals.realtimeDbHelpers.decrKey;
|
||||
this.addKey = req.srf.locals.realtimeDbHelpers.addKey;
|
||||
this.retrieveKey = req.srf.locals.realtimeDbHelpers.retrieveKey;
|
||||
this.retrieveHash = req.srf.locals.realtimeDbHelpers.retrieveHash;
|
||||
|
||||
const {
|
||||
lookupOutboundCarrierForAccount,
|
||||
@@ -366,7 +352,8 @@ class CallSession extends Emitter {
|
||||
* like a rare use case -- encryption is usually an all or nothing requirement.
|
||||
*/
|
||||
this.logger.info({u}, `using SRTP for outbound call, pad crypto: ${o.pad_crypto ? 'yes' : 'no'}`);
|
||||
this.rtpEngineOpts = makeRtpEngineOpts(this.req, false, true, o.pad_crypto, true);
|
||||
// eslint-disable-next-line max-len
|
||||
this.rtpEngineOpts = makeRtpEngineOpts(this.req, false, true, o.pad_crypto, true, o.remove_ice, o.dtls_off);
|
||||
encryptedMedia = true;
|
||||
}
|
||||
});
|
||||
@@ -398,13 +385,13 @@ class CallSession extends Emitter {
|
||||
const toPublic = uris.some((u) => u.private_network === false);
|
||||
let isOfferUpdatedToPrivate = toPrivate && !toPublic;
|
||||
|
||||
const opts = updateRtpEngineFlags(this.req.body, {
|
||||
const opts = {
|
||||
...this.rtpEngineOpts.common,
|
||||
...this.rtpEngineOpts.uac.mediaOpts,
|
||||
'from-tag': this.rtpEngineOpts.uas.tag,
|
||||
direction: ['private', toPublic ? 'public' : 'private'],
|
||||
sdp: this.req.body
|
||||
});
|
||||
};
|
||||
let response = await this.offer(opts);
|
||||
this.logger.debug({offer: opts, response}, 'initial offer to rtpengine');
|
||||
if ('ok' !== response.result) {
|
||||
@@ -516,7 +503,7 @@ class CallSession extends Emitter {
|
||||
})
|
||||
};
|
||||
const p = proxy ? ` via ${proxy}` : '';
|
||||
this.logger.info({uri, p}, `sending INVITE to ${uri}${p}`);
|
||||
this.logger.info({uri, p}, `sending INVITE${p}`);
|
||||
}
|
||||
|
||||
/* now launch an outbound call attempt */
|
||||
@@ -557,15 +544,16 @@ class CallSession extends Emitter {
|
||||
localSdpA: async(sdp, res) => {
|
||||
this.rtpEngineOpts.uac.tag = res.getParsedHeader('To').params.tag;
|
||||
|
||||
// Determine which codec to use based on far end negotiation
|
||||
const {codec} = determineAnswerCodec(sdp, this.req.body, this.logger);
|
||||
|
||||
const opts = {
|
||||
...this.rtpEngineOpts.common,
|
||||
...this.rtpEngineOpts.uas.mediaOpts,
|
||||
'from-tag': this.rtpEngineOpts.uas.tag,
|
||||
'to-tag': this.rtpEngineOpts.uac.tag,
|
||||
flags: ['single codec', 'inject DTMF', `codec-accept-${codec}`, 'codec-accept-telephone-event'],
|
||||
flags: [
|
||||
'single codec',
|
||||
'inject DTMF',
|
||||
'reuse codecs',
|
||||
],
|
||||
sdp
|
||||
};
|
||||
const response = await this.answer(opts);
|
||||
@@ -737,10 +725,13 @@ class CallSession extends Emitter {
|
||||
if (this.req.locals.cdr) {
|
||||
const now = Date.now();
|
||||
const day = new Date();
|
||||
const recordAllCalls = this.req.locals.record_all_calls;
|
||||
// eslint-disable-next-line max-len
|
||||
const {hasRecording = false} = await this.retrieveHash(`call:${this.account_sid}:${this.req.locals.cdr.call_sid}`) ?? {};
|
||||
const recordAllCalls = this.req.locals.record_all_calls || hasRecording;
|
||||
const record_format = this.req.locals.account.record_format || 'mp3';
|
||||
let recording_url = `/Accounts/${this.account_sid}/RecentCalls/${this.req.locals.cdr.call_sid}/record`;
|
||||
recording_url += `/${day.getFullYear()}/${(day.getMonth() + 1).toString().padStart(2, '0')}`;
|
||||
recording_url += `/${day.getDate().toString().padStart(2, '0')}/${recordAllCalls}`;
|
||||
recording_url += `/${day.getDate().toString().padStart(2, '0')}/${record_format}`;
|
||||
this.writeCdrs({...this.req.locals.cdr,
|
||||
terminated_at: now,
|
||||
termination_reason: dlg.type === 'uas' ? 'caller hungup' : 'called party hungup',
|
||||
@@ -1222,7 +1213,6 @@ Duration=${payload.duration} `
|
||||
...(req.has('X-Retain-Call-Sid') && {'X-Retain-Call-Sid': req.get('X-Retain-Call-Sid')}),
|
||||
...(req.has('X-Account-Sid') && {'X-Account-Sid': req.get('X-Account-Sid')})
|
||||
};
|
||||
|
||||
const uac = await this.srf.createUAC(referTo.uri, {localSdp: dlg.local.sdp, headers});
|
||||
this.uas = uac;
|
||||
uac.type = 'uas';
|
||||
@@ -1231,7 +1221,7 @@ Duration=${payload.duration} `
|
||||
uac.on('info', this._onInfo.bind(this, uac));
|
||||
uac.on('modify', this._onReinvite.bind(this, uac));
|
||||
uac.on('refer', this._onFeatureServerTransfer.bind(this, uac));
|
||||
uac.on('destroy', () => {
|
||||
uac.on('destroy', async() => {
|
||||
this.logger.info('call ended with normal termination');
|
||||
this.rtpEngineResource.destroy();
|
||||
this.activeCallIds.delete(this.req.get('Call-ID'));
|
||||
@@ -1240,10 +1230,13 @@ Duration=${payload.duration} `
|
||||
if (this.req.locals.cdr) {
|
||||
const now = Date.now();
|
||||
const day = new Date();
|
||||
const recordAllCalls = this.req.locals.record_all_calls;
|
||||
// eslint-disable-next-line max-len
|
||||
const {hasRecording = false} = await this.retrieveHash(`call:${this.account_sid}:${this.req.locals.cdr.call_sid}`) ?? {} ;
|
||||
const recordAllCalls = this.req.locals.record_all_calls || hasRecording;
|
||||
const record_format = this.req.locals.account.record_format || 'mp3';
|
||||
let recording_url = `/Accounts/${this.account_sid}/RecentCalls/${this.req.locals.cdr.call_sid}/record`;
|
||||
recording_url += `/${day.getFullYear()}/${(day.getMonth() + 1).toString().padStart(2, '0')}`;
|
||||
recording_url += `/${day.getDate().toString().padStart(2, '0')}/${recordAllCalls}`;
|
||||
recording_url += `/${day.getDate().toString().padStart(2, '0')}/${record_format}`;
|
||||
this.writeCdrs({...this.req.locals.cdr,
|
||||
terminated_at: now,
|
||||
termination_reason: 'caller hungup',
|
||||
|
||||
+23
-54
@@ -5,7 +5,8 @@ const CIDRMatcher = require('cidr-matcher');
|
||||
const dns = require('dns');
|
||||
const sdpTransform = require('sdp-transform');
|
||||
|
||||
function makeRtpEngineOpts(req, srcIsUsingSrtp, dstIsUsingSrtp, padCrypto, teams) {
|
||||
// eslint-disable-next-line max-len
|
||||
function makeRtpEngineOpts(req, srcIsUsingSrtp, dstIsUsingSrtp, padCrypto, teams, remove_ice = false, dtls_off = false) {
|
||||
const from = req.getParsedHeader('from');
|
||||
const rtpCopy = JSON.parse(JSON.stringify(rtpCharacteristics));
|
||||
const srtpCopy = JSON.parse(JSON.stringify(srtpCharacteristics));
|
||||
@@ -16,8 +17,16 @@ function makeRtpEngineOpts(req, srcIsUsingSrtp, dstIsUsingSrtp, padCrypto, teams
|
||||
}
|
||||
|
||||
const srtpOpts = teams ? srtpCopy['teams'] : srtpCopy['default'];
|
||||
const dstOpts = dstIsUsingSrtp ? srtpOpts : rtpCopy;
|
||||
const srcOpts = srcIsUsingSrtp ? srtpOpts : rtpCopy;
|
||||
|
||||
if (remove_ice) {
|
||||
srtpOpts.ICE = 'remove';
|
||||
}
|
||||
if (dtls_off) {
|
||||
srtpOpts.DTLS = 'off';
|
||||
}
|
||||
|
||||
const dstOpts = JSON.parse(JSON.stringify(dstIsUsingSrtp ? srtpOpts : rtpCopy));
|
||||
const srcOpts = JSON.parse(JSON.stringify(srcIsUsingSrtp ? srtpOpts : rtpCopy));
|
||||
|
||||
/** Allow feature server to send DTMF to the call excepts call from/to teams */
|
||||
if (!teams) {
|
||||
@@ -41,6 +50,16 @@ function makeRtpEngineOpts(req, srcIsUsingSrtp, dstIsUsingSrtp, padCrypto, teams
|
||||
'replace': ['origin', 'session-connection'],
|
||||
'record call': process.env.JAMBONES_RECORD_ALL_CALLS ? 'yes' : 'no'
|
||||
};
|
||||
|
||||
const codec = {
|
||||
accept: ['PCMU', 'PCMA', 'telephone-event'],
|
||||
...(process.env.JAMBONES_CODEC_OFFER_WITH_ORDER &&
|
||||
{
|
||||
offer: process.env.JAMBONES_CODEC_OFFER_WITH_ORDER.split(','),
|
||||
strip: 'all'
|
||||
}),
|
||||
};
|
||||
|
||||
return {
|
||||
common,
|
||||
uas: {
|
||||
@@ -51,8 +70,7 @@ function makeRtpEngineOpts(req, srcIsUsingSrtp, dstIsUsingSrtp, padCrypto, teams
|
||||
tag: null,
|
||||
mediaOpts: {
|
||||
...dstOpts,
|
||||
...(process.env.JAMBONES_CODEC_OFFER_WITH_ORDER &&
|
||||
{ codec: { offer: process.env.JAMBONES_CODEC_OFFER_WITH_ORDER.split(','), strip: 'all' } }),
|
||||
codec,
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -350,54 +368,6 @@ const removeVideoSdp = (sdp) => {
|
||||
return sdpTransform.write(parsedSdp);
|
||||
};
|
||||
|
||||
const determineAnswerCodec = (farEndSdp, featureServerSdp, logger) => {
|
||||
try {
|
||||
// Parse both SDPs
|
||||
const farEndParsed = sdpTransform.parse(farEndSdp);
|
||||
const fsParsed = sdpTransform.parse(featureServerSdp);
|
||||
|
||||
// Get negotiated codec from far end (first codec in answer)
|
||||
const negotiatedCodec = farEndParsed.media[0].rtp[0].codec;
|
||||
|
||||
// Get all codecs offered by feature server
|
||||
const fsCodecs = fsParsed.media[0].rtp.map((r) => r.codec);
|
||||
|
||||
logger.debug({negotiatedCodec, fsCodecs}, 'determineAnswerCodec: analyzing codec negotiation');
|
||||
|
||||
// If far end negotiated G.711 (PCMU/PCMA) AND it was in the FS offer, pass it through
|
||||
if (['PCMU', 'PCMA'].includes(negotiatedCodec) && fsCodecs.includes(negotiatedCodec)) {
|
||||
logger.info({negotiatedCodec}, 'G.711 codec passthrough - no transcoding needed');
|
||||
return {
|
||||
codec: negotiatedCodec,
|
||||
needsTranscoding: false
|
||||
};
|
||||
}
|
||||
|
||||
// Otherwise, we need to transcode to first G.711 codec in FS offer
|
||||
const firstG711 = fsCodecs.find((c) => ['PCMU', 'PCMA'].includes(c));
|
||||
if (firstG711) {
|
||||
logger.info({negotiatedCodec, transcodeTarget: firstG711}, 'Transcoding required to G.711');
|
||||
return {
|
||||
codec: firstG711,
|
||||
needsTranscoding: true
|
||||
};
|
||||
}
|
||||
|
||||
// Fallback: use PCMU
|
||||
logger.info({negotiatedCodec}, 'No G.711 in FS offer, defaulting to PCMU');
|
||||
return {
|
||||
codec: 'PCMU',
|
||||
needsTranscoding: true
|
||||
};
|
||||
} catch (err) {
|
||||
logger.error({err}, 'Error determining answer codec, defaulting to PCMU');
|
||||
return {
|
||||
codec: 'PCMU',
|
||||
needsTranscoding: true
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
makeRtpEngineOpts,
|
||||
selectHostPort,
|
||||
@@ -414,5 +384,4 @@ module.exports = {
|
||||
makePartnerFullMediaReleaseKey,
|
||||
isValidDomainOrIP,
|
||||
removeVideoSdp,
|
||||
determineAnswerCodec
|
||||
};
|
||||
|
||||
Generated
+584
-498
File diff suppressed because it is too large
Load Diff
+4
-4
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "sbc-outbound",
|
||||
"version": "0.9.5",
|
||||
"version": "0.9.6",
|
||||
"main": "app.js",
|
||||
"engines": {
|
||||
"node": ">= 18.0.0"
|
||||
@@ -39,7 +39,7 @@
|
||||
"cidr-matcher": "^2.1.1",
|
||||
"debug": "^4.4.3",
|
||||
"drachtio-fn-b2b-sugar": "^0.2.1",
|
||||
"drachtio-srf": "^5.0.12",
|
||||
"drachtio-srf": "^5.0.20",
|
||||
"express": "^4.21.2",
|
||||
"pino": "^10.1.0",
|
||||
"sdp-transform": "^2.15.0"
|
||||
@@ -48,7 +48,7 @@
|
||||
"bent": "^7.3.12",
|
||||
"eslint": "^9.17.0",
|
||||
"eslint-plugin-promise": "^7.2.1",
|
||||
"nyc": "^15.1.0",
|
||||
"tape": "^5.7.5"
|
||||
"nyc": "^17.1.0",
|
||||
"tape": "^5.9.0"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user