Commit 02115997 authored by Simon Morlat's avatar Simon Morlat

ec works quite well on android

parent 293a484a
......@@ -25,6 +25,8 @@
#include "mediastreamer2/msjava.h"
#include <jni.h>
static const float sndwrite_flush_threshold=0.050; //ms
static void sound_read_setup(MSFilter *f);
static void set_high_prio(void){
......@@ -490,7 +492,7 @@ static void* msandroid_write_cb(msandroid_sound_write_data* d) {
ms_mutex_lock(&d->mutex);
while((bufferizer_size = ms_bufferizer_get_avail(d->bufferizer)) >= d->write_chunk_size) {
if (bufferizer_size > (d->rate*(d->bits/8)*d->nchannels)*.250) { //250 ms
if (bufferizer_size > (d->rate*(d->bits/8)*d->nchannels)*sndwrite_flush_threshold) {
ms_warning("we are late [%i] bytes, flushing",bufferizer_size);
ms_bufferizer_flush(d->bufferizer);
......
......@@ -390,12 +390,15 @@ MSFilterDesc ms_speex_enc_desc={
#endif
static const int plc_max=10;/*10 frames of plc, no more*/
typedef struct DecState{
int rate;
int penh;
int frsz;
uint64_t sample_time;
void *state;
int plc_count;
bool_t plc;
} DecState;
......@@ -407,6 +410,7 @@ static void dec_init(MSFilter *f){
s->penh=1;
s->sample_time=0;
s->plc=1;
s->plc_count=0;
f->data=s;
}
......@@ -480,16 +484,12 @@ static void dec_process(MSFilter *f){
while((im=ms_queue_get(f->inputs[0]))!=NULL){
int rem_bits=(im->b_wptr-im->b_rptr)*8;
if (s->sample_time==0){
s->sample_time=f->ticker->time;
}
if (!bits_initd) {
speex_bits_init(&bits);
bits_initd=TRUE;
}else speex_bits_reset(&bits);
speex_bits_read_from(&bits,(char*)im->b_rptr,im->b_wptr-im->b_rptr);
/* support for multiple frame in one RTP packet */
......@@ -500,7 +500,13 @@ static void dec_process(MSFilter *f){
if (err==0){
ms_queue_put(f->outputs[0],om);
if (s->sample_time==0) s->sample_time=f->ticker->time;
s->sample_time+=20;
if (s->plc_count>0){
ms_warning("Did speex packet loss concealment during %i ms",s->plc_count*20);
s->plc_count=0;
}
}else {
if (err==-1)
ms_warning("speex end of stream");
......@@ -511,15 +517,19 @@ static void dec_process(MSFilter *f){
}while((rem_bits= speex_bits_remaining(&bits))>10);
freemsg(im);
}
if (s->plc && s->sample_time!=0 && f->ticker->time>s->sample_time){
if (s->plc && s->sample_time!=0 && f->ticker->time>=s->sample_time){
/* we should output a frame but no packet were decoded
thus do packet loss concealment*/
om=allocb(bytes,0);
err=speex_decode_int(s->state,NULL,(int16_t*)om->b_wptr);
om->b_wptr+=bytes;
ms_queue_put(f->outputs[0],om);
ms_warning("Doing speex packet loss concealment.");
s->sample_time+=20;
s->plc_count++;
if (s->plc_count>=plc_max){
s->sample_time=0;
}
}
if (bits_initd) speex_bits_destroy(&bits);
}
......@@ -545,7 +555,8 @@ MSFilterDesc ms_speex_dec_desc={
dec_process,
dec_postprocess,
dec_uninit,
dec_methods
dec_methods,
MS_FILTER_IS_PUMP
};
#else
......@@ -563,10 +574,12 @@ MSFilterDesc ms_speex_dec_desc={
.postprocess=dec_postprocess,
.process=dec_process,
.uninit=dec_uninit,
.methods=dec_methods
.methods=dec_methods,
.flags=MS_FILTER_IS_PUMP
};
#endif
MS_FILTER_DESC_EXPORT(ms_speex_dec_desc)
MS_FILTER_DESC_EXPORT(ms_speex_enc_desc)
......@@ -193,21 +193,20 @@ static void speex_ec_process(MSFilter *f){
diff=((int)s->ref_bufsize_ms) - s->delay_ms;
idiff=((int)ref_bufsize_ms) - s->delay_ms;
threshold=s->tail_length_ms/4;
drift=diff/4;
drift=diff;
if (diff > threshold && ref_bufsize_ms>idiff) {
nbytes=2*(int)((drift*s->samplerate)/1000.0);
ms_warning("threshold=%i, tail_length_ms=%i",threshold,s->tail_length_ms);
ms_warning("Averaged reference bufsize is %f while expected delay is %i, diff=%i, need to drop %i bytes",s->ref_bufsize_ms,s->delay_ms,diff,nbytes);
ms_bufferizer_skip_bytes(&s->delayed_ref,nbytes);
s->ref_bufsize_ms-=threshold;
s->ref_bufsize_ms=s->delay_ms;
}else if ((diff < (-threshold)) && (idiff < (-threshold))){
nbytes=2*(int)((-drift*s->samplerate)/1000.0);
ms_warning("threshold=%i, tail_length_ms=%i",threshold,s->tail_length_ms);
ms_warning("Averaged reference bufsize is %f while expected delay is %i, diff=%i, need to add %i bytes",s->ref_bufsize_ms,s->delay_ms,diff,nbytes);
refm=allocb(nbytes,0);
memset(refm->b_wptr,0,nbytes);
refm->b_wptr+=nbytes;
ms_bufferizer_put(&s->delayed_ref,refm);
s->ref_bufsize_ms+=threshold;
s->ref_bufsize_ms=s->delay_ms;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment