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