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