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