Using MJPG-Streamer on Raspberry Pi for live streaming using webcam

piwebcam

View your Raspberry Pi Webcam from anywhere.  Connect to it from a unique domain name and check in on your house or pets while you are away.

Requirements For This Walkthrough

Materials

  1. Local network
  2. Mac or PC
  3. Raspberry Pi camera module, a Compatible Webcam (or the exact same one I used to write this tutorial)
  4. Raspberry Pi running Raspbian “wheezy”
  5. Domain name
  6. Router capable of supporting DynDNSnoip, or other dynamic DNS service; or a software-based dynamic DNS client (not covered in this walkthrough)
  7. lighttpd Web server running on the Pi (optional–allows your to embed a stream to a Webpage, which can be password-protected)
  8. HDMI Cable (*optional)
  9. Keyboard (*optional)
  10. Mouse (*optional)
  11. Monitor with HDMI input (*optional)
  12. *If the Raspberry Pi is set up as a headless machine, you will not need a monitor, keyboard, or mouse–just another computer, which would be used to access it remotely over the network via SSH.

Downloads

  1. MJPG-Streamer

Resources

Step-by-step Walkthrough

Short-and-sweet Version (for advanced users)

lsusb
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install libjpeg8-dev imagemagick libv4l-dev
sudo ln -s /usr/include/linux/videodev2.h /usr/include/linux/videodev.h
sudo apt-get install subversion
cd ~
svn co https://svn.code.sf.net/p/mjpg-streamer/code/mjpg-streamer/ mjpg-streamer
cd mjpg-streamer
make mjpg_streamer input_file.so input_uvc.so output_http.so
sudo cp mjpg_streamer /usr/local/bin
sudo cp output_http.so input_file.so input_uvc.so /usr/local/lib/
sudo cp -R www /usr/local/www
sudo vi ~/.bashrc
export LD_LIBRARY_PATH=/usr/local/lib/
source ~/.bashrc
/usr/local/bin/mjpg_streamer -i “input_file.so -f /tmp/stream -n pic.jpg”-o “output_http.so -w /usr/local/www”
sudo vi /usr/sbin/livestream.sh
#!/bin/bash
/usr/local/bin/mjpg_streamer -i “/usr/local/lib/input_uvc.so” -o “/usr/local/lib/output_http.so -w /var/www/stream -c username:password”
sudo chmod 755 /etc/init.d/livestream.sh
sudo update-rc.d livestream.sh defaults

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

lsusb

sudo aptget update

sudo aptget upgrade

sudo aptget install libjpeg8dev imagemagick libv4ldev

sudo ln s /usr/include/linux/videodev2.h /usr/include/linux/videodev.h

sudo aptget install subversion

cd ~

svn co https://svn.code.sf.net/p/mjpgstreamer/code/mjpgstreamer/ mjpgstreamer

cd mjpgstreamer

make mjpg_streamer input_file.so input_uvc.so output_http.so

sudo cp mjpg_streamer /usr/local/bin

sudo cp output_http.so input_file.so input_uvc.so /usr/local/lib/

sudo cp R www /usr/local/www

sudo vi ~/.bashrc

     export LD_LIBRARY_PATH=/usr/local/lib/

source ~/.bashrc

/usr/local/bin/mjpg_streamer i “input_file.so -f /tmp/stream -n pic.jpg”o “output_http.so -w /usr/local/www”

sudo vi /usr/sbin/livestream.sh

     #!/bin/bash

     /usr/local/bin/mjpg_streamer i “/usr/local/lib/input_uvc.so” o “/usr/local/lib/output_http.so -w /var/www/stream -c username:password”

sudo chmod 755 /etc/init.d/livestream.sh

sudo updaterc.d livestream.sh defaults

Conceptual Overview

  1. Update the Raspberry Pi and install software dependencies
  2. Download the MJPG-Streamer source code
  3. Compile the source code
  4. Capture images from the Webcam and serve them on a Webpage
  5. Set MJPG-Streamer to run at boot

Two Methods: Camera Module or USB Webcam

You can use the Raspberry Pi camera module, or any compatible USB Webcam.  I found that that the USB Webcam method is easier and better quality, but there are instructions for both.  In the walkthrough, I’ll be using a USB Webcam.  The camera module instructions can be found in the appendix.

Step-by-step Walkthrough

Check If the Pi Recognizes the Webcam

Plug in the USB Webcam and run the command:

lsusb

1

lsusb

Look for an entry relating to your Webcam.  This is a good indicator it has been recognized.

picamheadon

Update Pi and Install Dependencies

First, update the Raspberry Pi and then install some of the dependencies of MJPG-Streamer.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install libjpeg8-dev imagemagick libv4l-dev

1

2

3

sudo aptget update

sudo aptget upgrade

sudo aptget install libjpeg8dev imagemagick libv4ldev

An adjustment needs to be made:  the videodev.h  file has been replaced with videodev2.h  but this change needs to be made manually.  To do this, just create a symbolic link.

sudo ln -s /usr/include/linux/videodev2.h /usr/include/linux/videodev.h

1

sudo ln s /usr/include/linux/videodev2.h /usr/include/linux/videodev.h

Download and Compile MJPG-Streamer

Unfortunately, MJPG-Streamer isn’t available via aptget , so we need to compile it from source.  This may be more complex than setting up a Webcam using Motion, but I found MJPG-Streamer to be less resource-intensive so the additional complexity of setting it up is worth it.

There are a few different ways to install MJPG-Streamer.  The easiest is via subversion, which is what I will walkthrough.  If you want directions for some other methods, look in the appendix at the bottom of the post.

Download via Subversion (svn)

Install subversion (if necessaary).

sudo apt-get install subversion

1

sudo aptget install subversion

Then, download a copy of the MJPG-Streamer source code to your home folder and move into the directory when the download is complete:

cd ~
svn co https://svn.code.sf.net/p/mjpg-streamer/code/mjpg-streamer/ mjpg-streamer
cd mjpg-streamer

1

2

3

cd ~

svn co https://svn.code.sf.net/p/mjpgstreamer/code/mjpgstreamer/ mjpgstreamer

cd mjpgstreamer

There are a lot of plugins included with MJPG-Stream, but for this walkthrough, we will just compile three.  If you only plan to use a USB Webcam, you can modify the command below to suit your needs.  Just leave off the items you do not want.

  • input_uvc.so (for USB Webcams) : copies JPGs from a single input (the Webcam in this case) to one or more output plugins.  This is good for streaming larger images at a higher framerate with lower CPU usage.
  • input_file.so (for camera module): similar to the above, but copies them from a directory.
  • output_http.so: streams the files to a Webserver

make mjpg_streamer input_file.so input_uvc.so output_http.so

1

make mjpg_streamer input_file.so input_uvc.so output_http.so

Alternatively, running make , you can compile everything that comes with MJPG-Streamer.  I chose to just install what was necessary to save on storage space and resources.

Copy MJPG-Streamer to an Alternate Location

You could run MJPG-Streamer right from the folder after it is compiled ( ./mjpg_streamer ), but it might be better to give it a permanent home.

Run the following commands to copy it to a more globally-accessible area:

sudo cp mjpg_streamer /usr/local/bin
sudo cp output_http.so input_file.so input_uvc.so /usr/local/lib/
sudo cp -R www /usr/local/www

1

2

3

sudo cp mjpg_streamer /usr/local/bin

sudo cp output_http.so input_file.so input_uvc.so /usr/local/lib/

sudo cp R www /usr/local/www

/usr/local/ is a common place for third-party items or things added to a system by an admin.  But you could put them wherever you like, or just leave it where it is.  You will just need to modify the paths for the rest of the walkthrough.

Export Paths

If you did try to run mjpg_streamer  now, it probably returned an error about search paths.  This is because right now, the system doesn’t know where to find the files.  This is an easy fix.  Just append the following line to ~/.bashrc (assuming you ran the commands above to copy the files to /usr/local/ .  This will make it a permanent change so that you don’t need to do this every time you log in:

export LD_LIBRARY_PATH=/usr/local/lib/

1

export LD_LIBRARY_PATH=/usr/local/lib/

Now you can either log out and back in again, but it is easier to run the source  command to apply the change (this has the same effect as logging in):

source ~/.bashrc

1

source ~/.bashrc

Now, you can simply call mjpg_streamer  no matter what directory you are in.  We are almost ready to start capturing images.

Using input_uvc.so to Capture Images

Start running mjpg_streamer  with the command:

mjpg_streamer i “/usr/local/lib/input_uvc.so -f 15 “ o “/usr/local/lib/output_http.so -p 8091”

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s