Browse Source

Pulseaudio backend

Marcus Thiesen 9 years ago
parent
commit
5ca2c35dbc
7 changed files with 89 additions and 2 deletions
  1. 3 1
      .travis.yml
  2. 9 0
      Cargo.lock
  3. 2 0
      Cargo.toml
  4. 2 1
      README.md
  5. 7 0
      src/audio_backend/mod.rs
  6. 63 0
      src/audio_backend/pulseaudio.rs
  7. 3 0
      src/lib.rs

+ 3 - 1
.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;

+ 9 - 0
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"

+ 2 - 0
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"]

+ 2 - 1
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 :

+ 7 - 0
src/audio_backend/mod.rs

@@ -57,9 +57,16 @@ fn mk_sink<S: Sink + Open + 'static>() -> Box<Sink> {
 #[cfg(feature = "portaudio-backend")]
 mod portaudio;
 
+#[cfg(feature = "pulseaudio-backend")]
+mod pulseaudio;
+
+
 declare_backends! {
     pub const BACKENDS : &'static [(&'static str, &'static (Fn() -> Box<Sink> + Sync + Send + 'static))] = &[
         #[cfg(feature = "portaudio-backend")]
         ("portaudio", &mk_sink::<self::portaudio::PortAudioSink>),
+        #[cfg(feature = "pulseaudio-backend")]
+        ("pulseaudio", &mk_sink::<self::pulseaudio::PulseAudioSink>),
+
     ];
 }

+ 63 - 0
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(())
+    }
+}
+
+
+
+

+ 3 - 0
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