xref: /aosp_15_r20/external/webrtc/pc/test/android_test_initializer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2015 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 "pc/test/android_test_initializer.h"
12 
13 #include <jni.h>
14 #include <pthread.h>
15 #include <stddef.h>
16 
17 #include "modules/utility/include/jvm_android.h"
18 #include "rtc_base/checks.h"
19 #include "sdk/android/src/jni/jvm.h"
20 // TODO(phoglund): This include is to a target we can't really depend on.
21 // We need to either break it out into a smaller target or find some way to
22 // not use it.
23 #include "rtc_base/ssl_adapter.h"
24 
25 namespace webrtc {
26 
27 namespace {
28 
29 static pthread_once_t g_initialize_once = PTHREAD_ONCE_INIT;
30 
31 // There can only be one JNI_OnLoad in each binary. So since this is a GTEST
32 // C++ runner binary, we want to initialize the same global objects we normally
33 // do if this had been a Java binary.
EnsureInitializedOnce()34 void EnsureInitializedOnce() {
35   RTC_CHECK(::webrtc::jni::GetJVM() != nullptr);
36   JNIEnv* jni = ::webrtc::jni::AttachCurrentThreadIfNeeded();
37   JavaVM* jvm = NULL;
38   RTC_CHECK_EQ(0, jni->GetJavaVM(&jvm));
39 
40   RTC_CHECK(rtc::InitializeSSL()) << "Failed to InitializeSSL()";
41 
42   webrtc::JVM::Initialize(jvm);
43 }
44 
45 }  // anonymous namespace
46 
InitializeAndroidObjects()47 void InitializeAndroidObjects() {
48   RTC_CHECK_EQ(0, pthread_once(&g_initialize_once, &EnsureInitializedOnce));
49 }
50 
51 }  // namespace webrtc
52