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> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 15 | #include <string> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
| 18 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/ref_counted_object.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/thread.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 24 | // RFC4733 |
| 25 | // +-------+--------+------+---------+ |
| 26 | // | Event | Code | Type | Volume? | |
| 27 | // +-------+--------+------+---------+ |
| 28 | // | 0--9 | 0--9 | tone | yes | |
| 29 | // | * | 10 | tone | yes | |
| 30 | // | # | 11 | tone | yes | |
| 31 | // | A--D | 12--15 | tone | yes | |
| 32 | // +-------+--------+------+---------+ |
| 33 | // The "," is a special event defined by the WebRTC spec. It means to delay for |
| 34 | // 2 seconds before processing the next tone. We use -1 as its code. |
| 35 | static const int kDtmfCodeTwoSecondDelay = -1; |
| 36 | static const int kDtmfTwoSecondInMs = 2000; |
| 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), |
| 78 | inter_tone_gap_(kDtmfDefaultGapMs) { |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 79 | RTC_DCHECK(signaling_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 80 | if (provider_) { |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 81 | RTC_DCHECK(provider_->GetOnDestroyedSignal()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | provider_->GetOnDestroyedSignal()->connect( |
| 83 | this, &DtmfSender::OnProviderDestroyed); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | DtmfSender::~DtmfSender() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 88 | StopSending(); |
| 89 | } |
| 90 | |
| 91 | void DtmfSender::RegisterObserver(DtmfSenderObserverInterface* observer) { |
| 92 | observer_ = observer; |
| 93 | } |
| 94 | |
| 95 | void DtmfSender::UnregisterObserver() { |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 96 | observer_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool DtmfSender::CanInsertDtmf() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 100 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 101 | if (!provider_) { |
| 102 | return false; |
| 103 | } |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 104 | return provider_->CanInsertDtmf(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 107 | bool DtmfSender::InsertDtmf(const std::string& tones, |
| 108 | int duration, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | int inter_tone_gap) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 110 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 111 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 112 | if (duration > kDtmfMaxDurationMs || duration < kDtmfMinDurationMs || |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | inter_tone_gap < kDtmfMinGapMs) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 114 | RTC_LOG(LS_ERROR) |
| 115 | << "InsertDtmf is called with invalid duration or tones gap. " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 116 | "The duration cannot be more than " |
| 117 | << kDtmfMaxDurationMs << "ms or less than " << kDtmfMinDurationMs |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 118 | << "ms. The gap between tones must be at least " << kDtmfMinGapMs |
| 119 | << "ms."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 120 | return false; |
| 121 | } |
| 122 | |
| 123 | if (!CanInsertDtmf()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 124 | RTC_LOG(LS_ERROR) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 125 | << "InsertDtmf is called on DtmfSender that can't send DTMF."; |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | tones_ = tones; |
| 130 | duration_ = duration; |
| 131 | inter_tone_gap_ = inter_tone_gap; |
| 132 | // Clear the previous queue. |
Steve Anton | 9a4fd9b | 2018-08-31 15:20:10 -0700 | [diff] [blame] | 133 | dtmf_driver_.Clear(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 134 | // Kick off a new DTMF task queue. |
Steve Anton | 9a4fd9b | 2018-08-31 15:20:10 -0700 | [diff] [blame] | 135 | QueueInsertDtmf(RTC_FROM_HERE, 1 /*ms*/); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 139 | std::string DtmfSender::tones() const { |
| 140 | return tones_; |
| 141 | } |
| 142 | |
| 143 | int DtmfSender::duration() const { |
| 144 | return duration_; |
| 145 | } |
| 146 | |
| 147 | int DtmfSender::inter_tone_gap() const { |
| 148 | return inter_tone_gap_; |
| 149 | } |
| 150 | |
Steve Anton | 9a4fd9b | 2018-08-31 15:20:10 -0700 | [diff] [blame] | 151 | void DtmfSender::QueueInsertDtmf(const rtc::Location& posted_from, |
| 152 | uint32_t delay_ms) { |
| 153 | dtmf_driver_.AsyncInvokeDelayed<void>(posted_from, signaling_thread_, |
| 154 | [this] { DoInsertDtmf(); }, delay_ms); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void DtmfSender::DoInsertDtmf() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 158 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 159 | |
| 160 | // Get the first DTMF tone from the tone buffer. Unrecognized characters will |
| 161 | // be ignored and skipped. |
| 162 | size_t first_tone_pos = tones_.find_first_of(kDtmfValidTones); |
| 163 | int code = 0; |
| 164 | if (first_tone_pos == std::string::npos) { |
| 165 | tones_.clear(); |
| 166 | // Fire a “OnToneChange” event with an empty string and stop. |
| 167 | if (observer_) { |
Harald Alvestrand | d7b79af | 2018-09-06 09:33:56 +0200 | [diff] [blame] | 168 | observer_->OnToneChange(std::string(), tones_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 169 | observer_->OnToneChange(std::string()); |
| 170 | } |
| 171 | return; |
| 172 | } else { |
| 173 | char tone = tones_[first_tone_pos]; |
| 174 | if (!GetDtmfCode(tone, &code)) { |
| 175 | // The find_first_of(kDtmfValidTones) should have guarantee |tone| is |
| 176 | // a valid DTMF tone. |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 177 | RTC_NOTREACHED(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | int tone_gap = inter_tone_gap_; |
| 182 | if (code == kDtmfCodeTwoSecondDelay) { |
| 183 | // Special case defined by WebRTC - The character',' indicates a delay of 2 |
| 184 | // seconds before processing the next character in the tones parameter. |
| 185 | tone_gap = kDtmfTwoSecondInMs; |
| 186 | } else { |
| 187 | if (!provider_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 188 | RTC_LOG(LS_ERROR) << "The DtmfProvider has been destroyed."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 189 | return; |
| 190 | } |
| 191 | // The provider starts playout of the given tone on the |
| 192 | // associated RTP media stream, using the appropriate codec. |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 193 | if (!provider_->InsertDtmf(code, duration_)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 194 | RTC_LOG(LS_ERROR) << "The DtmfProvider can no longer send DTMF."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | // Wait for the number of milliseconds specified by |duration_|. |
| 198 | tone_gap += duration_; |
| 199 | } |
| 200 | |
| 201 | // Fire a “OnToneChange” event with the tone that's just processed. |
| 202 | if (observer_) { |
Harald Alvestrand | d7b79af | 2018-09-06 09:33:56 +0200 | [diff] [blame] | 203 | observer_->OnToneChange(tones_.substr(first_tone_pos, 1), |
| 204 | tones_.substr(first_tone_pos + 1)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 205 | observer_->OnToneChange(tones_.substr(first_tone_pos, 1)); |
| 206 | } |
| 207 | |
| 208 | // Erase the unrecognized characters plus the tone that's just processed. |
| 209 | tones_.erase(0, first_tone_pos + 1); |
| 210 | |
| 211 | // Continue with the next tone. |
Steve Anton | 9a4fd9b | 2018-08-31 15:20:10 -0700 | [diff] [blame] | 212 | QueueInsertDtmf(RTC_FROM_HERE, tone_gap); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void DtmfSender::OnProviderDestroyed() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 216 | 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] | 217 | StopSending(); |
Steve Anton | b983bae | 2018-06-20 11:16:53 -0700 | [diff] [blame] | 218 | provider_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void DtmfSender::StopSending() { |
Steve Anton | 9a4fd9b | 2018-08-31 15:20:10 -0700 | [diff] [blame] | 222 | dtmf_driver_.Clear(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | } // namespace webrtc |