blob: 7c1d3c156a90eb227dcce44086e16c2fa9ec2f55 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/dtmf_sender.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
13#include <ctype.h>
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <string.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015#include <string>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
18#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/ref_counted_object.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22namespace webrtc {
23
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024// RFC4733
25// +-------+--------+------+---------+
26// | Event | Code | Type | Volume? |
27// +-------+--------+------+---------+
28// | 0--9 | 0--9 | tone | yes |
29// | * | 10 | tone | yes |
30// | # | 11 | tone | yes |
31// | A--D | 12--15 | tone | yes |
32// +-------+--------+------+---------+
33// The "," is a special event defined by the WebRTC spec. It means to delay for
34// 2 seconds before processing the next tone. We use -1 as its code.
35static const int kDtmfCodeTwoSecondDelay = -1;
36static const int kDtmfTwoSecondInMs = 2000;
37static const char kDtmfValidTones[] = ",0123456789*#ABCDabcd";
38static const char kDtmfTonesTable[] = ",0123456789*#ABCD";
dminor588101c2017-03-28 11:18:32 -070039// The duration cannot be more than 6000ms or less than 40ms. The gap between
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040// tones must be at least 50 ms.
Harald Alvestrand52e58522018-02-20 08:15:36 +010041// Source for values: W3C WEBRTC specification.
42// https://w3c.github.io/webrtc-pc/#dom-rtcdtmfsender-insertdtmf
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043static const int kDtmfDefaultDurationMs = 100;
dminor588101c2017-03-28 11:18:32 -070044static const int kDtmfMinDurationMs = 40;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045static const int kDtmfMaxDurationMs = 6000;
46static const int kDtmfDefaultGapMs = 50;
Harald Alvestrand52e58522018-02-20 08:15:36 +010047static const int kDtmfMinGapMs = 30;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
49// Get DTMF code from the DTMF event character.
50bool GetDtmfCode(char tone, int* code) {
51 // Convert a-d to A-D.
52 char event = toupper(tone);
53 const char* p = strchr(kDtmfTonesTable, event);
54 if (!p) {
55 return false;
56 }
57 *code = p - kDtmfTonesTable - 1;
58 return true;
59}
60
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000061rtc::scoped_refptr<DtmfSender> DtmfSender::Create(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000062 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 DtmfProviderInterface* provider) {
deadbeef20cb0c12017-02-01 20:27:00 -080064 if (!signaling_thread) {
65 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000067 rtc::scoped_refptr<DtmfSender> dtmf_sender(
Steve Antonb983bae2018-06-20 11:16:53 -070068 new rtc::RefCountedObject<DtmfSender>(signaling_thread, provider));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 return dtmf_sender;
70}
71
Steve Antonb983bae2018-06-20 11:16:53 -070072DtmfSender::DtmfSender(rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 DtmfProviderInterface* provider)
Steve Antonb983bae2018-06-20 11:16:53 -070074 : observer_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 signaling_thread_(signaling_thread),
76 provider_(provider),
77 duration_(kDtmfDefaultDurationMs),
78 inter_tone_gap_(kDtmfDefaultGapMs) {
Steve Antonb983bae2018-06-20 11:16:53 -070079 RTC_DCHECK(signaling_thread_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 if (provider_) {
Steve Antonb983bae2018-06-20 11:16:53 -070081 RTC_DCHECK(provider_->GetOnDestroyedSignal());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 provider_->GetOnDestroyedSignal()->connect(
83 this, &DtmfSender::OnProviderDestroyed);
84 }
85}
86
87DtmfSender::~DtmfSender() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 StopSending();
89}
90
91void DtmfSender::RegisterObserver(DtmfSenderObserverInterface* observer) {
92 observer_ = observer;
93}
94
95void DtmfSender::UnregisterObserver() {
Steve Antonb983bae2018-06-20 11:16:53 -070096 observer_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097}
98
99bool DtmfSender::CanInsertDtmf() {
nisseede5da42017-01-12 05:15:36 -0800100 RTC_DCHECK(signaling_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 if (!provider_) {
102 return false;
103 }
deadbeef20cb0c12017-02-01 20:27:00 -0800104 return provider_->CanInsertDtmf();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105}
106
Yves Gerey665174f2018-06-19 15:03:05 +0200107bool DtmfSender::InsertDtmf(const std::string& tones,
108 int duration,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 int inter_tone_gap) {
nisseede5da42017-01-12 05:15:36 -0800110 RTC_DCHECK(signaling_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111
Yves Gerey665174f2018-06-19 15:03:05 +0200112 if (duration > kDtmfMaxDurationMs || duration < kDtmfMinDurationMs ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 inter_tone_gap < kDtmfMinGapMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100114 RTC_LOG(LS_ERROR)
115 << "InsertDtmf is called with invalid duration or tones gap. "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100116 "The duration cannot be more than "
117 << kDtmfMaxDurationMs << "ms or less than " << kDtmfMinDurationMs
Yves Gerey665174f2018-06-19 15:03:05 +0200118 << "ms. The gap between tones must be at least " << kDtmfMinGapMs
119 << "ms.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 return false;
121 }
122
123 if (!CanInsertDtmf()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100124 RTC_LOG(LS_ERROR)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 << "InsertDtmf is called on DtmfSender that can't send DTMF.";
126 return false;
127 }
128
129 tones_ = tones;
130 duration_ = duration;
131 inter_tone_gap_ = inter_tone_gap;
132 // Clear the previous queue.
Steve Anton9a4fd9b2018-08-31 15:20:10 -0700133 dtmf_driver_.Clear();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 // Kick off a new DTMF task queue.
Steve Anton9a4fd9b2018-08-31 15:20:10 -0700135 QueueInsertDtmf(RTC_FROM_HERE, 1 /*ms*/);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 return true;
137}
138
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139std::string DtmfSender::tones() const {
140 return tones_;
141}
142
143int DtmfSender::duration() const {
144 return duration_;
145}
146
147int DtmfSender::inter_tone_gap() const {
148 return inter_tone_gap_;
149}
150
Steve Anton9a4fd9b2018-08-31 15:20:10 -0700151void DtmfSender::QueueInsertDtmf(const rtc::Location& posted_from,
152 uint32_t delay_ms) {
153 dtmf_driver_.AsyncInvokeDelayed<void>(posted_from, signaling_thread_,
154 [this] { DoInsertDtmf(); }, delay_ms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155}
156
157void DtmfSender::DoInsertDtmf() {
nisseede5da42017-01-12 05:15:36 -0800158 RTC_DCHECK(signaling_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159
160 // Get the first DTMF tone from the tone buffer. Unrecognized characters will
161 // be ignored and skipped.
162 size_t first_tone_pos = tones_.find_first_of(kDtmfValidTones);
163 int code = 0;
164 if (first_tone_pos == std::string::npos) {
165 tones_.clear();
166 // Fire a “OnToneChange” event with an empty string and stop.
167 if (observer_) {
Harald Alvestrandd7b79af2018-09-06 09:33:56 +0200168 observer_->OnToneChange(std::string(), tones_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 observer_->OnToneChange(std::string());
170 }
171 return;
172 } else {
173 char tone = tones_[first_tone_pos];
174 if (!GetDtmfCode(tone, &code)) {
175 // The find_first_of(kDtmfValidTones) should have guarantee |tone| is
176 // a valid DTMF tone.
nissec80e7412017-01-11 05:56:46 -0800177 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 }
179 }
180
181 int tone_gap = inter_tone_gap_;
182 if (code == kDtmfCodeTwoSecondDelay) {
183 // Special case defined by WebRTC - The character',' indicates a delay of 2
184 // seconds before processing the next character in the tones parameter.
185 tone_gap = kDtmfTwoSecondInMs;
186 } else {
187 if (!provider_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100188 RTC_LOG(LS_ERROR) << "The DtmfProvider has been destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 return;
190 }
191 // The provider starts playout of the given tone on the
192 // associated RTP media stream, using the appropriate codec.
deadbeef20cb0c12017-02-01 20:27:00 -0800193 if (!provider_->InsertDtmf(code, duration_)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100194 RTC_LOG(LS_ERROR) << "The DtmfProvider can no longer send DTMF.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 return;
196 }
197 // Wait for the number of milliseconds specified by |duration_|.
198 tone_gap += duration_;
199 }
200
201 // Fire a “OnToneChange” event with the tone that's just processed.
202 if (observer_) {
Harald Alvestrandd7b79af2018-09-06 09:33:56 +0200203 observer_->OnToneChange(tones_.substr(first_tone_pos, 1),
204 tones_.substr(first_tone_pos + 1));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 observer_->OnToneChange(tones_.substr(first_tone_pos, 1));
206 }
207
208 // Erase the unrecognized characters plus the tone that's just processed.
209 tones_.erase(0, first_tone_pos + 1);
210
211 // Continue with the next tone.
Steve Anton9a4fd9b2018-08-31 15:20:10 -0700212 QueueInsertDtmf(RTC_FROM_HERE, tone_gap);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213}
214
215void DtmfSender::OnProviderDestroyed() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100216 RTC_LOG(LS_INFO) << "The Dtmf provider is deleted. Clear the sending queue.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 StopSending();
Steve Antonb983bae2018-06-20 11:16:53 -0700218 provider_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219}
220
221void DtmfSender::StopSending() {
Steve Anton9a4fd9b2018-08-31 15:20:10 -0700222 dtmf_driver_.Clear();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223}
224
225} // namespace webrtc