blob: 3f2ebd4c3ba1a809d2ff372b1c606093c8fbc9d8 [file] [log] [blame]
phoglund37ebcf02016-01-08 05:04:57 -08001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
phoglund37ebcf02016-01-08 05:04:57 -08003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
phoglund37ebcf02016-01-08 05:04:57 -08009 */
10
ossu7bb87ee2017-01-23 04:56:25 -080011#include "webrtc/pc/test/androidtestinitializer.h"
phoglund37ebcf02016-01-08 05:04:57 -080012
13#include <pthread.h>
14
kwiberg77eab702016-09-28 17:42:01 -070015#include "webrtc/base/ignore_wundef.h"
16
phoglund37ebcf02016-01-08 05:04:57 -080017// Note: this dependency is dangerous since it reaches into Chromium's base.
18// There's a risk of e.g. macro clashes. This file may only be used in tests.
19// Since we use Chromes build system for creating the gtest binary, this should
20// be fine.
kwiberg77eab702016-09-28 17:42:01 -070021RTC_PUSH_IGNORING_WUNDEF()
lliuu548cdce2017-05-24 16:45:57 -070022#include "base/android/context_utils.h"
phoglund37ebcf02016-01-08 05:04:57 -080023#include "base/android/jni_android.h"
kwiberg77eab702016-09-28 17:42:01 -070024RTC_POP_IGNORING_WUNDEF()
phoglund37ebcf02016-01-08 05:04:57 -080025
phoglund37ebcf02016-01-08 05:04:57 -080026#include "webrtc/base/checks.h"
27#include "webrtc/base/ssladapter.h"
deadbeefb4fc73a2017-04-10 15:08:02 -070028#include "webrtc/modules/utility/include/jvm_android.h"
phoglund37ebcf02016-01-08 05:04:57 -080029
30namespace webrtc {
31
32namespace {
33
34static pthread_once_t g_initialize_once = PTHREAD_ONCE_INIT;
35
36// There can only be one JNI_OnLoad in each binary. So since this is a GTEST
37// C++ runner binary, we want to initialize the same global objects we normally
38// do if this had been a Java binary.
39void EnsureInitializedOnce() {
40 RTC_CHECK(::base::android::IsVMInitialized());
41 JNIEnv* jni = ::base::android::AttachCurrentThread();
42 JavaVM* jvm = NULL;
43 RTC_CHECK_EQ(0, jni->GetJavaVM(&jvm));
lliuu548cdce2017-05-24 16:45:57 -070044 jobject context = ::base::android::GetApplicationContext().obj();
phoglund37ebcf02016-01-08 05:04:57 -080045
phoglund37ebcf02016-01-08 05:04:57 -080046 RTC_CHECK(rtc::InitializeSSL()) << "Failed to InitializeSSL()";
phoglund37ebcf02016-01-08 05:04:57 -080047
lliuu548cdce2017-05-24 16:45:57 -070048 webrtc::JVM::Initialize(jvm, context);
phoglund37ebcf02016-01-08 05:04:57 -080049}
50
51} // anonymous namespace
52
53void InitializeAndroidObjects() {
54 RTC_CHECK_EQ(0, pthread_once(&g_initialize_once, &EnsureInitializedOnce));
55}
56
57} // namespace webrtc