[Asterisk-cvs] asterisk jitterbuf.c,1.11,1.12 jitterbuf.h,1.4,1.5

markster at lists.digium.com markster at lists.digium.com
Wed May 18 20:29:25 CDT 2005


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

Modified Files:
	jitterbuf.c jitterbuf.h 
Log Message:
More jitter buffer fixes for large resync (bug #4311)


Index: jitterbuf.c
===================================================================
RCS file: /usr/cvsroot/asterisk/jitterbuf.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- jitterbuf.c	15 May 2005 00:03:17 -0000	1.11
+++ jitterbuf.c	19 May 2005 00:34:28 -0000	1.12
@@ -106,14 +106,37 @@
 }
 #endif
 
-static void history_put(jitterbuf *jb, long ts, long now) 
+static int history_put(jitterbuf *jb, long ts, long now, long ms) 
 {
-	long delay = now - ts;
+	long delay = now - (ts - jb->info.resync_offset);
+	long threshold = 2 * jb->info.jitter + jb->info.resync_threshold;
 	long kicked;
 
 	/* don't add special/negative times to history */
 	if (ts <= 0) 
-		return;
+		return 0;
+
+	/* check for drastic change in delay */
+	if (jb->info.resync_threshold != -1) {
+		if (abs(delay - jb->info.last_delay) > threshold) {
+			jb->info.cnt_delay_discont++;
+			if (jb->info.cnt_delay_discont > 3) {
+				/* resync the jitterbuffer */
+				jb->info.cnt_delay_discont = 0;
+				jb->hist_ptr = 0;
+				jb->hist_maxbuf_valid = 0;
+
+				jb_warn("Resyncing the jb. last_delay %ld, this delay %ld, threshold %ld, new offset %ld\n", jb->info.last_delay, delay, threshold, ts - now);
+				jb->info.resync_offset = ts - now;
+				jb->info.last_delay = 0; /* after resync, frame is right on time */
+			} else {
+				return -1;
+			}
+		} else {
+			jb->info.last_delay = delay;
+			jb->info.cnt_delay_discont = 0;
+		}
+	}
 
 	kicked = jb->history[jb->hist_ptr & JB_HISTORY_SZ];
 
@@ -125,7 +148,7 @@
 	 * We do a number of comparisons, but it's probably still worthwhile, because it will usually
 	 * succeed, and should be a lot faster than going through all 500 packets in history */
 	if (!jb->hist_maxbuf_valid)
-		return;
+		return 0;
 
 	/* don't do this until we've filled history 
 	 * (reduces some edge cases below) */
@@ -149,13 +172,13 @@
 
 	/* if we got here, we don't need to invalidate, 'cause this delay didn't 
 	 * affect things */
-	return;
+	return 0;
 	/* end optimization */
 
 
 invalidate:
 	jb->hist_maxbuf_valid = 0;
-	return;
+	return 0;
 }
 
 static void history_calc_maxbuf(jitterbuf *jb) 
@@ -281,6 +304,7 @@
 {
 	jb_frame *frame;
 	jb_frame *p;
+	long resync_ts = ts - jb->info.resync_offset;
 
 	frame = jb->free;
 	if (frame) {
@@ -297,7 +321,7 @@
 	jb->info.frames_cur++;
 
 	frame->data = data;
-	frame->ts = ts;
+	frame->ts = resync_ts;
 	frame->ms = ms;
 	frame->type = type;
 
@@ -310,7 +334,7 @@
 		jb->frames = frame;
 		frame->next = frame;
 		frame->prev = frame;
-	} else if (ts < jb->frames->ts) {
+	} else if (resync_ts < jb->frames->ts) {
 		frame->next = jb->frames;
 		frame->prev = jb->frames->prev;
 
@@ -325,9 +349,9 @@
 		p = jb->frames;
 
 		/* frame is out of order */
-		if (ts < p->prev->ts) jb->info.frames_ooo++;
+		if (resync_ts < p->prev->ts) jb->info.frames_ooo++;
 
-		while (ts < p->prev->ts && p->prev != jb->frames) 
+		while (resync_ts < p->prev->ts && p->prev != jb->frames) 
 			p = p->prev;
 
 		frame->next = p;
@@ -474,7 +498,8 @@
 	if (type == JB_TYPE_VOICE) {
 		/* presently, I'm only adding VOICE frames to history and drift calculations; mostly because with the
 		 * IAX integrations, I'm sending retransmitted control frames with their awkward timestamps through */
-		history_put(jb,ts,now);
+		if (history_put(jb,ts,now,ms))
+			return JB_DROP;
 	}
 
 	queue_put(jb,data,type,ms,ts);
@@ -750,6 +775,7 @@
 	/* take selected settings from the struct */
 
 	jb->info.max_jitterbuf = settings->max_jitterbuf;
+ 	jb->info.resync_threshold = settings->resync_threshold;
 
 	return JB_OK;
 }

Index: jitterbuf.h
===================================================================
RCS file: /usr/cvsroot/asterisk/jitterbuf.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- jitterbuf.h	12 May 2005 19:01:03 -0000	1.4
+++ jitterbuf.h	19 May 2005 00:34:28 -0000	1.5
@@ -67,9 +67,13 @@
 	long last_voice_ms;	/* the duration of the last voice frame */
 	long silence_begin_ts;	/* the time of the last CNG frame, when in silence */
 	long last_adjustment;   /* the time of the last adjustment */
+ 	long last_delay;        /* the last now added to history */
+ 	long cnt_delay_discont;	/* the count of discontinuous delays */
+ 	long resync_offset;     /* the amount to offset ts to support resyncs */
 
 	/* settings */
 	long max_jitterbuf;	/* defines a hard clamp to use in setting the jitter buffer delay */
+ 	long resync_threshold;  /* the jb will resync when delay increases to (2 * jitter) + this param */
 } jb_info;
 
 typedef struct jb_frame {




More information about the svn-commits mailing list