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