andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 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 | |
| 11 | #if defined(WEBRTC_ANDROID) && !defined(WEBRTC_ANDROID_OPENSLES) |
| 12 | #include "modules/audio_device/android/audio_device_jni_android.h" |
| 13 | #endif |
| 14 | |
| 15 | #include "voice_engine_impl.h" |
| 16 | #include "trace.h" |
| 17 | |
| 18 | namespace webrtc |
| 19 | { |
| 20 | |
| 21 | // Counter to be ensure that we can add a correct ID in all static trace |
| 22 | // methods. It is not the nicest solution, especially not since we already |
| 23 | // have a counter in VoEBaseImpl. In other words, there is room for |
| 24 | // improvement here. |
| 25 | static WebRtc_Word32 gVoiceEngineInstanceCounter = 0; |
| 26 | |
| 27 | extern "C" |
| 28 | { |
| 29 | WEBRTC_DLLEXPORT VoiceEngine* GetVoiceEngine(); |
| 30 | |
| 31 | VoiceEngine* GetVoiceEngine() |
| 32 | { |
| 33 | VoiceEngineImpl* self = new VoiceEngineImpl(); |
| 34 | VoiceEngine* ve = reinterpret_cast<VoiceEngine*>(self); |
| 35 | if (ve != NULL) |
| 36 | { |
| 37 | self->AddRef(); // First reference. Released in VoiceEngine::Delete. |
| 38 | gVoiceEngineInstanceCounter++; |
| 39 | } |
| 40 | return ve; |
| 41 | } |
| 42 | } // extern "C" |
| 43 | |
| 44 | int VoiceEngineImpl::AddRef() { |
| 45 | return ++_ref_count; |
| 46 | } |
| 47 | |
| 48 | // This implements the Release() method for all the inherited interfaces. |
| 49 | int VoiceEngineImpl::Release() { |
| 50 | int new_ref = --_ref_count; |
| 51 | assert(new_ref >= 0); |
| 52 | if (new_ref == 0) { |
| 53 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, -1, |
| 54 | "VoiceEngineImpl self deleting (voiceEngine=0x%p)", |
| 55 | this); |
| 56 | |
| 57 | delete this; |
| 58 | } |
| 59 | |
| 60 | return new_ref; |
| 61 | } |
| 62 | |
| 63 | VoiceEngine* VoiceEngine::Create() |
| 64 | { |
| 65 | #if (defined _WIN32) |
| 66 | HMODULE hmod_ = LoadLibrary(TEXT("VoiceEngineTestingDynamic.dll")); |
| 67 | |
| 68 | if (hmod_) |
| 69 | { |
| 70 | typedef VoiceEngine* (*PfnGetVoiceEngine)(void); |
| 71 | PfnGetVoiceEngine pfn = (PfnGetVoiceEngine)GetProcAddress( |
| 72 | hmod_,"GetVoiceEngine"); |
| 73 | if (pfn) |
| 74 | { |
| 75 | VoiceEngine* self = pfn(); |
| 76 | return (self); |
| 77 | } |
| 78 | } |
| 79 | #endif |
| 80 | |
| 81 | return GetVoiceEngine(); |
| 82 | } |
| 83 | |
| 84 | int VoiceEngine::SetTraceFilter(const unsigned int filter) |
| 85 | { |
| 86 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, |
| 87 | VoEId(gVoiceEngineInstanceCounter, -1), |
| 88 | "SetTraceFilter(filter=0x%x)", filter); |
| 89 | |
| 90 | // Remember old filter |
| 91 | WebRtc_UWord32 oldFilter = 0; |
| 92 | Trace::LevelFilter(oldFilter); |
| 93 | |
| 94 | // Set new filter |
| 95 | WebRtc_Word32 ret = Trace::SetLevelFilter(filter); |
| 96 | |
| 97 | // If previous log was ignored, log again after changing filter |
| 98 | if (kTraceNone == oldFilter) |
| 99 | { |
| 100 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, -1, |
| 101 | "SetTraceFilter(filter=0x%x)", filter); |
| 102 | } |
| 103 | |
| 104 | return (ret); |
| 105 | } |
| 106 | |
| 107 | int VoiceEngine::SetTraceFile(const char* fileNameUTF8, |
| 108 | const bool addFileCounter) |
| 109 | { |
| 110 | int ret = Trace::SetTraceFile(fileNameUTF8, addFileCounter); |
| 111 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, |
| 112 | VoEId(gVoiceEngineInstanceCounter, -1), |
| 113 | "SetTraceFile(fileNameUTF8=%s, addFileCounter=%d)", |
| 114 | fileNameUTF8, addFileCounter); |
| 115 | return (ret); |
| 116 | } |
| 117 | |
| 118 | int VoiceEngine::SetTraceCallback(TraceCallback* callback) |
| 119 | { |
| 120 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, |
| 121 | VoEId(gVoiceEngineInstanceCounter, -1), |
| 122 | "SetTraceCallback(callback=0x%x)", callback); |
| 123 | return (Trace::SetTraceCallback(callback)); |
| 124 | } |
| 125 | |
| 126 | bool VoiceEngine::Delete(VoiceEngine*& voiceEngine) |
| 127 | { |
| 128 | if (voiceEngine == NULL) |
| 129 | return false; |
| 130 | |
| 131 | VoiceEngineImpl* s = reinterpret_cast<VoiceEngineImpl*>(voiceEngine); |
| 132 | // Release the reference that was added in GetVoiceEngine. |
| 133 | int ref = s->Release(); |
| 134 | voiceEngine = NULL; |
| 135 | |
| 136 | if (ref != 0) { |
| 137 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, -1, |
| 138 | "VoiceEngine::Delete did not release the very last reference. " |
| 139 | "%d references remain.", ref); |
| 140 | } |
| 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | int VoiceEngine::SetAndroidObjects(void* javaVM, void* env, void* context) |
| 146 | { |
| 147 | #ifdef WEBRTC_ANDROID |
| 148 | #ifdef WEBRTC_ANDROID_OPENSLES |
| 149 | return 0; |
| 150 | #else |
| 151 | return AudioDeviceAndroidJni::SetAndroidAudioDeviceObjects( |
| 152 | javaVM, env, context); |
| 153 | #endif |
| 154 | #else |
| 155 | return -1; |
| 156 | #endif |
| 157 | } |
| 158 | |
| 159 | } //namespace webrtc |