I wrote a little php script to control the volume of my local machine with alsa:
<?php
# for simplicity and testing it really just executes the command:
echo exec('amixer set Master 5%+') . " \n";
Now when I run this script on command line it works fine:
$ php volume.php
Front Right: Playback 39226 [60%] [on]
$ php volume.php
Front Right: Playback 42503 [65%] [on]
$ php volume.php
Front Right: Playback 45780 [70%] [on]
I have music playing and I hear it getting louder.
But when I try to run the script via apache from the browser calling http://localhost/volume.php
it doesn't work.
# http://localhost/volume.php
Front Right: Playback 55709 [10%] [on]
# F5
Front Right: Playback 55709 [15%] [on]
# F5
Front Right: Playback 55709 [20%] [on]
Now I hear no change in volume and the percentages seem to be unrelated to the current state. It says 10% - 15% - 20% when it really is still at 70%.
My apache is running as my user so exec('whoami')
gives me the username I am logged in with on my shell where everything works fine.
# httpd.conf
User mkt
Group mkt
I'm on Fedora 22.
Probably it's due to the apache2 process environment. Any ideas how to fix this?
UPDATE:
This is the output of aplay -L:
[mkt@localhost ~]$ aplay -L
null
Discard all samples (playback) or generate zero samples (capture)
pulse
PulseAudio Sound Server
default
Default ALSA Output (currently PulseAudio Sound Server)
sysdefault:CARD=Intel
HDA Intel, ALC888 Analog
Default Audio Device
front:CARD=Intel,DEV=0
HDA Intel, ALC888 Analog
Front speakers
surround21:CARD=Intel,DEV=0
HDA Intel, ALC888 Analog
2.1 Surround output to Front and Subwoofer speakers
surround40:CARD=Intel,DEV=0
HDA Intel, ALC888 Analog
4.0 Surround output to Front and Rear speakers
surround41:CARD=Intel,DEV=0
HDA Intel, ALC888 Analog
4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=Intel,DEV=0
HDA Intel, ALC888 Analog
5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=Intel,DEV=0
HDA Intel, ALC888 Analog
5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=Intel,DEV=0
HDA Intel, ALC888 Analog
7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
iec958:CARD=Intel,DEV=0
HDA Intel, ALC888 Digital
IEC958 (S/PDIF) Digital Audio Output
hdmi:CARD=NVidia,DEV=0
HDA NVidia, HDMI 0
HDMI Audio Output
hdmi:CARD=NVidia,DEV=1
HDA NVidia, HDMI 1
HDMI Audio Output
hdmi:CARD=NVidia,DEV=2
HDA NVidia, HDMI 2
HDMI Audio Output
hdmi:CARD=NVidia,DEV=3
HDA NVidia, HDMI 3
HDMI Audio Output
On command line only default and pulse are working:
amixer -D pulse set Master 5%+
amixer -D default set Master 5%+
With PHP even those two don't work. Anyway... my sound comes from my monitors speakers which is plugged in via hdmi. So I guess the last 4 devices are my candidates. But non of them work.
$ amixer -D hdmi:CARD=NVidia,DEV=0 set Master 5%+
$ amixer -D hdmi:CARD=NVidia,DEV=1 set Master 5%+
$ amixer -D hdmi:CARD=NVidia,DEV=2 set Master 5%+
$ amixer -D hdmi:CARD=NVidia,DEV=3 set Master 5%+
In all four cases it says: (with DEV=[0-3] of course)
ALSA lib control.c:954:(snd_ctl_open_noupdate) Invalid CTL hdmi:CARD=NVidia,DEV=3
amixer: Mixer attach hdmi:CARD=NVidia,DEV=3 error: No such file or directory
UPDATE
Output of aplay -l:
$ aplay -l
**** List of Hardware-Devices (PLAYBACK) ****
Card 0: Intel [HDA Intel], Device 0: ALC888 Analog [ALC888 Analog]
Sub-Devices: 1/1
Sub-Device #0: subdevice #0
Card 0: Intel [HDA Intel], Device 1: ALC888 Digital [ALC888 Digital]
Sub-Devices: 1/1
Sub-Device #0: subdevice #0
Card 1: NVidia [HDA NVidia], Device 3: HDMI 0 [HDMI 0]
Sub-Devices: 1/1
Sub-Device #0: subdevice #0
Card 1: NVidia [HDA NVidia], Device 7: HDMI 1 [HDMI 1]
Sub-Devices: 0/1
Sub-Device #0: subdevice #0
Card 1: NVidia [HDA NVidia], Device 8: HDMI 2 [HDMI 2]
Sub-Devices: 1/1
Sub-Device #0: subdevice #0
Card 1: NVidia [HDA NVidia], Device 9: HDMI 3 [HDMI 3]
Sub-Devices: 1/1
Sub-Device #0: subdevice #0
$ amixer -c0 set Master 5%+
$ amixer -c1 set Master 5%+
Both don't work!
SOLUTION:
Thanks for all the help! The answer though came from https://superuser.com/questions/1069981/set-volume-using-php-exec-and-amixer
putenv("PULSE_SERVER=/run/user/".getmyuid()."/pulse/native");
amixer
may try to access local settings that are not available to it. – Atavismecho exec('echo ${HOME}')
outputs the correct path on command line but is empty in browser... so I guess thats the problem? But how to solve this...? – Phosphoresceputenv()
possibly, php.net/manual/en/function.putenv.php – Rhebarheeputenv('HOME='. getenv('HOME'));
to your script. Most other variables should be working, you might want to compare the phpinfo()-output if something is missing. A suspect could beALSA_CONFIG_PATH
. – Ridley