mediastreamer2_adaptive_tester.c 20.2 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 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 164 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 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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
/*
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 "qosanalyzer.h"

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


#ifdef _MSC_VER
#define unlink _unlink
#endif

static RtpProfile rtp_profile;

#define OPUS_PAYLOAD_TYPE    121
#define SPEEX16_PAYLOAD_TYPE 122
#define SILK16_PAYLOAD_TYPE  123
#define PCMA8_PAYLOAD_TYPE 8
#define H263_PAYLOAD_TYPE 34
#define H264_PAYLOAD_TYPE 102
#define VP8_PAYLOAD_TYPE 103

#define EDGE_BW 10000
#define THIRDGENERATION_BW 200000

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);
	rtp_profile_set_payload (&rtp_profile,PCMA8_PAYLOAD_TYPE,&payload_type_pcma8000);

	rtp_profile_set_payload (&rtp_profile,H263_PAYLOAD_TYPE,&payload_type_h263);
	rtp_profile_set_payload(&rtp_profile,H264_PAYLOAD_TYPE,&payload_type_h264);
	rtp_profile_set_payload(&rtp_profile, VP8_PAYLOAD_TYPE, &payload_type_vp8);
	return 0;
}

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

#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"

typedef struct _audio_stats_t {
	rtp_stats_t rtp;
	int number_of_EndOfFile;
} audio_stats_t;

typedef struct _video_stats_t {
	float loss;
	float rtt;
	MSQosAnalyserNetworkState network_state;
	float loss_estim;
	float congestion_bw_estim;
} video_stats_t;

typedef struct _stream_manager_t {
	StreamType type;
	int local_rtp;
	int local_rtcp;

	union{
		AudioStream* audio_stream;
		VideoStream* video_stream;
	};
	union{
		audio_stats_t audio_stats;
		video_stats_t video_stats;
	};
} stream_manager_t ;

static stream_manager_t * stream_manager_new(StreamType type) {
	stream_manager_t * mgr = ms_new0(stream_manager_t,1);
	mgr->type=type;
	mgr->local_rtp=(rand() % ((2^16)-1024) + 1024) & ~0x1;
	mgr->local_rtcp=mgr->local_rtp+1;
	if (mgr->type==AudioStreamType){
		mgr->audio_stream=audio_stream_new (mgr->local_rtp, mgr->local_rtcp,FALSE);
		memset(&mgr->audio_stats,0,sizeof(audio_stats_t));
	}else{
		mgr->video_stream=video_stream_new (mgr->local_rtp, mgr->local_rtcp,FALSE);
	}
	return mgr;
}
static void stream_manager_delete(stream_manager_t * mgr) {
	if (mgr->type==AudioStreamType){
		audio_stream_stop(mgr->audio_stream);
	}else{
		video_stream_stop(mgr->video_stream);
	}
	ms_free(mgr);
}

static void audio_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->audio_stream->ms,target_bitrate);

	CU_ASSERT_EQUAL(audio_stream_start_full(mgr->audio_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 video_manager_start(	stream_manager_t * mgr
									,int payload_type
									,int remote_port
									,int target_bitrate
									,MSWebCam * cam) {

	media_stream_set_target_network_bitrate(&mgr->video_stream->ms,target_bitrate);

	int result=video_stream_start(mgr->video_stream
												, &rtp_profile
												, "127.0.0.1"
												, remote_port
												, "127.0.0.1"
												, remote_port+1
												, payload_type
												, 60
												, cam);
	CU_ASSERT_EQUAL(result,0);
}

static void notify_cb(void *user_data, MSFilter *f, unsigned int event, void *eventdata) {
	audio_stats_t* stats = (audio_stats_t*)user_data;
	switch (event) {
	case MS_FILE_PLAYER_EOF: {
		ms_message("EndOfFile received");
		stats->number_of_EndOfFile++;
		break;
	}
	break;
	}
}

static void basic_audio_stream() {
	stream_manager_t * marielle = stream_manager_new(AudioStreamType);
	stream_manager_t * margaux = stream_manager_new(AudioStreamType);
	RtpProfile* profile = rtp_profile_new("default profile");

	rtp_profile_set_payload (profile,0,&payload_type_pcmu8000);

	/*recorder should be initialized before sender to avoid missing the first
	emitted packets*/
	audio_manager_start(margaux, 0, marielle->local_rtp,0,0,RECORDED_8K_1S_FILE);
	audio_manager_start(marielle, 0, margaux->local_rtp,0,HELLO_8K_1S_FILE,0);

	ms_filter_add_notify_callback(marielle->audio_stream->soundread, notify_cb,
		&marielle->audio_stats,TRUE);

	CU_ASSERT_TRUE(wait_for_until(&marielle->audio_stream->ms,&margaux->audio_stream->ms,
		&marielle->audio_stats.number_of_EndOfFile,1,12000));
	audio_stream_get_local_rtp_stats(marielle->audio_stream,&marielle->audio_stats.rtp);
	audio_stream_get_local_rtp_stats(margaux->audio_stream,&margaux->audio_stats.rtp);

	/* No packet loss is assumed */
	CU_ASSERT_EQUAL(marielle->audio_stats.rtp.sent,margaux->audio_stats.rtp.recv);

	stream_manager_delete(marielle);
	stream_manager_delete(margaux);

	unlink(RECORDED_8K_1S_FILE);
}

static void handle_queue_events(stream_manager_t * stream_mgr, OrtpEvQueue * evq) {
	OrtpEvent *ev;
	while (NULL != (ev=ortp_ev_queue_get(evq))){
		OrtpEventType evt=ortp_event_get_type(ev);
		OrtpEventData *evd=ortp_event_get_data(ev);

		if (evt == ORTP_EVENT_RTCP_PACKET_RECEIVED) {
			const report_block_t *rb=NULL;
			if (rtcp_is_SR(evd->packet)){
				rb=rtcp_SR_get_report_block(evd->packet,0);
			}else if (rtcp_is_RR(evd->packet)){
				rb=rtcp_RR_get_report_block(evd->packet,0);
			}

			if (rb && stream_mgr->type==VideoStreamType) {
				if (stream_mgr->video_stream->ms.use_rc){
					const MSQosAnalyser *analyser=ms_bitrate_controller_get_qos_analyser(stream_mgr->video_stream->ms.rc);
					if (analyser->type==Stateful){
						const MSStatefulQosAnalyser *stateful_analyser=((const MSStatefulQosAnalyser*)analyser);
						stream_mgr->video_stats.network_state=stateful_analyser->network_state;
						stream_mgr->video_stats.loss_estim =100*stateful_analyser->network_loss_rate;
						stream_mgr->video_stats.congestion_bw_estim =stateful_analyser->congestion_bandwidth;
					}
				}

				stream_mgr->video_stats.loss=100.0*(float)report_block_get_fraction_lost(rb)/256.0;
				stream_mgr->video_stats.rtt=rtp_session_get_round_trip_propagation(stream_mgr->video_stream->ms.sessions.rtp_session);

				ms_message("mediastreamer2_video_stream_tester: received RTCP packet: loss=%f, RTT=%f, network_state=%d"
					,stream_mgr->video_stats.loss
					,stream_mgr->video_stats.rtt
					,stream_mgr->video_stats.network_state);
			}
		}
		ortp_event_destroy(ev);
	}
}

252 253 254
static OrtpEvQueue * start_adaptive_stream(StreamType type, stream_manager_t ** pmarielle, stream_manager_t ** pmargaux,
	int payload, int initial_bitrate, int target_bw, float loss_rate, int latency, float dup_ratio) {
	OrtpEvQueue * evq;
255 256 257 258 259 260 261
	OrtpNetworkSimulatorParams params={0};
	params.enabled=TRUE;
	params.loss_rate=loss_rate;
	params.max_bandwidth=target_bw;
	params.latency=latency;
	int pause_time=0;
	MediaStream *marielle_ms,*margaux_ms;
262 263 264 265 266

	stream_manager_t * marielle = *pmarielle=stream_manager_new(type);
	stream_manager_t * margaux = *pmargaux=stream_manager_new(type);

	if (type == AudioStreamType){
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
		marielle_ms=&marielle->audio_stream->ms;
		margaux_ms=&margaux->audio_stream->ms;
	}else{
		marielle_ms=&marielle->video_stream->ms;
		margaux_ms=&margaux->video_stream->ms;
	}

	MSWebCam * marielle_webcam=ms_web_cam_manager_get_default_cam (ms_web_cam_manager_get());

	media_stream_enable_adaptive_bitrate_control(marielle_ms,TRUE);
	rtp_session_set_duplication_ratio(marielle_ms->sessions.rtp_session, dup_ratio);

	if (marielle->type == AudioStreamType){
		audio_manager_start(marielle,payload, margaux->local_rtp,initial_bitrate,HELLO_16K_1S_FILE,NULL);
		ms_filter_call_method(marielle->audio_stream->soundread,MS_FILE_PLAYER_LOOP,&pause_time);

		audio_manager_start(margaux,payload, marielle->local_rtp,-1,NULL,RECORDED_16K_1S_FILE);
	}else{
		video_manager_start(marielle, payload, margaux->local_rtp, initial_bitrate, marielle_webcam);
		video_stream_set_direction(margaux->video_stream,VideoStreamRecvOnly);

		video_manager_start(margaux, payload, marielle->local_rtp, -1, NULL);
	}
	rtp_session_enable_network_simulation(margaux_ms->sessions.rtp_session,&params);

292
	evq=ortp_ev_queue_new();
293
	rtp_session_register_event_queue(marielle_ms->sessions.rtp_session,evq);
294 295
	return evq;
}
296

297 298 299 300 301
static int timeout_receive_rtcp(int max_recv_rtcp_packet){
	int packets_after_start=max_recv_rtcp_packet - (15000.0/2500);
	return ((packets_after_start > 0) ? 15000 + (packets_after_start + .5) * 5000 : (max_recv_rtcp_packet + .5) * 2500);
}

302
static void iterate_adaptive_stream(stream_manager_t * marielle, stream_manager_t * margaux,
303
	OrtpEvQueue * evq, int timeout_ms, int* current, int expected){
304
	int retry=0;
305 306 307 308 309 310 311 312 313 314

	MediaStream *marielle_ms,*margaux_ms;
	if (marielle->type == AudioStreamType){
		marielle_ms=&marielle->audio_stream->ms;
		margaux_ms=&margaux->audio_stream->ms;
	}else{
		marielle_ms=&marielle->video_stream->ms;
		margaux_ms=&margaux->video_stream->ms;
	}

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
	while ((!current||*current<expected) && retry++ <timeout_ms/100) {
		media_stream_iterate(marielle_ms);
		media_stream_iterate(margaux_ms);
		handle_queue_events(marielle, evq);
		if (retry%10==0) {
			 ms_message("stream [%p] bandwidth usage: [d=%.1f,u=%.1f] kbit/sec"	,
			 	marielle_ms, media_stream_get_down_bw(marielle_ms)/1000, media_stream_get_up_bw(marielle_ms)/1000);
			 ms_message("stream [%p] bandwidth usage: [d=%.1f,u=%.1f] kbit/sec"	,
			 	margaux_ms, media_stream_get_down_bw(margaux_ms)/1000, media_stream_get_up_bw(margaux_ms)/1000);
		 }
		ms_usleep(100000);
	}
}

#define DEINIT() \
330 331 332 333 334 335 336
	if (marielle->type == AudioStreamType){ \
		rtp_session_unregister_event_queue(marielle->audio_stream->ms.sessions.rtp_session,evq); \
		unlink(RECORDED_16K_1S_FILE); \
	}else{\
		rtp_session_unregister_event_queue(marielle->video_stream->ms.sessions.rtp_session,evq); \
	}\
	ortp_ev_queue_destroy(evq); \
337 338 339 340 341 342 343 344 345 346
	stream_manager_delete(marielle); \
	stream_manager_delete(margaux);

static void adaptive_opus_audio_stream()  {
	bool_t supported = ms_filter_codec_supported("opus");
	if( supported ) {
		// at 8KHz -> 24kb/s
		// at 48KHz -> 48kb/s
		float bw_usage;
		stream_manager_t * marielle, * margaux;
347
		OrtpEvQueue * evq;
348 349

		// on EDGEBW, both should be overconsumming
350 351
		evq=start_adaptive_stream(AudioStreamType, &marielle, &margaux, OPUS_PAYLOAD_TYPE, 8000, EDGE_BW, 0, 0, 0);
		iterate_adaptive_stream(marielle, margaux, evq, timeout_receive_rtcp(14), &marielle->audio_stats.number_of_EndOfFile, 10);
352
		bw_usage=media_stream_get_up_bw(&marielle->audio_stream->ms)*1./EDGE_BW;
353 354 355
		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
		DEINIT();

356 357
		evq=start_adaptive_stream(AudioStreamType, &marielle, &margaux, OPUS_PAYLOAD_TYPE, 48000, EDGE_BW, 0, 0, 0);
		iterate_adaptive_stream(marielle, margaux, evq, timeout_receive_rtcp(11), &marielle->audio_stats.number_of_EndOfFile, 10);
358
		bw_usage=media_stream_get_up_bw(&marielle->audio_stream->ms)*1./EDGE_BW;
359 360 361 362
		CU_ASSERT_IN_RANGE(bw_usage, 1.f, 1.4f); // bad!
		DEINIT();

		// on 3G BW, both should be at max
363 364
		evq=start_adaptive_stream(AudioStreamType, &marielle, &margaux, OPUS_PAYLOAD_TYPE, 8000, THIRDGENERATION_BW, 0, 0, 0);
		iterate_adaptive_stream(marielle, margaux, evq, timeout_receive_rtcp(5), &marielle->audio_stats.number_of_EndOfFile, 10);
365
		bw_usage=media_stream_get_up_bw(&marielle->audio_stream->ms)*1./THIRDGENERATION_BW;
366 367 368
		CU_ASSERT_IN_RANGE(bw_usage, .1f, .15f);
		DEINIT();

369 370
		evq=start_adaptive_stream(AudioStreamType, &marielle, &margaux, OPUS_PAYLOAD_TYPE, 48000, THIRDGENERATION_BW, 0, 0, 0);
		iterate_adaptive_stream(marielle, margaux, evq, timeout_receive_rtcp(5), &marielle->audio_stats.number_of_EndOfFile, 10);
371
		bw_usage=media_stream_get_up_bw(&marielle->audio_stream->ms)*1./THIRDGENERATION_BW;
372 373 374 375 376 377 378 379 380 381 382 383
		CU_ASSERT_IN_RANGE(bw_usage, .2f, .3f);
		DEINIT();
	}
}

static void adaptive_speex16_audio_stream()  {
	bool_t supported = ms_filter_codec_supported("speex");
	if( supported ) {
		// at 16KHz -> 20 kb/s
		// at 32KHz -> 30 kb/s
		float bw_usage;
		stream_manager_t * marielle, * margaux;
384
		OrtpEvQueue * evq;
385

386 387
		evq=start_adaptive_stream(AudioStreamType, &marielle, &margaux, SPEEX16_PAYLOAD_TYPE, 32000, EDGE_BW / 2., 0, 0, 0);
		iterate_adaptive_stream(marielle, margaux, evq, timeout_receive_rtcp(10), &marielle->audio_stats.number_of_EndOfFile, 10);
388
		bw_usage=media_stream_get_up_bw(&marielle->audio_stream->ms)*1./(EDGE_BW / 2.);
389 390 391 392 393 394 395 396 397 398 399
		CU_ASSERT_IN_RANGE(bw_usage, 1.f, 5.f);
		DEINIT();
	}
}

static void adaptive_pcma_audio_stream() {
	bool_t supported = ms_filter_codec_supported("pcma");
	if( supported ) {
		// at 8KHz -> 80 kb/s
		float bw_usage;
		stream_manager_t * marielle, * margaux;
400
		OrtpEvQueue * evq;
401 402

		// yet non-adaptative codecs cannot respect low throughput limitations
403 404
		evq=start_adaptive_stream(AudioStreamType, &marielle, &margaux, PCMA8_PAYLOAD_TYPE, 8000, EDGE_BW, 0, 0, 0);
		iterate_adaptive_stream(marielle, margaux, evq, timeout_receive_rtcp(10), &marielle->audio_stats.number_of_EndOfFile, 10);
405
		bw_usage=media_stream_get_up_bw(&marielle->audio_stream->ms)*1./EDGE_BW;
406 407 408
		CU_ASSERT_IN_RANGE(bw_usage,6.f, 8.f); // this is bad!
		DEINIT();

409 410
		evq=start_adaptive_stream(AudioStreamType, &marielle, &margaux, PCMA8_PAYLOAD_TYPE, 8000, THIRDGENERATION_BW, 0, 0, 0);
		iterate_adaptive_stream(marielle, margaux, evq, timeout_receive_rtcp(5), &marielle->audio_stats.number_of_EndOfFile, 10);
411
		bw_usage=media_stream_get_up_bw(&marielle->audio_stream->ms)*1./THIRDGENERATION_BW;
412 413 414 415 416 417 418 419 420
		CU_ASSERT_IN_RANGE(bw_usage, .3f, .5f);
		DEINIT();
	}
}

static void upload_bandwidth_computation() {
	bool_t supported = ms_filter_codec_supported("pcma");
	if( supported ) {
		stream_manager_t * marielle, * margaux;
421
		OrtpEvQueue * evq;
422 423
		int i;
		const MSQosAnalyser *analyser;
424

425
		evq=start_adaptive_stream(AudioStreamType, &marielle, &margaux, PCMA8_PAYLOAD_TYPE, 8000, 0, 0, 0, 0);
426 427 428 429 430 431
		analyser=ms_bitrate_controller_get_qos_analyser(marielle->audio_stream->ms.rc);
		if (analyser->type==Stateful){
			const MSStatefulQosAnalyser *stateful_analyser=((const MSStatefulQosAnalyser*)analyser);

			for (i = 0; i < 5; i++){
				rtp_session_set_duplication_ratio(marielle->audio_stream->ms.sessions.rtp_session, i);
432
				iterate_adaptive_stream(marielle, margaux, evq, timeout_receive_rtcp(2), NULL, 0);
433
				/*since PCMA uses 80kbit/s, upload bandwidth should just be 80x(duplication_ratio+1) kbits/s */
434
				CU_ASSERT_TRUE(fabs(stateful_analyser->upload_bandwidth_latest - 80.*(i+1)) < 1.f);
435 436
			}
		}
437 438 439 440 441 442
		DEINIT();
	}
}

static void stability_network_detection() {
	stream_manager_t * marielle, * margaux;
443
	OrtpEvQueue * evq;
444 445
	evq=start_adaptive_stream(VideoStreamType, &marielle, &margaux, VP8_PAYLOAD_TYPE, 300000, 0, 0, 500, 0);
	iterate_adaptive_stream(marielle, margaux, evq, timeout_receive_rtcp(10), NULL, 0);
446 447 448
	CU_ASSERT_EQUAL(marielle->video_stats.network_state, MSQosAnalyserNetworkFine);
	DEINIT();

449 450
	evq=start_adaptive_stream(VideoStreamType, &marielle, &margaux, VP8_PAYLOAD_TYPE, 300000, 70000, 0, 250,0);
	iterate_adaptive_stream(marielle, margaux, evq, timeout_receive_rtcp(10), NULL, 0);
451 452 453
	CU_ASSERT_EQUAL(marielle->video_stats.network_state, MSQosAnalyserNetworkCongested);
	DEINIT();

454 455
	evq=start_adaptive_stream(VideoStreamType, &marielle, &margaux, VP8_PAYLOAD_TYPE, 300000, 0, 15, 250,0);
	iterate_adaptive_stream(marielle, margaux, evq, timeout_receive_rtcp(10), NULL, 0);
456 457 458 459 460 461
	CU_ASSERT_EQUAL(marielle->video_stats.network_state, MSQosAnalyserNetworkLossy);
	DEINIT();
}

static void adaptive_vp8() {
	stream_manager_t * marielle, * margaux;
462
	OrtpEvQueue * evq;
463

464
	evq=start_adaptive_stream(VideoStreamType, &marielle, &margaux, VP8_PAYLOAD_TYPE, 300000, 0, 25, 50, 0);
465
	iterate_adaptive_stream(marielle, margaux, evq, timeout_receive_rtcp(18), NULL, 0);
466
	CU_ASSERT_IN_RANGE(marielle->video_stats.loss_estim, 20, 30);
467
	CU_ASSERT_TRUE(marielle->video_stats.congestion_bw_estim > 200);
468 469 470 471
	DEINIT();

	/*very low bandwidth cause a lot of packets to be dropped since congestion is
	always present even if we are below the limit due to encoding variance*/
472
	evq=start_adaptive_stream(VideoStreamType, &marielle, &margaux, VP8_PAYLOAD_TYPE, 300000, 40000, 0, 50,0);
473
	iterate_adaptive_stream(marielle, margaux, evq, timeout_receive_rtcp(18), NULL, 0);
474 475 476 477
	CU_ASSERT_IN_RANGE(marielle->video_stats.loss_estim, 0, 15);
	CU_ASSERT_IN_RANGE(marielle->video_stats.congestion_bw_estim, 20, 65);
	DEINIT();

478
	evq=start_adaptive_stream(VideoStreamType, &marielle, &margaux, VP8_PAYLOAD_TYPE, 300000, 70000,0, 50,0);
479
	iterate_adaptive_stream(marielle, margaux, evq, timeout_receive_rtcp(18), NULL, 0);
480 481 482 483
	CU_ASSERT_IN_RANGE(marielle->video_stats.loss_estim, 0, 10);
	CU_ASSERT_IN_RANGE(marielle->video_stats.congestion_bw_estim, 50, 95);
	DEINIT();

484
	evq=start_adaptive_stream(VideoStreamType, &marielle, &margaux, VP8_PAYLOAD_TYPE, 300000, 100000,15, 50,0);
485
	iterate_adaptive_stream(marielle, margaux, evq, timeout_receive_rtcp(18), NULL, 0);
486 487 488 489 490 491 492 493 494
	CU_ASSERT_IN_RANGE(marielle->video_stats.loss_estim, 10, 20);
	CU_ASSERT_IN_RANGE(marielle->video_stats.congestion_bw_estim, 80, 125);
	DEINIT();
}

static void packet_duplication() {
	const rtp_stats_t *stats;
	double dup_ratio;
	stream_manager_t * marielle, * margaux;
495
	OrtpEvQueue * evq;
496 497

	dup_ratio = 0;
498 499
	evq=start_adaptive_stream(VideoStreamType, &marielle, &margaux, VP8_PAYLOAD_TYPE, 300000, 0, 0, 50,dup_ratio);
	iterate_adaptive_stream(marielle, margaux, evq, timeout_receive_rtcp(2), NULL, 0);
500 501 502 503 504 505 506 507 508
	stats=rtp_session_get_stats(margaux->video_stream->ms.sessions.rtp_session);
	CU_ASSERT_EQUAL(stats->duplicated, dup_ratio ? stats->packet_recv / (dup_ratio+1) : 0);
	/*in theory, cumulative loss should be the invert of duplicated count, but
	since cumulative loss is computed only on received RTCP report and duplicated
	count is updated on each RTP packet received, we cannot accurately verify the values*/
	CU_ASSERT_TRUE(stats->cum_packet_loss <= -.5*stats->duplicated);
	DEINIT();

	dup_ratio = 1;
509 510
	evq=start_adaptive_stream(VideoStreamType, &marielle, &margaux, VP8_PAYLOAD_TYPE, 300000, 0, 0, 50,dup_ratio);
	iterate_adaptive_stream(marielle, margaux, evq, timeout_receive_rtcp(2), NULL, 0);
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
	stats=rtp_session_get_stats(margaux->video_stream->ms.sessions.rtp_session);
	CU_ASSERT_EQUAL(stats->duplicated, dup_ratio ? stats->packet_recv / (dup_ratio+1) : 0);
	CU_ASSERT_TRUE(stats->cum_packet_loss <= -.5*stats->duplicated);
	DEINIT();
}

static test_t tests[] = {
	{ "Basic audio stream", basic_audio_stream },
	{ "Packet duplication", packet_duplication},
	{ "Adaptive audio stream [opus]", adaptive_opus_audio_stream },
	{ "Adaptive audio stream [speex]", adaptive_speex16_audio_stream },
	{ "Adaptive audio stream [pcma]", adaptive_pcma_audio_stream },
	{ "Upload bandwidth computation", upload_bandwidth_computation },
	{ "Stability detection", stability_network_detection },
	{ "Adaptive video stream [VP8]", adaptive_vp8 },
};

test_suite_t adaptive_test_suite = {
	"Adaptive algorithm",
	tester_init,
	tester_cleanup,
	sizeof(tests) / sizeof(tests[0]),
	tests
};