#!/bin/bash
# Deliver mixer1 output to screen.

# IMPORTANT >>>>>You need to get port, ip and feed_id right<<<<<
#if [ "X$SNOWMIX_PORT" = X ] ; then export SNOWMIX_PORT=9999 ;fi
#if [ "X$SNOWMIX_IP" = X ] ; then export SNOWMIX_IP=127.0.0.1 ;fi
#if [ "X$SNOWMIX" = X ] ; then echo "You need to set the SNOWMIX environment variable" ; exit 1 ; fi

# Set video feed 
#feed_id=$1
#audio_feed_id=$1
audio_sink_id=1
file=$1
# Check for SNOWMIX variable and the snowmix and gstreamer settings
#if [ ! -f $SNOWMIX/scripts/gstreamer-settings -o ! -f $SNOWMIX/scripts/snowmix-settings ] ; then
#  echo "You need to se the environment variable SNOWMIX to the base of the Snowmix directory"
#  exit 1
#fi

# Load the Snowmix and GStreamer settings
#. $SNOWMIX/scripts/gstreamer-settings
#. $SNOWMIX/scripts/snowmix-settings
# This will set
# a) feed_rate
# b) feed_channels
# c) feed_control_pipe
# d) feed_width
# e) feed_height
# f) ctrsocket
# g) system_width
# h) system_height
# i) ratefraction
# j) snowmix
# k) channels
# l) rate

#if [ X$ctrsocket = X -o X$system_width = X -o X$system_height = X ] ; then
#  echo Failed to get control pipe or width or height from running snowmix
#  exit 1
#fi

VIDEOFORMAT=$VIDEOBGRA', width=(int)'$system_width', height=(int)'$system_height', framerate=(fraction)'$ratefraction
OUTPUTFORMAT=$VIDEOBGRA', width=853, height=480'
OUTPUTFORMAT=$VIDEOBGRA', width=853, height=480'

AUDIOFORMATOUT='audio/x-vorbis'
VIDEOFORMATOUT='video/x-theora'
AUDIO_SRC='fdsrc fd=0 do-timestamp=true'
#AUDIO_SRC='audiotestsrc is-live=true'
#VIDEO_SRC='shmsrc socket-path='$ctrsocket' do-timestamp=true is-live=true'
SERVER_PORT=5010
MUXER='matroskamux name=muxer streamable=true'
MUXER='mpegtsmux name=muxer'

THEORASETTINGS='speed-level=3 quality=5'
SHOUTSETTINGS='ip=185.52.224.3 mount=adm-tv.ogv port=8000 password=icecold streamname=test description="" '
SPEEXSETTINGS='quality=5'
VIDEO_SRC='dv1394src'

while true ; do
#  snowmix=`ps c |cut -c28-34 | grep snowmix | head -1`
#  if [ X$snowmix != X ] ;then
#    if [ X$rate != X -a X$channels != X ] ; then
#      ( head -1
	gst-launch-1.0 -v \
          $VIDEO_SRC !\
          decodebin name=decode !\
	  videorate !\
          queue !\
          flvmux name=mux streamable=true !\
          rtmpsink location=rtmp://185.52.224.3/live/test \
          decode. !\
          videoconvert !\
          queue !\
          x264enc !\
          mux.		
#      )
#    else
#	$gstlaunch -v		\
#	  $VIDEO_SRC		!\
#	  $VIDEOFORMAT		!\
#	  queue			!\
#          $VIDEOCONVERT		! \
#	  x264enc bitrate=3000 tune=zerolatency speed-preset=2 key-int-max=50 bframes=0 !\
#	  $VIDEOFORMATOUT	!\
#	  h264parse		!\
#	  queue			!\
#	  matroskamux name=muxer streamable=true !\
#	  queue			!\
#	  tcpserversink host=0.0.0.0 port=$SERVER_PORT sync-method=2 recover-policy=keyframe
#
#	  #videoscale ! 'video/x-raw, width=(int)1024, height=(int)576' ! \
#    fi
#  else
#    echo Snowmix is not running. Quitting $0
#    exit 1
#  fi
#  sleep 2
sleep 2
done
