Raspberry Pi / PiRider – Install Mjpg_streamer for streaming webcam

Posted: 05/14/2013 in ARM, debian, K2000, PiRider, Raspberry pi
Tags: , , ,

The following procedure has been found here: Raspberry pi webcam with mjpeg_streamer
Mjpg_streamer allows to connect to the webcam device and to either provide the Mjpeg stream directly from the webcam, or to convert the YUV raw stream into MJPEG format.

The main procedure is, in a console type:

lsusb

you should have  your device listed (something like that for a Guillemot HD Exchange camera):

Bus 001 Device 0005: ID 06f8:3014 Guillemot Corp.

or for a Microsoft LifeCam 720p 30fps

Bus 001 Device 004: ID 045e:075d Microsoft Corp. LifeCam Cinema
  • then install the proper dependancies:
sudo apt-get install subversion libv4l-dev libjpeg8-dev imagemagick fswebcam
  • check that your camera support MJPEG:
sudo fswebcam -–verbose

The result should be something like that:

— Opening /dev/video0…
Trying source module v4l2…
/dev/video0 opened.

src_v4l2_set_pix_format,541: Device offers the following V4L2 pixel formats:
src_v4l2_set_pix_format,554: 0: [0×56595559] ‘YUYV’ (YUV 4:2:2 (YUYV))
src_v4l2_set_pix_format,554: 1: [0x47504A4D] ‘MJPG’ (MJPEG)
Using palette MJPEG
...
  • Then Download and Compile Mjpg_Streamer:
svn co https://mjpg-streamer.svn.sourceforge.net/svnroot/mjpg-streamer
mjpg-streamer
cd mjpg-streamer/mjpg-streamer
make USE_LIBV4L2=true clean all
sudo make DESTDIR=/usr install
cd ../.. 
rm -rf mjpg-streamer
  • Now check that everything is working:
mjpg_streamer -i “/usr/lib/input_uvc.so -d /dev/video0″ -o
“/usr/lib/output_http.so -p 80 -w /var/www/mjpg_streamer -n”

and with your browser connect to http://IP/?action=stream
You should see the video coming

You can adjust the framerate the resolution and even setup some credentials to connect to your stream
for example here are my settings:

mjpg_streamer -i “/usr/lib/input_uvc.so -d /dev/video0 -fr 15 -r 320x240″ -o
“/usr/lib/output_http.so -p 80 -w /var/www/mjpg_streamer -n -c YourLogin:YourPassword”

You can eventually put some web pages under your /var/www/mjpg_streamer folder.

  • Set the application as a service:

Now if you want to set it as a service you can create a the following file “/etc/init.d/mjpg_streamer.sh” with the following script:

#!/bin/sh
# /etc/init.d/mjpg_streamer.sh
### BEGIN INIT INFO
# Provides:          mjpg_streamer.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"
                mjpg_streamer -b -i "/usr/lib/input_uvc.so -d /dev/video0 -f 15 -r 320x240 -l off" -o "/usr/lib/output_http.so -p 8080 -w /var/www/mjpg_streamer -n -c YourLogin:YourPassword"
                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
                mjpg_streamer -b -i "/usr/lib/input_uvc.so -d /dev/video0 -f 15 -r 320x240 -l off" -o "/usr/lib/output_http.so -p 8080 -w /var/www/mjpg_streamer -n -c YourLogin:YourPassword"
                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

Don’t forget to change the Login and the password inside the script.
Also you will need to make it executable:

sudo chmod +x /etc/init.d/mjpg_streamer.sh

Install the script as a service:

sudo update-rc.d mjpg_streamer.sh defaults

OR edit  /etc/rc.local and add the line at the bottom of the file (before exit 0)

/etc/init.d/mjpg_streamer.sh

Why 2 solutions? => the first installation procedure starts the service as a standard daemon. So during the boot sequence.
You might need to wait for some services before starting this one.

The second way is triggered at the very end of the boot sequence. So you are sure that all services are started before starting this service manually.
For mjpg_streamer i had to use the second way of installing service because, some times, the startup of the webcam was causing the system to hang before any watchdog was started.
So I installed the hadware watchdog and started it as a service. On webcam hang the watchdog triggers a system reboot.
On the other scenario, the watchdog was not yet started and the system could get stuck due to the webcam hang.

Rcconf

You can even install “rcconf” => sudo apt-get install rcconf and type rcconf to enable/disable the service.

Performances:

About performances here what i could see on a raspbery pi overcloked at 950 Mhz:

If you are using your native webcam MJPEG resolution the CPU usage is very low!:
– about 0.3% (with top command) for a 320×240 resolution @ 15 fps
– about max 1.6% for a 640×480 resolution @15/25fps
– about max 1.6% for a 1024×720 resolution @ 25/30fps
– about 0.3% for 720p but i couldn’t have a smooth playback (1 or 2 fps only)
I tried to use the -q command to change the mjpeg quality and the CPU usage was rising up to 25% for a 640×480 resolution this time because the application is reconverting the webcam stream in real time.

Advertisements
Comments

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s