Add validation to detect non-monotonic RTP timestamps during packet writing. If the current timestamp is less than the previous timestamp (taking wrap-around into account), a warning log is emitted.
Non-monotonic timestamps may indicate issues in timestamp generation or incorrect encoder behavior, which can lead to playback issues or synchronization problems in RTP streams. This change improves debugging and helps developers identify incorrect timestamp handling in RTP streaming workflows. Signed-off-by: Devraj Ajmera <[email protected]> --- libavformat/rtpenc.c | 10 ++++++++++ libavformat/rtpenc.h | 1 + 2 files changed, 11 insertions(+) diff --git a/libavformat/rtpenc.c b/libavformat/rtpenc.c index 1d4b00a0f4..86a8b93be5 100644 --- a/libavformat/rtpenc.c +++ b/libavformat/rtpenc.c @@ -127,6 +127,7 @@ static int rtp_write_header(AVFormatContext *s1) s->base_timestamp = av_get_random_seed(); s->timestamp = s->base_timestamp; s->cur_timestamp = 0; + s->prev_timestamp = 0; if (!s->ssrc) s->ssrc = av_get_random_seed(); s->first_packet = 1; @@ -565,6 +566,15 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt) } s->cur_timestamp = s->base_timestamp + pkt->pts; + if (s->prev_timestamp && + (int32_t)(s->cur_timestamp - s->prev_timestamp) < 0) { + av_log(s1, AV_LOG_WARNING, + "Non-monotonic RTP timestamp detected: prev=%u current=%u\n", + s->prev_timestamp, s->cur_timestamp); + } + + s->prev_timestamp = s->cur_timestamp; + switch(st->codecpar->codec_id) { case AV_CODEC_ID_PCM_MULAW: case AV_CODEC_ID_PCM_ALAW: diff --git a/libavformat/rtpenc.h b/libavformat/rtpenc.h index ba88bfefc0..8312735f23 100644 --- a/libavformat/rtpenc.h +++ b/libavformat/rtpenc.h @@ -35,6 +35,7 @@ struct RTPMuxContext { uint32_t timestamp; uint32_t base_timestamp; uint32_t cur_timestamp; + uint32_t prev_timestamp; int max_payload_size; int num_frames; -- 2.53.0.windows.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
