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 #ifndef TALK_MEDIA_WEBRTCVOE_H_
29 #define TALK_MEDIA_WEBRTCVOE_H_
30 
31 #include "talk/media/webrtc/webrtccommon.h"
32 #include "webrtc/base/common.h"
33 
34 #include "webrtc/common_types.h"
35 #include "webrtc/modules/audio_device/include/audio_device.h"
36 #include "webrtc/voice_engine/include/voe_audio_processing.h"
37 #include "webrtc/voice_engine/include/voe_base.h"
38 #include "webrtc/voice_engine/include/voe_codec.h"
39 #include "webrtc/voice_engine/include/voe_errors.h"
40 #include "webrtc/voice_engine/include/voe_hardware.h"
41 #include "webrtc/voice_engine/include/voe_network.h"
42 #include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
43 #include "webrtc/voice_engine/include/voe_volume_control.h"
44 
45 namespace cricket {
46 // automatically handles lifetime of WebRtc VoiceEngine
47 class scoped_voe_engine {
48  public:
scoped_voe_engine(webrtc::VoiceEngine * e)49   explicit scoped_voe_engine(webrtc::VoiceEngine* e) : ptr(e) {}
50   // VERIFY, to ensure that there are no leaks at shutdown
~scoped_voe_engine()51   ~scoped_voe_engine() { if (ptr) VERIFY(webrtc::VoiceEngine::Delete(ptr)); }
52   // Releases the current pointer.
reset()53   void reset() {
54     if (ptr) {
55       VERIFY(webrtc::VoiceEngine::Delete(ptr));
56       ptr = NULL;
57     }
58   }
get()59   webrtc::VoiceEngine* get() const { return ptr; }
60  private:
61   webrtc::VoiceEngine* ptr;
62 };
63 
64 // scoped_ptr class to handle obtaining and releasing WebRTC interface pointers
65 template<class T>
66 class scoped_voe_ptr {
67  public:
scoped_voe_ptr(const scoped_voe_engine & e)68   explicit scoped_voe_ptr(const scoped_voe_engine& e)
69       : ptr(T::GetInterface(e.get())) {}
scoped_voe_ptr(T * p)70   explicit scoped_voe_ptr(T* p) : ptr(p) {}
~scoped_voe_ptr()71   ~scoped_voe_ptr() { if (ptr) ptr->Release(); }
72   T* operator->() const { return ptr; }
get()73   T* get() const { return ptr; }
74 
75   // Releases the current pointer.
reset()76   void reset() {
77     if (ptr) {
78       ptr->Release();
79       ptr = NULL;
80     }
81   }
82 
83  private:
84   T* ptr;
85 };
86 
87 // Utility class for aggregating the various WebRTC interface.
88 // Fake implementations can also be injected for testing.
89 class VoEWrapper {
90  public:
VoEWrapper()91   VoEWrapper()
92       : engine_(webrtc::VoiceEngine::Create()), processing_(engine_),
93         base_(engine_), codec_(engine_),
94         hw_(engine_), network_(engine_),
95         rtp_(engine_), volume_(engine_) {
96   }
VoEWrapper(webrtc::VoEAudioProcessing * processing,webrtc::VoEBase * base,webrtc::VoECodec * codec,webrtc::VoEHardware * hw,webrtc::VoENetwork * network,webrtc::VoERTP_RTCP * rtp,webrtc::VoEVolumeControl * volume)97   VoEWrapper(webrtc::VoEAudioProcessing* processing,
98              webrtc::VoEBase* base,
99              webrtc::VoECodec* codec,
100              webrtc::VoEHardware* hw,
101              webrtc::VoENetwork* network,
102              webrtc::VoERTP_RTCP* rtp,
103              webrtc::VoEVolumeControl* volume)
104       : engine_(NULL),
105         processing_(processing),
106         base_(base),
107         codec_(codec),
108         hw_(hw),
109         network_(network),
110         rtp_(rtp),
111         volume_(volume) {
112   }
~VoEWrapper()113   ~VoEWrapper() {}
engine()114   webrtc::VoiceEngine* engine() const { return engine_.get(); }
processing()115   webrtc::VoEAudioProcessing* processing() const { return processing_.get(); }
base()116   webrtc::VoEBase* base() const { return base_.get(); }
codec()117   webrtc::VoECodec* codec() const { return codec_.get(); }
hw()118   webrtc::VoEHardware* hw() const { return hw_.get(); }
network()119   webrtc::VoENetwork* network() const { return network_.get(); }
rtp()120   webrtc::VoERTP_RTCP* rtp() const { return rtp_.get(); }
volume()121   webrtc::VoEVolumeControl* volume() const { return volume_.get(); }
error()122   int error() { return base_->LastError(); }
123 
124  private:
125   scoped_voe_engine engine_;
126   scoped_voe_ptr<webrtc::VoEAudioProcessing> processing_;
127   scoped_voe_ptr<webrtc::VoEBase> base_;
128   scoped_voe_ptr<webrtc::VoECodec> codec_;
129   scoped_voe_ptr<webrtc::VoEHardware> hw_;
130   scoped_voe_ptr<webrtc::VoENetwork> network_;
131   scoped_voe_ptr<webrtc::VoERTP_RTCP> rtp_;
132   scoped_voe_ptr<webrtc::VoEVolumeControl> volume_;
133 };
134 }  // namespace cricket
135 
136 #endif  // TALK_MEDIA_WEBRTCVOE_H_
137