#!/bin/sh
PIDFILE=/run/rtsp-server.pid

[ -f /run/ground ] || exit 0

format=$(uci -q get keenfalcon.encoder.format)
if [ "${format}" != "h265" ]; then
	format=h264
fi

#launch="( udpsrc host=127.0.0.1 port=5000 do-timestamp=1 ! ${format}parse ! rtp${format}pay name=pay0 pt=96 config-interval=-1 )"

case "$1" in
	start)
		printf "Starting RTSP server: "
		start-stop-daemon -S -b -p ${PIDFILE} -m -x /usr/bin/rtsp-server
		[ $? = 0 ] && echo "OK" || echo "FAIL"
		;;
	stop)
		printf "Stopping RTSP server: "
		start-stop-daemon -K -q -p ${PIDFILE}
		rm -f ${PIDFILE}
		[ $? = 0 ] && echo "OK" || echo "FAIL"
		;;
	restart|reload)
		$0 stop
		$0 start
		;;
	*)
		echo "Usage: $0 {start|stop|restart}"
		exit 1
esac

exit 0
