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 | #include "video_engine/vie_impl.h" |
| 12 | |
| 13 | #if (defined(WIN32_) || defined(WIN64_)) |
| 14 | #include <Windows.h> // For LoadLibrary. |
| 15 | #include <tchar.h> // For T_. |
| 16 | #endif |
| 17 | |
| 18 | #include "system_wrappers/interface/trace.h" |
| 19 | |
| 20 | #ifdef WEBRTC_ANDROID |
andrew@webrtc.org | 5f6856f | 2012-10-30 21:58:00 +0000 | [diff] [blame] | 21 | #include "webrtc/modules/video_capture/include/video_capture_factory.h" |
andrew@webrtc.org | b43b611 | 2012-10-31 05:22:11 +0000 | [diff] [blame^] | 22 | #include "webrtc/modules/video_render/include/video_render.h" |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
| 25 | // Global counter to get an id for each new ViE instance. |
| 26 | static WebRtc_Word32 g_vie_active_instance_counter = 0; |
| 27 | |
| 28 | namespace webrtc { |
| 29 | |
| 30 | // extern "C" ensures that GetProcAddress() can find the function address. |
| 31 | extern "C" { |
| 32 | VideoEngine* GetVideoEngine() { |
| 33 | VideoEngineImpl* self = new VideoEngineImpl(); |
| 34 | if (!self) { |
| 35 | return NULL; |
| 36 | } |
| 37 | g_vie_active_instance_counter++; |
| 38 | VideoEngine* vie = reinterpret_cast<VideoEngine*>(self); |
| 39 | return vie; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | VideoEngine* VideoEngine::Create() { |
| 44 | #if (defined(WIN32_) || defined(WIN64_)) |
| 45 | // Load a debug dll, if there is one. |
| 46 | HMODULE hmod_ = LoadLibrary(TEXT("VideoEngineTestingDLL.dll")); |
| 47 | if (hmod_) { |
| 48 | typedef VideoEngine* (*PFNGetVideoEngineLib)(void); |
| 49 | PFNGetVideoEngineLib pfn = |
| 50 | (PFNGetVideoEngineLib)GetProcAddress(hmod_, "GetVideoEngine"); |
| 51 | if (pfn) { |
| 52 | VideoEngine* self = pfn(); |
| 53 | return self; |
| 54 | } else { |
| 55 | assert(false && "Failed to open test dll VideoEngineTestingDLL.dll"); |
| 56 | return NULL; |
| 57 | } |
| 58 | } |
| 59 | #endif |
| 60 | |
| 61 | return GetVideoEngine(); |
| 62 | } |
| 63 | |
| 64 | bool VideoEngine::Delete(VideoEngine*& video_engine) { |
| 65 | if (!video_engine) { |
| 66 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 67 | "VideoEngine::Delete - No argument"); |
| 68 | return false; |
| 69 | } |
| 70 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, g_vie_active_instance_counter, |
| 71 | "VideoEngine::Delete(vie = 0x%p)", video_engine); |
| 72 | VideoEngineImpl* vie_impl = reinterpret_cast<VideoEngineImpl*>(video_engine); |
| 73 | |
| 74 | // Check all reference counters. |
| 75 | ViEBaseImpl* vie_base = vie_impl; |
| 76 | if (vie_base->GetCount() > 0) { |
| 77 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 78 | "ViEBase ref count: %d", vie_base->GetCount()); |
| 79 | return false; |
| 80 | } |
| 81 | #ifdef WEBRTC_VIDEO_ENGINE_CAPTURE_API |
| 82 | ViECaptureImpl* vie_capture = vie_impl; |
| 83 | if (vie_capture->GetCount() > 0) { |
| 84 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 85 | "ViECapture ref count: %d", vie_capture->GetCount()); |
| 86 | return false; |
| 87 | } |
| 88 | #endif |
| 89 | #ifdef WEBRTC_VIDEO_ENGINE_CODEC_API |
| 90 | ViECodecImpl* vie_codec = vie_impl; |
| 91 | if (vie_codec->GetCount() > 0) { |
| 92 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 93 | "ViECodec ref count: %d", vie_codec->GetCount()); |
| 94 | return false; |
| 95 | } |
| 96 | #endif |
| 97 | #ifdef WEBRTC_VIDEO_ENGINE_ENCRYPTION_API |
| 98 | ViEEncryptionImpl* vie_encryption = vie_impl; |
| 99 | if (vie_encryption->GetCount() > 0) { |
| 100 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 101 | "ViEEncryption ref count: %d", vie_encryption->GetCount()); |
| 102 | return false; |
| 103 | } |
| 104 | #endif |
| 105 | #ifdef WEBRTC_VIDEO_ENGINE_EXTERNAL_CODEC_API |
| 106 | ViEExternalCodecImpl* vie_external_codec = vie_impl; |
| 107 | if (vie_external_codec->GetCount() > 0) { |
| 108 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 109 | "ViEEncryption ref count: %d", vie_encryption->GetCount()); |
| 110 | return false; |
| 111 | } |
| 112 | #endif |
| 113 | #ifdef WEBRTC_VIDEO_ENGINE_FILE_API |
| 114 | ViEFileImpl* vie_file = vie_impl; |
| 115 | if (vie_file->GetCount() > 0) { |
| 116 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 117 | "ViEFile ref count: %d", vie_file->GetCount()); |
| 118 | return false; |
| 119 | } |
| 120 | #endif |
| 121 | #ifdef WEBRTC_VIDEO_ENGINE_IMAGE_PROCESS_API |
| 122 | ViEImageProcessImpl* vie_image_process = vie_impl; |
| 123 | if (vie_image_process->GetCount() > 0) { |
| 124 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 125 | "ViEImageProcess ref count: %d", |
| 126 | vie_image_process->GetCount()); |
| 127 | return false; |
| 128 | } |
| 129 | #endif |
| 130 | #ifdef WEBRTC_VIDEO_ENGINE_NETWORK_API |
| 131 | ViENetworkImpl* vie_network = vie_impl; |
| 132 | if (vie_network->GetCount() > 0) { |
| 133 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 134 | "ViENetwork ref count: %d", vie_network->GetCount()); |
| 135 | return false; |
| 136 | } |
| 137 | #endif |
| 138 | #ifdef WEBRTC_VIDEO_ENGINE_RENDER_API |
| 139 | ViERenderImpl* vie_render = vie_impl; |
| 140 | if (vie_render->GetCount() > 0) { |
| 141 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 142 | "ViERender ref count: %d", vie_render->GetCount()); |
| 143 | return false; |
| 144 | } |
| 145 | #endif |
| 146 | #ifdef WEBRTC_VIDEO_ENGINE_RTP_RTCP_API |
| 147 | ViERTP_RTCPImpl* vie_rtp_rtcp = vie_impl; |
| 148 | if (vie_rtp_rtcp->GetCount() > 0) { |
| 149 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 150 | "ViERTP_RTCP ref count: %d", vie_rtp_rtcp->GetCount()); |
| 151 | return false; |
| 152 | } |
| 153 | #endif |
| 154 | |
| 155 | delete vie_impl; |
| 156 | vie_impl = NULL; |
| 157 | video_engine = NULL; |
| 158 | |
| 159 | // Decrease the number of instances. |
| 160 | g_vie_active_instance_counter--; |
| 161 | |
| 162 | WEBRTC_TRACE(kTraceInfo, kTraceVideo, g_vie_active_instance_counter, |
| 163 | "%s: instance deleted. Remaining instances: %d", __FUNCTION__, |
| 164 | g_vie_active_instance_counter); |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | int VideoEngine::SetTraceFile(const char* file_nameUTF8, |
| 169 | const bool add_file_counter) { |
| 170 | if (!file_nameUTF8) { |
| 171 | return -1; |
| 172 | } |
| 173 | if (Trace::SetTraceFile(file_nameUTF8, add_file_counter) == -1) { |
| 174 | return -1; |
| 175 | } |
| 176 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, g_vie_active_instance_counter, |
| 177 | "SetTraceFileName(file_nameUTF8 = %s, add_file_counter = %d", |
| 178 | file_nameUTF8, add_file_counter); |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | int VideoEngine::SetTraceFilter(const unsigned int filter) { |
| 183 | WebRtc_UWord32 old_filter = 0; |
| 184 | Trace::LevelFilter(old_filter); |
| 185 | |
| 186 | if (filter == kTraceNone && old_filter != kTraceNone) { |
| 187 | // Do the logging before turning it off. |
| 188 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, g_vie_active_instance_counter, |
| 189 | "SetTraceFilter(filter = 0x%x)", filter); |
| 190 | } |
| 191 | |
| 192 | WebRtc_Word32 error = Trace::SetLevelFilter(filter); |
| 193 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, g_vie_active_instance_counter, |
| 194 | "SetTraceFilter(filter = 0x%x)", filter); |
| 195 | if (error != 0) { |
| 196 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 197 | "SetTraceFilter error: %d", error); |
| 198 | return -1; |
| 199 | } |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | int VideoEngine::SetTraceCallback(TraceCallback* callback) { |
| 204 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, g_vie_active_instance_counter, |
| 205 | "SetTraceCallback(TraceCallback = 0x%p)", callback); |
| 206 | return Trace::SetTraceCallback(callback); |
| 207 | } |
| 208 | |
| 209 | int VideoEngine::SetAndroidObjects(void* javaVM, void* javaContext) { |
| 210 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, g_vie_active_instance_counter, |
| 211 | "SetAndroidObjects()"); |
| 212 | |
| 213 | #if defined(WEBRTC_ANDROID) && !defined(WEBRTC_CHROMIUM_BUILD) |
| 214 | if (SetCaptureAndroidVM(javaVM, javaContext) != 0) { |
| 215 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 216 | "Could not set capture Android VM"); |
| 217 | return -1; |
| 218 | } |
| 219 | if (SetRenderAndroidVM(javaVM) != 0) { |
| 220 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 221 | "Could not set render Android VM"); |
| 222 | return -1; |
| 223 | } |
| 224 | return 0; |
| 225 | #else |
| 226 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 227 | "WEBRTC_ANDROID not defined for VideoEngine::SetAndroidObjects"); |
| 228 | return -1; |
| 229 | #endif |
| 230 | } |
| 231 | |
| 232 | } // namespace webrtc |