diff --git a/desktop/src-tauri/Cargo.toml b/desktop/src-tauri/Cargo.toml index 5757d4b..26bc58f 100644 --- a/desktop/src-tauri/Cargo.toml +++ b/desktop/src-tauri/Cargo.toml @@ -17,10 +17,6 @@ path = "src/main.rs" [build-dependencies] tauri-build = { version = "2", features = [] } -# cc is always available because build.rs runs on the HOST, not the TARGET. -# We gate the actual cc::Build::new() calls on `target.contains("android")` at -# build-script runtime. -cc = "1" [dependencies] tauri = { version = "2", features = [] } @@ -40,17 +36,11 @@ wzp-fec = { path = "../../crates/wzp-fec" } wzp-crypto = { path = "../../crates/wzp-crypto" } wzp-transport = { path = "../../crates/wzp-transport" } -# wzp-client on desktop: full audio stack (CPAL + macOS VoiceProcessingIO) +# wzp-client pulls CPAL + (on macOS) VoiceProcessingIO — desktop only. +# Android gets an oboe/AAudio backend in Step 3 of the Tauri mobile rewrite. [target.'cfg(not(target_os = "android"))'.dependencies] wzp-client = { path = "../../crates/wzp-client", features = ["audio", "vpio"] } -# wzp-client on Android: only the codec/handshake/call modules. Audio I/O is -# handled by our own oboe_audio module (C++ Oboe bridge). -[target.'cfg(target_os = "android")'.dependencies] -wzp-client = { path = "../../crates/wzp-client", default-features = false } -# On-device logcat bridge for the tracing crate — useful for native debugging. -tracing-android = "0.2" - # Platform-specific [target.'cfg(target_os = "macos")'.dependencies] coreaudio-rs = "0.11" diff --git a/desktop/src-tauri/build.rs b/desktop/src-tauri/build.rs index e17495c..16a154b 100644 --- a/desktop/src-tauri/build.rs +++ b/desktop/src-tauri/build.rs @@ -1,8 +1,9 @@ -use std::path::PathBuf; use std::process::Command; fn main() { - // ─── Embedded git hash ───────────────────────────────────────────────── + // Capture short git hash so the running app can prove which build it is. + // Falls back to "unknown" if git isn't available (e.g. when building from + // a tarball without a .git dir). let git_hash = Command::new("git") .args(["rev-parse", "--short", "HEAD"]) .output() @@ -13,138 +14,10 @@ fn main() { .unwrap_or_else(|| "unknown".into()); println!("cargo:rustc-env=WZP_GIT_HASH={git_hash}"); + // Re-run if the HEAD pointer or its target moves so the embedded hash + // tracks reality between builds. println!("cargo:rerun-if-changed=../../.git/HEAD"); println!("cargo:rerun-if-changed=../../.git/refs/heads"); - // ─── Oboe C++ bridge (Android only) ──────────────────────────────────── - let target = std::env::var("TARGET").unwrap_or_default(); - if target.contains("android") { - build_oboe_android(&target); - } - tauri_build::build() } - -fn build_oboe_android(target: &str) { - // Rerun if bridge sources change - println!("cargo:rerun-if-changed=cpp/oboe_bridge.cpp"); - println!("cargo:rerun-if-changed=cpp/oboe_bridge.h"); - println!("cargo:rerun-if-changed=cpp/oboe_stub.cpp"); - println!("cargo:rerun-if-changed=cpp/getauxval_fix.c"); - - // getauxval_fix: override broken static getauxval from compiler-rt that - // SIGSEGVs in shared libraries (runs before any app code). - cc::Build::new() - .file("cpp/getauxval_fix.c") - .compile("getauxval_fix"); - - let oboe_dir = fetch_oboe(); - match oboe_dir { - Some(oboe_path) => { - println!("cargo:warning=Building with Oboe from {:?}", oboe_path); - - let mut build = cc::Build::new(); - build - .cpp(true) - .std("c++17") - // Shared libc++ — static pulls broken libc stubs that crash - // in shared libraries (getauxval, __init_tcb, pthread_create). - .cpp_link_stdlib(Some("c++_shared")) - .include("cpp") - .include(oboe_path.join("include")) - .include(oboe_path.join("src")) - .define("WZP_HAS_OBOE", None) - .file("cpp/oboe_bridge.cpp"); - - add_cpp_files_recursive(&mut build, &oboe_path.join("src")); - build.compile("oboe_bridge"); - } - None => { - println!("cargo:warning=Oboe not found, building with stub"); - cc::Build::new() - .cpp(true) - .std("c++17") - .cpp_link_stdlib(Some("c++_shared")) - .file("cpp/oboe_stub.cpp") - .include("cpp") - .compile("oboe_bridge"); - } - } - - // libc++_shared.so must sit next to libwzp_desktop_lib.so in jniLibs — - // copy it there from the NDK sysroot. Tauri's gradle project expects the - // jniLibs in gen/android/app/src/main/jniLibs//. - if let Ok(ndk) = std::env::var("ANDROID_NDK_HOME") - .or_else(|_| std::env::var("NDK_HOME")) - { - let (triple, abi) = match target { - t if t.contains("aarch64") => ("aarch64-linux-android", "arm64-v8a"), - t if t.contains("armv7") => ("arm-linux-androideabi", "armeabi-v7a"), - t if t.contains("x86_64") => ("x86_64-linux-android", "x86_64"), - t if t.contains("i686") => ("i686-linux-android", "x86"), - _ => ("aarch64-linux-android", "arm64-v8a"), - }; - let lib_dir = format!( - "{ndk}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/{triple}" - ); - println!("cargo:rustc-link-search=native={lib_dir}"); - - let shared_so = format!("{lib_dir}/libc++_shared.so"); - if std::path::Path::new(&shared_so).exists() { - let manifest = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_default(); - // Tauri mobile layout: gen/android/app/src/main/jniLibs// - let jni_dir = format!("{manifest}/gen/android/app/src/main/jniLibs/{abi}"); - if std::fs::create_dir_all(&jni_dir).is_ok() { - let _ = std::fs::copy(&shared_so, format!("{jni_dir}/libc++_shared.so")); - println!("cargo:warning=Copied libc++_shared.so to {jni_dir}"); - } - } - } - - // Oboe requires Android log + OpenSLES backends - println!("cargo:rustc-link-lib=log"); - println!("cargo:rustc-link-lib=OpenSLES"); -} - -/// Recursively add all .cpp files from a directory to a cc::Build. -fn add_cpp_files_recursive(build: &mut cc::Build, dir: &std::path::Path) { - if !dir.is_dir() { - return; - } - for entry in std::fs::read_dir(dir).unwrap() { - let entry = entry.unwrap(); - let path = entry.path(); - if path.is_dir() { - add_cpp_files_recursive(build, &path); - } else if path.extension().map_or(false, |e| e == "cpp") { - build.file(&path); - } - } -} - -/// Try to find or fetch Oboe headers + source (v1.8.1). -fn fetch_oboe() -> Option { - let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap()); - let oboe_dir = out_dir.join("oboe"); - - if oboe_dir.join("include").join("oboe").join("Oboe.h").exists() { - return Some(oboe_dir); - } - - let status = Command::new("git") - .args([ - "clone", - "--depth=1", - "--branch=1.8.1", - "https://github.com/google/oboe.git", - oboe_dir.to_str().unwrap(), - ]) - .status(); - - match status { - Ok(s) if s.success() && oboe_dir.join("include").join("oboe").join("Oboe.h").exists() => { - Some(oboe_dir) - } - _ => None, - } -} diff --git a/desktop/src-tauri/cpp/getauxval_fix.c b/desktop/src-tauri/cpp/getauxval_fix.c deleted file mode 100644 index 2f287cb..0000000 --- a/desktop/src-tauri/cpp/getauxval_fix.c +++ /dev/null @@ -1,21 +0,0 @@ -// Override the broken static getauxval from compiler-rt/CRT. -// The static version reads from __libc_auxv which is NULL in shared libs -// loaded via dlopen, causing SIGSEGV in init_have_lse_atomics at load time. -// This version calls the real bionic getauxval via dlsym. -#ifdef __ANDROID__ -#include -#include - -typedef unsigned long (*getauxval_fn)(unsigned long); - -unsigned long getauxval(unsigned long type) { - static getauxval_fn real_getauxval = (getauxval_fn)0; - if (!real_getauxval) { - real_getauxval = (getauxval_fn)dlsym((void*)-1L /* RTLD_DEFAULT */, "getauxval"); - if (!real_getauxval) { - return 0; - } - } - return real_getauxval(type); -} -#endif diff --git a/desktop/src-tauri/cpp/oboe_bridge.cpp b/desktop/src-tauri/cpp/oboe_bridge.cpp deleted file mode 100644 index 066bb61..0000000 --- a/desktop/src-tauri/cpp/oboe_bridge.cpp +++ /dev/null @@ -1,278 +0,0 @@ -// Full Oboe implementation for Android -// This file is compiled only when targeting Android - -#include "oboe_bridge.h" - -#ifdef __ANDROID__ -#include -#include -#include -#include - -#define LOG_TAG "wzp-oboe" -#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) -#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__) -#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) - -// --------------------------------------------------------------------------- -// Ring buffer helpers (SPSC, lock-free) -// --------------------------------------------------------------------------- - -static inline int32_t ring_available_read(const wzp_atomic_int* write_idx, - const wzp_atomic_int* read_idx, - int32_t capacity) { - int32_t w = std::atomic_load_explicit(write_idx, std::memory_order_acquire); - int32_t r = std::atomic_load_explicit(read_idx, std::memory_order_relaxed); - int32_t avail = w - r; - if (avail < 0) avail += capacity; - return avail; -} - -static inline int32_t ring_available_write(const wzp_atomic_int* write_idx, - const wzp_atomic_int* read_idx, - int32_t capacity) { - return capacity - 1 - ring_available_read(write_idx, read_idx, capacity); -} - -static inline void ring_write(int16_t* buf, int32_t capacity, - wzp_atomic_int* write_idx, const wzp_atomic_int* read_idx, - const int16_t* src, int32_t count) { - int32_t w = std::atomic_load_explicit(write_idx, std::memory_order_relaxed); - for (int32_t i = 0; i < count; i++) { - buf[w] = src[i]; - w++; - if (w >= capacity) w = 0; - } - std::atomic_store_explicit(write_idx, w, std::memory_order_release); -} - -static inline void ring_read(int16_t* buf, int32_t capacity, - const wzp_atomic_int* write_idx, wzp_atomic_int* read_idx, - int16_t* dst, int32_t count) { - int32_t r = std::atomic_load_explicit(read_idx, std::memory_order_relaxed); - for (int32_t i = 0; i < count; i++) { - dst[i] = buf[r]; - r++; - if (r >= capacity) r = 0; - } - std::atomic_store_explicit(read_idx, r, std::memory_order_release); -} - -// --------------------------------------------------------------------------- -// Global state -// --------------------------------------------------------------------------- - -static std::shared_ptr g_capture_stream; -static std::shared_ptr g_playout_stream; -static const WzpOboeRings* g_rings = nullptr; -static std::atomic g_running{false}; -static std::atomic g_capture_latency_ms{0.0f}; -static std::atomic g_playout_latency_ms{0.0f}; - -// --------------------------------------------------------------------------- -// Capture callback -// --------------------------------------------------------------------------- - -class CaptureCallback : public oboe::AudioStreamDataCallback { -public: - oboe::DataCallbackResult onAudioReady( - oboe::AudioStream* stream, - void* audioData, - int32_t numFrames) override { - if (!g_running.load(std::memory_order_relaxed) || !g_rings) { - return oboe::DataCallbackResult::Stop; - } - - const int16_t* src = static_cast(audioData); - int32_t avail = ring_available_write(g_rings->capture_write_idx, - g_rings->capture_read_idx, - g_rings->capture_capacity); - int32_t to_write = (numFrames < avail) ? numFrames : avail; - if (to_write > 0) { - ring_write(g_rings->capture_buf, g_rings->capture_capacity, - g_rings->capture_write_idx, g_rings->capture_read_idx, - src, to_write); - } - - // Update latency estimate - auto result = stream->calculateLatencyMillis(); - if (result) { - g_capture_latency_ms.store(static_cast(result.value()), - std::memory_order_relaxed); - } - - return oboe::DataCallbackResult::Continue; - } -}; - -// --------------------------------------------------------------------------- -// Playout callback -// --------------------------------------------------------------------------- - -class PlayoutCallback : public oboe::AudioStreamDataCallback { -public: - oboe::DataCallbackResult onAudioReady( - oboe::AudioStream* stream, - void* audioData, - int32_t numFrames) override { - if (!g_running.load(std::memory_order_relaxed) || !g_rings) { - memset(audioData, 0, numFrames * sizeof(int16_t)); - return oboe::DataCallbackResult::Stop; - } - - int16_t* dst = static_cast(audioData); - int32_t avail = ring_available_read(g_rings->playout_write_idx, - g_rings->playout_read_idx, - g_rings->playout_capacity); - int32_t to_read = (numFrames < avail) ? numFrames : avail; - - if (to_read > 0) { - ring_read(g_rings->playout_buf, g_rings->playout_capacity, - g_rings->playout_write_idx, g_rings->playout_read_idx, - dst, to_read); - } - // Fill remainder with silence on underrun - if (to_read < numFrames) { - memset(dst + to_read, 0, (numFrames - to_read) * sizeof(int16_t)); - } - - // Update latency estimate - auto result = stream->calculateLatencyMillis(); - if (result) { - g_playout_latency_ms.store(static_cast(result.value()), - std::memory_order_relaxed); - } - - return oboe::DataCallbackResult::Continue; - } -}; - -static CaptureCallback g_capture_cb; -static PlayoutCallback g_playout_cb; - -// --------------------------------------------------------------------------- -// Public C API -// --------------------------------------------------------------------------- - -int wzp_oboe_start(const WzpOboeConfig* config, const WzpOboeRings* rings) { - if (g_running.load(std::memory_order_relaxed)) { - LOGW("wzp_oboe_start: already running"); - return -1; - } - - g_rings = rings; - - // Build capture stream - oboe::AudioStreamBuilder captureBuilder; - captureBuilder.setDirection(oboe::Direction::Input) - ->setPerformanceMode(oboe::PerformanceMode::LowLatency) - ->setSharingMode(oboe::SharingMode::Exclusive) - ->setFormat(oboe::AudioFormat::I16) - ->setChannelCount(config->channel_count) - ->setSampleRate(config->sample_rate) - ->setFramesPerDataCallback(config->frames_per_burst) - ->setInputPreset(oboe::InputPreset::VoiceCommunication) - ->setDataCallback(&g_capture_cb); - - oboe::Result result = captureBuilder.openStream(g_capture_stream); - if (result != oboe::Result::OK) { - LOGE("Failed to open capture stream: %s", oboe::convertToText(result)); - return -2; - } - - // Build playout stream - oboe::AudioStreamBuilder playoutBuilder; - playoutBuilder.setDirection(oboe::Direction::Output) - ->setPerformanceMode(oboe::PerformanceMode::LowLatency) - ->setSharingMode(oboe::SharingMode::Exclusive) - ->setFormat(oboe::AudioFormat::I16) - ->setChannelCount(config->channel_count) - ->setSampleRate(config->sample_rate) - ->setFramesPerDataCallback(config->frames_per_burst) - ->setUsage(oboe::Usage::VoiceCommunication) - ->setDataCallback(&g_playout_cb); - - result = playoutBuilder.openStream(g_playout_stream); - if (result != oboe::Result::OK) { - LOGE("Failed to open playout stream: %s", oboe::convertToText(result)); - g_capture_stream->close(); - g_capture_stream.reset(); - return -3; - } - - g_running.store(true, std::memory_order_release); - - // Start both streams - result = g_capture_stream->requestStart(); - if (result != oboe::Result::OK) { - LOGE("Failed to start capture: %s", oboe::convertToText(result)); - g_running.store(false, std::memory_order_release); - g_capture_stream->close(); - g_playout_stream->close(); - g_capture_stream.reset(); - g_playout_stream.reset(); - return -4; - } - - result = g_playout_stream->requestStart(); - if (result != oboe::Result::OK) { - LOGE("Failed to start playout: %s", oboe::convertToText(result)); - g_running.store(false, std::memory_order_release); - g_capture_stream->requestStop(); - g_capture_stream->close(); - g_playout_stream->close(); - g_capture_stream.reset(); - g_playout_stream.reset(); - return -5; - } - - LOGI("Oboe started: sr=%d burst=%d ch=%d", - config->sample_rate, config->frames_per_burst, config->channel_count); - return 0; -} - -void wzp_oboe_stop(void) { - g_running.store(false, std::memory_order_release); - - if (g_capture_stream) { - g_capture_stream->requestStop(); - g_capture_stream->close(); - g_capture_stream.reset(); - } - if (g_playout_stream) { - g_playout_stream->requestStop(); - g_playout_stream->close(); - g_playout_stream.reset(); - } - - g_rings = nullptr; - LOGI("Oboe stopped"); -} - -float wzp_oboe_capture_latency_ms(void) { - return g_capture_latency_ms.load(std::memory_order_relaxed); -} - -float wzp_oboe_playout_latency_ms(void) { - return g_playout_latency_ms.load(std::memory_order_relaxed); -} - -int wzp_oboe_is_running(void) { - return g_running.load(std::memory_order_relaxed) ? 1 : 0; -} - -#else -// Non-Android fallback — should not be reached; oboe_stub.cpp is used instead. -// Provide empty implementations just in case. - -int wzp_oboe_start(const WzpOboeConfig* config, const WzpOboeRings* rings) { - (void)config; (void)rings; - return -99; -} - -void wzp_oboe_stop(void) {} -float wzp_oboe_capture_latency_ms(void) { return 0.0f; } -float wzp_oboe_playout_latency_ms(void) { return 0.0f; } -int wzp_oboe_is_running(void) { return 0; } - -#endif // __ANDROID__ diff --git a/desktop/src-tauri/cpp/oboe_bridge.h b/desktop/src-tauri/cpp/oboe_bridge.h deleted file mode 100644 index 8c2f143..0000000 --- a/desktop/src-tauri/cpp/oboe_bridge.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef WZP_OBOE_BRIDGE_H -#define WZP_OBOE_BRIDGE_H - -#include - -#ifdef __cplusplus -#include -typedef std::atomic wzp_atomic_int; -extern "C" { -#else -#include -typedef atomic_int wzp_atomic_int; -#endif - -typedef struct { - int32_t sample_rate; - int32_t frames_per_burst; - int32_t channel_count; -} WzpOboeConfig; - -typedef struct { - int16_t* capture_buf; - int32_t capture_capacity; - wzp_atomic_int* capture_write_idx; - wzp_atomic_int* capture_read_idx; - - int16_t* playout_buf; - int32_t playout_capacity; - wzp_atomic_int* playout_write_idx; - wzp_atomic_int* playout_read_idx; -} WzpOboeRings; - -int wzp_oboe_start(const WzpOboeConfig* config, const WzpOboeRings* rings); -void wzp_oboe_stop(void); -float wzp_oboe_capture_latency_ms(void); -float wzp_oboe_playout_latency_ms(void); -int wzp_oboe_is_running(void); - -#ifdef __cplusplus -} -#endif - -#endif // WZP_OBOE_BRIDGE_H diff --git a/desktop/src-tauri/cpp/oboe_stub.cpp b/desktop/src-tauri/cpp/oboe_stub.cpp deleted file mode 100644 index 6792259..0000000 --- a/desktop/src-tauri/cpp/oboe_stub.cpp +++ /dev/null @@ -1,27 +0,0 @@ -// Stub implementation for non-Android host builds (testing, cargo check, etc.) - -#include "oboe_bridge.h" -#include - -int wzp_oboe_start(const WzpOboeConfig* config, const WzpOboeRings* rings) { - (void)config; - (void)rings; - fprintf(stderr, "wzp_oboe_start: stub (not on Android)\n"); - return 0; -} - -void wzp_oboe_stop(void) { - fprintf(stderr, "wzp_oboe_stop: stub (not on Android)\n"); -} - -float wzp_oboe_capture_latency_ms(void) { - return 0.0f; -} - -float wzp_oboe_playout_latency_ms(void) { - return 0.0f; -} - -int wzp_oboe_is_running(void) { - return 0; -} diff --git a/desktop/src-tauri/gen/schemas/capabilities.json b/desktop/src-tauri/gen/schemas/capabilities.json index 9e26dfe..4080621 100644 --- a/desktop/src-tauri/gen/schemas/capabilities.json +++ b/desktop/src-tauri/gen/schemas/capabilities.json @@ -1 +1 @@ -{} \ No newline at end of file +{"default":{"identifier":"default","description":"Default capability — grants core APIs (events, path, window, app, clipboard) to the main window on every platform we ship to.","local":true,"windows":["main"],"permissions":["core:default","core:event:default","core:event:allow-listen","core:event:allow-unlisten","core:event:allow-emit","core:event:allow-emit-to","core:path:default","core:window:default","core:app:default","core:webview:default","shell:default"],"platforms":["linux","macOS","windows","android","iOS"]}} \ No newline at end of file diff --git a/desktop/src-tauri/src/engine.rs b/desktop/src-tauri/src/engine.rs index 5d1ca39..4247238 100644 --- a/desktop/src-tauri/src/engine.rs +++ b/desktop/src-tauri/src/engine.rs @@ -1,11 +1,5 @@ -//! Call engine for the Tauri app — desktop and Android share this exact file. -//! -//! Desktop (macOS/Linux/Windows) uses `wzp_client::audio_io::{AudioCapture, -//! AudioPlayback}` on top of CPAL (and VoiceProcessingIO on macOS for OS-level -//! AEC). Android uses `crate::oboe_audio::OboeHandle` which wraps the C++ Oboe -//! bridge on high-priority AAudio callback threads. Both expose the same -//! `Arc` shape (`available()` / `read()` / `write()`) so the -//! send/recv tasks below are identical across platforms. +//! Call engine for the desktop app — wraps wzp-client audio + transport +//! into a clean async interface for Tauri commands. use std::net::SocketAddr; use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering}; @@ -15,20 +9,10 @@ use std::time::Instant; use tokio::sync::Mutex; use tracing::{error, info}; +use wzp_client::audio_io::{AudioCapture, AudioPlayback}; use wzp_client::call::{CallConfig, CallEncoder}; use wzp_proto::{CodecId, MediaTransport, QualityProfile}; -// Ring buffer type used by the send/recv tasks. Different concrete type on -// each platform but both provide the same method surface: `available()`, -// `read(&mut [i16]) -> usize`, `write(&[i16]) -> usize`. -#[cfg(not(target_os = "android"))] -use wzp_client::audio_ring::AudioRing; -#[cfg(not(target_os = "android"))] -use wzp_client::audio_io::{AudioCapture, AudioPlayback}; - -#[cfg(target_os = "android")] -use crate::oboe_audio::{AudioRing, OboeHandle}; - const FRAME_SAMPLES_40MS: usize = 1920; /// Resolve a quality string from the UI to a QualityProfile. @@ -111,11 +95,32 @@ impl CallEngine { let relay_addr: SocketAddr = relay.parse()?; - // Load or generate identity. Uses the same helper as the signaling - // commands so the desktop and the Android app both read from the - // platform-correct data dir (resolved via Tauri's path().app_data_dir()). - let seed = crate::load_or_create_seed() - .map_err(|e| anyhow::anyhow!("identity: {e}"))?; + // Load or generate identity + let seed = { + let path = { + let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); + std::path::PathBuf::from(home).join(".wzp").join("identity") + }; + if path.exists() { + if let Ok(hex) = std::fs::read_to_string(&path) { + if let Ok(s) = wzp_crypto::Seed::from_hex(hex.trim()) { + s + } else { + wzp_crypto::Seed::generate() + } + } else { + wzp_crypto::Seed::generate() + } + } else { + let s = wzp_crypto::Seed::generate(); + if let Some(p) = path.parent() { + std::fs::create_dir_all(p).ok(); + } + let hex: String = s.0.iter().map(|b| format!("{b:02x}")).collect(); + std::fs::write(&path, hex).ok(); + s + } + }; let fp = seed.derive_identity().public_identity().fingerprint; let fingerprint = fp.to_string(); @@ -139,28 +144,12 @@ impl CallEngine { info!("connected to relay, handshake complete"); event_cb("connected", &format!("joined room {room}")); - // Audio I/O — platform-dispatched. - // macOS VoiceProcessingIO (OS AEC) with CPAL fallback - // Linux/Win plain CPAL - // Android Oboe (AAudio / OpenSLES) via the C++ bridge - // - // The `audio_handle` must outlive the send/recv tasks so audio streams - // keep running — stored in the CallEngine below. - let (capture_ring, playout_ring, audio_handle): ( - Arc, - Arc, - Box, - ) = { - #[cfg(target_os = "android")] - { - let _ = _os_aec; // AEC TBD on Android (step 6 polish) - let (cr, pr, handle) = OboeHandle::start()?; - info!("using Oboe backend (AAudio/OpenSLES)"); - (cr, pr, Box::new(handle)) - } - #[cfg(target_os = "macos")] - { - if _os_aec { + // Audio I/O — VPIO (OS AEC) on macOS, plain CPAL otherwise. + // The audio handle must be stored in CallEngine to keep streams alive. + let (capture_ring, playout_ring, audio_handle): (_, _, Box) = + if _os_aec { + #[cfg(target_os = "macos")] + { match wzp_client::audio_vpio::VpioAudio::start() { Ok(v) => { let cr = v.capture_ring().clone(); @@ -177,25 +166,23 @@ impl CallEngine { (cr, pr, Box::new((capture, playback))) } } - } else { + } + #[cfg(not(target_os = "macos"))] + { + info!("OS AEC not available on this platform, using CPAL"); let capture = AudioCapture::start()?; let playback = AudioPlayback::start()?; let cr = capture.ring().clone(); let pr = playback.ring().clone(); (cr, pr, Box::new((capture, playback))) } - } - #[cfg(all(not(target_os = "macos"), not(target_os = "android")))] - { - let _ = _os_aec; // OS AEC not available on Linux/Windows here - info!("OS AEC not available on this platform, using CPAL"); + } else { let capture = AudioCapture::start()?; let playback = AudioPlayback::start()?; let cr = capture.ring().clone(); let pr = playback.ring().clone(); (cr, pr, Box::new((capture, playback))) - } - }; + }; let running = Arc::new(AtomicBool::new(true)); let mic_muted = Arc::new(AtomicBool::new(false)); diff --git a/desktop/src-tauri/src/lib.rs b/desktop/src-tauri/src/lib.rs index ab2c9c7..71074cf 100644 --- a/desktop/src-tauri/src/lib.rs +++ b/desktop/src-tauri/src/lib.rs @@ -6,12 +6,12 @@ windows_subsystem = "windows" )] -// Call engine — compiled on every platform. Audio backend is cfg-switched -// inside engine.rs (CPAL/VPIO on desktop, Oboe on Android). +// CPAL-backed audio engine — desktop only. On Android we'll plug in an +// oboe/AAudio backend in a later step. +#[cfg(not(target_os = "android"))] mod engine; -#[cfg(target_os = "android")] -mod oboe_audio; +#[cfg(not(target_os = "android"))] use engine::CallEngine; use serde::Serialize; @@ -83,6 +83,7 @@ struct CallStatus { } struct AppState { + #[cfg(not(target_os = "android"))] engine: Mutex>, signal: Arc>, } @@ -151,7 +152,7 @@ async fn ping_relay(relay: String) -> Result { /// Falls back to `$HOME/.wzp` on the desktop side if the OnceLock hasn't been /// initialised yet (shouldn't happen in normal startup, but keeps the fn /// total). -pub(crate) fn identity_dir() -> PathBuf { +fn identity_dir() -> PathBuf { if let Some(dir) = APP_DATA_DIR.get() { return dir.clone(); } @@ -172,7 +173,7 @@ fn identity_path() -> std::path::PathBuf { } /// Load the persisted seed, or generate-and-persist a new one if missing. -pub(crate) fn load_or_create_seed() -> Result { +fn load_or_create_seed() -> Result { let path = identity_path(); if path.exists() { let hex = std::fs::read_to_string(&path).map_err(|e| format!("read identity: {e}"))?; @@ -220,6 +221,7 @@ fn get_app_info() -> Result { }) } +#[cfg(not(target_os = "android"))] #[tauri::command] async fn connect( state: tauri::State<'_, Arc>, @@ -255,6 +257,7 @@ async fn connect( } } +#[cfg(not(target_os = "android"))] #[tauri::command] async fn disconnect(state: tauri::State<'_, Arc>) -> Result { let mut engine_lock = state.engine.lock().await; @@ -266,6 +269,7 @@ async fn disconnect(state: tauri::State<'_, Arc>) -> Result>) -> Result { let engine_lock = state.engine.lock().await; @@ -276,6 +280,7 @@ async fn toggle_mic(state: tauri::State<'_, Arc>) -> Result>) -> Result { let engine_lock = state.engine.lock().await; @@ -286,6 +291,7 @@ async fn toggle_speaker(state: tauri::State<'_, Arc>) -> Result>) -> Result { let engine_lock = state.engine.lock().await; @@ -329,6 +335,62 @@ async fn get_status(state: tauri::State<'_, Arc>) -> Result>, + _app: tauri::AppHandle, + _relay: String, + _room: String, + _alias: String, + _os_aec: bool, + _quality: String, +) -> Result { + Err("audio backend not yet wired on Android (step 3)".into()) +} + +#[cfg(target_os = "android")] +#[tauri::command] +async fn disconnect(_state: tauri::State<'_, Arc>) -> Result { + Ok("not connected".into()) +} + +#[cfg(target_os = "android")] +#[tauri::command] +async fn toggle_mic(_state: tauri::State<'_, Arc>) -> Result { + Err("not connected".into()) +} + +#[cfg(target_os = "android")] +#[tauri::command] +async fn toggle_speaker(_state: tauri::State<'_, Arc>) -> Result { + Err("not connected".into()) +} + +#[cfg(target_os = "android")] +#[tauri::command] +async fn get_status(_state: tauri::State<'_, Arc>) -> Result { + Ok(CallStatus { + active: false, + mic_muted: false, + spk_muted: false, + participants: vec![], + encode_fps: 0, + recv_fps: 0, + audio_level: 0, + call_duration_secs: 0.0, + fingerprint: String::new(), + tx_codec: String::new(), + rx_codec: String::new(), + }) +} + // ─── Signaling commands — platform independent ─────────────────────────────── struct SignalState { @@ -446,6 +508,7 @@ pub fn run() { tracing_subscriber::fmt().init(); let state = Arc::new(AppState { + #[cfg(not(target_os = "android"))] engine: Mutex::new(None), signal: Arc::new(Mutex::new(SignalState { transport: None, fingerprint: String::new(), signal_status: "idle".into(), diff --git a/desktop/src-tauri/src/oboe_audio.rs b/desktop/src-tauri/src/oboe_audio.rs deleted file mode 100644 index 0dac16f..0000000 --- a/desktop/src-tauri/src/oboe_audio.rs +++ /dev/null @@ -1,220 +0,0 @@ -//! Android audio backend for WarzonePhone Tauri mobile. -//! -//! Wraps the C++ Oboe bridge (cpp/oboe_bridge.cpp) — Oboe runs on high-priority -//! AAudio/OpenSL callback threads and reads/writes two shared SPSC ring buffers. -//! The Rust side drains the capture ring into the Opus encoder and fills the -//! playout ring with decoded PCM — no locking, no allocations, no cross-thread -//! channels on the hot path. -//! -//! Mirror of the proven implementation from `crates/wzp-android/src/audio_android.rs` -//! but stripped of the JNI-centric bits — this crate is a pure Tauri mobile lib. - -#![cfg(target_os = "android")] - -use std::sync::atomic::{AtomicI32, Ordering}; -use std::sync::Arc; - -use tracing::info; - -/// 20 ms @ 48 kHz mono. -pub const FRAME_SAMPLES: usize = 960; - -/// 8 frames × 960 samples = 160 ms headroom at 48 kHz. -const RING_CAPACITY: usize = 7680; - -// ─── FFI to cpp/oboe_bridge.cpp ───────────────────────────────────────────── - -#[repr(C)] -#[allow(non_snake_case)] -struct WzpOboeConfig { - sample_rate: i32, - frames_per_burst: i32, - channel_count: i32, -} - -#[repr(C)] -#[allow(non_snake_case)] -struct WzpOboeRings { - capture_buf: *mut i16, - capture_capacity: i32, - capture_write_idx: *mut AtomicI32, - capture_read_idx: *mut AtomicI32, - playout_buf: *mut i16, - playout_capacity: i32, - playout_write_idx: *mut AtomicI32, - playout_read_idx: *mut AtomicI32, -} - -unsafe impl Send for WzpOboeRings {} -unsafe impl Sync for WzpOboeRings {} - -unsafe extern "C" { - fn wzp_oboe_start(config: *const WzpOboeConfig, rings: *const WzpOboeRings) -> i32; - fn wzp_oboe_stop(); - fn wzp_oboe_capture_latency_ms() -> f32; - fn wzp_oboe_playout_latency_ms() -> f32; - fn wzp_oboe_is_running() -> i32; -} - -// ─── Lock-free SPSC ring buffer (shared with C++ via AtomicI32) ──────────── - -/// Single-producer single-consumer ring buffer shared with the C++ Oboe callback. -/// -/// The exposed method names (`available`, `read`, `write`) mirror -/// `wzp_client::audio_ring::AudioRing` so the CallEngine code can treat both -/// backends interchangeably via a cfg-switched type alias. -pub struct AudioRing { - buf: Vec, - capacity: usize, - write_idx: AtomicI32, - read_idx: AtomicI32, -} - -// SAFETY: SPSC — producer owns write_idx, consumer owns read_idx, atomics -// provide acquire/release visibility between threads. -unsafe impl Send for AudioRing {} -unsafe impl Sync for AudioRing {} - -impl AudioRing { - pub fn new() -> Self { - Self { - buf: vec![0i16; RING_CAPACITY], - capacity: RING_CAPACITY, - write_idx: AtomicI32::new(0), - read_idx: AtomicI32::new(0), - } - } - - /// Samples currently available to read. - pub fn available(&self) -> usize { - let w = self.write_idx.load(Ordering::Acquire); - let r = self.read_idx.load(Ordering::Relaxed); - let avail = w - r; - if avail < 0 { - (avail + self.capacity as i32) as usize - } else { - avail as usize - } - } - - /// Samples that can still be written without overflowing. - pub fn available_write(&self) -> usize { - self.capacity.saturating_sub(1).saturating_sub(self.available()) - } - - /// Producer side: write `data.len()` samples if room permits, else as many - /// as fit. Returns the number of samples actually written. - pub fn write(&self, data: &[i16]) -> usize { - let avail = self.available_write(); - let count = data.len().min(avail); - if count == 0 { - return 0; - } - let mut w = self.write_idx.load(Ordering::Relaxed) as usize; - let cap = self.capacity; - let buf_ptr = self.buf.as_ptr() as *mut i16; - for sample in &data[..count] { - unsafe { *buf_ptr.add(w) = *sample; } - w += 1; - if w >= cap { w = 0; } - } - self.write_idx.store(w as i32, Ordering::Release); - count - } - - /// Consumer side: read up to `out.len()` samples. Returns count actually read. - pub fn read(&self, out: &mut [i16]) -> usize { - let avail = self.available(); - let count = out.len().min(avail); - if count == 0 { - return 0; - } - let mut r = self.read_idx.load(Ordering::Relaxed) as usize; - let cap = self.capacity; - let buf_ptr = self.buf.as_ptr(); - for slot in &mut out[..count] { - unsafe { *slot = *buf_ptr.add(r); } - r += 1; - if r >= cap { r = 0; } - } - self.read_idx.store(r as i32, Ordering::Release); - count - } - - fn buf_ptr(&self) -> *mut i16 { - self.buf.as_ptr() as *mut i16 - } - fn write_idx_ptr(&self) -> *mut AtomicI32 { - &self.write_idx as *const AtomicI32 as *mut AtomicI32 - } - fn read_idx_ptr(&self) -> *mut AtomicI32 { - &self.read_idx as *const AtomicI32 as *mut AtomicI32 - } -} - -// ─── Owning handle: starts/stops the Oboe streams and holds the rings alive ── - -/// Owns both ring buffers and the active Oboe streams. Dropping this handle -/// stops the streams; the rings cannot outlive it because they're owned inside. -pub struct OboeHandle { - capture: Arc, - playout: Arc, - started: bool, -} - -impl OboeHandle { - /// Allocate ring buffers and start the Oboe capture + playout streams. - /// Returns the handle plus cloned ring Arcs for the CallEngine's send/recv tasks. - pub fn start() -> Result<(Arc, Arc, Self), anyhow::Error> { - let capture = Arc::new(AudioRing::new()); - let playout = Arc::new(AudioRing::new()); - - let config = WzpOboeConfig { - sample_rate: 48_000, - frames_per_burst: FRAME_SAMPLES as i32, - channel_count: 1, - }; - let rings = WzpOboeRings { - capture_buf: capture.buf_ptr(), - capture_capacity: capture.capacity as i32, - capture_write_idx: capture.write_idx_ptr(), - capture_read_idx: capture.read_idx_ptr(), - playout_buf: playout.buf_ptr(), - playout_capacity: playout.capacity as i32, - playout_write_idx: playout.write_idx_ptr(), - playout_read_idx: playout.read_idx_ptr(), - }; - - let ret = unsafe { wzp_oboe_start(&config, &rings) }; - if ret != 0 { - return Err(anyhow::anyhow!("wzp_oboe_start failed: code {ret}")); - } - info!(capture_latency_ms = unsafe { wzp_oboe_capture_latency_ms() }, - playout_latency_ms = unsafe { wzp_oboe_playout_latency_ms() }, - "oboe backend started"); - - Ok(( - capture.clone(), - playout.clone(), - Self { capture, playout, started: true }, - )) - } - - #[allow(unused)] - pub fn is_running(&self) -> bool { - unsafe { wzp_oboe_is_running() != 0 } - } -} - -impl Drop for OboeHandle { - fn drop(&mut self) { - if self.started { - unsafe { wzp_oboe_stop() }; - info!("oboe backend stopped"); - self.started = false; - } - // Rings live as long as their Arcs do — the C++ side has already - // stopped, so no more callbacks will touch the atomics. - let _ = (&self.capture, &self.playout); - } -} diff --git a/scripts/Dockerfile.android-builder b/scripts/Dockerfile.android-builder index 53c61d3..b5ea21f 100644 --- a/scripts/Dockerfile.android-builder +++ b/scripts/Dockerfile.android-builder @@ -74,27 +74,6 @@ RUN yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --licenses > /dev/nu "platform-tools" \ 2>&1 | grep -v '^\[' > /dev/null -# Work around broken API-24 libc/libdl stubs in the NDK. Rust's pre-compiled -# libstd.rlib for aarch64-linux-android links against pthread_create / dlsym -# from the sysroot. With --target=...24, those resolve to the API-24 "stub" -# libc.a/libdl.a that return "libdl.a is a stub --- use libdl.so instead" at -# runtime. API-26 has real dynamic bindings. Tauri-cli hard-codes the -# android24-clang path so env var overrides don't stick. -# -# Permanent fix inside the image: replace every `${abi}24-clang` binary with -# a shim that exec()s the corresponding `${abi}26-clang`. Any build using -# this image now transparently gets the API-26 sysroot even when the caller -# asks for API-24. -RUN set -eux; \ - BIN=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin; \ - for abi in aarch64-linux-android armv7a-linux-androideabi i686-linux-android x86_64-linux-android; do \ - for suffix in clang clang++; do \ - mv "$BIN/${abi}24-${suffix}" "$BIN/${abi}24-${suffix}.orig"; \ - printf '#!/bin/sh\nexec "%s/%s26-%s" "$@"\n' "$BIN" "$abi" "$suffix" > "$BIN/${abi}24-${suffix}"; \ - chmod +x "$BIN/${abi}24-${suffix}"; \ - done; \ - done - # Make SDK world-readable so builder user can access it RUN chmod -R a+rX $ANDROID_HOME diff --git a/scripts/build-tauri-android.sh b/scripts/build-tauri-android.sh index f8db6a4..a54960f 100755 --- a/scripts/build-tauri-android.sh +++ b/scripts/build-tauri-android.sh @@ -36,7 +36,6 @@ REBUILD_RUST=0 DO_PULL=1 DO_INIT=0 BUILD_RELEASE=0 -DROP_SHELL=0 for arg in "$@"; do case "$arg" in --rust) REBUILD_RUST=1 ;; @@ -44,7 +43,6 @@ for arg in "$@"; do --no-pull) DO_PULL=0 ;; --init) DO_INIT=1 ;; --release) BUILD_RELEASE=1 ;; - --shell) DROP_SHELL=1 ;; # interactive debug shell inside container -h|--help) sed -n '3,30p' "$0" exit 0 @@ -52,34 +50,6 @@ for arg in "$@"; do esac done -# ── --shell: drop into an interactive container for fast manual iteration ── -# The container is NOT --rm'd so you can keep hacking between invocations, -# and has the same mounts / env as the build path above so `cargo tauri -# android build ...` just works. -if [ "$DROP_SHELL" = "1" ]; then - log "Starting interactive shell in wzp-android-builder container..." - log " cd /build/source/desktop/src-tauri && cargo tauri android build --debug --target aarch64 --apk" - log " (exit the shell with ^D; container will be removed)" - ssh -t -A $SSH_OPTS "$REMOTE_HOST" " - set -euo pipefail - BASE=$BASE_DIR - # Make sure the source/cache is writable by uid 1000 - sudo chown -R 1000:1000 \$BASE/data/source \$BASE/data/cache 2>/dev/null || true - docker run --rm -it \ - --user 1000:1000 \ - -v \$BASE/data/source:/build/source \ - -v \$BASE/data/cache/cargo-registry:/home/builder/.cargo/registry \ - -v \$BASE/data/cache/cargo-git:/home/builder/.cargo/git \ - -v \$BASE/data/cache/target:/build/source/target \ - -v \$BASE/data/cache/gradle:/home/builder/.gradle \ - -v \$BASE/data/cache/android-home:/home/builder/.android \ - -w /build/source/desktop/src-tauri \ - wzp-android-builder \ - bash - " - exit 0 -fi - log() { echo -e "\033[1;36m>>> $*\033[0m"; } ssh_cmd() { ssh -A $SSH_OPTS "$REMOTE_HOST" "$@"; } @@ -183,10 +153,6 @@ docker run --rm \ --user 1000:1000 \ -e DO_INIT="$DO_INIT" \ -e PROFILE_FLAG="$PROFILE_FLAG" \ - -e CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=/opt/android-sdk/ndk/26.1.10909125/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android26-clang \ - -e CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER=/opt/android-sdk/ndk/26.1.10909125/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi26-clang \ - -e CARGO_TARGET_X86_64_LINUX_ANDROID_LINKER=/opt/android-sdk/ndk/26.1.10909125/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android26-clang \ - -e CARGO_TARGET_I686_LINUX_ANDROID_LINKER=/opt/android-sdk/ndk/26.1.10909125/toolchains/llvm/prebuilt/linux-x86_64/bin/i686-linux-android26-clang \ -v "$BASE_DIR/data/source:/build/source" \ -v "$BASE_DIR/data/cache/cargo-registry:/home/builder/.cargo/registry" \ -v "$BASE_DIR/data/cache/cargo-git:/home/builder/.cargo/git" \ @@ -213,57 +179,6 @@ if [ "${DO_INIT}" = "1" ] || [ ! -x gen/android/gradlew ]; then cargo tauri android init 2>&1 | tail -20 fi -# ── Post-init patches ──────────────────────────────────────────────────────── - -# Bump minSdk 24 -> 26. Tauri scaffolds with minSdk=24, which forces cargo to -# use the aarch64-linux-android24-clang linker. That linker pulls a broken -# compiler-rt stub for __init_tcb / pthread_create that SIGSEGVs on first -# thread::spawn inside a .so (static libc init never runs in dlopen-loaded -# libraries). API 26 has working runtime symbols. Oboe also requires API 26+. -BUILD_GRADLE=gen/android/app/build.gradle.kts -if grep -q "minSdk = 24" "$BUILD_GRADLE"; then - echo ">>> bumping minSdk 24 -> 26 in build.gradle.kts" - sed -i "s|minSdk = 24|minSdk = 26|" "$BUILD_GRADLE" -fi - -MANIFEST=gen/android/app/src/main/AndroidManifest.xml -if ! grep -q "RECORD_AUDIO" "$MANIFEST"; then - echo ">>> injecting RECORD_AUDIO + MODIFY_AUDIO_SETTINGS into AndroidManifest" - sed -i "s||\n \n |" "$MANIFEST" -fi - -# Overwrite MainActivity to request the mic permission on launch. Idempotent — -# Tauri re-init would reset it, and we re-write it here on every build. -MAIN_ACTIVITY=gen/android/app/src/main/java/com/wzp/desktop/MainActivity.kt -cat > "$MAIN_ACTIVITY" <>> cargo tauri android build ${PROFILE_FLAG} --target aarch64 --apk" cargo tauri android build ${PROFILE_FLAG} --target aarch64 --apk