Home > Back-end >  Linux: Allow www-data to mute/unmute sound
Linux: Allow www-data to mute/unmute sound

Time:11-15

To mute and unmute sound I successfully use the following command in the terminal (as root):

amixer -D pulse set Master 1  toggle

But it's not working if I trigger this command via PHP (shell_exec) as user www-data. I get the error:

ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refused
amixer: Mixer attach pulse error: Connection refused

This probably has to do with user rights, but I can't figure out how to grant these rights to user www-data, can you help?

Additional info from pactl info:

pi@raspberrypi:~ $ pactl info
Server-Zeichenkette: /run/user/1000/pulse/native
Bibliotheks-Protokollversion: 34
Server-Protokollversion: 34
ist lokal: ja
Client-Index: 32
Tile-Größe: 65496
Name des Benutzers: pi
Rechnername: raspberrypi
Name des Servers: pulseaudio
Version des Servers: 14.2
Standard-Abtastwert-Angabe: s16le 2ch 44100Hz
Standard-Kanal-Zuordnung: front-left,front-right
Standard-Ziel: alsa_output.usb-Generic_USB2.0_Device_20130100ph0-00.analog-stere                                     o
Standard-Quelle: alsa_output.usb-Generic_USB2.0_Device_20130100ph0-00.analog-ste                                     reo.monitor
Cookie: 249b:a636

pi@raspberrypi:~ $ stat /run/user/1000/pulse/native
 Datei: /run/user/1000/pulse/native
 Größe: 0               Blöcke: 0          EA Block: 4096   Socket
 Gerät: 20h/32d Inode: 33          Verknüpfungen: 1
Zugriff: (0666/srw-rw-rw-)  Uid: ( 1000/      pi)   Gid: ( 1000/      pi)
Zugriff: 2021-11-14 11:34:40.192044159  0100
Modifiziert: 2021-11-12 15:33:34.489999995  0100
Geändert: 2021-11-12 15:33:34.489999995  0100
Geburt: -

CodePudding user response:

 Datei: /run/user/1000/pulse/native
Zugriff: (0666/srw-rw-rw-)  Uid: ( 1000/      pi)   Gid: ( 1000/      pi)

Either way, you have to point your application to the correct path and give permission to access it.

I believe adding www-data user to pi group, and then executing env PULSE_RUNTIME_PATH=/run/user/1000/pulse/native pamixer.... should do the trick. Actually, the socket is rw-rw-rw- anyway, so maybe you don't have to add the user to group anyway.

The other alternative is to open RDP to pulseaudio and connect from php to it.

CodePudding user response:

I finally solved it with the help of Eli Billauer's tech blog: http://billauer.co.il/blog/2014/01/pa-multiple-users/

For more details visit his blog post.

In short, the steps are:

To do as the desktop’s user (in my case 'pi') copy /etc/pulse/default.pa into a file with the same name in the .pulse directory, that is

cp /etc/pulse/default.pa ~/.pulse/

And then edit the file, adding the following line at the end:

load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1

At this point, restart the pulseaudio deamon,

$ pulseaudio -k
$ pulseaudio -D

To do as "www-data" user Now switch to the second user (www-data), and create a file named client.conf under that user’s .pulse subdirectory (I had to create that. in www-data's 'home' direction which is /var/www/)

$ echo "default-server = 127.0.0.1" > ~/.pulse/client.conf

Note that default.pa and client.conf are in completely different directories, each belonging to a different user!

  • Related