Wednesday, November 06, 2013

mjpg-streamer on Rasberry Pi

Commands copied all across the webs so real credit to the Universe, mainly http://blog.miguelgrinberg.com/post/how-to-build-and-run-mjpg-streamer-on-the-raspberry-pi but not limited to.

Checkout the latest code:
svn checkout svn://svn.code.sf.net/p/mjpg-streamer/code/ mjpg-streamer-code

Intstall dependencies and work around bugs:
apt-get install libjpeg8-dev imagemagick libv4l-dev
ln -s /usr/include/linux/videodev2.h /usr/include/linux/videodev.h

I don't have a special Pi camera but a bunch of cheap Linux-supported USB webcams so I really want the input_uvc.so:
make mjpg_streamer input_file.so output_http.so input_uvc.so

Copy binaries around:
cp mjpg_streamer /usr/local/bin
cp input_uvc.so output_http.so input_file.so /usr/local/lib/
cp -R www /usr/local/www
cd /usr/local/www

Limit the index to a more limited choice, do whatever you want here really.
mv index.html index.html.fubar
mv stream.html index.html

Run the mjpg_streamer from a script, input is the /dev/videoX  and a port offset so that I can run multiple cameras:
#!/bin/bash
LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_uvc.so -f 12 -d $1 -r 1280x720" -o "output_http.so -p $2 -w /usr/local/www"

i.e., streamcam /dev/video0 8080 or streamcam /dev/video1 8081 from an rc script.

Finally set up a NAT on your router to point it to this or use Apache reverse proxies from an existing web server whatever floats your boat.

Here's the easiest way to do it on Apache, assuming the Pi is on 192.168.1.99 and you've got Apache server with ProxyPass etc. modules loaded. Go and look somewhere else on how to setup an Apache server. You might even prefer to install the Apache Server on your Pi, it might be easier to manage the Apache server instead of the locally managed one.

<VirtualHost *:80>
  ServerName webcam.fubar.com
   DocumentRoot /home/fubar/public_html/webcam
   ProxyPass / http://192.168.1.99:8080/
   ProxyPassReverse / http://192.168.1.99:8080/
   <Directory />
   </Directory>
</VirtualHost>

<VirtualHost *:443>
  ServerName webcam.fubar.com
   DocumentRoot /home/fubar/public_html/webcam
   SSLEngine On
   SSLProxyEngine On
   SSLCertificateFile ssl/server.crt
   SSLCertificateKeyFile ssl/server.key.insecure
   ProxyPass / http://192.168.1.99:8080/
   ProxyPassReverse / http://192.168.1.99:8080/
   <Directory />
   </Directory>
</VirtualHost>