Skip to content

PipeWire implementation #938

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

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions .github/workflows/cpal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
run: sudo apt-get install libasound2-dev
- name: Install libjack
run: sudo apt-get install libjack-jackd2-dev libjack-jackd2-0
- name: Install libpipewire
run: sudo apt-get install libpipewire-0.3-dev
- name: Install stable
uses: dtolnay/rust-toolchain@stable
with:
Expand Down Expand Up @@ -67,6 +69,8 @@ jobs:
run: sudo apt-get install libasound2-dev
- name: Install libjack
run: sudo apt-get install libjack-jackd2-dev libjack-jackd2-0
- name: Install libpipewire
run: sudo apt-get install libpipewire-0.3-dev
- name: Install stable
uses: dtolnay/rust-toolchain@stable
- name: Run without features
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/target
/Cargo.lock
target/
Cargo.lock
.cargo/
.DS_Store
recorded.wav
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ rust-version = "1.70"

[features]
asio = ["asio-sys", "num-traits"] # Only available on Windows. See README for setup instructions.
jack = ["dep:jack"]
pipewire = ["dep:pipewire-client", "dep:tokio"]

[dependencies]
dasp_sample = "0.11"
Expand Down Expand Up @@ -42,6 +44,8 @@ num-traits = { version = "0.2.6", optional = true }
alsa = "0.9"
libc = "0.2"
jack = { version = "0.13.0", optional = true }
pipewire-client = { version = "0.1", git = "https://github.com/midoriiro/pipewire-client.git", optional = true }
tokio = { version = "1.43", features = ["full"], optional = true } # For PipeWire client, will change in the future. client will provide sync and async api.

[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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This library currently supports the following:

Currently, supported hosts include:

- Linux (via ALSA or JACK)
- Linux (via ALSA, JACK or PipeWire)
- Windows (via WASAPI by default, see ASIO instructions below)
- macOS (via CoreAudio)
- iOS (via CoreAudio)
Expand All @@ -27,6 +27,10 @@ Note that on Linux, the ALSA development files are required. These are provided
as part of the `libasound2-dev` package on Debian and Ubuntu distributions and
`alsa-lib-devel` on Fedora.

When building with the `pipewire` feature flag, development files for PipeWire and Clang are required:
- On Debian and Ubuntu: install the `libpipewire-0.3-dev` and `libclang-19-dev` packages.
- On Fedora: install the `pipewire-devel` and `clang-devel` packages.

## Compiling for Web Assembly

If you are interested in using CPAL with WASM, please see [this guide](https://github.com/RustAudio/cpal/wiki/Setting-up-a-new-CPAL-WASM-project) in our Wiki which walks through setting up a new project from scratch.
Expand All @@ -36,6 +40,7 @@ If you are interested in using CPAL with WASM, please see [this guide](https://g
Some audio backends are optional and will only be compiled with a [feature flag](https://doc.rust-lang.org/cargo/reference/features.html).

- JACK (on Linux): `jack`
- PipeWire (on Linux): `pipewire` (currently in testing, feel free to share your feedback!)
- ASIO (on Windows): `asio`

## ASIO on Windows
Expand Down
42 changes: 41 additions & 1 deletion examples/beep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ 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"
))]
#[arg(short, long)]
#[allow(dead_code)]
pipewire: bool,
}

fn main() -> anyhow::Result<()> {
Expand Down Expand Up @@ -52,14 +66,40 @@ 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
45 changes: 45 additions & 0 deletions examples/synth_tones.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,51 @@ where

pub fn host_device_setup(
) -> Result<(cpal::Host, cpal::Device, cpal::SupportedStreamConfig), anyhow::Error> {
#[cfg(all(
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd"
),
feature = "jack"
))]
// Manually check for flags. Can be passed through cargo with -- e.g.
// cargo run --release --example beep --features jack -- --jack
let host = cpal::host_from_id(cpal::available_hosts()
.into_iter()
.find(|id| *id == cpal::HostId::Jack)
.expect(
"make sure --features jack is specified. only works on OSes where jack is available",
)).expect("jack host unavailable");
#[cfg(all(
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd"
),
feature = "pipewire"
))]
let host = 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");

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

let device = host
Expand Down
10 changes: 10 additions & 0 deletions src/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ pub(crate) mod emscripten;
feature = "jack"
))]
pub(crate) mod jack;
#[cfg(all(
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd"
),
feature = "pipewire"
))]
pub(crate)mod pipewire;
pub(crate) mod null;
#[cfg(windows)]
pub(crate) mod wasapi;
Expand Down
Loading
Loading