1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #if defined(WEBRTC_ANDROID)
12 #include "modules/audio_device/android/audio_device_template.h"
13 #include "modules/audio_device/android/audio_record_jni.h"
14 #include "modules/audio_device/android/audio_track_jni.h"
15 #endif
16 
17 #include "modules/audio_coding/include/audio_coding_module.h"
18 #include "rtc_base/checks.h"
19 #include "voice_engine/channel_proxy.h"
20 #include "voice_engine/voice_engine_impl.h"
21 
22 namespace webrtc {
23 
24 // Counter to be ensure that we can add a correct ID in all static trace
25 // methods. It is not the nicest solution, especially not since we already
26 // have a counter in VoEBaseImpl. In other words, there is room for
27 // improvement here.
28 static int32_t gVoiceEngineInstanceCounter = 0;
29 
GetVoiceEngine()30 VoiceEngine* GetVoiceEngine() {
31   VoiceEngineImpl* self = new VoiceEngineImpl();
32   if (self != NULL) {
33     self->AddRef();  // First reference.  Released in VoiceEngine::Delete.
34     gVoiceEngineInstanceCounter++;
35   }
36   return self;
37 }
38 
AddRef()39 int VoiceEngineImpl::AddRef() {
40   return ++_ref_count;
41 }
42 
43 // This implements the Release() method for all the inherited interfaces.
Release()44 int VoiceEngineImpl::Release() {
45   int new_ref = --_ref_count;
46   assert(new_ref >= 0);
47   if (new_ref == 0) {
48     // Clear any pointers before starting destruction. Otherwise worker-
49     // threads will still have pointers to a partially destructed object.
50     // Example: AudioDeviceBuffer::RequestPlayoutData() can access a
51     // partially deconstructed |_ptrCbAudioTransport| during destruction
52     // if we don't call Terminate here.
53     Terminate();
54     delete this;
55   }
56 
57   return new_ref;
58 }
59 
GetChannelProxy(int channel_id)60 std::unique_ptr<voe::ChannelProxy> VoiceEngineImpl::GetChannelProxy(
61     int channel_id) {
62   RTC_DCHECK(channel_id >= 0);
63   rtc::CritScope cs(crit_sec());
64   return std::unique_ptr<voe::ChannelProxy>(
65       new voe::ChannelProxy(channel_manager().GetChannel(channel_id)));
66 }
67 
Create()68 VoiceEngine* VoiceEngine::Create() {
69   return GetVoiceEngine();
70 }
71 
Delete(VoiceEngine * & voiceEngine)72 bool VoiceEngine::Delete(VoiceEngine*& voiceEngine) {
73   if (voiceEngine == NULL)
74     return false;
75 
76   VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
77   s->Release();
78   voiceEngine = NULL;
79   return true;
80 }
81 }  // namespace webrtc
82