bench.c 11.4 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)

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
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
aymeric's avatar
aymeric committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
*/


#include "mediastreamer2/msticker.h"

#include "mediastreamer2/msrtp.h"
#include "mediastreamer2/msfileplayer.h"
#include "mediastreamer2/msfilerec.h"

#include <signal.h>

#define MAX_RTP_SIZE	1500

static int run=1;

static void stop(int signum){
	run=0;
}

struct test_session {
	RtpSession *rtps;
40

aymeric's avatar
aymeric committed
41 42 43
	MSFilter *fplayer;
	MSFilter *encoder;
	MSFilter *rtpsend;
44

aymeric's avatar
aymeric committed
45 46 47 48 49 50 51 52
	MSFilter *rtprecv;
	MSFilter *decoder;
	MSFilter *frecorder;
};

struct bench_config {
	int num_session;
	int num_session_record;
53

aymeric's avatar
aymeric committed
54 55 56
	int port_origin;
	char *ip_destination;
	int port_destination;
57

aymeric's avatar
aymeric committed
58 59 60 61
	int payload;
	int rate;
	int ptime;
	char *wavfile;
62 63
	
	MSFactory *factory;
aymeric's avatar
aymeric committed
64
	MSTicker *ticker;
65
	bctbx_list_t *tsessions; /* list of struct test_session */
aymeric's avatar
aymeric committed
66 67
};

aymeric's avatar
aymeric committed
68
#define NUM_SESSION 50 /* num of session to start per block */
aymeric's avatar
aymeric committed
69 70 71 72
#define NUM_SESSION_RECORD 1

struct bench_config cfg[] = {
	{	NUM_SESSION,NUM_SESSION_RECORD,
73 74 75 76
		8000,"127.0.0.1",9000,
		8,8000,20,"test1.wav",
		NULL,NULL,NULL	},

aymeric's avatar
aymeric committed
77
	{	NUM_SESSION,NUM_SESSION_RECORD,
78 79 80
		9000,"127.0.0.1",8000,
		8,8000,20,"test1.wav",
		NULL,NULL,NULL	},
81

aymeric's avatar
aymeric committed
82
	{	NUM_SESSION,NUM_SESSION_RECORD,
83 84 85 86
		10000,"127.0.0.1",11000,
		8,8000,20,"test1.wav",
		NULL,NULL,NULL	},

aymeric's avatar
aymeric committed
87
	{	NUM_SESSION,NUM_SESSION_RECORD,
88 89 90
		11000,"127.0.0.1",10000,
		8,8000,20,"test1.wav",
		NULL,NULL,NULL	},
91

92
	{	0,0,0,"",0,0,0,0,NULL,NULL,NULL,NULL	},
aymeric's avatar
aymeric committed
93 94
};

95
static RtpSession *create_duplex_rtpsession(int locport){
aymeric's avatar
aymeric committed
96 97 98 99 100 101 102
	RtpSession *rtpr;
	rtpr=rtp_session_new(RTP_SESSION_SENDRECV);
	rtp_session_set_recv_buf_size(rtpr,MAX_RTP_SIZE);
	rtp_session_set_scheduling_mode(rtpr,0);
	rtp_session_set_blocking_mode(rtpr,0);
	rtp_session_enable_adaptive_jitter_compensation(rtpr,FALSE);
	rtp_session_set_symmetric_rtp(rtpr,TRUE);
103
	rtp_session_set_local_addr(rtpr,"0.0.0.0",locport,locport+1);
104 105
	rtp_session_signal_connect(rtpr,"timestamp_jump",(RtpCallback)rtp_session_resync,NULL);
	rtp_session_signal_connect(rtpr,"ssrc_changed",(RtpCallback)rtp_session_resync,NULL);
aymeric's avatar
aymeric committed
106 107 108
	return rtpr;
}

109
static int init_bench(struct bench_config *bench)
aymeric's avatar
aymeric committed
110 111 112 113 114
{
	PayloadType *pt;
	int pos;
	int val;
	int count;
115
	
116
	bench->factory = ms_factory_new_with_voip();
aymeric's avatar
aymeric committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	bench->ticker=ms_ticker_new();

	count = 0;
	/* creates the couple of encoder/decoder */
	pt=rtp_profile_get_payload(&av_profile,bench->payload);
	if (pt==NULL){
		ms_error("audiostream.c: undefined payload type.");
		return count;
	}
	if (pt->clock_rate!=8000 && pt->clock_rate!=16000 && pt->clock_rate!=32000){
		ms_error("audiostream.c: wrong rate.");
		return count;
	}
	for (pos=0;pos<bench->num_session;pos++)
		{
			struct test_session *ts = (struct test_session *)ortp_malloc(sizeof(struct test_session));
			memset(ts, 0, sizeof(struct test_session));
134

aymeric's avatar
aymeric committed
135 136 137 138 139 140 141
			ts->rtps = create_duplex_rtpsession(bench->port_origin+pos*2);
			if (ts->rtps==NULL)
				{
					ms_error("bench.c: cannot create rtp_session!");
					ortp_free(ts);
					return count;
				}
142

aymeric's avatar
aymeric committed
143 144 145 146
			rtp_session_set_payload_type(ts->rtps,bench->payload);
			rtp_session_set_remote_addr_full(ts->rtps,
											 bench->ip_destination,
											 bench->port_destination+pos*2,
147
											 bench->ip_destination,
aymeric's avatar
aymeric committed
148
											 bench->port_destination+1+pos*2);
149

150
			ts->fplayer = ms_factory_create_filter(bench->factory, MS_FILE_PLAYER_ID);
aymeric's avatar
aymeric committed
151
			if (strstr(bench->wavfile, ".au")==NULL)
152 153
				ts->encoder = ms_factory_create_encoder(bench->factory, pt->mime_type);
			ts->rtpsend = ms_factory_create_filter(bench->factory, MS_RTP_SEND_ID);
154

155 156 157
			ts->rtprecv = ms_factory_create_filter(bench->factory, MS_RTP_RECV_ID);
			ts->decoder = ms_factory_create_decoder(bench->factory, pt->mime_type);
			ts->frecorder = ms_factory_create_filter(bench->factory, MS_FILE_REC_ID);
158

aymeric's avatar
aymeric committed
159 160 161 162 163
			if ((ts->encoder==NULL && strstr(bench->wavfile, ".au")==NULL)
				|| (ts->decoder==NULL )){
				ms_error("bench.c: No decoder available for payload %i.",bench->payload);
				if (ts->fplayer) ms_filter_destroy(ts->fplayer);
				if (ts->encoder) ms_filter_destroy(ts->encoder);
164
				if (ts->rtpsend) ms_filter_destroy(ts->rtpsend);
aymeric's avatar
aymeric committed
165 166 167 168 169 170 171 172 173 174
				if (ts->rtprecv) ms_filter_destroy(ts->rtprecv);
				if (ts->decoder) ms_filter_destroy(ts->decoder);
				if (ts->frecorder) ms_filter_destroy(ts->frecorder);
				ortp_free(ts);
				return count;
			}
			if (ts->fplayer==NULL){
				ms_error("bench.c: missing player filter.");
				if (ts->fplayer) ms_filter_destroy(ts->fplayer);
				if (ts->encoder) ms_filter_destroy(ts->encoder);
175
				if (ts->rtpsend) ms_filter_destroy(ts->rtpsend);
aymeric's avatar
aymeric committed
176 177 178 179 180 181 182 183 184 185
				if (ts->rtprecv) ms_filter_destroy(ts->rtprecv);
				if (ts->decoder) ms_filter_destroy(ts->decoder);
				if (ts->frecorder) ms_filter_destroy(ts->frecorder);
				ortp_free(ts);
				return count;
			}
			if (ts->frecorder==NULL){
				ms_error("bench.c: missing recorder filter.");
				if (ts->fplayer) ms_filter_destroy(ts->fplayer);
				if (ts->encoder) ms_filter_destroy(ts->encoder);
186
				if (ts->rtpsend) ms_filter_destroy(ts->rtpsend);
aymeric's avatar
aymeric committed
187 188 189 190 191 192 193 194 195 196
				if (ts->rtprecv) ms_filter_destroy(ts->rtprecv);
				if (ts->decoder) ms_filter_destroy(ts->decoder);
				if (ts->frecorder) ms_filter_destroy(ts->frecorder);
				ortp_free(ts);
				return count;
			}
			if (ts->rtpsend==NULL){
				ms_error("bench.c: missing rtpsend filter.");
				if (ts->fplayer) ms_filter_destroy(ts->fplayer);
				if (ts->encoder) ms_filter_destroy(ts->encoder);
197
				if (ts->rtpsend) ms_filter_destroy(ts->rtpsend);
aymeric's avatar
aymeric committed
198 199 200 201 202 203 204 205 206 207
				if (ts->rtprecv) ms_filter_destroy(ts->rtprecv);
				if (ts->decoder) ms_filter_destroy(ts->decoder);
				if (ts->frecorder) ms_filter_destroy(ts->frecorder);
				ortp_free(ts);
				return count;
			}
			if (ts->rtprecv==NULL){
				ms_error("bench.c: missing rtprecv filter.");
				if (ts->fplayer) ms_filter_destroy(ts->fplayer);
				if (ts->encoder) ms_filter_destroy(ts->encoder);
208
				if (ts->rtpsend) ms_filter_destroy(ts->rtpsend);
aymeric's avatar
aymeric committed
209 210 211 212 213 214
				if (ts->rtprecv) ms_filter_destroy(ts->rtprecv);
				if (ts->decoder) ms_filter_destroy(ts->decoder);
				if (ts->frecorder) ms_filter_destroy(ts->frecorder);
				ortp_free(ts);
				return count;
			}
215

aymeric's avatar
aymeric committed
216 217
			ms_filter_call_method(ts->rtpsend,MS_RTP_SEND_SET_SESSION,ts->rtps);
			ms_filter_call_method(ts->rtprecv,MS_RTP_RECV_SET_SESSION,ts->rtps);
218

aymeric's avatar
aymeric committed
219 220
			ms_filter_call_method (ts->rtprecv, MS_FILTER_SET_SAMPLE_RATE,
								   &pt->clock_rate);
221

aymeric's avatar
aymeric committed
222 223
			ms_filter_call_method (ts->frecorder, MS_FILTER_SET_SAMPLE_RATE,
								   &pt->clock_rate);
224

aymeric's avatar
aymeric committed
225 226 227 228 229 230
			val = ms_filter_call_method(ts->fplayer,MS_FILE_PLAYER_OPEN,(void*)bench->wavfile);
			if (val!=0)
				{
					ms_error("bench.c: Cannot open wav file (%s)", bench->wavfile);
					if (ts->fplayer) ms_filter_destroy(ts->fplayer);
					if (ts->encoder) ms_filter_destroy(ts->encoder);
231
					if (ts->rtpsend) ms_filter_destroy(ts->rtpsend);
aymeric's avatar
aymeric committed
232 233 234 235 236 237
					if (ts->rtprecv) ms_filter_destroy(ts->rtprecv);
					if (ts->decoder) ms_filter_destroy(ts->decoder);
					if (ts->frecorder) ms_filter_destroy(ts->frecorder);
					ortp_free(ts);
					return count;
				}
238

aymeric's avatar
aymeric committed
239 240 241 242 243 244 245 246 247
			val=0;
			ms_filter_call_method (ts->fplayer, MS_FILTER_GET_SAMPLE_RATE,
								   &val);
			if (val!=pt->clock_rate)
				{
					ms_error("bench.c: unsupported rate for wav file: codec=%i / file=%i",
							 pt->clock_rate, val);
					if (ts->fplayer) ms_filter_destroy(ts->fplayer);
					if (ts->encoder) ms_filter_destroy(ts->encoder);
248
					if (ts->rtpsend) ms_filter_destroy(ts->rtpsend);
aymeric's avatar
aymeric committed
249 250 251 252 253 254 255 256
					if (ts->rtprecv) ms_filter_destroy(ts->rtprecv);
					if (ts->decoder) ms_filter_destroy(ts->decoder);
					if (ts->frecorder) ms_filter_destroy(ts->frecorder);
					ortp_free(ts);
					return count;
				}
			ms_filter_call_method (ts->fplayer, MS_FILTER_GET_NCHANNELS,
								   &val);
257

aymeric's avatar
aymeric committed
258 259 260 261 262 263
			if (val!=1)
				{
					ms_error("bench.c: unsupported number of channel for wav file: codec=1 / file=%i",
							 val);
					if (ts->fplayer) ms_filter_destroy(ts->fplayer);
					if (ts->encoder) ms_filter_destroy(ts->encoder);
264
					if (ts->rtpsend) ms_filter_destroy(ts->rtpsend);
aymeric's avatar
aymeric committed
265 266 267 268 269 270 271
					if (ts->rtprecv) ms_filter_destroy(ts->rtprecv);
					if (ts->decoder) ms_filter_destroy(ts->decoder);
					if (ts->frecorder) ms_filter_destroy(ts->frecorder);
					ortp_free(ts);
					return count;
				}
			ms_filter_call_method_noarg(ts->fplayer,MS_FILE_PLAYER_START);
272

aymeric's avatar
aymeric committed
273 274 275 276 277 278 279 280 281
			if (strstr(bench->wavfile, ".au")==NULL)
				{
					ms_filter_link(ts->fplayer,0,ts->encoder,0);
					ms_filter_link(ts->encoder,0,ts->rtpsend,0);
				}
			else
				{
					ms_filter_link(ts->fplayer,0,ts->rtpsend,0);
				}
282

aymeric's avatar
aymeric committed
283 284
			ms_filter_link(ts->rtprecv,0,ts->decoder,0);
			ms_filter_link(ts->decoder,0,ts->frecorder,0);
285

aymeric's avatar
aymeric committed
286 287
			ms_ticker_attach(bench->ticker,ts->fplayer);
			ms_ticker_attach(bench->ticker,ts->rtprecv);
288

aymeric's avatar
aymeric committed
289 290 291 292 293 294 295 296 297
			if (pos < bench->num_session_record)
			{
				char rec_file[128];
				snprintf(rec_file, sizeof(rec_file), "rec_%s_%i.wav",
						 bench->ip_destination,
						 bench->port_destination+pos*2);
				ms_filter_call_method(ts->frecorder,MS_FILE_REC_OPEN,(void*)rec_file);
				ms_filter_call_method_noarg(ts->frecorder,MS_FILE_REC_START);
			}
298

299
			bench->tsessions = bctbx_list_append(bench->tsessions, (void*)ts);
aymeric's avatar
aymeric committed
300 301 302 303 304 305 306 307
			count++;
		}

	return count;
}

static int uninit_bench(struct bench_config *bench)
{
308
	bctbx_list_t *it;
aymeric's avatar
aymeric committed
309 310
	for(it=bench->tsessions;it!=NULL;it=bench->tsessions){
		struct test_session *ts = (struct test_session *)it->data;
311
		bench->tsessions = bctbx_list_erase_link(bench->tsessions, it);
aymeric's avatar
aymeric committed
312 313 314

		ms_ticker_detach(bench->ticker,ts->fplayer);
		ms_ticker_detach(bench->ticker,ts->rtprecv);
315

aymeric's avatar
aymeric committed
316
		ms_filter_call_method_noarg(ts->frecorder,MS_FILE_REC_CLOSE);
317

aymeric's avatar
aymeric committed
318 319 320 321 322 323 324 325 326
		if (strstr(bench->wavfile, ".au")==NULL)
			{
				ms_filter_unlink(ts->fplayer,0,ts->encoder,0);
				ms_filter_unlink(ts->encoder,0,ts->rtpsend,0);
			}
		else
			{
				ms_filter_unlink(ts->fplayer,0,ts->rtpsend,0);
			}
327

aymeric's avatar
aymeric committed
328 329
		ms_filter_unlink(ts->rtprecv,0,ts->decoder,0);
		ms_filter_unlink(ts->decoder,0,ts->frecorder,0);
330

aymeric's avatar
aymeric committed
331 332 333
		if (ts->fplayer) ms_filter_destroy(ts->fplayer);
		if (ts->encoder) ms_filter_destroy(ts->encoder);
		if (ts->rtpsend) ms_filter_destroy(ts->rtpsend);
334

aymeric's avatar
aymeric committed
335 336 337 338 339 340
		if (ts->rtprecv) ms_filter_destroy(ts->rtprecv);
		if (ts->decoder) ms_filter_destroy(ts->decoder);
		if (ts->frecorder) ms_filter_destroy(ts->frecorder);

		ortp_free(ts);
	}
341

aymeric's avatar
aymeric committed
342
	ms_ticker_destroy(bench->ticker);
343
	ms_factory_destroy(bench->factory);
aymeric's avatar
aymeric committed
344 345 346 347 348 349 350 351
	return 0;
}


int main(int argc, char *argv[]){
	int pos;
	int count;
	ortp_init();
352
	ortp_set_log_level_mask(ORTP_LOG_DOMAIN, ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
353 354 355
	

	
aymeric's avatar
aymeric committed
356 357 358 359
	rtp_profile_set_payload(&av_profile,115,&payload_type_lpc1015);
	rtp_profile_set_payload(&av_profile,110,&payload_type_speex_nb);
	rtp_profile_set_payload(&av_profile,111,&payload_type_speex_wb);
	rtp_profile_set_payload(&av_profile,112,&payload_type_ilbc);
360

aymeric's avatar
aymeric committed
361 362 363 364 365
	signal(SIGINT,stop);

	count=0;
	for (pos=0;cfg[pos].num_session!=0;pos++)
		{
366
			count = count + init_bench( &cfg[pos]);
367
			ms_sleep(10);
aymeric's avatar
aymeric committed
368 369 370
		}

	ms_message("Number of session started: %i.", count);
371

aymeric's avatar
aymeric committed
372
	while(run)
373
		ms_sleep(1);
aymeric's avatar
aymeric committed
374 375 376 377 378

	for (pos=0;cfg[pos].num_session!=0;pos++)
		{
			uninit_bench(&cfg[pos]);
		}
379 380
	

aymeric's avatar
aymeric committed
381 382 383 384

	return 0;
}