ywnkp / boot2docker

Apache License 2.0
1 stars 1 forks source link

Add docker container audio/sound: ALSA support on MacOS and Windows #1

Open noriyoshi-k opened 4 years ago

noriyoshi-k commented 4 years ago

The purpose of this issue is to support audio playback and recording in containers running on docker-machine.

In the past, a pull request was posted in the original boot2docker repository to add similar functionality as shown below, but it is no longer available because the master branch of boot2docker has been updated.

https://github.com/boot2docker/boot2docker/pull/920

This issue fixes the latest boot2docker to enable audio/sound functionality in containers running on docker-machine.

noriyoshi-k commented 4 years ago

How to use it

First, create a docker-machine using the following command. In the following command, the machine name is set to dev, but you can change it if you want. (Please see also A complete one-by-one guide to install Docker on your Mac OS using Homebrew)

$ brew install docker-machine
$ brew cask install virtualbox
$ docker-machine create -d virtualbox --virtualbox-boot2docker-url \
    https://github.com/ywnkp/boot2docker/releases/download/v19.03.6-alpha/boot2docker.iso \
    dev
$ docker-machine stop dev
$ vboxmanage modifyvm dev --audioin on && \
   vboxmanage modifyvm dev --audioout on && \
   vboxmanage modifyvm dev --audiocontroller hda
$ docker-machine start dev

Next, by using --device=/dev/snd option of docker command to provide an audio device file on docker-machine to a container, it is possible to enable audio functionalities on the container. The following example uses ubuntu:18.04, but you can also change the name of the container image if necessary.

$ docker $(docker-machine config dev) run -it --rm --device=/dev/snd ubuntu:18.04

How to check this modification

I created boot2docker.iso by executing the following command and branch, created a docker-machine using it, and checked the audio functionality on the container running on it. The host OS is MacOS 10.15 Catalina.

feature/#1-add-audio-support 05bb119ec1bc8385872a66144d217bfb4f1342a3

# Execute the following commands in the project root

$ docker build -t boot2docker:latest .  
$ docker run --rm boot2docker:latest > boot2docker.iso

The Intel HD Audio virtual device provided by VirtualBox is used to provide audio functionality to the container running on docker-machine.

# Execute the following commands in the project root
# Activate the audio feature on docker-machine by running the following command

$ brew install docker-machine
$ docker-machine --version
docker-machine version 0.16.2, build bd45ab1

$ brew cask install virtualbox
$ vboxmanage --version
6.1.6r137129

$ docker-machine create -d virtualbox --virtualbox-boot2docker-url `pwd`/boot2docker.iso dev
$ docker-machine stop dev
$ vboxmanage modifyvm dev --audioin on && \
   vboxmanage modifyvm dev --audioout on && \
   vboxmanage modifyvm dev --audiocontroller hda
$ docker-machine start dev

By using --device=/dev/snd option of docker command to provide an audio device file on docker-machine to a container, it is possible to enable audio functionalities on the container.

# Execute the following command to confirm that the sound can be played back on the container

$ docker --version
Docker version 19.03.8, build afacb8b

$ docker $(docker-machine config dev) run -it --rm --device=/dev/snd ubuntu:18.04
$ apt-get update && apt-get install -y alsa-utils
$ aplay /usr/local/share/sounds/alsa/Front_Right.wav                                                                                                                                                                                                                                                     
Playing WAVE '/usr/local/share/sounds/alsa/Front_Right.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono

About the changes

In 05bb119ec1bc8385872a66144d217bfb4f1342a3, I try not to make any complicated modifications. There are only two changes in this commit.

First, in boot2docker, you can change Linux kernel build options in files under files/kernel-config.d. To enable the use of Intel HD Audio virtual devices provided by VirtualBox on docker-machine, I have enabled the following two codec drivers as built-in modules of the Linux kernel provided by boot2docker.

CONFIG_SND_HDA_CODEC_SIGMATEL=y
CONFIG_SND_HDA_GENERIC=y

Next, in order to make the audio device immediately available on the container, the startup script that performs the audio initialization at docker-machine startup is enabled by including it in boot2docker.iso from the alsa and alsa-config packages of Tiny Core Linux.

RUN tcl-tce-load \
...
        alsa \
        alsa-config

If the above packages is not included in boot2docker.iso, the audio device will be visible from the container, but the alsactl init command will not be executed on docker-machine startup, and everything will be silenced, both playback and recording.

Impact on release

The changes in the file size of boot2docker.iso due to this modification are as follows:

Source ISO size
Before(v19.03.5) 57 MB (59,768,832 bytes)
After(05bb119ec1bc8385872a66144d217bfb4f1342a3) 60 MB (62,914,560 bytes)

The boot2docker.iso file size is very small, just like the original one.

Limitations

Currently, the audio function cannot be used in a cross-container execution environment using multiarch/qemu-user-static.

$ docker $(docker-machine config dev) run --rm --privileged multiarch/qemu-user-static --reset -p yes

After that, I'm trying to run audio playback on a Raspbian Buster container (armv7a), but it's not working.

$ docker $(docker-machine config dev) run -it --rm --device=/dev/snd resin/rpi-raspbian:buster-20190121
# apt-get update && apt-get install -y alsa-utils
# aplay /usr/share/sounds/alsa/Front_Left.wav
Unsupported ioctl: cmd=0xffffffff81785501
Unsupported ioctl: cmd=0xffffffff81785501
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4568:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5047:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM default
aplay: main:828: audio open error: No such file or directory
# aplay -l
Unsupported ioctl: cmd=0xffffffff81785501
aplay: device_list:272: no soundcards found...