Search code examples
cmpegcircular-buffer

c circular buffer with constant time delay for mpeg-ts


Could You say me the best way of implementing circular buffer with constant time delay between input and output in c on linux. I write to the buffer in on thread and read it on another. I need to save constant time difference between read and write, and the writing stream has variable bitrate. Now I add timpestamps every few packets to synchronize them, and I get timestamps from another thread which is using clock_gettime every 1ms.


Solution

  • The overall approach is correct as you suggested. However, there are many things needs to be taken care of -

    1. The source thread submitting to CBuf and sink thread removing from CBuf.

    2. The CBuf must be large enough so that data doesn't get lost.

    3. The CBuf must be locked by both threads to ensure no corruption.

    4. The CBuf data must be time stamped. This timestamps must be accurate enough not to cause transmission jitter.

    5. The sink thread must be delayed to ensure that it doesn't under flow.

    6. The source and sink thread must derive time only from a common source of time so that there is no drift between their respective rate of inlfow and outflow.

    Probably if you are thinking of Transmitting it to some hardware system outside, the timestamp precision of 1 ms, is quite low because MPEG transmission compliance need 500 ns. However, if you are using hardware adapter to transmit, then generally it takes care of it.