From 85492608c661b70fb3f523a1ebd75a98eac8166b Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Sat, 7 Oct 2023 09:41:42 +0200 Subject: [PATCH] rtp: use uint64_t for ts_arrive and fix video jitter calculation --- include/re_rtp.h | 2 +- src/rtp/rtcp.h | 1 + src/rtp/sess.c | 15 +++++++++++---- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/include/re_rtp.h b/include/re_rtp.h index 5855e27a6..2e8bb71b7 100644 --- a/include/re_rtp.h +++ b/include/re_rtp.h @@ -23,7 +23,7 @@ struct rtp_header { uint8_t pt; /**< Payload type */ uint16_t seq; /**< Sequence number */ uint32_t ts; /**< Timestamp */ - uint32_t ts_arrive; /**< Arrival Timestamp */ + uint64_t ts_arrive; /**< Arrival Timestamp */ uint32_t ssrc; /**< Synchronization source */ uint32_t csrc[16]; /**< Contributing sources */ struct { diff --git a/src/rtp/rtcp.h b/src/rtp/rtcp.h index 194b6b692..d85404866 100644 --- a/src/rtp/rtcp.h +++ b/src/rtp/rtcp.h @@ -44,6 +44,7 @@ struct rtp_source { uint64_t sr_recv; /**< When the last SR was received */ struct ntp_time last_sr; /**< NTP Timestamp from last SR received */ uint32_t rtp_ts; /**< RTP timestamp */ + uint32_t last_rtp_ts; /**< Last RTP timestamp */ uint32_t psent; /**< RTP packets sent */ uint32_t osent; /**< RTP octets sent */ }; diff --git a/src/rtp/sess.c b/src/rtp/sess.c index bba3a387a..4d6e5f565 100644 --- a/src/rtp/sess.c +++ b/src/rtp/sess.c @@ -598,12 +598,19 @@ void rtcp_sess_rx_rtp(struct rtcp_sess *sess, struct rtp_header *hdr, if (sess->srate_rx) { /* Convert from wall-clock time to timestamp units */ - hdr->ts_arrive = - (uint32_t)(tmr_jiffies() * sess->srate_rx / 1000); - - source_calc_jitter(mbr->s, hdr->ts, hdr->ts_arrive); + hdr->ts_arrive = tmr_jiffies() * sess->srate_rx / 1000; + + /* + * Calculate jitter only when the timestamp is different than + * last packet (see RTP FAQ + * https://www.cs.columbia.edu/~hgs/rtp/faq.html#jitter). + */ + if (hdr->ts != mbr->s->last_rtp_ts) + source_calc_jitter(mbr->s, hdr->ts, + (uint32_t)hdr->ts_arrive); } + mbr->s->last_rtp_ts = hdr->ts; mbr->s->rtp_rx_bytes += payload_size; }