mediastreamer2_audio_stream_tester.c 12.2 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*
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
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#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 "private.h"
#include "mediastreamer2_tester.h"
#include "mediastreamer2_tester_private.h"

#include <stdio.h>
#include "CUnit/Basic.h"


#ifdef _MSC_VER
#define unlink _unlink
#endif

static RtpProfile rtp_profile;

40
#define OPUS_PAYLOAD_TYPE    121
jehan's avatar
jehan committed
41
#define SPEEX16_PAYLOAD_TYPE 122
42
#define SILK16_PAYLOAD_TYPE  123
43
#define PCMA8_PAYLOAD_TYPE 8
jehan's avatar
jehan committed
44 45 46 47 48 49 50 51 52

static int tester_init(void) {
	ms_init();
	ms_filter_enable_statistics(TRUE);
	ortp_init();
	rtp_profile_set_payload (&rtp_profile,0,&payload_type_pcmu8000);
	rtp_profile_set_payload (&rtp_profile,OPUS_PAYLOAD_TYPE,&payload_type_opus);
	rtp_profile_set_payload (&rtp_profile,SPEEX16_PAYLOAD_TYPE,&payload_type_speex_wb);
	rtp_profile_set_payload (&rtp_profile,SILK16_PAYLOAD_TYPE,&payload_type_silk_wb);
53
	rtp_profile_set_payload (&rtp_profile,PCMA8_PAYLOAD_TYPE,&payload_type_pcma8000);
jehan's avatar
jehan committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

	return 0;
}

static int tester_cleanup(void) {
	ms_exit();
	rtp_profile_clear_all(&rtp_profile);
	return 0;
}

#define MARIELLE_RTP_PORT 2564
#define MARIELLE_RTCP_PORT 2565
#define MARIELLE_IP "127.0.0.1"

#define MARGAUX_RTP_PORT 9864
#define MARGAUX_RTCP_PORT 9865
#define MARGAUX_IP "127.0.0.1"

72 73 74 75
#define HELLO_8K_1S_FILE SOUND_FILE_PATH "hello8000-1s.wav"
#define HELLO_16K_1S_FILE SOUND_FILE_PATH "hello16000-1s.wav"
#define RECORDED_8K_1S_FILE WRITE_FILE_PATH "recorded_hello8000-1s.wav"
#define RECORDED_16K_1S_FILE WRITE_FILE_PATH "recorded_hello16000-1s.wav"
jehan's avatar
jehan committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

typedef struct _stats_t {
	rtp_stats_t rtp;
	int number_of_EndOfFile;
} stats_t;
static void reset_stats(stats_t* s) {
	memset(s,0,sizeof(stats_t));
}


bool_t wait_for_list(MSList* mss,int* counter,int value,int timeout_ms) {
	int retry=0;
	MSList* iterator;
	while (*counter<value && retry++ <timeout_ms/100) {
		 for (iterator=mss;iterator!=NULL;iterator=iterator->next) {
			 MediaStream* stream = (MediaStream*)(iterator->data);
			 media_stream_iterate(stream);
			 if (retry%10==0) {
				 ms_message("stream [%p] bandwidth usage: [d=%.1f,u=%.1f] kbit/sec"	, stream
95 96
																					, media_stream_get_down_bw(stream)/1000
																					, media_stream_get_up_bw(stream)/1000);
jehan's avatar
jehan committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

			 }
		 }
		ms_usleep(100000);

	}
	if(*counter<value) return FALSE;
	else return TRUE;
}

bool_t wait_for_until(MediaStream* ms_1, MediaStream* ms_2,int* counter,int value,int timeout) {
	MSList* mss=NULL;
	bool_t result;
	if (ms_1)
		mss=ms_list_append(mss,ms_1);
	if (ms_2)
		mss=ms_list_append(mss,ms_2);
	result=wait_for_list(mss,counter,value,timeout);
	ms_list_free(mss);
	return result;
}
bool_t wait_for(MediaStream* ms_1, MediaStream* ms_2,int* counter,int value)  {
	return wait_for_until( ms_1, ms_2,counter,value,2000);
}

static void notify_cb(void *user_data, MSFilter *f, unsigned int event, void *eventdata) {

	stats_t* stats = (stats_t*)user_data;
	switch (event) {
	case MS_FILE_PLAYER_EOF: {
		ms_message("EndOfFile received");
		stats->number_of_EndOfFile++;
		break;
	}
	break;
	}

}

typedef struct _stream_manager_t {
	AudioStream* stream;
	int local_rtp;
	int local_rtcp;
	stats_t stats;

} stream_manager_t ;
static stream_manager_t * stream_manager_new() {
	stream_manager_t * mgr =  ms_new0(stream_manager_t,1);
	mgr->local_rtp= (rand() % ((2^16)-1024) + 1024) & ~0x1;
	mgr->local_rtcp=mgr->local_rtp+1;
	mgr->stream = audio_stream_new (mgr->local_rtp, mgr->local_rtcp,FALSE);
	return mgr;

}
static void stream_manager_delete(stream_manager_t * mgr) {
	audio_stream_stop(mgr->stream);
	ms_free(mgr);
}


static void stream_manager_start(	stream_manager_t * mgr
									,int payload_type
									,int remote_port
									,int target_bitrate
									,const char* player_file
									,const char* recorder_file) {
	media_stream_set_target_network_bitrate(&mgr->stream->ms,target_bitrate);
164

jehan's avatar
jehan committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	CU_ASSERT_EQUAL(audio_stream_start_full(mgr->stream
												, &rtp_profile
												, "127.0.0.1"
												, remote_port
												, "127.0.0.1"
												, remote_port+1
												, payload_type
												, 50
												, player_file
												, recorder_file
												, NULL
												, NULL
												, 0),0);

}
static void basic_audio_stream() {
	AudioStream * 	marielle = audio_stream_new (MARIELLE_RTP_PORT, MARIELLE_RTCP_PORT,FALSE);
	stats_t marielle_stats;
	AudioStream * 	margaux = audio_stream_new (MARGAUX_RTP_PORT,MARGAUX_RTCP_PORT, FALSE);
	stats_t margaux_stats;
	RtpProfile* profile = rtp_profile_new("default profile");

	reset_stats(&marielle_stats);
	reset_stats(&margaux_stats);

	rtp_profile_set_payload (profile,0,&payload_type_pcmu8000);

192
	CU_ASSERT_EQUAL(audio_stream_start_full(margaux
jehan's avatar
jehan committed
193
											, profile
194 195 196 197
											, MARIELLE_IP
											, MARIELLE_RTP_PORT
											, MARIELLE_IP
											, MARIELLE_RTCP_PORT
jehan's avatar
jehan committed
198 199 200
											, 0
											, 50
											, NULL
201
											, RECORDED_8K_1S_FILE
jehan's avatar
jehan committed
202 203 204 205
											, NULL
											, NULL
											, 0),0);

206
	CU_ASSERT_EQUAL(audio_stream_start_full(marielle
jehan's avatar
jehan committed
207
											, profile
208 209 210 211
											, MARGAUX_IP
											, MARGAUX_RTP_PORT
											, MARGAUX_IP
											, MARGAUX_RTCP_PORT
jehan's avatar
jehan committed
212 213
											, 0
											, 50
214
											, HELLO_8K_1S_FILE
jehan's avatar
jehan committed
215 216 217 218 219
											, NULL
											, NULL
											, NULL
											, 0),0);

220 221
	ms_filter_add_notify_callback(marielle->soundread, notify_cb, &marielle_stats,TRUE);

jehan's avatar
jehan committed
222 223 224 225 226
	CU_ASSERT_TRUE(wait_for_until(&marielle->ms,&margaux->ms,&marielle_stats.number_of_EndOfFile,1,12000));

	audio_stream_get_local_rtp_stats(marielle,&marielle_stats.rtp);
	audio_stream_get_local_rtp_stats(margaux,&margaux_stats.rtp);

227
	/* No packet loss is assumed */
jehan's avatar
jehan committed
228 229 230 231 232 233 234 235 236
	CU_ASSERT_EQUAL(marielle_stats.rtp.sent,margaux_stats.rtp.recv);

	audio_stream_stop(marielle);
	audio_stream_stop(margaux);

	unlink(RECORDED_8K_1S_FILE);
}

#define EDGE_BW 10000
237
#define THIRDGENERATION_BW 200000
jehan's avatar
jehan committed
238

239
static float adaptive_audio_stream(int codec_payload, int initial_bitrate,int target_bw, int max_recv_rtcp_packet) {
jehan's avatar
jehan committed
240 241 242 243 244 245 246 247 248
	stream_manager_t * marielle = stream_manager_new();
	stream_manager_t * margaux = stream_manager_new();
	int pause_time=0;

	OrtpNetworkSimulatorParams params={0};
	params.enabled=TRUE;
	params.loss_rate=0;
	params.max_bandwidth=target_bw;
	params.max_buffer_size=initial_bitrate;
249 250 251 252
	float bw_usage_ratio;
	// this variable should not be changed, since algorithm results rely on this value
	// (the bigger it is, the more accurate is bandwidth estimation)
	int rtcp_interval = RTCP_DEFAULT_REPORT_INTERVAL;
jehan's avatar
jehan committed
253 254 255 256 257 258 259
	float marielle_send_bw;

	media_stream_enable_adaptive_bitrate_control(&marielle->stream->ms,TRUE);

	stream_manager_start(marielle,codec_payload, margaux->local_rtp,initial_bitrate,HELLO_16K_1S_FILE,NULL);
	ms_filter_call_method(marielle->stream->soundread,MS_FILE_PLAYER_LOOP,&pause_time);

260
	stream_manager_start(margaux,codec_payload, marielle->local_rtp,-1,NULL,RECORDED_16K_1S_FILE);
261
	rtp_session_enable_network_simulation(margaux->stream->ms.sessions.rtp_session,&params);
jehan's avatar
jehan committed
262

263
	rtp_session_set_rtcp_report_interval(margaux->stream->ms.sessions.rtp_session, rtcp_interval);
jehan's avatar
jehan committed
264 265 266
	wait_for_until(&marielle->stream->ms,&margaux->stream->ms,&marielle->stats.number_of_EndOfFile,10,rtcp_interval*max_recv_rtcp_packet);

	marielle_send_bw=media_stream_get_up_bw(&marielle->stream->ms);
267 268
	bw_usage_ratio=marielle_send_bw/params.max_bandwidth;
	ms_message("marielle sent bw=[%f], target was [%f] bw_usage_ratio [%f]",marielle_send_bw,params.max_bandwidth,bw_usage_ratio);
jehan's avatar
jehan committed
269 270 271 272

	stream_manager_delete(marielle);
	stream_manager_delete(margaux);

273
	unlink(RECORDED_16K_1S_FILE);
274 275

	return bw_usage_ratio;
jehan's avatar
jehan committed
276
}
277

278 279
#define CU_ASSERT_IN_RANGE(value, inf, sup) CU_ASSERT_TRUE(value >= inf); CU_ASSERT_TRUE(value <= sup)

jehan's avatar
jehan committed
280
static void adaptive_opus_audio_stream()  {
281 282
	bool_t supported = ms_filter_codec_supported("opus");
	if( supported ) {
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
		// at 8KHz -> 24kb/s
		// at 48KHz -> 48kb/s
		float bw_usage;

		// on EDGEBW, both should be overconsumming
		bw_usage = adaptive_audio_stream(OPUS_PAYLOAD_TYPE, 8000, EDGE_BW, 14);
		CU_ASSERT_IN_RANGE(bw_usage, 2.f, 3.f); // bad! since this codec cant change its ptime and it is the lower bitrate, no improvement can occur
		bw_usage = adaptive_audio_stream(OPUS_PAYLOAD_TYPE, 48000, EDGE_BW, 11);
		CU_ASSERT_IN_RANGE(bw_usage, 1.f, 1.2f); // bad!

		// on 3G BW, both should be at max
		bw_usage = adaptive_audio_stream(OPUS_PAYLOAD_TYPE, 8000, THIRDGENERATION_BW, 5);
		CU_ASSERT_IN_RANGE(bw_usage, .1f, .15f);
		bw_usage = adaptive_audio_stream(OPUS_PAYLOAD_TYPE, 48000, THIRDGENERATION_BW, 5);
		CU_ASSERT_IN_RANGE(bw_usage, .2f, .3f);
298
	}
jehan's avatar
jehan committed
299
}
300

jehan's avatar
jehan committed
301
static void adaptive_speek16_audio_stream()  {
302 303
	bool_t supported = ms_filter_codec_supported("speex");
	if( supported ) {
304 305 306 307 308 309 310 311 312 313
		// at 16KHz -> 20 kb/s
		// at 32KHz -> 30 kb/s
		float bw_usage;

		bw_usage = adaptive_audio_stream(SPEEX16_PAYLOAD_TYPE, 32000, EDGE_BW, 12);
		CU_ASSERT_IN_RANGE(bw_usage, .9f, 1.f);
		bw_usage = adaptive_audio_stream(SPEEX16_PAYLOAD_TYPE, 16000, EDGE_BW, 8);
		CU_ASSERT_IN_RANGE(bw_usage, .9f, 1.f);
		bw_usage = adaptive_audio_stream(SPEEX16_PAYLOAD_TYPE, 32000, THIRDGENERATION_BW, 5);
		CU_ASSERT_IN_RANGE(bw_usage, .1f, .2f);
314
	}
jehan's avatar
jehan committed
315 316
}

317 318
static void adaptative_pcma_audio_stream() {
	bool_t supported = ms_filter_codec_supported("pcma");
319
	if( supported ) {
320 321 322 323 324 325 326 327
		// at 8KHz -> 80 kb/s
		float bw_usage;

		// yet non-adaptative codecs cannot respect low throughput limitations
		bw_usage = adaptive_audio_stream(PCMA8_PAYLOAD_TYPE, 8000, EDGE_BW, 10);
		CU_ASSERT_IN_RANGE(bw_usage,6.f, 8.f); // this is bad!
		bw_usage = adaptive_audio_stream(PCMA8_PAYLOAD_TYPE, 8000, THIRDGENERATION_BW, 5);
		CU_ASSERT_IN_RANGE(bw_usage, .3f, .5f);
328 329 330
	}
}

Simon Morlat's avatar
Simon Morlat committed
331
#if 0
332
static void audio_stream_dtmf(int codec_payload, int initial_bitrate,int target_bw, int max_recv_rtcp_packet) {
Simon Morlat's avatar
Simon Morlat committed
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
	stream_manager_t * marielle = stream_manager_new();
	stream_manager_t * margaux = stream_manager_new();
	int pause_time=0;

	OrtpNetworkSimulatorParams params={0};
	params.enabled=TRUE;
	params.loss_rate=0;
	params.max_bandwidth=target_bw;
	params.max_buffer_size=initial_bitrate;
	float recv_send_bw_ratio;
	int rtcp_interval = 1000;
	float marielle_send_bw;

	media_stream_enable_adaptive_bitrate_control(&marielle->stream->ms,TRUE);


	stream_manager_start(marielle,codec_payload, margaux->local_rtp,initial_bitrate,HELLO_16K_1S_FILE,NULL);
	ms_filter_call_method(marielle->stream->soundread,MS_FILE_PLAYER_LOOP,&pause_time);

	unlink("blibi.wav");
	stream_manager_start(margaux,codec_payload, marielle->local_rtp,-1,NULL,"blibi.wav");
	rtp_session_enable_network_simulation(margaux->stream->ms.session,&params);
	rtp_session_set_rtcp_report_interval(margaux->stream->ms.session, rtcp_interval);

	wait_for_until(&marielle->stream->ms,&margaux->stream->ms,&marielle->stats.number_of_EndOfFile,10,rtcp_interval*max_recv_rtcp_packet);

	marielle_send_bw=media_stream_get_up_bw(&marielle->stream->ms);
	recv_send_bw_ratio=params.max_bandwidth/marielle_send_bw;
	ms_message("marielle sent bw= [%f] , target was [%f] recv/send [%f]",marielle_send_bw,params.max_bandwidth,recv_send_bw_ratio);
	CU_ASSERT_TRUE(recv_send_bw_ratio>0.9);

	stream_manager_delete(marielle);
	stream_manager_delete(margaux);
366 367 368

}

Simon Morlat's avatar
Simon Morlat committed
369 370
#endif

371

jehan's avatar
jehan committed
372 373 374
static test_t tests[] = {
	{ "Basic audio stream", basic_audio_stream },
	{ "Adaptive audio stream [opus]", adaptive_opus_audio_stream },
375
	{ "Adaptive audio stream [speex]", adaptive_speek16_audio_stream },
376
	{ "Adaptive audio stream [pcma]", adaptative_pcma_audio_stream }
jehan's avatar
jehan committed
377 378 379 380 381 382 383 384 385
};

test_suite_t audio_stream_test_suite = {
	"AudioStream",
	tester_init,
	tester_cleanup,
	sizeof(tests) / sizeof(tests[0]),
	tests
};