diff options
author | Guy Sheffer <guysoft@gmail.com> | 2018-02-27 12:25:20 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-27 12:25:20 +0300 |
commit | 32a51fc9fc9e653fcf0e4869ae688576b6120c9f (patch) | |
tree | 99ea2f90b580086e8d251713f62d1c5456f27ddb | |
parent | 784e9734b4db24ee8ce8b96db7d892a633d4e3b3 (diff) | |
parent | a55b3a1c7a521490a48f605ec1e7ed8a17c6ed72 (diff) |
Merge pull request #492 from bionik/usbwebcamfixes
Support for other webcams than /dev/video0, fixed a regular expression
-rwxr-xr-x | src/modules/octopi/filesystem/home/root/bin/webcamd | 70 |
1 files changed, 60 insertions, 10 deletions
diff --git a/src/modules/octopi/filesystem/home/root/bin/webcamd b/src/modules/octopi/filesystem/home/root/bin/webcamd index 00f078f..b4fc612 100755 --- a/src/modules/octopi/filesystem/home/root/bin/webcamd +++ b/src/modules/octopi/filesystem/home/root/bin/webcamd @@ -29,6 +29,14 @@ fi brokenfps_usb_devices=("046d:082b" "1908:2310" "0458:708c" "${additional_brokenfps_usb_devices[@]}") +# check if array contains a string +function containsString() { + local e match="$1" + shift + for e; do [[ "$e" == "$match" ]] && return 0; done + return 1 +} + # cleans up when the script receives a SIGINT or SIGTERM function cleanup() { # make sure that all child processed die when we die @@ -63,16 +71,18 @@ function startRaspi { # starts up the USB webcam function startUsb { - options="$camera_usb_options" + options="$usb_options" device="video0" - extracted_device=`echo $options | sed 's@.*-d /dev/\(video[0-9]+\).*@\1@'` - if [ "$extracted_device" != "$options" ] - then - # the camera options refer to another device, use that for determining product - device=$extracted_device + # check for parameter and set the device if it is given as a parameter + input=$1 + if [[ -n $input ]]; then + device=`basename "$input"` fi + # add video device into options + options="$options -d /dev/$device" + uevent_file="/sys/class/video4linux/$device/device/uevent" if [ -e $uevent_file ]; then # let's see what kind of webcam we have here, fetch vid and pid... @@ -118,21 +128,61 @@ echo "" # I have no idea why, but that's how it is... vcgencmd version > /dev/null 2>&1 +usb_options="$camera_usb_options" + +# if webcam device is explicitly given in /boot/octopi.txt, save the path of the device +# to a variable and remove its parameter from usb_options +extracted_device=`echo $usb_options | sed 's@.*-d \(/dev/video[0-9]\+\).*@\1@'` +if [ "$extracted_device" != "$usb_options" ] +then + # the camera options refer to a device, save it in a variable + usb_device_path="$extracted_device" + # replace video device parameter with empty string and strip extra whitespace + usb_options=`echo $usb_options | sed 's/\-d \/dev\/video[0-9]\+//g' | awk '$1=$1'` + echo "Explicitly set USB device was found in options: $usb_device_path" +fi + # keep mjpg streamer running if some camera is attached while true; do - # get number of usb video devices - video_device_count=$(ls /dev/video? 2> /dev/null | wc -l) - if [ "$video_device_count" != "0" ] && { [ "$camera" = "auto" ] || [ "$camera" = "usb" ] ; }; then - startUsb + + # get list of usb video devices into an array + video_devices=($(find /dev -regextype sed -regex '\/dev/video[0-9]\+' | sort 2> /dev/null)) + + if [ ${#video_devices[@]} != 0 ] && { [ "$camera" = "auto" ] || [ "$camera" = "usb" ] ; }; then + + echo "Found video devices:" + printf '%s\n' "${video_devices[@]}" + + if [[ $usb_device_path ]]; then + # usb device is explicitly set in options + if containsString "$usb_device_path" "${video_devices[@]}"; then + # explicitly set usb device was found in video_devices array, start usb with the found device + echo "USB device was set in options and found in devices, start MJPG-streamer with the configured USB video device: $usb_device_path" + startUsb "$usb_device_path" + else + # explicitly set usb device was not found + echo "Configured USB camera was not detected, trying again in two minutes" + sleep 120 & + wait + fi + else + # device is not set explicitly in options, start usb with first found usb camera as the device + echo "USB device was not set in options, start MJPG-streamer with the first found video device: ${video_devices[0]}" + startUsb "${video_devices[0]}" + fi + sleep 30 & wait + elif [ "`vcgencmd get_camera`" = "supported=1 detected=1" ] && { [ "$camera" = "auto" ] || [ "$camera" = "raspi" ] ; }; then startRaspi sleep 30 & wait + else echo "No camera detected, trying again in two minutes" sleep 120 & wait + fi done |