blob: 14002069a993bc5d95d94e383586a474ad833611 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/dtmfsender.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
13#include <ctype.h>
14
15#include <string>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
18#include "rtc_base/logging.h"
19#include "rtc_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";
dminor588101c2017-03-28 11:18:32 -070042// The duration cannot be more than 6000ms or less than 40ms. The gap between
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043// tones must be at least 50 ms.
44static const int kDtmfDefaultDurationMs = 100;
dminor588101c2017-03-28 11:18:32 -070045static const int kDtmfMinDurationMs = 40;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046static 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) {
deadbeef20cb0c12017-02-01 20:27:00 -080066 if (!signaling_thread) {
67 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 }
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) {
nisseede5da42017-01-12 05:15:36 -080084 RTC_DCHECK(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_) {
nisseede5da42017-01-12 05:15:36 -080088 RTC_DCHECK(provider_->GetOnDestroyedSignal() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 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() {
nisseede5da42017-01-12 05:15:36 -0800107 RTC_DCHECK(signaling_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108 if (!provider_) {
109 return false;
110 }
deadbeef20cb0c12017-02-01 20:27:00 -0800111 return provider_->CanInsertDtmf();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112}
113
114bool DtmfSender::InsertDtmf(const std::string& tones, int duration,
115 int inter_tone_gap) {
nisseede5da42017-01-12 05:15:36 -0800116 RTC_DCHECK(signaling_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117
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.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700140 signaling_thread_->Post(RTC_FROM_HERE, this, MSG_DO_INSERT_DTMF);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 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: {
nissec80e7412017-01-11 05:56:46 -0800167 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 break;
169 }
170 }
171}
172
173void DtmfSender::DoInsertDtmf() {
nisseede5da42017-01-12 05:15:36 -0800174 RTC_DCHECK(signaling_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175
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.
nissec80e7412017-01-11 05:56:46 -0800192 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 }
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.
deadbeef20cb0c12017-02-01 20:27:00 -0800208 if (!provider_->InsertDtmf(code, duration_)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209 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.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700225 signaling_thread_->PostDelayed(RTC_FROM_HERE, tone_gap, this,
226 MSG_DO_INSERT_DTMF);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227}
228
229void DtmfSender::OnProviderDestroyed() {
230 LOG(LS_INFO) << "The Dtmf provider is deleted. Clear the sending queue.";
231 StopSending();
232 provider_ = NULL;
233}
234
235void DtmfSender::StopSending() {
236 signaling_thread_->Clear(this);
237}
238
239} // namespace webrtc