From 3e6ed806971540558ae6b55cc247c0ee447da915 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz <58853838+mryel00@users.noreply.github.com> Date: Sat, 22 Jul 2023 13:01:56 +0200 Subject: [PATCH 1/3] feat: add legacy cam support Signed-off-by: Patrick Gehrsitz <58853838+mryel00@users.noreply.github.com> --- libs/hwhandler.sh | 12 ++++++++++++ libs/logging.sh | 11 ++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/libs/hwhandler.sh b/libs/hwhandler.sh index 9bf4f3b3..a7e2260e 100755 --- a/libs/hwhandler.sh +++ b/libs/hwhandler.sh @@ -75,6 +75,18 @@ detect_libcamera() { 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}" +} + ## Spit /base/soc path for libcamera device get_libcamera_path() { if [[ "$(is_raspberry_pi)" = "1" ]] && diff --git a/libs/logging.sh b/libs/logging.sh index 43a38cec..828cd7c6 100755 --- a/libs/logging.sh +++ b/libs/logging.sh @@ -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 @@ -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 From 225acd849da79514f6f9f735823363d96bbcd5e3 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz <58853838+mryel00@users.noreply.github.com> Date: Sat, 22 Jul 2023 14:35:31 +0200 Subject: [PATCH 2/3] feat: add blockyfix Signed-off-by: Patrick Gehrsitz <58853838+mryel00@users.noreply.github.com> --- libs/ustreamer.sh | 1 + libs/v4l2_control.sh | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/libs/ustreamer.sh b/libs/ustreamer.sh index 000d38d3..0a48313d 100755 --- a/libs/ustreamer.sh +++ b/libs/ustreamer.sh @@ -23,6 +23,7 @@ run_mjpg() { for instance in ${cams} ; do run_ustreamer "${instance}" & done + blockyfix brokenfocus return } diff --git a/libs/v4l2_control.sh b/libs/v4l2_control.sh index 15ddecb8..139dae33 100755 --- a/libs/v4l2_control.sh +++ b/libs/v4l2_control.sh @@ -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 + 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_raspicam)" ]; then + if [ -z "${v4l2ctl}" ] || + [ "$(grep -c "video_bitrate" <<< "${v4l2ctl}")" == "0" ]; then + set_bitrate "${dev}" + blockyfix_msg_1 + fi + fi + done +} From d48f3dfab81b3554ddbee7b9e518f93a1edfb028 Mon Sep 17 00:00:00 2001 From: Patrick Gehrsitz <58853838+mryel00@users.noreply.github.com> Date: Sat, 22 Jul 2023 15:29:00 +0200 Subject: [PATCH 3/3] fix: add ustreamer legacy cam workaround Signed-off-by: Patrick Gehrsitz <58853838+mryel00@users.noreply.github.com> --- libs/hwhandler.sh | 19 ++++++++++++------- libs/ustreamer.sh | 15 ++++++++++----- libs/v4l2_control.sh | 2 +- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/libs/hwhandler.sh b/libs/hwhandler.sh index a7e2260e..16026d2b 100755 --- a/libs/hwhandler.sh +++ b/libs/hwhandler.sh @@ -75,6 +75,15 @@ detect_libcamera() { fi } +## Spit /base/soc path for libcamera device +get_libcamera_path() { + if [[ "$(is_raspberry_pi)" = "1" ]] && + [[ -x "$(command -v libcamera-hello)" ]]; then + libcamera-hello --list-cameras | sed '1,2d' \ + | grep "\(/base/*\)" | cut -d"(" -f2 | tr -d '$)' + fi +} + # Determine connected "legacy" device function detect_legacy { local avail @@ -87,13 +96,9 @@ function detect_legacy { echo "${avail}" } -## Spit /base/soc path for libcamera device -get_libcamera_path() { - if [[ "$(is_raspberry_pi)" = "1" ]] && - [[ -x "$(command -v libcamera-hello)" ]]; then - libcamera-hello --list-cameras | sed '1,2d' \ - | grep "\(/base/*\)" | cut -d"(" -f2 | tr -d '$)' - fi +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 diff --git a/libs/ustreamer.sh b/libs/ustreamer.sh index 0a48313d..3f588eec 100755 --- a/libs/ustreamer.sh +++ b/libs/ustreamer.sh @@ -46,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 diff --git a/libs/v4l2_control.sh b/libs/v4l2_control.sh index 139dae33..d7630219 100755 --- a/libs/v4l2_control.sh +++ b/libs/v4l2_control.sh @@ -154,7 +154,7 @@ function blockyfix { for cam in $(configured_cams); do dev="$(get_param "cam ${cam}" device)" v4l2ctl="$(get_param "cam ${cam}" v4l2ctl)" - if [ "${dev}" = "$(dev_is_raspicam)" ]; then + if [ "${dev}" = "$(dev_is_legacy)" ]; then if [ -z "${v4l2ctl}" ] || [ "$(grep -c "video_bitrate" <<< "${v4l2ctl}")" == "0" ]; then set_bitrate "${dev}"