blob: 8fda78430a40b242defd6b09cfb0df31281f32d1 [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
nissec80e7412017-01-11 05:56:46 -080017#include "webrtc/base/checks.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000018#include "webrtc/base/logging.h"
19#include "webrtc/base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
21namespace webrtc {
22
23enum {
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.
38static const int kDtmfCodeTwoSecondDelay = -1;
39static const int kDtmfTwoSecondInMs = 2000;
40static const char kDtmfValidTones[] = ",0123456789*#ABCDabcd";
41static 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.
44static const int kDtmfDefaultDurationMs = 100;
45static const int kDtmfMinDurationMs = 70;
46static const int kDtmfMaxDurationMs = 6000;
47static const int kDtmfDefaultGapMs = 50;
48static const int kDtmfMinGapMs = 50;
49
50// Get DTMF code from the DTMF event character.
51bool 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.orgd4e598d2014-07-29 17:36:52 +000062rtc::scoped_refptr<DtmfSender> DtmfSender::Create(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 AudioTrackInterface* track,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000064 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065 DtmfProviderInterface* provider) {
66 if (!track || !signaling_thread) {
67 return NULL;
68 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000069 rtc::scoped_refptr<DtmfSender> dtmf_sender(
70 new rtc::RefCountedObject<DtmfSender>(track, signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 provider));
72 return dtmf_sender;
73}
74
75DtmfSender::DtmfSender(AudioTrackInterface* track,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 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 ASSERT(track_ != NULL);
85 ASSERT(signaling_thread_ != NULL);
deadbeef057ecf02016-01-20 14:30:43 -080086 // TODO(deadbeef): Once we can use shared_ptr and weak_ptr,
87 // do that instead of relying on a "destroyed" signal.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 if (provider_) {
89 ASSERT(provider_->GetOnDestroyedSignal() != NULL);
90 provider_->GetOnDestroyedSignal()->connect(
91 this, &DtmfSender::OnProviderDestroyed);
92 }
93}
94
95DtmfSender::~DtmfSender() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 StopSending();
97}
98
99void DtmfSender::RegisterObserver(DtmfSenderObserverInterface* observer) {
100 observer_ = observer;
101}
102
103void DtmfSender::UnregisterObserver() {
104 observer_ = NULL;
105}
106
107bool DtmfSender::CanInsertDtmf() {
108 ASSERT(signaling_thread_->IsCurrent());
109 if (!provider_) {
110 return false;
111 }
112 return provider_->CanInsertDtmf(track_->id());
113}
114
115bool DtmfSender::InsertDtmf(const std::string& tones, int duration,
116 int inter_tone_gap) {
117 ASSERT(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.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700141 signaling_thread_->Post(RTC_FROM_HERE, this, MSG_DO_INSERT_DTMF);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142 return true;
143}
144
145const AudioTrackInterface* DtmfSender::track() const {
146 return track_;
147}
148
149std::string DtmfSender::tones() const {
150 return tones_;
151}
152
153int DtmfSender::duration() const {
154 return duration_;
155}
156
157int DtmfSender::inter_tone_gap() const {
158 return inter_tone_gap_;
159}
160
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000161void DtmfSender::OnMessage(rtc::Message* msg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 switch (msg->message_id) {
163 case MSG_DO_INSERT_DTMF: {
164 DoInsertDtmf();
165 break;
166 }
167 default: {
nissec80e7412017-01-11 05:56:46 -0800168 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 break;
170 }
171 }
172}
173
174void DtmfSender::DoInsertDtmf() {
175 ASSERT(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.
nissec80e7412017-01-11 05:56:46 -0800193 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 }
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.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700226 signaling_thread_->PostDelayed(RTC_FROM_HERE, tone_gap, this,
227 MSG_DO_INSERT_DTMF);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228}
229
230void DtmfSender::OnProviderDestroyed() {
231 LOG(LS_INFO) << "The Dtmf provider is deleted. Clear the sending queue.";
232 StopSending();
233 provider_ = NULL;
234}
235
236void DtmfSender::StopSending() {
237 signaling_thread_->Clear(this);
238}
239
240} // namespace webrtc