blob: f47b4d7dbd7b70a8fff7faed956a8b8bdc5e6592 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
tommi@webrtc.orga990e122012-04-26 15:28:22 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 Bonadei92ea95e2017-09-15 06:47:31 +020010#include "voice_engine/voice_engine_impl.h"
henrika@google.com73d65512011-09-07 15:11:18 +000011
Jelena Marusic0d266052015-05-04 14:15:32 +020012namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000013
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.org6141e132013-04-09 10:09:10 +000018static int32_t gVoiceEngineInstanceCounter = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000019
solenberg88499ec2016-09-07 07:34:41 -070020VoiceEngine* GetVoiceEngine() {
21 VoiceEngineImpl* self = new VoiceEngineImpl();
Jelena Marusic0d266052015-05-04 14:15:32 +020022 if (self != NULL) {
23 self->AddRef(); // First reference. Released in VoiceEngine::Delete.
24 gVoiceEngineInstanceCounter++;
25 }
26 return self;
niklase@google.com470e71d2011-07-07 08:21:25 +000027}
niklase@google.com470e71d2011-07-07 08:21:25 +000028
tommi@webrtc.orga990e122012-04-26 15:28:22 +000029int VoiceEngineImpl::AddRef() {
30 return ++_ref_count;
31}
32
33// This implements the Release() method for all the inherited interfaces.
34int VoiceEngineImpl::Release() {
35 int new_ref = --_ref_count;
36 assert(new_ref >= 0);
37 if (new_ref == 0) {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +000038 // 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.orga990e122012-04-26 15:28:22 +000044 delete this;
45 }
46
47 return new_ref;
48}
49
minyue@webrtc.orge509f942013-09-12 17:03:00 +000050VoiceEngine* VoiceEngine::Create() {
solenberg88499ec2016-09-07 07:34:41 -070051 return GetVoiceEngine();
niklase@google.com470e71d2011-07-07 08:21:25 +000052}
53
Jelena Marusic0d266052015-05-04 14:15:32 +020054bool VoiceEngine::Delete(VoiceEngine*& voiceEngine) {
55 if (voiceEngine == NULL)
56 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +000057
Jelena Marusic0d266052015-05-04 14:15:32 +020058 VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
Fredrik Solenberg4332d092017-10-04 09:53:35 +020059 s->Release();
Jelena Marusic0d266052015-05-04 14:15:32 +020060 voiceEngine = NULL;
Jelena Marusic0d266052015-05-04 14:15:32 +020061 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +000062}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000063} // namespace webrtc