Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/host/pipewire/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ use crate::{

pub type Devices = std::vec::IntoIter<Device>;

const INIT_TIMEOUT: Duration = Duration::from_secs(2);

// This enum record whether it is created by human or just default device
#[derive(Clone, Debug, Default, Copy)]
pub(crate) enum Class {
Expand Down Expand Up @@ -320,9 +322,9 @@ impl DeviceTrait for Device {
let (pw_play_tx, pw_play_rx) = pw::channel::channel::<StreamCommand>();

let (pw_init_tx, pw_init_rx) = std::sync::mpsc::channel::<bool>();
let (ready_tx, ready_rx) = std::sync::mpsc::sync_channel::<()>(0);
let (ready_tx, ready_rx) = std::sync::mpsc::channel::<()>();
let device = self.clone();
let wait_timeout = timeout.unwrap_or(Duration::from_secs(2));
let wait_timeout = timeout.unwrap_or(INIT_TIMEOUT);
let initial_quantum = match config.buffer_size {
BufferSize::Fixed(n) => n as u64,
BufferSize::Default => self.quantum as u64,
Expand Down Expand Up @@ -482,9 +484,9 @@ impl DeviceTrait for Device {
let (pw_play_tx, pw_play_rx) = pw::channel::channel::<StreamCommand>();

let (pw_init_tx, pw_init_rx) = std::sync::mpsc::channel::<bool>();
let (ready_tx, ready_rx) = std::sync::mpsc::sync_channel::<()>(0);
let (ready_tx, ready_rx) = std::sync::mpsc::channel::<()>();
let device = self.clone();
let wait_timeout = timeout.unwrap_or(Duration::from_secs(2));
let wait_timeout = timeout.unwrap_or(INIT_TIMEOUT);
let initial_quantum = match config.buffer_size {
BufferSize::Fixed(n) => n as u64,
BufferSize::Default => self.quantum as u64,
Expand Down Expand Up @@ -960,7 +962,21 @@ pub fn init_devices() -> Option<Vec<Device>> {
})
.register();

// Guard against PipeWire daemons that accept a connection but never send `done` events.
let (cancel_tx, cancel_rx) = std::sync::mpsc::channel::<()>();
let (timeout_tx, timeout_rx) = pw::channel::channel::<()>();
let loop_quit = mainloop.clone();
let _timeout_watcher = timeout_rx.attach(mainloop.loop_(), move |_| {
loop_quit.quit();
});
thread::spawn(move || {
if cancel_rx.recv_timeout(INIT_TIMEOUT).is_err() {
let _ = timeout_tx.send(());
}
});

mainloop.run();
let _ = cancel_tx.send(());

// If PipeWire connected but discovered no real audio nodes, it cannot route any streams. Treat
// this as unavailable so the caller can fall back to PulseAudio or ALSA.
Expand Down
29 changes: 22 additions & 7 deletions src/host/pulseaudio/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use std::{sync::mpsc, time::Duration};

use futures::executor::block_on;
use pulseaudio::protocol;
Expand All @@ -16,6 +16,8 @@ use crate::{
SupportedStreamConfigRange,
};

const INIT_TIMEOUT: Duration = Duration::from_secs(2);

const MIN_SAMPLE_RATE: SampleRate = 8000;

const PULSE_FORMATS: &[SampleFormat] = &[
Expand Down Expand Up @@ -124,12 +126,25 @@ pub struct Host {

impl Host {
pub fn new() -> Result<Self, Error> {
let client = pulseaudio::Client::from_env(c"cpal-pulseaudio").map_err(|e| {
Error::with_message(
ErrorKind::HostUnavailable,
format!("PulseAudio unavailable: {e}"),
)
})?;
// `Client::from_env` does a blocking auth handshake with no socket timeout. If this never
// returns, fall through to the next host with no other option than to leak the thread.
let (tx, rx) = mpsc::channel();
std::thread::spawn(move || {
let _ = tx.send(pulseaudio::Client::from_env(c"cpal-pulseaudio"));
});
let client = rx
.recv_timeout(INIT_TIMEOUT)
.map_err(|err| match err {
mpsc::RecvTimeoutError::Timeout => Error::with_message(
ErrorKind::HostUnavailable,
"timed out waiting for PulseAudio",
),
mpsc::RecvTimeoutError::Disconnected => Error::with_message(
ErrorKind::HostUnavailable,
"PulseAudio initialization thread disconnected before sending a result",
),
})
.and_then(|r| r.map_err(Error::from))?;

Ok(Self { client })
}
Expand Down
Loading