audio_bypass_tester.c 15.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
	liblinphone_tester - liblinphone test suite
	Copyright (C) 2013  Belledonne Communications SARL

	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, see <http://www.gnu.org/licenses/>.
*/

#include "liblinphone_tester.h"
Benjamin REIS's avatar
Benjamin REIS committed
20
#include "tester_utils.h"
21
#include "audio_bypass_wav_header.h" // This is a copy of mediastreamer2/src/audiofilters/wav_header.h
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/**********************************************************************
 * This is a (simpler) copy of msfileplay filter in mediastreamer2   *
 *********************************************************************/

struct _PlayerData{
	int fd;
	MSPlayerState state;
	int rate;
	int nchannels;
	int hsize;
	int loop_after;
	int pause_time;
	int count;
	int samplesize;
	char *mime;
	uint32_t ts;
	bool_t swap;
	bool_t is_raw;
};

typedef struct _PlayerData PlayerData;

45
static void audio_bypass_snd_read_init(MSFilter *f) {
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	PlayerData *d=ms_new0(PlayerData,1);
	d->fd=-1;
	d->state=MSPlayerClosed;
	d->swap=TRUE;
	d->rate=44100;
	d->nchannels=1;
	d->samplesize=2;
	d->mime = "L16";
	d->hsize=0;
	d->loop_after=-1; /*by default, don't loop*/
	d->pause_time=0;
	d->count=0;
	d->ts=0;
	d->is_raw=TRUE;
	f->data=d;
}

63
int audio_bypass_read_wav_header_from_fd(wave_header_t *header,int fd){
64 65 66 67 68 69
	int count;
	int skip;
	int hsize=0;
	riff_t *riff_chunk=&header->riff_chunk;
	format_t *format_chunk=&header->format_chunk;
	data_t *data_chunk=&header->data_chunk;
70

Ronan's avatar
Ronan committed
71
	ssize_t len = read(fd, (char*)riff_chunk, sizeof(riff_t)) ;
72 73 74
	if (len != sizeof(riff_t)){
		goto not_a_wav;
	}
75

76 77 78
	if (0!=strncmp(riff_chunk->riff, "RIFF", 4) || 0!=strncmp(riff_chunk->wave, "WAVE", 4)){
		goto not_a_wav;
	}
79 80

	len = read(fd, (char*)format_chunk, sizeof(format_t)) ;
81 82 83 84
	if (len != sizeof(format_t)){
		ms_warning("Wrong wav header: cannot read file");
		goto not_a_wav;
	}
85

Ronan's avatar
Ronan committed
86
	if ((skip=(int)le_uint32(format_chunk->len)-0x10)>0)
87 88 89 90
	{
		lseek(fd,skip,SEEK_CUR);
	}
	hsize=sizeof(wave_header_t)-0x10+le_uint32(format_chunk->len);
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	count=0;
	do{
		len = read(fd, data_chunk, sizeof(data_t)) ;
		if (len != sizeof(data_t)){
			ms_warning("Wrong wav header: cannot read file");
			goto not_a_wav;
		}
		if (strncmp(data_chunk->data, "data", 4)!=0){
			ms_warning("skipping chunk=%c%c%c%c len=%i", data_chunk->data[0],data_chunk->data[1],data_chunk->data[2],data_chunk->data[3], data_chunk->len);
			lseek(fd,le_uint32(data_chunk->len),SEEK_CUR);
			count++;
			hsize+=len+le_uint32(data_chunk->len);
		}else{
			hsize+=len;
			break;
		}
	}while(count<30);
	return hsize;

	not_a_wav:
		/*rewind*/
		lseek(fd,0,SEEK_SET);
		return -1;
}

static int read_wav_header(PlayerData *d){
	wave_header_t header;
	format_t *format_chunk=&header.format_chunk;
120 121
	int ret=audio_bypass_read_wav_header_from_fd(&header,d->fd);

122 123
	d->samplesize=le_uint16(format_chunk->blockalign)/d->nchannels;
	d->hsize=ret;
124

125 126 127 128 129 130
	#ifdef WORDS_BIGENDIAN
	if (le_uint16(format_chunk->blockalign)==le_uint16(format_chunk->channel) * 2)
		d->swap=TRUE;
	#endif
	d->is_raw=FALSE;
	return 0;
131 132 133
}

static void audio_bypass_snd_read_preprocess(MSFilter *f) {
134 135
	PlayerData *d=(PlayerData*)f->data;
	int fd;
Simon Morlat's avatar
Simon Morlat committed
136
	char *file=bc_tester_res("sounds/hello44100.wav");
137 138 139

	if ((fd=open(file,O_RDONLY|O_BINARY))==-1){
		ms_warning("MSFilePlayer[%p]: failed to open %s: %s",f,file,strerror(errno));
Simon Morlat's avatar
Simon Morlat committed
140
		bc_free(file);
141 142 143 144 145 146 147 148 149 150 151
		return;
	}

	d->state=MSPlayerPaused;
	d->fd=fd;
	d->ts=0;
	if (read_wav_header(d)!=0 && strstr(file,".wav")){
		ms_warning("File %s has .wav extension but wav header could be found.",file);
	}
	ms_filter_notify_no_arg(f,MS_FILTER_OUTPUT_FMT_CHANGED);
	ms_message("MSFilePlayer[%p]: %s opened: rate=%i,channel=%i",f,file,d->rate,d->nchannels);
152

153 154
	if (d->state==MSPlayerPaused)
		d->state=MSPlayerPlaying;
Simon Morlat's avatar
Simon Morlat committed
155
	bc_free(file);
156 157 158 159 160 161 162 163 164 165 166
	return;
}

static void swap_bytes(unsigned char *bytes, int len){
	int i;
	unsigned char tmp;
	for(i=0;i<len;i+=2){
		tmp=bytes[i];
		bytes[i]=bytes[i+1];
		bytes[i+1]=tmp;
	}
167 168 169
}

static void audio_bypass_snd_read_process(MSFilter *f) {
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 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
	PlayerData *d=(PlayerData*)f->data;
	int nsamples=(f->ticker->interval*d->rate*d->nchannels)/1000;
	int bytes;
	/*send an even number of samples each tick. At 22050Hz the number of samples per 10 ms chunk is odd.
	Odd size buffer of samples cause troubles to alsa. Fixing in alsa is difficult, so workaround here.
	*/
	if (nsamples & 0x1 ) { //odd number of samples
		if (d->count & 0x1 )
			nsamples++;
		else
			nsamples--;
	}
	bytes=nsamples*d->samplesize;
	d->count++;
	ms_filter_lock(f);
	if (d->state==MSPlayerPlaying){
		{
			int err;
			mblk_t *om=allocb(bytes,0);
			if (d->pause_time>0){
				err=bytes;
				memset(om->b_wptr,0,bytes);
				d->pause_time-=f->ticker->interval;
			}else{
				err=read(d->fd,om->b_wptr,bytes);
				if (d->swap) swap_bytes(om->b_wptr,bytes);
			}
			if (err>=0){
				if (err!=0){
					if (err<bytes)
						memset(om->b_wptr+err,0,bytes-err);
					om->b_wptr+=bytes;
					mblk_set_timestamp_info(om,d->ts);
					d->ts+=nsamples;
					ms_queue_put(f->outputs[0],om);
				}else freemsg(om);
				if (err<bytes){
					ms_filter_notify_no_arg(f,MS_PLAYER_EOF);
					/*for compatibility:*/
					lseek(d->fd,d->hsize,SEEK_SET);

					/* special value for playing file only once */
					if (d->loop_after<0)
					{
						d->state=MSPlayerPaused;
						ms_filter_unlock(f);
						return;
					}

					if (d->loop_after>=0){
						d->pause_time=d->loop_after;
					}
				}
			}else{
				ms_warning("Fail to read %i bytes: %s",bytes,strerror(errno));
			}
		}
	}
	ms_filter_unlock(f);
229 230 231
}

static void audio_bypass_snd_read_postprocess(MSFilter *f) {
232 233 234 235 236 237 238 239 240 241
	PlayerData *d=(PlayerData*)f->data;
	ms_filter_lock(f);
	if (d->state!=MSPlayerClosed){
		d->state=MSPlayerPaused;
		lseek(d->fd,d->hsize,SEEK_SET);
	}
	ms_filter_unlock(f);
	if (d->fd!=-1)	close(d->fd);
	d->fd=-1;
	d->state=MSPlayerClosed;
242 243 244
}

static void audio_bypass_snd_read_uninit(MSFilter *f) {
245 246
	PlayerData *d=(PlayerData*)f->data;
	ms_free(d);
247 248
}

249 250 251 252 253 254 255 256
static int audio_bypass_snd_read_set_sample_rate(MSFilter *f, void *arg) { // This is to prevent ms2 to put a resampler between this filter and the rtpsend
	return 0;
}

static int audio_bypass_snd_read_set_nchannels(MSFilter *f, void *arg) { // This is to prevent ms2 to put a resampler between this filter and the rtpsend
	return 0;
}

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
static int audio_bypass_snd_read_get_sample_rate(MSFilter *f, void *arg) {
	int *sample_rate = (int *)arg;
	*sample_rate = 44100;
	return 0;
}

static int audio_bypass_snd_read_get_nchannels(MSFilter *f, void *arg) {
	int *nchannels = (int *)arg;
	*nchannels = 1;
	return 0;
}

static int audio_bypass_snd_read_get_fmt(MSFilter *f, void *arg) {
	MSPinFormat *pinFmt = (MSPinFormat *)arg;
	pinFmt->fmt = ms_factory_get_audio_format(f->factory, "L16", 44100, 1, NULL);
	return 0;
}

static MSFilterMethod audio_bypass_snd_read_methods[] = {
276 277
	{ MS_FILTER_SET_SAMPLE_RATE, audio_bypass_snd_read_set_sample_rate },
	{ MS_FILTER_SET_NCHANNELS, audio_bypass_snd_read_set_nchannels },
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
	{ MS_FILTER_GET_SAMPLE_RATE, audio_bypass_snd_read_get_sample_rate },
	{ MS_FILTER_GET_NCHANNELS, audio_bypass_snd_read_get_nchannels },
	{ MS_FILTER_GET_OUTPUT_FMT, audio_bypass_snd_read_get_fmt },
	{ 0, NULL }
};

MSFilterDesc audio_bypass_snd_read_desc = {
	MS_FILTER_PLUGIN_ID,
	"AudioBypassReader",
	"audio bypass source",
	MS_FILTER_OTHER,
	NULL,
	0,
	1,
	audio_bypass_snd_read_init,
	audio_bypass_snd_read_preprocess,
	audio_bypass_snd_read_process,
	audio_bypass_snd_read_postprocess,
	audio_bypass_snd_read_uninit,
297 298
	audio_bypass_snd_read_methods,
	0
299 300 301
};

static void audio_bypass_snd_write_init(MSFilter *f) {
302

303 304 305
}

static void audio_bypass_snd_write_preprocess(MSFilter *f) {
306

307 308 309
}

static void audio_bypass_snd_write_process(MSFilter *f) {
Simon Morlat's avatar
Simon Morlat committed
310
	ms_queue_flush(f->inputs[0]);
311 312 313
}

static void audio_bypass_snd_write_postprocess(MSFilter *f) {
314

315 316 317
}

static void audio_bypass_snd_write_uninit(MSFilter *f) {
318

319 320
}

321 322 323 324 325 326 327 328
static int audio_bypass_snd_write_set_sample_rate(MSFilter *f, void *arg) { // This is to prevent ms2 to put a resampler between this filter and the rtprecv
	return 0;
}

static int audio_bypass_snd_write_set_nchannels(MSFilter *f, void *arg) { // This is to prevent ms2 to put a resampler between this filter and the rtprecv
	return 0;
}

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
static int audio_bypass_snd_write_get_sample_rate(MSFilter *f, void *arg) {
	int *sample_rate = (int*)arg;
	*sample_rate = 44100;
	return 0;
}

static int audio_bypass_snd_write_get_nchannels(MSFilter *obj, void *arg) {
	int *nchannels = (int*)arg;
	*nchannels = 1;
	return 0;
}

static int audio_bypass_snd_write_get_fmt(MSFilter *f, void *arg) {
	MSPinFormat *pinFmt = (MSPinFormat *)arg;
	pinFmt->fmt = ms_factory_get_audio_format(f->factory, "L16", 44100, 1, NULL);
	return 0;
}

static MSFilterMethod audio_bypass_snd_write_methods[] = {
348 349
	{ MS_FILTER_SET_SAMPLE_RATE, audio_bypass_snd_write_set_sample_rate },
	{ MS_FILTER_SET_NCHANNELS, audio_bypass_snd_write_set_nchannels },
350 351
	{ MS_FILTER_GET_SAMPLE_RATE, audio_bypass_snd_write_get_sample_rate },
	{ MS_FILTER_GET_NCHANNELS, audio_bypass_snd_write_get_nchannels },
352
	{ MS_FILTER_GET_OUTPUT_FMT, audio_bypass_snd_write_get_fmt },
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
	{ 0, NULL }
};

MSFilterDesc audio_bypass_snd_write_desc = {
	MS_FILTER_PLUGIN_ID,
	"AudioBypassWriter",
	"audio bypass output",
	MS_FILTER_OTHER,
	NULL,
	1,
	0,
	audio_bypass_snd_write_init,
	audio_bypass_snd_write_preprocess,
	audio_bypass_snd_write_process,
	audio_bypass_snd_write_postprocess,
	audio_bypass_snd_write_uninit,
369 370
	audio_bypass_snd_write_methods,
	0
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
};

static MSFilter* audio_bypass_snd_card_create_reader(MSSndCard *sndcard) {
	MSFactory *factory = ms_snd_card_get_factory(sndcard);
	MSFilter *f = ms_factory_create_filter_from_desc(factory, &audio_bypass_snd_read_desc);
	return f;
}

static MSFilter* audio_bypass_snd_card_create_writer(MSSndCard *sndcard) {
	MSFactory *factory = ms_snd_card_get_factory(sndcard);
	MSFilter *f = ms_factory_create_filter_from_desc(factory, &audio_bypass_snd_write_desc);
	return f;
}

static void audio_bypass_snd_card_detect(MSSndCardManager *m);

MSSndCardDesc audio_bypass_snd_card_desc = {
	"audioBypass",
	audio_bypass_snd_card_detect,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
395
	NULL,
396 397
	audio_bypass_snd_card_create_reader,
	audio_bypass_snd_card_create_writer,
398 399 400
	NULL,
	NULL,
	NULL,
401 402 403 404 405 406 407 408 409 410 411 412 413
	NULL
};

static MSSndCard* create_audio_bypass_snd_card(void) {
	MSSndCard* sndcard;
	sndcard = ms_snd_card_new(&audio_bypass_snd_card_desc);
	sndcard->data = NULL;
	sndcard->name = ms_strdup("audio bypass sound card");
	sndcard->capabilities = MS_SND_CARD_CAP_PLAYBACK | MS_SND_CARD_CAP_CAPTURE;
	sndcard->latency = 0;
	return sndcard;
}

414 415
#define AUDIO_BYPASS_SOUNDCARD "audioBypass: audio bypass sound card"

416 417 418 419
static void audio_bypass_snd_card_detect(MSSndCardManager *m) {
	ms_snd_card_manager_add_card(m, create_audio_bypass_snd_card());
}

420 421 422 423 424 425 426 427 428 429 430
static void only_enable_payload(LinphoneCore *lc, const char *mime, int rate, int channels) {
	const MSList *elem = linphone_core_get_audio_codecs(lc);
	PayloadType *pt;

	for(; elem != NULL; elem = elem->next) {
		pt = (PayloadType *)elem->data;
		linphone_core_enable_payload_type(lc, pt, FALSE);
	}
	pt = linphone_core_find_payload_type(lc, mime, rate, channels);
	if (BC_ASSERT_PTR_NOT_NULL(pt)) {
		linphone_core_enable_payload_type(lc, pt, TRUE);
431 432 433
	}
}

Simon Morlat's avatar
Simon Morlat committed
434 435 436 437
/*
 * set some conservative jitter buffer params to be more robust to late ticks.
 * This is important so that the audio comparison is succesful*/
static void set_jitter_buffer_params(LinphoneCore *lc){
438
	int jitter_buffer_ms = 300;
439
	lp_config_set_int(linphone_core_get_config(lc), "rtp", "jitter_buffer_min_size", jitter_buffer_ms);
Simon Morlat's avatar
Simon Morlat committed
440 441 442
	linphone_core_set_audio_jittcomp(lc, jitter_buffer_ms);
}

443
static void audio_bypass(void) {
444
	LinphoneCoreManager *marie = linphone_core_manager_new("marie_rc");
445 446 447
	LinphoneCore *marie_lc = marie->lc;
	MSFactory *marie_factory = linphone_core_get_ms_factory(marie_lc);
	MSSndCardManager *marie_sndcard_manager = ms_factory_get_snd_card_manager(marie_factory);
448

449
	LinphoneCoreManager *pauline = linphone_core_manager_new("pauline_rc");
450 451 452
	LinphoneCore *pauline_lc = pauline->lc;
	MSFactory *pauline_factory = linphone_core_get_ms_factory(pauline_lc);
	MSSndCardManager *pauline_sndcard_manager = ms_factory_get_snd_card_manager(pauline_factory);
453

454
	bool_t call_ok;
455 456 457 458
	char *hellopath = bc_tester_res("sounds/hello44100.wav");
	char *recordpath = bc_tester_file("audiobypass-record.wav");
	double similar=1;
	const double threshold = 0.85;
459

460 461
	lp_config_set_string(linphone_core_get_config(marie_lc), "sound", "features", "None");
	lp_config_set_string(linphone_core_get_config(pauline_lc), "sound", "features", "None");
462

463 464
	/*make sure the record file doesn't already exists, otherwise this test will append new samples to it*/
	unlink(recordpath);
465

466
	// Enable L16 audio codec
467 468
	only_enable_payload(marie_lc, "L16", 44100, 1);
	only_enable_payload(pauline_lc, "L16", 44100, 1);
469

470 471 472
	// Add our custom sound card
	ms_snd_card_manager_register_desc(marie_sndcard_manager, &audio_bypass_snd_card_desc);
	ms_snd_card_manager_register_desc(pauline_sndcard_manager, &audio_bypass_snd_card_desc);
473 474 475 476 477 478 479 480 481 482 483
	linphone_core_reload_sound_devices(marie_lc);
	linphone_core_reload_sound_devices(pauline_lc);
	linphone_core_set_playback_device(marie_lc, AUDIO_BYPASS_SOUNDCARD);
	linphone_core_set_playback_device(pauline_lc, AUDIO_BYPASS_SOUNDCARD);
	linphone_core_set_capture_device(marie_lc, AUDIO_BYPASS_SOUNDCARD);
	linphone_core_set_capture_device(pauline_lc, AUDIO_BYPASS_SOUNDCARD);
	BC_ASSERT_STRING_EQUAL(linphone_core_get_capture_device(marie_lc), AUDIO_BYPASS_SOUNDCARD);
	BC_ASSERT_STRING_EQUAL(linphone_core_get_capture_device(pauline_lc), AUDIO_BYPASS_SOUNDCARD);
	BC_ASSERT_STRING_EQUAL(linphone_core_get_playback_device(marie_lc), AUDIO_BYPASS_SOUNDCARD);
	BC_ASSERT_STRING_EQUAL(linphone_core_get_playback_device(pauline_lc), AUDIO_BYPASS_SOUNDCARD);

Simon Morlat's avatar
Simon Morlat committed
484
	set_jitter_buffer_params(pauline_lc);
485 486 487
	linphone_core_use_files(pauline_lc, TRUE);
	linphone_core_set_play_file(pauline_lc, NULL);
	linphone_core_set_record_file(pauline_lc, recordpath);
488 489 490 491

	call_ok = call(marie, pauline);
	BC_ASSERT_TRUE(call_ok);
	if (!call_ok) goto end;
Ronan's avatar
Ronan committed
492

493

494
	BC_ASSERT_STRING_EQUAL(linphone_call_params_get_used_audio_codec(linphone_call_get_current_params(linphone_core_get_current_call(marie_lc)))->mime_type, "L16");
495

Simon Morlat's avatar
Simon Morlat committed
496
	wait_for_until(pauline_lc, marie_lc, NULL, 0, 5000); //hello44100.wav is 4 seconds long
497
	end_call(marie, pauline);
498

499 500 501 502
	BC_ASSERT_EQUAL(ms_audio_diff(hellopath, recordpath, &similar, &audio_cmp_params, NULL, NULL), 0, int, "%d");
	BC_ASSERT_GREATER(similar, threshold, double, "%g");
	BC_ASSERT_LOWER(similar, 1.0, double, "%g");

503
end:
504 505
	bc_free(recordpath);
	bc_free(hellopath);
506 507 508 509 510 511 512 513
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

test_t audio_bypass_tests[] = {
	TEST_NO_TAG("Audio Bypass", audio_bypass)
};

514
test_suite_t audio_bypass_suite = { "Audio Bypass", NULL, NULL,
515 516
	liblinphone_tester_before_each, liblinphone_tester_after_each,
	sizeof(audio_bypass_tests) / sizeof(audio_bypass_tests[0]), audio_bypass_tests };