mediastreamer2_sound_card_tester.c 23 KB
Newer Older
Ghislain MARY's avatar
Ghislain MARY committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006-2013 Belledonne Communications, Grenoble

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
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Ghislain MARY's avatar
Ghislain MARY committed
18 19 20 21 22 23 24 25 26 27 28
*/

#include "mediastreamer2/mediastream.h"
#include "mediastreamer2/dtmfgen.h"
#include "mediastreamer2/msfileplayer.h"
#include "mediastreamer2/msfilerec.h"
#include "mediastreamer2/msrtp.h"
#include "mediastreamer2/mstonedetector.h"
#include "mediastreamer2_tester.h"
#include "mediastreamer2_tester_private.h"

Ghislain MARY's avatar
Ghislain MARY committed
29 30 31
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#else
32
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Ghislain MARY's avatar
Ghislain MARY committed
33
#endif
34

35
static int sound_card_tester_before_all(void) {
Ghislain MARY's avatar
Ghislain MARY committed
36 37 38 39 40
	ms_init();
	ms_filter_enable_statistics(TRUE);
	return 0;
}

41
static int sound_card_tester_after_all(void) {
Ghislain MARY's avatar
Ghislain MARY committed
42 43 44 45 46 47 48 49 50 51 52 53 54
	ms_exit();
	return 0;
}

static void configure_resampler(MSFilter *resampler, MSFilter *from, MSFilter *to) {
	int from_rate = 0, to_rate = 0;
	int from_channels = 0, to_channels = 0;
	ms_filter_call_method(from, MS_FILTER_GET_SAMPLE_RATE, &from_rate);
	ms_filter_call_method(to, MS_FILTER_GET_SAMPLE_RATE, &to_rate);
	ms_filter_call_method(resampler, MS_FILTER_SET_SAMPLE_RATE, &from_rate);
	ms_filter_call_method(resampler, MS_FILTER_SET_OUTPUT_SAMPLE_RATE, &to_rate);
	ms_filter_call_method(from, MS_FILTER_GET_NCHANNELS, &from_channels);
	ms_filter_call_method(to, MS_FILTER_GET_NCHANNELS, &to_channels);
55 56
	if (from_channels == 0) from_channels = 1;
	if (to_channels == 0) to_channels = 1;
Ghislain MARY's avatar
Ghislain MARY committed
57 58 59 60 61 62 63 64 65 66
	ms_filter_call_method(resampler, MS_FILTER_SET_NCHANNELS, &from_channels);
	ms_filter_call_method(resampler, MS_FILTER_SET_OUTPUT_NCHANNELS, &to_channels);
}

static void dtmfgen_soundwrite(void) {
	MSConnectionHelper h;
	bool_t need_resampler = FALSE;
	unsigned int filter_mask = FILTER_MASK_VOIDSOURCE | FILTER_MASK_DTMFGEN | FILTER_MASK_SOUNDWRITE;
	int sample_rate = 8000;
	int nchannels = 1;
67
	bool_t send_silence = TRUE;
Ghislain MARY's avatar
Ghislain MARY committed
68 69 70

	ms_filter_reset_statistics();
	ms_tester_create_ticker();
71
	ms_tester_create_filters(filter_mask, ms_factory_get_fallback());
72
	ms_filter_call_method(ms_tester_voidsource, MS_FILTER_SET_SAMPLE_RATE, &sample_rate);
Ghislain MARY's avatar
Ghislain MARY committed
73
	ms_filter_call_method(ms_tester_voidsource, MS_FILTER_SET_NCHANNELS, &nchannels);
74
	ms_filter_call_method(ms_tester_voidsource, MS_VOID_SOURCE_SEND_SILENCE, &send_silence);
75
	ms_filter_call_method(ms_tester_dtmfgen, MS_FILTER_SET_SAMPLE_RATE, &sample_rate);
Ghislain MARY's avatar
Ghislain MARY committed
76
	ms_filter_call_method(ms_tester_dtmfgen, MS_FILTER_SET_NCHANNELS, &nchannels);
77
	if (ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_SET_SAMPLE_RATE, &sample_rate) != 0) {
Ghislain MARY's avatar
Ghislain MARY committed
78 79 80 81 82 83
		need_resampler = TRUE;
	}
	if (ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_SET_NCHANNELS, &nchannels) != 0) {
		need_resampler = TRUE;
	}
	if (need_resampler == TRUE) {
84
		ms_tester_create_filters(FILTER_MASK_RESAMPLER, ms_factory_get_fallback());
Ghislain MARY's avatar
Ghislain MARY committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
		configure_resampler(ms_tester_resampler, ms_tester_dtmfgen, ms_tester_soundwrite);
	}
	ms_connection_helper_start(&h);
	ms_connection_helper_link(&h, ms_tester_voidsource, -1, 0);
	ms_connection_helper_link(&h, ms_tester_dtmfgen, 0, 0);
	if (need_resampler == TRUE) {
		ms_connection_helper_link(&h, ms_tester_resampler, 0, 0);
	}
	ms_connection_helper_link(&h, ms_tester_soundwrite, 0, -1);
	ms_ticker_attach(ms_tester_ticker, ms_tester_voidsource);

	ms_tester_tone_generation_loop();

	ms_ticker_detach(ms_tester_ticker, ms_tester_voidsource);
	ms_connection_helper_start(&h);
	ms_connection_helper_unlink(&h, ms_tester_voidsource, -1, 0);
	ms_connection_helper_unlink(&h, ms_tester_dtmfgen, 0, 0);
	if (need_resampler == TRUE) {
		ms_connection_helper_unlink(&h, ms_tester_resampler, 0, 0);
	}
	ms_connection_helper_unlink(&h, ms_tester_soundwrite, 0, -1);
	if (need_resampler == TRUE) {
		ms_tester_destroy_filters(FILTER_MASK_RESAMPLER);
	}
	ms_filter_log_statistics();
	ms_tester_destroy_filters(filter_mask);
	ms_tester_destroy_ticker();
}

114 115 116 117 118 119 120 121
#define CHIMES_48000_STEREO_FILE_NAME		"sounds/chimes_48000_stereo.wav"
#define BIRD_44100_STEREO_FILE_NAME			"sounds/bird_44100_stereo.wav"
#define PUNCH_16000_STEREO_FILE_NAME		"sounds/punch_16000_stereo.wav"
#define PIANO_8000_STEREO_FILE_NAME			"sounds/piano_8000_stereo.wav"
#define NYLON_48000_MONO_FILE_NAME			"sounds/nylon_48000_mono.wav"
#define OWL_44100_MONO_FILE_NAME			"sounds/owl_44100_mono.wav"
#define LASERROCKET_16000_MONO_FILE_NAME	"sounds/laserrocket_16000_mono.wav"
#define ARPEGGIO_8000_MONO_FILE_NAME		"sounds/arpeggio_8000_mono.wav"
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

static void fileplay_eof(void *user_data, MSFilter *f, unsigned int event, void *event_data) {
	if (event == MS_FILE_PLAYER_EOF) {
		int *done = (int *)user_data;
		*done = TRUE;
	}
	MS_UNUSED(f), MS_UNUSED(event_data);
}

static void fileplay_soundwrite(const char *filename) {
	MSConnectionHelper h;
	bool_t need_resampler = FALSE;
	unsigned int filter_mask = FILTER_MASK_FILEPLAY | FILTER_MASK_SOUNDWRITE;
	int sample_rate = 8000;
	int nchannels = 1;
	int done = FALSE;
138
    int elapsed = 0;
139 140 141

	ms_filter_reset_statistics();
	ms_tester_create_ticker();
142
	ms_tester_create_filters(filter_mask, ms_factory_get_fallback());
143
	ms_filter_add_notify_callback(ms_tester_fileplay, fileplay_eof, &done,TRUE);
144 145 146 147
	ms_filter_call_method_noarg(ms_tester_fileplay, MS_FILE_PLAYER_CLOSE);
	ms_filter_call_method(ms_tester_fileplay, MS_FILE_PLAYER_OPEN, (void *)filename);
	ms_filter_call_method(ms_tester_fileplay, MS_FILTER_GET_SAMPLE_RATE, &sample_rate);
	ms_filter_call_method(ms_tester_fileplay, MS_FILTER_GET_NCHANNELS, &nchannels);
148
	if (ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_SET_SAMPLE_RATE, &sample_rate) != 0) {
149
		int soundwrite_sample_rate = 48000;
150
		ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_GET_SAMPLE_RATE, &soundwrite_sample_rate);
151 152 153 154 155 156 157 158
		if (sample_rate != soundwrite_sample_rate) need_resampler = TRUE;
	}
	if (ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_SET_NCHANNELS, &nchannels) != 0) {
		int soundwrite_nchannels = 1;
		ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_GET_NCHANNELS, &soundwrite_nchannels);
		if (nchannels != soundwrite_nchannels) need_resampler = TRUE;
	}
	if (need_resampler == TRUE) {
159
		ms_tester_create_filters(FILTER_MASK_RESAMPLER, ms_factory_get_fallback());
160 161 162 163 164 165 166 167 168 169 170
		configure_resampler(ms_tester_resampler, ms_tester_fileplay, ms_tester_soundwrite);
	}
	ms_filter_call_method_noarg(ms_tester_fileplay, MS_FILE_PLAYER_START);
	ms_connection_helper_start(&h);
	ms_connection_helper_link(&h, ms_tester_fileplay, -1, 0);
	if (need_resampler == TRUE) {
		ms_connection_helper_link(&h, ms_tester_resampler, 0, 0);
	}
	ms_connection_helper_link(&h, ms_tester_soundwrite, 0, -1);
	ms_ticker_attach(ms_tester_ticker, ms_tester_fileplay);

171 172 173

	while (done != TRUE && elapsed < 6000) {
        elapsed++;
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
		ms_usleep(10000);
	}

	ms_filter_call_method_noarg(ms_tester_fileplay, MS_FILE_PLAYER_CLOSE);
	ms_ticker_detach(ms_tester_ticker, ms_tester_fileplay);
	ms_connection_helper_start(&h);
	ms_connection_helper_unlink(&h, ms_tester_fileplay, -1, 0);
	if (need_resampler == TRUE) {
		ms_connection_helper_unlink(&h, ms_tester_resampler, 0, 0);
	}
	ms_connection_helper_unlink(&h, ms_tester_soundwrite, 0, -1);
	if (need_resampler == TRUE) {
		ms_tester_destroy_filters(FILTER_MASK_RESAMPLER);
	}
	ms_filter_log_statistics();
	ms_tester_destroy_filters(filter_mask);
	ms_tester_destroy_ticker();
}

193
static void fileplay_soundwrite_from_file(const char *filepath){
194
	char* file = bc_tester_res(filepath);
195
    fileplay_soundwrite(file);
196
    free(file);
197 198
}

199
static void fileplay_soundwrite_48000_stereo(void) {
200
	fileplay_soundwrite_from_file(CHIMES_48000_STEREO_FILE_NAME);
201 202
}

203
static void fileplay_soundwrite_44100_stereo(void) {
204
	fileplay_soundwrite_from_file(BIRD_44100_STEREO_FILE_NAME);
205 206
}

207
static void fileplay_soundwrite_16000_stereo(void) {
208
	fileplay_soundwrite_from_file(PUNCH_16000_STEREO_FILE_NAME);
209 210 211
}

static void fileplay_soundwrite_8000_stereo(void) {
212
	fileplay_soundwrite_from_file(PIANO_8000_STEREO_FILE_NAME);
213 214 215
}

static void fileplay_soundwrite_48000_mono(void) {
216
	fileplay_soundwrite_from_file(NYLON_48000_MONO_FILE_NAME);
217 218
}

219
static void fileplay_soundwrite_44100_mono(void) {
220
	fileplay_soundwrite_from_file(OWL_44100_MONO_FILE_NAME);
221 222
}

223
static void fileplay_soundwrite_16000_mono(void) {
224
	fileplay_soundwrite_from_file(LASERROCKET_16000_MONO_FILE_NAME);
225 226 227
}

static void fileplay_soundwrite_8000_mono(void) {
228
	fileplay_soundwrite_from_file(ARPEGGIO_8000_MONO_FILE_NAME);
229 230
}

231 232 233 234 235 236 237 238 239
static void soundread_soundwrite(void) {
	MSConnectionHelper h;
	bool_t need_resampler = FALSE;
	unsigned int filter_mask = FILTER_MASK_SOUNDREAD | FILTER_MASK_SOUNDWRITE;
	int sample_rate = 8000;
	int nchannels = 1;

	ms_filter_reset_statistics();
	ms_tester_create_ticker();
240
	ms_tester_create_filters(filter_mask, ms_factory_get_fallback());
241 242
	ms_filter_call_method(ms_tester_soundread, MS_FILTER_GET_SAMPLE_RATE, &sample_rate);
	ms_filter_call_method(ms_tester_soundread, MS_FILTER_GET_NCHANNELS, &nchannels);
243
	if (ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_SET_SAMPLE_RATE, &sample_rate) != 0) {
244
		int soundwrite_sample_rate = 48000;
245
		ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_GET_SAMPLE_RATE, &soundwrite_sample_rate);
246 247 248 249 250 251 252 253
		if (sample_rate != soundwrite_sample_rate) need_resampler = TRUE;
	}
	if (ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_SET_NCHANNELS, &nchannels) != 0) {
		int soundwrite_nchannels = 1;
		ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_GET_NCHANNELS, &soundwrite_nchannels);
		if (nchannels != soundwrite_nchannels) need_resampler = TRUE;
	}
	if (need_resampler == TRUE) {
254
		ms_tester_create_filters(FILTER_MASK_RESAMPLER, ms_factory_get_fallback());
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
		configure_resampler(ms_tester_resampler, ms_tester_soundread, ms_tester_soundwrite);
	}
	ms_connection_helper_start(&h);
	ms_connection_helper_link(&h, ms_tester_soundread, -1, 0);
	if (need_resampler == TRUE) {
		ms_connection_helper_link(&h, ms_tester_resampler, 0, 0);
	}
	ms_connection_helper_link(&h, ms_tester_soundwrite, 0, -1);
	ms_ticker_attach(ms_tester_ticker, ms_tester_soundread);

	ms_sleep(3);

	ms_ticker_detach(ms_tester_ticker, ms_tester_soundread);
	ms_connection_helper_start(&h);
	ms_connection_helper_unlink(&h, ms_tester_soundread, -1, 0);
	if (need_resampler == TRUE) {
		ms_connection_helper_unlink(&h, ms_tester_resampler, 0, 0);
	}
	ms_connection_helper_unlink(&h, ms_tester_soundwrite, 0, -1);
	if (need_resampler == TRUE) {
		ms_tester_destroy_filters(FILTER_MASK_RESAMPLER);
	}
	ms_filter_log_statistics();
	ms_tester_destroy_filters(filter_mask);
	ms_tester_destroy_ticker();
}

282
static void fileplay_bv16enc_bv16dec_soundwrite(void) {
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	if (ms_factory_codec_supported(ms_factory_get_fallback(), "bv16")) {
		MSConnectionHelper h;
		MSFilter *read_resampler = NULL, *write_resampler = NULL;
		bool_t need_read_resampler = FALSE, need_write_resampler = TRUE;
		unsigned int filter_mask = FILTER_MASK_FILEPLAY  | FILTER_MASK_ENCODER | FILTER_MASK_DECODER | FILTER_MASK_SOUNDWRITE;
		int sample_rate = 8000;
		int nchannels = 1;
		int done = FALSE;
		const char* filepath = ARPEGGIO_8000_MONO_FILE_NAME;

		char* filename = bc_tester_res(filepath);

		int elapsed = 0;

		ms_filter_reset_statistics();
		ms_tester_create_ticker();
		ms_tester_codec_mime = "bv16";
		ms_tester_create_filters(filter_mask, ms_factory_get_fallback());


		//file
		ms_filter_add_notify_callback(ms_tester_fileplay, fileplay_eof, &done,TRUE);
		ms_filter_call_method_noarg(ms_tester_fileplay, MS_FILE_PLAYER_CLOSE);

		ms_filter_call_method(ms_tester_fileplay, MS_FILE_PLAYER_OPEN, (void *)filename);
		ms_filter_call_method_noarg(ms_tester_fileplay, MS_FILE_PLAYER_START);
		ms_filter_call_method(ms_tester_fileplay, MS_FILTER_GET_SAMPLE_RATE, &sample_rate);
		ms_filter_call_method(ms_tester_fileplay, MS_FILTER_GET_NCHANNELS, &nchannels);

		ms_filter_call_method(ms_tester_encoder, MS_FILTER_GET_BITRATE, &sample_rate);
		ms_filter_call_method(ms_tester_decoder, MS_FILTER_SET_BITRATE, &sample_rate);


		if (need_read_resampler == TRUE) {
			ms_tester_create_filters(FILTER_MASK_RESAMPLER, ms_factory_get_fallback());
			configure_resampler(ms_tester_resampler, ms_tester_fileplay, ms_tester_encoder);
		}


		ms_connection_helper_start(&h);
		ms_connection_helper_link(&h, ms_tester_fileplay, -1, 0);
		if (need_read_resampler == TRUE) {
			ms_connection_helper_link(&h, ms_tester_resampler, 0, 0);
		}

		if (ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_SET_BITRATE, &sample_rate) != 0) {
			int soundwrite_sample_rate = 8000;
			ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_GET_BITRATE, &soundwrite_sample_rate);
			if (sample_rate != soundwrite_sample_rate) need_write_resampler = TRUE;
		}
		if (ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_SET_NCHANNELS, &nchannels) != 0) {
			int soundwrite_nchannels = 1;
			ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_GET_NCHANNELS, &soundwrite_nchannels);
			if (nchannels != soundwrite_nchannels) need_write_resampler = TRUE;
		}
		if (need_write_resampler == TRUE) {
			ms_tester_create_filter(&write_resampler, MS_RESAMPLE_ID, ms_factory_get_fallback());
			configure_resampler(write_resampler, ms_tester_decoder, ms_tester_soundwrite);
		}

		ms_connection_helper_link(&h, ms_tester_encoder, 0, 0);
		ms_connection_helper_link(&h, ms_tester_decoder, 0, 0);
		if (need_write_resampler == TRUE) {
			ms_connection_helper_link(&h, write_resampler, 0, 0);
		}
		ms_connection_helper_link(&h, ms_tester_soundwrite, 0, -1);
		ms_ticker_attach(ms_tester_ticker, ms_tester_fileplay);


		while (done != TRUE && elapsed < 6000) {
			elapsed++;
			ms_usleep(10000);
		}

		ms_filter_call_method_noarg(ms_tester_fileplay, MS_FILE_PLAYER_CLOSE);
		ms_ticker_detach(ms_tester_ticker, ms_tester_fileplay);
		ms_connection_helper_start(&h);
		ms_connection_helper_unlink(&h, ms_tester_fileplay, -1, 0);
		if (need_read_resampler == TRUE) {
			ms_connection_helper_unlink(&h, ms_tester_resampler, 0, 0);
		}
		ms_connection_helper_unlink(&h, ms_tester_encoder, 0, 0);
		ms_connection_helper_unlink(&h, ms_tester_decoder, 0, 0);
		if (need_write_resampler == TRUE) {
			ms_connection_helper_unlink(&h, write_resampler, 0, 0);
		}
		ms_connection_helper_unlink(&h, ms_tester_soundwrite, 0, -1);
		if (need_read_resampler == TRUE) {
			ms_tester_destroy_filter(&read_resampler);
		}
		if (need_write_resampler == TRUE) {
			ms_tester_destroy_filter(&write_resampler);
		}
		ms_filter_log_statistics();
		ms_tester_destroy_filters(filter_mask);
		ms_tester_destroy_ticker();
		free(filename);
380 381 382
	}
}

383 384 385 386 387 388 389 390 391 392 393
static void soundread_speexenc_speexdec_soundwrite(void) {
	MSConnectionHelper h;
	MSFilter *read_resampler = NULL, *write_resampler = NULL;
	bool_t need_read_resampler = FALSE, need_write_resampler = FALSE;
	unsigned int filter_mask = FILTER_MASK_SOUNDREAD | FILTER_MASK_ENCODER | FILTER_MASK_DECODER | FILTER_MASK_SOUNDWRITE;
	int sample_rate = 8000;
	int nchannels = 1;

	ms_filter_reset_statistics();
	ms_tester_create_ticker();
	ms_tester_codec_mime = "speex";
394
	ms_tester_create_filters(filter_mask, ms_factory_get_fallback());
395 396 397 398 399 400 401 402 403 404 405 406 407
	ms_filter_call_method(ms_tester_encoder, MS_FILTER_GET_BITRATE, &sample_rate);
	ms_filter_call_method(ms_tester_decoder, MS_FILTER_SET_BITRATE, &sample_rate);
	if (ms_filter_call_method(ms_tester_soundread, MS_FILTER_SET_BITRATE, &sample_rate) != 0) {
		int soundread_sample_rate = 48000;
		ms_filter_call_method(ms_tester_soundread, MS_FILTER_GET_BITRATE, &soundread_sample_rate);
		if (sample_rate != soundread_sample_rate) need_read_resampler = TRUE;
	}
	if (ms_filter_call_method(ms_tester_soundread, MS_FILTER_SET_NCHANNELS, &nchannels) != 0) {
		int soundread_nchannels = 1;
		ms_filter_call_method(ms_tester_soundread, MS_FILTER_GET_NCHANNELS, &soundread_nchannels);
		if (nchannels != soundread_nchannels) need_read_resampler = TRUE;
	}
	if (need_read_resampler == TRUE) {
408
		ms_tester_create_filter(&read_resampler, MS_RESAMPLE_ID, ms_factory_get_fallback());
409 410 411 412 413 414 415 416 417 418 419 420 421
		configure_resampler(read_resampler, ms_tester_soundread, ms_tester_encoder);
	}
	if (ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_SET_BITRATE, &sample_rate) != 0) {
		int soundwrite_sample_rate = 48000;
		ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_GET_BITRATE, &soundwrite_sample_rate);
		if (sample_rate != soundwrite_sample_rate) need_write_resampler = TRUE;
	}
	if (ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_SET_NCHANNELS, &nchannels) != 0) {
		int soundwrite_nchannels = 1;
		ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_GET_NCHANNELS, &soundwrite_nchannels);
		if (nchannels != soundwrite_nchannels) need_write_resampler = TRUE;
	}
	if (need_write_resampler == TRUE) {
422
		ms_tester_create_filter(&write_resampler, MS_RESAMPLE_ID, ms_factory_get_fallback());
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
		configure_resampler(write_resampler, ms_tester_decoder, ms_tester_soundwrite);
	}
	ms_connection_helper_start(&h);
	ms_connection_helper_link(&h, ms_tester_soundread, -1, 0);
	if (need_read_resampler == TRUE) {
		ms_connection_helper_link(&h, read_resampler, 0, 0);
	}
	ms_connection_helper_link(&h, ms_tester_encoder, 0, 0);
	ms_connection_helper_link(&h, ms_tester_decoder, 0, 0);
	if (need_write_resampler == TRUE) {
		ms_connection_helper_link(&h, write_resampler, 0, 0);
	}
	ms_connection_helper_link(&h, ms_tester_soundwrite, 0, -1);
	ms_ticker_attach(ms_tester_ticker, ms_tester_soundread);

	ms_sleep(5);

	ms_ticker_detach(ms_tester_ticker, ms_tester_soundread);
	ms_connection_helper_start(&h);
	ms_connection_helper_unlink(&h, ms_tester_soundread, -1, 0);
	if (need_read_resampler == TRUE) {
		ms_connection_helper_unlink(&h, read_resampler, 0, 0);
	}
	ms_connection_helper_unlink(&h, ms_tester_encoder, 0, 0);
	ms_connection_helper_unlink(&h, ms_tester_decoder, 0, 0);
	if (need_write_resampler == TRUE) {
		ms_connection_helper_unlink(&h, write_resampler, 0, 0);
	}
	ms_connection_helper_unlink(&h, ms_tester_soundwrite, 0, -1);
	if (need_read_resampler == TRUE) {
		ms_tester_destroy_filter(&read_resampler);
	}
	if (need_write_resampler == TRUE) {
		ms_tester_destroy_filter(&write_resampler);
	}
	ms_filter_log_statistics();
	ms_tester_destroy_filters(filter_mask);
	ms_tester_destroy_ticker();
}

463
#define SOUNDREAD_FILE_NAME "soundread_file.raw"
464 465 466 467 468 469 470 471

static void soundread_filerec_fileplay_soundwrite(void) {
	MSConnectionHelper h;
	unsigned int filter_mask = FILTER_MASK_SOUNDREAD | FILTER_MASK_FILEREC | FILTER_MASK_FILEPLAY | FILTER_MASK_SOUNDWRITE;
	int capture_sample_rate = 8000;
	int playback_sample_rate = 8000;
	int capture_nchannels = 1;
	int playback_nchannels = 1;
472
	char *writable_filename = bc_tester_file(SOUNDREAD_FILE_NAME);
473 474 475

	ms_filter_reset_statistics();
	ms_tester_create_ticker();
476
	ms_tester_create_filters(filter_mask, ms_factory_get_fallback());
477 478 479 480 481 482 483

	// Write audio capture to a file
	ms_filter_call_method(ms_tester_soundread, MS_FILTER_GET_SAMPLE_RATE, &capture_sample_rate);
	ms_filter_call_method(ms_tester_soundread, MS_FILTER_GET_NCHANNELS, &capture_nchannels);
	ms_filter_call_method(ms_tester_filerec, MS_FILTER_SET_SAMPLE_RATE, &capture_sample_rate);
	ms_filter_call_method(ms_tester_filerec, MS_FILTER_SET_NCHANNELS, &capture_nchannels);
	ms_filter_call_method_noarg(ms_tester_filerec, MS_FILE_REC_CLOSE);
484
	ms_filter_call_method(ms_tester_filerec, MS_FILE_REC_OPEN, writable_filename);
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
	ms_filter_call_method_noarg(ms_tester_filerec, MS_FILE_REC_START);
	ms_connection_helper_start(&h);
	ms_connection_helper_link(&h, ms_tester_soundread, -1, 0);
	ms_connection_helper_link(&h, ms_tester_filerec, 0, -1);
	ms_ticker_attach(ms_tester_ticker, ms_tester_soundread);

	ms_sleep(4);

	ms_filter_call_method_noarg(ms_tester_filerec, MS_FILE_REC_CLOSE);
	ms_ticker_detach(ms_tester_ticker, ms_tester_soundread);
	ms_connection_helper_start(&h);
	ms_connection_helper_unlink(&h, ms_tester_soundread, -1, 0);
	ms_connection_helper_unlink(&h, ms_tester_filerec, 0, -1);

	// Read the previous file and play it
	ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_GET_SAMPLE_RATE, &playback_sample_rate);
	ms_filter_call_method(ms_tester_soundwrite, MS_FILTER_GET_NCHANNELS, &playback_nchannels);
	if ((capture_sample_rate != playback_sample_rate) || (capture_nchannels != playback_nchannels)) {
503
		ms_tester_create_filter(&ms_tester_resampler, MS_RESAMPLE_ID, ms_factory_get_fallback());
504 505
	}
	ms_filter_call_method_noarg(ms_tester_fileplay, MS_FILE_PLAYER_CLOSE);
506
	ms_filter_call_method(ms_tester_fileplay, MS_FILE_PLAYER_OPEN, writable_filename);
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
	ms_filter_call_method_noarg(ms_tester_fileplay, MS_FILE_PLAYER_START);
	ms_connection_helper_start(&h);
	ms_connection_helper_link(&h, ms_tester_fileplay, -1, 0);
	if (ms_tester_resampler != NULL) {
		ms_connection_helper_link(&h, ms_tester_resampler, 0, 0);
		ms_filter_call_method(ms_tester_resampler, MS_FILTER_SET_SAMPLE_RATE, &capture_sample_rate);
		ms_filter_call_method(ms_tester_resampler, MS_FILTER_SET_OUTPUT_SAMPLE_RATE, &playback_sample_rate);
		ms_filter_call_method(ms_tester_resampler, MS_FILTER_SET_NCHANNELS, &capture_nchannels);
		ms_filter_call_method(ms_tester_resampler, MS_FILTER_SET_OUTPUT_NCHANNELS, &capture_nchannels);
	}
	ms_connection_helper_link(&h, ms_tester_soundwrite, 0, -1);
	ms_ticker_attach(ms_tester_ticker, ms_tester_fileplay);

	ms_sleep(4);

	ms_filter_call_method_noarg(ms_tester_fileplay, MS_FILE_PLAYER_CLOSE);
	ms_ticker_detach(ms_tester_ticker, ms_tester_fileplay);
	ms_connection_helper_start(&h);
	ms_connection_helper_unlink(&h, ms_tester_fileplay, -1, 0);
	if (ms_tester_resampler != NULL) {
		ms_connection_helper_unlink(&h, ms_tester_resampler, 0, 0);
	}
	ms_connection_helper_unlink(&h, ms_tester_soundwrite, 0, -1);
	ms_filter_log_statistics();
	ms_tester_destroy_filters(filter_mask);
	ms_tester_destroy_ticker();

534
	unlink(writable_filename);
535
    free(writable_filename);
536 537
}

Ghislain MARY's avatar
Ghislain MARY committed
538 539

test_t sound_card_tests[] = {
540 541 542 543 544 545 546 547 548 549 550 551 552
	TEST_NO_TAG("dtmfgen-soundwrite", dtmfgen_soundwrite),
	TEST_NO_TAG("fileplay-soundwrite-48000-stereo", fileplay_soundwrite_48000_stereo),
	TEST_NO_TAG("fileplay-soundwrite-44100-stereo", fileplay_soundwrite_44100_stereo),
	TEST_NO_TAG("fileplay-soundwrite-16000-stereo", fileplay_soundwrite_16000_stereo),
	TEST_NO_TAG("fileplay-soundwrite-8000-stereo", fileplay_soundwrite_8000_stereo),
	TEST_NO_TAG("fileplay-soundwrite-48000-mono", fileplay_soundwrite_48000_mono),
	TEST_NO_TAG("fileplay-soundwrite-44100-mono", fileplay_soundwrite_44100_mono),
	TEST_NO_TAG("fileplay-soundwrite-16000-mono", fileplay_soundwrite_16000_mono),
	TEST_NO_TAG("fileplay-soundwrite-8000-mono", fileplay_soundwrite_8000_mono),
	TEST_NO_TAG("soundread-soundwrite", soundread_soundwrite),
	TEST_NO_TAG("soundread-speexenc-speexdec-soundwrite", soundread_speexenc_speexdec_soundwrite),
	TEST_NO_TAG("soundread-filerec-fileplay-soundwrite", soundread_filerec_fileplay_soundwrite),
	TEST_NO_TAG("fileplay-bv16enc-bv16dec-soundwrite", fileplay_bv16enc_bv16dec_soundwrite)
Ghislain MARY's avatar
Ghislain MARY committed
553 554 555 556
};

test_suite_t sound_card_test_suite = {
	"Sound Card",
557 558 559 560
	sound_card_tester_before_all,
	sound_card_tester_after_all,
	NULL,
	NULL,
Ghislain MARY's avatar
Ghislain MARY committed
561 562 563
	sizeof(sound_card_tests) / sizeof(sound_card_tests[0]),
	sound_card_tests
};