Home | History | Annotate | Download | only in webrtc
      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 "talk/base/common.h"
     37 #include "talk/base/messagehandler.h"
     38 #include "talk/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 talk_base {
     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:
     67   virtual ~DtmfProviderInterface() {}
     68 };
     69 
     70 class DtmfSender
     71     : public DtmfSenderInterface,
     72       public sigslot::has_slots<>,
     73       public talk_base::MessageHandler {
     74  public:
     75   static talk_base::scoped_refptr<DtmfSender> Create(
     76       AudioTrackInterface* track,
     77       talk_base::Thread* signaling_thread,
     78       DtmfProviderInterface* provider);
     79 
     80   // Implements DtmfSenderInterface.
     81   virtual void RegisterObserver(DtmfSenderObserverInterface* observer) OVERRIDE;
     82   virtual void UnregisterObserver() OVERRIDE;
     83   virtual bool CanInsertDtmf() OVERRIDE;
     84   virtual bool InsertDtmf(const std::string& tones, int duration,
     85                           int inter_tone_gap) OVERRIDE;
     86   virtual const AudioTrackInterface* track() const OVERRIDE;
     87   virtual std::string tones() const OVERRIDE;
     88   virtual int duration() const OVERRIDE;
     89   virtual int inter_tone_gap() const OVERRIDE;
     90 
     91  protected:
     92   DtmfSender(AudioTrackInterface* track,
     93              talk_base::Thread* signaling_thread,
     94              DtmfProviderInterface* provider);
     95   virtual ~DtmfSender();
     96 
     97  private:
     98   DtmfSender();
     99 
    100   // Implements MessageHandler.
    101   virtual void OnMessage(talk_base::Message* msg);
    102 
    103   // The DTMF sending task.
    104   void DoInsertDtmf();
    105 
    106   void OnProviderDestroyed();
    107 
    108   void StopSending();
    109 
    110   talk_base::scoped_refptr<AudioTrackInterface> track_;
    111   DtmfSenderObserverInterface* observer_;
    112   talk_base::Thread* signaling_thread_;
    113   DtmfProviderInterface* provider_;
    114   std::string tones_;
    115   int duration_;
    116   int inter_tone_gap_;
    117 
    118   DISALLOW_COPY_AND_ASSIGN(DtmfSender);
    119 };
    120 
    121 // Define proxy for DtmfSenderInterface.
    122 BEGIN_PROXY_MAP(DtmfSender)
    123   PROXY_METHOD1(void, RegisterObserver, DtmfSenderObserverInterface*)
    124   PROXY_METHOD0(void, UnregisterObserver)
    125   PROXY_METHOD0(bool, CanInsertDtmf)
    126   PROXY_METHOD3(bool, InsertDtmf, const std::string&, int, int)
    127   PROXY_CONSTMETHOD0(const AudioTrackInterface*, track)
    128   PROXY_CONSTMETHOD0(std::string, tones)
    129   PROXY_CONSTMETHOD0(int, duration)
    130   PROXY_CONSTMETHOD0(int, inter_tone_gap)
    131 END_PROXY()
    132 
    133 // Get DTMF code from the DTMF event character.
    134 bool GetDtmfCode(char tone, int* code);
    135 
    136 }  // namespace webrtc
    137 
    138 #endif  // TALK_APP_WEBRTC_DTMFSENDER_H_
    139