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 | */ |
| 10 | |
henrike@webrtc.org | 82f014a | 2013-09-10 18:24:07 +0000 | [diff] [blame] | 11 | #if defined(WEBRTC_ANDROID) |
henrike@webrtc.org | 9ee75e9 | 2013-12-11 21:42:44 +0000 | [diff] [blame] | 12 | #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" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 15 | #include "webrtc/modules/utility/include/jvm_android.h" |
henrika@webrtc.org | 8883a0f | 2014-04-09 13:04:12 +0000 | [diff] [blame] | 16 | #endif |
leozwang@webrtc.org | 2db85bc | 2012-09-18 20:19:00 +0000 | [diff] [blame] | 17 | |
Henrik Kjellander | 7464089 | 2015-10-29 11:31:02 +0100 | [diff] [blame] | 18 | #include "webrtc/modules/audio_coding/main/include/audio_coding_module.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 19 | #include "webrtc/system_wrappers/include/trace.h" |
pbos@webrtc.org | 956aa7e | 2013-05-21 13:52:32 +0000 | [diff] [blame] | 20 | #include "webrtc/voice_engine/voice_engine_impl.h" |
henrika@google.com | 73d6551 | 2011-09-07 15:11:18 +0000 | [diff] [blame] | 21 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 22 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | |
| 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.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 28 | static int32_t gVoiceEngineInstanceCounter = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 30 | VoiceEngine* GetVoiceEngine(const Config* config, bool owns_config) { |
minyue@webrtc.org | e509f94 | 2013-09-12 17:03:00 +0000 | [diff] [blame] | 31 | #if (defined _WIN32) |
| 32 | HMODULE hmod = LoadLibrary(TEXT("VoiceEngineTestingDynamic.dll")); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | |
minyue@webrtc.org | e509f94 | 2013-09-12 17:03:00 +0000 | [diff] [blame] | 34 | if (hmod) { |
| 35 | typedef VoiceEngine* (*PfnGetVoiceEngine)(void); |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 36 | PfnGetVoiceEngine pfn = |
| 37 | (PfnGetVoiceEngine)GetProcAddress(hmod, "GetVoiceEngine"); |
minyue@webrtc.org | e509f94 | 2013-09-12 17:03:00 +0000 | [diff] [blame] | 38 | if (pfn) { |
| 39 | VoiceEngine* self = pfn(); |
| 40 | if (owns_config) { |
| 41 | delete config; |
| 42 | } |
| 43 | return (self); |
| 44 | } |
| 45 | } |
| 46 | #endif |
| 47 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 48 | VoiceEngineImpl* self = new VoiceEngineImpl(config, owns_config); |
| 49 | if (self != NULL) { |
| 50 | self->AddRef(); // First reference. Released in VoiceEngine::Delete. |
| 51 | gVoiceEngineInstanceCounter++; |
| 52 | } |
| 53 | return self; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | |
tommi@webrtc.org | a990e12 | 2012-04-26 15:28:22 +0000 | [diff] [blame] | 56 | int VoiceEngineImpl::AddRef() { |
| 57 | return ++_ref_count; |
| 58 | } |
| 59 | |
| 60 | // This implements the Release() method for all the inherited interfaces. |
| 61 | int VoiceEngineImpl::Release() { |
| 62 | int new_ref = --_ref_count; |
| 63 | assert(new_ref >= 0); |
| 64 | if (new_ref == 0) { |
| 65 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, -1, |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 66 | "VoiceEngineImpl self deleting (voiceEngine=0x%p)", this); |
tommi@webrtc.org | a990e12 | 2012-04-26 15:28:22 +0000 | [diff] [blame] | 67 | |
henrika@webrtc.org | 944cbeb | 2014-03-18 10:32:33 +0000 | [diff] [blame] | 68 | // Clear any pointers before starting destruction. Otherwise worker- |
| 69 | // threads will still have pointers to a partially destructed object. |
| 70 | // Example: AudioDeviceBuffer::RequestPlayoutData() can access a |
| 71 | // partially deconstructed |_ptrCbAudioTransport| during destruction |
| 72 | // if we don't call Terminate here. |
| 73 | Terminate(); |
tommi@webrtc.org | a990e12 | 2012-04-26 15:28:22 +0000 | [diff] [blame] | 74 | delete this; |
| 75 | } |
| 76 | |
| 77 | return new_ref; |
| 78 | } |
| 79 | |
minyue@webrtc.org | e509f94 | 2013-09-12 17:03:00 +0000 | [diff] [blame] | 80 | VoiceEngine* VoiceEngine::Create() { |
| 81 | Config* config = new Config(); |
minyue@webrtc.org | e509f94 | 2013-09-12 17:03:00 +0000 | [diff] [blame] | 82 | return GetVoiceEngine(config, true); |
| 83 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | |
minyue@webrtc.org | e509f94 | 2013-09-12 17:03:00 +0000 | [diff] [blame] | 85 | VoiceEngine* VoiceEngine::Create(const Config& config) { |
| 86 | return GetVoiceEngine(&config, false); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 89 | int VoiceEngine::SetTraceFilter(unsigned int filter) { |
| 90 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, |
| 91 | VoEId(gVoiceEngineInstanceCounter, -1), |
| 92 | "SetTraceFilter(filter=0x%x)", filter); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 94 | // Remember old filter |
| 95 | uint32_t oldFilter = Trace::level_filter(); |
| 96 | Trace::set_level_filter(filter); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 98 | // If previous log was ignored, log again after changing filter |
| 99 | if (kTraceNone == oldFilter) { |
| 100 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, -1, "SetTraceFilter(filter=0x%x)", |
| 101 | filter); |
| 102 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 104 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 107 | int VoiceEngine::SetTraceFile(const char* fileNameUTF8, bool addFileCounter) { |
| 108 | int ret = Trace::SetTraceFile(fileNameUTF8, addFileCounter); |
| 109 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, |
| 110 | VoEId(gVoiceEngineInstanceCounter, -1), |
| 111 | "SetTraceFile(fileNameUTF8=%s, addFileCounter=%d)", fileNameUTF8, |
| 112 | addFileCounter); |
| 113 | return (ret); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 116 | int VoiceEngine::SetTraceCallback(TraceCallback* callback) { |
| 117 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, |
| 118 | VoEId(gVoiceEngineInstanceCounter, -1), |
| 119 | "SetTraceCallback(callback=0x%x)", callback); |
| 120 | return (Trace::SetTraceCallback(callback)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 123 | bool VoiceEngine::Delete(VoiceEngine*& voiceEngine) { |
| 124 | if (voiceEngine == NULL) |
| 125 | return false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 126 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 127 | VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine); |
| 128 | // Release the reference that was added in GetVoiceEngine. |
| 129 | int ref = s->Release(); |
| 130 | voiceEngine = NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 131 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 132 | if (ref != 0) { |
| 133 | WEBRTC_TRACE( |
| 134 | kTraceWarning, kTraceVoice, -1, |
| 135 | "VoiceEngine::Delete did not release the very last reference. " |
| 136 | "%d references remain.", |
| 137 | ref); |
| 138 | } |
tommi@webrtc.org | a990e12 | 2012-04-26 15:28:22 +0000 | [diff] [blame] | 139 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 140 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 141 | } |
| 142 | |
henrika@webrtc.org | 8883a0f | 2014-04-09 13:04:12 +0000 | [diff] [blame] | 143 | #if !defined(WEBRTC_CHROMIUM_BUILD) |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 144 | // TODO(henrika): change types to JavaVM* and jobject instead of void*. |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 145 | int VoiceEngine::SetAndroidObjects(void* javaVM, void* context) { |
leozwang@webrtc.org | cf1375a | 2012-09-21 17:39:45 +0000 | [diff] [blame] | 146 | #ifdef WEBRTC_ANDROID |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 147 | webrtc::JVM::Initialize(reinterpret_cast<JavaVM*>(javaVM), |
| 148 | reinterpret_cast<jobject>(context)); |
henrike@webrtc.org | 573a1b4 | 2014-01-10 22:58:06 +0000 | [diff] [blame] | 149 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 150 | #else |
leozwang@webrtc.org | 2db85bc | 2012-09-18 20:19:00 +0000 | [diff] [blame] | 151 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 152 | #endif |
| 153 | } |
henrika@webrtc.org | 8883a0f | 2014-04-09 13:04:12 +0000 | [diff] [blame] | 154 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 155 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 156 | } // namespace webrtc |