| niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* | 
| tommi@webrtc.org | a990e12 | 2012-04-26 15:28:22 +0000 | [diff] [blame] | 2 |  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 
| niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 |  | 
| henrike@webrtc.org | 82f014a | 2013-09-10 18:24:07 +0000 | [diff] [blame] | 11 | #if defined(WEBRTC_ANDROID) | 
| Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 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" | 
| henrika@webrtc.org | 8883a0f | 2014-04-09 13:04:12 +0000 | [diff] [blame] | 15 | #endif | 
| leozwang@webrtc.org | 2db85bc | 2012-09-18 20:19:00 +0000 | [diff] [blame] | 16 |  | 
| Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/audio_coding/include/audio_coding_module.h" | 
 | 18 | #include "rtc_base/checks.h" | 
 | 19 | #include "system_wrappers/include/trace.h" | 
 | 20 | #include "voice_engine/channel_proxy.h" | 
 | 21 | #include "voice_engine/voice_engine_impl.h" | 
| henrika@google.com | 73d6551 | 2011-09-07 15:11:18 +0000 | [diff] [blame] | 22 |  | 
| Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 23 | namespace webrtc { | 
| niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 |  | 
 | 25 | // Counter to be ensure that we can add a correct ID in all static trace | 
 | 26 | // methods. It is not the nicest solution, especially not since we already | 
 | 27 | // have a counter in VoEBaseImpl. In other words, there is room for | 
 | 28 | // improvement here. | 
| pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 29 | static int32_t gVoiceEngineInstanceCounter = 0; | 
| niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 |  | 
| solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 31 | VoiceEngine* GetVoiceEngine() { | 
 | 32 |   VoiceEngineImpl* self = new VoiceEngineImpl(); | 
| Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 33 |   if (self != NULL) { | 
 | 34 |     self->AddRef();  // First reference.  Released in VoiceEngine::Delete. | 
 | 35 |     gVoiceEngineInstanceCounter++; | 
 | 36 |   } | 
 | 37 |   return self; | 
| niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | } | 
| niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 |  | 
| tommi@webrtc.org | a990e12 | 2012-04-26 15:28:22 +0000 | [diff] [blame] | 40 | int VoiceEngineImpl::AddRef() { | 
 | 41 |   return ++_ref_count; | 
 | 42 | } | 
 | 43 |  | 
 | 44 | // This implements the Release() method for all the inherited interfaces. | 
 | 45 | int VoiceEngineImpl::Release() { | 
 | 46 |   int new_ref = --_ref_count; | 
 | 47 |   assert(new_ref >= 0); | 
 | 48 |   if (new_ref == 0) { | 
 | 49 |     WEBRTC_TRACE(kTraceApiCall, kTraceVoice, -1, | 
| Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 50 |                  "VoiceEngineImpl self deleting (voiceEngine=0x%p)", this); | 
| tommi@webrtc.org | a990e12 | 2012-04-26 15:28:22 +0000 | [diff] [blame] | 51 |  | 
| henrika@webrtc.org | 944cbeb | 2014-03-18 10:32:33 +0000 | [diff] [blame] | 52 |     // Clear any pointers before starting destruction. Otherwise worker- | 
 | 53 |     // threads will still have pointers to a partially destructed object. | 
 | 54 |     // Example: AudioDeviceBuffer::RequestPlayoutData() can access a | 
 | 55 |     // partially deconstructed |_ptrCbAudioTransport| during destruction | 
 | 56 |     // if we don't call Terminate here. | 
 | 57 |     Terminate(); | 
| tommi@webrtc.org | a990e12 | 2012-04-26 15:28:22 +0000 | [diff] [blame] | 58 |     delete this; | 
 | 59 |   } | 
 | 60 |  | 
 | 61 |   return new_ref; | 
 | 62 | } | 
 | 63 |  | 
| kwiberg | b7f89d6 | 2016-02-17 10:04:18 -0800 | [diff] [blame] | 64 | std::unique_ptr<voe::ChannelProxy> VoiceEngineImpl::GetChannelProxy( | 
| solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 65 |     int channel_id) { | 
 | 66 |   RTC_DCHECK(channel_id >= 0); | 
| tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 67 |   rtc::CritScope cs(crit_sec()); | 
| kwiberg | b7f89d6 | 2016-02-17 10:04:18 -0800 | [diff] [blame] | 68 |   return std::unique_ptr<voe::ChannelProxy>( | 
| solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 69 |       new voe::ChannelProxy(channel_manager().GetChannel(channel_id))); | 
 | 70 | } | 
 | 71 |  | 
| minyue@webrtc.org | e509f94 | 2013-09-12 17:03:00 +0000 | [diff] [blame] | 72 | VoiceEngine* VoiceEngine::Create() { | 
| solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 73 |   return GetVoiceEngine(); | 
| niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 74 | } | 
 | 75 |  | 
| Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 76 | bool VoiceEngine::Delete(VoiceEngine*& voiceEngine) { | 
 | 77 |   if (voiceEngine == NULL) | 
 | 78 |     return false; | 
| niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 |  | 
| Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 80 |   VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine); | 
 | 81 |   // Release the reference that was added in GetVoiceEngine. | 
 | 82 |   int ref = s->Release(); | 
 | 83 |   voiceEngine = NULL; | 
| niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 |  | 
| Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 85 |   if (ref != 0) { | 
 | 86 |     WEBRTC_TRACE( | 
 | 87 |         kTraceWarning, kTraceVoice, -1, | 
 | 88 |         "VoiceEngine::Delete did not release the very last reference.  " | 
 | 89 |         "%d references remain.", | 
 | 90 |         ref); | 
 | 91 |   } | 
| tommi@webrtc.org | a990e12 | 2012-04-26 15:28:22 +0000 | [diff] [blame] | 92 |  | 
| Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 93 |   return true; | 
| niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | } | 
| pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 95 | }  // namespace webrtc |