[Asterisk-cvs] asterisk frame.c,1.25,1.26 rtp.c,1.51,1.52

markster at lists.digium.com markster at lists.digium.com
Fri Mar 12 22:58:46 CST 2004


Update of /usr/cvsroot/asterisk
In directory mongoose.digium.com:/tmp/cvs-serv17418

Modified Files:
	frame.c rtp.c 
Log Message:
Move timestamp around in RTP....  Gotta do iax2 eventually here...


Index: frame.c
===================================================================
RCS file: /usr/cvsroot/asterisk/frame.c,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -d -r1.25 -r1.26
--- frame.c	25 Feb 2004 18:43:55 -0000	1.25
+++ frame.c	13 Mar 2004 03:52:14 -0000	1.26
@@ -40,6 +40,7 @@
 	int optimizablestream;
 	float samplesperbyte;
 	struct ast_frame f;
+	struct timeval delivery;
 	char data[SMOOTHER_SIZE];
 	char framedata[SMOOTHER_SIZE + AST_FRIENDLY_OFFSET];
 	struct ast_frame *opt;
@@ -103,6 +104,9 @@
 	} else 
 		s->optimizablestream = 0;
 	memcpy(s->data + s->len, f->data, f->datalen);
+	/* If we're empty, reset delivery time */
+	if (!s->len)
+		s->delivery = f->delivery;
 	s->len += f->datalen;
 	return 0;
 }
@@ -129,12 +133,20 @@
 	s->f.offset = AST_FRIENDLY_OFFSET;
 	s->f.datalen = s->size;
 	s->f.samples = s->size * s->samplesperbyte;
+	s->f.delivery = s->delivery;
 	/* Fill Data */
 	memcpy(s->f.data, s->data, s->size);
 	s->len -= s->size;
 	/* Move remaining data to the front if applicable */
-	if (s->len) 
+	if (s->len) {
 		memmove(s->data, s->data + s->size, s->len);
+		s->delivery.tv_sec += (s->size * s->samplesperbyte) / 8000.0;
+		s->delivery.tv_usec += (((int)(s->size * s->samplesperbyte)) % 8000) * 125;
+		if (s->delivery.tv_usec > 1000000) {
+			s->delivery.tv_usec -= 1000000;
+			s->delivery.tv_sec += 1;
+		}
+	}
 	/* Return frame */
 	return &s->f;
 }
@@ -257,6 +269,7 @@
 	out->subclass = f->subclass;
 	out->datalen = f->datalen;
 	out->samples = f->samples;
+	out->delivery = f->delivery;
 	out->mallocd = AST_MALLOCD_HDR;
 	out->offset = AST_FRIENDLY_OFFSET;
 	out->data = buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET;

Index: rtp.c
===================================================================
RCS file: /usr/cvsroot/asterisk/rtp.c,v
retrieving revision 1.51
retrieving revision 1.52
diff -u -d -r1.51 -r1.52
--- rtp.c	5 Mar 2004 17:32:00 -0000	1.51
+++ rtp.c	13 Mar 2004 03:52:14 -0000	1.52
@@ -349,6 +349,26 @@
 	return &null_frame;
 }
 
+static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp)
+{
+	if (!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) {
+		gettimeofday(&rtp->rxcore, NULL);
+		rtp->rxcore.tv_usec -= timestamp / 8000;
+		rtp->rxcore.tv_usec -= (timestamp % 8000) * 125;
+		if (rtp->rxcore.tv_usec < 0) {
+			/* Adjust appropriately if necessary */
+			rtp->rxcore.tv_usec += 1000000;
+			rtp->rxcore.tv_sec -= 1;
+		}
+	}
+	tv->tv_sec = rtp->rxcore.tv_sec + timestamp / 8000;
+	tv->tv_usec = rtp->rxcore.tv_usec + (timestamp % 8000) * 125;
+	if (tv->tv_usec >= 1000000) {
+		tv->tv_usec -= 1000000;
+		tv->tv_sec += 1;
+	}
+}
+
 struct ast_frame *ast_rtp_read(struct ast_rtp *rtp)
 {
 	int res;
@@ -485,6 +505,7 @@
 			ast_log(LOG_NOTICE, "Unable to calculate samples for format %s\n", ast_getformatname(rtp->f.subclass));
 			break;
 		}
+		calc_rxstamp(&rtp->f.delivery, rtp, timestamp);
 	} else {
 		/* Video -- samples is # of samples vs. 90000 */
 		if (!rtp->lastividtimestamp)
@@ -817,19 +838,27 @@
 	free(rtp);
 }
 
-static unsigned int calc_txstamp(struct ast_rtp *rtp)
+static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
 {
 	struct timeval now;
 	unsigned int ms;
 	if (!rtp->txcore.tv_sec && !rtp->txcore.tv_usec) {
 		gettimeofday(&rtp->txcore, NULL);
 	}
-	gettimeofday(&now, NULL);
-	ms = (now.tv_sec - rtp->txcore.tv_sec) * 1000;
-	ms += (now.tv_usec - rtp->txcore.tv_usec) / 1000;
-	/* Use what we just got for next time */
-	rtp->txcore.tv_sec = now.tv_sec;
-	rtp->txcore.tv_usec = now.tv_usec;
+	if (delivery && (delivery->tv_sec || delivery->tv_usec)) {
+		/* Use previous txcore */
+		ms = (delivery->tv_sec - rtp->txcore.tv_usec) * 1000;
+		ms += (delivery->tv_usec - rtp->txcore.tv_usec) / 1000;
+		rtp->txcore.tv_sec = delivery->tv_sec;
+		rtp->txcore.tv_usec = delivery->tv_usec;
+	} else {
+		gettimeofday(&now, NULL);
+		ms = (now.tv_sec - rtp->txcore.tv_sec) * 1000;
+		ms += (now.tv_usec - rtp->txcore.tv_usec) / 1000;
+		/* Use what we just got for next time */
+		rtp->txcore.tv_sec = now.tv_sec;
+		rtp->txcore.tv_usec = now.tv_usec;
+	}
 	return ms;
 }
 
@@ -863,7 +892,7 @@
 	if (!rtp->them.sin_addr.s_addr)
 		return 0;
 
-	ms = calc_txstamp(rtp);
+	ms = calc_txstamp(rtp, NULL);
 	/* Default prediction */
 	pred = rtp->lastts + ms * 8;
 	
@@ -903,7 +932,7 @@
 	int pred;
 	int mark = 0;
 
-	ms = calc_txstamp(rtp);
+	ms = calc_txstamp(rtp, &f->delivery);
 	/* Default prediction */
 	if (f->subclass < AST_FORMAT_MAX_AUDIO) {
 		pred = rtp->lastts + ms * 8;
@@ -934,7 +963,7 @@
 			pred = rtp->lastts + g723_samples(f->data, f->datalen);
 			break;
 		case AST_FORMAT_SPEEX:
-		        pred = rtp->lastts + 160;
+		    pred = rtp->lastts + 160;
 			// assumes that the RTP packet contains one Speex frame
 			break;
 		default:




More information about the svn-commits mailing list