Skip to content

Commit

Permalink
WIP Android
Browse files Browse the repository at this point in the history
  • Loading branch information
padenot committed Nov 18, 2024
1 parent 2742ea5 commit efb1e7d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ libc = "0.2"
[target.'cfg(target_os = "linux")'.dependencies.dbus]
version = "0.6.4"
optional = true


[target.'cfg(target_os = "android")'.dependencies]
jni = "0.21"
libc = "0.2"
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ cfg_if! {
#[no_mangle]
/// Size of a RtPriorityThreadInfo or atp_thread_info struct, for use in FFI.
pub static ATP_THREAD_INFO_SIZE: usize = std::mem::size_of::<RtPriorityThreadInfo>();
} else if #[cfg(target_os = "android")] {
mod rt_android;
pub fn promote_current_thread_to_real_time_internal(_: u32, audio_samplerate_hz: u32) -> Result<RtPriorityHandle, AudioThreadPriorityError> {
panic!("Don't use this, pass a pointer to the JVM with to promote_current_thread_to_real_time_with_jvm instead");
}
use rt_android::promote_current_thread_to_real_time_with_jvm;
use rt_android::demote_current_thread_from_real_time_internal;
use rt_android::RtPriorityHandleInternal;
} else {
// blanket implementations for Android, Linux Desktop without dbus and others
pub struct RtPriorityHandleInternal {}
Expand Down
68 changes: 68 additions & 0 deletions src/rt_android.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
extern crate libc;
use crate::AudioThreadPriorityError;
use jni::objects::JClass;
use jni::sys::jint;
use jni::JNIEnv;
use log::info;

#[derive(Debug)]
pub struct RtPriorityHandleInternal {
previous_priority: libc::c_int,
}

pub fn promote_current_thread_to_real_time_with_jvm(
jvm: &jni::JavaVM,
) -> Result<RtPriorityHandleInternal, AudioThreadPriorityError> {
let mut env = match jvm.attach_current_thread() {
Ok(env) => env,
Err(e) => {
return Err(AudioThreadPriorityError {
message: "Couldn't attach to JVM".into(),
inner: Some(Box::new(e)),
});
}
};
let class = env
.find_class("java/sdk/Process")
.expect("Failed to load the target class");
let rv = env.call_static_method(&class, "getThreadPriority", "()I", &[]);
let previous_priority: jint = match rv {
Ok(p) => p.i().unwrap(),
Err(_) => {
return Err(AudioThreadPriorityError {
message: "Couldn't get previous thread priority".into(),
inner: None,
});
}
};

// From the SDK
let THREAD_PRIORITY_URGENT_AUDIO = -19 as jint;
match env.call_static_method(
class,
"setThreadPriority",
"(I)V",
&[THREAD_PRIORITY_URGENT_AUDIO.into()],
) {
Ok(v) => {
return Ok(RtPriorityHandleInternal {
previous_priority: previous_priority,
})
}
Err(_) => {
return Err(AudioThreadPriorityError {
message: "Couldn't get previous thread priority".into(),
inner: None,
})
}
}
}

pub fn demote_current_thread_from_real_time_internal(
rt_priority_handle: RtPriorityHandleInternal,
) -> Result<(), AudioThreadPriorityError> {
Err(AudioThreadPriorityError {
message: "Not implemented".into(),
inner: None,
})
}

0 comments on commit efb1e7d

Please sign in to comment.