diff --git a/playback/src/audio_backend/gstreamer.rs b/playback/src/audio_backend/gstreamer.rs index 4678bfb07..e2b52d744 100644 --- a/playback/src/audio_backend/gstreamer.rs +++ b/playback/src/audio_backend/gstreamer.rs @@ -1,7 +1,9 @@ use super::{Open, Sink}; use crate::audio::AudioPacket; + use gst::prelude::*; -use gst::*; +use gstreamer as gst; +use gstreamer_app as gst_app; use zerocopy::*; use std::sync::mpsc::{sync_channel, SyncSender}; @@ -52,14 +54,13 @@ impl Open for GstreamerSink { thread::spawn(move || { for data in rx { let buffer = bufferpool.acquire_buffer(None); - if !buffer.is_err() { - let mut okbuffer = buffer.unwrap(); - let mutbuf = okbuffer.make_mut(); + if let Ok(mut buffer) = buffer { + let mutbuf = buffer.make_mut(); mutbuf.set_size(data.len()); mutbuf .copy_from_slice(0, data.as_bytes()) .expect("Failed to copy from slice"); - let _eat = appsrc.push_buffer(okbuffer); + let _eat = appsrc.push_buffer(buffer); } } }); @@ -69,8 +70,8 @@ impl Open for GstreamerSink { let watch_mainloop = thread_mainloop.clone(); bus.add_watch(move |_, msg| { match msg.view() { - MessageView::Eos(..) => watch_mainloop.quit(), - MessageView::Error(err) => { + gst::MessageView::Eos(..) => watch_mainloop.quit(), + gst::MessageView::Error(err) => { println!( "Error from {:?}: {} ({:?})", err.get_src().map(|s| s.get_path_string()), diff --git a/playback/src/audio_backend/mod.rs b/playback/src/audio_backend/mod.rs index c816a6d66..5fa518ee1 100644 --- a/playback/src/audio_backend/mod.rs +++ b/playback/src/audio_backend/mod.rs @@ -11,9 +11,9 @@ pub trait Sink { fn write(&mut self, packet: &AudioPacket) -> io::Result<()>; } -pub type SinkBuilder = fn(Option) -> Box; +pub type SinkBuilder = fn(Option) -> Box; -fn mk_sink(device: Option) -> Box { +fn mk_sink(device: Option) -> Box { Box::new(S::open(device)) } diff --git a/playback/src/audio_backend/rodio.rs b/playback/src/audio_backend/rodio.rs index 338dfbbfe..8e4482114 100644 --- a/playback/src/audio_backend/rodio.rs +++ b/playback/src/audio_backend/rodio.rs @@ -1,5 +1,4 @@ use std::process::exit; -use std::{convert::Infallible, sync::mpsc}; use std::{io, thread, time}; use cpal::traits::{DeviceTrait, HostTrait}; @@ -15,12 +14,12 @@ use crate::audio::AudioPacket; compile_error!("Rodio JACK backend is currently only supported on linux."); #[cfg(feature = "rodio-backend")] -pub fn mk_rodio(device: Option) -> Box { +pub fn mk_rodio(device: Option) -> Box { Box::new(open(cpal::default_host(), device)) } #[cfg(feature = "rodiojack-backend")] -pub fn mk_rodiojack(device: Option) -> Box { +pub fn mk_rodiojack(device: Option) -> Box { Box::new(open( cpal::host_from_id(cpal::HostId::Jack).unwrap(), device, @@ -43,8 +42,7 @@ pub enum RodioError { pub struct RodioSink { rodio_sink: rodio::Sink, - // will produce a TryRecvError on the receiver side when it is dropped. - _close_tx: mpsc::SyncSender, + _stream: rodio::OutputStream, } fn list_formats(device: &rodio::Device) { @@ -152,29 +150,12 @@ fn create_sink( pub fn open(host: cpal::Host, device: Option) -> RodioSink { debug!("Using rodio sink with cpal host: {}", host.id().name()); - let (sink_tx, sink_rx) = mpsc::sync_channel(1); - let (close_tx, close_rx) = mpsc::sync_channel(1); - - std::thread::spawn(move || match create_sink(&host, device) { - Ok((sink, stream)) => { - sink_tx.send(Ok(sink)).unwrap(); - - close_rx.recv().unwrap_err(); // This will fail as soon as the sender is dropped - debug!("drop rodio::OutputStream"); - drop(stream); - } - Err(e) => { - sink_tx.send(Err(e)).unwrap(); - } - }); - - // Instead of the second `unwrap`, better error handling could be introduced - let sink = sink_rx.recv().unwrap().unwrap(); + let (sink, stream) = create_sink(&host, device).unwrap(); debug!("Rodio sink was created"); RodioSink { rodio_sink: sink, - _close_tx: close_tx, + _stream: stream, } } diff --git a/playback/src/player.rs b/playback/src/player.rs index 0d2380e73..497a4a115 100644 --- a/playback/src/player.rs +++ b/playback/src/player.rs @@ -50,7 +50,7 @@ struct PlayerInternal { state: PlayerState, preload: PlayerPreload, - sink: Box, + sink: Box, sink_status: SinkStatus, sink_event_callback: Option, audio_filter: Option>, @@ -242,7 +242,7 @@ impl Player { sink_builder: F, ) -> (Player, PlayerEventChannel) where - F: FnOnce() -> Box + Send + 'static, + F: FnOnce() -> Box + Send + 'static, { let (cmd_tx, cmd_rx) = mpsc::unbounded_channel(); let (event_sender, event_receiver) = mpsc::unbounded_channel(); diff --git a/src/main.rs b/src/main.rs index cd2a78613..54057aed5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -105,7 +105,7 @@ fn print_version() { #[derive(Clone)] struct Setup { - backend: fn(Option) -> Box, + backend: fn(Option) -> Box, device: Option, mixer: fn(Option) -> Box,