BlueGit

ict rusty-teams

Files

rusty-teams / vendor / webrtc-sys / src / android.cpp

android.cpp

android.cpp 5.3 KiB Download Raw
1/*
2 * Copyright 2025 LiveKit, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "livekit/android.h"
18
19#include <atomic>
20#include <jni.h>
21#include <stdio.h>
22#include <unistd.h>
23#include <memory>
24
25#include "api/video_codecs/video_decoder_factory.h"
26#include "rtc_base/logging.h"
27#include "sdk/android/native_api/base/init.h"
28#include "sdk/android/native_api/codecs/wrapper.h"
29#include "sdk/android/native_api/jni/class_loader.h"
30#include "sdk/android/native_api/jni/scoped_java_ref.h"
31#include "sdk/android/src/jni/jni_helpers.h"
32
33// When compiling the examples app on Android, the linker complains that
34// `stdout` and `stderr` symbols cannot be found. The previous workaround
35// referenced `__sF`, but that symbol was removed in NDK 28+.
36// Use POSIX `fdopen()` with the standard file descriptors instead — works
37// across all NDK versions.
38#undef stdout
39FILE *stdout = fdopen(STDOUT_FILENO, "w");
40
41#undef stderr
42FILE *stderr = fdopen(STDERR_FILENO, "w");
43
44namespace livekit_ffi {
45
46// Track whether Android WebRTC has been initialized to prevent crashes on double-init.
47static std::atomic<bool> g_android_initialized{false};
48
49void init_android(JavaVM* jvm) {
50 // Idempotent - safe to call multiple times
51 if (g_android_initialized.exchange(true)) {
52 RTC_LOG(LS_INFO) << "livekit_ffi::init_android() - already initialized, skipping";
53 return;
54 }
55
56 RTC_LOG(LS_INFO) << "livekit_ffi::init_android() called with jvm=" << (jvm ? "valid" : "null");
57 if (!jvm) {
58 RTC_LOG(LS_ERROR) << "livekit_ffi::init_android() - JavaVM is null! Cannot initialize Android WebRTC.";
59 g_android_initialized.store(false);
60 return;
61 }
62 webrtc::InitAndroid(jvm);
63 RTC_LOG(LS_INFO) << "livekit_ffi::init_android() - webrtc::InitAndroid() completed";
64}
65
66bool init_android_context(JavaVM* jvm, uintptr_t context_ptr) {
67 RTC_LOG(LS_INFO) << "livekit_ffi::init_android_context() called";
68
69 if (!jvm || !context_ptr) {
70 RTC_LOG(LS_ERROR) << "livekit_ffi::init_android_context() - jvm or context is null";
71 return false;
72 }
73
74 // Initialize JVM first (idempotent - WebRTC handles double-init internally)
75 init_android(jvm);
76
77 // Cast uintptr_t back to jobject
78 jobject context = reinterpret_cast<jobject>(context_ptr);
79
80 JNIEnv* env = webrtc::AttachCurrentThreadIfNeeded();
81 if (!env) {
82 RTC_LOG(LS_ERROR) << "livekit_ffi::init_android_context() - Failed to attach to JNI";
83 return false;
84 }
85
86 // Find livekit.org.webrtc.ContextUtils class
87 jclass context_utils_class = env->FindClass("livekit/org/webrtc/ContextUtils");
88 if (!context_utils_class) {
89 RTC_LOG(LS_ERROR) << "livekit_ffi::init_android_context() - Failed to find ContextUtils class";
90 env->ExceptionClear();
91 return false;
92 }
93
94 // Get the initialize method
95 jmethodID initialize_method = env->GetStaticMethodID(
96 context_utils_class, "initialize", "(Landroid/content/Context;)V");
97 if (!initialize_method) {
98 RTC_LOG(LS_ERROR) << "livekit_ffi::init_android_context() - Failed to find initialize method";
99 env->ExceptionClear();
100 env->DeleteLocalRef(context_utils_class);
101 return false;
102 }
103
104 // Call ContextUtils.initialize(context)
105 env->CallStaticVoidMethod(context_utils_class, initialize_method, context);
106
107 // Check for exceptions
108 if (env->ExceptionCheck()) {
109 RTC_LOG(LS_ERROR) << "livekit_ffi::init_android_context() - Exception during initialize";
110 env->ExceptionDescribe();
111 env->ExceptionClear();
112 env->DeleteLocalRef(context_utils_class);
113 return false;
114 }
115
116 env->DeleteLocalRef(context_utils_class);
117 RTC_LOG(LS_INFO) << "livekit_ffi::init_android_context() - ContextUtils initialized successfully";
118 return true;
119}
120
121std::unique_ptr<webrtc::VideoEncoderFactory>
122CreateAndroidVideoEncoderFactory() {
123 JNIEnv* env = webrtc::AttachCurrentThreadIfNeeded();
124 webrtc::ScopedJavaLocalRef<jclass> factory_class =
125 webrtc::GetClass(env, "livekit/org/webrtc/DefaultVideoEncoderFactory");
126
127 jmethodID ctor = env->GetMethodID(factory_class.obj(), "<init>",
128 "(Llivekit/org/webrtc/EglBase$Context;ZZ)V");
129
130 jobject encoder_factory =
131 env->NewObject(factory_class.obj(), ctor, nullptr, true, false);
132
133 return webrtc::JavaToNativeVideoEncoderFactory(env, encoder_factory);
134}
135
136std::unique_ptr<webrtc::VideoDecoderFactory>
137CreateAndroidVideoDecoderFactory() {
138 JNIEnv* env = webrtc::AttachCurrentThreadIfNeeded();
139
140 webrtc::ScopedJavaLocalRef<jclass> factory_class =
141 webrtc::GetClass(env, "livekit/org/webrtc/WrappedVideoDecoderFactory");
142
143 jmethodID ctor = env->GetMethodID(factory_class.obj(), "<init>",
144 "(Llivekit/org/webrtc/EglBase$Context;)V");
145
146 jobject decoder_factory = env->NewObject(factory_class.obj(), ctor, nullptr);
147 return webrtc::JavaToNativeVideoDecoderFactory(env, decoder_factory);
148}
149
150} // namespace livekit_ffi