Skip to content

Load ALSA at runtime with dlopen instead of linking it - #25

Open
emilk wants to merge 2 commits into
mrDIMAS:mainfrom
emilk:alsa-dlopen
Open

emilk wants to merge 2 commits into
mrDIMAS:mainfrom
emilk:alsa-dlopen

Conversation

@emilk

@emilk emilk commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

What

The alsa backend now loads libasound.so.2 at runtime with dlopen (via libloading) instead of linking it at build time through alsa-sys.

Why

A binary linked against libasound cannot even start on a machine without ALSA (minimal containers, headless CI runners, servers, etc): the dynamic loader fails before main runs. With this change the program starts fine everywhere, and only run_output_device returns an error such as:

Failed to load libasound.so.2: libasound.so.2: cannot open shared object file: No such file or directory

A side benefit: building no longer needs libasound2-dev / pkg-config (the CI apt install is left alone since pulse still needs libpulse-dev).

Downside: more complex code

`libasound.so.2` is now loaded with `libloading` when the output device
is created, rather than being linked at build time via `alsa-sys`.

This means:
- A binary using tinyaudio can start on a machine without ALSA
  installed (minimal containers, headless servers). Previously the
  dynamic loader failed before `main` ran. Now `run_output_device`
  returns an error instead.
- Building no longer requires `libasound2-dev` / `pkg-config`.

The ALSA call sequence (hw/sw params, S16_LE, period/buffer sizes,
recover loop) is unchanged; only the way the symbols are resolved
differs.

MSRV is bumped to 1.71 as required by `libloading` 0.8. The previous
`rust-version = "1.56"` was already stale since `dep:` feature syntax
needs 1.60.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Comment thread Cargo.toml
repository = "https://github.com/mrDIMAS/tinyaudio"
readme = "README.md"
rust-version = "1.56"
rust-version = "1.71"

@emilk emilk Sep 21, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: we can get away with a lower MSRV if we use an older libloading. Up to you!

Comment thread src/alsa.rs Outdated
Avoids a panic if snd_strerror ever returns a non-UTF-8 string.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant