blob: 681f00ed0b518a22b3f9d88a82cd06cc2665386e [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 */
10
henrike@webrtc.org82f014a2013-09-10 18:24:07 +000011#if defined(WEBRTC_ANDROID)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020012#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.org8883a0f2014-04-09 13:04:12 +000015#endif
leozwang@webrtc.org2db85bc2012-09-18 20:19:00 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#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.com73d65512011-09-07 15:11:18 +000022
Jelena Marusic0d266052015-05-04 14:15:32 +020023namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000024
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.org6141e132013-04-09 10:09:10 +000029static int32_t gVoiceEngineInstanceCounter = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000030
solenberg88499ec2016-09-07 07:34:41 -070031VoiceEngine* GetVoiceEngine() {
32 VoiceEngineImpl* self = new VoiceEngineImpl();
Jelena Marusic0d266052015-05-04 14:15:32 +020033 if (self != NULL) {
34 self->AddRef(); // First reference. Released in VoiceEngine::Delete.
35 gVoiceEngineInstanceCounter++;
36 }
37 return self;
niklase@google.com470e71d2011-07-07 08:21:25 +000038}
niklase@google.com470e71d2011-07-07 08:21:25 +000039
tommi@webrtc.orga990e122012-04-26 15:28:22 +000040int VoiceEngineImpl::AddRef() {
41 return ++_ref_count;
42}
43
44// This implements the Release() method for all the inherited interfaces.
45int 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 Marusic0d266052015-05-04 14:15:32 +020050 "VoiceEngineImpl self deleting (voiceEngine=0x%p)", this);
tommi@webrtc.orga990e122012-04-26 15:28:22 +000051
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +000052 // 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.orga990e122012-04-26 15:28:22 +000058 delete this;
59 }
60
61 return new_ref;
62}
63
kwibergb7f89d62016-02-17 10:04:18 -080064std::unique_ptr<voe::ChannelProxy> VoiceEngineImpl::GetChannelProxy(
solenberg13725082015-11-25 08:16:52 -080065 int channel_id) {
66 RTC_DCHECK(channel_id >= 0);
tommi31fc21f2016-01-21 10:37:37 -080067 rtc::CritScope cs(crit_sec());
kwibergb7f89d62016-02-17 10:04:18 -080068 return std::unique_ptr<voe::ChannelProxy>(
solenberg13725082015-11-25 08:16:52 -080069 new voe::ChannelProxy(channel_manager().GetChannel(channel_id)));
70}
71
minyue@webrtc.orge509f942013-09-12 17:03:00 +000072VoiceEngine* VoiceEngine::Create() {
solenberg88499ec2016-09-07 07:34:41 -070073 return GetVoiceEngine();
niklase@google.com470e71d2011-07-07 08:21:25 +000074}
75
Jelena Marusic0d266052015-05-04 14:15:32 +020076bool VoiceEngine::Delete(VoiceEngine*& voiceEngine) {
77 if (voiceEngine == NULL)
78 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +000079
Jelena Marusic0d266052015-05-04 14:15:32 +020080 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.com470e71d2011-07-07 08:21:25 +000084
Jelena Marusic0d266052015-05-04 14:15:32 +020085 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.orga990e122012-04-26 15:28:22 +000092
Jelena Marusic0d266052015-05-04 14:15:32 +020093 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +000094}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000095} // namespace webrtc