Raspberry PI IP webcam monitor

You can monitor a IP webcam with a RPI to a composite or HDMI screen pretty straight forwardly. This lets you decode a mjpeg stream which is a pretty common standard used by these IP cameras. The frame rate isn’t great for my setup but there are plenty of variables that might be causing that.

I setup the pi with some GPU fixes and the code which lets it dynamically manage it’s GPU ram. This appears to work pretty well. It also means I’ve plenty of ram for the other services. This stuff goes at the end of /boot/config.txt YMMV.

gpu_mem_256=112
gpu_mem_512=368
cma_lwm=16
cma_hwm=32
cma_offline_start=16

start_file=start_x.elf
fixup_file=fixup_x.elf

The following script is based on a number of sources. This is specifically designed to be robust for GPU/network/stream issues. OMX player can be a bit sensitive to poorly formed mjpeg streams.

while [ 1 ]
do
 tvservice -c 'PAL 4:3'
 echo deleting
 rm /tmp/stream.mjpeg -rf
 echo making
 mkfifo /tmp/stream.mjpeg
 echo getting
 timeout 14400 wget -c --timeout=1 -t 1 --http-user=xxxx --http-password=xxxx -O /tmp/stream.mjpeg 'http://xxx.xxx.xxx.xxx/videostream.cgi' &
 echo sleep
 echo startomx
 screen -dmS camera1 sh -c 'omxplayer --live --threshold 0 -r /tmp/stream.mjpeg; exec bash'
 wait 
 screen -X -S camera1 kill
 echo deleting
 rm /tmp/stream.mjpeg -rf
 echo finito
done
  • First the script ensures the composite output is live using the tvservice command (this is important for composite as the pi often shows a blank screen and needs it resetting), this would need adjusting or omitting for an HDMI screen.
  • Then it clears the existing fifo (if there is one) and creates a new one. Then it uses wget to connect the fifo to the camera stream. The timeout command automaticaly kills the wget command after 4 hours, this clears freezing problems I had at the expense of a couple of seconds while it restarts. You would need to change the URL so it works for your camera, this is for a foscam.
  • Then omxplayer is started in a screen session, this is to give it a live console (which it likes) and allows you to kill it later when wget has failed/been killed.
  • The wait command waits for wget to finish.
  • When wget is finished it kills the omxplayer session, deletes the fifo and starts again. It will repeat indefinitely.

Hopefully this helps someone out. It’s been a bit of a faff fixing all the little bugs with this. This script does appear to be fairly robust now though and it is a light enough method to coexist with motion for at least one camera. I have this script start up with the pi with no X window environment.

Leave a Reply