Home | History | Annotate | Download | only in webrtc
      1 /*
      2  * libjingle
      3  * Copyright 2004 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 
     29 #ifndef TALK_MEDIA_WEBRTCVOE_H_
     30 #define TALK_MEDIA_WEBRTCVOE_H_
     31 
     32 #include "talk/base/common.h"
     33 #include "talk/media/webrtc/webrtccommon.h"
     34 
     35 #include "webrtc/common_types.h"
     36 #include "webrtc/modules/audio_device/include/audio_device.h"
     37 #include "webrtc/voice_engine/include/voe_audio_processing.h"
     38 #include "webrtc/voice_engine/include/voe_base.h"
     39 #include "webrtc/voice_engine/include/voe_codec.h"
     40 #include "webrtc/voice_engine/include/voe_dtmf.h"
     41 #include "webrtc/voice_engine/include/voe_errors.h"
     42 #include "webrtc/voice_engine/include/voe_external_media.h"
     43 #include "webrtc/voice_engine/include/voe_file.h"
     44 #include "webrtc/voice_engine/include/voe_hardware.h"
     45 #include "webrtc/voice_engine/include/voe_neteq_stats.h"
     46 #include "webrtc/voice_engine/include/voe_network.h"
     47 #include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
     48 #include "webrtc/voice_engine/include/voe_video_sync.h"
     49 #include "webrtc/voice_engine/include/voe_volume_control.h"
     50 
     51 namespace cricket {
     52 // automatically handles lifetime of WebRtc VoiceEngine
     53 class scoped_voe_engine {
     54  public:
     55   explicit scoped_voe_engine(webrtc::VoiceEngine* e) : ptr(e) {}
     56   // VERIFY, to ensure that there are no leaks at shutdown
     57   ~scoped_voe_engine() { if (ptr) VERIFY(webrtc::VoiceEngine::Delete(ptr)); }
     58   // Releases the current pointer.
     59   void reset() {
     60     if (ptr) {
     61       VERIFY(webrtc::VoiceEngine::Delete(ptr));
     62       ptr = NULL;
     63     }
     64   }
     65   webrtc::VoiceEngine* get() const { return ptr; }
     66  private:
     67   webrtc::VoiceEngine* ptr;
     68 };
     69 
     70 // scoped_ptr class to handle obtaining and releasing WebRTC interface pointers
     71 template<class T>
     72 class scoped_voe_ptr {
     73  public:
     74   explicit scoped_voe_ptr(const scoped_voe_engine& e)
     75       : ptr(T::GetInterface(e.get())) {}
     76   explicit scoped_voe_ptr(T* p) : ptr(p) {}
     77   ~scoped_voe_ptr() { if (ptr) ptr->Release(); }
     78   T* operator->() const { return ptr; }
     79   T* get() const { return ptr; }
     80 
     81   // Releases the current pointer.
     82   void reset() {
     83     if (ptr) {
     84       ptr->Release();
     85       ptr = NULL;
     86     }
     87   }
     88 
     89  private:
     90   T* ptr;
     91 };
     92 
     93 // Utility class for aggregating the various WebRTC interface.
     94 // Fake implementations can also be injected for testing.
     95 class VoEWrapper {
     96  public:
     97   VoEWrapper()
     98       : engine_(webrtc::VoiceEngine::Create()), processing_(engine_),
     99         base_(engine_), codec_(engine_), dtmf_(engine_), file_(engine_),
    100         hw_(engine_), media_(engine_), neteq_(engine_), network_(engine_),
    101         rtp_(engine_), sync_(engine_), volume_(engine_) {
    102   }
    103   VoEWrapper(webrtc::VoEAudioProcessing* processing,
    104              webrtc::VoEBase* base,
    105              webrtc::VoECodec* codec,
    106              webrtc::VoEDtmf* dtmf,
    107              webrtc::VoEFile* file,
    108              webrtc::VoEHardware* hw,
    109              webrtc::VoEExternalMedia* media,
    110              webrtc::VoENetEqStats* neteq,
    111              webrtc::VoENetwork* network,
    112              webrtc::VoERTP_RTCP* rtp,
    113              webrtc::VoEVideoSync* sync,
    114              webrtc::VoEVolumeControl* volume)
    115       : engine_(NULL),
    116         processing_(processing),
    117         base_(base),
    118         codec_(codec),
    119         dtmf_(dtmf),
    120         file_(file),
    121         hw_(hw),
    122         media_(media),
    123         neteq_(neteq),
    124         network_(network),
    125         rtp_(rtp),
    126         sync_(sync),
    127         volume_(volume) {
    128   }
    129   ~VoEWrapper() {}
    130   webrtc::VoiceEngine* engine() const { return engine_.get(); }
    131   webrtc::VoEAudioProcessing* processing() const { return processing_.get(); }
    132   webrtc::VoEBase* base() const { return base_.get(); }
    133   webrtc::VoECodec* codec() const { return codec_.get(); }
    134   webrtc::VoEDtmf* dtmf() const { return dtmf_.get(); }
    135   webrtc::VoEFile* file() const { return file_.get(); }
    136   webrtc::VoEHardware* hw() const { return hw_.get(); }
    137   webrtc::VoEExternalMedia* media() const { return media_.get(); }
    138   webrtc::VoENetEqStats* neteq() const { return neteq_.get(); }
    139   webrtc::VoENetwork* network() const { return network_.get(); }
    140   webrtc::VoERTP_RTCP* rtp() const { return rtp_.get(); }
    141   webrtc::VoEVideoSync* sync() const { return sync_.get(); }
    142   webrtc::VoEVolumeControl* volume() const { return volume_.get(); }
    143   int error() { return base_->LastError(); }
    144 
    145  private:
    146   scoped_voe_engine engine_;
    147   scoped_voe_ptr<webrtc::VoEAudioProcessing> processing_;
    148   scoped_voe_ptr<webrtc::VoEBase> base_;
    149   scoped_voe_ptr<webrtc::VoECodec> codec_;
    150   scoped_voe_ptr<webrtc::VoEDtmf> dtmf_;
    151   scoped_voe_ptr<webrtc::VoEFile> file_;
    152   scoped_voe_ptr<webrtc::VoEHardware> hw_;
    153   scoped_voe_ptr<webrtc::VoEExternalMedia> media_;
    154   scoped_voe_ptr<webrtc::VoENetEqStats> neteq_;
    155   scoped_voe_ptr<webrtc::VoENetwork> network_;
    156   scoped_voe_ptr<webrtc::VoERTP_RTCP> rtp_;
    157   scoped_voe_ptr<webrtc::VoEVideoSync> sync_;
    158   scoped_voe_ptr<webrtc::VoEVolumeControl> volume_;
    159 };
    160 
    161 // Adds indirection to static WebRtc functions, allowing them to be mocked.
    162 class VoETraceWrapper {
    163  public:
    164   virtual ~VoETraceWrapper() {}
    165 
    166   virtual int SetTraceFilter(const unsigned int filter) {
    167     return webrtc::VoiceEngine::SetTraceFilter(filter);
    168   }
    169   virtual int SetTraceFile(const char* fileNameUTF8) {
    170     return webrtc::VoiceEngine::SetTraceFile(fileNameUTF8);
    171   }
    172   virtual int SetTraceCallback(webrtc::TraceCallback* callback) {
    173     return webrtc::VoiceEngine::SetTraceCallback(callback);
    174   }
    175 };
    176 
    177 }  // namespace cricket
    178 
    179 #endif  // TALK_MEDIA_WEBRTCVOE_H_
    180