Can anyone play audio in container without install sound server in host?

I thought just pass char device to container will be fine, but not.

Step I have done:

ls -l /dev/snd/

crw-rw---- 1 root audio 116, 6 controlC0
crw-rw---- 1 root audio 116, 5 midiC0D0
crw-rw---- 1 root audio 116, 3 pcmC0D0c
crw-rw---- 1 root audio 116, 2 pcmC0D0p
crw-rw---- 1 root audio 116, 4 pcmC0D1p
crw-rw---- 1 root audio 116, 1 seq
crw-rw---- 1 root audio 116, 33 timer

Add all of them in profile, audio group is 29 in container:

config: {}
description: sound device
devices:
  controlC0:
    gid: "29"
    mode: "0660"
    path: /dev/snd/controlC0
    required: "false"
    source: /dev/snd/controlC0
    type: unix-char
  midiC0D0:
    gid: "29"
    mode: "0660"
    path: /dev/snd/midiC0D0
    required: "false"
    source: /dev/snd/midiC0D0
    type: unix-char
  pcmC0D0c:
    gid: "29"
    mode: "0660"
    path: /dev/snd/pcmC0D0c
    required: "false"
    source: /dev/snd/pcmC0D0c
    type: unix-char
  pcmC0D0p:
    gid: "29"
    mode: "0660"
    path: /dev/snd/pcmC0D0p
    required: "false"
    source: /dev/snd/pcmC0D0p
    type: unix-char
  pcmC0D1p:
    gid: "29"
    mode: "0660"
    path: /dev/snd/pcmC0D1p
    required: "false"
    source: /dev/snd/pcmC0D1p
    type: unix-char
  seq:
    gid: "29"
    mode: "0660"
    path: /dev/snd/seq
    required: "false"
    source: /dev/snd/seq
    type: unix-char
  timer:
    gid: "29"
    mode: "0660"
    path: /dev/snd/timer
    required: "false"
    source: /dev/snd/timer
    type: unix-char

Add profile to container.

Install packages:

apt install pipewire-audio alsa-utils -y

Create a user with audio group:

useradd -m -G audio -s /bin/bash username

Change to new user: su -l username

List device:

aplay -l

**** List of PLAYBACK Hardware Devices ****
card 0: AudioPCI [Ensoniq AudioPCI], device 0: ES1371/1 [ES1371 DAC2/ADC]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 0: AudioPCI [Ensoniq AudioPCI], device 1: ES1371/2 [ES1371 DAC1]
Subdevices: 1/1
Subdevice #0: subdevice #0

Testing:

aplay -D hw:0 /usr/share/sounds/alsa/Front_Center.wav

NO sound.

So there’s no sound server on the host (no PulseAudio, no Pipewire, etc) on purpose.

I would try to play audio in the container using the simplest Alsa player. Especially no pipewire package.

I would try to run this ALSA script on both the host and the container and try to make changes so that both would give the same/similar output.

If you are OK to use a sound server on the host, you can access that sound server over the network.

I used alsamixer to select the right sound card, and there is sound.

Not close yet, I need to reproduce step so everyone can easily follow.

At the end you mention that you cannot aplay an audio file. Did you manage to play any audio?

I can use aplayto play audio now. Pipewire now works without trouble.

1 Like

OK, I need to take some of my words, Pipewire works with trouble. But I manage to reproduce with alsa and pulseaudio. Due to Pipewire’s doc is too abstract, I’m only able to produce discontinuous sound, I will talk about it later.

Profile is up there, you need to change it to fit your need.

pulseaudio:

As root.

apt install pulseaudio alsa-utils alsa-tools -y

Use it to find the real sound card:

aplay -l

As you can see, my sound card is card 0:

**** List of PLAYBACK Hardware Devices ****
card 0: AudioPCI [Ensoniq AudioPCI], device 0: ES1371/1 [ES1371 DAC2/ADC]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 0: AudioPCI [Ensoniq AudioPCI], device 1: ES1371/2 [ES1371 DAC1]
Subdevices: 1/1
Subdevice #0: subdevice #0

Default volume is 0, use it to tweak:

alsamixer

Notice, this is dummy output, which is useless:

dummy

This is the real sound card:

real

Now you can test sound with:

aplay /usr/share/sounds/alsa/Front_Center.wav

Change to normal user with audio group, and test with above cmd. No sound right, to fix it, change #load-module module-alsa-sinkto load-module module-alsa-sink device=hw:0 in /etc/pulse/default.pa according to https://unix.stackexchange.com/questions/14454/changing-default-audio-device-in-pulseaudio . Remember to change hw:0 to your card. There is sound now.

alsa:

As root:

apt install pipewire-alsa alsa-utils alsa-tools -y

apt purge pipewire -y

apt autoremove -y

Exit container and restart it. Now you can play sound.

pipewire:

As root:

apt install pipewire-audio pulseaudio-utils alsa-utils alsa-tools -y

Change to normal user with audio group, add blew to ~/.config/pipewire/pipewire-pulse.conf.d/custom.conf

pulse.cmd = [{ cmd = “load-module” args = “module-alsa-sink device=hw:0” flags = []}]

Run following cmds:

systemctl --user enable --now pipewire pipewire-pulse wireplumber

systemctl --user daemon-reload

systemctl --user restart pipewire pipewire-pulse wireplumber

Now, use the test cmd, you can hear discontinuous sound. If you know how to config pipewire, please read https://docs.pipewire.org/page_pulse_module_alsa_sink.html , and post how to use module-alsa-sink, thanks in advance.