#!/bin/sh
# ffpip 006 von ice32
#+yadif=2:-1:0,mcdeint=2:1:1

function ffplay() {
    if [[ "$osd_res" == 0 ]]; then
        x_scale=4*"$zoom"
        y_scale=3*"$zoom"/1.3
        x_offset=1280-"$x_scale"-"$x_pos"
    fi
    nice -n -1 ffmpeg -nostdin -loglevel panic -threads 2 -err_detect ignore_err -fflags igndts \
        -i http://"$ip_box":"$port"/id="$channel":loop=-1:50:0 -streamid 0:0 \
        -map 0:v -vf scale="$x_scale":"$y_scale":force_original_aspect_ratio=disable \
        -f:v fps=50+idet+field=1 -sws_flags fast_bilinear \
        -vsync -1 -an -sn -dn \
	-pix_fmt bgra -yoffset "$y_pos" -xoffset "$x_offset" -r 1 \
        -f fbdev /dev/fb0 &
}


function ffplay2() {
    if [[ "$osd_res" == 0 ]]; then
        x_scale=4*"$zoom"
        y_scale=3*"$zoom"/1.3
        x_offset=1280-"$x_scale"-"$x_pos"
    fi
    nice -n -1 ffmpeg -nostdin -loglevel panic -threads 2 -err_detect ignore_err -fflags igndts \
        -i http://192.168.178.33:"$port"/id="$channel":loop=-1:50:0 -streamid 0:0 \
        -map 0:v -vf scale="$x_scale":"$y_scale":force_original_aspect_ratio=disable \
        -f:v fps=50+idet+field=1 -sws_flags fast_bilinear \
        -vsync -1 -an -sn -dn \
	-pix_fmt bgra -yoffset "$y_pos" -xoffset "$x_offset" -r 1 \
        -f fbdev /dev/fb0 &
}


function ffkill() {
    kill -TERM "$ffmpid"
}


function text() {
    msgbox popup="PiP ""$1" timeout=3 hide=1 refresh=1 icon=/var/tuxbox/plugins/ffpip_hint.png
}

function ffnow() {
    ffget=$(ps aux | grep id= | cut -d'=' -f2 | awk '{print $1}')
}

ffmpid=$(pidof ffmpeg)
osd_res=$(pzapit -osd)
channel=$(pzapit -gi | awk '{print $1}')
ip_box=$(ifconfig |grep -m 1 'inet addr'| cut -d':' -f2| awk '{print $1}')
port=31339

zoom=80
x_scale=6*"$zoom"
y_scale=4.5*"$zoom"/1.3
x_pos=15
y_pos=15
x_offset=1920-"$x_scale"-"$x_pos"
x_zap="$x_offset"

schon="läuft schon..."
starten="wird gestartet..."
tausch="tausche Bild..."
ende="wird beendet..."
webtv="spielt kein webtv..."
links="Start links..."
rechts="Start rechts..."
links2="-Stream Start links..."
rechts2="-Stream Start rechts..."

msgbox size=20 title="PiP Auswahl" select="Links,Rechts,Switch,Stopp,Stream links,Stream rechts" default=2 cyclic=0 order=2 msg="" absolute=1 refresh=1 icon=/var/tuxbox/plugins/ffpip_hint.png

case "$?" in

     1)	 ffnow
	 	ffkill
	 	channel="$ffget"
		x_offset=15
	 	ffplay
	 	text "$links"
	 ;;

     2)	 ffnow
	 	ffkill
	 	channel="$ffget"
	 	x_offset="$x_zap"
	 	ffplay
	 	text "$rechts"
	 ;;

	 3)   ffnow
         no_webtv $channel
         ffkill
         ffplay
         text "$tausch"
         pzapit -zi "$ffget"
	 ;;

	 4)	 ffkill
    	 text "$ende"
     ;;

     5)	 ffnow
	 	ffkill
	 	channel="$ffget"
	 	x_offset=15
	 	ffplay2
	 	text "$links2"
	 ;;

	 6)	 ffnow
	 	ffkill
	 	channel="$ffget"
	 	x_offset="$x_zap"
	 	ffplay2
	 	text "$rechts2"
	 ;;

esac

exit 0





