hal7df / bardcast

Command-line application to stream system audio to a Discord voice chat
GNU General Public License v3.0
2 stars 0 forks source link

Pulse driver does not work with the pipewire shim #9

Open ckcr4lyf opened 1 year ago

ckcr4lyf commented 1 year ago

I am on Arch Linux, trying to use this with pulse. I am getting errors if I try and use it (though I am technically on pipewire).

System:

$ uname -a
Linux arch 6.3.9-arch1-1 #1 SMP PREEMPT_DYNAMIC Wed, 21 Jun 2023 20:46:20 +0000 x86_64 GNU/Linux

$ pacman -Qi pipewire
Name            : pipewire
Version         : 1:0.3.72-1

$ pacman -Qi pulseaudio
Name            : pipewire-pulse
Version         : 1:0.3.72-1

$ pacman -Qi libpulse
Name            : libpulse
Version         : 16.1-6

Tried via:

$ ./target/debug/bardcast -d pulse -m peek
==================================
bardcast v0.1.1
(c) Paul Bonnen <hal7df@gmail.com>
==================================
2023-07-02T16:37:28.557+08:00 INFO  [bardcast::snd] Configured sound drivers: pulse
2023-07-02T16:37:28.570+08:00 INFO  [bardcast::snd::pulse] Connected to PulseAudio server
Runtime error: Sound driver failed to initialize: PulseAudio driver failed to initialize (PulseAudio error: Bad state)

$ ./target/debug/bardcast -d pulse -m monitor
==================================
bardcast v0.1.1
(c) Paul Bonnen <hal7df@gmail.com>
==================================
2023-07-02T16:37:33.209+08:00 INFO  [bardcast::snd] Configured sound drivers: pulse
2023-07-02T16:37:33.214+08:00 INFO  [bardcast::snd::pulse] Connected to PulseAudio server
2023-07-02T16:37:33.229+08:00 INFO  [bardcast::snd] Started application using driver 'pulse'
Segmentation fault (core dumped)

$ ./target/debug/bardcast -d pulse -m capture
==================================
bardcast v0.1.1
(c) Paul Bonnen <hal7df@gmail.com>
==================================
2023-07-02T16:38:06.474+08:00 INFO  [bardcast::snd] Configured sound drivers: pulse
2023-07-02T16:38:06.480+08:00 INFO  [bardcast::snd::pulse] Connected to PulseAudio server
Runtime error: Sound driver failed to initialize: PulseAudio driver failed to initialize (PulseAudio error: Bad state)

Let me know if there's anything I can do to help.

hal7df commented 1 year ago

Could you try running the application with debug logging enabled? (--log-level debug)

Also worth noting that I haven't properly tested this with pipewire yet, so it's possible that some further changes are needed.

ckcr4lyf commented 1 year ago

With debug logging:

peek

$ ./target/debug/bardcast -d pulse -m peek --log-level debug
==================================
bardcast v0.1.1
(c) Paul Bonnen <hal7df@gmail.com>
==================================
2023-07-02T18:36:44.974+08:00 DEBUG [bardcast] Initializing async runtime...
2023-07-02T18:36:44.980+08:00 DEBUG [bardcast] Async runtime initialized
2023-07-02T18:36:44.980+08:00 DEBUG [bardcast] Setting up application shutdown notification channel
2023-07-02T18:36:44.980+08:00 DEBUG [bardcast] Starting audio driver
2023-07-02T18:36:44.981+08:00 INFO  [bardcast::snd] Configured sound drivers: pulse
2023-07-02T18:36:44.989+08:00 DEBUG [bardcast::snd::pulse::context] Connecting to PulseAudio server
2023-07-02T18:36:44.989+08:00 DEBUG [bardcast::snd::pulse::context] Mainloop started
2023-07-02T18:36:44.997+08:00 DEBUG [bardcast::snd::pulse::context] PulseAudio context ready
2023-07-02T18:36:44.998+08:00 INFO  [bardcast::snd::pulse] Connected to PulseAudio server
2023-07-02T18:36:44.998+08:00 DEBUG [bardcast::snd::pulse::context] Task queue closed, terminating context handler
2023-07-02T18:36:44.998+08:00 DEBUG [bardcast::snd::pulse::context] Context handler shutting down
2023-07-02T18:36:45.001+08:00 DEBUG [bardcast::snd::pulse::context] Mainloop exiting
2023-07-02T18:36:45.001+08:00 DEBUG [bardcast::snd::pulse::context] Context handler shut down
Runtime error: Sound driver failed to initialize: PulseAudio driver failed to initialize (PulseAudio error: Bad state)

monitor

$ ./target/debug/bardcast -d pulse -m monitor --log-level debug
==================================
bardcast v0.1.1
(c) Paul Bonnen <hal7df@gmail.com>
==================================
2023-07-02T18:36:59.890+08:00 DEBUG [bardcast] Initializing async runtime...
2023-07-02T18:36:59.893+08:00 DEBUG [bardcast] Async runtime initialized
2023-07-02T18:36:59.893+08:00 DEBUG [bardcast] Setting up application shutdown notification channel
2023-07-02T18:36:59.893+08:00 DEBUG [bardcast] Starting audio driver
2023-07-02T18:36:59.893+08:00 INFO  [bardcast::snd] Configured sound drivers: pulse
2023-07-02T18:36:59.900+08:00 DEBUG [bardcast::snd::pulse::context] Connecting to PulseAudio server
2023-07-02T18:36:59.900+08:00 DEBUG [bardcast::snd::pulse::context] Mainloop started
2023-07-02T18:36:59.905+08:00 DEBUG [bardcast::snd::pulse::context] PulseAudio context ready
2023-07-02T18:36:59.905+08:00 INFO  [bardcast::snd::pulse] Connected to PulseAudio server
2023-07-02T18:36:59.926+08:00 INFO  [bardcast::snd] Started application using driver 'pulse'
Segmentation fault (core dumped)

P.S. I may be using it incorrectly, just happened to stumble across this project when looking for references to songbird/serenity so decided to try it out.

hal7df commented 1 year ago

Ah, I do see one issue: peek and capture modes require you to specify -E, since they target specific applications, rather than just capturing all system audio (as monitor does).

For example, if you wanted to capture audio from Firefox:

./target/debug/bardcast -E firefox -m capture ...

I thought there was a clearer error message for that, but maybe not.

Not sure why monitor mode is segfaulting, but I'm guessing that might have to do with differences between pipewire and pulseaudio.

hal7df commented 1 year ago

Looking a bit more closely at the error message you posted, it looks like it manages to successfully set up the sound driver, and segfaults shortly thereafter. I'm guessing it is probably still something in the libpulse C library (all of the segfaults I've encountered with this application so far have been from libpulse) failing, possibly because Pipewire implemented the API slightly differently. It's still a little weird, though, because at the point where it segfaults, the application shouldn't be doing much with libpulse...

Once you run it with -E, do you see the same behavior with capture or peek modes?

ckcr4lyf commented 1 year ago

Running with -E to specify the app, seems it can run into different kind of errors.

SEGFAULT:

$ ltrace ./target/debug/bardcast -E firefox -m capture --log-level debug
==================================
bardcast v0.1.1
(c) Paul Bonnen <hal7df@gmail.com>
==================================
2023-07-03T10:35:05.167+08:00 DEBUG [bardcast] Initializing async runtime...
2023-07-03T10:35:05.168+08:00 DEBUG [bardcast] Async runtime initialized
2023-07-03T10:35:05.168+08:00 DEBUG [bardcast] Setting up application shutdown notification channel
2023-07-03T10:35:05.168+08:00 DEBUG [bardcast] Starting audio driver
2023-07-03T10:35:05.168+08:00 INFO  [bardcast::snd] Configured sound drivers: pulse
2023-07-03T10:35:05.171+08:00 DEBUG [bardcast::snd::pulse::context] Connecting to PulseAudio server
2023-07-03T10:35:05.171+08:00 DEBUG [bardcast::snd::pulse::context] Mainloop started
2023-07-03T10:35:05.189+08:00 DEBUG [bardcast::snd::pulse::context] PulseAudio context ready
2023-07-03T10:35:05.189+08:00 INFO  [bardcast::snd::pulse] Connected to PulseAudio server
2023-07-03T10:35:05.190+08:00 INFO  [bardcast::snd::pulse] Matching applications against pattern 'firefox' on properties: application.process.binary,application.name
2023-07-03T10:35:05.199+08:00 DEBUG [bardcast::snd::pulse::event::factory] Set server entity subscription.
2023-07-03T10:35:05.211+08:00 DEBUG [bardcast::snd::pulse] Establishing event listener for application stream intercept
2023-07-03T10:35:05.212+08:00 INFO  [bardcast::snd::pulse] Intercepting matched applications using mode: Capture
2023-07-03T10:35:05.226+08:00 DEBUG [bardcast::snd::pulse::intercept] Creating audio capture sink with name 'bardcast-12344_rec-0'
2023-07-03T10:35:05.243+08:00 DEBUG [bardcast::snd::pulse::intercept] Audio capture module loaded (index 536870945)
2023-07-03T10:35:05.264+08:00 DEBUG [bardcast::snd::pulse::intercept] Created rec sink at index 1051
2023-07-03T10:35:05.264+08:00 DEBUG [bardcast::snd::pulse] Starting application stream intercept task
2023-07-03T10:35:05.265+08:00 INFO  [bardcast::snd::pulse] Intercepting matching application 'Firefox' (index 355)
2023-07-03T10:35:05.351+08:00 INFO  [bardcast::snd] Started application using driver 'pulse'
--- SIGSEGV (Segmentation fault) ---
--- SIGSEGV (Segmentation fault) ---
+++ killed by SIGSEGV +++

SIGABRT (malloc error)

==================================
bardcast v0.1.1
(c) Paul Bonnen <hal7df@gmail.com>
==================================
2023-07-03T10:35:06.296+08:00 DEBUG [bardcast] Initializing async runtime...
2023-07-03T10:35:06.297+08:00 DEBUG [bardcast] Async runtime initialized
2023-07-03T10:35:06.297+08:00 DEBUG [bardcast] Setting up application shutdown notification channel
2023-07-03T10:35:06.297+08:00 DEBUG [bardcast] Starting audio driver
2023-07-03T10:35:06.297+08:00 INFO  [bardcast::snd] Configured sound drivers: pulse
2023-07-03T10:35:06.299+08:00 DEBUG [bardcast::snd::pulse::context] Connecting to PulseAudio server
2023-07-03T10:35:06.300+08:00 DEBUG [bardcast::snd::pulse::context] Mainloop started
2023-07-03T10:35:06.315+08:00 DEBUG [bardcast::snd::pulse::context] PulseAudio context ready
2023-07-03T10:35:06.315+08:00 INFO  [bardcast::snd::pulse] Connected to PulseAudio server
2023-07-03T10:35:06.315+08:00 INFO  [bardcast::snd::pulse] Matching applications against pattern 'firefox' on properties: application.name,application.process.binary
2023-07-03T10:35:06.322+08:00 DEBUG [bardcast::snd::pulse::event::factory] Set server entity subscription.
2023-07-03T10:35:06.333+08:00 DEBUG [bardcast::snd::pulse] Establishing event listener for application stream intercept
2023-07-03T10:35:06.333+08:00 INFO  [bardcast::snd::pulse] Intercepting matched applications using mode: Capture
2023-07-03T10:35:06.346+08:00 DEBUG [bardcast::snd::pulse::intercept] Creating audio capture sink with name 'bardcast-12381_rec-0'
2023-07-03T10:35:06.361+08:00 DEBUG [bardcast::snd::pulse::intercept] Audio capture module loaded (index 536870946)
2023-07-03T10:35:06.380+08:00 DEBUG [bardcast::snd::pulse::intercept] Created rec sink at index 1074
2023-07-03T10:35:06.380+08:00 DEBUG [bardcast::snd::pulse] Starting application stream intercept task
2023-07-03T10:35:06.380+08:00 INFO  [bardcast::snd::pulse] Intercepting matching application 'Firefox' (index 355)
2023-07-03T10:35:06.543+08:00 INFO  [bardcast::snd] Started application using driver 'pulse'
malloc(): unsorted double linked list corrupted
--- SIGABRT (Aborted) ---
+++ killed by SIGABRT +++

SIGABRT (Wfree error)

==================================
bardcast v0.1.1
(c) Paul Bonnen <hal7df@gmail.com>
==================================
2023-07-03T10:37:12.145+08:00 DEBUG [bardcast] Initializing async runtime...
2023-07-03T10:37:12.146+08:00 DEBUG [bardcast] Async runtime initialized
2023-07-03T10:37:12.146+08:00 DEBUG [bardcast] Setting up application shutdown notification channel
2023-07-03T10:37:12.146+08:00 DEBUG [bardcast] Starting audio driver
2023-07-03T10:37:12.146+08:00 INFO  [bardcast::snd] Configured sound drivers: pulse
2023-07-03T10:37:12.148+08:00 DEBUG [bardcast::snd::pulse::context] Connecting to PulseAudio server
2023-07-03T10:37:12.149+08:00 DEBUG [bardcast::snd::pulse::context] Mainloop started
2023-07-03T10:37:12.164+08:00 DEBUG [bardcast::snd::pulse::context] PulseAudio context ready
2023-07-03T10:37:12.164+08:00 INFO  [bardcast::snd::pulse] Connected to PulseAudio server
2023-07-03T10:37:12.164+08:00 INFO  [bardcast::snd::pulse] Matching applications against pattern 'firefox' on properties: application.process.binary,application.name
2023-07-03T10:37:12.175+08:00 DEBUG [bardcast::snd::pulse::event::factory] Set server entity subscription.
2023-07-03T10:37:12.188+08:00 DEBUG [bardcast::snd::pulse] Establishing event listener for application stream intercept
2023-07-03T10:37:12.188+08:00 INFO  [bardcast::snd::pulse] Intercepting matched applications using mode: Capture
2023-07-03T10:37:12.205+08:00 DEBUG [bardcast::snd::pulse::intercept] Creating audio capture sink with name 'bardcast-12509_rec-0'
2023-07-03T10:37:12.223+08:00 DEBUG [bardcast::snd::pulse::intercept] Audio capture module loaded (index 536870949)
2023-07-03T10:37:12.245+08:00 DEBUG [bardcast::snd::pulse::intercept] Created rec sink at index 1152
2023-07-03T10:37:12.245+08:00 DEBUG [bardcast::snd::pulse] Starting application stream intercept task
2023-07-03T10:37:12.245+08:00 INFO  [bardcast::snd::pulse] Intercepting matching application 'Firefox' (index 355)
2023-07-03T10:37:12.331+08:00 INFO  [bardcast::snd] Started application using driver 'pulse'
Wfree(): corrupted unsorted chunks
--- SIGABRT (Aborted) ---
+++ killed by SIGABRT +++

Could be related to either the binding or the pipewire shim, but if it's hard for you to reproduce don't worry, just something I noticed so thought I'd share.

hal7df commented 1 year ago

Interesting, thanks for the info. bardcast does have some fairly complex interactions with pulseaudio, so I'm not entirely surprised that pipewire would struggle with it.

I'm running on a Ubuntu 22.04 derivative, which is still using pulseaudio by default, so I don't have a test setup ready for this. But, adding a native pipewire implementation (#4) is fairly high on my priority list, likely after fixing up the pulseaudio error messages and adding metadata tracking. I'm inclined to leave this issue open for the time being in order to document the issue, and in case myself or anyone else who comes across this wants to take a stab at debugging this before I can get around to adding native support.

In the meantime, if you're just looking to see it run, maybe you can spin up bardcast and (original) pulseaudio in a VM or container? I think it should work without any normal audio sinks if you use capture mode.

ckcr4lyf commented 1 year ago

Thanks for the reply. I am working on a different program so was just looking for some tips to interface with songbird.

Trying to make a program that will take input from stdin and stream it to discord. Very very early stage but the repo is at https://github.com/ckcr4lyf/stdinman

hal7df commented 1 year ago

I would probably recommend looking at the impl block for theConnectedClient type here: https://github.com/hal7df/bardcast/blob/main/src/discord/mod.rs#L73