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 |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 130 | ViENetworkImpl* vie_network = vie_impl; |
| 131 | if (vie_network->GetCount() > 0) { |
| 132 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 133 | "ViENetwork ref count: %d", vie_network->GetCount()); |
| 134 | return false; |
| 135 | } |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 136 | #ifdef WEBRTC_VIDEO_ENGINE_RENDER_API |
| 137 | ViERenderImpl* vie_render = vie_impl; |
| 138 | if (vie_render->GetCount() > 0) { |
| 139 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 140 | "ViERender ref count: %d", vie_render->GetCount()); |
| 141 | return false; |
| 142 | } |
| 143 | #endif |
| 144 | #ifdef WEBRTC_VIDEO_ENGINE_RTP_RTCP_API |
| 145 | ViERTP_RTCPImpl* vie_rtp_rtcp = vie_impl; |
| 146 | if (vie_rtp_rtcp->GetCount() > 0) { |
| 147 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 148 | "ViERTP_RTCP ref count: %d", vie_rtp_rtcp->GetCount()); |
| 149 | return false; |
| 150 | } |
| 151 | #endif |
| 152 | |
| 153 | delete vie_impl; |
| 154 | vie_impl = NULL; |
| 155 | video_engine = NULL; |
| 156 | |
| 157 | // Decrease the number of instances. |
| 158 | g_vie_active_instance_counter--; |
| 159 | |
| 160 | WEBRTC_TRACE(kTraceInfo, kTraceVideo, g_vie_active_instance_counter, |
| 161 | "%s: instance deleted. Remaining instances: %d", __FUNCTION__, |
| 162 | g_vie_active_instance_counter); |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | int VideoEngine::SetTraceFile(const char* file_nameUTF8, |
| 167 | const bool add_file_counter) { |
| 168 | if (!file_nameUTF8) { |
| 169 | return -1; |
| 170 | } |
| 171 | if (Trace::SetTraceFile(file_nameUTF8, add_file_counter) == -1) { |
| 172 | return -1; |
| 173 | } |
| 174 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, g_vie_active_instance_counter, |
| 175 | "SetTraceFileName(file_nameUTF8 = %s, add_file_counter = %d", |
| 176 | file_nameUTF8, add_file_counter); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | int VideoEngine::SetTraceFilter(const unsigned int filter) { |
| 181 | WebRtc_UWord32 old_filter = 0; |
| 182 | Trace::LevelFilter(old_filter); |
| 183 | |
| 184 | if (filter == kTraceNone && old_filter != kTraceNone) { |
| 185 | // Do the logging before turning it off. |
| 186 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, g_vie_active_instance_counter, |
| 187 | "SetTraceFilter(filter = 0x%x)", filter); |
| 188 | } |
| 189 | |
| 190 | WebRtc_Word32 error = Trace::SetLevelFilter(filter); |
| 191 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, g_vie_active_instance_counter, |
| 192 | "SetTraceFilter(filter = 0x%x)", filter); |
| 193 | if (error != 0) { |
| 194 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 195 | "SetTraceFilter error: %d", error); |
| 196 | return -1; |
| 197 | } |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | int VideoEngine::SetTraceCallback(TraceCallback* callback) { |
| 202 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, g_vie_active_instance_counter, |
| 203 | "SetTraceCallback(TraceCallback = 0x%p)", callback); |
| 204 | return Trace::SetTraceCallback(callback); |
| 205 | } |
| 206 | |
| 207 | int VideoEngine::SetAndroidObjects(void* javaVM, void* javaContext) { |
| 208 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, g_vie_active_instance_counter, |
| 209 | "SetAndroidObjects()"); |
| 210 | |
| 211 | #if defined(WEBRTC_ANDROID) && !defined(WEBRTC_CHROMIUM_BUILD) |
| 212 | if (SetCaptureAndroidVM(javaVM, javaContext) != 0) { |
| 213 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 214 | "Could not set capture Android VM"); |
| 215 | return -1; |
| 216 | } |
| 217 | if (SetRenderAndroidVM(javaVM) != 0) { |
| 218 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 219 | "Could not set render Android VM"); |
| 220 | return -1; |
| 221 | } |
| 222 | return 0; |
| 223 | #else |
| 224 | WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter, |
| 225 | "WEBRTC_ANDROID not defined for VideoEngine::SetAndroidObjects"); |
| 226 | return -1; |
| 227 | #endif |
| 228 | } |
| 229 | |
| 230 | } // namespace webrtc |