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