Saturday, June 28, 2014

Streaming USB webcam on Raspberry PI

I had a USB webcam lying around and a Raspberry Pi. And, I wanted to stream it over my local WiFi network. To do this, I picked up MJPEG-Streamer. Connecting the webcam and issuing 'lsmod' showed this:
$ lsmod
.
.
Bus 001 Device 004: ID 0ac8:3420 Z-Star Microelectronics Corp. Venus USB2.0 Camera
The device is supported by UVC driver.
ls /dev/vid*
/dev/video0
So, we have '/dev/video0' as our webcam.
Let's install some required packages.
On a RPi, I have faced multiple issues trying to install packages without first upgrading the system to the latest state. So, let's upgrade the system first.
$ sudo apt-get update && sudo apt-get upgrade
And, some requirements for MJPEG-Streamer
$ sudo apt-get install subversion imagemagick libjpeg8-dev
Now, let's download the latest source
$ svn co https://svn.code.sf.net/p/mjpg-streamer/code/mjpg-streamer/ mjpg-streamer
$ make
When it finishes, it leaves "mjpeg-streamer" binary to the folder. This may be added to path variable. Now, let's stream. 15fps with hardware MJPEG Compression
$ ./mjpg_streamer -i "./input_uvc.so -d /dev/video0 -f 15 " -o "./output_http.so -w ./www"
If you get the error like this:
The input device does not supports MJPEG mode
You may also try the YUV mode (-yuv option), but it requires a much more CPU power
 Init v4L2 failed !! exit fatal 
 i: init_VideoIn failed
Then we need to add '-y' argument to fallback to software mode.
$ ./mjpg_streamer -i "./input_uvc.so -d /dev/video0 -y -f 15" -o "./output_http.so -w ./www"
You can access the web interface at http://<ip>:8080/ where 'ip' is the IP of the Raspberry Pi server. The frame rate can also be changed by specifying the custom value instead of 15. To stream at different resolution with 30fps:
$ ./mjpg_streamer -i "./input_uvc.so -d /dev/video0 -f 15 -r 960x720" -o "./output_http.so -w ./www"
The best part of MJPEG-Streamer is that you can also use VLC or any other streaming player to watch the stream and you are not limited to a web interface.
Happy streaming.