BLOG | DOCUMENTATION | TRAC

Home --> Documentations --> PJMEDIA Reference

Samples: Benchmarking Conference Bridge

Benchmarking pjmedia (conference bridge+resample). For my use only, and it only works in Win32.

This file is pjsip-apps/src/samples/confbench.c

1 /* $Id: confbench.c 4537 2013-06-19 06:47:43Z riza $ */
2 /*
3  * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5  *
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 
21 
34 #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  */
50 #define TEST_SET LARGE_SET
51 #define HAS_RESAMPLE 0
52 
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 
74 static 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 
83 struct 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 
92 static 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 
99 static 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 */
135 typedef struct
136 {
137  pj_int16_t *samples; /* Sine samples. */
138 } port_data;
139 
140 
141 /* This callback is called to feed more samples */
142 static pj_status_t sine_get_frame( pjmedia_port *port,
143  pjmedia_frame *frame)
144 {
145  port_data *sine = port->port_data.pdata;
146  pj_int16_t *samples = frame->buf;
147  unsigned i, left, right;
148  pj_size_t count;
149 
150  /* Get number of samples */
151  count = frame->size / 2 / PJMEDIA_PIA_CCNT(&port->info);
152 
153  left = 0;
154  right = 0;
155 
156  for (i=0; i<count; ++i) {
157  *samples++ = sine->samples[left];
158  ++left;
159 
160  if (PJMEDIA_PIA_CCNT(&port->info) == 2) {
161  *samples++ = sine->samples[right];
162  right += 2; /* higher pitch so we can distinguish left and right. */
163  if (right >= count)
164  right = 0;
165  }
166  }
167 
168  /* Must set frame->type correctly, otherwise the sound device
169  * will refuse to play.
170  */
172 
173  return PJ_SUCCESS;
174 }
175 
176 #ifndef M_PI
177 #define M_PI (3.14159265)
178 #endif
179 
180 /*
181  * Create a media port to generate sine wave samples.
182  */
183 static pj_status_t create_sine_port(pj_pool_t *pool,
184  unsigned sampling_rate,
185  unsigned channel_count,
186  pjmedia_port **p_port)
187 {
188  pjmedia_port *port;
189  unsigned i;
190  unsigned count;
191  pj_str_t port_name;
192  port_data *sine;
193 
194  PJ_ASSERT_RETURN(pool && channel_count > 0 && channel_count <= 2,
195  PJ_EINVAL);
196 
197  port = pj_pool_zalloc(pool, sizeof(pjmedia_port));
198  PJ_ASSERT_RETURN(port != NULL, PJ_ENOMEM);
199 
200  /* Fill in port info. */
201  port_name = pj_str("sine generator");
202  pjmedia_port_info_init(&port->info, &port_name,
203  12345, sampling_rate, channel_count, 16,
204  sampling_rate * SINE_PTIME / 1000 * channel_count);
205 
206  /* Set the function to feed frame */
207  port->get_frame = &sine_get_frame;
208 
209  /* Create sine port data */
210  port->port_data.pdata = sine = pj_pool_zalloc(pool, sizeof(port_data));
211 
212  /* Create samples */
213  count = PJMEDIA_PIA_SPF(&port->info) / channel_count;
214  sine->samples = pj_pool_alloc(pool, count * sizeof(pj_int16_t));
215  PJ_ASSERT_RETURN(sine->samples != NULL, PJ_ENOMEM);
216 
217  /* initialise sinusoidal wavetable */
218  for( i=0; i<count; i++ )
219  {
220  sine->samples[i] = (pj_int16_t) (10000.0 *
221  sin(((double)i/(double)count) * M_PI * 8.) );
222  }
223 
224  *p_port = port;
225 
226  return PJ_SUCCESS;
227 }
228 
229 int main()
230 {
231  pj_caching_pool cp;
232  pjmedia_endpt *med_endpt;
233  pj_pool_t *pool;
234  pjmedia_conf *conf;
235  int i;
236  pjmedia_port *sine_port[SINE_COUNT], *null_port, *conf_port;
237  pjmedia_port *nulls[NULL_COUNT];
238  unsigned null_slots[NULL_COUNT];
239  pjmedia_master_port *master_port;
240  pj_status_t status;
241 
242 
243  pj_log_set_level(3);
244 
245  status = pj_init();
246  PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
247 
249  pool = pj_pool_create( &cp.factory, /* pool factory */
250  "wav", /* pool name. */
251  4000, /* init size */
252  4000, /* increment size */
253  NULL /* callback on error */
254  );
255 
256  status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
257  PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
258 
259 
260 
261  status = pjmedia_conf_create( pool,
262  PORT_COUNT,
263  CLOCK_RATE,
264  1, SAMPLES_PER_FRAME, 16,
266  &conf);
267  if (status != PJ_SUCCESS) {
268  app_perror(THIS_FILE, "Unable to create conference bridge", status);
269  return 1;
270  }
271 
272  printf("Resampling is %s\n", (HAS_RESAMPLE?"active":"disabled"));
273 
274  /* Create Null ports */
275  printf("Creating %d null ports..\n", NULL_COUNT);
276  for (i=0; i<NULL_COUNT; ++i) {
277  status = pjmedia_null_port_create(pool, CLOCK_RATE, 1, SAMPLES_PER_FRAME*2, 16, &nulls[i]);
278  PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
279 
280  status = pjmedia_conf_add_port(conf, pool, nulls[i], NULL, &null_slots[i]);
281  PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
282  }
283 
284  /* Create sine ports. */
285  printf("Creating %d sine generator ports..\n", SINE_COUNT);
286  for (i=0; i<SINE_COUNT; ++i) {
287  unsigned j, slot;
288 
289  /* Load the WAV file to file port. */
290  status = create_sine_port(pool, SINE_CLOCK, 1, &sine_port[i]);
291  PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
292 
293  /* Add the file port to conference bridge */
294  status = pjmedia_conf_add_port( conf, /* The bridge */
295  pool, /* pool */
296  sine_port[i], /* port to connect */
297  NULL, /* Use port's name */
298  &slot /* ptr for slot # */
299  );
300  if (status != PJ_SUCCESS) {
301  app_perror(THIS_FILE, "Unable to add conference port", status);
302  return 1;
303  }
304 
305  status = pjmedia_conf_connect_port(conf, slot, 0, 0);
306  PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
307 
308  for (j=0; j<NULL_COUNT; ++j) {
309  status = pjmedia_conf_connect_port(conf, slot, null_slots[j], 0);
310  PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
311  }
312  }
313 
314  /* Create idle ports */
315  printf("Creating %d idle ports..\n", IDLE_COUNT);
316  for (i=0; i<IDLE_COUNT; ++i) {
317  pjmedia_port *dummy;
318  status = pjmedia_null_port_create(pool, CLOCK_RATE, 1, SAMPLES_PER_FRAME, 16, &dummy);
319  PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
320  status = pjmedia_conf_add_port(conf, pool, dummy, NULL, NULL);
321  PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
322  }
323 
324  /* Create null port */
325  status = pjmedia_null_port_create(pool, CLOCK_RATE, 1, SAMPLES_PER_FRAME, 16,
326  &null_port);
327  PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
328 
329  conf_port = pjmedia_conf_get_master_port(conf);
330 
331  /* Create master port */
332  status = pjmedia_master_port_create(pool, null_port, conf_port, 0, &master_port);
333 
334 
335  pjmedia_master_port_start(master_port);
336 
337  puts("Waiting to settle.."); fflush(stdout);
338  pj_thread_sleep(5000);
339 
340 
341  benchmark();
342 
343 
344  /* Done. */
345  return 0;
346 }
347 
348 
pj_status_t pjmedia_conf_create(pj_pool_t *pool, unsigned max_slots, unsigned sampling_rate, unsigned channel_count, unsigned samples_per_frame, unsigned bits_per_sample, unsigned options, pjmedia_conf **p_conf)
PJMEDIA main header file.
pj_status_t pjmedia_port_info_init(pjmedia_port_info *info, const pj_str_t *name, unsigned signature, unsigned clock_rate, unsigned channel_count, unsigned bits_per_sample, unsigned samples_per_frame)
#define PJ_ASSERT_RETURN(expr, retval)
pjmedia_port * pjmedia_conf_get_master_port(pjmedia_conf *conf)
pj_size_t size
Definition: frame.h:60
pjmedia_port_info info
Definition: port.h:366
void * pj_memcpy(void *dst, const void *src, pj_size_t size)
struct pjmedia_endpt pjmedia_endpt
Definition: types.h:187
pj_status_t pjmedia_master_port_create(pj_pool_t *pool, pjmedia_port *u_port, pjmedia_port *d_port, unsigned options, pjmedia_master_port **p_m)
#define PJ_LOG(level, arg)
int pj_status_t
short pj_int16_t
pj_pool_factory_policy pj_pool_factory_default_policy
void * pj_pool_alloc(pj_pool_t *pool, pj_size_t size)
Definition: port.h:364
pj_pool_factory factory
pj_status_t pjmedia_master_port_start(pjmedia_master_port *m)
Definition: confbench.c:83
pj_status_t pjmedia_conf_connect_port(pjmedia_conf *conf, unsigned src_slot, unsigned sink_slot, int adj_level)
pj_status_t(* get_frame)(struct pjmedia_port *this_port, pjmedia_frame *frame)
Definition: port.h:394
unsigned PJMEDIA_PIA_CCNT(const pjmedia_port_info *pia)
Definition: port.h:257
pj_status_t pjmedia_endpt_create(pj_pool_factory *pf, pj_ioqueue_t *ioqueue, unsigned worker_cnt, pjmedia_endpt **p_endpt)
Definition: endpoint.h:105
pj_str_t pj_str(char *str)
struct pjmedia_conf pjmedia_conf
Definition: conference.h:60
void pj_caching_pool_init(pj_caching_pool *ch_pool, const pj_pool_factory_policy *policy, pj_size_t max_capacity)
void * buf
Definition: frame.h:59
pj_str_t pj_strerror(pj_status_t statcode, char *buf, pj_size_t bufsize)
pj_status_t pjmedia_conf_add_port(pjmedia_conf *conf, pj_pool_t *pool, pjmedia_port *strm_port, const pj_str_t *name, unsigned *p_slot)
unsigned PJMEDIA_PIA_SPF(const pjmedia_port_info *pia)
Definition: port.h:298
#define PJ_ERR_MSG_SIZE
pj_pool_t * pj_pool_create(pj_pool_factory *factory, const char *name, pj_size_t initial_size, pj_size_t increment_size, pj_pool_callback *callback)
void * pdata
Definition: port.h:372
pjmedia_frame_type type
Definition: frame.h:58
Definition: frame.h:56
pj_status_t pj_thread_sleep(unsigned msec)
typedefPJ_BEGIN_DECL struct pjmedia_master_port pjmedia_master_port
Definition: master_port.h:66
void * pj_pool_zalloc(pj_pool_t *pool, pj_size_t size)
PJ_SUCCESS
PJ_BEGIN_DECL pj_status_t pjmedia_null_port_create(pj_pool_t *pool, unsigned sampling_rate, unsigned channel_count, unsigned samples_per_frame, unsigned bits_per_sample, pjmedia_port **p_port)
pj_status_t pj_init(void)
size_t pj_size_t
void pj_log_set_level(int level)
Definition: frame.h:46
Definition: conference.h:94

 


PJMEDIA small footprint Open Source media stack
Copyright (C) 2006-2008 Teluu Inc.