From 7192fbfae700a3f71a82d3a0cdbf54f2890e243d Mon Sep 17 00:00:00 2001
From: Mark Spencer <markster@digium.com>
Date: Sat, 20 Mar 2004 05:19:45 +0000
Subject: [PATCH] Round offset timestamps to nearest 20ms to clean up numbers.

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@2486 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 channels/chan_iax2.c | 10 ++++++++--
 rtp.c                |  1 +
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c
index 657687e1fc..172ba9ac58 100755
--- a/channels/chan_iax2.c
+++ b/channels/chan_iax2.c
@@ -2562,6 +2562,8 @@ static unsigned int fix_peerts(struct iax2_peer *peer, int callno, unsigned int
 	if (!iaxs[callno]->rxcore.tv_sec && !iaxs[callno]->rxcore.tv_usec) {
 		/* Initialize rxcore time if appropriate */
 		gettimeofday(&iaxs[callno]->rxcore, NULL);
+		/* Round to nearest 20ms */
+		iaxs[callno]->rxcore.tv_usec -= iaxs[callno]->rxcore.tv_usec % 20000;
 	}
 	/* Calculate difference between trunk and channel */
 	ms = (peer->rxtrunktime.tv_sec - iaxs[callno]->rxcore.tv_sec) * 1000 + 
@@ -2574,8 +2576,11 @@ static unsigned int calc_timestamp(struct chan_iax2_pvt *p, unsigned int ts, str
 {
 	struct timeval tv;
 	unsigned int ms;
-	if (!p->offset.tv_sec && !p->offset.tv_usec)
+	if (!p->offset.tv_sec && !p->offset.tv_usec) {
 		gettimeofday(&p->offset, NULL);
+		/* Round to nearest 20ms */
+		p->offset.tv_usec -= p->offset.tv_usec % 20000;
+	}
 	/* If the timestamp is specified, just send it as is */
 	if (ts)
 		return ts;
@@ -2586,8 +2591,9 @@ static unsigned int calc_timestamp(struct chan_iax2_pvt *p, unsigned int ts, str
 		ms = (tv.tv_sec - p->offset.tv_sec) * 1000 + (tv.tv_usec - p->offset.tv_usec) / 1000;
 	}
 	/* We never send the same timestamp twice, so fudge a little if we must */
-	if (ms <= p->lastsent)
+	if (ms <= p->lastsent) {
 		ms = p->lastsent + 1;
+	}
 	p->lastsent = ms;
 	return ms;
 }
diff --git a/rtp.c b/rtp.c
index a1e06f75d9..50ec19acb6 100755
--- a/rtp.c
+++ b/rtp.c
@@ -366,6 +366,7 @@ static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int t
 		gettimeofday(&rtp->rxcore, NULL);
 		rtp->rxcore.tv_sec -= timestamp / 8000;
 		rtp->rxcore.tv_usec -= (timestamp % 8000) * 125;
+		rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 20000;
 		if (rtp->rxcore.tv_usec < 0) {
 			/* Adjust appropriately if necessary */
 			rtp->rxcore.tv_usec += 1000000;
-- 
GitLab