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

fix rtpengine re-invite inactive sdp #78

Merged
merged 5 commits into from
Jan 1, 2024
Merged
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
8 changes: 4 additions & 4 deletions lib/freeswitch-call-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ const handler = (req, res) => {
* @param {*} sdp SDP offered by Freeswitch in leg2 INVITE
* @param {*} res res SIP Response object
*/
function createSdpForResponse(sessionId, sdp, res) {
function createSdpForResponse(opts, sdp, res) {
return new Promise((resolve, reject) => {
client.get(sessionId, (err, result) => {
client.get(opts.sessionId, (err, result) => {
if (err) {
return reject(err);
}
resolve(payloadCombiner(sdp, result));
resolve(payloadCombiner(sdp, result, opts.sdp1, opts.sdp2));
});
});
}
Expand All @@ -93,7 +93,7 @@ function handleIncomingSiprecInvite(req, res, opts) {
passProvisionalResponses: false,
headers,
localSdpB: opts.sdp1,
localSdpA: createSdpForResponse.bind(null, opts.sessionId)
localSdpA: createSdpForResponse.bind(null, opts)
};

return srf.createB2BUA(req, res, fsUri, callOpts);
Expand Down
16 changes: 14 additions & 2 deletions lib/payload-combiner.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const transform = require('sdp-transform');
const debug = require('debug')('drachtio:siprec-recording-server');

module.exports = function(sdp1, sdp2) {
module.exports = function(sdp1, sdp2, originalSdp1, originalSdp2) {
const sdpObj = [];
sdpObj.push(transform.parse(sdp1));
sdpObj.push(transform.parse(sdp2));
const originalParsedSdp1 = transform.parse(originalSdp1);
const originalParsedSdp2 = transform.parse(originalSdp2);

//const arr1 = /^([^]+)(c=[^]+)t=[^]+(m=[^]+?)(a=[^]+)$/.exec(sdp1) ;
//const arr2 = /^([^]+)(c=[^]+)t=[^]+(m=[^]+?)(a=[^]+)$/.exec(sdp2) ;
Expand All @@ -23,13 +25,23 @@ module.exports = function(sdp1, sdp2) {

if (!sdpObj[0].media[0].label) sdpObj[0].media[0].label = 1;
if (!sdpObj[1].media[0].label) sdpObj[1].media[0].label = 2;
// Update SDP Direction
if (originalParsedSdp1.media[0].direction === 'inactive') {
sdpObj[0].media[0].direction = 'inactive';
} else {
sdpObj[0].media[0].direction = 'recvonly';
}
if (originalParsedSdp2.media[0].direction === 'inactive') {
sdpObj[1].media[0].direction = 'inactive';
} else {
sdpObj[1].media[0].direction = 'recvonly';
}

//const aLabel = sdp1.includes('a=label:') ? '' : 'a=label:1\r\n';
//const bLabel = sdp2.includes('a=label:') ? '' : 'a=label:2\r\n';

sdpObj[0].media = sdpObj[0].media.concat(sdpObj[1].media);
const combinedSdp = transform.write(sdpObj[0])
.replace(/a=sendonly\r\n/g, '')
.replace(/a=direction:both\r\n/g, '');

debug(`combined ${combinedSdp}`);
Expand Down
43 changes: 37 additions & 6 deletions lib/rtpengine-call-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const debug = require('debug')('drachtio:siprec-recording-server');
module.exports = (req, res) => {
const callid = req.get('Call-ID');
const from = req.getParsedHeader('From');
const totag = v4();
const logger = req.srf.locals.logger.child({callid});
const opts = {
req,
Expand All @@ -22,21 +23,51 @@ module.exports = (req, res) => {
const rtpEngine = getAvailableRtpengine();

parseSiprecPayload(opts)
.then(allocateEndpoint.bind(null, 'caller', rtpEngine))
.then(allocateEndpoint.bind(null, 'callee', rtpEngine))
.then(allocateEndpoint.bind(null, 'caller', rtpEngine, totag))
.then(allocateEndpoint.bind(null, 'callee', rtpEngine, totag))
.then(respondToInvite)
.then((dlg) => {
logger.info(`call connected successfully, using rtpengine at ${JSON.stringify(rtpEngine.remote)}`);
dlg.on('modify', _onReinvite.bind(null, rtpEngine, logger, totag));
return dlg.on('destroy', onCallEnd.bind(null, rtpEngine, opts));
})
.catch((err) => {
logger.error(`Error connecting call: ${err}`);
});
};

function allocateEndpoint(which, rtpEngine, opts) {
function _onReinvite(rtpEngine, logger, totag, req, res) {
const callid = req.get('Call-ID');
const from = req.getParsedHeader('From');
const opts = {
req,
res,
logger,
callDetails: {
'call-id': callid,
'from-tag': from.params.tag,
}
};

parseSiprecPayload(opts)
.then(allocateEndpoint.bind(null, 'caller', rtpEngine, totag))
.then(allocateEndpoint.bind(null, 'callee', rtpEngine, totag))
.then((opts) => {
const body = constructSiprecPayload(opts.rtpengineCallerSdp, opts.rtpengineCalleeSdp, opts.sdp1, opts.sdp2);
return opts.res.send(200, {body});
})
.catch((err) => {
logger.error(`Error connecting call: ${err}`);
});

logger.info(`received SIPREC Re-invite: ${req.uri}`);
}

function allocateEndpoint(which, rtpEngine, totag, opts) {
// If audio is inactive, rtpengine will stop recording and there is no blank audio in record file.
const sdp = (which === 'caller' ? opts.sdp1 : opts.sdp2).replace(/a=inactive\r\n/g, 'a=sendonly\r\n');
const args = Object.assign({}, opts.callDetails, {
'sdp': which === 'caller' ? opts.sdp1 : opts.sdp2,
sdp,
'replace': ['origin', 'session-connection'],
'transport protocol': 'RTP/AVP',
'record call': 'yes',
Expand All @@ -47,7 +78,7 @@ function allocateEndpoint(which, rtpEngine, opts) {
'rtcp-mux': ['accept'],
'direction': ['public', 'public'],
});
if (which === 'callee') Object.assign(args, {'to-tag': v4()});
if (which === 'callee') Object.assign(args, {'to-tag': totag});

debug(`callDetails: ${JSON.stringify(opts.callDetails)}`);
debug(`rtpengine args for ${which}: ${JSON.stringify(args)}, sending to ${JSON.stringify(rtpEngine.remote)}`);
Expand All @@ -63,7 +94,7 @@ function allocateEndpoint(which, rtpEngine, opts) {

function respondToInvite(opts) {
const srf = opts.req.srf;
const payload = constructSiprecPayload(opts.rtpengineCallerSdp, opts.rtpengineCalleeSdp);
const payload = constructSiprecPayload(opts.rtpengineCallerSdp, opts.rtpengineCalleeSdp, opts.sdp1, opts.sdp2);
return srf.createUAS(opts.req, opts.res, {localSdp: payload});
}

Expand Down
2 changes: 1 addition & 1 deletion test/call_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@ test('siprec with multiple rtpengines', (t) => {
.on('error', (err) => {
t.end(err, 'error connecting to drachtio');
});
}) ;
}) ;
2 changes: 1 addition & 1 deletion test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function combineAndVerifyPayloads(filename, delimiter, t) {
.then((data) => {
const sdp = data.split('__split_here__');
t.ok(sdp.length === 2, 'read two sdps');
const full = combinePayloads(sdp[0], sdp[1]);
const full = combinePayloads(sdp[0], sdp[1], sdp[0], sdp[1]);
t.pass('combined payloads');
t.end();
})
Expand Down
92 changes: 92 additions & 0 deletions test/scenarios/uac_siprec_pcap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,98 @@
</action>
</nop>

<send>
<![CDATA[

INVITE sip:drachtio:[remote_port] SIP/2.0
Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
From: sipp <sip:sipp@[local_ip]:[local_port]>;tag=[pid]SIPpTag09[call_number]
To: sip:drachtio:[remote_port][peer_tag_param]
Call-ID: [call_id]
CSeq: 2 INVITE
Contact: <sip:[local_ip]:[local_port]>
Max-Forwards: 70
Subject: siprec Test
Content-Type: multipart/mixed; boundary=690DEB64
Content-Length: [len]

--690DEB64
Content-Type: application/sdp

v=0
o=user1 53655765 2353687637 IN IP[local_ip_type] [local_ip]
s=-
c=IN IP[local_ip_type] [local_ip]
t=0 0
m=audio [auto_media_port] RTP/AVP 8
c=IN IP[local_ip_type] [local_ip]
a=rtpmap:8 PCMA/8000
a=direction:both
a=inactive
a=label:a_leg
m=audio 60000 RTP/AVP 8
c=IN IP[local_ip_type] [local_ip]
a=rtpmap:8 PCMA/8000
a=direction:both
a=inactive
a=label:b_leg

--690DEB64
Content-Type: application/rs-metadata
Content-Disposition: recording-session

<?xml version="1.0" encoding="UTF-8"?>
<recording xmlns='urn:ietf:params:xml:ns:recording:1'>
<datamode>complete</datamode>
<session session_id="XoM2pKdFR8C3eX33qJKcww==">
<sipSessionID>hvij5wK4Wx</sipSessionID>
</session>
<participant participant_id="omFyWlFETEyFz/KXNLe4iQ==">
<nameID aor="sip:6001@localhost"/>
</participant>
<participant participant_id="fLbZzLMgRXGp+F5w0hOt6w==">
<nameID aor="sip:101@localhost"/>
</participant>
<stream stream_id="1LiAtnDVR9m469/0L4bwCA==" session_id="XoM2pKdFR8C3eX33qJKcww==">
<label>1</label>
</stream>
<stream stream_id="WJgj5uxeRH+nwUah02dWqg==" session_id="XoM2pKdFR8C3eX33qJKcww==">
<label>2</label>
</stream>
</recording>

--690DEB64--

]]>
</send>

<recv response="100" optional="true">
</recv>

<recv response="180" optional="true">
</recv>

<recv response="200" rtd="true" crlf="true">
</recv>

<!-- Send ACK for the 200 OK received from the re-INVITE -->
<send>
<![CDATA[

ACK sip:[service]@[remote_ip]:[remote_port] SIP/2.0
Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
From: sipp <sip:sipp@[local_ip]:[local_port]>;tag=[pid]SIPpTag09[call_number]
To: [service] <sip:[service]@[remote_ip]:[remote_port]>[peer_tag_param]
Call-ID: [call_id]
CSeq: 2 ACK
Contact: sip:sipp@[local_ip]:[local_port]
Max-Forwards: 70
Subject: Performance Test
Content-Length: 0

]]>
</send>

<!-- Pause 8 seconds, which is approximately the duration of the -->
<!-- PCAP file -->
<pause milliseconds="3000"/>
Expand Down