Mittwoch, Mai 04, 2016

Using a remote pulseaudio to mitigate Virtualbox soundproblems

Applicability

I had this problem with audio in my Virtualbox guest, Xubuntu, on my Mac OS X host.
But this should be applicable -- in part or in whole -- to any host which is able to run pulseaudio and ssh and any unix guest which uses pulseaudio as defaultsoundsystem.


What did go wrong?

So i have this Mac from work, but I like working under Linux, xfce, sometimes I install debian, sometimes Xubuntu. This I did in a Xubuntu guest in Virtualbox on a Mac OS X host.
So I like using my Xubuntu, and I use it for most things that work: Watching movies, listening to music, working, everything. So I like the audio to work.

It does mostly work, but not perfect. Sometimes I connect the Macbook to a bluetooth audio receiver or plug in the hdmi to watch something on my tv screen. Sometimes I plug in headphones. All of these are visible in the audio menu up top, which shows a list of all this devices when I alt-click it. I can choose an audio output device from this list and the Mac promptly plays over that device. 
But not the Virtualbox. The Virtualbox just stays on the device it was playing audio over when it got startet. It doesn't switch to what I choose. (Sometimes it did, but not always, and that got very annoying)


The Solution

This one is kind of complicated.

the host

First, you have to install pulseaudio on your host machine, I did this with brew install pulseaudio, using homebrew.

Now, you have to configure your pulseaudio so you can use it over the net:
pulseaudio --dump-conf dumps you the pulseaudio config which includes the path of the default.pa file which we have to edit. On my machine it lies under
/usr/local/Cellar/pulseaudio/8.0/etc/pulse/default.pa
In this file, there is a commented out line which loads the module
module-native-protocol-tcp
I commented that line in and added some parameters:

load-module module-native-protocol-tcp  auth-ip-acl=127.0.0.1;192.168.0.0/24;10.0.2.15 auth-anonymous=1
(yeah, I googled that, don't really know, what that's doing, although I have an idea, of course. You can adjustt the IP ranges of course, in this example, we will only use the 10.0.2.15)

In the same directory as the default.pa there is another config file named daemon.conf. In this file you set allow-exit=no and comment out the exit-idle-time so pulseaudio doesn't exit if it's idle.

the guest

On the guest, you only have to run pax11publish -e -S 10.0.2.2 where 10.0.2.2 is the IP of the host, and you should be able to use the host's pulseaudio to play sound. done.

Automating it

Make sure you have an ssh-server on the host. Have an ssh-client on the guest. Create a keypair on the guest with ssh-keygen and configure everything so that you can connect from the guest to the host without a password.

Install screen on the host using brew install screen screen. Also, create a script:
cat > startpulseaudio.sh << EOF
#!/bin/bash
exec > ~/.pulselog 2>&1
echo starting pulse
pulseaudio
echo pulse aborted

EOF

On the guest, you create a script:
cat > starthostaudio.sh << EOF
#!/bin/bash

ssh -vT 10.0.2.2 "bash -l -c 'screen -S hostaudiopulse -d -m bash ~/startpulseaudio.sh'"
pax11publish -e -S 10.0.2.2
EOF


where -- as before -- 10.0.2.2 stands for the host IP.

Add starthostaudio.sh to your autostart on the guest, and you should be all set.

Well d'uh!