Advertisement
FoinB

/root/bin/webcamd (C270)

Jan 22nd, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ########################################################################
  4. ### DO NOT EDIT THIS FILE TO CHANGE THE CONFIG!!! ###
  5. ### ---------------------------------------------------------------- ###
  6. ### There is no need to edit this file for changing resolution, ###
  7. ### frame rates or any other mjpg-streamer parameters. Please edit ###
  8. ### /boot/octopi.txt instead - that's what it's there for! You can ###
  9. ### even do this with your Pi powered down by directly accessing the ###
  10. ### file when using the SD card as thumb drive in your regular ###
  11. ### computer. ###
  12. ########################################################################
  13.  
  14. MJPGSTREAMER_HOME=/home/pi/mjpg-streamer
  15. MJPGSTREAMER_INPUT_USB="input_uvc.so"
  16. MJPGSTREAMER_INPUT_RASPICAM="input_raspicam.so"
  17.  
  18. # init configuration - DO NOT EDIT, USE /boot/octopi.txt INSTEAD!
  19. camera="auto"
  20. camera_usb_options="-r 1280x720 -f 10"
  21. camera_raspi_options="-fps 10"
  22. camera_http_webroot="./www-octopi"
  23. camera_http_options="-n"
  24. additional_brokenfps_usb_devices=()
  25.  
  26. if [ -e "/boot/octopi.txt" ]; then
  27. source "/boot/octopi.txt"
  28. fi
  29.  
  30. brokenfps_usb_devices=("046d:082b" "${additional_brokenfps_usb_devices[@]}")
  31.  
  32. # cleans up when the script receives a SIGINT or SIGTERM
  33. function cleanup() {
  34. # make sure that all child processed die when we die
  35. local pids=$(jobs -pr)
  36. [ -n "$pids" ] && kill $pids
  37. exit 0
  38. }
  39.  
  40. # says goodbye when the script shuts down
  41. function goodbye() {
  42. # say goodbye
  43. echo ""
  44. echo "Goodbye..."
  45. echo ""
  46. }
  47.  
  48. # runs MJPG Streamer, using the provided input plugin + configuration
  49. function runMjpgStreamer {
  50. input=$1
  51. pushd $MJPGSTREAMER_HOME > /dev/null 2>&1
  52. echo Running ./mjpg_streamer -o "output_http.so -w $camera_http_webroot$
  53. LD_LIBRARY_PATH=. ./mjpg_streamer -o "output_http.so -w $camera_http_we$
  54. wait
  55. popd > /dev/null 2>&1
  56. }
  57.  
  58. # starts up the RasPiCam
  59. function startRaspi {
  60. logger -s "Starting Raspberry Pi camera"
  61. runMjpgStreamer "$MJPGSTREAMER_INPUT_RASPICAM $camera_raspi_options"
  62. }
  63.  
  64. # starts up the USB webcam
  65. function startUsb {
  66. options="$camera_usb_options"
  67. device="video0"
  68.  
  69. extracted_device=`echo $options | sed 's@.*-d /dev/\(video[0-9]+\).*@\1@'`
  70. if [ "$extracted_device" != "$options" ]
  71. then
  72. # the camera options refer to another device, use that for determining $
  73. device=$extracted_device
  74. fi
  75.  
  76. uevent_file="/sys/class/video4linux/$device/device/uevent"
  77. if [ -e $uevent_file ]; then
  78. # let's see what kind of webcam we have here, fetch vid and pid...
  79. product=`cat $uevent_file | grep PRODUCT | cut -d"=" -f2`
  80. vid=`echo $product | cut -d"/" -f1`
  81. pid=`echo $product | cut -d"/" -f2`
  82. vidpid=`printf "%04x:%04x" "0x$vid" "0x$pid"`
  83.  
  84. # ... then look if it is in our list of known broken-fps-devices and if$
  85. # the -f parameter from the options (if it's in there, else that's just$
  86. for identifier in ${brokenfps_usb_devices[@]};
  87. do
  88. if [ "$vidpid" = "$identifier" ]; then
  89. echo
  90. echo "Camera model $vidpid is known to not work with -f paramet$
  91. echo
  92. options=`echo $options | sed -e "s/\(\s\+\|^\)-f\s\+[0-9]\+//g"`
  93. fi
  94. done
  95. fi
  96.  
  97. logger -s "Starting USB webcam"
  98. runMjpgStreamer "$MJPGSTREAMER_INPUT_USB $options"
  99. }
  100.  
  101. # make sure our cleanup function gets called when we receive SIGINT, SIGTERM
  102. trap "cleanup" SIGINT SIGTERM
  103. # say goodbye when we EXIT
  104. trap "goodbye" EXIT
  105.  
  106. # echo configuration
  107. echo "Starting up webcamDaemon..."
  108. echo ""
  109. echo "--- Configuration: ----------------------------"
  110. echo "camera: $camera"
  111. echo "usb options: $camera_usb_options"
  112. echo "raspi options: $camera_raspi_options"
  113. echo "http options: -w $camera_http_webroot $camera_http_options"
  114. echo "-----------------------------------------------"
  115. echo ""
  116.  
  117. # we need this to prevent the later calls to vcgencmd from blocking
  118. # I have no idea why, but that's how it is...
  119. vcgencmd version > /dev/null 2>&1
  120.  
  121. # keep mjpg streamer running if some camera is attached
  122. while true; do
  123. if [ -e "/dev/video0" ] && { [ "$camera" = "auto" ] || [ "$camera" = "usb" $
  124. startUsb
  125. sleep 30 &
  126. wait
  127. elif [ "`vcgencmd get_camera`" = "supported=1 detected=1" ] && { [ "$camera$
  128. startRaspi
  129. sleep 30 &
  130. wait
  131. else
  132. echo "No camera detected, trying again in two minutes"
  133. sleep 120 &
  134. wait
  135. fi
  136. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement