#!/bin/bash

set -e
set -x

stream_server="rtmp://49.235.45.154:1935"
framerate=30
main_bitrate=720
sub_bitrate=480
process_name="streamer_whip"
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"

if [ ! -f "$SCRIPT_DIR/$process_name" ]; then
    echo "可执行文件${SCRIPT_DIR}/${process_name}不存在"
    exit 1
fi

if [ ! -x "$SCRIPT_DIR/$process_name" ]; then
    echo "可执行文件${process_name}没有执行权限"
    exit 1
fi

echo $SCRIPT_DIR/$process_name

start_veh01() {
    # nohup gst-launch-1.0 -e v4l2src device=/dev/video3 io-mode=4 do-timestamp=true \
    # ! video/x-raw,width=1280,height=720,framerate=30/1 ! queue max-size-buffers=1 leaky=downstream \
    # ! nvvidconv ! 'video/x-raw(memory:NVMM),format=NV12' ! queue max-size-buffers=1 leaky=downstream \
    # ! nvv4l2h264enc control-rate=0 ratecontrol-enable=1 bitrate=$((main_bitrate * 1000)) vbv-size=$((main_bitrate * 2000)) peak-bitrate=$((main_bitrate * 1500)) preset-level=3 profile=2 insert-sps-pps=0 iframeinterval=${framerate} \
    # ! 'video/x-h264,stream-format=byte-stream' ! h264parse ! flvmux streamable=true \
    # ! rtmpsink location=${stream_server}/xuantong/veh01/front >/dev/null 2>&1 &
    
    # nohup gst-launch-1.0 -e v4l2src device=/dev/video5 io-mode=4 do-timestamp=true \
    # ! video/x-raw,width=1280,height=720,framerate=30/1 ! queue max-size-buffers=1 leaky=downstream \
    # ! nvvidconv ! 'video/x-raw(memory:NVMM),format=NV12,width=640,height=360' ! queue max-size-buffers=1 leaky=downstream \
    # ! nvv4l2h264enc control-rate=0 ratecontrol-enable=1 bitrate=$((sub_bitrate * 1000)) vbv-size=$((sub_bitrate * 2000)) peak-bitrate=$((sub_bitrate * 1500)) preset-level=3 profile=2 insert-sps-pps=0 iframeinterval=${framerate} \
    # ! 'video/x-h264,stream-format=byte-stream' ! h264parse ! flvmux streamable=true \
    # ! rtmpsink location=${stream_server}/xuantong/veh01/back >/dev/null 2>&1 &
    
    # nohup gst-launch-1.0 -e v4l2src device=/dev/video0 io-mode=4 do-timestamp=true \
    # ! video/x-raw,width=1280,height=720,framerate=30/1 ! queue max-size-buffers=1 leaky=downstream \
    # ! nvvidconv ! 'video/x-raw(memory:NVMM),format=NV12,width=640,height=360' ! queue max-size-buffers=1 leaky=downstream \
    # ! nvv4l2h264enc control-rate=0 ratecontrol-enable=1 bitrate=$((sub_bitrate * 1000)) vbv-size=$((sub_bitrate * 2000)) peak-bitrate=$((sub_bitrate * 1500)) preset-level=3 profile=2 insert-sps-pps=0 iframeinterval=${framerate} \
    # ! 'video/x-h264,stream-format=byte-stream' ! h264parse ! flvmux streamable=true \
    # ! rtmpsink location=${stream_server}/xuantong/veh01/left >/dev/null 2>&1 &
    
    # nohup gst-launch-1.0 -e v4l2src device=/dev/video2 io-mode=4 do-timestamp=true \
    # ! video/x-raw,width=1280,height=720,framerate=30/1 ! queue max-size-buffers=1 leaky=downstream \
    # ! nvvidconv ! 'video/x-raw(memory:NVMM),format=NV12,width=640,height=360' ! queue max-size-buffers=1 leaky=downstream \
    # ! nvv4l2h264enc control-rate=0 ratecontrol-enable=1 bitrate=$((sub_bitrate * 1000)) vbv-size=$((sub_bitrate * 2000)) peak-bitrate=$((sub_bitrate * 1500)) preset-level=3 profile=2 insert-sps-pps=0 iframeinterval=${framerate} \
    # ! 'video/x-h264,stream-format=byte-stream' ! h264parse ! flvmux streamable=true \
    # ! rtmpsink location=${stream_server}/xuantong/veh01/right >/dev/null 2>&1 &
    
    nohup $SCRIPT_DIR/$process_name -c $SCRIPT_DIR/config_xuantong.yaml -d veh01 >$SCRIPT_DIR/running.log 2>&1 &
}

start_veh02() {
    # nohup gst-launch-1.0 -e v4l2src device=/dev/video2 io-mode=4 do-timestamp=true \
    # ! video/x-raw,width=1280,height=720,framerate=30/1 ! queue max-size-buffers=1 leaky=downstream \
    # ! nvvidconv ! 'video/x-raw(memory:NVMM),format=NV12,width=640,height=360' ! queue max-size-buffers=1 leaky=downstream \
    # ! nvv4l2h264enc control-rate=0 ratecontrol-enable=1 bitrate=$((main_bitrate * 1000)) vbv-size=$((main_bitrate * 2000)) peak-bitrate=$((main_bitrate * 1500)) preset-level=3 profile=2 insert-sps-pps=0 iframeinterval=${framerate} \
    # ! 'video/x-h264,stream-format=byte-stream' ! h264parse ! flvmux streamable=true \
    # ! rtmpsink location=${stream_server}/xuantong/veh02/front >/dev/null 2>&1 &
    
    # nohup gst-launch-1.0 -e v4l2src device=/dev/video5 io-mode=4 do-timestamp=true \
    # ! video/x-raw,width=1280,height=720,framerate=30/1 ! queue max-size-buffers=1 leaky=downstream \
    # ! nvvidconv ! 'video/x-raw(memory:NVMM),format=NV12,width=640,height=360' ! queue max-size-buffers=1 leaky=downstream \
    # ! nvv4l2h264enc control-rate=0 ratecontrol-enable=1 bitrate=$((sub_bitrate * 1000)) vbv-size=$((sub_bitrate * 2000)) peak-bitrate=$((sub_bitrate * 1500)) preset-level=3 profile=2 insert-sps-pps=0 iframeinterval=${framerate} \
    # ! 'video/x-h264,stream-format=byte-stream' ! h264parse ! flvmux streamable=true \
    # ! rtmpsink location=${stream_server}/xuantong/veh02/back >/dev/null 2>&1 &
    
    # nohup gst-launch-1.0 -e v4l2src device=/dev/video0 io-mode=4 do-timestamp=true \
    # ! video/x-raw,width=1280,height=720,framerate=30/1 ! queue max-size-buffers=1 leaky=downstream \
    # ! nvvidconv ! 'video/x-raw(memory:NVMM),format=NV12,width=640,height=360' ! queue max-size-buffers=1 leaky=downstream \
    # ! nvv4l2h264enc control-rate=0 ratecontrol-enable=1 bitrate=$((sub_bitrate * 1000)) vbv-size=$((sub_bitrate * 2000)) peak-bitrate=$((sub_bitrate * 1500)) preset-level=3 profile=2 insert-sps-pps=0 iframeinterval=${framerate} \
    # ! 'video/x-h264,stream-format=byte-stream' ! h264parse ! flvmux streamable=true \
    # ! rtmpsink location=${stream_server}/xuantong/veh02/left >/dev/null 2>&1 &
    
    # nohup gst-launch-1.0 -e v4l2src device=/dev/video3 io-mode=4 do-timestamp=true \
    # ! video/x-raw,width=1280,height=720,framerate=30/1 ! queue max-size-buffers=1 leaky=downstream \
    # ! nvvidconv ! 'video/x-raw(memory:NVMM),format=NV12,width=640,height=360' ! queue max-size-buffers=1 leaky=downstream \
    # ! nvv4l2h264enc control-rate=0 ratecontrol-enable=1 bitrate=$((sub_bitrate * 1000)) vbv-size=$((sub_bitrate * 2000)) peak-bitrate=$((sub_bitrate * 1500)) preset-level=3 profile=2 insert-sps-pps=0 iframeinterval=${framerate} \
    # ! 'video/x-h264,stream-format=byte-stream' ! h264parse ! flvmux streamable=true \
    # ! rtmpsink location=${stream_server}/xuantong/veh02/right >/dev/null 2>&1 &
    nohup $SCRIPT_DIR/$process_name -c $SCRIPT_DIR/config_xuantong.yaml -d veh02 >$SCRIPT_DIR/running.log 2>&1 &
}


stop() {
    ps -ef|grep "${process_name}"|grep -v grep|awk '{print $2}'|xargs -I{} kill -9 {}
    
    # trigger_count=$(ps -ef | grep "tztek-jetson-tool-internal-trigger-camera" | grep -v grep| wc -l)
    # if [ $trigger_count -gt 0 ]; then
    #     ps -ef|grep "tztek-jetson-tool-internal-trigger-camera" |grep -v grep| awk '{print $2}' | xargs -I{} sudo kill -9 {}
    # fi
}


start() {
    gst_count=$(ps -ef | grep "${process_name}" | grep -v grep| wc -l)
    if [ $gst_count -ge 1 ]; then
        exit 0
    fi
    
    # ps -ef|grep "${process_name}"|grep -v grep|awk '{print $2}'|xargs -I{} kill -9 {}
    
    # trigger_count=$(ps -ef | grep "tztek-jetson-tool-internal-trigger-camera" | grep -v grep| wc -l)
    # if [ $trigger_count -eq 0 ]; then
    #     sudo nohup tztek-jetson-tool-internal-trigger-camera /dev/ttyTHS1 30 1000 > /dev/null 2>&1 &
    # fi
    
    sleep 1
    
    if [ $# -eq 0 ]; then
        stop
    else
        case $1 in
            veh01)
                start_veh01
            ;;
            veh02)
                start_veh02
            ;;
            *)
                echo "Usage: $0 [veh01 | veh02 | stop]"
                exit 1
            ;;
        esac
    fi
}


if [ $# -eq 0 ]; then
    stop
    echo "Usage: $0 [veh01 | veh02 | stop]"
    exit 1
else
    case $1 in
        veh01)
            start veh01
        ;;
        veh02)
            start veh02
        ;;
        stop)
            stop
        ;;
        *)
            echo "Usage: $0 [veh01 | veh02 | stop]"
            exit 1
        ;;
    esac
fi
