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