Check if v4l2 device has video inputs

This commit is contained in:
Mathias Koehler 2023-04-29 19:27:05 +02:00
parent 9f81008a74
commit 09a2404fc3
Signed by: interru
GPG key ID: E01E350F5E2A884A

View file

@ -5,6 +5,11 @@ if ! command -v inotifywait >/dev/null 2>&1; then
exit 1
fi
if ! command -v v4l2-ctl >/dev/null 2>&1; then
echo "Please install v4l-utils"
exit 1
fi
declare -A STREAMS
failed() {
@ -28,13 +33,21 @@ streaming() {
) || failed "$1"
}
check_initial_devices() {
for file in /dev/video*; do
file="${file##*/}"
echo "Found $file"
check() {
file=$1
echo "Found $file"
if v4l2-ctl -n -d "/dev/$file" | grep -q 'Input'; then
(streaming "$file") &
STREAMS[$file]=$!
echo "$file PID ${STREAMS[$file]}"
fi
}
check_initial_devices() {
for file in /dev/video*; do
file="${file##*/}"
check "$file"
done
}
@ -42,16 +55,13 @@ listen_for_new_devices() {
inotifywait -m /dev --format "%f" -e create |
while read -r file; do
if [[ "$file" =~ video[0-9]+$ ]]; then
echo "Found $file"
if ! ps -p "${STREAMS[$file]}"> /dev/null; then
unset "STREAMS[$file]"
fi
if [[ ! -v "STREAMS[$file]" ]]; then
(streaming "$file") &
STREAMS[$file]=$!
echo "$file PID ${STREAMS[$file]}"
check "$file"
fi
fi
done