1
/* $Id: confbench.c 3664 2011-07-19 03:42:28Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
* \page page_pjmedia_samples_confbench_c Samples: Benchmarking Conference Bridge
25
* Benchmarking pjmedia (conference bridge+resample). For my use only,
26
* and it only works in Win32.
28
* This file is pjsip-apps/src/samples/confbench.c
30
* \includelineno confbench.c
35
#include <pjlib-util.h> /* pj_getopt */
37
#include <stdlib.h> /* atoi() */
41
/* For logging purpose. */
42
#define THIS_FILE "confsample.c"
46
* LARGE_SET will create in total of about 232 ports.
47
* HAS_RESAMPLE will activate resampling on about half
50
#define TEST_SET LARGE_SET
51
#define HAS_RESAMPLE 0
58
#define PORT_COUNT 254
59
#define CLOCK_RATE 16000
60
#define SAMPLES_PER_FRAME (CLOCK_RATE/100)
62
# define SINE_CLOCK 32000
64
# define SINE_CLOCK CLOCK_RATE
69
#define SINE_COUNT TEST_SET
70
#define NULL_COUNT TEST_SET
74
static void app_perror(const char *sender, const char *title, pj_status_t status)
76
char errmsg[PJ_ERR_MSG_SIZE];
78
pj_strerror(status, errmsg, sizeof(errmsg));
79
PJ_LOG(1,(sender, "%s: %s", title, errmsg));
86
ULARGE_INTEGER u_kernel_time;
88
ULARGE_INTEGER u_user_time;
89
ULARGE_INTEGER u_total;
92
static void process(struct Times *t)
94
pj_memcpy(&t->u_kernel_time, &t->kernel_time, sizeof(FILETIME));
95
pj_memcpy(&t->u_user_time, &t->user_time, sizeof(FILETIME));
96
t->u_total.QuadPart = t->u_kernel_time.QuadPart + t->u_user_time.QuadPart;
99
static void benchmark(void)
101
FILETIME creation_time, exit_time;
102
struct Times start, end;
104
LARGE_INTEGER elapsed;
109
puts("Test started!"); fflush(stdout);
112
rc = GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time,
113
&start.kernel_time, &start.user_time);
114
for (i=DURATION; i>0; --i) {
115
printf("\r%d ", i); fflush(stdout);
116
pj_thread_sleep(1000);
118
rc = GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time,
119
&end.kernel_time, &end.user_time);
125
elapsed.QuadPart = end.u_total.QuadPart - start.u_total.QuadPart;
127
pct = elapsed.QuadPart * 100.0 / ((te-ts)*10000.0);
129
printf("CPU usage=%6.4f%%\n", pct); fflush(stdout);
134
/* Struct attached to sine generator */
137
pj_int16_t *samples; /* Sine samples. */
141
/* This callback is called to feed more samples */
142
static pj_status_t sine_get_frame( pjmedia_port *port,
143
pjmedia_frame *frame)
145
port_data *sine = port->port_data.pdata;
146
pj_int16_t *samples = frame->buf;
147
unsigned i, count, left, right;
149
/* Get number of samples */
150
count = frame->size / 2 / PJMEDIA_PIA_CCNT(&port->info);
155
for (i=0; i<count; ++i) {
156
*samples++ = sine->samples[left];
159
if (PJMEDIA_PIA_CCNT(&port->info) == 2) {
160
*samples++ = sine->samples[right];
161
right += 2; /* higher pitch so we can distinguish left and right. */
167
/* Must set frame->type correctly, otherwise the sound device
168
* will refuse to play.
170
frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
176
#define M_PI (3.14159265)
180
* Create a media port to generate sine wave samples.
182
static pj_status_t create_sine_port(pj_pool_t *pool,
183
unsigned sampling_rate,
184
unsigned channel_count,
185
pjmedia_port **p_port)
193
PJ_ASSERT_RETURN(pool && channel_count > 0 && channel_count <= 2,
196
port = pj_pool_zalloc(pool, sizeof(pjmedia_port));
197
PJ_ASSERT_RETURN(port != NULL, PJ_ENOMEM);
199
/* Fill in port info. */
200
port_name = pj_str("sine generator");
201
pjmedia_port_info_init(&port->info, &port_name,
202
12345, sampling_rate, channel_count, 16,
203
sampling_rate * SINE_PTIME / 1000 * channel_count);
205
/* Set the function to feed frame */
206
port->get_frame = &sine_get_frame;
208
/* Create sine port data */
209
port->port_data.pdata = sine = pj_pool_zalloc(pool, sizeof(port_data));
212
count = PJMEDIA_PIA_SPF(&port->info) / channel_count;
213
sine->samples = pj_pool_alloc(pool, count * sizeof(pj_int16_t));
214
PJ_ASSERT_RETURN(sine->samples != NULL, PJ_ENOMEM);
216
/* initialise sinusoidal wavetable */
217
for( i=0; i<count; i++ )
219
sine->samples[i] = (pj_int16_t) (10000.0 *
220
sin(((double)i/(double)count) * M_PI * 8.) );
231
pjmedia_endpt *med_endpt;
235
pjmedia_port *sine_port[SINE_COUNT], *null_port, *conf_port;
236
pjmedia_port *nulls[NULL_COUNT];
237
unsigned null_slots[NULL_COUNT];
238
pjmedia_master_port *master_port;
245
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
247
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
248
pool = pj_pool_create( &cp.factory, /* pool factory */
249
"wav", /* pool name. */
250
4000, /* init size */
251
4000, /* increment size */
252
NULL /* callback on error */
255
status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
256
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
260
status = pjmedia_conf_create( pool,
263
1, SAMPLES_PER_FRAME, 16,
264
PJMEDIA_CONF_NO_DEVICE,
266
if (status != PJ_SUCCESS) {
267
app_perror(THIS_FILE, "Unable to create conference bridge", status);
271
printf("Resampling is %s\n", (HAS_RESAMPLE?"active":"disabled"));
273
/* Create Null ports */
274
printf("Creating %d null ports..\n", NULL_COUNT);
275
for (i=0; i<NULL_COUNT; ++i) {
276
status = pjmedia_null_port_create(pool, CLOCK_RATE, 1, SAMPLES_PER_FRAME*2, 16, &nulls[i]);
277
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
279
status = pjmedia_conf_add_port(conf, pool, nulls[i], NULL, &null_slots[i]);
280
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
283
/* Create sine ports. */
284
printf("Creating %d sine generator ports..\n", SINE_COUNT);
285
for (i=0; i<SINE_COUNT; ++i) {
288
/* Load the WAV file to file port. */
289
status = create_sine_port(pool, SINE_CLOCK, 1, &sine_port[i]);
290
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
292
/* Add the file port to conference bridge */
293
status = pjmedia_conf_add_port( conf, /* The bridge */
295
sine_port[i], /* port to connect */
296
NULL, /* Use port's name */
297
&slot /* ptr for slot # */
299
if (status != PJ_SUCCESS) {
300
app_perror(THIS_FILE, "Unable to add conference port", status);
304
status = pjmedia_conf_connect_port(conf, slot, 0, 0);
305
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
307
for (j=0; j<NULL_COUNT; ++j) {
308
status = pjmedia_conf_connect_port(conf, slot, null_slots[j], 0);
309
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
313
/* Create idle ports */
314
printf("Creating %d idle ports..\n", IDLE_COUNT);
315
for (i=0; i<IDLE_COUNT; ++i) {
317
status = pjmedia_null_port_create(pool, CLOCK_RATE, 1, SAMPLES_PER_FRAME, 16, &dummy);
318
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
319
status = pjmedia_conf_add_port(conf, pool, dummy, NULL, NULL);
320
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
323
/* Create null port */
324
status = pjmedia_null_port_create(pool, CLOCK_RATE, 1, SAMPLES_PER_FRAME, 16,
326
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
328
conf_port = pjmedia_conf_get_master_port(conf);
330
/* Create master port */
331
status = pjmedia_master_port_create(pool, null_port, conf_port, 0, &master_port);
334
pjmedia_master_port_start(master_port);
336
puts("Waiting to settle.."); fflush(stdout);
337
pj_thread_sleep(5000);