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 11 #ifndef PC_DTMF_SENDER_H_ 12 #define PC_DTMF_SENDER_H_ 13 14 #include <stdint.h> 15 16 #include <string> 17 18 #include "api/dtmf_sender_interface.h" 19 #include "api/scoped_refptr.h" 20 #include "api/sequence_checker.h" 21 #include "api/task_queue/pending_task_safety_flag.h" 22 #include "api/task_queue/task_queue_base.h" 23 #include "pc/proxy.h" 24 #include "rtc_base/ref_count.h" 25 #include "rtc_base/thread_annotations.h" 26 27 // DtmfSender is the native implementation of the RTCDTMFSender defined by 28 // the WebRTC W3C Editor's Draft. 29 // https://w3c.github.io/webrtc-pc/#rtcdtmfsender 30 31 namespace webrtc { 32 33 // This interface is called by DtmfSender to talk to the actual audio channel 34 // to send DTMF. 35 class DtmfProviderInterface { 36 public: 37 // Returns true if the audio sender is capable of sending DTMF. Otherwise 38 // returns false. 39 virtual bool CanInsertDtmf() = 0; 40 // Sends DTMF `code`. 41 // The `duration` indicates the length of the DTMF tone in ms. 42 // Returns true on success and false on failure. 43 virtual bool InsertDtmf(int code, int duration) = 0; 44 45 protected: ~DtmfProviderInterface()46 virtual ~DtmfProviderInterface() {} 47 }; 48 49 class DtmfSender : public DtmfSenderInterface { 50 public: 51 static rtc::scoped_refptr<DtmfSender> Create(TaskQueueBase* signaling_thread, 52 DtmfProviderInterface* provider); 53 54 void OnDtmfProviderDestroyed(); 55 56 // Implements DtmfSenderInterface. 57 void RegisterObserver(DtmfSenderObserverInterface* observer) override; 58 void UnregisterObserver() override; 59 bool CanInsertDtmf() override; 60 bool InsertDtmf(const std::string& tones, 61 int duration, 62 int inter_tone_gap, 63 int comma_delay = kDtmfDefaultCommaDelayMs) override; 64 std::string tones() const override; 65 int duration() const override; 66 int inter_tone_gap() const override; 67 int comma_delay() const override; 68 69 protected: 70 DtmfSender(TaskQueueBase* signaling_thread, DtmfProviderInterface* provider); 71 virtual ~DtmfSender(); 72 73 DtmfSender(const DtmfSender&) = delete; 74 DtmfSender& operator=(const DtmfSender&) = delete; 75 76 private: 77 DtmfSender(); 78 79 void QueueInsertDtmf(uint32_t delay_ms) RTC_RUN_ON(signaling_thread_); 80 81 // The DTMF sending task. 82 void DoInsertDtmf() RTC_RUN_ON(signaling_thread_); 83 84 void StopSending() RTC_RUN_ON(signaling_thread_); 85 86 DtmfSenderObserverInterface* observer_ RTC_GUARDED_BY(signaling_thread_); 87 TaskQueueBase* const signaling_thread_; 88 DtmfProviderInterface* provider_ RTC_GUARDED_BY(signaling_thread_); 89 std::string tones_ RTC_GUARDED_BY(signaling_thread_); 90 int duration_ RTC_GUARDED_BY(signaling_thread_); 91 int inter_tone_gap_ RTC_GUARDED_BY(signaling_thread_); 92 int comma_delay_ RTC_GUARDED_BY(signaling_thread_); 93 94 // For cancelling the tasks which feed the DTMF provider one tone at a time. 95 rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag_ RTC_GUARDED_BY( 96 signaling_thread_) RTC_PT_GUARDED_BY(signaling_thread_) = nullptr; 97 }; 98 99 // Define proxy for DtmfSenderInterface. 100 BEGIN_PRIMARY_PROXY_MAP(DtmfSender) 101 102 PROXY_PRIMARY_THREAD_DESTRUCTOR() 103 PROXY_METHOD1(void, RegisterObserver, DtmfSenderObserverInterface*) 104 PROXY_METHOD0(void, UnregisterObserver) 105 PROXY_METHOD0(bool, CanInsertDtmf) 106 PROXY_METHOD4(bool, InsertDtmf, const std::string&, int, int, int) 107 PROXY_CONSTMETHOD0(std::string, tones) 108 PROXY_CONSTMETHOD0(int, duration) 109 PROXY_CONSTMETHOD0(int, inter_tone_gap) 110 PROXY_CONSTMETHOD0(int, comma_delay) 111 END_PROXY_MAP(DtmfSender) 112 113 // Get DTMF code from the DTMF event character. 114 bool GetDtmfCode(char tone, int* code); 115 116 } // namespace webrtc 117 118 #endif // PC_DTMF_SENDER_H_ 119