I'd like to have Raspberry Pi stream video, so I'm doing this:

raspivid ... -o - | nc -l -k 4242

But the problem is that the pipe or nc, is caching the data until someone connects. But I don't want to do that. I want to drop all data, until someone connects.

  • 1
    So why not stream video with a real-time protocol that's meant for streaming video? – dirkt Nov 2 '17 at 9:55

You've set up a listening TCP socket, which requires a client to connect before data can flow. But even if you set up a UDP socket instead you'd likely end up having the client connect midstream without any of the metadata that defines how the stream should be interpreted.

If you have a protocol that provides metadata on a frequent and regular basis you could try a UDP socket. My version of nc doesn't have the -k option but I'm aware there are (at least) two different versions available, so I can't give you a tested command-line example.

If you want to stream video you should use a purpose built piece of software. It's a complex technology area (a complex legal area, too, as there are many patents) and unless you're streaming something simple such as MJPEG you're unlikely to have any success whatsoever with a naïve solution such as netcat.

Some examples of software applications can be found from the likes of Videolan, Icecast, Opencast, or Red5.

Actually, reading some documentation for raspivid it seems that you could use a listener such as that offered by xinetd or systemd to handle the incoming connection, and then start raspivid once the TCP connection is established. You still don't get any flow control or real-time bandwidth management, but in a controlled network you might achieve your aim satisfactorily.

  • Not entirely true. H.264 has Annex B format, which puts metadata (SPS, PPS) before each keyframe. It's purpose is exactly that, to be able to decode video mid-stream. – Pitel Nov 2 '17 at 11:10
  • @Pitel ah ok, I didn't know about Annex B. I've used MPEG and H.264, and for H.264 I ended up needing to use a streamer (although I can't remember which one we finally settled on - it was well over five years ago). Question amended. – roaima Nov 2 '17 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.