CreatBotCrowsnest/libs/v4l2_control.sh
Stephan Wendel f6e2156ab3
Merge develop branch (#163)
* Fix makefile flags on low memory devices (#124)

* Fix `makefile` flags on low memory devices

On devices with less than 512MB existing devices will use `-j2` due to wrong condition.

* docs: Update index with correct heading and link to backends (#131)

This is a follow-up to #108 and fixes heading and hyperlink to the list of backends

* Typo in `core.sh` (#138)

* Fix makefile flags on low memory devices (#124) (#125)

* Fix `makefile` flags on low memory devices

On devices with less than 512MB existing devices will use `-j2` due to wrong condition.

Co-authored-by: Kamil Trzciński <ayufan@ayufan.eu>

* Typo in `core.sh`

'Dependencys' --> dependencies

---------

Co-authored-by: Stephan Wendel <43513802+KwadFan@users.noreply.github.com>
Co-authored-by: Kamil Trzciński <ayufan@ayufan.eu>

* Fix makefile flags on low memory devices (#124) (#125) (#144)

* Fix `makefile` flags on low memory devices

On devices with less than 512MB existing devices will use `-j2` due to wrong condition.

Co-authored-by: Kamil Trzciński <ayufan@ayufan.eu>

* docs(messages.sh): fix typos, improve spelling (#145)

* feat: add legacy cam support (#146)

* 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>

* Fix makefile flags on low memory devices (#124) (#125) (#147)

* Fix `makefile` flags on low memory devices

On devices with less than 512MB existing devices will use `-j2` due to wrong condition.

Co-authored-by: Kamil Trzciński <ayufan@ayufan.eu>

* fix: fix make config empty path (#148)

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

* fix: fix default_path_msg function name (#149)

* fix: fix default_path_msg function name

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

* fix: fix message

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

---------

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

* fix: fix libcamera-apps-lite not getting updated (#160)

libcamera-apps-lite is needed for libcamera-hello.
Crowsnest uses libcamera-hello to detected raspicams

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

* fix: fix Shellcheck test error (#153)

* fix: fix Shellcheck test error

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

* style: remove empty condition

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

---------

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

* fix: fix CustomPiOS docker build error (#158)

This should skip any check of `SUDO_USER`

Tested with Win & Linux host with Docker 

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

---------

Signed-off-by: Patrick Gehrsitz <58853838+mryel00@users.noreply.github.com>
Co-authored-by: Kamil Trzciński <ayufan@ayufan.eu>
Co-authored-by: DeviousFusion <dj3tusk@gmail.com>
Co-authored-by: Sergei <67871383+slepiavka@users.noreply.github.com>
Co-authored-by: Patrick Gehrsitz <58853838+mryel00@users.noreply.github.com>
2023-08-11 18:04:33 +02:00

166 lines
5.4 KiB
Bash
Executable File

#!/bin/bash
#### v4l2 control library
#### crowsnest - A webcam Service for multiple Cams and Stream Services.
####
#### Written by Stephan Wendel aka KwadFan <me@stephanwe.de>
#### Copyright 2021
#### https://github.com/mainsail-crew/crowsnest
####
#### This File is distributed under GPLv3
####
#### Description: Configure Cam with v4l2-ctl options
#### ex.: v4l2-ctl -c brightness=100
# shellcheck enable=require-variable-braces
# Exit upon Errors
set -Ee
function v4l2_control {
v4c_log_msg() {
log_msg "V4L2 Control: ${1}"
}
function main {
local device v4l2ctl valueless opt_avail
for cam in $(configured_cams); do
# get device from cam section
device="$(get_param "cam ${cam}" device)"
# get v4l2ctl parameters
v4l2ctl="$(get_param "cam ${cam}" v4l2ctl)"
# if not empty do
if [[ -n "${v4l2ctl}" ]]; then
# Write configured options to Log
v4c_log_msg "Device: [cam ${cam}]"
v4c_log_msg "Options: ${v4l2ctl}"
# Split options to array
IFS="," read -ra opt < <(echo "${v4l2ctl}" | tr -d " "); unset IFS
# loop through options
for param in "${opt[@]}"; do
# parameter available for device
# needs || true to prevent script to exit
valueless="$(echo "${param}" | cut -d "=" -f1)"
opt_avail="$(v4l2-ctl -d "${device}" -L | \
grep -c "${valueless}" || true)"
if [[ "${opt_avail}" -eq "0" ]]; then
v4c_log_msg "Parameter '${param}' not available for '${device}'. Skipped."
else
v4l2-ctl -d "${device}" -c "${param}" 2> /dev/null ||
v4c_log_msg "Failed to set parameter: '${param}' ..."
fi
done
if [[ "${CROWSNEST_LOG_LEVEL}" == "debug" ]]; then
v4l2-ctl -d "${device}" -L | log_output "v4l2ctl"
fi
else
v4c_log_msg "No parameters set for [cam ${cam}]. Skipped."
fi
done
}
### MAIN
main
}
function brokenfocus {
# checks if "focus_absolute" is configured
# call if_focus_absolute <mycamnameornumber>
# returns 1 = true, 0 = false
function if_focus_absolute {
local cam
cam="${1}"
get_param "cam ${cam}" v4l2ctl | grep -c "focus_absolute"
}
# checks if device has "focus_absolute"
# call has_focus_absolute <device>
# returns 1 if true, 0 if false
function has_focus_absolute {
v4l2-ctl -d "${1}" -C "focus_absolute" &> /dev/null \
&& echo "1" || echo "0"
}
# call get_conf_value <mycamnameornumber>
# spits out value from config file
function get_conf_value {
local cam conf_val
local -a params
cam="${1}"
conf_val="$(get_param "cam ${cam}" v4l2ctl)"
if [ -n "${conf_val}" ]; then
IFS=','; read -ra params <<< "${conf_val}"
unset IFS
for i in "${params[@]}"; do
grep "focus_absolute" <<< "${i}" || true
done
fi
}
# call get_current_value <device>
# ex.: get_current_value /dev/video0
# spits out focus_absolute=20 ( if set to 20 )
function get_current_value {
v4l2-ctl -d "${1}" -C "focus_absolute" 2> /dev/null | sed 's/:[[:space:]]/=/'
}
# call set_current_value <device> <value>
# ex.: set_current_value /dev/video0 focus_absolute=30
function set_focus_absolute {
local device value
device="${1}"
value="${2}"
v4l2-ctl -d "${device}" -c "${value}"
}
function main {
local cur_val conf_val device
for cam in $(configured_cams); do
device="$(get_param "cam ${cam}" device)"
cur_val="$(get_current_value "${device}")"
conf_val="$(get_conf_value "${cam}")"
if [ "$(has_focus_absolute "${device}")" == "1" ] &&
[ "$(if_focus_absolute "${cam}")" == "1" ] &&
[ "${cur_val}" != "${conf_val}" ]; then
detected_broken_dev_msg
set_focus_absolute "${device}" "${conf_val}"
fi
if [[ "${CROWSNEST_LOG_LEVEL}" == "debug" ]] && [[ -n "${cur_val}" ]]; then
debug_focus_val_msg "$(get_current_value "${device}")"
fi
done
}
### MAIN
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
}