mediastreamer2_jitterbuffer_tester.c 22.8 KB
Newer Older
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
/*
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.
*/

#ifdef HAVE_CONFIG_H
#include "mediastreamer-config.h"
#endif

#include "mediastreamer2/mediastream.h"
#include "mediastreamer2/dtmfgen.h"
#include "mediastreamer2/msfileplayer.h"
#include "mediastreamer2/mspcapfileplayer.h"
#include "mediastreamer2/msfilerec.h"
#include "mediastreamer2/msrtp.h"
#include "mediastreamer2/mstonedetector.h"
31
#include "mediastreamer2/mseventqueue.h"
32 33 34 35 36 37 38
#include "pcap_sender.h"
#include "private.h"
#include "mediastreamer2_tester.h"
#include "mediastreamer2_tester_private.h"

static RtpProfile *profile;
static AudioStream *receiver = NULL;
39

40
#ifdef HAVE_PCAP
41
static OrtpEvQueue *receiver_q = NULL;
42 43 44 45 46 47 48 49 50 51 52 53
static AudioStream *sender = NULL;
static VideoStream * receiverv = NULL;
static OrtpEvQueue *receiverv_q = NULL;

#endif

int RECEIVER_RTP_PORT;

#define RECEIVER_IP "127.0.0.1"

#define PIANO_FILE  "sounds/hello16000.wav"
#define RECORD_FILE "adaptive_jitter.wav"
54

55 56 57

static void set_random_ports(void){
	/*This allow multiple tester to be run on the same machine*/
58
	RECEIVER_RTP_PORT = rand()%(0xffff - 1024) + 1024;
59 60 61 62 63
}

int drifting_ticker(void *data, uint64_t virt_ticker_time){
	ortpTimeSpec ts;
	ortp_get_cur_time(&ts);
Erwan Croze's avatar
Erwan Croze committed
64
	return (int)(1 * ((ts.tv_sec * 1000LL) + ((ts.tv_nsec + 500000LL) / 1000000LL)));
65 66 67 68 69
}


static MSFactory *_factory = NULL;

70
static int tester_before_all(void) {
71 72 73 74 75 76 77 78
	_factory = ms_factory_new();
	ms_factory_init_voip(_factory);
	ms_factory_init_plugins(_factory);

	//ms_filter_enable_statistics(TRUE);
	ms_factory_enable_statistics(_factory, TRUE);
	profile = ms_tester_create_rtp_profile();
	set_random_ports();
79
	ms_factory_create_event_queue(_factory);
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	return 0;
}

static int tester_after_all(void) {
	ms_factory_destroy(_factory);
	return 0;
}

void iterate_stats_logger(MediaStream *ms, void *user_pointer) {
	ms_message("count=%u cum_loss=%ld late=%ld discarded=%lu"
			, ms->sessions.rtp_session->rtp.jittctl.count
			, (unsigned long)rtp_session_get_stats(ms->sessions.rtp_session)->cum_packet_loss
			, (unsigned long)rtp_session_get_stats(ms->sessions.rtp_session)->outoftime
			, (unsigned long)rtp_session_get_stats(ms->sessions.rtp_session)->discarded
			);
}

void test_setup(int payload, OrtpJitterBufferAlgorithm algo) {
	JBParameters params;

	/*if record file exists, delete it before starting to rewrite it*/
Erwan Croze's avatar
Erwan Croze committed
101
	if (bctbx_file_exist(RECORD_FILE) != -1) {
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
		unlink(RECORD_FILE);
	}

	BC_ASSERT_EQUAL(audio_stream_start_full(receiver
						, profile
						, 0
						, 0
						, 0
						, 0
						, payload
						, 50
						, NULL
						, RECORD_FILE
						, NULL
						, NULL
						, 0),0, int, "%d");


	rtp_session_get_jitter_buffer_params(receiver->ms.sessions.rtp_session, &params);
	params.buffer_algorithm = algo;
	params.refresh_ms = 5000;
	params.max_size = 1000;
124 125
	params.nom_size = 80;
	params.min_size = 40;
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
	//Warning: if you use an max_size of more than 500 ms, you also need to increase
	//			number of packets allowing in queue. Otherwise many packets will be
	//			discarded by the queue.
	params.max_packets = MAX(params.max_packets,3*params.max_size/10);
	rtp_session_set_jitter_buffer_params(receiver->ms.sessions.rtp_session, &params);
}

void test_clean(const char* file, int clock_rate) {
	struct rtp_stats *stats = &receiver->ms.sessions.rtp_session->stats;
	JBParameters params;
	rtp_session_get_jitter_buffer_params(receiver->ms.sessions.rtp_session, &params);

	ms_message("file=%s, algo=%s, rate=%d, refresh_ms=%d, max_size_ms=%d\n"
			"bad=%ld, cum_loss=%ld, late=%lu, discarded=%lu"
		, file, params.buffer_algorithm==OrtpJitterBufferRecursiveLeastSquare?"rls":"mean"
		, clock_rate, params.refresh_ms, params.max_size
		, (unsigned long)stats->bad
		, (unsigned long)stats->cum_packet_loss
		, (unsigned long)stats->outoftime
		, (unsigned long)stats->discarded
	);
	audio_stream_stop(receiver);
}

#ifdef HAVE_PCAP

152 153
static int congestion_count = 0;
static int running_streams = 0;
154 155

static void end_of_pcap(MSPCAPSender *s, void* user_data)  {
156
	running_streams--;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
}

static void process_queue(OrtpEvQueue *evq) {
	OrtpEvent *ev;
	
	while((ev = ortp_ev_queue_get(evq)) != NULL){
		if (ortp_event_get_type(ev) == ORTP_EVENT_CONGESTION_STATE_CHANGED){
			OrtpEventData *evd = ortp_event_get_data(ev);
			ms_message("Congestion detected: %s", evd->info.congestion_detected ? "TRUE" : "FALSE");
			if (evd->info.congestion_detected) {
				congestion_count++;
			}
		}
		ortp_event_destroy(ev);
	}
}

174
typedef struct _PcapTesterContext{
175 176 177 178 179 180 181 182
	const char *file;
	OrtpJitterBufferAlgorithm algo; 
	int congestion_count_expected;
	int audio_clock_rate;
	int audio_payload;
	int video_clock_rate;
	int video_payload;
	uint32_t ts_offset;
183 184 185
	bool_t enable_congestion_detection;
}PcapTesterContext;

186

187 188 189 190 191 192 193 194 195

rtp_stats_t final_audio_rtp_stats, final_video_rtp_stats;

static OrtpEvQueue * setup_event_queue(MediaStream *ms){
	OrtpEvQueue *evq = ortp_ev_queue_new();
	rtp_session_register_event_queue(ms->sessions.rtp_session, evq);
	return evq;
}

196
static void pcap_tester_streams_start(const PcapTesterContext *params,
197 198
						int audio_file_port, int audio_to_port,
						int video_file_port, int video_to_port) {
199 200 201 202 203
	bool_t use_audio = (audio_file_port != -1);
	bool_t use_video = (video_file_port != -1);
	MSIPPort audio_dest = { RECEIVER_IP, audio_to_port };
	MSIPPort video_dest = { RECEIVER_IP, video_to_port };

204 205
	congestion_count = 0;
	running_streams = 0;
206 207
	if (use_audio) {
		receiver = audio_stream_new (_factory, audio_to_port, 0, FALSE);
208
		test_setup(params->audio_payload, params->algo);
209 210

		rtp_session_enable_avpf_feature(receiver->ms.sessions.rtp_session, ORTP_AVPF_FEATURE_TMMBR, TRUE);
211
		rtp_session_enable_congestion_detection(receiver->ms.sessions.rtp_session, params->enable_congestion_detection);
212
		
213 214 215
		receiver_q = setup_event_queue(&receiver->ms);
	}
	if (use_video) {
216
#ifdef VIDEO_ENABLED
217 218 219
		JBParameters video_params;
		receiverv = video_stream_new(_factory, video_to_port, 0, FALSE);
		video_stream_set_direction(receiverv, MediaStreamRecvOnly);
220
		//video_stream_set_display_filter_name(receiverv, "MSGLXVideo");
221
		rtp_session_enable_avpf_feature(receiverv->ms.sessions.rtp_session, ORTP_AVPF_FEATURE_TMMBR, TRUE);
222
		rtp_session_enable_congestion_detection(receiverv->ms.sessions.rtp_session, params->enable_congestion_detection);
223 224 225 226 227 228 229

		BC_ASSERT_EQUAL(video_stream_start(receiverv
							, profile
							, 0
							, 0
							, 0
							, 0
230
							, params->video_payload
231 232 233
							, 50
							, mediastreamer2_tester_get_mire(_factory)),0, int, "%i");
		rtp_session_get_jitter_buffer_params(receiverv->ms.sessions.rtp_session, &video_params);
234
		video_params.buffer_algorithm = params->algo;
235 236 237 238 239
		video_params.refresh_ms = 5000;
		video_params.max_size = 1000;
		video_params.min_size = 20;
		rtp_session_set_jitter_buffer_params(receiverv->ms.sessions.rtp_session, &video_params);

240
		receiverv_q = setup_event_queue(&receiverv->ms);
241 242 243
#else
		ms_fatal("Video support disabled at compilation time");
#endif
244 245
	}

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
	/* Let the stream start a few seconds without receiving anything, then send the RTP simulation.
	 * This allows to have user_ts and remote_ts different in the computations*/
	ms_sleep(5);
	if (use_audio) {
		running_streams++;
		if (ms_pcap_sendto(_factory, params->file, audio_file_port, &audio_dest, params->audio_clock_rate, params->ts_offset, end_of_pcap, NULL) == NULL){
			ms_error("Cannot start audio pcap playout");
			BC_ASSERT_FALSE(TRUE);
		}
	}
	if (use_video) {
		running_streams++;
		if (ms_pcap_sendto(_factory, params->file, video_file_port, &video_dest, params->video_clock_rate, params->ts_offset, end_of_pcap, NULL) == NULL){
			ms_error("Cannot start video pcap playout");
			BC_ASSERT_FALSE(TRUE);
		}
	}
263

264
	BC_ASSERT_FALSE(running_streams == 0);
265 266 267
}

void pcap_tester_iterate_until(void) {
268 269 270
	int purging_last_packets = 100;
	while (running_streams > 0 || purging_last_packets) {
		ms_event_queue_pump(ms_factory_get_event_queue(_factory));
271 272 273 274 275 276 277 278 279 280 281
		if (receiver) {
			media_stream_iterate(&receiver->ms);
			process_queue(receiver_q);
			// iterate_stats_logger(&receiver->ms, NULL);
		}
		if (receiverv) {
			media_stream_iterate(&receiverv->ms);
			process_queue(receiverv_q);
			// iterate_stats_logger(&receiverv->ms, NULL);
		}
		ms_usleep(100000);
282
		if (running_streams == 0) purging_last_packets--;
283 284 285 286 287 288 289 290
	}
}

static void clean_evq(MediaStream *ms, OrtpEvQueue *evq){
	rtp_session_unregister_event_queue(ms->sessions.rtp_session, evq);
	ortp_ev_queue_destroy(evq);
}

291 292 293 294
void pcap_tester_stop(const PcapTesterContext *params) {
	if (params->congestion_count_expected != -1){
		BC_ASSERT_EQUAL(congestion_count, params->congestion_count_expected, int, "%d");
	}
295 296 297 298
	if (receiverv) {
		clean_evq(&receiverv->ms, receiverv_q);
		receiverv_q = NULL;
		memcpy(&final_video_rtp_stats, rtp_session_get_stats(receiverv->ms.sessions.rtp_session), sizeof(rtp_stats_t));
299
#ifdef VIDEO_ENABLED
300
		video_stream_stop(receiverv);
301
#endif
302
		receiverv = NULL;
303 304 305 306 307
	}
	if (receiver) {
		clean_evq(&receiver->ms, receiver_q);
		receiver_q = NULL;
		memcpy(&final_audio_rtp_stats, rtp_session_get_stats(receiver->ms.sessions.rtp_session), sizeof(rtp_stats_t));
308 309
		test_clean(params->file, params->audio_clock_rate);
		receiver = NULL;
310 311 312 313
	}
}


314
void pcap_tester_audio_with_params(const PcapTesterContext *params){
315 316 317
	pcap_tester_streams_start(params,
						0, RECEIVER_RTP_PORT,
						-1, -1);
318
	pcap_tester_iterate_until();
319
	pcap_tester_stop(params);
320 321 322 323
}

void pcap_tester_audio(const char* file, OrtpJitterBufferAlgorithm algo, int congestion_count_expected
						, int clock_rate, int payload) {
324
	PcapTesterContext params = {0};
325 326 327 328 329
	params.file = file;
	params.algo = algo;
	params.congestion_count_expected = congestion_count_expected;
	params.audio_clock_rate = clock_rate;
	params.audio_payload = payload;
330
	params.enable_congestion_detection = congestion_count_expected>=0;
331
	pcap_tester_audio_with_params(&params);
332
}
333

334 335 336
void pcap_tester_video(const char* file
						, OrtpJitterBufferAlgorithm algo, int congestion_count_expected
						, int clock_rate, int payload) {
337
	PcapTesterContext params = {0};
338 339 340 341 342
	params.file = file;
	params.algo = algo;
	params.congestion_count_expected = congestion_count_expected;
	params.video_clock_rate = clock_rate;
	params.video_payload = payload;
343
	params.enable_congestion_detection = congestion_count_expected>=0;
344 345
	pcap_tester_streams_start(&params,-1, -1,
						0, RECEIVER_RTP_PORT);
346
	pcap_tester_iterate_until();
347
	pcap_tester_stop(&params);
348 349 350
}

static void ideal_network_basic(void) {
351
	pcap_tester_audio("./scenarios/pcmu_8k_no_jitter.pcap", OrtpJitterBufferBasic, -1
352
				, payload_type_pcmu8000.clock_rate, 0);
353
	BC_ASSERT_LOWER((int)final_audio_rtp_stats.outoftime, 2, int, "%i");
354
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.discarded, 0, int, "%i");
355
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.packet_recv, 2524, int, "%i");
356 357 358 359 360
}

static void ideal_network_rls(void) {
	pcap_tester_audio("./scenarios/pcmu_8k_no_jitter.pcap", OrtpJitterBufferRecursiveLeastSquare, 0
				, payload_type_pcmu8000.clock_rate, 0);
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	BC_ASSERT_LOWER((int)final_audio_rtp_stats.outoftime, 2, int, "%i");
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.discarded, 0, int, "%i");
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.packet_recv, 2524, int, "%i");
}

static void ideal_network_with_ts_rollover_basic(void) {
	PcapTesterContext params = {0};
	params.file = "./scenarios/pcmu_8k_no_jitter.pcap";
	params.algo = OrtpJitterBufferBasic;
	params.audio_clock_rate = payload_type_pcmu8000.clock_rate;
	params.audio_payload = 0;
	params.ts_offset = 0xffffffff - params.audio_clock_rate*2; /*rollover will happen after 2 seconds*/
	params.enable_congestion_detection = FALSE;
	
	pcap_tester_audio_with_params(&params);
	
	BC_ASSERT_LOWER((int)final_audio_rtp_stats.outoftime, 2, int, "%i");
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.discarded, 0, int, "%i");
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.packet_recv, 2524, int, "%i");
}

static void ideal_network_with_ts_rollover_rls(void) {
	PcapTesterContext params = {0};
	params.file = "./scenarios/pcmu_8k_no_jitter.pcap";
	params.algo = OrtpJitterBufferRecursiveLeastSquare;
	params.audio_clock_rate = payload_type_pcmu8000.clock_rate;
	params.audio_payload = 0;
	params.ts_offset = 0xffffffff - params.audio_clock_rate*2; /*rollover will happen after 2 seconds*/
	params.enable_congestion_detection = FALSE;
	
	pcap_tester_audio_with_params(&params);
	
	BC_ASSERT_LOWER((int)final_audio_rtp_stats.outoftime, 2, int, "%i");
394
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.discarded, 0, int, "%i");
395
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.packet_recv, 2524, int, "%i");
396 397
}

398

399
static void burstly_network_basic(void) {
400
	pcap_tester_audio("./scenarios/rtp-534late-24loss-7000total.pcapng", OrtpJitterBufferBasic, -1
401 402
				, payload_type_opus.clock_rate, OPUS_PAYLOAD_TYPE);
	BC_ASSERT_GREATER((int)final_audio_rtp_stats.outoftime, 580, int, "%i");
403
	BC_ASSERT_LOWER((int)final_audio_rtp_stats.outoftime, 690, int, "%i");
404
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.discarded, 0, int, "%i");
405
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.packet_recv, 7108, int, "%i");
406 407 408 409
}
static void burstly_network_rls(void) {
	pcap_tester_audio("./scenarios/rtp-534late-24loss-7000total.pcapng", OrtpJitterBufferRecursiveLeastSquare, 0
				, payload_type_opus.clock_rate, OPUS_PAYLOAD_TYPE);
410
	BC_ASSERT_GREATER((int)final_audio_rtp_stats.outoftime, 190, int, "%i");
411 412
	BC_ASSERT_LOWER((int)final_audio_rtp_stats.outoftime, 240, int, "%i");
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.discarded, 0, int, "%i");
413
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.packet_recv, 7108, int, "%i");
414 415
}

416
static void burstly_network_rls_with_ts_offset(void) {
417
	PcapTesterContext params = {0};
418 419 420 421 422
	params.file = "./scenarios/rtp-534late-24loss-7000total.pcapng";
	params.algo = OrtpJitterBufferRecursiveLeastSquare;
	params.audio_clock_rate = payload_type_opus.clock_rate;
	params.audio_payload = OPUS_PAYLOAD_TYPE;
	params.ts_offset = 0x7ffffffc;
Simon Morlat's avatar
Simon Morlat committed
423
	params.enable_congestion_detection = TRUE;
424 425
	
	pcap_tester_audio_with_params(&params);
426
	BC_ASSERT_GREATER((int)final_audio_rtp_stats.outoftime, 190, int, "%i");
427 428
	BC_ASSERT_LOWER((int)final_audio_rtp_stats.outoftime, 240, int, "%i");
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.discarded, 0, int, "%i");
429
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.packet_recv, 7108, int, "%i");
430 431
}

432
static void chaotic_start_basic(void) {
433
	pcap_tester_audio("./scenarios/opus-poor-quality.pcapng", OrtpJitterBufferBasic, -1
434 435
				, payload_type_opus.clock_rate, OPUS_PAYLOAD_TYPE);
	BC_ASSERT_GREATER((int)final_audio_rtp_stats.outoftime, 200, int, "%i");
436
	BC_ASSERT_LOWER((int)final_audio_rtp_stats.outoftime, 250, int, "%i");
437
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.discarded, 0, int, "%i");
438
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.packet_recv, 4228, int, "%i");
439 440 441 442 443 444
}

/*
 * The RLS algorithm is lost by the chaotic start (clock ratio deviates from 1 to 2 rapidly ).
 * It takes around 10 seconds before goes back to reasonable values*/
static void chaotic_start_rls(void) {
445
	pcap_tester_audio("./scenarios/opus-poor-quality.pcapng", OrtpJitterBufferRecursiveLeastSquare, -1
446
				, payload_type_opus.clock_rate, OPUS_PAYLOAD_TYPE);
447 448
	BC_ASSERT_GREATER((int)final_audio_rtp_stats.outoftime, 290, int, "%i");
	BC_ASSERT_LOWER((int)final_audio_rtp_stats.outoftime, 400, int, "%i");
449
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.discarded, 0, int, "%i");
450
	BC_ASSERT_EQUAL((int)final_audio_rtp_stats.packet_recv, 4228, int, "%i");
451 452 453 454
}


static void edge_congestion(void) {
Simon Morlat's avatar
Simon Morlat committed
455
	pcap_tester_audio("./scenarios/opus-edge-congestion20_60_40.pcapng", OrtpJitterBufferRecursiveLeastSquare, 1
456 457 458 459
				, payload_type_opus.clock_rate, OPUS_PAYLOAD_TYPE);
}

static void congestion_detector_ideal_video(void) {
460 461 462 463 464 465 466 467
	PcapTesterContext params = {0};
	
	params.file = "./scenarios/congestion/video-160-0-0a5f30.pcapng";
	params.algo = OrtpJitterBufferRecursiveLeastSquare;
	params.audio_clock_rate = payload_type_pcmu8000.clock_rate;
	params.audio_payload = 0;
	params.video_clock_rate = payload_type_vp8.clock_rate;
	params.video_payload = 96;
Simon Morlat's avatar
Simon Morlat committed
468
	params.congestion_count_expected = 1; /*not so ideal video, there is a small congestion in the middle*/
469 470 471 472 473
	params.enable_congestion_detection = TRUE;
	
	pcap_tester_streams_start(&params
				, 7078, RECEIVER_RTP_PORT
				, 9078, RECEIVER_RTP_PORT + 1234);
474
	pcap_tester_iterate_until();
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	pcap_tester_stop(&params);
	
}
static void congestion_detector_limited_video_800kbits(void) {
	PcapTesterContext params = {0};
	
	params.file = "./scenarios/congestion/video-160-120-0f20c60.pcapng";
	params.algo = OrtpJitterBufferRecursiveLeastSquare;
	params.audio_clock_rate = 0;
	params.audio_payload = 0;
	params.video_clock_rate = payload_type_vp8.clock_rate;
	params.video_payload = 96;
	params.congestion_count_expected = 1;
	params.enable_congestion_detection = TRUE;
	
	pcap_tester_streams_start(&params
				, -1, 0
				, 9078, RECEIVER_RTP_PORT + 1234);
493
	pcap_tester_iterate_until();
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
	pcap_tester_stop(&params);
}

static void congestion_detector_limited_video_100kbits(void) {
	PcapTesterContext params = {0};
	
	params.file = "./scenarios/congestion/video-160-90-0a5c30.pcapng";
	params.algo = OrtpJitterBufferRecursiveLeastSquare;
	params.audio_clock_rate = 0;
	params.audio_payload = 0;
	params.video_clock_rate = payload_type_vp8.clock_rate;
	params.video_payload = 96;
	params.congestion_count_expected = 1;
	params.enable_congestion_detection = TRUE;
	
	
	pcap_tester_streams_start(&params, -1, 0
				, 9078, RECEIVER_RTP_PORT + 1234);
512
	pcap_tester_iterate_until();
513
	pcap_tester_stop(&params);
514 515 516 517 518 519 520 521
}

void congestion_adaptation(OrtpJitterBufferAlgorithm algo) {
	int pause_time = 0;
	char* hello_file = bc_tester_res(PIANO_FILE);
	unsigned init_time = 0;
	int sender_port = rand()%(0xffff - 1024) + 1024;
	OrtpNetworkSimulatorParams netsim =  {0};
522
	
523 524 525 526 527 528
	netsim.enabled = TRUE;
	netsim.max_buffer_size = 40000 * 5; // 5 secs of buffering allowed

	congestion_count = 0;
	sender = audio_stream_new (_factory,sender_port, 0,FALSE);
	receiver = audio_stream_new (_factory, RECEIVER_RTP_PORT, 0,FALSE);
529 530
	
	rtp_session_enable_congestion_detection(receiver->ms.sessions.rtp_session, TRUE);
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 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
	receiver_q = setup_event_queue(&receiver->ms);
	test_setup(0, algo);

	BC_ASSERT_EQUAL(audio_stream_start_full(sender
						, profile
						, RECEIVER_IP
						, RECEIVER_RTP_PORT
						, RECEIVER_IP
						, 0
						, 0
						, 50
						, hello_file
						, NULL
						, NULL
						, NULL
						, 0),0, int, "%i");
	ms_filter_call_method(sender->soundread,MS_FILE_PLAYER_LOOP,&pause_time);

	// ms_ticker_set_tick_func(sender->ms.sessions.ticker, drifting_ticker, sender->ms.sessions.ticker);
	// ms_ticker_set_tick_func(receiver->ms.sessions.ticker, drifting_ticker, receiver->ms.sessions.ticker);

	media_stream_iterate(&sender->ms);


	init_time = receiver->ms.sessions.ticker->get_cur_time_ptr(sender->ms.sessions.ticker);
	while (receiver->ms.sessions.ticker->get_cur_time_ptr(receiver->ms.sessions.ticker) - init_time < 12000) {
		media_stream_iterate(&sender->ms);
		media_stream_iterate(&receiver->ms);
		process_queue(receiver_q);
		// iterate_stats_logger(&receiver->ms, &sender->ms.sessions.rtp_session->cum_loss);
	}
	BC_ASSERT_EQUAL(congestion_count, 0, int, "%d");

	ms_message("Applying limitation");
	netsim.max_bandwidth = 40000;
	rtp_session_enable_network_simulation(receiver->ms.sessions.rtp_session,&netsim);
	// wait_for_until_with_parse_events(&receiver->ms,&sender->ms, &netsim.enabled,1e7,30000,iterate_stats_logger,&sender->ms.sessions.rtp_session->cum_loss,NULL,NULL);
	init_time = receiver->ms.sessions.ticker->get_cur_time_ptr(receiver->ms.sessions.ticker);
	while (receiver->ms.sessions.ticker->get_cur_time_ptr(receiver->ms.sessions.ticker) - init_time < 30000) {
		media_stream_iterate(&sender->ms);
		media_stream_iterate(&receiver->ms);
		process_queue(receiver_q);
		// iterate_stats_logger(&receiver->ms, &sender->ms.sessions.rtp_session->cum_loss);
	}
	BC_ASSERT_EQUAL(congestion_count, 1, int, "%d");

	ms_message("Removing limitation");
	netsim.max_bandwidth = 1000000;
	rtp_session_enable_network_simulation(receiver->ms.sessions.rtp_session,&netsim);
	// wait_for_until_with_parse_events(&receiver->ms,&sender->ms, &netsim.enabled,1e7,30000,iterate_stats_logger,&sender->ms.sessions.rtp_session->cum_loss,NULL,NULL);
	init_time = receiver->ms.sessions.ticker->get_cur_time_ptr(receiver->ms.sessions.ticker);
	while (receiver->ms.sessions.ticker->get_cur_time_ptr(receiver->ms.sessions.ticker) - init_time < 30000) {
		media_stream_iterate(&sender->ms);
		media_stream_iterate(&receiver->ms);
		process_queue(receiver_q);
		// iterate_stats_logger(&receiver->ms, &sender->ms.sessions.rtp_session->cum_loss);
	}

	audio_stream_stop(sender);
	// wait_for_until_with_parse_events(&receiver->ms,NULL, &netsim.enabled, 180, 2000,iterate_stats_logger,NULL,NULL,NULL);
	init_time = receiver->ms.sessions.ticker->get_cur_time_ptr(receiver->ms.sessions.ticker);
	while (receiver->ms.sessions.ticker->get_cur_time_ptr(receiver->ms.sessions.ticker) - init_time < 2000) {
		media_stream_iterate(&receiver->ms);
		process_queue(receiver_q);
		// iterate_stats_logger(&receiver->ms, &sender->ms.sessions.rtp_session->cum_loss);
	}

	BC_ASSERT_EQUAL(congestion_count, 1, int, "%d");

	clean_evq(&receiver->ms, receiver_q);
	receiver_q = NULL;
	test_clean("congestion", 8000);
	ms_free(hello_file);
}

static void congestion_adaptation_rls(void) {
	congestion_adaptation(OrtpJitterBufferRecursiveLeastSquare);
}
609

Erwan Croze's avatar
Erwan Croze committed
610 611 612 613 614

#else

static void dummy_test(void) {}

615 616 617 618 619 620
#endif

static test_t tests[] = {
#ifdef HAVE_PCAP
	{ "Ideal network basic", ideal_network_basic },
	{ "Ideal network rls", ideal_network_rls },
621 622
	{ "Ideal network with ts rollover basic", ideal_network_with_ts_rollover_basic},
	{ "Ideal network with ts rollover rls", ideal_network_with_ts_rollover_rls},
623 624
	{ "Burstly network basic", burstly_network_basic },
	{ "Burstly network rls", burstly_network_rls },
625
	{ "Burstly network rls with offset", burstly_network_rls_with_ts_offset },
626 627 628 629
	{ "Chaotic start basic", chaotic_start_basic },
	{ "Chaotic start rls", chaotic_start_rls },
	{ "Edge congestion", edge_congestion },
	{ "Congestion detector ideal video", congestion_detector_ideal_video },
630 631
	{ "Congestion detector limited video 800 kbits/s", congestion_detector_limited_video_800kbits },
	{ "Congestion detector limited video 100 kbits/s", congestion_detector_limited_video_100kbits },
632
	{ "Congestion adaptation rls", congestion_adaptation_rls },
Erwan Croze's avatar
Erwan Croze committed
633 634
#else
	{ "Dummy test", dummy_test }
635 636 637 638 639 640 641 642 643 644 645 646 647 648
#endif

};

test_suite_t jitterbuffer_test_suite = {
	"Jitter buffer",
	tester_before_all,
	tester_after_all,
	NULL,
	NULL,
	sizeof(tests) / sizeof(tests[0]),
	tests
};