blob: 82644aacf0acb801962b506f0df24a38b563f04a [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) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100121 RTC_LOG(LS_ERROR)
122 << "InsertDtmf is called with invalid duration or tones gap. "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 << "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()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100130 RTC_LOG(LS_ERROR)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 << "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() {
nisseede5da42017-01-12 05:15:36 -0800175 RTC_DCHECK(signaling_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176
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_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100204 RTC_LOG(LS_ERROR) << "The DtmfProvider has been destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 return;
206 }
207 // The provider starts playout of the given tone on the
208 // associated RTP media stream, using the appropriate codec.
deadbeef20cb0c12017-02-01 20:27:00 -0800209 if (!provider_->InsertDtmf(code, duration_)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100210 RTC_LOG(LS_ERROR) << "The DtmfProvider can no longer send DTMF.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 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() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100231 RTC_LOG(LS_INFO) << "The Dtmf provider is deleted. Clear the sending queue.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 StopSending();
233 provider_ = NULL;
234}
235
236void DtmfSender::StopSending() {
237 signaling_thread_->Clear(this);
238}
239
240} // namespace webrtc