User Tools

Site Tools


rpi_livestream

RPI Live Stream

Copied from

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

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

Download and Compile MJPG-Streamer

Unfortunately, MJPG-Streamer isn’t available via apt-get , 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

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

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

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

/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/

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

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:

/usr/local/bin/mjpg_streamer -i "/usr/local/lib/input_uvc.so" -o "/usr/local/lib/output_http.so -w /usr/local/www"

=== this worked or Logitech C615 ==
/usr/local/bin/mjpg_streamer -i "/usr/local/lib/input_uvc.so -d /dev/video0 -y -r 1200x900 -f 30" -o "/usr/local/lib/output_http.so -w /usr/local/www"

====


This command options are as follows:

    -i : uses input_uvc.so (the USB Webcam) as input
    -o : output_http.so for the output (sending the images to a Web server
    -w : the directory, which has the HTML, CSS, and JS files:   /usr/local/www

You can cancel the stream by pressing Ctrl+C.  If you want to experiment with some other options, some are listed below:

    -b : runs in the background
    -p : set a default port instead of the default 8080
    -c : prompts for username:password
    -y YUYV : enables YUYV format and disables MJPEG mode
    -f : framerate (in seconds)

If you used the -b  option, you will get your prompt back.  So how do you stop it from running?  After running the command, you will see a line like:

	
forked to background (4979)

If you want to stop the stream, just run:

	
kill 4979

which just kills the process ID (PID) of MJPG-Streamer.
View the Webcam Live-stream From a Browser
(while connected to your local network)

Even if you have already set up a Webserver, you can still run this without problem because it is accessed on a different (default) port: 8080 .  So when you navigate to it in a browser, just append :8080 after the IP address:

    locally:  http://localhost:8080
    from another device on the network:  http://<raspberry_pi's_ip_address>:8080

catcam
Accessing Your Pi Over the Internet

Viewing your Webcam while connected to your local network is cool, but I’ll show you how to access your Webcam from anywhere by going to a URL such as: myDomain.com:8080 .  This stream can be password-protected, but it really isn’t very secure, so be careful if you make this available online.

pioverinternet
Pre-requisites

    Forward port  8080 to your Raspberry Pi
    Enable a dynamic DNS service on your router
    (optional) Setup a lighttpd Web server with a password-protected directory

Once the steps above are complete, you will be able to access your Webcam stream from any browser by navigating to http://myDomainName.org:8080 .  But there is no password yet!  Anyone on the Internet will be able to see your Webcam.

We can tell MJPG-Streamer to use a password with the -c  option.  This password is not very secure (it is only base 64 encoded), but it can offer a first line of defense.   Someone could easily sniff and decode the password, but if you are on a trusted network then it is not as much of a problem.

The command would look like this:
	
/usr/local/bin/mjpg_streamer -i "/usr/local/lib/input_uvc.so" -o "/usr/local/lib/output_http.so -w /usr/local/www -c username:password"

The best way to secure any online resource is with a layered-defense model.   So before you would want this out on the Internet, you might want to be protected using other tools.
Embed the Stream into a Web Page

For simplicity, just make the page in the same directory that is serving up the HTML files ( /usr/local/www/  in this walkthrough).  This makes it easier to make sure the page works.  After you know its working, you can move it somewhere else, but you will just need to adjust the paths.  Enter the following HTML code (adjusting to your setup) and it is ready to go.
	
<html>
   <head><title>Kitty-cam Live-stream</title></head>
   <body>
      <center><h1>Meow</h1></center>
        <center>
      <img src="/?action=stream" />
        </center>
<body>
</html>

If you want, you can also use lighttpd to password-protect your embedded-stream.
Now, just navigate to your page to see it:

    http://192.168.1.100:8080/catcam.html (on the local network)
    http://myDomainName.org:8080/catcam.html  (over the Internet)

Run MJPG-Streamer as a Daemon (Background Service)

Until now, we have just been launching MJPG-Streamer on an as-needed basis by running a command with our options.  If you want to make this more permanent and have the Raspberry Pi start the Webcam stream when it boots up, we need to tell it to do so.

There are two scripts below: a very basic one that just runs the command, and an advanced script that will allow you to use the service  command to control it:
	
sudo service livestream.sh start
sudo service livestream.sh stop
sudo service livestream.sh restart

Simple Script

A very simple script to do this is below:

	
sudo vi /etc/init.d/livestream.sh

	
#!/bin/bash
/usr/local/bin/mjpg_streamer -i "/usr/local/lib/input_uvc.so" -o "/usr/local/lib/output_http.so -w /usr/local/www -c username:password"

Save the script.  Make it executable:
	
sudo chmod 755 /usr/sbin/livestream.sh

This next command makes sure it is executed during boot:
	
sudo update-rc.d livestream.sh defaults

Advanced Script
	
#!/bin/sh
# /etc/init.d/livestream.sh
### BEGIN INIT INFO
# Provides:          livestream.sh
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: mjpg_streamer for webcam
# Description:       Streams /dev/video0 to http://IP/?action=stream
### END INIT INFO
f_message(){
        echo "[+] $1"
}
 
# Carry out specific functions when asked to by the system
case "$1" in
        start)
                f_message "Starting mjpg_streamer"
                /usr/local/bin/mjpg_streamer -b -i "/usr/local/lib/input_uvc.so" -o "/usr/local/lib/output_http.so -w /usr/local/www -c username:password"
                sleep 2
                f_message "mjpg_streamer started"
                ;;
        stop)
                f_message "Stopping mjpg_streamer…"
                killall mjpg_streamer
                f_message "mjpg_streamer stopped"
                ;;
        restart)
                f_message "Restarting daemon: mjpg_streamer"
                killall mjpg_streamer
                /usr/local/bin/mjpg_streamer -b -i "/usr/local/lib/input_uvc.so" -o "/usr/local/lib/output_http.so -w /usr/local/www -c username:password"
                sleep 2
                f_message "Restarted daemon: mjpg_streamer"
                ;;
        status)
                pid=`ps -A | grep mjpg_streamer | grep -v "grep" | grep -v mjpg_streamer. | awk ‘{print $1}’ | head -n 1`
                if [ -n "$pid" ];
                then
                        f_message "mjpg_streamer is running with pid ${pid}"
                        f_message "mjpg_streamer was started with the following command line"
                        cat /proc/${pid}/cmdline ; echo ""
                else
                        f_message "Could not find mjpg_streamer running"
                fi
                ;;
        *)
                f_message "Usage: $0 {start|stop|status|restart}"
                exit 1
                ;;
esac
exit 0

Once saved, run the same commands as above:
sudo chmod 755 /etc/init.d/livestream.sh sudo update-rc.d livestream.sh defaults
1
2
	
sudo chmod 755 /etc/init.d/livestream.sh
sudo update-rc.d livestream.sh defaults

Success!

Now your Webcam is available over the Internet and starts up when you turn on your Raspberry Pi.
rpi_livestream.txt · Last modified: 2021/06/04 02:16 (external edit)