Technology
 

Hack:stream-monitor

From Music Player Daemon Community Wiki

stream-monitor.sh - monitors mpd while it is playing an internet stream.

#!/bin/sh
#
#stream-monitor.sh
#Developed on Debian, with mpd 0.12.1
#Requires mpd and mpc
#
#To Install:
# cp stream-monitor.sh /usr/local/bin/stream-monitor.sh
# chmod 755 /usr/local/bin/stream-monitor.sh
#
#This script monitors mpd while it is playing an internet stream. It checks every 30 seconds
# to see if the status time variable has changed. (The stream is not playing when the time
# is not changing.) Then it clears the playlist, adds the audio stream, and starts playing.
#If the mpd daemon is not running for some reason, it starts it. i.e. if mpd were to crash.
#
#This is great for a robust stream player that keeps on playing, even after a network 
# outage or stream server outage. Useful in situations where the networking is less than perfect.
#
#Check with your streaming server provider before running this. It's not nice to connect to
# a stream 24/7 if you don't need it or own it.
#
#Also does logging.
#
#set AUDIO_SERVER to your server's dns name and port. 
# (read the m3u or pls file in a text editor)

AUDIO_SERVER=http://example.com:5454
LOG_FILE=/var/log/mpd-monitor.log

while [ 1 ]
do

        TIME1=`echo -e "status\\nclose" | nc localhost 6600 | grep time | sed -e "s/time: //"`
        STATUS=`ps -A | grep -c mpd`

        if [ $STATUS -eq 0 ]; then
                echo "`date +"%F %T %s"` - (no mpd processes, starting mpd)" >> $LOG_FILE
                /etc/init.d/mpd start
        else
                if [ "$TIME1" = "$TIME2" ]; then
                        echo "`date +"%F %T %s"` - (mpd not playing, restarting stream)" >> $LOG_FILE
                        mpc clear
                        mpc add $AUDIO_SERVER
                        mpc play
                fi
        fi

        TIME2=`echo -e "status\\nclose" | nc localhost 6600 | grep time | sed -e "s/time: //"`
        sleep 30
done


stream-monitor - Debian startup script to launch stream-monitor.sh

#! /bin/sh
set -e
#
#stream-monitor.sh launch script
#This script is a Debian startup script that handles the startup of stream-monitor.sh
#You may need to use something a little different on other OSs
#
#To Install on Debian:
# cp stream-monitor /etc/init.d/stream-monitor
# chmod 755 /etc/init.d/stream-monitor
# update-rc.d stream-monitor defaults 70

test -x /usr/local/bin/stream-monitor.sh || exit 0 #test for stream-monitor.sh in /usr/local/bin

case "$1" in
  start)
        echo -n "Starting stream-monitor"
        start-stop-daemon --start --make-pidfile --background --quiet --pidfile /var/run/stream-monitor.pid --exec /usr/local/bin/stream-monitor.sh
        echo "."
        ;;
  stop)
        echo -n "Stopping stream-monitor"
        start-stop-daemon --stop --pidfile /var/run/stream-monitor.pid
        echo "."
        ;;

  restart)
        echo -n "Restarting stream-monitor"
        start-stop-daemon --stop --pidfile /var/run/stream-monitor.pid
        start-stop-daemon --start --make-pidfile --background --quiet --pidfile /var/run/stream-monitor.pid --exec /usr/local/bin/stream-monitor.sh
        echo "."
        ;;

  *)
        echo "Usage: /etc/init.d/stream-monitor {start|stop|restart}"
        exit 1
esac

exit 0