Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / video_capture / ensure_initialized.cc
1 /*
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
15 namespace webrtc {
16 namespace videocapturemodule {
17 void 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
28 // 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
36 namespace webrtc {
37
38 // Declared in webrtc/modules/video_capture/include/video_capture.h.
39 int32_t SetCaptureAndroidVM(JavaVM* javaVM);
40
41 namespace videocapturemodule {
42
43 static pthread_once_t g_initialize_once = PTHREAD_ONCE_INIT;
44
45 void EnsureInitializedOnce() {
46   JNIEnv* jni = ::base::android::AttachCurrentThread();
47   JavaVM* jvm = NULL;
48   int status = jni->GetJavaVM(&jvm);
49   ASSERT(status == 0);
50   status = webrtc::SetCaptureAndroidVM(jvm) == 0;
51   ASSERT(status);
52 }
53
54 void EnsureInitialized() {
55   int ret = pthread_once(&g_initialize_once, &EnsureInitializedOnce);
56   ASSERT(ret == 0);
57 }
58
59 }  // namespace videocapturemodule
60 }  // namespace webrtc
61
62 #endif  // ANDROID & WEBRTC_CHROMIUM_BUILD