WSL icon indicating copy to clipboard operation
WSL copied to clipboard

PulseAudio doesn't work.

Open KoolenDasheppi opened this issue 10 years ago • 73 comments

I am trying to use minimodem on the bash shell, and it gives me an error:

E: Cannot create PulseAudio stream: Connection refused.

I am not sure how to fix it... Help please?

KoolenDasheppi avatar Jun 08 '16 17:06 KoolenDasheppi

This looks related to #237 in the sense that we are not exposing hardware / drivers through WSL. For this, your best bet is to submit a feature request over on our User Voice Page to help us prioritize future releases.

russalex avatar Jun 08 '16 22:06 russalex

It is possible to run PulseAudio over TCP, and there is a native Windows port of the PulseAudio server. However, it seems to want to use a SOCK_DGRAM, which doesn't seem to be supported yet.

aseering avatar Jun 09 '16 00:06 aseering

Specifically, the failure is as follows:

socketpair(PF_LOCAL, SOCK_DGRAM, 0, 0x21daae0) = -1 ESOCKTNOSUPPORT (Socket type not supported)
write(2, "AO: [pulse] Init failed: Connect"..., 44AO: [pulse] Init failed: Connection refused) = 44

On real-Ubuntu, the equivalent socketpair() call succeeds.

My understanding is that Pulse uses local sockets for some sort of internal communication. I think, though I'm not sure, that it starts a helper process to handle communication with the remote server, and it handles communication with that process using this socket pair.

aseering avatar Jul 22 '16 02:07 aseering

@aseering So I commented on the other thread with some info, and I also found some other stuff: By default, Pulse loads a module called module-native-protocol-unix, and another module called module-native-protocol-tcp can also be enabled. I wonder if you can start pulse with a config that does not enable module-native-protocol-unix at all, then disable all of the non-native modules that listen over datagram-mode unix sockets. It looks like this might be possible, and if it is, Pulse won't try opening that unix socket and should startup fine, I think. As I mentioned in the other thread though, I won't be able to test it this weekend bc hardware problems, but maybe this could help.

Here is a list of the modules that have both TCP and unix variants: https://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/User/Modules/#index22h3

Maybe if you turn off all of the unix modules and replace them with TCP variants, you can start the pulse server.

fpqc avatar Jul 22 '16 23:07 fpqc

Aren't those modules loaded by the server? The server in this model runs under Windows. This strace is from the client.

aseering avatar Jul 23 '16 01:07 aseering

@aseering Nope, it runs those as well as the sink modules on the linux side. That's why I mentioned "trunking" from one to the other. Basically, there are two modes, a "bridging mode" that basically pipes all of the channels in multiplex to the remote server (Windows), to allow mixing of channels, etc on the remote machine, and a "flattened" version where the mixing and filtering and stuff are done on the "linux side" and then the output of the mix is piped to the Windows "client".

However, both modes of operation do have to use some kind of socket or port. For example, OSS sound (for which pulse has a sink) uses unix ports, so Pulse's OSS sink module will trick all OSS applications into piping their output to a unix socket opened by Pulse. Similarly, you can have a pulse module for Alsa, etc. In addition to those sink modules, pulse has a "pulse sink" for pulse-aware programs. This "pulse sink" module is "pulse-native-protocol-*".

Remember, pulse needs a some kind of socket that the linux programs have to talk to on the linux side. Then Pulse can either bundle them up and pipe a multiplexed stream to another Pulse server for processing or process them and then pipe a single processed stream to the other pulse server.

I believe (and I can't check bc my pc broke) that if you disable linux-side pulse from using any modules that use unix sockets, it should launch fine. Basically, this hypothesized fix should work similar to how the dbus fix works, I think.

fpqc avatar Jul 23 '16 02:07 fpqc

That all sounds very complicated... I just have every Pulse application individually opening a TCP socket to the Windows daemon.

aseering avatar Jul 23 '16 02:07 aseering

@aseering Oh, seems like that should work. You are saying that you aren't running pulse on the Linux side, but you also told me that it crashes, which seems like you are running it.

Or are you saying you used a workaround to get it to work?

(Also, fwiw, the benefit of running Pulse as a remote server and connecting it to another instance of pulse on your local machine is that not every Linux application with sound will output audio to pulse over tcp, mostly older programs or games. That's why pulse in Linux has all of those sink modules).

fpqc avatar Jul 23 '16 05:07 fpqc

@fpqc -- oh, sorry, terminology confusion. Let me define some more-specific terms:

  • libpulse -- the client library that pulse-based applications link
  • Linux pulseaudio -- the daemon that run on Linux
  • pulseaudio.exe -- the daemon that runs on Windows

By editing /etc/pulse/client.conf in WSL, I have configured Linux binary applications that link libpulse (tested with both paplay and mplayer) to connect over TCP to localhost.

I have also configured pulseaudio.exe to listen on TCP localhost.

I have not configured Linux pulseaudio, nor attempted to start it.

I am observing a failure (not a crash, simply an error as well as no audio) coming from libpulse inside WSL, when I run applications that link libpulse. If I modify the IP addresses used in this setup and use libpulse on a real Linux system (but still pulseaudio.exe on Windows), I do not see this failure, and I do hear audio, so the Windows side of the setup seems to be working properly.

When I strace the application that's calling into the relevant libpulse code, I observe that the first syscall that obviously differs between the working Linux execution and the failing WSL execution is socketpair(). socketpair() generates a pair of sockets that's typically used to communicate between a parent and a child process, or two threads within the same process; it has no on-disk nor TCP representation so AFAIK it can't be easily used to emulate any of these protocols.

I was looking around on the Internet and I thought I read somewhere (unfortunately I don't remember the source; it was unofficial) that libpulse actually tries to internally start is own mini Linux pulseaudio subsystem, to handle communication with the actual target pulseaudio. My suspicion, though I didn't read enough code to prove it, was that this socket was for communicating between the client application and that application's own custom pulseaudio instance.

aseering avatar Jul 23 '16 05:07 aseering

I wonder, if you do run the pulse daemon in unix socket mode, do the applications still start a mini pulse instance and create its socketpair in order to connect to the daemon. How about if you run the pulse daemon in tcp mode? (on a single linux system, no remote machine).

fpqc avatar Jul 23 '16 05:07 fpqc

Actually, @aseering, could it just be something as simple as this https://wiki.archlinux.org/index.php/PulseAudio/Examples#Disabling_automatic_spawning_of_PulseAudio_server

https://wiki.archlinux.org/index.php/PulseAudio/Examples#Disabling_pulseaudio_daemon_altogether

Namely that it's starting the daemon and choking?

fpqc avatar Jul 23 '16 06:07 fpqc

@fpqc -- good point re: disabling automatic spawning. Unfortunately, I tried it and it doesn't seem to help...

I tried running a local pulse daemon (which started after I disabled a bunch of plugins for things like bluetooth and hardware autodiscovery); that actually failed differently. In libpulse, as run from paplay:

getsockopt(7, SOL_SOCKET, SO_ERROR, 0x7fffd7c21f08, 0x7fffd7c21f0c) = -1 EINVAL (Invalid argument)
write(2, "getsockopt(): Invalid argument\n", 31getsockopt(): Invalid argument
) = 31

mplayer segfaults in this configuration. I haven't dug into the root cause, but prior to segfaulting, one of its pids tries a getsockopt(SO_ERROR) and fails in the same ways as above.

aseering avatar Jul 23 '16 14:07 aseering

I got paplay working with pulseaudio 9.0 on WSL and the win32 server here. Build instructions are here.

Interestingly, I didn't need to patch the source, just judiciously disable some stuff with configure. The SO_ERROR problem that @aseering found, which is in pulsecore/socket-client.c, doesn't appear to be in the control flow for paplay, for whatever reason.

On the WSL (ie, client) side, all you need is:

export PULSE_SERVER=tcp:localhost
/usr/local/bin/paplay -p <somefile>

You can also edit /etc/pulse/client.conf and set default-server = tcp:localhost. The server is never launched on WSL, so you can ignore default.pa. [Heads up that paplay doesn't support .mp3 files. You can get the list of supported formats with paplay --list-file-formats.]

That's the good news. The bad news is that mplayer and minimodem don't work because:

futex(0xb325c4, FUTEX_CMP_REQUEUE_PI_PRIVATE, 1, 2147483647, 0xbbe770, 2) =
-1 ENOSYS (Function not implemented)

Firefox and VLC also don't make noise yet, but I haven't tracked why. They do open the socket to the win32 server before choking, though.

therealkenc avatar Jul 25 '16 11:07 therealkenc

@therealkenc -- ooh, that's really interesting. Good to know.

What ABI-compatibility guarantees does pulseaudio provide? Ubuntu 14.04's packages are all built against pulseaudio 4.0.

aseering avatar Jul 25 '16 15:07 aseering

@aseering Hahahahahaha, Compatibility guarantees with pulse.

I'm an Arch user from time to time, pulse just breaks for no reason and the only answer is to reinstall the system or track down some strange config file you need to destroy. Any time you do an Arch update, it's a potential adventure!

fpqc avatar Jul 25 '16 20:07 fpqc

@aseering - the Pulse client API is stable to a first order, FWIW. That is, the same Firefox binary will make the same sounds on Trusty, Wily, Xenial, and Yakkety. It looks like Pulse keeps bumping major numbers because the server plugin API is relatively unstable.

therealkenc avatar Jul 25 '16 23:07 therealkenc

I got back to the futex problem. Amusingly, the patch is utterly trivial. Just add #undef HAVE_PTHREAD_PRIO_INHERIT at the top of pulsecore/mutex-posix.c at around line 23 like this:

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#undef HAVE_PTHREAD_PRIO_INHERIT

#include <pthread.h>

VLC works out of the box with the replacement shared library installed. I confess I don't know how to get minimodem to make screeching noises but it probably works too. Youtube videos on Firefox stream with audio completely in sync.

wsl-vlc-cap

therealkenc avatar Jul 26 '16 03:07 therealkenc

@therealkenc Wow, that's fantastic! How is performance? Also, some insight on how you tracked that down if you don't mind!?

fpqc avatar Jul 26 '16 04:07 fpqc

Performance (if that is the word) is great. The amount of cpu horsepower and memory bandwidth required to push a couple of audio channels over localhost is dwarfed by what it takes to decode an H.264 video stream coming down from GoogleCorp. That is, the difference between having audio and not audio isn't measurable. I pessimistically just assumed there was going to be sync problems (which is an A/V problem on a good day), because "buffering", but apparently not.

Tracking this one down was pretty straightforward because the failing syscall is futexey, and there's only one place the in PA library that calls those. Widely ported OSS packages always have build configuration knobs to tweak because the FreeBSD, Android, IoT and OS X guys don't necessarily have all the esoteric Linux syscalls or crazy /proc and /sys stuff either. When in doubt, you just turn the feature off.

therealkenc avatar Jul 26 '16 04:07 therealkenc

@therealkenc -- I played around some more with your instructions. From your list of packages, it sounds like you're using an upgraded xenial system? I got your instructions to work on trusty by playing around with the set of installed build-deps and the set of enabled/disabled flags for pulseaudio's ./configure script.

So far, works great for me! I haven't exercised it very much, though.

aseering avatar Jul 27 '16 15:07 aseering