OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved. | |
3 * | |
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. | |
9 */ | |
10 | |
11 #include "webrtc/api/dtmfsender.h" | |
12 | |
13 #include <ctype.h> | |
14 | |
15 #include <string> | |
16 | |
17 #include "webrtc/base/checks.h" | |
18 #include "webrtc/base/logging.h" | |
19 #include "webrtc/base/thread.h" | |
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"; | |
42 // The duration cannot be more than 6000ms or less than 70ms. The gap between | |
43 // tones must be at least 50 ms. | |
44 static const int kDtmfDefaultDurationMs = 100; | |
45 static const int kDtmfMinDurationMs = 70; | |
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 | |
62 rtc::scoped_refptr<DtmfSender> DtmfSender::Create( | |
63 AudioTrackInterface* track, | |
64 rtc::Thread* signaling_thread, | |
65 DtmfProviderInterface* provider) { | |
66 if (!track || !signaling_thread) { | |
67 return NULL; | |
68 } | |
69 rtc::scoped_refptr<DtmfSender> dtmf_sender( | |
70 new rtc::RefCountedObject<DtmfSender>(track, signaling_thread, | |
71 provider)); | |
72 return dtmf_sender; | |
73 } | |
74 | |
75 DtmfSender::DtmfSender(AudioTrackInterface* track, | |
76 rtc::Thread* signaling_thread, | |
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) { | |
84 RTC_DCHECK(track_ != NULL); | |
85 RTC_DCHECK(signaling_thread_ != NULL); | |
86 // TODO(deadbeef): Once we can use shared_ptr and weak_ptr, | |
87 // do that instead of relying on a "destroyed" signal. | |
88 if (provider_) { | |
89 RTC_DCHECK(provider_->GetOnDestroyedSignal() != NULL); | |
90 provider_->GetOnDestroyedSignal()->connect( | |
91 this, &DtmfSender::OnProviderDestroyed); | |
92 } | |
93 } | |
94 | |
95 DtmfSender::~DtmfSender() { | |
96 StopSending(); | |
97 } | |
98 | |
99 void DtmfSender::RegisterObserver(DtmfSenderObserverInterface* observer) { | |
100 observer_ = observer; | |
101 } | |
102 | |
103 void DtmfSender::UnregisterObserver() { | |
104 observer_ = NULL; | |
105 } | |
106 | |
107 bool DtmfSender::CanInsertDtmf() { | |
108 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
109 if (!provider_) { | |
110 return false; | |
111 } | |
112 return provider_->CanInsertDtmf(track_->id()); | |
113 } | |
114 | |
115 bool DtmfSender::InsertDtmf(const std::string& tones, int duration, | |
116 int inter_tone_gap) { | |
117 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
118 | |
119 if (duration > kDtmfMaxDurationMs || | |
120 duration < kDtmfMinDurationMs || | |
121 inter_tone_gap < kDtmfMinGapMs) { | |
122 LOG(LS_ERROR) << "InsertDtmf is called with invalid duration or tones gap. " | |
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()) { | |
130 LOG(LS_ERROR) | |
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. | |
141 signaling_thread_->Post(RTC_FROM_HERE, this, MSG_DO_INSERT_DTMF); | |
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 | |
161 void DtmfSender::OnMessage(rtc::Message* msg) { | |
162 switch (msg->message_id) { | |
163 case MSG_DO_INSERT_DTMF: { | |
164 DoInsertDtmf(); | |
165 break; | |
166 } | |
167 default: { | |
168 RTC_NOTREACHED(); | |
169 break; | |
170 } | |
171 } | |
172 } | |
173 | |
174 void DtmfSender::DoInsertDtmf() { | |
175 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
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. | |
193 RTC_NOTREACHED(); | |
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_) { | |
204 LOG(LS_ERROR) << "The DtmfProvider has been destroyed."; | |
205 return; | |
206 } | |
207 // The provider starts playout of the given tone on the | |
208 // associated RTP media stream, using the appropriate codec. | |
209 if (!provider_->InsertDtmf(track_->id(), code, duration_)) { | |
210 LOG(LS_ERROR) << "The DtmfProvider can no longer send DTMF."; | |
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. | |
226 signaling_thread_->PostDelayed(RTC_FROM_HERE, tone_gap, this, | |
227 MSG_DO_INSERT_DTMF); | |
228 } | |
229 | |
230 void DtmfSender::OnProviderDestroyed() { | |
231 LOG(LS_INFO) << "The Dtmf provider is deleted. Clear the sending queue."; | |
232 StopSending(); | |
233 provider_ = NULL; | |
234 } | |
235 | |
236 void DtmfSender::StopSending() { | |
237 signaling_thread_->Clear(this); | |
238 } | |
239 | |
240 } // namespace webrtc | |
OLD | NEW |