From 5ca2c35dbc815d2a0768889176d6c8cda5ae062f Mon Sep 17 00:00:00 2001 From: Marcus Thiesen Date: Sun, 20 Mar 2016 20:16:32 +0100 Subject: [PATCH 1/3] Pulseaudio backend --- .travis.yml | 4 ++- Cargo.lock | 9 +++++ Cargo.toml | 2 ++ README.md | 3 +- src/audio_backend/mod.rs | 7 ++++ src/audio_backend/pulseaudio.rs | 63 +++++++++++++++++++++++++++++++++ src/lib.rs | 3 ++ 7 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 src/audio_backend/pulseaudio.rs diff --git a/.travis.yml b/.travis.yml index a9327e91..daba5a5b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,12 +10,14 @@ addons: packages: - libprotoc-dev - portaudio19-dev - + - libpulse-dev + script: - cargo build --no-default-features --features "with-syntex" - cargo build --no-default-features --features "with-syntex with-tremor" - cargo build --no-default-features --features "with-syntex facebook" - cargo build --no-default-features --features "with-syntex portaudio-backend" + - cargo build --no-default-features --features "with-syntex pulseaudio-backend" # Building without syntex only works on nightly - if [[ $TRAVIS_RUST_VERSION == *"nightly"* ]]; then cargo build --no-default-features; diff --git a/Cargo.lock b/Cargo.lock index 5006aa19..64d6335f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,7 @@ dependencies = [ "hyper 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "json_macros 0.3.0 (git+https://github.com/plietar/json_macros)", "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", + "libpulse-sys 0.0.0 (git+https://github.com/astro/libpulse-sys)", "librespot-protocol 0.1.0", "lmdb-rs 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "num 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)", @@ -233,6 +234,14 @@ dependencies = [ "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "libpulse-sys" +version = "0.0.0" +source = "git+https://github.com/astro/libpulse-sys#3e167c0d75dae1ce3946eec7526f6afd92adda0f" +dependencies = [ + "libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "librespot-protocol" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 9dda1a25..82eeae42 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,6 +41,7 @@ tremor = { git = "https://github.com/plietar/rust-tremor", optional = t dns-sd = { version = "~0.1.1", optional = true } portaudio = { git = "https://github.com/mvdnes/portaudio-rs", optional = true } +libpulse-sys = { git = "https://github.com/astro/libpulse-sys", optional = true } json_macros = { git = "https://github.com/plietar/json_macros" } protobuf_macros = { git = "https://github.com/plietar/rust-protobuf-macros" } @@ -61,5 +62,6 @@ with-syntex = ["syntex", "protobuf_macros/with-syntex", "json_macros/with- with-tremor = ["tremor"] facebook = ["hyper/ssl", "openssl"] portaudio-backend = ["portaudio"] +pulseaudio-backend= ["libpulse-sys"] static-appkey = [] default = ["with-syntex"] diff --git a/README.md b/README.md index 5b93de64..c70dd38b 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,8 @@ target/release/librespot [...] --backend portaudio ``` The following backends are currently available : -- PortAudio +- PortAudio +- PulseAudio (feature pulseaudio-backend, --backend pulseaudio) ## Development When developing *librespot*, it is preferable to use Rust nightly, and build it using the following : diff --git a/src/audio_backend/mod.rs b/src/audio_backend/mod.rs index 93150d1c..ad7dd441 100644 --- a/src/audio_backend/mod.rs +++ b/src/audio_backend/mod.rs @@ -57,9 +57,16 @@ fn mk_sink() -> Box { #[cfg(feature = "portaudio-backend")] mod portaudio; +#[cfg(feature = "pulseaudio-backend")] +mod pulseaudio; + + declare_backends! { pub const BACKENDS : &'static [(&'static str, &'static (Fn() -> Box + Sync + Send + 'static))] = &[ #[cfg(feature = "portaudio-backend")] ("portaudio", &mk_sink::), + #[cfg(feature = "pulseaudio-backend")] + ("pulseaudio", &mk_sink::), + ]; } diff --git a/src/audio_backend/pulseaudio.rs b/src/audio_backend/pulseaudio.rs new file mode 100644 index 00000000..9333ab45 --- /dev/null +++ b/src/audio_backend/pulseaudio.rs @@ -0,0 +1,63 @@ +use super::{Open, Sink}; +use std::io; +use libpulse_sys::*; +use std::ptr::{null, null_mut}; +use std::mem::{transmute}; +use std::ffi::CString; + +pub struct PulseAudioSink(*mut pa_simple); + +impl Open for PulseAudioSink { + fn open() -> PulseAudioSink { + println!("Using PulseAudioSink"); + + let ss = pa_sample_spec { + format: PA_SAMPLE_S16LE, + channels: 2, // stereo + rate: 44100 + }; + + let name = CString::new("librespot").unwrap(); + let description = CString::new("A spoty client library").unwrap(); + + let s = unsafe { + pa_simple_new(null(), // Use the default server. + name.as_ptr(), // Our application's name. + PA_STREAM_PLAYBACK, + null(), // Use the default device. + description.as_ptr(), // Description of our stream. + &ss, // Our sample format. + null(), // Use default channel map + null(), // Use default buffering attributes. + null_mut(), // Ignore error code. + ) + }; + assert!(s != null_mut()); + + PulseAudioSink(s) + } +} + +impl Sink for PulseAudioSink { + fn start(&self) -> io::Result<()> { + Ok(()) + } + + fn stop(&self) -> io::Result<()> { + Ok(()) + } + + fn write(&self, data: &[i16]) -> io::Result<()> { + unsafe { + let ptr = transmute(data.as_ptr()); + let bytes = data.len() as u64 * 2; + pa_simple_write(self.0, ptr, bytes, null_mut()); + }; + + Ok(()) + } +} + + + + diff --git a/src/lib.rs b/src/lib.rs index 6a4185f3..5c293a31 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,9 @@ extern crate openssl; #[cfg(feature = "portaudio")] extern crate portaudio; +#[cfg(feature = "libpulse-sys")] +extern crate libpulse_sys; + extern crate librespot_protocol as protocol; // This doesn't play nice with syntex, so place it here From f587b375e8afb132524291543fb231d9ba8b5b08 Mon Sep 17 00:00:00 2001 From: Marcus Thiesen Date: Mon, 21 Mar 2016 10:04:18 +0100 Subject: [PATCH 2/3] Update dependencies --- Cargo.lock | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 64d6335f..0eed2e42 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = "0.1.0" dependencies = [ "bit-set 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "clippy 0.0.54 (registry+https://github.com/rust-lang/crates.io-index)", + "clippy 0.0.55 (registry+https://github.com/rust-lang/crates.io-index)", "dns-sd 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "eventual 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", @@ -94,7 +94,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "clippy" -version = "0.0.54" +version = "0.0.55" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "regex-syntax 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -135,7 +135,7 @@ dependencies = [ [[package]] name = "gcc" -version = "0.3.25" +version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -239,7 +239,7 @@ name = "libpulse-sys" version = "0.0.0" source = "git+https://github.com/astro/libpulse-sys#3e167c0d75dae1ce3946eec7526f6afd92adda0f" dependencies = [ - "libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -327,7 +327,7 @@ name = "ogg-sys" version = "0.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)", + "gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -338,7 +338,7 @@ version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)", + "gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-sys 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -362,7 +362,7 @@ name = "openssl-sys-extras" version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)", + "gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-sys 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -409,7 +409,7 @@ name = "protobuf_build" version = "0.1.1" source = "git+https://github.com/plietar/rust-protobuf-build.git#77ea08e75b66433104a035309751d48170a7aed6" dependencies = [ - "gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)", + "gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "protobuf 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)", @@ -483,7 +483,7 @@ name = "rust-crypto" version = "0.2.34" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)", + "gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", @@ -539,7 +539,7 @@ name = "shannon-sys" version = "0.1.0" source = "git+https://github.com/plietar/rust-shannon#7000b3e49a53daaa890727ba2b2bd5a43cc4ffef" dependencies = [ - "gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)", + "gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -655,7 +655,7 @@ name = "tremor-sys" version = "0.1.0" source = "git+https://github.com/plietar/rust-tremor#5ced876f3cffb041fcf31238da7f3273178029fe" dependencies = [ - "gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)", + "gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "ogg-sys 0.0.9 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -751,7 +751,7 @@ name = "vorbis-sys" version = "0.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)", + "gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "ogg-sys 0.0.9 (registry+https://github.com/rust-lang/crates.io-index)", "pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -762,7 +762,7 @@ name = "vorbisfile-sys" version = "0.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)", + "gcc 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "ogg-sys 0.0.9 (registry+https://github.com/rust-lang/crates.io-index)", "pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", From 11e62d56d890b02d5f20cd38fe83fa50dcd15d50 Mon Sep 17 00:00:00 2001 From: Marcus Thiesen Date: Mon, 21 Mar 2016 10:13:28 +0100 Subject: [PATCH 3/3] Use usize for pointer size --- src/audio_backend/pulseaudio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/audio_backend/pulseaudio.rs b/src/audio_backend/pulseaudio.rs index 9333ab45..c76643b0 100644 --- a/src/audio_backend/pulseaudio.rs +++ b/src/audio_backend/pulseaudio.rs @@ -50,7 +50,7 @@ impl Sink for PulseAudioSink { fn write(&self, data: &[i16]) -> io::Result<()> { unsafe { let ptr = transmute(data.as_ptr()); - let bytes = data.len() as u64 * 2; + let bytes = data.len() as usize * 2; pa_simple_write(self.0, ptr, bytes, null_mut()); };