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