This document outlines the steps to modify the PulseAudio configuration on Docker host (Jetson) so to run an audio enabled application in continer. It can be referenced when one tries to enable similar PulseAudio based application inside a container.
- Share PulseAudio socket file (
/run/user/1000/pulse/native) - Set
PULSE_SERVERenv variable in the container - Allow root access to the socket file
To output sound from your Docker container while using the host's sound device, you can map the appropriate sound devices and audio services from the host to the container.
We use PulseAudio from within the container. To do this, we need share the PulseAudio socket between the host and the container.
Expand the docker run options for sound like this.
--device /dev/snd \
-e PULSE_SERVER=unix:${XDG_RUNTIME_DIR}/pulse/native \
-v ${XDG_RUNTIME_DIR}/pulse:${XDG_RUNTIME_DIR}/pulse \Option: If not working, further expand like following and try.
--device /dev/snd \
-e XDG_RUNTIME_DIR=/run/user/1000 \
-e PULSE_SERVER=unix:${XDG_RUNTIME_DIR}/pulse/native \
-v ${XDG_RUNTIME_DIR}/pulse:${XDG_RUNTIME_DIR}/pulse \
-v /etc/machine-id:/etc/machine-id \
--group-add audio \We operate as root in container, so we need to mofidy PusleAudio configuraiton file so that it allows the root user access to the socket file.
sudo vi /etc/pulse/default.paFind the section loading module-native-protomocl-unix and add auth-anonymous=1
### Load several protocols
.ifexists module-esound-protocol-unix.so
load-module module-esound-protocol-unix auth-anonymous=1
.endif
load-module module-native-protocol-unix auth-anonymous=1Restart Pulse Audio service.
pulseaudio --kill
pulseaudio --startInside the container, install speech-dispatcher and run its server inside the container (as opposed to running the speech-dispatcher server on the host).
Here is the Dockefile snippet.
RUN apt-get update && \
apt install -y speech-dispatcher alsa-utils && \
rm -rf /var/lib/apt/lists/* && \
apt-get clean
CMD speech-dispatcher --spawn && /bin/bashNotice it's starting the speech-dispatcher server as a background process within the container with speech-dispatcher --spawn.
Reboot the host system to make sure PulseAudio starts with the modified config file.
Then, on the host, check the default audio sink.
pactl info
If need to be set, set-default-sink.
pactl list short sinks
pactl set-default-sink [SINK_NAME_OR_INDEX]jetson-containers run $(autotag speech-dispatcher)Once inside the container;
spd-say "Hello world"You should here TTS audio coming out from your host audio device.
pulseaudio -k
pulseaudio -vvv
ALSA should work in your container just with /dev/snd mapped.
Test with speaker-test -t wav -c 2 (alsa-utils).
RUN CONTAINER
To start the container, you can use jetson-containers run and autotag, or manually put together a docker run command:
# automatically pull or build a compatible container image
jetson-containers run $(autotag speech-dispatcher)
# or if using 'docker run' (specify image and mounts/ect)
sudo docker run --runtime nvidia -it --rm --network=host speech-dispatcher:36.4.0
jetson-containers runforwards arguments todocker runwith some defaults added (like--runtime nvidia, mounts a/datacache, and detects devices)
autotagfinds a container image that's compatible with your version of JetPack/L4T - either locally, pulled from a registry, or by building it.
To mount your own directories into the container, use the -v or --volume flags:
jetson-containers run -v /path/on/host:/path/in/container $(autotag speech-dispatcher)To launch the container running a command, as opposed to an interactive shell:
jetson-containers run $(autotag speech-dispatcher) my_app --abc xyzYou can pass any options to it that you would to docker run, and it'll print out the full command that it constructs before executing it.
BUILD CONTAINER
If you use autotag as shown above, it'll ask to build the container for you if needed. To manually build it, first do the system setup, then run:
jetson-containers build speech-dispatcherThe dependencies from above will be built into the container, and it'll be tested during. Run it with --help for build options.