PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/CSipSimple/jni/pjsip/sources/pjsip-apps/src/samples/confbench.c

https://bitbucket.org/bohlooli/csipsimple2
C | 347 lines | 213 code | 77 blank | 57 comment | 28 complexity | 5d8058c566e132f9a1234a4394467cd9 MD5 | raw file
  1. /* $Id: confbench.c 3664 2011-07-19 03:42:28Z nanang $ */
  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. * \page page_pjmedia_samples_confbench_c Samples: Benchmarking Conference Bridge
  22. *
  23. * Benchmarking pjmedia (conference bridge+resample). For my use only,
  24. * and it only works in Win32.
  25. *
  26. * This file is pjsip-apps/src/samples/confbench.c
  27. *
  28. * \includelineno confbench.c
  29. */
  30. #include <pjmedia.h>
  31. #include <pjlib-util.h> /* pj_getopt */
  32. #include <pjlib.h>
  33. #include <stdlib.h> /* atoi() */
  34. #include <stdio.h>
  35. #include <windows.h>
  36. /* For logging purpose. */
  37. #define THIS_FILE "confsample.c"
  38. /* Configurable:
  39. * LARGE_SET will create in total of about 232 ports.
  40. * HAS_RESAMPLE will activate resampling on about half
  41. * the port.
  42. */
  43. #define TEST_SET LARGE_SET
  44. #define HAS_RESAMPLE 0
  45. #define SMALL_SET 16
  46. #define LARGE_SET 100
  47. #define PORT_COUNT 254
  48. #define CLOCK_RATE 16000
  49. #define SAMPLES_PER_FRAME (CLOCK_RATE/100)
  50. #if HAS_RESAMPLE
  51. # define SINE_CLOCK 32000
  52. #else
  53. # define SINE_CLOCK CLOCK_RATE
  54. #endif
  55. #define SINE_PTIME 20
  56. #define DURATION 10
  57. #define SINE_COUNT TEST_SET
  58. #define NULL_COUNT TEST_SET
  59. #define IDLE_COUNT 32
  60. static void app_perror(const char *sender, const char *title, pj_status_t status)
  61. {
  62. char errmsg[PJ_ERR_MSG_SIZE];
  63. pj_strerror(status, errmsg, sizeof(errmsg));
  64. PJ_LOG(1,(sender, "%s: %s", title, errmsg));
  65. }
  66. struct Times
  67. {
  68. FILETIME kernel_time;
  69. ULARGE_INTEGER u_kernel_time;
  70. FILETIME user_time;
  71. ULARGE_INTEGER u_user_time;
  72. ULARGE_INTEGER u_total;
  73. };
  74. static void process(struct Times *t)
  75. {
  76. pj_memcpy(&t->u_kernel_time, &t->kernel_time, sizeof(FILETIME));
  77. pj_memcpy(&t->u_user_time, &t->user_time, sizeof(FILETIME));
  78. t->u_total.QuadPart = t->u_kernel_time.QuadPart + t->u_user_time.QuadPart;
  79. }
  80. static void benchmark(void)
  81. {
  82. FILETIME creation_time, exit_time;
  83. struct Times start, end;
  84. DWORD ts, te;
  85. LARGE_INTEGER elapsed;
  86. BOOL rc;
  87. int i;
  88. double pct;
  89. puts("Test started!"); fflush(stdout);
  90. ts = GetTickCount();
  91. rc = GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time,
  92. &start.kernel_time, &start.user_time);
  93. for (i=DURATION; i>0; --i) {
  94. printf("\r%d ", i); fflush(stdout);
  95. pj_thread_sleep(1000);
  96. }
  97. rc = GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time,
  98. &end.kernel_time, &end.user_time);
  99. te = GetTickCount();
  100. process(&start);
  101. process(&end);
  102. elapsed.QuadPart = end.u_total.QuadPart - start.u_total.QuadPart;
  103. pct = elapsed.QuadPart * 100.0 / ((te-ts)*10000.0);
  104. printf("CPU usage=%6.4f%%\n", pct); fflush(stdout);
  105. }
  106. /* Struct attached to sine generator */
  107. typedef struct
  108. {
  109. pj_int16_t *samples; /* Sine samples. */
  110. } port_data;
  111. /* This callback is called to feed more samples */
  112. static pj_status_t sine_get_frame( pjmedia_port *port,
  113. pjmedia_frame *frame)
  114. {
  115. port_data *sine = port->port_data.pdata;
  116. pj_int16_t *samples = frame->buf;
  117. unsigned i, count, left, right;
  118. /* Get number of samples */
  119. count = frame->size / 2 / PJMEDIA_PIA_CCNT(&port->info);
  120. left = 0;
  121. right = 0;
  122. for (i=0; i<count; ++i) {
  123. *samples++ = sine->samples[left];
  124. ++left;
  125. if (PJMEDIA_PIA_CCNT(&port->info) == 2) {
  126. *samples++ = sine->samples[right];
  127. right += 2; /* higher pitch so we can distinguish left and right. */
  128. if (right >= count)
  129. right = 0;
  130. }
  131. }
  132. /* Must set frame->type correctly, otherwise the sound device
  133. * will refuse to play.
  134. */
  135. frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
  136. return PJ_SUCCESS;
  137. }
  138. #ifndef M_PI
  139. #define M_PI (3.14159265)
  140. #endif
  141. /*
  142. * Create a media port to generate sine wave samples.
  143. */
  144. static pj_status_t create_sine_port(pj_pool_t *pool,
  145. unsigned sampling_rate,
  146. unsigned channel_count,
  147. pjmedia_port **p_port)
  148. {
  149. pjmedia_port *port;
  150. unsigned i;
  151. unsigned count;
  152. pj_str_t port_name;
  153. port_data *sine;
  154. PJ_ASSERT_RETURN(pool && channel_count > 0 && channel_count <= 2,
  155. PJ_EINVAL);
  156. port = pj_pool_zalloc(pool, sizeof(pjmedia_port));
  157. PJ_ASSERT_RETURN(port != NULL, PJ_ENOMEM);
  158. /* Fill in port info. */
  159. port_name = pj_str("sine generator");
  160. pjmedia_port_info_init(&port->info, &port_name,
  161. 12345, sampling_rate, channel_count, 16,
  162. sampling_rate * SINE_PTIME / 1000 * channel_count);
  163. /* Set the function to feed frame */
  164. port->get_frame = &sine_get_frame;
  165. /* Create sine port data */
  166. port->port_data.pdata = sine = pj_pool_zalloc(pool, sizeof(port_data));
  167. /* Create samples */
  168. count = PJMEDIA_PIA_SPF(&port->info) / channel_count;
  169. sine->samples = pj_pool_alloc(pool, count * sizeof(pj_int16_t));
  170. PJ_ASSERT_RETURN(sine->samples != NULL, PJ_ENOMEM);
  171. /* initialise sinusoidal wavetable */
  172. for( i=0; i<count; i++ )
  173. {
  174. sine->samples[i] = (pj_int16_t) (10000.0 *
  175. sin(((double)i/(double)count) * M_PI * 8.) );
  176. }
  177. *p_port = port;
  178. return PJ_SUCCESS;
  179. }
  180. int main()
  181. {
  182. pj_caching_pool cp;
  183. pjmedia_endpt *med_endpt;
  184. pj_pool_t *pool;
  185. pjmedia_conf *conf;
  186. int i;
  187. pjmedia_port *sine_port[SINE_COUNT], *null_port, *conf_port;
  188. pjmedia_port *nulls[NULL_COUNT];
  189. unsigned null_slots[NULL_COUNT];
  190. pjmedia_master_port *master_port;
  191. pj_status_t status;
  192. pj_log_set_level(3);
  193. status = pj_init();
  194. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  195. pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
  196. pool = pj_pool_create( &cp.factory, /* pool factory */
  197. "wav", /* pool name. */
  198. 4000, /* init size */
  199. 4000, /* increment size */
  200. NULL /* callback on error */
  201. );
  202. status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
  203. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  204. status = pjmedia_conf_create( pool,
  205. PORT_COUNT,
  206. CLOCK_RATE,
  207. 1, SAMPLES_PER_FRAME, 16,
  208. PJMEDIA_CONF_NO_DEVICE,
  209. &conf);
  210. if (status != PJ_SUCCESS) {
  211. app_perror(THIS_FILE, "Unable to create conference bridge", status);
  212. return 1;
  213. }
  214. printf("Resampling is %s\n", (HAS_RESAMPLE?"active":"disabled"));
  215. /* Create Null ports */
  216. printf("Creating %d null ports..\n", NULL_COUNT);
  217. for (i=0; i<NULL_COUNT; ++i) {
  218. status = pjmedia_null_port_create(pool, CLOCK_RATE, 1, SAMPLES_PER_FRAME*2, 16, &nulls[i]);
  219. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  220. status = pjmedia_conf_add_port(conf, pool, nulls[i], NULL, &null_slots[i]);
  221. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  222. }
  223. /* Create sine ports. */
  224. printf("Creating %d sine generator ports..\n", SINE_COUNT);
  225. for (i=0; i<SINE_COUNT; ++i) {
  226. unsigned j, slot;
  227. /* Load the WAV file to file port. */
  228. status = create_sine_port(pool, SINE_CLOCK, 1, &sine_port[i]);
  229. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  230. /* Add the file port to conference bridge */
  231. status = pjmedia_conf_add_port( conf, /* The bridge */
  232. pool, /* pool */
  233. sine_port[i], /* port to connect */
  234. NULL, /* Use port's name */
  235. &slot /* ptr for slot # */
  236. );
  237. if (status != PJ_SUCCESS) {
  238. app_perror(THIS_FILE, "Unable to add conference port", status);
  239. return 1;
  240. }
  241. status = pjmedia_conf_connect_port(conf, slot, 0, 0);
  242. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  243. for (j=0; j<NULL_COUNT; ++j) {
  244. status = pjmedia_conf_connect_port(conf, slot, null_slots[j], 0);
  245. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  246. }
  247. }
  248. /* Create idle ports */
  249. printf("Creating %d idle ports..\n", IDLE_COUNT);
  250. for (i=0; i<IDLE_COUNT; ++i) {
  251. pjmedia_port *dummy;
  252. status = pjmedia_null_port_create(pool, CLOCK_RATE, 1, SAMPLES_PER_FRAME, 16, &dummy);
  253. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  254. status = pjmedia_conf_add_port(conf, pool, dummy, NULL, NULL);
  255. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  256. }
  257. /* Create null port */
  258. status = pjmedia_null_port_create(pool, CLOCK_RATE, 1, SAMPLES_PER_FRAME, 16,
  259. &null_port);
  260. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  261. conf_port = pjmedia_conf_get_master_port(conf);
  262. /* Create master port */
  263. status = pjmedia_master_port_create(pool, null_port, conf_port, 0, &master_port);
  264. pjmedia_master_port_start(master_port);
  265. puts("Waiting to settle.."); fflush(stdout);
  266. pj_thread_sleep(5000);
  267. benchmark();
  268. /* Done. */
  269. return 0;
  270. }