mirror of
https://github.com/librespot-org/librespot.git
synced 2025-01-07 17:24:04 +00:00
64 lines
1.7 KiB
Rust
64 lines
1.7 KiB
Rust
|
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(())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|