From afdb88761790a86a4c7d2793fed5f16ab9aa3094 Mon Sep 17 00:00:00 2001 From: Avi Charlop Date: Mon, 11 May 2020 18:08:18 +0200 Subject: [PATCH 1/7] add support for IN_BAND DTMF --- lib/Constants.js | 6 +++++ lib/RTCSession.js | 59 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/lib/Constants.js b/lib/Constants.js index cb3275bf2..a9b8f72c7 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', + IN_BAND : 'IN_BAND' + }, + /* 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..ead79e9ef 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,32 @@ module.exports = class RTCSession extends EventEmitter } else { - const dtmf = new RTCSession_DTMF(this); + // send DTMF according to transport config + switch (transportType) + { + case JsSIP_C.DTMF_TRANSPORT.IN_BAND: { + // 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: + default: { + // 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 +2652,33 @@ module.exports = class RTCSession extends EventEmitter }); } + /** + * DTMF RTP Sender + */ + _getDTMFRTPSender() + { + const senders = this._connection.getSenders().find((sender) => + { + return sender.track && sender.track.kind === 'audio'; + }); + + if (!senders) + { + debugerror('No local audio track to send DTMF with'); + + return; + } + + if (!senders.dtmf) + { + debugerror('This demo requires DTMF which is not support by this browser.'); + + return; + } + + return senders.dtmf; + } + /** * Reception of Response for Initial INVITE */ From fa620d0022f935ccaca689e60868dadc1069a6df Mon Sep 17 00:00:00 2001 From: Avi Charlop Date: Mon, 11 May 2020 18:45:55 +0200 Subject: [PATCH 2/7] updates from comments --- lib/RTCSession.js | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/RTCSession.js b/lib/RTCSession.js index ead79e9ef..f0fd3c133 100644 --- a/lib/RTCSession.js +++ b/lib/RTCSession.js @@ -1047,11 +1047,16 @@ module.exports = class RTCSession extends EventEmitter } else { - // send DTMF according to transport config + if (transportType !== JsSIP_C.DTMF_TRANSPORT.INFO || + transportType !== JsSIP_C.DTMF_TRANSPORT.IN_BAND) + { + throw new TypeError(`invalid transportType: ${transportType}`); + } + // Send DTMF according to transport config. switch (transportType) { case JsSIP_C.DTMF_TRANSPORT.IN_BAND: { - // send DTMF in current audio RTP stream + // Send DTMF in current audio RTP stream. const sender = this._getDTMFRTPSender(); if (sender) @@ -1061,9 +1066,8 @@ module.exports = class RTCSession extends EventEmitter break; } - case JsSIP_C.DTMF_TRANSPORT.INFO: - default: { - // send DTMF via SIP INFO messages + case JsSIP_C.DTMF_TRANSPORT.INFO: { + // Send DTMF via SIP INFO messages. const dtmf = new RTCSession_DTMF(this); options.eventHandlers = { @@ -2653,7 +2657,7 @@ module.exports = class RTCSession extends EventEmitter } /** - * DTMF RTP Sender + * Get DTMF RTCRtpSender. */ _getDTMFRTPSender() { @@ -2662,16 +2666,9 @@ module.exports = class RTCSession extends EventEmitter return sender.track && sender.track.kind === 'audio'; }); - if (!senders) - { - debugerror('No local audio track to send DTMF with'); - - return; - } - - if (!senders.dtmf) + if (!(senders && senders.dtmf)) { - debugerror('This demo requires DTMF which is not support by this browser.'); + debugerror('sendDTMF() | No local audio track to send DTMF with'); return; } From 38deb236a0958fb83610302ab96b944fd8038b88 Mon Sep 17 00:00:00 2001 From: Avi Charlop Date: Mon, 11 May 2020 18:47:21 +0200 Subject: [PATCH 3/7] update comment case --- lib/RTCSession.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/RTCSession.js b/lib/RTCSession.js index f0fd3c133..507a5e515 100644 --- a/lib/RTCSession.js +++ b/lib/RTCSession.js @@ -2668,7 +2668,7 @@ module.exports = class RTCSession extends EventEmitter if (!(senders && senders.dtmf)) { - debugerror('sendDTMF() | No local audio track to send DTMF with'); + debugerror('sendDTMF() | no local audio track to send DTMF with'); return; } From 741d61ad34614ee1b7931ae056bbe9392422ed27 Mon Sep 17 00:00:00 2001 From: Avi Charlop Date: Mon, 11 May 2020 20:22:41 +0200 Subject: [PATCH 4/7] added requested changes --- lib/Constants.js | 2 +- lib/RTCSession.js | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/Constants.js b/lib/Constants.js index a9b8f72c7..216d75408 100644 --- a/lib/Constants.js +++ b/lib/Constants.js @@ -64,7 +64,7 @@ module.exports = { UPDATE : 'UPDATE', SUBSCRIBE : 'SUBSCRIBE', - // DTMF transport methods + // DTMF transport methods. DTMF_TRANSPORT : { INFO : 'INFO', IN_BAND : 'IN_BAND' diff --git a/lib/RTCSession.js b/lib/RTCSession.js index 507a5e515..7dd75889f 100644 --- a/lib/RTCSession.js +++ b/lib/RTCSession.js @@ -1052,6 +1052,7 @@ module.exports = class RTCSession extends EventEmitter { throw new TypeError(`invalid transportType: ${transportType}`); } + // Send DTMF according to transport config. switch (transportType) { @@ -1066,6 +1067,7 @@ module.exports = class RTCSession extends EventEmitter break; } + case JsSIP_C.DTMF_TRANSPORT.INFO: { // Send DTMF via SIP INFO messages. const dtmf = new RTCSession_DTMF(this); @@ -2661,19 +2663,19 @@ module.exports = class RTCSession extends EventEmitter */ _getDTMFRTPSender() { - const senders = this._connection.getSenders().find((sender) => + const sender = this._connection.getSenders().find((sender) => { return sender.track && sender.track.kind === 'audio'; }); - if (!(senders && senders.dtmf)) + if (!(sender && sender.dtmf)) { debugerror('sendDTMF() | no local audio track to send DTMF with'); return; } - return senders.dtmf; + return sender.dtmf; } /** From 4d9c71b7ba22ed43f119085ef302044a9618e9ad Mon Sep 17 00:00:00 2001 From: Avi Charlop Date: Tue, 12 May 2020 09:40:44 +0200 Subject: [PATCH 5/7] rename IN_BAND to RFC2833 --- lib/Constants.js | 2 +- lib/RTCSession.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Constants.js b/lib/Constants.js index 216d75408..5cb3a88a4 100644 --- a/lib/Constants.js +++ b/lib/Constants.js @@ -67,7 +67,7 @@ module.exports = { // DTMF transport methods. DTMF_TRANSPORT : { INFO : 'INFO', - IN_BAND : 'IN_BAND' + RFC2833 : 'RFC2833' }, /* SIP Response Reasons diff --git a/lib/RTCSession.js b/lib/RTCSession.js index 7dd75889f..b4d8180ca 100644 --- a/lib/RTCSession.js +++ b/lib/RTCSession.js @@ -1048,7 +1048,7 @@ module.exports = class RTCSession extends EventEmitter else { if (transportType !== JsSIP_C.DTMF_TRANSPORT.INFO || - transportType !== JsSIP_C.DTMF_TRANSPORT.IN_BAND) + transportType !== JsSIP_C.DTMF_TRANSPORT.RFC2833) { throw new TypeError(`invalid transportType: ${transportType}`); } @@ -1056,7 +1056,7 @@ module.exports = class RTCSession extends EventEmitter // Send DTMF according to transport config. switch (transportType) { - case JsSIP_C.DTMF_TRANSPORT.IN_BAND: { + case JsSIP_C.DTMF_TRANSPORT.RFC2833: { // Send DTMF in current audio RTP stream. const sender = this._getDTMFRTPSender(); From 846b5a9c2bcc4f7421327b95067545da969b5e64 Mon Sep 17 00:00:00 2001 From: Avi Charlop Date: Tue, 12 May 2020 09:41:04 +0200 Subject: [PATCH 6/7] fix linting errors --- lib/RTCSession.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/RTCSession.js b/lib/RTCSession.js index b4d8180ca..eaf7ccc47 100644 --- a/lib/RTCSession.js +++ b/lib/RTCSession.js @@ -2663,9 +2663,9 @@ module.exports = class RTCSession extends EventEmitter */ _getDTMFRTPSender() { - const sender = this._connection.getSenders().find((sender) => + const sender = this._connection.getSenders().find((rtpSender) => { - return sender.track && sender.track.kind === 'audio'; + return rtpSender.track && rtpSender.track.kind === 'audio'; }); if (!(sender && sender.dtmf)) From a9748adb45956cc46b6ca47dc17ae6bfe4341716 Mon Sep 17 00:00:00 2001 From: Avi Charlop Date: Tue, 12 May 2020 20:28:33 +0200 Subject: [PATCH 7/7] Update lib/RTCSession.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: IƱaki Baz Castillo --- lib/RTCSession.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/RTCSession.js b/lib/RTCSession.js index eaf7ccc47..3db52cdbc 100644 --- a/lib/RTCSession.js +++ b/lib/RTCSession.js @@ -1047,8 +1047,10 @@ module.exports = class RTCSession extends EventEmitter } else { - if (transportType !== JsSIP_C.DTMF_TRANSPORT.INFO || - transportType !== JsSIP_C.DTMF_TRANSPORT.RFC2833) + if ( + transportType !== JsSIP_C.DTMF_TRANSPORT.INFO || + transportType !== JsSIP_C.DTMF_TRANSPORT.RFC2833 + ) { throw new TypeError(`invalid transportType: ${transportType}`); }