diff --git a/.github/workflows/cpal.yml b/.github/workflows/cpal.yml index 3e0b1cb..e5a75e5 100644 --- a/.github/workflows/cpal.yml +++ b/.github/workflows/cpal.yml @@ -3,6 +3,7 @@ name: cpal on: [push, pull_request] jobs: + clippy-test: runs-on: ubuntu-latest steps: @@ -224,3 +225,18 @@ jobs: run: cargo check --example feedback --target armv7-linux-androideabi --verbose - name: Check record_wav run: cargo check --example record_wav --target armv7-linux-androideabi --verbose + + android-apk-build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install Android targets + run: | + rustup target add armv7-linux-androideabi + rustup target add aarch64-linux-android + rustup target add i686-linux-android + rustup target add x86_64-linux-android + - name: Install Cargo APK + run: cargo install cargo-apk + - name: Build APK + run: cargo apk build --example beep diff --git a/CHANGELOG.md b/CHANGELOG.md index 58c1109..26e8da3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# Version 0.13.0 (2020-10-28) + +- Add Android support via `oboe-rs`. +- Add Android APK build an CI job. + # Version 0.12.1 (2020-07-23) - Bugfix release to get the asio feature working again. diff --git a/Cargo.toml b/Cargo.toml index 8b9f163..ffda344 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cpal" -version = "0.12.1" +version = "0.13.0" authors = ["The CPAL contributors", "Pierre Krieger "] description = "Low-level cross-platform audio I/O library in pure Rust." repository = "https://github.com/rustaudio/cpal" @@ -11,7 +11,6 @@ keywords = ["audio", "sound"] [features] asio = ["asio-sys", "num-traits"] # Only available on Windows. See README for setup instructions. - [dependencies] thiserror = "1.0.2" @@ -47,7 +46,27 @@ js-sys = { version = "0.3.35" } web-sys = { version = "0.3.35", features = [ "AudioContext", "AudioContextOptions", "AudioBuffer", "AudioBufferSourceNode", "AudioNode", "AudioDestinationNode", "Window", "AudioContextState"] } [target.'cfg(target_os = "android")'.dependencies] -oboe = { version = "0.2.1", features = [ "java-interface" ] } +oboe = { version = "0.3.0", features = [ "java-interface" ] } ndk = "0.2" ndk-glue = "0.2" jni = "0.17" + +[[example]] +name = "beep" +path = "examples/beep.rs" +crate-type = ["cdylib"] + +[[example]] +name = "enumerate" +path = "examples/enumerate.rs" +crate-type = ["cdylib"] + +[[example]] +name = "feedback" +path = "examples/feedback.rs" +crate-type = ["cdylib"] + +[[example]] +name = "record_wav" +path = "examples/record_wav.rs" +crate-type = ["cdylib"] diff --git a/examples/beep.rs b/examples/beep.rs index 2f36f3f..696f3f0 100644 --- a/examples/beep.rs +++ b/examples/beep.rs @@ -3,7 +3,8 @@ extern crate cpal; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; -fn main() -> Result<(), anyhow::Error> { +#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "full"))] +fn main() { // Conditionally compile with jack if the feature is specified. #[cfg(all( any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"), @@ -34,15 +35,13 @@ fn main() -> Result<(), anyhow::Error> { let device = host .default_output_device() .expect("failed to find a default output device"); - let config = device.default_output_config()?; + let config = device.default_output_config().unwrap(); match config.sample_format() { - cpal::SampleFormat::F32 => run::(&device, &config.into())?, - cpal::SampleFormat::I16 => run::(&device, &config.into())?, - cpal::SampleFormat::U16 => run::(&device, &config.into())?, + cpal::SampleFormat::F32 => run::(&device, &config.into()).unwrap(), + cpal::SampleFormat::I16 => run::(&device, &config.into()).unwrap(), + cpal::SampleFormat::U16 => run::(&device, &config.into()).unwrap(), } - - Ok(()) } fn run(device: &cpal::Device, config: &cpal::StreamConfig) -> Result<(), anyhow::Error> diff --git a/src/host/oboe/convert.rs b/src/host/oboe/convert.rs index 519e4c0..711d2f2 100644 --- a/src/host/oboe/convert.rs +++ b/src/host/oboe/convert.rs @@ -15,7 +15,7 @@ pub fn to_stream_instant(duration: Duration) -> StreamInstant { ) } -pub fn stream_instant(stream: &mut T) -> StreamInstant { +pub fn stream_instant(stream: &mut T) -> StreamInstant { const CLOCK_MONOTONIC: i32 = 1; let ts = stream .get_timestamp(CLOCK_MONOTONIC) diff --git a/src/host/oboe/input_callback.rs b/src/host/oboe/input_callback.rs index a7341d9..785f61f 100644 --- a/src/host/oboe/input_callback.rs +++ b/src/host/oboe/input_callback.rs @@ -31,7 +31,7 @@ impl CpalInputCallback { fn make_callback_info( &self, - audio_stream: &mut dyn oboe::AudioInputStream, + audio_stream: &mut dyn oboe::AudioInputStreamSafe, ) -> InputCallbackInfo { InputCallbackInfo { timestamp: InputStreamTimestamp { @@ -50,7 +50,7 @@ where fn on_audio_ready( &mut self, - audio_stream: &mut dyn oboe::AudioInputStream, + audio_stream: &mut dyn oboe::AudioInputStreamSafe, audio_data: &[<::FrameType as oboe::IsFrameType>::Type], ) -> oboe::DataCallbackResult { let cb_info = self.make_callback_info(audio_stream); @@ -74,7 +74,7 @@ where fn on_error_before_close( &mut self, - _audio_stream: &mut dyn oboe::AudioInputStream, + _audio_stream: &mut dyn oboe::AudioInputStreamSafe, error: oboe::Error, ) { (self.error_cb)(StreamError::from(error)) @@ -82,7 +82,7 @@ where fn on_error_after_close( &mut self, - _audio_stream: &mut dyn oboe::AudioInputStream, + _audio_stream: &mut dyn oboe::AudioInputStreamSafe, error: oboe::Error, ) { (self.error_cb)(StreamError::from(error)) diff --git a/src/host/oboe/mod.rs b/src/host/oboe/mod.rs index 0a831ad..4e245cd 100644 --- a/src/host/oboe/mod.rs +++ b/src/host/oboe/mod.rs @@ -21,6 +21,7 @@ mod output_callback; use self::android_media::{get_audio_record_min_buffer_size, get_audio_track_min_buffer_size}; use self::input_callback::CpalInputCallback; +use self::oboe::{AudioInputStream, AudioOutputStream}; use self::output_callback::CpalOutputCallback; // Android Java API supports up to 8 channels, but oboe API @@ -36,7 +37,10 @@ const SAMPLE_RATES: [i32; 13] = [ pub struct Host; pub struct Device(Option); -pub struct Stream(Box>); +pub enum Stream { + Input(Box>), + Output(Box>), +} pub type SupportedInputConfigs = VecIntoIter; pub type SupportedOutputConfigs = VecIntoIter; pub type Devices = VecIntoIter; @@ -242,7 +246,7 @@ where error_callback, )) .open_stream()?; - Ok(Stream(Box::new(RefCell::new(stream)))) + Ok(Stream::Input(Box::new(RefCell::new(stream)))) } fn build_output_stream( @@ -266,7 +270,7 @@ where error_callback, )) .open_stream()?; - Ok(Stream(Box::new(RefCell::new(stream)))) + Ok(Stream::Output(Box::new(RefCell::new(stream)))) } impl DeviceTrait for Device { @@ -472,16 +476,28 @@ impl DeviceTrait for Device { impl StreamTrait for Stream { fn play(&self) -> Result<(), PlayStreamError> { - self.0 - .borrow_mut() - .request_start() - .map_err(PlayStreamError::from) + match self { + Self::Input(stream) => stream + .borrow_mut() + .request_start() + .map_err(PlayStreamError::from), + Self::Output(stream) => stream + .borrow_mut() + .request_start() + .map_err(PlayStreamError::from), + } } fn pause(&self) -> Result<(), PauseStreamError> { - self.0 - .borrow_mut() - .request_pause() - .map_err(PauseStreamError::from) + match self { + Self::Input(_) => Err(BackendSpecificError { + description: "Pause called on the input stream.".to_owned(), + } + .into()), + Self::Output(stream) => stream + .borrow_mut() + .request_pause() + .map_err(PauseStreamError::from), + } } } diff --git a/src/host/oboe/output_callback.rs b/src/host/oboe/output_callback.rs index 7b43ce8..625854f 100644 --- a/src/host/oboe/output_callback.rs +++ b/src/host/oboe/output_callback.rs @@ -31,7 +31,7 @@ impl CpalOutputCallback { fn make_callback_info( &self, - audio_stream: &mut dyn oboe::AudioOutputStream, + audio_stream: &mut dyn oboe::AudioOutputStreamSafe, ) -> OutputCallbackInfo { OutputCallbackInfo { timestamp: OutputStreamTimestamp { @@ -50,7 +50,7 @@ where fn on_audio_ready( &mut self, - audio_stream: &mut dyn oboe::AudioOutputStream, + audio_stream: &mut dyn oboe::AudioOutputStreamSafe, audio_data: &mut [<::FrameType as oboe::IsFrameType>::Type], ) -> oboe::DataCallbackResult { let cb_info = self.make_callback_info(audio_stream); @@ -74,7 +74,7 @@ where fn on_error_before_close( &mut self, - _audio_stream: &mut dyn oboe::AudioOutputStream, + _audio_stream: &mut dyn oboe::AudioOutputStreamSafe, error: oboe::Error, ) { (self.error_cb)(StreamError::from(error)) @@ -82,7 +82,7 @@ where fn on_error_after_close( &mut self, - _audio_stream: &mut dyn oboe::AudioOutputStream, + _audio_stream: &mut dyn oboe::AudioOutputStreamSafe, error: oboe::Error, ) { (self.error_cb)(StreamError::from(error))