blob: 78a73c4b3769777aa22716b382522984b463782b [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/dtmfsender.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
13#include <ctype.h>
14
15#include <string>
16
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000017#include "webrtc/base/logging.h"
18#include "webrtc/base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
20namespace webrtc {
21
22enum {
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.
37static const int kDtmfCodeTwoSecondDelay = -1;
38static const int kDtmfTwoSecondInMs = 2000;
39static const char kDtmfValidTones[] = ",0123456789*#ABCDabcd";
40static 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.
43static const int kDtmfDefaultDurationMs = 100;
44static const int kDtmfMinDurationMs = 70;
45static const int kDtmfMaxDurationMs = 6000;
46static const int kDtmfDefaultGapMs = 50;
47static const int kDtmfMinGapMs = 50;
48
49// Get DTMF code from the DTMF event character.
50bool 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.orgd4e598d2014-07-29 17:36:52 +000061rtc::scoped_refptr<DtmfSender> DtmfSender::Create(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 AudioTrackInterface* track,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000063 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 DtmfProviderInterface* provider) {
65 if (!track || !signaling_thread) {
66 return NULL;
67 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000068 rtc::scoped_refptr<DtmfSender> dtmf_sender(
69 new rtc::RefCountedObject<DtmfSender>(track, signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 provider));
71 return dtmf_sender;
72}
73
74DtmfSender::DtmfSender(AudioTrackInterface* track,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000075 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 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);
deadbeef057ecf02016-01-20 14:30:43 -080085 // TODO(deadbeef): Once we can use shared_ptr and weak_ptr,
86 // do that instead of relying on a "destroyed" signal.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 if (provider_) {
88 ASSERT(provider_->GetOnDestroyedSignal() != NULL);
89 provider_->GetOnDestroyedSignal()->connect(
90 this, &DtmfSender::OnProviderDestroyed);
91 }
92}
93
94DtmfSender::~DtmfSender() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 StopSending();
96}
97
98void DtmfSender::RegisterObserver(DtmfSenderObserverInterface* observer) {
99 observer_ = observer;
100}
101
102void DtmfSender::UnregisterObserver() {
103 observer_ = NULL;
104}
105
106bool DtmfSender::CanInsertDtmf() {
107 ASSERT(signaling_thread_->IsCurrent());
108 if (!provider_) {
109 return false;
110 }
111 return provider_->CanInsertDtmf(track_->id());
112}
113
114bool 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
144const AudioTrackInterface* DtmfSender::track() const {
145 return track_;
146}
147
148std::string DtmfSender::tones() const {
149 return tones_;
150}
151
152int DtmfSender::duration() const {
153 return duration_;
154}
155
156int DtmfSender::inter_tone_gap() const {
157 return inter_tone_gap_;
158}
159
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000160void DtmfSender::OnMessage(rtc::Message* msg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 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
173void 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
228void DtmfSender::OnProviderDestroyed() {
229 LOG(LS_INFO) << "The Dtmf provider is deleted. Clear the sending queue.";
230 StopSending();
231 provider_ = NULL;
232}
233
234void DtmfSender::StopSending() {
235 signaling_thread_->Clear(this);
236}
237
238} // namespace webrtc