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_WEBRTCVIE_H_ 30 #define TALK_MEDIA_WEBRTCVIE_H_ 31 32 #include "talk/base/common.h" 33 #include "talk/media/webrtc/webrtccommon.h" 34 #include "webrtc/common_types.h" 35 #include "webrtc/modules/interface/module_common_types.h" 36 #include "webrtc/modules/video_capture/include/video_capture.h" 37 #include "webrtc/modules/video_coding/codecs/interface/video_codec_interface.h" 38 #include "webrtc/modules/video_render/include/video_render.h" 39 #include "webrtc/video_engine/include/vie_base.h" 40 #include "webrtc/video_engine/include/vie_capture.h" 41 #include "webrtc/video_engine/include/vie_codec.h" 42 #include "webrtc/video_engine/include/vie_errors.h" 43 #include "webrtc/video_engine/include/vie_external_codec.h" 44 #include "webrtc/video_engine/include/vie_image_process.h" 45 #include "webrtc/video_engine/include/vie_network.h" 46 #include "webrtc/video_engine/include/vie_render.h" 47 #include "webrtc/video_engine/include/vie_rtp_rtcp.h" 48 49 namespace cricket { 50 51 // all tracing macros should go to a common file 52 53 // automatically handles lifetime of VideoEngine 54 class scoped_vie_engine { 55 public: 56 explicit scoped_vie_engine(webrtc::VideoEngine* e) : ptr(e) {} 57 // VERIFY, to ensure that there are no leaks at shutdown 58 ~scoped_vie_engine() { 59 if (ptr) { 60 webrtc::VideoEngine::Delete(ptr); 61 } 62 } 63 webrtc::VideoEngine* get() const { return ptr; } 64 private: 65 webrtc::VideoEngine* ptr; 66 }; 67 68 // scoped_ptr class to handle obtaining and releasing VideoEngine 69 // interface pointers 70 template<class T> class scoped_vie_ptr { 71 public: 72 explicit scoped_vie_ptr(const scoped_vie_engine& e) 73 : ptr(T::GetInterface(e.get())) {} 74 explicit scoped_vie_ptr(T* p) : ptr(p) {} 75 ~scoped_vie_ptr() { if (ptr) ptr->Release(); } 76 T* operator->() const { return ptr; } 77 T* get() const { return ptr; } 78 private: 79 T* ptr; 80 }; 81 82 // Utility class for aggregating the various WebRTC interface. 83 // Fake implementations can also be injected for testing. 84 class ViEWrapper { 85 public: 86 ViEWrapper() 87 : engine_(webrtc::VideoEngine::Create()), 88 base_(engine_), codec_(engine_), capture_(engine_), 89 network_(engine_), render_(engine_), rtp_(engine_), 90 image_(engine_), ext_codec_(engine_) { 91 } 92 93 ViEWrapper(webrtc::ViEBase* base, webrtc::ViECodec* codec, 94 webrtc::ViECapture* capture, webrtc::ViENetwork* network, 95 webrtc::ViERender* render, webrtc::ViERTP_RTCP* rtp, 96 webrtc::ViEImageProcess* image, 97 webrtc::ViEExternalCodec* ext_codec) 98 : engine_(NULL), 99 base_(base), 100 codec_(codec), 101 capture_(capture), 102 network_(network), 103 render_(render), 104 rtp_(rtp), 105 image_(image), 106 ext_codec_(ext_codec) { 107 } 108 109 virtual ~ViEWrapper() {} 110 webrtc::VideoEngine* engine() { return engine_.get(); } 111 webrtc::ViEBase* base() { return base_.get(); } 112 webrtc::ViECodec* codec() { return codec_.get(); } 113 webrtc::ViECapture* capture() { return capture_.get(); } 114 webrtc::ViENetwork* network() { return network_.get(); } 115 webrtc::ViERender* render() { return render_.get(); } 116 webrtc::ViERTP_RTCP* rtp() { return rtp_.get(); } 117 webrtc::ViEImageProcess* image() { return image_.get(); } 118 webrtc::ViEExternalCodec* ext_codec() { return ext_codec_.get(); } 119 int error() { return base_->LastError(); } 120 121 private: 122 scoped_vie_engine engine_; 123 scoped_vie_ptr<webrtc::ViEBase> base_; 124 scoped_vie_ptr<webrtc::ViECodec> codec_; 125 scoped_vie_ptr<webrtc::ViECapture> capture_; 126 scoped_vie_ptr<webrtc::ViENetwork> network_; 127 scoped_vie_ptr<webrtc::ViERender> render_; 128 scoped_vie_ptr<webrtc::ViERTP_RTCP> rtp_; 129 scoped_vie_ptr<webrtc::ViEImageProcess> image_; 130 scoped_vie_ptr<webrtc::ViEExternalCodec> ext_codec_; 131 }; 132 133 // Adds indirection to static WebRtc functions, allowing them to be mocked. 134 class ViETraceWrapper { 135 public: 136 virtual ~ViETraceWrapper() {} 137 138 virtual int SetTraceFilter(const unsigned int filter) { 139 return webrtc::VideoEngine::SetTraceFilter(filter); 140 } 141 virtual int SetTraceFile(const char* fileNameUTF8) { 142 return webrtc::VideoEngine::SetTraceFile(fileNameUTF8); 143 } 144 virtual int SetTraceCallback(webrtc::TraceCallback* callback) { 145 return webrtc::VideoEngine::SetTraceCallback(callback); 146 } 147 }; 148 149 } // namespace cricket 150 151 #endif // TALK_MEDIA_WEBRTCVIE_H_ 152