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