The Android twin of `pf-client-core`'s pad_audio: drain the host's per-pad DualSense streams, Opus-decode haptics (kind 0) and speaker (kind 1), interleave into the pad's own 4-channel layout, and render on the pad itself. Every other client hands that stream to the platform's audio graph. Android cannot: AOSP's UsbAlsaManager denylists the DualSense's output by VID/PID, so the kernel enumerates the pad's playback node and the framework discards it — `hasOutput: false`, nothing for setPreferredDevice to target, /dev/snd closed by SELinux, and UsbRequest rejects non-bulk/interrupt endpoints. So this drives the pad's isochronous endpoint directly via uac-host on the descriptor Java owns. That is measured, not assumed. On a Nothing Phone (3): the claim succeeds unprivileged, the gamepad and the pad's microphone both keep working, and the underrun-free floor is 4 ms — holding under eight-core load with the SoC in severe thermal throttling. The renderer runs at 6 ms, one step of headroom, because the same measurement found transient events that are not depth-dependent. Structured to the crate's own convention: the mixer and PLC are ungated so they compile and unit-test in the host workspace (8 tests), while everything touching an Android-only dependency is cfg'd to android. Two details worth review: - The kinds arrive on different cadences (5 ms vs 10 ms), so each has its own write cursor and both shift together on overflow — a haptics-only session renders with a silent speaker pair instead of stalling on a kind that will never arrive, and the two can never skew. - An unrecognised kind is dropped rather than folded into the coil pair. A `min(1)` clamp would have rendered a future kind straight into the actuators. Lifecycle mirrors MicCapture: dropping the handle joins the thread, and nativeStopPadAudio returns only once it has, so Kotlin may close the UsbDeviceConnection as soon as it returns and not before. usbfs-iso/uac-host enter as git dependencies pinned by revision — a transport under a real-time deadline should move when we choose. They become version dependencies once published to crates.io.
96 lines
4.4 KiB
Rust
96 lines
4.4 KiB
Rust
//! punktfunk Android client — the JNI bridge ("nativecore") over `punktfunk-core`.
|
|
//!
|
|
//! Architecture: the **Rust-heavy** client model (like `punktfunk-client-linux`, *not* the
|
|
//! thin-native-over-C-ABI Apple model). This `cdylib` links `punktfunk-core` directly and drives
|
|
//! the whole `punktfunk/1` protocol through [`punktfunk_core::client::NativeClient`]; Kotlin owns
|
|
//! only the Android-framework surface (Compose UI, `SurfaceView` lifecycle, input capture, the
|
|
//! Wi-Fi `MulticastLock` + permission UX, Keystore). The JNI seam below is the one place the two
|
|
//! languages meet.
|
|
//!
|
|
//! Why Rust-heavy: Kotlin cannot `import` the cbindgen C header the way Swift can, so a native
|
|
//! bridge is unavoidable. Writing it in Rust lets the Android client reuse the Linux client's
|
|
//! orchestration verbatim — audio jitter ring, the VK keymap inverse, latency/skew math, the
|
|
//! input capture state machine, trust/pairing logic, **mDNS discovery** ([`discovery`], the same
|
|
//! `mdns-sd` browse the Linux/Windows clients use) — instead of re-porting it into Kotlin. Kotlin
|
|
//! keeps only the Android-framework surface it must (Compose UI, `SurfaceView`, input capture, the
|
|
//! Wi-Fi `MulticastLock` + permission UX, Keystore identity).
|
|
//!
|
|
//! JNI symbols map to `io.unom.punktfunk.kit.NativeBridge` in the `:kit` Gradle module
|
|
//! (`clients/android`). The surface: the native-link proof (`abiVersion`/`coreVersion`), mDNS host
|
|
//! discovery ([`discovery`]), and the session lifecycle in [`session`] — connect/pair + the trust
|
|
//! surface, the per-plane pumps (video → AMediaCodec, audio ↔ AAudio, mic uplink), input, and
|
|
//! rumble/HID feedback ([`feedback`]). Mode renegotiation is still TODO (see [`session`]).
|
|
|
|
use jni::objects::JObject;
|
|
use jni::sys::jint;
|
|
use jni::JNIEnv;
|
|
|
|
#[cfg(target_os = "android")]
|
|
mod adpf;
|
|
#[cfg(target_os = "android")]
|
|
mod audio;
|
|
#[cfg(target_os = "android")]
|
|
mod decode;
|
|
// Ungated: pure `mdns-sd` + `jni`, so the browse + its JNI seam link into the host workspace build
|
|
// (and its unit test runs there) exactly like `session`/`stats`. Kotlin only ever calls it on device.
|
|
mod discovery;
|
|
mod feedback;
|
|
#[cfg(target_os = "android")]
|
|
mod mic;
|
|
/// Tier-A DualSense pad audio: the 0xD1 plane rendered on the pad's own USB endpoint.
|
|
mod pad_audio;
|
|
mod session;
|
|
mod stats;
|
|
// Ungated like `discovery`: pure `jni` + `punktfunk_core::wol` (no Android framework), so it links
|
|
// into the host workspace build too. Kotlin only ever calls it on device.
|
|
mod wol;
|
|
// Ungated like `wol`: pure `jni` + `punktfunk_core::client` (the reachability probe). Kotlin calls
|
|
// it off the main thread to light saved-host "online" pips independently of mDNS.
|
|
mod probe;
|
|
|
|
/// Initialize `android_logger` once when the JVM loads the library. Logs land in logcat under the
|
|
/// `punktfunk` tag. Core `tracing` events (transport warnings: socket-buffer clamp, QoS failures)
|
|
/// arrive here too: tracing's "log" feature — declared explicitly in Cargo.toml rather than relied
|
|
/// on via quinn's defaults — forwards them as `log` records since no tracing subscriber is ever
|
|
/// installed. Android-only — there is no JVM (and no logcat) on the host build.
|
|
#[cfg(target_os = "android")]
|
|
#[no_mangle]
|
|
pub extern "system" fn JNI_OnLoad(
|
|
_vm: *mut jni::sys::JavaVM,
|
|
_reserved: *mut std::ffi::c_void,
|
|
) -> jint {
|
|
android_logger::init_once(
|
|
android_logger::Config::default()
|
|
.with_max_level(log::LevelFilter::Info)
|
|
.with_tag("punktfunk"),
|
|
);
|
|
log::info!(
|
|
"punktfunk_android loaded (core ABI v{})",
|
|
punktfunk_core::ABI_VERSION
|
|
);
|
|
jni::sys::JNI_VERSION_1_6
|
|
}
|
|
|
|
/// `NativeBridge.abiVersion(): Int` — the core's C-ABI version. A non-error return is the
|
|
/// scaffold's proof that `System.loadLibrary` found the `.so`, the JNI symbol resolved, and the
|
|
/// linked `punktfunk-core` is the one we expect.
|
|
#[no_mangle]
|
|
pub extern "system" fn Java_io_unom_punktfunk_kit_NativeBridge_abiVersion(
|
|
_env: JNIEnv,
|
|
_this: JObject,
|
|
) -> jint {
|
|
punktfunk_core::ABI_VERSION as jint
|
|
}
|
|
|
|
/// `NativeBridge.coreVersion(): String` — the crate version, proving JNI string marshaling works.
|
|
#[no_mangle]
|
|
pub extern "system" fn Java_io_unom_punktfunk_kit_NativeBridge_coreVersion<'local>(
|
|
env: JNIEnv<'local>,
|
|
_this: JObject<'local>,
|
|
) -> jni::sys::jstring {
|
|
match env.new_string(env!("CARGO_PKG_VERSION")) {
|
|
Ok(s) => s.into_raw(),
|
|
Err(_) => JObject::null().into_raw(),
|
|
}
|
|
}
|