blob: 65c9a8dbe7a57959e780b307a676a903b945c12f [file] [log] [blame]
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +00001/*
2 * Copyright (c) 2014 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// Platform-specific initialization bits, if any, go here.
12
13#if !defined(ANDROID) || !defined(WEBRTC_CHROMIUM_BUILD)
14
15namespace webrtc {
16namespace videocapturemodule {
17void EnsureInitialized() {}
18} // namespace videocapturemodule
19} // namespace webrtc
20
21#else // !defined(ANDROID) || !defined(WEBRTC_CHROMIUM_BUILD)
22
23#include <assert.h>
24#include <pthread.h>
25
26#include "base/android/jni_android.h"
27
fischman@webrtc.orgf9302142014-04-09 21:19:55 +000028// Handy alternative to assert() which suppresses unused-variable warnings when
29// assert() is a no-op (i.e. in Release builds).
30#ifdef NDEBUG
31#define ASSERT(x) if (false && (x)); else
32#else
33#define ASSERT(x) assert(x)
34#endif
35
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +000036namespace webrtc {
37
38// Declared in webrtc/modules/video_capture/include/video_capture.h.
fischman@webrtc.org95127192014-06-06 18:40:44 +000039int32_t SetCaptureAndroidVM(JavaVM* javaVM, jobject g_context);
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +000040
41namespace videocapturemodule {
42
43static pthread_once_t g_initialize_once = PTHREAD_ONCE_INIT;
44
45void EnsureInitializedOnce() {
46 JNIEnv* jni = ::base::android::AttachCurrentThread();
fischman@webrtc.org95127192014-06-06 18:40:44 +000047 jobject context = ::base::android::GetApplicationContext();
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +000048 JavaVM* jvm = NULL;
49 int status = jni->GetJavaVM(&jvm);
fischman@webrtc.orgf9302142014-04-09 21:19:55 +000050 ASSERT(status == 0);
fischman@webrtc.org95127192014-06-06 18:40:44 +000051 status = webrtc::SetCaptureAndroidVM(jvm, context) == 0;
fischman@webrtc.orgf9302142014-04-09 21:19:55 +000052 ASSERT(status);
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +000053}
54
55void EnsureInitialized() {
56 int ret = pthread_once(&g_initialize_once, &EnsureInitializedOnce);
fischman@webrtc.orgf9302142014-04-09 21:19:55 +000057 ASSERT(ret == 0);
fischman@webrtc.orgb0b135e2014-04-09 01:18:32 +000058}
59
60} // namespace videocapturemodule
61} // namespace webrtc
62
63#endif // ANDROID & WEBRTC_CHROMIUM_BUILD