Skip to content

Commit

Permalink
linux android use cpal (rustdesk#9914)
Browse files Browse the repository at this point in the history
Signed-off-by: 21pages <[email protected]>
  • Loading branch information
21pages authored Nov 14, 2024
1 parent 9e4cc91 commit 06c7bc1
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 331 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ fon = "0.6"
zip = "0.6"
shutdown_hooks = "0.1"
totp-rs = { version = "5.4", default-features = false, features = ["gen_secret", "otpauth"] }

[target.'cfg(not(any(target_os = "android", target_os = "linux")))'.dependencies]
cpal = "0.15"
ringbuf = "0.3"

Expand Down
3 changes: 1 addition & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(windows)]
fn build_windows() {
let file = "src/platform/windows.cc";
let file2 = "src/platform/windows_delete_test_cert.cc";
let file2 = "src/platform/windows_delete_test_cert.cc";
cc::Build::new().file(file).file(file2).compile("windows");
println!("cargo:rustc-link-lib=WtsApi32");
println!("cargo:rerun-if-changed={}", file);
Expand Down Expand Up @@ -72,7 +72,6 @@ fn install_android_deps() {
);
println!("cargo:rustc-link-lib=ndk_compat");
println!("cargo:rustc-link-lib=oboe");
println!("cargo:rustc-link-lib=oboe_wrapper");
println!("cargo:rustc-link-lib=c++");
println!("cargo:rustc-link-lib=OpenSLES");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import com.hjq.permissions.XXPermissions
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import kotlin.concurrent.thread


Expand Down Expand Up @@ -57,6 +61,7 @@ class MainActivity : FlutterActivity() {
channelTag
)
initFlutterChannel(flutterMethodChannel!!)
flutterEngine.plugins.add(ContextPlugin())
thread { setCodecInfo() }
}

Expand Down Expand Up @@ -389,3 +394,16 @@ class MainActivity : FlutterActivity() {
stopService(Intent(this, FloatingWindowService::class.java))
}
}

// https://cjycode.com/flutter_rust_bridge/guides/how-to/ndk-init
class ContextPlugin : FlutterPlugin, MethodCallHandler {
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
FFI.initContext(flutterPluginBinding.applicationContext)
}
override fun onMethodCall(call: MethodCall, result: Result) {
result.notImplemented()
}

override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
}
}
1 change: 1 addition & 0 deletions flutter/android/app/src/main/kotlin/ffi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ object FFI {
}

external fun init(ctx: Context)
external fun initContext(ctx: Context)
external fun startServer(app_dir: String, custom_client_config: String)
external fun startService()
external fun onVideoFrameUpdate(buf: ByteBuffer)
Expand Down
58 changes: 39 additions & 19 deletions libs/scrap/src/android/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use jni::errors::{Error as JniError, Result as JniResult};
use lazy_static::lazy_static;
use serde::Deserialize;
use std::ops::Not;
use std::os::raw::c_void;
use std::sync::atomic::{AtomicPtr, Ordering::SeqCst};
use std::sync::{Mutex, RwLock};
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -155,10 +156,24 @@ pub extern "system" fn Java_ffi_FFI_setFrameRawEnable(
pub extern "system" fn Java_ffi_FFI_init(env: JNIEnv, _class: JClass, ctx: JObject) {
log::debug!("MainService init from java");
if let Ok(jvm) = env.get_java_vm() {
let java_vm = jvm.get_java_vm_pointer() as *mut c_void;
*JVM.write().unwrap() = Some(jvm);
if let Ok(context) = env.new_global_ref(ctx) {
let context_jobject = context.as_obj().as_raw() as *mut c_void;
*MAIN_SERVICE_CTX.write().unwrap() = Some(context);
init_ndk_context().ok();
init_ndk_context(java_vm, context_jobject);
}
}
}

#[no_mangle]
pub extern "system" fn Java_ffi_FFI_initContext(env: JNIEnv, _class: JClass, ctx: JObject) {
log::debug!("MainActivity initContext from java");
if let Ok(jvm) = env.get_java_vm() {
if let Ok(context) = env.new_global_ref(ctx) {
let java_vm = jvm.get_java_vm_pointer() as *mut c_void;
let context_jobject = context.as_obj().as_raw() as *mut c_void;
init_ndk_context(java_vm, context_jobject);
}
}
}
Expand Down Expand Up @@ -332,30 +347,35 @@ pub fn call_main_service_set_by_name(
}
}

fn init_ndk_context() -> JniResult<()> {
// Difference between MainService, MainActivity, JNI_OnLoad:
// jvm is the same, ctx is differen and ctx of JNI_OnLoad is null.
// cpal: all three works
// Service(GetByName, ...): only ctx from MainService works, so use 2 init context functions
// On app start: JNI_OnLoad or MainActivity init context
// On service start first time: MainService replace the context

fn init_ndk_context(java_vm: *mut c_void, context_jobject: *mut c_void) {
let mut lock = NDK_CONTEXT_INITED.lock().unwrap();
if *lock {
unsafe {
ndk_context::release_android_context();
}
*lock = false;
}
if let (Some(jvm), Some(ctx)) = (
JVM.read().unwrap().as_ref(),
MAIN_SERVICE_CTX.read().unwrap().as_ref(),
) {
unsafe {
ndk_context::initialize_android_context(
jvm.get_java_vm_pointer() as _,
ctx.as_obj().as_raw() as _,
);
#[cfg(feature = "hwcodec")]
hwcodec::android::ffmpeg_set_java_vm(
jvm.get_java_vm_pointer() as _,
);
}
*lock = true;
return Ok(());
unsafe {
ndk_context::initialize_android_context(java_vm, context_jobject);
#[cfg(feature = "hwcodec")]
hwcodec::android::ffmpeg_set_java_vm(java_vm);
}
Err(JniError::ThrowFailed(-1))
*lock = true;
}

// // https://cjycode.com/flutter_rust_bridge/guides/how-to/ndk-init
// #[no_mangle]
// pub extern "C" fn JNI_OnLoad(vm: jni::JavaVM, res: *mut std::os::raw::c_void) -> jni::sys::jint {
// if let Ok(env) = vm.get_env() {
// let vm = vm.get_java_vm_pointer() as *mut std::os::raw::c_void;
// init_ndk_context(vm, res);
// }
// jni::JNIVersion::V6.into()
// }
15 changes: 0 additions & 15 deletions res/vcpkg/oboe-wrapper/CMakeLists.txt

This file was deleted.

118 changes: 0 additions & 118 deletions res/vcpkg/oboe-wrapper/oboe.cc

This file was deleted.

8 changes: 0 additions & 8 deletions res/vcpkg/oboe-wrapper/portfile.cmake

This file was deleted.

19 changes: 0 additions & 19 deletions res/vcpkg/oboe-wrapper/vcpkg.json

This file was deleted.

Loading

0 comments on commit 06c7bc1

Please sign in to comment.