Index: /pjproject/trunk/pjmedia/src/pjmedia/jbuf.c
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia/jbuf.c	(revision 1886)
+++ /pjproject/trunk/pjmedia/src/pjmedia/jbuf.c	(revision 1887)
@@ -30,4 +30,6 @@
 #define THIS_FILE   "jbuf.c"
 
+#define SAFE_SHRINKING_DIFF	2
+#define USE_PREFETCH_BUFFERING	0
 
 typedef struct jb_framelist_t
@@ -55,9 +57,6 @@
     int		    jb_level;		  // delay between source & destination
 					  // (calculated according of the number of get/put operations)
-    int		    jb_last_level;	  // last delay	
-    int		    jb_last_jitter;	  // last jitter calculated
-    int		    jb_max_hist_jitter;   // max jitter	during the last	jitter calculations
+    int		    jb_max_hist_level;    // max level during the last level calculations
     int		    jb_stable_hist;	  // num of times the delay has	been lower then	the prefetch num
-    unsigned	    jb_op_count;	  // of of operations.
     int		    jb_last_op;		  // last operation executed on	the framelist->flist_buffer (put/get)
     int		    jb_last_seq_no;	  // seq no. of	the last frame inserted	to the framelist->flist_buffer
@@ -84,5 +83,5 @@
  * second.
  */
-#if 0
+#if 1
 #  define TRACE__(args)	    PJ_LOG(4,args)
 #else
@@ -305,6 +304,4 @@
     jb->jb_last_seq_no	 = -1;
     jb->jb_level	 = 0;
-    jb->jb_last_level	 = 0;
-    jb->jb_last_jitter	 = 0;
     jb->jb_last_op	 = JB_OP_INIT;
     jb->jb_prefetch	 = PJ_MIN(PJMEDIA_JB_DEFAULT_INIT_DELAY,max_count*4/5);
@@ -314,5 +311,5 @@
     jb->jb_stable_hist	 = 0;
     jb->jb_status	 = JB_STATUS_INITIALIZING;
-    jb->jb_max_hist_jitter = 0;
+    jb->jb_max_hist_level = 0;
     jb->jb_max_count	 = max_count;
 
@@ -367,11 +364,9 @@
     jb->jb_last_seq_no	 = -1;
     jb->jb_level	 = 0;
-    jb->jb_last_level	 = 0;
-    jb->jb_last_jitter	 = 0;
     jb->jb_last_op	 = JB_OP_INIT;
     jb->jb_prefetch_cnt	 = 0;
     jb->jb_stable_hist	 = 0;
     jb->jb_status	 = JB_STATUS_INITIALIZING;
-    jb->jb_max_hist_jitter = 0;
+    jb->jb_max_hist_level = 0;
 
     jb_framelist_remove_head(&jb->jb_framelist, 
@@ -389,79 +384,78 @@
 static void jbuf_calculate_jitter(pjmedia_jbuf *jb)
 {
-    unsigned stable_history_limit;
-
-    stable_history_limit = 1000 / jb->jb_frame_ptime;
-
-    jb->jb_last_jitter = PJ_ABS(jb->jb_level-jb->jb_last_level);
-    jb->jb_last_level = jb->jb_level;
-    jb->jb_max_hist_jitter = PJ_MAX(jb->jb_max_hist_jitter,jb->jb_last_jitter);
-    jb->jb_op_count++;
-
-    if (jb->jb_last_jitter < jb->jb_prefetch) {
-	jb->jb_stable_hist += jb->jb_last_jitter;
-	if (jb->jb_stable_hist > (int)stable_history_limit) {
-	    int seq_diff = (jb->jb_prefetch - jb->jb_max_hist_jitter)/3;
-	    if (seq_diff<1) seq_diff = 1;
-
-	    jb->jb_prefetch -= seq_diff;
-	    if (jb->jb_prefetch < jb->jb_min_prefetch) 
-		jb->jb_prefetch = jb->jb_min_prefetch;
-
+    int diff;
+
+    /* Only apply burst-level calculation on PUT operation since if VAD is 
+     * active the burst-level may not be accurate.
+     */
+    if (jb->jb_last_op == JB_OP_PUT) {
+
+	jb->jb_max_hist_level = PJ_MAX(jb->jb_max_hist_level,jb->jb_level);
+
+	/* Level is decreasing */
+	if (jb->jb_level < jb->jb_prefetch) {
+
+	    static const int stable_history_limit = 100;
+	    
+	    jb->jb_stable_hist++;
+	    
+	    /* Only update the prefetch if 'stable' condition is reached 
+	     * (not just short time impulse)
+	     */
+	    if (jb->jb_stable_hist > (int)stable_history_limit) {
+		diff = (jb->jb_prefetch - jb->jb_max_hist_level) / 3;
+
+		if (diff < 1)
+		    diff = 1;
+
+		jb->jb_prefetch -= diff;
+		if (jb->jb_prefetch < jb->jb_min_prefetch) 
+		    jb->jb_prefetch = jb->jb_min_prefetch;
+
+		TRACE__((jb->name.ptr,"jb updated(1), prefetch=%d, size=%d", 
+			 jb->jb_prefetch, jb_framelist_size(&jb->jb_framelist)));
+
+		jb->jb_stable_hist = 0;
+		jb->jb_max_hist_level = 0;
+	    }
+	}
+
+	/* Level is increasing */
+	else if (jb->jb_level > jb->jb_prefetch) {
+
+	    /* Instaneous set prefetch */
+	    jb->jb_prefetch = PJ_MIN(jb->jb_max_hist_level,
+				     (int)(jb->jb_max_count*4/5));
+	    if (jb->jb_prefetch > jb->jb_max_prefetch)
+		jb->jb_prefetch = jb->jb_max_prefetch;
 	    jb->jb_stable_hist = 0;
-	    jb->jb_max_hist_jitter = 0;
-
-	    TRACE__((THIS_FILE,"jb updated(1), prefetch=%d, size=%d", 
+	    jb->jb_max_hist_level = 0;
+
+	    TRACE__((jb->name.ptr,"jb updated(2), prefetch=%d, size=%d", 
 		     jb->jb_prefetch, jb_framelist_size(&jb->jb_framelist)));
-
-	    /* These code is used to shorten the delay in the jitter buffer.
-
-	    if (jb->jb_op_count >= stable_history_limit*2 &&
-		(int)jb_framelist_size(&jb->jb_framelist) > jb->jb_prefetch+2)
-	    {
-		jb_framelist_remove_head(&jb->jb_framelist,1);
-
-		PJ_LOG(5,(jb->name.ptr, 
-			  "jbuf optimizing, prefetch: %d, size=%d", 
-			  jb->jb_prefetch,
-			  jb_framelist_size(&jb->jb_framelist)));
-		jb->jb_op_count = 0;
-	    }
-	    */
-
-	}
-    } else {
-	jb->jb_prefetch = PJ_MIN(jb->jb_last_jitter,
-				 (int)(jb->jb_max_count*4/5));
-	if (jb->jb_prefetch > jb->jb_max_prefetch)
-	    jb->jb_prefetch = jb->jb_max_prefetch;
-	jb->jb_stable_hist = 0;
-	jb->jb_max_hist_jitter = 0;
-
-	TRACE__((THIS_FILE,"jb updated(2), prefetch=%d, size=%d", 
-	         jb->jb_prefetch, jb_framelist_size(&jb->jb_framelist)));
-
-	/* These code is used to shorten the delay in the jitter buffer
-	   when the current size is larger than the prefetch. But it does
-	   not really work when the stream supports multiple frames, since
-	   the size may grow only temporarily.
-
-	if (jb->jb_op_count >= stable_history_limit * 2) {
-	    if ((int)jb_framelist_size(&jb->jb_framelist) > jb->jb_prefetch+2) 
-	    {
-		jb_framelist_remove_head(&jb->jb_framelist,1);
-
-		PJ_LOG(5,(jb->name.ptr, 
-			  "jbuf optimizing prefetch: %d, size=%d",
-			  jb->jb_prefetch,
-			  jb_framelist_size(&jb->jb_framelist)));
-	    }
-
-	    jb->jb_op_count = 0;
-	}
-	*/
-    }
-}
-
-static void jbuf_update(pjmedia_jbuf *jb, int oper)
+	}
+
+	/* Level is unchanged */
+	else {
+	    jb->jb_stable_hist = 0;
+	}
+    }
+
+    /* These code is used for shortening the delay in the jitter buffer. */
+    diff = jb_framelist_size(&jb->jb_framelist) - jb->jb_prefetch;
+    if (diff > SAFE_SHRINKING_DIFF) {
+	
+	/* Drop some frames so jitter buffer size should be == jb_prefetch */
+	jb_framelist_remove_head(&jb->jb_framelist, diff);
+
+	TRACE__((jb->name.ptr, 
+		 "JB shrinking %d frames, size=%d", diff,
+		 jb_framelist_size(&jb->jb_framelist)));
+    }
+
+    jb->jb_level = 0;
+}
+
+PJ_INLINE(void) jbuf_update(pjmedia_jbuf *jb, int oper)
 {
     if(jb->jb_last_op != oper) {
@@ -490,6 +484,4 @@
 	jb->jb_status = JB_STATUS_PROCESSING;
 	jb->jb_level = 0;
-	jb->jb_last_level = 0;
-	jb->jb_last_jitter = 0;
     } else {
 	jbuf_update(jb, JB_OP_PUT);
@@ -538,7 +530,9 @@
     pjmedia_jb_frame_type ftype;
 
-    jb->jb_level--;
+    jb->jb_level++;
 
     jbuf_update(jb, JB_OP_GET);
+
+#if USE_PREFETCH_BUFFERING
 
     if (jb_framelist_size(&jb->jb_framelist) == 0) {
@@ -561,4 +555,6 @@
     }
 
+#endif
+
     /* Retrieve a frame from frame list */
     if (jb_framelist_get(&jb->jb_framelist,frame,size,&ftype) == PJ_FALSE) {
Index: /pjproject/trunk/pjmedia/src/pjmedia/stream.c
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia/stream.c	(revision 1886)
+++ /pjproject/trunk/pjmedia/src/pjmedia/stream.c	(revision 1887)
@@ -1459,15 +1459,18 @@
 	jb_min_pre = info->jb_min_pre;
     else
-	jb_min_pre = 60 / stream->codec_param.info.frm_ptime;
+	//jb_min_pre = 60 / stream->codec_param.info.frm_ptime;
+	jb_min_pre = 1;
 
     if (info->jb_max_pre > 0)
 	jb_max_pre = info->jb_max_pre;
     else
-	jb_max_pre = 240 / stream->codec_param.info.frm_ptime;
+	//jb_max_pre = 240 / stream->codec_param.info.frm_ptime;
+	jb_max_pre = jb_max * 4 / 5;
 
     if (info->jb_init >= 0)
 	jb_init = info->jb_init;
     else
-	jb_init = (jb_min_pre + jb_max_pre) / 2;
+	//jb_init = (jb_min_pre + jb_max_pre) / 2;
+	jb_init = jb_min_pre;
 
 
