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