Raspberry Pi 4: enable streamEye & more

This commit is contained in:
Calin Crisan 2019-07-27 16:32:43 +03:00
parent ee6a72badc
commit 9caf02e706
4 changed files with 1750 additions and 8 deletions

View File

@ -48,14 +48,6 @@ speed=$(($speed / 1024))
echo -n "$temp°|$load|${speed}kB/s"
'''
OVERCLOCK = {
700: '700|250|400|0',
800: '800|250|400|0',
900: '900|250|450|0',
950: '950|250|450|0',
1000: '1000|500|600|6'
}
def _get_board_settings():
gpu_mem = 128
camera_led = True

View File

@ -0,0 +1,225 @@
# Copyright (c) 2015 Calin Crisan
# This file is part of motionEyeOS.
#
# motionEyeOS is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import os.path
from config import additional_config
import streameyectl
CONFIG_TXT = '/boot/config.txt'
MONITOR = '/data/etc/monitor_1'
MONITOR_SCRIPT = '''#!/bin/bash
net_tmp=/tmp/netspeed.tmp
temp=$(($(cat /sys/devices/virtual/thermal/thermal_zone0/temp) / 1000))
load=$(cat /proc/loadavg | cut -d ' ' -f 2)
recv=$(cat /proc/net/dev | grep -v 'lo:' | tr -s ' ' | cut -d ' ' -f 3 | tail -n +3 | awk '{s+=$1} END {print s}')
send=$(cat /proc/net/dev | grep -v 'lo:' | tr -s ' ' | cut -d ' ' -f 11 | tail -n +3 | awk '{s+=$1} END {print s}')
total=$(($recv + $send))
if [ -e $net_tmp ]; then
prev_total=$(cat $net_tmp)
speed=$(($total - $prev_total))
else
speed=0
fi
echo $total > $net_tmp
speed=$(($speed / 1024))
echo -n "$temp&deg;|$load|${speed}kB/s"
'''
def _get_board_settings():
gpu_mem = 128
camera_led = True
arm_freq = None
if os.path.exists(CONFIG_TXT):
logging.debug('reading board settings from %s' % CONFIG_TXT)
with open(CONFIG_TXT) as f:
for line in f:
line = line.strip()
if not line or line.startswith('#'):
continue
parts = line.split('=', 1)
if len(parts) != 2:
continue
name, value = parts
name = name.strip()
value = value.strip()
if name == 'gpu_mem':
gpu_mem = int(value)
elif name == 'arm_freq':
arm_freq = int(value)
elif name == 'disable_camera_led':
camera_led = value == '0'
s = {
'gpuMem': gpu_mem,
'cameraLed': camera_led
}
logging.debug('board settings: gpu_mem=%(gpuMem)s, camera_led=%(cameraLed)s' % s)
return s
def _set_board_settings(s):
s.setdefault('gpuMem', 128)
s.setdefault('cameraLed', True)
old_settings = _get_board_settings()
if s == old_settings:
return # nothing has changed
seen = set()
logging.debug('writing board settings to %s: ' % CONFIG_TXT +
'gpu_mem=%(gpuMem)s, camera_led=%(cameraLed)s' % s)
lines = []
if os.path.exists(CONFIG_TXT):
with open(CONFIG_TXT) as f:
lines = f.readlines()
for i, line in enumerate(lines):
line = line.strip()
if not line:
continue
line = line.strip('#')
try:
name, _ = line.split('=', 1)
name = name.strip()
except:
continue
seen.add(name)
if name.startswith('gpu_mem'):
lines[i] = '%s=%s' % (name, s['gpuMem'])
elif name == 'disable_camera_led':
lines[i] = 'disable_camera_led=%s' % ['1', '0'][s['cameraLed']]
if 'gpu_mem' not in seen:
lines.append('gpu_mem=%s' % s['gpuMem'])
if 'disable_camera_led' not in seen:
lines.append('disable_camera_led=%s' % ['1', '0'][s['cameraLed']])
logging.debug('remounting /boot read-write')
if os.system('mount -o remount,rw /boot'):
logging.error('failed to remount /boot read-write')
with open(CONFIG_TXT, 'w') as f:
for line in lines:
if not line.strip():
continue
if not line.endswith('\n'):
line += '\n'
f.write(line)
def _get_sys_mon():
return os.access(MONITOR, os.X_OK)
def _set_sys_mon(sys_mon):
if sys_mon:
if os.access(MONITOR, os.X_OK):
pass
else:
if os.path.exists(MONITOR):
os.system('chmod +x %s' % MONITOR)
else:
with open(MONITOR, 'w') as f:
f.write(MONITOR_SCRIPT)
os.system('chmod +x %s' % MONITOR)
else:
if os.access(MONITOR, os.X_OK):
os.system('chmod -x %s' % MONITOR)
@additional_config
def boardSeparator():
return {
'type': 'separator',
'section': 'expertSettings'
}
@additional_config
def gpuMem():
return {
'label': 'GPU Memory',
'description': 'set the amount of memory reserved for the GPU (choose at least 96MB if you use the CSI camera board)',
'type': 'number',
'min': '16',
'max': '944',
'section': 'expertSettings',
'reboot': True,
'get': _get_board_settings,
'set': _set_board_settings,
'get_set_dict': True
}
@additional_config
def cameraLed():
return {
'label': 'Enable CSI Camera Led',
'description': 'control the led on the CSI camera board',
'type': 'bool',
'section': 'expertSettings',
'reboot': True,
'get': _get_board_settings,
'set': _set_board_settings,
'get_set_dict': True
}
@additional_config
def sysMon():
return {
'label': 'Enable System Monitoring',
'description': 'when this is enabled, system monitoring info will be overlaid on top of the first camera frame',
'type': 'bool',
'section': 'expertSettings',
'reboot': False,
'get': _get_sys_mon,
'set': _set_sys_mon
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,305 @@
#!/bin/bash
RASPIMJPEG_CONF=/data/etc/raspimjpeg.conf
RASPIMJPEG_LOG=/var/log/raspimjpeg.log
GSTREAMER_LOG=/var/log/gstreamer.log
MOTIONEYE_CONF=/data/etc/motioneye.conf
STREAMEYE_CONF=/data/etc/streameye.conf
STREAMEYE_LOG=/var/log/streameye.log
test -r ${RASPIMJPEG_CONF} || exit 1
test -r ${STREAMEYE_CONF} || exit 1
function watch() {
source ${STREAMEYE_CONF}
count=0
while true; do
sleep 5
if [ "${PROTO}" = "rtsp" ]; then
if ! ps aux | grep test-launch | grep -v grep &>/dev/null; then
logger -t streameye -s "not running, respawning"
start
fi
else
if ! ps aux | grep raspimjpeg.py | grep -v grep &>/dev/null; then
logger -t streameye -s "not running, respawning"
start
fi
fi
done
}
function configure_v4l2_cam() {
video_arg="-d $1"
horizontal_flip="0"
hflip=$(grep -e ^hflip ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
if [ "${hflip}" = "true" ]; then horizontal_flip="1"; fi
v4l2-ctl ${video_arg} --set-ctrl=horizontal_flip=${horizontal_flip} &>/dev/null
vertical_flip="0"
vflip=$(grep -e ^vflip ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
if [ "${vflip}" = "true" ]; then vertical_flip="1"; fi
v4l2-ctl ${video_arg} --set-ctrl=vertical_flip=${vertical_flip} &>/dev/null
rotate=$(grep -e ^rotation ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
if [ -z "${rotate}" ]; then rotate="0"; fi
v4l2-ctl ${video_arg} --set-ctrl=rotate=${rotate} &>/dev/null
brightness=$(grep -e ^brightness ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
if [ -z "${brightness}" ]; then brightness="50"; fi
v4l2-ctl ${video_arg} --set-ctrl=brightness=${brightness} &>/dev/null
contrast=$(grep -e ^contrast ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
if [ -z "${contrast}" ]; then contrast="0"; fi
v4l2-ctl ${video_arg} --set-ctrl=contrast=${contrast} &>/dev/null
saturation=$(grep -e ^saturation ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
if [ -z "${saturation}" ]; then saturation="0"; fi
v4l2-ctl ${video_arg} --set-ctrl=saturation=${saturation} &>/dev/null
sharpness=$(grep -e ^sharpness ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
if [ -z "${sharpness}" ]; then sharpness="0"; fi
v4l2-ctl ${video_arg} --set-ctrl=sharpness=${sharpness} &>/dev/null
vstab=$(grep -e ^vstab ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
if [ -z "${vstab}" ]; then vstab="0"; fi
v4l2-ctl ${video_arg} --set-ctrl=image_stabilization=${vstab} &>/dev/null
shutter=$(grep -e ^shutter ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
if [ -z "${shutter}" ]; then shutter="0"; fi
if [ "${#shutter}" -gt 2 ]; then shutter=$(echo ${shutter/%??/}); else shutter="0"; fi
v4l2-ctl ${video_arg} --set-ctrl=exposure_time_absolute=${shutter} &>/dev/null
iso=$(grep -e ^iso ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
if [ -z "${iso}" ]; then iso="0"; fi
if [ "${iso}" -ge 800 ]; then iso="4"
elif [ "${iso}" -ge 400 ]; then iso="3"
elif [ "${iso}" -ge 200 ]; then iso="2"
elif [ "${iso}" -ge 100 ]; then iso="1"
else iso="0"
fi
v4l2-ctl ${video_arg} --set-ctrl=iso_sensitivity=${iso} &>/dev/null
awb=$(grep -e ^awb ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
if [ "${awb}" = "off" ]; then awb="0"
elif [ "${awb}" = "auto" ]; then awb="1"
elif [ "${awb}" = "sunlight" ]; then awb="6"
elif [ "${awb}" = "cloudy" ]; then awb="8"
elif [ "${awb}" = "shade" ]; then awb="9"
elif [ "${awb}" = "tungsten" ]; then awb="4"
elif [ "${awb}" = "fluorescent" ]; then awb="3"
elif [ "${awb}" = "incandescent" ]; then awb="2"
elif [ "${awb}" = "flash" ]; then awb="7"
elif [ "${awb}" = "horizon" ]; then awb="5"
else awb="1"
fi
v4l2-ctl ${video_arg} --set-ctrl=white_balance_auto_preset=${awb} &>/dev/null
metering=$(grep -e ^metering ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
if [ "${metering}" = "spot" ]; then metering="2"
elif [ "${metering}" = "center" ]; then metering="1"
else metering="0"
fi
v4l2-ctl ${video_arg} --set-ctrl=exposure_metering_mode=${awb} &>/dev/null
scene="0"
exposure=$(grep -e ^exposure ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
if [ "${exposure}" = "off" ]; then
exposure="1"
else
if [ "${exposure}" = "night" ]; then scene="8"
elif [ "${exposure}" = "backlight" ]; then scene="1"
elif [ "${exposure}" = "sports" ]; then scene="11"
elif [ "${exposure}" = "snow" ]; then scene="2"
elif [ "${exposure}" = "beach" ]; then scene="2"
elif [ "${exposure}" = "fireworks" ]; then scene="6"
fi
exposure="0"
fi
v4l2-ctl ${video_arg} --set-ctrl=auto_exposure=${exposure} &>/dev/null
v4l2-ctl ${video_arg} --set-ctrl=scene_mode=${scene} &>/dev/null
ev=$(grep -e ^ev ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
if [ -z "${ev}" ]; then ev="0"; fi
ev=$(expr ${ev} + 25)
ev=$(expr ${ev} / 2)
if [ "${ev}" -gt 24 ]; then ev="24"; fi
v4l2-ctl ${video_arg} --set-ctrl=auto_exposure_bias=${ev} &>/dev/null
video_bitrate=$(grep -e ^bitrate ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
if [ -z "${video_bitrate}" ]; then video_bitrate="1000000"; fi
v4l2-ctl ${video_arg} --set-ctrl=video_bitrate=${video_bitrate} &>/dev/null
}
function invalid_opt() {
local e match="$1"
shift
for e; do [[ "${e}" == "${match}" ]] && return 1; done
return 0
}
function start() {
source ${STREAMEYE_CONF}
streameye_opts="-p ${PORT}"
if [ -n "${CREDENTIALS}" ] && [ "${AUTH}" = "basic" ]; then
streameye_opts="${streameye_opts} -a basic -c ${CREDENTIALS}"
fi
if [ "${PROTO}" = "rtsp" ]; then
pid=$(ps | grep test-launch | grep -v grep | tr -s ' ' | sed -e 's/^\s//' | cut -d ' ' -f 1)
if [ -n "${pid}" ]; then
return
fi
RTSP_PORT=${RTSP_PORT:-554}
iptables -A INPUT -p tcp -s localhost --dport ${RTSP_PORT} -j ACCEPT
iptables -A INPUT -p tcp --dport ${RTSP_PORT} -j DROP
audio_opts=""
if [ -n "${AUDIO_DEV}" ]; then
audio_bitrate=$(grep -e ^audio_bitrate ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
audio_channels=$(grep -e ^audio_channels ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
audio_extras=""
if [ -n "${audio_bitrate}" ]; then
audio_extras="${audio_extras},rate=${audio_bitrate}"
fi
if [ -n "${audio_channels}" ]; then
audio_extras="${audio_extras},channels=${audio_channels}"
fi
audio_opts="alsasrc device=${AUDIO_DEV} ! audioresample ! audio/x-raw${audio_extras} ! queue ! voaacenc ! rtpmp4gpay pt=97 name=pay1"
fi
video_path="/dev/video0"
if [ -n "${VIDEO_DEV}" ]; then
video_path=${VIDEO_DEV}
fi
if [ -e "${video_path}" ]; then
# Only configure camera if it is a Pi Cam
if v4l2-ctl -d ${video_path} -D | grep -q 'bm2835 mmal'; then
configure_v4l2_cam ${video_path}
fi
width=$(grep -e ^width ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
height=$(grep -e ^height ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
framerate=$(grep -e ^framerate ${RASPIMJPEG_CONF} | cut -d ' ' -f 2)
video_extras=""
if [ -n "${width}" ]; then
video_extras="${video_extras},width=${width}"
fi
if [ -n "${height}" ]; then
video_extras="${video_extras},height=${height}"
fi
if [ -n "${framerate}" ]; then
video_extras="${video_extras},framerate=${framerate}/1"
fi
video_opts="v4l2src device=${video_path} ! video/x-h264${video_extras} ! h264parse ! rtph264pay name=pay0 pt=96"
if [ -r ${MOTIONEYE_CONF} ] && grep 'log-level debug' ${MOTIONEYE_CONF} >/dev/null; then
streameye_opts="${streameye_opts} -d"
fi
test-launch -p ${RTSP_PORT} -m h264 "\"( ${video_opts} ${audio_opts} )\"" &>${GSTREAMER_LOG} &
sleep 10
gst-launch-1.0 -v rtspsrc location=rtsp://127.0.0.1:${RTSP_PORT}/h264 latency=0 drop-on-latency=1 ! rtph264depay ! h264parse ! omxh264dec ! videorate ! video/x-raw,framerate=5/1 ! jpegenc ! filesink location=/dev/stdout | streameye ${streameye_opts} &>${STREAMEYE_LOG} &
sleep 5
fi
iptables -D INPUT -p tcp --dport ${RTSP_PORT} -j DROP
iptables -D INPUT -p tcp -s localhost --dport ${RTSP_PORT} -j ACCEPT
else
pid=$(ps | grep raspimjpeg.py | grep -v grep | tr -s ' ' | sed -e 's/^\s//' | cut -d ' ' -f 1)
if [ -n "${pid}" ]; then
return
fi
valid_opts=("awb" "brightness" "contrast" "denoise" "drc" "ev" "exposure" "framerate" "height" "hflip" "imxfx" "iso" "metering" "preview" "quality" "rotation" "saturation" "sharpness" "shutter" "vflip" "vstab" "width" "zoom")
raspimjpeg_opts=""
while read line; do
key=$(echo ${line} | cut -d ' ' -f 1)
if invalid_opt "${key}" "${valid_opts[@]}"; then
continue
fi
if echo "${line}" | grep false &>/dev/null; then
continue
fi
if echo "${line}" | grep true &>/dev/null; then
line=${key}
fi
raspimjpeg_opts="${raspimjpeg_opts} --${line}"
done < ${RASPIMJPEG_CONF}
if [ -r ${MOTIONEYE_CONF} ] && grep 'log-level debug' ${MOTIONEYE_CONF} >/dev/null; then
raspimjpeg_opts="${raspimjpeg_opts} -d"
streameye_opts="${streameye_opts} -d"
fi
raspimjpeg.py ${raspimjpeg_opts} 2>${RASPIMJPEG_LOG} | streameye ${streameye_opts} &>${STREAMEYE_LOG} &
fi
}
function stop() {
# stop the streameye background watch process
ps | grep streameye.sh | grep -v $$ | grep -v S94streameye| grep -v grep | tr -s ' ' | sed -e 's/^\s//' | cut -d ' ' -f 1 | xargs -r kill
# stop the raspimjpeg process
raspimjpeg_pid=$(ps | grep raspimjpeg.py | grep -v grep | tr -s ' ' | sed -e 's/^\s//' | cut -d ' ' -f 1)
# stop the gst-launch-1.0 process
gst_launch_pid=$(ps | grep gst-launch-1.0 | grep -v grep | tr -s ' ' | sed -e 's/^\s//' | cut -d ' ' -f 1)
# stop the test-launch process
test_launch_pid=$(ps | grep test-launch | grep -v grep | tr -s ' ' | sed -e 's/^\s//' | cut -d ' ' -f 1)
if [ -n "${raspimjpeg_pid}" ]; then
kill -HUP "${raspimjpeg_pid}" &>/dev/null
count=0
while kill -0 "${raspimjpeg_pid}" &>/dev/null && [ ${count} -lt 5 ]; do
sleep 1
count=$((${count} + 1))
done
kill -KILL "${raspimjpeg_pid}" &>/dev/null || true
fi
if [ -n "${gst_launch_pid}" ]; then
kill -HUP "${gst_launch_pid}" &>/dev/null
count=0
while kill -0 "${gst_launch_pid}" &>/dev/null && [ ${count} -lt 5 ]; do
sleep 1
count=$((${count} + 1))
done
kill -KILL "${gst_launch_pid}" &>/dev/null || true
fi
if [ -n "${test_launch_pid}" ]; then
kill -HUP "${test_launch_pid}" &>/dev/null
count=0
while kill -0 "${test_launch_pid}" &>/dev/null && [ ${count} -lt 5 ]; do
sleep 1
count=$((${count} + 1))
done
kill -KILL "${test_launch_pid}" &>/dev/null || true
fi
}
case "$1" in
start)
start
watch &
;;
stop)
stop
;;
restart)
stop
start
watch &
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac