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