henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/dtmf_sender.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
| 13 | #include <ctype.h> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 16 | #include <string> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "rtc_base/ref_counted_object.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/thread.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 25 | // RFC4733 |
| 26 | // +-------+--------+------+---------+ |
| 27 | // | Event | Code | Type | Volume? | |
| 28 | // +-------+--------+------+---------+ |
| 29 | // | 0--9 | 0--9 | tone | yes | |
| 30 | // | * | 10 | tone | yes | |
| 31 | // | # | 11 | tone | yes | |
| 32 | // | A--D | 12--15 | tone | yes | |
| 33 | // +-------+--------+------+---------+ |
| 34 | // The "," is a special event defined by the WebRTC spec. It means to delay for |
| 35 | // 2 seconds before processing the next tone. We use -1 as its code. |
Aaron Alaniz | 529d886 | 2020-01-21 03:09:47 +0000 | [diff] [blame] | 36 | static const int kDtmfCommaDelay = -1; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 37 | static const char kDtmfValidTones[] = ",0123456789*#ABCDabcd"; |
| 38 | static const char kDtmfTonesTable[] = ",0123456789*#ABCD"; |
dminor | 588101c | 2017-03-28 11:18:32 -0700 | [diff] [blame] | 39 | // The duration cannot be more than 6000ms or less than 40ms. The gap between |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 40 | // tones must be at least 50 ms. |
Harald Alvestrand | 52e5852 | 2018-02-20 08:15:36 +0100 | [diff] [blame] | 41 | // Source for values: W3C WEBRTC specification. |
| 42 | // https://w3c.github.io/webrtc-pc/#dom-rtcdtmfsender-insertdtmf |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 43 | static const int kDtmfDefaultDurationMs = 100; |
dminor | 588101c | 2017-03-28 11:18:32 -0700 | [diff] [blame] | 44 | static const int kDtmfMinDurationMs = 40; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 45 | static const int kDtmfMaxDurationMs = 6000; |
| 46 | static const int kDtmfDefaultGapMs = 50; |
Harald Alvestrand | 52e5852 | 2018-02-20 08:15:36 +0100 | [diff] [blame] | 47 | static const int kDtmfMinGapMs = 30; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 48 | |
| 49 | // Get DTMF code from the DTMF event character. |
| 50 | bool GetDtmfCode(char tone, int* code) { |
| 51 | // Convert a-d to A-D. |
| 52 | char event = toupper(tone); |
| 53 | const char* p = strchr(kDtmfTonesTable, event); |
| 54 | if (!p) { |
| 55 | return false; |
| 56 | } |
| 57 | *code = p - kDtmfTonesTable - 1; |
| 58 | return true; |
| 59 | } |
| 60 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 61 | rtc::scoped_refptr<DtmfSender> DtmfSender::Create( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 62 | rtc::Thread* signaling_thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 63 | DtmfProviderInterface* provider) { |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 64 | if (!signaling_thread) { |
| 65 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 67 | rtc::scoped_refptr<DtmfSender> dtmf_sender( |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 68 | new rtc::RefCountedObject<DtmfSender>(signaling_thread, provider)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | return dtmf_sender; |
| 70 | } |
| 71 | |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 72 | DtmfSender::DtmfSender(rtc::Thread* signaling_thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 73 | DtmfProviderInterface* provider) |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 74 | : observer_(nullptr), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | signaling_thread_(signaling_thread), |
| 76 | provider_(provider), |
| 77 | duration_(kDtmfDefaultDurationMs), |
Aaron Alaniz | 529d886 | 2020-01-21 03:09:47 +0000 | [diff] [blame] | 78 | inter_tone_gap_(kDtmfDefaultGapMs), |
| 79 | comma_delay_(kDtmfDefaultCommaDelayMs) { |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 80 | RTC_DCHECK(signaling_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 81 | if (provider_) { |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 82 | RTC_DCHECK(provider_->GetOnDestroyedSignal()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 83 | provider_->GetOnDestroyedSignal()->connect( |
| 84 | this, &DtmfSender::OnProviderDestroyed); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | DtmfSender::~DtmfSender() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | StopSending(); |
| 90 | } |
| 91 | |
| 92 | void DtmfSender::RegisterObserver(DtmfSenderObserverInterface* observer) { |
| 93 | observer_ = observer; |
| 94 | } |
| 95 | |
| 96 | void DtmfSender::UnregisterObserver() { |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 97 | observer_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | bool DtmfSender::CanInsertDtmf() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 101 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | if (!provider_) { |
| 103 | return false; |
| 104 | } |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 105 | return provider_->CanInsertDtmf(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 108 | bool DtmfSender::InsertDtmf(const std::string& tones, |
| 109 | int duration, |
Aaron Alaniz | 529d886 | 2020-01-21 03:09:47 +0000 | [diff] [blame] | 110 | int inter_tone_gap, |
| 111 | int comma_delay) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 112 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 114 | if (duration > kDtmfMaxDurationMs || duration < kDtmfMinDurationMs || |
Aaron Alaniz | 529d886 | 2020-01-21 03:09:47 +0000 | [diff] [blame] | 115 | inter_tone_gap < kDtmfMinGapMs || comma_delay < kDtmfMinGapMs) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 116 | RTC_LOG(LS_ERROR) |
| 117 | << "InsertDtmf is called with invalid duration or tones gap. " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 118 | "The duration cannot be more than " |
| 119 | << kDtmfMaxDurationMs << "ms or less than " << kDtmfMinDurationMs |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 120 | << "ms. The gap between tones must be at least " << kDtmfMinGapMs |
| 121 | << "ms."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | |
| 125 | if (!CanInsertDtmf()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 126 | RTC_LOG(LS_ERROR) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 127 | << "InsertDtmf is called on DtmfSender that can't send DTMF."; |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | tones_ = tones; |
| 132 | duration_ = duration; |
| 133 | inter_tone_gap_ = inter_tone_gap; |
Aaron Alaniz | 529d886 | 2020-01-21 03:09:47 +0000 | [diff] [blame] | 134 | comma_delay_ = comma_delay; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 135 | // Clear the previous queue. |
Steve Anton | 9a4fd9b | 2018-08-31 15:20:10 -0700 | [diff] [blame] | 136 | dtmf_driver_.Clear(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 137 | // Kick off a new DTMF task queue. |
Steve Anton | 9a4fd9b | 2018-08-31 15:20:10 -0700 | [diff] [blame] | 138 | QueueInsertDtmf(RTC_FROM_HERE, 1 /*ms*/); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 139 | return true; |
| 140 | } |
| 141 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 142 | std::string DtmfSender::tones() const { |
| 143 | return tones_; |
| 144 | } |
| 145 | |
| 146 | int DtmfSender::duration() const { |
| 147 | return duration_; |
| 148 | } |
| 149 | |
| 150 | int DtmfSender::inter_tone_gap() const { |
| 151 | return inter_tone_gap_; |
| 152 | } |
| 153 | |
Aaron Alaniz | 529d886 | 2020-01-21 03:09:47 +0000 | [diff] [blame] | 154 | int DtmfSender::comma_delay() const { |
| 155 | return comma_delay_; |
| 156 | } |
| 157 | |
Steve Anton | 9a4fd9b | 2018-08-31 15:20:10 -0700 | [diff] [blame] | 158 | void DtmfSender::QueueInsertDtmf(const rtc::Location& posted_from, |
| 159 | uint32_t delay_ms) { |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 160 | dtmf_driver_.AsyncInvokeDelayed<void>( |
| 161 | posted_from, signaling_thread_, [this] { DoInsertDtmf(); }, delay_ms); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void DtmfSender::DoInsertDtmf() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 165 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 166 | |
| 167 | // Get the first DTMF tone from the tone buffer. Unrecognized characters will |
| 168 | // be ignored and skipped. |
| 169 | size_t first_tone_pos = tones_.find_first_of(kDtmfValidTones); |
| 170 | int code = 0; |
| 171 | if (first_tone_pos == std::string::npos) { |
| 172 | tones_.clear(); |
| 173 | // Fire a “OnToneChange” event with an empty string and stop. |
| 174 | if (observer_) { |
Harald Alvestrand | d7b79af | 2018-09-06 09:33:56 +0200 | [diff] [blame] | 175 | observer_->OnToneChange(std::string(), tones_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 176 | observer_->OnToneChange(std::string()); |
| 177 | } |
| 178 | return; |
| 179 | } else { |
| 180 | char tone = tones_[first_tone_pos]; |
| 181 | if (!GetDtmfCode(tone, &code)) { |
| 182 | // The find_first_of(kDtmfValidTones) should have guarantee |tone| is |
| 183 | // a valid DTMF tone. |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 184 | RTC_NOTREACHED(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | int tone_gap = inter_tone_gap_; |
Aaron Alaniz | 529d886 | 2020-01-21 03:09:47 +0000 | [diff] [blame] | 189 | if (code == kDtmfCommaDelay) { |
| 190 | // Special case defined by WebRTC - By default, the character ',' indicates |
| 191 | // a delay of 2 seconds before processing the next character in the tones |
| 192 | // parameter. The comma delay can be set to a non default value via |
| 193 | // InsertDtmf to comply with legacy WebRTC clients. |
| 194 | tone_gap = comma_delay_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 195 | } else { |
| 196 | if (!provider_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 197 | RTC_LOG(LS_ERROR) << "The DtmfProvider has been destroyed."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 198 | return; |
| 199 | } |
| 200 | // The provider starts playout of the given tone on the |
| 201 | // associated RTP media stream, using the appropriate codec. |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 202 | if (!provider_->InsertDtmf(code, duration_)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 203 | RTC_LOG(LS_ERROR) << "The DtmfProvider can no longer send DTMF."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 204 | return; |
| 205 | } |
| 206 | // Wait for the number of milliseconds specified by |duration_|. |
| 207 | tone_gap += duration_; |
| 208 | } |
| 209 | |
| 210 | // Fire a “OnToneChange” event with the tone that's just processed. |
| 211 | if (observer_) { |
Harald Alvestrand | d7b79af | 2018-09-06 09:33:56 +0200 | [diff] [blame] | 212 | observer_->OnToneChange(tones_.substr(first_tone_pos, 1), |
| 213 | tones_.substr(first_tone_pos + 1)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 214 | observer_->OnToneChange(tones_.substr(first_tone_pos, 1)); |
| 215 | } |
| 216 | |
| 217 | // Erase the unrecognized characters plus the tone that's just processed. |
| 218 | tones_.erase(0, first_tone_pos + 1); |
| 219 | |
| 220 | // Continue with the next tone. |
Steve Anton | 9a4fd9b | 2018-08-31 15:20:10 -0700 | [diff] [blame] | 221 | QueueInsertDtmf(RTC_FROM_HERE, tone_gap); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void DtmfSender::OnProviderDestroyed() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 225 | RTC_LOG(LS_INFO) << "The Dtmf provider is deleted. Clear the sending queue."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 226 | StopSending(); |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 227 | provider_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void DtmfSender::StopSending() { |
Steve Anton | 9a4fd9b | 2018-08-31 15:20:10 -0700 | [diff] [blame] | 231 | dtmf_driver_.Clear(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | } // namespace webrtc |