Startup applications can not play audio

Asked by Christian Stussak

I want to launch a media player through "Startup Application Preferences". Login is set to automatic.

The application launches but there is no audio output to the speakers. I checked with pavucontrol and the app outputs something to PulseAudio. The audio is not muted and the correct output device and configuration/profile is selected. When I launch other apps that use the sound system afterwards, there is also no audio output.

I can make it work again by selecting a different audio profile in pavucontrol and then switch back to the original one.

However, when there is no audio app in "startup applications" and I wait a bit after login, I can start the media player and it works as expected.

Also, when I launch the media player through a script on startup and I add a "sleep 5" before starting the media player, sound also works. With "sleep 2", it sometimes works, sometimes not. But I don't want to play with random delays because I can never be sure that the delay is large enough to make it work.

There seems to be a race condition involved.

Can anyone confirm this behavior? Any fix/workaround for this?

I am running
- Ubuntu 22.04.1
- Intel NUC 12 i5
- Speakers connected to the headphone port
- Any media player that uses PulseAudio (e.g. paplay)

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Christian Stussak
Solved:
Last query:
Last reply:
Revision history for this message
Christian Stussak (lauechpad) said :
#1

I added an external USB sound card to the system. The issue does not seem to occur there. It only appears when I use the built-in sound card.

How can I analyze this further? Any ideas?

Revision history for this message
Bernard Stafford (bernard010) said (last edit ):
#2
Revision history for this message
Christian Stussak (lauechpad) said (last edit ):
#3

I ran alsa-info.sh twice in a single script launched through startup applications.

In the first run, the script plays an audio file through paplay and runs alsa-info.sh after a short delay (0.25s).
I can not hear any audio output through the external speakers.
https://www.alsa-project.org/db/?f=d55401b352905c3ab1680fa6220ff46dee7188b1

Then, I let the script finish playing the audio file (takes around 35s) and do the procedure again.
The audio now plays through the external speakers, which is what I want.
https://www.alsa-project.org/db/?f=22eddb129b53ec17c6cfbe8337ef28c18f3b31c4

Here is a diff of the two files:
$ diff alsa-info.broken.paplay.txt alsa-info.working.paplay.txt
5c5
< !!Script ran on: Fri Nov 18 08:35:05 UTC 2022
---
> !!Script ran on: Fri Nov 18 08:35:41 UTC 2022
41,43d40
< /sys/bus/acpi/devices/LNXPOWER:04/status 1
< /sys/bus/acpi/devices/LNXPOWER:05/status 1
< /sys/bus/acpi/devices/LNXPOWER:06/status 1
581c578
< Amp-Out vals: [0x00 0x00]
---
> Amp-Out vals: [0x57 0x57]
614,615c611,612
< Amp-In vals: [0x80 0x80]
< Converter: stream=1, channel=0
---
> Amp-In vals: [0x13 0x13]
> Converter: stream=0, channel=0
776c773
< Amp-Out vals: [0x80 0x80]
---
> Amp-Out vals: [0x00 0x00]

So I suppose the issue is with the "Amp-Out vals" being 0 for the "Master Playback Volume" during the first run.

Somehow, the "Master Playback Volume" is set correctly for the second run. However, and this is where we come closer to the problem, when I start the second run while the first run of paplay is still active, the "Master Playback Volume" is still wrong. It seems that it only sets it properly when there is no stream active. My application (what I called "media player" above) is supposed to run all the time, so it never closes the stream to PulseAudio even if it is outputting silence.

So what's going on here?

Revision history for this message
Bernard Stafford (bernard010) said (last edit ):
#4

Terminal:
amixer scontrols | grep -oE "'.*'" | awk -F\' \
 '{print "amixer -c 0 set \""$2"\" unmute 100"}' | sh

Unmutes all audio devices & Master volume at 100

Revision history for this message
Christian Stussak (lauechpad) said :
#5

Thanks for your reply. Unfortunately, this does not work. I does not seem to do anything at that stage after automatic login. Probably, PulseAudio does something similar already, but fails in the same way as the above command.

Revision history for this message
Bernard Stafford (bernard010) said :
#6
Revision history for this message
actionparsnip (andrew-woodhead666) said :
#7

If you add a command to restart pulse, when you log in, does it help?

Revision history for this message
Christian Stussak (lauechpad) said :
#8

I added
systemctl --user restart pulse
Sometimes this works and sometimes it doesn't. But I think the main reason that it sometimes works is that the restart introduces a delay (~1-2 seconds) that sometimes seems sufficiently long. So this seems kind of equivalent to adding "sleep 2" (see OP).

Revision history for this message
Christian Stussak (lauechpad) said :
#9

BTW: I filed a bug report. Let's see what the devs/maintainers say.
https://bugs.launchpad.net/ubuntu/+source/alsa-driver/+bug/1997224

Revision history for this message
Christian Stussak (lauechpad) said :
#10

I played a bit more with amixer. This is the command that made it work in the end:
amixer sset Master mute unmute 1- 1+

Both parts are necessary (mute/unmute and volume adjustment). Just muting/unmuting or adjusting the volume does not bring back audio output.

I can put it in my start script without adding any possibly insufficient delay prior to it.

I still don't understand why this is needed. Both, alsamixer and pavucontrol, look the same before and after the command. Saving alsa-info.sh debug info before and after the command still gives the same diff as above (see #3).

Even though this is not a fix, it is a workaround that is good enough for my use case. I'll still keep the above bug report open. Maybe we can track it down to the root cause.