tvjon
Posts: 802
Joined: Mon Jan 07, 2013 9:11 am

socat, streaming, & broadcasting RPi-camera

Fri May 24, 2013 4:15 pm

I've been experimenting with vlc, mplayer, avplay, etc. in connection with streaming the new Pi camera to other Pi's, & mac's, on a LAN (not W-LAN).
As I wanted the full rate video from the camera, none of the above were satisfactory of course. Massive numbers of errors were generated by vlc, amazing to get a picture at all!

I read jamesh's post in the Foundation camera thread about good low latency, so that was the impetus to experiment. I've used netcat successfully for Pi to Pi video streaming, but also wanted to broadcast the pi camera video to potentially several other monitors (via Pi's) on the LAN, similar to having monitors or iPads in your airline seat, all watching that gripping in-flight survival video. Such a setup could also be useful for a large school hall presentation, with monitors at the back of the room for example.

Naturally, using the VideoCore processor is the only realistic way to get good performance (I'm itching to get Wayland running) so I used "hello_video" from the

/opt/vc/src/hello_pi /hello_video

directory.

If you've never used hello_video before, you'll need to compile it. Ensure that Makefile.include is inside the "hello_pi" directory. I didn't want to rebuild everything, just hello_video.

So, type

make

at the $ prompt & a hello_video.bin executable file will be produced in the same hello_video directory.

If all you want is Camera-Pi to other Pi streaming, you can just use netcat (nc) thus from the RPi with camera:

On the receiving RPi:

pi@raspberrypi /opt/vc/src/hello_pi/hello_video $ nc -l -k 8080 | ./hello_video.bin /dev/stdin

Note that no IP is required here, just the same port that you'll stream from, & that also I used -k

-k isn't essential, but I find it very useful as the last image received, before the source stops transmitting, remains on screen at the receiving monitor. Handy to catch the perpetrator who's just unplugged your surveillance camera :)

EDIT: Today's rip-update has changed the way the raspivid executable interprets the time value. I've been using -1 for several days, but you need to revert to an unsigned value, say 999999 for the update. Should you want to try these statements on the existing firmware, -1 will be fine.

Now on Camera Pi:

pi@raspberrypi $ raspivid -t -1 -hf -o -| nc 192.168.1.107 8080

ensuring you replace my LAN IP & port with your own choice.


The latency & video quality in this setup is vastly better than using vlc, et al, & in line with jamesh's experience, & bear in mind I'm transmitting the full image, not a cut down version. Most of my testing is done using ethernet, but the above setup was fine during a few minutes testing with a receive RPi on a wifi dongle.

The next phase was to try broadcasting RPi camera to several receiver RPi's. Well, I did try netcat, but no joy :( I'm no networking expert, so if any of you know how to use it for such, please report back.

What does work however (again after much trial & ERROR) is socat

sudo apt-get install socat

as always to install a new application. Next from the CameraPi:

pi@raspberrypi $ raspivid -t -1 -hf -o -| socat - udp-datagram:192.168.1.255:8080,broadcast

Here you'd need to substitute the first 3 IP decimal values with your own, leaving the 255 as it is (unless you need thousands of monitors of course :) )

& on the receiving RPi:

pi@raspberrypi /opt/vc/src/hello_pi/hello_video $ socat - udp-recv:8080,reuseaddr | ./hello_video.bin /dev/stdin

Certainly in my tests, the Camera Pi almost saturates the LAN when broadcasting, to the extent that other computers can't get in easily. Interestingly no perceptible errors, so I imagine the recent USB improvements have helped a lot. I'm just hopeful my DVB-T2 tuner will work properly soon :)

So far I've used 3 RPi's in the broadcast setup, all on ethernet. I need more Pi's to test further :) Hopefully I can demonstrate the setup at the MK jam on Sunday.
I briefly tried a wifi dongle, but no joy on the broadcast setup, so I need to investigate that, but I thought it worth posting my findings on the off chance it may be interesting to others.

Now I need to mux in some audio channels to the stream :)

User avatar
fbutler
Posts: 302
Joined: Thu Mar 15, 2012 4:09 pm
Location: Surrey, England

Re: socat, streaming, & broadcasting RPi-camera

Fri May 24, 2013 7:25 pm

tvjon wrote: The next phase was to try broadcasting RPi camera to several receiver RPi's. Well, I did try netcat, but no joy :( I'm no networking expert, so if any of you know how to use it for such, please report back.
Just curious, I haven't looked at this yet, but did you try netcat with the 192.168.1.255 broadcast address? If so what was the result?

M33P
Posts: 199
Joined: Sun Sep 02, 2012 1:14 pm

Re: socat, streaming, & broadcasting RPi-camera

Fri May 24, 2013 7:38 pm

Multicast.

http://en.wikipedia.org/wiki/IP_multicast

If you have a sensible switch/router knocking around on the network, it should correctly deal with multicast traffic.

If a host then listens on a multicast address, the switch should then direct multicast traffic to it.

Check out multicat, available via apt-get.

User avatar
Grid
Posts: 22
Joined: Fri Jan 04, 2013 5:02 pm
Location: Lodz, Poland

Re: socat, streaming, & broadcasting RPi-camera

Mon May 27, 2013 9:20 am

UDP streaming via socat works rather well, after the latest rpi-update you should use
-t 999999
rather than
-t -1
Fix instead of throwing away. Save the planet one gadget at a time.

dynamitemedia
Posts: 30
Joined: Sun Dec 01, 2013 9:12 pm

Re: socat, streaming, & broadcasting RPi-camera

Tue Feb 25, 2014 6:37 pm

i know this is a old thread but i am trying to get socat working with just a normal file and udp streaming... can you post more code on how you got this working?

tvjon
Posts: 802
Joined: Mon Jan 07, 2013 9:11 am

Re: socat, streaming, & broadcasting RPi-camera

Fri Feb 28, 2014 8:30 pm

"i am trying to get socat working with just a normal file and udp streaming..."

Wouldn't it be more useful if you posted what statements you've already tried, along with a description of your hardware setup?

User avatar
ekla
Posts: 47
Joined: Wed Apr 09, 2014 9:39 am
Location: Lyon, France

Re: socat, streaming, & broadcasting RPi-camera

Tue Jul 08, 2014 8:52 am

it works well for me, with netcat and with socat but i have a problem and i think everybody too.

I noticed that the latency between the two pi is drifting and start at 50-80ms to 1 second and stop drifting and stay at 1 second after 2 or 3 minutes transmitting. I don't know why, first i was sure it was my switch but no.
You just have to plug-unplug the lan cable on the TX or the RX pi to get the good latency 50-80ms. But after, it's drifting again.

Plug-unplug is not a good solution, and i'm sure there is a way to fix that.

I have somes ideas :
- synchronise the pi (src-sinc ? ntp ?)
- genlock the pi
- modify the network buffer on the pi ( existing ?).

do you have some ideas ?
do you see you too that the latency between the 2 pi goes up ?

thanks a lot to you
our fears are like dragons guarding our most precious treasures

User avatar
ekla
Posts: 47
Joined: Wed Apr 09, 2014 9:39 am
Location: Lyon, France

Re: socat, streaming, & broadcasting RPi-camera

Wed Jul 23, 2014 4:51 pm

just put -fps 29,9 and it never drifts
our fears are like dragons guarding our most precious treasures

Return to “Camera board”