Home | History | Annotate | Download | only in video_capture
      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, jobject g_context);
     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   jobject context = ::base::android::GetApplicationContext();
     48   JavaVM* jvm = NULL;
     49   int status = jni->GetJavaVM(&jvm);
     50   ASSERT(status == 0);
     51   status = webrtc::SetCaptureAndroidVM(jvm, context) == 0;
     52   ASSERT(status);
     53 }
     54 
     55 void EnsureInitialized() {
     56   int ret = pthread_once(&g_initialize_once, &EnsureInitializedOnce);
     57   ASSERT(ret == 0);
     58 }
     59 
     60 }  // namespace videocapturemodule
     61 }  // namespace webrtc
     62 
     63 #endif  // ANDROID & WEBRTC_CHROMIUM_BUILD
     64