Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Pipewire as a host #692

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ alsa = "0.6"
libc = "0.2"
parking_lot = "0.12"
jack = { version = "0.10", optional = true }
intmap = "2.0"
pipewire = { git = "https://gitlab.freedesktop.org/pipewire/pipewire-rs", optional = true }

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
core-foundation-sys = "0.8.2" # For linking to CoreFoundation.framework and handling device name `CFString`s.
Expand Down
41 changes: 38 additions & 3 deletions examples/beep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,26 @@ struct Opt {
#[arg(short, long)]
#[allow(dead_code)]
jack: bool,
/// Use the Pipewire host
#[cfg(all(
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd"
),
feature = "pipewire"
))]
#[allow(dead_code)]
pipewire: bool,
}

fn main() -> anyhow::Result<()> {
let opt = Opt::parse();

// Conditionally compile with jack if the feature is specified.
// Manually check for flags. Can be passed through cargo with -- e.g.
// cargo run --release --example beep --features jack -- --jack
#[cfg(all(
any(
target_os = "linux",
Expand All @@ -40,8 +54,6 @@ fn main() -> anyhow::Result<()> {
),
feature = "jack"
))]
// Manually check for flags. Can be passed through cargo with -- e.g.
// cargo run --release --example beep --features jack -- --jack
let host = if opt.jack {
cpal::host_from_id(cpal::available_hosts()
.into_iter()
Expand All @@ -53,14 +65,37 @@ fn main() -> anyhow::Result<()> {
cpal::default_host()
};

// Conditionally compile with PipeWire if the feature is specified.
#[cfg(all(
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd"
),
feature = "pipewire"
))]
// Manually check for flags. Can be passed through cargo with -- e.g.
// cargo run --release --example beep --features pipewire -- --pipewire
let host = if opt.pipewire {
cpal::host_from_id(cpal::available_hosts()
.into_iter()
.find(|id| *id == cpal::HostId::PipeWire)
.expect(
"make sure --features pipewire is specified. only works on OSes where PipeWire is available",
)).expect("PipeWire host unavailable")
} else {
cpal::default_host()
};

#[cfg(any(
not(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd"
)),
not(feature = "jack")
not(any(feature = "jack", feature = "pipewire"))
))]
let host = cpal::default_host();

Expand Down
5 changes: 5 additions & 0 deletions src/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ pub(crate) mod jack;
pub(crate) mod null;
#[cfg(target_os = "android")]
pub(crate) mod oboe;
#[cfg(all(
any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"),
feature = "pipewire"
))]
pub(crate) mod pipewire;
#[cfg(windows)]
pub(crate) mod wasapi;
#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
Expand Down
Loading