Merge pull request #6 from brain0/work

Make librespot more robust against audio failures
This commit is contained in:
Colm 2017-12-19 21:46:22 +00:00 committed by GitHub
commit 5302bd1072
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 153 additions and 49 deletions

1
Cargo.lock generated
View file

@ -264,6 +264,7 @@ dependencies = [
"futures 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
"hyper 0.11.2 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.11.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
"libpulse-sys 0.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "libpulse-sys 0.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"librespot-audio 0.1.0", "librespot-audio 0.1.0",
"librespot-core 0.1.0", "librespot-core 0.1.0",

View file

@ -52,6 +52,7 @@ url = "1.3"
alsa = { git = "https://github.com/plietar/rust-alsa", optional = true } alsa = { git = "https://github.com/plietar/rust-alsa", optional = true }
portaudio-rs = { version = "0.3.0", optional = true } portaudio-rs = { version = "0.3.0", optional = true }
libpulse-sys = { version = "0.0.0", optional = true } libpulse-sys = { version = "0.0.0", optional = true }
libc = { version = "0.2", optional = true }
[build-dependencies] [build-dependencies]
rand = "0.3.13" rand = "0.3.13"
@ -61,7 +62,7 @@ protobuf_macros = { git = "https://github.com/plietar/rust-protobuf-macros", fea
[features] [features]
alsa-backend = ["alsa"] alsa-backend = ["alsa"]
portaudio-backend = ["portaudio-rs"] portaudio-backend = ["portaudio-rs"]
pulseaudio-backend = ["libpulse-sys"] pulseaudio-backend = ["libpulse-sys", "libc"]
with-tremor = ["librespot-audio/with-tremor"] with-tremor = ["librespot-audio/with-tremor"]
with-lewton = ["librespot-audio/with-lewton"] with-lewton = ["librespot-audio/with-lewton"]

View file

@ -1 +1,2 @@
#[allow(unused_mut)]
pub mod connection; pub mod connection;

View file

@ -2,8 +2,10 @@ use super::{Open, Sink};
use std::io; use std::io;
use libpulse_sys::*; use libpulse_sys::*;
use std::ptr::{null, null_mut}; use std::ptr::{null, null_mut};
use std::mem::{transmute};
use std::ffi::CString; use std::ffi::CString;
use std::ffi::CStr;
use std::mem;
use libc;
pub struct PulseAudioSink { pub struct PulseAudioSink {
s : *mut pa_simple, s : *mut pa_simple,
@ -12,6 +14,39 @@ pub struct PulseAudioSink {
desc : CString desc : CString
} }
fn call_pulseaudio<T, F, FailCheck>(f: F, fail_check: FailCheck, kind: io::ErrorKind) -> io::Result<T> where
T: Copy,
F: Fn(*mut libc::c_int) -> T,
FailCheck: Fn(T) -> bool,
{
let mut error: libc::c_int = 0;
let ret = f(&mut error);
if fail_check(ret) {
let err_cstr = unsafe { CStr::from_ptr(pa_strerror(error)) };
let errstr = err_cstr.to_string_lossy().into_owned();
Err(io::Error::new(kind, errstr))
} else {
Ok(ret)
}
}
impl PulseAudioSink {
fn free_connection(&mut self) {
if self.s != null_mut() {
unsafe {
pa_simple_free(self.s);
}
self.s = null_mut();
}
}
}
impl Drop for PulseAudioSink {
fn drop(&mut self) {
self.free_connection();
}
}
impl Open for PulseAudioSink { impl Open for PulseAudioSink {
fn open(device: Option<String>) -> PulseAudioSink { fn open(device: Option<String>) -> PulseAudioSink {
debug!("Using PulseAudio sink"); debug!("Using PulseAudio sink");
@ -27,7 +62,7 @@ impl Open for PulseAudioSink {
}; };
let name = CString::new("librespot").unwrap(); let name = CString::new("librespot").unwrap();
let description = CString::new("A spoty client library").unwrap(); let description = CString::new("Spotify endpoint").unwrap();
PulseAudioSink { PulseAudioSink {
s: null_mut(), s: null_mut(),
@ -41,7 +76,8 @@ impl Open for PulseAudioSink {
impl Sink for PulseAudioSink { impl Sink for PulseAudioSink {
fn start(&mut self) -> io::Result<()> { fn start(&mut self) -> io::Result<()> {
if self.s == null_mut() { if self.s == null_mut() {
self.s = unsafe { self.s = call_pulseaudio(
|err| unsafe {
pa_simple_new(null(), // Use the default server. pa_simple_new(null(), // Use the default server.
self.name.as_ptr(), // Our application's name. self.name.as_ptr(), // Our application's name.
PA_STREAM_PLAYBACK, PA_STREAM_PLAYBACK,
@ -50,29 +86,33 @@ impl Sink for PulseAudioSink {
&self.ss, // Our sample format. &self.ss, // Our sample format.
null(), // Use default channel map null(), // Use default channel map
null(), // Use default buffering attributes. null(), // Use default buffering attributes.
null_mut(), // Ignore error code. err)
) },
}; |ptr| ptr == null_mut(),
assert!(self.s != null_mut()); io::ErrorKind::ConnectionRefused)?;
} }
Ok(()) Ok(())
} }
fn stop(&mut self) -> io::Result<()> { fn stop(&mut self) -> io::Result<()> {
unsafe { self.free_connection();
pa_simple_free(self.s);
}
self.s = null_mut();
Ok(()) Ok(())
} }
fn write(&mut self, data: &[i16]) -> io::Result<()> { fn write(&mut self, data: &[i16]) -> io::Result<()> {
unsafe { if self.s == null_mut() {
let ptr = transmute(data.as_ptr()); Err(io::Error::new(io::ErrorKind::NotConnected, "Not connected to pulseaudio"))
let bytes = data.len() as usize * 2; }
pa_simple_write(self.s, ptr, bytes, null_mut()); else {
}; let ptr = data.as_ptr() as *const libc::c_void;
let len = data.len() as usize * mem::size_of::<i16>();
call_pulseaudio(
|err| unsafe {
pa_simple_write(self.s, ptr, len, err)
},
|ret| ret < 0,
io::ErrorKind::BrokenPipe)?;
Ok(()) Ok(())
} }
} }
}

View file

@ -1 +1,2 @@
#[allow(unused_mut)]
pub mod spirc; pub mod spirc;

View file

@ -34,6 +34,9 @@ extern crate portaudio_rs;
#[cfg(feature = "libpulse-sys")] #[cfg(feature = "libpulse-sys")]
extern crate libpulse_sys; extern crate libpulse_sys;
#[cfg(feature = "libc")]
extern crate libc;
pub mod audio_backend; pub mod audio_backend;
pub mod discovery; pub mod discovery;
pub mod keymaster; pub mod keymaster;

View file

@ -2,8 +2,9 @@ use futures::sync::oneshot;
use futures::{future, Future}; use futures::{future, Future};
use std::borrow::Cow; use std::borrow::Cow;
use std::mem; use std::mem;
use std::sync::mpsc::{RecvError, TryRecvError}; use std::sync::mpsc::{RecvError, TryRecvError, RecvTimeoutError};
use std::thread; use std::thread;
use std::time::Duration;
use std; use std;
use core::config::{Bitrate, PlayerConfig}; use core::config::{Bitrate, PlayerConfig};
@ -16,9 +17,9 @@ use audio::{VorbisDecoder, VorbisPacket};
use metadata::{FileFormat, Track, Metadata}; use metadata::{FileFormat, Track, Metadata};
use mixer::AudioFilter; use mixer::AudioFilter;
#[derive(Clone)]
pub struct Player { pub struct Player {
commands: std::sync::mpsc::Sender<PlayerCommand>, commands: Option<std::sync::mpsc::Sender<PlayerCommand>>,
thread_handle: Option<thread::JoinHandle<()>>,
} }
struct PlayerInternal { struct PlayerInternal {
@ -28,6 +29,7 @@ struct PlayerInternal {
state: PlayerState, state: PlayerState,
sink: Box<Sink>, sink: Box<Sink>,
sink_running: bool,
audio_filter: Option<Box<AudioFilter + Send>>, audio_filter: Option<Box<AudioFilter + Send>>,
} }
@ -47,7 +49,7 @@ impl Player {
{ {
let (cmd_tx, cmd_rx) = std::sync::mpsc::channel(); let (cmd_tx, cmd_rx) = std::sync::mpsc::channel();
thread::spawn(move || { let handle = thread::spawn(move || {
debug!("new Player[{}]", session.session_id()); debug!("new Player[{}]", session.session_id());
let internal = PlayerInternal { let internal = PlayerInternal {
@ -57,6 +59,7 @@ impl Player {
state: PlayerState::Stopped, state: PlayerState::Stopped,
sink: sink_builder(), sink: sink_builder(),
sink_running: false,
audio_filter: audio_filter, audio_filter: audio_filter,
}; };
@ -64,12 +67,13 @@ impl Player {
}); });
Player { Player {
commands: cmd_tx, commands: Some(cmd_tx),
thread_handle: Some(handle),
} }
} }
fn command(&self, cmd: PlayerCommand) { fn command(&self, cmd: PlayerCommand) {
self.commands.send(cmd).unwrap(); self.commands.as_ref().unwrap().send(cmd).unwrap();
} }
pub fn load(&self, track: SpotifyId, start_playing: bool, position_ms: u32) pub fn load(&self, track: SpotifyId, start_playing: bool, position_ms: u32)
@ -98,6 +102,19 @@ impl Player {
} }
} }
impl Drop for Player {
fn drop(&mut self) {
debug!("Shutting down player thread ...");
self.commands = None;
if let Some(handle) = self.thread_handle.take() {
match handle.join() {
Ok(_) => (),
Err(_) => error!("Player thread panicked!")
}
}
}
}
type Decoder = VorbisDecoder<Subfile<AudioDecrypt<AudioFile>>>; type Decoder = VorbisDecoder<Subfile<AudioDecrypt<AudioFile>>>;
enum PlayerState { enum PlayerState {
Stopped, Stopped,
@ -177,11 +194,22 @@ impl PlayerInternal {
fn run(mut self) { fn run(mut self) {
loop { loop {
let cmd = if self.state.is_playing() { let cmd = if self.state.is_playing() {
if self.sink_running
{
match self.commands.try_recv() { match self.commands.try_recv() {
Ok(cmd) => Some(cmd), Ok(cmd) => Some(cmd),
Err(TryRecvError::Empty) => None, Err(TryRecvError::Empty) => None,
Err(TryRecvError::Disconnected) => return, Err(TryRecvError::Disconnected) => return,
} }
}
else
{
match self.commands.recv_timeout(Duration::from_secs(5)) {
Ok(cmd) => Some(cmd),
Err(RecvTimeoutError::Timeout) => None,
Err(RecvTimeoutError::Disconnected) => return,
}
}
} else { } else {
match self.commands.recv() { match self.commands.recv() {
Ok(cmd) => Some(cmd), Ok(cmd) => Some(cmd),
@ -193,15 +221,41 @@ impl PlayerInternal {
self.handle_command(cmd); self.handle_command(cmd);
} }
if self.state.is_playing() && ! self.sink_running {
self.start_sink();
}
if self.sink_running {
let packet = if let PlayerState::Playing { ref mut decoder, .. } = self.state { let packet = if let PlayerState::Playing { ref mut decoder, .. } = self.state {
Some(decoder.next_packet().expect("Vorbis error")) Some(decoder.next_packet().expect("Vorbis error"))
} else { None }; } else {
None
};
if let Some(packet) = packet { if let Some(packet) = packet {
self.handle_packet(packet); self.handle_packet(packet);
} }
} }
} }
}
fn start_sink(&mut self) {
match self.sink.start() {
Ok(()) => self.sink_running = true,
Err(err) => error!("Could not start audio: {}", err),
}
}
fn stop_sink_if_running(&mut self) {
if self.sink_running {
self.stop_sink();
}
}
fn stop_sink(&mut self) {
self.sink.stop().unwrap();
self.sink_running = false;
}
fn handle_packet(&mut self, packet: Option<VorbisPacket>) { fn handle_packet(&mut self, packet: Option<VorbisPacket>) {
match packet { match packet {
@ -210,11 +264,14 @@ impl PlayerInternal {
editor.modify_stream(&mut packet.data_mut()) editor.modify_stream(&mut packet.data_mut())
}; };
self.sink.write(&packet.data()).unwrap(); if let Err(err) = self.sink.write(&packet.data()) {
error!("Could not write audio: {}", err);
self.stop_sink();
}
} }
None => { None => {
self.sink.stop().unwrap(); self.stop_sink();
self.run_onstop(); self.run_onstop();
let old_state = mem::replace(&mut self.state, PlayerState::Stopped); let old_state = mem::replace(&mut self.state, PlayerState::Stopped);
@ -228,7 +285,7 @@ impl PlayerInternal {
match cmd { match cmd {
PlayerCommand::Load(track_id, play, position, end_of_track) => { PlayerCommand::Load(track_id, play, position, end_of_track) => {
if self.state.is_playing() { if self.state.is_playing() {
self.sink.stop().unwrap(); self.stop_sink_if_running();
} }
match self.load_track(track_id, position as i64) { match self.load_track(track_id, position as i64) {
@ -237,7 +294,7 @@ impl PlayerInternal {
if !self.state.is_playing() { if !self.state.is_playing() {
self.run_onstart(); self.run_onstart();
} }
self.sink.start().unwrap(); self.start_sink();
self.state = PlayerState::Playing { self.state = PlayerState::Playing {
decoder: decoder, decoder: decoder,
@ -280,7 +337,7 @@ impl PlayerInternal {
self.state.paused_to_playing(); self.state.paused_to_playing();
self.run_onstart(); self.run_onstart();
self.sink.start().unwrap(); self.start_sink();
} else { } else {
warn!("Player::play called from invalid state"); warn!("Player::play called from invalid state");
} }
@ -290,7 +347,7 @@ impl PlayerInternal {
if let PlayerState::Playing { .. } = self.state { if let PlayerState::Playing { .. } = self.state {
self.state.playing_to_paused(); self.state.playing_to_paused();
self.sink.stop().unwrap(); self.stop_sink_if_running();
self.run_onstop(); self.run_onstop();
} else { } else {
warn!("Player::pause called from invalid state"); warn!("Player::pause called from invalid state");
@ -300,7 +357,7 @@ impl PlayerInternal {
PlayerCommand::Stop => { PlayerCommand::Stop => {
match self.state { match self.state {
PlayerState::Playing { .. } => { PlayerState::Playing { .. } => {
self.sink.stop().unwrap(); self.stop_sink_if_running();
self.run_onstop(); self.run_onstop();
self.state = PlayerState::Stopped; self.state = PlayerState::Stopped;
} }