Index: pjproject/trunk/pjmedia/src/pjmedia/delaybuf.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia/delaybuf.c	(revision 1981)
+++ pjproject/trunk/pjmedia/src/pjmedia/delaybuf.c	(revision 1982)
@@ -217,22 +217,6 @@
 	b->buf_cnt -= erase_cnt;
 
-	PJ_LOG(5,(b->obj_name,"Overflow, %d samples reduced, "
-		  "buf_cnt=%d", erase_cnt, b->buf_cnt));
-    }
-
-    /* Shrinking failed or erased count is less than requested,
-     * delaybuf needs to drop eldest samples, this is bad since the voice
-     * samples may not have smooth transition.
-     */
-    if (b->buf_cnt + b->samples_per_frame > b->max_cnt) {
-	erase_cnt = b->buf_cnt + b->samples_per_frame - b->max_cnt;
-
-	b->buf_cnt -= erase_cnt;
-
-	/* Shift get_pos forward */
-	b->get_pos = (b->get_pos + erase_cnt) % b->max_cnt;
-
-	PJ_LOG(4,(b->obj_name,"Shrinking failed or insufficient, dropping"
-		  " %d eldest samples, buf_cnt=%d", erase_cnt, b->buf_cnt));
+	PJ_LOG(5,(b->obj_name,"%d samples reduced, buf_cnt=%d", 
+	       erase_cnt, b->buf_cnt));
     }
 }
@@ -287,6 +271,6 @@
 
 	shrink_buffer(b, erase_cnt);
-	PJ_LOG(5,(b->obj_name,"Buffer size adjusted from %d to %d",
-		  old_buf_cnt, b->buf_cnt));
+	PJ_LOG(4,(b->obj_name,"Buffer size adjusted from %d to %d (eff_cnt=%d)",
+	          old_buf_cnt, b->buf_cnt, b->eff_cnt));
     }
 }
@@ -312,9 +296,27 @@
     if (b->buf_cnt + b->samples_per_frame > b->max_cnt)
     {
+	unsigned erase_cnt;
+	
 	/* shrink one frame or just the diff? */
-	//unsigned erase_cnt = b->samples_per_frame;
-	unsigned erase_cnt = b->buf_cnt + b->samples_per_frame - b->max_cnt;
+	//erase_cnt = b->samples_per_frame;
+	erase_cnt = b->buf_cnt + b->samples_per_frame - b->max_cnt;
 
 	shrink_buffer(b, erase_cnt);
+
+	/* Check if shrinking failed or erased count is less than requested,
+	 * delaybuf needs to drop eldest samples, this is bad since the voice
+	 * samples get rough transition which may produce tick noise.
+	 */
+	if (b->buf_cnt + b->samples_per_frame > b->max_cnt) {
+	    erase_cnt = b->buf_cnt + b->samples_per_frame - b->max_cnt;
+
+	    b->buf_cnt -= erase_cnt;
+
+	    /* Shift get_pos forward */
+	    b->get_pos = (b->get_pos + erase_cnt) % b->max_cnt;
+
+	    PJ_LOG(4,(b->obj_name,"Shrinking failed or insufficient, dropping"
+		      " %d eldest samples, buf_cnt=%d", erase_cnt, b->buf_cnt));
+	}
     }
 
@@ -355,5 +357,5 @@
     if (b->buf_cnt < b->samples_per_frame) {
 
-	PJ_LOG(5,(b->obj_name,"Underflow, buf_cnt=%d, will generate 1 frame",
+	PJ_LOG(4,(b->obj_name,"Underflow, buf_cnt=%d, will generate 1 frame",
 		  b->buf_cnt));
 
@@ -385,16 +387,25 @@
 
 	} else {
-	    /* Give all what delay buffer has, then pad zeroes */
+	    /* Give all what delay buffer has, then pad with zeroes */
 	    PJ_LOG(4,(b->obj_name,"Error generating frame, status=%d", 
 		      status));
 
-	    pjmedia_copy_samples(frame, &b->frame_buf[b->get_pos], b->buf_cnt);
+	    if (b->get_pos + b->buf_cnt <= b->max_cnt) {
+		pjmedia_copy_samples(frame, &b->frame_buf[b->get_pos], b->buf_cnt);
+	    } else {
+		int remainder = b->get_pos + b->buf_cnt - b->max_cnt;
+
+		pjmedia_copy_samples(frame, &b->frame_buf[b->get_pos],
+				     b->buf_cnt - remainder);
+		pjmedia_copy_samples(&frame[b->buf_cnt - remainder],
+				     &b->frame_buf[0], remainder);
+	    }
+
 	    pjmedia_zero_samples(&frame[b->buf_cnt], 
 				 b->samples_per_frame - b->buf_cnt);
-	    b->get_pos += b->buf_cnt;
+
+	    /* Delay buf is empty */
+	    b->get_pos = b->put_pos = 0;
 	    b->buf_cnt = 0;
-
-	    /* Consistency checking */
-	    pj_assert(b->get_pos == b->put_pos);
 
 	    pj_lock_release(b->lock);
@@ -404,6 +415,16 @@
     }
 
-    pjmedia_copy_samples(frame, &b->frame_buf[b->get_pos],
-			 b->samples_per_frame);
+    if (b->get_pos + b->samples_per_frame <= b->max_cnt) {
+	pjmedia_copy_samples(frame, &b->frame_buf[b->get_pos], 
+			     b->samples_per_frame);
+    } else {
+	int remainder = b->get_pos + b->samples_per_frame - b->max_cnt;
+
+	pjmedia_copy_samples(frame, &b->frame_buf[b->get_pos],
+			     b->samples_per_frame - remainder);
+	pjmedia_copy_samples(&frame[b->samples_per_frame - remainder],
+			     &b->frame_buf[0], 
+			     remainder);
+    }
 
     b->get_pos = (b->get_pos + b->samples_per_frame) % b->max_cnt;
