Pseudo Engineer

ソフトウェアの話とか書いてくよ

Pulseaudio in docker

CentOS7 上でdockerを使ってpulseaudioを起動してみた。

環境

Centos7 でdockerとdocker-composeをインストールしておく。alsa関連もインストールされてることを確認。

$ yum list installed | grep alsa
alsa-firmware.noarch                  1.0.28-2.el7                     @anaconda
alsa-lib.x86_64                       1.0.28-2.el7                     @anaconda
alsa-plugins-pulseaudio.x86_64        1.0.27-3.el7                     @base    
alsa-tools-firmware.x86_64            1.0.28-2.el7                     @base    
alsa-utils.x86_64                     1.0.28-4.el7                     @base

今回の環境のサウンドカードはこちら。

$ cat /proc/asound/cards
 0 [HDMI           ]: HDA-Intel - HDA Intel HDMI
                      HDA Intel HDMI at 0xf7234000 irq 54
 1 [PCH            ]: HDA-Intel - HDA Intel PCH
                      HDA Intel PCH at 0xf7230000 irq 53

PCHで音がなることを確認。

# cat <<EOF > ~/.asoundrc
pcm.!default {
  type hw
  card 1
}
ctl.!default {
  type hw
  card 1
}
EOF
# play sample.wav

docker

とくに解説はなし。

$ mkdir -p docker/pulseaudio
$ cd docker
$
$ cat <<EOF > pulseaudio/Dockerfile
FROM centos:7

RUN yum -y install \
    alsa-utils \
    pulseaudio \
    pulseaudio-libs \
    pulseaudio-utils

RUN useradd pulseaudio
RUN usermod -aG audio,pulse,pulse-access pulseaudio

USER pulseaudio

ENTRYPOINT [ "pulseaudio" ]
CMD [ "--log-level=4", "--log-target=stderr", "-v" ]
EOF
$
$ cat <<EOF > pulseaudio/asound.conf
pcm.!default {
  type hw
  card 1
}
ctl.!default {
  type hw
  card 1
}
EOF
$
$ cat <<EOF > pulseaudio/clinet.conf
autospawn = no
daemon-binary = /bin/true 
EOF
$
$ cat <<EOF > pulseaudio/daemon.conf
exit-idle-time = -1
flat-volumes = yes
EOF
$
$ cat <<EOF > pulseaudio/default.pa
# Replace the *entire* content of this file with these few lines and
# read the comments

.fail
    # Set tsched=0 here if you experience glitchy playback. This will
    # revert back to interrupt-based scheduling and should fix it.
    #
    # Replace the device= part if you want pulse to use a specific device
    # such as "dmix" and "dsnoop" so it doesn't lock an hw: device.
    
    # INPUT/RECORD
    load-module module-alsa-source device="default" tsched=1
    
    # OUTPUT/PLAYBACK
    load-module module-alsa-sink device="default" tsched=1 
    
    # Accept clients -- very important
    load-module module-native-protocol-unix
    load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1;192.168.0.0/24;10.0.0.0/8;172.0.0.0/8 auth-anonymous=1

.nofail
.ifexists module-x11-publish.so
    # Publish to X11 so the clients know how to connect to Pulse. Will
    # clear itself on unload.
    load-module module-x11-publish
.endif
EOF
$
$ cat <<EOF > docker-compose.yml
pulseaudio:
  build: pulseaudio
  container_name: pulseaudio
  command: pulseaudio -vvv --log-target=stderr
  volumes:
    - /etc/localtime:/etc/localtime:ro
    - /var/run/dbus:/var/run/dbus
    - /etc/machine-id:/etc/machine-id:ro
    - ./pulseaudio/asound.conf:/etc/asound.conf
    - ./pulseaudio/default.pa:/etc/pulse/default.pa
    - ./pulseaudio/client.conf:/etc/pulse/client.conf
    - ./pulseaudio/daemon.conf:/etc/pulse/daemon.conf
  ports:
    - "4713:4713"
  devices:
    - /dev/snd:/dev/snd
  environment:
    - HOME:/home/pulseaudio
  working_dir: /home/pulseaudio
EOF

docker起動

$ docker-compose up -d pulseaudio

鳴らす

pulseaudio経由で音がなることを確認。

# su -
# yum install pulseaudio-utils 
# exit
$ paplay -v -s localhost sample.wav