blob: bd28c45a59acaa2f70981dcdbf7f13b65244691e [file] [log] [blame]
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +00001/*
2 * Copyright (c) 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 "webrtc/modules/audio_device/android/ensure_initialized.h"
12
13#include <pthread.h>
14
15#include "base/android/jni_android.h"
16#include "webrtc/base/checks.h"
17#include "webrtc/modules/audio_device/android/audio_device_template.h"
18#include "webrtc/modules/audio_device/android/audio_record_jni.h"
19#include "webrtc/modules/audio_device/android/audio_track_jni.h"
henrikab2619892015-05-18 16:49:16 +020020#include "webrtc/modules/utility/interface/jvm_android.h"
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +000021
22namespace webrtc {
23namespace audiodevicemodule {
24
25static pthread_once_t g_initialize_once = PTHREAD_ONCE_INIT;
26
27void EnsureInitializedOnce() {
28 CHECK(::base::android::IsVMInitialized());
29 JNIEnv* jni = ::base::android::AttachCurrentThread();
30 JavaVM* jvm = NULL;
31 CHECK_EQ(0, jni->GetJavaVM(&jvm));
32 jobject context = ::base::android::GetApplicationContext();
33
henrikab2619892015-05-18 16:49:16 +020034 // Initialize the Java environment (currently only used by the audio manager).
35 webrtc::JVM::Initialize(jvm, context);
36 // TODO(henrika): remove this call when AudioRecordJni and AudioTrackJni
37 // are modified to use the same sort of Java initialization as the audio
38 // manager.
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +000039 using AudioDeviceJava = AudioDeviceTemplate<AudioRecordJni, AudioTrackJni>;
40 AudioDeviceJava::SetAndroidAudioDeviceObjects(jvm, context);
41
henrika@webrtc.org474d1eb2015-03-09 12:39:53 +000042}
43
44void EnsureInitialized() {
45 CHECK_EQ(0, pthread_once(&g_initialize_once, &EnsureInitializedOnce));
46}
47
48} // namespace audiodevicemodule
49} // namespace webrtc