Skip to content

tsmux: prevent rounding error when padding

Problem: when tsmux is running for a long time (weeks), it starts to add padding buffers in increasingly large batches. For many seconds there is no padding, and then suddenly a lot of null packets.

Reason: current code in tsmux calculates current bitrate from total number of bytes and timestamp. Then it adds padding while bitrate<=mux->bitrate. Since bitrate is integer this means - we add padding until average bitrate reaches mux->bitrate+1. This is a lot of padding!

Example: bitrate is 1000000 (1mbps), 1,000,000s (12 days). We wrote 10^12 bits so far. We start padding and add 999,000 more bits, and bitrate is still 1000000.999, rounded down to 1000,000. We keep padding. Only when we wrote 1mbps of data, padding stops. That's entire second full of padding data, and it gets worse the longer we run.

With larger bitrates this happens later, but always in a matter of weeks PCR/PTS difference becomes larger, and we start getting these huge fragments of padding data.

Solution: instead of targeting bitrate we should target the number of bytes that must be written according to current PTS.

Edited by Yury Shatz

Merge request reports