blob: 8a17b031b746c7a834b04e8ea1bf5c61ed16d5e7 [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023// RFC4733
24// +-------+--------+------+---------+
25// | Event | Code | Type | Volume? |
26// +-------+--------+------+---------+
27// | 0--9 | 0--9 | tone | yes |
28// | * | 10 | tone | yes |
29// | # | 11 | tone | yes |
30// | A--D | 12--15 | tone | yes |
31// +-------+--------+------+---------+
32// The "," is a special event defined by the WebRTC spec. It means to delay for
33// 2 seconds before processing the next tone. We use -1 as its code.
34static const int kDtmfCodeTwoSecondDelay = -1;
35static const int kDtmfTwoSecondInMs = 2000;
36static const char kDtmfValidTones[] = ",0123456789*#ABCDabcd";
37static const char kDtmfTonesTable[] = ",0123456789*#ABCD";
dminor588101c2017-03-28 11:18:32 -070038// The duration cannot be more than 6000ms or less than 40ms. The gap between
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039// tones must be at least 50 ms.
Harald Alvestrand52e58522018-02-20 08:15:36 +010040// Source for values: W3C WEBRTC specification.
41// https://w3c.github.io/webrtc-pc/#dom-rtcdtmfsender-insertdtmf
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042static const int kDtmfDefaultDurationMs = 100;
dminor588101c2017-03-28 11:18:32 -070043static const int kDtmfMinDurationMs = 40;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044static const int kDtmfMaxDurationMs = 6000;
45static const int kDtmfDefaultGapMs = 50;
Harald Alvestrand52e58522018-02-20 08:15:36 +010046static const int kDtmfMinGapMs = 30;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047
48// Get DTMF code from the DTMF event character.
49bool GetDtmfCode(char tone, int* code) {
50 // Convert a-d to A-D.
51 char event = toupper(tone);
52 const char* p = strchr(kDtmfTonesTable, event);
53 if (!p) {
54 return false;
55 }
56 *code = p - kDtmfTonesTable - 1;
57 return true;
58}
59
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000060rtc::scoped_refptr<DtmfSender> DtmfSender::Create(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000061 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 DtmfProviderInterface* provider) {
deadbeef20cb0c12017-02-01 20:27:00 -080063 if (!signaling_thread) {
64 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000066 rtc::scoped_refptr<DtmfSender> dtmf_sender(
Steve Antonb983bae2018-06-20 11:16:53 -070067 new rtc::RefCountedObject<DtmfSender>(signaling_thread, provider));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 return dtmf_sender;
69}
70
Steve Antonb983bae2018-06-20 11:16:53 -070071DtmfSender::DtmfSender(rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 DtmfProviderInterface* provider)
Steve Antonb983bae2018-06-20 11:16:53 -070073 : observer_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 signaling_thread_(signaling_thread),
75 provider_(provider),
76 duration_(kDtmfDefaultDurationMs),
77 inter_tone_gap_(kDtmfDefaultGapMs) {
Steve Antonb983bae2018-06-20 11:16:53 -070078 RTC_DCHECK(signaling_thread_);
deadbeef057ecf02016-01-20 14:30:43 -080079 // TODO(deadbeef): Once we can use shared_ptr and weak_ptr,
80 // do that instead of relying on a "destroyed" signal.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 if (provider_) {
Steve Antonb983bae2018-06-20 11:16:53 -070082 RTC_DCHECK(provider_->GetOnDestroyedSignal());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 provider_->GetOnDestroyedSignal()->connect(
84 this, &DtmfSender::OnProviderDestroyed);
85 }
86}
87
88DtmfSender::~DtmfSender() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 StopSending();
90}
91
92void DtmfSender::RegisterObserver(DtmfSenderObserverInterface* observer) {
93 observer_ = observer;
94}
95
96void DtmfSender::UnregisterObserver() {
Steve Antonb983bae2018-06-20 11:16:53 -070097 observer_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098}
99
100bool DtmfSender::CanInsertDtmf() {
nisseede5da42017-01-12 05:15:36 -0800101 RTC_DCHECK(signaling_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 if (!provider_) {
103 return false;
104 }
deadbeef20cb0c12017-02-01 20:27:00 -0800105 return provider_->CanInsertDtmf();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106}
107
Yves Gerey665174f2018-06-19 15:03:05 +0200108bool DtmfSender::InsertDtmf(const std::string& tones,
109 int duration,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 int inter_tone_gap) {
nisseede5da42017-01-12 05:15:36 -0800111 RTC_DCHECK(signaling_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112
Yves Gerey665174f2018-06-19 15:03:05 +0200113 if (duration > kDtmfMaxDurationMs || duration < kDtmfMinDurationMs ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 inter_tone_gap < kDtmfMinGapMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100115 RTC_LOG(LS_ERROR)
116 << "InsertDtmf is called with invalid duration or tones gap. "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100117 "The duration cannot be more than "
118 << kDtmfMaxDurationMs << "ms or less than " << kDtmfMinDurationMs
Yves Gerey665174f2018-06-19 15:03:05 +0200119 << "ms. The gap between tones must be at least " << kDtmfMinGapMs
120 << "ms.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 return false;
122 }
123
124 if (!CanInsertDtmf()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100125 RTC_LOG(LS_ERROR)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 << "InsertDtmf is called on DtmfSender that can't send DTMF.";
127 return false;
128 }
129
130 tones_ = tones;
131 duration_ = duration;
132 inter_tone_gap_ = inter_tone_gap;
133 // Clear the previous queue.
Steve Anton9a4fd9b2018-08-31 15:20:10 -0700134 dtmf_driver_.Clear();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 // Kick off a new DTMF task queue.
Steve Anton9a4fd9b2018-08-31 15:20:10 -0700136 QueueInsertDtmf(RTC_FROM_HERE, 1 /*ms*/);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 return true;
138}
139
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140std::string DtmfSender::tones() const {
141 return tones_;
142}
143
144int DtmfSender::duration() const {
145 return duration_;
146}
147
148int DtmfSender::inter_tone_gap() const {
149 return inter_tone_gap_;
150}
151
Steve Anton9a4fd9b2018-08-31 15:20:10 -0700152void DtmfSender::QueueInsertDtmf(const rtc::Location& posted_from,
153 uint32_t delay_ms) {
154 dtmf_driver_.AsyncInvokeDelayed<void>(posted_from, signaling_thread_,
155 [this] { DoInsertDtmf(); }, delay_ms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156}
157
158void DtmfSender::DoInsertDtmf() {
nisseede5da42017-01-12 05:15:36 -0800159 RTC_DCHECK(signaling_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160
161 // Get the first DTMF tone from the tone buffer. Unrecognized characters will
162 // be ignored and skipped.
163 size_t first_tone_pos = tones_.find_first_of(kDtmfValidTones);
164 int code = 0;
165 if (first_tone_pos == std::string::npos) {
166 tones_.clear();
167 // Fire a “OnToneChange” event with an empty string and stop.
168 if (observer_) {
Harald Alvestrandd7b79af2018-09-06 09:33:56 +0200169 observer_->OnToneChange(std::string(), tones_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 observer_->OnToneChange(std::string());
171 }
172 return;
173 } else {
174 char tone = tones_[first_tone_pos];
175 if (!GetDtmfCode(tone, &code)) {
176 // The find_first_of(kDtmfValidTones) should have guarantee |tone| is
177 // a valid DTMF tone.
nissec80e7412017-01-11 05:56:46 -0800178 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 }
180 }
181
182 int tone_gap = inter_tone_gap_;
183 if (code == kDtmfCodeTwoSecondDelay) {
184 // Special case defined by WebRTC - The character',' indicates a delay of 2
185 // seconds before processing the next character in the tones parameter.
186 tone_gap = kDtmfTwoSecondInMs;
187 } else {
188 if (!provider_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100189 RTC_LOG(LS_ERROR) << "The DtmfProvider has been destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 return;
191 }
192 // The provider starts playout of the given tone on the
193 // associated RTP media stream, using the appropriate codec.
deadbeef20cb0c12017-02-01 20:27:00 -0800194 if (!provider_->InsertDtmf(code, duration_)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100195 RTC_LOG(LS_ERROR) << "The DtmfProvider can no longer send DTMF.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 return;
197 }
198 // Wait for the number of milliseconds specified by |duration_|.
199 tone_gap += duration_;
200 }
201
202 // Fire a “OnToneChange” event with the tone that's just processed.
203 if (observer_) {
Harald Alvestrandd7b79af2018-09-06 09:33:56 +0200204 observer_->OnToneChange(tones_.substr(first_tone_pos, 1),
205 tones_.substr(first_tone_pos + 1));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 observer_->OnToneChange(tones_.substr(first_tone_pos, 1));
207 }
208
209 // Erase the unrecognized characters plus the tone that's just processed.
210 tones_.erase(0, first_tone_pos + 1);
211
212 // Continue with the next tone.
Steve Anton9a4fd9b2018-08-31 15:20:10 -0700213 QueueInsertDtmf(RTC_FROM_HERE, tone_gap);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214}
215
216void DtmfSender::OnProviderDestroyed() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100217 RTC_LOG(LS_INFO) << "The Dtmf provider is deleted. Clear the sending queue.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 StopSending();
Steve Antonb983bae2018-06-20 11:16:53 -0700219 provider_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220}
221
222void DtmfSender::StopSending() {
Steve Anton9a4fd9b2018-08-31 15:20:10 -0700223 dtmf_driver_.Clear();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224}
225
226} // namespace webrtc