1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
/*
* Copyright 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "pc/dtmf_sender.h"
#include <ctype.h>
#include <string.h>
#include <cstdint>
#include <string>
#include "api/dtmf_sender_interface.h"
#include "api/make_ref_counted.h"
#include "api/scoped_refptr.h"
#include "api/sequence_checker.h"
#include "api/task_queue/pending_task_safety_flag.h"
#include "api/task_queue/task_queue_base.h"
#include "api/units/time_delta.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
namespace webrtc {
// RFC4733
// +-------+--------+------+---------+
// | Event | Code | Type | Volume? |
// +-------+--------+------+---------+
// | 0--9 | 0--9 | tone | yes |
// | * | 10 | tone | yes |
// | # | 11 | tone | yes |
// | A--D | 12--15 | tone | yes |
// +-------+--------+------+---------+
// The "," is a special event defined by the WebRTC spec. It means to delay for
// 2 seconds before processing the next tone. We use -1 as its code.
static const int kDtmfCommaDelay = -1;
static const char kDtmfValidTones[] = ",0123456789*#ABCDabcd";
static const char kDtmfTonesTable[] = ",0123456789*#ABCD";
// The duration cannot be more than 6000ms or less than 40ms. The gap between
// tones must be at least 50 ms.
// Source for values: W3C WEBRTC specification.
// https://w3c.github.io/webrtc-pc/#dom-rtcdtmfsender-insertdtmf
static const int kDtmfDefaultDurationMs = 100;
static const int kDtmfMinDurationMs = 40;
static const int kDtmfMaxDurationMs = 6000;
static const int kDtmfDefaultGapMs = 50;
static const int kDtmfMinGapMs = 30;
// Get DTMF code from the DTMF event character.
bool GetDtmfCode(char tone, int* code) {
// Convert a-d to A-D.
char event = toupper(tone);
const char* p = strchr(kDtmfTonesTable, event);
if (!p) {
return false;
}
*code = p - kDtmfTonesTable - 1;
return true;
}
scoped_refptr<DtmfSender> DtmfSender::Create(TaskQueueBase* signaling_thread,
DtmfProviderInterface* provider) {
if (!signaling_thread) {
return nullptr;
}
return make_ref_counted<DtmfSender>(signaling_thread, provider);
}
DtmfSender::DtmfSender(TaskQueueBase* signaling_thread,
DtmfProviderInterface* provider)
: observer_(nullptr),
signaling_thread_(signaling_thread),
provider_(provider),
duration_(kDtmfDefaultDurationMs),
inter_tone_gap_(kDtmfDefaultGapMs),
comma_delay_(kDtmfDefaultCommaDelayMs) {
RTC_DCHECK(signaling_thread_);
RTC_DCHECK(provider_);
}
void DtmfSender::OnDtmfProviderDestroyed() {
RTC_DCHECK_RUN_ON(signaling_thread_);
RTC_DLOG(LS_INFO) << "The Dtmf provider is deleted. Clear the sending queue.";
StopSending();
provider_ = nullptr;
}
DtmfSender::~DtmfSender() {
RTC_DCHECK_RUN_ON(signaling_thread_);
StopSending();
}
void DtmfSender::RegisterObserver(DtmfSenderObserverInterface* observer) {
RTC_DCHECK_RUN_ON(signaling_thread_);
observer_ = observer;
}
void DtmfSender::UnregisterObserver() {
RTC_DCHECK_RUN_ON(signaling_thread_);
observer_ = nullptr;
}
bool DtmfSender::CanInsertDtmf() {
RTC_DCHECK_RUN_ON(signaling_thread_);
if (!provider_) {
return false;
}
return provider_->CanInsertDtmf();
}
bool DtmfSender::InsertDtmf(const std::string& tones,
int duration,
int inter_tone_gap,
int comma_delay) {
RTC_DCHECK_RUN_ON(signaling_thread_);
if (duration > kDtmfMaxDurationMs || duration < kDtmfMinDurationMs ||
inter_tone_gap < kDtmfMinGapMs || comma_delay < kDtmfMinGapMs) {
RTC_LOG(LS_ERROR)
<< "InsertDtmf is called with invalid duration or tones gap. "
"The duration cannot be more than "
<< kDtmfMaxDurationMs << "ms or less than " << kDtmfMinDurationMs
<< "ms. The gap between tones must be at least " << kDtmfMinGapMs
<< "ms.";
return false;
}
if (!CanInsertDtmf()) {
RTC_LOG(LS_ERROR)
<< "InsertDtmf is called on DtmfSender that can't send DTMF.";
return false;
}
tones_ = tones;
duration_ = duration;
inter_tone_gap_ = inter_tone_gap;
comma_delay_ = comma_delay;
// Cancel any remaining tasks for previous tones.
if (safety_flag_) {
safety_flag_->SetNotAlive();
}
safety_flag_ = PendingTaskSafetyFlag::Create();
// Kick off a new DTMF task.
QueueInsertDtmf(1 /*ms*/);
return true;
}
std::string DtmfSender::tones() const {
RTC_DCHECK_RUN_ON(signaling_thread_);
return tones_;
}
int DtmfSender::duration() const {
RTC_DCHECK_RUN_ON(signaling_thread_);
return duration_;
}
int DtmfSender::inter_tone_gap() const {
RTC_DCHECK_RUN_ON(signaling_thread_);
return inter_tone_gap_;
}
int DtmfSender::comma_delay() const {
RTC_DCHECK_RUN_ON(signaling_thread_);
return comma_delay_;
}
void DtmfSender::QueueInsertDtmf(uint32_t delay_ms) {
signaling_thread_->PostDelayedHighPrecisionTask(
SafeTask(safety_flag_,
[this] {
RTC_DCHECK_RUN_ON(signaling_thread_);
DoInsertDtmf();
}),
TimeDelta::Millis(delay_ms));
}
void DtmfSender::DoInsertDtmf() {
// Get the first DTMF tone from the tone buffer. Unrecognized characters will
// be ignored and skipped.
size_t first_tone_pos = tones_.find_first_of(kDtmfValidTones);
int code = 0;
if (first_tone_pos == std::string::npos) {
tones_.clear();
// Fire a “OnToneChange” event with an empty string and stop.
if (observer_) {
observer_->OnToneChange(std::string(), tones_);
observer_->OnToneChange(std::string());
}
return;
} else {
char tone = tones_[first_tone_pos];
if (!GetDtmfCode(tone, &code)) {
// The find_first_of(kDtmfValidTones) should have guarantee `tone` is
// a valid DTMF tone.
RTC_DCHECK_NOTREACHED();
}
}
int tone_gap = inter_tone_gap_;
if (code == kDtmfCommaDelay) {
// Special case defined by WebRTC - By default, the character ',' indicates
// a delay of 2 seconds before processing the next character in the tones
// parameter. The comma delay can be set to a non default value via
// InsertDtmf to comply with legacy WebRTC clients.
tone_gap = comma_delay_;
} else {
if (!provider_) {
RTC_LOG(LS_ERROR) << "The DtmfProvider has been destroyed.";
return;
}
// The provider starts playout of the given tone on the
// associated RTP media stream, using the appropriate codec.
if (!provider_->InsertDtmf(code, duration_)) {
RTC_LOG(LS_ERROR) << "The DtmfProvider can no longer send DTMF.";
return;
}
// Wait for the number of milliseconds specified by `duration_`.
tone_gap += duration_;
}
// Fire a “OnToneChange” event with the tone that's just processed.
if (observer_) {
observer_->OnToneChange(tones_.substr(first_tone_pos, 1),
tones_.substr(first_tone_pos + 1));
observer_->OnToneChange(tones_.substr(first_tone_pos, 1));
}
// Erase the unrecognized characters plus the tone that's just processed.
tones_.erase(0, first_tone_pos + 1);
// Continue with the next tone.
QueueInsertDtmf(tone_gap);
}
void DtmfSender::StopSending() {
if (safety_flag_) {
safety_flag_->SetNotAlive();
}
}
} // namespace webrtc
|