henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2012 Google Inc. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame^] | 28 | #include "webrtc/api/dtmfsender.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | |
| 30 | #include <ctype.h> |
| 31 | |
| 32 | #include <string> |
| 33 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 34 | #include "webrtc/base/logging.h" |
| 35 | #include "webrtc/base/thread.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | |
| 37 | namespace webrtc { |
| 38 | |
| 39 | enum { |
| 40 | MSG_DO_INSERT_DTMF = 0, |
| 41 | }; |
| 42 | |
| 43 | // RFC4733 |
| 44 | // +-------+--------+------+---------+ |
| 45 | // | Event | Code | Type | Volume? | |
| 46 | // +-------+--------+------+---------+ |
| 47 | // | 0--9 | 0--9 | tone | yes | |
| 48 | // | * | 10 | tone | yes | |
| 49 | // | # | 11 | tone | yes | |
| 50 | // | A--D | 12--15 | tone | yes | |
| 51 | // +-------+--------+------+---------+ |
| 52 | // The "," is a special event defined by the WebRTC spec. It means to delay for |
| 53 | // 2 seconds before processing the next tone. We use -1 as its code. |
| 54 | static const int kDtmfCodeTwoSecondDelay = -1; |
| 55 | static const int kDtmfTwoSecondInMs = 2000; |
| 56 | static const char kDtmfValidTones[] = ",0123456789*#ABCDabcd"; |
| 57 | static const char kDtmfTonesTable[] = ",0123456789*#ABCD"; |
| 58 | // The duration cannot be more than 6000ms or less than 70ms. The gap between |
| 59 | // tones must be at least 50 ms. |
| 60 | static const int kDtmfDefaultDurationMs = 100; |
| 61 | static const int kDtmfMinDurationMs = 70; |
| 62 | static const int kDtmfMaxDurationMs = 6000; |
| 63 | static const int kDtmfDefaultGapMs = 50; |
| 64 | static const int kDtmfMinGapMs = 50; |
| 65 | |
| 66 | // Get DTMF code from the DTMF event character. |
| 67 | bool GetDtmfCode(char tone, int* code) { |
| 68 | // Convert a-d to A-D. |
| 69 | char event = toupper(tone); |
| 70 | const char* p = strchr(kDtmfTonesTable, event); |
| 71 | if (!p) { |
| 72 | return false; |
| 73 | } |
| 74 | *code = p - kDtmfTonesTable - 1; |
| 75 | return true; |
| 76 | } |
| 77 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 78 | rtc::scoped_refptr<DtmfSender> DtmfSender::Create( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | AudioTrackInterface* track, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 80 | rtc::Thread* signaling_thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 81 | DtmfProviderInterface* provider) { |
| 82 | if (!track || !signaling_thread) { |
| 83 | return NULL; |
| 84 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 85 | rtc::scoped_refptr<DtmfSender> dtmf_sender( |
| 86 | new rtc::RefCountedObject<DtmfSender>(track, signaling_thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 87 | provider)); |
| 88 | return dtmf_sender; |
| 89 | } |
| 90 | |
| 91 | DtmfSender::DtmfSender(AudioTrackInterface* track, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 92 | rtc::Thread* signaling_thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 93 | DtmfProviderInterface* provider) |
| 94 | : track_(track), |
| 95 | observer_(NULL), |
| 96 | signaling_thread_(signaling_thread), |
| 97 | provider_(provider), |
| 98 | duration_(kDtmfDefaultDurationMs), |
| 99 | inter_tone_gap_(kDtmfDefaultGapMs) { |
| 100 | ASSERT(track_ != NULL); |
| 101 | ASSERT(signaling_thread_ != NULL); |
deadbeef | 057ecf0 | 2016-01-20 14:30:43 -0800 | [diff] [blame] | 102 | // TODO(deadbeef): Once we can use shared_ptr and weak_ptr, |
| 103 | // do that instead of relying on a "destroyed" signal. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | if (provider_) { |
| 105 | ASSERT(provider_->GetOnDestroyedSignal() != NULL); |
| 106 | provider_->GetOnDestroyedSignal()->connect( |
| 107 | this, &DtmfSender::OnProviderDestroyed); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | DtmfSender::~DtmfSender() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | StopSending(); |
| 113 | } |
| 114 | |
| 115 | void DtmfSender::RegisterObserver(DtmfSenderObserverInterface* observer) { |
| 116 | observer_ = observer; |
| 117 | } |
| 118 | |
| 119 | void DtmfSender::UnregisterObserver() { |
| 120 | observer_ = NULL; |
| 121 | } |
| 122 | |
| 123 | bool DtmfSender::CanInsertDtmf() { |
| 124 | ASSERT(signaling_thread_->IsCurrent()); |
| 125 | if (!provider_) { |
| 126 | return false; |
| 127 | } |
| 128 | return provider_->CanInsertDtmf(track_->id()); |
| 129 | } |
| 130 | |
| 131 | bool DtmfSender::InsertDtmf(const std::string& tones, int duration, |
| 132 | int inter_tone_gap) { |
| 133 | ASSERT(signaling_thread_->IsCurrent()); |
| 134 | |
| 135 | if (duration > kDtmfMaxDurationMs || |
| 136 | duration < kDtmfMinDurationMs || |
| 137 | inter_tone_gap < kDtmfMinGapMs) { |
| 138 | LOG(LS_ERROR) << "InsertDtmf is called with invalid duration or tones gap. " |
| 139 | << "The duration cannot be more than " << kDtmfMaxDurationMs |
| 140 | << "ms or less than " << kDtmfMinDurationMs << "ms. " |
| 141 | << "The gap between tones must be at least " << kDtmfMinGapMs << "ms."; |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | if (!CanInsertDtmf()) { |
| 146 | LOG(LS_ERROR) |
| 147 | << "InsertDtmf is called on DtmfSender that can't send DTMF."; |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | tones_ = tones; |
| 152 | duration_ = duration; |
| 153 | inter_tone_gap_ = inter_tone_gap; |
| 154 | // Clear the previous queue. |
| 155 | signaling_thread_->Clear(this, MSG_DO_INSERT_DTMF); |
| 156 | // Kick off a new DTMF task queue. |
| 157 | signaling_thread_->Post(this, MSG_DO_INSERT_DTMF); |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | const AudioTrackInterface* DtmfSender::track() const { |
| 162 | return track_; |
| 163 | } |
| 164 | |
| 165 | std::string DtmfSender::tones() const { |
| 166 | return tones_; |
| 167 | } |
| 168 | |
| 169 | int DtmfSender::duration() const { |
| 170 | return duration_; |
| 171 | } |
| 172 | |
| 173 | int DtmfSender::inter_tone_gap() const { |
| 174 | return inter_tone_gap_; |
| 175 | } |
| 176 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 177 | void DtmfSender::OnMessage(rtc::Message* msg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 178 | switch (msg->message_id) { |
| 179 | case MSG_DO_INSERT_DTMF: { |
| 180 | DoInsertDtmf(); |
| 181 | break; |
| 182 | } |
| 183 | default: { |
| 184 | ASSERT(false); |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void DtmfSender::DoInsertDtmf() { |
| 191 | ASSERT(signaling_thread_->IsCurrent()); |
| 192 | |
| 193 | // Get the first DTMF tone from the tone buffer. Unrecognized characters will |
| 194 | // be ignored and skipped. |
| 195 | size_t first_tone_pos = tones_.find_first_of(kDtmfValidTones); |
| 196 | int code = 0; |
| 197 | if (first_tone_pos == std::string::npos) { |
| 198 | tones_.clear(); |
| 199 | // Fire a “OnToneChange” event with an empty string and stop. |
| 200 | if (observer_) { |
| 201 | observer_->OnToneChange(std::string()); |
| 202 | } |
| 203 | return; |
| 204 | } else { |
| 205 | char tone = tones_[first_tone_pos]; |
| 206 | if (!GetDtmfCode(tone, &code)) { |
| 207 | // The find_first_of(kDtmfValidTones) should have guarantee |tone| is |
| 208 | // a valid DTMF tone. |
| 209 | ASSERT(false); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | int tone_gap = inter_tone_gap_; |
| 214 | if (code == kDtmfCodeTwoSecondDelay) { |
| 215 | // Special case defined by WebRTC - The character',' indicates a delay of 2 |
| 216 | // seconds before processing the next character in the tones parameter. |
| 217 | tone_gap = kDtmfTwoSecondInMs; |
| 218 | } else { |
| 219 | if (!provider_) { |
| 220 | LOG(LS_ERROR) << "The DtmfProvider has been destroyed."; |
| 221 | return; |
| 222 | } |
| 223 | // The provider starts playout of the given tone on the |
| 224 | // associated RTP media stream, using the appropriate codec. |
| 225 | if (!provider_->InsertDtmf(track_->id(), code, duration_)) { |
| 226 | LOG(LS_ERROR) << "The DtmfProvider can no longer send DTMF."; |
| 227 | return; |
| 228 | } |
| 229 | // Wait for the number of milliseconds specified by |duration_|. |
| 230 | tone_gap += duration_; |
| 231 | } |
| 232 | |
| 233 | // Fire a “OnToneChange” event with the tone that's just processed. |
| 234 | if (observer_) { |
| 235 | observer_->OnToneChange(tones_.substr(first_tone_pos, 1)); |
| 236 | } |
| 237 | |
| 238 | // Erase the unrecognized characters plus the tone that's just processed. |
| 239 | tones_.erase(0, first_tone_pos + 1); |
| 240 | |
| 241 | // Continue with the next tone. |
| 242 | signaling_thread_->PostDelayed(tone_gap, this, MSG_DO_INSERT_DTMF); |
| 243 | } |
| 244 | |
| 245 | void DtmfSender::OnProviderDestroyed() { |
| 246 | LOG(LS_INFO) << "The Dtmf provider is deleted. Clear the sending queue."; |
| 247 | StopSending(); |
| 248 | provider_ = NULL; |
| 249 | } |
| 250 | |
| 251 | void DtmfSender::StopSending() { |
| 252 | signaling_thread_->Clear(this); |
| 253 | } |
| 254 | |
| 255 | } // namespace webrtc |