2016-03-20 16:16:11 +00:00
|
|
|
use std::io;
|
|
|
|
|
|
|
|
pub trait Open {
|
2019-10-08 09:31:18 +00:00
|
|
|
fn open(_: Option<String>) -> Self;
|
2016-03-20 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Sink {
|
2016-08-01 19:20:17 +00:00
|
|
|
fn start(&mut self) -> io::Result<()>;
|
|
|
|
fn stop(&mut self) -> io::Result<()>;
|
|
|
|
fn write(&mut self, data: &[i16]) -> io::Result<()>;
|
2016-03-20 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 09:31:18 +00:00
|
|
|
fn mk_sink<S: Sink + Open + 'static>(device: Option<String>) -> Box<dyn Sink> {
|
2016-07-06 09:54:46 +00:00
|
|
|
Box::new(S::open(device))
|
2016-03-20 16:16:11 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 02:16:59 +00:00
|
|
|
#[cfg(feature = "alsa-backend")]
|
|
|
|
mod alsa;
|
|
|
|
#[cfg(feature = "alsa-backend")]
|
|
|
|
use self::alsa::AlsaSink;
|
|
|
|
|
2016-03-20 16:16:11 +00:00
|
|
|
#[cfg(feature = "portaudio-backend")]
|
|
|
|
mod portaudio;
|
2016-05-04 08:56:23 +00:00
|
|
|
#[cfg(feature = "portaudio-backend")]
|
|
|
|
use self::portaudio::PortAudioSink;
|
2016-03-20 16:16:11 +00:00
|
|
|
|
2016-03-20 19:16:32 +00:00
|
|
|
#[cfg(feature = "pulseaudio-backend")]
|
|
|
|
mod pulseaudio;
|
2016-05-04 08:56:23 +00:00
|
|
|
#[cfg(feature = "pulseaudio-backend")]
|
|
|
|
use self::pulseaudio::PulseAudioSink;
|
2016-03-20 19:16:32 +00:00
|
|
|
|
2017-10-05 18:41:02 +00:00
|
|
|
#[cfg(feature = "jackaudio-backend")]
|
|
|
|
mod jackaudio;
|
|
|
|
#[cfg(feature = "jackaudio-backend")]
|
|
|
|
use self::jackaudio::JackSink;
|
|
|
|
|
2019-03-20 13:24:03 +00:00
|
|
|
#[cfg(feature = "rodio-backend")]
|
|
|
|
mod rodio;
|
|
|
|
#[cfg(feature = "rodio-backend")]
|
|
|
|
use self::rodio::RodioSink;
|
2018-12-28 02:46:27 +00:00
|
|
|
#[cfg(feature = "sdl-backend")]
|
|
|
|
mod sdl;
|
|
|
|
#[cfg(feature = "sdl-backend")]
|
|
|
|
use self::sdl::SdlSink;
|
2018-11-15 19:34:13 +00:00
|
|
|
|
2016-12-31 12:17:06 +00:00
|
|
|
mod pipe;
|
|
|
|
use self::pipe::StdoutSink;
|
2016-03-20 19:16:32 +00:00
|
|
|
|
2019-10-08 09:31:18 +00:00
|
|
|
pub const BACKENDS: &'static [(&'static str, fn(Option<String>) -> Box<dyn Sink>)] = &[
|
2017-05-10 15:26:48 +00:00
|
|
|
#[cfg(feature = "alsa-backend")]
|
|
|
|
("alsa", mk_sink::<AlsaSink>),
|
|
|
|
#[cfg(feature = "portaudio-backend")]
|
|
|
|
("portaudio", mk_sink::<PortAudioSink>),
|
|
|
|
#[cfg(feature = "pulseaudio-backend")]
|
|
|
|
("pulseaudio", mk_sink::<PulseAudioSink>),
|
2017-10-05 18:41:02 +00:00
|
|
|
#[cfg(feature = "jackaudio-backend")]
|
|
|
|
("jackaudio", mk_sink::<JackSink>),
|
2019-03-20 13:24:03 +00:00
|
|
|
#[cfg(feature = "rodio-backend")]
|
|
|
|
("rodio", mk_sink::<RodioSink>),
|
2018-12-28 02:46:27 +00:00
|
|
|
#[cfg(feature = "sdl-backend")]
|
|
|
|
("sdl", mk_sink::<SdlSink>),
|
2017-05-10 15:26:48 +00:00
|
|
|
("pipe", mk_sink::<StdoutSink>),
|
|
|
|
];
|
2017-01-10 16:31:12 +00:00
|
|
|
|
2019-10-08 09:31:18 +00:00
|
|
|
pub fn find(name: Option<String>) -> Option<fn(Option<String>) -> Box<dyn Sink>> {
|
2017-04-28 22:24:55 +00:00
|
|
|
if let Some(name) = name {
|
2018-02-26 01:50:41 +00:00
|
|
|
BACKENDS
|
|
|
|
.iter()
|
|
|
|
.find(|backend| name == backend.0)
|
|
|
|
.map(|backend| backend.1)
|
2017-01-10 16:31:12 +00:00
|
|
|
} else {
|
2018-02-26 01:50:41 +00:00
|
|
|
Some(
|
|
|
|
BACKENDS
|
|
|
|
.first()
|
|
|
|
.expect("No backends were enabled at build time")
|
|
|
|
.1,
|
|
|
|
)
|
2017-01-10 16:31:12 +00:00
|
|
|
}
|
|
|
|
}
|