blob: ab2dc65e5bf8b531cbf5e939809cf38a27e8c0b3 [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)
solenberg71b9b582016-11-25 11:45:05 -080012#include "webrtc/modules/audio_device/android/audio_device_template.h"
13#include "webrtc/modules/audio_device/android/audio_record_jni.h"
14#include "webrtc/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
Henrik Kjellandera80c16a2017-07-01 16:48:15 +020017#include "webrtc/base/checks.h"
kjellander3e6db232015-11-26 04:44:54 -080018#include "webrtc/modules/audio_coding/include/audio_coding_module.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010019#include "webrtc/system_wrappers/include/trace.h"
solenberg13725082015-11-25 08:16:52 -080020#include "webrtc/voice_engine/channel_proxy.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000021#include "webrtc/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());
solenberg13725082015-11-25 08:16:52 -080068 RTC_DCHECK(statistics().Initialized());
kwibergb7f89d62016-02-17 10:04:18 -080069 return std::unique_ptr<voe::ChannelProxy>(
solenberg13725082015-11-25 08:16:52 -080070 new voe::ChannelProxy(channel_manager().GetChannel(channel_id)));
71}
72
minyue@webrtc.orge509f942013-09-12 17:03:00 +000073VoiceEngine* VoiceEngine::Create() {
solenberg88499ec2016-09-07 07:34:41 -070074 return GetVoiceEngine();
niklase@google.com470e71d2011-07-07 08:21:25 +000075}
76
Jelena Marusic0d266052015-05-04 14:15:32 +020077int VoiceEngine::SetTraceFilter(unsigned int filter) {
78 WEBRTC_TRACE(kTraceApiCall, kTraceVoice,
79 VoEId(gVoiceEngineInstanceCounter, -1),
80 "SetTraceFilter(filter=0x%x)", filter);
niklase@google.com470e71d2011-07-07 08:21:25 +000081
Jelena Marusic0d266052015-05-04 14:15:32 +020082 // Remember old filter
83 uint32_t oldFilter = Trace::level_filter();
84 Trace::set_level_filter(filter);
niklase@google.com470e71d2011-07-07 08:21:25 +000085
Jelena Marusic0d266052015-05-04 14:15:32 +020086 // If previous log was ignored, log again after changing filter
87 if (kTraceNone == oldFilter) {
88 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, -1, "SetTraceFilter(filter=0x%x)",
89 filter);
90 }
niklase@google.com470e71d2011-07-07 08:21:25 +000091
Jelena Marusic0d266052015-05-04 14:15:32 +020092 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000093}
94
Jelena Marusic0d266052015-05-04 14:15:32 +020095int VoiceEngine::SetTraceFile(const char* fileNameUTF8, bool addFileCounter) {
96 int ret = Trace::SetTraceFile(fileNameUTF8, addFileCounter);
97 WEBRTC_TRACE(kTraceApiCall, kTraceVoice,
98 VoEId(gVoiceEngineInstanceCounter, -1),
99 "SetTraceFile(fileNameUTF8=%s, addFileCounter=%d)", fileNameUTF8,
100 addFileCounter);
101 return (ret);
niklase@google.com470e71d2011-07-07 08:21:25 +0000102}
103
Jelena Marusic0d266052015-05-04 14:15:32 +0200104int VoiceEngine::SetTraceCallback(TraceCallback* callback) {
105 WEBRTC_TRACE(kTraceApiCall, kTraceVoice,
106 VoEId(gVoiceEngineInstanceCounter, -1),
107 "SetTraceCallback(callback=0x%x)", callback);
108 return (Trace::SetTraceCallback(callback));
niklase@google.com470e71d2011-07-07 08:21:25 +0000109}
110
Jelena Marusic0d266052015-05-04 14:15:32 +0200111bool VoiceEngine::Delete(VoiceEngine*& voiceEngine) {
112 if (voiceEngine == NULL)
113 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000114
Jelena Marusic0d266052015-05-04 14:15:32 +0200115 VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
116 // Release the reference that was added in GetVoiceEngine.
117 int ref = s->Release();
118 voiceEngine = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000119
Jelena Marusic0d266052015-05-04 14:15:32 +0200120 if (ref != 0) {
121 WEBRTC_TRACE(
122 kTraceWarning, kTraceVoice, -1,
123 "VoiceEngine::Delete did not release the very last reference. "
124 "%d references remain.",
125 ref);
126 }
tommi@webrtc.orga990e122012-04-26 15:28:22 +0000127
Jelena Marusic0d266052015-05-04 14:15:32 +0200128 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000129}
130
solenberg2515af22015-12-02 06:19:36 -0800131std::string VoiceEngine::GetVersionString() {
132 std::string version = "VoiceEngine 4.1.0";
133#ifdef WEBRTC_EXTERNAL_TRANSPORT
134 version += " (External transport build)";
135#endif
136 return version;
137}
138
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000139} // namespace webrtc