blob: 5ab498643e88c66842dfe8811c37925d00cda8ee [file] [log] [blame]
Benny Prijonoc78c3a32006-06-16 15:54:43 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonoc78c3a32006-06-16 15:54:43 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Benny Prijono1ec70b32006-06-20 15:39:07 +000020
21/**
22 * \page page_pjmedia_samples_confbench_c Samples: Benchmarking Conference Bridge
23 *
Benny Prijonoc78c3a32006-06-16 15:54:43 +000024 * Benchmarking pjmedia (conference bridge+resample). For my use only,
25 * and it only works in Win32.
Benny Prijono1ec70b32006-06-20 15:39:07 +000026 *
27 * This file is pjsip-apps/src/samples/confbench.c
28 *
29 * \includelineno confbench.c
Benny Prijonoc78c3a32006-06-16 15:54:43 +000030 */
31
Benny Prijono1ec70b32006-06-20 15:39:07 +000032
Benny Prijonoc78c3a32006-06-16 15:54:43 +000033#include <pjmedia.h>
34#include <pjlib-util.h> /* pj_getopt */
35#include <pjlib.h>
36#include <stdlib.h> /* atoi() */
37#include <stdio.h>
38#include <windows.h>
39
40/* For logging purpose. */
41#define THIS_FILE "confsample.c"
42
43
44/* Configurable:
45 * LARGE_SET will create in total of about 232 ports.
46 * HAS_RESAMPLE will activate resampling on about half
47 * the port.
48 */
Benny Prijono1ec70b32006-06-20 15:39:07 +000049#define TEST_SET LARGE_SET
50#define HAS_RESAMPLE 0
Benny Prijonoc78c3a32006-06-16 15:54:43 +000051
52
53#define SMALL_SET 16
54#define LARGE_SET 100
55
56
57#define PORT_COUNT 254
58#define CLOCK_RATE 16000
59#define SAMPLES_PER_FRAME (CLOCK_RATE/100)
60#if HAS_RESAMPLE
61# define SINE_CLOCK 32000
62#else
63# define SINE_CLOCK CLOCK_RATE
64#endif
65#define SINE_PTIME 20
66#define DURATION 10
67
68#define SINE_COUNT TEST_SET
69#define NULL_COUNT TEST_SET
70#define IDLE_COUNT 32
71
72
73static void app_perror(const char *sender, const char *title, pj_status_t status)
74{
75 char errmsg[PJ_ERR_MSG_SIZE];
76
77 pj_strerror(status, errmsg, sizeof(errmsg));
78 PJ_LOG(1,(sender, "%s: %s", title, errmsg));
79}
80
81
82struct Times
83{
84 FILETIME kernel_time;
85 ULARGE_INTEGER u_kernel_time;
86 FILETIME user_time;
87 ULARGE_INTEGER u_user_time;
88 ULARGE_INTEGER u_total;
89};
90
91static void process(struct Times *t)
92{
93 pj_memcpy(&t->u_kernel_time, &t->kernel_time, sizeof(FILETIME));
94 pj_memcpy(&t->u_user_time, &t->user_time, sizeof(FILETIME));
95 t->u_total.QuadPart = t->u_kernel_time.QuadPart + t->u_user_time.QuadPart;
96}
97
98static void benchmark(void)
99{
100 FILETIME creation_time, exit_time;
101 struct Times start, end;
102 DWORD ts, te;
103 LARGE_INTEGER elapsed;
104 BOOL rc;
105 int i;
106 double pct;
107
108 puts("Test started!"); fflush(stdout);
109
110 ts = GetTickCount();
111 rc = GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time,
112 &start.kernel_time, &start.user_time);
113 for (i=DURATION; i>0; --i) {
114 printf("\r%d ", i); fflush(stdout);
115 pj_thread_sleep(1000);
116 }
117 rc = GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time,
118 &end.kernel_time, &end.user_time);
119 te = GetTickCount();
120
121 process(&start);
122 process(&end);
123
124 elapsed.QuadPart = end.u_total.QuadPart - start.u_total.QuadPart;
125
126 pct = elapsed.QuadPart * 100.0 / ((te-ts)*10000.0);
127
128 printf("CPU usage=%6.4f%%\n", pct); fflush(stdout);
129}
130
131
132
133/* Struct attached to sine generator */
134typedef struct
135{
136 pj_int16_t *samples; /* Sine samples. */
137} port_data;
138
139
140/* This callback is called to feed more samples */
141static pj_status_t sine_get_frame( pjmedia_port *port,
142 pjmedia_frame *frame)
143{
Benny Prijonoe85bc412006-07-29 20:29:24 +0000144 port_data *sine = port->port_data.pdata;
Benny Prijonoc78c3a32006-06-16 15:54:43 +0000145 pj_int16_t *samples = frame->buf;
146 unsigned i, count, left, right;
147
148 /* Get number of samples */
149 count = frame->size / 2 / port->info.channel_count;
150
151 left = 0;
152 right = 0;
153
154 for (i=0; i<count; ++i) {
155 *samples++ = sine->samples[left];
156 ++left;
157
158 if (port->info.channel_count == 2) {
159 *samples++ = sine->samples[right];
160 right += 2; /* higher pitch so we can distinguish left and right. */
161 if (right >= count)
162 right = 0;
163 }
164 }
165
166 /* Must set frame->type correctly, otherwise the sound device
167 * will refuse to play.
168 */
169 frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
170
171 return PJ_SUCCESS;
172}
173
174#ifndef M_PI
175#define M_PI (3.14159265)
176#endif
177
178/*
179 * Create a media port to generate sine wave samples.
180 */
181static pj_status_t create_sine_port(pj_pool_t *pool,
182 unsigned sampling_rate,
183 unsigned channel_count,
184 pjmedia_port **p_port)
185{
186 pjmedia_port *port;
187 unsigned i;
188 unsigned count;
189 port_data *sine;
190
191 PJ_ASSERT_RETURN(pool && channel_count > 0 && channel_count <= 2,
192 PJ_EINVAL);
193
194 port = pj_pool_zalloc(pool, sizeof(pjmedia_port));
195 PJ_ASSERT_RETURN(port != NULL, PJ_ENOMEM);
196
197 /* Fill in port info. */
198 port->info.bits_per_sample = 16;
199 port->info.channel_count = channel_count;
200 port->info.encoding_name = pj_str("pcm");
201 port->info.has_info = 1;
202 port->info.name = pj_str("sine generator");
203 port->info.need_info = 0;
204 port->info.pt = 0xFF;
205 port->info.clock_rate = sampling_rate;
206 port->info.samples_per_frame = sampling_rate * SINE_PTIME / 1000 * channel_count;
207 port->info.bytes_per_frame = port->info.samples_per_frame * 2;
208 port->info.type = PJMEDIA_TYPE_AUDIO;
209
210 /* Set the function to feed frame */
211 port->get_frame = &sine_get_frame;
212
213 /* Create sine port data */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000214 port->port_data.pdata = sine = pj_pool_zalloc(pool, sizeof(port_data));
Benny Prijonoc78c3a32006-06-16 15:54:43 +0000215
216 /* Create samples */
217 count = port->info.samples_per_frame / channel_count;
218 sine->samples = pj_pool_alloc(pool, count * sizeof(pj_int16_t));
219 PJ_ASSERT_RETURN(sine->samples != NULL, PJ_ENOMEM);
220
221 /* initialise sinusoidal wavetable */
222 for( i=0; i<count; i++ )
223 {
224 sine->samples[i] = (pj_int16_t) (10000.0 *
225 sin(((double)i/(double)count) * M_PI * 8.) );
226 }
227
228 *p_port = port;
229
230 return PJ_SUCCESS;
231}
232
233int main()
234{
235 pj_caching_pool cp;
236 pjmedia_endpt *med_endpt;
237 pj_pool_t *pool;
238 pjmedia_conf *conf;
239 int i;
240 pjmedia_port *sine_port[SINE_COUNT], *null_port, *conf_port;
241 pjmedia_port *nulls[NULL_COUNT];
242 unsigned null_slots[NULL_COUNT];
243 pjmedia_master_port *master_port;
244 pj_status_t status;
245
246
247 pj_log_set_level(3);
248
249 status = pj_init();
250 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
251
252 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
253 pool = pj_pool_create( &cp.factory, /* pool factory */
254 "wav", /* pool name. */
255 4000, /* init size */
256 4000, /* increment size */
257 NULL /* callback on error */
258 );
259
260 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
261 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
262
263
264
265 status = pjmedia_conf_create( pool,
266 PORT_COUNT,
267 CLOCK_RATE,
268 1, SAMPLES_PER_FRAME, 16,
269 PJMEDIA_CONF_NO_DEVICE,
270 &conf);
271 if (status != PJ_SUCCESS) {
272 app_perror(THIS_FILE, "Unable to create conference bridge", status);
273 return 1;
274 }
275
Benny Prijono1ec70b32006-06-20 15:39:07 +0000276 printf("Resampling is %s\n", (HAS_RESAMPLE?"active":"disabled"));
277
Benny Prijonoc78c3a32006-06-16 15:54:43 +0000278 /* Create Null ports */
Benny Prijono1ec70b32006-06-20 15:39:07 +0000279 printf("Creating %d null ports..\n", NULL_COUNT);
Benny Prijonoc78c3a32006-06-16 15:54:43 +0000280 for (i=0; i<NULL_COUNT; ++i) {
281 status = pjmedia_null_port_create(pool, CLOCK_RATE, 1, SAMPLES_PER_FRAME*2, 16, &nulls[i]);
282 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
283
284 status = pjmedia_conf_add_port(conf, pool, nulls[i], NULL, &null_slots[i]);
285 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
286 }
287
288 /* Create sine ports. */
Benny Prijono1ec70b32006-06-20 15:39:07 +0000289 printf("Creating %d sine generator ports..\n", SINE_COUNT);
Benny Prijonoc78c3a32006-06-16 15:54:43 +0000290 for (i=0; i<SINE_COUNT; ++i) {
291 unsigned j, slot;
292
293 /* Load the WAV file to file port. */
294 status = create_sine_port(pool, SINE_CLOCK, 1, &sine_port[i]);
295 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
296
297 /* Add the file port to conference bridge */
298 status = pjmedia_conf_add_port( conf, /* The bridge */
299 pool, /* pool */
300 sine_port[i], /* port to connect */
301 NULL, /* Use port's name */
302 &slot /* ptr for slot # */
303 );
304 if (status != PJ_SUCCESS) {
305 app_perror(THIS_FILE, "Unable to add conference port", status);
306 return 1;
307 }
308
309 status = pjmedia_conf_connect_port(conf, slot, 0, 0);
310 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
311
312 for (j=0; j<NULL_COUNT; ++j) {
313 status = pjmedia_conf_connect_port(conf, slot, null_slots[j], 0);
314 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
315 }
316 }
317
318 /* Create idle ports */
Benny Prijono1ec70b32006-06-20 15:39:07 +0000319 printf("Creating %d idle ports..\n", IDLE_COUNT);
Benny Prijonoc78c3a32006-06-16 15:54:43 +0000320 for (i=0; i<IDLE_COUNT; ++i) {
321 pjmedia_port *dummy;
322 status = pjmedia_null_port_create(pool, CLOCK_RATE, 1, SAMPLES_PER_FRAME, 16, &dummy);
323 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
324 status = pjmedia_conf_add_port(conf, pool, dummy, NULL, NULL);
325 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
326 }
327
328 /* Create null port */
329 status = pjmedia_null_port_create(pool, CLOCK_RATE, 1, SAMPLES_PER_FRAME, 16,
330 &null_port);
331 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
332
333 conf_port = pjmedia_conf_get_master_port(conf);
334
335 /* Create master port */
336 status = pjmedia_master_port_create(pool, null_port, conf_port, 0, &master_port);
337
338
339 pjmedia_master_port_start(master_port);
340
341 puts("Waiting to settle.."); fflush(stdout);
342 pj_thread_sleep(5000);
343
344
345 benchmark();
346
347
348 /* Done. */
349 return 0;
350}
351
352