You are not logged in.

#1 2021-09-19 23:03:54

unsus
Member
Registered: 2021-09-19
Posts: 9

[SOLVED] Audio output for multiple users

Hello,


I have been trying to setup ALSA/Pulseaudio in a way to allow multiple different applications running under different users to output audio.

So far I have managed to allow multiple applications of the same user to play audio at the same time , even if one is using ALSA directly and the other is using pulseaudio.

However, if there is audio output from an application runnnig as user A, the audio device still gets blocked for user B

I have implemented this https://wiki.archlinux.org/title/Advanc … _at_a_time
and this https://wiki.archlinux.org/title/PulseA … are_device

The affected users are not part of the 'audio' group as recommended to avoid locking

I am pretty much out of options to try and my lack of understanding the asound config  does not help either, so Im hoping someone could point out any obvious mistakes


My /etc/asound.conf is this:

defaults.pcm.card 1
defaults.ctl.card 1

pcm.!default {
    type plug
    slave.pcm "asymed"
}


pcm.asymed {
    type asym
    playback.pcm {
        @func getenv
        vars [ ALSAPCM ]
        default "dmixer"
    }
    capture.pcm "dsnoop"
}


pcm.dmixer {
    type dmix
    ipc_key 1024
    ipc_key_add_uid 0
    ipc_perm 0666
}


Hoping for any ideas or hints what I can still try to get parallel audio output for multiple users

Last edited by unsus (2021-09-25 16:39:25)

Offline

#2 2021-09-19 23:09:08

ltsdw
Member
Registered: 2020-04-20
Posts: 73

Re: [SOLVED] Audio output for multiple users

I don't know if understand it right, but I think this will help: https://dhole.github.io/post/pulseaudio_multiple_users/

Offline

#3 2021-09-20 00:38:23

unsus
Member
Registered: 2021-09-19
Posts: 9

Re: [SOLVED] Audio output for multiple users

The issue seems also present when only ALSA is used directly, without pulseaudio being involved.

Therefore i still suspect my asound.conf file to be missing something

Offline

#4 2021-09-20 09:16:58

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,657

Re: [SOLVED] Audio output for multiple users

How are you testing whether this works exactly and what outputs from a terminal do you get? In general since you are configuring the default pcm again, the first line is irrelevant and will get overridden by pcm.!default. So you configure a new dmixer instance, that itself doesn't know to which pcm it should play back to. so at the minimum you'll at least need to define the card you want to play back on.

pcm.dmixer {
    type dmix
    ipc_key 1024
    ipc_key_add_uid 0
    ipc_perm 0666
    slave {
        pcm "hw:1"
    }
}

I'd still agree that you should, in the general sense, ignore that asound.conf entirely and configure pulseaudio properly. As in you shouldn't hook everything onto  dmix  because you are processing everything twice, and if you configure pulse correctly with it's own means and use pulse clients rather than plain ALSA ones (... and remove the asound.conf and set up pulseaudio-alsa instead) you'll generally have better control over the audio stream.

Offline

#5 2021-09-20 09:20:18

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: [SOLVED] Audio output for multiple users


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#6 2021-09-20 15:44:27

unsus
Member
Registered: 2021-09-19
Posts: 9

Re: [SOLVED] Audio output for multiple users

My general objective here is to not have to use pulseaudio with one user while the second user may do so. The main reason is that some applications like firefox are sandboxed and therefore I want to avoid providing access to the pulseaudio socket.
For the same reason the pulseaudio setup for multiple users would not help since it further removes isolation.
Additionally I aim to reduce the need of additional applications and complexity on my setup and since I do not need the functionality provided by pulseaudio, I want to avoid it where I can.


Im testing the audio by using apulse to enforce the direct use of alsa and I have also identified some application (like certain steam games) that use pulseaudio directly.

With the change suggested by V1del I am now one step further with ALSA applications running as different users are working at the same time now, but pulseaudio and ALSA still does not.

When I run a steam game that uses pulseaudio directly, I get the following error both on the same user and on other users:

speaker-test

speaker-test 1.2.5.1

Playback device is default
Stream parameters are 48000Hz, S16_LE, 1 channels
Using 16 octaves of pink noise
ALSA lib pcm_dmix.c:1035:(snd_pcm_dmix_open) unable to open slave
Playback open error: -16,Device or resource busy

(same result with 'apulse speaker-test')

apulse firefox-esr
ALSA lib pcm_dmix.c:1035:(snd_pcm_dmix_open) unable to open slave
[apulse] [error] do_connect_pcm: can't open playback device "default". Error code -16 (Device or resource busy)
[apulse] [error] do_connect_pcm: failed to open ALSA device. Apulse does no resampling or format conversion, leaving that task to ALSA plugins. Ensure that selected device is capable of playing a particular sample format at a particular rate. They have to be supported by either hardware directly, or by "plug" and "dmix" ALSA plugins which will perform required conversions on CPU.
[Child 2911, MediaDecoderStateMachine #1] WARNING: 7ff15f8aeba0 OpenCubeb() failed to init cubeb: file /builds/worker/checkouts/gecko/dom/media/AudioStream.cpp:324
[Child 2911, MediaDecoderStateMachine #1] WARNING: Decoder=7ff16e05bc00 [OnMediaSinkAudioError]: file /builds/worker/checkouts/gecko/dom/media/MediaDecoderStateMachine.cpp:3980

When I run firefox without apulse I do not get any error message, but the sound does not play anyway.

So it seems while ALSA is behaving like I want it to now, pulseaudio still locks the audio device, preventing the use of both different pulseaudio applications and apps that use ALSA.
Note that pulseaudio also blocks additional applications from using sound without involving a second user (when everything is running as the same user)

The only thing I changed for pulseaudio is this
https://wiki.archlinux.org/title/PulseA … are_device

Offline

#7 2021-09-20 15:48:06

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,657

Re: [SOLVED] Audio output for multiple users

The alsa-sink pulse device you want to attach to is the one you defined, i.e dmixer , if you try to attach dmix you try to use the existing normal config which bites itself with what you try to do. You will also want to comment/remove the load-module udev-detect.so line, since you do not want pulse to access the devices like it would normally.

And in any case you still need to do the socket config for pulse as has been linked, as otherwise "other user" applications can't communicate with the "main" pulse process.

FWIW if your goal is actually sandboxing processes, instead of trying a different user setup and opening up actual holes by allowing shared access to all audio, consider using actual sandboxing solutions like flatpaks and firejail.

Last edited by V1del (2021-09-20 16:02:46)

Offline

#8 2021-09-21 16:01:36

unsus
Member
Registered: 2021-09-19
Posts: 9

Re: [SOLVED] Audio output for multiple users

V1del wrote:

And in any case you still need to do the socket config for pulse as has been linked, as otherwise "other user" applications can't communicate with the "main" pulse process.

Only one user needs to have access to pulseaudio, the other uses ALSA only

V1del wrote:

FWIW if your goal is actually sandboxing processes, instead of trying a different user setup and opening up actual holes by allowing shared access to all audio, consider using actual sandboxing solutions like flatpaks and firejail.

Allowing access to the sound devices is an acceptable risk that would also exist with other solutions. In the end I value the reduced complexity more then more control over audio access.


V1del wrote:

The alsa-sink pulse device you want to attach to is the one you defined, i.e dmixer , if you try to attach dmix you try to use the existing normal config which bites itself with what you try to do. You will also want to comment/remove the load-module udev-detect.so line, since you do not want pulse to access the devices like it would normally.

I believe I understand now how it should work but so far I still cant get it to work.

My current setup:

/etc/asound.conf

defaults.pcm.card 1
defaults.ctl.card 1

pcm.!default {
    type plug
    slave.pcm "asymed"
}


pcm.asymed {
    type asym
    playback.pcm {
        @func getenv
        vars [ ALSAPCM ]
        default "dmix"
    }
    capture.pcm "dsnoop"
}


pcm.dmixer {
    type dmix
    ipc_key 1024
    ipc_key_add_uid 0
    ipc_perm 0666
    slave {
        pcm "hw:1"
    }
}

/etc/pulse/default.pa.d/load-audio-drivers-statically.pa

unload-module module-udev-detect
unload-module module-detect
load-module module-alsa-sink device=dmixer
load-module module-alsa-source device=dsnoop


In /etc/pulse/defauld.pa and system.pa, the following lines are commented

#.ifexists module-udev-detect.so
#load-module module-udev-detect
#.else
### Use the static hardware detection module (for systems that lack udev support)
#load-module module-detect
#.endif

If I understand it correctly, both pulse and ALSA should use pcm.dmixer now, but when I run audio applications that use ALSA while playing audio with pulse I still get


ALSA lib pcm_dmix.c:1035:(snd_pcm_dmix_open) unable to open slave
Playback open error: -16,Device or resource busy

Additionally it seems the audio is delayed after i removed the load-module module-udev-detect

I suspect I could also add

default-sink = dmixer

in /etc/pulse/client.conf ?

Last edited by unsus (2021-09-21 16:31:39)

Offline

#9 2021-09-21 17:18:25

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,657

Re: [SOLVED] Audio output for multiple users

Your "asymed" pcm in the alsa conf references the default dmix , rather than dmixer, not sure why you changed that it was correct in the OP.

Explicitly setting the default pulse sink shouldn't be necessary as you should only have one now, maybe post/check

pacmd list-sinks
pacmd list-sink-inputs

during this situation.

The delay you notice now is likely a direct side effect of only loading pulse on the generic dmix card. Pulse does a lot in the background to ensure you get minimal latency as possible in consideration of the things you have actively active. It does so by actively regulating the card parameters, by offloading to dmix it can't do that anymore and you get the static config dmix gives you.

On a side note please use [ code ] and not [ quote ] when you post files or command outputs.

Last edited by V1del (2021-09-21 17:22:53)

Offline

#10 2021-09-21 17:57:45

unsus
Member
Registered: 2021-09-19
Posts: 9

Re: [SOLVED] Audio output for multiple users

Thanks, it is now working in parallel, somewhat at least.

However the delay when using pulseaudio makes this setup unuseable.
Is there perhaps a way to fix this? Using alsa directly does not cause any delay, so I dont get why using pulseaudio does.

Alternatively I will have to look for a way to force all audio through apulse, but I have not been successful with that so far.

Offline

#11 2021-09-22 00:55:33

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,657

Re: [SOLVED] Audio output for multiple users

Post the outputs of the commands I asked for in such a delayed situation.

Offline

#12 2021-09-22 15:05:22

unsus
Member
Registered: 2021-09-19
Posts: 9

Re: [SOLVED] Audio output for multiple users

V1del wrote:

Post the outputs of the commands I asked for in such a delayed situation.

pacmd list-sinks

1 sink(s) available.
  * index: 1
	name: <alsa_output.dmixer>
	driver: <module-alsa-sink.c>
	flags: HARDWARE DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY
	state: RUNNING
	suspend cause: (none)
	priority: 9030
	volume: front-left: 65536 / 100% / 0.00 dB,   front-right: 65536 / 100% / 0.00 dB
	        balance 0.00
	base volume: 65536 / 100% / 0.00 dB
	volume steps: 65537
	muted: no
	current latency: 21.27 ms
	max request: 3 KiB
	max rewind: 3 KiB
	monitor source: 1
	sample spec: s16le 2ch 48000Hz
	channel map: front-left,front-right
	             Stereo
	used by: 1
	linked by: 1
	configured latency: 21.33 ms; range is 0.50 .. 376.00 ms
	module: 22
	properties:
		alsa.resolution_bits = "16"
		device.api = "alsa"
		device.class = "sound"
		alsa.class = "generic"
		alsa.subclass = "generic-mix"
		alsa.name = "ALC1220 Analog"
		alsa.id = "ALC1220 Analog"
		alsa.subdevice = "0"
		alsa.subdevice_name = "subdevice #0"
		alsa.device = "0"
		alsa.card = "1"
		alsa.card_name = "HD-Audio Generic"
		alsa.long_card_name = "HD-Audio Generic at 0xfcd00000 irq 66"
		alsa.driver_name = "snd_hda_intel"
		device.bus_path = "pci-0000:31:00.4"
		sysfs.path = "/devices/pci0000:00/0000:00:08.1/0000:31:00.4/sound/card1"
		device.bus = "pci"
		device.vendor.id = "1022"
		device.vendor.name = "Advanced Micro Devices, Inc. [AMD]"
		device.product.id = "1487"
		device.product.name = "Starship/Matisse HD Audio Controller"
		device.string = "dmixer"
		device.buffering.buffer_size = "72192"
		device.buffering.fragment_size = "24064"
		device.access_mode = "mmap+timer"
		device.description = "Starship/Matisse HD Audio Controller"
		device.icon_name = "audio-card-pci"

pacmd list-sink-inputs

1 sink input(s) available.
    index: 1
	driver: <protocol-native.c>
	flags: DONT_MOVE START_CORKED FIX_RATE
	state: RUNNING
	sink: 1 <alsa_output.dmixer>
	volume: front-left: 65536 / 100% / 0.00 dB,   front-right: 65536 / 100% / 0.00 dB
	        balance 0.00
	muted: no
	current latency: 50.83 ms
	requested latency: 21.33 ms
	sample spec: float32le 2ch 48000Hz
	channel map: front-left,front-right
	             Stereo
	resample method: copy
	module: 9
	client: 3 <nwmain-linux>
	properties:
		media.role = hex:
		phonon.streamid = hex:
		media.name = "Playback Stream"
		application.name = "nwmain-linux"
		native-protocol.peer = "UNIX socket client"
		native-protocol.version = "35"
		application.process.id = "4212"
		application.process.user = "unsus"
		application.process.host = "gamebox"
		application.process.binary = "nwmain-linux"
		application.language = "C"
		window.x11.display = ":1"
		application.process.machine_id = "SOMEID"
		application.process.session_id = "3"
		module-stream-restore.id = "sink-input-by-application-name:nwmain-linux"

Last edited by unsus (2021-09-22 15:14:42)

Offline

#13 2021-09-24 16:24:06

unsus
Member
Registered: 2021-09-19
Posts: 9

Re: [SOLVED] Audio output for multiple users

I solved the issue, even though Im not yet sure it will not cause other problems

Basic solution is

pacman -R pulseaudio

Once pulseaudio is removed, Alsa handles everything just like its supposed to do and with a rather simple config

/etc/asound.con

defaults.pcm.card 1
defaults.ctl.card 1

pcm.!default {
    type plug
    slave.pcm "asymed"
}


pcm.asymed {
    type asym
    playback.pcm {
        @func getenv
        vars [ ALSAPCM ]
        default "dmix"
    }
    capture.pcm "dsnoop"
}

Applications (like firefox) that require pulseaudio because the direct also support was remove, work just fine with apulse.

So far everything works as expected, even with multiple users running audio
Hope this helps others with the same issue

Last edited by unsus (2021-09-24 16:24:52)

Offline

#14 2021-09-24 17:56:48

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,657

Re: [SOLVED] Audio output for multiple users

Which is generally  expected. As you don't mix things twice now. The reverse, allowing multiple users to access the pulse server that handles the devices would work as well.

If you consider this solved, please mark it as such by editing the title in your first post.

Also fwiw some side notes, firefox's ALSA support wasn't removed it just has a totally broken ALSA implementation anyway which can't really deal with non default setups. You will also want to add

    hint {
        show on
        description "Default ALSA Output"
    }

to your default pcm so that utilities that rely on the aplay -L listing (which should currently not list a default pcm) don't get tripped up.

Last edited by V1del (2021-09-24 17:57:37)

Offline

#15 2021-09-25 16:38:53

unsus
Member
Registered: 2021-09-19
Posts: 9

Re: [SOLVED] Audio output for multiple users

Thanks for the help

Offline

#16 2021-09-25 18:49:58

unsus
Member
Registered: 2021-09-19
Posts: 9

Re: [SOLVED] Audio output for multiple users

V1del wrote:

You will also want to add

    hint {
        show on
        description "Default ALSA Output"
    }

to your default pcm so that utilities that rely on the aplay -L listing (which should currently not list a default pcm) don't get tripped up.

Like this?

defaults.pcm.card 1
defaults.ctl.card 1

pcm.!default {
    type plug
    slave.pcm "asymed"
}


pcm.asymed {
    type asym
    playback.pcm {
        @func getenv
        vars [ ALSAPCM ]
        default "dmix"
    }
    capture.pcm "dsnoop"

    hint {
        show on
        description "Default ALSA Output"
    }
}

Offline

#17 2021-09-26 12:10:47

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,657

Re: [SOLVED] Audio output for multiple users

I'd rather have added it to the pcm.!default so that the "default" device gets exposed.

Offline

Board footer

Powered by FluxBB