mediastreamer2_audio_stream_tester.c 10.6 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 43
#define SILK16_PAYLOAD_TYPE  123
#define ISAC16_PAYLOAD_TYPE  124
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,ISAC16_PAYLOAD_TYPE,&payload_type_isac);
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
	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

static void adaptive_audio_stream(int codec_payload, int initial_bitrate,int target_bw, int max_recv_rtcp_packet) {
	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);

257
	stream_manager_start(margaux,codec_payload, marielle->local_rtp,-1,NULL,RECORDED_16K_1S_FILE);
258 259
	rtp_session_enable_network_simulation(margaux->stream->ms.sessions.rtp_session,&params);
	rtp_session_set_rtcp_report_interval(margaux->stream->ms.sessions.rtp_session, rtcp_interval);
jehan's avatar
jehan committed
260 261 262 263 264 265 266 267 268 269 270

	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);

271
	unlink(RECORDED_16K_1S_FILE);
jehan's avatar
jehan committed
272
}
273

jehan's avatar
jehan committed
274
static void adaptive_opus_audio_stream()  {
275 276 277 278
	bool_t supported = ms_filter_codec_supported("opus");
	if( supported ) {
		adaptive_audio_stream(OPUS_PAYLOAD_TYPE, 32000, EDGE_BW, 7);
	}
jehan's avatar
jehan committed
279
}
280

jehan's avatar
jehan committed
281
static void adaptive_speek16_audio_stream()  {
282 283 284 285
	bool_t supported = ms_filter_codec_supported("speex");
	if( supported ) {
		adaptive_audio_stream(SPEEX16_PAYLOAD_TYPE, 32000, EDGE_BW, 7);
	}
jehan's avatar
jehan committed
286 287
}

288 289 290
static void adaptative_isac16_audio_stream() {
	bool_t supported = ms_filter_codec_supported("iSAC");
	if( supported ) {
291
		adaptive_audio_stream(ISAC16_PAYLOAD_TYPE, 32000, 32000, 7);
292 293 294
	}
}

Simon Morlat's avatar
Simon Morlat committed
295
#if 0
296
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
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
	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);
330 331 332

}

Simon Morlat's avatar
Simon Morlat committed
333 334
#endif

335

jehan's avatar
jehan committed
336 337 338
static test_t tests[] = {
	{ "Basic audio stream", basic_audio_stream },
	{ "Adaptive audio stream [opus]", adaptive_opus_audio_stream },
339 340
	{ "Adaptive audio stream [speex]", adaptive_speek16_audio_stream },
	{ "Adaptive audio stream [iSAC]", adaptative_isac16_audio_stream }
jehan's avatar
jehan committed
341 342 343 344 345 346 347 348 349
};

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