blob: 10378028c8fea76aff6357282f1b7a8599531819 [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016#include <string>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/ref_counted_object.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
23namespace webrtc {
24
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025// RFC4733
26// +-------+--------+------+---------+
27// | Event | Code | Type | Volume? |
28// +-------+--------+------+---------+
29// | 0--9 | 0--9 | tone | yes |
30// | * | 10 | tone | yes |
31// | # | 11 | tone | yes |
32// | A--D | 12--15 | tone | yes |
33// +-------+--------+------+---------+
34// The "," is a special event defined by the WebRTC spec. It means to delay for
35// 2 seconds before processing the next tone. We use -1 as its code.
Aaron Alaniz529d8862020-01-21 03:09:47 +000036static const int kDtmfCommaDelay = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037static 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),
Aaron Alaniz529d8862020-01-21 03:09:47 +000078 inter_tone_gap_(kDtmfDefaultGapMs),
79 comma_delay_(kDtmfDefaultCommaDelayMs) {
Steve Antonb983bae2018-06-20 11:16:53 -070080 RTC_DCHECK(signaling_thread_);
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,
Aaron Alaniz529d8862020-01-21 03:09:47 +0000110 int inter_tone_gap,
111 int comma_delay) {
nisseede5da42017-01-12 05:15:36 -0800112 RTC_DCHECK(signaling_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113
Yves Gerey665174f2018-06-19 15:03:05 +0200114 if (duration > kDtmfMaxDurationMs || duration < kDtmfMinDurationMs ||
Aaron Alaniz529d8862020-01-21 03:09:47 +0000115 inter_tone_gap < kDtmfMinGapMs || comma_delay < kDtmfMinGapMs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100116 RTC_LOG(LS_ERROR)
117 << "InsertDtmf is called with invalid duration or tones gap. "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100118 "The duration cannot be more than "
119 << kDtmfMaxDurationMs << "ms or less than " << kDtmfMinDurationMs
Yves Gerey665174f2018-06-19 15:03:05 +0200120 << "ms. The gap between tones must be at least " << kDtmfMinGapMs
121 << "ms.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 return false;
123 }
124
125 if (!CanInsertDtmf()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100126 RTC_LOG(LS_ERROR)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 << "InsertDtmf is called on DtmfSender that can't send DTMF.";
128 return false;
129 }
130
131 tones_ = tones;
132 duration_ = duration;
133 inter_tone_gap_ = inter_tone_gap;
Aaron Alaniz529d8862020-01-21 03:09:47 +0000134 comma_delay_ = comma_delay;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 // Clear the previous queue.
Steve Anton9a4fd9b2018-08-31 15:20:10 -0700136 dtmf_driver_.Clear();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 // Kick off a new DTMF task queue.
Steve Anton9a4fd9b2018-08-31 15:20:10 -0700138 QueueInsertDtmf(RTC_FROM_HERE, 1 /*ms*/);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 return true;
140}
141
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142std::string DtmfSender::tones() const {
143 return tones_;
144}
145
146int DtmfSender::duration() const {
147 return duration_;
148}
149
150int DtmfSender::inter_tone_gap() const {
151 return inter_tone_gap_;
152}
153
Aaron Alaniz529d8862020-01-21 03:09:47 +0000154int DtmfSender::comma_delay() const {
155 return comma_delay_;
156}
157
Steve Anton9a4fd9b2018-08-31 15:20:10 -0700158void DtmfSender::QueueInsertDtmf(const rtc::Location& posted_from,
159 uint32_t delay_ms) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200160 dtmf_driver_.AsyncInvokeDelayed<void>(
161 posted_from, signaling_thread_, [this] { DoInsertDtmf(); }, delay_ms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162}
163
164void DtmfSender::DoInsertDtmf() {
nisseede5da42017-01-12 05:15:36 -0800165 RTC_DCHECK(signaling_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166
167 // Get the first DTMF tone from the tone buffer. Unrecognized characters will
168 // be ignored and skipped.
169 size_t first_tone_pos = tones_.find_first_of(kDtmfValidTones);
170 int code = 0;
171 if (first_tone_pos == std::string::npos) {
172 tones_.clear();
173 // Fire a “OnToneChange” event with an empty string and stop.
174 if (observer_) {
Harald Alvestrandd7b79af2018-09-06 09:33:56 +0200175 observer_->OnToneChange(std::string(), tones_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 observer_->OnToneChange(std::string());
177 }
178 return;
179 } else {
180 char tone = tones_[first_tone_pos];
181 if (!GetDtmfCode(tone, &code)) {
182 // The find_first_of(kDtmfValidTones) should have guarantee |tone| is
183 // a valid DTMF tone.
nissec80e7412017-01-11 05:56:46 -0800184 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 }
186 }
187
188 int tone_gap = inter_tone_gap_;
Aaron Alaniz529d8862020-01-21 03:09:47 +0000189 if (code == kDtmfCommaDelay) {
190 // Special case defined by WebRTC - By default, the character ',' indicates
191 // a delay of 2 seconds before processing the next character in the tones
192 // parameter. The comma delay can be set to a non default value via
193 // InsertDtmf to comply with legacy WebRTC clients.
194 tone_gap = comma_delay_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 } else {
196 if (!provider_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100197 RTC_LOG(LS_ERROR) << "The DtmfProvider has been destroyed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 return;
199 }
200 // The provider starts playout of the given tone on the
201 // associated RTP media stream, using the appropriate codec.
deadbeef20cb0c12017-02-01 20:27:00 -0800202 if (!provider_->InsertDtmf(code, duration_)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100203 RTC_LOG(LS_ERROR) << "The DtmfProvider can no longer send DTMF.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 return;
205 }
206 // Wait for the number of milliseconds specified by |duration_|.
207 tone_gap += duration_;
208 }
209
210 // Fire a “OnToneChange” event with the tone that's just processed.
211 if (observer_) {
Harald Alvestrandd7b79af2018-09-06 09:33:56 +0200212 observer_->OnToneChange(tones_.substr(first_tone_pos, 1),
213 tones_.substr(first_tone_pos + 1));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 observer_->OnToneChange(tones_.substr(first_tone_pos, 1));
215 }
216
217 // Erase the unrecognized characters plus the tone that's just processed.
218 tones_.erase(0, first_tone_pos + 1);
219
220 // Continue with the next tone.
Steve Anton9a4fd9b2018-08-31 15:20:10 -0700221 QueueInsertDtmf(RTC_FROM_HERE, tone_gap);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222}
223
224void DtmfSender::OnProviderDestroyed() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100225 RTC_LOG(LS_INFO) << "The Dtmf provider is deleted. Clear the sending queue.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 StopSending();
Steve Antonb983bae2018-06-20 11:16:53 -0700227 provider_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228}
229
230void DtmfSender::StopSending() {
Steve Anton9a4fd9b2018-08-31 15:20:10 -0700231 dtmf_driver_.Clear();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232}
233
234} // namespace webrtc