You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
6.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2012 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * 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.
  9. */
  10. #include "pc/dtmf_sender.h"
  11. #include <ctype.h>
  12. #include <string.h>
  13. #include <string>
  14. #include "rtc_base/checks.h"
  15. #include "rtc_base/logging.h"
  16. #include "rtc_base/ref_counted_object.h"
  17. #include "rtc_base/thread.h"
  18. namespace webrtc {
  19. // RFC4733
  20. // +-------+--------+------+---------+
  21. // | Event | Code | Type | Volume? |
  22. // +-------+--------+------+---------+
  23. // | 0--9 | 0--9 | tone | yes |
  24. // | * | 10 | tone | yes |
  25. // | # | 11 | tone | yes |
  26. // | A--D | 12--15 | tone | yes |
  27. // +-------+--------+------+---------+
  28. // The "," is a special event defined by the WebRTC spec. It means to delay for
  29. // 2 seconds before processing the next tone. We use -1 as its code.
  30. static const int kDtmfCodeTwoSecondDelay = -1;
  31. static const int kDtmfTwoSecondInMs = 2000;
  32. static const char kDtmfValidTones[] = ",0123456789*#ABCDabcd";
  33. static const char kDtmfTonesTable[] = ",0123456789*#ABCD";
  34. // The duration cannot be more than 6000ms or less than 40ms. The gap between
  35. // tones must be at least 50 ms.
  36. // Source for values: W3C WEBRTC specification.
  37. // https://w3c.github.io/webrtc-pc/#dom-rtcdtmfsender-insertdtmf
  38. static const int kDtmfDefaultDurationMs = 100;
  39. static const int kDtmfMinDurationMs = 40;
  40. static const int kDtmfMaxDurationMs = 6000;
  41. static const int kDtmfDefaultGapMs = 50;
  42. static const int kDtmfMinGapMs = 30;
  43. // Get DTMF code from the DTMF event character.
  44. bool GetDtmfCode(char tone, int* code) {
  45. // Convert a-d to A-D.
  46. char event = toupper(tone);
  47. const char* p = strchr(kDtmfTonesTable, event);
  48. if (!p) {
  49. return false;
  50. }
  51. *code = p - kDtmfTonesTable - 1;
  52. return true;
  53. }
  54. rtc::scoped_refptr<DtmfSender> DtmfSender::Create(
  55. rtc::Thread* signaling_thread,
  56. DtmfProviderInterface* provider) {
  57. if (!signaling_thread) {
  58. return nullptr;
  59. }
  60. rtc::scoped_refptr<DtmfSender> dtmf_sender(
  61. new rtc::RefCountedObject<DtmfSender>(signaling_thread, provider));
  62. return dtmf_sender;
  63. }
  64. DtmfSender::DtmfSender(rtc::Thread* signaling_thread,
  65. DtmfProviderInterface* provider)
  66. : observer_(nullptr),
  67. signaling_thread_(signaling_thread),
  68. provider_(provider),
  69. duration_(kDtmfDefaultDurationMs),
  70. inter_tone_gap_(kDtmfDefaultGapMs) {
  71. RTC_DCHECK(signaling_thread_);
  72. if (provider_) {
  73. RTC_DCHECK(provider_->GetOnDestroyedSignal());
  74. provider_->GetOnDestroyedSignal()->connect(
  75. this, &DtmfSender::OnProviderDestroyed);
  76. }
  77. }
  78. DtmfSender::~DtmfSender() {
  79. StopSending();
  80. }
  81. void DtmfSender::RegisterObserver(DtmfSenderObserverInterface* observer) {
  82. observer_ = observer;
  83. }
  84. void DtmfSender::UnregisterObserver() {
  85. observer_ = nullptr;
  86. }
  87. bool DtmfSender::CanInsertDtmf() {
  88. RTC_DCHECK(signaling_thread_->IsCurrent());
  89. if (!provider_) {
  90. return false;
  91. }
  92. return provider_->CanInsertDtmf();
  93. }
  94. bool DtmfSender::InsertDtmf(const std::string& tones,
  95. int duration,
  96. int inter_tone_gap) {
  97. RTC_DCHECK(signaling_thread_->IsCurrent());
  98. if (duration > kDtmfMaxDurationMs || duration < kDtmfMinDurationMs ||
  99. inter_tone_gap < kDtmfMinGapMs) {
  100. RTC_LOG(LS_ERROR)
  101. << "InsertDtmf is called with invalid duration or tones gap. "
  102. "The duration cannot be more than "
  103. << kDtmfMaxDurationMs << "ms or less than " << kDtmfMinDurationMs
  104. << "ms. The gap between tones must be at least " << kDtmfMinGapMs
  105. << "ms.";
  106. return false;
  107. }
  108. if (!CanInsertDtmf()) {
  109. RTC_LOG(LS_ERROR)
  110. << "InsertDtmf is called on DtmfSender that can't send DTMF.";
  111. return false;
  112. }
  113. tones_ = tones;
  114. duration_ = duration;
  115. inter_tone_gap_ = inter_tone_gap;
  116. // Clear the previous queue.
  117. dtmf_driver_.Clear();
  118. // Kick off a new DTMF task queue.
  119. QueueInsertDtmf(RTC_FROM_HERE, 1 /*ms*/);
  120. return true;
  121. }
  122. std::string DtmfSender::tones() const {
  123. return tones_;
  124. }
  125. int DtmfSender::duration() const {
  126. return duration_;
  127. }
  128. int DtmfSender::inter_tone_gap() const {
  129. return inter_tone_gap_;
  130. }
  131. void DtmfSender::QueueInsertDtmf(const rtc::Location& posted_from,
  132. uint32_t delay_ms) {
  133. dtmf_driver_.AsyncInvokeDelayed<void>(
  134. posted_from, signaling_thread_, [this] { DoInsertDtmf(); }, delay_ms);
  135. }
  136. void DtmfSender::DoInsertDtmf() {
  137. RTC_DCHECK(signaling_thread_->IsCurrent());
  138. // Get the first DTMF tone from the tone buffer. Unrecognized characters will
  139. // be ignored and skipped.
  140. size_t first_tone_pos = tones_.find_first_of(kDtmfValidTones);
  141. int code = 0;
  142. if (first_tone_pos == std::string::npos) {
  143. tones_.clear();
  144. // Fire a “OnToneChange” event with an empty string and stop.
  145. if (observer_) {
  146. observer_->OnToneChange(std::string(), tones_);
  147. observer_->OnToneChange(std::string());
  148. }
  149. return;
  150. } else {
  151. char tone = tones_[first_tone_pos];
  152. if (!GetDtmfCode(tone, &code)) {
  153. // The find_first_of(kDtmfValidTones) should have guarantee |tone| is
  154. // a valid DTMF tone.
  155. RTC_NOTREACHED();
  156. }
  157. }
  158. int tone_gap = inter_tone_gap_;
  159. if (code == kDtmfCodeTwoSecondDelay) {
  160. // Special case defined by WebRTC - The character',' indicates a delay of 2
  161. // seconds before processing the next character in the tones parameter.
  162. tone_gap = kDtmfTwoSecondInMs;
  163. } else {
  164. if (!provider_) {
  165. RTC_LOG(LS_ERROR) << "The DtmfProvider has been destroyed.";
  166. return;
  167. }
  168. // The provider starts playout of the given tone on the
  169. // associated RTP media stream, using the appropriate codec.
  170. if (!provider_->InsertDtmf(code, duration_)) {
  171. RTC_LOG(LS_ERROR) << "The DtmfProvider can no longer send DTMF.";
  172. return;
  173. }
  174. // Wait for the number of milliseconds specified by |duration_|.
  175. tone_gap += duration_;
  176. }
  177. // Fire a “OnToneChange” event with the tone that's just processed.
  178. if (observer_) {
  179. observer_->OnToneChange(tones_.substr(first_tone_pos, 1),
  180. tones_.substr(first_tone_pos + 1));
  181. observer_->OnToneChange(tones_.substr(first_tone_pos, 1));
  182. }
  183. // Erase the unrecognized characters plus the tone that's just processed.
  184. tones_.erase(0, first_tone_pos + 1);
  185. // Continue with the next tone.
  186. QueueInsertDtmf(RTC_FROM_HERE, tone_gap);
  187. }
  188. void DtmfSender::OnProviderDestroyed() {
  189. RTC_LOG(LS_INFO) << "The Dtmf provider is deleted. Clear the sending queue.";
  190. StopSending();
  191. provider_ = nullptr;
  192. }
  193. void DtmfSender::StopSending() {
  194. dtmf_driver_.Clear();
  195. }
  196. } // namespace webrtc