How to send MPEGTS streams over UDP

pAkY88 picture pAkY88 · May 23, 2012 · Viewed 20.6k times · Source

I am developing a realtime video-streaming system which is composed basically by a server and several clients.

For now, let's ignore how packets are forwarded among the server and the clients, let's focus just on how the server can send a MPEGTS stream over UDP packets.

The stream is encoded in MPEGTS format.

What I'm trying to do is reading some packets (the main question is "how many?") and encapsulating them in UDP packets. The destination (a client) reads these UDP packets and then forward them to VLC, which is able to play MPEGTS network streams by reading UDP packets.

If I send only video packets, everything works fine, instead if I try to encapsulate in the same UDP packet, both some video packets and some audio packets, VLC is not able to decode and play the stream. I read somewhere that each UDP packet should contain 7 TS packets, but unfortunately even if I comply with this rule, VLC doesn't decode the stream correctly.

Here is a sample code of my program: http://pastebin.com/evMi6FkY

How should I encapsulate MPEGTS packets in UDP packets?

Thanks!

Answer

Seth Noble picture Seth Noble · Jun 4, 2012

Your problem is: "let's ignore how packets are forwarded among the server and the clients".

UDP requires you to deal with all of the issues of network transport including flow-control, error detection and recovery, path maximum transmit unit size, packetization, buffering, serialization, de-duplication, etc.

Even if you break your data up into packets of just the right size and send them at just the right rate, some will still be lost, duplicated, or delivered out of order. Your code must handle all of those conditions, otherwise you cannot trust that what you receive is what you sent.

In this particular case, I would guess that your packets have become too large, resulting in fragmentation and high drop rate. Generally speaking, it is best not to have more than about 1400 bytes per packet. But incorrect ordering, loss, and duplication are all possible as well and all become more likely as you try to send larger volumes of data.

Disclaimer: I work for a company that produces commercial UDP data transport software.