-
Notifications
You must be signed in to change notification settings - Fork 1
/
mjpg-streamer.sh
146 lines (109 loc) · 4.13 KB
/
mjpg-streamer.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
# chmod +x mjpg-streamer.sh
# Crontab: @reboot /home/pi/mjpg-streamer/mjpg-streamer.sh start
# Crontab: @reboot /home/pi/mjpg-streamer/mjpg-streamer-experimental/mjpg-streamer.sh start
MJPG_STREAMER_BIN="/usr/local/bin/mjpg_streamer" # "$(dirname $0)/mjpg_streamer"
MJPG_STREAMER_WWW="/usr/local/share/mjpg-streamer/www"
MJPG_STREAMER_LOG_FILE="${0%.*}.log" # "$(dirname $0)/mjpg-streamer.log"
RUNNING_CHECK_INTERVAL="2" # how often to check to make sure the server is running (in seconds)
HANGING_CHECK_INTERVAL="3" # how often to check to make sure the server is not hanging (in seconds)
VIDEO_DEV="/dev/video0"
FRAME_RATE="5"
QUALITY="80"
RESOLUTION="1280x720" # 160x120 176x144 320x240 352x288 424x240 432x240 640x360 640x480 800x448 800x600 960x544 1280x720 1920x1080 (QVGA, VGA, SVGA, WXGA) # lsusb -s 001:006 -v | egrep "Width|Height" # https://www.textfixer.com/tools/alphabetical-order.php # v4l2-ctl --list-formats-ext # Show Supported Video Formates
PORT="8080"
YUV="yes"
################INPUT_OPTIONS="-r ${RESOLUTION} -d ${VIDEO_DEV} -f ${FRAME_RATE} -q ${QUALITY} -pl 60hz"
INPUT_OPTIONS="-r ${RESOLUTION} -d ${VIDEO_DEV} -q ${QUALITY} -pl 60hz --every_frame 2" # Limit Framerate with "--every_frame ", ( mjpg_streamer --input "input_uvc.so --help" )
if [ "${YUV}" == "true" ]; then
INPUT_OPTIONS+=" -y"
fi
OUTPUT_OPTIONS="-p ${PORT} -w ${MJPG_STREAMER_WWW}"
# ==========================================================
function running() {
if ps aux | grep ${MJPG_STREAMER_BIN} | grep ${VIDEO_DEV} >/dev/null 2>&1; then
return 0
else
return 1
fi
}
function start() {
if running; then
echo "[$VIDEO_DEV] already started"
return 1
fi
export LD_LIBRARY_PATH="$(dirname $MJPG_STREAMER_BIN):."
echo "Starting: [$VIDEO_DEV] ${MJPG_STREAMER_BIN} -i \"input_uvc.so ${INPUT_OPTIONS}\" -o \"output_http.so ${OUTPUT_OPTIONS}\""
${MJPG_STREAMER_BIN} -i "input_uvc.so ${INPUT_OPTIONS}" -o "output_http.so ${OUTPUT_OPTIONS}" >> ${MJPG_STREAMER_LOG_FILE} 2>&1 &
sleep 1
if running; then
if [ "$1" != "nocheck" ]; then
check_running & > /dev/null 2>&1 # start the running checking task
check_hanging & > /dev/null 2>&1 # start the hanging checking task
fi
echo "[$VIDEO_DEV] started"
return 0
else
echo "[$VIDEO_DEV] failed to start"
return 1
fi
}
function stop() {
if ! running; then
echo "[$VIDEO_DEV] not running"
return 1
fi
own_pid=$$
if [ "$1" != "nocheck" ]; then
# stop the script running check task
ps aux | grep $0 | grep start | tr -s ' ' | cut -d ' ' -f 2 | grep -v ${own_pid} | xargs -r kill
sleep 0.5
fi
# stop the server
ps aux | grep ${MJPG_STREAMER_BIN} | grep ${VIDEO_DEV} | tr -s ' ' | cut -d ' ' -f 2 | grep -v ${own_pid} | xargs -r kill
echo "[$VIDEO_DEV] stopped"
return 0
}
function check_running() {
echo "[$VIDEO_DEV] starting running check task" >> ${MJPG_STREAMER_LOG_FILE}
while true; do
sleep ${RUNNING_CHECK_INTERVAL}
if ! running; then
echo "[$VIDEO_DEV] server stopped, starting" >> ${MJPG_STREAMER_LOG_FILE}
start nocheck
fi
done
}
function check_hanging() {
echo "[$VIDEO_DEV] starting hanging check task" >> ${MJPG_STREAMER_LOG_FILE}
while true; do
sleep ${HANGING_CHECK_INTERVAL}
# treat the "error grabbing frames" case
if tail -n2 ${MJPG_STREAMER_LOG_FILE} | grep -i "error grabbing frames" > /dev/null; then
echo "[$VIDEO_DEV] server is hanging, killing" >> ${MJPG_STREAMER_LOG_FILE}
stop nocheck
fi
done
}
function help() {
echo "Usage: $0 [start|stop|restart|status]"
return 0
}
if [ "$1" == "start" ]; then
start && exit 0 || exit -1
elif [ "$1" == "stop" ]; then
stop && exit 0 || exit -1
elif [ "$1" == "restart" ]; then
stop && sleep 1
start && exit 0 || exit -1
elif [ "$1" == "status" ]; then
if running; then
echo "[$VIDEO_DEV] running"
exit 0
else
echo "[$VIDEO_DEV] stopped"
exit 1
fi
else
help
fi