Real-time webcam streaming tutorial


20 posts
by funnel » Tue Jun 05, 2012 3:25 pm
By researching over the internet I found a way to stream a live webcam mjpeg stream with minimal delay (<1s). It could be useful for anybody who wants to use it on a rc car or robot in first person view where high latencies are not an option.

I'm using a PS2 Eyetoy but it'll will probably work on any cam that streams mjpeg video.

I'm doing it on Debian squeeze with the latest kernel updated with rpi-update which supports many webcams including the PS2 Eyetoy.

First we need to install Netcat on RPi and the target linux machine where we are sending the video stream
Code: Select all
sudo apt-get install netcat


on the target machine we also need mplayer to play the video.
Code: Select all
sudo apt-get install mplayer


when everything installed we run on the target machine
Code: Select all
mkfifo a.mjpg

this is needed because mplayer doesn't detect correctly mjpeg streams without the .mjpg at the end.

next on the target machine we run Netcat in listen udp mode on any port we want. I'm using port 5000 in this example
Code: Select all
nc.traditional -lu -p 5000 > a.mjpg | mplayer -cache 32 -vo gl -demuxer lavf a.mjpg


on RPi we start netcat and send the raw contents of /dev/video0 to the target machine
Code: Select all
sudo su
cat /dev/video0 | nc.traditional -u [YOUR TARGET IP HERE] 5000

I'm using 'sudo su' because I'm getting input/output errors from the cam driver if I only do sudo in front of cat.

That's it, the video on the target machine should start in a few seconds.
Posts: 46
Joined: Sat May 05, 2012 8:00 am
by tidge27 » Mon Jun 18, 2012 3:56 pm
Hi, do you know which parameters could be changed to make the delay even shorter?

Thanks,
Tom.
Posts: 6
Joined: Sun Jun 17, 2012 8:38 pm
by AndrewS » Tue Jun 19, 2012 1:53 am
I suspect that cat and netcat will probably be working as fast as possible (subject to the speed of your network connection), which I guess only leaves you the mplayer options to experiment with...
User avatar
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
by jandafields » Thu Jun 21, 2012 3:13 am
That is GREAT!!! But, I would like to stream mjpeg TO the Raspberry Pi. For example, I have security cameras that send mjpeg over the network, and I would like to connect the RaspberryPi to my TV and watch the stream that way.

Any ideas???
Posts: 11
Joined: Thu Jun 21, 2012 3:11 am
by AndrewS » Thu Jun 21, 2012 11:26 am
VLC? That seems to have fairly good network support.
User avatar
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
by jandafields » Thu Jun 21, 2012 3:34 pm
Can VLC be installed with a simple apt-get install vlc on Raspberry Pi?

I don't know if VLC will run the video if it's MJPEG very well, as there is no support for that codec on the Pi.
Posts: 11
Joined: Thu Jun 21, 2012 3:11 am
by funnel » Thu Jun 21, 2012 8:59 pm
Mplayer should play it. How do you access the camera stream?

Try using the mplayer option "-vo fbdev" or "-vo fbdev2". Right now I can't remember which one worked for me.

for example
Code: Select all
mplayer http://cam_url -vo fbdev
Posts: 46
Joined: Sat May 05, 2012 8:00 am
by AndrewS » Fri Jun 22, 2012 12:12 am
jandafields wrote:Can VLC be installed with a simple apt-get install vlc on Raspberry Pi?

Just tried it on the Debian-wheezy-beta image on my RPi - installs fine but has a very big list of dependencies - says it'll use 75MB of diskspace.

I don't know if VLC will run the video if it's MJPEG very well, as there is no support for that codec on the Pi.

With the typical low resolutions of webcams, I'd expect it to be fine?
User avatar
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
by jandafields » Fri Jun 22, 2012 3:50 am
I tried it. It works, but is not really usable. The video is very choppy and delayed. Even if I put -nocache, it still has a delay of several seconds and the video is choppy regardless of the cache.

The source is only sending 320x240 resolution. I've tried setting the source device to 5 fps and to 30 fps. I've tried using -framedrop. Regardless, the Pi cannot play it smoothly and without delay.
Posts: 11
Joined: Thu Jun 21, 2012 3:11 am
by AndrewS » Fri Jun 22, 2012 10:20 am
Sounds like this is one of those situations where it would help to try the same thing on "normal" linux PCs that you're trying on the RPi, which should at least help you narrow down exactly where the problem is, i.e. is it the slow webcam, the slow network, VLC simply not being compatible, the Pi simply not being fast enough, etc. etc.
User avatar
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
by zali » Fri Jun 29, 2012 10:29 am
Did anyone fing some solution for
Code: Select all
cat: /dev/video0: Input/output error

I get that everytime after some time of streaming and then I am unable to even stream again, just the same error again.
Posts: 2
Joined: Wed Jun 27, 2012 2:38 pm
by cegan09 » Tue Jul 17, 2012 10:13 pm
i seem to be missing something on mine. exactly following your instructions, on the target machine i get the following over and over whenever i start the stream on the pi

cache empty, consider increasing -cache and/or -cache-min. [performance issue]


when i remove the cache parameter alltogether i get it as far as

libavformat file detected.

then nothing. mplayer never opens a window, i never see the stream. if i hit ^c on the target machine i get a "signal interupted" but then a bunch of lines that look like it's starting to open the stream before imediatly going to a prompt.

chris@chris-ThinkPad-T60:~$ nc.traditional -lu -p 8081 > a.mjpg | mplayer -vo gl -demuxer lavf a.mjpg
MPlayer svn r34540 (Ubuntu), built with gcc-4.6 (C) 2000-2012 MPlayer Team
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

Playing a.mjpg.
libavformat version 53.21.0 (external)
Mismatching header version 53.19.0
libavformat file format detected.
^C


MPlayer interrupted by signal 2 in module: demux_open
[mjpeg @ 0xb6b4ad80]Estimating duration from bitrate, this may be inaccurate
[lavf] stream 0: video (mjpeg), -vid 0
VIDEO: [MJPG] 0x0 0bpp 25.000 fps 0.0 kbps ( 0.0 kbyte/s)
Load subtitles in ./
[gl] using extended formats. Use -vo gl:nomanyfmts if playback fails.
==========================================================================
Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
libavcodec version 53.35.0 (external)
Mismatching header version 53.32.2
Selected video codec: [ffmjpeg] vfm: ffmpeg (FFmpeg MJPEG)
==========================================================================
Audio: no sound
Starting playback...
V: 0.0 0/ 0 ??% ??% ??,?% 0 0

Exiting... (Quit)
chris@chris-ThinkPad-T60:~$


any ideas what i'm missing?
Posts: 1
Joined: Tue Jul 17, 2012 10:05 pm
by bkor » Sun Aug 26, 2012 9:06 am
Can I use netcat on RPi and see the video stream on a windows PC using VLC?
(There is a netcat windows version)
Posts: 1
Joined: Sun Aug 26, 2012 8:58 am
by rlovelett » Sat Oct 13, 2012 3:05 pm
zali wrote:Did anyone fing some solution for
Code: Select all
cat: /dev/video0: Input/output error

I get that everytime after some time of streaming and then I am unable to even stream again, just the same error again.


I was having the same issue. Mine was because /dev/video0 was mounted by root:video. The user I was logged in as was not part of the video group. So I had to add my user to the group (then log-out, and then back in again). Once I did that I was no longer having an issue.

Code: Select all
$ ls -alh /dev/video0
crw-rw---T+ 1 root video 81, 0 Oct 13 09:21 /dev/video0


If you are not root or in the video group you cannot read the video stream.

Code: Select all
$ groups
ryan


If you do not see the video group listed as one of your groups then you need to add it.

Code: Select all
$ usermod -a -G video ryan


Group permissions are reloaded at login; so logout then login.

Code: Select all
$ groups
ryan video


TADA! That should allow you to read the video stream.
Posts: 4
Joined: Sat Oct 13, 2012 2:34 am
by ev5unleash » Mon Dec 03, 2012 4:40 am
Hi all,

This tutorial doesn't seem to work for me. Everytime I get to entering "cat /dev/video0" I get a "cat: /dev/vide0: invalid argument. I'm using a Logitech c720 and it lists in lsusb.

Thanks for the help!
Posts: 4
Joined: Sat Aug 04, 2012 11:20 pm
by mattadamsnet » Wed Jan 09, 2013 3:50 am
by ev5unleash » Sun Dec 02, 2012 10:40 pm

Hi all,

This tutorial doesn't seem to work for me. Everytime I get to entering "cat /dev/video0" I get a "cat: /dev/vide0: invalid argument. I'm using a Logitech c720 and it lists in lsusb.

Thanks for the help!


Same problem
Posts: 5
Joined: Wed Jan 09, 2013 3:46 am
by emdarcher » Tue Mar 05, 2013 6:43 am
i have the same problem with the /dev/video0 invalid issue
Main setup:

Raspberry Pi model B
Lego case
4 port USB hub
Wi-Pi USB wifi adapter
1999 Apple iMac keyboard
ball mouse
Raspbian

Robot proto setup:
Raspberry Pi model B
Make:Pi case
4 port USB hub
RTL8191 USB wifi
12000mah battery
Arduino UNO
Occidentalis
Posts: 1
Joined: Wed Feb 20, 2013 7:40 pm
by jniedung » Fri Mar 22, 2013 7:19 pm
won't work for me either , because its no tutorial at all.
Let the world know what you are doing - at least no one understands this Beasty Bastard style OS and
it's stupid permission rules.

sudo su means ? never heard off.
what ist netcat doing ? will we get an rtsp http or whetever typ stream readable
by vlc ?

how can we proof cat is doing something - not only telling No passwd for user cat and
why the hell it is "user cat" - Thought its a program.

Isn't one linux Bastard enough @ home ? you really dont use Windows ? no chance
to proof functionality with an OS "user friendly".

Keep laughing about me but I will never understand these Beasty Bastard OS Types.

Why are all these Linux likers so horroble unable to explain us unknowing Guys the mystiques
of Linux ? Perhaps they dont even understand better.

Trying Linux is the most effective Way to fall in Love with windows.

Regards JoE
Posts: 4
Joined: Mon Mar 11, 2013 10:00 pm
by rpisead » Tue Apr 16, 2013 11:58 am
Thanks for the info!

I am currently testing different cam settings, checkout (using v4l2-ctl):
http://answers.opencv.org/question/146/ ... sb-webcam/

I am very sad when I read (most of) these post replies of fools with the lack of basic Linux or networking knowledge. If you don't have a clue at least be polite!
Posts: 1
Joined: Tue Apr 16, 2013 11:40 am
by cjm » Thu Apr 18, 2013 12:44 am
I'm trying to replicate this using a different webcam and raspbian. I can do cat /dev/video0 and it will output the stream to the terminal. however I am getting stuck with the netcat part of the tutorial. When I try to connect with netcat to a listener, it never works. Even if I try it using two terminals on localhost?!

Is a reason why netcat wouldn't work right on raspbian? This blocker is making me want to go to a different solution, but I like how eloquent this one is!
Posts: 2
Joined: Thu Apr 18, 2013 12:42 am