jac1d
Posts: 22
Joined: Sun Aug 26, 2012 4:56 pm

Raspbian Buster - ALSA - dmix interface for onboard audio hangs - workaround is use device sysdefault

Wed Jan 15, 2020 10:22 pm

The auto ALSA dmix device, auto configured for the onboard audio, appears to have a bug and does not function correctly.

I have identified a repeatable problem with the default (auto configured) dmix pathways for the Pi onboard audio sound chip under ALSA in Raspbian Buster (lite). Using dmix:CARD=ALSA,DEV=0 as the last stop in an alsa configuration chain, or calling it directly as an output device, hangs the process.

tl;dr A dmix interface for a second USB sound card does operate correctly. Outside of dmix, the auto configured interfaces for the Pi onboard audio chip appear to work correctly. I have tested sysdefault, plughw:0,0 and direct writing to hw:0,0 and they worked fine for me. Surprisingly, the device "sysdefault" is already capable of being opened by multiple processes at once and appears to support mixing of inputs, so I am able to use it in the way I hoped to use the dmix interface. It's a shame this hasn't been well publicized, there are a lot of forum, stack exchange and quora posts asking "what have I done wrong, dmix hangs on my pi?".

NOTE: I am not currently on github so I am hoping a reader can confirm this issue on their system and then post the details to github so it can be resolved in a future update and save others from hitting it. It took up a lot of time to ultimately realize it wasn't a configuration issue but a bug.

REQUIREMENT: This assumes you have working speakers connected to the onboard 3.5mm jack, they are powered on and you have confirmed that you can hear the audible test results. I did my testing on a Pi 3B. If I have time I will try various other Pi models I have access to, but if I use the same SD card I do anticipate a similar results.

To confirm your speakers are ok before you start:

Code: Select all

speaker-test
STEPS TO REPRODUCE THE HANG BUG USING RaspberryPi ALSA dmix DEVICE

0) Confirm your system has the default dmix:* devices with aplay

Code: Select all

aplay -L | grep dmix

pi@pi:~ $ aplay -L | grep dmix
dmix:CARD=ALSA,DEV=0
dmix:CARD=ALSA,DEV=1
dmix:CARD=ALSA,DEV=2
1) Create a ~/.asoundrc file and paste in this basic config, so we don't impact the rest of the audio system configuration directly. The type "plug" is used to ensure there is resampling of the input to adapt to the sample rate required by the dmix plugin, regardless of the sample rate of the file:

Code: Select all

nano ~/.asoundrc

pcm.dmix_debug_auto {
        type plug
        slave.pcm "dmix:CARD=ALSA,DEV=0"
}

pcm.dmix_debug_35mm {
        type plug
        slave.pcm "dmix:CARD=ALSA,DEV=1"
}

pcm.dmix_debug_HDMI {
        type plug
        slave.pcm "dmix:CARD=ALSA,DEV=2"
}

2) Tell ALSA to reload it's sound configuration

Code: Select all

sudo alsactl kill rescan
3) Confirm the new interfaces appear with aplay -L

Code: Select all

aplay -L | grep dmix
dmix_debug_auto
dmix_debug_35mm
dmix_debug_HDMI
dmix:CARD=ALSA,DEV=0
dmix:CARD=ALSA,DEV=1
dmix:CARD=ALSA,DEV=2
4) Download a ~6s wav file sample for testing and confirm you can hear it with the default device:

Code: Select all

wget https://www.kozco.com/tech/piano2.wav

aplay -Dsysdefault piano2.wav
5) Try and play the same file via each of the three debug configs (use CTRL-C to exit if more than 8s have passed as aplay should have completed the file and exited if it played correctly but the audio was sent elsewhere successfully) :

Code: Select all

time aplay -Ddmix_debug_auto piano2.wav
Playing WAVE 'piano2.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo
^CAborted by signal Interrupt...
aplay: pcm_write:2053: write error: Interrupted system call

real    0m10.562s
user    0m0.038s
sys     0m0.011s
Auto does not always auto detect correctly, so try it with each of the explicit device configurations as well.

3.5mm Jack / Headphone Connector:

Code: Select all

time aplay -Ddmix_debug_35mm piano2.wav
Playing WAVE 'piano2.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo
^CAborted by signal Interrupt...
aplay: pcm_write:2053: write error: Interrupted system call

real    0m10.938s
user    0m0.036s
sys     0m0.014s
And the HDMI port:

Code: Select all

time aplay -Ddmix_debug_HDMI piano2.wav
Playing WAVE 'piano2.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo
^CAborted by signal Interrupt...
aplay: pcm_write:2053: write error: Interrupted system call

real    0m9.864s
user    0m0.036s
sys     0m0.014s
As you can see the system hangs on a system call when trying to write to the default dmix devices for any of the three output options.

STRACE

Using strace we can see where it is hanging:

Code: Select all

 strace aplay -Ddmix_debug_auto piano2.wav
 
 -- Note: Only the last dozen or so lines shown here for brevity, but here is where it stalls before I did a CTRL-C to break out--

read(5, "\365\377\371\377\3\0\0\0\0\0\0\0\371\377\371\377\0\0\0\0\356\377\365\377\0\0\0\0\0\0\0\0"..., 4096) = 4096
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0x7305d0) = 0
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0x7305d0) = 0
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0x7305d0) = 0
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0x7305d0) = 0
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0x7305d0) = 0
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0x7305d0) = 0
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0x7305d0) = 0
poll([{fd=3, events=POLLIN|POLLERR|POLLNVAL}], 1, -1) = 1 ([{fd=3, revents=POLLIN}])
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0x7305d0) = 0
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0x7305d0) = 0
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0x7305d0) = 0
read(3, "\6\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 64) = 16
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0x7305d0) = 0
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0x7305d0) = 0
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0x7305d0) = 0
read(3, 0x7e8ec20c, 64)                 = -1 EAGAIN (Resource temporarily unavailable)
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0x7305d0) = 0
poll([{fd=3, events=POLLIN|POLLERR|POLLNVAL}], 1, -1^Cstrace: Process 1707 detached
 <detached ...>
Aborted by signal Interrupt...
aplay: pcm_write:2053: write error: Interrupted system call
Resolving it from here is far above my knowledge level, but hopefully this gives enough information for the subject matter experts to look in to. The strace output for each of the interfaces ends in the same fashion.

As you might expect, aplay also stalls consistently in the same place regardless of the interface selected:

Code: Select all

aplay -vvv -Ddmix_debug_auto piano2.wav
Playing WAVE 'piano2.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo
Plug PCM: Direct Stream Mixing PCM
Its setup is:
  stream       : PLAYBACK
  access       : RW_INTERLEAVED
  format       : S16_LE
  subformat    : STD
  channels     : 2
  rate         : 48000
  exact rate   : 48000 (48000/1)
  msbits       : 16
  buffer_size  : 16384
  period_size  : 1024
  period_time  : 21333
  tstamp_mode  : NONE
  tstamp_type  : MONOTONIC
  period_step  : 1
  avail_min    : 1024
  period_event : 0
  start_threshold  : 16384
  stop_threshold   : 16384
  silence_threshold: 0
  silence_size : 0
  boundary     : 1073741824
Hardware PCM card 0 'bcm2835 ALSA' device 0 subdevice 0
Its setup is:
  stream       : PLAYBACK
  access       : MMAP_INTERLEAVED
  format       : S16_LE
  subformat    : STD
  channels     : 2
  rate         : 48000
  exact rate   : 48000 (48000/1)
  msbits       : 16
  buffer_size  : 16384
  period_size  : 1024
  period_time  : 21333
  tstamp_mode  : ENABLE
  tstamp_type  : MONOTONIC
  period_step  : 1
  avail_min    : 1024
  period_event : 0
  start_threshold  : 1
  stop_threshold   : 1073741824
  silence_threshold: 0
  silence_size : 1073741824
  boundary     : 1073741824
  appl_ptr     : 0
  hw_ptr       : 0
Max peak (2048 samples): 0x00000016 #                    0%
Max peak (2048 samples): 0x0000001a #                    0%
Max peak (2048 samples): 0x00000016 #                    0%
Max peak (2048 samples): 0x0000001a #                    0%
Max peak (2048 samples): 0x00000012 #                    0%
Max peak (2048 samples): 0x0000001a #                    0%
Max peak (2048 samples): 0x00000016 #                    0%
Max peak (2048 samples): 0x00000016 #                    0%
Max peak (2048 samples): 0x00000012 #                    0%
Max peak (2048 samples): 0x00000012 #                    0%
Max peak (2048 samples): 0x00000016 #                    0%
Max peak (2048 samples): 0x00000012 #                    0%
Max peak (2048 samples): 0x00000016 #                    0%
Max peak (2048 samples): 0x0000001a #                    0%
Max peak (2048 samples): 0x00000016 #                    0%
Max peak (2048 samples): 0x00000016 #                    0%
6) Update the ~/.asoundrc file to use sysdefault for output and repeat the test:

Code: Select all

nano .asoundrc

pcm.dmix_debug_auto {
        type plug
        slave.pcm "sysdefault"
}
And tell ALSA to reload the configuration and run your test again:

Code: Select all

sudo alsactl kill rescan

time aplay -Ddmix_debug_auto piano2.wav
Playing WAVE 'piano2.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo

real    0m6.470s
user    0m0.092s
sys     0m0.023s
That plays beautifully out my 3.5mm port.

I hope this detail makes it to the appropriate maintainer and the issue can get resolved upstream. I also hope it helps the next person and saves them a few hours or days of head banging and asking "why does it work fine on my usb sound card but hangs for an identical configuration on the pi with dmix?".

-Jeff

noexit
Posts: 5
Joined: Fri Dec 20, 2019 2:23 pm

Re: Raspbian Buster - ALSA - dmix interface for onboard audio hangs - workaround is use device sysdefault

Fri Jan 17, 2020 4:43 pm

Hey Jeff! I just was referred to your post by another reader. I'd been struggling with DMix for a while and somehow got it working by simply putting the following in /etc/asound.conf:

Code: Select all

pcm.!default {
        type plug
        slave.pcm "dmix"
}
My intent was to use my Pi 4 as a Media Desktop and I need all sounds mixed together if the users desire. So far it seems to be working except that I'm having a little trouble getting the Volume Control on the Pi desktop to work. Here's the post on another forum explaining my progress with DMix:
https://forums.slimdevices.com/showthre ... post960659

I'm by no means a Linux anything. I can barely spell it. Leenucks... something. But I'll help all I can.

r_spb
Posts: 5
Joined: Sat Feb 21, 2015 4:16 pm

Re: Raspbian Buster - ALSA - dmix interface for onboard audio hangs - workaround is use device sysdefault

Thu Oct 08, 2020 4:08 pm

Any updates here? Looks like a serious issue for raspberry board that should be solved.

Return to “Raspberry Pi OS”