msconf.c 20 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

20

aymeric's avatar
aymeric committed
21 22 23 24
#ifdef HAVE_CONFIG_H
#include "mediastreamer-config.h"
#endif

aymeric's avatar
aymeric committed
25
#include "mediastreamer2/msfilter.h"
26
#include <math.h>
aymeric's avatar
aymeric committed
27 28 29 30 31

#if defined(_WIN32_WCE)
#define DISABLE_SPEEX
#endif

32 33 34 35
#ifndef HAVE_SPEEXDSP
#define DISABLE_SPEEX
#endif

aymeric's avatar
aymeric committed
36 37 38 39 40 41
#ifndef DISABLE_SPEEX
#include <speex/speex_preprocess.h>
#endif

#define CONF_NSAMPLES 160*4*4 /* (CONF_GRAN/2) */
#ifndef CONF_MAX_PINS
42
#define CONF_MAX_PINS 128
aymeric's avatar
aymeric committed
43 44
#endif

jehan's avatar
jehan committed
45

aymeric's avatar
aymeric committed
46
static const float coef=(float)0.01;
47

aymeric's avatar
aymeric committed
48 49 50 51 52
typedef struct Channel{
	MSBufferizer buff;
	int16_t input[CONF_NSAMPLES];
	bool_t has_contributed;
	bool_t is_used;
53 54

	int is_speaking;
55
	int count_speaking;
56

aymeric's avatar
aymeric committed
57 58 59 60 61 62 63 64 65 66 67
	int count;
	int missed;

	int stat_discarded;
	int stat_missed;
	int stat_processed;

#ifndef DISABLE_SPEEX
	SpeexPreprocessState *speex_pp;
#endif

68
	float energy;
69
	double average_psd;
70

aymeric's avatar
aymeric committed
71 72 73 74 75 76 77
} Channel;

typedef struct ConfState{
	Channel channels[CONF_MAX_PINS];
	int sum[CONF_NSAMPLES];
	int enable_directmode;
	int enable_vad;
78 79 80 81 82

	int enable_halfduplex;
	int vad_prob_start;
	int vad_prob_continue;

aymeric's avatar
aymeric committed
83
	int agc_level;
84
	int max_gain;
aymeric's avatar
aymeric committed
85 86 87 88 89 90 91 92 93 94
	int mix_mode;
	int samplerate;

	int adaptative_msconf_buf;
	int conf_gran;
	int conf_nsamples;
} ConfState;


static void channel_init(ConfState *s, Channel *chan, int pos){
95
#ifndef DISABLE_SPEEX
96 97
	float f;
	int val;
98
#endif
99
	memset(chan, 0, sizeof(Channel));
aymeric's avatar
aymeric committed
100 101 102
	ms_bufferizer_init(&chan->buff);
#ifndef DISABLE_SPEEX
	chan->speex_pp = speex_preprocess_state_init(s->conf_gran/2, s->samplerate);
103 104
	if (chan->speex_pp==NULL)
		return;
105

106 107 108 109 110 111 112 113 114
	/* configure sound card input on pin 0 */
	val=0;
	if (pos==0)
		val=1;

	if (s->enable_halfduplex>0 && pos%2==1)
		val=1;

	speex_preprocess_ctl(chan->speex_pp, SPEEX_PREPROCESS_SET_DENOISE, &val);
aymeric's avatar
aymeric committed
115 116
	val = -30;
	speex_preprocess_ctl(chan->speex_pp, SPEEX_PREPROCESS_SET_NOISE_SUPPRESS, &val);
117 118 119 120 121 122 123 124 125 126 127

	/* enable VAD only on incoming RTP stream */
	val=0;
	if (pos%2==1 || (pos==0 && s->enable_halfduplex>0))
	{
		val=1;
	}

	speex_preprocess_ctl(chan->speex_pp, SPEEX_PREPROCESS_SET_VAD, &val);
	if (s->vad_prob_start>0 && s->vad_prob_continue>0)
	{
aymeric's avatar
aymeric committed
128 129 130
		val = s->vad_prob_start; // xx%
		speex_preprocess_ctl(chan->speex_pp, SPEEX_PREPROCESS_SET_PROB_START, &val);
		val = s->vad_prob_continue; // xx%
131 132 133 134 135
		speex_preprocess_ctl(chan->speex_pp, SPEEX_PREPROCESS_SET_PROB_CONTINUE, &val);
	}

	/* enable AGC only on local soundcard */
	val=0;
136
	f=12000;
137 138 139 140 141
	if (s->agc_level>0 && pos==0)
		val=1;
	else if (pos==0 && s->enable_halfduplex>0)
		val=1;
	else if ( pos%2==1 && s->enable_halfduplex>0)
142
		val=1; //1; should not do that
143
	if (s->agc_level>0)
144
		f=(float)s->agc_level;
145 146 147 148


	speex_preprocess_ctl(chan->speex_pp, SPEEX_PREPROCESS_SET_AGC, &val);
	speex_preprocess_ctl(chan->speex_pp, SPEEX_PREPROCESS_SET_AGC_LEVEL, &f);
149

150
	val=s->max_gain;
151 152 153
	if ( pos%2==1 && s->enable_halfduplex>0)
		val=1;

154 155 156 157 158 159 160
	speex_preprocess_ctl(chan->speex_pp, SPEEX_PREPROCESS_SET_AGC_MAX_GAIN, &val);

	val=0;
#if 0
	val=1; // do more testing
#endif
	speex_preprocess_ctl(chan->speex_pp, SPEEX_PREPROCESS_SET_DEREVERB, &val);
161
	f=(float).4;
162
	speex_preprocess_ctl(chan->speex_pp, SPEEX_PREPROCESS_SET_DEREVERB_DECAY, &f);
163
	f=(float).3;
164 165
	speex_preprocess_ctl(chan->speex_pp, SPEEX_PREPROCESS_SET_DEREVERB_LEVEL, &f);

aymeric's avatar
aymeric committed
166 167 168 169 170
#endif
}

static void channel_uninit(Channel *chan){
	ms_bufferizer_uninit(&chan->buff);
aymeric's avatar
aymeric committed
171 172
	chan->is_speaking=0;
	chan->energy=0;
173
	chan->average_psd=0;
aymeric's avatar
aymeric committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
#ifndef DISABLE_SPEEX
	if (chan->speex_pp!=NULL)
	    speex_preprocess_state_destroy(chan->speex_pp);
	chan->speex_pp=NULL;
#endif
}

static void conf_init(MSFilter *f){
	ConfState *s=(ConfState *)ms_new0(ConfState,1);
	int i;
	s->samplerate=8000;
	s->conf_gran=((16 * s->samplerate) / 800) *2;
	s->conf_nsamples=s->conf_gran/2;
    for (i=0;i<CONF_MAX_PINS;i++)
		channel_init(s, &s->channels[i], i);
	s->enable_directmode=FALSE;
	s->enable_vad=TRUE;
	s->agc_level=0;
192
	s->max_gain=30;
aymeric's avatar
aymeric committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	s->mix_mode=TRUE;
	s->adaptative_msconf_buf=2;
	f->data=s;
}

static void conf_uninit(MSFilter *f){
	ConfState *s=(ConfState*)f->data;
	int i;
	for (i=0;i<CONF_MAX_PINS;i++)
		channel_uninit(&s->channels[i]);
	ms_free(f->data);
}

static void conf_preprocess(MSFilter *f){
	ConfState *s=(ConfState*)f->data;
	int i;
	for (i=0;i<CONF_MAX_PINS;i++)
	  {
	    s->channels[i].is_used=FALSE;
	    s->channels[i].missed=0;
	    s->channels[i].stat_discarded=0;
	    s->channels[i].stat_missed=0;
	    s->channels[i].stat_processed=0;
	  }
}

static bool_t should_process(MSFilter *f, ConfState *s){
	Channel *chan;
	int active_channel=0;
	int i;

	if (ms_bufferizer_get_avail(&(&s->channels[0])->buff)>s->conf_gran
	    && s->channels[0].is_used==FALSE)
	  {
	    /* soundread has just started */
	    s->channels[0].is_used=TRUE;
	  }
	else if (s->channels[0].is_used==FALSE)
	  {
	    return FALSE;
	  }

	/* count active channel */
	for (i=1;i<CONF_MAX_PINS;++i){
		chan=&s->channels[i];
		if (chan->is_used == TRUE)
		{
			active_channel++;
		}
	}

	if (active_channel<=1) /* disable mix mode when it's not needed */
		s->mix_mode = FALSE;
	else
		s->mix_mode = TRUE;

	if (s->enable_directmode==FALSE)
	{
		s->mix_mode = TRUE;
	}

	if (s->mix_mode == FALSE)
		return FALSE;

	if (ms_bufferizer_get_avail(&(&s->channels[0])->buff)>=s->conf_gran)
	{
		return TRUE;
	}
	return FALSE;
}

264
#ifndef DISABLE_SPEEX
265
static double powerspectrum_stat_beyond8K(struct Channel *chan){
aymeric's avatar
aymeric committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	spx_int32_t ps_size = 0;
	spx_int32_t *ps = NULL;
	double mystat = 0;
	float fftmul = 1.0 / (32768.0);
	int i;
	
	speex_preprocess_ctl(chan->speex_pp, SPEEX_PREPROCESS_GET_PSD_SIZE, &ps_size);
	ps = (spx_int32_t*)ortp_malloc(sizeof(spx_int32_t)*ps_size);
	speex_preprocess_ctl(chan->speex_pp, SPEEX_PREPROCESS_GET_PSD, ps);

	mystat = 0;
	for (i=ps_size/2;i < ps_size; i++) {
		double yp;
#if defined(__cplusplus)
		yp = sqrtf(sqrtf(static_cast<float>(ps[i]))) - 1.0f;
#else
		yp = sqrtf(sqrtf((float)(ps[i]))) - 1.0f;
#endif
		yp = yp * fftmul;
		yp = MIN(yp * 3000.0, 1.0);
286
		yp = (1 - yp) * (100 - 1.0f);
aymeric's avatar
aymeric committed
287 288 289
		mystat = yp + mystat;
	}

290 291
	mystat = (mystat*2)/ps_size;

aymeric's avatar
aymeric committed
292 293
	ortp_free(ps);

294 295 296
	/* values:
	Maximum: 108,064 low volume on high frequency.
	Decrease when volume increase. */
aymeric's avatar
aymeric committed
297

298
	/* return value between 0 and 108,064? */
299 300 301 302 303 304
	mystat = 108.064-mystat;

	chan->average_psd=(mystat*mystat*coef) + (1.0-coef)*chan->average_psd;
	//ms_message("average power spectrum on half highest values ONLY: stat=%.3lf", chan->average_psd);

	return mystat;
305 306 307
}
#endif

308
static void conf_sum(MSFilter *f, ConfState *s){
aymeric's avatar
aymeric committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
	int i,j;
	Channel *chan;
	memset(s->sum,0,s->conf_nsamples*sizeof(int));

	chan=&s->channels[0];
	if (s->adaptative_msconf_buf*s->conf_gran<ms_bufferizer_get_avail(&chan->buff))
	{
		i = ms_bufferizer_get_avail(&chan->buff)/s->conf_gran;
		if (i>5)
			ms_message("Increasing buffer because sound card is late. (nb_buf=%i /old=%i)", i, s->adaptative_msconf_buf);
		s->adaptative_msconf_buf=i;
		if (s->adaptative_msconf_buf>10)
		{
			while (ms_bufferizer_get_avail(&chan->buff)> s->conf_gran*6)
			{
				ms_bufferizer_read(&chan->buff,(uint8_t*)chan->input,s->conf_gran);
				ms_message("Deleting extra sound card data %i", ms_bufferizer_get_avail(&chan->buff));
			}
		}
	}
	else if (s->adaptative_msconf_buf*s->conf_gran>ms_bufferizer_get_avail(&chan->buff))
	{
		if (s->adaptative_msconf_buf>3)
		{
			s->adaptative_msconf_buf--;
			s->adaptative_msconf_buf=ms_bufferizer_get_avail(&chan->buff)/s->conf_gran;
			//ms_message("decreasing buffer because sound card is in advance. (nb_buf=%i)", s->adaptative_msconf_buf);
		}
	}

	if (s->adaptative_msconf_buf>6)
		s->adaptative_msconf_buf=6;

	for (i=0;i<CONF_MAX_PINS;++i){
		chan=&s->channels[i];

		/* skip soundread and short buffer entry */
		if (i>0 
			&& ms_bufferizer_get_avail(&chan->buff)> s->conf_gran
			&& ms_bufferizer_get_avail(&chan->buff)> (ms_bufferizer_get_avail(&s->channels[0].buff)+s->conf_gran*6) )
		{
aymeric's avatar
aymeric committed
350 351
#if 0
			int loudness;
352
#endif
aymeric's avatar
aymeric committed
353 354
			while (ms_bufferizer_get_avail(&chan->buff)> s->conf_gran
				&& ms_bufferizer_get_avail(&chan->buff)> (ms_bufferizer_get_avail(&s->channels[0].buff)) )
aymeric's avatar
aymeric committed
355 356
			{
				ms_bufferizer_read(&chan->buff,(uint8_t*)chan->input,s->conf_gran);
aymeric's avatar
aymeric committed
357 358
#if 0
				speex_preprocess_ctl(chan->speex_pp, SPEEX_PREPROCESS_GET_AGC_LOUDNESS, &loudness);
359
#endif
aymeric's avatar
aymeric committed
360 361 362 363
				/* we want to remove 4 packets (40ms) in a near future: */
#ifndef DISABLE_SPEEX
				if (chan->speex_pp!=NULL && s->enable_vad==TRUE)
				{
364
					int vad=0;
aymeric's avatar
aymeric committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
					vad = speex_preprocess(chan->speex_pp, (short*)chan->input, NULL);
					if (vad==1)
						break; /* voice detected: process as usual */
					if (ms_bufferizer_get_avail(&chan->buff)<s->conf_gran)
						break; /* no more data to remove */
					ms_message("No voice detected: discarding sample. (idx=%i - bufsize=%i sncardbufsize=%i)",
						i, ms_bufferizer_get_avail(&chan->buff), ms_bufferizer_get_avail(&s->channels[0].buff));
				}
				if (ms_bufferizer_get_avail(&chan->buff) == (ms_bufferizer_get_avail(&s->channels[0].buff)))
					ms_message("same data in soundcard and incoming rtp. (idx=%i - bufsize=%i sncardbufsize=%i)",
						i, ms_bufferizer_get_avail(&chan->buff), ms_bufferizer_get_avail(&s->channels[0].buff));
#endif
				chan->stat_discarded++;
			}

380 381 382 383 384
			for(j=0;j<s->conf_nsamples;++j){
				s->sum[j]+=chan->input[j];
			}
			chan->has_contributed=TRUE;

aymeric's avatar
aymeric committed
385 386 387 388
			chan->stat_processed++;
		}
		else if (ms_bufferizer_get_avail(&chan->buff)>=s->conf_gran)
		{
389 390 391 392 393 394 395
			struct channel_volume {
				float energy;
				int channel;
			};
			struct channel_volume vol;
			float en;

aymeric's avatar
aymeric committed
396
			ms_bufferizer_read(&chan->buff,(uint8_t*)chan->input,s->conf_gran);
397 398 399 400 401 402 403

			en=chan->energy;
			for(j=0;j<s->conf_nsamples;++j){
				float s=chan->input[j];
				en=(s*s*coef) + ((float)1.0-coef)*en;
			}
			chan->energy=en;
aymeric's avatar
aymeric committed
404
			vol.energy = chan->energy; //10*log10f(chan->energy/max_e);
405 406 407
			vol.channel = i;
			ms_filter_notify(f, MS_CONF_CHANNEL_VOLUME, (void*)&vol);

408 409
			if (i>0) /* not for MIC */
			{
410
				if (chan->energy>65)
411 412 413 414 415
					chan->count_speaking++;
				else
					chan->count_speaking=0;
			}

aymeric's avatar
aymeric committed
416
#ifndef DISABLE_SPEEX
417
			if (chan->speex_pp!=NULL && s->enable_vad==TRUE && i==0)
aymeric's avatar
aymeric committed
418
			{
419
				speex_preprocess(chan->speex_pp, (short*)chan->input, NULL);
420
				ms_filter_notify(f, MS_CONF_SPEEX_PREPROCESS_MIC, (void*)chan->speex_pp);
aymeric's avatar
aymeric committed
421

422 423
				if (s->enable_halfduplex>0)
				{
aymeric's avatar
aymeric committed
424
					powerspectrum_stat_beyond8K(chan);
425 426
					if (chan->average_psd>s->vad_prob_start)
					{
427 428 429
						if (s->channels[0].is_speaking<0)
							ms_message("MIC is turned on");
						//ms_message("is_speaking (chan=%i) -> on/stat=%.3lf", i, chan->average_psd);
430 431 432 433
						s->channels[0].is_speaking=20; /* keep RTP unmuted for the next few ms */
					}
					else if (chan->average_psd<s->vad_prob_continue)
					{
434 435 436 437 438
						if (s->channels[0].is_speaking==0)
						{
							ms_message("MIC is turned off");
							s->channels[0].count_speaking=0;
						}
439 440
						s->channels[0].is_speaking--;
					}
441 442 443 444
					if (s->channels[0].is_speaking>0)
					{
						s->channels[0].count_speaking++;
					}
445 446 447 448 449 450
				}
			}
			else if (chan->speex_pp!=NULL && s->enable_vad==TRUE)
			{
				if (s->enable_halfduplex>0)
				{
451
					speex_preprocess(chan->speex_pp, (short*)chan->input, NULL);
452
				}
453
				else
454
				{
455
					speex_preprocess(chan->speex_pp, (short*)chan->input, NULL);
456
					//speex_preprocess_estimate_update(chan->speex_pp, (short*)chan->input);
457 458
				}
			}
aymeric's avatar
aymeric committed
459 460
#endif

461 462
			for(j=0;j<s->conf_nsamples;++j){
				s->sum[j]+=chan->input[j];
aymeric's avatar
aymeric committed
463
			}
464 465
			chan->has_contributed=TRUE;

aymeric's avatar
aymeric committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
			chan->stat_processed++;
		} else {
			chan->stat_missed++;
			if (i>0 && chan->is_used == TRUE)
			{
				chan->missed++;
				/* delete stream if data is missing since a long time */
				if (chan->missed>15)
				{
					chan->is_used=FALSE;
					ms_message("msconf: deleted contributing stream (pin=%i)", i);
				}
				/* couldn't we add confort noise for those outputs? */
			}
			chan->has_contributed=FALSE;
		}
	}
	return;
}

static inline int16_t saturate(int sample){
	if (sample>32000)
		sample=32000;
	else if (sample<-32000)
		sample=-32000;
	return (int16_t)sample;
}

494
static mblk_t * conf_output(ConfState *s, Channel *chan, int16_t attenuation){
aymeric's avatar
aymeric committed
495 496 497 498 499 500
	mblk_t *m=allocb(s->conf_gran,0);
	int i;
	int tmp;
	if (chan->has_contributed==TRUE){
		for (i=0;i<s->conf_nsamples;++i){
			tmp=s->sum[i]-(int)chan->input[i];
501
			*((int16_t*)m->b_wptr)=saturate(tmp)/attenuation;
aymeric's avatar
aymeric committed
502 503 504 505 506
			m->b_wptr+=2;
		}
	}else{
		for (i=0;i<s->conf_nsamples;++i){
			tmp=s->sum[i];
507
			*((int16_t*)m->b_wptr)=saturate(tmp)/attenuation;
aymeric's avatar
aymeric committed
508 509 510 511 512 513 514 515 516 517
			m->b_wptr+=2;
		}
	}
	return m;
}

static void conf_dispatch(MSFilter *f, ConfState *s){
	int i;
	Channel *chan;
	mblk_t *m;
518

aymeric's avatar
aymeric committed
519 520 521
	for (i=0;i<CONF_MAX_PINS;++i){
		if (f->outputs[i]!=NULL){
			chan=&s->channels[i];
522
			if (s->channels[0].is_speaking>0 // if MIC is speaking 
523
				&& s->channels[0].count_speaking<75 // if MIC was silence a few time ago
524
				&& chan->count_speaking>0 // if RTP has started to talk
525
				&& chan->count_speaking<75 // if RTP was silence a few ms ago
526 527 528
				&& i%2==1) // false detection of MIC speaking
			{
				ms_message("false detection of MIC speaking: turned off");
aymeric's avatar
aymeric committed
529
				m=conf_output(s,chan, 32000);
530 531
			}
			else if (s->channels[0].is_speaking<0 && i%2==1) // MIC is NOT speaking -> send silence on RTP
532
				m=conf_output(s,chan, 32000);
533 534
			else
				m=conf_output(s,chan, 1);
aymeric's avatar
aymeric committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
			ms_queue_put(f->outputs[i],m);
		}
	}
}

static void conf_process(MSFilter *f){
	int i;
	ConfState *s=(ConfState*)f->data;
	Channel *chan;
	Channel *chan0;
	/*read from all inputs and put into bufferizers*/
	for (i=0;i<CONF_MAX_PINS;++i){
		if (f->inputs[i]!=NULL){
			chan=&s->channels[i];
			ms_bufferizer_put_from_queue(&chan->buff,f->inputs[i]);
			if (ms_bufferizer_get_avail(&chan->buff)>0)
			{
				chan->missed=0; /* reset counter of missed packet */
				if (i>0 && chan->is_used==FALSE)
				{
					chan->is_used=TRUE;
aymeric's avatar
aymeric committed
556
					ms_message("msconf: new contributing stream (chan=%i) %i", ms_bufferizer_get_avail(&chan->buff), i);
aymeric's avatar
aymeric committed
557 558 559 560 561 562 563
				}
			}
		}
	}

	/*do the job */
	while(should_process(f,s)==TRUE){
564
		conf_sum(f, s);
aymeric's avatar
aymeric committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
		conf_dispatch(f,s);
	}

	/* mixer is disabled! -> copy A->B and B->A*/
	if (s->mix_mode == FALSE)
	{
		/* get the soundread data and copy it to pinX */
		for (i=1;i<CONF_MAX_PINS;i=i+2){
			if (f->inputs[i]!=NULL){
				chan0=&s->channels[0];
				chan=&s->channels[i];
				if (chan->is_used==TRUE)
				{
					while (ms_bufferizer_read(&chan->buff,(uint8_t*)chan->input,s->conf_gran)==s->conf_gran)
					{
						if (f->outputs[0]!=NULL)
						{
							/* send in pin0 */
							mblk_t *m=allocb(s->conf_gran,0);
							memcpy(m->b_wptr, chan->input, s->conf_gran);
							m->b_wptr+=s->conf_gran;
							ms_queue_put(f->outputs[0],m);
						}
					}
				}

				if (chan0->is_used==TRUE)
				{
					while (ms_bufferizer_read(&chan0->buff,(uint8_t*)chan0->input,s->conf_gran)==s->conf_gran)
					{
						if (f->outputs[i]!=NULL)
						{
							/* send in pinI */
							mblk_t *m=allocb(s->conf_gran,0);
							memcpy(m->b_wptr, chan0->input, s->conf_gran);
							m->b_wptr+=s->conf_gran;
							ms_queue_put(f->outputs[i],m);
						}
					}
				}
				break;
			}
		}
	}

}

static void conf_postprocess(MSFilter *f){
	int i;
	ConfState *s=(ConfState*)f->data;
615 616 617 618
	for (i=0;i<CONF_MAX_PINS;i++)
		channel_uninit(&s->channels[i]);
    for (i=0;i<CONF_MAX_PINS;i++)
		channel_init(s, &s->channels[i], i);
aymeric's avatar
aymeric committed
619
}
620

aymeric's avatar
aymeric committed
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
static int msconf_set_sr(MSFilter *f, void *arg){
	ConfState *s=(ConfState*)f->data;
	int i;

	s->samplerate = *(int*)arg;
	s->conf_gran = ((16 * s->samplerate) / 800) *2;
	s->conf_nsamples=s->conf_gran/2;
	for (i=0;i<CONF_MAX_PINS;i++)
		channel_uninit(&s->channels[i]);
    for (i=0;i<CONF_MAX_PINS;i++)
		channel_init(s, &s->channels[i], i);
	return 0;
}

static int msconf_enable_directmode(MSFilter *f, void *arg){
	ConfState *s=(ConfState*)f->data;
	s->enable_directmode = *(int*)arg;
	return 0;
}

static int msconf_enable_agc(MSFilter *f, void *arg){
	ConfState *s=(ConfState*)f->data;
	int i;
	s->agc_level = *(int*)arg;

	for (i=0;i<CONF_MAX_PINS;i++)
		channel_uninit(&s->channels[i]);
    for (i=0;i<CONF_MAX_PINS;i++)
		channel_init(s, &s->channels[i], i);
	return 0;
}

653 654 655 656 657 658 659 660 661 662 663 664 665
static int msconf_set_max_gain(MSFilter *f, void *arg){
	ConfState *s=(ConfState*)f->data;
	int i;
	s->max_gain = *(int*)arg;

	for (i=0;i<CONF_MAX_PINS;i++)
		channel_uninit(&s->channels[i]);
    for (i=0;i<CONF_MAX_PINS;i++)
		channel_init(s, &s->channels[i], i);
	return 0;
}


666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
static int msconf_enable_halfduplex(MSFilter *f, void *arg){
	ConfState *s=(ConfState*)f->data;
	int i;
	s->enable_halfduplex = *(int*)arg;

	for (i=0;i<CONF_MAX_PINS;i++)
		channel_uninit(&s->channels[i]);
    for (i=0;i<CONF_MAX_PINS;i++)
		channel_init(s, &s->channels[i], i);
	return 0;
}

static int msconf_set_vad_prob_start(MSFilter *f, void *arg){
	ConfState *s=(ConfState*)f->data;
	int i;
	s->vad_prob_start = *(int*)arg;

	for (i=0;i<CONF_MAX_PINS;i++)
		channel_uninit(&s->channels[i]);
    for (i=0;i<CONF_MAX_PINS;i++)
		channel_init(s, &s->channels[i], i);
	return 0;
}

static int msconf_set_vad_prob_continue(MSFilter *f, void *arg){
	ConfState *s=(ConfState*)f->data;
	int i;
	s->vad_prob_continue = *(int*)arg;

	for (i=0;i<CONF_MAX_PINS;i++)
		channel_uninit(&s->channels[i]);
    for (i=0;i<CONF_MAX_PINS;i++)
		channel_init(s, &s->channels[i], i);
	return 0;
}

aymeric's avatar
aymeric committed
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
static int msconf_enable_vad(MSFilter *f, void *arg){
	ConfState *s=(ConfState*)f->data;
	int i;
	s->enable_vad = *(int*)arg;

	for (i=0;i<CONF_MAX_PINS;i++)
		channel_uninit(&s->channels[i]);
    for (i=0;i<CONF_MAX_PINS;i++)
		channel_init(s, &s->channels[i], i);
	return 0;
}

static int msconf_get_stat_discarded(MSFilter *f, void *arg){
	ConfState *s=(ConfState*)f->data;
	Channel *chan;
	int i;
	i = *(int*)arg;
	/*read from all inputs and put into bufferizers*/
	if (i<0 || i>CONF_MAX_PINS)
	  return -1;

	if (f->inputs[i]!=NULL){
		chan=&s->channels[i];
		return chan->stat_discarded;
	}
	return -1;
}

static int msconf_get_stat_missed(MSFilter *f, void *arg){
	ConfState *s=(ConfState*)f->data;
	Channel *chan;
	int i;
	i = *(int*)arg;
	/*read from all inputs and put into bufferizers*/
	if (i<0 || i>CONF_MAX_PINS)
	  return -1;

	if (f->inputs[i]!=NULL){
		chan=&s->channels[i];
		return chan->stat_missed;
	}
	return -1;
}

static int msconf_get_stat_processed(MSFilter *f, void *arg){
	ConfState *s=(ConfState*)f->data;
	Channel *chan;
	int i;
	i = *(int*)arg;
	/*read from all inputs and put into bufferizers*/
	if (i<0 || i>CONF_MAX_PINS)
	  return -1;

	if (f->inputs[i]!=NULL){
		chan=&s->channels[i];
		return chan->stat_processed;
	}
	return -1;
}

static MSFilterMethod msconf_methods[]={
	{	MS_FILTER_SET_SAMPLE_RATE, msconf_set_sr },
	{	MS_FILTER_ENABLE_DIRECTMODE, msconf_enable_directmode },
	{	MS_FILTER_ENABLE_VAD, msconf_enable_vad },
	{	MS_FILTER_ENABLE_AGC, msconf_enable_agc },
767 768
	{	MS_FILTER_SET_MAX_GAIN, msconf_set_max_gain },
	
aymeric's avatar
aymeric committed
769 770 771
	{	MS_FILTER_GET_STAT_DISCARDED, msconf_get_stat_discarded },
	{	MS_FILTER_GET_STAT_MISSED, msconf_get_stat_missed },
	{	MS_FILTER_GET_STAT_OUTPUT, msconf_get_stat_processed },
772 773 774 775

	{	MS_FILTER_ENABLE_HALFDUPLEX, msconf_enable_halfduplex },
	{	MS_FILTER_SET_VAD_PROB_START, msconf_set_vad_prob_start },
	{	MS_FILTER_SET_VAD_PROB_CONTINUE, msconf_set_vad_prob_continue },
aymeric's avatar
aymeric committed
776 777 778 779 780 781 782 783
	{	0			, NULL}
};

#ifdef _MSC_VER

MSFilterDesc ms_conf_desc={
	MS_CONF_ID,
	"MSConf",
784
	N_("A filter to make conferencing"),
aymeric's avatar
aymeric committed
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
	MS_FILTER_OTHER,
	NULL,
	CONF_MAX_PINS,
	CONF_MAX_PINS,
	conf_init,
	conf_preprocess,
	conf_process,
	conf_postprocess,
	conf_uninit,
	msconf_methods
};

#else

MSFilterDesc ms_conf_desc={
	.id=MS_CONF_ID,
	.name="MSConf",
802
	.text=N_("A filter to make conferencing"),
aymeric's avatar
aymeric committed
803 804 805 806 807 808 809 810 811 812 813 814 815 816
	.category=MS_FILTER_OTHER,
	.ninputs=CONF_MAX_PINS,
	.noutputs=CONF_MAX_PINS,
	.init=conf_init,
	.preprocess=conf_preprocess,
	.process=conf_process,
	.postprocess=conf_postprocess,
	.uninit=conf_uninit,
	.methods=msconf_methods
};

#endif

MS_FILTER_DESC_EXPORT(ms_conf_desc)
817