blob: b0ff0849ba770d07f979bc965b3c227ae6a2d554 [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "voice_engine/channel_proxy.h"
20#include "voice_engine/voice_engine_impl.h"
henrika@google.com73d65512011-09-07 15:11:18 +000021
Jelena Marusic0d266052015-05-04 14:15:32 +020022namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
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.
pbos@webrtc.org6141e132013-04-09 10:09:10 +000028static int32_t gVoiceEngineInstanceCounter = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000029
solenberg88499ec2016-09-07 07:34:41 -070030VoiceEngine* GetVoiceEngine() {
31 VoiceEngineImpl* self = new VoiceEngineImpl();
Jelena Marusic0d266052015-05-04 14:15:32 +020032 if (self != NULL) {
33 self->AddRef(); // First reference. Released in VoiceEngine::Delete.
34 gVoiceEngineInstanceCounter++;
35 }
36 return self;
niklase@google.com470e71d2011-07-07 08:21:25 +000037}
niklase@google.com470e71d2011-07-07 08:21:25 +000038
tommi@webrtc.orga990e122012-04-26 15:28:22 +000039int VoiceEngineImpl::AddRef() {
40 return ++_ref_count;
41}
42
43// This implements the Release() method for all the inherited interfaces.
44int VoiceEngineImpl::Release() {
45 int new_ref = --_ref_count;
46 assert(new_ref >= 0);
47 if (new_ref == 0) {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +000048 // 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();
tommi@webrtc.orga990e122012-04-26 15:28:22 +000054 delete this;
55 }
56
57 return new_ref;
58}
59
kwibergb7f89d62016-02-17 10:04:18 -080060std::unique_ptr<voe::ChannelProxy> VoiceEngineImpl::GetChannelProxy(
solenberg13725082015-11-25 08:16:52 -080061 int channel_id) {
62 RTC_DCHECK(channel_id >= 0);
tommi31fc21f2016-01-21 10:37:37 -080063 rtc::CritScope cs(crit_sec());
kwibergb7f89d62016-02-17 10:04:18 -080064 return std::unique_ptr<voe::ChannelProxy>(
solenberg13725082015-11-25 08:16:52 -080065 new voe::ChannelProxy(channel_manager().GetChannel(channel_id)));
66}
67
minyue@webrtc.orge509f942013-09-12 17:03:00 +000068VoiceEngine* VoiceEngine::Create() {
solenberg88499ec2016-09-07 07:34:41 -070069 return GetVoiceEngine();
niklase@google.com470e71d2011-07-07 08:21:25 +000070}
71
Jelena Marusic0d266052015-05-04 14:15:32 +020072bool VoiceEngine::Delete(VoiceEngine*& voiceEngine) {
73 if (voiceEngine == NULL)
74 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +000075
Jelena Marusic0d266052015-05-04 14:15:32 +020076 VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
Fredrik Solenberg4332d092017-10-04 09:53:35 +020077 s->Release();
Jelena Marusic0d266052015-05-04 14:15:32 +020078 voiceEngine = NULL;
Jelena Marusic0d266052015-05-04 14:15:32 +020079 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +000080}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000081} // namespace webrtc