1 /*
2  * libjingle
3  * Copyright 2012 Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef TALK_APP_WEBRTC_DTMFSENDER_H_
29 #define TALK_APP_WEBRTC_DTMFSENDER_H_
30 
31 #include <string>
32 
33 #include "talk/app/webrtc/dtmfsenderinterface.h"
34 #include "talk/app/webrtc/mediastreaminterface.h"
35 #include "talk/app/webrtc/proxy.h"
36 #include "webrtc/base/common.h"
37 #include "webrtc/base/messagehandler.h"
38 #include "webrtc/base/refcount.h"
39 
40 // DtmfSender is the native implementation of the RTCDTMFSender defined by
41 // the WebRTC W3C Editor's Draft.
42 // http://dev.w3.org/2011/webrtc/editor/webrtc.html
43 
44 namespace rtc {
45 class Thread;
46 }
47 
48 namespace webrtc {
49 
50 // This interface is called by DtmfSender to talk to the actual audio channel
51 // to send DTMF.
52 class DtmfProviderInterface {
53  public:
54   // Returns true if the audio track with given id (|track_id|) is capable
55   // of sending DTMF. Otherwise returns false.
56   virtual bool CanInsertDtmf(const std::string& track_id) = 0;
57   // Sends DTMF |code| via the audio track with given id (|track_id|).
58   // The |duration| indicates the length of the DTMF tone in ms.
59   // Returns true on success and false on failure.
60   virtual bool InsertDtmf(const std::string& track_id,
61                           int code, int duration) = 0;
62   // Returns a |sigslot::signal0<>| signal. The signal should fire before
63   // the provider is destroyed.
64   virtual sigslot::signal0<>* GetOnDestroyedSignal() = 0;
65 
66  protected:
~DtmfProviderInterface()67   virtual ~DtmfProviderInterface() {}
68 };
69 
70 class DtmfSender
71     : public DtmfSenderInterface,
72       public sigslot::has_slots<>,
73       public rtc::MessageHandler {
74  public:
75   static rtc::scoped_refptr<DtmfSender> Create(
76       AudioTrackInterface* track,
77       rtc::Thread* signaling_thread,
78       DtmfProviderInterface* provider);
79 
80   // Implements DtmfSenderInterface.
81   void RegisterObserver(DtmfSenderObserverInterface* observer) override;
82   void UnregisterObserver() override;
83   bool CanInsertDtmf() override;
84   bool InsertDtmf(const std::string& tones,
85                   int duration,
86                   int inter_tone_gap) override;
87   const AudioTrackInterface* track() const override;
88   std::string tones() const override;
89   int duration() const override;
90   int inter_tone_gap() const override;
91 
92  protected:
93   DtmfSender(AudioTrackInterface* track,
94              rtc::Thread* signaling_thread,
95              DtmfProviderInterface* provider);
96   virtual ~DtmfSender();
97 
98  private:
99   DtmfSender();
100 
101   // Implements MessageHandler.
102   virtual void OnMessage(rtc::Message* msg);
103 
104   // The DTMF sending task.
105   void DoInsertDtmf();
106 
107   void OnProviderDestroyed();
108 
109   void StopSending();
110 
111   rtc::scoped_refptr<AudioTrackInterface> track_;
112   DtmfSenderObserverInterface* observer_;
113   rtc::Thread* signaling_thread_;
114   DtmfProviderInterface* provider_;
115   std::string tones_;
116   int duration_;
117   int inter_tone_gap_;
118 
119   RTC_DISALLOW_COPY_AND_ASSIGN(DtmfSender);
120 };
121 
122 // Define proxy for DtmfSenderInterface.
123 BEGIN_PROXY_MAP(DtmfSender)
124   PROXY_METHOD1(void, RegisterObserver, DtmfSenderObserverInterface*)
125   PROXY_METHOD0(void, UnregisterObserver)
126   PROXY_METHOD0(bool, CanInsertDtmf)
127   PROXY_METHOD3(bool, InsertDtmf, const std::string&, int, int)
128   PROXY_CONSTMETHOD0(const AudioTrackInterface*, track)
129   PROXY_CONSTMETHOD0(std::string, tones)
130   PROXY_CONSTMETHOD0(int, duration)
131   PROXY_CONSTMETHOD0(int, inter_tone_gap)
132 END_PROXY()
133 
134 // Get DTMF code from the DTMF event character.
135 bool GetDtmfCode(char tone, int* code);
136 
137 }  // namespace webrtc
138 
139 #endif  // TALK_APP_WEBRTC_DTMFSENDER_H_
140