Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expose audio from Docker container to a Mac?

I know it's possible by using pulse audio on a Linux host system But paprefs is built for linux not mac.

like image 440
benwiz Avatar asked Oct 19 '16 16:10

benwiz


People also ask

Where are Docker volumes stored Mac?

As a Linux user, you learn that Volumes are stored in a part of the host filesystem managed by Docker, and that is /var/lib/docker/volumes . When you're running Docker on a Windows or Mac OS machine, you will read the same documentation and instructions but feel frustrated as that path don't exist on your system.


2 Answers

The Docker-for-Mac VM doesn't have any sound passthrough device, so there isn't anything that you could take advantage of from that angle. By contrast, a virtualbox or vmware fusion VM does have the ability to do passthrough audio.

I was able to get pulseaudio installed and working on OSX with the following command:

brew install pulseaudio

I was able to verify this worked by running the following, hearing sound come out of my speakers:

paplay cockatiel.wav

My next step is to find an image that has a copy of paplay. I found jess/pulseaudio, which appears to be intended to be a pulseaudio server, but I should be able to use it as a client as well.

I found the following guide on the Archlinux Wiki discussing setting up pulseaudio network sound: https://wiki.archlinux.org/index.php/PulseAudio/Examples#PulseAudio_over_network

I was able to adapt it to this situation by doing the following. I edited /usr/local/Cellar/pulseaudio/9.0/etc/pulse/default.pa on my mac, and uncommented the following two lines:

load-module module-esound-protocol-tcp
load-module module-native-protocol-tcp

I reran paplay cockatiel.wav on my mac to make sure my changes still worked. the pulseaudio daemon seems to start on demand, and passes its complaints back to paplay to be printed on my screen if I made a typo. I still have sound with those changes to default.pa, so I'm satisfied that my changes didn't break anything.

Next, I ran the pulseaudio client in a container like this:

docker run --rm -v $HOME:$HOME -w $HOME -it \
  -e PULSE_SERVER=192.168.10.23 \
  -e HOME=$HOME --entrypoint paplay \
  jess/pulseaudio $HOME/cockatiel.wav

What this does is run a container with my local home directory as a volume. This serves two purposes. The first is the fact that my cockatiel.wav is located inside $HOME. The second is because both the client and the server need to have a copy of the same ~/.config/pulse/cookie file (per that archlinux wiki guide).

The PULSE_SERVER environment variable is the en0 IP address of my OSX host, so paplay knows what to connect to.

The HOME environment variable is necessary so paplay can find the same ~/.config/pulse/cookie file.

I was able to play sound from a container running on my docker-for-mac via pulseaudio.

As long as you get the ~/.config/pulse/cookie file to appear in the correct location, you should be able to play sound. You don't have to use a host volume to accomplish this-- you could also do a 'docker cp', or even COPY it into an image.

like image 116
programmerq Avatar answered Oct 16 '22 21:10

programmerq


Install PulseAudio on the Mac:

brew install pulseaudio

Run the daemon:

pulseaudio --load=module-native-protocol-tcp --exit-idle-time=-1 --daemon

In your Docker container:

  1. Install PulseAudio, e.g., apt-get install pulseaudio.
  2. set the following environment variable: ENV PULSE_SERVER=docker.for.mac.localhost

When you run it, share your ~/.config/pulse directory with the container for authentication.

You can run a test to see if it's working like this:

docker run -it -e PULSE_SERVER=docker.for.mac.localhost -v ~/.config/pulse:/home/pulseaudio/.config/pulse --entrypoint speaker-test --rm jess/pulseaudio -c 2 -l 1 -t wav
like image 28
David Braun Avatar answered Oct 16 '22 21:10

David Braun