Index: build/android/Android.mk
===================================================================
--- build/android/Android.mk	(revision 1010)
+++ build/android/Android.mk	(revision 1035)
@@ -141,5 +141,5 @@
 	$(LOCAL_PATH)/../../../externals/speex/include \
 	$(LOCAL_PATH)/../../../externals/build/speex \
-	$(LOCAL_PATH)/../../../externals/gsm/ \
+	$(LOCAL_PATH)/../../../externals/gsm/inc \
 	$(LOCAL_PATH)/../../../externals/ffmpeg
 
Index: src/gsm.c
===================================================================
--- src/gsm.c	(revision 911)
+++ src/gsm.c	(revision 1035)
@@ -19,6 +19,9 @@
 
 #include "mediastreamer2/msfilter.h"
-
+#ifdef ANDROID
+#include "gsm.h"
+#else
 #include <gsm/gsm.h>
+#endif
 
 #ifdef _MSC_VER
Index: src/msandroid.cpp
===================================================================
--- src/msandroid.cpp	(revision 1033)
+++ src/msandroid.cpp	(revision 1028)
@@ -22,5 +22,4 @@
 #include "mediastreamer2/mssndcard.h"
 #include "mediastreamer2/msfilter.h"
-#include "mediastreamer2/msticker.h"
 #include <jni.h>
 
@@ -107,4 +106,5 @@
 	bool			started;
 	ms_mutex_t		mutex;
+	queue_t			rq;
 	ms_thread_t     thread_id;
 	int	buff_size; /*buffer size in bytes*/
@@ -112,5 +112,5 @@
 
 
-static int get_rate(MSFilter *f, void *data){
+int get_rate(MSFilter *f, void *data){
 	msandroid_sound_data *d=(msandroid_sound_data*)f->data;
 	*(int*)data=d->rate;
@@ -119,5 +119,5 @@
 
 
-static int set_nchannels(MSFilter *f, void *arg){
+int set_nchannels(MSFilter *f, void *arg){
 	ms_debug("set_nchannels %d", *((int*)arg));
 	msandroid_sound_data *d=(msandroid_sound_data*)f->data;
@@ -130,5 +130,5 @@
 
 /***********************************read filter********************/
-static int set_read_rate(MSFilter *f, void *arg){
+int set_read_rate(MSFilter *f, void *arg){
 	int proposed_rate = *((int*)arg);
 	ms_debug("set_rate %d",proposed_rate);
@@ -154,18 +154,27 @@
 class msandroid_sound_read_data : public msandroid_sound_data{
 public:
-	msandroid_sound_read_data() : audio_record(0),audio_record_class(0),read_buff(0),read_chunk_size(0) {
-		ms_bufferizer_init(&rb);
-	}
-	~msandroid_sound_read_data() {
-		ms_bufferizer_uninit (&rb);
+	msandroid_sound_read_data() : audio_record(0),audio_record_class(0),read_buff(0),read_chunk_size(0),ticker_count(0) {
+		qinit(&rq);
+		ms_mutex_init(&mutex,NULL);
+	};
+~msandroid_sound_read_data() {
+		ms_mutex_lock(&mutex);
+		flushq(&rq,0);
+		ms_mutex_unlock(&mutex);
+		ms_mutex_destroy(&mutex);
 	}
 	jobject			audio_record;
 	jclass 			audio_record_class;
 	jbyteArray		read_buff;
-	MSBufferizer 		rb;
-	int			read_chunk_size;
+	ms_mutex_t		mutex;
+	queue_t			rq;
+	int				read_chunk_size;
+	unsigned long 	ticker_count;
+
+
+
 };
 
-static void* msandroid_read_cb(msandroid_sound_read_data* d) {
+void* msandroid_read_cb(msandroid_sound_read_data* d) {
 	mblk_t *m;
 	int nread;
@@ -201,5 +210,5 @@
 		m->b_wptr += nread;
 		ms_mutex_lock(&d->mutex);
-		ms_bufferizer_put (&d->rb,m);
+		putq(&d->rq,m);
 		ms_mutex_unlock(&d->mutex);
 	};
@@ -243,5 +252,5 @@
 	}
 	d->buff_size = jni_env->CallStaticIntMethod(d->audio_record_class,min_buff_size_id,d->rate,2/*CHANNEL_CONFIGURATION_MONO*/,2/*  ENCODING_PCM_16BIT */);
-	d->read_chunk_size = d->buff_size;
+	d->read_chunk_size = (d->rate*(d->bits/8)*d->nchannels)*0.02;
 
 	if (d->buff_size > 0) {
@@ -348,18 +357,19 @@
 	msandroid_sound_read_data *d=(msandroid_sound_read_data*)f->data;
 	mblk_t *m;
-	int nbytes=0.02*(float)d->rate*2.0*(float)d->nchannels;
-
-	// output a buffer only every 2 ticks + alpha
-	if ((f->ticker->time % 20)==0 || (f->ticker->time % 510)==0){
-		mblk_t *om=allocb(nbytes,0);
-		int err;
-		ms_mutex_lock(&d->mutex);
-		err=ms_bufferizer_read(&d->rb,om->b_wptr,nbytes);
-		ms_mutex_unlock(&d->mutex);
-		if (err==nbytes){
-			om->b_wptr+=nbytes;
-			ms_queue_put(f->outputs[0],om);
-		}else freemsg(om);
-	}
+	
+	ms_mutex_lock(&d->mutex);
+	m=peekq(&d->rq);
+	
+	if (d->ticker_count!=0 || m!=NULL){
+		//start incrementing the first time we have a packet
+		d->ticker_count++;
+	}
+	// get buffer only every 2 ticks + alpha
+	if (m != NULL && ((d->ticker_count % 2)==1 ||   (d->ticker_count % 15)==0 ) ) {
+		m=getq(&d->rq);
+		ms_queue_put(f->outputs[0],m);
+	}
+	ms_mutex_unlock(&d->mutex);
+
 }
 
@@ -391,5 +401,5 @@
 
 /***********************************write filter********************/
-static int set_write_rate(MSFilter *f, void *arg){
+int set_write_rate(MSFilter *f, void *arg){
 	int proposed_rate = *((int*)arg);
 	ms_debug("set_rate %d",proposed_rate);
@@ -441,5 +451,5 @@
 };
 
-static void* msandroid_write_cb(msandroid_sound_write_data* d) {
+void* msandroid_write_cb(msandroid_sound_write_data* d) {
 	JNIEnv 			*jni_env = 0;
 	jbyteArray 		write_buff;
@@ -505,5 +515,4 @@
 
 }
-
 void msandroid_sound_write_preprocess(MSFilter *f){
 	ms_debug("andsnd_write_preprocess");
@@ -647,13 +656,11 @@
 void msandroid_sound_write_process(MSFilter *f){
 	msandroid_sound_write_data *d=(msandroid_sound_write_data*)f->data;
-	
+	if (d->started == false) return;
 	mblk_t *m;
 	while((m=ms_queue_get(f->inputs[0]))!=NULL){
-		if (d->started){
-			ms_mutex_lock(&d->mutex);
-			ms_bufferizer_put(d->bufferizer,m);
-			ms_cond_signal(&d->cond);
-			ms_mutex_unlock(&d->mutex);
-		}else freemsg(m);
+		ms_mutex_lock(&d->mutex);
+		ms_bufferizer_put(d->bufferizer,m);
+		ms_cond_signal(&d->cond);
+		ms_mutex_unlock(&d->mutex);
 	}
 }
@@ -695,4 +702,3 @@
 
 	ms_snd_card_manager_register_desc(ms_snd_card_manager_get(),&msandroid_sound_card_desc);
-}
-	
+}	
Index: src/speexec.c
===================================================================
--- src/speexec.c	(revision 1034)
+++ src/speexec.c	(revision 1028)
@@ -31,11 +31,18 @@
 #endif
 
-//#define EC_DUMP 1
-
-#define EC_DUMP_PREFIX "/sdcard"
-
 static const int framesize=128;
 static const int ref_max_delay=60;
 
+#if 0
+typedef struct _BufferSizeEstimator{
+	float mean;
+	float jitter;
+}BufferSizeEstimator;
+
+void buffer_size_estimator_update(BufferSizeEstimator *bse, int size){
+	static const float smooth=0.04;
+}
+
+#endif
 
 typedef struct SpeexECState{
@@ -51,10 +58,5 @@
 	int delay_ms;
 	int tail_length_ms;
-#ifdef EC_DUMP
-	FILE *echofile;
-	FILE *reffile;
-#endif
 	bool_t using_silence;
-	bool_t echostarted;
 }SpeexECState;
 
@@ -72,17 +74,5 @@
 	s->den = NULL;
 	s->using_silence=FALSE;
-	s->echostarted=FALSE;
-
-#ifdef EC_DUMP
-	{
-		char *fname=ms_strdup_printf("%s/msspeexec-%p-echo.raw", EC_DUMP_PREFIX,f);
-		s->echofile=fopen(fname,"w");
-		ms_free(fname);
-		fname=ms_strdup_printf("%s/msspeexec-%p-ref.raw", EC_DUMP_PREFIX,f);
-		s->reffile=fopen(fname,"w");
-		ms_free(fname);
-	}
-#endif
-	
+
 	f->data=s;
 }
@@ -92,10 +82,4 @@
 	ms_bufferizer_uninit(&s->ref);
 	ms_bufferizer_uninit(&s->delayed_ref);
-#ifdef EC_DUMP
-	if (s->echofile)
-		fclose(s->echofile);
-	if (s->reffile)
-		fclose(s->reffile);
-#endif
 	ms_free(s);
 }
@@ -107,5 +91,4 @@
 	mblk_t *m;
 
-	s->echostarted=FALSE;
 	s->filterlength=(s->tail_length_ms*s->samplerate)/1000;
 	delay_samples=s->delay_ms*s->samplerate/1000;
@@ -133,17 +116,4 @@
 	int size;
 	
-	if (f->inputs[1]!=NULL){
-		int maxsize;
-		ms_bufferizer_put_from_queue (&s->echo,f->inputs[1]);
-		maxsize=ms_bufferizer_get_avail(&s->echo);
-		if (s->echostarted==FALSE && maxsize>0){
-			s->echostarted=TRUE;
-		}
-		if (maxsize>=s->ref_bytes_limit){
-			ms_message("ref_bytes_limit adjusted from %i to %i",s->ref_bytes_limit,maxsize);
-			s->ref_bytes_limit=maxsize;
-		}
-	}
-	
 	if (f->inputs[0]!=NULL){
 		while((refm=ms_queue_get(f->inputs[0]))!=NULL){
@@ -153,5 +123,12 @@
 		}
 	}
-	
+	if (f->inputs[1]!=NULL){
+		int maxsize;
+		ms_bufferizer_put_from_queue (&s->echo,f->inputs[1]);
+		if ((maxsize=ms_bufferizer_get_avail(&s->echo))>=s->ref_bytes_limit){
+			ms_message("ref_bytes_limit adjusted from %i to %i",s->ref_bytes_limit,maxsize);
+			s->ref_bytes_limit=maxsize;
+		}
+	}
 /*
 	ms_message("echo bytes=%i, ref bytes=%i",ms_bufferizer_get_avail(&s->echo),
@@ -180,10 +157,4 @@
 		oref->b_wptr+=nbytes;
 		ms_queue_put(f->outputs[0],oref);
-#ifdef EC_DUMP
-		if (s->reffile)
-			fwrite(ref,nbytes,1,s->reffile);
-		if (s->echofile)
-			fwrite(echo,nbytes,1,s->echofile);
-#endif
 		speex_echo_cancellation(s->ecstate,(short*)echo,(short*)ref,(short*)oecho->b_wptr);
 		speex_preprocess_run(s->den, (short*)oecho->b_wptr);
@@ -191,15 +162,4 @@
 		ms_queue_put(f->outputs[1],oecho);
 	}
-	if (!s->echostarted){
-		/*if we have not yet receive anything from the soundcard, bypass the reference signal*/
-		while (ms_bufferizer_get_avail(&s->ref)>=nbytes){
-			mblk_t *oref=allocb(nbytes,0);
-			ms_bufferizer_read(&s->ref,oref->b_wptr,nbytes);
-			oref->b_wptr+=nbytes;
-			ms_bufferizer_skip_bytes(&s->delayed_ref,nbytes);
-			ms_queue_put(f->outputs[0],oref);
-		}
-	}
-
 	/* do not accumulate too much reference signal */
 	if ((size=ms_bufferizer_get_avail(&s->ref))> (s->ref_bytes_limit+nbytes)) {
