Page 1 of 1

Real-time webcam streaming tutorial

Posted: Tue Jun 05, 2012 3:25 pm
by funnel
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.

Re: Real-time webcam streaming tutorial

Posted: Mon Jun 18, 2012 3:56 pm
by tidge27
Hi, do you know which parameters could be changed to make the delay even shorter?

Thanks,
Tom.

Re: Real-time webcam streaming tutorial

Posted: Tue Jun 19, 2012 1:53 am
by AndrewS
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...

Re: Real-time webcam streaming tutorial

Posted: Thu Jun 21, 2012 3:13 am
by jandafields
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???

Re: Real-time webcam streaming tutorial

Posted: Thu Jun 21, 2012 11:26 am
by AndrewS
VLC? That seems to have fairly good network support.

Re: Real-time webcam streaming tutorial

Posted: Thu Jun 21, 2012 3:34 pm
by jandafields
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.

Re: Real-time webcam streaming tutorial

Posted: Thu Jun 21, 2012 8:59 pm
by funnel
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

Re: Real-time webcam streaming tutorial

Posted: Fri Jun 22, 2012 12:12 am
by AndrewS
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?

Re: Real-time webcam streaming tutorial

Posted: Fri Jun 22, 2012 3:50 am
by jandafields
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.

Re: Real-time webcam streaming tutorial

Posted: Fri Jun 22, 2012 10:20 am
by AndrewS
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.

Re: Real-time webcam streaming tutorial

Posted: Fri Jun 29, 2012 10:29 am
by zali
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.

Re: Real-time webcam streaming tutorial

Posted: Tue Jul 17, 2012 10:13 pm
by cegan09
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.
[email protected]:~$ 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)
[email protected]:~$
any ideas what i'm missing?

Re: Real-time webcam streaming tutorial

Posted: Sun Aug 26, 2012 9:06 am
by bkor
Can I use netcat on RPi and see the video stream on a windows PC using VLC?
(There is a netcat windows version)

Re: Real-time webcam streaming tutorial

Posted: Sat Oct 13, 2012 3:05 pm
by rlovelett
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.

Re: Real-time webcam streaming tutorial

Posted: Mon Dec 03, 2012 4:40 am
by ev5unleash
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!

Re: Real-time webcam streaming tutorial

Posted: Wed Jan 09, 2013 3:50 am
by mattadamsnet
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

Re: Real-time webcam streaming tutorial

Posted: Tue Mar 05, 2013 6:43 am
by emdarcher
i have the same problem with the /dev/video0 invalid issue

Re: Real-time webcam streaming tutorial

Posted: Fri Mar 22, 2013 7:19 pm
by jniedung
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

Re: Real-time webcam streaming tutorial

Posted: Tue Apr 16, 2013 11:58 am
by rpisead
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!

Re: Real-time webcam streaming tutorial

Posted: Thu Apr 18, 2013 12:44 am
by cjm
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!