blob: b8066d74ab17981dd7ab66755f174d7a24273e65 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgae197202012-04-04 14:43:54 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000011#include "video_engine/vie_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000013#if (defined(WIN32_) || defined(WIN64_))
14#include <Windows.h> // For LoadLibrary.
15#include <tchar.h> // For T_.
niklase@google.com470e71d2011-07-07 08:21:25 +000016#endif
17
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000018#include "system_wrappers/interface/trace.h"
19
leozwang@google.com22b699d2011-08-09 17:27:33 +000020#ifdef WEBRTC_ANDROID
andrew@webrtc.org94caca72012-10-30 21:58:00 +000021#include "webrtc/modules/video_capture/include/video_capture_factory.h"
andrew@webrtc.org9841d922012-10-31 05:22:11 +000022#include "webrtc/modules/video_render/include/video_render.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023#endif
24
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000025// Global counter to get an id for each new ViE instance.
26static WebRtc_Word32 g_vie_active_instance_counter = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000027
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000028namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000029
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000030// extern "C" ensures that GetProcAddress() can find the function address.
31extern "C" {
32 VideoEngine* GetVideoEngine() {
niklase@google.com470e71d2011-07-07 08:21:25 +000033 VideoEngineImpl* self = new VideoEngineImpl();
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000034 if (!self) {
35 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000036 }
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000037 g_vie_active_instance_counter++;
38 VideoEngine* vie = reinterpret_cast<VideoEngine*>(self);
niklase@google.com470e71d2011-07-07 08:21:25 +000039 return vie;
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000040 }
niklase@google.com470e71d2011-07-07 08:21:25 +000041}
42
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000043VideoEngine* 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 {
pwestin@webrtc.orgae197202012-04-04 14:43:54 +000055 assert(false && "Failed to open test dll VideoEngineTestingDLL.dll");
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000056 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000057 }
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000058 }
niklase@google.com470e71d2011-07-07 08:21:25 +000059#endif
60
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000061 return GetVideoEngine();
niklase@google.com470e71d2011-07-07 08:21:25 +000062}
63
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000064bool 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);
niklase@google.com470e71d2011-07-07 08:21:25 +000073
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000074 // 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 }
niklase@google.com470e71d2011-07-07 08:21:25 +000081#ifdef WEBRTC_VIDEO_ENGINE_CAPTURE_API
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000082 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 }
niklase@google.com470e71d2011-07-07 08:21:25 +000088#endif
89#ifdef WEBRTC_VIDEO_ENGINE_CODEC_API
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000090 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 }
niklase@google.com470e71d2011-07-07 08:21:25 +000096#endif
97#ifdef WEBRTC_VIDEO_ENGINE_ENCRYPTION_API
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000098 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 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000104#endif
105#ifdef WEBRTC_VIDEO_ENGINE_EXTERNAL_CODEC_API
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000106 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 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000112#endif
113#ifdef WEBRTC_VIDEO_ENGINE_FILE_API
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000114 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 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000120#endif
121#ifdef WEBRTC_VIDEO_ENGINE_IMAGE_PROCESS_API
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000122 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 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000129#endif
130#ifdef WEBRTC_VIDEO_ENGINE_NETWORK_API
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000131 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 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000137#endif
138#ifdef WEBRTC_VIDEO_ENGINE_RENDER_API
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000139 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 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000145#endif
146#ifdef WEBRTC_VIDEO_ENGINE_RTP_RTCP_API
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000147 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 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000153#endif
154
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000155 delete vie_impl;
156 vie_impl = NULL;
157 video_engine = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000158
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000159 // Decrease the number of instances.
160 g_vie_active_instance_counter--;
niklase@google.com470e71d2011-07-07 08:21:25 +0000161
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000162 WEBRTC_TRACE(kTraceInfo, kTraceVideo, g_vie_active_instance_counter,
niklase@google.com470e71d2011-07-07 08:21:25 +0000163 "%s: instance deleted. Remaining instances: %d", __FUNCTION__,
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000164 g_vie_active_instance_counter);
165 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000166}
167
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000168int 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;
niklase@google.com470e71d2011-07-07 08:21:25 +0000180}
181
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000182int VideoEngine::SetTraceFilter(const unsigned int filter) {
183 WebRtc_UWord32 old_filter = 0;
184 Trace::LevelFilter(old_filter);
niklase@google.com470e71d2011-07-07 08:21:25 +0000185
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000186 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 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000191
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000192 WebRtc_Word32 error = Trace::SetLevelFilter(filter);
193 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, g_vie_active_instance_counter,
niklase@google.com470e71d2011-07-07 08:21:25 +0000194 "SetTraceFilter(filter = 0x%x)", filter);
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000195 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;
niklase@google.com470e71d2011-07-07 08:21:25 +0000201}
202
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000203int 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);
niklase@google.com470e71d2011-07-07 08:21:25 +0000207}
208
leozwang@webrtc.orgf9cd6932012-01-23 16:56:13 +0000209int VideoEngine::SetAndroidObjects(void* javaVM, void* javaContext) {
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000210 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, g_vie_active_instance_counter,
niklase@google.com470e71d2011-07-07 08:21:25 +0000211 "SetAndroidObjects()");
212
leozwang@webrtc.org77609632012-07-13 22:00:43 +0000213#if defined(WEBRTC_ANDROID) && !defined(WEBRTC_CHROMIUM_BUILD)
214 if (SetCaptureAndroidVM(javaVM, javaContext) != 0) {
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000215 WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter,
leozwang@webrtc.org77609632012-07-13 22:00:43 +0000216 "Could not set capture Android VM");
niklase@google.com470e71d2011-07-07 08:21:25 +0000217 return -1;
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000218 }
leozwang@webrtc.org77609632012-07-13 22:00:43 +0000219 if (SetRenderAndroidVM(javaVM) != 0) {
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000220 WEBRTC_TRACE(kTraceError, kTraceVideo, g_vie_active_instance_counter,
leozwang@webrtc.org77609632012-07-13 22:00:43 +0000221 "Could not set render Android VM");
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000222 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;
niklase@google.com470e71d2011-07-07 08:21:25 +0000229#endif
230}
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000231
232} // namespace webrtc