| 1 | // Copyright 2025 LiveKit, Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | use jni::objects::JObject; |
| 16 | use webrtc_sys::android::ffi as sys_android; |
| 17 | |
| 18 | /// Initialize Android WebRTC with the JVM. |
| 19 | /// |
| 20 | /// This is automatically called by [`initialize_android_context`], so you only |
| 21 | /// need to call this directly if you don't have access to an Android Context |
| 22 | /// (e.g., in `JNI_OnLoad`). |
| 23 | /// |
| 24 | /// This function is idempotent - safe to call multiple times. |
| 25 | pub fn initialize_android(vm: &jni::JavaVM) { |
| 26 | unsafe { |
| 27 | sys_android::init_android(vm.get_java_vm_pointer() as *mut _); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /// Initialize Android WebRTC with the application context. |
| 32 | /// |
| 33 | /// This is the main initialization function for Android. It performs both: |
| 34 | /// 1. JVM initialization (same as [`initialize_android`]) |
| 35 | /// 2. Context initialization (required for PlatformAudio) |
| 36 | /// |
| 37 | /// This function is idempotent - safe to call multiple times. |
| 38 | /// |
| 39 | /// # Arguments |
| 40 | /// * `vm` - The JavaVM instance |
| 41 | /// * `context` - The Android application context |
| 42 | /// |
| 43 | /// # Returns |
| 44 | /// `true` if context initialization succeeded, `false` otherwise. |
| 45 | /// Note: JVM initialization always happens regardless of return value. |
| 46 | /// |
| 47 | /// # Example |
| 48 | /// ```ignore |
| 49 | /// use jni::JavaVM; |
| 50 | /// use jni::objects::JObject; |
| 51 | /// use livekit::webrtc::android::initialize_android_context; |
| 52 | /// |
| 53 | /// fn init(vm: JavaVM, context: JObject) { |
| 54 | /// // Just one call needed - handles both JVM and context init |
| 55 | /// initialize_android_context(&vm, &context); |
| 56 | /// } |
| 57 | /// ``` |
| 58 | pub fn initialize_android_context(vm: &jni::JavaVM, context: &JObject) -> bool { |
| 59 | unsafe { |
| 60 | sys_android::init_android_context( |
| 61 | vm.get_java_vm_pointer() as *mut _, |
| 62 | context.as_raw() as usize, |
| 63 | ) |
| 64 | } |
| 65 | } |