diff --git a/lib/Constants.js b/lib/Constants.js index cb3275bf2..5cb3a88a4 100644 --- a/lib/Constants.js +++ b/lib/Constants.js @@ -64,6 +64,12 @@ module.exports = { UPDATE : 'UPDATE', SUBSCRIBE : 'SUBSCRIBE', + // DTMF transport methods. + DTMF_TRANSPORT : { + INFO : 'INFO', + RFC2833 : 'RFC2833' + }, + /* SIP Response Reasons * DOC: https://www.iana.org/assignments/sip-parameters * Copied from https://github.com/versatica/OverSIP/blob/master/lib/oversip/sip/constants.rb#L7 diff --git a/lib/RTCSession.js b/lib/RTCSession.js index 979d58d07..3db52cdbc 100644 --- a/lib/RTCSession.js +++ b/lib/RTCSession.js @@ -942,6 +942,7 @@ module.exports = class RTCSession extends EventEmitter let position = 0; let duration = options.duration || null; let interToneGap = options.interToneGap || null; + const transportType = options.transportType || JsSIP_C.DTMF_TRANSPORT.INFO; if (tones === undefined) { @@ -1046,13 +1047,40 @@ module.exports = class RTCSession extends EventEmitter } else { - const dtmf = new RTCSession_DTMF(this); + if ( + transportType !== JsSIP_C.DTMF_TRANSPORT.INFO || + transportType !== JsSIP_C.DTMF_TRANSPORT.RFC2833 + ) + { + throw new TypeError(`invalid transportType: ${transportType}`); + } + + // Send DTMF according to transport config. + switch (transportType) + { + case JsSIP_C.DTMF_TRANSPORT.RFC2833: { + // Send DTMF in current audio RTP stream. + const sender = this._getDTMFRTPSender(); + + if (sender) + { + sender.insertDTMF(tone, duration, interToneGap); + } - options.eventHandlers = { - onFailed : () => { this._tones = null; } - }; - dtmf.send(tone, options); - timeout = duration + interToneGap; + break; + } + + case JsSIP_C.DTMF_TRANSPORT.INFO: { + // Send DTMF via SIP INFO messages. + const dtmf = new RTCSession_DTMF(this); + + options.eventHandlers = { + onFailed : () => { this._tones = null; } + }; + dtmf.send(tone, options); + timeout = duration + interToneGap; + } + } } // Set timeout for the next tone. @@ -2632,6 +2660,26 @@ module.exports = class RTCSession extends EventEmitter }); } + /** + * Get DTMF RTCRtpSender. + */ + _getDTMFRTPSender() + { + const sender = this._connection.getSenders().find((rtpSender) => + { + return rtpSender.track && rtpSender.track.kind === 'audio'; + }); + + if (!(sender && sender.dtmf)) + { + debugerror('sendDTMF() | no local audio track to send DTMF with'); + + return; + } + + return sender.dtmf; + } + /** * Reception of Response for Initial INVITE */