From a186b4e09e78f3b0fafcef55ebf530bf2220c130 Mon Sep 17 00:00:00 2001 From: Stephan Wendel Date: Mon, 13 Dec 2021 22:08:36 +0100 Subject: [PATCH] REFACTOR: Ensure shellcheck compatibility * Modified install.sh * Added libs/ustreamer.sh * Added libs/v4l2rtspserver.sh * Modified uninstall.sh * Modified webcamd REFACTOR: Split run_rtsp to libs/v4l2rtspserver.sh * Modified webcamd accordingly REFACTOR: Split run_ustreamer to libs/ustreamer.sh * Updated webcamd accordingly Signed-off-by: Stephan Wendel --- install.sh | 45 +++++++++++++--------- libs/ustreamer.sh | 61 +++++++++++++++++++++++++++++ libs/v4l2rtspserver.sh | 45 ++++++++++++++++++++++ uninstall.sh | 17 +++++---- webcamd | 87 +++--------------------------------------- 5 files changed, 147 insertions(+), 108 deletions(-) create mode 100644 libs/ustreamer.sh create mode 100644 libs/v4l2rtspserver.sh diff --git a/install.sh b/install.sh index a7055ec..2d8e041 100755 --- a/install.sh +++ b/install.sh @@ -8,6 +8,9 @@ # Version 2 ######## +# shellcheck enable=requires-variable-braces +## disabeld SC2086 for some lines because there we want 'word splitting' + set -e ## Debug @@ -63,9 +66,9 @@ function install_cleanup_trap() { function cleanup() { # make sure that all child processed die when we die - local pids=$(jobs -pr) echo -e "Killed by user ...\r\nGoodBye ...\r" - [ -n "$pids" ] && kill $pids && sleep 5 && kill -9 $pids + # shellcheck disable=2046 + [ -n "$(jobs -pr)" ] && kill $(jobs -pr) && sleep 5 && kill -9 $(jobs -pr) } ## @@ -75,10 +78,9 @@ function err_exit { echo -e "ERROR: Stopping $(basename "$0")." echo -e "Goodbye..." fi - if [ -n "$(jobs -pr)" ]; then - kill $(jobs -pr) - fi - exit 1 + # shellcheck disable=2046 + [ -n "$(jobs -pr)" ] && kill $(jobs -pr) && sleep 5 && kill -9 $(jobs -pr) + exit 1 } ### Init ERR Trap @@ -87,7 +89,8 @@ trap 'err_exit $? $LINENO' ERR ### Import config from custompios. function import_config { if [ -f "${HOME}/crowsnest/custompios/crowsnest/config" ]; then - source ${HOME}/crowsnest/custompios/crowsnest/config + # shellcheck source-path=file_templates/custompios/crowsnest/config + source "$PWD"/custompios/crowsnest/config else echo -e "${TITLE}\n" echo -e "OOPS!\nConfiguration File missing! Exiting..." @@ -124,7 +127,7 @@ function remove_existing_webcamd { fi if [ -d "${HOME}/mjpg-streamer" ]; then echo -en "Removing 'mjpg-streamer' ...\r" - sudo rm -rf ${HOME}/mjpg-streamer > /dev/null + sudo rm -rf "${HOME}"/mjpg-streamer > /dev/null echo -e "Removing 'mjpg-streamer' ... \t[OK]\r" fi if [ -f "/etc/systemd/system/webcamd.service" ]; then @@ -135,7 +138,7 @@ function remove_existing_webcamd { if [ -f "/var/log/webcamd.log" ]; then echo -en "Removing 'webcamd.log' ...\r" sudo rm -f /var/log/webcamd.log > /dev/null - sudo rm -f ${HOME}/klipper_logs/webcamd.log > /dev/null + sudo rm -f "${HOME}"/klipper_logs/webcamd.log > /dev/null echo -e "Removing 'webcamd.log' ... \t[OK]\r" fi if [ -f "/etc/logrotate.d/webcamd" ]; then @@ -178,6 +181,7 @@ function install_crowsnest { echo -e "\nInstall webcamd Service ..." ## Install Dependencies echo -e "Installing 'crowsnest' Dependencies ..." + # shellcheck disable=2086 sudo apt install --yes --no-install-recommends $CROWSNEST_DEPS > /dev/null echo -e "Installing 'crowsnest' Dependencies ... [OK]" ## Link webcamd to $PATH @@ -188,6 +192,7 @@ function install_crowsnest { # Make sure not to overwrite existing! if [ ! -f "${CROWSNEST_DEFAULT_CONF_DIR}/webcam.conf" ]; then echo -en "Copying webcam.conf ...\r" + # shellcheck disable=2086 sudo -u "${BASE_USER}" cp -rf $PWD/sample_configs/${CROWSNEST_DEFAULT_CONF} "${CROWSNEST_DEFAULT_CONF_DIR}"/webcam.conf echo -e "Copying webcam.conf ... [OK]\r" fi @@ -214,26 +219,27 @@ function install_ustreamer { bin_path="/usr/local/bin" echo -e "\nInstalling ustreamer ..." echo -e "Installing ustreamer Dependencies ..." + # shellcheck disable=2086 sudo apt install --yes --no-install-recommends $CROWSNEST_USTREAMER_DEPS > /dev/null echo -e "Installing ustreamer Dependencies ... \t[OK]" echo -e "Cloning ustreamer Repo ..." - pushd ${HOME} > /dev/null + pushd "${HOME}" > /dev/null git clone "${CROWSNEST_USTREAMER_REPO_SHIP}" --depth=1 popd > /dev/null echo -e "Cloning ustreamer Repo ... [OK]" - pushd ${HOME}/ustreamer > /dev/null + pushd "${HOME}"/ustreamer > /dev/null echo -e "Compiling ustreamer ..." if [ "${CROWSNEST_USTREAMER_WITH_OMX}" = "y" ] && \ [ "${CROWSNEST_USTREAMER_WITH_GPIO}" = "y" ]; then echo -e "Compile ustreamer with OMX and GPIO Support..." - WITH_OMX=1 WITH_GPIO=1 make -j$(nproc) + WITH_OMX=1 WITH_GPIO=1 make -j"$(nproc)" elif [ "${CROWSNEST_USTREAMER_WITH_OMX}" = "y" ] && \ [ "${CROWSNEST_USTREAMER_WITH_GPIO}" = "n" ]; then echo -e "Compile ustreamer with OMX Support..." - WITH_OMX=1 make -j $(nproc) + WITH_OMX=1 make -j"$(nproc)" else echo -e"Compile ustreamer without OMX and GPIO Support..." - make -j $(nproc) + make -j"$(nproc)" fi popd > /dev/null echo -en "Linking ustreamer ...\r" @@ -249,16 +255,17 @@ function install_v4l2rtspserver { bin_path="/usr/local/bin" echo -e "\nInstalling v4l2rtspserver ..." echo -e "Installing v4l2rtspserver Dependencies ..." + # shellcheck disable=2086 sudo apt install --yes --no-install-recommends $CROWSNEST_V4L2RTSP_DEPS > /dev/null echo -e "Installing v4l2rtspserver Dependencies ... \t[OK]" echo -e "Cloning v4l2rtspserver Repo ..." - pushd ${HOME} > /dev/null + pushd "${HOME}" > /dev/null git clone "${CROWSNEST_V4L2RTSP_REPO_SHIP}" --depth=1 popd > /dev/null echo -e "Cloning v4l2rtspserver Repo ... [OK]" - pushd ${HOME}/v4l2rtspserver > /dev/null + pushd "${HOME}"/v4l2rtspserver > /dev/null echo -e "Compiling v4l2rtspserver ..." - cmake . && make -j $(nproc) + cmake . && make -j"$(nproc)" popd > /dev/null echo -en "Linking v4l2rtspserver ...\r" sudo ln -sf "${v4l2rtsp_bin}" "${bin_path}" > /dev/null @@ -301,7 +308,7 @@ detect_existing_webcamd ## Golang not needed for now! # enable_backports echo -e "Running apt update first ..." -sudo apt update +sudo apt update install_crowsnest install_ustreamer install_v4l2rtspserver @@ -309,4 +316,4 @@ install_v4l2rtspserver install_raspicam_fix goodbye_msg -exit 0 \ No newline at end of file +exit 0 diff --git a/libs/ustreamer.sh b/libs/ustreamer.sh new file mode 100644 index 0000000..3e5804a --- /dev/null +++ b/libs/ustreamer.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +#### ustreamer library + +#### webcamd - A webcam Service for multiple Cams and Stream Services. +#### +#### written by Stephan Wendel aka KwadFan +#### Copyright 2021 +#### https://github.com/mainsail-crew/crowsnest +#### +#### This File is distributed under GPLv3 +#### + +# Exit upon Errors +set -e + + +function run_ustreamer { + local cam_section ustreamer_bin device port resolution fps custom + local raspicam start_param wwwroot + cam_section="${1}" + ustreamer_bin="$(whereis ustreamer | awk '{print $2}')" + device="$(get_param "cam ${cam_section}" device)" + port=$(get_param "cam ${cam_section}" port) + resolution=$(get_param "cam ${cam_section}" resolution) + fps=$(get_param "cam ${cam_section}" max_fps) + custom="$(get_param "cam ${cam_section}" custom_flags 2> /dev/null)" + raspicam="$(v4l2-ctl --list-devices | grep -A1 -e 'mmal' | \ + awk 'NR==2 {print $1}')" + check_section "${cam_section}" + wwwroot="$(dirname $(readlink -qe $(whereis webcamd)))/ustreamer-www" + #Raspicam Workaround + if [ "${device}" == "${raspicam}" ]; then + start_param=( + --host 127.0.0.1 -p "${port}" -m MJPEG --device-timeout=5 + --buffers=3 -r "${resolution}" -f "${fps}" --allow-origin=\* + --static "${wwwroot}" + ) + else + start_param=( + -d "${device}" -r "${resolution}" -f "${fps}" + --host 127.0.0.1 -p "${port}" --allow-origin=\* + --device-timeout=2 --static "${wwwroot}" + ) + fi + # Custom Flag Handling + if [ -n "${custom}" ]; then + start_param=(${start_param[@]} "${custom}" ) + fi + log_msg "Starting ustreamer with Device ${device} ..." + echo "Parameters: ${start_param[*]}" | \ + log_output "ustreamer [cam ${cam_section}]" + # Ustreamer is designed to run even if the device is not ready or readable. + # I dont like that! ustreamer has to exit if Cam isnt there. + if [ -e "${device}" ]; then + "${ustreamer_bin}" ${start_param[*]} 2>&1 | \ + log_output "ustreamer [cam ${cam_section}]" + else + log_msg "ERROR: Start of ustreamer [cam ${cam_section}] failed!" + fi +} diff --git a/libs/v4l2rtspserver.sh b/libs/v4l2rtspserver.sh new file mode 100644 index 0000000..4e6fd9c --- /dev/null +++ b/libs/v4l2rtspserver.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +#### ustreamer library + +#### webcamd - A webcam Service for multiple Cams and Stream Services. +#### +#### written by Stephan Wendel aka KwadFan +#### Copyright 2021 +#### https://github.com/mainsail-crew/crowsnest +#### +#### This File is distributed under GPLv3 +#### + +# Exit upon Errors +set -e + +function run_rtsp { + local cam_section rtsp_bin device port resolution fps custom + local raspicam start_param + cam_section="${1}" + rtsp_bin="$(whereis v4l2rtspserver | awk '{print $2}')" + device="$(get_param "cam ${cam_section}" device)" + port=$(get_param "cam ${cam_section}" port) + resolution=$(get_param "cam ${cam_section}" resolution) + fps=$(get_param "cam ${cam_section}" max_fps) + custom="$(get_param "cam ${cam_section}" custom_flags 2> /dev/null)" + check_section "${cam_section}" + split_res="$(echo "${resolution}" | \ + awk -F 'x' '{print "-W "$1 " -H "$2}')" + start_param=( + -I 0.0.0.0 -P "${port}" "${split_res}" -F "${fps}" \ + "${device}" + ) + # Custom Flag Handling + if [ -n "${custom}" ]; then + start_param=(${start_param[@]} "${custom}" ) + fi + log_msg "Starting v4l2rtspserver with Device ${device} ..." + echo "Parameters: ${start_param[*]}" | \ + log_output "v4l2rtspserver [cam ${cam_section}]" + + "${rtsp_bin}" ${start_param[*]} 2>&1 | \ + log_output "v4l2rtspserver [cam ${cam_section}]" + log_msg "ERROR: Start of v4l2rtspserver [cam ${cam_section}] failed!" +} diff --git a/uninstall.sh b/uninstall.sh index c4968c7..dbb684f 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -8,13 +8,15 @@ # Version 1.1 ######## +# shellcheck enable=requires-variable-braces + +## Exit on Error set -e ## Debug # set -x # Global Vars -BASE_USER=$(whoami) TITLE="crowsnest - A Webcam Daemon for Raspberry Pi OS" ### Functions @@ -45,9 +47,9 @@ function install_cleanup_trap() { function cleanup() { # make sure that all child processed die when we die - local pids=$(jobs -pr) echo -e "Killed by user ...\r\nGoodBye ...\r" - [ -n "$pids" ] && kill $pids && sleep 5 && kill -9 $pids + # shellcheck disable=2046 + [ -n "$(jobs -pr)" ] && kill $(jobs -pr) && sleep 5 && kill -9 $(jobs -pr) } ## @@ -57,10 +59,9 @@ function err_exit { echo -e "ERROR: Stopping $(basename "$0")." echo -e "Goodbye..." fi - if [ -n "$(jobs -pr)" ]; then - kill $(jobs -pr) - fi - exit 1 + # shellcheck disable=2046 + [ -n "$(jobs -pr)" ] && kill $(jobs -pr) && sleep 5 && kill -9 $(jobs -pr) + exit 1 } ### Init ERR Trap @@ -162,4 +163,4 @@ install_cleanup_trap welcome_msg ask_uninstall -exit 0 \ No newline at end of file +exit 0 diff --git a/webcamd b/webcamd index 22a5c67..fd75209 100755 --- a/webcamd +++ b/webcamd @@ -12,9 +12,6 @@ #### #### Version 3 -### Disable shellcheck Errors -# shellcheck disable=1091 - # Exit upon Errors set -e @@ -22,89 +19,17 @@ set -e BASE_CN_PATH="$(dirname "$(readlink -f "${0}")")" ## Import Librarys -source "${BASE_CN_PATH}/libs/core.sh" +# shellcheck source-path=SCRIPTDIR/libs/ source "${BASE_CN_PATH}/libs/configparser.sh" +source "${BASE_CN_PATH}/libs/core.sh" source "${BASE_CN_PATH}/libs/hwhandler.sh" +source "${BASE_CN_PATH}/libs/init_stream.sh" source "${BASE_CN_PATH}/libs/logging.sh" source "${BASE_CN_PATH}/libs/messages.sh" -source "${BASE_CN_PATH}/libs/watchdog.sh" -source "${BASE_CN_PATH}/libs/init_stream.sh" +source "${BASE_CN_PATH}/libs/v4l2rtspserver.sh" +source "${BASE_CN_PATH}/libs/ustreamer.sh" source "${BASE_CN_PATH}/libs/v4l2_control.sh" - -function run_ustreamer { - local cam_section ustreamer_bin device port resolution fps custom - local raspicam start_param wwwroot - cam_section="${1}" - ustreamer_bin="$(whereis ustreamer | awk '{print $2}')" - device="$(get_param "cam ${cam_section}" device)" - port=$(get_param "cam ${cam_section}" port) - resolution=$(get_param "cam ${cam_section}" resolution) - fps=$(get_param "cam ${cam_section}" max_fps) - custom="$(get_param "cam ${cam_section}" custom_flags 2> /dev/null)" - raspicam="$(v4l2-ctl --list-devices | grep -A1 -e 'mmal' | \ - awk 'NR==2 {print $1}')" - check_section "${cam_section}" - wwwroot="$(dirname $(readlink -qe $(whereis webcamd)))/ustreamer-www" - #Raspicam Workaround - if [ "${device}" == "${raspicam}" ]; then - start_param=( - --host 127.0.0.1 -p "${port}" -m MJPEG --device-timeout=5 - --buffers=3 -r "${resolution}" -f "${fps}" --allow-origin=\* - --static "${wwwroot}" - ) - else - start_param=( - -d "${device}" -r "${resolution}" -f "${fps}" - --host 127.0.0.1 -p "${port}" --allow-origin=\* - --device-timeout=2 --static "${wwwroot}" - ) - fi - # Custom Flag Handling - if [ -n "${custom}" ]; then - start_param=(${start_param[@]} "${custom}" ) - fi - log_msg "Starting ustreamer with Device ${device} ..." - echo "Parameters: ${start_param[*]}" | \ - log_output "ustreamer [cam ${cam_section}]" - # Ustreamer is designed to run even if the device is not ready or readable. - # I dont like that! ustreamer has to exit if Cam isnt there. - if [ -e "${device}" ]; then - "${ustreamer_bin}" ${start_param[*]} 2>&1 | \ - log_output "ustreamer [cam ${cam_section}]" - else - log_msg "ERROR: Start of ustreamer [cam ${cam_section}] failed!" - fi -} - -function run_rtsp { - local cam_section rtsp_bin device port resolution fps custom - local raspicam start_param - cam_section="${1}" - rtsp_bin="$(whereis v4l2rtspserver | awk '{print $2}')" - device="$(get_param "cam ${cam_section}" device)" - port=$(get_param "cam ${cam_section}" port) - resolution=$(get_param "cam ${cam_section}" resolution) - fps=$(get_param "cam ${cam_section}" max_fps) - custom="$(get_param "cam ${cam_section}" custom_flags 2> /dev/null)" - check_section "${cam_section}" - split_res="$(echo "${resolution}" | \ - awk -F 'x' '{print "-W "$1 " -H "$2}')" - start_param=( - -I 0.0.0.0 -P "${port}" "${split_res}" -F "${fps}" \ - "${device}" - ) - # Custom Flag Handling - if [ -n "${custom}" ]; then - start_param=(${start_param[@]} "${custom}" ) - fi - log_msg "Starting v4l2rtspserver with Device ${device} ..." - echo "Parameters: ${start_param[*]}" | \ - log_output "v4l2rtspserver [cam ${cam_section}]" - - "${rtsp_bin}" ${start_param[*]} 2>&1 | \ - log_output "v4l2rtspserver [cam ${cam_section}]" - log_msg "ERROR: Start of v4l2rtspserver [cam ${cam_section}] failed!" -} +source "${BASE_CN_PATH}/libs/watchdog.sh" #### MAIN ## Args given?