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 | */ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 10 | #include "voice_engine/voice_engine_impl.h" |
henrika@google.com | 73d6551 | 2011-09-07 15:11:18 +0000 | [diff] [blame] | 11 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 12 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
| 14 | // Counter to be ensure that we can add a correct ID in all static trace |
| 15 | // methods. It is not the nicest solution, especially not since we already |
| 16 | // have a counter in VoEBaseImpl. In other words, there is room for |
| 17 | // improvement here. |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 18 | static int32_t gVoiceEngineInstanceCounter = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 20 | VoiceEngine* GetVoiceEngine() { |
| 21 | VoiceEngineImpl* self = new VoiceEngineImpl(); |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 22 | if (self != NULL) { |
| 23 | self->AddRef(); // First reference. Released in VoiceEngine::Delete. |
| 24 | gVoiceEngineInstanceCounter++; |
| 25 | } |
| 26 | return self; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | |
tommi@webrtc.org | a990e12 | 2012-04-26 15:28:22 +0000 | [diff] [blame] | 29 | int VoiceEngineImpl::AddRef() { |
| 30 | return ++_ref_count; |
| 31 | } |
| 32 | |
| 33 | // This implements the Release() method for all the inherited interfaces. |
| 34 | int VoiceEngineImpl::Release() { |
| 35 | int new_ref = --_ref_count; |
| 36 | assert(new_ref >= 0); |
| 37 | if (new_ref == 0) { |
henrika@webrtc.org | 944cbeb | 2014-03-18 10:32:33 +0000 | [diff] [blame] | 38 | // Clear any pointers before starting destruction. Otherwise worker- |
| 39 | // threads will still have pointers to a partially destructed object. |
| 40 | // Example: AudioDeviceBuffer::RequestPlayoutData() can access a |
| 41 | // partially deconstructed |_ptrCbAudioTransport| during destruction |
| 42 | // if we don't call Terminate here. |
| 43 | Terminate(); |
tommi@webrtc.org | a990e12 | 2012-04-26 15:28:22 +0000 | [diff] [blame] | 44 | delete this; |
| 45 | } |
| 46 | |
| 47 | return new_ref; |
| 48 | } |
| 49 | |
minyue@webrtc.org | e509f94 | 2013-09-12 17:03:00 +0000 | [diff] [blame] | 50 | VoiceEngine* VoiceEngine::Create() { |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 51 | return GetVoiceEngine(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 54 | bool VoiceEngine::Delete(VoiceEngine*& voiceEngine) { |
| 55 | if (voiceEngine == NULL) |
| 56 | return false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 58 | VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine); |
Fredrik Solenberg | 4332d09 | 2017-10-04 09:53:35 +0200 | [diff] [blame] | 59 | s->Release(); |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 60 | voiceEngine = NULL; |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 61 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 63 | } // namespace webrtc |