#!/bin/bash

trim() {
    local var=$@
    var="${var#"${var%%[![:space:]]*}"}"   # remove leading whitespace characters
    var="${var%"${var##*[![:space:]]}"}"   # remove trailing whitespace characters
    echo -n "$var"
}

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
IP=`ip route get 8.8.8.8 | awk 'NR==1 {print $NF}'`
VIDEO_DEVICE=`trim \`v4l2-ctl --list-devices | grep \/dev\/video | head -n 1\``

# bail if no valid video devices
if [ -z "$VIDEO_DEVICE" ]; then
  echo "error: could not find a valid v4l device"
  exit 1
fi

# run as sudo if necessary
#[ "$(whoami)" != "root" ] && exec sudo -- "$0" "$@"

# at this point we should be root
echo "operating on video device $VIDEO_DEVICE"

# touch log
touch video.log
echo "" >> video.log
echo "*** BEGINNING RUN AT `date` ***" >> video.log
echo "" >> video.log

# configure v4l2
sudo v4l2-ctl --device=$VIDEO_DEVICE --set-fmt-video=width=320,height=240,pixelformat=1 >>video.log 2>&1

# set mic volume
amixer -c 1 sset Mic,0 80%,80% unmute cap >>video.log 2>&1

# now stream
echo "stream is available as http://$IP:8080/"
echo "log file is available as video.log"

# working, video only
#cvlc \
#v4l2://$VIDEO_DEVICE:chroma=MJPG:width=800:height=600 \
#--sout '#standard{access=http,mux=ts,dst=:8080,name=stream,mime=video/ts}' \
#-vvv \
#>>video.log 2>&1

# working, video+audio with vlc, audio-only with mplayer*
#--sout '#transcode{acodec=a52,ab=32}:http{mux=ts,dst=:8080,name=stream}' \
#--sout '#transcode{vcodec=FLV1,acodec=mp3,vb=200,deinterlace,fps=25,samplerate=44100,ab=32}:duplicate{dst=std{access=http,mux=ffmpeg{mux=flv},dst=/stream.flv},dst=display}' \
#:input-slave="alsa://hw:1,0:audio=0:channels=1:rate=44100" \
cvlc \
v4l2://$VIDEO_DEVICE:chroma=MJPG:width=320:height=240 \
:input-slave="alsa://plughw:CinemaTM,0" \
--sout '#transcode{acodec=lpcm}:http{mux=ts,dst=:8080,name=stream}' \
:live-cache=10000 :sout-mux-caching=10000 :sout-all :ttl=1 :sout-keep \
--network-caching=10000 --sout-transcode-hurry-up --no-drop-late-frames \
--no-skip-frames --rt-priority \
-vvv \
>>video.log 2>&1

# gstraemer version
#gst-launch-1.0 -v v4l2src \
#! "image/jpeg,width=320,height=240,framerate=30/1" \
#! multipartmux \
#! tcpserversink host=127.0.0.1 port=5000 sync=false alsasrc device=hw:1 \
#! audioconvert ! audioresample \
#! 'audio/x-raw,format=S16LE,channels=1,layout=interleaved' \
#! udpsink host=127.0.0.1 port=5001
#! 'audio/x-raw-int,rate=8000,width=16,channels=1' \

# attempt at using mp4v
#cvlc \
#v4l2://$VIDEO_DEVICE:chroma=MJPG:width=800:height=600 \
#:input-slave="alsa://hw:1,0" \
#--sout '#transcode{vcodec=mp4v,acodec=mpga,vb=800,ab=128}:standard{access=http,mux=ts,dst=:8080}' \
#-vvv \
#>>video.log 2>&1

# attempt at using x264
#cvlc v4l2://$VIDEO_DEVICE:chroma=YUY2:width=800:height=600 :input-slave="alsa://hw:1,0" --sout '#transcode{vcodec=h264,vb=1024,fps=25,width=800,height=600,venc=x264{profile=baseline,level=1},acodec=mp3,ab=128,channels=2,samplerate=48000}:http{dst=:8080,mux=ts}' -vvv

# attempt at using webm
#cvlc \
#v4l2://$VIDEO_DEVICE:chroma=YUY2:width=800:height=600 \
#:input-slave="alsa://hw:1,0" \
#--sout '#transcode{vcodec=VP80,vb=1000}:std{access=http{mime=video/webm},mux=webm,dst=:8080}' \
#-vvv \
#>>video.log 2>&1

# attempt at using ogg
#cvlc \
#v4l2://$VIDEO_DEVICE:chroma=YUY2:width=800:height=600 \
#:input-slave="alsa://hw:1,0" \
#--sout '#transcode{vcodec=theo,vb=2000}:std{access=http{mime=video/ogg},mux=ogg,dst=:8080}' \
#-vvv \
#>>video.log 2>&1

#cvlc v4l2://$VIDEO_DEVICE:chroma=h264:width=1280:height=720 :input-slave="alsa://hw:1,0" --sout '#transcode{acodec=a52,ab=32}:http{mux=ts,dst=:8080,name=stream,mime=video/ts}' -vvv

#new stream broadcast enabled input "http://192.168.0.205:8001/1:0:1:6E2E:431:1:C00000:0:0:0:" output #transcode{vcodec=h264,vb=1024,fps=25,width=800,height=480,venc=x264{profile=baseline,level=1},acodec=mp3,ab=128,channels=2,samplerate=48000}:http{dst=:8080/stream.ts}
