#!/bin/sh
# pepoliveserver ;  liveserver for raspberry pi
# licence GPLv3 ; this scripts designed by Isamu.Yamauchi 2011.11.28 update 2015.1.14
# pepoliveserver dev timer
PATH=$PATH:/usr/local/bin:/usr/local/sbin
WORKDIR=/www/remote-hand/tmp
pid=`echo $$`
prog=pepoliveserver
pidfile=/var/run/${prog}.pid
#CONF=${WORKDIR}/ffserver.conf
LIVEFILE=${WORKDIR}/remote-hand.ffm
#VIDEOPG=ffmpeg
VIDEOPG=avconv
#VIDEOSV=ffserver
VIDEOSV=avserver
CONF=/etc/${VIDEOSV}.conf
TIMER=45000
if [ $# -lt 1 -o $# -gt 2 ];then
  echo "usage: $0 /dev/video0 | timer(seconds)"
  exit
fi
DEV=$1
if [ ! -e $DEV ];then
  echo $1 not found ;exit
fi
[ $# = 2 ] && TIMER=$2
if [ -e $pidfile ];then
  tpid=`cat $pidfile`
  kill -HUP $tpid
  msleep 1000
  [ -e $pidfile ] && rm -f $pidfile
fi
if [ ${VIDEOPG} != "ffmpeg" ];then
  OPT="-r 5 -vcodec flv"
#  OPT="-r 5 -vcodec mpeg4"
#OPT="-r 5 -vcodec h264"
else
  OPT=""
fi
error(){
  if [ -e $pidfile ];then
    VIDEOPG_PID=`pgrep ${VIDEOPG}` ; [ ! -z ${VIDEOPG_PID} ] && kill -HUP ${VIDEOPG_PID}
    VIDEOSERVER_PID=`pgrep ${VIDEOSV}` ; [ ! -z ${VIDEOSERVER_PID} ] && kill -HUP ${VIDEOSERVER_PID}
    rm -f $pidfile
	[ -e ${LIVEFILE} ] && rm -f ${LIVEFILE}
  fi
  exit
}
trap error SIGINT SIGTERM SIGHUP SIGKILL SIGCHLD 
IP=`ip -f inet addr|awk '/inet 192.168/{split($2,I,"/");printf I[1]}'`
[  -e $CONF ] && rm -f $CONF
if [ ! -e $CONF ];then
cat >$CONF<<EOF
# $CONF
######################################
# This file specifies all the options for streaming
# an avconv produced file as flash
######################################
Port 8090 # this is the port you have set up for streaming
BindAddress 0.0.0.0
MaxHTTPConnections 10
MaxClients 10
MaxBandwidth 1000
<Feed remote-hand.ffm>
File ${LIVEFILE}
FileMaxSize 10M
</Feed>
# swf output - good for streaming
<Stream remote-hand.swf>
# the source feed
Feed remote-hand.ffm
# the output stream format - FLV = FLash Video
Format swf
# this must match the ffmpeg -r argument
VideoFrameRate 5
# generally leave this is a large number
VideoBufferSize 80000
# another quality tweak
VideoBitRate 500
# quality ranges - 1-31 (1 = best, 31 = worst)
VideoQMin 1
VideoQMax 5
#VideoSize 640x480
VideoSize 320x240
# this sets how many seconds in past to start
PreRoll 0
# wecams don't have audio
NoAudio
</Stream>
<Stream stat.html>
Format status
</Stream>
<Redirect index.html>
# credits!
URL http://$IP:8090/stat.html
</Redirect>
################################################################################
#
#URL http://ffmpeg.sourceforge.net/
# ${VIDEOPG} -f video4linux2 -s 320x240 -i $DEV $OPT http://$IP:8090/remote-hand.ffm >/dev/null 2>&1
EOF
fi

VIDEOPG_PID=`pgrep ${VIDEOPG}` ; [ ! -z ${VIDEOPG_PID} ] && kill -HUP ${VIDEOPG_PID}
VIDEOSERVER_PID=`pgrep ${VIDEOSV}` ; [ ! -z ${VIDEOSERVER_PID} ] && kill -HUP ${VIDEOSERVER_PID}
msleep 100
[ -e $pidfile ] && rm -f $pidfile
echo -n $pid >${pidfile}
[ -e ${LIVEFILE} ] && rm -f ${LIVEFILE}
${VIDEOSV} -v verbose >/dev/null 2>&1 &
${VIDEOPG} -f video4linux2 -s 320x240 -i $DEV $OPT http://$IP:8090/remote-hand.ffm >/dev/null 2>&1 &
msleep $TIMER
VIDEOPG_PID=`pgrep ${VIDEOPG}` ; [ ! -z ${VIDEOPG_PID} ] && kill -HUP ${VIDEOPG_PID}
VIDEOSERVER_PID=`pgrep ${VIDEOSV}` ; [ ! -z ${VIDEOSERVER_PID} ] && kill -HUP ${VIDEOSERVER_PID}
kill -TERM $pid
