Skip to content

Commit

Permalink
feat: add legacy cam support (#146)
Browse files Browse the repository at this point in the history
* feat: add legacy cam support

Signed-off-by: Patrick Gehrsitz <58853838+mryel00@users.noreply.github.com>

* feat: add blockyfix

Signed-off-by: Patrick Gehrsitz <58853838+mryel00@users.noreply.github.com>

* fix: add ustreamer legacy cam workaround

Signed-off-by: Patrick Gehrsitz <58853838+mryel00@users.noreply.github.com>

---------

Signed-off-by: Patrick Gehrsitz <58853838+mryel00@users.noreply.github.com>
  • Loading branch information
mryel00 committed Jul 22, 2023
1 parent 7dd2454 commit 66a43b6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
17 changes: 17 additions & 0 deletions libs/hwhandler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ get_libcamera_path() {
fi
}

# Determine connected "legacy" device
function detect_legacy {
local avail
if [[ -f /proc/device-tree/model ]] &&
grep -q "Raspberry" /proc/device-tree/model; then
avail="$(vcgencmd get_camera | awk -F '=' '{ print $3 }' | cut -d',' -f1)"
else
avail="0"
fi
echo "${avail}"
}

function dev_is_legacy {
v4l2-ctl --list-devices | grep -A1 -e 'mmal' | \
awk 'NR==2 {print $1}'
}

## Determine if cam has H.264 Hardware encoder
## call detect_h264 <nameornumber> ex.: detect_h264 foobar
## returns 1 = true / 0 = false ( numbers are strings! not int!)
Expand Down
11 changes: 10 additions & 1 deletion libs/logging.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function print_cfg {
function print_cams {
local total v4l
v4l="$(find /dev/v4l/by-id/ -iname "*index0" 2> /dev/null | wc -l)"
total="$((v4l+($(detect_libcamera))))"
total="$((v4l+($(detect_libcamera))+($(detect_legacy))))"
if [ "${total}" -eq 0 ]; then
log_msg "ERROR: No usable Devices Found. Stopping $(basename "${0}")."
exit 1
Expand All @@ -100,6 +100,15 @@ function print_cams {
if [[ "$(detect_libcamera)" -ne 0 ]]; then
log_msg "Detected 'libcamera' device -> $(get_libcamera_path)"
fi
if [[ "$(detect_legacy)" -ne 0 ]]; then
raspicam="$(v4l2-ctl --list-devices | grep -A1 -e 'mmal' | \
awk 'NR==2 {print $1}')"
log_msg "Detected 'Raspicam' Device -> ${raspicam}"
if [[ ! "${CROWSNEST_LOG_LEVEL}" = "quiet" ]]; then
list_cam_formats "${raspicam}"
list_cam_v4l2ctrls "${raspicam}"
fi
fi
if [[ -d "/dev/v4l/by-id/" ]]; then
detect_avail_cams
fi
Expand Down
16 changes: 11 additions & 5 deletions libs/ustreamer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ run_mjpg() {
for instance in ${cams} ; do
run_ustreamer "${instance}" &
done
blockyfix
brokenfocus
return
}
Expand All @@ -45,12 +46,17 @@ run_ustreamer() {
start_param=( --host 127.0.0.1 -p "${pt}" )
fi

# Add device
start_param+=( -d "${dev}" --device-timeout=2 )
#Raspicam Workaround
if [[ "${dev}" = "$(dev_is_legacy)" ]]; then
start_param+=( -m MJPEG --device-timeout=5 --buffers=3 )
else
# Add device
start_param+=( -d "${dev}" --device-timeout=2 )

# Use MJPEG Hardware encoder if possible
if [ "$(detect_mjpeg "${cam_sec}")" = "1" ]; then
start_param+=( -m MJPEG --encoder=HW )
# Use MJPEG Hardware encoder if possible
if [ "$(detect_mjpeg "${cam_sec}")" = "1" ]; then
start_param+=( -m MJPEG --encoder=HW )
fi
fi

# set max framerate
Expand Down
27 changes: 27 additions & 0 deletions libs/v4l2_control.sh
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,30 @@ function brokenfocus {
main

}

# This function is to set bitrate on raspicams.
# If raspicams set to variable bitrate, they tend to show
# a "block-like" view after reboots
# To prevent that blockyfix should apply constant bitrate befor start of ustreamer
# See https://github.com/mainsail-crew/crowsnest/issues/33
function blockyfix {
local dev v4l2ctl

# call set_bitrate <device>
function set_bitrate {
v4l2-ctl -d "${1}" -c video_bitrate_mode=1 2> /dev/null
v4l2-ctl -d "${1}" -c video_bitrate=15000000 2> /dev/null
}

for cam in $(configured_cams); do
dev="$(get_param "cam ${cam}" device)"
v4l2ctl="$(get_param "cam ${cam}" v4l2ctl)"
if [ "${dev}" = "$(dev_is_legacy)" ]; then
if [ -z "${v4l2ctl}" ] ||
[ "$(grep -c "video_bitrate" <<< "${v4l2ctl}")" == "0" ]; then
set_bitrate "${dev}"
blockyfix_msg_1
fi
fi
done
}

0 comments on commit 66a43b6

Please sign in to comment.