use crate::mixer::MixerSeed;
use cranpose_services::AudioError;
#[cfg(all(feature = "aaudio", target_os = "android"))]
mod aaudio;
#[cfg(all(
feature = "cpal-backend",
not(any(target_os = "android", target_arch = "wasm32"))
))]
mod cpal_device;
pub trait AudioSink {
fn suspend(&self) {}
fn resume(&self) {}
fn park(&self) {}
}
pub fn is_compiled() -> bool {
cfg!(all(feature = "aaudio", target_os = "android"))
|| cfg!(all(
feature = "cpal-backend",
not(any(target_os = "android", target_arch = "wasm32"))
))
}
#[allow(clippy::needless_return)]
pub fn open(seed: MixerSeed) -> Result<Box<dyn AudioSink>, AudioError> {
#[cfg(all(feature = "aaudio", target_os = "android"))]
{
return aaudio::open(seed);
}
#[cfg(all(
feature = "cpal-backend",
not(any(target_os = "android", target_arch = "wasm32"))
))]
{
return cpal_device::open(seed);
}
#[cfg(not(any(
all(feature = "aaudio", target_os = "android"),
all(
feature = "cpal-backend",
not(any(target_os = "android", target_arch = "wasm32"))
)
)))]
{
drop(seed);
Err(AudioError::Unsupported)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_build_without_a_device_reports_unsupported() {
if is_compiled() {
return;
}
let (_command_tx, command_rx) = crate::ring::channel(4);
let (retired_tx, _retired_rx) = crate::ring::channel(4);
let seed = MixerSeed {
commands: command_rx,
retired: retired_tx,
leaked_clips: std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0)),
underruns: std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0)),
streaming: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
};
assert!(matches!(open(seed), Err(AudioError::Unsupported)));
}
}