android_audio/
libaaudio_stub.rs

1// Copyright 2024 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Stub implementation of Android AAudio NDK
6//!
7//! This implementation is used to enable the virtio-snd for Android to be compiled without
8//! Andoird AAudio NDK available. It is only used for testing purposes and not functional at
9//! runtime.
10
11use std::os::raw::c_void;
12
13use crate::AAudioStream;
14use crate::AAudioStreamBuilder;
15use crate::AaudioFormatT;
16use crate::AaudioResultT;
17use crate::AaudioStreamStateT;
18
19/// Handle for which `AAudioStream_waitForStateChange()` reports a disconnected stream, so tests
20/// can exercise that path. Every other handle reports a started stream.
21pub(crate) const DISCONNECTED_STREAM: usize = 0xd15c;
22const AAUDIO_STREAM_STATE_DISCONNECTED: AaudioStreamStateT = 13;
23
24#[no_mangle]
25extern "C" fn AAudio_createStreamBuilder(builder: *mut *mut AAudioStreamBuilder) -> AaudioResultT {
26    if !builder.is_null() {
27        // SAFETY: `builder` is a valid out-pointer provided by the caller in tests.
28        unsafe {
29            *builder = std::ptr::NonNull::<AAudioStreamBuilder>::dangling().as_ptr();
30        }
31    }
32    0
33}
34
35#[no_mangle]
36extern "C" fn AAudioStreamBuilder_delete(_builder: *mut AAudioStreamBuilder) -> AaudioResultT {
37    0
38}
39
40#[no_mangle]
41extern "C" fn AAudioStreamBuilder_setBufferCapacityInFrames(
42    _builder: *mut AAudioStreamBuilder,
43    _num_frames: i32,
44) {
45}
46
47#[no_mangle]
48extern "C" fn AAudioStreamBuilder_setDirection(
49    _builder: *mut AAudioStreamBuilder,
50    _direction: u32,
51) {
52}
53
54#[no_mangle]
55extern "C" fn AAudioStreamBuilder_setFormat(
56    _builder: *mut AAudioStreamBuilder,
57    _format: AaudioFormatT,
58) {
59}
60
61#[no_mangle]
62extern "C" fn AAudioStreamBuilder_setSampleRate(
63    _builder: *mut AAudioStreamBuilder,
64    _sample_rate: i32,
65) {
66}
67
68#[no_mangle]
69extern "C" fn AAudioStreamBuilder_setChannelCount(
70    _builder: *mut AAudioStreamBuilder,
71    _channel_count: i32,
72) {
73}
74
75#[no_mangle]
76extern "C" fn AAudioStreamBuilder_openStream(
77    _builder: *mut AAudioStreamBuilder,
78    stream: *mut *mut AAudioStream,
79) -> AaudioResultT {
80    if !stream.is_null() {
81        // SAFETY: `stream` is a valid out-pointer provided by the caller in tests.
82        unsafe {
83            *stream = std::ptr::NonNull::<AAudioStream>::dangling().as_ptr();
84        }
85    }
86    0
87}
88
89#[no_mangle]
90extern "C" fn AAudioStream_getBufferSizeInFrames(_stream: *mut AAudioStream) -> i32 {
91    960
92}
93
94#[no_mangle]
95extern "C" fn AAudioStream_requestStart(_stream: *mut AAudioStream) -> AaudioResultT {
96    0
97}
98
99#[no_mangle]
100extern "C" fn AAudioStream_read(
101    _stream: *mut AAudioStream,
102    _buffer: *mut c_void,
103    num_frames: i32,
104    _timeout_nanoseconds: i64,
105) -> AaudioResultT {
106    num_frames
107}
108
109#[no_mangle]
110extern "C" fn AAudioStream_write(
111    _stream: *mut AAudioStream,
112    _buffer: *const c_void,
113    num_frames: i32,
114    _timeout_nanoseconds: i64,
115) -> AaudioResultT {
116    num_frames
117}
118
119#[no_mangle]
120extern "C" fn AAudioStream_requestStop(_stream: *mut AAudioStream) -> AaudioResultT {
121    0
122}
123
124#[no_mangle]
125extern "C" fn AAudioStream_close(_stream: *mut AAudioStream) -> AaudioResultT {
126    0
127}
128
129#[no_mangle]
130extern "C" fn AAudioStream_waitForStateChange(
131    stream: *mut AAudioStream,
132    _input_state: AaudioStreamStateT,
133    next_state: *mut AaudioStreamStateT,
134    _timeout_nanoseconds: i64,
135) -> AaudioResultT {
136    if !next_state.is_null() {
137        let state = if stream as usize == DISCONNECTED_STREAM {
138            AAUDIO_STREAM_STATE_DISCONNECTED
139        } else {
140            crate::AAUDIO_STREAM_STATE_STARTED
141        };
142        // SAFETY: `next_state` is a valid out-pointer provided by the caller in tests.
143        unsafe {
144            *next_state = state;
145        }
146    }
147    0
148}