From 63861bec2684f4c486a24b1cabb0a9b1b20bd133 Mon Sep 17 00:00:00 2001 From: bit9992 <84845600+bit9992@users.noreply.github.com> Date: Sat, 14 Nov 2026 18:33:17 +0300 Subject: [PATCH] Honor manual Android endpoints and apply version-safe voice output attributes --- .gitea/workflows/android.yml | 3 + CHANGELOG.md | 7 +- Cargo.lock | 2 - Cargo.toml | 3 +- LICENSES/NOTICE.md | 11 + docs/mobile-port-progress.md | 41 + native/src/app/runtime/lifecycle_tests.rs | 29 + native/src/app/runtime/mobile_calls.rs | 4 +- .../src/app/runtime/settings_actions/audio.rs | 32 +- native/src/media/audio/devices.rs | 25 +- native/src/media/audio/devices_tests.rs | 4 + native/src/media/audio/engine.rs | 65 +- native/src/media/audio/engine_tests.rs | 164 ++ native/src/platform/android.rs | 71 +- native/src/platform/android/call_audio.rs | 1 + .../src/host/aaudio/device_catalog.rs | 47 +- .../aaudio/java_interface/audio_manager.rs | 43 +- .../aaudio/java_interface/devices_info.rs | 13 + .../vendor/cpal-0.18.2/src/host/aaudio/mod.rs | 152 +- .../src/host/aaudio/output_usage.rs | 211 +++ native/vendor/ndk-0.9.0/.cargo_vcs_info.json | 6 + native/vendor/ndk-0.9.0/.gitignore | 3 + native/vendor/ndk-0.9.0/CHANGELOG.md | 137 ++ native/vendor/ndk-0.9.0/Cargo.toml | 125 ++ native/vendor/ndk-0.9.0/Cargo.toml.orig | 70 + native/vendor/ndk-0.9.0/LICENSE-MIT | 19 + native/vendor/ndk-0.9.0/README.md | 21 + native/vendor/ndk-0.9.0/src/asset.rs | 323 ++++ native/vendor/ndk-0.9.0/src/audio.rs | 1429 +++++++++++++++ native/vendor/ndk-0.9.0/src/bitmap.rs | 489 +++++ native/vendor/ndk-0.9.0/src/configuration.rs | 600 +++++++ native/vendor/ndk-0.9.0/src/data_space.rs | 648 +++++++ native/vendor/ndk-0.9.0/src/event.rs | 1583 +++++++++++++++++ native/vendor/ndk-0.9.0/src/font.rs | 552 ++++++ .../vendor/ndk-0.9.0/src/hardware_buffer.rs | 640 +++++++ .../ndk-0.9.0/src/hardware_buffer_format.rs | 106 ++ native/vendor/ndk-0.9.0/src/input_queue.rs | 143 ++ native/vendor/ndk-0.9.0/src/lib.rs | 33 + native/vendor/ndk-0.9.0/src/looper.rs | 459 +++++ .../ndk-0.9.0/src/media/image_reader.rs | 459 +++++ .../vendor/ndk-0.9.0/src/media/media_codec.rs | 630 +++++++ .../ndk-0.9.0/src/media/media_format.rs | 264 +++ native/vendor/ndk-0.9.0/src/media/mod.rs | 8 + native/vendor/ndk-0.9.0/src/media_error.rs | 140 ++ .../vendor/ndk-0.9.0/src/native_activity.rs | 241 +++ native/vendor/ndk-0.9.0/src/native_window.rs | 459 +++++ native/vendor/ndk-0.9.0/src/shared_memory.rs | 153 ++ .../vendor/ndk-0.9.0/src/surface_texture.rs | 159 ++ native/vendor/ndk-0.9.0/src/sync.rs | 143 ++ native/vendor/ndk-0.9.0/src/trace.rs | 92 + native/vendor/ndk-0.9.0/src/utils.rs | 70 + .../mobile/CommunicationRoutePolicy.java | 21 +- .../dev/teamspeach/mobile/PlatformBridge.java | 14 +- .../dev/teamspeach/mobile/VoiceService.java | 179 +- .../mobile/CommunicationRoutePolicyTest.java | 35 +- 55 files changed, 11176 insertions(+), 205 deletions(-) create mode 100644 native/vendor/cpal-0.18.2/src/host/aaudio/output_usage.rs create mode 100644 native/vendor/ndk-0.9.0/.cargo_vcs_info.json create mode 100644 native/vendor/ndk-0.9.0/.gitignore create mode 100644 native/vendor/ndk-0.9.0/CHANGELOG.md create mode 100644 native/vendor/ndk-0.9.0/Cargo.toml create mode 100644 native/vendor/ndk-0.9.0/Cargo.toml.orig create mode 100644 native/vendor/ndk-0.9.0/LICENSE-MIT create mode 100644 native/vendor/ndk-0.9.0/README.md create mode 100644 native/vendor/ndk-0.9.0/src/asset.rs create mode 100644 native/vendor/ndk-0.9.0/src/audio.rs create mode 100644 native/vendor/ndk-0.9.0/src/bitmap.rs create mode 100644 native/vendor/ndk-0.9.0/src/configuration.rs create mode 100644 native/vendor/ndk-0.9.0/src/data_space.rs create mode 100644 native/vendor/ndk-0.9.0/src/event.rs create mode 100644 native/vendor/ndk-0.9.0/src/font.rs create mode 100644 native/vendor/ndk-0.9.0/src/hardware_buffer.rs create mode 100644 native/vendor/ndk-0.9.0/src/hardware_buffer_format.rs create mode 100644 native/vendor/ndk-0.9.0/src/input_queue.rs create mode 100644 native/vendor/ndk-0.9.0/src/lib.rs create mode 100644 native/vendor/ndk-0.9.0/src/looper.rs create mode 100644 native/vendor/ndk-0.9.0/src/media/image_reader.rs create mode 100644 native/vendor/ndk-0.9.0/src/media/media_codec.rs create mode 100644 native/vendor/ndk-0.9.0/src/media/media_format.rs create mode 100644 native/vendor/ndk-0.9.0/src/media/mod.rs create mode 100644 native/vendor/ndk-0.9.0/src/media_error.rs create mode 100644 native/vendor/ndk-0.9.0/src/native_activity.rs create mode 100644 native/vendor/ndk-0.9.0/src/native_window.rs create mode 100644 native/vendor/ndk-0.9.0/src/shared_memory.rs create mode 100644 native/vendor/ndk-0.9.0/src/surface_texture.rs create mode 100644 native/vendor/ndk-0.9.0/src/sync.rs create mode 100644 native/vendor/ndk-0.9.0/src/trace.rs create mode 100644 native/vendor/ndk-0.9.0/src/utils.rs diff --git a/.gitea/workflows/android.yml b/.gitea/workflows/android.yml index f9285fd..73cd576 100644 --- a/.gitea/workflows/android.yml +++ b/.gitea/workflows/android.yml @@ -234,6 +234,8 @@ jobs: .android-ci/audio-test-results/device-catalog-tests --test-threads=1 rustc --edition=2021 --test native/vendor/cpal-0.18.2/src/host/aaudio/capture_timing.rs -o .android-ci/audio-test-results/capture-timing-tests .android-ci/audio-test-results/capture-timing-tests --test-threads=1 + rustc --edition=2021 --test native/vendor/cpal-0.18.2/src/host/aaudio/output_usage.rs -o .android-ci/audio-test-results/output-usage-tests + .android-ci/audio-test-results/output-usage-tests --test-threads=1 printf '#[path = "%s/native/src/platform/android/call_audio.rs"] mod call_audio;\n' "$GITHUB_WORKSPACE" > .android-ci/audio-test-results/call-audio-check.rs rustc --edition=2021 --test .android-ci/audio-test-results/call-audio-check.rs -o .android-ci/audio-test-results/call-audio-tests .android-ci/audio-test-results/call-audio-tests --test-threads=1 @@ -244,6 +246,7 @@ jobs: cargo test --locked -p teamspeach-native --no-default-features --target x86_64-unknown-linux-gnu --lib media::audio:: --jobs "$CARGO_BUILD_JOBS" -- --test-threads=1 cargo test --locked -p teamspeach-native --no-default-features --target x86_64-unknown-linux-gnu --lib capture_transition_tests --jobs "$CARGO_BUILD_JOBS" -- --test-threads=1 cargo test --locked -p teamspeach-native --no-default-features --target x86_64-unknown-linux-gnu --lib audit_lifecycle --jobs "$CARGO_BUILD_JOBS" -- --test-threads=1 + cargo test --locked -p teamspeach-native --no-default-features --target x86_64-unknown-linux-gnu --lib only_explicit_output_device_actions_request_platform_routing --jobs "$CARGO_BUILD_JOBS" -- --test-threads=1 cargo test --locked -p teamspeach-native --no-default-features --target x86_64-unknown-linux-gnu --lib android_attachment_ --jobs "$CARGO_BUILD_JOBS" -- --test-threads=1 cargo test --locked -p teamspeach-native --no-default-features --target x86_64-unknown-linux-gnu --lib app::connections::tests --jobs "$CARGO_BUILD_JOBS" -- --test-threads=1 cargo test --locked -p teamspeach-core --target x86_64-unknown-linux-gnu --lib audio::tests --jobs "$CARGO_BUILD_JOBS" -- --test-threads=1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e1c9c4..2c0484c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,9 +8,10 @@ without logging; dropped output blocks and suppressed diagnostic events are counted separately. - Join a terminated Android runtime before first re-entry instead of attaching to its frozen snapshot; ordinary Activity recreation retains a live call and stale UI generations cannot stop the new owner. -- Follow connected communication headsets and restore the prior handset route on removal. Fence old - PCM until Android acknowledges routing, then rebind previously authorized capture; newer mute, - permission loss or focus interruption wins. Exact external selections remain explicit. +- Keep Android capture and playback choices explicit: attaching a headset never selects it for the user, + exact built-in microphone selections are retained, and only explicit playback actions change the + communication route. Voice outputs use runtime-resolved AAudio voice/speech attributes on API28+; + API26/27 keep the link-safe legacy behavior. Independent notification output retains media policy. - Keep already-authorized steady Android voice calls alive across Home, screen-off and UI recreation; release momentary PTT/whisper controls, preserve permission/audio-focus revocation, and scope CPU wake ownership to the foreground call. Removing the app from Recents ends its call and capture services. diff --git a/Cargo.lock b/Cargo.lock index 6dacd5f..0562ddb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4568,8 +4568,6 @@ dependencies = [ [[package]] name = "ndk" version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3f42e7bbe13d351b6bead8286a43aac9534b82bd3cc43e47037f012ebfd62d4" dependencies = [ "bitflags 2.13.2", "jni-sys 0.3.1", diff --git a/Cargo.toml b/Cargo.toml index e924f09..b8df666 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] resolver = "3" members = ["core", "dsp", "native", "xtask"] -exclude = ["core/vendor/tsclientlib", "core/vendor/tsproto", "dsp/vendor/sonora", "dsp/vendor/sonora-agc2", "native/vendor/libwebrtc-0.4.0", "native/vendor/webrtc-sys-0.3.53", "native/vendor/katex-rs-0.3.0", "native/vendor/mermaid-rs-renderer-0.3.1", "native/vendor/cpal-0.18.2", "native/vendor/i-slint-backend-android-activity-1.18.1"] +exclude = ["core/vendor/tsclientlib", "core/vendor/tsproto", "dsp/vendor/sonora", "dsp/vendor/sonora-agc2", "native/vendor/libwebrtc-0.4.0", "native/vendor/webrtc-sys-0.3.53", "native/vendor/katex-rs-0.3.0", "native/vendor/mermaid-rs-renderer-0.3.1", "native/vendor/cpal-0.18.2", "native/vendor/i-slint-backend-android-activity-1.18.1", "native/vendor/ndk-0.9.0"] [workspace.package] version = "0.1.7" @@ -39,6 +39,7 @@ katex-rs = { path = "native/vendor/katex-rs-0.3.0" } mermaid-rs-renderer = { path = "native/vendor/mermaid-rs-renderer-0.3.1" } cpal = { path = "native/vendor/cpal-0.18.2" } i-slint-backend-android-activity = { path = "native/vendor/i-slint-backend-android-activity-1.18.1" } +ndk = { path = "native/vendor/ndk-0.9.0" } [profile.release] lto = "thin" diff --git a/LICENSES/NOTICE.md b/LICENSES/NOTICE.md index fca482f..d5646d4 100644 --- a/LICENSES/NOTICE.md +++ b/LICENSES/NOTICE.md @@ -26,3 +26,14 @@ `teamspeak-client-sounds-PROVENANCE.md`. They are not relicensed by TeamsPeach. - TeamSpeak is a trademark of TeamSpeak Systems GmbH. TeamsPeach is an independent client and is not affiliated with or endorsed by TeamSpeak Systems GmbH. +- Android uses Rust Mobile `ndk` 0.9.0 (MIT OR Apache-2.0; the MIT alternative is reproduced in + `native/vendor/ndk-0.9.0/LICENSE-MIT` from upstream revision + `49bbbba16c58ff63cb8a0ad0eca5a9fb7ecaec25`). The vendored archive is + `https://static.crates.io/crates/ndk/ndk-0.9.0.crate`, SHA-256 + `c3f42e7bbe13d351b6bead8286a43aac9534b82bd3cc43e47037f012ebfd62d4`. + Its only source patch exposes and documents the existing builder/stream `as_ptr` methods in + `src/audio.rs`; no struct layout, dependency, feature or Android API-level requirement changes. + The archive hash identifies the pristine upstream package, not the locally patched directory. + Cargo uses the root `[patch.crates-io]` path, so its lockfile path entry must not retain the + upstream registry `source`/`checksum`; no fabricated `.cargo-checksum.json` is added. + CPAL remains the single audio backend and resolves optional API28 attribute functions at runtime. diff --git a/docs/mobile-port-progress.md b/docs/mobile-port-progress.md index dd9a616..0013644 100644 --- a/docs/mobile-port-progress.md +++ b/docs/mobile-port-progress.md @@ -698,6 +698,47 @@ to this pre-UI scope. No speculative microphone gain/audio refactor on accepted audio_route Java/native each <=16records/10s reports actual type selection, request/ACK/timeout/focus and recovery phase/supersession. No arbitrary ServiceError text, PCM or endpoint identities logged. New counter/rate-gate/route suppression tests await CI; no speculative route behavior change. +- 4ea413f58930a5f0832292f9d82edf70d564b78c run4943/job5539 SUCCESS: native155,route-state7, + capture18,lifecycle12,restart3,manager7,DSP15,catalog4,timestamp3,core23+output2 passed/1ignored. + APK downloaded, ZIP+6 hashes verified: f11270b4e32e7c76bcadb49f289f7c63ee2f290e1a756e254b1694ef73ee4a04. + Confirmed signature mismatch; authorized disposable uninstall/install4943 both Success; cold launch + completed. No server connection or microphone action by assistant. Next owner scenario: connect, + headset migration, peer speech; if RX silent leave state unchanged and collect only audio_route, + audio_command/audio_summary/audio_first_fault diagnostics. Instrumentation itself is verified, + Bluetooth root cause and physical route recovery remain unresolved; no diagnostic pass = audio pass. +- While awaiting owner Bluetooth reproduction on4943, static source review found AAudio output builder + sets direction/channels/format but no usage/content type (vendored cpal aaudio/mod.rs:762–765). + Android defaults AAudio usage to MEDIA; VoiceService chooses communication device/VOICE_COMMUNICATION + focus. This is a concrete stream-policy mismatch candidate for BT TX-only, not device attribution. + Primary API: https://developer.android.com/ndk/reference/group/audio; pinned ndk0.9 builder usage and + content_type methods are API28-gated while client minAPI26 remains supported. Do not blindly enable + API28 imports/raise minSDK or use unsafe private builder layout; a compatible fix needs validated + route evidence and explicit version-safe binding. No additional source change/build from this finding. +- Owner now explicitly REVOKES automatic headset adoption. Replace topology-triggered priority switching + and built-in→default coercion with manual independent Capture/Playback selection; system default + follows OS, explicit external/phone IDs stay exact. Owner still reports headset TX but silent RX and + inability to select another microphone while headset connected. No new retained bounded audio logs + even with direct current-PID filter; installed4943 SHA verified. Do not fabricate device attribution. +- Scoped work: explicit Playback may request matching communication device with ACK; Capture selection + must not redirect output. Expose one representative real built-in microphone rather than hiding all + phone inputs. Unknown/vendor devices remain identifiable by real IDs, never name heuristics. +- Native output fix requires API26-safe usage configuration: exact ndk0.9 vendoring approved against + Cargo.lock checksum c3f42e7bbe13d351b6bead8286a43aac9534b82bd3cc43e47037f012ebfd62d4; tiny documented + raw-pointer accessors permit CPAL dynamic API28 symbol resolution without private layout casts or + raised minSDK. Voice outputs request VoiceCommunication/Speech; independent notifications remain + MEDIA, same-sink voice upgrade must rebuild properly. API26/27 absence reported as legacy default, + not claimed equivalent routing. Implementation/testing pending; no source usage fix pass yet. +- Manual routing implemented: no priority/SCO/speaker forcing on topology callbacks; one typed real + built-in mic is selectable alongside external sources. Capture/DSP refresh never requests playback + route. Only Output Select/Map/New requests exact communication output; startup route ACK precedes + nativeVoiceStarted. Manual ACK completes caller configuration without a duplicate queued recovery. +- NDK0.9 vendored with upstream license/provenance and minimal borrowed-pointer accessor patch; + Cargo.lock synchronized offline at same version. Voice usage/content dynamically set+queried on28+; + open/callback PCM shape validated, physical endpoint tracked without logging IDs. MEDIA-only owners + are retired before voice-role upgrade; default/exact alias handling preserves one physical owner + per shared endpoint. Ambiguous older-platform defaults reconcile serially (brief interruption possible). + Java manual selection fixture passed; portable usage and native lifecycle regressions registered in CI. + No hardware mixed-microphone/headset support or RX repair claimed before new device evidence. ## First Android APK build diff --git a/native/src/app/runtime/lifecycle_tests.rs b/native/src/app/runtime/lifecycle_tests.rs index a42ae5a..c10beea 100644 --- a/native/src/app/runtime/lifecycle_tests.rs +++ b/native/src/app/runtime/lifecycle_tests.rs @@ -286,3 +286,32 @@ async fn audit_lifecycle_route_fence_never_promotes_receive_only_or_momentary_ca assert!(!ctx.transmission.lock().unwrap().capture_desired,"another route observation is never a new grant"); assert!(!ctx.audio.input_active());cleanup(owner).await; } + +#[tokio::test(flavor="current_thread")] +async fn audit_lifecycle_manual_phone_capture_keeps_playback_selection_and_later_mute_wins() { + use crate::media::audio::{devices::{DeviceSelection,Direction},engine::{AudioCommand,AudioEvent}}; + let(_directory,mut owner,_events,_controls)=fixture().await;let ctx=owner.ctx.clone(); + // Selection command consumption is real; no Android route or hardware-open + // success is claimed by these inert IDs and the synthetic capture boundary. + ctx.audio.request(AudioCommand::SelectOutput(DeviceSelection::Exact("aaudio:80".into()))).await.unwrap(); + let key=route_capture_fixture(&owner).await; + let(_entered,release)=route_capture_barrier(&mut owner);release.send(()).unwrap();ctx.refresh_capture().await.unwrap(); + let old=ctx.connected.connections.current_capture_permit().unwrap(); + owner.audio_events.try_iter().for_each(drop); + let output_revision=ctx.audio.notification_revision(); + let phone=DeviceSelection::Exact("aaudio:11".into()); + ctx.audio.request(AudioCommand::SelectInput(phone.clone())).await.unwrap(); + let selections:Vec<_>=owner.audio_events.try_iter().filter_map(|event|match event{AudioEvent::Selected{direction,selection}=>Some((direction,selection)),_=>None}).collect(); + assert_eq!(selections,vec![(Direction::Input,phone)],"manual phone capture must neither alias to default nor select a playback endpoint"); + assert_eq!(ctx.audio.notification_revision(),output_revision,"capture selection cannot redirect the voice-follow output"); + assert!(!ctx.audio.input_active(),"selecting an endpoint is not capture authorization or a StartInput ACK"); + controller::suspend_route_immediate(&ctx,&key,true); + let pending=ctx.transmission.lock().unwrap().revision; + let stale=teamspeach_core::audio::CapturedFrame{pcm:[1;960],captured_at:Instant::now(),connection_generation:key.generation,capture_epoch:old.capture_epoch,whisper:None}; + assert_eq!(ctx.connected.connections.push_audio(&key,stale),crate::app::connections::CaptureIngress::PermitRevoked); + controller::release_immediate(&ctx); + let configuration=ctx.audio_configuration.lock().await; + assert!(!ctx.resume_mobile_route_capture_locked(&key,pending,&configuration).await.unwrap(),"a late manual playback/OS route ACK cannot override a newer mute"); + assert!(!ctx.audio.input_active());assert!(!ctx.transmission.lock().unwrap().capture_desired); + drop(configuration);cleanup(owner).await; +} diff --git a/native/src/app/runtime/mobile_calls.rs b/native/src/app/runtime/mobile_calls.rs index 22aceb4..73ef6e3 100644 --- a/native/src/app/runtime/mobile_calls.rs +++ b/native/src/app/runtime/mobile_calls.rs @@ -31,7 +31,7 @@ impl RuntimeServices { #[cfg(target_os="android")] { if let Some((current,_))=call.as_ref(){if current!=key{crate::platform::android::stop_voice(current)?;*call=None;}} - if call.is_none(){let runtime=self.runtime.upgrade().ok_or_else(closed)?;crate::platform::android::start_voice(runtime,key.clone(),microphone,self.policy).await?;} + if call.is_none(){let runtime=self.runtime.upgrade().ok_or_else(closed)?;let prefs=self.profile()?.prefs.get();crate::platform::android::start_voice(runtime,key.clone(),microphone,prefs["outputDeviceId"].as_str().unwrap_or_default(),self.policy).await?;} else{crate::platform::android::set_voice_capture(key,microphone,self.policy).await?;} } #[cfg(target_os="ios")] @@ -82,6 +82,8 @@ impl RuntimeServices { self.audio_input_active.store(self.audio.input_active(),Ordering::Release); if let Err(changed)=current(){reason=changed;return Ok(())} let prefs=self.profile()?.prefs.get(); + // Recovery preserves both exact IDs. Observing an OS topology change + // must never request or rank a new communication output device. phase=Phase::SelectInput; self.audio.request(AudioCommand::SelectInput(crate::platform::android::voice_device_selection(prefs["inputDeviceId"].as_str().unwrap_or_default())?)).await?; if let Err(changed)=current(){reason=changed;return Ok(())} diff --git a/native/src/app/runtime/settings_actions/audio.rs b/native/src/app/runtime/settings_actions/audio.rs index b0f4dfd..f0a41c5 100644 --- a/native/src/app/runtime/settings_actions/audio.rs +++ b/native/src/app/runtime/settings_actions/audio.rs @@ -43,8 +43,7 @@ async fn sync_audio_to(ctx:&Arc,resume:Option<(bool,bool)>,expe ctx.connected.connections.configure_workflow(WorkflowSettings{auto_enable_microphone:prefs["autoEnableMicrophone"]==true,auto_reactivate_previous:prefs["autoReactivateMicOnPreviousTarget"]==true}).await.map_err(super::super::context::connection_error)?; let selected=(selection(prefs["inputDeviceId"].as_str().unwrap_or_default())?,selection(prefs["outputDeviceId"].as_str().unwrap_or_default())?); let selected_id=|selection:&DeviceSelection|match selection{DeviceSelection::SystemDefault=>String::new(),DeviceSelection::Exact(id)=>id.clone()}; - // Cache the applied role, not a persisted phone endpoint that becomes the - // communication default only after the foreground call lease is acquired. + // Cache the exact applied selections; phone endpoints are not default aliases. let applied=(selected_id(&selected.0),selected_id(&selected.1)); let previous=lock(&ctx.applied_audio_selections)?.clone(); if previous.as_ref().is_none_or(|p|p.0!=applied.0){stage="select_input";ctx.audio.request(AudioCommand::SelectInput(selected.0)).await?;} @@ -116,9 +115,15 @@ pub(super) async fn update_notifications(ctx:&Arc)->Result<()> if lock(&ctx.microphone_test_resume)?.is_none(){sync_audio_to(ctx,Some(active),None,&configuration).await?;}else{sync_notifications(ctx).await?;} Ok(()) } +#[cfg(any(target_os="android",test))] +fn requests_playback_route(op:&Operation)->bool { + matches!(op,Operation::SelectDevice{kind:DeviceKind::Output,..}|Operation::MapDevice{kind:DeviceKind::Output,..}|Operation::NewDevice{kind:DeviceKind::Output,..}) +} pub(super) async fn invoke(ctx:Arc,op:Operation)->Result{ let notification_only=matches!(&op,Operation::SelectDevice{kind:DeviceKind::Notification,..}|Operation::MapDevice{kind:DeviceKind::Notification,..}|Operation::NewDevice{kind:DeviceKind::Notification,..}); if !notification_only&&!matches!(&op,Operation::AudioTest(_)|Operation::PreviewNotification(_))&&lock(&ctx.microphone_test_resume)?.is_some(){return Err(ServiceError::new("microphone_test_active","Finish the private microphone test before changing devices"))} + #[cfg(target_os="android")] + let playback_selection=requests_playback_route(&op); match op { Operation::SelectDevice{kind,id}=>{patch(&ctx,selection_key(kind),json!(id)).await?;}, Operation::MapDevice{kind,legacy,native}=>{ctx.mutate(move|p|{p.prefs.map_legacy_device(kind,&legacy,&native)?;Ok(())}).await?;}, @@ -177,10 +182,31 @@ pub(super) async fn invoke(ctx:Arc,op:Operation)->Resultreturn Err(bad("Invalid audio operation")), - }if notification_only{update_notifications(&ctx).await?;}else{sync_audio(ctx).await?;}done() + } + #[cfg(target_os="android")] + if playback_selection { + let configuration=ctx.audio_configuration.lock().await; + ctx.audio.revoke_capture(); + let prefs=ctx.profile()?.prefs.get(); + let route=crate::platform::android::select_voice_playback(prefs["outputDeviceId"].as_str().unwrap_or_default()).await?; + sync_audio_to(&ctx,None,None,&configuration).await?; + if let Some((token,revision))=route{crate::platform::android::acknowledge_voice_playback(token,revision,ctx.audio.output_active());} + return done() + } + if notification_only{update_notifications(&ctx).await?;}else{sync_audio(ctx).await?;}done() } #[cfg(test)]mod tests { use super::*; + #[test]fn only_explicit_output_device_actions_request_platform_routing(){ + for kind in [DeviceKind::Input,DeviceKind::Notification,DeviceKind::Output] { + let expected=matches!(kind,DeviceKind::Output); + assert_eq!(requests_playback_route(&Operation::SelectDevice{kind,id:"aaudio:11".into()}),expected); + assert_eq!(requests_playback_route(&Operation::MapDevice{kind,legacy:"old".into(),native:"aaudio:11".into()}),expected); + assert_eq!(requests_playback_route(&Operation::NewDevice{kind,native:"aaudio:11".into()}),expected); + assert!(!requests_playback_route(&Operation::CopyDspToTemplate{kind,device:"aaudio:11".into()})); + assert!(!requests_playback_route(&Operation::ApplyDspTemplate{kind,device:"aaudio:11".into()})); + } + } #[test]fn ptt_forwarding_never_rewrites_selected_device_vad_policy(){ let mut prefs=crate::services::prefs::defaults();prefs["pttEnabled"]=json!(true);prefs["inputDeviceId"]=json!("native-input"); let mut device=crate::services::prefs::input_default();device["vadMode"]=json!("volume_gate");device["vadLevel"]=json!(-17);prefs["inputDsp"]["devices"]["native-input"]=device; diff --git a/native/src/media/audio/devices.rs b/native/src/media/audio/devices.rs index aa5913c..b0d0ee8 100644 --- a/native/src/media/audio/devices.rs +++ b/native/src/media/audio/devices.rs @@ -86,6 +86,23 @@ pub struct SelectedDevice { device: cpal::Device, } +impl SelectedDevice { + /// Resolve the output role to an exact Android endpoint when available. + /// Unresolved defaults are reconciled serially by the engine, never guessed. + pub fn prepare_output(&mut self, _voice: bool) -> Result { + #[cfg(target_os = "android")] + { + let resolved = if let cpal::platform::DeviceInner::AAudio(device) = self.device.as_inner_mut() { + device.prepare_output(_voice).map_err(audio_error)? + } else { true }; + self.id = self.device.id().map_err(audio_error)?.to_string(); + Ok(resolved) + } + #[cfg(not(target_os = "android"))] + { Ok(true) } + } +} + /// Host choice is made once. Refresh, selection and stream failure never pick /// another backend. The persisted host can be restored with `pinned`. pub struct DeviceHost { host: cpal::Host } @@ -762,10 +779,16 @@ pub fn start_playback(selected: SelectedDevice, permission: StartPermission, epo let (references, rendered) = ring::channel(capacity); let callback = PlaybackCallback { consumer, references, state: state.clone(), clock: clock.clone(), format: selected.format, current: None, current_offset: 0, frame_position: 0, expected_at: None }; let stream = dispatch_pcm!(selected.format.sample_format, build_playback, &selected.device, callback, state.clone()).map_err(|error| StreamStartFailure::observed(error, &state))?; + #[cfg(target_os = "android")] + let device_id = if let cpal::platform::StreamInner::AAudio(aaudio) = stream.as_inner() { + aaudio.device_id().map_err(audio_error).map_err(|error| StreamStartFailure::observed(error, &state))?.to_string() + } else { selected.id }; + #[cfg(not(target_os = "android"))] + let device_id = selected.id; clock.calibrate(&stream); // The engine, not backend construction, grants the playback generation. stream.play().map_err(audio_error).map_err(|error| StreamStartFailure::observed(error, &state))?; - let owner = StreamOwner { stream: Some(stream), state: state.clone(), device_id: selected.id, format: selected.format, _owner_thread: PhantomData }; + let owner = StreamOwner { stream: Some(stream), state: state.clone(), device_id, format: selected.format, _owner_thread: PhantomData }; Ok((owner, PlaybackPort { samples, rendered, state })) } diff --git a/native/src/media/audio/devices_tests.rs b/native/src/media/audio/devices_tests.rs index 850298d..bf5f9c0 100644 --- a/native/src/media/audio/devices_tests.rs +++ b/native/src/media/audio/devices_tests.rs @@ -521,6 +521,10 @@ fn synchronous_start_errors_keep_the_known_backend_cause_code() { #[path = "../../../vendor/cpal-0.18.2/src/host/aaudio/capture_timing.rs"] mod android_capture_timing; +#[cfg(not(target_os = "android"))] +#[path = "../../../vendor/cpal-0.18.2/src/host/aaudio/output_usage.rs"] +mod android_output_usage; + #[test] fn android_end_counter_projects_complete_capture_buffers_without_time_compression() { for rate in [44_100_u32, 48_000] { diff --git a/native/src/media/audio/engine.rs b/native/src/media/audio/engine.rs index c6b674d..58c654f 100644 --- a/native/src/media/audio/engine.rs +++ b/native/src/media/audio/engine.rs @@ -80,6 +80,8 @@ pub enum AudioEvent { struct Envelope { revision: u64, output_revision: u64, test_revision: u64, command: AudioCommand, reply: Option>> } #[cfg(test)] type CaptureStart = Box std::result::Result<(DeviceLease, CapturePort, MonoConversion), devices::StreamStartFailure> + Send>; +#[cfg(test)] +type PlaybackStart = Box) -> std::result::Result + Send>; enum PlaybackMessage { Voice { key: SessionKey, epoch: u64, pcm: [i16; 960], received_at: Instant }, Stream { key: SessionKey, epoch: u64, id: u64, pcm: StereoBlock, received_at: Instant }, @@ -559,6 +561,7 @@ impl PreparedOutput { enum OpenedOutput { Existing(String), New(DeviceLease, PlaybackPort, PreparedOutput), + UnresolvedDefault(DeviceSelection), } impl Output { fn new(owner: DeviceLease, port: PlaybackPort, prepared: PreparedOutput, epoch: u64, route_revision: u64) -> Self { @@ -706,6 +709,8 @@ struct Worker { command_gate: diagnostics::CommandGate, #[cfg(test)] output_reset: Option) + Send>>, + #[cfg(test)] + playback_start: VecDeque, } impl Worker { fn new(shared: Arc, settings: CaptureSettings, events: SyncSender) -> Result { @@ -720,7 +725,8 @@ impl Worker { pending_events: VecDeque::with_capacity(EVENT_CAPACITY), current_command: "none", #[cfg(target_os = "android")] summary_gate: diagnostics::SummaryGate::default(), #[cfg(target_os = "android")] command_gate: diagnostics::CommandGate::default(), - #[cfg(test)] output_reset: None }) + #[cfg(test)] output_reset: None, + #[cfg(test)] playback_start: VecDeque::new() }) } fn emit(&mut self, event: AudioEvent) { if self.pending_events.is_empty() { @@ -1078,6 +1084,9 @@ impl Worker { Ok(()) } fn open_output(&mut self, selection: DeviceSelection, permission: StartPermission, voice: bool) -> Result<()> { + self.open_output_for_policy(selection, permission, voice, cfg!(target_os = "android")) + } + fn open_output_for_policy(&mut self, selection: DeviceSelection, permission: StartPermission, voice: bool, separate_voice_policy: bool) -> Result<()> { self.require_permission(Direction::Output, permission)?; self.shared.outputs.check(self.output_revision)?; // Once resolved, even a default role stays pinned until an explicit route change. @@ -1090,9 +1099,17 @@ impl Worker { let epoch = self.output_epoch; let revision = self.output_revision; let shared = self.shared.clone(); + #[cfg(test)] + let test_start = self.playback_start.pop_front(); let opened = self.lifecycle(move |owner| -> std::result::Result<_, devices::StreamStartFailure> { shared.outputs.check(revision)?; - let selected = owner.host()?.select(Direction::Output, &selection, None, permission)?; + #[cfg(test)] + if let Some(start) = test_start { return start(epoch, voice, known); } + let mut selected = owner.host()?.select(Direction::Output, &selection, None, permission)?; + let resolved = selected.prepare_output(voice)?; + if separate_voice_policy && !resolved && !known.is_empty() { + return Ok(OpenedOutput::UnresolvedDefault(selection)); + } if known.contains(&selected.id) { return Ok(OpenedOutput::Existing(selected.id)); } if known.len() == 2 { return Err(ServiceError::new("audio_output_limit", "Voice and notification routes already own two devices").into()); } // A new acoustic topology invalidates capture before hardware can start. @@ -1108,9 +1125,47 @@ impl Worker { self.shared.outputs.check(revision)?; if self.shared.stop.load(Ordering::Acquire) { return Err(closed()); } let (owner, port, prepared) = match opened { + OpenedOutput::UnresolvedDefault(selection) => { + // Before API33 (API31 for voice) a default role may alias the + // existing exact endpoint, but public APIs cannot prove it. + // Close the complementary owner first, open the requested role, + // then restore the old role by its *observed* physical ID. The + // normal Existing path shares/upgrades when both IDs coincide. + if self.outputs.len() != 1 { return Err(changed()); } + let previous = self.outputs.pop().ok_or_else(changed)?; + let id = previous.owner.device_id.clone(); + let previous_voice = previous.voice; + let previous_notification = previous.notification; + drop(previous); + self.publish_output_states(); + self.references.clear(); + self.open_output_for_policy(selection, permission, voice, separate_voice_policy)?; + if previous_voice { + self.open_output_for_policy(DeviceSelection::Exact(id.clone()), permission, true, separate_voice_policy)?; + } + if previous_notification { + self.open_output_for_policy(DeviceSelection::Exact(id), permission, false, separate_voice_policy)?; + } + return Ok(()); + } OpenedOutput::Existing(id) => { - let existing = self.outputs.iter_mut().find(|output| output.owner.device_id == id).ok_or_else(changed)?; + let index = self.outputs.iter().position(|output| output.owner.device_id == id).ok_or_else(changed)?; + let existing = &self.outputs[index]; if !existing.port.state.stats().enabled { return Err(changed()); } + if separate_voice_policy && voice && !existing.voice { + let notification = existing.notification; + // Retire MEDIA before opening communication on the same endpoint. + // The lifecycle owner destroys retired streams before its next job, + // so two callbacks never concurrently own this endpoint. + drop(self.outputs.remove(index)); + self.publish_output_states(); + self.references.clear(); + self.open_output_for_policy(DeviceSelection::Exact(id.clone()), permission, true, separate_voice_policy)?; + let reopened = self.outputs.iter_mut().find(|output| output.owner.device_id == id).ok_or_else(changed)?; + reopened.notification = notification; + return Ok(()); + } + let existing = &mut self.outputs[index]; if voice { existing.voice = true; } else { existing.notification = true; } return Ok(()); } @@ -1209,6 +1264,10 @@ impl Worker { self.emit(AudioEvent::Selected { direction: Direction::Input, selection }); } AudioCommand::SelectOutput(selection) => { + // A shared Android communication stream must not survive as an + // independent notification-only stream after voice moves away. + #[cfg(target_os = "android")] + self.outputs.retain(|output| !output.voice); for output in &mut self.outputs { output.voice = false; } self.outputs.retain(|output| output.notification); self.reset_outputs()?; diff --git a/native/src/media/audio/engine_tests.rs b/native/src/media/audio/engine_tests.rs index 55ee9fc..35766a4 100644 --- a/native/src/media/audio/engine_tests.rs +++ b/native/src/media/audio/engine_tests.rs @@ -384,6 +384,124 @@ fn install_voice_output(worker: &mut Worker) -> super::super::ring::Consumer OpenedOutput { + let (mut owner, port, _audible) = DeviceLease::playback_test(epoch); + owner.device_id = id.into(); + OpenedOutput::New(owner, port, PreparedOutput::new(48_000).unwrap()) +} + +#[test] +fn android_notification_first_owner_is_retired_before_voice_policy_upgrade() { + let (mut worker, _handle, _, _) = command_fixture(); + worker.playback_start.push_back(Box::new(|epoch, voice, known| { + assert!(!voice && known.is_empty()); + Ok(opened_playback_fixture("physical-output", epoch)) + })); + worker.open_output_for_policy(DeviceSelection::SystemDefault, StartPermission::Granted, false, true).unwrap(); + let media = worker.outputs[0].port.state.clone(); + let media_epoch = worker.outputs[0].epoch; + assert!(media.stats().enabled); + assert!(worker.outputs[0].notification && !worker.outputs[0].voice); + worker.playback_start.push_back(Box::new(|_, voice, known| { + assert!(voice); + assert_eq!(known, ["physical-output"]); + Ok(OpenedOutput::Existing("physical-output".into())) + })); + let old = media.clone(); + worker.playback_start.push_back(Box::new(move |epoch, voice, known| { + assert!(voice && known.is_empty()); + assert!(!old.stats().enabled, "MEDIA must be silent before communication opens"); + Ok(opened_playback_fixture("physical-output", epoch)) + })); + worker.open_output_for_policy(DeviceSelection::Exact("physical-output".into()), StartPermission::Granted, true, true).unwrap(); + assert_eq!(worker.outputs.len(), 1); + assert!(worker.outputs[0].voice && worker.outputs[0].notification); + assert!(worker.outputs[0].epoch > media_epoch); + assert!(!media.stats().enabled); + assert_eq!(worker.shared.outputs.states.lock().unwrap().len(), 1); + assert!(worker.playback_start.is_empty()); + assert!(!worker.lifecycle(|owner| owner.host_initialized()).unwrap()); +} + +#[test] +fn android_unknown_default_alias_reconciles_without_overlapping_owners() { + let (mut worker, _handle, _, _) = command_fixture(); + worker.playback_start.push_back(Box::new(|epoch, voice, _| { + assert!(!voice); + Ok(opened_playback_fixture("physical-output", epoch)) + })); + worker.open_output_for_policy(DeviceSelection::SystemDefault, StartPermission::Granted, false, true).unwrap(); + let media = worker.outputs[0].port.state.clone(); + worker.playback_start.push_back(Box::new(|_, voice, known| { + assert!(voice); + assert_eq!(known, ["physical-output"]); + Ok(OpenedOutput::UnresolvedDefault(DeviceSelection::SystemDefault)) + })); + let old = media.clone(); + worker.playback_start.push_back(Box::new(move |epoch, voice, known| { + assert!(voice && known.is_empty()); + assert!(!old.stats().enabled); + Ok(opened_playback_fixture("physical-output", epoch)) + })); + worker.playback_start.push_back(Box::new(|_, voice, known| { + assert!(!voice); + assert_eq!(known, ["physical-output"]); + Ok(OpenedOutput::Existing("physical-output".into())) + })); + worker.open_output_for_policy(DeviceSelection::SystemDefault, StartPermission::Granted, true, true).unwrap(); + assert_eq!(worker.outputs.len(), 1); + assert!(worker.outputs[0].voice && worker.outputs[0].notification); + assert!(!media.stats().enabled); + assert!(worker.playback_start.is_empty()); +} + +#[test] +fn android_unknown_distinct_default_restores_independent_media_role() { + let (mut worker, _handle, _, _) = command_fixture(); + worker.playback_start.push_back(Box::new(|epoch, voice, _| { + assert!(!voice); + Ok(opened_playback_fixture("media-output", epoch)) + })); + worker.open_output_for_policy(DeviceSelection::SystemDefault, StartPermission::Granted, false, true).unwrap(); + let media = worker.outputs[0].port.state.clone(); + worker.playback_start.push_back(Box::new(|_, voice, _| { + assert!(voice); + Ok(OpenedOutput::UnresolvedDefault(DeviceSelection::SystemDefault)) + })); + let old = media.clone(); + worker.playback_start.push_back(Box::new(move |epoch, voice, known| { + assert!(voice && known.is_empty()); + assert!(!old.stats().enabled); + Ok(opened_playback_fixture("voice-output", epoch)) + })); + worker.playback_start.push_back(Box::new(|epoch, voice, known| { + assert!(!voice); + assert_eq!(known, ["voice-output"]); + Ok(opened_playback_fixture("media-output", epoch)) + })); + worker.open_output_for_policy(DeviceSelection::SystemDefault, StartPermission::Granted, true, true).unwrap(); + assert_eq!(worker.outputs.len(), 2); + assert!(worker.outputs.iter().any(|output| output.owner.device_id == "voice-output" && output.voice && !output.notification)); + assert!(worker.outputs.iter().any(|output| output.owner.device_id == "media-output" && output.notification && !output.voice)); + assert!(!media.stats().enabled); + assert!(worker.playback_start.is_empty()); +} + +#[test] +fn desktop_notification_first_owner_is_shared_without_mobile_policy_reopen() { + let (mut worker, _handle, _, _) = command_fixture(); + worker.playback_start.push_back(Box::new(|epoch, _, _| Ok(opened_playback_fixture("physical-output", epoch)))); + worker.open_output_for_policy(DeviceSelection::SystemDefault, StartPermission::Granted, false, false).unwrap(); + let state = worker.outputs[0].port.state.clone(); + worker.playback_start.push_back(Box::new(|_, _, _| Ok(OpenedOutput::Existing("physical-output".into())))); + worker.open_output_for_policy(DeviceSelection::Exact("physical-output".into()), StartPermission::Granted, true, false).unwrap(); + assert_eq!(worker.outputs.len(), 1); + assert!(Arc::ptr_eq(&state, &worker.outputs[0].port.state)); + assert!(state.stats().enabled); + assert!(worker.outputs[0].voice && worker.outputs[0].notification); + assert!(worker.playback_start.is_empty()); +} + #[test] fn production_worker_mute_lifecycle_retains_device_and_restores_fresh_rx_for_multiple_connections() { let (mut worker, handle, commands, _events) = command_fixture(); @@ -1324,3 +1442,49 @@ fn diagnostic_voice_activity_retains_only_nonzero_count_and_coarse_peak_bucket() } assert_eq!(counters.totals().voice_nonzero_blocks, 4); } + +#[test] +fn android_voice_first_unknown_notification_alias_restores_communication_owner() { + let (mut worker, _handle, _, _) = command_fixture(); + worker.playback_start.push_back(Box::new(|epoch, voice, _| { + assert!(voice); + Ok(opened_playback_fixture("physical-output", epoch)) + })); + worker.open_output_for_policy(DeviceSelection::SystemDefault, StartPermission::Granted, true, true).unwrap(); + let first_voice = worker.outputs[0].port.state.clone(); + let transient: Arc>>> = Arc::new(Mutex::new(None)); + worker.playback_start.push_back(Box::new(|_, voice, known| { + assert!(!voice); + assert_eq!(known, ["physical-output"]); + Ok(OpenedOutput::UnresolvedDefault(DeviceSelection::SystemDefault)) + })); + let old_voice = first_voice.clone(); + let transient_media = transient.clone(); + worker.playback_start.push_back(Box::new(move |epoch, voice, known| { + assert!(!voice && known.is_empty()); + assert!(!old_voice.stats().enabled); + let opened = opened_playback_fixture("physical-output", epoch); + if let OpenedOutput::New(_, port, _) = &opened { + *transient_media.lock().unwrap() = Some(port.state.clone()); + } + Ok(opened) + })); + worker.playback_start.push_back(Box::new(|_, voice, known| { + assert!(voice); + assert_eq!(known, ["physical-output"]); + Ok(OpenedOutput::Existing("physical-output".into())) + })); + let transient_media = transient.clone(); + worker.playback_start.push_back(Box::new(move |epoch, voice, known| { + assert!(voice && known.is_empty()); + assert!(!transient_media.lock().unwrap().as_ref().unwrap().stats().enabled); + Ok(opened_playback_fixture("physical-output", epoch)) + })); + worker.open_output_for_policy(DeviceSelection::SystemDefault, StartPermission::Granted, false, true).unwrap(); + assert_eq!(worker.outputs.len(), 1); + assert!(worker.outputs[0].voice && worker.outputs[0].notification); + assert!(worker.outputs[0].port.state.stats().enabled); + assert!(!first_voice.stats().enabled); + assert!(!transient.lock().unwrap().as_ref().unwrap().stats().enabled); + assert!(worker.playback_start.is_empty()); +} diff --git a/native/src/platform/android.rs b/native/src/platform/android.rs index 3d1a236..412e702 100644 --- a/native/src/platform/android.rs +++ b/native/src/platform/android.rs @@ -27,7 +27,7 @@ static ROUTE_LOG_BUDGET: LazyLock> = LazyLock: #[derive(Clone, Copy, Debug)] pub(crate) enum VoiceRoutePhase { CallStart, CaptureChange, Authorization, Fence, Selection, Settings, Validate, Configuration, StopInput, SelectInput, SelectOutput, StopOutput, StartOutput, OutputAck, CaptureIntent, ResumeCapture, MuteCapture, Enumerate, Complete } #[derive(Clone, Copy, Debug)] -pub(crate) enum VoiceRouteReason { Queued, Timeout, RouteChanged, FocusGained, FocusLost, CaptureRevoked, Pending, Completed, Failed, OwnerChanged, AudioRevisionChanged, CaptureIntentChanged, PlaybackRevoked, OutputNotGranted, DefaultSelection, BuiltinDefault, ExactSelection } +pub(crate) enum VoiceRouteReason { Queued, Timeout, RouteChanged, FocusGained, FocusLost, CaptureRevoked, Pending, Completed, Failed, OwnerChanged, AudioRevisionChanged, CaptureIntentChanged, PlaybackRevoked, OutputNotGranted, DefaultSelection, ExactSelection } // Called only by service/control workers, never a PCM callback. No identity-bearing // keys, endpoint IDs, device descriptions or arbitrary platform error strings. fn route_log_permit() -> Option { ROUTE_LOG_BUDGET.lock().expect("Android route log budget").take(std::time::Instant::now()) } @@ -169,14 +169,14 @@ pub async fn request_permission(permission: Permission, policy: ActionPolicy) -> match pending.receive().await? { Response::Permission(value) => Ok(value), _ => Err(error("android_os_error", "Unexpected permission completion")) } } /// Caller revalidates `key` and active-call intent before this explicit foreground action. -pub async fn start_voice(runtime: Arc, key: SessionKey, microphone: bool, policy: ActionPolicy) -> Result<()> { - policy.check()?; let request = Request::new()?; let token = request.id; +pub async fn start_voice(runtime: Arc, key: SessionKey, microphone: bool, playback:&str, policy: ActionPolicy) -> Result<()> { + policy.check()?; let endpoint=voice_playback_endpoint(playback)?;let request = Request::new()?; let token = request.id; { let mut voice = VOICE.lock().expect("Android voice"); if voice.is_some() { return Err(error("call_lease_exists", "An Android foreground call already owns the audio session")); } *voice = Some(Voice { token, key: key.clone(), microphone: false, capture_revision: 0, capture_change: None, audio: Default::default(), _runtime: runtime }); } struct PendingVoice(u64, bool); impl Drop for PendingVoice { fn drop(&mut self) { if !self.1 { end_voice(self.0); let _ = call_void("stopVoice", "(J)V", &[JValue::Long(self.0 as i64)]); } } } let mut pending = PendingVoice(token, false); let started=std::time::Instant::now(); - if let Err(error)=with_env(|env, class| accepted(java(env.call_static_method(class, "startVoice", "(JZ)Z", &[JValue::Long(token as i64), JValue::Bool(microphone as u8)]))?.z().unwrap_or(false))){ + if let Err(error)=with_env(|env, class| accepted(java(env.call_static_method(class, "startVoice", "(JZI)Z", &[JValue::Long(token as i64), JValue::Bool(microphone as u8),JValue::Int(endpoint)]))?.z().unwrap_or(false))){ log_voice_request(VoiceRoutePhase::CallStart,VoiceRouteReason::Failed,started,microphone);return Err(error) } log_voice_request(VoiceRoutePhase::CallStart,VoiceRouteReason::Queued,started,microphone); @@ -236,23 +236,41 @@ fn voice_audio_changed(token:u64, focused:Option, restart_output:bool) { log_voice_authorization(token,VoiceRoutePhase::Authorization,reason); } } -/// Phone endpoints follow Android's communication route during an active call. -/// Explicit external and unknown endpoints keep their exact-ID failure behavior. +/// Capture and playback retain exact endpoint identity, including the phone mic. pub(crate) fn voice_device_selection(raw:&str)->Result{ use crate::media::audio::devices::DeviceSelection; - if raw.is_empty(){log_voice_selection(VoiceRouteReason::DefaultSelection);return Ok(DeviceSelection::SystemDefault)} - if VOICE.lock().expect("Android voice").is_some(){ - if let Ok(id)=raw.parse::(){ - if id.host()==cpal::HostId::AAudio { - if let Ok(endpoint)=id.id().parse::(){ - let builtin=with_env(|env,class|java(env.call_static_method(class,"voiceDeviceIsBuiltin","(I)Z",&[JValue::Int(endpoint)]))?.z().map_err(|_|error("android_os_error","Invalid audio endpoint metadata")))?; - if builtin{log_voice_selection(VoiceRouteReason::BuiltinDefault);return Ok(DeviceSelection::SystemDefault)} - } - } - } + if raw.is_empty(){log_voice_selection(VoiceRouteReason::DefaultSelection);Ok(DeviceSelection::SystemDefault)} + else{log_voice_selection(VoiceRouteReason::ExactSelection);Ok(DeviceSelection::Exact(raw.into()))} +} +fn voice_playback_endpoint(raw:&str)->Result { + if raw.is_empty(){return Ok(0)} + let id=raw.parse::().map_err(|_|error("device_unavailable","Invalid exact Android playback endpoint"))?; + if id.host()!=cpal::HostId::AAudio{return Err(error("device_unavailable","The exact playback endpoint is not an Android audio device"))} + match id.id().parse::(){Ok(-1)=>Ok(0),Ok(id) if id>0=>Ok(id),_=>Err(error("device_unavailable","The exact Android playback endpoint is unavailable"))} +} +/// Only an explicit playback action requests a communication-device override. +/// Its caller owns configuration and recovery; capture/DSP refresh never calls it. +pub(crate) async fn select_voice_playback(raw:&str)->Result> { + let token=match VOICE.lock().expect("Android voice").as_ref(){Some(voice)=>voice.token,None=>return Ok(None)}; + let endpoint=voice_playback_endpoint(raw)?; + let request=Request::new()?;let id=request.id; + struct PlaybackChange {token:u64,id:u64,completed:bool} + impl Drop for PlaybackChange {fn drop(&mut self){if !self.completed{let _=call_void("cancelVoicePlayback","(JJ)V",&[JValue::Long(self.token as i64),JValue::Long(self.id as i64)]);}}} + let mut change=PlaybackChange{token,id,completed:false}; + with_env(|env,class|accepted(java(env.call_static_method(class,"setVoicePlayback","(JJI)Z",&[JValue::Long(token as i64),JValue::Long(id as i64),JValue::Int(endpoint)]))?.z().unwrap_or(false)))?; + match tokio::time::timeout(std::time::Duration::from_secs(15),request.receive()).await { + Ok(Ok(Response::Text(_)))=>{ + let slot=VOICE.lock().expect("Android voice"); + let voice=slot.as_ref().filter(|voice|voice.token==token).ok_or_else(||error("target_changed","The Android call changed during playback selection"))?; + change.completed=true;Ok(Some((token,voice.audio.revision()))) + }, + Ok(Err(error))=>Err(error), + Err(_)=>Err(error("audio_route_timeout","Android did not acknowledge the exact playback communication route")), + _=>Err(error("target_changed","The Android call changed during playback selection")), } - log_voice_selection(VoiceRouteReason::ExactSelection); - Ok(DeviceSelection::Exact(raw.into())) +} +pub(crate) fn acknowledge_voice_playback(token:u64,revision:u64,active:bool) { + if let Some(voice)=VOICE.lock().expect("Android voice").as_mut().filter(|voice|voice.token==token){voice.audio.acknowledge_output(revision,active);} } pub(crate) fn voice_audio_allowed(key:&SessionKey,microphone:bool)->bool { VOICE.lock().expect("Android voice").as_ref().is_some_and(|voice|&voice.key==key&&voice.audio.allows(microphone)) @@ -450,8 +468,8 @@ pub extern "system" fn Java_dev_teamspeach_mobile_PlatformBridge_nativeVoiceEven match event{ 2=>voice_audio_changed(token as u64,Some(value==0),true), 3=>voice_audio_changed(token as u64,None,true), - // Fence before AudioManager changes hardware. Recovery is emitted only - // after the communication-device/SCO acknowledgement (or bounded failure). + // Fence before explicit playback requests or after an observed OS route + // change; only the acknowledged/observed continuation may recover PCM. 8=>{ let owner={let mut slot=VOICE.lock().expect("Android voice");slot.as_mut().filter(|voice|voice.token==token as u64).map(|voice|{ let pending=voice.capture_change.take(); @@ -477,6 +495,19 @@ pub extern "system" fn Java_dev_teamspeach_mobile_PlatformBridge_nativeVoiceCapt VOICE.lock().expect("Android voice").as_ref().is_some_and(|voice|voice.token==token as u64&&voice.capture_revision==id as u64) as u8 } #[unsafe(no_mangle)] +pub extern "system" fn Java_dev_teamspeach_mobile_PlatformBridge_nativeVoicePlaybackValid(_env:JNIEnv,_class:JClass,token:jlong,id:jlong)->jboolean{ + (VOICE.lock().expect("Android voice").as_ref().is_some_and(|voice|voice.token==token as u64)&&REQUESTS.lock().expect("Android requests").contains_key(&(id as u64))) as u8 +} +#[unsafe(no_mangle)] +pub extern "system" fn Java_dev_teamspeach_mobile_PlatformBridge_nativeVoicePlaybackFinished(_env:JNIEnv,_class:JClass,token:jlong,id:jlong){ + // The explicit settings caller resumes configuration after its Text ACK. + // Do not enqueue a second recovery worker that could revoke the new permit. + let mut slot=VOICE.lock().expect("Android voice"); + if REQUESTS.lock().expect("Android requests").contains_key(&(id as u64)) { + if let Some(voice)=slot.as_mut().filter(|voice|voice.token==token as u64){voice.audio.route_changed();} + } +} +#[unsafe(no_mangle)] pub extern "system" fn Java_dev_teamspeach_mobile_PlatformBridge_nativeVoiceCaptureChanged(_env:JNIEnv,_class:JClass,token:jlong,id:jlong,enabled:jboolean)->jboolean{ let active={let mut slot=VOICE.lock().expect("Android voice");if let Some(voice)=slot.as_mut().filter(|voice|voice.token==token as u64&&voice.capture_revision==id as u64&&voice.capture_change==Some(id as u64)){if !voice.audio.change_capture(enabled!=0){false}else{voice.microphone=enabled!=0;voice.capture_change=None;true}}else{false}}; log_voice_authorization(token as u64,VoiceRoutePhase::Authorization,if active{VoiceRouteReason::Completed}else{VoiceRouteReason::AudioRevisionChanged}); diff --git a/native/src/platform/android/call_audio.rs b/native/src/platform/android/call_audio.rs index 08e4e40..8472e09 100644 --- a/native/src/platform/android/call_audio.rs +++ b/native/src/platform/android/call_audio.rs @@ -8,6 +8,7 @@ pub(super) struct CallAudioState { route_pending: bool, } impl CallAudioState { + pub fn revision(&self) -> u64 { self.revision } pub fn start(&mut self, capture: bool) { self.focused = true; self.capture = capture; self.route_pending = false; } pub fn change_capture(&mut self, enabled: bool) -> bool { if enabled && (!self.focused || self.route_pending) { return false; } diff --git a/native/vendor/cpal-0.18.2/src/host/aaudio/device_catalog.rs b/native/vendor/cpal-0.18.2/src/host/aaudio/device_catalog.rs index 19fe42f..db71eb8 100644 --- a/native/vendor/cpal-0.18.2/src/host/aaudio/device_catalog.rs +++ b/native/vendor/cpal-0.18.2/src/host/aaudio/device_catalog.rs @@ -40,15 +40,12 @@ pub enum AudioDeviceType { } impl AudioDeviceType { - /// Built-in microphones are Android-managed routes of the system default, - /// not independent user microphones. Internal capture buses, loopbacks and - /// tuners likewise must not be offered as voice capture endpoints. Unknown - /// types remain visible: a vendor-specific external device must not vanish. + /// Physical built-in microphones remain selectable. Internal capture buses, + /// loopbacks and tuners are not voice microphones; unknown vendor types stay visible. pub fn selectable_caps(self, is_source: bool, is_sink: bool) -> (bool, bool) { let internal_capture = matches!( self, - Self::BuiltinMic - | Self::BuiltinEarpiece + Self::BuiltinEarpiece | Self::BuiltinSpeaker | Self::BuiltinSpeakerSafe | Self::Bus @@ -106,6 +103,11 @@ impl AudioDeviceType { } } +/// Pick one physical phone mic by type and stable Android endpoint ID, never by +/// product name or enumeration order. External microphones are never deduplicated. +pub fn builtin_mic_representative(endpoints: impl Iterator) -> Option { + endpoints.filter_map(|(id, kind, source)| (source && matches!(kind, AudioDeviceType::BuiltinMic)).then_some(id)).min() +} /// AAudio may choose any route only for the system-default role. An explicit /// endpoint must never silently become a different microphone or loudspeaker. pub fn accepts_route(requested: Option, actual: i32) -> bool { @@ -125,15 +127,15 @@ mod tests { } fn capture_ids(endpoints: &[Endpoint]) -> Vec { - endpoints - .iter() + let phone = builtin_mic_representative(endpoints.iter().map(|endpoint| (endpoint.id, endpoint.kind, endpoint.source))); + endpoints.iter() .filter(|endpoint| endpoint.kind.selectable_caps(endpoint.source, endpoint.sink).0) - .map(|endpoint| endpoint.id) - .collect() + .filter(|endpoint| !matches!(endpoint.kind, AudioDeviceType::BuiltinMic) || Some(endpoint.id) == phone) + .map(|endpoint| endpoint.id).collect() } #[test] - fn handset_internal_capture_ports_leave_only_system_default_role() { + fn handset_exposes_one_real_phone_mic_without_internal_capture_ports() { let endpoints = [ Endpoint { id: 11, kind: AudioDeviceType::BuiltinMic, product: "TECNO KJ7", source: true, sink: false }, Endpoint { id: 12, kind: AudioDeviceType::BuiltinMic, product: "TECNO KJ7", source: true, sink: false }, @@ -142,9 +144,10 @@ mod tests { Endpoint { id: 15, kind: AudioDeviceType::RemoteSubmix, product: "TECNO KJ7", source: true, sink: false }, Endpoint { id: 16, kind: AudioDeviceType::FmTuner, product: "TECNO KJ7", source: true, sink: false }, ]; - // The host adds its existing aaudio:-1 default role separately; none of - // these internal IDs should appear alongside that role as microphones. - assert!(capture_ids(&endpoints).is_empty()); + assert_eq!(capture_ids(&endpoints), [11]); + let reversed: Vec<_> = endpoints.into_iter().rev().collect(); + assert_eq!(capture_ids(&reversed), [11], "enumeration order and duplicate product names cannot change the representative"); + assert_eq!(capture_ids(&reversed[..5]), [12], "removal exposes the remaining real endpoint, not an invented alias"); } #[test] @@ -170,7 +173,7 @@ mod tests { #[test] fn filtering_capture_never_invents_sources_or_removes_sinks() { - assert_eq!(AudioDeviceType::BuiltinMic.selectable_caps(true, false), (false, false)); + assert_eq!(AudioDeviceType::BuiltinMic.selectable_caps(true, false), (true, false)); assert_eq!(AudioDeviceType::Bus.selectable_caps(true, true), (false, true)); assert_eq!(AudioDeviceType::BuiltinSpeaker.selectable_caps(false, true), (false, true)); assert_eq!(AudioDeviceType::BluetoothA2DP.selectable_caps(false, true), (false, true)); @@ -178,6 +181,20 @@ mod tests { assert_eq!(AudioDeviceType::UsbDevice.selectable_caps(false, false), (false, false)); } + #[test] + fn phone_mic_remains_exact_and_visible_with_identically_named_headset() { + let endpoints = [ + Endpoint { id: 12, kind: AudioDeviceType::BuiltinMic, product: "Phone", source: true, sink: false }, + Endpoint { id: 4, kind: AudioDeviceType::BuiltinMic, product: "Phone", source: false, sink: true }, + Endpoint { id: 11, kind: AudioDeviceType::BuiltinMic, product: "Phone", source: true, sink: false }, + Endpoint { id: 80, kind: AudioDeviceType::WiredHeadset, product: "Phone", source: true, sink: true }, + ]; + assert_eq!(capture_ids(&endpoints), [11, 80]); + assert_eq!(capture_ids(&endpoints[..2]), [12], "sink-only metadata cannot become the mic representative"); + assert!(accepts_route(Some(11), 11)); + assert!(!accepts_route(Some(11), 80), "Android rejecting mixed phone mic/headset routing must not silently substitute headset capture"); + } + #[test] fn disconnected_explicit_endpoint_cannot_become_a_default_route() { assert!(accepts_route(Some(22), 22)); diff --git a/native/vendor/cpal-0.18.2/src/host/aaudio/java_interface/audio_manager.rs b/native/vendor/cpal-0.18.2/src/host/aaudio/java_interface/audio_manager.rs index 35021ff..a0acfb7 100644 --- a/native/vendor/cpal-0.18.2/src/host/aaudio/java_interface/audio_manager.rs +++ b/native/vendor/cpal-0.18.2/src/host/aaudio/java_interface/audio_manager.rs @@ -1,9 +1,48 @@ use super::{ - utils::{get_context, get_system_property, with_attached, Env, JResult}, - AudioManager, + utils::{call_method_no_args_ret_int, get_context, get_system_property, get_system_service, with_attached, Env, JResult}, + AudioManager, Context, }; impl AudioManager { + /// Observe one OS-predicted output for the requested usage; never changes routing. + /// API33+ can predict voice or MEDIA. API31–32 can observe voice only. + /// An unsupported API or a multi-device/absent route has no single identity. + pub fn output_device_id(voice: bool) -> Result, String> { + with_attached(get_context(), |env, context| { + let sdk = env.get_static_field( + jni::jni_str!("android/os/Build$VERSION"), + jni::jni_str!("SDK_INT"), + jni::jni_sig!("I"), + )?.i()?; + if sdk < 31 || (!voice && sdk < 33) { return Ok(None); } + let audio = get_system_service(env, &context, Context::AUDIO_SERVICE)?; + let device = if sdk >= 33 { + let builder = env.new_object( + jni::jni_str!("android/media/AudioAttributes$Builder"), + jni::jni_sig!("()V"), &[], + )?; + env.call_method(&builder, jni::jni_str!("setUsage"), + jni::jni_sig!("(I)Landroid/media/AudioAttributes$Builder;"), + &[if voice { 2i32 } else { 1i32 }.into()])?; + env.call_method(&builder, jni::jni_str!("setContentType"), + jni::jni_sig!("(I)Landroid/media/AudioAttributes$Builder;"), + &[if voice { 1i32 } else { 2i32 }.into()])?; + let attributes = env.call_method(&builder, jni::jni_str!("build"), + jni::jni_sig!("()Landroid/media/AudioAttributes;"), &[])?.l()?; + let devices = env.call_method(&audio, jni::jni_str!("getAudioDevicesForAttributes"), + jni::jni_sig!("(Landroid/media/AudioAttributes;)Ljava/util/List;"), &[(&attributes).into()])?.l()?; + if call_method_no_args_ret_int(env, &devices, "size")? != 1 { return Ok(None); } + env.call_method(&devices, jni::jni_str!("get"), + jni::jni_sig!("(I)Ljava/lang/Object;"), &[0i32.into()])?.l()? + } else { + env.call_method(&audio, jni::jni_str!("getCommunicationDevice"), + jni::jni_sig!("()Landroid/media/AudioDeviceInfo;"), &[])?.l()? + }; + if device.is_null() { return Ok(None); } + call_method_no_args_ret_int(env, &device, "getId").map(Some) + }).map_err(|error| error.to_string()) + } + /// Get the AAudio mixer burst count from system property pub fn get_mixer_bursts() -> Result { let context = get_context(); diff --git a/native/vendor/cpal-0.18.2/src/host/aaudio/java_interface/devices_info.rs b/native/vendor/cpal-0.18.2/src/host/aaudio/java_interface/devices_info.rs index 9c4c970..9cf54b7 100644 --- a/native/vendor/cpal-0.18.2/src/host/aaudio/java_interface/devices_info.rs +++ b/native/vendor/cpal-0.18.2/src/host/aaudio/java_interface/devices_info.rs @@ -90,5 +90,18 @@ fn try_request_devices_info<'j>( formats, }); } + let phone = super::super::device_catalog::builtin_mic_representative(result.iter().map(|device| { + (device.id, device.device_type, matches!(device.direction, DeviceDirection::Input | DeviceDirection::Duplex)) + })); + result.retain_mut(|device| { + if matches!(device.device_type, AudioDeviceType::BuiltinMic) && Some(device.id) != phone { + match device.direction { + DeviceDirection::Input => return false, + DeviceDirection::Duplex => device.direction = DeviceDirection::Output, + _ => {} + } + } + true + }); Ok(result) } diff --git a/native/vendor/cpal-0.18.2/src/host/aaudio/mod.rs b/native/vendor/cpal-0.18.2/src/host/aaudio/mod.rs index cb53d7a..5f9c05e 100644 --- a/native/vendor/cpal-0.18.2/src/host/aaudio/mod.rs +++ b/native/vendor/cpal-0.18.2/src/host/aaudio/mod.rs @@ -31,6 +31,7 @@ mod convert; mod device_catalog; mod capture_timing; mod java_interface; +mod output_usage; use convert::{input_stream_instant, now_stream_instant, output_stream_instant}; use java_interface::{AudioDeviceInfo, AudioDeviceType as AndroidDeviceType, AudioManager}; @@ -118,7 +119,32 @@ const DEFAULT_TIMEOUT_NANOS: i64 = 2_000_000_000; pub struct Host; #[derive(Clone, Debug)] -pub struct Device(Option); +pub struct Device(Option, bool); + +impl Device { + /// Prepare a voice or independent-media output without opening hardware. + /// + /// Exact endpoints stay exact. When Android can identify a single current + /// default endpoint for these attributes, pin it before opening the stream. + /// `false` means this platform cannot resolve the default identity in advance. + /// Voice-follow notifications should reuse a prepared voice stream. + pub fn prepare_output(&mut self, voice: bool) -> Result { + self.1 = voice; + if self.0.is_some() { return Ok(true); } + let communication = output_usage::policy_for(voice) == output_usage::OutputPolicy::VoiceCommunication; + let Some(id) = AudioManager::output_device_id(communication) + .map_err(|message| Error::with_message(ErrorKind::BackendError, message))? else { + return Ok(false); + }; + let info = AudioDeviceInfo::request(DeviceDirection::Output) + .map_err(|message| Error::with_message(ErrorKind::BackendError, message))? + .into_iter().find(|info| info.id == id) + .ok_or_else(|| Error::with_message(ErrorKind::DeviceNotAvailable, + "Android's current output endpoint disappeared; no replacement was selected"))?; + self.0 = Some(info); + Ok(true) + } +} /// Stream wraps AudioStream in Arc> to provide Send + Sync semantics. /// @@ -139,6 +165,26 @@ pub struct Device(Option); pub struct Stream { inner: Arc>, direction: DeviceDirection, + device_id: i32, +} + +impl Stream { + /// The physical endpoint actually opened by AAudio, not a default-role alias. + pub fn device_id(&self) -> Result { + let stream = self.inner.lock().map_err(|_| { + Error::with_message(ErrorKind::StreamInvalidated, "Stream lock poisoned") + })?; + if stream.device_id() != self.device_id { + return Err(Error::with_message(ErrorKind::DeviceChanged, + "AAudio changed the opened endpoint; its selection was preserved")); + } + let id = self.device_id; + if id <= 0 { + return Err(Error::with_message(ErrorKind::DeviceNotAvailable, + "AAudio did not identify its opened physical endpoint")); + } + Ok(DeviceId::new(crate::platform::HostId::AAudio, id.to_string())) + } } // SAFETY: AudioStream can be safely sent between threads. The AAudio C API is thread-safe @@ -153,6 +199,14 @@ unsafe impl Sync for Stream {} crate::assert_stream_send!(Stream); crate::assert_stream_sync!(Stream); +// Share the post-open identity with callbacks using the existing xrun allocation. +// In particular, the first callback must not adopt a route changed after open. +#[derive(Default)] +struct CallbackContract { + device_id: AtomicI32, + last_xrun_count: AtomicI32, +} + /// State for dynamic buffer tuning on output streams. #[derive(Default)] struct BufferTuningState { @@ -182,20 +236,20 @@ impl HostTrait for Host { if let Ok(devices) = AudioDeviceInfo::request(DeviceDirection::Duplex) { Ok(devices .into_iter() - .map(|d| Device(Some(d))) + .map(|d| Device(Some(d), false)) .collect::>() .into_iter()) } else { - Ok(vec![Device(None)].into_iter()) + Ok(vec![Device(None, false)].into_iter()) } } fn default_input_device(&self) -> Option { - Some(Device(None)) + Some(Device(None, false)) } fn default_output_device(&self) -> Option { - Some(Device(None)) + Some(Device(None, false)) } } @@ -315,6 +369,51 @@ fn validate_stream_device(device: &Device, stream: &AudioStream) -> Result<(), E } } +// Never construct a typed PCM slice from the requested shape if AAudio opened +// (or subsequently reports) a different one. +fn stream_shape_matches(stream: &AudioStream, config: StreamConfig, format: SampleFormat) -> bool { + let format_matches = matches!( + (format, stream.format()), + (SampleFormat::I16, ndk::audio::AudioFormat::PCM_I16) + | (SampleFormat::F32, ndk::audio::AudioFormat::PCM_Float) + ); + format_matches + && stream.channel_count() == i32::from(config.channels) + && stream.sample_rate() == config.sample_rate as i32 +} + +fn validate_callback_stream( + stream: &AudioStream, + device_id: i32, + config: StreamConfig, + format: SampleFormat, +) -> Result<(), Error> { + if stream.device_id() != device_id { + return Err(Error::with_message( + ErrorKind::DeviceChanged, + "AAudio changed the opened endpoint; its selection was preserved", + )); + } + if !stream_shape_matches(stream, config, format) { + return Err(Error::with_message( + ErrorKind::StreamInvalidated, + "AAudio changed the opened PCM format, channel count or sample rate", + )); + } + Ok(()) +} + +fn validate_opened_shape(stream: &AudioStream, config: StreamConfig, format: SampleFormat) -> Result<(), Error> { + if stream_shape_matches(stream, config, format) { + Ok(()) + } else { + Err(Error::with_message( + ErrorKind::UnsupportedConfig, + "AAudio did not open the requested PCM format, channel count and sample rate", + )) + } +} + fn build_input_stream( device: &Device, config: StreamConfig, @@ -336,7 +435,8 @@ where let error_callback_for_stream = error_callback.clone(); let error_callback_for_data = error_callback.clone(); let error_callback_for_xrun = error_callback.clone(); - let last_xrun_count = Arc::new(AtomicI32::new(0)); + let contract = Arc::new(CallbackContract::default()); + let callback_contract = contract.clone(); #[cfg(feature = "realtime")] let mut rt_checked = false; @@ -345,6 +445,11 @@ where let stream = builder .data_callback(Box::new(move |stream, data, num_frames| { + let endpoint = callback_contract.device_id.load(Ordering::Acquire); + if let Err(error) = validate_callback_stream(stream, endpoint, config, sample_format) { + emit_error(&error_callback_for_data, error); + return ndk::audio::AudioCallbackResult::Stop; + } #[cfg(feature = "realtime")] if !rt_checked { if stream.performance_mode() != ndk::audio::AudioPerformanceMode::LowLatency { @@ -362,7 +467,7 @@ where } let xrun_count = stream.x_run_count(); - if last_xrun_count.swap(xrun_count, Ordering::Relaxed) < xrun_count { + if callback_contract.last_xrun_count.swap(xrun_count, Ordering::Relaxed) < xrun_count { let _ = try_emit_error(&error_callback_for_xrun, ErrorKind::Xrun.into()); } @@ -410,6 +515,9 @@ where })) .open_stream()?; validate_stream_device(device, &stream)?; + validate_opened_shape(&stream, config, sample_format)?; + let endpoint = device.0.as_ref().map(|info| info.id).unwrap_or_else(|| stream.device_id()); + contract.device_id.store(endpoint, Ordering::Release); // SAFETY: Stream implements Send + Sync (see unsafe impl below). Arc> // is safe because the Mutex provides exclusive access and AudioStream's thread safety @@ -418,6 +526,7 @@ where Ok(Stream { inner: Arc::new(Mutex::new(stream)), direction: DeviceDirection::Input, + device_id: endpoint, }) } @@ -434,6 +543,7 @@ where E: FnMut(Error) + Send + 'static, { let builder = configure_for_device(builder, device, config); + let (builder, output_policy) = output_usage::configure(builder, device.1); let channel_count = config.channels as i32; let sample_rate = config.sample_rate; let tune_dynamically = config.buffer_size == BufferSize::Default; @@ -445,7 +555,8 @@ where let error_callback_for_stream = error_callback.clone(); let error_callback_for_data = error_callback.clone(); let error_callback_for_xrun = error_callback.clone(); - let last_xrun_count = Arc::new(AtomicI32::new(0)); + let contract = Arc::new(CallbackContract::default()); + let callback_contract = contract.clone(); #[cfg(feature = "realtime")] let mut rt_checked = false; @@ -454,6 +565,11 @@ where let stream = builder .data_callback(Box::new(move |stream, data, num_frames| { + let endpoint = callback_contract.device_id.load(Ordering::Acquire); + if let Err(error) = validate_callback_stream(stream, endpoint, config, sample_format) { + emit_error(&error_callback_for_data, error); + return ndk::audio::AudioCallbackResult::Stop; + } #[cfg(feature = "realtime")] if !rt_checked { if stream.performance_mode() != ndk::audio::AudioPerformanceMode::LowLatency { @@ -471,7 +587,7 @@ where } let xrun_count = stream.x_run_count(); - if last_xrun_count.swap(xrun_count, Ordering::Relaxed) < xrun_count { + if callback_contract.last_xrun_count.swap(xrun_count, Ordering::Relaxed) < xrun_count { let _ = try_emit_error(&error_callback_for_xrun, ErrorKind::Xrun.into()); } @@ -560,7 +676,24 @@ where emit_error(&error_callback_for_stream, Error::from(error)); })) .open_stream()?; + let (usage, content_type) = output_usage::observe(&stream); + let route_matches = device_catalog::accepts_route(device.0.as_ref().map(|info| info.id), stream.device_id()); + let shape_matches = stream_shape_matches(&stream, config, sample_format); + let policy_matches = output_usage::attributes_match(output_policy, usage, content_type); + eprintln!( + "audio_stream platform=aaudio direction=Output policy={output_policy:?} usage={usage:?} content_type={content_type:?} format={:?} channels={} sample_rate={} route_matches={route_matches} shape_matches={shape_matches} policy_matches={policy_matches}", + stream.format(), stream.channel_count(), stream.sample_rate(), + ); validate_stream_device(device, &stream)?; + validate_opened_shape(&stream, config, sample_format)?; + let endpoint = device.0.as_ref().map(|info| info.id).unwrap_or_else(|| stream.device_id()); + contract.device_id.store(endpoint, Ordering::Release); + if !policy_matches { + return Err(Error::with_message( + ErrorKind::UnsupportedConfig, + "AAudio did not open the requested voice communication output attributes", + )); + } // After stream opens, query and cache the values let capacity = stream.buffer_capacity_in_frames(); @@ -586,6 +719,7 @@ where Ok(Stream { inner: Arc::new(Mutex::new(stream)), direction: DeviceDirection::Output, + device_id: endpoint, }) } diff --git a/native/vendor/cpal-0.18.2/src/host/aaudio/output_usage.rs b/native/vendor/cpal-0.18.2/src/host/aaudio/output_usage.rs new file mode 100644 index 0000000..a2dbb05 --- /dev/null +++ b/native/vendor/cpal-0.18.2/src/host/aaudio/output_usage.rs @@ -0,0 +1,211 @@ +// AAudio's usage/content-type API was added in Android 28. Keep the API26 +// baseline link-safe by resolving optional functions rather than enabling ndk's +// api-level-28 feature. No Android imports are compiled into host policy tests. + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum OutputPolicy { + MediaDefault, + VoiceCommunication, + LegacyMediaDefault, +} + +pub(crate) fn choose_policy(voice: bool, attribute_api_available: bool) -> OutputPolicy { + match (voice, attribute_api_available) { + (false, _) => OutputPolicy::MediaDefault, + (true, true) => OutputPolicy::VoiceCommunication, + (true, false) => OutputPolicy::LegacyMediaDefault, + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Usage { + Unavailable, + Media, + VoiceCommunication, + Other(i32), +} + +impl From> for Usage { + fn from(value: Option) -> Self { + match value { + None => Self::Unavailable, + Some(1) => Self::Media, + Some(2) => Self::VoiceCommunication, + Some(other) => Self::Other(other), + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum ContentType { + Unavailable, + Speech, + Music, + Other(i32), +} + +impl From> for ContentType { + fn from(value: Option) -> Self { + match value { + None => Self::Unavailable, + Some(1) => Self::Speech, + Some(2) => Self::Music, + Some(other) => Self::Other(other), + } + } +} + +pub(crate) fn attributes_match(policy: OutputPolicy, usage: Usage, content: ContentType) -> bool { + policy != OutputPolicy::VoiceCommunication + || (usage == Usage::VoiceCommunication && content == ContentType::Speech) +} + +// Require the complete pair of setters and getters before changing anything. +// This also makes a partially exposed vendor implementation observable as legacy +// rather than claiming an unverified communication policy. +pub(crate) fn attribute_api_available(symbols: [bool; 4]) -> bool { + symbols.into_iter().all(|present| present) +} + +#[cfg(target_os = "android")] +mod android { + use super::{attribute_api_available, choose_policy, ContentType, OutputPolicy, Usage}; + use ndk::audio::{AudioStream, AudioStreamBuilder}; + use std::{ffi::{c_void, CStr}, sync::LazyLock}; + + // The C ABI uses opaque AAudioStreamBuilder*/AAudioStream* plus int32_t. + // Raw pointers are borrowed from ndk's documented accessors, never inferred + // from a private Rust struct layout. Values are stable NDK AudioAttributes + // constants: VOICE_COMMUNICATION=2, SPEECH=1. + type SetAttribute = unsafe extern "C" fn(*mut c_void, i32); + type GetAttribute = unsafe extern "C" fn(*mut c_void) -> i32; + + struct AttributeApi { + set_usage: Option, + set_content: Option, + get_usage: Option, + get_content: Option, + } + + impl AttributeApi { + fn load() -> Self { + // AAudio itself is available from API26. Keep this dlopen reference + // for process lifetime: the cached function pointers must stay valid. + let library = unsafe { libc::dlopen(c"libaaudio.so".as_ptr(), libc::RTLD_NOW | libc::RTLD_LOCAL) }; + let symbol = |name: &CStr| { + if library.is_null() { + std::ptr::null_mut() + } else { + // SAFETY: library is a live dlopen handle; name is terminated. + unsafe { libc::dlsym(library, name.as_ptr()) } + } + }; + let setter = |name| { + let pointer = symbol(name); + if pointer.is_null() { None } else { + // SAFETY: these symbols have the documented setter C ABI. + Some(unsafe { std::mem::transmute::<*mut c_void, SetAttribute>(pointer) }) + } + }; + let getter = |name| { + let pointer = symbol(name); + if pointer.is_null() { None } else { + // SAFETY: these symbols have the documented getter C ABI. + Some(unsafe { std::mem::transmute::<*mut c_void, GetAttribute>(pointer) }) + } + }; + Self { + set_usage: setter(c"AAudioStreamBuilder_setUsage"), + set_content: setter(c"AAudioStreamBuilder_setContentType"), + get_usage: getter(c"AAudioStream_getUsage"), + get_content: getter(c"AAudioStream_getContentType"), + } + } + + fn available(&self) -> bool { + attribute_api_available([ + self.set_usage.is_some(), self.set_content.is_some(), + self.get_usage.is_some(), self.get_content.is_some(), + ]) + } + } + + static API: LazyLock = LazyLock::new(AttributeApi::load); + + pub(crate) fn policy_for(voice: bool) -> OutputPolicy { + choose_policy(voice, API.available()) + } + + pub(crate) fn configure(builder: AudioStreamBuilder, voice: bool) -> (AudioStreamBuilder, OutputPolicy) { + let api = &*API; + let policy = policy_for(voice); + if let (OutputPolicy::VoiceCommunication, Some(set_usage), Some(set_content)) = + (policy, api.set_usage, api.set_content) + { + // SAFETY: ndk owns a live, unopened builder throughout these calls. + unsafe { + set_usage(builder.as_ptr().cast(), 2); + set_content(builder.as_ptr().cast(), 1); + } + } + (builder, policy) + } + + pub(crate) fn observe(stream: &AudioStream) -> (Usage, ContentType) { + let api = &*API; + // SAFETY: ndk owns a live stream throughout these getter calls. + let usage = api.get_usage.map(|get| unsafe { get(stream.as_ptr().cast()) }); + let content = api.get_content.map(|get| unsafe { get(stream.as_ptr().cast()) }); + (usage.into(), content.into()) + } +} + +#[cfg(target_os = "android")] +pub(crate) use android::{configure, observe, policy_for}; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn independent_media_never_acquires_communication_policy() { + for available in [false, true] { + assert_eq!(choose_policy(false, available), OutputPolicy::MediaDefault); + } + } + + #[test] + fn missing_api28_symbols_preserve_explicit_legacy_policy() { + assert_eq!(choose_policy(true, false), OutputPolicy::LegacyMediaDefault); + assert_eq!(choose_policy(true, true), OutputPolicy::VoiceCommunication); + for missing in 0..4 { + let mut symbols = [true; 4]; + symbols[missing] = false; + assert!(!attribute_api_available(symbols)); + } + assert!(!attribute_api_available([false; 4])); + assert!(attribute_api_available([true; 4])); + } + + #[test] + fn actual_attributes_must_match_the_applied_voice_policy() { + let voice = OutputPolicy::VoiceCommunication; + assert!(attributes_match(voice, Usage::VoiceCommunication, ContentType::Speech)); + assert!(!attributes_match(voice, Usage::Media, ContentType::Speech)); + assert!(!attributes_match(voice, Usage::VoiceCommunication, ContentType::Music)); + assert!(!attributes_match(voice, Usage::Unavailable, ContentType::Unavailable)); + assert!(attributes_match(OutputPolicy::LegacyMediaDefault, Usage::Unavailable, ContentType::Unavailable)); + } + + #[test] + fn diagnostics_preserve_unknown_values_and_api_absence() { + assert_eq!(Usage::from(None), Usage::Unavailable); + assert_eq!(Usage::from(Some(1)), Usage::Media); + assert_eq!(Usage::from(Some(2)), Usage::VoiceCommunication); + assert_eq!(Usage::from(Some(1234)), Usage::Other(1234)); + assert_eq!(ContentType::from(None), ContentType::Unavailable); + assert_eq!(ContentType::from(Some(1)), ContentType::Speech); + assert_eq!(ContentType::from(Some(2)), ContentType::Music); + assert_eq!(ContentType::from(Some(1234)), ContentType::Other(1234)); + } +} diff --git a/native/vendor/ndk-0.9.0/.cargo_vcs_info.json b/native/vendor/ndk-0.9.0/.cargo_vcs_info.json new file mode 100644 index 0000000..4e57d9e --- /dev/null +++ b/native/vendor/ndk-0.9.0/.cargo_vcs_info.json @@ -0,0 +1,6 @@ +{ + "git": { + "sha1": "49bbbba16c58ff63cb8a0ad0eca5a9fb7ecaec25" + }, + "path_in_vcs": "ndk" +} \ No newline at end of file diff --git a/native/vendor/ndk-0.9.0/.gitignore b/native/vendor/ndk-0.9.0/.gitignore new file mode 100644 index 0000000..6936990 --- /dev/null +++ b/native/vendor/ndk-0.9.0/.gitignore @@ -0,0 +1,3 @@ +/target +**/*.rs.bk +Cargo.lock diff --git a/native/vendor/ndk-0.9.0/CHANGELOG.md b/native/vendor/ndk-0.9.0/CHANGELOG.md new file mode 100644 index 0000000..e4dc75d --- /dev/null +++ b/native/vendor/ndk-0.9.0/CHANGELOG.md @@ -0,0 +1,137 @@ +# Unreleased + +# 0.9.0 (2024-04-26) + +- Move `MediaFormat` from `media::media_codec` to its own `media::media_format` module. (#442) +- media_format: Expose `MediaFormat::copy()` and `MediaFormat::clear()` from API level 29. (#449) +- **Breaking:** media_format: Mark all `fn set_*()` and `fn str()` as taking `self` by `&mut`. (#452) +- **Breaking:** Require all `dyn Fn*` types to implement `Send` when the FFI implementation invokes them on a separate thread: (#455) + - `audio::AudioStreamDataCallback`; + - `audio::AudioStreamErrorCallback`; + - `media::image_reader::BufferRemovedListener`; + - `media::image_reader::ImageListener`; + - `media::media_codec::ErrorCallback`; + - `media::media_codec::FormatChangedCallback`; + - `media::media_codec::InputAvailableCallback`; + - `media::media_codec::OutputAvailableCallback`. +- Drop previous `Box`ed callbacks _after_ registering new ones, instead of before. (#455) +- input_queue: Add `from_java()` constructor, available since API level 33. (#456) +- event: Add `from_java()` constructors to `KeyEvent` and `MotionEvent`, available since API level 31. (#456) +- **Breaking:** image_reader: Special-case return statuses in `Image`-acquire functions. (#457) +- **Breaking:** image_reader: Mark `ImageReader::acquire_latest_image_async()` `unsafe` to match the safety requirements on `ImageReader::acquire_next_image_async()`. (#457) +- event: Implement `SourceClass` `bitflag` and provide `Source::class()` getter. (#458) +- Ensure all `bitflags` implementations consider all (including unknown) bits in negation and `all()`. (#458) +- **Breaking:** Mark all enums as `non_exhaustive` and fix `repr` types. (#459) +- **Breaking:** native_window: Remove redundant `TRANSFORM_` prefix from `NativeWindowTransform` variants. (#460) +- **Breaking:** hardware_buffer: Convert `HardwareBufferUsage` to `bitflags`. (#461) +- bitmap: Guard `BitmapCompressError` behind missing `api-level-30` feature. (#462) +- native_window: Require linking against `libnativewindow` for most API >= 26 functions. (#465) +- **Breaking:** audio: Merge `AudioResult` variant enum into `AudioError`. (#467) +- data_space: Add missing `DataSpaceRange::Unspecified` variant. (#468) +- **Breaking:** looper: Require `Send` marker when adding fd event callbacks on `ForeignLooper`. (#469) +- **Breaking:** Upgrade to [`ndk-sys 0.6.0`](../ndk-sys/CHANGELOG.md#060-2024-04-26). (#472) + +# 0.8.0 (2023-10-15) + +- event: Add `tool_type` getter for `Pointer`. (#323) +- input_queue: Allow any non-zero return code from `pre_dispatch()` again, as per documentation. (#325) +- asset: Use entire asset length when mapping buffer. (#387) +- Bump MSRV to 1.66 for `raw-window-handle 0.5.1`, `num_enum`'s `catch_all` with arbitrary enum discriminants. (#388, #431) +- Bump optional `jni` dependency for doctest example from `0.19` to `0.21`. (#390) +- **Breaking:** Upgrade to [`ndk-sys 0.5.0`](../ndk-sys/CHANGELOG.md#050-2023-10-15). (#370) +- **Breaking:** Upgrade `bitflags` crate from `1` to `2`. (#394) +- bitmap: Add `try_format()` to `AndroidBitmapInfo` to handle unexpected formats without panicking. (#395) +- Add `Font` bindings. (#397) +- **Breaking:** Upgrade `num_enum` crate from `0.5.1` to `0.7`. (#398, #419) +- **Breaking:** Renamed, moved and flattened "`media`" error types and helpers to a new `media_error` module. (#399, #432) +- **Breaking:** media_codec: Wrap common dequeued-buffer status codes in enum. (#401) +- **Breaking:** media_codec: Return `MaybeUninit` bytes in `buffer_mut()`. (#403) +- native_window: Add `lock()` to blit raw pixel data. (#404) +- hardware_buffer_format: Add `YCbCr_P010` and `R8_UNORM` variants. (#405) +- **Breaking:** hardware_buffer_format: Add catch-all variant. (#407) +- asset: Add missing `is_allocated()` and `open_file_descriptor()` methods. (#409) +- **Breaking:** media_codec: Add support for asynchronous notification callbacks. (#410) +- Add panic guards to callbacks. (#412) +- looper: Add `remove_fd()` to unregister events/callbacks for a file descriptor. (#416) +- **Breaking:** Use `BorrowedFd` and `OwnedFd` to clarify possible ownership transitions. (#417) +- **Breaking:** Upgrade to [`ndk-sys 0.5.0`](../ndk-sys/CHANGELOG.md#050-2023-10-15). (#420) +- Add bindings for `sync.h`. (#423) +- **Breaking:** bitmap: Provide detailed implementation for `AndroidBitmapInfoFlags`. (#424) +- native_window: Add `set_buffers_transform()`, `try_allocate_buffers()` and `set_frame_rate*()`. (#425) +- Add bindings for `ASharedMemory`. (#427) +- hardware_buffer: Add `id()` to retrieve a system-wide unique identifier for a `HardwareBuffer`. (#428) +- **Breaking:** bitmap: Strip `Android` prefix from structs and enums, and `Bitmap` from `Result`. (#430) +- **Breaking:** `raw-window-handle 0.5` support is now behind an _optional_ `rwh_05` crate feature and `raw-window-handle` `0.4` and `0.6` support is provided via the new `rwh_04` and (default-enabled) `rwh_06` crate features. (#434) +- **Breaking:** looper: Provide `event` value to file descriptor poll callback. (#435) +- **Breaking:** `HardwareBufferFormat` is no longer exported from `hardware_buffer` and `native_window`, and can only be reached through the `hardware_buffer_format` module. (#436) +- **Breaking:** `get_` prefixes have been removed from all public functions in light of the [C-GETTER](https://rust-lang.github.io/api-guidelines/naming.html#getter-names-follow-rust-convention-c-getter) convention. (#437) +- Add `DataSpace` type and relevant functions on `Bitmap` and `NativeWindow`. (#438) +- bitmap: Add `Bitmap::compress()` and `Bitmap::compress_raw()` functions. (#440) +- **Breaking:** Turn `BitmapError` into a `non_exhaustive` `enum`. (#440) +- **Breaking:** audio: Rename `AudioErrorResult` to `AudioResult` and turn into a `non_exhaustive` `enum`. (#441) + +# 0.7.0 (2022-07-24) + +- hardware_buffer: Make `HardwareBuffer::as_ptr()` public for interop with Vulkan. (#213) +- **Breaking:** `Configuration::country()` now returns `None` when the country is unset (akin to `Configuration::language()`). (#220) +- Add `MediaCodec` and `MediaFormat` bindings. (#216) +- **Breaking:** Upgrade to [`ndk-sys 0.4.0`](../ndk-sys/CHANGELOG.md#040-2022-07-24) and use new `enum` newtype wrappers. (#245) +- native_window: Use `release`/`acquire` for `Drop` and `Clone` respectively. (#207) +- **Breaking:** audio: Rename from `aaudio` to `audio` and drop `A` prefix. (#273) +- Implement `HasRawWindowHandle` directly on `NativeWindow`. (#274, #319) +- **Breaking:** native_activity: Replace `CStr` return types with `Path`. (#279) +- native_window: Add `format()` getter and `set_buffers_geometry()` setter. (#276) +- native_activity: Add `set_window_format()` setter. (#277) +- native_activity: Add `set_window_flags()` to change window behavior. (#278) +- Add `SurfaceTexture` bindings. (#267) +- Improve library and structure documentation, linking back to the NDK docs more rigorously. (#290) +- **Breaking:** input_queue: `get_event()` now returns a `Result` with `std::io::Error`; `InputQueueError` has been removed. (#292) +- **Breaking:** input_queue: `has_events()` now returns a `bool` directly without being wrapped in `Result`. (#294) +- **Breaking:** hardware_buffer: `HardwareBufferError` has been removed and replaced with `std::io::Error` in return types. (#295) +- Fixed `HardwareBuffer` leak on buffers returned from `AndroidBitmap::get_hardware_buffer()`. (#296) +- Bump optional `jni` dependency for doctest example from `0.18` to `0.19`. (#300) +- hardware_buffer: Made `HardwareBufferDesc` fields `pub`. (#313) +- **Breaking:** Remove `hardware_buffer` and `trace` features in favour of using `api-level-26` or `api-level-23` directly. (#320) + +# 0.6.0 (2022-01-05) + +- **Breaking:** Upgrade to [`ndk-sys 0.3.0`](../ndk-sys/CHANGELOG.md#030-2022-01-05) and migrate to `jni-sys` types that it now directly uses in its bindings. (#209 / #214) + +# 0.5.0 (2021-11-22) + +- **Breaking:** Replace `add_fd_with_callback` `ident` with constant value `ALOOPER_POLL_CALLBACK`, + as per . +- **Breaking:** Accept unboxed closure in `add_fd_with_callback`. +- aaudio: Replace "Added in" comments with missing `#[cfg(feature)]`. +- aaudio: Add missing `fn get_allowed_capture_policy()`. +- configuration: Add missing `api-level-30` feature to `fn screen_round()`. + +# 0.4.0 (2021-08-02) + +- **Breaking:** Model looper file descriptor events integer as `bitflags`. + +# 0.3.0 (2021-01-30) + +- **Breaking:** Looper `ident` not passed in `data` pointer anymore. + `attach_looper` now only sets the `ident` field when attaching an + `InputQueue` to a `ForeignLooper`. + If you are relying on `Poll::Event::data` to tell event fd and + input queue apart, please use `Poll::Event::ident` and the new + constants introduced in `ndk-glue`! + +# 0.2.1 (2020-10-15) + +- Fix documentation build on docs.rs + +# 0.2.0 (2020-09-15) + +- **Breaking:** Updated to use [ndk-sys 0.2.0](../ndk-sys/CHANGELOG.md#020-2020-09-15) +- Added `media` bindings +- Added `bitmap` and `hardware_buffer` bindings +- Added `aaudio` bindings +- Fixed assets directory path to be relative to the manifest +- Added `trace` feature for native tracing + +# 0.1.0 (2020-04-22) + +- Initial release! 🎉 diff --git a/native/vendor/ndk-0.9.0/Cargo.toml b/native/vendor/ndk-0.9.0/Cargo.toml new file mode 100644 index 0000000..fd8ea26 --- /dev/null +++ b/native/vendor/ndk-0.9.0/Cargo.toml @@ -0,0 +1,125 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2021" +rust-version = "1.66" +name = "ndk" +version = "0.9.0" +authors = ["The Rust Mobile contributors"] +description = "Safe Rust bindings to the Android NDK" +homepage = "https://github.com/rust-mobile/ndk" +documentation = "https://docs.rs/ndk" +readme = "README.md" +keywords = [ + "android", + "ndk", +] +license = "MIT OR Apache-2.0" +repository = "https://github.com/rust-mobile/ndk" + +[package.metadata.docs.rs] +features = [ + "jni", + "all", +] +rustdoc-args = [ + "--cfg", + "docsrs", +] +targets = [ + "aarch64-linux-android", + "armv7-linux-androideabi", + "i686-linux-android", + "x86_64-linux-android", +] + +[dependencies.bitflags] +version = "2.4" + +[dependencies.ffi] +version = "0.6.0" +package = "ndk-sys" + +[dependencies.jni] +version = "0.21" +optional = true + +[dependencies.jni-sys] +version = "0.3" + +[dependencies.log] +version = "0.4.6" + +[dependencies.num_enum] +version = "0.7" + +[dependencies.rwh_04] +version = "0.4" +optional = true +package = "raw-window-handle" + +[dependencies.rwh_05] +version = "0.5" +optional = true +package = "raw-window-handle" + +[dependencies.rwh_06] +version = "0.6" +optional = true +package = "raw-window-handle" + +[dependencies.thiserror] +version = "1.0.23" + +[dev-dependencies.libc] +version = "0.2.3" + +[features] +all = [ + "audio", + "bitmap", + "media", + "nativewindow", + "sync", + "api-level-33", + "rwh_04", + "rwh_05", + "rwh_06", +] +api-level-23 = [] +api-level-24 = ["api-level-23"] +api-level-25 = ["api-level-24"] +api-level-26 = ["api-level-25"] +api-level-27 = ["api-level-26"] +api-level-28 = ["api-level-27"] +api-level-29 = ["api-level-28"] +api-level-30 = ["api-level-29"] +api-level-31 = ["api-level-30"] +api-level-32 = ["api-level-31"] +api-level-33 = ["api-level-32"] +audio = [ + "ffi/audio", + "api-level-26", +] +bitmap = ["ffi/bitmap"] +default = ["rwh_06"] +media = ["ffi/media"] +nativewindow = ["ffi/nativewindow"] +sync = [ + "ffi/sync", + "api-level-26", +] +test = [ + "ffi/test", + "jni", + "all", +] diff --git a/native/vendor/ndk-0.9.0/Cargo.toml.orig b/native/vendor/ndk-0.9.0/Cargo.toml.orig new file mode 100644 index 0000000..553c135 --- /dev/null +++ b/native/vendor/ndk-0.9.0/Cargo.toml.orig @@ -0,0 +1,70 @@ +[package] +name = "ndk" +version = "0.9.0" +authors = ["The Rust Mobile contributors"] +edition = "2021" +description = "Safe Rust bindings to the Android NDK" +license = "MIT OR Apache-2.0" +keywords = ["android", "ndk"] +readme = "../README.md" +documentation = "https://docs.rs/ndk" +homepage = "https://github.com/rust-mobile/ndk" +repository = "https://github.com/rust-mobile/ndk" +rust-version = "1.66" + +[features] +default = ["rwh_06"] +all = ["audio", "bitmap", "media", "nativewindow", "sync", "api-level-33", "rwh_04", "rwh_05", "rwh_06"] + +audio = ["ffi/audio", "api-level-26"] +bitmap = ["ffi/bitmap"] +media = ["ffi/media"] +nativewindow = ["ffi/nativewindow"] +sync = ["ffi/sync", "api-level-26"] + +api-level-23 = [] +api-level-24 = ["api-level-23"] +api-level-25 = ["api-level-24"] +api-level-26 = ["api-level-25"] +api-level-27 = ["api-level-26"] +api-level-28 = ["api-level-27"] +api-level-29 = ["api-level-28"] +api-level-30 = ["api-level-29"] +api-level-31 = ["api-level-30"] +api-level-32 = ["api-level-31"] +api-level-33 = ["api-level-32"] + +test = ["ffi/test", "jni", "all"] + +[dependencies] +bitflags = "2.4" # At least 2.4.0 for `const _ = !0` +jni-sys = "0.3" +log = "0.4.6" +num_enum = "0.7" +rwh_04 = { package = "raw-window-handle", version = "0.4", optional = true } +rwh_05 = { package = "raw-window-handle", version = "0.5", optional = true } +rwh_06 = { package = "raw-window-handle", version = "0.6", optional = true } +thiserror = "1.0.23" + +[dependencies.jni] +version = "0.21" +optional = true + +[dependencies.ffi] +package = "ndk-sys" +path = "../ndk-sys" +version = "0.6.0" + +[dev-dependencies] +# Only for use in documentation and doc-tests +libc = "0.2.3" + +[package.metadata.docs.rs] +features = ["jni", "all"] +rustdoc-args = ["--cfg", "docsrs"] +targets = [ + "aarch64-linux-android", + "armv7-linux-androideabi", + "i686-linux-android", + "x86_64-linux-android", +] diff --git a/native/vendor/ndk-0.9.0/LICENSE-MIT b/native/vendor/ndk-0.9.0/LICENSE-MIT new file mode 100644 index 0000000..9cf1062 --- /dev/null +++ b/native/vendor/ndk-0.9.0/LICENSE-MIT @@ -0,0 +1,19 @@ +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/native/vendor/ndk-0.9.0/README.md b/native/vendor/ndk-0.9.0/README.md new file mode 100644 index 0000000..cf7d71c --- /dev/null +++ b/native/vendor/ndk-0.9.0/README.md @@ -0,0 +1,21 @@ +[![ci](https://github.com/rust-mobile/ndk/actions/workflows/rust.yml/badge.svg)](https://github.com/rust-mobile/ndk/actions/workflows/rust.yml) ![MIT license](https://img.shields.io/badge/License-MIT-green.svg) ![APACHE2 license](https://img.shields.io/badge/License-APACHE2-green.svg) + +Rust bindings to the [Android NDK](https://developer.android.com/ndk) + +Name | Description | Badges +--- | --- | --- +[`ndk-sys`](./ndk-sys) | Raw FFI bindings to the NDK | [![crates.io](https://img.shields.io/crates/v/ndk-sys.svg)](https://crates.io/crates/ndk-sys) [![Docs](https://docs.rs/ndk-sys/badge.svg)](https://docs.rs/ndk-sys) [![MSRV](https://img.shields.io/badge/rustc-1.60.0+-ab6000.svg)](https://blog.rust-lang.org/2022/04/07/Rust-1.60.0.html) +[`ndk`](./ndk) | Safe abstraction of the bindings | [![crates.io](https://img.shields.io/crates/v/ndk.svg)](https://crates.io/crates/ndk) [![Docs](https://docs.rs/ndk/badge.svg)](https://docs.rs/ndk) [![MSRV](https://img.shields.io/badge/rustc-1.64.0+-ab6000.svg)](https://blog.rust-lang.org/2022/09/22/Rust-1.64.0.html) + +See these [`ndk-examples`](https://github.com/rust-mobile/cargo-apk/tree/main/examples/examples) and these [`rust-android-examples`](https://github.com/rust-mobile/rust-android-examples) for examples using the NDK. + +> [!IMPORTANT] +> This repository was recently [modularized](https://github.com/rust-mobile/ndk/issues/372) and the following crates were split into separate repositories: +> +> Crate | New Location | Notes +> ------|--------------|------ +> ndk-context | https://github.com/rust-mobile/ndk-context | +> ndk-glue | https://github.com/rust-mobile/ndk-glue | ⛔ _deprecated_ - see [android-activity](https://github.com/rust-mobile/android-activity) +> ndk-macro | https://github.com/rust-mobile/ndk-glue | ⛔ _deprecated_ - see [android-activity](https://github.com/rust-mobile/android-activity) +> ndk-build | https://github.com/rust-mobile/cargo-apk | ⛔ _deprecated_ - see [xbuild](https://github.com/rust-mobile/xbuild) +> cargo-apk | https://github.com/rust-mobile/cargo-apk | ⛔ _deprecated_ - see [xbuild](https://github.com/rust-mobile/xbuild) diff --git a/native/vendor/ndk-0.9.0/src/asset.rs b/native/vendor/ndk-0.9.0/src/asset.rs new file mode 100644 index 0000000..4ae0c20 --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/asset.rs @@ -0,0 +1,323 @@ +//! Bindings for [`AAsset`], [`AAssetDir`] and [`AAssetManager`] +//! +//! [`AAsset`]: https://developer.android.com/ndk/reference/group/asset#aasset +//! [`AAssetDir`]: https://developer.android.com/ndk/reference/group/asset#aassetdir +//! [`AAssetManager`]: https://developer.android.com/ndk/reference/group/asset#aassetmanager + +use std::{ + ffi::{CStr, CString}, + io, + os::fd::{FromRawFd, OwnedFd}, + ptr::NonNull, +}; + +/// A native [`AAssetManager *`] +/// +/// [`AAssetManager *`]: https://developer.android.com/ndk/reference/group/asset#aassetmanager +#[derive(Debug)] +#[doc(alias = "AAssetManager")] +pub struct AssetManager { + ptr: NonNull, +} + +// AAssetManager is thread safe. +// See https://developer.android.com/ndk/reference/group/asset#aassetmanager +unsafe impl Send for AssetManager {} +unsafe impl Sync for AssetManager {} + +impl AssetManager { + /// Create an `AssetManager` from a pointer + /// + /// # Safety + /// By calling this function, you assert that the pointer is a valid pointer to a native + /// `AAssetManager`. + pub unsafe fn from_ptr(ptr: NonNull) -> Self { + Self { ptr } + } + + /// Returns the pointer to the native `AAssetManager`. + pub fn ptr(&self) -> NonNull { + self.ptr + } + + /// Open the asset. Returns [`None`] if opening the asset fails. + /// + /// This currently always opens the asset in the streaming mode. + #[doc(alias = "AAssetManager_open")] + pub fn open(&self, filename: &CStr) -> Option { + unsafe { + let ptr = ffi::AAssetManager_open( + self.ptr.as_ptr(), + filename.as_ptr(), + ffi::AASSET_MODE_STREAMING as i32, + ); + Some(Asset::from_ptr(NonNull::new(ptr)?)) + } + } + + /// Open an asset directory. Returns [`None`] if opening the directory fails. + #[doc(alias = "AAssetManager_openDir")] + pub fn open_dir(&self, filename: &CStr) -> Option { + unsafe { + let ptr = ffi::AAssetManager_openDir(self.ptr.as_ptr(), filename.as_ptr()); + Some(AssetDir::from_ptr(NonNull::new(ptr)?)) + } + } +} + +/// A native [`AAssetDir *`] +/// +/// ```no_run +/// # use std::ffi::CString; +/// # use ndk::asset::AssetManager; +/// # let asset_manager: AssetManager = unimplemented!(); +/// use std::io::Read; +/// +/// let mut my_dir = asset_manager +/// .open_dir(&CString::new("my_dir").unwrap()) +/// .expect("Could not open directory"); +/// +/// // Use it as an iterator +/// let all_files = my_dir.collect::>(); +/// +/// // Reset the iterator +/// my_dir.rewind(); +/// +/// // Use .with_next() to iterate without allocating `CString`s +/// while let Some(asset) = my_dir.with_next(|cstr| asset_manager.open(cstr).unwrap()) { +/// let mut text = String::new(); +/// asset.read_to_string(&mut text); +/// // ... +/// } +/// ``` +/// +/// [`AAssetDir *`]: https://developer.android.com/ndk/reference/group/asset#aassetdir +#[derive(Debug)] +#[doc(alias = "AAssetDir")] +pub struct AssetDir { + ptr: NonNull, +} + +// It's unclear if AAssetDir is thread safe. +// However, AAsset is not, so there's a good chance that AAssetDir is not either. + +impl Drop for AssetDir { + #[doc(alias = "AAssetDir_close")] + fn drop(&mut self) { + unsafe { ffi::AAssetDir_close(self.ptr.as_ptr()) } + } +} + +impl AssetDir { + /// Construct an `AssetDir` from the native `AAssetDir *`. This gives ownership of the + /// `AAssetDir *` to the `AssetDir`, which will handle closing the asset. Avoid using + /// the pointer after calling this function. + /// + /// # Safety + /// By calling this function, you assert that it points to a valid native `AAssetDir`. + pub unsafe fn from_ptr(ptr: NonNull) -> Self { + Self { ptr } + } + + /// The corresponding native `AAssetDir *` + pub fn ptr(&self) -> NonNull { + self.ptr + } + + /// Get the next filename, if any, and process it. Like [`Iterator::next()`], but performs + /// no additional allocation. + /// + /// The filenames are in the correct format to be passed to [`AssetManager::open()`]. + #[doc(alias = "AAssetDir_getNextFileName")] + pub fn with_next(&mut self, f: impl for<'a> FnOnce(&'a CStr) -> T) -> Option { + unsafe { + let next_name = ffi::AAssetDir_getNextFileName(self.ptr.as_ptr()); + if next_name.is_null() { + None + } else { + Some(f(CStr::from_ptr(next_name))) + } + } + } + + /// Reset the iteration state + #[doc(alias = "AAssetDir_rewind")] + pub fn rewind(&mut self) { + unsafe { + ffi::AAssetDir_rewind(self.ptr.as_ptr()); + } + } +} + +impl Iterator for AssetDir { + type Item = CString; + + fn next(&mut self) -> Option { + self.with_next(|cstr| cstr.to_owned()) + } +} + +/// A native [`AAsset *`], opened in streaming mode +/// +/// ```no_run +/// # use std::ffi::CString; +/// # use ndk::asset::AssetManager; +/// # let asset_manager: AssetManager = unimplemented!(); +/// use std::io::Read; +/// +/// let asset = asset_manager +/// .open(&CString::new("path/to/asset").unwrap()) +/// .expect("Could not open asset"); +/// +/// let mut data = vec![]; +/// asset.read_to_end(&mut data); +/// // ... use data ... +/// ``` +/// +/// [`AAsset *`]: https://developer.android.com/ndk/reference/group/asset#aasset +#[derive(Debug)] +#[doc(alias = "AAsset")] +pub struct Asset { + ptr: NonNull, +} + +// AAsset is *not* thread safe. +// See https://developer.android.com/ndk/reference/group/asset#aasset + +impl Drop for Asset { + #[doc(alias = "AAsset_close")] + fn drop(&mut self) { + unsafe { ffi::AAsset_close(self.ptr.as_ptr()) } + } +} + +impl Asset { + /// Construct an `Asset` from the native `AAsset *`. This gives ownership of the `AAsset *` to + /// the `Asset`, which will handle closing the asset. Avoid using the pointer after calling + /// this function. + /// + /// # Safety + /// By calling this function, you assert that it points to a valid native `AAsset`, open + /// in the streaming mode. + pub unsafe fn from_ptr(ptr: NonNull) -> Self { + Self { ptr } + } + + /// The corresponding native `AAsset *` + pub fn ptr(&self) -> NonNull { + self.ptr + } + + /// Returns the total length of the asset, in bytes + #[doc(alias = "AAsset_getLength64")] + pub fn length(&self) -> usize { + unsafe { ffi::AAsset_getLength64(self.ptr.as_ptr()) as usize } + } + + /// Returns the remaining length of the asset, in bytes + #[doc(alias = "AAsset_getRemainingLength64")] + pub fn remaining_length(&self) -> usize { + unsafe { ffi::AAsset_getRemainingLength64(self.ptr.as_ptr()) as usize } + } + + /// Maps all data into a buffer and returns it + #[doc(alias = "AAsset_getBuffer")] + pub fn buffer(&mut self) -> io::Result<&[u8]> { + unsafe { + let buf_ptr = ffi::AAsset_getBuffer(self.ptr.as_ptr()); + if buf_ptr.is_null() { + Err(io::Error::new( + io::ErrorKind::Other, + "Android Asset error creating buffer", + )) + } else { + Ok(std::slice::from_raw_parts( + buf_ptr as *const u8, + self.length(), + )) + } + } + } + + /// Returns whether this asset's internal buffer is allocated in ordinary RAM (i.e. not `mmap`ped). + #[doc(alias = "AAsset_isAllocated")] + pub fn is_allocated(&self) -> bool { + unsafe { ffi::AAsset_isAllocated(self.ptr.as_ptr()) != 0 } + } + + /// Open a new file descriptor that can be used to read the asset data. + /// + /// Returns an error if direct fd access is not possible (for example, if the asset is compressed). + #[doc(alias = "AAsset_openFileDescriptor64")] + pub fn open_file_descriptor(&self) -> io::Result { + let mut offset = 0; + let mut size = 0; + let res = + unsafe { ffi::AAsset_openFileDescriptor64(self.ptr.as_ptr(), &mut offset, &mut size) }; + if res >= 0 { + Ok(OpenedFileDescriptor { + fd: unsafe { OwnedFd::from_raw_fd(res) }, + offset: offset as usize, + size: size as usize, + }) + } else { + Err(io::Error::new( + io::ErrorKind::Other, + "Android Asset openFileDescriptor error", + )) + } + } +} + +impl io::Read for Asset { + #[doc(alias = "AAsset_read")] + fn read(&mut self, buf: &mut [u8]) -> io::Result { + unsafe { + let res = ffi::AAsset_read(self.ptr.as_ptr(), buf.as_mut_ptr() as *mut _, buf.len()); + if res >= 0 { + Ok(res as usize) + } else { + Err(io::Error::new( + io::ErrorKind::Other, + "Android Asset read error", + )) + } + } + } +} + +impl io::Seek for Asset { + #[doc(alias = "AAsset_seek64")] + fn seek(&mut self, seek: io::SeekFrom) -> io::Result { + unsafe { + let res = match seek { + io::SeekFrom::Start(x) => { + ffi::AAsset_seek64(self.ptr.as_ptr(), x as i64, ffi::SEEK_SET as i32) + } + io::SeekFrom::Current(x) => { + ffi::AAsset_seek64(self.ptr.as_ptr(), x, ffi::SEEK_CUR as i32) + } + io::SeekFrom::End(x) => { + ffi::AAsset_seek64(self.ptr.as_ptr(), x, ffi::SEEK_END as i32) + } + }; + if res < 0 { + Err(io::Error::new( + io::ErrorKind::Other, + "Android Asset seek error", + )) + } else { + Ok(res as u64) + } + } + } +} + +/// Contains the opened file descriptor returned by [`Asset::open_file_descriptor()`], together +/// with the offset and size of the given asset within that file descriptor. +#[derive(Debug)] +pub struct OpenedFileDescriptor { + pub fd: OwnedFd, + pub offset: usize, + pub size: usize, +} diff --git a/native/vendor/ndk-0.9.0/src/audio.rs b/native/vendor/ndk-0.9.0/src/audio.rs new file mode 100644 index 0000000..bbf3d98 --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/audio.rs @@ -0,0 +1,1429 @@ +//! Bindings for [`AAudioStream`] and [`AAudioStreamBuilder`] +//! +//! See [the NDK guide](https://developer.android.com/ndk/guides/audio/aaudio/aaudio) for +//! design and usage instructions, and [the NDK reference](https://developer.android.com/ndk/reference/group/audio) +//! for an API overview. +//! +//! [`AAudioStream`]: https://developer.android.com/ndk/reference/group/audio#aaudiostream +//! [`AAudioStreamBuilder`]: https://developer.android.com/ndk/reference/group/audio#aaudiostreambuilder +#![cfg(feature = "audio")] + +use std::{ + borrow::Cow, + ffi::{c_void, CStr}, + fmt, + mem::MaybeUninit, + num::NonZeroI32, + ptr::NonNull, +}; + +use num_enum::{FromPrimitive, IntoPrimitive}; + +use crate::utils::abort_on_panic; + +/// Specifying if audio may or may not be captured by other apps or the system. +/// +/// Note that these match the equivalent values in [`android.media.AudioAttributes`] +/// in the Android Java API. +/// +/// [`android.media.AudioAttributes`]: https://developer.android.com/reference/android/media/AudioAttributes +#[cfg(feature = "api-level-29")] +#[repr(i32)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[doc(alias = "aaudio_allowed_capture_policy_t")] +#[non_exhaustive] +pub enum AudioAllowedCapturePolicy { + /// Indicates that the audio may be captured by any app. + /// + /// For privacy, the following [usages][AudioUsage] can not be recorded: `VoiceCommunication*`, + /// `Notification*`, `Assistance*` and [`Assistant`][AudioUsage::Assistant]. + /// + /// On Android Q, this means only [`Media`][AudioUsage::Media] and [`Game`][AudioUsage::Game] may be captured. + /// + /// See [`MediaProjection`] and [`AudioStreamBuilder::allowed_capture_policy()`]. + /// + /// [`MediaProjection`]: https://developer.android.com/reference/android/media/projection/MediaProjection + #[doc(alias = "AAUDIO_ALLOW_CAPTURE_BY_ALL")] + AllowCaptureByAll = ffi::AAUDIO_ALLOW_CAPTURE_BY_ALL as ffi::aaudio_allowed_capture_policy_t, + /// Indicates that the audio may only be captured by system apps. + /// + /// System apps can capture for many purposes like accessibility, live captions, user + /// guidance... but abide to the following restrictions: + /// - the audio cannot leave the device; + /// - the audio cannot be passed to a third party app; + /// - the audio cannot be recorded at a higher quality than 16kHz 16bit mono. + /// + /// See [`AudioStreamBuilder::allowed_capture_policy()`]. + #[doc(alias = "AAUDIO_ALLOW_CAPTURE_BY_SYSTEM")] + AllowCaptureBySystem = + ffi::AAUDIO_ALLOW_CAPTURE_BY_SYSTEM as ffi::aaudio_allowed_capture_policy_t, + /// Indicates that the audio may not be recorded by any app, even if it is a system app. + /// + /// It is encouraged to use [`AllowCaptureBySystem`][Self::AllowCaptureBySystem] instead of + /// this value as system apps provide significant and useful features for the user (such as + /// live captioning and accessibility). + #[doc(alias = "AAUDIO_ALLOW_CAPTURE_BY_NONE")] + AllowCaptureByNone = ffi::AAUDIO_ALLOW_CAPTURE_BY_NONE as ffi::aaudio_allowed_capture_policy_t, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +/// The ContentType attribute describes "what" you are playing. +/// It expresses the general category of the content. This information is optional. +/// But in case it is known (for instance `Movie` for a +/// movie streaming service or `Speech` for +/// an audio book application) this information might be used by the audio framework to +/// enforce audio focus. +/// +/// Note that these match the equivalent values in [`android.media.AudioAttributes`] +/// in the Android Java API. +/// +/// [`android.media.AudioAttributes`]: https://developer.android.com/reference/android/media/AudioAttributes +#[cfg(feature = "api-level-28")] +#[repr(i32)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[doc(alias = "aaudio_content_type_t")] +#[non_exhaustive] +pub enum AudioContentType { + /// Use this for spoken voice, audio books, etcetera. + #[doc(alias = "AAUDIO_CONTENT_TYPE_SPEECH")] + Speech = ffi::AAUDIO_CONTENT_TYPE_SPEECH as ffi::aaudio_content_type_t, + /// Use this for pre-recorded or live music. + #[doc(alias = "AAUDIO_CONTENT_TYPE_MUSIC")] + Music = ffi::AAUDIO_CONTENT_TYPE_MUSIC as ffi::aaudio_content_type_t, + /// Use this for a movie or video soundtrack. + #[doc(alias = "AAUDIO_CONTENT_TYPE_MOVIE")] + Movie = ffi::AAUDIO_CONTENT_TYPE_MOVIE as ffi::aaudio_content_type_t, + /// Use this for sound is designed to accompany a user action, + /// such as a click or beep sound made when the user presses a button. + #[doc(alias = "AAUDIO_CONTENT_TYPE_SONIFICATION")] + Sonification = ffi::AAUDIO_CONTENT_TYPE_SONIFICATION as ffi::aaudio_content_type_t, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[repr(i32)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[doc(alias = "aaudio_direction_t")] +#[non_exhaustive] +pub enum AudioDirection { + /// Audio data will travel into the device, for example from a microphone. + #[doc(alias = "AAUDIO_DIRECTION_OUTPUT")] + Output = ffi::AAUDIO_DIRECTION_OUTPUT as ffi::aaudio_direction_t, + /// Audio data will travel out of the device, for example through a speaker. + #[doc(alias = "AAUDIO_DIRECTION_INPUT")] + Input = ffi::AAUDIO_DIRECTION_INPUT as ffi::aaudio_direction_t, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[repr(i32)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[allow(non_camel_case_types)] +#[doc(alias = "aaudio_format_t")] +#[non_exhaustive] +pub enum AudioFormat { + #[doc(alias = "AAUDIO_FORMAT_INVALID")] + Invalid = ffi::AAUDIO_FORMAT_INVALID as ffi::aaudio_format_t, + #[doc(alias = "AAUDIO_FORMAT_UNSPECIFIED")] + Unspecified = ffi::AAUDIO_FORMAT_UNSPECIFIED as ffi::aaudio_format_t, + + /// This format uses the i16 data type. + /// The maximum range of the data is -32768 to 32767. + #[doc(alias = "AAUDIO_FORMAT_PCM_I16")] + PCM_I16 = ffi::AAUDIO_FORMAT_PCM_I16 as ffi::aaudio_format_t, + /// This format uses the float data type. + /// The nominal range of the data is [-1.0f32, 1.0f32). + /// Values outside that range may be clipped. + /// + /// See also `audioData` at + /// AudioTrack#write(float[], int, int, int). + #[doc(alias = "AAUDIO_FORMAT_PCM_FLOAT")] + PCM_Float = ffi::AAUDIO_FORMAT_PCM_FLOAT as ffi::aaudio_format_t, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +/// Defines the audio source. +/// An audio source defines both a default physical source of audio signal, and a recording +/// configuration. +/// +/// Note that these match the equivalent values in MediaRecorder.AudioSource in the Android Java API. +#[cfg(feature = "api-level-28")] +#[repr(i32)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[doc(alias = "aaudio_input_preset_t")] +#[non_exhaustive] +pub enum AudioInputPreset { + /// Use this preset when other presets do not apply. + #[doc(alias = "AAUDIO_INPUT_PRESET_GENERIC")] + Generic = ffi::AAUDIO_INPUT_PRESET_GENERIC as ffi::aaudio_input_preset_t, + /// Use this preset when recording video. + #[doc(alias = "AAUDIO_INPUT_PRESET_CAMCORDER")] + Camcorder = ffi::AAUDIO_INPUT_PRESET_CAMCORDER as ffi::aaudio_input_preset_t, + /// Use this preset when doing speech recognition. + #[doc(alias = "AAUDIO_INPUT_PRESET_VOICE_RECOGNITION")] + VoiceRecognition = ffi::AAUDIO_INPUT_PRESET_VOICE_RECOGNITION as ffi::aaudio_input_preset_t, + /// Use this preset when doing telephony or voice messaging. + #[doc(alias = "AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION")] + VoiceCommunication = ffi::AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION as ffi::aaudio_input_preset_t, + /// Use this preset to obtain an input with no effects. + /// Note that this input will not have automatic gain control + /// so the recorded volume may be very low. + #[doc(alias = "AAUDIO_INPUT_PRESET_UNPROCESSED")] + Unprocessed = ffi::AAUDIO_INPUT_PRESET_UNPROCESSED as ffi::aaudio_input_preset_t, + /// Use this preset for capturing audio meant to be processed in real time + /// and played back for live performance (e.g karaoke). + /// The capture path will minimize latency and coupling with playback path. + #[cfg(feature = "api-level-29")] + #[doc(alias = "AAUDIO_INPUT_PRESET_VOICE_PERFORMANCE")] + VoicePerformance = ffi::AAUDIO_INPUT_PRESET_VOICE_PERFORMANCE as ffi::aaudio_input_preset_t, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[repr(i32)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[doc(alias = "aaudio_performance_mode_t")] +#[non_exhaustive] +pub enum AudioPerformanceMode { + /// No particular performance needs. Default. + #[doc(alias = "AAUDIO_PERFORMANCE_MODE_NONE")] + None = ffi::AAUDIO_PERFORMANCE_MODE_NONE as ffi::aaudio_performance_mode_t, + /// Extending battery life is more important than low latency. + /// + /// This mode is not supported in input streams. + /// For input, mode NONE will be used if this is requested. + #[doc(alias = "AAUDIO_PERFORMANCE_MODE_POWER_SAVING")] + PowerSaving = ffi::AAUDIO_PERFORMANCE_MODE_POWER_SAVING as ffi::aaudio_performance_mode_t, + /// Reducing latency is more important than battery life. + #[doc(alias = "AAUDIO_PERFORMANCE_MODE_LOW_LATENCY")] + LowLatency = ffi::AAUDIO_PERFORMANCE_MODE_LOW_LATENCY as ffi::aaudio_performance_mode_t, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[repr(i32)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[doc(alias = "aaudio_sharing_mode_t")] +#[non_exhaustive] +pub enum AudioSharingMode { + /// This will be the only stream using a particular source or sink. + /// This mode will provide the lowest possible latency. + /// You should close Exclusive streams immediately when you are not using them. + #[doc(alias = "AAUDIO_SHARING_MODE_EXCLUSIVE")] + Exclusive = ffi::AAUDIO_SHARING_MODE_EXCLUSIVE as ffi::aaudio_sharing_mode_t, + /// Multiple applications will be mixed by the AAudio Server. + /// This will have higher latency than the Exclusive mode. + #[doc(alias = "AAUDIO_SHARING_MODE_SHARED")] + Shared = ffi::AAUDIO_SHARING_MODE_SHARED as ffi::aaudio_sharing_mode_t, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +/// The Usage attribute expresses "why" you are playing a sound, what is this sound used for. +/// This information is used by certain platforms or routing policies +/// to make more refined volume or routing decisions. +/// +/// Note that these match the equivalent values in [`android.media.AudioAttributes`] +/// in the Android Java API. +/// +/// [`android.media.AudioAttributes`]: https://developer.android.com/reference/android/media/AudioAttributes +#[cfg(feature = "api-level-28")] +#[repr(i32)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[doc(alias = "aaudio_usage_t")] +#[non_exhaustive] +pub enum AudioUsage { + /// Use this for streaming media, music performance, video, podcasts, etcetera. + #[doc(alias = "AAUDIO_USAGE_MEDIA")] + Media = ffi::AAUDIO_USAGE_MEDIA as ffi::aaudio_usage_t, + /// Use this for voice over IP, telephony, etcetera. + #[doc(alias = "AAUDIO_USAGE_VOICE_COMMUNICATION")] + VoiceCommunication = ffi::AAUDIO_USAGE_VOICE_COMMUNICATION as ffi::aaudio_usage_t, + /// Use this for sounds associated with telephony such as busy tones, DTMF, etcetera. + #[doc(alias = "AAUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING")] + VoiceCommunicationSignalling = + ffi::AAUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING as ffi::aaudio_usage_t, + /// Use this to demand the users attention. + #[doc(alias = "AAUDIO_USAGE_ALARM")] + Alarm = ffi::AAUDIO_USAGE_ALARM as ffi::aaudio_usage_t, + /// Use this for notifying the user when a message has arrived or some + /// other background event has occured. + #[doc(alias = "AAUDIO_USAGE_NOTIFICATION")] + Notification = ffi::AAUDIO_USAGE_NOTIFICATION as ffi::aaudio_usage_t, + /// Use this when the phone rings. + #[doc(alias = "AAUDIO_USAGE_NOTIFICATION_RINGTONE")] + NotificationRingtone = ffi::AAUDIO_USAGE_NOTIFICATION_RINGTONE as ffi::aaudio_usage_t, + /// Use this to attract the users attention when, for example, the battery is low. + #[doc(alias = "AAUDIO_USAGE_NOTIFICATION_EVENT")] + NotificationEvent = ffi::AAUDIO_USAGE_NOTIFICATION_EVENT as ffi::aaudio_usage_t, + /// Use this for screen readers, etcetera. + #[doc(alias = "AAUDIO_USAGE_ASSISTANCE_ACCESSIBILITY")] + AssistanceAccessibility = ffi::AAUDIO_USAGE_ASSISTANCE_ACCESSIBILITY as ffi::aaudio_usage_t, + /// Use this for driving or navigation directions. + #[doc(alias = "AAUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE")] + AssistanceNavigationGuidance = + ffi::AAUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE as ffi::aaudio_usage_t, + /// Use this for user interface sounds, beeps, etcetera. + #[doc(alias = "AAUDIO_USAGE_ASSISTANCE_SONIFICATION")] + AssistanceSonification = ffi::AAUDIO_USAGE_ASSISTANCE_SONIFICATION as ffi::aaudio_usage_t, + /// Use this for game audio and sound effects. + #[doc(alias = "AAUDIO_USAGE_GAME")] + Game = ffi::AAUDIO_USAGE_GAME as ffi::aaudio_usage_t, + /// Use this for audio responses to user queries, audio instructions or help utterances. + #[doc(alias = "AAUDIO_USAGE_ASSISTANT")] + Assistant = ffi::AAUDIO_USAGE_ASSISTANT as ffi::aaudio_usage_t, + /// Use this in case of playing sounds in an emergency. + /// Privileged MODIFY_AUDIO_ROUTING permission required. + #[doc(alias = "AAUDIO_SYSTEM_USAGE_EMERGENCY")] + SystemEmergency = ffi::AAUDIO_SYSTEM_USAGE_EMERGENCY as ffi::aaudio_usage_t, + /// Use this for safety sounds and alerts, for example backup camera obstacle detection. + /// Privileged MODIFY_AUDIO_ROUTING permission required. + #[doc(alias = "AAUDIO_SYSTEM_USAGE_SAFETY")] + SystemSafety = ffi::AAUDIO_SYSTEM_USAGE_SAFETY as ffi::aaudio_usage_t, + /// Use this for vehicle status alerts and information, for example the check engine light. + /// Privileged MODIFY_AUDIO_ROUTING permission required. + #[doc(alias = "AAUDIO_SYSTEM_USAGE_VEHICLE_STATUS")] + SystemVehicleStatus = ffi::AAUDIO_SYSTEM_USAGE_VEHICLE_STATUS as ffi::aaudio_usage_t, + #[doc(alias = "announcements")] + /// Use this for traffic announcements as ffi::aaudio_usage_t, etc. + /// Privileged MODIFY_AUDIO_ROUTING permission required. + #[doc(alias = "AAUDIO_SYSTEM_USAGE_ANNOUNCEMENT")] + SystemAnnouncement = ffi::AAUDIO_SYSTEM_USAGE_ANNOUNCEMENT as ffi::aaudio_usage_t, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[repr(i32)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[doc(alias = "aaudio_stream_state_t")] +#[non_exhaustive] +pub enum AudioStreamState { + #[doc(alias = "AAUDIO_STREAM_STATE_UNINITIALIZED")] + Uninitialized = ffi::AAUDIO_STREAM_STATE_UNINITIALIZED as ffi::aaudio_stream_state_t, + #[doc(alias = "AAUDIO_STREAM_STATE_UNKNOWN")] + Unknown = ffi::AAUDIO_STREAM_STATE_UNKNOWN as ffi::aaudio_stream_state_t, + #[doc(alias = "AAUDIO_STREAM_STATE_OPEN")] + Open = ffi::AAUDIO_STREAM_STATE_OPEN as ffi::aaudio_stream_state_t, + #[doc(alias = "AAUDIO_STREAM_STATE_STARTING")] + Starting = ffi::AAUDIO_STREAM_STATE_STARTING as ffi::aaudio_stream_state_t, + #[doc(alias = "AAUDIO_STREAM_STATE_STARTED")] + Started = ffi::AAUDIO_STREAM_STATE_STARTED as ffi::aaudio_stream_state_t, + #[doc(alias = "AAUDIO_STREAM_STATE_PAUSING")] + Pausing = ffi::AAUDIO_STREAM_STATE_PAUSING as ffi::aaudio_stream_state_t, + #[doc(alias = "AAUDIO_STREAM_STATE_PAUSED")] + Paused = ffi::AAUDIO_STREAM_STATE_PAUSED as ffi::aaudio_stream_state_t, + #[doc(alias = "AAUDIO_STREAM_STATE_FLUSHING")] + Flushing = ffi::AAUDIO_STREAM_STATE_FLUSHING as ffi::aaudio_stream_state_t, + #[doc(alias = "AAUDIO_STREAM_STATE_FLUSHED")] + Flushed = ffi::AAUDIO_STREAM_STATE_FLUSHED as ffi::aaudio_stream_state_t, + #[doc(alias = "AAUDIO_STREAM_STATE_STOPPING")] + Stopping = ffi::AAUDIO_STREAM_STATE_STOPPING as ffi::aaudio_stream_state_t, + #[doc(alias = "AAUDIO_STREAM_STATE_STOPPED")] + Stopped = ffi::AAUDIO_STREAM_STATE_STOPPED as ffi::aaudio_stream_state_t, + #[doc(alias = "AAUDIO_STREAM_STATE_CLOSING")] + Closing = ffi::AAUDIO_STREAM_STATE_CLOSING as ffi::aaudio_stream_state_t, + #[doc(alias = "AAUDIO_STREAM_STATE_CLOSED")] + Closed = ffi::AAUDIO_STREAM_STATE_CLOSED as ffi::aaudio_stream_state_t, + #[doc(alias = "AAUDIO_STREAM_STATE_DISCONNECTED")] + Disconnected = ffi::AAUDIO_STREAM_STATE_DISCONNECTED as ffi::aaudio_stream_state_t, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +impl AudioStreamState { + #[doc(alias = "AAudio_convertStreamStateToText")] + pub fn to_text(self) -> Cow<'static, str> { + let ptr = unsafe { CStr::from_ptr(ffi::AAudio_convertStreamStateToText(self.into())) }; + ptr.to_string_lossy() + } +} + +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[doc(alias = "aaudio_session_id_t")] +pub enum SessionId { + None, + Allocated(NonZeroI32), +} + +#[derive(Copy, Clone, Debug)] +pub struct Timestamp { + pub frame_position: i64, + pub time_nanoseconds: i64, +} + +#[repr(u32)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub enum Clockid { + #[doc(alias = "CLOCK_MONOTONIC")] + Monotonic = ffi::CLOCK_MONOTONIC, + #[doc(alias = "CLOCK_BOOTTIME")] + Boottime = ffi::CLOCK_BOOTTIME, +} + +/// Value returned the data callback function. +#[repr(i32)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, IntoPrimitive)] +#[doc(alias = "aaudio_data_callback_result_t")] +#[non_exhaustive] +pub enum AudioCallbackResult { + /// Continue calling the callback. + #[doc(alias = "AAUDIO_CALLBACK_RESULT_CONTINUE")] + Continue = ffi::AAUDIO_CALLBACK_RESULT_CONTINUE as ffi::aaudio_data_callback_result_t, + /// Stop calling the callback. + /// + /// The application will still need to call [`AudioStream::request_pause()`] + /// or [`AudioStream::request_stop()`]. + #[doc(alias = "AAUDIO_CALLBACK_RESULT_STOP")] + Stop = ffi::AAUDIO_CALLBACK_RESULT_STOP as ffi::aaudio_data_callback_result_t, +} + +#[repr(i32)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[doc(alias = "aaudio_result_t")] +#[non_exhaustive] +pub enum AudioError { + #[doc(alias = "AAUDIO_ERROR_BASE")] + Base = ffi::AAUDIO_ERROR_BASE, + /// The audio device was disconnected. This could occur, for example, when headphones + /// are plugged in or unplugged. The stream cannot be used after the device is disconnected. + /// Applications should stop and close the stream. + /// If this error is received in an error callback then another thread should be + /// used to stop and close the stream. + #[doc(alias = "AAUDIO_ERROR_DISCONNECTED")] + Disconnected = ffi::AAUDIO_ERROR_DISCONNECTED, + /// An invalid parameter was passed to AAudio. + #[doc(alias = "AAUDIO_ERROR_ILLEGAL_ARGUMENT")] + IllegalArgument = ffi::AAUDIO_ERROR_ILLEGAL_ARGUMENT, + /// The requested operation is not appropriate for the current state of AAudio. + #[doc(alias = "AAUDIO_ERROR_INTERNAL")] + Internal = ffi::AAUDIO_ERROR_INTERNAL, + /// The requested operation is not appropriate for the current state of AAudio. + #[doc(alias = "AAUDIO_ERROR_INVALID_STATE")] + InvalidState = ffi::AAUDIO_ERROR_INVALID_STATE, + /// The server rejected the handle used to identify the stream. + #[doc(alias = "AAUDIO_ERROR_INVALID_HANDLE")] + InvalidHandle = ffi::AAUDIO_ERROR_INVALID_HANDLE, + /// The function is not implemented for this stream. + #[doc(alias = "AAUDIO_ERROR_UNIMPLEMENTED")] + Unimplemented = ffi::AAUDIO_ERROR_UNIMPLEMENTED, + /// A resource or information is unavailable. + /// This could occur when an application tries to open too many streams, + /// or a timestamp is not available. + #[doc(alias = "AAUDIO_ERROR_UNAVAILABLE")] + Unavailable = ffi::AAUDIO_ERROR_UNAVAILABLE, + /// Memory could not be allocated. + #[doc(alias = "AAUDIO_ERROR_NO_FREE_HANDLES")] + NoFreeHandles = ffi::AAUDIO_ERROR_NO_FREE_HANDLES, + /// Memory could not be allocated. + #[doc(alias = "AAUDIO_ERROR_NO_MEMORY")] + NoMemory = ffi::AAUDIO_ERROR_NO_MEMORY, + #[doc(alias = "AAUDIO_ERROR_NULL")] + Null = ffi::AAUDIO_ERROR_NULL, + #[doc(alias = "AAUDIO_ERROR_TIMEOUT")] + Timeout = ffi::AAUDIO_ERROR_TIMEOUT, + #[doc(alias = "AAUDIO_ERROR_WOULD_BLOCK")] + WouldBlock = ffi::AAUDIO_ERROR_WOULD_BLOCK, + /// The requested data format is not supported. + #[doc(alias = "AAUDIO_ERROR_INVALID_FORMAT")] + InvalidFormat = ffi::AAUDIO_ERROR_INVALID_FORMAT, + /// A requested was out of range. + #[doc(alias = "AAUDIO_ERROR_OUT_OF_RANGE")] + OutOfRange = ffi::AAUDIO_ERROR_OUT_OF_RANGE, + /// The audio service was not available. + #[doc(alias = "AAUDIO_ERROR_NO_SERVICE")] + NoService = ffi::AAUDIO_ERROR_NO_SERVICE, + /// The requested sample rate was not supported. + #[doc(alias = "AAUDIO_ERROR_INVALID_RATE")] + InvalidRate = ffi::AAUDIO_ERROR_INVALID_RATE, + + // Use the OK discriminant, as no-one will be able to call `as i32` and only has access to the + // constants via `From` provided by `IntoPrimitive` which reads the contained value. + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +impl fmt::Display for AudioError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self) + } +} + +impl std::error::Error for AudioError {} + +impl AudioError { + #[doc(alias = "AAudio_convertStreamStateToText")] + pub fn to_text(self) -> Cow<'static, str> { + let ptr = unsafe { CStr::from_ptr(ffi::AAudio_convertStreamStateToText(self.into())) }; + ptr.to_string_lossy() + } + + /// Returns [`Ok`] on [`ffi::AAUDIO_OK`], [`Err`] otherwise (including positive values). + /// + /// Note that some known error codes (currently only for `AMediaCodec`) are positive. + pub(crate) fn from_result(status: ffi::aaudio_result_t) -> Result<()> { + match status { + ffi::AAUDIO_OK => Ok(()), + x => Err(Self::from(x)), + } + } +} + +pub type Result = std::result::Result; + +fn construct(with_ptr: impl FnOnce(*mut T) -> ffi::aaudio_result_t) -> Result { + let mut result = MaybeUninit::uninit(); + let status = with_ptr(result.as_mut_ptr()); + AudioError::from_result(status).map(|()| unsafe { result.assume_init() }) +} + +/// A native [`AAudioStreamBuilder *`] +/// +/// [`AAudioStreamBuilder *`]: https://developer.android.com/ndk/reference/group/audio#aaudiostreambuilder +#[doc(alias = "AAudioStreamBuilder")] +pub struct AudioStreamBuilder { + inner: NonNull, + data_callback: Option, + error_callback: Option, +} + +impl fmt::Debug for AudioStreamBuilder { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("AAudioStreamBuilder") + .field("inner", &self.inner) + .field( + "data_callback", + match &self.data_callback { + Some(_) => &"Some(_)", + None => &"None", + }, + ) + .field( + "error_callback", + match &self.error_callback { + Some(_) => &"Some(_)", + None => &"None", + }, + ) + .finish() + } +} + +#[doc(alias = "AAudioStream_dataCallback")] +pub type AudioStreamDataCallback = + Box AudioCallbackResult + Send>; +#[doc(alias = "AAudioStream_errorCallback")] +pub type AudioStreamErrorCallback = Box; + +impl AudioStreamBuilder { + fn from_ptr(inner: NonNull) -> Self { + Self { + inner, + data_callback: None, + error_callback: None, + } + } + + /// Returns the borrowed native builder pointer for optional AAudio APIs. + /// + /// The pointer is valid only while this builder lives. Foreign calls must + /// preserve ndk's ownership: never delete, retain, or open the builder through + /// this pointer, and do not replace its callbacks outside this wrapper. + pub fn as_ptr(&self) -> *mut ffi::AAudioStreamBuilder { + self.inner.as_ptr() + } + + #[doc(alias = "AAudio_createStreamBuilder")] + pub fn new() -> Result { + unsafe { + let ptr = construct(|res| ffi::AAudio_createStreamBuilder(res))?; + Ok(Self::from_ptr(NonNull::new_unchecked(ptr))) + } + } + + /// Specify whether this stream audio may or may not be captured by other apps or the system. + /// + /// The default is [`AudioAllowedCapturePolicy::AllowCaptureByAll`]. + /// + /// Note that an application can also set its global policy, in which case the most restrictive + /// policy is always applied. See [`android.media.AudioAttributes#setAllowedCapturePolicy(int)`]. + /// + /// # Parameters + /// + /// - `policy`: the desired level of opt-out from being captured. + /// + /// [`android.media.AudioAttributes#setAllowedCapturePolicy(int)`]: https://developer.android.com/reference/android/media/AudioAttributes.Builder#setAllowedCapturePolicy(int) + #[cfg(feature = "api-level-29")] + #[doc(alias = "AAudioStreamBuilder_setAllowedCapturePolicy")] + pub fn allowed_capture_policy(self, capture_policy: AudioAllowedCapturePolicy) -> Self { + unsafe { + ffi::AAudioStreamBuilder_setAllowedCapturePolicy(self.as_ptr(), capture_policy.into()) + }; + self + } + + /// Set the requested buffer capacity in frames. + /// The final AAudioStream capacity may differ, but will probably be at least this big. + /// + /// The default, if you do not call this function, is unspecified. + /// + /// # Parameters + /// + /// - `num_frames`: the desired buffer capacity in frames or 0 for unspecified + #[doc(alias = "AAudioStreamBuilder_setBufferCapacityInFrames")] + pub fn buffer_capacity_in_frames(self, num_frames: i32) -> Self { + unsafe { ffi::AAudioStreamBuilder_setBufferCapacityInFrames(self.as_ptr(), num_frames) }; + self + } + + /// Request a number of channels for the stream. + /// + /// The default, if you do not call this function, is unspecified. + /// An optimal value will then be chosen when the stream is opened. + /// After opening a stream with an unspecified value, the application must + /// query for the actual value, which may vary by device. + /// + /// If an exact value is specified then an opened stream will use that value. + /// If a stream cannot be opened with the specified value then the open will fail. + /// + /// # Parameters + /// + /// - `channel_count`: Number of channels desired. + #[doc(alias = "AAudioStreamBuilder_setChannelCount")] + pub fn channel_count(self, channel_count: i32) -> Self { + unsafe { ffi::AAudioStreamBuilder_setChannelCount(self.as_ptr(), channel_count) }; + self + } + + /// Set the type of audio data that the stream will carry. + /// + /// The AAudio system will use this information to optimize the + /// behavior of the stream. + /// This could, for example, affect whether a stream is paused when a notification occurs. + /// + /// The default, if you do not call this function, is [`AudioContentType::Music`]. + /// + /// # Parameters + /// + /// - `content_type`: the type of audio data, eg. [`AudioContentType::Speech`] + #[cfg(feature = "api-level-28")] + #[doc(alias = "AAudioStreamBuilder_setContentType")] + pub fn content_type(self, content_type: AudioContentType) -> Self { + unsafe { ffi::AAudioStreamBuilder_setContentType(self.as_ptr(), content_type.into()) }; + self + } + + /// Request that AAudio call the `data_callback` when the stream is running. + /// + /// Note that when using data callback, the audio data will be passed in or out + /// of the function as an argument. + /// So you cannot call [`AudioStream::write()`] or [`AudioStream::read()`] + /// on the same stream that has an active data callback. + /// + /// The data callback function will start being called after [`AudioStream::request_start()`] + /// is called. + /// It will stop being called after [`AudioStream::request_pause()`] or + /// [`AudioStream::request_stop()`] is called. + /// + /// The `data_callback` function will be called on a real-time thread owned by AAudio. + /// Note that numFrames can vary unless [`AudioStreamBuilder::frames_per_data_callback()`] + /// is called. + /// + /// Also note that this callback function should be considered a "real-time" function. + /// It must not do anything that could cause an unbounded delay because that can cause the + /// audio to glitch or pop. + /// + /// These are things the function should NOT do: + /// * allocate memory using, for example, `malloc()` or new + /// * any file operations such as opening, closing, reading or writing + /// * any network operations such as streaming + /// * use any mutexes or other synchronization primitives + /// * sleep + /// * stop or close the stream + /// * [`AudioStream::read()`] + /// * [`AudioStream::write()`] + /// + /// If you need to move data, eg. MIDI commands, in or out of the callback function then + /// we recommend the use of non-blocking techniques such as an atomic FIFO. + /// + /// Note that the AAudio callbacks will never be called simultaneously from multiple threads. + #[doc(alias = "AAudioStreamBuilder_setDataCallback")] + pub fn data_callback(mut self, callback: AudioStreamDataCallback) -> Self { + let mut boxed = Box::new(callback); + let ptr: *mut AudioStreamDataCallback = &mut *boxed; + + unsafe extern "C" fn ffi_callback( + stream: *mut ffi::AAudioStreamStruct, + user_data: *mut c_void, + audio_data: *mut c_void, + num_frames: i32, + ) -> ffi::aaudio_data_callback_result_t { + abort_on_panic(|| { + let callback = user_data as *mut AudioStreamDataCallback; + let stream = AudioStream { + inner: NonNull::new_unchecked(stream), + data_callback: None, + error_callback: None, + }; + let result = (*callback)(&stream, audio_data, num_frames); + std::mem::forget(stream); + result.into() + }) + } + + unsafe { + ffi::AAudioStreamBuilder_setDataCallback( + self.as_ptr(), + Some(ffi_callback), + ptr as *mut c_void, + ) + }; + + self.data_callback = Some(boxed); + + self + } + + /// Request an audio device identified device using an ID. + /// On Android, for example, the ID could be obtained from the Java AudioManager. + /// + /// The default, if you do not call this function, is 0, + /// in which case the primary device will be used. + /// + /// # Parameters + /// + /// - `device_id`: device identifier or 0 for unspecified + #[doc(alias = "AAudioStreamBuilder_setDeviceId")] + pub fn device_id(self, device_id: i32) -> Self { + unsafe { ffi::AAudioStreamBuilder_setDeviceId(self.as_ptr(), device_id) }; + self + } + + /// Request the direction for a stream. + /// + /// The default, if you do not call this function, is [`Output`][AudioDirection::Output]. + /// + /// # Parameters + /// + /// - `direction`: [`Output`][AudioDirection::Output] or [`Input`][AudioDirection::Input] + #[doc(alias = "AAudioStreamBuilder_setDirection")] + pub fn direction(self, direction: AudioDirection) -> Self { + unsafe { ffi::AAudioStreamBuilder_setDirection(self.as_ptr(), direction.into()) }; + self + } + + /// Request that AAudio call the `data_callback` when the stream is running and the + /// `error_callback` if any error occurs or the stream is disconnected. + /// + /// The `error_callback` will be called, for example, if a headset or a USB device is unplugged causing the stream's + /// device to be unavailable or "disconnected". + /// Another possible cause of error would be a timeout or an unanticipated internal error. + /// + /// In response, this function should signal or create another thread to stop + /// and close this stream. The other thread could then reopen a stream on another device. + /// Do not stop or close the stream, or reopen the new stream, directly from this callback. + /// + /// The `error_callback` will not be called because of actions by the application, such as stopping + /// or closing a stream. + /// + /// Note that the AAudio callbacks will never be called simultaneously from multiple threads. + #[doc(alias = "AAudioStreamBuilder_setErrorCallback")] + pub fn error_callback(mut self, callback: AudioStreamErrorCallback) -> Self { + let mut boxed = Box::new(callback); + let ptr: *mut AudioStreamErrorCallback = &mut *boxed; + + unsafe extern "C" fn ffi_callback( + stream: *mut ffi::AAudioStreamStruct, + user_data: *mut c_void, + error: ffi::aaudio_result_t, + ) { + abort_on_panic(|| { + let callback = user_data as *mut AudioStreamErrorCallback; + let stream = AudioStream { + inner: NonNull::new_unchecked(stream), + data_callback: None, + error_callback: None, + }; + let err = AudioError::from_result(error).unwrap_err(); + (*callback)(&stream, err); + std::mem::forget(stream); + }) + } + + unsafe { + ffi::AAudioStreamBuilder_setErrorCallback( + self.as_ptr(), + Some(ffi_callback), + ptr as *mut c_void, + ) + }; + + self.error_callback = Some(boxed); + + self + } + + /// Request a sample data format, for example `Format::I16`. + /// + /// The default, if you do not call this function, is [`Unspecified`][AudioFormat::Unspecified]. + /// An optimal value will then be chosen when the stream is opened. + /// After opening a stream with an unspecified value, the application must + /// query for the actual value, which may vary by device. + /// + /// If an exact value is specified then an opened stream will use that value. + /// If a stream cannot be opened with the specified value then the open will fail. + /// + /// # Parameters + /// + /// - `format`: the sample data format. + #[doc(alias = "AAudioStreamBuilder_setFormat")] + pub fn format(self, format: AudioFormat) -> Self { + unsafe { ffi::AAudioStreamBuilder_setFormat(self.as_ptr(), format.into()) }; + self + } + + /// Set the requested data callback buffer size in frames. + /// + /// See [`AudioStreamDataCallback`]. + /// + /// The default, if you do not call this function, is unspecified. + /// + /// For the lowest possible latency, do not call this function. AAudio will then + /// call the [`data_callback`][Self::data_callback] function with whatever size is optimal. + /// That size may vary from one callback to another. + /// + /// Only use this function if the application requires a specific number of frames for processing. + /// The application might, for example, be using an FFT that requires + /// a specific power-of-two sized buffer. + /// + /// AAudio may need to add additional buffering in order to adapt between the internal + /// buffer size and the requested buffer size. + /// + /// If you do call this function then the requested size should be less than + /// half the buffer capacity, to allow double buffering. + /// + /// - `num_frames`: the desired buffer size in frames or 0 for unspecified + #[doc(alias = "AAudioStreamBuilder_setFramesPerDataCallback")] + pub fn frames_per_data_callback(self, num_frames: i32) -> Self { + unsafe { ffi::AAudioStreamBuilder_setFramesPerDataCallback(self.as_ptr(), num_frames) }; + self + } + + /// Set the input (capture) preset for the stream. + /// + /// The AAudio system will use this information to optimize the + /// behavior of the stream. + /// This could, for example, affect which microphones are used and how the + /// recorded data is processed. + /// + /// The default, if you do not call this function, is [`VoiceRecognition`][AudioInputPreset::VoiceRecognition] + /// which is the preset with the lowest latency on many platforms. + /// + /// # Parameters + /// + /// - `input_preset`: the desired configuration for recording + #[cfg(feature = "api-level-28")] + #[doc(alias = "AAudioStreamBuilder_setInputPreset")] + pub fn input_preset(self, input_preset: AudioInputPreset) -> Self { + unsafe { ffi::AAudioStreamBuilder_setInputPreset(self.as_ptr(), input_preset.into()) }; + self + } + + /// Set the requested performance mode. + /// + /// Supported modes are None, PowerSaving and LowLatency. + /// + /// The default, if you do not call this function, is None. + /// + /// You may not get the mode you requested. + /// You can call [`AudioStream::performance_mode()`] + /// to find out the final mode for the stream. + /// + /// # Parameters + /// + /// - `mode`: the desired performance mode, eg. [`AudioPerformanceMode::LowLatency`] + #[doc(alias = "AAudioStreamBuilder_setPerformanceMode")] + pub fn performance_mode(self, mode: AudioPerformanceMode) -> Self { + unsafe { ffi::AAudioStreamBuilder_setPerformanceMode(self.as_ptr(), mode.into()) }; + self + } + + /// Request a sample rate in Hertz. + /// + /// The default, if you do not call this function, is 0 (unspecified). + /// An optimal value will then be chosen when the stream is opened. + /// After opening a stream with an unspecified value, the application must + /// query for the actual value, which may vary by device. + /// + /// If an exact value is specified then an opened stream will use that value. + /// If a stream cannot be opened with the specified value then the open will fail. + /// + /// # Parameters + /// + /// - `sample_rate`: frames per second. Common rates include 44100 and 48000 Hz. + #[doc(alias = "AAudioStreamBuilder_setSampleRate")] + pub fn sample_rate(self, sample_rate: i32) -> Self { + unsafe { ffi::AAudioStreamBuilder_setSampleRate(self.as_ptr(), sample_rate) }; + self + } + + #[doc(alias = "AAudioStreamBuilder_setSamplesPerFrame")] + pub fn samples_per_frame(self, samples_per_frame: i32) -> Self { + unsafe { ffi::AAudioStreamBuilder_setSamplesPerFrame(self.as_ptr(), samples_per_frame) }; + self + } + + /// The session ID can be used to associate a stream with effects processors. + /// The effects are controlled using the Android AudioEffect Java API. + /// + /// The default, if you do not call this function, is -1 (none). + /// + /// If set to [`Option::None`] then a session ID will be allocated when the stream is opened. + /// + /// The allocated session ID can be obtained by calling [`AudioStream::session_id()`] + /// and then used with this function when opening another stream. + /// This allows effects to be shared between streams. + /// + /// Session IDs from AAudio can be used with the Android Java APIs and vice versa. + /// So a session ID from an AAudio stream can be passed to Java + /// and effects applied using the Java AudioEffect API. + /// + /// Note that allocating or setting a session ID may result in a stream with higher latency. + /// + /// Allocated session IDs will always be positive and nonzero. + /// + /// # Parameters + /// + /// - `session_id`: an allocated sessionID or [`Option::None`] to allocate a new sessionID + #[cfg(feature = "api-level-28")] + #[doc(alias = "AAudioStreamBuilder_setSessionId")] + pub fn session_id(self, session_id_or_allocate: Option) -> Self { + let session_id = match session_id_or_allocate { + None => ffi::AAUDIO_SESSION_ID_ALLOCATE, + Some(SessionId::None) => ffi::AAUDIO_SESSION_ID_NONE, + Some(SessionId::Allocated(value)) => value.get(), + }; + + unsafe { ffi::AAudioStreamBuilder_setSessionId(self.as_ptr(), session_id) }; + self + } + + /// Request a mode for sharing the device. + /// + /// The default, if you do not call this function, is [`AudioSharingMode::Shared`]. + /// + /// The requested sharing mode may not be available. + /// The application can query for the actual mode after the stream is opened. + /// + /// # Parameters + /// + /// - `sharing_mode`: [`AudioSharingMode::Shared`] or [`AudioSharingMode::Exclusive`] + #[doc(alias = "AAudioStreamBuilder_setSharingMode")] + pub fn sharing_mode(self, sharing_mode: AudioSharingMode) -> Self { + unsafe { ffi::AAudioStreamBuilder_setSharingMode(self.as_ptr(), sharing_mode.into()) }; + self + } + + /// Set the intended use case for the stream. + /// + /// The AAudio system will use this information to optimize the + /// behavior of the stream. + /// This could, for example, affect how volume and focus is handled for the stream. + /// + /// The default, if you do not call this function, is [`AudioUsage::Media`]. + /// + /// - `usage`: the desired usage, eg. [`AudioUsage::Game`] + #[cfg(feature = "api-level-28")] + #[doc(alias = "AAudioStreamBuilder_setUsage")] + pub fn usage(self, usage: AudioUsage) -> Self { + unsafe { ffi::AAudioStreamBuilder_setUsage(self.as_ptr(), usage.into()) }; + self + } + + /// Open a stream based on the options in the AAudioStreamBuilder. + #[doc(alias = "AAudioStreamBuilder_openStream")] + pub fn open_stream(mut self) -> Result { + unsafe { + let ptr = construct(|res| ffi::AAudioStreamBuilder_openStream(self.as_ptr(), res))?; + + Ok(AudioStream { + inner: NonNull::new_unchecked(ptr), + data_callback: self.data_callback.take(), + error_callback: self.error_callback.take(), + }) + } + } +} + +impl Drop for AudioStreamBuilder { + #[doc(alias = "AAudioStreamBuilder_delete")] + fn drop(&mut self) { + let status = unsafe { ffi::AAudioStreamBuilder_delete(self.as_ptr()) }; + AudioError::from_result(status).unwrap(); + } +} + +/// A native [`AAudioStream *`] +/// +/// [`AAudioStream *`]: https://developer.android.com/ndk/reference/group/audio#aaudiostream +#[doc(alias = "AAudioStream")] +pub struct AudioStream { + inner: NonNull, + data_callback: Option, + error_callback: Option, +} + +impl fmt::Debug for AudioStream { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("AAudioStream") + .field("inner", &self.inner) + .field( + "data_callback", + match &self.data_callback { + Some(_) => &"Some(_)", + None => &"None", + }, + ) + .field( + "error_callback", + match &self.error_callback { + Some(_) => &"Some(_)", + None => &"None", + }, + ) + .finish() + } +} + +impl AudioStream { + /// Returns the borrowed native stream pointer for optional AAudio APIs. + /// + /// The pointer is valid only while this stream lives. Foreign calls must + /// preserve ndk's ownership and callback state; never close, release, or + /// retain the stream through this pointer. + pub fn as_ptr(&self) -> *mut ffi::AAudioStream { + self.inner.as_ptr() + } + + /// Returns the policy that determines whether the audio may or + /// may not be captured by other apps or the system. + #[cfg(feature = "api-level-29")] + #[doc(alias = "AAudioStream_getAllowedCapturePolicy")] + pub fn allowed_capture_policy(self) -> AudioAllowedCapturePolicy { + unsafe { ffi::AAudioStream_getAllowedCapturePolicy(self.as_ptr()) }.into() + } + + /// Query maximum buffer capacity in frames. + #[doc(alias = "AAudioStream_getBufferCapacityInFrames")] + pub fn buffer_capacity_in_frames(&self) -> i32 { + unsafe { ffi::AAudioStream_getBufferCapacityInFrames(self.as_ptr()) } + } + + /// Query the maximum number of frames that can be filled without blocking. + #[doc(alias = "AAudioStream_getBufferSizeInFrames")] + pub fn buffer_size_in_frames(&self) -> i32 { + unsafe { ffi::AAudioStream_getBufferSizeInFrames(self.as_ptr()) } + } + + /// A stream has one or more channels of data. + /// A frame will contain one sample for each channel. + #[doc(alias = "AAudioStream_getChannelCount")] + pub fn channel_count(&self) -> i32 { + unsafe { ffi::AAudioStream_getChannelCount(self.as_ptr()) } + } + + #[cfg(feature = "api-level-28")] + #[doc(alias = "AAudioStream_getContentType")] + pub fn content_type(&self) -> AudioContentType { + unsafe { ffi::AAudioStream_getContentType(self.as_ptr()) }.into() + } + + /// Returns the actual device ID. + #[doc(alias = "AAudioStream_getDeviceId")] + pub fn device_id(&self) -> i32 { + unsafe { ffi::AAudioStream_getDeviceId(self.as_ptr()) } + } + + /// Available since API level 26. + #[doc(alias = "AAudioStream_getDirection")] + pub fn direction(&self) -> AudioDirection { + unsafe { ffi::AAudioStream_getDirection(self.as_ptr()) }.into() + } + + /// Returns the actual data format. + #[doc(alias = "AAudioStream_getFormat")] + pub fn format(&self) -> AudioFormat { + unsafe { ffi::AAudioStream_getFormat(self.as_ptr()) }.into() + } + + /// Query the number of frames that the application should read or write at + /// one time for optimal performance. It is OK if an application writes + /// a different number of frames. But the buffer size may need to be larger + /// in order to avoid underruns or overruns. + /// + /// Note that this may or may not match the actual device burst size. + /// For some endpoints, the burst size can vary dynamically. + /// But these tend to be devices with high latency. + #[doc(alias = "AAudioStream_getFramesPerBurst")] + pub fn frames_per_burst(&self) -> i32 { + unsafe { ffi::AAudioStream_getFramesPerBurst(self.as_ptr()) } + } + + /// Query the size of the buffer that will be passed to the data callback in the `numFrames` parameter. + /// This call can be used if the application needs to know the value of numFrames before + /// the stream is started. This is not normally necessary. + /// + /// If a specific size was requested by calling + /// [`AudioStreamBuilder::frames_per_data_callback()`] then this will be the same size. + /// + /// If [`AudioStreamBuilder::frames_per_data_callback()`] was not called then this will + /// return the size chosen by AAudio, or 0. + /// + /// `None` indicates that the callback buffer size for this stream may vary from one dataProc callback to the next. + #[doc(alias = "AAudioStream_getFramesPerDataCallback")] + pub fn frames_per_data_callback(&self) -> Option { + let value = unsafe { ffi::AAudioStream_getFramesPerDataCallback(self.as_ptr()) }; + const AAUDIO_UNSPECIFIED: i32 = ffi::AAUDIO_UNSPECIFIED as i32; + match value { + AAUDIO_UNSPECIFIED => None, + val => Some(val), + } + } + + /// Returns the number of frames that have been read since the stream was created. + /// For an output stream, this will be advanced by the endpoint. + /// For an input stream, this will be advanced by the application calling [`read()`][Self::read] + /// or by a data callback. + /// + /// The frame position is monotonically increasing. + #[doc(alias = "AAudioStream_getFramesRead")] + pub fn frames_read(&self) -> i64 { + unsafe { ffi::AAudioStream_getFramesRead(self.as_ptr()) } + } + + /// Returns the number of frames that have been written since the stream was created. + /// For an output stream, this will be advanced by the application calling write() + /// or by a data callback. + /// For an input stream, this will be advanced by the endpoint. + /// + /// The frame position is monotonically increasing. + #[doc(alias = "AAudioStream_getFramesWritten")] + pub fn frames_written(&self) -> i64 { + unsafe { ffi::AAudioStream_getFramesWritten(self.as_ptr()) } + } + + #[cfg(feature = "api-level-28")] + #[doc(alias = "AAudioStream_getInputPreset")] + pub fn input_preset(&self) -> AudioInputPreset { + unsafe { ffi::AAudioStream_getInputPreset(self.as_ptr()) }.into() + } + + /// Get the performance mode used by the stream. + #[doc(alias = "AAudioStream_getPerformanceMode")] + pub fn performance_mode(&self) -> AudioPerformanceMode { + unsafe { ffi::AAudioStream_getPerformanceMode(self.as_ptr()) }.into() + } + + /// Returns the actual sample rate. + #[doc(alias = "AAudioStream_getSampleRate")] + pub fn sample_rate(&self) -> i32 { + unsafe { ffi::AAudioStream_getSampleRate(self.as_ptr()) } + } + + #[doc(alias = "AAudioStream_getSamplesPerFrame")] + pub fn samples_per_frame(&self) -> i32 { + unsafe { ffi::AAudioStream_getSamplesPerFrame(self.as_ptr()) } + } + + /// Passes back the session ID associated with this stream. + /// + /// The session ID can be used to associate a stream with effects processors. + /// The effects are controlled using the Android AudioEffect Java API. + /// + /// If [`AudioStreamBuilder::session_id()`] was called with `0` + /// then a new session ID should be allocated once when the stream is opened. + /// + /// If [`AudioStreamBuilder::session_id()`] was called with a previously allocated + /// session ID then that value should be returned. + /// + /// If [`AudioStreamBuilder::session_id()`] was not called then this function should + /// return `-1`. + /// + /// The sessionID for a stream should not change once the stream has been opened. + #[cfg(feature = "api-level-28")] + #[doc(alias = "AAudioStream_getSessionId")] + pub fn session_id(&self) -> SessionId { + let value = unsafe { ffi::AAudioStream_getSessionId(self.as_ptr()) }; + match value { + ffi::AAUDIO_SESSION_ID_NONE => SessionId::None, + allocated => SessionId::Allocated(NonZeroI32::new(allocated).unwrap()), + } + } + + /// Provide actual sharing mode. + #[doc(alias = "AAudioStream_getSharingMode")] + pub fn sharing_mode(&self) -> AudioSharingMode { + unsafe { ffi::AAudioStream_getSharingMode(self.as_ptr()) }.into() + } + + /// Query the current state of the client, eg. [`Pausing`][AudioStreamState::Pausing]. + /// + /// This function will immediately return the state without updating the state. + /// If you want to update the client state based on the server state then + /// call [`AudioStream::wait_for_state_change()`] with currentState + /// set to [`Unknown`][AudioStreamState::Unknown] and a zero timeout. + #[doc(alias = "AAudioStream_getState")] + pub fn state(&self) -> AudioStreamState { + unsafe { ffi::AAudioStream_getState(self.as_ptr()) }.into() + } + + /// Returns the time at which a particular frame was presented. + /// This can be used to synchronize audio with video or MIDI. + /// It can also be used to align a recorded stream with a playback stream. + /// + /// Timestamps are only valid when the stream is in `Started` state. + /// [`InvalidState`][AudioError::InvalidState] will be returned + /// if the stream is not started. + /// Note that because [`AudioStream::request_start()`] is asynchronous, + /// timestamps will not be valid until a short time after calling + /// [`AudioStream::request_start()`]. + /// So [`InvalidState`][AudioError::InvalidState] should not be + /// considered a fatal error. + /// Just try calling again later. + /// + /// If an error occurs, then the position and time will not be modified. + /// + /// The position and time passed back are monotonically increasing. + #[doc(alias = "AAudioStream_getTimestamp")] + pub fn timestamp(&self, clockid: Clockid) -> Result { + let frame_position; + let time_nanoseconds = unsafe { + let mut nanoseconds = MaybeUninit::uninit(); + frame_position = construct(|ptr| { + ffi::AAudioStream_getTimestamp( + self.as_ptr(), + clockid as ffi::clockid_t, + ptr, + nanoseconds.as_mut_ptr(), + ) + })?; + nanoseconds.assume_init() + }; + + Ok(Timestamp { + frame_position, + time_nanoseconds, + }) + } + + #[cfg(feature = "api-level-28")] + #[doc(alias = "AAudioStream_getUsage")] + pub fn usage(&self) -> AudioUsage { + unsafe { ffi::AAudioStream_getUsage(self.as_ptr()) }.into() + } + + /// An XRun is an Underrun or an Overrun. + /// During playing, an underrun will occur if the stream is not written in time + /// and the system runs out of valid data. + /// During recording, an overrun will occur if the stream is not read in time + /// and there is no place to put the incoming data so it is discarded. + /// + /// An underrun or overrun can cause an audible "pop" or "glitch". + /// + /// Note that some INPUT devices may not support this function. + /// In that case a 0 will always be returned. + #[doc(alias = "AAudioStream_getXRunCount")] + pub fn x_run_count(&self) -> i32 { + unsafe { ffi::AAudioStream_getXRunCount(self.as_ptr()) } + } + + /// Read data from the stream. + /// Returns the number of frames actually read or a negative error. + /// + /// The call will wait until the read is complete or until it runs out of time. + /// If timeoutNanos is zero then this call will not wait. + /// + /// Note that timeoutNanoseconds is a relative duration in wall clock time. + /// Time will not stop if the thread is asleep. + /// So it will be implemented using CLOCK_BOOTTIME. + /// + /// This call is "strong non-blocking" unless it has to wait for data. + /// + /// If the call times out then zero or a partial frame count will be returned. + /// + /// # Parameters + /// + /// - `buffer`: The slice with the samples. + /// - `num_frames`: Number of frames to read. Only complete frames will be written. + /// - `timeout_nanoseconds`: Maximum number of nanoseconds to wait for completion. + /// + /// # Safety + /// `buffer` must be a valid pointer to at least `num_frames` samples. + #[doc(alias = "AAudioStream_read")] + pub unsafe fn read( + &self, + buffer: *mut c_void, + num_frames: i32, + timeout_nanoseconds: i64, + ) -> Result { + let result = ffi::AAudioStream_read(self.as_ptr(), buffer, num_frames, timeout_nanoseconds); + + AudioError::from_result(result).map(|()| result as u32) + } + + /// Asynchronous request for the stream to flush. + /// Flushing will discard any pending data. + /// This call only works if the stream is pausing or paused. + /// Frame counters are not reset by a flush. They may be advanced. + /// After this call the state will be in [`Flushing`][AudioStreamState::Flushing] or + /// [`Flushed`][AudioStreamState::Flushed]. + /// + /// This will return [`Unimplemented`][AudioError::Unimplemented] for input streams. + #[doc(alias = "AAudioStream_requestFlush")] + pub fn request_flush(&self) -> Result<()> { + let result = unsafe { ffi::AAudioStream_requestFlush(self.as_ptr()) }; + AudioError::from_result(result) + } + + /// Asynchronous request for the stream to pause. + /// Pausing a stream will freeze the data flow but not flush any buffers. + /// Use [`AudioStream::request_start()`] to resume playback after a pause. + /// After this call the state will be in [`Pausing`][AudioStreamState::Pausing] or + /// [`Paused`][AudioStreamState::Paused]. + /// + /// This will return [`Unimplemented`][AudioError::Unimplemented] for input streams. + /// For input streams use [`AudioStream::request_stop()`]. + #[doc(alias = "AAudioStream_requestPause")] + pub fn request_pause(&self) -> Result<()> { + let result = unsafe { ffi::AAudioStream_requestPause(self.as_ptr()) }; + AudioError::from_result(result) + } + + /// Asynchronously request to start playing the stream. For output streams, one should + /// write to the stream to fill the buffer before starting. + /// Otherwise it will underflow. + /// After this call the state will be in [`Starting`][AudioStreamState::Starting] or + /// [`Started`][AudioStreamState::Started]. + #[doc(alias = "AAudioStream_requestStart")] + pub fn request_start(&self) -> Result<()> { + let result = unsafe { ffi::AAudioStream_requestStart(self.as_ptr()) }; + AudioError::from_result(result) + } + + /// Asynchronous request for the stream to stop. + /// The stream will stop after all of the data currently buffered has been played. + /// After this call the state will be in [`Stopping`][AudioStreamState::Stopping] or + /// [`Stopped`][AudioStreamState::Stopped]. + #[doc(alias = "AAudioStream_requestStop")] + pub fn request_stop(&self) -> Result<()> { + let result = unsafe { ffi::AAudioStream_requestStop(self.as_ptr()) }; + AudioError::from_result(result) + } + + /// This can be used to adjust the latency of the buffer by changing + /// the threshold where blocking will occur. + /// By combining this with [`AudioStream::x_run_count()`], the latency can be tuned + /// at run-time for each device. + /// Returns actual buffer size in frames or a negative error. + /// + /// This cannot be set higher than [`AudioStream::buffer_capacity_in_frames()`]. + /// + /// Note that you will probably not get the exact size you request. + /// You can check the return value or call [`AudioStream::buffer_size_in_frames()`] + /// to see what the actual final size is. + /// + /// # Parameters + /// + /// - `num_frames`: requested number of frames that can be filled without blocking + #[doc(alias = "AAudioStream_setBufferSizeInFrames")] + pub fn set_buffer_size_in_frames(&self, num_frames: i32) -> Result { + let result = unsafe { ffi::AAudioStream_setBufferSizeInFrames(self.as_ptr(), num_frames) }; + AudioError::from_result(result).map(|()| result) + } + + /// Wait until the current state no longer matches the input state. + /// + /// This will update the current client state. + /// + /// Returns the new state. + #[doc(alias = "AAudioStream_waitForStateChange")] + pub fn wait_for_state_change( + &self, + input_state: AudioStreamState, + timeout_nanoseconds: i64, + ) -> Result { + let value = construct(|ptr| unsafe { + ffi::AAudioStream_waitForStateChange( + self.as_ptr(), + input_state.into(), + ptr, + timeout_nanoseconds, + ) + })?; + Ok(value.into()) + } + + /// Write data to the stream. + /// Returns the number of frames actually written or a negative error. + /// + /// The call will wait until the write is complete or until it runs out of time. + /// If `timeout_nanoseconds` is zero then this call will not wait. + /// + /// Note that `timeout_nanoseconds` is a relative duration in wall clock time. + /// Time will not stop if the thread is asleep. + /// So it will be implemented using `CLOCK_BOOTTIME`. + /// + /// This call is "strong non-blocking" unless it has to wait for room in the buffer. + /// + /// If the call times out then zero or a partial frame count will be returned. + /// + /// # Parameters + /// + /// - `buffer`: The address of the first sample. + /// - `num_frames`: Number of frames to write. Only complete frames will be written. + /// - `timeout_nanoseconds`: Maximum number of nanoseconds to wait for completion. + /// + /// # Safety + /// `buffer` must be a valid pointer to at least `num_frames` samples. + #[doc(alias = "AAudioStream_write")] + pub unsafe fn write( + &self, + buffer: *const c_void, + num_frames: i32, + timeout_nanoseconds: i64, + ) -> Result { + let result = + ffi::AAudioStream_write(self.as_ptr(), buffer, num_frames, timeout_nanoseconds); + + AudioError::from_result(result).map(|()| result as u32) + } +} + +impl Drop for AudioStream { + #[doc(alias = "AAudioStream_close")] + fn drop(&mut self) { + let status = unsafe { ffi::AAudioStream_close(self.as_ptr()) }; + AudioError::from_result(status).unwrap(); + } +} diff --git a/native/vendor/ndk-0.9.0/src/bitmap.rs b/native/vendor/ndk-0.9.0/src/bitmap.rs new file mode 100644 index 0000000..be3687b --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/bitmap.rs @@ -0,0 +1,489 @@ +//! Bindings for [`AndroidBitmap`] functions +//! +//! These functions operate directly on a JNI [`android.graphics.Bitmap`] instance. +//! +//! [`AndroidBitmap`]: https://developer.android.com/ndk/reference/group/bitmap +//! [`android.graphics.Bitmap`]: https://developer.android.com/reference/android/graphics/Bitmap +#![cfg(feature = "bitmap")] + +use jni_sys::{jobject, JNIEnv}; +use num_enum::{FromPrimitive, IntoPrimitive}; +use std::{error, fmt, mem::MaybeUninit}; + +#[cfg(feature = "api-level-30")] +use crate::data_space::DataSpace; +#[cfg(feature = "api-level-30")] +use crate::hardware_buffer::HardwareBufferRef; + +#[repr(i32)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[non_exhaustive] +pub enum BitmapError { + #[doc(alias = "ANDROID_BITMAP_RESULT_ALLOCATION_FAILED")] + AllocationFailed = ffi::ANDROID_BITMAP_RESULT_ALLOCATION_FAILED, + #[doc(alias = "ANDROID_BITMAP_RESULT_BAD_PARAMETER")] + BadParameter = ffi::ANDROID_BITMAP_RESULT_BAD_PARAMETER, + #[doc(alias = "ANDROID_BITMAP_RESULT_JNI_EXCEPTION")] + JniException = ffi::ANDROID_BITMAP_RESULT_JNI_EXCEPTION, + + // Use the SUCCESS discriminant, as no-one will be able to call `as i32` and only has access to + // the constants via `From` provided by `IntoPrimitive` which reads the contained value. + // An autogenerated ` + 1` discriminant is normally fine, except that the + // previous variant is negative and `+ 1` would match the variant before that. + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32) = ffi::ANDROID_BITMAP_RESULT_SUCCESS, +} + +impl fmt::Display for BitmapError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self) + } +} + +impl error::Error for BitmapError {} + +pub type Result = std::result::Result; + +impl BitmapError { + pub(crate) fn from_status(status: i32) -> Result<()> { + match status { + ffi::ANDROID_BITMAP_RESULT_SUCCESS => Ok(()), + x => Err(Self::from(x)), + } + } +} + +fn construct(with_ptr: impl FnOnce(*mut T) -> i32) -> Result { + let mut result = MaybeUninit::uninit(); + let status = with_ptr(result.as_mut_ptr()); + BitmapError::from_status(status).map(|()| unsafe { result.assume_init() }) +} + +#[repr(i32)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, IntoPrimitive, FromPrimitive)] +#[allow(non_camel_case_types)] +#[doc(alias = "AndroidBitmapFormat")] +#[non_exhaustive] +pub enum BitmapFormat { + #[doc(alias = "ANDROID_BITMAP_FORMAT_NONE")] + NONE = ffi::AndroidBitmapFormat::ANDROID_BITMAP_FORMAT_NONE.0 as i32, + #[doc(alias = "ANDROID_BITMAP_FORMAT_RGBA_8888")] + RGBA_8888 = ffi::AndroidBitmapFormat::ANDROID_BITMAP_FORMAT_RGBA_8888.0 as i32, + #[doc(alias = "ANDROID_BITMAP_FORMAT_RGB_565")] + RGB_565 = ffi::AndroidBitmapFormat::ANDROID_BITMAP_FORMAT_RGB_565.0 as i32, + #[deprecated = "Deprecated in API level 13. Because of the poor quality of this configuration, it is advised to use ARGB_8888 instead."] + #[doc(alias = "ANDROID_BITMAP_FORMAT_RGBA_4444")] + RGBA_4444 = ffi::AndroidBitmapFormat::ANDROID_BITMAP_FORMAT_RGBA_4444.0 as i32, + #[doc(alias = "ANDROID_BITMAP_FORMAT_A_8")] + A_8 = ffi::AndroidBitmapFormat::ANDROID_BITMAP_FORMAT_A_8.0 as i32, + #[doc(alias = "ANDROID_BITMAP_FORMAT_RGBA_F16")] + RGBA_F16 = ffi::AndroidBitmapFormat::ANDROID_BITMAP_FORMAT_RGBA_F16.0 as i32, + #[doc(alias = "ANDROID_BITMAP_FORMAT_RGBA_1010102")] + RGBA_1010102 = ffi::AndroidBitmapFormat::ANDROID_BITMAP_FORMAT_RGBA_1010102.0 as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +/// An immediate wrapper over [`android.graphics.Bitmap`] +/// +/// [`android.graphics.Bitmap`]: https://developer.android.com/reference/android/graphics/Bitmap +#[derive(Debug)] +pub struct Bitmap { + env: *mut JNIEnv, + inner: jobject, +} + +impl Bitmap { + /// Create a [`Bitmap`] wrapper from JNI pointers + /// + /// # Safety + /// This function should be called with a healthy JVM pointer and with a non-null + /// [`android.graphics.Bitmap`], which must be kept alive on the Java/Kotlin side. + /// + /// [`android.graphics.Bitmap`]: https://developer.android.com/reference/android/graphics/Bitmap + pub unsafe fn from_jni(env: *mut JNIEnv, bitmap: jobject) -> Self { + Self { env, inner: bitmap } + } + + /// Fills out and returns the [`BitmapInfo`] struct for the given Java bitmap object. + #[doc(alias = "AndroidBitmap_getInfo")] + pub fn info(&self) -> Result { + let inner = + construct(|res| unsafe { ffi::AndroidBitmap_getInfo(self.env, self.inner, res) })?; + + Ok(BitmapInfo { inner }) + } + + /// Returns the [`DataSpace`] of this [`Bitmap`]. + /// + /// Note that [`DataSpace`] only exposes a few values. This may return [`DataSpace::Unknown`], + /// even for Named ColorSpaces, if they have no corresponding [`DataSpace`]. + #[cfg(feature = "api-level-30")] + #[doc(alias = "AndroidBitmap_getDataSpace")] + pub fn data_space(&self) -> DataSpace { + let value = unsafe { ffi::AndroidBitmap_getDataSpace(self.env, self.inner) }; + value.into() + } + + /// Attempt to lock the pixel address. + /// + /// Locking will ensure that the memory for the pixels will not move until the + /// [`Bitmap::unlock_pixels()`] call, and ensure that, if the pixels had been previously purged, + /// they will have been restored. + /// + /// If this call succeeds, it must be balanced by a call to [`Bitmap::unlock_pixels()`], after + /// which time the address of the pixels should no longer be used. + #[doc(alias = "AndroidBitmap_lockPixels")] + pub fn lock_pixels(&self) -> Result<*mut std::os::raw::c_void> { + construct(|res| unsafe { ffi::AndroidBitmap_lockPixels(self.env, self.inner, res) }) + } + + /// Call this to balance a successful call to [`Bitmap::lock_pixels()`]. + #[doc(alias = "AndroidBitmap_unlockPixels")] + pub fn unlock_pixels(&self) -> Result<()> { + let status = unsafe { ffi::AndroidBitmap_unlockPixels(self.env, self.inner) }; + BitmapError::from_status(status) + } + + /// Retrieve the native object associated with an [`ffi::ANDROID_BITMAP_FLAGS_IS_HARDWARE`] + /// [`Bitmap`] (requires [`BitmapInfoFlags::is_hardware()`] on [`BitmapInfo::flags()`] to return + /// [`true`]). + /// + /// Client must not modify it while a [`Bitmap`] is wrapping it. + #[cfg(feature = "api-level-30")] + #[doc(alias = "AndroidBitmap_getHardwareBuffer")] + pub fn hardware_buffer(&self) -> Result { + unsafe { + let result = + construct(|res| ffi::AndroidBitmap_getHardwareBuffer(self.env, self.inner, res))?; + let non_null = if cfg!(debug_assertions) { + std::ptr::NonNull::new(result).expect("result should never be null") + } else { + std::ptr::NonNull::new_unchecked(result) + }; + Ok(HardwareBufferRef::from_ptr(non_null)) + } + } + + /// [Lock] the pixels in `self` and compress them as described by [`info()`]. + /// + /// Unlike [`compress_raw()`] this requires a [`Bitmap`] object (as `self`) backed by a + /// [`jobject`]. + /// + /// # Parameters + /// - `format`: [`BitmapCompressFormat`] to compress to. + /// - `quality`: Hint to the compressor, `0-100`. The value is interpreted differently + /// depending on [`BitmapCompressFormat`]. + /// - `compress_callback`: Closure that writes the compressed data. Will be called on the + /// current thread, each time the compressor has compressed more data that is ready to be + /// written. May be called more than once for each call to this method. + /// + /// [Lock]: Self::lock_pixels() + /// [`info()`]: Self::info() + /// [`compress_raw()`]: Self::compress_raw() + #[cfg(feature = "api-level-30")] + #[doc(alias = "AndroidBitmap_compress")] + pub fn compress Result<(), ()>>( + &self, + format: BitmapCompressFormat, + quality: i32, + compress_callback: F, + ) -> Result<(), BitmapCompressError> { + let info = self.info()?; + let data_space = self.data_space(); + let pixels = self.lock_pixels()?; + // SAFETY: When lock_pixels() succeeds, assume it returns a valid pointer that stays + // valid until we call unlock_pixels(). + let result = unsafe { + Self::compress_raw( + &info, + data_space, + pixels, + format, + quality, + compress_callback, + ) + }; + self.unlock_pixels()?; + result + } + + /// Compress `pixels` as described by `info`. + /// + /// Unlike [`compress()`] this takes a raw pointer to pixels and does not need a [`Bitmap`] + /// object backed by a [`jobject`]. + /// + /// # Parameters + /// - `info`: Description of the pixels to compress. + /// - `data_space`: [`DataSpace`] describing the color space of the pixels. Should _not_ be + /// [`DataSpace::Unknown`] [^1]. + /// - `pixels`: Pointer to pixels to compress. + /// - `format`: [`BitmapCompressFormat`] to compress to. + /// - `quality`: Hint to the compressor, `0-100`. The value is interpreted differently + /// depending on [`BitmapCompressFormat`]. + /// - `compress_callback`: Closure that writes the compressed data. Will be called on the + /// current thread, each time the compressor has compressed more data that is ready to be + /// written. May be called more than once for each call to this method. + /// + /// # Safety + /// `pixels` must point to a valid buffer that matches the size, stride and format in `info`. + /// + /// [`compress()`]: Self::compress() + /// [^1]: + #[cfg(feature = "api-level-30")] + #[doc(alias = "AndroidBitmap_compress")] + pub unsafe fn compress_raw Result<(), ()>>( + info: &BitmapInfo, + data_space: DataSpace, + pixels: *const std::ffi::c_void, + format: BitmapCompressFormat, + quality: i32, + compress_callback: F, + ) -> Result<(), BitmapCompressError> { + if data_space == DataSpace::Unknown { + return Err(BitmapCompressError::DataSpaceUnknown); + } + + use std::{any::Any, ffi::c_void, panic::AssertUnwindSafe}; + struct CallbackState Result<(), ()>> { + callback: F, + panic: Option>, + } + let mut cb_state = CallbackState:: { + callback: compress_callback, + panic: None, + }; + + extern "C" fn compress_cb Result<(), ()>>( + context: *mut c_void, + data: *const c_void, + size: usize, + ) -> bool { + // SAFETY: This callback will only be called serially on a single thread. Both the + // panic state and the FnMut context need to be available mutably. + let cb_state = unsafe { context.cast::>().as_mut() }.unwrap(); + let data = unsafe { std::slice::from_raw_parts(data.cast(), size) }; + let panic = std::panic::catch_unwind(AssertUnwindSafe(|| (cb_state.callback)(data))); + match panic { + Ok(r) => r.is_ok(), + Err(e) => { + cb_state.panic = Some(e); + false + } + } + } + + let status = unsafe { + ffi::AndroidBitmap_compress( + &info.inner, + data_space.into(), + pixels, + format.into(), + quality, + <*mut _>::cast(&mut cb_state), + Some(compress_cb::), + ) + }; + + if let Some(panic) = cb_state.panic { + std::panic::resume_unwind(panic) + } + + Ok(BitmapError::from_status(status)?) + } +} + +/// Possible values for [`ffi::ANDROID_BITMAP_FLAGS_ALPHA_MASK`] within [`BitmapInfoFlags`] +#[repr(u32)] +#[cfg(feature = "api-level-30")] +#[derive(Clone, Copy, Debug, IntoPrimitive, FromPrimitive)] +#[doc(alias = "ANDROID_BITMAP_FLAGS_ALPHA_MASK")] +#[non_exhaustive] +pub enum BitmapInfoFlagsAlpha { + /// Pixel components are premultiplied by alpha. + #[doc(alias = "ANDROID_BITMAP_FLAGS_ALPHA_PREMUL")] + Premultiplied = ffi::ANDROID_BITMAP_FLAGS_ALPHA_PREMUL, + /// Pixels are opaque. + #[doc(alias = "ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE")] + Opaque = ffi::ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE, + /// Pixel components are independent of alpha. + #[doc(alias = "ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL")] + Unpremultiplied = ffi::ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(u32), +} + +/// Bitfield containing information about the bitmap. +#[cfg(feature = "api-level-30")] +#[repr(transparent)] +#[derive(Clone, Copy, Hash, PartialEq, Eq)] +pub struct BitmapInfoFlags(u32); + +#[cfg(feature = "api-level-30")] +impl std::fmt::Debug for BitmapInfoFlags { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "BitmapInfoFlags({:#x}, alpha: {:?}, is_hardware: {})", + self.0, + self.alpha(), + self.is_hardware() + ) + } +} + +#[cfg(feature = "api-level-30")] +impl BitmapInfoFlags { + /// Returns the alpha value contained in the [`ffi::ANDROID_BITMAP_FLAGS_ALPHA_MASK`] bit range + #[doc(alias = "ANDROID_BITMAP_FLAGS_ALPHA_MASK")] + pub fn alpha(self) -> BitmapInfoFlagsAlpha { + // Note that ffi::ANDROID_BITMAP_FLAGS_ALPHA_SHIFT is 0 and hence irrelevant. + (self.0 & ffi::ANDROID_BITMAP_FLAGS_ALPHA_MASK).into() + } + + /// Returns [`true`] when [`ffi::ANDROID_BITMAP_FLAGS_IS_HARDWARE`] is set, meaning this + /// [`Bitmap`] uses "HARDWARE Config" and its [`HardwareBufferRef`] can be retrieved via + /// [`Bitmap::hardware_buffer()`]. + #[doc(alias = "ANDROID_BITMAP_FLAGS_IS_HARDWARE")] + pub fn is_hardware(self) -> bool { + // This constant is defined in a separate anonymous enum which bindgen treats as i32. + (self.0 & ffi::ANDROID_BITMAP_FLAGS_IS_HARDWARE as u32) != 0 + } +} + +/// A native [`AndroidBitmapInfo`] +/// +/// [`AndroidBitmapInfo`]: https://developer.android.com/ndk/reference/struct/android-bitmap-info#struct_android_bitmap_info +#[derive(Clone, Copy)] +#[doc(alias = "AndroidBitmapInfo")] +pub struct BitmapInfo { + inner: ffi::AndroidBitmapInfo, +} + +impl std::fmt::Debug for BitmapInfo { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut f = f.debug_struct("BitmapInfo"); + f.field("width", &self.width()) + .field("height", &self.height()) + .field("stride", &self.stride()) + .field("format", &self.format()); + + #[cfg(feature = "api-level-30")] + f.field("flags", &self.flags()); + + f.finish() + } +} + +impl BitmapInfo { + pub fn new(width: u32, height: u32, stride: u32, format: BitmapFormat) -> Self { + Self { + inner: ffi::AndroidBitmapInfo { + width, + height, + stride, + format: format.into(), + flags: 0, + }, + } + } + + #[cfg(feature = "api-level-30")] + pub fn new_with_flags( + width: u32, + height: u32, + stride: u32, + format: BitmapFormat, + flags: BitmapInfoFlags, + ) -> Self { + Self { + inner: ffi::AndroidBitmapInfo { + flags: flags.0, + ..Self::new(width, height, stride, format).inner + }, + } + } + + /// The bitmap width in pixels. + pub fn width(&self) -> u32 { + self.inner.width + } + + /// The bitmap height in pixels. + pub fn height(&self) -> u32 { + self.inner.height + } + + /// The number of byte per row. + pub fn stride(&self) -> u32 { + self.inner.stride + } + + /// Convert the internal, native [`ffi::AndroidBitmapInfo::format`] into a [`BitmapFormat`]. + pub fn format(&self) -> BitmapFormat { + self.inner.format.into() + } + + /// Bitfield containing information about the bitmap. + #[cfg(feature = "api-level-30")] + pub fn flags(&self) -> BitmapInfoFlags { + BitmapInfoFlags(self.inner.flags) + } +} + +/// Specifies the formats that can be compressed to with [`Bitmap::compress()`] and +/// [`Bitmap::compress_raw()`]. +#[cfg(feature = "api-level-30")] +#[repr(i32)] +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[doc(alias = "AndroidBitmapCompressFormat")] +#[non_exhaustive] +pub enum BitmapCompressFormat { + /// Compress to the JPEG format. + /// + /// quality of `0` means compress for the smallest size. `100` means compress for max visual + /// quality. + #[doc(alias = "ANDROID_BITMAP_COMPRESS_FORMAT_JPEG")] + Jpeg = ffi::AndroidBitmapCompressFormat::ANDROID_BITMAP_COMPRESS_FORMAT_JPEG.0 as i32, + /// Compress to the PNG format. + /// + /// PNG is lossless, so quality is ignored. + #[doc(alias = "ANDROID_BITMAP_COMPRESS_FORMAT_PNG")] + Png = ffi::AndroidBitmapCompressFormat::ANDROID_BITMAP_COMPRESS_FORMAT_PNG.0 as i32, + /// Compress to the WEBP lossless format. + /// + /// quality refers to how much effort to put into compression. A value of `0` means to + /// compress quickly, resulting in a relatively large file size. `100` means to spend more time + /// compressing, resulting in a smaller file. + #[doc(alias = "ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSY")] + WebPLossy = + ffi::AndroidBitmapCompressFormat::ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSY.0 as i32, + /// Compress to the WEBP lossy format. + /// + /// quality of `0` means compress for the smallest size. `100` means compress for max visual quality. + #[doc(alias = "ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSLESS")] + WebPLossless = + ffi::AndroidBitmapCompressFormat::ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSLESS.0 as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +/// Encapsulates possible errors returned by [`Bitmap::compress()`] or [`Bitmap::compress_raw()`]. +#[cfg(feature = "api-level-30")] +#[derive(Debug, thiserror::Error)] +pub enum BitmapCompressError { + #[error(transparent)] + BitmapError(#[from] BitmapError), + /// [`Bitmap`] compression requires a known [`DataSpace`]. [`DataSpace::Unknown`] is invalid + /// even though it is typically treated as `sRGB`, for that [`DataSpace::Srgb`] has to be passed + /// explicitly. + #[error("The dataspace for this Bitmap is Unknown")] + DataSpaceUnknown, +} diff --git a/native/vendor/ndk-0.9.0/src/configuration.rs b/native/vendor/ndk-0.9.0/src/configuration.rs new file mode 100644 index 0000000..fcd98dd --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/configuration.rs @@ -0,0 +1,600 @@ +//! Bindings for [`AConfiguration`] +//! +//! See also the [NDK docs](https://developer.android.com/ndk/reference/group/configuration) for +//! [`AConfiguration`], as well as the [docs for providing +//! resources](https://developer.android.com/guide/topics/resources/providing-resources.html), +//! which explain many of the configuration values. The [`android.content.res.Configuration` +//! javadoc](https://developer.android.com/reference/android/content/res/Configuration.html) may +//! also have useful information. +//! +//! [`AConfiguration`]: https://developer.android.com/ndk/reference/group/configuration#aconfiguration + +use crate::asset::AssetManager; +use num_enum::{FromPrimitive, IntoPrimitive}; +use std::fmt; +use std::ptr::NonNull; + +/// A native [`AConfiguration *`] +/// +/// [`Configuration`] is an opaque type used to get and set various subsystem configurations. +/// +/// [`AConfiguration *`]: https://developer.android.com/ndk/reference/group/configuration#aconfiguration +pub struct Configuration { + ptr: NonNull, +} + +unsafe impl Send for Configuration {} +unsafe impl Sync for Configuration {} + +impl Drop for Configuration { + fn drop(&mut self) { + unsafe { ffi::AConfiguration_delete(self.ptr.as_ptr()) } + } +} + +impl Clone for Configuration { + fn clone(&self) -> Self { + let mut new = Self::new(); + new.copy(self); + new + } +} + +impl PartialEq for Configuration { + fn eq(&self, other: &Self) -> bool { + self.diff(other).0 == 0 + } +} +impl Eq for Configuration {} + +impl fmt::Debug for Configuration { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Configuration") + .field("mcc", &self.mcc()) + .field("mnc", &self.mnc()) + .field("lang", &self.language()) + .field("country", &self.country()) + .field("orientation", &self.orientation()) + .field("touchscreen", &self.touchscreen()) + .field("density", &self.density()) + .field("keyboard", &self.keyboard()) + .field("navigation", &self.navigation()) + .field("keys_hidden", &self.keys_hidden()) + .field("nav_hidden", &self.nav_hidden()) + .field("sdk_version", &self.sdk_version()) + .field("screen_size", &self.screen_size()) + .field("screen_long", &self.screen_long()) + .field("ui_mode_type", &self.ui_mode_type()) + .field("ui_mode_night", &self.ui_mode_night()) + .finish() + } +} + +impl Configuration { + /// Construct a `Configuration` from a pointer. + /// + /// # Safety + /// By calling this function, you assert that it is a valid pointer to a native + /// `AConfiguration`, and give ownership of it to the `Configuration` instance. + pub unsafe fn from_ptr(ptr: NonNull) -> Self { + Self { ptr } + } + + /// Create a new `Configuration`, with the same contents as the `AConfiguration` referenced by + /// the pointer. + /// + /// This is useful if you have a pointer, but not ownership of it. + /// + /// # Safety + /// By calling this function, you assert that it is a valid pointer to a native + /// `AConfiguration`. + pub unsafe fn clone_from_ptr(ptr: NonNull) -> Self { + let conf = Self::new(); + ffi::AConfiguration_copy(conf.ptr.as_ptr(), ptr.as_ptr()); + conf + } + + /// The pointer to the native `AConfiguration`. Keep in mind that the `Configuration` object + /// still has ownership, and will free it when dropped. + pub fn ptr(&self) -> NonNull { + self.ptr + } + + pub fn from_asset_manager(am: &AssetManager) -> Self { + let config = Self::new(); + unsafe { + ffi::AConfiguration_fromAssetManager(config.ptr().as_mut(), am.ptr().as_mut()); + } + config + } + + /// Create a new `Configuration`, with none of the values set. + pub fn new() -> Self { + unsafe { + Self { + ptr: NonNull::new(ffi::AConfiguration_new()).unwrap(), + } + } + } + + /// `dest.copy(&src)` copies the contents of `src` to `dest` + pub fn copy(&mut self, other: &Self) { + unsafe { ffi::AConfiguration_copy(self.ptr.as_ptr(), other.ptr.as_ptr()) } + } + + /// Information about what fields differ between the two configurations + pub fn diff(&self, other: &Self) -> DiffResult { + unsafe { + DiffResult(ffi::AConfiguration_diff(self.ptr.as_ptr(), other.ptr.as_ptr()) as u32) + } + } + + /// Returns false if anything in `self` conflicts with `requested` + pub fn matches(&self, requested: &Self) -> bool { + unsafe { ffi::AConfiguration_match(self.ptr.as_ptr(), requested.ptr.as_ptr()) != 0 } + } + + /// Returns the country code, as a [`String`] of two characters, if set + pub fn country(&self) -> Option { + let mut chars = [0u8; 2]; + unsafe { + ffi::AConfiguration_getCountry(self.ptr.as_ptr(), chars.as_mut_ptr().cast()); + } + if chars[0] == 0 { + None + } else { + Some(std::str::from_utf8(chars.as_slice()).unwrap().to_owned()) + } + } + + /// Returns the screen density in dpi. + /// + /// On some devices it can return values outside of the density enum. + pub fn density(&self) -> Option { + let density = unsafe { ffi::AConfiguration_getDensity(self.ptr.as_ptr()) as u32 }; + match density { + ffi::ACONFIGURATION_DENSITY_DEFAULT => Some(160), + ffi::ACONFIGURATION_DENSITY_ANY => None, + ffi::ACONFIGURATION_DENSITY_NONE => None, + density => Some(density), + } + } + + /// Returns the keyboard type. + pub fn keyboard(&self) -> Keyboard { + unsafe { ffi::AConfiguration_getKeyboard(self.ptr.as_ptr()).into() } + } + + /// Returns keyboard visibility/availability. + pub fn keys_hidden(&self) -> KeysHidden { + unsafe { ffi::AConfiguration_getKeysHidden(self.ptr.as_ptr()).into() } + } + + /// Returns the language, as a [`String`] of two characters, if set + pub fn language(&self) -> Option { + let mut chars = [0u8; 2]; + unsafe { + ffi::AConfiguration_getLanguage(self.ptr.as_ptr(), chars.as_mut_ptr().cast()); + } + if chars[0] == 0 { + None + } else { + Some(std::str::from_utf8(chars.as_slice()).unwrap().to_owned()) + } + } + + /// Returns the layout direction + pub fn layout_direction(&self) -> LayoutDir { + unsafe { ffi::AConfiguration_getLayoutDirection(self.ptr.as_ptr()).into() } + } + + /// Returns the mobile country code. + pub fn mcc(&self) -> i32 { + unsafe { ffi::AConfiguration_getMcc(self.ptr.as_ptr()) } + } + + /// Returns the mobile network code, if one is defined + pub fn mnc(&self) -> Option { + unsafe { + match ffi::AConfiguration_getMnc(self.ptr.as_ptr()) { + 0 => None, + x if x == ffi::ACONFIGURATION_MNC_ZERO as i32 => Some(0), + x => Some(x), + } + } + } + + pub fn nav_hidden(&self) -> NavHidden { + unsafe { ffi::AConfiguration_getNavHidden(self.ptr.as_ptr()).into() } + } + + pub fn navigation(&self) -> Navigation { + unsafe { ffi::AConfiguration_getNavigation(self.ptr.as_ptr()).into() } + } + + pub fn orientation(&self) -> Orientation { + unsafe { ffi::AConfiguration_getOrientation(self.ptr.as_ptr()).into() } + } + + pub fn screen_height_dp(&self) -> Option { + unsafe { + let height = ffi::AConfiguration_getScreenHeightDp(self.ptr.as_ptr()); + if height == ffi::ACONFIGURATION_SCREEN_HEIGHT_DP_ANY as i32 { + None + } else { + Some(height) + } + } + } + + pub fn screen_width_dp(&self) -> Option { + unsafe { + let width = ffi::AConfiguration_getScreenWidthDp(self.ptr.as_ptr()); + if width == ffi::ACONFIGURATION_SCREEN_WIDTH_DP_ANY as i32 { + None + } else { + Some(width) + } + } + } + + pub fn screen_long(&self) -> ScreenLong { + unsafe { ffi::AConfiguration_getScreenLong(self.ptr.as_ptr()).into() } + } + + #[cfg(feature = "api-level-30")] + pub fn screen_round(&self) -> ScreenRound { + unsafe { ffi::AConfiguration_getScreenRound(self.ptr.as_ptr()).into() } + } + + pub fn screen_size(&self) -> ScreenSize { + unsafe { ffi::AConfiguration_getScreenSize(self.ptr.as_ptr()).into() } + } + + pub fn sdk_version(&self) -> i32 { + unsafe { ffi::AConfiguration_getSdkVersion(self.ptr.as_ptr()) } + } + + pub fn smallest_screen_width_dp(&self) -> Option { + unsafe { + let width = ffi::AConfiguration_getSmallestScreenWidthDp(self.ptr.as_ptr()); + if width == ffi::ACONFIGURATION_SMALLEST_SCREEN_WIDTH_DP_ANY as i32 { + None + } else { + Some(width) + } + } + } + + pub fn touchscreen(&self) -> Touchscreen { + unsafe { ffi::AConfiguration_getTouchscreen(self.ptr.as_ptr()).into() } + } + + pub fn ui_mode_night(&self) -> UiModeNight { + unsafe { ffi::AConfiguration_getUiModeNight(self.ptr.as_ptr()).into() } + } + + pub fn ui_mode_type(&self) -> UiModeType { + unsafe { ffi::AConfiguration_getUiModeType(self.ptr.as_ptr()).into() } + } +} + +/// A bitfield representing the differences between two [`Configuration`]s +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct DiffResult(pub u32); + +impl DiffResult { + pub fn mcc(self) -> bool { + self.0 & ffi::ACONFIGURATION_MCC != 0 + } + pub fn mnc(self) -> bool { + self.0 & ffi::ACONFIGURATION_MNC != 0 + } + pub fn locale(self) -> bool { + self.0 & ffi::ACONFIGURATION_LOCALE != 0 + } + pub fn touchscreen(self) -> bool { + self.0 & ffi::ACONFIGURATION_TOUCHSCREEN != 0 + } + pub fn keyboard(self) -> bool { + self.0 & ffi::ACONFIGURATION_KEYBOARD != 0 + } + pub fn keyboard_hidden(self) -> bool { + self.0 & ffi::ACONFIGURATION_KEYBOARD_HIDDEN != 0 + } + pub fn navigation(self) -> bool { + self.0 & ffi::ACONFIGURATION_NAVIGATION != 0 + } + pub fn orientation(self) -> bool { + self.0 & ffi::ACONFIGURATION_ORIENTATION != 0 + } + pub fn density(self) -> bool { + self.0 & ffi::ACONFIGURATION_DENSITY != 0 + } + pub fn screen_size(self) -> bool { + self.0 & ffi::ACONFIGURATION_SCREEN_SIZE != 0 + } + pub fn version(self) -> bool { + self.0 & ffi::ACONFIGURATION_VERSION != 0 + } + pub fn screen_layout(self) -> bool { + self.0 & ffi::ACONFIGURATION_SCREEN_LAYOUT != 0 + } + pub fn ui_mode(self) -> bool { + self.0 & ffi::ACONFIGURATION_UI_MODE != 0 + } + pub fn smallest_screen_size(self) -> bool { + self.0 & ffi::ACONFIGURATION_SMALLEST_SCREEN_SIZE != 0 + } + pub fn layout_dir(self) -> bool { + self.0 & ffi::ACONFIGURATION_LAYOUTDIR != 0 + } + pub fn screen_round(self) -> bool { + self.0 & ffi::ACONFIGURATION_SCREEN_ROUND != 0 + } + pub fn color_mode(self) -> bool { + self.0 & ffi::ACONFIGURATION_COLOR_MODE != 0 + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum Orientation { + Any = ffi::ACONFIGURATION_ORIENTATION_ANY as i32, + Port = ffi::ACONFIGURATION_ORIENTATION_PORT as i32, + Land = ffi::ACONFIGURATION_ORIENTATION_LAND as i32, + Square = ffi::ACONFIGURATION_ORIENTATION_SQUARE as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum Touchscreen { + Any = ffi::ACONFIGURATION_TOUCHSCREEN_ANY as i32, + NoTouch = ffi::ACONFIGURATION_TOUCHSCREEN_NOTOUCH as i32, + Stylus = ffi::ACONFIGURATION_TOUCHSCREEN_STYLUS as i32, + Finger = ffi::ACONFIGURATION_TOUCHSCREEN_FINGER as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum Density { + Default = ffi::ACONFIGURATION_DENSITY_DEFAULT as i32, + Low = ffi::ACONFIGURATION_DENSITY_LOW as i32, + Medium = ffi::ACONFIGURATION_DENSITY_MEDIUM as i32, + TV = ffi::ACONFIGURATION_DENSITY_TV as i32, + High = ffi::ACONFIGURATION_DENSITY_HIGH as i32, + XHigh = ffi::ACONFIGURATION_DENSITY_XHIGH as i32, + XXHigh = ffi::ACONFIGURATION_DENSITY_XXHIGH as i32, + XXXHigh = ffi::ACONFIGURATION_DENSITY_XXXHIGH as i32, + Any = ffi::ACONFIGURATION_DENSITY_ANY as i32, + None = ffi::ACONFIGURATION_DENSITY_NONE as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +impl Density { + /// The DPI associated with the density class. + /// See [the Android screen density + /// docs](https://developer.android.com/training/multiscreen/screendensities#TaskProvideAltBmp) + /// + /// There are some [`Density`] values that have no associated DPI; these values return [`None`]. + pub fn dpi(self) -> Option { + match self { + Self::Default => Some(160), // Or should it be None? + Self::Low => Some(120), + Self::Medium => Some(160), + Self::High => Some(240), + Self::XHigh => Some(320), + Self::XXHigh => Some(480), + Self::XXXHigh => Some(640), + Self::TV => Some(213), + Self::Any => None, + Self::None => None, + // TODO + Self::__Unknown(v) => Some(v as u32), + } + } + + /// The Hi-DPI factor associated with the density class. This is the factor by which an + /// image/resource should be scaled to match its size across devices. The baseline is a 160dpi + /// screen (i.e., Hi-DPI factor = DPI / 160). + /// See [the Android screen density + /// docs](https://developer.android.com/training/multiscreen/screendensities#TaskProvideAltBmp) + /// + /// There are some [`Density`] values that have no associated DPI; these values return [`None`]. + pub fn approx_hidpi_factor(self) -> Option { + match self { + Self::Default => Some(1.), // Or should it be None? + Self::Low => Some(0.75), + Self::Medium => Some(1.), + Self::High => Some(1.5), + Self::XHigh => Some(2.), + Self::XXHigh => Some(3.), + Self::XXXHigh => Some(4.), + Self::TV => Some(4. / 3.), + Self::Any => None, + Self::None => None, + Self::__Unknown(_) => None, + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum Keyboard { + Any = ffi::ACONFIGURATION_KEYBOARD_ANY as i32, + NoKeys = ffi::ACONFIGURATION_KEYBOARD_NOKEYS as i32, + Qwerty = ffi::ACONFIGURATION_KEYBOARD_QWERTY as i32, + TwelveKey = ffi::ACONFIGURATION_KEYBOARD_12KEY as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum Navigation { + Any = ffi::ACONFIGURATION_NAVIGATION_ANY as i32, + NoNav = ffi::ACONFIGURATION_NAVIGATION_NONAV as i32, + DPad = ffi::ACONFIGURATION_NAVIGATION_DPAD as i32, + Trackball = ffi::ACONFIGURATION_NAVIGATION_TRACKBALL as i32, + Wheel = ffi::ACONFIGURATION_NAVIGATION_WHEEL as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum KeysHidden { + Any = ffi::ACONFIGURATION_KEYSHIDDEN_ANY as i32, + No = ffi::ACONFIGURATION_KEYSHIDDEN_NO as i32, + Yes = ffi::ACONFIGURATION_KEYSHIDDEN_YES as i32, + Soft = ffi::ACONFIGURATION_KEYSHIDDEN_SOFT as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum NavHidden { + Any = ffi::ACONFIGURATION_NAVHIDDEN_ANY as i32, + No = ffi::ACONFIGURATION_NAVHIDDEN_NO as i32, + Yes = ffi::ACONFIGURATION_NAVHIDDEN_YES as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum ScreenSize { + Any = ffi::ACONFIGURATION_SCREENSIZE_ANY as i32, + Small = ffi::ACONFIGURATION_SCREENSIZE_SMALL as i32, + Normal = ffi::ACONFIGURATION_SCREENSIZE_NORMAL as i32, + Large = ffi::ACONFIGURATION_SCREENSIZE_LARGE as i32, + XLarge = ffi::ACONFIGURATION_SCREENSIZE_XLARGE as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum ScreenLong { + Any = ffi::ACONFIGURATION_SCREENLONG_ANY as i32, + No = ffi::ACONFIGURATION_SCREENLONG_NO as i32, + Yes = ffi::ACONFIGURATION_SCREENLONG_YES as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum ScreenRound { + Any = ffi::ACONFIGURATION_SCREENROUND_ANY as i32, + No = ffi::ACONFIGURATION_SCREENROUND_NO as i32, + Yes = ffi::ACONFIGURATION_SCREENROUND_YES as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum WideColorGamut { + Any = ffi::ACONFIGURATION_WIDE_COLOR_GAMUT_ANY as i32, + No = ffi::ACONFIGURATION_WIDE_COLOR_GAMUT_NO as i32, + Yes = ffi::ACONFIGURATION_WIDE_COLOR_GAMUT_YES as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum HDR { + Any = ffi::ACONFIGURATION_HDR_ANY as i32, + No = ffi::ACONFIGURATION_HDR_NO as i32, + Yes = ffi::ACONFIGURATION_HDR_YES as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum LayoutDir { + Any = ffi::ACONFIGURATION_LAYOUTDIR_ANY as i32, + Ltr = ffi::ACONFIGURATION_LAYOUTDIR_LTR as i32, + Rtl = ffi::ACONFIGURATION_LAYOUTDIR_RTL as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum UiModeType { + Any = ffi::ACONFIGURATION_UI_MODE_TYPE_ANY as i32, + Normal = ffi::ACONFIGURATION_UI_MODE_TYPE_NORMAL as i32, + Desk = ffi::ACONFIGURATION_UI_MODE_TYPE_DESK as i32, + Car = ffi::ACONFIGURATION_UI_MODE_TYPE_CAR as i32, + Television = ffi::ACONFIGURATION_UI_MODE_TYPE_TELEVISION as i32, + Applicance = ffi::ACONFIGURATION_UI_MODE_TYPE_APPLIANCE as i32, + Watch = ffi::ACONFIGURATION_UI_MODE_TYPE_WATCH as i32, + VrHeadset = ffi::ACONFIGURATION_UI_MODE_TYPE_VR_HEADSET as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum UiModeNight { + Any = ffi::ACONFIGURATION_UI_MODE_NIGHT_ANY as i32, + No = ffi::ACONFIGURATION_UI_MODE_NIGHT_NO as i32, + Yes = ffi::ACONFIGURATION_UI_MODE_NIGHT_YES as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} diff --git a/native/vendor/ndk-0.9.0/src/data_space.rs b/native/vendor/ndk-0.9.0/src/data_space.rs new file mode 100644 index 0000000..f25691d --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/data_space.rs @@ -0,0 +1,648 @@ +//! Bindings for [`ADataSpace`] +//! +//! [`ADataSpace`]: https://developer.android.com/ndk/reference/group/a-data-space#group___a_data_space_1ga2759ad19cae46646cc5f7002758c4a1c +#![cfg(feature = "api-level-28")] + +use std::fmt; + +use num_enum::{FromPrimitive, IntoPrimitive}; + +/// Describes how to interpret colors. +/// +/// +#[repr(i32)] +#[derive(Clone, Copy, Hash, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[doc(alias = "ADataSpace")] +#[non_exhaustive] +pub enum DataSpace { + /// Default-assumption data space, when not explicitly specified. + /// + /// It is safest to assume the buffer is an image with `sRGB` primaries and encoding ranges, + /// but the consumer and/or the producer of the data may simply be using defaults. No automatic + /// gamma transform should be expected, except for a possible display gamma transform when drawn + /// to a screen. + #[doc(alias = "ADATASPACE_UNKNOWN")] + Unknown = ffi::ADataSpace::ADATASPACE_UNKNOWN.0, + + /// Adobe RGB. + /// + /// Uses [full range], [gamma `2.2` transfer] and [Adobe RGB standard]. + /// + /// Note: Application is responsible for gamma encoding the data as a `2.2` gamma encoding is + /// not supported in HW. + /// + /// [full range]: DataSpaceRange::Full + /// [gamma `2.2` transfer]: DataSpaceTransfer::Gamma2_2 + /// [Adobe RGB standard]: DataSpaceStandard::AdobeRgb + #[doc(alias = "ADATASPACE_ADOBE_RGB")] + AdobeRgb = ffi::ADataSpace::ADATASPACE_ADOBE_RGB.0, + /// ITU-R Recommendation 2020 (`BT.2020`). + /// + /// Ultra High-definition television. + /// + /// Uses [full range], [`SMPTE 170M` transfer] and [`BT2020` standard]. + /// + /// [full range]: DataSpaceRange::Full + /// [`SMPTE 170M` transfer]: DataSpaceTransfer::Smpte170M + /// [`BT2020` standard]: DataSpaceStandard::Bt2020 + #[doc(alias = "ADATASPACE_BT2020")] + Bt2020 = ffi::ADataSpace::ADATASPACE_BT2020.0, + /// Hybrid Log Gamma encoding. + /// + /// Uses [full range], [hybrid log gamma transfer] and [`BT2020` standard]. + /// + /// [full range]: DataSpaceRange::Full + /// [hybrid log gamma transfer]: DataSpaceTransfer::HLG + /// [`BT2020` standard]: DataSpaceStandard::Bt2020 + #[doc(alias = "ADATASPACE_BT2020_HLG")] + Bt2020Hlg = ffi::ADataSpace::ADATASPACE_BT2020_HLG.0, + /// ITU Hybrid Log Gamma encoding. + /// + /// Uses [limited range], [hybrid log gamma transfer] and [`BT2020` standard]. + /// + /// [limited range]: DataSpaceRange::Limited + /// [hybrid log gamma transfer]: DataSpaceTransfer::HLG + /// [`BT2020` standard]: DataSpaceStandard::Bt2020 + #[doc(alias = "ADATASPACE_BT2020_ITU_HLG")] + Bt2020ItuHlg = ffi::ADataSpace::ADATASPACE_BT2020_ITU_HLG.0, + /// ITU-R Recommendation 2020 (`BT.2020`). + /// + /// Ultra High-definition television. + /// + /// Uses [limited range], [`SMPTE 2084 (PQ)` transfer] and [`BT2020` standard]. + /// + /// [limited range]: DataSpaceRange::Limited + /// [`SMPTE 2084 (PQ)` transfer]: DataSpaceTransfer::St2084 + /// [`BT2020` standard]: DataSpaceStandard::Bt2020 + #[doc(alias = "ADATASPACE_BT2020_ITU_PQ")] + Bt2020ItuPq = ffi::ADataSpace::ADATASPACE_BT2020_ITU_PQ.0, + /// ITU-R Recommendation 2020 (`BT.2020`). + /// + /// Ultra High-definition television. + /// + /// Uses [full range], [`SMPTE 2084 (PQ)` transfer] and [`BT2020` standard]. + /// + /// [full range]: DataSpaceRange::Full + /// [`SMPTE 2084 (PQ)` transfer]: DataSpaceTransfer::St2084 + /// [`BT2020` standard]: DataSpaceStandard::Bt2020 + #[doc(alias = "ADATASPACE_BT2020_PQ")] + Bt2020Pq = ffi::ADataSpace::ADATASPACE_BT2020_PQ.0, + /// ITU-R Recommendation 601 (`BT.601`) - 525-line. + /// + /// Standard-definition television, 525 Lines (NTSC). + /// + /// Uses [limited range], [`SMPTE 170M` transfer] and [`BT.601_525` standard]. + /// + /// [limited range]: DataSpaceRange::Limited + /// [`SMPTE 170M` transfer]: DataSpaceTransfer::Smpte170M + /// [`BT.601_525` standard]: DataSpaceStandard::Bt601_525 + #[doc(alias = "ADATASPACE_BT601_525")] + Bt601_525 = ffi::ADataSpace::ADATASPACE_BT601_525.0, + /// ITU-R Recommendation 601 (`BT.601`) - 625-line. + /// + /// Standard-definition television, 625 Lines (PAL). + /// + /// Uses [limited range], [`SMPTE 170M` transfer] and [`BT.601_625` standard]. + /// + /// [limited range]: DataSpaceRange::Limited + /// [`SMPTE 170M` transfer]: DataSpaceTransfer::Smpte170M + /// [`BT.601_625` standard]: DataSpaceStandard::Bt601_625 + #[doc(alias = "ADATASPACE_BT601_625")] + Bt601_625 = ffi::ADataSpace::ADATASPACE_BT601_625.0, + /// ITU-R Recommendation 709 (`BT.709`). + /// + /// High-definition television. + /// + /// Uses [limited range], [`SMPTE 170M` transfer] and [`BT.709` standard]. + /// + /// [limited range]: DataSpaceRange::Limited + /// [`SMPTE 170M` transfer]: DataSpaceTransfer::Smpte170M + /// [`BT.709` standard]: DataSpaceStandard::Bt709 + #[doc(alias = "ADATASPACE_BT709")] + Bt709 = ffi::ADataSpace::ADATASPACE_BT709.0, + /// `SMPTE EG 432-1` and `SMPTE RP 431-2`. + /// + /// Digital Cinema `DCI-P3`. + /// + /// Uses [full range], [gamma `2.6` transfer] and [`D65` `DCI-P3` standard]. + /// + /// Note: Application is responsible for gamma encoding the data as a `2.6` gamma encoding is + /// not supported in HW. + /// + /// [full range]: DataSpaceRange::Full + /// [gamma `2.6` transfer]: DataSpaceTransfer::Gamma2_6 + /// [`D65` `DCI-P3` standard]: DataSpaceStandard::DciP3 + #[doc(alias = "ADATASPACE_DCI_P3")] + DciP3 = ffi::ADataSpace::ADATASPACE_DCI_P3.0, + /// Display P3. + /// + /// Uses [full range], [`sRGB` transfer] and [`D65` `DCI-P3` standard]. + /// + /// [full range]: DataSpaceRange::Full + /// [`sRGB` transfer]: DataSpaceTransfer::Srgb + /// [`D65` `DCI-P3` standard]: DataSpaceStandard::DciP3 + #[doc(alias = "ADATASPACE_DISPLAY_P3")] + DisplayP3 = ffi::ADataSpace::ADATASPACE_DISPLAY_P3.0, + /// JPEG File Interchange Format (`JFIF`). + /// + /// Same model as `BT.601-625`, but all values (`Y`, `Cb`, `Cr`) range from `0` to `255`. + /// + /// Uses [full range], [`SMPTE 170M` transfer] and [`BT.601_625` standard]. + /// + /// [full range]: DataSpaceRange::Full + /// [`SMPTE 170M` transfer]: DataSpaceTransfer::Smpte170M + /// [`BT.601_625` standard]: DataSpaceStandard::Bt601_625 + #[doc(alias = "ADATASPACE_JFIF")] + Jfif = ffi::ADataSpace::ADATASPACE_JFIF.0, + /// `scRGB`. + /// + /// The `red`, `green`, and `blue` components are stored in [extended][extended range] `sRGB` + /// space, and gamma- encoded using the [`sRGB` transfer] function. + /// + /// The values are floating point. A pixel value of `1.0`, `1.0`, `1.0` corresponds to `sRGB` + /// white (`D65`) at `80` nits. Values beyond the range `[0.0 - 1.0]` would correspond to other + /// colors spaces and/or HDR content. + /// + /// Uses [extended range], [`sRGB` transfer] and [`BT.709` standard]. + /// + /// [extended range]: DataSpaceRange::Extended + /// [`sRGB` transfer]: DataSpaceTransfer::Srgb + /// [`BT.709` standard]: DataSpaceStandard::Bt709 + #[doc(alias = "ADATASPACE_SCRGB")] + Scrgb = ffi::ADataSpace::ADATASPACE_SCRGB.0, + /// `scRGB` linear encoding + /// + /// The `red`, `green`, and `blue` components are stored in [extended][extended range] `sRGB` + /// space, but are linear, not gamma-encoded. + /// + /// The values are floating point. A pixel value of `1.0`, `1.0`, `1.0` corresponds to `sRGB` + /// white (`D65`) at `80` nits. Values beyond the range `[0.0 - 1.0]` would correspond to other + /// colors spaces and/or HDR content. + /// + /// Uses [extended range], [linear transfer] and [`BT.709` standard]. + /// + /// [extended range]: DataSpaceRange::Extended + /// [linear transfer]: DataSpaceTransfer::Linear + /// [`BT.709` standard]: DataSpaceStandard::Bt709 + #[doc(alias = "ADATASPACE_SCRGB_LINEAR")] + ScrgbLinear = ffi::ADataSpace::ADATASPACE_SCRGB_LINEAR.0, + /// `sRGB` gamma encoding. + /// + /// The `red`, `green` and `blue` components are stored in `sRGB` space, and converted to linear + /// space when read, using the [`sRGB` transfer] function for each of the `R`, `G` and `B` + /// components. When written, the inverse transformation is performed. + /// + /// The `alpha` component, if present, is always stored in linear space and is left unmodified + /// when read or written. + /// + /// Uses [full range], [`sRGB` transfer] and [`BT.709` standard]. + /// + /// [full range]: DataSpaceRange::Full + /// [`sRGB` transfer]: DataSpaceTransfer::Srgb + /// [`BT.709` standard]: DataSpaceStandard::Bt709 + #[doc(alias = "ADATASPACE_SRGB")] + Srgb = ffi::ADataSpace::ADATASPACE_SRGB.0, + /// `sRGB` linear encoding. + /// + /// The `red`, `green`, and `blue` components are stored in `sRGB` space, but are linear, not + /// gamma-encoded. The `RGB` primaries and the white point are the same as [`BT.709]`. + /// + /// The values are encoded using the [full range] (`[0, 255]` for 8-bit) for all components. + /// + /// Uses [full range], [linear transfer] and [`BT.709` standard]. + /// + /// [full range]: DataSpaceRange::Full + /// [linear transfer]: DataSpaceTransfer::Linear + /// [`BT.709` standard]: DataSpaceStandard::Bt709 + #[doc(alias = "ADATASPACE_SRGB_LINEAR")] + SrgbLinear = ffi::ADataSpace::ADATASPACE_SRGB_LINEAR.0, + + /// Depth. + /// + /// This value is valid with formats [`HAL_PIXEL_FORMAT_Y16`] and [`HAL_PIXEL_FORMAT_BLOB`]. + /// + /// [`HAL_PIXEL_FORMAT_Y16`]: https://cs.android.com/android/platform/superproject/main/+/main:frameworks/native/libs/nativewindow/include/vndk/hardware_buffer.h;l=74-75;drc=45317f5c7c966fc816843217adc96a2ddea8bf29 + /// [`HAL_PIXEL_FORMAT_BLOB`]: super::hardware_buffer_format::HardwareBufferFormat::BLOB + #[doc(alias = "ADATASPACE_DEPTH")] + Depth = ffi::ADataSpace::ADATASPACE_DEPTH.0, + /// ISO `16684-1:2011(E)` Dynamic Depth. + /// + /// Embedded depth metadata following the dynamic depth specification. + #[doc(alias = "ADATASPACE_DYNAMIC_DEPTH")] + DynamicDepth = ffi::ADataSpace::ADATASPACE_DYNAMIC_DEPTH.0, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +impl fmt::Display for DataSpace { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(match self { + Self::Unknown => "Unknown", + Self::AdobeRgb => "AdobeRgb", + Self::Bt2020 => "Bt2020", + Self::Bt2020Hlg => "Bt2020Hlg", + Self::Bt2020ItuHlg => "Bt2020ItuHlg", + Self::Bt2020ItuPq => "Bt2020ItuPq", + Self::Bt2020Pq => "Bt2020Pq", + Self::Bt601_525 => "Bt601_525", + Self::Bt601_625 => "Bt601_625", + Self::Bt709 => "Bt709", + Self::DciP3 => "DciP3", + Self::DisplayP3 => "DisplayP3", + Self::Jfif => "Jfif", + Self::Scrgb => "Scrgb", + Self::ScrgbLinear => "ScrgbLinear", + Self::Srgb => "Srgb", + Self::SrgbLinear => "SrgbLinear", + Self::Depth => "Depth", + Self::DynamicDepth => "DynamicDepth", + Self::__Unknown(u) => { + return write!( + f, + "Unknown DataSpace({u:x?}, standard: {:?}, transfer: {:?}, range: {:?})", + self.standard(), + self.transfer(), + self.range() + ) + } + }) + } +} + +impl fmt::Debug for DataSpace { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "DataSpace({self}, standard: {:?}, transfer: {:?}, range: {:?})", + self.standard(), + self.transfer(), + self.range(), + ) + } +} + +impl DataSpace { + /// Construct a [`DataSpace`] from individual `standard`, `transfer` and `range` components. + /// + /// Together these should correspond to a single format. + pub fn from_parts( + standard: DataSpaceStandard, + transfer: DataSpaceTransfer, + range: DataSpaceRange, + ) -> Self { + Self::from(i32::from(standard) | i32::from(transfer) | i32::from(range)) + } + + /// Extracts and returns the color-description aspect from this [`DataSpace`]. + #[doc(alias = "STANDARD_MASK")] + pub fn standard(self) -> DataSpaceStandard { + let standard = i32::from(self) & ffi::ADataSpace::STANDARD_MASK.0; + standard.into() + } + + /// Extracts and returns the transfer aspect from this [`DataSpace`]. + #[doc(alias = "TRANSFER_MASK")] + pub fn transfer(self) -> DataSpaceTransfer { + let transfer = i32::from(self) & ffi::ADataSpace::TRANSFER_MASK.0; + transfer.into() + } + + /// Extracts and returns the range aspect from this [`DataSpace`]. + #[doc(alias = "RANGE_MASK")] + pub fn range(self) -> DataSpaceRange { + let range = i32::from(self) & ffi::ADataSpace::RANGE_MASK.0; + range.into() + } +} + +/// Color-description aspects. +/// +/// The following aspects define various characteristics of the color specification. These represent +/// bitfields, so that a data space value can specify each of them independently. Standard aspect +/// defines the chromaticity coordinates of the source primaries in terms of the CIE 1931 definition +/// of `x` and `y` specified in ISO 11664-1. +#[repr(i32)] +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[doc(alias = "STANDARD_MASK")] +#[non_exhaustive] +pub enum DataSpaceStandard { + /// Chromacity coordinates are unknown or are determined by the application. Implementations + /// shall use the following suggested standards: + /// + /// All `YCbCr` formats: [`BT.709`] if size is `720p` or larger (since most video content is + /// letterboxed this corresponds to width is `1280` or greater, or height + /// is 720 or greater). [`BT.601_625`] if size is smaller than `720p` or + /// is `JPEG`. + /// All `RGB` formats: [`BT.709`]. + /// + /// For all other formats the standard is undefined, and implementations should use an + /// appropriate standard for the data represented. + /// + /// [`BT.709`]: Self::Bt709 + /// [`BT.601_625`]: Self::Bt601_625 + #[doc(alias = "STANDARD_UNSPECIFIED")] + Unspecified = ffi::ADataSpace::STANDARD_UNSPECIFIED.0, + /// | Primaries | x | y | + /// | ----------- | ------ | ------ | + /// | green | 0.300 | 0.600 | + /// | blue | 0.150 | 0.060 | + /// | red | 0.640 | 0.330 | + /// | white (D65) | 0.3127 | 0.3290 | + /// + /// Use the unadjusted `KR = 0.2126`, `KB = 0.0722` luminance interpretation for `RGB` + /// conversion. + #[doc(alias = "STANDARD_BT709")] + Bt709 = ffi::ADataSpace::STANDARD_BT709.0, + /// | Primaries | x | y | + /// | ----------- | ------ | ------ | + /// | green | 0.290 | 0.600 | + /// | blue | 0.150 | 0.060 | + /// | red | 0.640 | 0.330 | + /// | white (D65) | 0.3127 | 0.3290 | + /// + /// `KR = 0.299`, `KB = 0.114`. This adjusts the luminance interpretation for `RGB` conversion + /// from the one purely determined by the primaries to minimize the color shift into `RGB` + /// space that uses [`BT.709`] primaries. + /// + /// [`BT.709`]: Self::Bt709 + #[doc(alias = "STANDARD_BT601_625")] + Bt601_625 = ffi::ADataSpace::STANDARD_BT601_625.0, + /// | Primaries | x | y | + /// | ----------- | ------ | ------ | + /// | green | 0.290 | 0.600 | + /// | blue | 0.150 | 0.060 | + /// | red | 0.640 | 0.330 | + /// | white (D65) | 0.3127 | 0.3290 | + /// + /// Use the unadjusted `KR = 0.222`, `KB = 0.071` luminance interpretation for `RGB` conversion. + #[doc(alias = "STANDARD_BT601_625_UNADJUSTED")] + Bt601_625Unadjusted = ffi::ADataSpace::STANDARD_BT601_625_UNADJUSTED.0, + /// | Primaries | x | y | + /// | ----------- | ------ | ------ | + /// | green | 0.310 | 0.595 | + /// | blue | 0.155 | 0.070 | + /// | red | 0.630 | 0.340 | + /// | white (D65) | 0.3127 | 0.3290 | + /// + /// `KR = 0.299`, `KB = 0.114`. This adjusts the luminance interpretation for `RGB` conversion + /// from the one purely determined by the primaries to minimize the color shift into `RGB` space + /// that uses [`BT.709`] primaries. + /// + /// [`BT.709`]: Self::Bt709 + #[doc(alias = "STANDARD_BT601_525")] + Bt601_525 = ffi::ADataSpace::STANDARD_BT601_525.0, + /// | Primaries | x | y | + /// | ----------- | ------ | ------ | + /// | green | 0.310 | 0.595 | + /// | blue | 0.155 | 0.070 | + /// | red | 0.630 | 0.340 | + /// | white (D65) | 0.3127 | 0.3290 | + /// + /// Use the unadjusted `KR = 0.212`, `KB = 0.087` luminance interpretation + /// for `RGB` conversion (as in `SMPTE 240M`). + #[doc(alias = "STANDARD_BT601_525_UNADJUSTED")] + Bt601_525Unadjusted = ffi::ADataSpace::STANDARD_BT601_525_UNADJUSTED.0, + /// | Primaries | x | y | + /// | ----------- | ------ | ------ | + /// | green | 0.170 | 0.797 | + /// | blue | 0.131 | 0.046 | + /// | red | 0.708 | 0.292 | + /// | white (D65) | 0.3127 | 0.3290 | + /// + /// Use the unadjusted `KR = 0.2627`, `KB = 0.0593` luminance interpretation for `RGB` + /// conversion. + #[doc(alias = "STANDARD_BT2020")] + Bt2020 = ffi::ADataSpace::STANDARD_BT2020.0, + /// | Primaries | x | y | + /// | ----------- | ------ | ------ | + /// | green | 0.170 | 0.797 | + /// | blue | 0.131 | 0.046 | + /// | red | 0.708 | 0.292 | + /// | white (D65) | 0.3127 | 0.3290 | + /// + /// Use the unadjusted `KR = 0.2627`, `KB = 0.0593` luminance interpretation for `RGB` + /// conversion using the linear domain. + #[doc(alias = "STANDARD_BT2020_CONSTANT_LUMINANCE")] + Bt2020ConstantLuminance = ffi::ADataSpace::STANDARD_BT2020_CONSTANT_LUMINANCE.0, + /// | Primaries | x | y | + /// | --------- | ----- | ---- | + /// | green | 0.21 |0.71 | + /// | blue | 0.14 |0.08 | + /// | red | 0.67 |0.33 | + /// | white (C) | 0.310 |0.316 | + /// + /// Use the unadjusted `KR = 0.30`, `KB = 0.11` luminance interpretation for `RGB` conversion. + #[doc(alias = "STANDARD_BT470M")] + Bt470M = ffi::ADataSpace::STANDARD_BT470M.0, + /// | Primaries | x | y | + /// | --------- | ----- | ----- | + /// | green | 0.243 | 0.692 | + /// | blue | 0.145 | 0.049 | + /// | red | 0.681 | 0.319 | + /// | white (C) | 0.310 | 0.316 | + /// + /// Use the unadjusted `KR = 0.254`, `KB = 0.068` luminance interpretation for `RGB` conversion. + #[doc(alias = "STANDARD_FILM")] + Film = ffi::ADataSpace::STANDARD_FILM.0, + /// `SMPTE EG 432-1` and `SMPTE RP 431-2`. (`DCI-P3`) + /// + /// | Primaries | x | y | + /// | ----------- | ------ | ------ | + /// | green | 0.265 | 0.690 | + /// | blue | 0.150 | 0.060 | + /// | red | 0.680 | 0.320 | + /// | white (D65) | 0.3127 | 0.3290 | + #[doc(alias = "STANDARD_DCI_P3")] + DciP3 = ffi::ADataSpace::STANDARD_DCI_P3.0, + /// Adobe RGB + /// + /// | Primaries | x | y | + /// | ----------- | ------ | ------ | + /// | green | 0.210 | 0.710 | + /// | blue | 0.150 | 0.060 | + /// | red | 0.640 | 0.330 | + /// | white (D65) | 0.3127 | 0.3290 | + #[doc(alias = "STANDARD_ADOBE_RGB")] + AdobeRgb = ffi::ADataSpace::STANDARD_ADOBE_RGB.0, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +/// Transfer aspect. +/// +/// Transfer characteristics are the opto-electronic transfer characteristic at the source as a +///function of linear optical intensity (luminance). +/// +/// For digital signals, `E` corresponds to the recorded value. Normally, the transfer function is +/// applied in `RGB` space to each of the `R`, `G` and `B` components independently. This may result +/// in color shift that can be minimized by applying the transfer function in `Lab` space only for +/// the `L` component. Implementation may apply the transfer function in `RGB` space for all pixel +/// formats if desired. +#[repr(i32)] +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[doc(alias = "TRANSFER_MASK")] +#[non_exhaustive] +pub enum DataSpaceTransfer { + /// Transfer characteristics are unknown or are determined by the application. + /// + /// Implementations should use the following transfer functions: + /// + /// - For `YCbCr` formats: use [`DataSpaceTransfer::Smpte170M`] + /// - For `RGB` formats: use [`DataSpaceTransfer::Srgb`] + /// + /// For all other formats the transfer function is undefined, and implementations should use an + /// appropriate standard for the data represented. + #[doc(alias = "TRANSFER_UNSPECIFIED")] + Unspecified = ffi::ADataSpace::TRANSFER_UNSPECIFIED.0, + + /// Linear transfer. + /// + /// Transfer characteristic curve: + /// ```ignore + /// E = L + /// ``` + /// - `L`: luminance of image `0 <= L <= 1` for conventional colorimetry + /// - `E`: corresponding electrical signal + #[doc(alias = "TRANSFER_LINEAR")] + Linear = ffi::ADataSpace::TRANSFER_LINEAR.0, + /// `sRGB` transfer. + /// + /// Transfer characteristic curve: + /// + /// ```ignore + /// E = 1.055 * L^(1/2.4) - 0.055 for 0.0031308 <= L <= 1 + /// = 12.92 * L for 0 <= L < 0.0031308 + /// ``` + /// - `L`: luminance of image `0 <= L <= 1` for conventional colorimetry + /// - `E`: corresponding electrical signal + #[doc(alias = "TRANSFER_SRGB")] + Srgb = ffi::ADataSpace::TRANSFER_SRGB.0, + /// SMPTE 170M transfer. + /// + /// Transfer characteristic curve: + /// ```ignore + /// E = 1.099 * L ^ 0.45 - 0.099 for 0.018 <= L <= 1 + /// = 4.500 * L for 0 <= L < 0.018 + /// ``` + /// - `L`: luminance of image `0 <= L <= 1` for conventional colorimetry + /// - `E`: corresponding electrical signal + #[doc(alias = "TRANSFER_SMPTE_170M")] + Smpte170M = ffi::ADataSpace::TRANSFER_SMPTE_170M.0, + /// Display gamma `2.2`. + /// + /// Transfer characteristic curve: + /// ```ignore + /// E = L ^ (1/2.2) + /// ``` + /// - `L`: luminance of image `0 <= L <= 1` for conventional colorimetry + /// - `E`: corresponding electrical signal + #[doc(alias = "TRANSFER_GAMMA2_2")] + Gamma2_2 = ffi::ADataSpace::TRANSFER_GAMMA2_2.0, + /// Display gamma `2.6`. + /// + /// Transfer characteristic curve: + /// ```ignore + /// E = L ^ (1/2.6) + /// ``` + /// - `L`: luminance of image `0 <= L <= 1` for conventional colorimetry + /// - `E`: corresponding electrical signal + #[doc(alias = "TRANSFER_GAMMA2_6")] + Gamma2_6 = ffi::ADataSpace::TRANSFER_GAMMA2_6.0, + /// Display gamma `2.8`. + /// + /// Transfer characteristic curve: + /// ```ignore + /// E = L ^ (1/2.8) + /// ``` + /// - `L`: luminance of image `0 <= L <= 1` for conventional colorimetry + /// - `E`: corresponding electrical signal + #[doc(alias = "TRANSFER_GAMMA2_8")] + Gamma2_8 = ffi::ADataSpace::TRANSFER_GAMMA2_8.0, + /// SMPTE ST 2084 (Dolby Perceptual Quantizer). + /// + /// Transfer characteristic curve: + /// ```ignore + /// E = ((c1 + c2 * L^n) / (1 + c3 * L^n)) ^ m + /// c1 = c3 - c2 + 1 = 3424 / 4096 = 0.8359375 + /// c2 = 32 * 2413 / 4096 = 18.8515625 + /// c3 = 32 * 2392 / 4096 = 18.6875 + /// m = 128 * 2523 / 4096 = 78.84375 + /// n = 0.25 * 2610 / 4096 = 0.1593017578125 + /// ``` + /// - `L`: luminance of image 0 <= L <= 1 for HDR colorimetry. + /// `L = 1` corresponds to `10000 cd/m2` + #[doc(alias = "TRANSFER_ST2084")] + St2084 = ffi::ADataSpace::TRANSFER_ST2084.0, + /// ARIB STD-B67 Hybrid Log Gamma. + /// + /// Transfer characteristic curve: + /// ```ignore + /// E = r * L^0.5 for 0 <= L <= 1 + /// = a * ln(L - b) + c for 1 < L + /// a = 0.17883277 + /// b = 0.28466892 + /// c = 0.55991073 + /// r = 0.5 + /// ``` + /// - `L`: luminance of image `0 <= L` for HDR colorimetry. + /// `L = 1` corresponds to reference white level of `100 cd/m2` + /// - `E`: corresponding electrical signal + #[doc(alias = "TRANSFER_HLG")] + HLG = ffi::ADataSpace::TRANSFER_HLG.0, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +/// Range aspect. +/// +/// Defines the range of values corresponding to the unit range of `0-1`. This is defined for +/// `YCbCr` only, but can be expanded to `RGB` space. +#[repr(i32)] +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[doc(alias = "RANGE_MASK")] +#[non_exhaustive] +pub enum DataSpaceRange { + /// Range is unknown or are determined by the application. Implementations shall use the + /// following suggested ranges: + /// + /// - All YCbCr formats: limited range. + /// - All RGB or RGBA formats (including RAW and Bayer): full range. + /// - All Y formats: full range + /// + /// For all other formats range is undefined, and implementations should use an appropriate + /// range for the data represented. + #[doc(alias = "RANGE_UNSPECIFIED")] + Unspecified = ffi::ADataSpace::RANGE_UNSPECIFIED.0, + /// Full range uses all values for `Y`, `Cb` and `Cr` from `0` to `2^b-1`, where `b` is the bit + /// depth of the color format. + #[doc(alias = "RANGE_FULL")] + Full = ffi::ADataSpace::RANGE_FULL.0, + /// Limited range uses values `16/256*2^b` to `235/256*2^b` for `Y`, and `1/16*2^b` to + /// `15/16*2^b` for `Cb`, `Cr`, `R`, `G` and `B`, where `b` is the bit depth of the color + /// format. + /// + /// E.g. For 8-bit-depth formats: Luma (`Y`) samples should range from `16` to `235`, inclusive + /// Chroma `(Cb, Cr)` samples should range from `16` to `240`, inclusive. + /// + /// For 10-bit-depth formats: Luma (`Y`) samples should range from `64` to `940`, inclusive + /// Chroma `(Cb, Cr)` samples should range from `64` to `960`, inclusive. + #[doc(alias = "RANGE_LIMITED")] + Limited = ffi::ADataSpace::RANGE_LIMITED.0, + /// Extended range is used for `scRGB`. + /// + /// Intended for use with floating point pixel formats. `[0.0 - 1.0]` is the standard `sRGB` + /// space. Values outside the range `0.0 - 1.0` can encode color outside the `sRGB` gamut. Used + /// to blend / merge multiple dataspaces on a single display. + #[doc(alias = "RANGE_EXTENDED")] + Extended = ffi::ADataSpace::RANGE_EXTENDED.0, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} diff --git a/native/vendor/ndk-0.9.0/src/event.rs b/native/vendor/ndk-0.9.0/src/event.rs new file mode 100644 index 0000000..ef447da --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/event.rs @@ -0,0 +1,1583 @@ +//! Bindings for [`AInputEvent`, `AKeyEvent` and `AMotionEvent`] +//! +//! Most of these operations directly wrap functions in the NDK. +//! +//! See also the Java docs for [`android.view.InputEvent`], [`android.view.MotionEvent`], and +//! [`android.view.KeyEvent`]. +//! +//! [`AInputEvent`, `AKeyEvent` and `AMotionEvent`]: https://developer.android.com/ndk/reference/group/input +//! [`android.view.InputEvent`]: https://developer.android.com/reference/android/view/InputEvent +//! [`android.view.MotionEvent`]: https://developer.android.com/reference/android/view/MotionEvent +//! [`android.view.KeyEvent`]: https://developer.android.com/reference/android/view/KeyEvent + +use std::ptr::NonNull; + +#[cfg(feature = "api-level-31")] +use jni_sys::{jobject, JNIEnv}; +use num_enum::{FromPrimitive, IntoPrimitive}; + +/// A native [`AInputEvent *`] +/// +/// [`AInputEvent *`]: https://developer.android.com/ndk/reference/group/input#ainputevent +#[derive(Debug)] +#[non_exhaustive] +pub enum InputEvent { + MotionEvent(MotionEvent), + KeyEvent(KeyEvent), +} + +/// Wraps a Java [`InputEvent`] acquired from [`KeyEvent::from_java()`] or +/// [`MotionEvent::from_java()`] with respective [`Drop`] semantics. +#[cfg(feature = "api-level-31")] +#[derive(Debug)] +pub struct InputEventJava(InputEvent); + +#[cfg(feature = "api-level-31")] +impl Drop for InputEventJava { + /// Releases interface objects created by [`KeyEvent::from_java()`] or + /// [`MotionEvent::from_java()`]. + /// + /// The underlying Java object remains valid and does not change its state. + #[doc(alias = "AInputEvent_release")] + fn drop(&mut self) { + let ptr = match self.0 { + InputEvent::MotionEvent(MotionEvent { ptr }) + | InputEvent::KeyEvent(KeyEvent { ptr }) => ptr.as_ptr().cast(), + }; + unsafe { ffi::AInputEvent_release(ptr) } + } +} + +/// An enum representing the source of an [`InputEvent`]. +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum Source { + Unknown = ffi::AINPUT_SOURCE_UNKNOWN as i32, + Keyboard = ffi::AINPUT_SOURCE_KEYBOARD as i32, + Dpad = ffi::AINPUT_SOURCE_DPAD as i32, + Gamepad = ffi::AINPUT_SOURCE_GAMEPAD as i32, + Touchscreen = ffi::AINPUT_SOURCE_TOUCHSCREEN as i32, + Mouse = ffi::AINPUT_SOURCE_MOUSE as i32, + Stylus = ffi::AINPUT_SOURCE_STYLUS as i32, + BluetoothStylus = ffi::AINPUT_SOURCE_BLUETOOTH_STYLUS as i32, + Trackball = ffi::AINPUT_SOURCE_TRACKBALL as i32, + MouseRelative = ffi::AINPUT_SOURCE_MOUSE_RELATIVE as i32, + Touchpad = ffi::AINPUT_SOURCE_TOUCHPAD as i32, + TouchNavigation = ffi::AINPUT_SOURCE_TOUCH_NAVIGATION as i32, + Joystick = ffi::AINPUT_SOURCE_JOYSTICK as i32, + Hdmi = ffi::AINPUT_SOURCE_HDMI as i32, + Sensor = ffi::AINPUT_SOURCE_SENSOR as i32, + RotaryEncoder = ffi::AINPUT_SOURCE_ROTARY_ENCODER as i32, + Any = ffi::AINPUT_SOURCE_ANY as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +impl Source { + pub fn class(self) -> SourceClass { + let class = i32::from(self) & ffi::AINPUT_SOURCE_CLASS_MASK as i32; + // The mask fits in a u8. + SourceClass::from_bits_retain(class as u8) + } +} + +bitflags::bitflags! { + /// Flags representing the class of an [`InputEvent`] [`Source`]. + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + pub struct SourceClass : u8 { + #[doc(alias = "AINPUT_SOURCE_CLASS_BUTTON")] + const BUTTON = ffi::AINPUT_SOURCE_CLASS_BUTTON as u8; + #[doc(alias = "AINPUT_SOURCE_CLASS_POINTER")] + const POINTER = ffi::AINPUT_SOURCE_CLASS_POINTER as u8; + #[doc(alias = "AINPUT_SOURCE_CLASS_NAVIGATION")] + const NAVIGATION = ffi::AINPUT_SOURCE_CLASS_NAVIGATION as u8; + #[doc(alias = "AINPUT_SOURCE_CLASS_POSITION")] + const POSITION = ffi::AINPUT_SOURCE_CLASS_POSITION as u8; + #[doc(alias = "AINPUT_SOURCE_CLASS_JOYSTICK")] + const JOYSTICK = ffi::AINPUT_SOURCE_CLASS_JOYSTICK as u8; + + // https://docs.rs/bitflags/latest/bitflags/#externally-defined-flags + const _ = ffi::AINPUT_SOURCE_CLASS_MASK as u8; + } +} + +impl InputEvent { + /// Initialize an [`InputEvent`] from a pointer + /// + /// # Safety + /// By calling this function, you assert that the pointer is a valid pointer to a + /// native [`ffi::AInputEvent`]. + #[inline] + pub unsafe fn from_ptr(ptr: NonNull) -> Self { + match ffi::AInputEvent_getType(ptr.as_ptr()) as u32 { + ffi::AINPUT_EVENT_TYPE_KEY => InputEvent::KeyEvent(KeyEvent::from_ptr(ptr)), + ffi::AINPUT_EVENT_TYPE_MOTION => InputEvent::MotionEvent(MotionEvent::from_ptr(ptr)), + x => panic!("Bad event type received: {}", x), + } + } + + /// Returns a pointer to the native [`ffi::AInputEvent`]. + #[inline] + pub fn ptr(&self) -> NonNull { + match self { + InputEvent::MotionEvent(MotionEvent { ptr }) => *ptr, + InputEvent::KeyEvent(KeyEvent { ptr }) => *ptr, + } + } + + /// Get the source of the event. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getsource) + #[inline] + pub fn source(&self) -> Source { + let source = unsafe { ffi::AInputEvent_getSource(self.ptr().as_ptr()) }; + source.into() + } + + /// Get the device id associated with the event. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getdeviceid) + #[inline] + pub fn device_id(&self) -> i32 { + unsafe { ffi::AInputEvent_getDeviceId(self.ptr().as_ptr()) } + } +} + +/// A bitfield representing the state of modifier keys during an event. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct MetaState(pub u32); + +impl MetaState { + #[inline] + pub fn alt_on(self) -> bool { + self.0 & ffi::AMETA_ALT_ON != 0 + } + #[inline] + pub fn alt_left_on(self) -> bool { + self.0 & ffi::AMETA_ALT_LEFT_ON != 0 + } + #[inline] + pub fn alt_right_on(self) -> bool { + self.0 & ffi::AMETA_ALT_RIGHT_ON != 0 + } + #[inline] + pub fn shift_on(self) -> bool { + self.0 & ffi::AMETA_SHIFT_ON != 0 + } + #[inline] + pub fn shift_left_on(self) -> bool { + self.0 & ffi::AMETA_SHIFT_LEFT_ON != 0 + } + #[inline] + pub fn shift_right_on(self) -> bool { + self.0 & ffi::AMETA_SHIFT_RIGHT_ON != 0 + } + #[inline] + pub fn sym_on(self) -> bool { + self.0 & ffi::AMETA_SYM_ON != 0 + } + #[inline] + pub fn function_on(self) -> bool { + self.0 & ffi::AMETA_FUNCTION_ON != 0 + } + #[inline] + pub fn ctrl_on(self) -> bool { + self.0 & ffi::AMETA_CTRL_ON != 0 + } + #[inline] + pub fn ctrl_left_on(self) -> bool { + self.0 & ffi::AMETA_CTRL_LEFT_ON != 0 + } + #[inline] + pub fn ctrl_right_on(self) -> bool { + self.0 & ffi::AMETA_CTRL_RIGHT_ON != 0 + } + #[inline] + pub fn meta_on(self) -> bool { + self.0 & ffi::AMETA_META_ON != 0 + } + #[inline] + pub fn meta_left_on(self) -> bool { + self.0 & ffi::AMETA_META_LEFT_ON != 0 + } + #[inline] + pub fn meta_right_on(self) -> bool { + self.0 & ffi::AMETA_META_RIGHT_ON != 0 + } + #[inline] + pub fn caps_lock_on(self) -> bool { + self.0 & ffi::AMETA_CAPS_LOCK_ON != 0 + } + #[inline] + pub fn num_lock_on(self) -> bool { + self.0 & ffi::AMETA_NUM_LOCK_ON != 0 + } + #[inline] + pub fn scroll_lock_on(self) -> bool { + self.0 & ffi::AMETA_SCROLL_LOCK_ON != 0 + } +} + +/// A motion event +/// +/// Wraps an [`AInputEvent *`] of the [`ffi::AINPUT_EVENT_TYPE_MOTION`] type. +/// +/// For general discussion of motion events in Android, see [the relevant +/// javadoc](https://developer.android.com/reference/android/view/MotionEvent). +/// +/// [`AInputEvent *`]: https://developer.android.com/ndk/reference/group/input#ainputevent +#[derive(Clone, Debug)] +pub struct MotionEvent { + ptr: NonNull, +} + +// TODO: thread safety? + +/// A motion action. +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum MotionAction { + Down = ffi::AMOTION_EVENT_ACTION_DOWN as i32, + Up = ffi::AMOTION_EVENT_ACTION_UP as i32, + Move = ffi::AMOTION_EVENT_ACTION_MOVE as i32, + Cancel = ffi::AMOTION_EVENT_ACTION_CANCEL as i32, + Outside = ffi::AMOTION_EVENT_ACTION_OUTSIDE as i32, + PointerDown = ffi::AMOTION_EVENT_ACTION_POINTER_DOWN as i32, + PointerUp = ffi::AMOTION_EVENT_ACTION_POINTER_UP as i32, + HoverMove = ffi::AMOTION_EVENT_ACTION_HOVER_MOVE as i32, + Scroll = ffi::AMOTION_EVENT_ACTION_SCROLL as i32, + HoverEnter = ffi::AMOTION_EVENT_ACTION_HOVER_ENTER as i32, + HoverExit = ffi::AMOTION_EVENT_ACTION_HOVER_EXIT as i32, + ButtonPress = ffi::AMOTION_EVENT_ACTION_BUTTON_PRESS as i32, + ButtonRelease = ffi::AMOTION_EVENT_ACTION_BUTTON_RELEASE as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +/// An axis of a motion event. +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum Axis { + X = ffi::AMOTION_EVENT_AXIS_X as i32, + Y = ffi::AMOTION_EVENT_AXIS_Y as i32, + Pressure = ffi::AMOTION_EVENT_AXIS_PRESSURE as i32, + Size = ffi::AMOTION_EVENT_AXIS_SIZE as i32, + TouchMajor = ffi::AMOTION_EVENT_AXIS_TOUCH_MAJOR as i32, + TouchMinor = ffi::AMOTION_EVENT_AXIS_TOUCH_MINOR as i32, + ToolMajor = ffi::AMOTION_EVENT_AXIS_TOOL_MAJOR as i32, + ToolMinor = ffi::AMOTION_EVENT_AXIS_TOOL_MINOR as i32, + Orientation = ffi::AMOTION_EVENT_AXIS_ORIENTATION as i32, + Vscroll = ffi::AMOTION_EVENT_AXIS_VSCROLL as i32, + Hscroll = ffi::AMOTION_EVENT_AXIS_HSCROLL as i32, + Z = ffi::AMOTION_EVENT_AXIS_Z as i32, + Rx = ffi::AMOTION_EVENT_AXIS_RX as i32, + Ry = ffi::AMOTION_EVENT_AXIS_RY as i32, + Rz = ffi::AMOTION_EVENT_AXIS_RZ as i32, + HatX = ffi::AMOTION_EVENT_AXIS_HAT_X as i32, + HatY = ffi::AMOTION_EVENT_AXIS_HAT_Y as i32, + Ltrigger = ffi::AMOTION_EVENT_AXIS_LTRIGGER as i32, + Rtrigger = ffi::AMOTION_EVENT_AXIS_RTRIGGER as i32, + Throttle = ffi::AMOTION_EVENT_AXIS_THROTTLE as i32, + Rudder = ffi::AMOTION_EVENT_AXIS_RUDDER as i32, + Wheel = ffi::AMOTION_EVENT_AXIS_WHEEL as i32, + Gas = ffi::AMOTION_EVENT_AXIS_GAS as i32, + Brake = ffi::AMOTION_EVENT_AXIS_BRAKE as i32, + Distance = ffi::AMOTION_EVENT_AXIS_DISTANCE as i32, + Tilt = ffi::AMOTION_EVENT_AXIS_TILT as i32, + Scroll = ffi::AMOTION_EVENT_AXIS_SCROLL as i32, + RelativeX = ffi::AMOTION_EVENT_AXIS_RELATIVE_X as i32, + RelativeY = ffi::AMOTION_EVENT_AXIS_RELATIVE_Y as i32, + Generic1 = ffi::AMOTION_EVENT_AXIS_GENERIC_1 as i32, + Generic2 = ffi::AMOTION_EVENT_AXIS_GENERIC_2 as i32, + Generic3 = ffi::AMOTION_EVENT_AXIS_GENERIC_3 as i32, + Generic4 = ffi::AMOTION_EVENT_AXIS_GENERIC_4 as i32, + Generic5 = ffi::AMOTION_EVENT_AXIS_GENERIC_5 as i32, + Generic6 = ffi::AMOTION_EVENT_AXIS_GENERIC_6 as i32, + Generic7 = ffi::AMOTION_EVENT_AXIS_GENERIC_7 as i32, + Generic8 = ffi::AMOTION_EVENT_AXIS_GENERIC_8 as i32, + Generic9 = ffi::AMOTION_EVENT_AXIS_GENERIC_9 as i32, + Generic10 = ffi::AMOTION_EVENT_AXIS_GENERIC_10 as i32, + Generic11 = ffi::AMOTION_EVENT_AXIS_GENERIC_11 as i32, + Generic12 = ffi::AMOTION_EVENT_AXIS_GENERIC_12 as i32, + Generic13 = ffi::AMOTION_EVENT_AXIS_GENERIC_13 as i32, + Generic14 = ffi::AMOTION_EVENT_AXIS_GENERIC_14 as i32, + Generic15 = ffi::AMOTION_EVENT_AXIS_GENERIC_15 as i32, + Generic16 = ffi::AMOTION_EVENT_AXIS_GENERIC_16 as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +/// The tool type of a pointer. +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum ToolType { + Unknown = ffi::AMOTION_EVENT_TOOL_TYPE_UNKNOWN as i32, + Finger = ffi::AMOTION_EVENT_TOOL_TYPE_FINGER as i32, + Stylus = ffi::AMOTION_EVENT_TOOL_TYPE_STYLUS as i32, + Mouse = ffi::AMOTION_EVENT_TOOL_TYPE_MOUSE as i32, + Eraser = ffi::AMOTION_EVENT_TOOL_TYPE_ERASER as i32, + Palm = ffi::AMOTION_EVENT_TOOL_TYPE_PALM as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +/// A bitfield representing the state of buttons during a motion event. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct ButtonState(pub u32); + +impl ButtonState { + #[inline] + pub fn primary(self) -> bool { + self.0 & ffi::AMOTION_EVENT_BUTTON_PRIMARY != 0 + } + #[inline] + pub fn secondary(self) -> bool { + self.0 & ffi::AMOTION_EVENT_BUTTON_SECONDARY != 0 + } + #[inline] + pub fn teriary(self) -> bool { + self.0 & ffi::AMOTION_EVENT_BUTTON_TERTIARY != 0 + } + #[inline] + pub fn back(self) -> bool { + self.0 & ffi::AMOTION_EVENT_BUTTON_BACK != 0 + } + #[inline] + pub fn forward(self) -> bool { + self.0 & ffi::AMOTION_EVENT_BUTTON_FORWARD != 0 + } + #[inline] + pub fn stylus_primary(self) -> bool { + self.0 & ffi::AMOTION_EVENT_BUTTON_STYLUS_PRIMARY != 0 + } + #[inline] + pub fn stylus_secondary(self) -> bool { + self.0 & ffi::AMOTION_EVENT_BUTTON_STYLUS_SECONDARY != 0 + } +} + +/// A bitfield representing which edges were touched by a motion event. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct EdgeFlags(pub u32); + +impl EdgeFlags { + #[inline] + pub fn top(self) -> bool { + self.0 & ffi::AMOTION_EVENT_EDGE_FLAG_TOP != 0 + } + #[inline] + pub fn bottom(self) -> bool { + self.0 & ffi::AMOTION_EVENT_EDGE_FLAG_BOTTOM != 0 + } + #[inline] + pub fn left(self) -> bool { + self.0 & ffi::AMOTION_EVENT_EDGE_FLAG_LEFT != 0 + } + #[inline] + pub fn right(self) -> bool { + self.0 & ffi::AMOTION_EVENT_EDGE_FLAG_RIGHT != 0 + } +} + +/// Flags associated with this [`MotionEvent`]. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct MotionEventFlags(pub u32); + +impl MotionEventFlags { + #[inline] + pub fn window_is_obscured(self) -> bool { + self.0 & ffi::AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED != 0 + } +} + +impl MotionEvent { + /// Constructs a MotionEvent from a pointer to a native [`ffi::AInputEvent`] + /// + /// # Safety + /// By calling this method, you assert that the pointer is a valid, non-null pointer to a + /// native [`ffi::AInputEvent`] and that [`ffi::AInputEvent`] + /// is an `AMotionEvent`. + #[inline] + pub unsafe fn from_ptr(ptr: NonNull) -> Self { + Self { ptr } + } + + /// Creates a native [`InputEvent`] object that is a copy of the specified + /// Java [`android.view.MotionEvent`]. The result may be used with generic and + /// [`MotionEvent`]-specific functions. + /// + /// # Safety + /// + /// This function should be called with a healthy JVM pointer and with a non-null + /// [`android.view.MotionEvent`]. + /// + /// [`android.view.MotionEvent`]: https://developer.android.com/reference/android/view/MotionEvent + #[cfg(feature = "api-level-31")] + #[doc(alias = "AMotionEvent_fromJava")] + pub unsafe fn from_java(env: *mut JNIEnv, key_event: jobject) -> Option { + let ptr = unsafe { ffi::AMotionEvent_fromJava(env, key_event) }; + Some(InputEventJava(InputEvent::MotionEvent(Self::from_ptr( + NonNull::new(ptr.cast_mut())?, + )))) + } + + /// Returns a pointer to the native [`ffi::AInputEvent`]. + #[inline] + pub fn ptr(&self) -> NonNull { + self.ptr + } + + /// Get the source of the event. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getsource) + #[inline] + pub fn source(&self) -> Source { + let source = unsafe { ffi::AInputEvent_getSource(self.ptr.as_ptr()) }; + source.into() + } + + /// Get the device id associated with the event. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getdeviceid) + #[inline] + pub fn device_id(&self) -> i32 { + unsafe { ffi::AInputEvent_getDeviceId(self.ptr.as_ptr()) } + } + + /// Returns the motion action associated with the event. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getaction) + #[inline] + pub fn action(&self) -> MotionAction { + let action = unsafe { ffi::AMotionEvent_getAction(self.ptr.as_ptr()) } + & ffi::AMOTION_EVENT_ACTION_MASK as i32; + action.into() + } + + /// Returns the pointer index of an `Up` or `Down` event. + /// + /// Pointer indices can change per motion event. For an identifier that stays the same, see + /// [`Pointer::pointer_id()`]. + /// + /// This only has a meaning when the [action][Self::action] is one of [`Up`][MotionAction::Up], + /// [`Down`][MotionAction::Down], [`PointerUp`][MotionAction::PointerUp], + /// or [`PointerDown`][MotionAction::PointerDown]. + #[inline] + pub fn pointer_index(&self) -> usize { + let action = unsafe { ffi::AMotionEvent_getAction(self.ptr.as_ptr()) as u32 }; + let index = (action & ffi::AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) + >> ffi::AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; + index as usize + } + + /* + /// Returns the pointer id associated with the given pointer index. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getpointerid) + // TODO: look at output with out-of-range pointer index + // Probably -1 though + pub fn pointer_id_for(&self, pointer_index: usize) -> i32 { + unsafe { ffi::AMotionEvent_getPointerId(self.ptr.as_ptr(), pointer_index) } + } + */ + + /// Returns the number of pointers in this event + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getpointercount) + #[inline] + pub fn pointer_count(&self) -> usize { + unsafe { ffi::AMotionEvent_getPointerCount(self.ptr.as_ptr()) } + } + + /// An iterator over the pointers in this motion event + #[inline] + pub fn pointers(&self) -> PointersIter<'_> { + PointersIter { + event: self.ptr, + next_index: 0, + count: self.pointer_count(), + _marker: std::marker::PhantomData, + } + } + + /// The pointer at a given pointer index. Panics if the pointer index is out of bounds. + /// + /// If you need to loop over all the pointers, prefer the [`pointers()`][Self::pointers] method. + #[inline] + pub fn pointer_at_index(&self, index: usize) -> Pointer<'_> { + if index >= self.pointer_count() { + panic!("Pointer index {} is out of bounds", index); + } + Pointer { + event: self.ptr, + index, + _marker: std::marker::PhantomData, + } + } + + /// Returns the size of the history contained in this event. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_gethistorysize) + #[inline] + pub fn history_size(&self) -> usize { + unsafe { ffi::AMotionEvent_getHistorySize(self.ptr.as_ptr()) } + } + + /// An iterator over the historical events contained in this event. + #[inline] + pub fn history(&self) -> HistoricalMotionEventsIter<'_> { + HistoricalMotionEventsIter { + event: self.ptr, + next_history_index: 0, + history_size: self.history_size(), + _marker: std::marker::PhantomData, + } + } + + /// Returns the state of any modifier keys that were pressed during the event. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getmetastate) + #[inline] + pub fn meta_state(&self) -> MetaState { + unsafe { MetaState(ffi::AMotionEvent_getMetaState(self.ptr.as_ptr()) as u32) } + } + + /// Returns the button state during this event, as a bitfield. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getbuttonstate) + #[inline] + pub fn button_state(&self) -> ButtonState { + unsafe { ButtonState(ffi::AMotionEvent_getButtonState(self.ptr.as_ptr()) as u32) } + } + + /// Returns the time of the start of this gesture, in the `java.lang.System.nanoTime()` time + /// base + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getdowntime) + #[inline] + pub fn down_time(&self) -> i64 { + unsafe { ffi::AMotionEvent_getDownTime(self.ptr.as_ptr()) } + } + + /// Returns a bitfield indicating which edges were touched by this event. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getedgeflags) + #[inline] + pub fn edge_flags(&self) -> EdgeFlags { + unsafe { EdgeFlags(ffi::AMotionEvent_getEdgeFlags(self.ptr.as_ptr()) as u32) } + } + + /// Returns the time of this event, in the `java.lang.System.nanoTime()` time base + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_geteventtime) + #[inline] + pub fn event_time(&self) -> i64 { + unsafe { ffi::AMotionEvent_getEventTime(self.ptr.as_ptr()) } + } + + /// The flags associated with a motion event. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getflags) + #[inline] + pub fn flags(&self) -> MotionEventFlags { + unsafe { MotionEventFlags(ffi::AMotionEvent_getFlags(self.ptr.as_ptr()) as u32) } + } + + /// Returns the offset in the x direction between the coordinates and the raw coordinates + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getxoffset) + #[inline] + pub fn x_offset(&self) -> f32 { + unsafe { ffi::AMotionEvent_getXOffset(self.ptr.as_ptr()) } + } + + /// Returns the offset in the y direction between the coordinates and the raw coordinates + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getyoffset) + #[inline] + pub fn y_offset(&self) -> f32 { + unsafe { ffi::AMotionEvent_getYOffset(self.ptr.as_ptr()) } + } + + /// Returns the precision of the x value of the coordinates + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getxprecision) + #[inline] + pub fn x_precision(&self) -> f32 { + unsafe { ffi::AMotionEvent_getXPrecision(self.ptr.as_ptr()) } + } + + /// Returns the precision of the y value of the coordinates + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getyprecision) + #[inline] + pub fn y_precision(&self) -> f32 { + unsafe { ffi::AMotionEvent_getYPrecision(self.ptr.as_ptr()) } + } +} + +/// A view into the data of a specific pointer in a motion event. +#[derive(Debug)] +pub struct Pointer<'a> { + event: NonNull, + index: usize, + _marker: std::marker::PhantomData<&'a MotionEvent>, +} + +// TODO: thread safety? + +impl<'a> Pointer<'a> { + #[inline] + pub fn pointer_index(&self) -> usize { + self.index + } + + #[inline] + pub fn pointer_id(&self) -> i32 { + unsafe { ffi::AMotionEvent_getPointerId(self.event.as_ptr(), self.index) } + } + + #[inline] + pub fn axis_value(&self, axis: Axis) -> f32 { + unsafe { ffi::AMotionEvent_getAxisValue(self.event.as_ptr(), axis.into(), self.index) } + } + + #[inline] + pub fn orientation(&self) -> f32 { + unsafe { ffi::AMotionEvent_getOrientation(self.event.as_ptr(), self.index) } + } + + #[inline] + pub fn pressure(&self) -> f32 { + unsafe { ffi::AMotionEvent_getPressure(self.event.as_ptr(), self.index) } + } + + #[inline] + pub fn raw_x(&self) -> f32 { + unsafe { ffi::AMotionEvent_getRawX(self.event.as_ptr(), self.index) } + } + + #[inline] + pub fn raw_y(&self) -> f32 { + unsafe { ffi::AMotionEvent_getRawY(self.event.as_ptr(), self.index) } + } + + #[inline] + pub fn x(&self) -> f32 { + unsafe { ffi::AMotionEvent_getX(self.event.as_ptr(), self.index) } + } + + #[inline] + pub fn y(&self) -> f32 { + unsafe { ffi::AMotionEvent_getY(self.event.as_ptr(), self.index) } + } + + #[inline] + pub fn size(&self) -> f32 { + unsafe { ffi::AMotionEvent_getSize(self.event.as_ptr(), self.index) } + } + + #[inline] + pub fn tool_major(&self) -> f32 { + unsafe { ffi::AMotionEvent_getToolMajor(self.event.as_ptr(), self.index) } + } + + #[inline] + pub fn tool_minor(&self) -> f32 { + unsafe { ffi::AMotionEvent_getToolMinor(self.event.as_ptr(), self.index) } + } + + #[inline] + pub fn touch_major(&self) -> f32 { + unsafe { ffi::AMotionEvent_getTouchMajor(self.event.as_ptr(), self.index) } + } + + #[inline] + pub fn touch_minor(&self) -> f32 { + unsafe { ffi::AMotionEvent_getTouchMinor(self.event.as_ptr(), self.index) } + } + + #[inline] + pub fn tool_type(&self) -> ToolType { + let tool_type = unsafe { ffi::AMotionEvent_getToolType(self.event.as_ptr(), self.index) }; + tool_type.into() + } +} + +/// An iterator over the pointers in a [`MotionEvent`]. +#[derive(Debug)] +pub struct PointersIter<'a> { + event: NonNull, + next_index: usize, + count: usize, + _marker: std::marker::PhantomData<&'a MotionEvent>, +} + +// TODO: thread safety? + +impl<'a> Iterator for PointersIter<'a> { + type Item = Pointer<'a>; + fn next(&mut self) -> Option> { + if self.next_index < self.count { + let ptr = Pointer { + event: self.event, + index: self.next_index, + _marker: std::marker::PhantomData, + }; + self.next_index += 1; + Some(ptr) + } else { + None + } + } + + fn size_hint(&self) -> (usize, Option) { + let size = self.count - self.next_index; + (size, Some(size)) + } +} +impl<'a> ExactSizeIterator for PointersIter<'a> { + fn len(&self) -> usize { + self.count - self.next_index + } +} + +/// Represents a view into a past moment of a motion event +#[derive(Debug)] +pub struct HistoricalMotionEvent<'a> { + event: NonNull, + history_index: usize, + _marker: std::marker::PhantomData<&'a MotionEvent>, +} + +// TODO: thread safety? + +impl<'a> HistoricalMotionEvent<'a> { + /// Returns the "history index" associated with this historical event. Older events have smaller indices. + #[inline] + pub fn history_index(&self) -> usize { + self.history_index + } + + /// Returns the time of the historical event, in the `java.lang.System.nanoTime()` time base + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_gethistoricaleventtime) + #[inline] + pub fn event_time(&self) -> i64 { + unsafe { ffi::AMotionEvent_getHistoricalEventTime(self.event.as_ptr(), self.history_index) } + } + + /// An iterator over the pointers of this historical motion event + #[inline] + pub fn pointers(&self) -> HistoricalPointersIter<'a> { + HistoricalPointersIter { + event: self.event, + history_index: self.history_index, + next_pointer_index: 0, + pointer_count: unsafe { ffi::AMotionEvent_getPointerCount(self.event.as_ptr()) }, + _marker: std::marker::PhantomData, + } + } +} + +/// An iterator over all the historical moments in a [`MotionEvent`]. +/// +/// It iterates from oldest to newest. +#[derive(Debug)] +pub struct HistoricalMotionEventsIter<'a> { + event: NonNull, + next_history_index: usize, + history_size: usize, + _marker: std::marker::PhantomData<&'a MotionEvent>, +} + +// TODO: thread safety? + +impl<'a> Iterator for HistoricalMotionEventsIter<'a> { + type Item = HistoricalMotionEvent<'a>; + + fn next(&mut self) -> Option> { + if self.next_history_index < self.history_size { + let res = HistoricalMotionEvent { + event: self.event, + history_index: self.next_history_index, + _marker: std::marker::PhantomData, + }; + self.next_history_index += 1; + Some(res) + } else { + None + } + } + + fn size_hint(&self) -> (usize, Option) { + let size = self.history_size - self.next_history_index; + (size, Some(size)) + } +} +impl ExactSizeIterator for HistoricalMotionEventsIter<'_> { + fn len(&self) -> usize { + self.history_size - self.next_history_index + } +} +impl<'a> DoubleEndedIterator for HistoricalMotionEventsIter<'a> { + fn next_back(&mut self) -> Option> { + if self.next_history_index < self.history_size { + self.history_size -= 1; + Some(HistoricalMotionEvent { + event: self.event, + history_index: self.history_size, + _marker: std::marker::PhantomData, + }) + } else { + None + } + } +} + +/// A view into a pointer at a historical moment +#[derive(Debug)] +pub struct HistoricalPointer<'a> { + event: NonNull, + pointer_index: usize, + history_index: usize, + _marker: std::marker::PhantomData<&'a MotionEvent>, +} + +// TODO: thread safety? + +impl<'a> HistoricalPointer<'a> { + #[inline] + pub fn pointer_index(&self) -> usize { + self.pointer_index + } + + #[inline] + pub fn pointer_id(&self) -> i32 { + unsafe { ffi::AMotionEvent_getPointerId(self.event.as_ptr(), self.pointer_index) } + } + + #[inline] + pub fn history_index(&self) -> usize { + self.history_index + } + + #[inline] + pub fn axis_value(&self, axis: Axis) -> f32 { + unsafe { + ffi::AMotionEvent_getHistoricalAxisValue( + self.event.as_ptr(), + axis.into(), + self.pointer_index, + self.history_index, + ) + } + } + + #[inline] + pub fn orientation(&self) -> f32 { + unsafe { + ffi::AMotionEvent_getHistoricalOrientation( + self.event.as_ptr(), + self.pointer_index, + self.history_index, + ) + } + } + + #[inline] + pub fn pressure(&self) -> f32 { + unsafe { + ffi::AMotionEvent_getHistoricalPressure( + self.event.as_ptr(), + self.pointer_index, + self.history_index, + ) + } + } + + #[inline] + pub fn raw_x(&self) -> f32 { + unsafe { + ffi::AMotionEvent_getHistoricalRawX( + self.event.as_ptr(), + self.pointer_index, + self.history_index, + ) + } + } + + #[inline] + pub fn raw_y(&self) -> f32 { + unsafe { + ffi::AMotionEvent_getHistoricalRawY( + self.event.as_ptr(), + self.pointer_index, + self.history_index, + ) + } + } + + #[inline] + pub fn x(&self) -> f32 { + unsafe { + ffi::AMotionEvent_getHistoricalX( + self.event.as_ptr(), + self.pointer_index, + self.history_index, + ) + } + } + + #[inline] + pub fn y(&self) -> f32 { + unsafe { + ffi::AMotionEvent_getHistoricalY( + self.event.as_ptr(), + self.pointer_index, + self.history_index, + ) + } + } + + #[inline] + pub fn size(&self) -> f32 { + unsafe { + ffi::AMotionEvent_getHistoricalSize( + self.event.as_ptr(), + self.pointer_index, + self.history_index, + ) + } + } + + #[inline] + pub fn tool_major(&self) -> f32 { + unsafe { + ffi::AMotionEvent_getHistoricalToolMajor( + self.event.as_ptr(), + self.pointer_index, + self.history_index, + ) + } + } + + #[inline] + pub fn tool_minor(&self) -> f32 { + unsafe { + ffi::AMotionEvent_getHistoricalToolMinor( + self.event.as_ptr(), + self.pointer_index, + self.history_index, + ) + } + } + + #[inline] + pub fn touch_major(&self) -> f32 { + unsafe { + ffi::AMotionEvent_getHistoricalTouchMajor( + self.event.as_ptr(), + self.pointer_index, + self.history_index, + ) + } + } + + #[inline] + pub fn touch_minor(&self) -> f32 { + unsafe { + ffi::AMotionEvent_getHistoricalTouchMinor( + self.event.as_ptr(), + self.pointer_index, + self.history_index, + ) + } + } +} + +/// An iterator over the pointers in a historical motion event +#[derive(Debug)] +pub struct HistoricalPointersIter<'a> { + event: NonNull, + history_index: usize, + next_pointer_index: usize, + pointer_count: usize, + _marker: std::marker::PhantomData<&'a MotionEvent>, +} + +// TODO: thread safety? + +impl<'a> Iterator for HistoricalPointersIter<'a> { + type Item = HistoricalPointer<'a>; + + fn next(&mut self) -> Option> { + if self.next_pointer_index < self.pointer_count { + let ptr = HistoricalPointer { + event: self.event, + history_index: self.history_index, + pointer_index: self.next_pointer_index, + _marker: std::marker::PhantomData, + }; + self.next_pointer_index += 1; + Some(ptr) + } else { + None + } + } + + fn size_hint(&self) -> (usize, Option) { + let size = self.pointer_count - self.next_pointer_index; + (size, Some(size)) + } +} +impl ExactSizeIterator for HistoricalPointersIter<'_> { + fn len(&self) -> usize { + self.pointer_count - self.next_pointer_index + } +} + +/// A key event +/// +/// Wraps an [`AInputEvent *`] of the [`ffi::AINPUT_EVENT_TYPE_KEY`] type. +/// +/// For general discussion of key events in Android, see [the relevant +/// javadoc](https://developer.android.com/reference/android/view/KeyEvent). +/// +/// [`AInputEvent *`]: https://developer.android.com/ndk/reference/group/input#ainputevent +#[derive(Debug)] +pub struct KeyEvent { + ptr: NonNull, +} + +// TODO: thread safety? + +/// Key actions. +/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-27) +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum KeyAction { + Down = ffi::AKEY_EVENT_ACTION_DOWN as i32, + Up = ffi::AKEY_EVENT_ACTION_UP as i32, + Multiple = ffi::AKEY_EVENT_ACTION_MULTIPLE as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +/// Key codes. +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[repr(i32)] +#[non_exhaustive] +pub enum Keycode { + Unknown = ffi::AKEYCODE_UNKNOWN as i32, + SoftLeft = ffi::AKEYCODE_SOFT_LEFT as i32, + SoftRight = ffi::AKEYCODE_SOFT_RIGHT as i32, + Home = ffi::AKEYCODE_HOME as i32, + Back = ffi::AKEYCODE_BACK as i32, + Call = ffi::AKEYCODE_CALL as i32, + Endcall = ffi::AKEYCODE_ENDCALL as i32, + Keycode0 = ffi::AKEYCODE_0 as i32, + Keycode1 = ffi::AKEYCODE_1 as i32, + Keycode2 = ffi::AKEYCODE_2 as i32, + Keycode3 = ffi::AKEYCODE_3 as i32, + Keycode4 = ffi::AKEYCODE_4 as i32, + Keycode5 = ffi::AKEYCODE_5 as i32, + Keycode6 = ffi::AKEYCODE_6 as i32, + Keycode7 = ffi::AKEYCODE_7 as i32, + Keycode8 = ffi::AKEYCODE_8 as i32, + Keycode9 = ffi::AKEYCODE_9 as i32, + Star = ffi::AKEYCODE_STAR as i32, + Pound = ffi::AKEYCODE_POUND as i32, + DpadUp = ffi::AKEYCODE_DPAD_UP as i32, + DpadDown = ffi::AKEYCODE_DPAD_DOWN as i32, + DpadLeft = ffi::AKEYCODE_DPAD_LEFT as i32, + DpadRight = ffi::AKEYCODE_DPAD_RIGHT as i32, + DpadCenter = ffi::AKEYCODE_DPAD_CENTER as i32, + VolumeUp = ffi::AKEYCODE_VOLUME_UP as i32, + VolumeDown = ffi::AKEYCODE_VOLUME_DOWN as i32, + Power = ffi::AKEYCODE_POWER as i32, + Camera = ffi::AKEYCODE_CAMERA as i32, + Clear = ffi::AKEYCODE_CLEAR as i32, + A = ffi::AKEYCODE_A as i32, + B = ffi::AKEYCODE_B as i32, + C = ffi::AKEYCODE_C as i32, + D = ffi::AKEYCODE_D as i32, + E = ffi::AKEYCODE_E as i32, + F = ffi::AKEYCODE_F as i32, + G = ffi::AKEYCODE_G as i32, + H = ffi::AKEYCODE_H as i32, + I = ffi::AKEYCODE_I as i32, + J = ffi::AKEYCODE_J as i32, + K = ffi::AKEYCODE_K as i32, + L = ffi::AKEYCODE_L as i32, + M = ffi::AKEYCODE_M as i32, + N = ffi::AKEYCODE_N as i32, + O = ffi::AKEYCODE_O as i32, + P = ffi::AKEYCODE_P as i32, + Q = ffi::AKEYCODE_Q as i32, + R = ffi::AKEYCODE_R as i32, + S = ffi::AKEYCODE_S as i32, + T = ffi::AKEYCODE_T as i32, + U = ffi::AKEYCODE_U as i32, + V = ffi::AKEYCODE_V as i32, + W = ffi::AKEYCODE_W as i32, + X = ffi::AKEYCODE_X as i32, + Y = ffi::AKEYCODE_Y as i32, + Z = ffi::AKEYCODE_Z as i32, + Comma = ffi::AKEYCODE_COMMA as i32, + Period = ffi::AKEYCODE_PERIOD as i32, + AltLeft = ffi::AKEYCODE_ALT_LEFT as i32, + AltRight = ffi::AKEYCODE_ALT_RIGHT as i32, + ShiftLeft = ffi::AKEYCODE_SHIFT_LEFT as i32, + ShiftRight = ffi::AKEYCODE_SHIFT_RIGHT as i32, + Tab = ffi::AKEYCODE_TAB as i32, + Space = ffi::AKEYCODE_SPACE as i32, + Sym = ffi::AKEYCODE_SYM as i32, + Explorer = ffi::AKEYCODE_EXPLORER as i32, + Envelope = ffi::AKEYCODE_ENVELOPE as i32, + Enter = ffi::AKEYCODE_ENTER as i32, + Del = ffi::AKEYCODE_DEL as i32, + Grave = ffi::AKEYCODE_GRAVE as i32, + Minus = ffi::AKEYCODE_MINUS as i32, + Equals = ffi::AKEYCODE_EQUALS as i32, + LeftBracket = ffi::AKEYCODE_LEFT_BRACKET as i32, + RightBracket = ffi::AKEYCODE_RIGHT_BRACKET as i32, + Backslash = ffi::AKEYCODE_BACKSLASH as i32, + Semicolon = ffi::AKEYCODE_SEMICOLON as i32, + Apostrophe = ffi::AKEYCODE_APOSTROPHE as i32, + Slash = ffi::AKEYCODE_SLASH as i32, + At = ffi::AKEYCODE_AT as i32, + Num = ffi::AKEYCODE_NUM as i32, + Headsethook = ffi::AKEYCODE_HEADSETHOOK as i32, + Focus = ffi::AKEYCODE_FOCUS as i32, + Plus = ffi::AKEYCODE_PLUS as i32, + Menu = ffi::AKEYCODE_MENU as i32, + Notification = ffi::AKEYCODE_NOTIFICATION as i32, + Search = ffi::AKEYCODE_SEARCH as i32, + MediaPlayPause = ffi::AKEYCODE_MEDIA_PLAY_PAUSE as i32, + MediaStop = ffi::AKEYCODE_MEDIA_STOP as i32, + MediaNext = ffi::AKEYCODE_MEDIA_NEXT as i32, + MediaPrevious = ffi::AKEYCODE_MEDIA_PREVIOUS as i32, + MediaRewind = ffi::AKEYCODE_MEDIA_REWIND as i32, + MediaFastForward = ffi::AKEYCODE_MEDIA_FAST_FORWARD as i32, + Mute = ffi::AKEYCODE_MUTE as i32, + PageUp = ffi::AKEYCODE_PAGE_UP as i32, + PageDown = ffi::AKEYCODE_PAGE_DOWN as i32, + Pictsymbols = ffi::AKEYCODE_PICTSYMBOLS as i32, + SwitchCharset = ffi::AKEYCODE_SWITCH_CHARSET as i32, + ButtonA = ffi::AKEYCODE_BUTTON_A as i32, + ButtonB = ffi::AKEYCODE_BUTTON_B as i32, + ButtonC = ffi::AKEYCODE_BUTTON_C as i32, + ButtonX = ffi::AKEYCODE_BUTTON_X as i32, + ButtonY = ffi::AKEYCODE_BUTTON_Y as i32, + ButtonZ = ffi::AKEYCODE_BUTTON_Z as i32, + ButtonL1 = ffi::AKEYCODE_BUTTON_L1 as i32, + ButtonR1 = ffi::AKEYCODE_BUTTON_R1 as i32, + ButtonL2 = ffi::AKEYCODE_BUTTON_L2 as i32, + ButtonR2 = ffi::AKEYCODE_BUTTON_R2 as i32, + ButtonThumbl = ffi::AKEYCODE_BUTTON_THUMBL as i32, + ButtonThumbr = ffi::AKEYCODE_BUTTON_THUMBR as i32, + ButtonStart = ffi::AKEYCODE_BUTTON_START as i32, + ButtonSelect = ffi::AKEYCODE_BUTTON_SELECT as i32, + ButtonMode = ffi::AKEYCODE_BUTTON_MODE as i32, + Escape = ffi::AKEYCODE_ESCAPE as i32, + ForwardDel = ffi::AKEYCODE_FORWARD_DEL as i32, + CtrlLeft = ffi::AKEYCODE_CTRL_LEFT as i32, + CtrlRight = ffi::AKEYCODE_CTRL_RIGHT as i32, + CapsLock = ffi::AKEYCODE_CAPS_LOCK as i32, + ScrollLock = ffi::AKEYCODE_SCROLL_LOCK as i32, + MetaLeft = ffi::AKEYCODE_META_LEFT as i32, + MetaRight = ffi::AKEYCODE_META_RIGHT as i32, + Function = ffi::AKEYCODE_FUNCTION as i32, + Sysrq = ffi::AKEYCODE_SYSRQ as i32, + Break = ffi::AKEYCODE_BREAK as i32, + MoveHome = ffi::AKEYCODE_MOVE_HOME as i32, + MoveEnd = ffi::AKEYCODE_MOVE_END as i32, + Insert = ffi::AKEYCODE_INSERT as i32, + Forward = ffi::AKEYCODE_FORWARD as i32, + MediaPlay = ffi::AKEYCODE_MEDIA_PLAY as i32, + MediaPause = ffi::AKEYCODE_MEDIA_PAUSE as i32, + MediaClose = ffi::AKEYCODE_MEDIA_CLOSE as i32, + MediaEject = ffi::AKEYCODE_MEDIA_EJECT as i32, + MediaRecord = ffi::AKEYCODE_MEDIA_RECORD as i32, + F1 = ffi::AKEYCODE_F1 as i32, + F2 = ffi::AKEYCODE_F2 as i32, + F3 = ffi::AKEYCODE_F3 as i32, + F4 = ffi::AKEYCODE_F4 as i32, + F5 = ffi::AKEYCODE_F5 as i32, + F6 = ffi::AKEYCODE_F6 as i32, + F7 = ffi::AKEYCODE_F7 as i32, + F8 = ffi::AKEYCODE_F8 as i32, + F9 = ffi::AKEYCODE_F9 as i32, + F10 = ffi::AKEYCODE_F10 as i32, + F11 = ffi::AKEYCODE_F11 as i32, + F12 = ffi::AKEYCODE_F12 as i32, + NumLock = ffi::AKEYCODE_NUM_LOCK as i32, + Numpad0 = ffi::AKEYCODE_NUMPAD_0 as i32, + Numpad1 = ffi::AKEYCODE_NUMPAD_1 as i32, + Numpad2 = ffi::AKEYCODE_NUMPAD_2 as i32, + Numpad3 = ffi::AKEYCODE_NUMPAD_3 as i32, + Numpad4 = ffi::AKEYCODE_NUMPAD_4 as i32, + Numpad5 = ffi::AKEYCODE_NUMPAD_5 as i32, + Numpad6 = ffi::AKEYCODE_NUMPAD_6 as i32, + Numpad7 = ffi::AKEYCODE_NUMPAD_7 as i32, + Numpad8 = ffi::AKEYCODE_NUMPAD_8 as i32, + Numpad9 = ffi::AKEYCODE_NUMPAD_9 as i32, + NumpadDivide = ffi::AKEYCODE_NUMPAD_DIVIDE as i32, + NumpadMultiply = ffi::AKEYCODE_NUMPAD_MULTIPLY as i32, + NumpadSubtract = ffi::AKEYCODE_NUMPAD_SUBTRACT as i32, + NumpadAdd = ffi::AKEYCODE_NUMPAD_ADD as i32, + NumpadDot = ffi::AKEYCODE_NUMPAD_DOT as i32, + NumpadComma = ffi::AKEYCODE_NUMPAD_COMMA as i32, + NumpadEnter = ffi::AKEYCODE_NUMPAD_ENTER as i32, + NumpadEquals = ffi::AKEYCODE_NUMPAD_EQUALS as i32, + NumpadLeftParen = ffi::AKEYCODE_NUMPAD_LEFT_PAREN as i32, + NumpadRightParen = ffi::AKEYCODE_NUMPAD_RIGHT_PAREN as i32, + VolumeMute = ffi::AKEYCODE_VOLUME_MUTE as i32, + Info = ffi::AKEYCODE_INFO as i32, + ChannelUp = ffi::AKEYCODE_CHANNEL_UP as i32, + ChannelDown = ffi::AKEYCODE_CHANNEL_DOWN as i32, + ZoomIn = ffi::AKEYCODE_ZOOM_IN as i32, + ZoomOut = ffi::AKEYCODE_ZOOM_OUT as i32, + Tv = ffi::AKEYCODE_TV as i32, + Window = ffi::AKEYCODE_WINDOW as i32, + Guide = ffi::AKEYCODE_GUIDE as i32, + Dvr = ffi::AKEYCODE_DVR as i32, + Bookmark = ffi::AKEYCODE_BOOKMARK as i32, + Captions = ffi::AKEYCODE_CAPTIONS as i32, + Settings = ffi::AKEYCODE_SETTINGS as i32, + TvPower = ffi::AKEYCODE_TV_POWER as i32, + TvInput = ffi::AKEYCODE_TV_INPUT as i32, + StbPower = ffi::AKEYCODE_STB_POWER as i32, + StbInput = ffi::AKEYCODE_STB_INPUT as i32, + AvrPower = ffi::AKEYCODE_AVR_POWER as i32, + AvrInput = ffi::AKEYCODE_AVR_INPUT as i32, + ProgRed = ffi::AKEYCODE_PROG_RED as i32, + ProgGreen = ffi::AKEYCODE_PROG_GREEN as i32, + ProgYellow = ffi::AKEYCODE_PROG_YELLOW as i32, + ProgBlue = ffi::AKEYCODE_PROG_BLUE as i32, + AppSwitch = ffi::AKEYCODE_APP_SWITCH as i32, + Button1 = ffi::AKEYCODE_BUTTON_1 as i32, + Button2 = ffi::AKEYCODE_BUTTON_2 as i32, + Button3 = ffi::AKEYCODE_BUTTON_3 as i32, + Button4 = ffi::AKEYCODE_BUTTON_4 as i32, + Button5 = ffi::AKEYCODE_BUTTON_5 as i32, + Button6 = ffi::AKEYCODE_BUTTON_6 as i32, + Button7 = ffi::AKEYCODE_BUTTON_7 as i32, + Button8 = ffi::AKEYCODE_BUTTON_8 as i32, + Button9 = ffi::AKEYCODE_BUTTON_9 as i32, + Button10 = ffi::AKEYCODE_BUTTON_10 as i32, + Button11 = ffi::AKEYCODE_BUTTON_11 as i32, + Button12 = ffi::AKEYCODE_BUTTON_12 as i32, + Button13 = ffi::AKEYCODE_BUTTON_13 as i32, + Button14 = ffi::AKEYCODE_BUTTON_14 as i32, + Button15 = ffi::AKEYCODE_BUTTON_15 as i32, + Button16 = ffi::AKEYCODE_BUTTON_16 as i32, + LanguageSwitch = ffi::AKEYCODE_LANGUAGE_SWITCH as i32, + MannerMode = ffi::AKEYCODE_MANNER_MODE as i32, + Keycode3dMode = ffi::AKEYCODE_3D_MODE as i32, + Contacts = ffi::AKEYCODE_CONTACTS as i32, + Calendar = ffi::AKEYCODE_CALENDAR as i32, + Music = ffi::AKEYCODE_MUSIC as i32, + Calculator = ffi::AKEYCODE_CALCULATOR as i32, + ZenkakuHankaku = ffi::AKEYCODE_ZENKAKU_HANKAKU as i32, + Eisu = ffi::AKEYCODE_EISU as i32, + Muhenkan = ffi::AKEYCODE_MUHENKAN as i32, + Henkan = ffi::AKEYCODE_HENKAN as i32, + KatakanaHiragana = ffi::AKEYCODE_KATAKANA_HIRAGANA as i32, + Yen = ffi::AKEYCODE_YEN as i32, + Ro = ffi::AKEYCODE_RO as i32, + Kana = ffi::AKEYCODE_KANA as i32, + Assist = ffi::AKEYCODE_ASSIST as i32, + BrightnessDown = ffi::AKEYCODE_BRIGHTNESS_DOWN as i32, + BrightnessUp = ffi::AKEYCODE_BRIGHTNESS_UP as i32, + MediaAudioTrack = ffi::AKEYCODE_MEDIA_AUDIO_TRACK as i32, + Sleep = ffi::AKEYCODE_SLEEP as i32, + Wakeup = ffi::AKEYCODE_WAKEUP as i32, + Pairing = ffi::AKEYCODE_PAIRING as i32, + MediaTopMenu = ffi::AKEYCODE_MEDIA_TOP_MENU as i32, + Keycode11 = ffi::AKEYCODE_11 as i32, + Keycode12 = ffi::AKEYCODE_12 as i32, + LastChannel = ffi::AKEYCODE_LAST_CHANNEL as i32, + TvDataService = ffi::AKEYCODE_TV_DATA_SERVICE as i32, + VoiceAssist = ffi::AKEYCODE_VOICE_ASSIST as i32, + TvRadioService = ffi::AKEYCODE_TV_RADIO_SERVICE as i32, + TvTeletext = ffi::AKEYCODE_TV_TELETEXT as i32, + TvNumberEntry = ffi::AKEYCODE_TV_NUMBER_ENTRY as i32, + TvTerrestrialAnalog = ffi::AKEYCODE_TV_TERRESTRIAL_ANALOG as i32, + TvTerrestrialDigital = ffi::AKEYCODE_TV_TERRESTRIAL_DIGITAL as i32, + TvSatellite = ffi::AKEYCODE_TV_SATELLITE as i32, + TvSatelliteBs = ffi::AKEYCODE_TV_SATELLITE_BS as i32, + TvSatelliteCs = ffi::AKEYCODE_TV_SATELLITE_CS as i32, + TvSatelliteService = ffi::AKEYCODE_TV_SATELLITE_SERVICE as i32, + TvNetwork = ffi::AKEYCODE_TV_NETWORK as i32, + TvAntennaCable = ffi::AKEYCODE_TV_ANTENNA_CABLE as i32, + TvInputHdmi1 = ffi::AKEYCODE_TV_INPUT_HDMI_1 as i32, + TvInputHdmi2 = ffi::AKEYCODE_TV_INPUT_HDMI_2 as i32, + TvInputHdmi3 = ffi::AKEYCODE_TV_INPUT_HDMI_3 as i32, + TvInputHdmi4 = ffi::AKEYCODE_TV_INPUT_HDMI_4 as i32, + TvInputComposite1 = ffi::AKEYCODE_TV_INPUT_COMPOSITE_1 as i32, + TvInputComposite2 = ffi::AKEYCODE_TV_INPUT_COMPOSITE_2 as i32, + TvInputComponent1 = ffi::AKEYCODE_TV_INPUT_COMPONENT_1 as i32, + TvInputComponent2 = ffi::AKEYCODE_TV_INPUT_COMPONENT_2 as i32, + TvInputVga1 = ffi::AKEYCODE_TV_INPUT_VGA_1 as i32, + TvAudioDescription = ffi::AKEYCODE_TV_AUDIO_DESCRIPTION as i32, + TvAudioDescriptionMixUp = ffi::AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP as i32, + TvAudioDescriptionMixDown = ffi::AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN as i32, + TvZoomMode = ffi::AKEYCODE_TV_ZOOM_MODE as i32, + TvContentsMenu = ffi::AKEYCODE_TV_CONTENTS_MENU as i32, + TvMediaContextMenu = ffi::AKEYCODE_TV_MEDIA_CONTEXT_MENU as i32, + TvTimerProgramming = ffi::AKEYCODE_TV_TIMER_PROGRAMMING as i32, + Help = ffi::AKEYCODE_HELP as i32, + NavigatePrevious = ffi::AKEYCODE_NAVIGATE_PREVIOUS as i32, + NavigateNext = ffi::AKEYCODE_NAVIGATE_NEXT as i32, + NavigateIn = ffi::AKEYCODE_NAVIGATE_IN as i32, + NavigateOut = ffi::AKEYCODE_NAVIGATE_OUT as i32, + StemPrimary = ffi::AKEYCODE_STEM_PRIMARY as i32, + Stem1 = ffi::AKEYCODE_STEM_1 as i32, + Stem2 = ffi::AKEYCODE_STEM_2 as i32, + Stem3 = ffi::AKEYCODE_STEM_3 as i32, + DpadUpLeft = ffi::AKEYCODE_DPAD_UP_LEFT as i32, + DpadDownLeft = ffi::AKEYCODE_DPAD_DOWN_LEFT as i32, + DpadUpRight = ffi::AKEYCODE_DPAD_UP_RIGHT as i32, + DpadDownRight = ffi::AKEYCODE_DPAD_DOWN_RIGHT as i32, + MediaSkipForward = ffi::AKEYCODE_MEDIA_SKIP_FORWARD as i32, + MediaSkipBackward = ffi::AKEYCODE_MEDIA_SKIP_BACKWARD as i32, + MediaStepForward = ffi::AKEYCODE_MEDIA_STEP_FORWARD as i32, + MediaStepBackward = ffi::AKEYCODE_MEDIA_STEP_BACKWARD as i32, + SoftSleep = ffi::AKEYCODE_SOFT_SLEEP as i32, + Cut = ffi::AKEYCODE_CUT as i32, + Copy = ffi::AKEYCODE_COPY as i32, + Paste = ffi::AKEYCODE_PASTE as i32, + SystemNavigationUp = ffi::AKEYCODE_SYSTEM_NAVIGATION_UP as i32, + SystemNavigationDown = ffi::AKEYCODE_SYSTEM_NAVIGATION_DOWN as i32, + SystemNavigationLeft = ffi::AKEYCODE_SYSTEM_NAVIGATION_LEFT as i32, + SystemNavigationRight = ffi::AKEYCODE_SYSTEM_NAVIGATION_RIGHT as i32, + AllApps = ffi::AKEYCODE_ALL_APPS as i32, + Refresh = ffi::AKEYCODE_REFRESH as i32, + ThumbsUp = ffi::AKEYCODE_THUMBS_UP as i32, + ThumbsDown = ffi::AKEYCODE_THUMBS_DOWN as i32, + ProfileSwitch = ffi::AKEYCODE_PROFILE_SWITCH as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +impl KeyEvent { + /// Constructs a KeyEvent from a pointer to a native [`ffi::AInputEvent`] + /// + /// # Safety + /// By calling this method, you assert that the pointer is a valid, non-null pointer to an + /// [`ffi::AInputEvent`], and that [`ffi::AInputEvent`] is an `AKeyEvent`. + #[inline] + pub unsafe fn from_ptr(ptr: NonNull) -> Self { + Self { ptr } + } + + /// Creates a native [`InputEvent`] object that is a copy of the specified Java + /// [`android.view.KeyEvent`]. The result may be used with generic and [`KeyEvent`]-specific + /// functions. + /// + /// # Safety + /// + /// This function should be called with a healthy JVM pointer and with a non-null + /// [`android.view.KeyEvent`]. + /// + /// [`android.view.KeyEvent`]: https://developer.android.com/reference/android/view/KeyEvent + #[cfg(feature = "api-level-31")] + #[doc(alias = "AKeyEvent_fromJava")] + pub unsafe fn from_java(env: *mut JNIEnv, key_event: jobject) -> Option { + let ptr = unsafe { ffi::AKeyEvent_fromJava(env, key_event) }; + Some(InputEventJava(InputEvent::KeyEvent(Self::from_ptr( + NonNull::new(ptr.cast_mut())?, + )))) + } + + /// Returns a pointer to the native [`ffi::AInputEvent`]. + #[inline] + pub fn ptr(&self) -> NonNull { + self.ptr + } + + /// Returns the key action represented by this event. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getaction) + #[inline] + pub fn action(&self) -> KeyAction { + let action = unsafe { ffi::AKeyEvent_getAction(self.ptr.as_ptr()) }; + action.into() + } + + /// Get the source of the event. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getsource) + #[inline] + pub fn source(&self) -> Source { + let source = unsafe { ffi::AInputEvent_getSource(self.ptr.as_ptr()) }; + source.into() + } + + /// Get the device id associated with the event. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#ainputevent_getdeviceid) + #[inline] + pub fn device_id(&self) -> i32 { + unsafe { ffi::AInputEvent_getDeviceId(self.ptr.as_ptr()) } + } + + /// Returns the last time the key was pressed. This is on the scale of + /// `java.lang.System.nanoTime()`, which has nanosecond precision, but no defined start time. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getdowntime) + #[inline] + pub fn down_time(&self) -> i64 { + unsafe { ffi::AKeyEvent_getDownTime(self.ptr.as_ptr()) } + } + + /// Returns the time this event occured. This is on the scale of + /// `java.lang.System.nanoTime()`, which has nanosecond precision, but no defined start time. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_geteventtime) + #[inline] + pub fn event_time(&self) -> i64 { + unsafe { ffi::AKeyEvent_getEventTime(self.ptr.as_ptr()) } + } + + /// Returns the keycode associated with this key event + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getkeycode) + #[inline] + pub fn key_code(&self) -> Keycode { + let keycode = unsafe { ffi::AKeyEvent_getKeyCode(self.ptr.as_ptr()) }; + keycode.into() + } + + /// Returns the number of repeats of a key. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getrepeatcount) + #[inline] + pub fn repeat_count(&self) -> i32 { + unsafe { ffi::AKeyEvent_getRepeatCount(self.ptr.as_ptr()) } + } + + /// Returns the hardware keycode of a key. This varies from device to device. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getscancode) + #[inline] + pub fn scan_code(&self) -> i32 { + unsafe { ffi::AKeyEvent_getScanCode(self.ptr.as_ptr()) } + } +} + +/// Flags associated with [`KeyEvent`]. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct KeyEventFlags(pub u32); + +impl KeyEventFlags { + #[inline] + pub fn cancelled(&self) -> bool { + self.0 & ffi::AKEY_EVENT_FLAG_CANCELED != 0 + } + #[inline] + pub fn cancelled_long_press(&self) -> bool { + self.0 & ffi::AKEY_EVENT_FLAG_CANCELED_LONG_PRESS != 0 + } + #[inline] + pub fn editor_action(&self) -> bool { + self.0 & ffi::AKEY_EVENT_FLAG_EDITOR_ACTION != 0 + } + #[inline] + pub fn fallback(&self) -> bool { + self.0 & ffi::AKEY_EVENT_FLAG_FALLBACK != 0 + } + #[inline] + pub fn from_system(&self) -> bool { + self.0 & ffi::AKEY_EVENT_FLAG_FROM_SYSTEM != 0 + } + #[inline] + pub fn keep_touch_mode(&self) -> bool { + self.0 & ffi::AKEY_EVENT_FLAG_KEEP_TOUCH_MODE != 0 + } + #[inline] + pub fn long_press(&self) -> bool { + self.0 & ffi::AKEY_EVENT_FLAG_LONG_PRESS != 0 + } + #[inline] + pub fn soft_keyboard(&self) -> bool { + self.0 & ffi::AKEY_EVENT_FLAG_SOFT_KEYBOARD != 0 + } + #[inline] + pub fn tracking(&self) -> bool { + self.0 & ffi::AKEY_EVENT_FLAG_TRACKING != 0 + } + #[inline] + pub fn virtual_hard_key(&self) -> bool { + self.0 & ffi::AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY != 0 + } + #[inline] + pub fn woke_here(&self) -> bool { + self.0 & ffi::AKEY_EVENT_FLAG_WOKE_HERE != 0 + } +} + +impl KeyEvent { + /// Flags associated with this [`KeyEvent`]. + /// + /// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getflags) + #[inline] + pub fn flags(&self) -> KeyEventFlags { + unsafe { KeyEventFlags(ffi::AKeyEvent_getFlags(self.ptr.as_ptr()) as u32) } + } + + /// Returns the state of the modifiers during this key event, represented by a bitmask. + /// + /// See [the NDK + /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getmetastate) + #[inline] + pub fn meta_state(&self) -> MetaState { + unsafe { MetaState(ffi::AKeyEvent_getMetaState(self.ptr.as_ptr()) as u32) } + } +} diff --git a/native/vendor/ndk-0.9.0/src/font.rs b/native/vendor/ndk-0.9.0/src/font.rs new file mode 100644 index 0000000..70f6900 --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/font.rs @@ -0,0 +1,552 @@ +//! Bindings for [`AFont`], [`AFontMatcher`], and [`ASystemFontIterator`] +//! +//! [`AFont`]: https://developer.android.com/ndk/reference/group/font +//! [`AFontMatcher`]: https://developer.android.com/ndk/reference/group/font#afontmatcher_create +//! [`ASystemFontIterator`]: https://developer.android.com/ndk/reference/group/font#asystemfontiterator_open + +#![cfg(feature = "api-level-29")] + +use std::convert::TryFrom; +use std::ffi::{CStr, OsStr}; +use std::fmt::{self, Write}; +use std::os::unix::prelude::OsStrExt; +use std::path::Path; +use std::ptr::NonNull; + +use num_enum::IntoPrimitive; + +/// An integer holding a valid font weight value between 1 and 1000. +/// +/// See the [`Font::weight`] definition for more details. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct FontWeight(u16); + +impl FontWeight { + pub const fn new(value: u16) -> Result { + if Self::MIN.0 <= value && value <= Self::MAX.0 { + Ok(Self(value)) + } else { + Err(FontWeightValueError(())) + } + } + + pub const fn to_u16(self) -> u16 { + self.0 + } + + /// The minimum value for the font weight value. Unlike [`ffi::AFONT_WEIGHT_MIN`] being `0`, + /// [`FontWeight::MIN`] is `1` to make the `MIN..MAX` range be inclusive, keeping consistency + /// between [`FontWeight`] and other types like `std::num::NonZeroU*`. + pub const MIN: FontWeight = FontWeight(ffi::AFONT_WEIGHT_MIN as u16 + 1); + + /// A font weight value for the thin weight. + pub const THIN: FontWeight = FontWeight(ffi::AFONT_WEIGHT_THIN as u16); + + /// A font weight value for the extra-light weight. + pub const EXTRA_LIGHT: FontWeight = FontWeight(ffi::AFONT_WEIGHT_EXTRA_LIGHT as u16); + + /// A font weight value for the light weight. + pub const LIGHT: FontWeight = FontWeight(ffi::AFONT_WEIGHT_LIGHT as u16); + + /// A font weight value for the normal weight. + pub const NORMAL: FontWeight = FontWeight(ffi::AFONT_WEIGHT_NORMAL as u16); + + /// A font weight value for the medium weight. + pub const MEDIUM: FontWeight = FontWeight(ffi::AFONT_WEIGHT_MEDIUM as u16); + + /// A font weight value for the semi-bold weight. + pub const SEMI_BOLD: FontWeight = FontWeight(ffi::AFONT_WEIGHT_SEMI_BOLD as u16); + + /// A font weight value for the bold weight. + pub const BOLD: FontWeight = FontWeight(ffi::AFONT_WEIGHT_BOLD as u16); + + /// A font weight value for the extra-bold weight. + pub const EXTRA_BOLD: FontWeight = FontWeight(ffi::AFONT_WEIGHT_EXTRA_BOLD as u16); + + /// A font weight value for the black weight. + pub const BLACK: FontWeight = FontWeight(ffi::AFONT_WEIGHT_BLACK as u16); + + /// The maximum value for the font weight value. + pub const MAX: FontWeight = FontWeight(ffi::AFONT_WEIGHT_MAX as u16); +} + +impl fmt::Display for FontWeight { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(match *self { + FontWeight::THIN => "Thin", + FontWeight::EXTRA_LIGHT => "Extra Light (Ultra Light)", + FontWeight::LIGHT => "Light", + FontWeight::NORMAL => "Normal (Regular)", + FontWeight::MEDIUM => "Medium", + FontWeight::SEMI_BOLD => "Semi Bold (Demi Bold)", + FontWeight::BOLD => "Bold", + FontWeight::EXTRA_BOLD => "Extra Bold (Ultra Bold)", + FontWeight::BLACK => "Black (Heavy)", + _ => return writeln!(f, "{}", self.0), + }) + } +} + +/// The error type returned when an invalid font weight value is passed. +#[derive(Debug)] +pub struct FontWeightValueError(()); + +impl fmt::Display for FontWeightValueError { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.write_str("font weight must be positive and less than or equal to 1000") + } +} + +impl std::error::Error for FontWeightValueError {} + +impl TryFrom for FontWeight { + type Error = FontWeightValueError; + + fn try_from(value: u16) -> Result { + FontWeight::new(value) + } +} + +/// A 4-byte integer representing an OpenType axis tag. +#[derive(Clone, Copy, PartialEq, Eq)] +pub struct AxisTag(u32); + +impl AxisTag { + /// Checks whether the given 4-byte array can construct a valid axis tag and returns + /// [`Ok(AxisTag)`] if the array is valid. + /// + /// Each byte in a tag must be in the range 0x20 to 0x7E. A space character cannot be followed + /// by a non-space character. A tag must have one to four non-space characters. See the + /// [OpenType spec] for more details. + /// + /// [OpenType spec]: https://learn.microsoft.com/en-us/typography/opentype/spec/otff#data-types + pub const fn from_be_bytes_checked(value: [u8; 4]) -> Result { + // Each byte in a tag must be in the range 0x20 to 0x7E. + macro_rules! check_byte_range { + ($($e:expr)+) => { + $( + if !(value[$e] as char).is_ascii_graphic() && value[$e] != b' ' { + return Err(AxisTagValueError::InvalidCharacter); + } + )+ + }; + } + check_byte_range!(0 1 2 3); + + if value[0] == b' ' { + return Err( + if value[1] == b' ' && value[2] == b' ' && value[3] == b' ' { + // A tag must have one to four non-space characters. + AxisTagValueError::EmptyTag + } else { + // A space character cannot be followed by a non-space character. + AxisTagValueError::InvalidSpacePadding + }, + ); + } + + macro_rules! check_if_valid { + ($e:expr ; $($f:expr)+) => { + if value[$e] == b' ' { + return if true $(&& value[$f] == b' ')+ { + Ok(Self(u32::from_be_bytes(value))) + } else { + // A space character cannot be followed by a non-space character. + Err(AxisTagValueError::InvalidSpacePadding) + }; + } + }; + } + + check_if_valid!(1; 2 3); + check_if_valid!(2; 3); + + // Whether or not value[3] is b' ', value is a valid axis tag. + Ok(Self(u32::from_be_bytes(value))) + } + + /// Checks whether the given 4-byte array can construct a valid axis tag and returns + /// [`Ok(AxisTag)`] if the array is valid. + /// + /// See [`AxisTag::from_be()`] for more details. + pub const fn from_be_checked(value: u32) -> Result { + Self::from_be_bytes_checked(value.to_be_bytes()) + } + + /// Construct an axis tag from the given 4-byte array. If the resulting axis tag is invalid, + /// this function panics. + /// + /// See [`AxisTag::from_be()`] for more details. + pub const fn from_be_bytes(value: [u8; 4]) -> Self { + Self::unwrap_result(Self::from_be_bytes_checked(value)) + } + + /// Construct an axis tag from the given 4-byte integer. If the resulting axis tag is invalid, + /// this function panics. + /// + /// See [`AxisTag::from_be()`] for more details. + pub const fn from_be(value: u32) -> Self { + Self::unwrap_result(Self::from_be_checked(value)) + } + + /// const-version of [`Result::unwrap`]. Should be removed when [`Option::unwrap`] or + /// [`Result::unwrap`] become `const`-stable. + const fn unwrap_result(result: Result) -> Self { + match result { + Ok(t) => t, + Err(e) => panic!("{}", e.as_str()), + } + } + + pub const fn to_u32(self) -> u32 { + self.0 + } + + pub const fn to_be_bytes(self) -> [u8; 4] { + self.0.to_be_bytes() + } +} + +impl fmt::Display for AxisTag { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let bytes = self.to_be_bytes(); + f.write_char(bytes[0] as char)?; + f.write_char(bytes[1] as char)?; + f.write_char(bytes[2] as char)?; + f.write_char(bytes[3] as char) + } +} + +impl fmt::Debug for AxisTag { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "AxisTag({} {:#x})", self, self.0) + } +} + +/// The error type returned when an invalid axis tag value is passed. +#[derive(Clone, Copy, Debug)] +pub enum AxisTagValueError { + /// There is a byte not in the range 0x20 to 0x7E. + InvalidCharacter, + /// There is a space character followed by a non-space character. + InvalidSpacePadding, + /// The tag only consists of space characters. + EmptyTag, +} + +impl AxisTagValueError { + pub const fn as_str(&self) -> &'static str { + match self { + Self::InvalidCharacter => "each byte in an axis tag must be in the range 0x20 to 0x7E", + Self::InvalidSpacePadding => { + "a space character cannot be followed by a non-space character" + } + Self::EmptyTag => "a tag must have one to four non-space characters", + } + } +} + +impl fmt::Display for AxisTagValueError { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.write_str(self.as_str()) + } +} + +impl std::error::Error for AxisTagValueError {} + +/// A native [`AFont *`] +/// +/// [`AFont *`]: https://developer.android.com/ndk/reference/group/font +#[derive(Debug)] +pub struct Font { + ptr: NonNull, +} + +impl Font { + /// Assumes ownership of `ptr`. + /// + /// # Safety + /// `ptr` must be a valid owning pointer to an Android [`ffi::AFont`]. + pub unsafe fn from_ptr(ptr: NonNull) -> Self { + Self { ptr } + } + + /// Returns s the pointer to the native [`ffi::AFont`]. + pub fn ptr(&self) -> NonNull { + self.ptr + } + + /// Returns a count of font variation settings associated with the current font. + /// + /// The font variation settings are provided as multiple tag-value pairs. + /// + /// For example, bold italic font may have following font variation settings: `'wght' 700`, + /// `'slnt' -12`. In this case, [`Font::axis_count()`] returns `2` and [`Font::axis_tag_at()`] and + /// [`Font::axis_value_at()`] return those variation names and the corresponding values. + /// + /// ```no_run + /// use ndk::font::Font; + /// + /// let font: Font = todo!(); + /// for idx in 0..font.axis_count() { + /// log::debug!("{}: {}", font.axis_tag_at(idx), font.axis_value_at(idx)); + /// } + /// // Output: + /// // wght: 700 + /// // slnt: -12 + /// ``` + pub fn axis_count(&self) -> usize { + unsafe { ffi::AFont_getAxisCount(self.ptr.as_ptr()) } + } + + /// Returns an OpenType axis tag associated with the current font. + /// + /// See [`Font::axis_count()`] for more details. + pub fn axis_tag_at(&self, idx: usize) -> AxisTag { + // Android returns Axis Tag in big-endian. + // See https://cs.android.com/android/platform/superproject/+/refs/heads/master:frameworks/base/native/android/system_fonts.cpp;l=197 for details + AxisTag(unsafe { ffi::AFont_getAxisTag(self.ptr.as_ptr(), idx as u32) }) + } + + /// Returns an OpenType axis value associated with the current font. + /// + /// See [`Font::axis_count()`] for more details. + pub fn axis_value_at(&self, idx: usize) -> f32 { + unsafe { ffi::AFont_getAxisValue(self.ptr.as_ptr(), idx as u32) } + } + + /// Returns a font collection index value associated with the current font. + /// + /// In case the target font file is a font collection (e.g. `.ttc` or `.otc`), this returns a + /// non-negative value as a font offset in the collection. This always returns 0 if the target + /// font file is a regular font. + pub fn collection_index(&self) -> usize { + unsafe { ffi::AFont_getCollectionIndex(self.ptr.as_ptr()) } + } + + /// Returns an absolute path to the current font file. + /// + /// Here is a list of font formats returned by this method: + /// + /// * OpenType + /// * OpenType Font Collection + /// * TrueType + /// * TrueType Collection + /// + /// The file extension could be one of `*.otf`, `*.ttf`, `*.otc` or `*.ttc`. + /// The font file specified by the returned path is guaranteed to be openable with `O_RDONLY`. + pub fn path(&self) -> &Path { + let path = unsafe { CStr::from_ptr(ffi::AFont_getFontFilePath(self.ptr.as_ptr())) }; + OsStr::from_bytes(path.to_bytes()).as_ref() + } + + /// Returns an IETF BCP47 compliant language tag associated with the current font. + /// + /// For information about IETF BCP47, read [`Locale.forLanguageTag(java.lang.String)`]. + /// + /// [`Locale.forLanguageTag(java.lang.String)`]: https://developer.android.com/reference/java/util/Locale.html#forLanguageTag(java.lang.String) + pub fn locale(&self) -> Option<&CStr> { + let ptr = unsafe { ffi::AFont_getLocale(self.ptr.as_ptr()) }; + if ptr.is_null() { + None + } else { + Some(unsafe { CStr::from_ptr(ptr) }) + } + } + + /// Returns a weight value associated with the current font. + /// + /// The weight values are positive and less than or equal to 1000. Here are pairs of the common + /// names and their values. + /// + /// | Value | Name | NDK Definition | + /// | ----- | ------------------------- | --------------------------- | + /// | 100 | Thin | [`FontWeight::THIN`] | + /// | 200 | Extra Light (Ultra Light) | [`FontWeight::EXTRA_LIGHT`] | + /// | 300 | Light | [`FontWeight::LIGHT`] | + /// | 400 | Normal (Regular) | [`FontWeight::NORMAL`] | + /// | 500 | Medium | [`FontWeight::MEDIUM`] | + /// | 600 | Semi Bold (Demi Bold) | [`FontWeight::SEMI_BOLD`] | + /// | 700 | Bold | [`FontWeight::BOLD`] | + /// | 800 | Extra Bold (Ultra Bold) | [`FontWeight::EXTRA_BOLD`] | + /// | 900 | Black (Heavy) | [`FontWeight::BLACK`] | + pub fn weight(&self) -> FontWeight { + FontWeight(unsafe { ffi::AFont_getWeight(self.ptr.as_ptr()) }) + } + + /// Returns [`true`] if the current font is italic, otherwise returns [`false`]. + pub fn is_italic(&self) -> bool { + unsafe { ffi::AFont_isItalic(self.ptr.as_ptr()) } + } +} + +impl Drop for Font { + fn drop(&mut self) { + unsafe { ffi::AFont_close(self.ptr.as_ptr()) } + } +} + +/// Corresponds to [`AFAMILY_VARIANT_*`]. +/// +/// [`AFAMILY_VARIANT_*`]: https://developer.android.com/ndk/reference/group/font#group___font_1gga96a58e29e8dbf2b5bdeb775cba46556ea662aafc7016e35d6758da93416fc0833 +#[repr(u32)] +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, IntoPrimitive)] +#[non_exhaustive] +pub enum FamilyVariant { + /// A family variant value for the compact font family variant. + /// The compact font family has Latin-based vertical metrics. + Compact = ffi::AFAMILY_VARIANT_COMPACT, + /// A family variant value for the system default variant. + Default = ffi::AFAMILY_VARIANT_DEFAULT, + /// A family variant value for the elegant font family variant. + /// The elegant font family may have larger vertical metrics than Latin font. + Elegant = ffi::AFAMILY_VARIANT_ELEGANT, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(u32), +} + +/// A native [`AFontMatcher *`] +/// +/// [`AFontMatcher *`]: https://developer.android.com/ndk/reference/group/font#afontmatcher_create +#[derive(Debug)] +pub struct FontMatcher { + ptr: NonNull, +} + +impl FontMatcher { + /// Assumes ownership of `ptr`. + /// + /// # Safety + /// `ptr` must be a valid owning pointer to an Android [`ffi::AFontMatcher`]. + pub unsafe fn from_ptr(ptr: NonNull) -> Self { + Self { ptr } + } + + /// Returns s the pointer to the native [`ffi::AFontMatcher`]. + pub fn ptr(&self) -> NonNull { + self.ptr + } + + /// Creates a new [`FontMatcher`] object. [`FontMatcher`] selects the best font from the + /// parameters set by the user. + pub fn new() -> Self { + let ptr = NonNull::new(unsafe { ffi::AFontMatcher_create() }) + .expect("AFontMatcher_create returned NULL"); + unsafe { FontMatcher::from_ptr(ptr) } + } + + /// Performs the matching from the generic font family for the text and select one font. + /// + /// For more information about generic font families, please read the + /// [W3C spec](https://www.w3.org/TR/css-fonts-4/#generic-font-families). + /// + /// Even if no font can render the given text, this function will return a non-null result for + /// drawing Tofu character. + /// + /// # Parameters + /// + /// - `family_name`: A font family name. + /// - `text`: A UTF-16 encoded text buffer to be rendered. If an empty string is given, this + /// function will panic. + /// - `run_length_out`: Set this to [`Some`] if you want to get the length of the text run with + /// the font returned. + pub fn match_font( + &mut self, + family_name: &CStr, + text: &[u16], + run_length_out: Option<&mut u32>, + ) -> Font { + if text.is_empty() { + panic!("text is empty"); + } + unsafe { + Font::from_ptr( + NonNull::new(ffi::AFontMatcher_match( + self.ptr.as_ptr(), + family_name.as_ptr(), + text.as_ptr(), + text.len() as _, + run_length_out.map_or(std::ptr::null_mut(), |u| u), + )) + .expect("AFontMatcher_match returned NULL"), + ) + } + } + + /// Sets the family variant of the font to be matched. + /// + /// If this function is not called, the match is performed with [`FamilyVariant::Default`]. + pub fn set_family_variant(&mut self, family_variant: FamilyVariant) { + unsafe { ffi::AFontMatcher_setFamilyVariant(self.ptr.as_ptr(), family_variant.into()) } + } + + /// Sets the locale of the font to be matched. + /// + /// If this function is not called, the match is performed with an empty locale list. + /// + /// # Parameters + /// + /// - `language_tags`: comma separated IETF BCP47 compliant language tags. + pub fn set_locales(&mut self, language_tags: &CStr) { + unsafe { ffi::AFontMatcher_setLocales(self.ptr.as_ptr(), language_tags.as_ptr()) } + } + + /// Sets the style of the font to be matched. + /// + /// If this function is not called, the match is performed with [`FontWeight::NORMAL`] with non-italic style. + pub fn set_style(&mut self, weight: FontWeight, italic: bool) { + unsafe { ffi::AFontMatcher_setStyle(self.ptr.as_ptr(), weight.to_u16(), italic) } + } +} + +impl Drop for FontMatcher { + fn drop(&mut self) { + unsafe { ffi::AFontMatcher_destroy(self.ptr.as_ptr()) } + } +} + +/// A native [`ASystemFontIterator *`] +/// +/// [`ASystemFontIterator *`]: https://developer.android.com/ndk/reference/group/font#asystemfontiterator_open +#[derive(Debug)] +pub struct SystemFontIterator { + ptr: NonNull, +} + +impl SystemFontIterator { + /// Assumes ownership of `ptr`. + /// + /// # Safety + /// `ptr` must be a valid owning pointer to an Android [`ffi::ASystemFontIterator`]. + pub unsafe fn from_ptr(ptr: NonNull) -> Self { + Self { ptr } + } + + /// Returns the pointer to the native [`ffi::ASystemFontIterator`]. + pub fn ptr(&self) -> NonNull { + self.ptr + } + + /// Creates a system font iterator. + pub fn new() -> Option { + NonNull::new(unsafe { ffi::ASystemFontIterator_open() }) + .map(|p| unsafe { SystemFontIterator::from_ptr(p) }) + } +} + +impl Iterator for SystemFontIterator { + type Item = Font; + + fn next(&mut self) -> Option { + NonNull::new(unsafe { ffi::ASystemFontIterator_next(self.ptr.as_ptr()) }) + .map(|p| unsafe { Font::from_ptr(p) }) + } +} + +impl Drop for SystemFontIterator { + fn drop(&mut self) { + unsafe { ffi::ASystemFontIterator_close(self.ptr.as_ptr()) } + } +} diff --git a/native/vendor/ndk-0.9.0/src/hardware_buffer.rs b/native/vendor/ndk-0.9.0/src/hardware_buffer.rs new file mode 100644 index 0000000..252b8bb --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/hardware_buffer.rs @@ -0,0 +1,640 @@ +//! Bindings for [`AHardwareBuffer`] +//! +//! [`AHardwareBuffer`]: https://developer.android.com/ndk/reference/group/a-hardware-buffer#ahardwarebuffer + +#![cfg(feature = "api-level-26")] + +use std::{ + io::Result, + mem::MaybeUninit, + ops::Deref, + os::{ + fd::{AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd}, + raw::c_void, + }, + ptr::NonNull, +}; + +use jni_sys::{jobject, JNIEnv}; + +use super::{hardware_buffer_format::HardwareBufferFormat, utils::status_to_io_result}; + +bitflags::bitflags! { + /// Buffer usage flags, specifying how the buffer will be accessed. + #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] + #[doc(alias = "AHardwareBuffer_UsageFlags")] + pub struct HardwareBufferUsage : u64 { + /// The buffer will never be locked for direct CPU reads using the + /// [`HardwareBuffer::lock()`] function. Note that reading the buffer using OpenGL or Vulkan + /// functions or memory mappings is still allowed. + #[doc(alias = "AHARDWAREBUFFER_USAGE_CPU_READ_NEVER")] + const CPU_READ_NEVER = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_NEVER.0; + /// The buffer will sometimes be locked for direct CPU reads using the + /// [`HardwareBuffer::lock()`] function. Note that reading the buffer using OpenGL or Vulkan + /// functions or memory mappings does not require the presence of this flag. + #[doc(alias = "AHARDWAREBUFFER_USAGE_CPU_READ_RARELY")] + const CPU_READ_RARELY = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_RARELY.0; + /// The buffer will often be locked for direct CPU reads using the + /// [`HardwareBuffer::lock()`] function. Note that reading the buffer using OpenGL or Vulkan + /// functions or memory mappings does not require the presence of this flag. + #[doc(alias = "AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN")] + const CPU_READ_OFTEN = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN.0; + /// CPU read value mask. + #[doc(alias = "AHARDWAREBUFFER_USAGE_CPU_READ_MASK")] + const CPU_READ_MASK = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_MASK.0; + + /// The buffer will never be locked for direct CPU writes using the + /// [`HardwareBuffer::lock()`] function. Note that writing the buffer using OpenGL or Vulkan + /// functions or memory mappings is still allowed. + #[doc(alias = "AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER")] + const CPU_WRITE_NEVER = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER.0; + /// The buffer will sometimes be locked for direct CPU writes using the + /// [`HardwareBuffer::lock()`] function. Note that writing the buffer using OpenGL or Vulkan + /// functions or memory mappings does not require the presence of this flag. + #[doc(alias = "AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY")] + const CPU_WRITE_RARELY = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY.0; + /// The buffer will often be locked for direct CPU writes using the + /// [`HardwareBuffer::lock()`] function. Note that writing the buffer using OpenGL or Vulkan + /// functions or memory mappings does not require the presence of this flag. + #[doc(alias = "AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN")] + const CPU_WRITE_OFTEN = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN.0; + /// CPU write value mask. + #[doc(alias = "AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK")] + const CPU_WRITE_MASK = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK.0; + + /// The buffer will be read from by the GPU as a texture. + #[doc(alias = "AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE")] + const GPU_SAMPLED_IMAGE = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE.0; + /// The buffer will be written to by the GPU as a framebuffer attachment. + #[doc(alias = "AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER")] + const GPU_FRAMEBUFFER = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER.0; + /// The buffer will be written to by the GPU as a framebuffer attachment. + /// + /// Note that the name of this flag is somewhat misleading: it does not imply that the + /// buffer contains a color format. A buffer with depth or stencil format that will be + /// used as a framebuffer attachment should also have this flag. Use the equivalent flag + /// [`HardwareBufferusage::GPU_FRAMEBUFFER`] to avoid this confusion. + #[doc(alias = "AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT")] + const GPU_COLOR_OUTPUT = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT.0; + /// The buffer will be used as a composer HAL overlay layer. + /// + /// This flag is currently only needed when using [`SurfaceTransaction::set_buffer()`] to + /// set a buffer. In all other cases, the framework adds this flag internally to buffers + /// that could be presented in a composer overlay. [`SurfaceTransaction::set_buffer()`] + /// is special because it uses buffers allocated directly through + /// [`HardwareBuffer::allocate()`] instead of buffers allocated by the framework. + #[doc(alias = "AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY")] + const COMPOSER_OVERLAY = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY.0; + /// The buffer is protected from direct CPU access or being read by non-secure hardware, + /// such as video encoders. + /// + /// This flag is incompatible with CPU read and write flags. It is mainly used when handling + /// DRM video. Refer to the EGL extension [`EGL_EXT_protected_content`] and GL extension + /// [`GL_EXT_protected_textures`] for more information on how these buffers are expected + /// to behave. + /// + /// [`EGL_EXT_protected_content`]: https://registry.khronos.org/EGL/extensions/EXT/EGL_EXT_protected_content.txt + /// [`GL_EXT_protected_textures`]: https://registry.khronos.org/OpenGL/extensions/EXT/EXT_protected_textures.txt + #[doc(alias = "AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT")] + const PROTECTED_CONTENT = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT.0; + /// The buffer will be read by a hardware video encoder. + #[doc(alias = "AHARDWAREBUFFER_USAGE_VIDEO_ENCODE")] + const VIDEO_ENCODE = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VIDEO_ENCODE.0; + /// The buffer will be used for direct writes from sensors. When this flag is present, the + /// format must be [`HardwareBufferFormat::Blob`]. + #[doc(alias = "AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA")] + const SENSOR_DIRECT_DATA = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA.0; + /// The buffer will be used as a shader storage or uniform buffer object. When this flag is + /// present, the format must be [`HardwareBufferFormat::Blob`]. + #[doc(alias = "AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER")] + const GPU_DATA_BUFFER = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER.0; + /// The buffer will be used as a cube map texture. When this flag is present, the buffer + /// must have a layer count that is a multiple of 6. Note that buffers with this flag must + /// be bound to OpenGL textures using the extension [`GL_EXT_EGL_image_storage`] instead + /// of [`GL_KHR_EGL_image`]. + /// + /// [`GL_EXT_EGL_image_storage`]: https://registry.khronos.org/OpenGL/extensions/EXT/EXT_EGL_image_storage.txt + // TODO: This extension only exists for VG. Reported at https://issuetracker.google.com/issues/300602767#comment16 + /// [`GL_KHR_EGL_image`]: https://registry.khronos.org/OpenVG/extensions/KHR/VG_KHR_EGL_image.txt + #[doc(alias = "AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP")] + const GPU_CUBE_MAP = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP.0; + /// The buffer contains a complete mipmap hierarchy. Note that buffers with this flag must + /// be bound to OpenGL textures using the extension [`GL_EXT_EGL_image_storage`] instead + /// of [`GL_KHR_EGL_image`]. + /// + /// [`GL_EXT_EGL_image_storage`]: https://registry.khronos.org/OpenGL/extensions/EXT/EXT_EGL_image_storage.txt + // TODO: This extension only exists for VG. Reported at https://issuetracker.google.com/issues/300602767#comment16 + /// [`GL_KHR_EGL_image`]: https://registry.khronos.org/OpenVG/extensions/KHR/VG_KHR_EGL_image.txt + #[doc(alias = "AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE")] + const GPU_MIPMAP_COMPLETE = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE.0; + + // TODO: Only available in a newer NDK + // /// Usage: The buffer is used for front-buffer rendering. When front-buffering rendering + // /// is specified, different usages may adjust their behavior as a result. For example, when + // /// used as [`HardwareBufferFormat::GPU_COLOR_OUTPUT`] the buffer will behave similar to a + // /// single-buffered window. When used with [`HardwareBufferFormat::COMPOSER_OVERLAY`], the + // /// system will try to prioritize the buffer receiving an overlay plane & avoid caching it + // /// in intermediate composition buffers. + // #[doc(alias = "AHARDWAREBUFFER_USAGE_FRONT_BUFFER")] + // const USAGE_FRONT_BUFFER = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_FRONT_BUFFER.0; + + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_0")] + const VENDOR_0 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_0.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_1")] + const VENDOR_1 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_1.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_2")] + const VENDOR_2 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_2.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_3")] + const VENDOR_3 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_3.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_4")] + const VENDOR_4 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_4.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_5")] + const VENDOR_5 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_5.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_6")] + const VENDOR_6 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_6.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_7")] + const VENDOR_7 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_7.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_8")] + const VENDOR_8 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_8.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_9")] + const VENDOR_9 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_9.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_10")] + const VENDOR_10 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_10.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_11")] + const VENDOR_11 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_11.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_12")] + const VENDOR_12 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_12.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_13")] + const VENDOR_13 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_13.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_14")] + const VENDOR_14 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_14.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_15")] + const VENDOR_15 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_15.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_16")] + const VENDOR_16 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_16.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_17")] + const VENDOR_17 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_17.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_18")] + const VENDOR_18 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_18.0; + #[doc(alias = "AHARDWAREBUFFER_USAGE_VENDOR_19")] + const VENDOR_19 = ffi::AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_VENDOR_19.0; + } +} + +impl HardwareBufferUsage { + /// Helper to read [`HardwareBufferUsage::CPU_READ_MASK`] values. + #[doc(alias = "AHARDWAREBUFFER_USAGE_CPU_READ_MASK")] + pub fn cpu_read(self) -> HardwareBufferUsage { + self.intersection(Self::CPU_READ_MASK) + } + + /// Helper to read [`HardwareBufferUsage::CPU_WRITE_MASK`] values. + #[doc(alias = "AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK")] + pub fn cpu_write(self) -> HardwareBufferUsage { + self.intersection(Self::CPU_WRITE_MASK) + } +} + +pub type Rect = ffi::ARect; + +fn construct(with_ptr: impl FnOnce(*mut T) -> i32) -> Result { + let mut result = MaybeUninit::uninit(); + let status = with_ptr(result.as_mut_ptr()); + status_to_io_result(status).map(|()| unsafe { result.assume_init() }) +} + +/// A native [`AHardwareBuffer *`] +/// +/// [`HardwareBuffer`] objects represent chunks of memory that can be accessed by various hardware +/// components in the system. +/// +/// It can be easily converted to the Java counterpart [`android.hardware.HardwareBuffer`] and +/// passed between processes using Binder. All operations involving [`HardwareBuffer`] and +/// [`android.hardware.HardwareBuffer`] are zero-copy, i.e., passing [`HardwareBuffer`] to another +/// process creates a shared view of the same region of memory. +/// +/// [`HardwareBuffer`] can be bound to EGL/OpenGL and Vulkan primitives. For EGL, use the extension +/// function [`eglGetNativeClientBufferANDROID`] to obtain an `EGLClientBuffer` and pass it +/// directly to [`eglCreateImageKHR`]. Refer to the EGL extensions +/// [`EGL_ANDROID_get_native_client_buffer`] and [`EGL_ANDROID_image_native_buffer`] for more +/// information. In Vulkan, the contents of the [`HardwareBuffer`] can be accessed as [external +/// memory]. See the [`VK_ANDROID_external_memory_android_hardware_buffer`] extension for details. +/// +/// [`AHardwareBuffer *`]: https://developer.android.com/ndk/reference/group/a-hardware-buffer#ahardwarebuffer +/// [`android.hardware.HardwareBuffer`]: https://developer.android.com/reference/android/hardware/HardwareBuffer +/// [`eglGetNativeClientBufferANDROID`]: https://www.khronos.org/registry/EGL/extensions/ANDROID/EGL_ANDROID_get_native_client_buffer.txt +/// [`eglCreateImageKHR`]: https://www.khronos.org/registry/EGL/extensions/KHR/EGL_KHR_image_base.txt +/// [`EGL_ANDROID_get_native_client_buffer`]: https://www.khronos.org/registry/EGL/extensions/ANDROID/EGL_ANDROID_get_native_client_buffer.txt +/// [`EGL_ANDROID_image_native_buffer`]: https://www.khronos.org/registry/EGL/extensions/ANDROID/EGL_ANDROID_image_native_buffer.txt +/// [external memory]: https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VK_KHR_external_memory.html +/// [`VK_ANDROID_external_memory_android_hardware_buffer`]: https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VK_ANDROID_external_memory_android_hardware_buffer.html +#[derive(Debug)] +pub struct HardwareBuffer { + inner: NonNull, +} + +impl HardwareBuffer { + /// Create an _unowned_ [`HardwareBuffer`] from a native pointer + /// + /// To wrap a strong reference (that is `release`d on [`Drop`]), call + /// [`HardwareBufferRef::from_ptr()`] instead. + /// + /// # Safety + /// By calling this function, you assert that it is a valid pointer to an NDK + /// [`ffi::AHardwareBuffer`] that is kept alive externally, or retrieve a strong reference + /// using [`HardwareBuffer::acquire()`]. + pub unsafe fn from_ptr(ptr: NonNull) -> Self { + Self { inner: ptr } + } + + /// Returns the underlying [`ffi::AHardwareBuffer`] pointer + /// + /// See the top-level [`HardwareBuffer`] struct documentation for (graphics) APIs that accept + /// this pointer. + pub fn as_ptr(&self) -> *mut ffi::AHardwareBuffer { + self.inner.as_ptr() + } + + /// Allocates a buffer that matches the passed [`HardwareBufferDesc`]. + /// + /// If allocation succeeds, the buffer can be used according to the usage flags specified in + /// its description. If a buffer is used in ways not compatible with its usage flags, the + /// results are undefined and may include program termination. + pub fn allocate(desc: HardwareBufferDesc) -> Result { + unsafe { + let ptr = construct(|res| ffi::AHardwareBuffer_allocate(&desc.into_native(), res))?; + + Ok(HardwareBufferRef::from_ptr(NonNull::new_unchecked(ptr))) + } + } + + /// Create a [`HardwareBuffer`] from JNI pointers + /// + /// # Safety + /// By calling this function, you assert that these are valid pointers to JNI objects. + /// + /// This method does not acquire any additional reference to the AHardwareBuffer that is + /// returned. To keep the [`HardwareBuffer`] alive after the [Java `HardwareBuffer`] object + /// is closed, explicitly or by the garbage collector, be sure to retrieve a strong reference + /// using [`HardwareBuffer::acquire()`]. + /// + /// [Java `HardwareBuffer`]: https://developer.android.com/reference/android/hardware/HardwareBuffer + pub unsafe fn from_jni(env: *mut JNIEnv, hardware_buffer: jobject) -> Self { + let ptr = ffi::AHardwareBuffer_fromHardwareBuffer(env, hardware_buffer); + + Self::from_ptr(NonNull::new_unchecked(ptr)) + } + + /// # Safety + /// By calling this function, you assert that `env` is a valid pointer to a [`JNIEnv`]. + pub unsafe fn to_jni(&self, env: *mut JNIEnv) -> jobject { + ffi::AHardwareBuffer_toHardwareBuffer(env, self.as_ptr()) + } + + /// Return a description of the [`HardwareBuffer`] in the passed [`HardwareBufferDesc`] struct. + pub fn describe(&self) -> HardwareBufferDesc { + let desc = unsafe { + let mut result = MaybeUninit::uninit(); + ffi::AHardwareBuffer_describe(self.as_ptr(), result.as_mut_ptr()); + result.assume_init() + }; + + HardwareBufferDesc { + width: desc.width, + height: desc.height, + layers: desc.layers, + format: i32::try_from(desc.format) + .expect("i32->u32 overflow in HardwareBuffer::describe()") + .into(), + usage: HardwareBufferUsage::from_bits_retain(desc.usage), + stride: desc.stride, + } + } + + /// Test whether the given format and usage flag combination is allocatable. + /// + /// If this function returns [`true`], it means that a buffer with the given description can + /// be allocated on this implementation, unless resource exhaustion occurs. If this function + /// returns [`false`], it means that the allocation of the given description will never + /// succeed. + /// + /// The return value of this function may depend on all fields in the description, except + /// [`HardwareBufferDesc::stride`], which is always ignored. For example, some implementations + /// have implementation-defined limits on texture size and layer count. + #[cfg(feature = "api-level-29")] + pub fn is_supported(desc: HardwareBufferDesc) -> bool { + let res = unsafe { ffi::AHardwareBuffer_isSupported(&desc.into_native()) }; + res == 1 + } + + /// Get the system-wide unique id for this [`HardwareBuffer`]. + #[cfg(feature = "api-level-31")] + #[doc(alias = "AHardwareBuffer_getId")] + pub fn id(&self) -> Result { + construct(|res| unsafe { ffi::AHardwareBuffer_getId(self.as_ptr(), res) }) + } + + /// Lock the [`HardwareBuffer`] for direct CPU access. + /// + /// This function can lock the buffer for either reading or writing. It may block if the + /// hardware needs to finish rendering, if CPU caches need to be synchronized, or possibly for + /// other implementation-specific reasons. + /// + /// The [`HardwareBuffer`] must have one layer, otherwise the call will fail. + /// + /// If `fence` is not [`None`], it specifies a fence file descriptor on which to wait before + /// locking the buffer. If it's [`None`], the caller is responsible for ensuring that writes + /// to the buffer have completed before calling this function. Using this parameter is more + /// efficient than waiting on the fence and then calling this function. + /// + /// The `usage` parameter may only specify `HardwareBufferUsage::CPU_*`. If set, then the + /// address of the buffer in virtual memory is returned. The flags must also be compatible with + /// usage flags specified at buffer creation: if a read flag is passed, the buffer must have + /// been created with [`HardwareBufferUsage::CPU_READ_RARELY`] or + /// [`HardwareBufferUsage::CPU_READ_OFTEN`]. If a write flag is passed, it must have been + /// created with [`HardwareBufferUsage::CPU_WRITE_RARELY`] or + /// [`HardwareBufferUsage::CPU_WRITE_OFTEN`]. + /// + /// If `rect` is not [`None`], the caller promises to modify only data in the area specified by + /// `rect`. If rect is [`None`], the caller may modify the contents of the entire buffer. The + /// content of the buffer outside of the specified rect is NOT modified by this call. + /// + /// It is legal for several different threads to lock a buffer for read access; none of the + /// threads are blocked. + /// + /// Locking a buffer simultaneously for write or read/write is undefined, but will neither + /// terminate the process nor block the caller. This function may return an error or leave the + /// buffer's content in an indeterminate state. + /// + /// If the buffer has [`HardwareBufferFormat::BLOB`], it is legal lock it for reading and + /// writing in multiple threads and/or processes simultaneously, and the contents of the buffer + /// behave like shared memory. + pub fn lock( + &self, + usage: HardwareBufferUsage, + fence: Option, + rect: Option, + ) -> Result<*mut c_void> { + let fence = fence.map_or(-1, IntoRawFd::into_raw_fd); + let rect = match rect { + Some(rect) => &rect, + None => std::ptr::null(), + }; + construct(|res| unsafe { + ffi::AHardwareBuffer_lock(self.as_ptr(), usage.bits(), fence, rect, res) + }) + } + + /// Lock a [`HardwareBuffer`] for direct CPU access. + /// + /// This function is the same as the above [`lock()`][Self::lock()] function, but passes back + /// additional information about the bytes per pixel and the bytes per stride of the locked + /// buffer. If the bytes per pixel or bytes per stride are unknown or variable, or if the + /// underlying mapper implementation does not support returning additional information, then + /// this call will fail with [`std::io::Error::kind()`] = [`std::io::ErrorKind::Unsupported`]. + #[cfg(feature = "api-level-29")] + pub fn lock_and_get_info( + &self, + usage: HardwareBufferUsage, + fence: Option, + rect: Option, + ) -> Result { + let fence = fence.map_or(-1, IntoRawFd::into_raw_fd); + let rect = match rect { + Some(rect) => &rect, + None => std::ptr::null(), + }; + let mut virtual_address = MaybeUninit::uninit(); + let mut bytes_per_pixel = MaybeUninit::uninit(); + let mut bytes_per_stride = MaybeUninit::uninit(); + let status = unsafe { + ffi::AHardwareBuffer_lockAndGetInfo( + self.as_ptr(), + usage.bits(), + fence, + rect, + virtual_address.as_mut_ptr(), + bytes_per_pixel.as_mut_ptr(), + bytes_per_stride.as_mut_ptr(), + ) + }; + status_to_io_result(status).map(|()| unsafe { + LockedPlaneInfo { + virtual_address: virtual_address.assume_init(), + bytes_per_pixel: bytes_per_pixel.assume_init() as u32, + bytes_per_stride: bytes_per_stride.assume_init() as u32, + } + }) + } + + /// Lock a potentially multi-planar [`HardwareBuffer`] for direct CPU access. + /// + /// This function is similar to [`lock()`][Self::lock()], but can lock multi-planar formats. + /// Note, that multi-planar should not be confused with multi-layer images, which this locking + /// function does not support. + /// + /// YUV formats are always represented by three separate planes of data, one for each color + /// plane. The order of planes in the array is guaranteed such that plane #0 is always `Y`, + /// plane #1 is always `U` (`Cb`), and plane #2 is always `V` (`Cr`). All other formats are + /// represented by a single plane. + /// + /// Additional information always accompanies the buffers, describing the row stride and the + /// pixel stride for each plane. + /// + /// In case the buffer cannot be locked, this will return zero planes. + /// + /// See the [`lock()`][Self::lock()] documentation for all other locking semantics. + #[cfg(feature = "api-level-29")] + pub fn lock_planes( + &self, + usage: HardwareBufferUsage, + fence: Option, + rect: Option, + ) -> Result { + let fence = fence.map_or(-1, IntoRawFd::into_raw_fd); + let rect = match rect { + Some(rect) => &rect, + None => std::ptr::null(), + }; + let planes = construct(|res| unsafe { + ffi::AHardwareBuffer_lockPlanes(self.as_ptr(), usage.bits(), fence, rect, res) + })?; + + Ok(HardwareBufferPlanes { + inner: planes, + index: 0, + }) + } + + /// Unlock the [`HardwareBuffer`] from direct CPU access. + /// + /// Must be called after all changes to the buffer are completed by the caller. The function + /// will block until all work is completed. See [`unlock_async()`][Self::unlock_async()] for + /// a non-blocking variant that returns a file descriptor to be signaled on unlocking instead. + pub fn unlock(&self) -> Result<()> { + let status = unsafe { ffi::AHardwareBuffer_unlock(self.as_ptr(), std::ptr::null_mut()) }; + status_to_io_result(status) + } + + /// Unlock the [`HardwareBuffer`] from direct CPU access. + /// + /// Returns a fence file descriptor that will become signaled when unlocking is completed, or + /// [`None`] if unlocking is already finished. The caller is responsible for closing the file + /// descriptor once it's no longer needed. See [`unlock()`][Self::unlock()] for a variant that + /// blocks instead. + pub fn unlock_async(&self) -> Result> { + let fence = construct(|res| unsafe { ffi::AHardwareBuffer_unlock(self.as_ptr(), res) })?; + Ok(match fence { + -1 => None, + fence => Some(unsafe { OwnedFd::from_raw_fd(fence) }), + }) + } + + /// Receive a [`HardwareBuffer`] from an `AF_UNIX` socket. + /// + /// `AF_UNIX` sockets are wrapped by [`std::os::unix::net::UnixListener`] and + /// [`std::os::unix::net::UnixStream`] in Rust and have a corresponding + /// [`std::os::unix::io::AsFd::as_fd()`] implementation. + pub fn recv_handle_from_unix_socket(socket_fd: BorrowedFd<'_>) -> Result { + unsafe { + let ptr = construct(|res| { + ffi::AHardwareBuffer_recvHandleFromUnixSocket(socket_fd.as_raw_fd(), res) + })?; + + Ok(Self::from_ptr(NonNull::new_unchecked(ptr))) + } + } + + /// Send the [`HardwareBuffer`] to an `AF_UNIX` socket. + /// + /// `AF_UNIX` sockets are wrapped by [`std::os::unix::net::UnixListener`] and + /// [`std::os::unix::net::UnixStream`] in Rust and have a corresponding + /// [`std::os::unix::io::AsFd::as_fd()`] implementation. + pub fn send_handle_to_unix_socket(&self, socket_fd: BorrowedFd<'_>) -> Result<()> { + let status = unsafe { + ffi::AHardwareBuffer_sendHandleToUnixSocket(self.as_ptr(), socket_fd.as_raw_fd()) + }; + status_to_io_result(status) + } + + /// Acquire a reference on the given [`HardwareBuffer`] object. + /// + /// This prevents the object from being deleted until the last strong reference, represented + /// by [`HardwareBufferRef`], is [`drop()`]ped. + pub fn acquire(&self) -> HardwareBufferRef { + unsafe { + ffi::AHardwareBuffer_acquire(self.as_ptr()); + HardwareBufferRef::from_ptr(self.inner) + } + } +} + +/// A [`HardwareBuffer`] with an owned reference, that is released when dropped. +/// It behaves much like a strong [`std::rc::Rc`] reference. +#[derive(Debug)] +pub struct HardwareBufferRef { + inner: HardwareBuffer, +} + +impl HardwareBufferRef { + /// Create an _owned_ [`HardwareBuffer`] from a native pointer + /// + /// To wrap a weak reference (that is **not** `release`d on [`Drop`]), call + /// [`HardwareBuffer::from_ptr()`] instead. + /// + /// # Safety + /// By calling this function, you assert that it is a valid pointer to an NDK + /// [`ffi::AHardwareBuffer`]. + pub unsafe fn from_ptr(ptr: NonNull) -> Self { + Self { + inner: HardwareBuffer { inner: ptr }, + } + } +} + +impl Deref for HardwareBufferRef { + type Target = HardwareBuffer; + + fn deref(&self) -> &Self::Target { + &self.inner + } +} + +impl Drop for HardwareBufferRef { + fn drop(&mut self) { + unsafe { ffi::AHardwareBuffer_release(self.inner.as_ptr()) } + } +} + +impl Clone for HardwareBufferRef { + fn clone(&self) -> Self { + self.acquire() + } +} + +/// Buffer description. +/// +/// Used for allocating new buffers and querying parameters of existing ones. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct HardwareBufferDesc { + pub width: u32, + pub height: u32, + pub layers: u32, + pub format: HardwareBufferFormat, + pub usage: HardwareBufferUsage, + pub stride: u32, +} + +impl HardwareBufferDesc { + fn into_native(self) -> ffi::AHardwareBuffer_Desc { + ffi::AHardwareBuffer_Desc { + width: self.width, + height: self.height, + layers: self.layers, + format: i32::from(self.format) + .try_into() + .expect("i32->u32 overflow in HardwareBufferDesc::into_native()"), + usage: self.usage.bits(), + stride: self.stride, + rfu0: 0, + rfu1: 0, + } + } +} + +/// A native [`AHardwareBuffer_Plane`] +/// +/// Contains the same fields as [`ffi::AHardwareBuffer_Plane`]. +/// +/// [`AHardwareBuffer_Plane`]: https://developer.android.com/ndk/reference/group/a-hardware-buffer#ahardwarebuffer_plane +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct LockedPlaneInfo { + pub virtual_address: *mut c_void, + pub bytes_per_pixel: u32, + pub bytes_per_stride: u32, +} + +/// Iterator over [`ffi::AHardwareBuffer_Planes`], containing a list of [`LockedPlaneInfo`]. +#[derive(Debug)] +pub struct HardwareBufferPlanes { + inner: ffi::AHardwareBuffer_Planes, + index: u32, +} + +impl Iterator for HardwareBufferPlanes { + type Item = LockedPlaneInfo; + + fn next(&mut self) -> Option { + if self.index == self.inner.planeCount { + None + } else { + let plane = self.inner.planes[self.index as usize]; + self.index += 1; + Some(LockedPlaneInfo { + virtual_address: plane.data, + bytes_per_pixel: plane.pixelStride, + bytes_per_stride: plane.rowStride, + }) + } + } +} diff --git a/native/vendor/ndk-0.9.0/src/hardware_buffer_format.rs b/native/vendor/ndk-0.9.0/src/hardware_buffer_format.rs new file mode 100644 index 0000000..555669f --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/hardware_buffer_format.rs @@ -0,0 +1,106 @@ +//! Bindings for [`AHardwareBuffer_Format`] +//! +//! [`AHardwareBuffer_Format`]: https://developer.android.com/ndk/reference/group/a-hardware-buffer#ahardwarebuffer_format + +use num_enum::{FromPrimitive, IntoPrimitive}; + +/// Buffer pixel formats. +#[repr(i32)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[allow(non_camel_case_types)] +#[non_exhaustive] +pub enum HardwareBufferFormat { + /// Matches deprecated [`ffi::ANativeWindow_LegacyFormat::WINDOW_FORMAT_RGBA_8888`].0. + #[doc(alias = "AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM")] + R8G8B8A8_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM.0 as i32, + /// Matches deprecated [`ffi::ANativeWindow_LegacyFormat::WINDOW_FORMAT_RGBX_8888`].0. + #[doc(alias = "AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM")] + R8G8B8X8_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM.0 as i32, + #[cfg(feature = "api-level-26")] + #[doc(alias = "AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM")] + R8G8B8_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM.0 as i32, + /// Matches deprecated [`ffi::ANativeWindow_LegacyFormat::WINDOW_FORMAT_RGB_565`].0. + #[doc(alias = "AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM")] + R5G6B5_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM.0 as i32, + #[cfg(feature = "api-level-26")] + #[doc(alias = "AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT")] + R16G16B16A16_FLOAT = + ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT.0 as i32, + #[cfg(feature = "api-level-26")] + #[doc(alias = "AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM")] + R10G10B10A2_UNORM = + ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM.0 as i32, + #[cfg(feature = "api-level-26")] + #[doc(alias = "AHARDWAREBUFFER_FORMAT_BLOB")] + BLOB = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_BLOB.0 as i32, + #[cfg(feature = "api-level-26")] + #[doc(alias = "AHARDWAREBUFFER_FORMAT_D16_UNORM")] + D16_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D16_UNORM.0 as i32, + #[cfg(feature = "api-level-26")] + #[doc(alias = "AHARDWAREBUFFER_FORMAT_D24_UNORM")] + D24_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D24_UNORM.0 as i32, + #[cfg(feature = "api-level-26")] + #[doc(alias = "AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT")] + D24_UNORM_S8_UINT = + ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT.0 as i32, + #[cfg(feature = "api-level-26")] + #[doc(alias = "AHARDWAREBUFFER_FORMAT_D32_FLOAT")] + D32_FLOAT = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D32_FLOAT.0 as i32, + #[cfg(feature = "api-level-26")] + #[doc(alias = "AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT")] + D32_FLOAT_S8_UINT = + ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT.0 as i32, + #[cfg(feature = "api-level-26")] + #[doc(alias = "AHARDWAREBUFFER_FORMAT_S8_UINT")] + S8_UINT = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_S8_UINT.0 as i32, + #[cfg(feature = "api-level-26")] + #[doc(alias = "AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420")] + Y8Cb8Cr8_420 = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420.0 as i32, + #[cfg(feature = "api-level-26")] + #[doc(alias = "AHARDWAREBUFFER_FORMAT_YCbCr_P010")] + YCbCr_P010 = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_YCbCr_P010.0 as i32, + #[cfg(feature = "api-level-26")] + R8_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8_UNORM.0 as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +impl HardwareBufferFormat { + /// Returns [`None`] when there is no immediate byte size available for this format, for + /// example on planar buffer formats. + pub fn bytes_per_pixel(self) -> Option { + Some(match self { + Self::R8G8B8A8_UNORM | Self::R8G8B8X8_UNORM => 4, + #[cfg(feature = "api-level-26")] + Self::R8G8B8_UNORM => 3, + Self::R5G6B5_UNORM => 2, + #[cfg(feature = "api-level-26")] + Self::R16G16B16A16_FLOAT => 8, + #[cfg(feature = "api-level-26")] + Self::R10G10B10A2_UNORM => 4, + #[cfg(feature = "api-level-26")] + Self::BLOB => 1, + #[cfg(feature = "api-level-26")] + Self::D16_UNORM => 2, + #[cfg(feature = "api-level-26")] + Self::D24_UNORM => 3, + #[cfg(feature = "api-level-26")] + Self::D24_UNORM_S8_UINT => 4, + #[cfg(feature = "api-level-26")] + Self::D32_FLOAT => 4, + #[cfg(feature = "api-level-26")] + Self::D32_FLOAT_S8_UINT => 5, + #[cfg(feature = "api-level-26")] + Self::S8_UINT => 1, + #[cfg(feature = "api-level-26")] + Self::Y8Cb8Cr8_420 => 3, + #[cfg(feature = "api-level-26")] + Self::YCbCr_P010 => return None, + #[cfg(feature = "api-level-26")] + Self::R8_UNORM => 1, + Self::__Unknown(_) => return None, + }) + } +} diff --git a/native/vendor/ndk-0.9.0/src/input_queue.rs b/native/vendor/ndk-0.9.0/src/input_queue.rs new file mode 100644 index 0000000..0fae5f1 --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/input_queue.rs @@ -0,0 +1,143 @@ +//! Bindings for [`AInputQueue`] +//! +//! [`AInputQueue`]: https://developer.android.com/ndk/reference/group/input#ainputqueue + +use std::io::Result; +use std::os::raw::c_int; +use std::ptr::{self, NonNull}; + +#[cfg(feature = "api-level-33")] +use jni_sys::{jobject, JNIEnv}; + +use crate::event::InputEvent; +#[cfg(doc)] +use crate::event::KeyEvent; +use crate::looper::ForeignLooper; +use crate::utils::status_to_io_result; + +/// A native [`AInputQueue *`] +/// +/// An input queue is the facility through which you retrieve input events. +/// +/// [`AInputQueue *`]: https://developer.android.com/ndk/reference/group/input#ainputqueue +#[derive(Debug)] +pub struct InputQueue { + ptr: NonNull, +} + +// It gets shared between threads in `ndk-glue` +unsafe impl Send for InputQueue {} +unsafe impl Sync for InputQueue {} + +impl InputQueue { + /// Construct an [`InputQueue`] from the native pointer. + /// + /// # Safety + /// By calling this function, you assert that the pointer is a valid pointer to an NDK [`ffi::AInputQueue`]. + pub unsafe fn from_ptr(ptr: NonNull) -> Self { + Self { ptr } + } + + /// Returns the [`InputQueue`] object associated with the supplied + /// [Java `InputQueue`][`android.view.InputQueue`] object. + /// + /// # Safety + /// + /// This function should be called with a healthy JVM pointer and with a non-null + /// [`android.view.InputQueue`], which must be kept alive on the Java/Kotlin side. + /// + /// The returned native object holds a weak reference to the Java object, and is only valid as + /// long as the Java object has not yet been disposed. You should ensure that there is a strong + /// reference to the Java object and that it has not been disposed before using the returned + /// object. + /// + /// [`android.view.InputQueue`]: https://developer.android.com/reference/android/view/InputQueue + #[cfg(feature = "api-level-33")] + #[doc(alias = "AInputQueue_fromJava")] + pub unsafe fn from_java(env: *mut JNIEnv, input_queue: jobject) -> Option { + let ptr = unsafe { ffi::AInputQueue_fromJava(env, input_queue) }; + Some(Self::from_ptr(NonNull::new(ptr)?)) + } + + pub fn ptr(&self) -> NonNull { + self.ptr + } + + /// Returns the next available [`InputEvent`] from the queue. + /// + /// Returns [`None`] if no event is available. + #[doc(alias = "AInputQueue_getEvent")] + pub fn event(&self) -> Result> { + let mut out_event = ptr::null_mut(); + let status = unsafe { ffi::AInputQueue_getEvent(self.ptr.as_ptr(), &mut out_event) }; + match status_to_io_result(status) { + Ok(()) => { + debug_assert!(!out_event.is_null()); + Ok(Some(unsafe { + InputEvent::from_ptr(NonNull::new_unchecked(out_event)) + })) + } + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => Ok(None), + Err(e) => Err(e), + } + } + + /// Returns [`true`] if there are one or more events available in the input queue. + #[doc(alias = "AInputQueue_hasEvents")] + pub fn has_events(&self) -> bool { + match unsafe { ffi::AInputQueue_hasEvents(self.ptr.as_ptr()) } { + 0 => false, + 1 => true, + r => unreachable!("AInputQueue_hasEvents returned non-boolean {}", r), + } + } + + /// Sends the key for standard pre-dispatching that is, possibly deliver it to the current IME + /// to be consumed before the app. + /// + /// Returns [`Some`] if it was not pre-dispatched, meaning you can process it right now. If + /// [`None`] is returned, you must abandon the current event processing and allow the event to + /// appear again in the event queue (if it does not get consumed during pre-dispatching). + /// + /// Also returns [`None`] if `event` is not a [`KeyEvent`]. + #[doc(alias = "AInputQueue_preDispatchEvent")] + pub fn pre_dispatch(&self, event: InputEvent) -> Option { + match unsafe { ffi::AInputQueue_preDispatchEvent(self.ptr.as_ptr(), event.ptr().as_ptr()) } + { + 0 => Some(event), + _ => None, + } + } + + /// Report that dispatching has finished with the given [`InputEvent`]. + /// + /// This must be called after receiving an event with [`InputQueue::event()`]. + #[doc(alias = "AInputQueue_finishEvent")] + pub fn finish_event(&self, event: InputEvent, handled: bool) { + unsafe { + ffi::AInputQueue_finishEvent(self.ptr.as_ptr(), event.ptr().as_ptr(), handled as c_int) + } + } + + /// Add this input queue to a [`ForeignLooper`] for processing. + /// + /// See [`ForeignLooper::add_fd()`] for information on the `ident`, `callback`, and `data` params. + #[doc(alias = "AInputQueue_attachLooper")] + pub fn attach_looper(&self, looper: &ForeignLooper, id: i32) { + unsafe { + ffi::AInputQueue_attachLooper( + self.ptr.as_ptr(), + looper.ptr().as_ptr(), + id, + None, + std::ptr::null_mut(), + ) + } + } + + /// Remove this input queue from the [`ForeignLooper`] it is currently attached to. + #[doc(alias = "AInputQueue_detachLooper")] + pub fn detach_looper(&self) { + unsafe { ffi::AInputQueue_detachLooper(self.ptr.as_ptr()) } + } +} diff --git a/native/vendor/ndk-0.9.0/src/lib.rs b/native/vendor/ndk-0.9.0/src/lib.rs new file mode 100644 index 0000000..81e2ef6 --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/lib.rs @@ -0,0 +1,33 @@ +//! # Android NDK +//! +//! Bindings to the [Android NDK]. +//! +//! [Android NDK]: https://developer.android.com/ndk/reference +#![warn( + missing_debug_implementations, + rust_2018_idioms, + trivial_casts, + unused_qualifications +)] +#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] + +pub mod asset; +pub mod audio; +pub mod bitmap; +pub mod configuration; +pub mod data_space; +pub mod event; +pub mod font; +pub mod hardware_buffer; +pub mod hardware_buffer_format; +pub mod input_queue; +pub mod looper; +pub mod media; +pub mod media_error; +pub mod native_activity; +pub mod native_window; +pub mod shared_memory; +pub mod surface_texture; +pub mod sync; +pub mod trace; +mod utils; diff --git a/native/vendor/ndk-0.9.0/src/looper.rs b/native/vendor/ndk-0.9.0/src/looper.rs new file mode 100644 index 0000000..d8eb644 --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/looper.rs @@ -0,0 +1,459 @@ +//! Bindings for [`ALooper`] +//! +//! In Android, [`ALooper`]s are inherently thread-local. Due to this, there are two different +//! [`ALooper`] interfaces exposed in this module: +//! +//! * [`ThreadLooper`], which has methods for the operations performable with a looper in one's own +//! thread; and +//! * [`ForeignLooper`], which has methods for the operations performable with any thread's looper. +//! +//! [`ALooper`]: https://developer.android.com/ndk/reference/group/looper#alooper + +use std::mem::ManuallyDrop; +use std::os::{ + fd::{AsRawFd, BorrowedFd, RawFd}, + raw::c_void, +}; +use std::ptr; +use std::time::Duration; +use thiserror::Error; + +use crate::utils::abort_on_panic; + +/// A thread-local native [`ALooper *`]. This promises that there is a looper associated with the +/// current thread. +/// +/// [`ALooper *`]: https://developer.android.com/ndk/reference/group/looper#alooper +#[derive(Debug)] +pub struct ThreadLooper { + _marker: std::marker::PhantomData<*mut ()>, // Not send or sync + foreign: ForeignLooper, +} + +bitflags::bitflags! { + /// Flags for file descriptor events that a looper can monitor. + /// + /// These flag bits can be combined to monitor multiple events at once. + #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] + pub struct FdEvent : u32 { + /// The file descriptor is available for read operations. + #[doc(alias = "ALOOPER_EVENT_INPUT")] + const INPUT = ffi::ALOOPER_EVENT_INPUT; + /// The file descriptor is available for write operations. + #[doc(alias = "ALOOPER_EVENT_OUTPUT")] + const OUTPUT = ffi::ALOOPER_EVENT_OUTPUT; + /// The file descriptor has encountered an error condition. + /// + /// The looper always sends notifications about errors; it is not necessary to specify this + /// event flag in the requested event set. + #[doc(alias = "ALOOPER_EVENT_ERROR")] + const ERROR = ffi::ALOOPER_EVENT_ERROR; + /// The file descriptor was hung up. + /// + /// For example, indicates that the remote end of a pipe or socket was closed. + /// + /// The looper always sends notifications about hangups; it is not necessary to specify this + /// event flag in the requested event set. + #[doc(alias = "ALOOPER_EVENT_HANGUP")] + const HANGUP = ffi::ALOOPER_EVENT_HANGUP; + /// The file descriptor is invalid. + /// + /// For example, the file descriptor was closed prematurely. + /// + /// The looper always sends notifications about invalid file descriptors; it is not + /// necessary to specify this event flag in the requested event set. + #[doc(alias = "ALOOPER_EVENT_INVALID")] + const INVALID = ffi::ALOOPER_EVENT_INVALID; + + // https://docs.rs/bitflags/latest/bitflags/#externally-defined-flags + const _ = !0; + } +} + +/// The poll result from a [`ThreadLooper`]. +#[derive(Debug)] +pub enum Poll<'fd> { + /// This looper was woken using [`ForeignLooper::wake()`] + Wake, + /// For [`ThreadLooper::poll_once*()`][ThreadLooper::poll_once()], an event was received and processed using a callback. + Callback, + /// For [`ThreadLooper::poll_*_timeout()`][ThreadLooper::poll_once_timeout()], the requested timeout was reached before any events. + Timeout, + /// An event was received + Event { + ident: i32, + /// # Safety + /// The caller should guarantee that this file descriptor remains open after it was added + /// via [`ForeignLooper::add_fd()`] or [`ForeignLooper::add_fd_with_callback()`]. + fd: BorrowedFd<'fd>, + events: FdEvent, + data: *mut c_void, + }, +} + +#[derive(Debug, Copy, Clone, Error)] +#[error("Android Looper error")] +pub struct LooperError; + +impl ThreadLooper { + /// Prepares a looper for the current thread and returns it + pub fn prepare() -> Self { + unsafe { + let ptr = ffi::ALooper_prepare(ffi::ALOOPER_PREPARE_ALLOW_NON_CALLBACKS as _); + let foreign = ForeignLooper::from_ptr(ptr::NonNull::new(ptr).expect("looper non null")); + Self { + _marker: std::marker::PhantomData, + foreign, + } + } + } + + /// Returns the looper associated with the current thread, if any. + pub fn for_thread() -> Option { + Some(Self { + _marker: std::marker::PhantomData, + foreign: ForeignLooper::for_thread()?, + }) + } + + /// Polls the looper, blocking on processing an event, but with a timeout in milliseconds. + /// Give a timeout of `0` to make this non-blocking. + fn poll_once_ms(&self, ms: i32) -> Result, LooperError> { + let mut fd = -1; + let mut events = -1; + let mut data: *mut c_void = ptr::null_mut(); + match unsafe { ffi::ALooper_pollOnce(ms, &mut fd, &mut events, &mut data) } { + ffi::ALOOPER_POLL_WAKE => Ok(Poll::Wake), + ffi::ALOOPER_POLL_CALLBACK => Ok(Poll::Callback), + ffi::ALOOPER_POLL_TIMEOUT => Ok(Poll::Timeout), + ffi::ALOOPER_POLL_ERROR => Err(LooperError), + ident if ident >= 0 => Ok(Poll::Event { + ident, + // SAFETY: Even though this FD at least shouldn't outlive self, a user could have + // closed it after calling add_fd or add_fd_with_callback. + fd: unsafe { BorrowedFd::borrow_raw(fd) }, + events: FdEvent::from_bits(events as u32) + .expect("poll event contains unknown bits"), + data, + }), + _ => unreachable!(), + } + } + + /// Polls the looper, blocking on processing an event. + #[inline] + pub fn poll_once(&self) -> Result, LooperError> { + self.poll_once_ms(-1) + } + + /// Polls the looper, blocking on processing an event, but with a timeout. Give a timeout of + /// [`Duration::ZERO`] to make this non-blocking. + /// + /// It panics if the timeout is larger than expressible as an [`i32`] of milliseconds (roughly 25 + /// days). + #[inline] + pub fn poll_once_timeout(&self, timeout: Duration) -> Result, LooperError> { + self.poll_once_ms( + timeout + .as_millis() + .try_into() + .expect("Supplied timeout is too large"), + ) + } + + /// Repeatedly polls the looper, blocking on processing an event, but with a timeout in + /// milliseconds. Give a timeout of `0` to make this non-blocking. + /// + /// This function will never return [`Poll::Callback`]. + fn poll_all_ms(&self, ms: i32) -> Result, LooperError> { + let mut fd = -1; + let mut events = -1; + let mut data: *mut c_void = ptr::null_mut(); + match unsafe { ffi::ALooper_pollAll(ms, &mut fd, &mut events, &mut data) } { + ffi::ALOOPER_POLL_WAKE => Ok(Poll::Wake), + ffi::ALOOPER_POLL_TIMEOUT => Ok(Poll::Timeout), + ffi::ALOOPER_POLL_ERROR => Err(LooperError), + ident if ident >= 0 => Ok(Poll::Event { + ident, + // SAFETY: Even though this FD at least shouldn't outlive self, a user could have + // closed it after calling add_fd or add_fd_with_callback. + fd: unsafe { BorrowedFd::borrow_raw(fd) }, + events: FdEvent::from_bits(events as u32) + .expect("poll event contains unknown bits"), + data, + }), + _ => unreachable!(), + } + } + + /// Repeatedly polls the looper, blocking on processing an event. + /// + /// This function will never return [`Poll::Callback`]. + #[inline] + pub fn poll_all(&self) -> Result, LooperError> { + self.poll_all_ms(-1) + } + + /// Repeatedly polls the looper, blocking on processing an event, but with a timeout. Give a + /// timeout of [`Duration::ZERO`] to make this non-blocking. + /// + /// This function will never return [`Poll::Callback`]. + /// + /// It panics if the timeout is larger than expressible as an [`i32`] of milliseconds (roughly 25 + /// days). + #[inline] + pub fn poll_all_timeout(&self, timeout: Duration) -> Result, LooperError> { + self.poll_all_ms( + timeout + .as_millis() + .try_into() + .expect("Supplied timeout is too large"), + ) + } + + /// Adds a file descriptor to be polled, with a callback that is invoked when any of the + /// [`FdEvent`]s described in `events` is triggered. + /// + /// The callback receives the file descriptor it is associated with and a bitmask of the poll + /// events that were triggered (typically [`FdEvent::INPUT`]). It should return [`true`] to + /// continue receiving callbacks, or [`false`] to have the callback unregistered. + /// + /// See also [the NDK + /// docs](https://developer.android.com/ndk/reference/group/looper.html#alooper_addfd). + /// + /// Note that this will leak a [`Box`] unless the callback returns [`false`] to unregister + /// itself. + /// + /// # Threading + /// This function will be called on the current thread when this [`ThreadLooper`] is + /// polled. A callback can also be registered from other threads via the equivalent + /// [`ForeignLooper::add_fd_with_callback()`] function, which requires a [`Send`] bound. + /// + /// # Safety + /// The caller should guarantee that this file descriptor stays open until it is removed via + /// [`remove_fd()`][ForeignLooper::remove_fd()] or by returning [`false`] from the callback, + /// and for however long the caller wishes to use this file descriptor inside and after the + /// callback. + #[doc(alias = "ALooper_addFd")] + pub fn add_fd_with_callback, FdEvent) -> bool>( + &self, + fd: BorrowedFd<'_>, + events: FdEvent, + callback: F, + ) -> Result<(), LooperError> { + unsafe { + self.foreign + .add_fd_with_callback_assume_send(fd, events, callback) + } + } + + /// Returns a reference to the [`ForeignLooper`] that is associated with the current thread. + pub fn as_foreign(&self) -> &ForeignLooper { + &self.foreign + } + + pub fn into_foreign(self) -> ForeignLooper { + self.foreign + } +} + +/// A native [`ALooper *`], not necessarily allocated with the current thread. +/// +/// [`ALooper *`]: https://developer.android.com/ndk/reference/group/looper#alooper +#[derive(Debug)] +pub struct ForeignLooper { + ptr: ptr::NonNull, +} + +unsafe impl Send for ForeignLooper {} +unsafe impl Sync for ForeignLooper {} + +impl Drop for ForeignLooper { + fn drop(&mut self) { + unsafe { ffi::ALooper_release(self.ptr.as_ptr()) } + } +} + +impl Clone for ForeignLooper { + fn clone(&self) -> Self { + unsafe { + ffi::ALooper_acquire(self.ptr.as_ptr()); + Self { ptr: self.ptr } + } + } +} + +impl ForeignLooper { + /// Returns the looper associated with the current thread, if any. + #[inline] + pub fn for_thread() -> Option { + ptr::NonNull::new(unsafe { ffi::ALooper_forThread() }) + .map(|ptr| unsafe { Self::from_ptr(ptr) }) + } + + /// Construct a [`ForeignLooper`] object from the given pointer. + /// + /// # Safety + /// By calling this function, you guarantee that the pointer is a valid, non-null pointer to an + /// NDK [`ffi::ALooper`]. + #[inline] + pub unsafe fn from_ptr(ptr: ptr::NonNull) -> Self { + ffi::ALooper_acquire(ptr.as_ptr()); + Self { ptr } + } + + /// Returns a pointer to the NDK `ALooper` object. + #[inline] + pub fn ptr(&self) -> ptr::NonNull { + self.ptr + } + + /// Wakes the looper. An event of [`Poll::Wake`] will be sent. + pub fn wake(&self) { + unsafe { ffi::ALooper_wake(self.ptr.as_ptr()) } + } + + /// Adds a file descriptor to be polled, without a callback. + /// + /// See also [the NDK + /// docs](https://developer.android.com/ndk/reference/group/looper.html#alooper_addfd). + /// + /// # Safety + /// The caller should guarantee that this file descriptor stays open until it is removed via + /// [`remove_fd()`][Self::remove_fd()], and for however long the caller wishes to use this file + /// descriptor when it is returned in [`Poll::Event::fd`]. + + // `ALooper_addFd` won't dereference `data`; it will only pass it on to the event. + // Optionally dereferencing it there already enforces `unsafe` context. + #[allow(clippy::not_unsafe_ptr_arg_deref)] + pub fn add_fd( + &self, + fd: BorrowedFd<'_>, + ident: i32, + events: FdEvent, + data: *mut c_void, + ) -> Result<(), LooperError> { + match unsafe { + ffi::ALooper_addFd( + self.ptr.as_ptr(), + fd.as_raw_fd(), + ident, + events.bits() as i32, + None, + data, + ) + } { + 1 => Ok(()), + -1 => Err(LooperError), + _ => unreachable!(), + } + } + + /// Adds a file descriptor to be polled, with a callback that is invoked when any of the + /// [`FdEvent`]s described in `events` is triggered. + /// + /// The callback receives the file descriptor it is associated with and a bitmask of the poll + /// events that were triggered (typically [`FdEvent::INPUT`]). It should return [`true`] to + /// continue receiving callbacks, or [`false`] to have the callback unregistered. + /// + /// See also [the NDK + /// docs](https://developer.android.com/ndk/reference/group/looper.html#alooper_addfd). + /// + /// Note that this will leak a [`Box`] unless the callback returns [`false`] to unregister + /// itself. + /// + /// # Threading + /// This function will be called on the looper thread where and when it is polled. + /// For registering callbacks without [`Send`] requirement, call the equivalent + /// [`ThreadLooper::add_fd_with_callback()`] function on the Looper thread. + /// + /// # Safety + /// The caller should guarantee that this file descriptor stays open until it is removed via + /// [`remove_fd()`][Self::remove_fd()] or by returning [`false`] from the callback, and for + /// however long the caller wishes to use this file descriptor inside and after the callback. + #[doc(alias = "ALooper_addFd")] + pub fn add_fd_with_callback, FdEvent) -> bool + Send>( + &self, + fd: BorrowedFd<'_>, + events: FdEvent, + callback: F, + ) -> Result<(), LooperError> { + unsafe { self.add_fd_with_callback_assume_send(fd, events, callback) } + } + + /// Private helper to deduplicate/commonize the implementation behind + /// [`ForeignLooper::add_fd_with_callback()`] and [`ThreadLooper::add_fd_with_callback()`], + /// as both have their own way of guaranteeing thread-safety. The former, [`ForeignLooper`], + /// requires the closure to be [`Send`]. The latter, [`ThreadLooper`], can only exist on the + /// thread where polling happens and where the closure will end up being invoked, and does not + /// require [`Send`]. + /// + /// # Safety + /// The caller must guarantee that `F` is [`Send`] or that `F` will only run on the current + /// thread. See the explanation above about why this function exists. + unsafe fn add_fd_with_callback_assume_send, FdEvent) -> bool>( + &self, + fd: BorrowedFd<'_>, + events: FdEvent, + callback: F, + ) -> Result<(), LooperError> { + extern "C" fn cb_handler, FdEvent) -> bool>( + fd: RawFd, + events: i32, + data: *mut c_void, + ) -> i32 { + abort_on_panic(|| unsafe { + let mut cb = ManuallyDrop::new(Box::::from_raw(data as *mut _)); + let events = FdEvent::from_bits_retain( + events.try_into().expect("Unexpected sign bit in `events`"), + ); + let keep_registered = cb(BorrowedFd::borrow_raw(fd), events); + if !keep_registered { + ManuallyDrop::into_inner(cb); + } + keep_registered as i32 + }) + } + let data = Box::into_raw(Box::new(callback)) as *mut _; + match unsafe { + ffi::ALooper_addFd( + self.ptr.as_ptr(), + fd.as_raw_fd(), + ffi::ALOOPER_POLL_CALLBACK, + events.bits() as i32, + Some(cb_handler::), + data, + ) + } { + 1 => Ok(()), + -1 => Err(LooperError), + _ => unreachable!(), + } + } + + /// Removes a previously added file descriptor from the looper. + /// + /// Returns [`true`] if the file descriptor was removed, [`false`] if it was not previously + /// registered. + /// + /// # Safety + /// When this method returns, it is safe to close the file descriptor since the looper will no + /// longer have a reference to it. However, it is possible for the callback to already be + /// running or for it to run one last time if the file descriptor was already signalled. + /// Calling code is responsible for ensuring that this case is safely handled. For example, if + /// the callback takes care of removing itself during its own execution either by returning `0` + /// or by calling this method, then it can be guaranteed to not be invoked again at any later + /// time unless registered anew. + /// + /// Note that unregistering a file descriptor with callback will leak a [`Box`] created in + /// [`add_fd_with_callback()`][Self::add_fd_with_callback()]. Consider returning [`false`] + /// from the callback instead to drop it. + pub fn remove_fd(&self, fd: BorrowedFd<'_>) -> Result { + match unsafe { ffi::ALooper_removeFd(self.ptr.as_ptr(), fd.as_raw_fd()) } { + 1 => Ok(true), + 0 => Ok(false), + -1 => Err(LooperError), + _ => unreachable!(), + } + } +} diff --git a/native/vendor/ndk-0.9.0/src/media/image_reader.rs b/native/vendor/ndk-0.9.0/src/media/image_reader.rs new file mode 100644 index 0000000..d414377 --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/media/image_reader.rs @@ -0,0 +1,459 @@ +//! Bindings for [`AImageReader`] and [`AImage`] +//! +//! [`AImageReader`]: https://developer.android.com/ndk/reference/group/media#aimagereader +//! [`AImage`]: https://developer.android.com/ndk/reference/group/media#aimage +#![cfg(feature = "api-level-24")] + +use crate::media_error::{construct, construct_never_null, MediaError, Result}; +use crate::native_window::NativeWindow; +use crate::utils::abort_on_panic; +use num_enum::{FromPrimitive, IntoPrimitive}; +use std::{ + ffi::c_void, + fmt::{self, Debug, Formatter}, + mem::MaybeUninit, + ptr::NonNull, +}; + +#[cfg(feature = "api-level-26")] +use std::os::fd::{FromRawFd, IntoRawFd, OwnedFd}; + +#[cfg(feature = "api-level-26")] +use crate::hardware_buffer::{HardwareBuffer, HardwareBufferUsage}; + +#[repr(i32)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[allow(non_camel_case_types)] +#[non_exhaustive] +pub enum ImageFormat { + RGBA_8888 = ffi::AIMAGE_FORMATS::AIMAGE_FORMAT_RGBA_8888.0 as i32, + RGBX_8888 = ffi::AIMAGE_FORMATS::AIMAGE_FORMAT_RGBX_8888.0 as i32, + RGB_888 = ffi::AIMAGE_FORMATS::AIMAGE_FORMAT_RGB_888.0 as i32, + RGB_565 = ffi::AIMAGE_FORMATS::AIMAGE_FORMAT_RGB_565.0 as i32, + RGBA_FP16 = ffi::AIMAGE_FORMATS::AIMAGE_FORMAT_RGBA_FP16.0 as i32, + YUV_420_888 = ffi::AIMAGE_FORMATS::AIMAGE_FORMAT_YUV_420_888.0 as i32, + JPEG = ffi::AIMAGE_FORMATS::AIMAGE_FORMAT_JPEG.0 as i32, + RAW16 = ffi::AIMAGE_FORMATS::AIMAGE_FORMAT_RAW16.0 as i32, + RAW_PRIVATE = ffi::AIMAGE_FORMATS::AIMAGE_FORMAT_RAW_PRIVATE.0 as i32, + RAW10 = ffi::AIMAGE_FORMATS::AIMAGE_FORMAT_RAW10.0 as i32, + RAW12 = ffi::AIMAGE_FORMATS::AIMAGE_FORMAT_RAW12.0 as i32, + DEPTH16 = ffi::AIMAGE_FORMATS::AIMAGE_FORMAT_DEPTH16.0 as i32, + DEPTH_POINT_CLOUD = ffi::AIMAGE_FORMATS::AIMAGE_FORMAT_DEPTH_POINT_CLOUD.0 as i32, + PRIVATE = ffi::AIMAGE_FORMATS::AIMAGE_FORMAT_PRIVATE.0 as i32, + Y8 = ffi::AIMAGE_FORMATS::AIMAGE_FORMAT_Y8.0 as i32, + HEIC = ffi::AIMAGE_FORMATS::AIMAGE_FORMAT_HEIC.0 as i32, + DEPTH_JPEG = ffi::AIMAGE_FORMATS::AIMAGE_FORMAT_DEPTH_JPEG.0 as i32, + + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32), +} + +pub type ImageListener = Box; + +#[cfg(feature = "api-level-26")] +pub type BufferRemovedListener = Box; + +/// Result returned by: +/// - [`ImageReader::acquire_next_image()`]` +/// - [`ImageReader::acquire_next_image_async()`]` +/// - [`ImageReader::acquire_latest_image()`]` +/// - [`ImageReader::acquire_latest_image_async()`]` +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum AcquireResult { + /// Returned if there is no buffers currently available in the reader queue. + #[doc(alias = "AMEDIA_IMGREADER_NO_BUFFER_AVAILABLE")] + NoBufferAvailable, + /// Returned if the number of concurrently acquired images has reached the limit. + #[doc(alias = "AMEDIA_IMGREADER_MAX_IMAGES_ACQUIRED")] + MaxImagesAcquired, + + /// Returned if an [`Image`] (optionally with fence) was successfully acquired. + Image(T), +} + +impl AcquireResult { + fn map(self, f: impl FnOnce(T) -> U) -> AcquireResult { + match self { + AcquireResult::Image(img) => AcquireResult::Image(f(img)), + AcquireResult::NoBufferAvailable => AcquireResult::NoBufferAvailable, + AcquireResult::MaxImagesAcquired => AcquireResult::MaxImagesAcquired, + } + } +} + +impl AcquireResult { + /// Inlined version of [`construct_never_null()`] with IMGREADER-specific result mapping. + fn construct_never_null( + with_ptr: impl FnOnce(*mut *mut ffi::AImage) -> ffi::media_status_t, + ) -> Result { + let mut result = MaybeUninit::uninit(); + let status = with_ptr(result.as_mut_ptr()); + match status { + ffi::media_status_t::AMEDIA_IMGREADER_NO_BUFFER_AVAILABLE => { + Ok(Self::NoBufferAvailable) + } + ffi::media_status_t::AMEDIA_IMGREADER_MAX_IMAGES_ACQUIRED => { + Ok(Self::MaxImagesAcquired) + } + status => MediaError::from_status(status).map(|()| { + let result = unsafe { result.assume_init() }; + Self::Image(Image { + inner: if cfg!(debug_assertions) { + NonNull::new(result).expect("result should never be null") + } else { + unsafe { NonNull::new_unchecked(result) } + }, + }) + }), + } + } +} + +/// A native [`AImageReader *`] +/// +/// [`AImageReader *`]: https://developer.android.com/ndk/reference/group/media#aimagereader +pub struct ImageReader { + inner: NonNull, + image_cb: Option>, + #[cfg(feature = "api-level-26")] + buffer_removed_cb: Option>, +} + +impl Debug for ImageReader { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.debug_struct("ImageReader") + .field("inner", &self.inner) + .field( + "image_cb", + match &self.image_cb { + Some(_) => &"Some(_)", + None => &"None", + }, + ) + .finish() + } +} + +impl ImageReader { + fn from_ptr(inner: NonNull) -> Self { + Self { + inner, + image_cb: None, + #[cfg(feature = "api-level-26")] + buffer_removed_cb: None, + } + } + + fn as_ptr(&self) -> *mut ffi::AImageReader { + self.inner.as_ptr() + } + + pub fn new(width: i32, height: i32, format: ImageFormat, max_images: i32) -> Result { + let inner = construct_never_null(|res| unsafe { + ffi::AImageReader_new(width, height, format.into(), max_images, res) + })?; + + Ok(Self::from_ptr(inner)) + } + + #[cfg(feature = "api-level-26")] + pub fn new_with_usage( + width: i32, + height: i32, + format: ImageFormat, + usage: HardwareBufferUsage, + max_images: i32, + ) -> Result { + let inner = construct_never_null(|res| unsafe { + ffi::AImageReader_newWithUsage( + width, + height, + format.into(), + usage.bits(), + max_images, + res, + ) + })?; + + Ok(Self::from_ptr(inner)) + } + + #[doc(alias = "AImageReader_setImageListener")] + pub fn set_image_listener(&mut self, listener: ImageListener) -> Result<()> { + let mut boxed = Box::new(listener); + let ptr: *mut ImageListener = &mut *boxed; + + unsafe extern "C" fn on_image_available( + context: *mut c_void, + reader: *mut ffi::AImageReader, + ) { + abort_on_panic(|| { + let reader = ImageReader::from_ptr(NonNull::new_unchecked(reader)); + let listener: *mut ImageListener = context.cast(); + (*listener)(&reader); + std::mem::forget(reader); + }) + } + + let mut listener = ffi::AImageReader_ImageListener { + context: ptr as _, + onImageAvailable: Some(on_image_available), + }; + let status = unsafe { ffi::AImageReader_setImageListener(self.as_ptr(), &mut listener) }; + + // keep listener alive until Drop or new listener is assigned + self.image_cb = Some(boxed); + + MediaError::from_status(status) + } + + #[cfg(feature = "api-level-26")] + #[doc(alias = "AImageReader_setBufferRemovedListener")] + pub fn set_buffer_removed_listener(&mut self, listener: BufferRemovedListener) -> Result<()> { + let mut boxed = Box::new(listener); + let ptr: *mut BufferRemovedListener = &mut *boxed; + + unsafe extern "C" fn on_buffer_removed( + context: *mut c_void, + reader: *mut ffi::AImageReader, + buffer: *mut ffi::AHardwareBuffer, + ) { + abort_on_panic(|| { + let reader = ImageReader::from_ptr(NonNull::new_unchecked(reader)); + let buffer = HardwareBuffer::from_ptr(NonNull::new_unchecked(buffer)); + let listener: *mut BufferRemovedListener = context.cast(); + (*listener)(&reader, &buffer); + std::mem::forget(reader); + }) + } + + let mut listener = ffi::AImageReader_BufferRemovedListener { + context: ptr as _, + onBufferRemoved: Some(on_buffer_removed), + }; + let status = + unsafe { ffi::AImageReader_setBufferRemovedListener(self.as_ptr(), &mut listener) }; + + // keep listener alive until Drop or new listener is assigned + self.buffer_removed_cb = Some(boxed); + + MediaError::from_status(status) + } + + /// Get a [`NativeWindow`] that can be used to produce [`Image`]s for this [`ImageReader`]. + /// + /// + #[doc(alias = "AImageReader_getWindow")] + pub fn window(&self) -> Result { + unsafe { + let ptr = construct_never_null(|res| ffi::AImageReader_getWindow(self.as_ptr(), res))?; + Ok(NativeWindow::clone_from_ptr(ptr)) + } + } + + #[doc(alias = "AImageReader_getWidth")] + pub fn width(&self) -> Result { + construct(|res| unsafe { ffi::AImageReader_getWidth(self.as_ptr(), res) }) + } + + #[doc(alias = "AImageReader_getHeight")] + pub fn height(&self) -> Result { + construct(|res| unsafe { ffi::AImageReader_getHeight(self.as_ptr(), res) }) + } + + #[doc(alias = "AImageReader_getFormat")] + pub fn format(&self) -> Result { + let format = construct(|res| unsafe { ffi::AImageReader_getFormat(self.as_ptr(), res) })?; + Ok(format.into()) + } + + #[doc(alias = "AImageReader_getMaxImages")] + pub fn max_images(&self) -> Result { + construct(|res| unsafe { ffi::AImageReader_getMaxImages(self.as_ptr(), res) }) + } + + #[doc(alias = "AImageReader_acquireNextImage")] + pub fn acquire_next_image(&self) -> Result> { + AcquireResult::construct_never_null(|res| unsafe { + ffi::AImageReader_acquireNextImage(self.as_ptr(), res) + }) + } + + /// Acquire the next [`Image`] from the image reader's queue asynchronously. + /// + /// # Safety + /// If the returned file descriptor is not [`None`], it must be awaited before attempting to + /// access the [`Image`] returned. + /// + /// + #[cfg(feature = "api-level-26")] + #[doc(alias = "AImageReader_acquireNextImageAsync")] + pub unsafe fn acquire_next_image_async( + &self, + ) -> Result)>> { + let mut fence = MaybeUninit::uninit(); + AcquireResult::construct_never_null(|res| { + ffi::AImageReader_acquireNextImageAsync(self.as_ptr(), res, fence.as_mut_ptr()) + }) + .map(|result| { + result.map(|image| match fence.assume_init() { + -1 => (image, None), + fence => (image, Some(unsafe { OwnedFd::from_raw_fd(fence) })), + }) + }) + } + + #[doc(alias = "AImageReader_acquireLatestImage")] + pub fn acquire_latest_image(&self) -> Result> { + AcquireResult::construct_never_null(|res| unsafe { + ffi::AImageReader_acquireLatestImage(self.as_ptr(), res) + }) + } + + /// Acquire the latest [`Image`] from the image reader's queue asynchronously, dropping older images. + /// + /// # Safety + /// If the returned file descriptor is not [`None`], it must be awaited before attempting to + /// access the [`Image`] returned. + /// + /// + #[cfg(feature = "api-level-26")] + #[doc(alias = "AImageReader_acquireLatestImageAsync")] + pub unsafe fn acquire_latest_image_async( + &self, + ) -> Result)>> { + let mut fence = MaybeUninit::uninit(); + AcquireResult::construct_never_null(|res| { + ffi::AImageReader_acquireLatestImageAsync(self.as_ptr(), res, fence.as_mut_ptr()) + }) + .map(|result| { + result.map(|image| match fence.assume_init() { + -1 => (image, None), + fence => (image, Some(unsafe { OwnedFd::from_raw_fd(fence) })), + }) + }) + } +} + +impl Drop for ImageReader { + #[doc(alias = "AImageReader_delete")] + fn drop(&mut self) { + unsafe { ffi::AImageReader_delete(self.as_ptr()) }; + } +} + +/// A native [`AImage *`] +/// +/// [`AImage *`]: https://developer.android.com/ndk/reference/group/media#aimage +#[derive(Debug)] +#[doc(alias = "AImage")] +pub struct Image { + inner: NonNull, +} + +#[doc(alias = "AImageCropRect")] +pub type CropRect = ffi::AImageCropRect; + +impl Image { + fn as_ptr(&self) -> *mut ffi::AImage { + self.inner.as_ptr() + } + + #[doc(alias = "AImage_getPlaneData")] + pub fn plane_data(&self, plane_idx: i32) -> Result<&[u8]> { + let mut result_ptr = MaybeUninit::uninit(); + let mut result_len = MaybeUninit::uninit(); + let status = unsafe { + ffi::AImage_getPlaneData( + self.as_ptr(), + plane_idx, + result_ptr.as_mut_ptr(), + result_len.as_mut_ptr(), + ) + }; + + MediaError::from_status(status).map(|()| unsafe { + std::slice::from_raw_parts(result_ptr.assume_init(), result_len.assume_init() as _) + }) + } + + #[doc(alias = "AImage_getPlanePixelStride")] + pub fn plane_pixel_stride(&self, plane_idx: i32) -> Result { + construct(|res| unsafe { ffi::AImage_getPlanePixelStride(self.as_ptr(), plane_idx, res) }) + } + + #[doc(alias = "AImage_getPlaneRowStride")] + pub fn plane_row_stride(&self, plane_idx: i32) -> Result { + construct(|res| unsafe { ffi::AImage_getPlaneRowStride(self.as_ptr(), plane_idx, res) }) + } + + #[doc(alias = "AImage_getCropRect")] + pub fn crop_rect(&self) -> Result { + construct(|res| unsafe { ffi::AImage_getCropRect(self.as_ptr(), res) }) + } + + #[doc(alias = "AImage_getWidth")] + pub fn width(&self) -> Result { + construct(|res| unsafe { ffi::AImage_getWidth(self.as_ptr(), res) }) + } + + #[doc(alias = "AImage_getHeight")] + pub fn height(&self) -> Result { + construct(|res| unsafe { ffi::AImage_getHeight(self.as_ptr(), res) }) + } + + #[doc(alias = "AImage_getFormat")] + pub fn format(&self) -> Result { + let format = construct(|res| unsafe { ffi::AImage_getFormat(self.as_ptr(), res) })?; + Ok(format.into()) + } + + #[doc(alias = "AImage_getTimestamp")] + pub fn timestamp(&self) -> Result { + construct(|res| unsafe { ffi::AImage_getTimestamp(self.as_ptr(), res) }) + } + + #[doc(alias = "AImage_getNumberOfPlanes")] + pub fn number_of_planes(&self) -> Result { + construct(|res| unsafe { ffi::AImage_getNumberOfPlanes(self.as_ptr(), res) }) + } + + /// Get the hardware buffer handle of the input image intended for GPU and/or hardware access. + /// + /// Note that no reference on the returned [`HardwareBuffer`] handle is acquired automatically. + /// Once the [`Image`] or the parent [`ImageReader`] is deleted, the [`HardwareBuffer`] handle + /// from previous [`Image::hardware_buffer()`] becomes invalid. + /// + /// If the caller ever needs to hold on a reference to the [`HardwareBuffer`] handle after the + /// [`Image`] or the parent [`ImageReader`] is deleted, it must call + /// [`HardwareBuffer::acquire()`] to acquire an extra reference, and [`drop()`] it when + /// finished using it in order to properly deallocate the underlying memory managed by + /// [`HardwareBuffer`]. If the caller has acquired an extra reference on a [`HardwareBuffer`] + /// returned from this function, it must also register a listener using + /// [`ImageReader::set_buffer_removed_listener()`] to be notified when the buffer is no longer + /// used by [`ImageReader`]. + #[cfg(feature = "api-level-26")] + #[doc(alias = "AImage_getHardwareBuffer")] + pub fn hardware_buffer(&self) -> Result { + unsafe { + let ptr = + construct_never_null(|res| ffi::AImage_getHardwareBuffer(self.as_ptr(), res))?; + Ok(HardwareBuffer::from_ptr(ptr)) + } + } + + #[cfg(feature = "api-level-26")] + #[doc(alias = "AImage_deleteAsync")] + pub fn delete_async(self, release_fence_fd: OwnedFd) { + unsafe { ffi::AImage_deleteAsync(self.as_ptr(), release_fence_fd.into_raw_fd()) }; + std::mem::forget(self); + } +} + +impl Drop for Image { + #[doc(alias = "AImage_delete")] + fn drop(&mut self) { + unsafe { ffi::AImage_delete(self.as_ptr()) }; + } +} diff --git a/native/vendor/ndk-0.9.0/src/media/media_codec.rs b/native/vendor/ndk-0.9.0/src/media/media_codec.rs new file mode 100644 index 0000000..7d6e42c --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/media/media_codec.rs @@ -0,0 +1,630 @@ +//! Bindings for [`AMediaCodec`] +//! +//! [`AMediaCodec`]: https://developer.android.com/ndk/reference/group/media#amediacodec + +#[deprecated = "MediaFormat should be referenced directly from the media_format module"] +pub use super::media_format::MediaFormat; +use crate::media_error::{MediaError, Result}; +use crate::native_window::NativeWindow; +use crate::utils::abort_on_panic; +use std::{ + ffi::{c_char, c_void, CStr, CString}, + fmt, + mem::MaybeUninit, + pin::Pin, + ptr::{self, NonNull}, + slice, + time::Duration, +}; + +#[derive(Debug, PartialEq, Eq)] +pub enum MediaCodecDirection { + Decoder, + Encoder, +} + +/// A native [`AMediaCodec *`] +/// +/// [`AMediaCodec *`]: https://developer.android.com/ndk/reference/group/media#amediacodec +#[derive(Debug)] +pub struct MediaCodec { + inner: NonNull, + async_notify_callback: Option>>, +} + +pub struct AsyncNotifyCallback { + /// Called when an input buffer becomes available. + /// + /// The specified index is the index of the available input buffer. + pub on_input_available: Option, + + /// Called when an output buffer becomes available. + /// + /// The specified index is the index of the available output buffer. The specified + /// [`BufferInfo`] contains information regarding the available output buffer. + pub on_output_available: Option, + + /// Called when the output format has changed. + /// + /// The specified format contains the new output format. + pub on_format_changed: Option, + + /// Called when the [`MediaCodec`] encountered an error. + /// + /// The specified [`ActionCode`] indicates the possible actions that client can take, and it can + /// be checked by calling [`ActionCode::is_recoverable`] or [`ActionCode::is_transient`]. If + /// both [`ActionCode::is_recoverable`] and [`ActionCode::is_transient`] return [`false`], then + /// the codec error is fatal and the codec must be deleted. The specified detail string may + /// contain more detailed messages about this error. + pub on_error: Option, +} + +impl fmt::Debug for AsyncNotifyCallback { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("AsyncNotifyCallback") + .field( + "on_input_available", + match &self.on_input_available { + Some(_) => &"Some(_)", + None => &"None", + }, + ) + .field( + "on_output_available", + match &self.on_output_available { + Some(_) => &"Some(_)", + None => &"None", + }, + ) + .field( + "on_format_changed", + match &self.on_format_changed { + Some(_) => &"Some(_)", + None => &"None", + }, + ) + .field( + "on_error", + match &self.on_error { + Some(_) => &"Some(_)", + None => &"None", + }, + ) + .finish() + } +} + +pub type InputAvailableCallback = Box; +pub type OutputAvailableCallback = Box; +pub type FormatChangedCallback = Box; +pub type ErrorCallback = Box; + +impl MediaCodec { + fn as_ptr(&self) -> *mut ffi::AMediaCodec { + self.inner.as_ptr() + } + + pub fn from_codec_name(name: &str) -> Option { + let c_string = CString::new(name).unwrap(); + Some(Self { + inner: NonNull::new(unsafe { ffi::AMediaCodec_createCodecByName(c_string.as_ptr()) })?, + async_notify_callback: None, + }) + } + + pub fn from_decoder_type(mime_type: &str) -> Option { + let c_string = CString::new(mime_type).unwrap(); + Some(Self { + inner: NonNull::new(unsafe { + ffi::AMediaCodec_createDecoderByType(c_string.as_ptr()) + })?, + async_notify_callback: None, + }) + } + + pub fn from_encoder_type(mime_type: &str) -> Option { + let c_string = CString::new(mime_type).unwrap(); + Some(Self { + inner: NonNull::new(unsafe { + ffi::AMediaCodec_createEncoderByType(c_string.as_ptr()) + })?, + async_notify_callback: None, + }) + } + + /// Set an asynchronous callback for actionable [`MediaCodec`] events. + /// + /// When asynchronous callback is enabled, it is an error for the client to call + /// [`MediaCodec::dequeue_input_buffer()`] or [`MediaCodec::dequeue_output_buffer()`]. + /// + /// [`MediaCodec::flush()`] behaves differently in asynchronous mode. After calling + /// [`MediaCodec::flush()`], the client must call [`MediaCodec::start()`] to "resume" receiving + /// input buffers. Even if the client does not receive + /// [`AsyncNotifyCallback::on_input_available`] callbacks from video encoders configured with an + /// input surface, the client still needs to call [`MediaCodec::start()`] to resume the input + /// surface to send buffers to the encoders. + /// + /// When called with [`None`] callback, this method unregisters any previously set callback. + /// + /// Refer to the definition of [`AsyncNotifyCallback`] on how each callback function is called + /// and what are specified. + /// + /// Once the callback is unregistered or the codec is reset / released, the previously + /// registered callback will not be called. + /// + /// All callbacks are fired on one NDK internal thread. + /// [`MediaCodec::set_async_notify_callback()`] should not be called on the callback thread. No + /// heavy duty task should be performed on callback thread. + #[cfg(feature = "api-level-28")] + pub fn set_async_notify_callback( + &mut self, + callback: Option, + ) -> Result<()> { + unsafe extern "C" fn ffi_on_input_available( + _codec: *mut ffi::AMediaCodec, + user_data: *mut c_void, + index: i32, + ) { + abort_on_panic(|| { + let callback = &mut *(user_data as *mut AsyncNotifyCallback); + if let Some(f) = callback.on_input_available.as_mut() { + f(index as usize); + } + }) + } + + unsafe extern "C" fn ffi_on_output_available( + _codec: *mut ffi::AMediaCodec, + user_data: *mut c_void, + index: i32, + buffer_info: *mut ffi::AMediaCodecBufferInfo, + ) { + abort_on_panic(|| { + let callback = &mut *(user_data as *mut AsyncNotifyCallback); + if let Some(f) = callback.on_output_available.as_mut() { + let buffer_info = BufferInfo { + inner: *buffer_info, + }; + f(index as usize, &buffer_info); + } + }) + } + + unsafe extern "C" fn ffi_on_format_changed( + _codec: *mut ffi::AMediaCodec, + user_data: *mut c_void, + format: *mut ffi::AMediaFormat, + ) { + abort_on_panic(|| { + // Ownership of the format is not documented, but the implementation allocates a new instance and does + // not free it, so assume it is ok for us to do so + // https://cs.android.com/android/platform/superproject/main/+/refs/heads/main:frameworks/av/media/ndk/NdkMediaCodec.cpp;l=248-254;drc=5e15c3e22f3fa32d64e57302201123ce41589adf + let format = MediaFormat::from_ptr(NonNull::new_unchecked(format)); + + let callback = &mut *(user_data as *mut AsyncNotifyCallback); + if let Some(f) = callback.on_format_changed.as_mut() { + f(&format); + } + }) + } + + unsafe extern "C" fn ffi_on_error( + _codec: *mut ffi::AMediaCodec, + user_data: *mut c_void, + error: ffi::media_status_t, + action_code: i32, + detail: *const c_char, + ) { + abort_on_panic(|| { + let callback = &mut *(user_data as *mut AsyncNotifyCallback); + if let Some(f) = callback.on_error.as_mut() { + f( + MediaError::from_status(error).unwrap_err(), + ActionCode(action_code), + CStr::from_ptr(detail), + ); + } + }) + } + + let (callback, ffi_callback, user_data) = if let Some(callback) = callback { + // On Android 12 and earlier, due to faulty null checks, if a callback is not set, but at least one other + // callback *is* set, then it will segfault in when trying to invoke the unset callback. See for example: + // https://cs.android.com/android/platform/superproject/+/android-12.0.0_r34:frameworks/av/media/ndk/NdkMediaCodec.cpp;l=161-162;drc=ef058464777739e2d9ffad5f00d0e57b186d9a13 + // To work around this we just enable all callbacks and do nothing if the corresponding callback is not set + // in AsyncNotifyCallback + let ffi_callback = ffi::AMediaCodecOnAsyncNotifyCallback { + onAsyncInputAvailable: Some(ffi_on_input_available), + onAsyncOutputAvailable: Some(ffi_on_output_available), + onAsyncFormatChanged: Some(ffi_on_format_changed), + onAsyncError: Some(ffi_on_error), + }; + + let mut boxed = Box::pin(callback); + let ptr: *mut AsyncNotifyCallback = &mut *boxed; + + (Some(boxed), ffi_callback, ptr as *mut c_void) + } else { + let ffi_callback = ffi::AMediaCodecOnAsyncNotifyCallback { + onAsyncInputAvailable: None, + onAsyncOutputAvailable: None, + onAsyncFormatChanged: None, + onAsyncError: None, + }; + + (None, ffi_callback, ptr::null_mut()) + }; + + let status = unsafe { + ffi::AMediaCodec_setAsyncNotifyCallback(self.as_ptr(), ffi_callback, user_data) + }; + let result = MediaError::from_status(status); + + // This behavior is not documented, but the implementation always clears the callback on failure, so we must + // clear any callback that may have been previously registered + // https://cs.android.com/android/platform/superproject/main/+/main:frameworks/av/media/ndk/NdkMediaCodec.cpp;l=581-584;drc=8c4e619c7461ac1a8c20c55364643662e9185e4d + if result.is_ok() { + self.async_notify_callback = callback; + } else { + self.async_notify_callback = None; + } + + result + } + + pub fn configure( + &self, + format: &MediaFormat, + surface: Option<&NativeWindow>, + direction: MediaCodecDirection, + ) -> Result<()> { + let status = unsafe { + ffi::AMediaCodec_configure( + self.as_ptr(), + format.as_ptr(), + surface.map_or(ptr::null_mut(), |s| s.ptr().as_ptr()), + ptr::null_mut(), + if direction == MediaCodecDirection::Encoder { + ffi::AMEDIACODEC_CONFIGURE_FLAG_ENCODE as u32 + } else { + 0 + }, + ) + }; + MediaError::from_status(status) + } + + #[cfg(feature = "api-level-26")] + pub fn create_input_surface(&self) -> Result { + use crate::media_error::construct_never_null; + unsafe { + let ptr = construct_never_null(|res| { + ffi::AMediaCodec_createInputSurface(self.as_ptr(), res) + })?; + Ok(NativeWindow::from_ptr(ptr)) + } + } + + #[cfg(feature = "api-level-26")] + pub fn create_persistent_input_surface() -> Result { + use crate::media_error::construct_never_null; + unsafe { + let ptr = + construct_never_null(|res| ffi::AMediaCodec_createPersistentInputSurface(res))?; + Ok(NativeWindow::from_ptr(ptr)) + } + } + + pub fn dequeue_input_buffer(&self, timeout: Duration) -> Result> { + let result = unsafe { + ffi::AMediaCodec_dequeueInputBuffer( + self.as_ptr(), + timeout + .as_micros() + .try_into() + .expect("Supplied timeout is too large"), + ) + }; + + if result == ffi::AMEDIACODEC_INFO_TRY_AGAIN_LATER as isize { + Ok(DequeuedInputBufferResult::TryAgainLater) + } else { + let index = MediaError::from_status_if_negative(result)? as usize; + Ok(DequeuedInputBufferResult::Buffer(InputBuffer { + codec: self, + index, + })) + } + } + + pub fn dequeue_output_buffer( + &self, + timeout: Duration, + ) -> Result> { + let mut info = MaybeUninit::uninit(); + + let result = unsafe { + ffi::AMediaCodec_dequeueOutputBuffer( + self.as_ptr(), + info.as_mut_ptr(), + timeout + .as_micros() + .try_into() + .expect("Supplied timeout is too large"), + ) + }; + + if result == ffi::AMEDIACODEC_INFO_TRY_AGAIN_LATER as isize { + Ok(DequeuedOutputBufferInfoResult::TryAgainLater) + } else if result == ffi::AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED as isize { + Ok(DequeuedOutputBufferInfoResult::OutputFormatChanged) + } else if result == ffi::AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED as isize { + Ok(DequeuedOutputBufferInfoResult::OutputBuffersChanged) + } else { + let index = MediaError::from_status_if_negative(result)? as usize; + Ok(DequeuedOutputBufferInfoResult::Buffer(OutputBuffer { + codec: self, + index, + info: BufferInfo { + inner: unsafe { info.assume_init() }, + }, + })) + } + } + + pub fn flush(&self) -> Result<()> { + let status = unsafe { ffi::AMediaCodec_flush(self.as_ptr()) }; + MediaError::from_status(status) + } + + pub fn input_buffer(&self, index: usize) -> Option<&mut [MaybeUninit]> { + unsafe { + let mut out_size = 0; + let buffer_ptr = ffi::AMediaCodec_getInputBuffer(self.as_ptr(), index, &mut out_size); + if buffer_ptr.is_null() { + return None; + } + Some(slice::from_raw_parts_mut(buffer_ptr.cast(), out_size)) + } + } + + pub fn output_buffer(&self, index: usize) -> Option<&[u8]> { + unsafe { + let mut out_size = 0; + let buffer_ptr = ffi::AMediaCodec_getOutputBuffer(self.as_ptr(), index, &mut out_size); + if buffer_ptr.is_null() { + return None; + } + Some(slice::from_raw_parts(buffer_ptr, out_size)) + } + } + + #[cfg(feature = "api-level-28")] + pub fn input_format(&self) -> MediaFormat { + let inner = NonNull::new(unsafe { ffi::AMediaCodec_getInputFormat(self.as_ptr()) }) + .expect("AMediaCodec_getInputFormat returned NULL"); + unsafe { MediaFormat::from_ptr(inner) } + } + + pub fn output_format(&self) -> MediaFormat { + let inner = NonNull::new(unsafe { ffi::AMediaCodec_getOutputFormat(self.as_ptr()) }) + .expect("AMediaCodec_getOutputFormat returned NULL"); + unsafe { MediaFormat::from_ptr(inner) } + } + + #[cfg(feature = "api-level-28")] + pub fn name(&self) -> Result { + use crate::media_error::construct; + unsafe { + let name_ptr = construct(|name| ffi::AMediaCodec_getName(self.as_ptr(), name))?; + let name = CStr::from_ptr(name_ptr).to_str().unwrap().to_owned(); + ffi::AMediaCodec_releaseName(self.as_ptr(), name_ptr); + + Ok(name) + } + } + + pub fn queue_input_buffer( + &self, + buffer: InputBuffer<'_>, + offset: usize, + size: usize, + time: u64, + flags: u32, + ) -> Result<()> { + debug_assert!(ptr::eq(self, buffer.codec)); + self.queue_input_buffer_by_index(buffer.index, offset, size, time, flags) + } + + pub fn queue_input_buffer_by_index( + &self, + buffer_index: usize, + offset: usize, + size: usize, + time: u64, + flags: u32, + ) -> Result<()> { + let status = unsafe { + ffi::AMediaCodec_queueInputBuffer( + self.as_ptr(), + buffer_index, + offset as ffi::off_t, + size, + time, + flags, + ) + }; + MediaError::from_status(status) + } + + pub fn release_output_buffer(&self, buffer: OutputBuffer<'_>, render: bool) -> Result<()> { + debug_assert!(ptr::eq(self, buffer.codec)); + self.release_output_buffer_by_index(buffer.index, render) + } + + pub fn release_output_buffer_by_index(&self, buffer_index: usize, render: bool) -> Result<()> { + let status = + unsafe { ffi::AMediaCodec_releaseOutputBuffer(self.as_ptr(), buffer_index, render) }; + MediaError::from_status(status) + } + + pub fn release_output_buffer_at_time( + &self, + buffer: OutputBuffer<'_>, + timestamp_ns: i64, + ) -> Result<()> { + debug_assert!(ptr::eq(self, buffer.codec)); + self.release_output_buffer_at_time_by_index(buffer.index, timestamp_ns) + } + + pub fn release_output_buffer_at_time_by_index( + &self, + buffer_index: usize, + timestamp_ns: i64, + ) -> Result<()> { + let status = unsafe { + ffi::AMediaCodec_releaseOutputBufferAtTime(self.as_ptr(), buffer_index, timestamp_ns) + }; + MediaError::from_status(status) + } + + #[cfg(feature = "api-level-26")] + pub fn set_input_surface(&self, surface: &NativeWindow) -> Result<()> { + let status = + unsafe { ffi::AMediaCodec_setInputSurface(self.as_ptr(), surface.ptr().as_ptr()) }; + MediaError::from_status(status) + } + + pub fn set_output_surface(&self, surface: &NativeWindow) -> Result<()> { + let status = + unsafe { ffi::AMediaCodec_setOutputSurface(self.as_ptr(), surface.ptr().as_ptr()) }; + MediaError::from_status(status) + } + + #[cfg(feature = "api-level-26")] + pub fn set_parameters(&self, params: MediaFormat) -> Result<()> { + let status = unsafe { ffi::AMediaCodec_setParameters(self.as_ptr(), params.as_ptr()) }; + MediaError::from_status(status) + } + + #[cfg(feature = "api-level-26")] + pub fn set_signal_end_of_input_stream(&self) -> Result<()> { + let status = unsafe { ffi::AMediaCodec_signalEndOfInputStream(self.as_ptr()) }; + MediaError::from_status(status) + } + + pub fn start(&self) -> Result<()> { + let status = unsafe { ffi::AMediaCodec_start(self.as_ptr()) }; + MediaError::from_status(status) + } + + pub fn stop(&self) -> Result<()> { + let status = unsafe { ffi::AMediaCodec_stop(self.as_ptr()) }; + MediaError::from_status(status) + } +} + +impl Drop for MediaCodec { + fn drop(&mut self) { + let status = unsafe { ffi::AMediaCodec_delete(self.as_ptr()) }; + MediaError::from_status(status).unwrap(); + } +} + +#[derive(Debug)] +pub struct InputBuffer<'a> { + codec: &'a MediaCodec, + index: usize, +} + +impl InputBuffer<'_> { + pub fn buffer_mut(&mut self) -> &mut [MaybeUninit] { + self.codec.input_buffer(self.index).unwrap_or_else(|| { + panic!( + "AMediaCodec_getInputBuffer returned NULL for index {}", + self.index + ) + }) + } +} + +#[derive(Debug)] +pub enum DequeuedInputBufferResult<'a> { + Buffer(InputBuffer<'a>), + TryAgainLater, +} + +#[derive(Debug)] +pub struct OutputBuffer<'a> { + codec: &'a MediaCodec, + index: usize, + info: BufferInfo, +} + +impl OutputBuffer<'_> { + pub fn buffer(&self) -> &[u8] { + self.codec.output_buffer(self.index).unwrap_or_else(|| { + panic!( + "AMediaCodec_getOutputBuffer returned NULL for index {}", + self.index + ) + }) + } + + #[cfg(feature = "api-level-28")] + pub fn format(&self) -> MediaFormat { + let inner = NonNull::new(unsafe { + ffi::AMediaCodec_getBufferFormat(self.codec.as_ptr(), self.index) + }) + .expect("AMediaCodec_getBufferFormat returned NULL"); + unsafe { MediaFormat::from_ptr(inner) } + } + + pub fn info(&self) -> &BufferInfo { + &self.info + } +} + +#[derive(Debug)] +pub enum DequeuedOutputBufferInfoResult<'a> { + Buffer(OutputBuffer<'a>), + TryAgainLater, + OutputFormatChanged, + OutputBuffersChanged, +} + +#[derive(Copy, Clone, Debug)] +pub struct BufferInfo { + inner: ffi::AMediaCodecBufferInfo, +} + +impl BufferInfo { + pub fn offset(&self) -> i32 { + self.inner.offset + } + + pub fn size(&self) -> i32 { + self.inner.size + } + + pub fn presentation_time_us(&self) -> i64 { + self.inner.presentationTimeUs + } + + pub fn flags(&self) -> u32 { + self.inner.flags + } +} + +#[derive(Copy, Clone, Debug)] +pub struct ActionCode(pub i32); + +impl ActionCode { + pub fn is_recoverable(self) -> bool { + unsafe { ffi::AMediaCodecActionCode_isRecoverable(self.0) } + } + + pub fn is_transient(self) -> bool { + unsafe { ffi::AMediaCodecActionCode_isTransient(self.0) } + } +} diff --git a/native/vendor/ndk-0.9.0/src/media/media_format.rs b/native/vendor/ndk-0.9.0/src/media/media_format.rs new file mode 100644 index 0000000..4997d52 --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/media/media_format.rs @@ -0,0 +1,264 @@ +//! Bindings for [`AMediaFormat`] +//! +//! [`AMediaFormat`]: https://developer.android.com/ndk/reference/group/media#amediaformat + +use std::{ + ffi::{CStr, CString}, + fmt, + ptr::{self, NonNull}, + slice, +}; + +use crate::media_error::{MediaError, Result}; + +/// A native [`AMediaFormat *`] +/// +/// [`AMediaFormat *`]: https://developer.android.com/ndk/reference/group/media#amediaformat +#[doc(alias = "AMediaFormat")] +pub struct MediaFormat { + inner: NonNull, +} + +impl fmt::Display for MediaFormat { + /// Human readable representation of the format. + #[doc(alias = "AMediaFormat_toString")] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let c_str = unsafe { CStr::from_ptr(ffi::AMediaFormat_toString(self.as_ptr())) }; + f.write_str(c_str.to_str().unwrap()) + } +} + +impl fmt::Debug for MediaFormat { + #[doc(alias = "AMediaFormat_toString")] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "MediaFormat({:?}: {})", self.inner, self) + } +} + +impl Default for MediaFormat { + #[doc(alias = "AMediaFormat_new")] + fn default() -> Self { + Self::new() + } +} + +impl MediaFormat { + /// Assumes ownership of `ptr` + /// + /// # Safety + /// `ptr` must be a valid pointer to an Android [`ffi::AMediaFormat`]. + pub unsafe fn from_ptr(ptr: NonNull) -> Self { + Self { inner: ptr } + } + + pub fn as_ptr(&self) -> *mut ffi::AMediaFormat { + self.inner.as_ptr() + } + + #[doc(alias = "AMediaFormat_new")] + pub fn new() -> Self { + Self { + inner: NonNull::new(unsafe { ffi::AMediaFormat_new() }).unwrap(), + } + } + + #[doc(alias = "AMediaFormat_getInt32")] + pub fn i32(&self, key: &str) -> Option { + let name = CString::new(key).unwrap(); + let mut out = 0; + if unsafe { ffi::AMediaFormat_getInt32(self.as_ptr(), name.as_ptr(), &mut out) } { + Some(out) + } else { + None + } + } + + #[doc(alias = "AMediaFormat_getInt64")] + pub fn i64(&self, key: &str) -> Option { + let name = CString::new(key).unwrap(); + let mut out = 0; + if unsafe { ffi::AMediaFormat_getInt64(self.as_ptr(), name.as_ptr(), &mut out) } { + Some(out) + } else { + None + } + } + + #[doc(alias = "AMediaFormat_getFloat")] + pub fn f32(&self, key: &str) -> Option { + let name = CString::new(key).unwrap(); + let mut out = 0.0; + if unsafe { ffi::AMediaFormat_getFloat(self.as_ptr(), name.as_ptr(), &mut out) } { + Some(out) + } else { + None + } + } + + #[doc(alias = "AMediaFormat_getSize")] + pub fn usize(&self, key: &str) -> Option { + let name = CString::new(key).unwrap(); + let mut out = 0; + if unsafe { ffi::AMediaFormat_getSize(self.as_ptr(), name.as_ptr(), &mut out) } { + Some(out) + } else { + None + } + } + + #[doc(alias = "AMediaFormat_getBuffer")] + pub fn buffer(&self, key: &str) -> Option<&[u8]> { + let name = CString::new(key).unwrap(); + let mut out_buffer = ptr::null_mut(); + let mut out_size = 0; + unsafe { + ffi::AMediaFormat_getBuffer( + self.as_ptr(), + name.as_ptr(), + &mut out_buffer, + &mut out_size, + ) + } + .then(|| unsafe { slice::from_raw_parts(out_buffer.cast(), out_size) }) + } + + /// The returned `&str` borrow is only valid until the next call to [`MediaFormat::str()`] for + /// the same key. + #[doc(alias = "AMediaFormat_getString")] + pub fn str(&mut self, key: &str) -> Option<&str> { + let name = CString::new(key).unwrap(); + let mut out = ptr::null(); + unsafe { ffi::AMediaFormat_getString(self.as_ptr(), name.as_ptr(), &mut out) } + .then(|| unsafe { CStr::from_ptr(out) }.to_str().unwrap()) + } + + #[doc(alias = "AMediaFormat_setInt32")] + pub fn set_i32(&mut self, key: &str, value: i32) { + let name = CString::new(key).unwrap(); + unsafe { ffi::AMediaFormat_setInt32(self.as_ptr(), name.as_ptr(), value) } + } + + #[doc(alias = "AMediaFormat_setInt64")] + pub fn set_i64(&mut self, key: &str, value: i64) { + let name = CString::new(key).unwrap(); + unsafe { ffi::AMediaFormat_setInt64(self.as_ptr(), name.as_ptr(), value) } + } + + #[doc(alias = "AMediaFormat_setFloat")] + pub fn set_f32(&mut self, key: &str, value: f32) { + let name = CString::new(key).unwrap(); + unsafe { ffi::AMediaFormat_setFloat(self.as_ptr(), name.as_ptr(), value) } + } + + #[doc(alias = "AMediaFormat_setString")] + pub fn set_str(&mut self, key: &str, value: &str) { + let name = CString::new(key).unwrap(); + let c_string = CString::new(value).unwrap(); + unsafe { ffi::AMediaFormat_setString(self.as_ptr(), name.as_ptr(), c_string.as_ptr()) } + } + + #[doc(alias = "AMediaFormat_setBuffer")] + pub fn set_buffer(&mut self, key: &str, value: &[u8]) { + let name = CString::new(key).unwrap(); + unsafe { + ffi::AMediaFormat_setBuffer( + self.as_ptr(), + name.as_ptr(), + value.as_ptr().cast(), + value.len(), + ) + } + } + + #[cfg(feature = "api-level-28")] + #[doc(alias = "AMediaFormat_getDouble")] + pub fn f64(&self, key: &str) -> Option { + let name = CString::new(key).unwrap(); + let mut out = 0.0; + if unsafe { ffi::AMediaFormat_getDouble(self.as_ptr(), name.as_ptr(), &mut out) } { + Some(out) + } else { + None + } + } + + /// Returns (left, top, right, bottom) + #[cfg(feature = "api-level-28")] + #[doc(alias = "AMediaFormat_getRect")] + pub fn rect(&self, key: &str) -> Option<(i32, i32, i32, i32)> { + let name = CString::new(key).unwrap(); + let mut left = 0; + let mut top = 0; + let mut right = 0; + let mut bottom = 0; + if unsafe { + ffi::AMediaFormat_getRect( + self.as_ptr(), + name.as_ptr(), + &mut left, + &mut top, + &mut right, + &mut bottom, + ) + } { + Some((left, top, right, bottom)) + } else { + None + } + } + + #[cfg(feature = "api-level-28")] + #[doc(alias = "AMediaFormat_setDouble")] + pub fn set_f64(&mut self, key: &str, value: f64) { + let name = CString::new(key).unwrap(); + unsafe { ffi::AMediaFormat_setDouble(self.as_ptr(), name.as_ptr(), value) } + } + + #[cfg(feature = "api-level-28")] + #[doc(alias = "AMediaFormat_setRect")] + pub fn set_rect(&mut self, key: &str, left: i32, top: i32, right: i32, bottom: i32) { + let name = CString::new(key).unwrap(); + unsafe { ffi::AMediaFormat_setRect(self.as_ptr(), name.as_ptr(), left, top, right, bottom) } + } + + #[cfg(feature = "api-level-28")] + #[doc(alias = "AMediaFormat_setSize")] + pub fn set_usize(&mut self, key: &str, value: usize) { + let name = CString::new(key).unwrap(); + unsafe { ffi::AMediaFormat_setSize(self.as_ptr(), name.as_ptr(), value) } + } + + /// Copy one [`MediaFormat`] to another. + #[cfg(feature = "api-level-29")] + #[doc(alias = "AMediaFormat_copy")] + pub fn copy(&self, to: &mut Self) -> Result<()> { + let status = unsafe { ffi::AMediaFormat_copy(to.as_ptr(), self.as_ptr()) }; + MediaError::from_status(status) + } + + /// Clones this [`MediaFormat`] into a [`MediaFormat::new()`] object using + /// [`MediaFormat::copy()`]. + #[cfg(feature = "api-level-29")] + #[doc(alias = "AMediaFormat_new")] + #[doc(alias = "AMediaFormat_copy")] + pub fn try_clone(&self) -> Result { + let mut copy = Self::new(); + self.copy(&mut copy)?; + Ok(copy) + } + + /// Remove all key/value pairs from this [`MediaFormat`]. + #[cfg(feature = "api-level-29")] + #[doc(alias = "AMediaFormat_clear")] + pub fn clear(&mut self) { + unsafe { ffi::AMediaFormat_clear(self.as_ptr()) } + } +} + +impl Drop for MediaFormat { + #[doc(alias = "AMediaFormat_delete")] + fn drop(&mut self) { + let status = unsafe { ffi::AMediaFormat_delete(self.as_ptr()) }; + MediaError::from_status(status).unwrap() + } +} diff --git a/native/vendor/ndk-0.9.0/src/media/mod.rs b/native/vendor/ndk-0.9.0/src/media/mod.rs new file mode 100644 index 0000000..1689a6e --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/media/mod.rs @@ -0,0 +1,8 @@ +//! Bindings for the NDK media classes. +//! +//! See also [the NDK docs](https://developer.android.com/ndk/reference/group/media) +#![cfg(feature = "media")] + +pub mod image_reader; +pub mod media_codec; +pub mod media_format; diff --git a/native/vendor/ndk-0.9.0/src/media_error.rs b/native/vendor/ndk-0.9.0/src/media_error.rs new file mode 100644 index 0000000..99fb0d5 --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/media_error.rs @@ -0,0 +1,140 @@ +//! Bindings for NDK media status codes. +//! +//! Also used outside of `libmediandk.so` in `libamidi.so` for example. +#![cfg(feature = "media")] +// The cfg(feature) bounds for some pub(crate) fn uses are non-trivial and will become even more +// complex going forward. Allow them to be unused when compiling with certain feature combinations. +#![allow(dead_code)] + +use std::{fmt, mem::MaybeUninit, ptr::NonNull}; + +use num_enum::{FromPrimitive, IntoPrimitive}; + +pub type Result = std::result::Result; + +/// Media Status codes for [`media_status_t`](https://developer.android.com/ndk/reference/group/media#group___media_1ga009a49041fe39f7bdc6d8b5cddbe760c) +#[repr(i32)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)] +#[doc(alias = "media_status_t")] +#[non_exhaustive] +pub enum MediaError { + #[doc(alias = "AMEDIACODEC_ERROR_INSUFFICIENT_RESOURCE")] + CodecErrorInsufficientResource = ffi::media_status_t::AMEDIACODEC_ERROR_INSUFFICIENT_RESOURCE.0, + #[doc(alias = "AMEDIACODEC_ERROR_RECLAIMED")] + CodecErrorReclaimed = ffi::media_status_t::AMEDIACODEC_ERROR_RECLAIMED.0, + #[doc(alias = "AMEDIA_ERROR_UNKNOWN")] + ErrorUnknown = ffi::media_status_t::AMEDIA_ERROR_UNKNOWN.0, + #[doc(alias = "AMEDIA_ERROR_MALFORMED")] + ErrorMalformed = ffi::media_status_t::AMEDIA_ERROR_MALFORMED.0, + #[doc(alias = "AMEDIA_ERROR_UNSUPPORTED")] + ErrorUnsupported = ffi::media_status_t::AMEDIA_ERROR_UNSUPPORTED.0, + #[doc(alias = "AMEDIA_ERROR_INVALID_OBJECT")] + ErrorInvalidObject = ffi::media_status_t::AMEDIA_ERROR_INVALID_OBJECT.0, + #[doc(alias = "AMEDIA_ERROR_INVALID_PARAMETER")] + ErrorInvalidParameter = ffi::media_status_t::AMEDIA_ERROR_INVALID_PARAMETER.0, + #[doc(alias = "AMEDIA_ERROR_INVALID_OPERATION")] + ErrorInvalidOperation = ffi::media_status_t::AMEDIA_ERROR_INVALID_OPERATION.0, + #[doc(alias = "AMEDIA_ERROR_END_OF_STREAM")] + ErrorEndOfStream = ffi::media_status_t::AMEDIA_ERROR_END_OF_STREAM.0, + #[doc(alias = "AMEDIA_ERROR_IO")] + ErrorIo = ffi::media_status_t::AMEDIA_ERROR_IO.0, + #[doc(alias = "AMEDIA_ERROR_WOULD_BLOCK")] + ErrorWouldBlock = ffi::media_status_t::AMEDIA_ERROR_WOULD_BLOCK.0, + #[doc(alias = "AMEDIA_DRM_ERROR_BASE")] + DrmErrorBase = ffi::media_status_t::AMEDIA_DRM_ERROR_BASE.0, + #[doc(alias = "AMEDIA_DRM_NOT_PROVISIONED")] + DrmNotProvisioned = ffi::media_status_t::AMEDIA_DRM_NOT_PROVISIONED.0, + #[doc(alias = "AMEDIA_DRM_RESOURCE_BUSY")] + DrmResourceBusy = ffi::media_status_t::AMEDIA_DRM_RESOURCE_BUSY.0, + #[doc(alias = "AMEDIA_DRM_DEVICE_REVOKED")] + DrmDeviceRevoked = ffi::media_status_t::AMEDIA_DRM_DEVICE_REVOKED.0, + #[doc(alias = "AMEDIA_DRM_SHORT_BUFFER")] + DrmShortBuffer = ffi::media_status_t::AMEDIA_DRM_SHORT_BUFFER.0, + #[doc(alias = "AMEDIA_DRM_SESSION_NOT_OPENED")] + DrmSessionNotOpened = ffi::media_status_t::AMEDIA_DRM_SESSION_NOT_OPENED.0, + #[doc(alias = "AMEDIA_DRM_TAMPER_DETECTED")] + DrmTamperDetected = ffi::media_status_t::AMEDIA_DRM_TAMPER_DETECTED.0, + #[doc(alias = "AMEDIA_DRM_VERIFY_FAILED")] + DrmVerifyFailed = ffi::media_status_t::AMEDIA_DRM_VERIFY_FAILED.0, + #[doc(alias = "AMEDIA_DRM_NEED_KEY")] + DrmNeedKey = ffi::media_status_t::AMEDIA_DRM_NEED_KEY.0, + #[doc(alias = "AMEDIA_DRM_LICENSE_EXPIRED")] + DrmLicenseExpired = ffi::media_status_t::AMEDIA_DRM_LICENSE_EXPIRED.0, + #[doc(alias = "AMEDIA_IMGREADER_ERROR_BASE")] + ImgreaderErrorBase = ffi::media_status_t::AMEDIA_IMGREADER_ERROR_BASE.0, + #[doc(alias = "AMEDIA_IMGREADER_CANNOT_LOCK_IMAGE")] + ImgreaderCannotLockImage = ffi::media_status_t::AMEDIA_IMGREADER_CANNOT_LOCK_IMAGE.0, + #[doc(alias = "AMEDIA_IMGREADER_CANNOT_UNLOCK_IMAGE")] + ImgreaderCannotUnlockImage = ffi::media_status_t::AMEDIA_IMGREADER_CANNOT_UNLOCK_IMAGE.0, + #[doc(alias = "AMEDIA_IMGREADER_IMAGE_NOT_LOCKED")] + ImgreaderImageNotLocked = ffi::media_status_t::AMEDIA_IMGREADER_IMAGE_NOT_LOCKED.0, + + /// This error code is unknown to the [`ndk`][crate] crate. Please report an issue if you + /// believe this code needs to be added to our mapping. + // Use the OK discriminant, as no-one will be able to call `as i32` and only has access to the + // constants via `From` provided by `IntoPrimitive` which reads the contained value. + // An autogenerated ` + 1` discriminant is normally fine, except that the + // previous variant is negative and `+1` would match the variant before that. + #[doc(hidden)] + #[num_enum(catch_all)] + __Unknown(i32) = ffi::media_status_t::AMEDIA_OK.0, +} + +impl fmt::Display for MediaError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self) + } +} + +impl std::error::Error for MediaError {} + +impl MediaError { + /// Returns [`Ok`] on [`ffi::media_status_t::AMEDIA_OK`], [`Err`] otherwise (including positive + /// values). + /// + /// Note that some known error codes (currently only for `AMediaCodec`) are positive. + pub(crate) fn from_status(status: ffi::media_status_t) -> Result<()> { + match status { + ffi::media_status_t::AMEDIA_OK => Ok(()), + x => Err(Self::from(x.0)), + } + } + + /// Returns the original value in [`Ok`] if it is not negative, [`Err`] otherwise. + /// + /// Note that some [`ffi::media_status_t`] codes are positive but will never be returned as + /// [`Err`] from this function. As of writing these codes are specific to the `AMediaCodec` API + /// and should not be handled generically. + pub(crate) fn from_status_if_negative + Copy>(value: T) -> Result { + let v = value.into(); + if v >= 0 { + Ok(value) + } else { + Err(Self::from( + i32::try_from(v).expect("Error code out of bounds"), + )) + } + } +} + +/// Calls the `with_ptr` construction function with a pointer to uninitialized stack memory, +/// expecting `with_ptr` to initialize it or otherwise return an error code. +pub(crate) fn construct(with_ptr: impl FnOnce(*mut T) -> ffi::media_status_t) -> Result { + let mut result = MaybeUninit::uninit(); + let status = with_ptr(result.as_mut_ptr()); + MediaError::from_status(status).map(|()| unsafe { result.assume_init() }) +} + +/// Calls the `with_ptr` construction function with a pointer to a pointer, and expects `with_ptr` +/// to initialize the second pointer to a valid address. That address is returned in the form of a +/// [`NonNull`] object. +pub(crate) fn construct_never_null( + with_ptr: impl FnOnce(*mut *mut T) -> ffi::media_status_t, +) -> Result> { + let result = construct(with_ptr)?; + Ok(if cfg!(debug_assertions) { + NonNull::new(result).expect("result should never be null") + } else { + unsafe { NonNull::new_unchecked(result) } + }) +} diff --git a/native/vendor/ndk-0.9.0/src/native_activity.rs b/native/vendor/ndk-0.9.0/src/native_activity.rs new file mode 100644 index 0000000..19c8965 --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/native_activity.rs @@ -0,0 +1,241 @@ +//! Bindings for [`ANativeActivity`] +//! +//! [`ANativeActivity`]: https://developer.android.com/ndk/reference/group/native-activity#anativeactivity + +use super::hardware_buffer_format::HardwareBufferFormat; +use std::{ + ffi::{CStr, OsStr}, + os::{raw::c_void, unix::prelude::OsStrExt}, + path::Path, + ptr::NonNull, +}; + +bitflags::bitflags! { + /// Window flags, as per the Java API at [`android.view.WindowManager.LayoutParams`]. + /// + /// + /// + /// [`android.view.WindowManager.LayoutParams`]: https://developer.android.com/reference/android/view/WindowManager.LayoutParams + #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] + pub struct WindowFlags : u32 { + const ALLOW_LOCK_WHILE_SCREEN_ON = ffi::AWINDOW_FLAG_ALLOW_LOCK_WHILE_SCREEN_ON; + const DIM_BEHIND = ffi::AWINDOW_FLAG_DIM_BEHIND; + #[deprecated = "Deprecated. Blurring is no longer supported."] + const BLUR_BEHIND = ffi::AWINDOW_FLAG_BLUR_BEHIND; + const NOT_FOCUSABLE = ffi::AWINDOW_FLAG_NOT_FOCUSABLE; + const NOT_TOUCHABLE = ffi::AWINDOW_FLAG_NOT_TOUCHABLE; + const NOT_TOUCH_MODAL = ffi::AWINDOW_FLAG_NOT_TOUCH_MODAL; + #[deprecated = "This constant was deprecated in API level 20. This flag has no effect."] + const TOUCHABLE_WHEN_WAKING = ffi::AWINDOW_FLAG_TOUCHABLE_WHEN_WAKING; + const KEEP_SCREEN_ON = ffi::AWINDOW_FLAG_KEEP_SCREEN_ON; + const LAYOUT_IN_SCREEN = ffi::AWINDOW_FLAG_LAYOUT_IN_SCREEN; + const LAYOUT_NO_LIMITS = ffi::AWINDOW_FLAG_LAYOUT_NO_LIMITS; + const FULLSCREEN = ffi::AWINDOW_FLAG_FULLSCREEN; + #[cfg_attr(feature = "api-level-30", deprecated = "This constant was deprecated in API level 30. This value became API \"by accident\", and shouldn't be used by 3rd party applications.")] + const FORCE_NOT_FULLSCREEN = ffi::AWINDOW_FLAG_FORCE_NOT_FULLSCREEN; + #[deprecated = "This constant was deprecated in API level 17. This flag is no longer used."] + const DITHER = ffi::AWINDOW_FLAG_DITHER; + const SECURE = ffi::AWINDOW_FLAG_SECURE; + const SCALED = ffi::AWINDOW_FLAG_SCALED; + const IGNORE_CHEEK_PRESSES = ffi::AWINDOW_FLAG_IGNORE_CHEEK_PRESSES; + const LAYOUT_INSET_DECOR = ffi::AWINDOW_FLAG_LAYOUT_INSET_DECOR; + const ALT_FOCUSABLE_IM = ffi::AWINDOW_FLAG_ALT_FOCUSABLE_IM; + const WATCH_OUTSIDE_TOUCH = ffi::AWINDOW_FLAG_WATCH_OUTSIDE_TOUCH; + const SHOW_WHEN_LOCKED = ffi::AWINDOW_FLAG_SHOW_WHEN_LOCKED; + const SHOW_WALLPAPER = ffi::AWINDOW_FLAG_SHOW_WALLPAPER; + const TURN_SCREEN_ON = ffi::AWINDOW_FLAG_TURN_SCREEN_ON; + #[cfg_attr(feature = "api-level-26", deprecated = "This constant was deprecated in API level 26. Use `SHOW_WHEN_LOCKED` instead.")] + const DISMISS_KEYGUARD = ffi::AWINDOW_FLAG_DISMISS_KEYGUARD; + const ATTACHED_IN_DECOR = 0x40000000; + + // https://docs.rs/bitflags/latest/bitflags/#externally-defined-flags + const _ = !0; + } +} + +/// A native [`ANativeActivity *`] +/// +/// This is either provided in [`ffi::ANativeActivity_onCreate()`], or accessible through +/// `ndk_glue::native_activity()`. +/// +/// [`ANativeActivity *`]: https://developer.android.com/ndk/reference/struct/a-native-activity +#[derive(Debug)] +pub struct NativeActivity { + ptr: NonNull, +} + +// It gets shared between threads in `ndk-glue` +unsafe impl Send for NativeActivity {} +unsafe impl Sync for NativeActivity {} + +impl NativeActivity { + /// Create a [`NativeActivity`] from a pointer + /// + /// # Safety + /// By calling this function, you assert that it is a valid pointer to a native + /// [`ffi::ANativeActivity`]. + pub unsafe fn from_ptr(ptr: NonNull) -> Self { + Self { ptr } + } + + /// The pointer to the native `ANativeActivity` + pub fn ptr(&self) -> NonNull { + self.ptr + } +} + +/// Methods that relate to fields of the struct itself +/// +/// The relevant NDK docs can be found +/// [here](https://developer.android.com/ndk/reference/struct/a-native-activity). +impl NativeActivity { + /// The platform's SDK version code + pub fn sdk_version(&self) -> i32 { + unsafe { self.ptr.as_ref().sdkVersion } + } + + /// Path to this application's internal data directory + pub fn internal_data_path(&self) -> &Path { + OsStr::from_bytes(unsafe { CStr::from_ptr(self.ptr.as_ref().internalDataPath) }.to_bytes()) + .as_ref() + } + + /// Path to this application's external (removable, mountable) data directory + pub fn external_data_path(&self) -> &Path { + OsStr::from_bytes(unsafe { CStr::from_ptr(self.ptr.as_ref().externalDataPath) }.to_bytes()) + .as_ref() + } + + /// This app's asset manager, which can be used to access assets from the `.apk` file. + pub fn asset_manager(&self) -> crate::asset::AssetManager { + unsafe { + crate::asset::AssetManager::from_ptr( + NonNull::new(self.ptr.as_ref().assetManager).unwrap(), + ) + } + } + + /// Instance data associated with the activity + pub fn instance(&self) -> *mut c_void { + unsafe { self.ptr.as_ref().instance } + } + + /// Set the instance data associated with the activity + /// + /// # Safety + /// This can invalidate assumptions held by `ndk-glue`, as well as cause data + /// races with concurrent access to the instance data. + pub unsafe fn set_instance(&mut self, data: *mut c_void) { + // FIXME Does this create undefined behavior by creating a mutable reference to what could + // also be accessed immutably at the same time? + // + // I think that as long as we warn the users to avoid concurrent access, and we pass along + // the `unsafe` burden, it's OK. + self.ptr.as_mut().instance = data; + } + + /// This process's `JavaVM` object. + /// + /// Usage with [__jni__](https://crates.io/crates/jni) crate: + /// ```no_run + /// # use ndk::native_activity::NativeActivity; + /// # let native_activity: NativeActivity = unimplemented!(); + /// let vm_ptr = native_activity.vm(); + /// let vm = unsafe { jni::JavaVM::from_raw(vm_ptr) }.unwrap(); + /// let env = vm.attach_current_thread(); + /// // Do JNI with env ... + /// ``` + pub fn vm(&self) -> *mut jni_sys::JavaVM { + unsafe { self.ptr.as_ref() }.vm + } + + /// The [`android.app.NativeActivity`] instance + /// + /// In the JNI, this is named `clazz`; however, as the docs say, "it should really be named + /// 'activity' instead of 'clazz', since it's a reference to the NativeActivity instance". + /// + /// [`android.app.NativeActivity`]: https://developer.android.com/reference/android/app/NativeActivity + pub fn activity(&self) -> jni_sys::jobject { + unsafe { self.ptr.as_ref() }.clazz + } + + /// Path to the directory with the application's OBB files. + /// + /// # Safety + /// Only available as of Honeycomb (Android 3.0+, API level 11+) + pub unsafe fn obb_path(&self) -> &Path { + OsStr::from_bytes(CStr::from_ptr(self.ptr.as_ref().obbPath).to_bytes()).as_ref() + } +} + +/// Methods that relate to `ANativeActivity_*` functions. +/// +/// The relevant NDK docs can be found +/// [here](https://developer.android.com/ndk/reference/group/native-activity). +impl NativeActivity { + /// Sends a destroy event to the activity and stops it. + pub fn finish(&self) { + unsafe { ffi::ANativeActivity_finish(self.ptr.as_ptr()) } + } + + /// Shows the IME (the on-screen keyboard). + /// + /// If `force` is true, the `SHOW_FORCED` flag is used; otherwise, the `SHOW_IMPLICIT` flag is + /// used. Depending on the value of this flag, the `hide_soft_input` method with behave + /// differently. See [the relevant + /// javadoc](https://developer.android.com/reference/android/view/inputmethod/InputMethodManager#constants_2) + /// for more information. + pub fn show_soft_input(&self, force: bool) { + let flag = if force { + ffi::ANATIVEACTIVITY_SHOW_SOFT_INPUT_FORCED + } else { + ffi::ANATIVEACTIVITY_SHOW_SOFT_INPUT_IMPLICIT + }; + unsafe { ffi::ANativeActivity_showSoftInput(self.ptr.as_ptr(), flag) } + } + + /// Hides the IME (the on-screen keyboard). + /// + /// If `not_always` is true, the `HIDE_NOT_ALWAYS` flag is used; otherwise, the + /// `HIDE_IMPLICIT_ONLY` flag is used. Depending on the value of this flag and the way the IME + /// was shown, it may or may not be hidden. See [the relevant + /// javadoc](https://developer.android.com/reference/android/view/inputmethod/InputMethodManager#constants_2) + /// for more information. + pub fn hide_soft_input(&self, not_always: bool) { + let flag = if not_always { + ffi::ANATIVEACTIVITY_HIDE_SOFT_INPUT_NOT_ALWAYS + } else { + ffi::ANATIVEACTIVITY_HIDE_SOFT_INPUT_IMPLICIT_ONLY + }; + unsafe { ffi::ANativeActivity_hideSoftInput(self.ptr.as_ptr(), flag) } + } + + /// Change the window format of the given activity. + /// + /// Calls [`getWindow().setFormat()`] of the given activity. Note that this method can be + /// called from any thread; it will send a message to the main thread of the process where the + /// Java finish call will take place. + /// + /// [`getWindow().setFormat()`]: https://developer.android.com/reference/android/view/Window#setFormat(int) + pub fn set_window_format(&self, format: HardwareBufferFormat) { + unsafe { ffi::ANativeActivity_setWindowFormat(self.ptr.as_ptr(), format.into()) } + } + + /// Change the window flags of the given activity. + /// + /// Calls [`getWindow().setFlags()`] of the given activity. + /// + /// Note that this method can be called from any thread; it will send a message to the main + /// thread of the process where the Java finish call will take place. + /// + /// [`getWindow().setFlags()`]: https://developer.android.com/reference/android/view/Window#setFlags(int,%20int) + pub fn set_window_flags(&self, add_flags: WindowFlags, remove_flags: WindowFlags) { + unsafe { + ffi::ANativeActivity_setWindowFlags( + self.ptr.as_ptr(), + add_flags.bits(), + remove_flags.bits(), + ) + } + } +} diff --git a/native/vendor/ndk-0.9.0/src/native_window.rs b/native/vendor/ndk-0.9.0/src/native_window.rs new file mode 100644 index 0000000..1a33903 --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/native_window.rs @@ -0,0 +1,459 @@ +//! Bindings for [`ANativeWindow`] +//! +//! [`ANativeWindow`]: https://developer.android.com/ndk/reference/group/a-native-window#anativewindow + +use std::{ffi::c_void, io, mem::MaybeUninit, ptr::NonNull}; + +use jni_sys::{jobject, JNIEnv}; + +use super::{hardware_buffer_format::HardwareBufferFormat, utils::status_to_io_result}; +#[cfg(all(feature = "nativewindow", feature = "api-level-28"))] +use crate::data_space::DataSpace; + +pub type Rect = ffi::ARect; + +// [`NativeWindow`] represents the producer end of an image queue +/// +/// It is the C counterpart of the [`android.view.Surface`] object in Java, and can be converted +/// both ways. Depending on the consumer, images submitted to [`NativeWindow`] can be shown on the +/// display or sent to other consumers, such as video encoders. +/// +/// [`android.view.Surface`]: https://developer.android.com/reference/android/view/Surface +#[derive(Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +pub struct NativeWindow { + ptr: NonNull, +} + +unsafe impl Send for NativeWindow {} +unsafe impl Sync for NativeWindow {} + +impl Drop for NativeWindow { + fn drop(&mut self) { + unsafe { ffi::ANativeWindow_release(self.ptr.as_ptr()) } + } +} + +impl Clone for NativeWindow { + fn clone(&self) -> Self { + unsafe { ffi::ANativeWindow_acquire(self.ptr.as_ptr()) } + Self { ptr: self.ptr } + } +} + +#[cfg(feature = "rwh_04")] +unsafe impl rwh_04::HasRawWindowHandle for NativeWindow { + fn raw_window_handle(&self) -> rwh_04::RawWindowHandle { + let mut handle = rwh_04::AndroidNdkHandle::empty(); + handle.a_native_window = self.ptr.as_ptr().cast(); + rwh_04::RawWindowHandle::AndroidNdk(handle) + } +} + +#[cfg(feature = "rwh_05")] +unsafe impl rwh_05::HasRawWindowHandle for NativeWindow { + fn raw_window_handle(&self) -> rwh_05::RawWindowHandle { + let mut handle = rwh_05::AndroidNdkWindowHandle::empty(); + handle.a_native_window = self.ptr.as_ptr().cast(); + rwh_05::RawWindowHandle::AndroidNdk(handle) + } +} + +#[cfg(feature = "rwh_06")] +impl rwh_06::HasWindowHandle for NativeWindow { + fn window_handle(&self) -> Result, rwh_06::HandleError> { + let handle = rwh_06::AndroidNdkWindowHandle::new(self.ptr.cast()); + let handle = rwh_06::RawWindowHandle::AndroidNdk(handle); + // SAFETY: All fields of the "raw" `AndroidNdkWindowHandle` struct are filled out. The + // returned pointer is also kept valid by `NativeWindow` (until `Drop`), which is lifetime- + // borrowed in the returned `WindowHandle<'_>` and cannot be outlived. Its value won't + // change throughout the lifetime of this `NativeWindow`. + Ok(unsafe { rwh_06::WindowHandle::borrow_raw(handle) }) + } +} + +impl NativeWindow { + /// Assumes ownership of `ptr` + /// + /// # Safety + /// `ptr` must be a valid pointer to an Android [`ffi::ANativeWindow`]. + pub unsafe fn from_ptr(ptr: NonNull) -> Self { + Self { ptr } + } + + /// Acquires ownership of `ptr` + /// + /// # Safety + /// `ptr` must be a valid pointer to an Android [`ffi::ANativeWindow`]. + pub unsafe fn clone_from_ptr(ptr: NonNull) -> Self { + ffi::ANativeWindow_acquire(ptr.as_ptr()); + Self::from_ptr(ptr) + } + + pub fn ptr(&self) -> NonNull { + self.ptr + } + + pub fn height(&self) -> i32 { + unsafe { ffi::ANativeWindow_getHeight(self.ptr.as_ptr()) } + } + + pub fn width(&self) -> i32 { + unsafe { ffi::ANativeWindow_getWidth(self.ptr.as_ptr()) } + } + + /// Return the current pixel format ([`HardwareBufferFormat`]) of the window surface. + pub fn format(&self) -> HardwareBufferFormat { + let value = unsafe { ffi::ANativeWindow_getFormat(self.ptr.as_ptr()) }; + value.into() + } + + /// Change the format and size of the window buffers. + /// + /// The width and height control the number of pixels in the buffers, not the dimensions of the + /// window on screen. If these are different than the window's physical size, then its buffer + /// will be scaled to match that size when compositing it to the screen. The width and height + /// must be either both zero or both non-zero. + /// + /// For all of these parameters, if `0` or [`None`] is supplied then the window's base value + /// will come back in force. + pub fn set_buffers_geometry( + &self, + width: i32, + height: i32, + format: Option, + ) -> io::Result<()> { + let format = format.map_or(0i32, |f| f.into()); + let status = unsafe { + ffi::ANativeWindow_setBuffersGeometry(self.ptr.as_ptr(), width, height, format) + }; + status_to_io_result(status) + } + + /// Set a transform that will be applied to future buffers posted to the window. + #[cfg(all(feature = "nativewindow", feature = "api-level-26"))] + #[doc(alias = "ANativeWindow_setBuffersTransform")] + pub fn set_buffers_transform(&self, transform: NativeWindowTransform) -> io::Result<()> { + let status = + unsafe { ffi::ANativeWindow_setBuffersTransform(self.ptr.as_ptr(), transform.bits()) }; + status_to_io_result(status) + } + + /// All buffers queued after this call will be associated with the dataSpace parameter + /// specified. + /// + /// `data_space` specifies additional information about the buffer. For example, it can be used + /// to convey the color space of the image data in the buffer, or it can be used to indicate + /// that the buffers contain depth measurement data instead of color images. The default + /// dataSpace is `0`, [`DataSpace::Unknown`], unless it has been overridden by the producer. + #[cfg(all(feature = "nativewindow", feature = "api-level-28"))] + #[doc(alias = "ANativeWindow_setBuffersDataSpace")] + pub fn set_buffers_data_space(&self, data_space: DataSpace) -> io::Result<()> { + let status = + unsafe { ffi::ANativeWindow_setBuffersDataSpace(self.ptr.as_ptr(), data_space.into()) }; + status_to_io_result(status) + } + + /// Get the dataspace of the buffers in this [`NativeWindow`]. + #[cfg(all(feature = "nativewindow", feature = "api-level-28"))] + #[doc(alias = "ANativeWindow_getBuffersDataSpace")] + pub fn buffers_data_space(&self) -> io::Result { + let status = unsafe { ffi::ANativeWindow_getBuffersDataSpace(self.ptr.as_ptr()) }; + if status >= 0 { + Ok(status.into()) + } else { + Err(status_to_io_result(status).unwrap_err()) + } + } + + /// Sets the intended frame rate for this window. + /// + /// Same as [`set_frame_rate_with_change_strategy(window, frame_rate, compatibility, ChangeFrameRateStrategy::OnlyIfSeamless)`][`NativeWindow::set_frame_rate_with_change_strategy()`]. + /// + #[cfg_attr( + not(feature = "api-level-31"), + doc = "[`NativeWindow::set_frame_rate_with_change_strategy()`]: https://developer.android.com/ndk/reference/group/a-native-window#anativewindow_setframeratewithchangestrategy" + )] + #[cfg(all(feature = "nativewindow", feature = "api-level-30"))] + #[doc(alias = "ANativeWindow_setFrameRate")] + pub fn set_frame_rate( + &self, + frame_rate: f32, + compatibility: FrameRateCompatibility, + ) -> io::Result<()> { + let status = unsafe { + ffi::ANativeWindow_setFrameRate(self.ptr.as_ptr(), frame_rate, compatibility as i8) + }; + status_to_io_result(status) + } + + /// Sets the intended frame rate for this window. + /// + /// On devices that are capable of running the display at different refresh rates, the system + /// may choose a display refresh rate to better match this window's frame rate. Usage of this + /// API won't introduce frame rate throttling, or affect other aspects of the application's + /// frame production pipeline. However, because the system may change the display refresh rate, + /// calls to this function may result in changes to Choreographer callback timings, and changes + /// to the time interval at which the system releases buffers back to the application. + /// + /// Note that this only has an effect for windows presented on the display. If this + /// [`NativeWindow`] is consumed by something other than the system compositor, e.g. a media + /// codec, this call has no effect. + /// + /// You can register for changes in the refresh rate using + /// [`ffi::AChoreographer_registerRefreshRateCallback()`]. + /// + /// # Parameters + /// + /// - `frame_rate`: The intended frame rate of this window, in frames per second. `0` is a + /// special value that indicates the app will accept the system's choice for the display + /// frame rate, which is the default behavior if this function isn't called. The `frame_rate` + /// param does not need to be a valid refresh rate for this device's display - e.g., it's + /// fine to pass `30`fps to a device that can only run the display at `60`fps. + /// - `compatibility`: The frame rate compatibility of this window. The compatibility value may + /// influence the system's choice of display refresh rate. See the [`FrameRateCompatibility`] + /// values for more info. This parameter is ignored when `frame_rate` is `0`. + /// - `change_frame_rate_strategy`: Whether display refresh rate transitions caused by this + /// window should be seamless. A seamless transition is one that doesn't have any visual + /// interruptions, such as a black screen for a second or two. See the + /// [`ChangeFrameRateStrategy`] values. This parameter is ignored when `frame_rate` is `0`. + #[cfg(all(feature = "nativewindow", feature = "api-level-31"))] + #[doc(alias = "ANativeWindow_setFrameRateWithChangeStrategy")] + pub fn set_frame_rate_with_change_strategy( + &self, + frame_rate: f32, + compatibility: FrameRateCompatibility, + change_frame_rate_strategy: ChangeFrameRateStrategy, + ) -> io::Result<()> { + let status = unsafe { + ffi::ANativeWindow_setFrameRateWithChangeStrategy( + self.ptr.as_ptr(), + frame_rate, + compatibility as i8, + change_frame_rate_strategy as i8, + ) + }; + status_to_io_result(status) + } + + /// Provides a hint to the window that buffers should be preallocated ahead of time. + /// + /// Note that the window implementation is not guaranteed to preallocate any buffers, for + /// instance if an implementation disallows allocation of new buffers, or if there is + /// insufficient memory in the system to preallocate additional buffers + #[cfg(all(feature = "nativewindow", feature = "api-level-30"))] + pub fn try_allocate_buffers(&self) { + unsafe { ffi::ANativeWindow_tryAllocateBuffers(self.ptr.as_ptr()) } + } + + /// Return the [`NativeWindow`] associated with a JNI [`android.view.Surface`] pointer. + /// + /// # Safety + /// By calling this function, you assert that `env` is a valid pointer to a [`JNIEnv`] and + /// `surface` is a valid pointer to an [`android.view.Surface`]. + /// + /// [`android.view.Surface`]: https://developer.android.com/reference/android/view/Surface + pub unsafe fn from_surface(env: *mut JNIEnv, surface: jobject) -> Option { + let ptr = ffi::ANativeWindow_fromSurface(env, surface); + Some(Self::from_ptr(NonNull::new(ptr)?)) + } + + /// Return a JNI [`android.view.Surface`] pointer derived from this [`NativeWindow`]. + /// + /// # Safety + /// By calling this function, you assert that `env` is a valid pointer to a [`JNIEnv`]. + /// + /// [`android.view.Surface`]: https://developer.android.com/reference/android/view/Surface + #[cfg(feature = "api-level-26")] + pub unsafe fn to_surface(&self, env: *mut JNIEnv) -> jobject { + ffi::ANativeWindow_toSurface(env, self.ptr().as_ptr()) + } + + /// Lock the window's next drawing surface for writing. + /// + /// Optionally pass the region you intend to draw into `dirty_bounds`. When this function + /// returns it is updated (commonly enlarged) with the actual area the caller needs to redraw. + pub fn lock( + &self, + dirty_bounds: Option<&mut Rect>, + ) -> io::Result> { + let dirty_bounds = match dirty_bounds { + Some(dirty_bounds) => dirty_bounds, + None => std::ptr::null_mut(), + }; + let mut buffer = MaybeUninit::uninit(); + let status = unsafe { + ffi::ANativeWindow_lock(self.ptr.as_ptr(), buffer.as_mut_ptr(), dirty_bounds) + }; + status_to_io_result(status)?; + + Ok(NativeWindowBufferLockGuard { + window: self, + buffer: unsafe { buffer.assume_init() }, + }) + } +} + +/// Lock holding the next drawing surface for writing. It is unlocked and posted on [`drop()`]. +#[derive(Debug)] +pub struct NativeWindowBufferLockGuard<'a> { + window: &'a NativeWindow, + buffer: ffi::ANativeWindow_Buffer, +} + +impl<'a> NativeWindowBufferLockGuard<'a> { + /// The number of pixels that are shown horizontally. + pub fn width(&self) -> usize { + usize::try_from(self.buffer.width).unwrap() + } + + // The number of pixels that are shown vertically. + pub fn height(&self) -> usize { + usize::try_from(self.buffer.height).unwrap() + } + + /// The number of _pixels_ that a line in the buffer takes in memory. + /// + /// This may be `>= width`. + pub fn stride(&self) -> usize { + usize::try_from(self.buffer.stride).unwrap() + } + + /// The format of the buffer. One of [`HardwareBufferFormat`]. + pub fn format(&self) -> HardwareBufferFormat { + self.buffer.format.into() + } + + /// The actual bits. + /// + /// This points to a memory segment of [`stride()`][Self::stride()] * + /// [`height()`][Self::height()] * [`HardwareBufferFormat::bytes_per_pixel()`] bytes. + /// + /// Only [`width()`][Self::width()] pixels are visible for each [`stride()`][Self::stride()] + /// line of pixels in the buffer. + /// + /// See [`bytes()`][Self::bytes()] for safe access to these bytes. + pub fn bits(&mut self) -> *mut c_void { + self.buffer.bits + } + + /// Safe write access to likely uninitialized pixel buffer data. + /// + /// Returns [`None`] when there is no [`HardwareBufferFormat::bytes_per_pixel()`] size + /// available for this [`format()`][Self::format()]. + /// + /// The returned slice consists of [`stride()`][Self::stride()] * [`height()`][Self::height()] + /// \* [`HardwareBufferFormat::bytes_per_pixel()`] bytes. + /// + /// Only [`width()`][Self::width()] pixels are visible for each [`stride()`][Self::stride()] + /// line of pixels in the buffer. + pub fn bytes(&mut self) -> Option<&mut [MaybeUninit]> { + let num_pixels = self.stride() * self.height(); + let num_bytes = num_pixels * self.format().bytes_per_pixel()?; + Some(unsafe { std::slice::from_raw_parts_mut(self.bits().cast(), num_bytes) }) + } + + /// Returns a slice of bytes for each line of visible pixels in the buffer, ignoring any + /// padding pixels incurred by the stride. + /// + /// See [`bits()`][Self::bits()] and [`bytes()`][Self::bytes()] for contiguous access to the + /// underlying buffer. + pub fn lines(&mut self) -> Option]>> { + let bpp = self.format().bytes_per_pixel()?; + let scanline_bytes = bpp * self.stride(); + let width_bytes = bpp * self.width(); + let bytes = self.bytes()?; + + Some( + bytes + .chunks_exact_mut(scanline_bytes) + .map(move |scanline| &mut scanline[..width_bytes]), + ) + } +} + +impl<'a> Drop for NativeWindowBufferLockGuard<'a> { + fn drop(&mut self) { + let ret = unsafe { ffi::ANativeWindow_unlockAndPost(self.window.ptr.as_ptr()) }; + assert_eq!(ret, 0); + } +} + +#[cfg(all(feature = "nativewindow", feature = "api-level-26"))] +bitflags::bitflags! { + /// Transforms that can be applied to buffers as they are displayed to a window. + /// + /// Supported transforms are any combination of horizontal mirror, vertical mirror, and + /// clockwise 90 degree rotation, in that order. Rotations of 180 and 270 degrees are made up + /// of those basic transforms. + #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] + #[doc(alias = "ANativeWindowTransform")] + pub struct NativeWindowTransform : i32 { + #[doc(alias = "ANATIVEWINDOW_TRANSFORM_IDENTITY")] + const IDENTITY = ffi::ANativeWindowTransform::ANATIVEWINDOW_TRANSFORM_IDENTITY.0 as i32; + #[doc(alias = "ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL")] + const MIRROR_HORIZONTAL = ffi::ANativeWindowTransform::ANATIVEWINDOW_TRANSFORM_MIRROR_HORIZONTAL.0 as i32; + #[doc(alias = "ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL")] + const MIRROR_VERTICAL = ffi::ANativeWindowTransform::ANATIVEWINDOW_TRANSFORM_MIRROR_VERTICAL.0 as i32; + #[doc(alias = "ANATIVEWINDOW_TRANSFORM_ROTATE_90")] + const ROTATE_90 = ffi::ANativeWindowTransform::ANATIVEWINDOW_TRANSFORM_ROTATE_90.0 as i32; + /// Defined as [`Self::MIRROR_HORIZONTAL`] `|` [`Self::MIRROR_VERTICAL`]. + #[doc(alias = "ANATIVEWINDOW_TRANSFORM_ROTATE_180")] + const ROTATE_180 = ffi::ANativeWindowTransform::ANATIVEWINDOW_TRANSFORM_ROTATE_180.0 as i32; + /// Defined as [`Self::ROTATE_180`] `|` [`Self::ROTATE_90`]. + #[doc(alias = "ANATIVEWINDOW_TRANSFORM_ROTATE_270")] + const ROTATE_270 = ffi::ANativeWindowTransform::ANATIVEWINDOW_TRANSFORM_ROTATE_270.0 as i32; + + // https://docs.rs/bitflags/latest/bitflags/#externally-defined-flags + const _ = !0; + } +} + +/// Compatibility value for [`NativeWindow::set_frame_rate()`] +#[cfg_attr( + feature = "api-level-31", + doc = " and [`NativeWindow::set_frame_rate_with_change_strategy()`]" +)] +/// . +#[cfg(all(feature = "nativewindow", feature = "api-level-30"))] +#[repr(i8)] +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] +#[doc(alias = "ANativeWindow_FrameRateCompatibility")] +#[non_exhaustive] +pub enum FrameRateCompatibility { + /// There are no inherent restrictions on the frame rate of this window. + /// + /// When the system selects a frame rate other than what the app requested, the app will be + /// able to run at the system frame rate without requiring pull down. This value should be used + /// when displaying game content, UIs, and anything that isn't video. + #[doc(alias = "ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT")] + Default = + ffi::ANativeWindow_FrameRateCompatibility::ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT.0 as i8, + /// This window is being used to display content with an inherently fixed frame rate, e.g. a + /// video that has a specific frame rate. + /// + /// When the system selects a frame rate other than what the app requested, the app will need + /// to do pull down or use some other technique to adapt to the system's frame rate. The user + /// experience is likely to be worse (e.g. more frame stuttering) than it would be if the + /// system had chosen the app's requested frame rate. This value should be used for video + /// content. + #[doc(alias = "ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE")] + FixedSource = ffi::ANativeWindow_FrameRateCompatibility::ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE.0 as i8, +} + +/// Change frame rate strategy value for [`NativeWindow::set_frame_rate_with_change_strategy()`]. +#[cfg(all(feature = "nativewindow", feature = "api-level-31"))] +#[repr(i8)] +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] +#[doc(alias = "ANativeWindow_ChangeFrameRateStrategy")] +#[non_exhaustive] +pub enum ChangeFrameRateStrategy { + /// Change the frame rate only if the transition is going to be seamless. + #[doc(alias = "ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS")] + OnlyIfSeamless = + ffi::ANativeWindow_ChangeFrameRateStrategy::ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS + .0 as i8, + /// Change the frame rate even if the transition is going to be non-seamless, i.e. with visual interruptions for the user. + #[doc(alias = "ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS")] + Always = + ffi::ANativeWindow_ChangeFrameRateStrategy::ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS.0 as i8, +} diff --git a/native/vendor/ndk-0.9.0/src/shared_memory.rs b/native/vendor/ndk-0.9.0/src/shared_memory.rs new file mode 100644 index 0000000..20182f9 --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/shared_memory.rs @@ -0,0 +1,153 @@ +//! Bindings for [`ASharedMemory`] +//! +//! [`ASharedMemory`]: https://developer.android.com/ndk/reference/group/memory +#![cfg(feature = "api-level-26")] + +use std::{ + ffi::CStr, + io::{Error, Result}, + // TODO: Import from std::os::fd::{} since Rust 1.66 + os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd}, + ptr, +}; + +#[cfg(feature = "api-level-27")] +use jni_sys::{jobject, JNIEnv}; + +/// Enables the creation, mapping, and protection control over anonymous shared memory. +#[derive(Debug)] +#[doc(alias = "ASharedMemory")] +pub struct SharedMemory(OwnedFd); + +impl SharedMemory { + /// Create a shared memory region. + /// + /// Creates shared memory region and returns a file descriptor. The resulting file descriptor + /// can be `mmap`'ed to process memory space with `PROT_READ | PROT_WRITE | PROT_EXEC`. Access + /// to this shared memory region can be restricted with [`set_prot()`][Self::set_prot()]. + /// + /// Use [`android.os.ParcelFileDescriptor`] to pass the file descriptor to another process. + /// File descriptors may also be sent to other processes over a Unix domain socket with + /// `sendmsg` and `SCM_RIGHTS`. See `sendmsg(3)` and `cmsg(3)` man pages for more information. + /// + /// If you intend to share this file descriptor with a child process after calling `exec(3)`, + /// note that you will need to use `fcntl(2)` with `F_SETFD` to clear the `FD_CLOEXEC` flag for + /// this to work on all versions of Android. + /// + /// [`android.os.ParcelFileDescriptor`]: https://developer.android.com/reference/android/os/ParcelFileDescriptor + #[doc(alias = "ASharedMemory_create")] + pub fn create(name: Option<&CStr>, size: usize) -> Result { + let fd = + unsafe { ffi::ASharedMemory_create(name.map_or(ptr::null(), |p| p.as_ptr()), size) }; + if fd < 0 { + Err(Error::last_os_error()) + } else { + Ok(unsafe { Self::from_raw_fd(fd) }) + } + } + + /// Returns a `dup`'d FD from the given Java [`android.os.SharedMemory`] object. + /// + /// The returned file descriptor has all the same properties & capabilities as the FD returned + /// from [`create()`][Self::create()], however the protection flags will be the same as those + /// of the [`android.os.SharedMemory`] object. + /// + /// [`android.os.SharedMemory`]: https://developer.android.com/reference/android/os/SharedMemory + #[cfg(feature = "api-level-27")] + #[doc(alias = "ASharedMemory_dupFromJava")] + #[allow(clippy::not_unsafe_ptr_arg_deref)] + pub fn dup_from_java(env: *mut JNIEnv, shared_memory: jobject) -> Result { + let fd = unsafe { ffi::ASharedMemory_dupFromJava(env, shared_memory) }; + if fd < 0 { + Err(Error::last_os_error()) + } else { + Ok(unsafe { Self::from_raw_fd(fd) }) + } + } + + /// Get the size of the shared memory region. + #[doc(alias = "ASharedMemory_getSize")] + pub fn size(&self) -> usize { + unsafe { ffi::ASharedMemory_getSize(self.as_raw_fd()) } + } + + /// Restrict access of shared memory region. + /// + /// This function restricts access of a shared memory region. Access can only be removed. The + /// effect applies globally to all file descriptors in all processes across the system that + /// refer to this shared memory region. Existing memory mapped regions are not affected. + /// + /// It is a common use case to create a shared memory region, map it read/write locally to + /// initialize content, and then send the shared memory to another process with read only + /// access. Code example as below: + /// + /// ```no_run + /// # use ndk::shared_memory::SharedMemory; + /// # // TODO: Import from std::os::fd::{} since Rust 1.66 + /// # use std::os::unix::io::AsRawFd; + /// # use std::ffi::CStr; + /// # unsafe { + /// let mem = SharedMemory::create(Some(CStr::from_bytes_with_nul_unchecked(b"memory\0")), 127).unwrap(); + /// // By default it has PROT_READ | PROT_WRITE | PROT_EXEC. + /// let size = mem.size(); + /// let buffer = libc::mmap( + /// std::ptr::null_mut(), + /// size, + /// libc::PROT_READ | libc::PROT_WRITE, + /// libc::MAP_SHARED, + /// mem.as_raw_fd(), + /// 0, + /// ); + /// let buffer_slice = std::slice::from_raw_parts_mut(buffer.cast(), size); + /// + /// // trivially initialize content + /// buffer_slice[..7].copy_from_slice(b"hello!\0"); + /// + /// // Existing mappings will retain their protection flags (PROT_WRITE here) after set_prod() + /// // unless it is unmapped: + /// libc::munmap(buffer, size); + /// + /// // limit access to read only + /// mem.set_prot(libc::PROT_READ); + /// + /// // share fd with another process here and the other process can only map with PROT_READ. + /// # } + /// ``` + #[doc(alias = "ASharedMemory_setProt")] + pub fn set_prot(&self, prot: i32) -> Result<()> { + let status = unsafe { ffi::ASharedMemory_setProt(self.as_raw_fd(), prot) }; + if status < 0 { + Err(Error::last_os_error()) + } else { + Ok(()) + } + } +} + +impl AsFd for SharedMemory { + fn as_fd(&self) -> BorrowedFd<'_> { + self.0.as_fd() + } +} + +impl AsRawFd for SharedMemory { + fn as_raw_fd(&self) -> RawFd { + self.0.as_raw_fd() + } +} + +impl IntoRawFd for SharedMemory { + fn into_raw_fd(self) -> RawFd { + self.0.into_raw_fd() + } +} + +impl FromRawFd for SharedMemory { + /// # Safety + /// + /// The resource pointed to by `fd` must be open and suitable for assuming + /// ownership. The resource must not require any cleanup other than `close`. + unsafe fn from_raw_fd(fd: RawFd) -> Self { + Self(OwnedFd::from_raw_fd(fd)) + } +} diff --git a/native/vendor/ndk-0.9.0/src/surface_texture.rs b/native/vendor/ndk-0.9.0/src/surface_texture.rs new file mode 100644 index 0000000..d38ffd7 --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/surface_texture.rs @@ -0,0 +1,159 @@ +//! Bindings for [`ASurfaceTexture`] +//! +//! See for an architectural overview of +//! [`SurfaceTexture`] internals. +//! +//! [`ASurfaceTexture`]: https://developer.android.com/ndk/reference/group/surface-texture +#![cfg(feature = "api-level-28")] + +use crate::{native_window::NativeWindow, utils::status_to_io_result}; +use jni_sys::{jobject, JNIEnv}; +use std::{io::Result, ptr::NonNull, time::Duration}; + +/// An opaque type to manage [`android.graphics.SurfaceTexture`] from native code +/// +/// [`android.graphics.SurfaceTexture`]: https://developer.android.com/reference/android/graphics/SurfaceTexture +#[derive(Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +pub struct SurfaceTexture { + ptr: NonNull, +} + +unsafe impl Send for SurfaceTexture {} + +impl Drop for SurfaceTexture { + fn drop(&mut self) { + unsafe { ffi::ASurfaceTexture_release(self.ptr.as_ptr()) } + } +} + +impl SurfaceTexture { + /// Assumes ownership of `ptr` + /// + /// # Safety + /// `ptr` must be a valid pointer to an Android [`ffi::ASurfaceTexture`]. + pub unsafe fn from_ptr(ptr: NonNull) -> Self { + Self { ptr } + } + + /// Get a reference to the native [`SurfaceTexture`] from the corresponding Java object. + /// + /// # Safety + /// + /// This function should be called with a healthy JVM pointer and with a non-null + /// [`android.graphics.SurfaceTexture`], which must be kept alive on the Java/Kotlin side. + /// + /// The caller must keep a reference to the Java [`android.graphics.SurfaceTexture`] during the + /// lifetime of the returned [`SurfaceTexture`]. Failing to do so could result in the + /// [`SurfaceTexture`] to stop functioning properly once the Java object gets finalized. + /// However, this will not result in program termination. + /// + /// [`android.graphics.SurfaceTexture`]: https://developer.android.com/reference/android/graphics/SurfaceTexture + pub unsafe fn from_surface_texture(env: *mut JNIEnv, surface_texture: jobject) -> Option { + let a_surface_texture_ptr = ffi::ASurfaceTexture_fromSurfaceTexture(env, surface_texture); + let s = NonNull::new(a_surface_texture_ptr)?; + Some(SurfaceTexture::from_ptr(s)) + } + + /// Returns a pointer to the native [`ffi::ASurfaceTexture`]. + pub fn ptr(&self) -> NonNull { + self.ptr + } + + /// Returns a reference to a [`NativeWindow`] (i.e. the Producer) for this [`SurfaceTexture`]. + /// + /// This is equivalent to Java's: + /// ```java + /// Surface sur = new Surface(surfaceTexture); + /// ``` + pub fn acquire_native_window(&self) -> Option { + let native_window = unsafe { ffi::ASurfaceTexture_acquireANativeWindow(self.ptr.as_ptr()) }; + let n = NonNull::new(native_window)?; + Some(unsafe { NativeWindow::from_ptr(n) }) + } + + /// Attach the [`SurfaceTexture`] to the OpenGL ES context that is current on the calling + /// thread. + /// + /// A new OpenGL ES texture object is created and populated with the [`SurfaceTexture`] image + /// frame that was current at the time of the last call to + /// [`detach_from_gl_context()`][Self::detach_from_gl_context()]. This new texture is bound to + /// the `GL_TEXTURE_EXTERNAL_OES` texture target. + /// + /// This can be used to access the [`SurfaceTexture`] image contents from multiple OpenGL ES + /// contexts. Note, however, that the image contents are only accessible from one OpenGL ES + /// context at a time. + pub fn attach_to_gl_context(&self, tex_name: u32) -> Result<()> { + let status = unsafe { ffi::ASurfaceTexture_attachToGLContext(self.ptr.as_ptr(), tex_name) }; + status_to_io_result(status) + } + + /// Detach the [`SurfaceTexture`] from the OpenGL ES context that owns the OpenGL ES texture + /// object. + /// + /// This call must be made with the OpenGL ES context current on the calling thread. The OpenGL + /// ES texture object will be deleted as a result of this call. After calling this method all + /// calls to [`update_tex_image()`][Self::update_tex_image()] will fail until a successful call + /// to [`attach_to_gl_context()`][Self::attach_to_gl_context()] is made. + /// + /// This can be used to access the [`SurfaceTexture`] image contents from multiple OpenGL ES + /// contexts. Note, however, that the image contents are only accessible from one OpenGL ES + /// context at a time. + pub fn detach_from_gl_context(&self) -> Result<()> { + let status = unsafe { ffi::ASurfaceTexture_detachFromGLContext(self.ptr.as_ptr()) }; + status_to_io_result(status) + } + + /// Retrieve the 4x4 texture coordinate transform matrix associated with the texture image set + /// by the most recent call to [`update_tex_image()`][Self::update_tex_image()]. + /// + /// This transform matrix maps 2D homogeneous texture coordinates of the form `(s, t, 0, 1)` + /// with `s` and `t` in the inclusive range `[0, 1]` to the texture coordinate that should be + /// used to sample that location from the texture. Sampling the texture outside of the range of + /// this transform is undefined. + /// + /// The matrix is stored in column-major order so that it may be passed directly to OpenGL ES + /// via the [`glLoadMatrixf()`] or [`glUniformMatrix4fv()`] functions. + /// + /// [`glLoadMatrixf()`]: https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glLoadMatrix.xml + /// [`gluniformmatrix4fv()`]: https://www.khronos.org/registry/OpenGL-Refpages/es3.1/html/glUniform.xhtml + pub fn transform_matrix(&self) -> [f32; 16] { + let mut r = [0f32; 16]; + unsafe { ffi::ASurfaceTexture_getTransformMatrix(self.ptr.as_ptr(), r.as_mut_ptr()) }; + r + } + + /// Retrieve the timestamp associated with the texture image set by the most recent call to + /// [`update_tex_image()`][Self::update_tex_image()]. + /// + /// This timestamp is in nanoseconds, and is normally monotonically increasing. The timestamp + /// should be unaffected by time-of-day adjustments, and for a camera should be strictly + /// monotonic but for a [`MediaPlayer`] may be reset when the position is set. The specific + /// meaning and zero point of the timestamp depends on the source providing images to the + /// [`SurfaceTexture`]. Unless otherwise specified by the image source, timestamps cannot + /// generally be compared across [`SurfaceTexture`] instances, or across multiple program + /// invocations. It is mostly useful for determining time offsets between subsequent frames. + /// + /// For EGL/Vulkan producers, this timestamp is the desired present time set with the + /// [`EGL_ANDROID_presentation_time`] or [`VK_GOOGLE_display_timing`] extensions. + /// + /// [`MediaPlayer`]: https://developer.android.com/reference/android/media/MediaPlayer + /// [`EGL_ANDROID_presentation_time`]: https://www.khronos.org/registry/EGL/extensions/ANDROID/EGL_ANDROID_presentation_time.txt + /// [`VK_GOOGLE_display_timing`]: https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VK_GOOGLE_display_timing.html + pub fn timestamp(&self) -> Duration { + Duration::from_nanos( + unsafe { ffi::ASurfaceTexture_getTimestamp(self.ptr.as_ptr()) } + .try_into() + .unwrap(), + ) + } + + /// Update the texture image to the most recent frame from the image stream. + /// + /// This may only be called while the OpenGL ES context that owns the texture is current on the + /// calling thread. It will implicitly bind its texture to the `GL_TEXTURE_EXTERNAL_OES` + /// texture target. + pub fn update_tex_image(&self) -> Result<()> { + let status = unsafe { ffi::ASurfaceTexture_updateTexImage(self.ptr.as_ptr()) }; + status_to_io_result(status) + } +} diff --git a/native/vendor/ndk-0.9.0/src/sync.rs b/native/vendor/ndk-0.9.0/src/sync.rs new file mode 100644 index 0000000..b17aff4 --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/sync.rs @@ -0,0 +1,143 @@ +//! Bindings for [sync functions] +//! +//! [sync functions]: https://developer.android.com/ndk/reference/group/sync +#![cfg(feature = "sync")] + +use std::{ + ffi::CStr, + fmt::Debug, + // TODO: Import from std::os::fd::{} since Rust 1.66 + os::unix::io::{AsRawFd, BorrowedFd, FromRawFd, OwnedFd}, + ptr::NonNull, +}; + +#[doc(alias = "sync_file_info")] +#[repr(transparent)] +pub struct SyncFileInfo { + inner: NonNull, +} + +impl Debug for SyncFileInfo { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("SyncFileInfo") + .field("name", &self.name()) + .field("status", &self.status()) + .field("flags", &self.flags()) + .field("num_fences", &self.num_fences()) + .field("fence_info", &self.fence_info()) + .finish() + } +} + +impl SyncFileInfo { + /// Retrieve detailed information about a sync file and its fences. + #[doc(alias = "sync_file_info")] + pub fn new(fd: BorrowedFd<'_>) -> Option { + let inner = NonNull::new(unsafe { ffi::sync_file_info(fd.as_raw_fd()) })?; + Some(Self { inner }) + } + + pub fn name(&self) -> &CStr { + let inner = unsafe { self.inner.as_ref() }; + // TODO: Switch to CStr::from_bytes_until_nul (with c_char -> u8 transmute) since MSRV 1.69 + // https://github.com/ash-rs/ash/pull/746 + unsafe { CStr::from_ptr(inner.name.as_ptr()) } + } + + pub fn status(&self) -> i32 { + let inner = unsafe { self.inner.as_ref() }; + inner.status + } + + pub fn flags(&self) -> u32 { + let inner = unsafe { self.inner.as_ref() }; + inner.flags + } + + pub fn num_fences(&self) -> usize { + let inner = unsafe { self.inner.as_ref() }; + inner.num_fences as usize + } + + /// Get the array of fence infos from the sync file's info. + #[doc(alias = "sync_get_fence_info")] + pub fn fence_info(&self) -> &[SyncFenceInfo] { + let inner = unsafe { self.inner.as_ref() }; + + if inner.num_fences == 0 { + &[] + } else { + let sync_fence_info = NonNull::new(inner.sync_fence_info as *mut _) + .expect("sync_fence_info cannot be null if num_fences > 0"); + unsafe { + std::slice::from_raw_parts(sync_fence_info.as_ptr(), inner.num_fences as usize) + } + } + } +} + +impl Drop for SyncFileInfo { + /// Free a [`struct@ffi::sync_file_info`] structure. + #[doc(alias = "sync_file_info_free")] + fn drop(&mut self) { + unsafe { ffi::sync_file_info_free(self.inner.as_ptr()) } + } +} + +#[doc(alias = "sync_fence_info")] +#[repr(transparent)] +pub struct SyncFenceInfo(ffi::sync_fence_info); + +impl Debug for SyncFenceInfo { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("SyncFenceInfo") + .field("obj_name", &self.obj_name()) + .field("driver_name", &self.driver_name()) + .field("status", &self.status()) + .field("flags", &self.flags()) + .field("timestamp_ns", &self.timestamp_ns()) + .finish() + } +} + +impl SyncFenceInfo { + pub fn obj_name(&self) -> &CStr { + // TODO: Switch to CStr::from_bytes_until_nul (with c_char -> u8 transmute) since MSRV 1.69 + unsafe { CStr::from_ptr(self.0.obj_name.as_ptr()) } + } + + pub fn driver_name(&self) -> &CStr { + // TODO: Switch to CStr::from_bytes_until_nul (with c_char -> u8 transmute) since MSRV 1.69 + unsafe { CStr::from_ptr(self.0.driver_name.as_ptr()) } + } + + pub fn status(&self) -> i32 { + self.0.status + } + + pub fn flags(&self) -> u32 { + self.0.flags + } + + pub fn timestamp_ns(&self) -> u64 { + self.0.timestamp_ns + } +} + +/// Merge two sync files. +/// +/// This produces a new sync file with the given name which has the union of the two original sync +/// file's fences; redundant fences may be removed. +/// +/// If one of the input sync files is signaled or invalid, then this function may behave like +/// `dup()`: the new file descriptor refers to the valid/unsignaled sync file with its original +/// name, rather than a new sync file. +pub fn sync_merge(name: &CStr, fd1: BorrowedFd<'_>, fd2: BorrowedFd<'_>) -> OwnedFd { + unsafe { + OwnedFd::from_raw_fd(ffi::sync_merge( + name.as_ptr(), + fd1.as_raw_fd(), + fd2.as_raw_fd(), + )) + } +} diff --git a/native/vendor/ndk-0.9.0/src/trace.rs b/native/vendor/ndk-0.9.0/src/trace.rs new file mode 100644 index 0000000..8c6cd6f --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/trace.rs @@ -0,0 +1,92 @@ +//! Bindings for the NDK tracing API. +//! +//! See also [the NDK docs](https://developer.android.com/ndk/reference/group/tracing) +#![cfg(feature = "api-level-23")] +use std::ffi::{CString, NulError}; +use std::marker::PhantomData; + +pub fn is_trace_enabled() -> bool { + unsafe { ffi::ATrace_isEnabled() } +} + +#[derive(Debug)] +pub struct Section { + // Section is !Sync and !Send + _pd: PhantomData<*mut ()>, +} + +impl Section { + pub fn new(name: &str) -> Result { + let section_name = CString::new(name)?; + unsafe { ffi::ATrace_beginSection(section_name.as_ptr()) }; + + Ok(Self { _pd: PhantomData }) + } + + pub fn end(self) { + drop(self) + } +} + +impl Drop for Section { + fn drop(&mut self) { + unsafe { ffi::ATrace_endSection() }; + } +} + +/// Unique identifier for distinguishing simultaneous events +#[derive(Debug)] +#[cfg(feature = "api-level-29")] +pub struct Cookie(pub i32); + +#[derive(Debug)] +#[cfg(feature = "api-level-29")] +pub struct AsyncSection { + section_name: CString, + cookie: Cookie, + // AsyncSection is !Sync + _pd: PhantomData<&'static ()>, +} + +#[cfg(feature = "api-level-29")] +impl AsyncSection { + pub fn new(name: &str, cookie: Cookie) -> Result { + let section_name = CString::new(name)?; + unsafe { ffi::ATrace_beginAsyncSection(section_name.as_ptr(), cookie.0) }; + + Ok(Self { + section_name, + cookie, + _pd: PhantomData, + }) + } + + pub fn end(self) { + drop(self) + } +} + +#[cfg(feature = "api-level-29")] +impl Drop for AsyncSection { + fn drop(&mut self) { + unsafe { ffi::ATrace_endAsyncSection(self.section_name.as_ptr(), self.cookie.0) }; + } +} + +#[cfg(feature = "api-level-29")] +#[derive(Debug)] +pub struct Counter { + name: CString, +} + +#[cfg(feature = "api-level-29")] +impl Counter { + pub fn new(name: &str) -> Result { + let name = CString::new(name)?; + Ok(Self { name }) + } + + pub fn set_value(&self, value: i64) { + unsafe { ffi::ATrace_setCounter(self.name.as_ptr(), value) } + } +} diff --git a/native/vendor/ndk-0.9.0/src/utils.rs b/native/vendor/ndk-0.9.0/src/utils.rs new file mode 100644 index 0000000..fe1ec5b --- /dev/null +++ b/native/vendor/ndk-0.9.0/src/utils.rs @@ -0,0 +1,70 @@ +//! Internal utilities +use log::{error, log_enabled, Level}; +use std::ffi::{c_int, CStr, CString}; +use std::io::{Error, Result}; + +/// Turns standard `` status codes - typically rewrapped by Android's [`Errors.h`] - into +/// Rust's [`std::io::Error`]. +/// +/// [`Errors.h`]: https://cs.android.com/android/platform/superproject/+/master:system/core/libutils/include/utils/Errors.h +pub(crate) fn status_to_io_result(status: i32) -> Result<()> { + match status { + 0 => Ok(()), + r if r < 0 => Err(Error::from_raw_os_error(-r)), + r => unreachable!("Status is positive integer {}", r), + } +} + +pub(crate) fn android_log(level: Level, tag: &CStr, msg: &CStr) { + let prio = match level { + Level::Error => ffi::android_LogPriority::ANDROID_LOG_ERROR, + Level::Warn => ffi::android_LogPriority::ANDROID_LOG_WARN, + Level::Info => ffi::android_LogPriority::ANDROID_LOG_INFO, + Level::Debug => ffi::android_LogPriority::ANDROID_LOG_DEBUG, + Level::Trace => ffi::android_LogPriority::ANDROID_LOG_VERBOSE, + }; + unsafe { + ffi::__android_log_write(prio.0 as c_int, tag.as_ptr(), msg.as_ptr()); + } +} + +pub(crate) fn log_panic(panic: Box) { + fn log_panic(panic_str: &str) { + const RUST_PANIC_TAG: &CStr = + unsafe { CStr::from_bytes_with_nul_unchecked(b"RustPanic\0") }; + + let panic_str = CString::new(panic_str).unwrap_or_default(); + + // Use the Rust logger if installed and enabled, otherwise fall back to the Android system + // logger so there is at least some record of the panic + if log_enabled!(Level::Error) { + error!("RustPanic: {}", panic_str.to_string_lossy()); + log::logger().flush(); + } else { + android_log(Level::Error, RUST_PANIC_TAG, &panic_str); + } + } + + match panic.downcast::() { + Ok(panic_string) => log_panic(&panic_string), + Err(panic) => match panic.downcast::<&str>() { + Ok(panic_str) => log_panic(&panic_str), + Err(_) => log_panic("Unknown panic message type"), + }, + } +} + +/// Run a closure and abort the program if it panics. +/// +/// This is generally used to ensure Rust callbacks won't unwind past the FFI boundary, which leads +/// to undefined behaviour. +pub(crate) fn abort_on_panic(f: impl FnOnce() -> R) -> R { + std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).unwrap_or_else(|panic| { + // Try logging the panic before aborting + // + // Just in case our attempt to log a panic could itself cause a panic we use a + // second catch_unwind here. + let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| log_panic(panic))); + std::process::abort(); + }) +} diff --git a/platform/android/src/main/java/dev/teamspeach/mobile/CommunicationRoutePolicy.java b/platform/android/src/main/java/dev/teamspeach/mobile/CommunicationRoutePolicy.java index 2a7e094..a0ac898 100644 --- a/platform/android/src/main/java/dev/teamspeach/mobile/CommunicationRoutePolicy.java +++ b/platform/android/src/main/java/dev/teamspeach/mobile/CommunicationRoutePolicy.java @@ -1,19 +1,16 @@ package dev.teamspeach.mobile; -/** AudioDeviceInfo type values; no product-name or endpoint-ID heuristics. */ +/** Manual communication routing; observations and capture choices never select output. */ final class CommunicationRoutePolicy { private CommunicationRoutePolicy() {} - static boolean isBuiltin(int type) { return type==1||type==2||type==15||type==24; } - static int priority(int type) { - switch(type) { - case 3: case 4: case 11: case 12: case 22: return 30; // Wired/USB. - case 7: case 23: case 26: case 27: return 20; // SCO, hearing aid, BLE. - case 1: case 2: case 24: return 0; // Preserve the current phone route. - default: return -1; // A2DP is not a duplex communication endpoint. - } + enum Action { NONE, CLEAR, SELECT, UNAVAILABLE } + static Action action(boolean playback,int requestedId,int appliedId,int currentId,boolean available) { + if(!playback)return Action.NONE; + if(requestedId==0)return appliedId==0?Action.NONE:Action.CLEAR; + if(!available)return Action.UNAVAILABLE; + return requestedId==currentId?Action.NONE:Action.SELECT; } - static boolean prefer(int id,int type,int bestId,int bestType,int currentId) { - int rank=priority(type),best=priority(bestType); - return rank>=0&&(bestId==0||rank>best||(rank==best&&id==currentId)); + static boolean observedChange(boolean starting,boolean topologyChanged,int previousId,int currentId) { + return starting||topologyChanged||previousId!=currentId; } } diff --git a/platform/android/src/main/java/dev/teamspeach/mobile/PlatformBridge.java b/platform/android/src/main/java/dev/teamspeach/mobile/PlatformBridge.java index ddc2a65..e13c466 100644 --- a/platform/android/src/main/java/dev/teamspeach/mobile/PlatformBridge.java +++ b/platform/android/src/main/java/dev/teamspeach/mobile/PlatformBridge.java @@ -71,12 +71,8 @@ public final class PlatformBridge { if(permission==2)return application.getSystemService(NotificationManager.class).areNotificationsEnabled() && (Build.VERSION.SDK_INT<33 || application.checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS)==PackageManager.PERMISSION_GRANTED); return application.checkSelfPermission(permissionName(permission))==PackageManager.PERMISSION_GRANTED; } - public static boolean voiceDeviceIsBuiltin(int id) { - for(android.media.AudioDeviceInfo device:application.getSystemService(android.media.AudioManager.class).getDevices(android.media.AudioManager.GET_DEVICES_ALL)){ - if(device.getId()==id)return CommunicationRoutePolicy.isBuiltin(device.getType()); - } - return false; - } + public static boolean setVoicePlayback(long token,long request,int id) { return post(() -> VoiceService.changePlayback(token,request,id)); } + public static void cancelVoicePlayback(long token,long request) { MAIN.post(() -> VoiceService.cancelPlayback(token,request)); } public static boolean pick(long id, int kind, String name, String mime, String[] types) { return post(() -> { TeamsPeachActivity value = visible(); if (value == null) nativePick(id, null, "activity_not_visible"); else value.pick(id, kind, name, mime, types); }); } @@ -84,11 +80,11 @@ public final class PlatformBridge { return post(() -> { TeamsPeachActivity value = visible(); if (value == null) nativeText(id, null, "activity_not_visible"); else value.permission(id, permission); }); } public static void cancel(long id) { MAIN.post(() -> { TeamsPeachActivity value = activity.get(); if (value != null) value.cancel(id); }); } - public static boolean startVoice(long token,boolean microphone) { + public static boolean startVoice(long token,boolean microphone,int playbackId) { return post(() -> { TeamsPeachActivity value = visible(); if (value == null || (microphone && !granted(0)) || !nativeVoiceValid(token)) { nativeVoiceStopped(token); return; } - try { value.startForegroundService(new Intent(value, VoiceService.class).putExtra("token", token).putExtra("microphone",microphone)); } + try { value.startForegroundService(new Intent(value, VoiceService.class).putExtra("token", token).putExtra("microphone",microphone).putExtra("playbackId",playbackId)); } catch (RuntimeException error) { nativeVoiceStopped(token); } }); } @@ -162,6 +158,8 @@ public final class PlatformBridge { static native void nativeVoiceStarted(long token,boolean microphone); static native boolean nativeVoiceCaptureValid(long token,long request); static native boolean nativeVoiceCaptureCurrent(long token,long request); + static native boolean nativeVoicePlaybackValid(long token,long request); + static native void nativeVoicePlaybackFinished(long token,long request); static native boolean nativeVoiceCaptureChanged(long token,long request,boolean enabled); static native void nativeVoiceCaptureRevoked(long token); static native void nativeVoiceEvent(long token,int event,boolean value); diff --git a/platform/android/src/main/java/dev/teamspeach/mobile/VoiceService.java b/platform/android/src/main/java/dev/teamspeach/mobile/VoiceService.java index eb05dfc..820b694 100644 --- a/platform/android/src/main/java/dev/teamspeach/mobile/VoiceService.java +++ b/platform/android/src/main/java/dev/teamspeach/mobile/VoiceService.java @@ -19,18 +19,13 @@ public final class VoiceService extends Service { private java.util.Set deviceIds = java.util.Collections.emptySet(); private AudioManager.OnCommunicationDeviceChangedListener communicationDeviceListener; private int communicationDeviceId; - private int phoneDeviceId; - private boolean phoneSpeaker; - private boolean headsetRoute; - private boolean pendingExternalRoute; + private int appliedPlaybackId; + private long pendingPlaybackRequest; private long routeGeneration; private int pendingDeviceId; private boolean starting; - private boolean scoRequested; - private boolean scoConnected; - private BroadcastReceiver scoReceiver; - private enum RoutePhase { REQUEST, PLATFORM_ACCEPTED, PLATFORM_RETURNED, CALLBACK, ACK, FINISH, FOCUS, SCO, RELEASE, CAPTURE } - private enum RouteReason { NONE, ALREADY_CURRENT, DEVICE_CALLBACK, SYNCHRONOUS, SCO_CONNECTED, LEGACY_IMMEDIATE, NO_TARGET, REQUEST_DENIED, PLATFORM_DENIED, TIMEOUT, SUPERSEDED, FOCUS_GRANTED, FOCUS_DENIED, FOCUS_GAIN, FOCUS_LOSS, CAPTURE_REQUEST, CAPTURE_ACK, CAPTURE_REVOKED, PERMISSION_DENIED, FOCUS_UNAVAILABLE, ROUTE_PENDING } + private enum RoutePhase { REQUEST, PLATFORM_ACCEPTED, CALLBACK, ACK, FINISH, FOCUS, RELEASE, CAPTURE } + private enum RouteReason { NONE, DEVICE_CALLBACK, SYNCHRONOUS, OBSERVED, NO_TARGET, REQUEST_DENIED, PLATFORM_DENIED, TIMEOUT, SUPERSEDED, FOCUS_GRANTED, FOCUS_DENIED, FOCUS_GAIN, FOCUS_LOSS, CAPTURE_REQUEST, CAPTURE_ACK, CAPTURE_REVOKED, PERMISSION_DENIED, FOCUS_UNAVAILABLE, ROUTE_PENDING } private int routeCurrentType; private int routeTargetType; private boolean routeTopologyChanged; @@ -43,7 +38,6 @@ public final class VoiceService extends Service { private boolean routeCaptureRequested; private int routeFocusChange; private int routeFocusRequestResult; - private int routeScoState=AudioManager.SCO_AUDIO_STATE_ERROR; private long routeStartedMs; private static long routeLogWindowMs; private static int routeLogCount; @@ -59,7 +53,7 @@ public final class VoiceService extends Service { +" topologyChanged="+routeTopologyChanged+" current_type="+routeCurrentType+" target_type="+routeTargetType +" device_count="+routeDeviceCount+" device_types="+routeDeviceTypes+" available_count="+routeAvailableCount+" available_types="+routeAvailableTypes +" requested="+routeRequested+" accepted="+routeAccepted+" acknowledged="+(phase==RoutePhase.ACK)+" focused="+focused+" focus_change="+routeFocusChange+" focus_request_result="+routeFocusRequestResult+" pending="+(pendingDeviceId!=0) - +" starting="+starting+" capture="+microphone+" capture_requested="+routeCaptureRequested+" sco_requested="+scoRequested+" sco_connected="+scoConnected+" sco_state="+routeScoState + +" starting="+starting+" capture="+microphone+" capture_requested="+routeCaptureRequested +" elapsed_ms="+(routeStartedMs==0?0:Math.max(0,now-routeStartedMs))+" suppressed="+routeLogSuppressed); routeLogSuppressed=0; } @@ -82,7 +76,7 @@ public final class VoiceService extends Service { if(service==null||service.token!=token||!PlatformBridge.nativeVoiceCaptureValid(token,request)){PlatformBridge.nativeText(request,null,"target_changed");return;} if(enabled && (PlatformBridge.visible()==null||!PlatformBridge.granted(0))){service.routeLog(RoutePhase.CAPTURE,RouteReason.PERMISSION_DENIED);service.receiveOnly();PlatformBridge.nativeText(request,null,"Microphone activation requires a visible user action and RECORD_AUDIO grant");return;} if(enabled && !service.focused){service.routeLog(RoutePhase.CAPTURE,RouteReason.FOCUS_UNAVAILABLE);PlatformBridge.nativeText(request,null,"Audio focus is unavailable; wait for the interruption to end");return;} - if(enabled && service.pendingDeviceId!=0){service.routeLog(RoutePhase.CAPTURE,RouteReason.ROUTE_PENDING);PlatformBridge.nativeText(request,null,"Audio route is changing; wait for the headset transition to finish");return;} + if(enabled && service.pendingPlaybackRequest!=0){service.routeLog(RoutePhase.CAPTURE,RouteReason.ROUTE_PENDING);PlatformBridge.nativeText(request,null,"Audio route is changing; wait for the playback selection acknowledgement");return;} try { service.foreground(enabled); boolean acknowledged=PlatformBridge.nativeVoiceCaptureChanged(token,request,enabled); @@ -112,32 +106,68 @@ public final class VoiceService extends Service { AudioDeviceInfo device=audio.getCommunicationDevice(); return device==null?0:device.getId(); } - private AudioDeviceInfo preferredCommunicationDevice(AudioManager audio,boolean rememberPhone) { + private AudioDeviceInfo availablePlayback(AudioManager audio,int id) { Iterable available=Build.VERSION.SDK_INT>=31?audio.getAvailableCommunicationDevices():java.util.Arrays.asList(audio.getDevices(AudioManager.GET_DEVICES_OUTPUTS)); - AudioDeviceInfo best=null;int selected=communicationDevice(audio); - routeAvailableCount=0;routeAvailableTypes=0; - if(rememberPhone&&!headsetRoute&&(!starting||phoneDeviceId==0)){ - for(AudioDeviceInfo device:available)if(device.getId()==selected&&CommunicationRoutePolicy.isBuiltin(device.getType())){phoneDeviceId=selected;break;} - } - int preferred=phoneDeviceId!=0?phoneDeviceId:selected; + AudioDeviceInfo target=null;routeAvailableCount=0;routeAvailableTypes=0; for(AudioDeviceInfo device:available){ ++routeAvailableCount;int type=device.getType();routeAvailableTypes|=1L<<(type>=0&&type<63?type:63); - if(CommunicationRoutePolicy.prefer(device.getId(),device.getType(),best==null?0:best.getId(),best==null?0:best.getType(),CommunicationRoutePolicy.isBuiltin(device.getType())?preferred:selected))best=device; + if(device.getId()==id)target=device; } - return best; + return target; } - private void finishRoute(long owner,long generation,boolean accepted,RouteReason reason) { + private void finishPlayback(long owner,long generation,boolean accepted,RouteReason reason) { if(token!=owner||routeGeneration!=generation||!PlatformBridge.nativeVoiceValid(owner)){routeLog(RoutePhase.FINISH,RouteReason.SUPERSEDED);return;} - pendingDeviceId=0; - if(accepted)headsetRoute=pendingExternalRoute; - if(!accepted)receiveOnly(); - if(starting){starting=false;if(!focused){PlatformBridge.nativeVoiceStopped(owner);stopSelf();return;}PlatformBridge.nativeVoiceStarted(owner,microphone);} - else PlatformBridge.nativeVoiceEvent(owner,3,true); + long request=pendingPlaybackRequest;pendingPlaybackRequest=0;pendingDeviceId=0; + AudioManager audio=getSystemService(AudioManager.class); + deviceIds=audioDevices(audio);communicationDeviceId=communicationDevice(audio); + boolean initial=starting;starting=false; + if(initial&&accepted)PlatformBridge.nativeVoiceStarted(owner,microphone); + else { + if(!accepted&&!initial)receiveOnly(); + if(!initial)PlatformBridge.nativeVoicePlaybackFinished(owner,request); + if(request!=0)PlatformBridge.nativeText(request,accepted?"":null,accepted?null:"Android could not acknowledge the exact playback communication route ("+reason+"); this microphone/headset combination may be unsupported"); + if(initial){PlatformBridge.nativeVoiceStopped(owner);stopSelf();} + } routeLog(accepted?RoutePhase.ACK:RoutePhase.FINISH,reason); } - private void awaitRoute(long owner,long generation) { - // A missing platform acknowledgement becomes receive-only, not a retry loop. - PlatformBridge.MAIN.postDelayed(()->{if(pendingDeviceId!=0)finishRoute(owner,generation,false,RouteReason.TIMEOUT);},3000); + static void changePlayback(long owner,long request,int id) { + VoiceService service=current.get(); + if(service==null||service.token!=owner||!PlatformBridge.nativeVoicePlaybackValid(owner,request)){PlatformBridge.nativeText(request,null,"target_changed");return;} + try{service.selectPlayback(owner,request,id);}catch(RuntimeException denied){PlatformBridge.nativeText(request,null,"Android denied access to the exact playback communication route");} + } + static void cancelPlayback(long owner,long request) { + VoiceService service=current.get(); + if(service!=null&&service.token==owner&&service.pendingPlaybackRequest==request)service.finishPlayback(owner,service.routeGeneration,false,RouteReason.SUPERSEDED); + } + private void selectPlayback(long owner,long request,int id) { + AudioManager audio=getSystemService(AudioManager.class); + AudioDeviceInfo target=availablePlayback(audio,id); + CommunicationRoutePolicy.Action action=CommunicationRoutePolicy.action(true,id,appliedPlaybackId,Build.VERSION.SDK_INT>=31?communicationDevice(audio):appliedPlaybackId,target!=null); + if(action==CommunicationRoutePolicy.Action.UNAVAILABLE){routeLog(RoutePhase.FINISH,RouteReason.NO_TARGET);PlatformBridge.nativeText(request,null,"The exact playback endpoint is unavailable as an Android communication device; choose another playback device explicitly");if(starting){starting=false;PlatformBridge.nativeVoiceStopped(owner);stopSelf();}return;} + if(pendingPlaybackRequest!=0)finishPlayback(owner,routeGeneration,false,RouteReason.SUPERSEDED); + if(action==CommunicationRoutePolicy.Action.NONE||Build.VERSION.SDK_INT<31){ + appliedPlaybackId=id; + if(starting){pendingPlaybackRequest=request;finishPlayback(owner,++routeGeneration,true,RouteReason.SYNCHRONOUS);} + else PlatformBridge.nativeText(request,"",null); + return; + } + routeStartedMs=SystemClock.elapsedRealtime();routeTopologyChanged=false;routeRequested=false;routeAccepted=false; + routeTargetType=target==null?AudioDeviceInfo.TYPE_UNKNOWN:target.getType(); + long generation=++routeGeneration;pendingPlaybackRequest=request;pendingDeviceId=id==0?-1:id; + if(!starting)PlatformBridge.nativeVoiceEvent(owner,8,true); + routeLog(RoutePhase.REQUEST,RouteReason.NONE); + try { + routeRequested=true; + if(action==CommunicationRoutePolicy.Action.CLEAR){audio.clearCommunicationDevice();appliedPlaybackId=0;routeAccepted=true;communicationDeviceId=communicationDevice(audio);finishPlayback(owner,generation,true,RouteReason.SYNCHRONOUS);} + else { + routeAccepted=audio.setCommunicationDevice(target); + routeLog(RoutePhase.PLATFORM_ACCEPTED,routeAccepted?RouteReason.NONE:RouteReason.REQUEST_DENIED); + if(!routeAccepted){finishPlayback(owner,generation,false,RouteReason.REQUEST_DENIED);return;} + appliedPlaybackId=id; + if(communicationDevice(audio)==id){communicationDeviceId=id;finishPlayback(owner,generation,true,RouteReason.SYNCHRONOUS);} + else PlatformBridge.MAIN.postDelayed(()->{if(pendingPlaybackRequest==request)finishPlayback(owner,generation,false,RouteReason.TIMEOUT);},3000); + } + }catch(RuntimeException denied){finishPlayback(owner,generation,false,RouteReason.PLATFORM_DENIED);} } private void routeChanged(long owner) { if(token!=owner||!PlatformBridge.nativeVoiceValid(owner))return; @@ -147,61 +177,27 @@ public final class VoiceService extends Service { int selected=selectedDevice==null?0:selectedDevice.getId(); routeCurrentType=selectedDevice==null?AudioDeviceInfo.TYPE_UNKNOWN:selectedDevice.getType(); boolean topologyChanged=!next.equals(deviceIds); - if(Build.VERSION.SDK_INT>=31&&pendingDeviceId!=0&&!topologyChanged){ - communicationDeviceId=selected; - if(selected==pendingDeviceId)finishRoute(owner,routeGeneration,true,RouteReason.DEVICE_CALLBACK); + if(pendingPlaybackRequest!=0){ + deviceIds=next;communicationDeviceId=selected; + if(selected==pendingDeviceId)finishPlayback(owner,routeGeneration,true,RouteReason.DEVICE_CALLBACK); + else if(topologyChanged&&availablePlayback(audio,pendingDeviceId)==null)finishPlayback(owner,routeGeneration,false,RouteReason.NO_TARGET); else routeLog(RoutePhase.CALLBACK,RouteReason.ROUTE_PENDING); return; } - if(!starting&&!topologyChanged&&selected==communicationDeviceId)return; - if(pendingDeviceId!=0)routeLog(RoutePhase.FINISH,RouteReason.SUPERSEDED); - routeStartedMs=SystemClock.elapsedRealtime();routeTopologyChanged=topologyChanged;routeRequested=false;routeAccepted=false; - deviceIds=next;communicationDeviceId=selected; - if(!starting)PlatformBridge.nativeVoiceEvent(owner,8,true); - long generation=++routeGeneration; - AudioDeviceInfo target=preferredCommunicationDevice(audio,starting||!topologyChanged); - // Topology changes adopt a headset automatically. A route-only callback - // instead accepts Android's actual choice (including an exact external - // stream selection); do not fight it with repeated setCommunicationDevice. - if(Build.VERSION.SDK_INT>=31&&!topologyChanged&&selected!=0){ - for(AudioDeviceInfo available:audio.getAvailableCommunicationDevices())if(available.getId()==selected){target=available;break;} - } - pendingExternalRoute=target!=null&&CommunicationRoutePolicy.priority(target.getType())>=20; - pendingDeviceId=target==null?-1:target.getId(); - routeTargetType=target==null?AudioDeviceInfo.TYPE_UNKNOWN:target.getType(); - routeLog(RoutePhase.REQUEST,RouteReason.NONE); - try { - if(Build.VERSION.SDK_INT>=31){ - if(target==null){audio.clearCommunicationDevice();finishRoute(owner,generation,false,RouteReason.NO_TARGET);} - else if(selected==target.getId()){finishRoute(owner,generation,true,RouteReason.ALREADY_CURRENT);} - else { - routeRequested=true;routeAccepted=audio.setCommunicationDevice(target); - routeLog(RoutePhase.PLATFORM_ACCEPTED,routeAccepted?RouteReason.NONE:RouteReason.REQUEST_DENIED); - if(!routeAccepted)finishRoute(owner,generation,false,RouteReason.REQUEST_DENIED); - else if(communicationDevice(audio)==target.getId()){communicationDeviceId=target.getId();routeCurrentType=target.getType();finishRoute(owner,generation,true,RouteReason.SYNCHRONOUS);} - else awaitRoute(owner,generation); - } - }else{ - boolean sco=target!=null&&target.getType()==AudioDeviceInfo.TYPE_BLUETOOTH_SCO; - audio.setSpeakerphoneOn(!pendingExternalRoute&&phoneSpeaker); - if(sco){ - routeRequested=true;scoRequested=true;audio.startBluetoothSco();audio.setBluetoothScoOn(true); - routeLog(RoutePhase.PLATFORM_RETURNED,RouteReason.NONE); - if(scoConnected)finishRoute(owner,generation,true,RouteReason.SCO_CONNECTED);else awaitRoute(owner,generation); - }else{ - if(scoRequested){audio.stopBluetoothSco();audio.setBluetoothScoOn(false);scoRequested=false;scoConnected=false;} - finishRoute(owner,generation,true,RouteReason.LEGACY_IMMEDIATE); - } - } - }catch(RuntimeException denied){finishRoute(owner,generation,false,RouteReason.PLATFORM_DENIED);} + if(!CommunicationRoutePolicy.observedChange(starting,topologyChanged,communicationDeviceId,selected))return; + routeStartedMs=SystemClock.elapsedRealtime();routeTopologyChanged=topologyChanged;routeRequested=false;routeAccepted=true;routeTargetType=routeCurrentType; + deviceIds=next;communicationDeviceId=selected;availablePlayback(audio,0); + // Observe the OS route and fence/recover streams; never choose a headset on plug/unplug. + if(starting){starting=false;PlatformBridge.nativeVoiceStarted(owner,microphone);} + else{PlatformBridge.nativeVoiceEvent(owner,8,true);PlatformBridge.nativeVoiceEvent(owner,3,true);} + routeLog(RoutePhase.ACK,RouteReason.OBSERVED); } private void releaseCommunicationRoute(AudioManager audio) { routeLog(RoutePhase.RELEASE,RouteReason.NONE); ++routeGeneration;pendingDeviceId=0; - if(Build.VERSION.SDK_INT>=31)audio.clearCommunicationDevice(); - else{if(scoRequested){audio.stopBluetoothSco();audio.setBluetoothScoOn(false);}audio.setSpeakerphoneOn(phoneSpeaker);} - scoRequested=false;scoConnected=false; - if(scoReceiver!=null){unregisterReceiver(scoReceiver);scoReceiver=null;} + if(pendingPlaybackRequest!=0){PlatformBridge.nativeText(pendingPlaybackRequest,null,"The Android call ended before playback selection was acknowledged");pendingPlaybackRequest=0;} + if(Build.VERSION.SDK_INT>=31&&appliedPlaybackId!=0)audio.clearCommunicationDevice(); + appliedPlaybackId=0; } @Override public void onCreate() { super.onCreate(); current=new java.lang.ref.WeakReference<>(this); getSystemService(NotificationManager.class).createNotificationChannel(new NotificationChannel("voice", "Active voice call", NotificationManager.IMPORTANCE_LOW)); } @Override public int onStartCommand(Intent intent, int flags, int startId) { @@ -223,10 +219,6 @@ public final class VoiceService extends Service { // Foreground notification alone does not keep the native audio/network worker awake. if(callWakeLock==null){callWakeLock=getSystemService(PowerManager.class).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"TeamsPeach:voice-call");callWakeLock.setReferenceCounted(false);callWakeLock.acquire();} AudioManager audio = getSystemService(AudioManager.class); - if(focus==null){ - phoneDeviceId=0;headsetRoute=false;phoneSpeaker=audio.isSpeakerphoneOn(); - if(Build.VERSION.SDK_INT>=31){AudioDeviceInfo phone=audio.getCommunicationDevice();if(phone!=null&&CommunicationRoutePolicy.isBuiltin(phone.getType()))phoneDeviceId=phone.getId();} - } audio.setMode(AudioManager.MODE_IN_COMMUNICATION); communicationMode=true; if (focus == null) { @@ -252,33 +244,14 @@ public final class VoiceService extends Service { communicationDeviceListener=device->routeChanged(owner); audio.addOnCommunicationDeviceChangedListener(PlatformBridge.MAIN::post,communicationDeviceListener); } - else{ - scoReceiver=new BroadcastReceiver(){@Override public void onReceive(Context context,Intent intent){ - if(token!=owner||!PlatformBridge.nativeVoiceValid(owner))return; - int state=intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE,AudioManager.SCO_AUDIO_STATE_ERROR); - boolean wasConnected=scoConnected;scoConnected=state==AudioManager.SCO_AUDIO_STATE_CONNECTED; - routeScoState=state; - routeLog(RoutePhase.SCO,RouteReason.NONE); - if(scoConnected&&!wasConnected&&scoRequested){ - if(pendingDeviceId!=0)finishRoute(owner,routeGeneration,true,RouteReason.SCO_CONNECTED); - else{routeStartedMs=SystemClock.elapsedRealtime();PlatformBridge.nativeVoiceEvent(owner,8,true);finishRoute(owner,++routeGeneration,true,RouteReason.SCO_CONNECTED);} - } - else if(state==AudioManager.SCO_AUDIO_STATE_DISCONNECTED&&wasConnected&&scoRequested){ - scoRequested=false;audio.setBluetoothScoOn(false);audio.setSpeakerphoneOn(phoneSpeaker);pendingExternalRoute=false; - PlatformBridge.nativeVoiceEvent(owner,8,true); - routeStartedMs=SystemClock.elapsedRealtime();finishRoute(owner,++routeGeneration,true,RouteReason.LEGACY_IMMEDIATE); - } - }}; - if(Build.VERSION.SDK_INT>=33)registerReceiver(scoReceiver,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED),Context.RECEIVER_NOT_EXPORTED); - else registerReceiver(scoReceiver,new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)); - } devices = new AudioDeviceCallback() { @Override public void onAudioDevicesAdded(AudioDeviceInfo[] values) { routeChanged(owner); } @Override public void onAudioDevicesRemoved(AudioDeviceInfo[] values) { routeChanged(owner); } }; audio.registerAudioDeviceCallback(devices, PlatformBridge.MAIN); } - routeChanged(token); + if(starting)selectPlayback(token,token,intent.getIntExtra("playbackId",0)); + else routeChanged(token); } catch (RuntimeException error) { routeLog(RoutePhase.FINISH,RouteReason.PLATFORM_DENIED);PlatformBridge.nativeVoiceStopped(token); stopSelf(); } return START_NOT_STICKY; } diff --git a/platform/android/src/test/java/dev/teamspeach/mobile/CommunicationRoutePolicyTest.java b/platform/android/src/test/java/dev/teamspeach/mobile/CommunicationRoutePolicyTest.java index 5af5738..f99a606 100644 --- a/platform/android/src/test/java/dev/teamspeach/mobile/CommunicationRoutePolicyTest.java +++ b/platform/android/src/test/java/dev/teamspeach/mobile/CommunicationRoutePolicyTest.java @@ -3,28 +3,19 @@ package dev.teamspeach.mobile; /** Plain javac/java fixture: deliberately independent of the Android SDK and JUnit. */ public final class CommunicationRoutePolicyTest { private static void require(boolean value,String message){if(!value)throw new AssertionError(message);} - private static int choose(int[][] endpoints,int current){ - int id=0,type=0; - for(int[] endpoint:endpoints){if(CommunicationRoutePolicy.prefer(endpoint[0],endpoint[1],id,type,current)){id=endpoint[0];type=endpoint[1];}} - return id; - } public static void main(String[] args){ - int[][] phone={{1,1},{2,2}}; - int[][] bluetooth={{1,1},{2,2},{40,7}}; - int[][] wired={{1,1},{2,2},{40,7},{80,3}}; - require(choose(phone,2)==2,"detached call preserves the current speaker route"); - require(choose(phone,1)==1,"detached call preserves the current receiver route"); - require(choose(phone,0)==1,"without a remembered route the available receiver is valid"); - require(choose(bluetooth,1)==40,"connected SCO headset is adopted"); - require(choose(wired,40)==80,"wired headset supersedes wireless"); - require(choose(bluetooth,80)==40,"wired unplug returns to available Bluetooth"); - require(choose(phone,40)==1,"Bluetooth unplug returns to phone"); - require(choose(phone,2)==2,"headset unplug restores a remembered speaker endpoint"); - require(choose(new int[][]{{1,1}},2)==1,"a removed remembered phone endpoint cannot be selected"); - require(choose(new int[][]{{1,1},{50,8}},1)==1,"A2DP-only endpoints are not duplex routes"); - require(choose(new int[][]{{1,1},{60,26}},1)==60,"BLE communication headset is adopted"); - require(choose(new int[][]{{40,7},{41,7}},41)==41,"equivalent available routes retain current endpoint"); - require(CommunicationRoutePolicy.isBuiltin(1)&&CommunicationRoutePolicy.isBuiltin(2)&&CommunicationRoutePolicy.isBuiltin(15)&&CommunicationRoutePolicy.isBuiltin(24),"only typed phone endpoints follow communication default"); - for(int type:new int[]{0,3,7,8,11,22,23,26,99})require(!CommunicationRoutePolicy.isBuiltin(type),"external and unknown exact selections remain exact: "+type); + require(CommunicationRoutePolicy.action(true,80,0,1,true)==CommunicationRoutePolicy.Action.SELECT,"explicit playback selects the exact available endpoint"); + require(CommunicationRoutePolicy.action(false,15,80,80,true)==CommunicationRoutePolicy.Action.NONE,"phone capture cannot redirect headset output"); + require(CommunicationRoutePolicy.action(false,0,80,80,true)==CommunicationRoutePolicy.Action.NONE,"default capture cannot clear playback selection"); + require(CommunicationRoutePolicy.action(true,80,80,80,true)==CommunicationRoutePolicy.Action.NONE,"unchanged playback does not re-request hardware"); + require(CommunicationRoutePolicy.action(true,80,80,1,true)==CommunicationRoutePolicy.Action.SELECT,"explicit user re-selection can request its exact available route again"); + require(CommunicationRoutePolicy.action(true,80,80,1,false)==CommunicationRoutePolicy.Action.UNAVAILABLE,"removed exact endpoint errors without fallback"); + require(CommunicationRoutePolicy.action(true,0,80,80,true)==CommunicationRoutePolicy.Action.CLEAR,"explicit default releases the app override to Android"); + require(CommunicationRoutePolicy.action(true,0,0,80,true)==CommunicationRoutePolicy.Action.NONE,"system default does not force a headset or speaker"); + require(CommunicationRoutePolicy.action(true,80,0,80,true)==CommunicationRoutePolicy.Action.NONE,"already-current exact playback needs no platform request"); + require(CommunicationRoutePolicy.observedChange(false,true,1,1),"plug/unplug topology still invalidates streams without selecting a route"); + require(CommunicationRoutePolicy.observedChange(false,false,1,80),"OS route-only change is observed"); + require(!CommunicationRoutePolicy.observedChange(false,false,80,80),"unchanged topology and route are ignored"); + require(CommunicationRoutePolicy.observedChange(true,false,80,80),"startup observes Android's route without ranking endpoints"); } }