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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/dtmfsender.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
| 13 | #include <ctype.h> |
| 14 | |
| 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" |
| 19 | #include "rtc_base/thread.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | enum { |
| 24 | MSG_DO_INSERT_DTMF = 0, |
| 25 | }; |
| 26 | |
| 27 | // RFC4733 |
| 28 | // +-------+--------+------+---------+ |
| 29 | // | Event | Code | Type | Volume? | |
| 30 | // +-------+--------+------+---------+ |
| 31 | // | 0--9 | 0--9 | tone | yes | |
| 32 | // | * | 10 | tone | yes | |
| 33 | // | # | 11 | tone | yes | |
| 34 | // | A--D | 12--15 | tone | yes | |
| 35 | // +-------+--------+------+---------+ |
| 36 | // The "," is a special event defined by the WebRTC spec. It means to delay for |
| 37 | // 2 seconds before processing the next tone. We use -1 as its code. |
| 38 | static const int kDtmfCodeTwoSecondDelay = -1; |
| 39 | static const int kDtmfTwoSecondInMs = 2000; |
| 40 | static const char kDtmfValidTones[] = ",0123456789*#ABCDabcd"; |
| 41 | static const char kDtmfTonesTable[] = ",0123456789*#ABCD"; |
dminor | 588101c | 2017-03-28 11:18:32 -0700 | [diff] [blame] | 42 | // 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] | 43 | // tones must be at least 50 ms. |
| 44 | static const int kDtmfDefaultDurationMs = 100; |
dminor | 588101c | 2017-03-28 11:18:32 -0700 | [diff] [blame] | 45 | static const int kDtmfMinDurationMs = 40; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 46 | static const int kDtmfMaxDurationMs = 6000; |
| 47 | static const int kDtmfDefaultGapMs = 50; |
| 48 | static const int kDtmfMinGapMs = 50; |
| 49 | |
| 50 | // Get DTMF code from the DTMF event character. |
| 51 | bool GetDtmfCode(char tone, int* code) { |
| 52 | // Convert a-d to A-D. |
| 53 | char event = toupper(tone); |
| 54 | const char* p = strchr(kDtmfTonesTable, event); |
| 55 | if (!p) { |
| 56 | return false; |
| 57 | } |
| 58 | *code = p - kDtmfTonesTable - 1; |
| 59 | return true; |
| 60 | } |
| 61 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 62 | rtc::scoped_refptr<DtmfSender> DtmfSender::Create( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 63 | AudioTrackInterface* track, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 64 | rtc::Thread* signaling_thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 65 | DtmfProviderInterface* provider) { |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 66 | if (!signaling_thread) { |
| 67 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 69 | rtc::scoped_refptr<DtmfSender> dtmf_sender( |
| 70 | new rtc::RefCountedObject<DtmfSender>(track, signaling_thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 71 | provider)); |
| 72 | return dtmf_sender; |
| 73 | } |
| 74 | |
| 75 | DtmfSender::DtmfSender(AudioTrackInterface* track, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 76 | rtc::Thread* signaling_thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 77 | DtmfProviderInterface* provider) |
| 78 | : track_(track), |
| 79 | observer_(NULL), |
| 80 | signaling_thread_(signaling_thread), |
| 81 | provider_(provider), |
| 82 | duration_(kDtmfDefaultDurationMs), |
| 83 | inter_tone_gap_(kDtmfDefaultGapMs) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 84 | RTC_DCHECK(signaling_thread_ != NULL); |
deadbeef | 057ecf0 | 2016-01-20 14:30:43 -0800 | [diff] [blame] | 85 | // TODO(deadbeef): Once we can use shared_ptr and weak_ptr, |
| 86 | // do that instead of relying on a "destroyed" signal. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 87 | if (provider_) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 88 | RTC_DCHECK(provider_->GetOnDestroyedSignal() != NULL); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | provider_->GetOnDestroyedSignal()->connect( |
| 90 | this, &DtmfSender::OnProviderDestroyed); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | DtmfSender::~DtmfSender() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | StopSending(); |
| 96 | } |
| 97 | |
| 98 | void DtmfSender::RegisterObserver(DtmfSenderObserverInterface* observer) { |
| 99 | observer_ = observer; |
| 100 | } |
| 101 | |
| 102 | void DtmfSender::UnregisterObserver() { |
| 103 | observer_ = NULL; |
| 104 | } |
| 105 | |
| 106 | bool DtmfSender::CanInsertDtmf() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 107 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 108 | if (!provider_) { |
| 109 | return false; |
| 110 | } |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 111 | return provider_->CanInsertDtmf(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | bool DtmfSender::InsertDtmf(const std::string& tones, int duration, |
| 115 | int inter_tone_gap) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 116 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 117 | |
| 118 | if (duration > kDtmfMaxDurationMs || |
| 119 | duration < kDtmfMinDurationMs || |
| 120 | inter_tone_gap < kDtmfMinGapMs) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 121 | RTC_LOG(LS_ERROR) |
| 122 | << "InsertDtmf is called with invalid duration or tones gap. " |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 123 | << "The duration cannot be more than " << kDtmfMaxDurationMs |
| 124 | << "ms or less than " << kDtmfMinDurationMs << "ms. " |
| 125 | << "The gap between tones must be at least " << kDtmfMinGapMs << "ms."; |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | if (!CanInsertDtmf()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 130 | RTC_LOG(LS_ERROR) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 131 | << "InsertDtmf is called on DtmfSender that can't send DTMF."; |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | tones_ = tones; |
| 136 | duration_ = duration; |
| 137 | inter_tone_gap_ = inter_tone_gap; |
| 138 | // Clear the previous queue. |
| 139 | signaling_thread_->Clear(this, MSG_DO_INSERT_DTMF); |
| 140 | // Kick off a new DTMF task queue. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 141 | signaling_thread_->Post(RTC_FROM_HERE, this, MSG_DO_INSERT_DTMF); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 142 | return true; |
| 143 | } |
| 144 | |
| 145 | const AudioTrackInterface* DtmfSender::track() const { |
| 146 | return track_; |
| 147 | } |
| 148 | |
| 149 | std::string DtmfSender::tones() const { |
| 150 | return tones_; |
| 151 | } |
| 152 | |
| 153 | int DtmfSender::duration() const { |
| 154 | return duration_; |
| 155 | } |
| 156 | |
| 157 | int DtmfSender::inter_tone_gap() const { |
| 158 | return inter_tone_gap_; |
| 159 | } |
| 160 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 161 | void DtmfSender::OnMessage(rtc::Message* msg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 162 | switch (msg->message_id) { |
| 163 | case MSG_DO_INSERT_DTMF: { |
| 164 | DoInsertDtmf(); |
| 165 | break; |
| 166 | } |
| 167 | default: { |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 168 | RTC_NOTREACHED(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 169 | break; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void DtmfSender::DoInsertDtmf() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 175 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 176 | |
| 177 | // Get the first DTMF tone from the tone buffer. Unrecognized characters will |
| 178 | // be ignored and skipped. |
| 179 | size_t first_tone_pos = tones_.find_first_of(kDtmfValidTones); |
| 180 | int code = 0; |
| 181 | if (first_tone_pos == std::string::npos) { |
| 182 | tones_.clear(); |
| 183 | // Fire a “OnToneChange” event with an empty string and stop. |
| 184 | if (observer_) { |
| 185 | observer_->OnToneChange(std::string()); |
| 186 | } |
| 187 | return; |
| 188 | } else { |
| 189 | char tone = tones_[first_tone_pos]; |
| 190 | if (!GetDtmfCode(tone, &code)) { |
| 191 | // The find_first_of(kDtmfValidTones) should have guarantee |tone| is |
| 192 | // a valid DTMF tone. |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 193 | RTC_NOTREACHED(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | int tone_gap = inter_tone_gap_; |
| 198 | if (code == kDtmfCodeTwoSecondDelay) { |
| 199 | // Special case defined by WebRTC - The character',' indicates a delay of 2 |
| 200 | // seconds before processing the next character in the tones parameter. |
| 201 | tone_gap = kDtmfTwoSecondInMs; |
| 202 | } else { |
| 203 | if (!provider_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 204 | RTC_LOG(LS_ERROR) << "The DtmfProvider has been destroyed."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | // The provider starts playout of the given tone on the |
| 208 | // associated RTP media stream, using the appropriate codec. |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 209 | if (!provider_->InsertDtmf(code, duration_)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 210 | RTC_LOG(LS_ERROR) << "The DtmfProvider can no longer send DTMF."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 211 | return; |
| 212 | } |
| 213 | // Wait for the number of milliseconds specified by |duration_|. |
| 214 | tone_gap += duration_; |
| 215 | } |
| 216 | |
| 217 | // Fire a “OnToneChange” event with the tone that's just processed. |
| 218 | if (observer_) { |
| 219 | observer_->OnToneChange(tones_.substr(first_tone_pos, 1)); |
| 220 | } |
| 221 | |
| 222 | // Erase the unrecognized characters plus the tone that's just processed. |
| 223 | tones_.erase(0, first_tone_pos + 1); |
| 224 | |
| 225 | // Continue with the next tone. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 226 | signaling_thread_->PostDelayed(RTC_FROM_HERE, tone_gap, this, |
| 227 | MSG_DO_INSERT_DTMF); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void DtmfSender::OnProviderDestroyed() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 231 | 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] | 232 | StopSending(); |
| 233 | provider_ = NULL; |
| 234 | } |
| 235 | |
| 236 | void DtmfSender::StopSending() { |
| 237 | signaling_thread_->Clear(this); |
| 238 | } |
| 239 | |
| 240 | } // namespace webrtc |