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