henrika@webrtc.org | 474d1eb | 2015-03-09 12:39:53 +0000 | [diff] [blame] | 1 | /* |
| 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" |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame^] | 20 | #include "webrtc/modules/utility/interface/jvm_android.h" |
henrika@webrtc.org | 474d1eb | 2015-03-09 12:39:53 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | namespace audiodevicemodule { |
| 24 | |
| 25 | static pthread_once_t g_initialize_once = PTHREAD_ONCE_INIT; |
| 26 | |
| 27 | void 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 | |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame^] | 34 | // 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.org | 474d1eb | 2015-03-09 12:39:53 +0000 | [diff] [blame] | 39 | using AudioDeviceJava = AudioDeviceTemplate<AudioRecordJni, AudioTrackJni>; |
| 40 | AudioDeviceJava::SetAndroidAudioDeviceObjects(jvm, context); |
| 41 | |
henrika@webrtc.org | 474d1eb | 2015-03-09 12:39:53 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void EnsureInitialized() { |
| 45 | CHECK_EQ(0, pthread_once(&g_initialize_once, &EnsureInitializedOnce)); |
| 46 | } |
| 47 | |
| 48 | } // namespace audiodevicemodule |
| 49 | } // namespace webrtc |