AirPlay Information tracking

Hello:

This is my 3rd question to the forum. My first 2 went unanswered. They were either too stupid to deserve an answer or people don’t know the answer.

Here we go with my 3rd:

When Airplay is active, is there a place where volumio tracks information about it? Basically I’m developing a script to start a slideshow screensaver when the player is in pause or stop mode and close it when play state is on. I got it working using the state_file and using the player buttons. However when airplaying, the player is not aware that the state is in play mode and it still thinks it’s in pause/stop mode. Basically the state_file only works with local music and activating local player buttons or via web or app.

What file can I tap into to find the state of the player when airplaying? I looked at running processes but nothing there. There must be a place or a file where it writes some data about what’s streaming via airplay.

Thanks

most of the things you could find with the rest api… but i don’t have airplay to test.

Now sure what you mean by the state_file – that is specific to mpd, while airplay is using a different service.
What you probably should be looking into is the provided Volumio APIs API Overview - Volumio Documentation particularly the websocket one if you are interested in state tracking.

Thanks for the info. The state file is created by mpd when music starts or stops. Inside the state file the state if the player is recorded and can be tapped into to check the state. Now I know that airplay doesn’t use mpd so I need to look elsewhere.

I was hoping that during airplay a file is created somewhere in the system. If that was the case I could easily implement an “if file exists exit screensaver” and the opposite, "if file doesn’t exist and state is stop/pause start the screensaver.

If you are planning to run this on the device itself, try typing volumio status.

But the methods your describing are all polling methods, whereas with the websocket API you could subscribe to the state. Try reading my previous links to the API…

That’s exactly what I was looking for. Thanks. I’m not much of a scripter and I know enough to get into trouble so I don’t need to make it too elegant. Your info helped me to get it working. However, I can’t get the script to start at reboot using the @reboot in crontab. I tried to put the sh script in a few locations but nothing is starting. The script works well when launched manually. Any advice? Thanks.

Share you code, you will get some eyeballs and hopefully some suggestions on it :wink:

Volumio is based on Debain, that uses systemd, so you will need to make a service that runs your script.

That helps. I thought I will have to use cron and specify @reboot with my script…this never worked.

So I build a service called screensaver.service and placed it in /lib/systemd/system:

[Unit]
Description=Screensaver
After=network.target
After=local-fs-pre.target

[Service]
Type=oneshot
User=root
ExecStart=/usr/bin/screensaver.sh

[Install]
WantedBy=multi-user.target

My script under /usr/bin. Don’t laugh, I’m not a programmer:

#!/bin/bash

#Path to photos
PHOTO_FOLDER=/mnt/LocalHDD/Pictures

while true
do	
	(volumio status) > /home/volumio/screensaver/status
	STAT=/home/volumio/screensaver/status
	grep -q ': "play",' $STAT

	if [ $? != 0 ]; then
		if ! pgrep -x "feh" > /dev/null; then
		DISPLAY=:0 feh -ZXYrzFD 10 $PHOTO_FOLDER &
		fi
	else
		pidof feh && pkill feh
	fi
	sleep 10s
done

I run:

systemctl enable screensaver.service
systemctl start screensaver.system
systemctl -l status screensaver.service  # and I get this:

● screensaver.service - Screensaver
   Loaded: loaded (/lib/systemd/system/screensaver.service; enabled)
   Active: activating (start) since Wed 2021-08-25 23:51:17 UTC; 4min 14s ago
 Main PID: 623 (screensaver.sh)
   CGroup: /system.slice/screensaver.service
           ├─ 623 /bin/bash /usr/bin/screensaver.sh
           └─2379 sleep 10s

Aug 25 23:54:51 volumio screensaver.sh[623]: No protocol specified
Aug 25 23:54:51 volumio screensaver.sh[623]: feh ERROR: Can't open X display. It *is* running, yeah?
Aug 25 23:55:01 volumio screensaver.sh[623]: No protocol specified
Aug 25 23:55:01 volumio screensaver.sh[623]: feh ERROR: Can't open X display. It *is* running, yeah?
Aug 25 23:55:11 volumio screensaver.sh[623]: No protocol specified
Aug 25 23:55:11 volumio screensaver.sh[623]: feh ERROR: Can't open X display. It *is* running, yeah?
Aug 25 23:55:21 volumio screensaver.sh[623]: No protocol specified
Aug 25 23:55:21 volumio screensaver.sh[623]: feh ERROR: Can't open X display. It *is* running, yeah?
Aug 25 23:55:31 volumio screensaver.sh[623]: No protocol specified
Aug 25 23:55:31 volumio screensaver.sh[623]: feh ERROR: Can't open X display. It *is* running, yeah?

Any idea what I’m doing wrong?

Thanks

That is a good start! But some tips :slight_smile:

– Do you have a X display up and running? Simplest way would probably be using the Touch Display plugin
I would get your screen saver up and running first, and once ironed out move on the the state parsing for Volumio.

– With (volumio status) > /home/volumio/screensaver/status you are creating a file every 10s, not sure if your SD card would like that. Instead, if you need the file write to /tmp or you can just store the output in a variable.

Instead something like this might be easer

#!/bin/bash
#Path to photos
PHOTO_FOLDER=/mnt/LocalHDD/Pictures

while true; do
	if volumio status | grep -q ': "play",'; then
	# Playing
		pidof feh && pkill feh
	else
	# Stopped
		if ! pgrep -x "feh" >/dev/null; then
			DISPLAY=:0 feh -ZXYrzFD 10 $PHOTO_FOLDER &
		fi
	fi
	sleep 10s
done

Optionally, you can use jq to parse the json response directly

	status=$(volumio status | jq -r .status)
	case "${status}" in
	play)
		pidof feh && pkill feh
		;;
	stop)
		if ! pgrep -x "feh" >/dev/null; then
			DISPLAY=:0 feh -ZXYrzFD 10 $PHOTO_FOLDER &
		fi
		;;
	*)
		# Something is wrong
		;;
	esac

wow, that’s amazing. I couldn’t get the result directly from volumio status so I wrote to disk. Thanks a lot, now it runs more efficient.

I’m running 2.907 on a laptop without touch display. The final product will have a touch display but it will not be used as such, although the touch is working out of the box without any plugins or any settings changes.

How can I get the new script to start when booted and run in the background. Do I still create a service for like I mentioned in the previous post?

Thanks again.

I made it into a service with the steps above and it looks like it starts at reboot and when I look at the service status I get this referring to x window again. No clue what that is.

volumio@volumio:~$ systemctl status screensaver.service
● screensaver.service - Screensaver
   Loaded: loaded (/lib/systemd/system/screensaver.service; enabled)
   Active: activating (start) since Thu 2021-08-26 20:00:11 UTC; 6min ago
 Main PID: 620 (screensaver2.sh)
   CGroup: /system.slice/screensaver.service
           ├─ 620 /bin/bash /usr/bin/screensaver2.sh
           └─2715 sleep 10s

Aug 26 20:05:35 volumio screensaver2.sh[620]: No protocol specified
Aug 26 20:05:35 volumio screensaver2.sh[620]: feh ERROR: Can't open X display. It *is* running, yeah?
Aug 26 20:05:45 volumio screensaver2.sh[620]: No protocol specified
Aug 26 20:05:45 volumio screensaver2.sh[620]: feh ERROR: Can't open X display. It *is* running, yeah?
Aug 26 20:05:55 volumio screensaver2.sh[620]: No protocol specified
Aug 26 20:05:55 volumio screensaver2.sh[620]: feh ERROR: Can't open X display. It *is* running, yeah?
Aug 26 20:06:05 volumio screensaver2.sh[620]: No protocol specified
Aug 26 20:06:05 volumio screensaver2.sh[620]: feh ERROR: Can't open X display. It *is* running, yeah?
Aug 26 20:06:15 volumio screensaver2.sh[620]: No protocol specified
Aug 26 20:06:15 volumio screensaver2.sh[620]: feh ERROR: Can't open X display. It *is* running, yeah?

Also, if I run the sh script from the command line it works fine. If I run the same script with sudo I get the same error:

Aug 26 20:06:15 volumio screensaver2.sh[620]: No protocol specified
Aug 26 20:06:15 volumio screensaver2.sh[620]: feh ERROR: Can’t open X display. It *is* running, yeah?

Anything to do with rights? I’ve added a few lines to the script based on this error and none made a difference. Don’t know what any of them do but they were suggestions by others for the same error:

sleep 60

export DISPLAY=:0
export XAUTHORITY=~/.Xauthority sudo
xhost +si:localuser:volumio:root

Did some more testing and removed User=root from the unit file. Now I get the error:

volumio@volumio:~$ systemctl -l status screensaver
● screensaver.service - Screensaver
   Loaded: loaded (/lib/systemd/system/screensaver.service; enabled)
   Active: active (running) since Fri 2021-08-27 00:52:29 UTC; 1min 6s ago
 Main PID: 2505 (screensaver2.sh)
   CGroup: /system.slice/screensaver.service
           ├─2505 /bin/bash /usr/bin/screensaver2.sh
           └─2659 sleep 11s

Aug 27 00:52:29 volumio systemd[1]: Started Screensaver.
Aug 27 00:53:29 volumio screensaver2.sh[2505]: feh ERROR: You have no HOME, cannot read themes

I got it working. Tried a lot of command until the right combo worked. When I start the service from command everything works well with no errors. However, although the service is enabled, it doesn’t start at startup, but it starts manually and works fine. Here is the status after a reboot:

volumio@volumio:~$ systemctl -l status screensaver
● screensaver.service - Screensaver
   Loaded: loaded (/lib/systemd/system/screensaver.service; enabled)
   Active: inactive (dead)

Here are the files:

screensaver.sh

#!/bin/bash

sleep 60
export HOME=/home/volumio
export DISPLAY=:0

#Path to photos
PHOTO_FOLDER=/mnt/LocalHDD/Pictures

while true; do
	if volumio status | grep -q ': "play",'; then
	# Playing
		pidof feh && pkill feh
	else
	# Stopped
		if ! pgrep -x "feh" >/dev/null; then
			DISPLAY=:0 feh -ZXYrzFD 10 $PHOTO_FOLDER &
		fi
	fi
	sleep 11s
done

screensaver.service

[Unit]
Description=Screensaver
After=network.target
After=local-fs-pre.target
After=graphical.target

[Service]
Environment=DISPLAY=:0
User=root
Type=simple
Restart=always
ExecStart=/usr/bin/screensaver2.sh

[Install]
WantedBy=graphical.target

That is a good step!
Try this that does the following
– Set the user/group as Volumio
– Ensure Volumio core is up

[Unit]
Description=Screensaver
After=network.target
After=local-fs-pre.target
After=graphical.target
Requires=volumio.service

[Service]
Environment=DISPLAY=:0
User=volumio
Group=volumio
Type=simple
Restart=always
ExecStart=/usr/bin/screensaver2.sh

[Install]
WantedBy=graphical.target
#!/bin/bash

# These should not longer be required
#sleep 60
#export HOME=/home/volumio
#export DISPLAY=:0

#Path to photos
PHOTO_FOLDER=/mnt/LocalHDD/Pictures

while true; do
	if volumio status | grep -q ': "play",'; then
	# Playing
		pidof feh && pkill feh
	else
	# Stopped
		if ! pgrep -x "feh" >/dev/null; then
			DISPLAY=:0 feh -ZXYrzFD 10 $PHOTO_FOLDER &
		fi
	fi
	sleep 11s
done
  • When I put user/group as volumio the service doesn’t start manually or at reboot.
  • when I take out “export HOME=/home/volumio” I get the error:
    Aug 27 15:23:52 volumio screensaver2.sh[1843]: No protocol specified
    Aug 27 15:23:52 volumio screensaver2.sh[1843]: feh ERROR: Can’t open X display. It is running, yeah?

My only problem now is that the service doesn’t start at reboot but it starts and works ok when done manually. After a reboot, the status shows as:
volumio@volumio:~$ systemctl -l status screensaver
● screensaver.service - Screensaver
Loaded: loaded (/lib/systemd/system/screensaver.service; enabled)
Active: inactive (dead)

Am I missing something to make it start automatically at reboot?

Hmm, I unfortunately run all my devices headless, so can’t test things out for you…
Some ideas though
– Check if the service hasn’t launched and exited on a fresh startup?
– Add a few echo statements to your script to debug

journalctl -u screensaver

Will try that. Thanks again for all your help. Much appreciated.