Debian Codegraph @

Xpjproject/pjsip-apps/src/samples/confsample.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 #include <pjmedia.h>
21 #include <pjlib-util.h> /* pj_getopt */
22 #include <pjlib.h>
23
24 #include <stdlib.h> /* atoi() */
25 #include <stdio.h>
26
27 #include "util.h"
28
29 /**
30 * \page page_pjmedia_samples_confsample_c Samples: Using Conference Bridge
31 *
32 * Sample to mix multiple files in the conference bridge and play the
33 * result to sound device.
34 *
35 * This file is pjsip-apps/src/samples/confsample.c
36 *
37 * \includelineno confsample.c
38 */
39
40
41 /* For logging purpose. */
42 #define THIS_FILE "confsample.c"
43
44
45 /* Shall we put recorder in the conference */
46 #define RECORDER 1
47
48
49 static const char *desc =
50 " FILE: \n"
51 " \n"
52 " confsample.c \n"
53 " \n"
54 " PURPOSE: \n"
55 " \n"
56 " Demonstrate how to use conference bridge. \n"
57 " \n"
58 " USAGE: \n"
59 " \n"
60 " confsample [options] [file1.wav] [file2.wav] ... \n"
61 " \n"
62 " options: \n"
63 SND_USAGE
64 " \n"
65 " fileN.wav are optional WAV files to be connected to the conference \n"
66 " bridge. The WAV files MUST have single channel (mono) and 16 bit PCM \n"
67 " samples. It can have arbitrary sampling rate. \n"
68 " \n"
69 " DESCRIPTION: \n"
70 " \n"
71 " Here we create a conference bridge, with at least one port (port zero \n"
72 " is always created for the sound device). \n"
73 " \n"
74 " If WAV files are specified, the WAV file player ports will be connected \n"
75 " to slot starting from number one in the bridge. The WAV files can have \n"
76 " arbitrary sampling rate; the bridge will convert it to its clock rate. \n"
77 " However, the files MUST have a single audio channel only (i.e. mono). \n";
78
79
80
81 /*
82 * Prototypes:
83 */
84
85 /* List the ports in the conference bridge */
86 static void conf_list(pjmedia_conf *conf, pj_bool_t detail);
87
88 /* Display VU meter */
89 static void monitor_level(pjmedia_conf *conf, int slot, int dir, int dur);
90
91
92 /* Show usage */
93 static void usage(void)
94 {
95 puts("");
96 puts(desc);
97 }
98
99
100
101 /* Input simple string */
102 static pj_bool_t input(const char *title, char *buf, pj_size_t len)
103 {
104 char *p;
105
106 printf("%s (empty to cancel): ", title); fflush(stdout);
107 if (fgets(buf, (int)len, stdin) == NULL)
108 return PJ_FALSE;
109
110 /* Remove trailing newlines. */
111 for (p=buf; ; ++p) {
112 if (*p=='\r' || *p=='\n') *p='\0';
113 else if (!*p) break;
114 }
115
116 if (!*buf)
117 return PJ_FALSE;
118
119 return PJ_TRUE;
120 }
121
122
123 /*****************************************************************************
124 * main()
125 */
126 int main(int argc, char *argv[])
127 {
128 int dev_id = -1;
129 int clock_rate = CLOCK_RATE;
130 int channel_count = NCHANNELS;
131 int samples_per_frame = NSAMPLES;
132 int bits_per_sample = NBITS;
133
134 pj_caching_pool cp;
135 pjmedia_endpt *med_endpt;
136 pj_pool_t *pool;
137 pjmedia_conf *conf;
138
139 int i, port_count, file_count;
140 pjmedia_port **file_port; /* Array of file ports */
141 pjmedia_port *rec_port = NULL; /* Wav writer port */
142
143 char tmp[10];
144 pj_status_t status;
145
146
147 /* Must init PJLIB first: */
148 status = pj_init();
149 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
150
151 /* Get command line options. */
152 if (get_snd_options(THIS_FILE, argc, argv, &dev_id, &clock_rate,
153 &channel_count, &samples_per_frame, &bits_per_sample))
154 {
155 usage();
156 return 1;
157 }
158
159 /* Must create a pool factory before we can allocate any memory. */
160 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
161
162 /*
163 * Initialize media endpoint.
164 * This will implicitly initialize PJMEDIA too.
165 */
166 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
167 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
168
169 /* Create memory pool to allocate memory */
170 pool = pj_pool_create( &cp.factory, /* pool factory */
171 "wav", /* pool name. */
172 4000, /* init size */
173 4000, /* increment size */
174 NULL /* callback on error */
175 );
176
177
178 file_count = argc - pj_optind;
179 port_count = file_count + 1 + RECORDER;
180
181 /* Create the conference bridge.
182 * With default options (zero), the bridge will create an instance of
183 * sound capture and playback device and connect them to slot zero.
184 */
185 status = pjmedia_conf_create( pool, /* pool to use */
186 port_count,/* number of ports */
187 clock_rate,
188 channel_count,
189 samples_per_frame,
190 bits_per_sample,
191 0, /* options */
192 &conf /* result */
193 );
194 if (status != PJ_SUCCESS) {
195 app_perror(THIS_FILE, "Unable to create conference bridge", status);
196 return 1;
197 }
198
199 #if RECORDER
200 status = pjmedia_wav_writer_port_create( pool, "confrecord.wav",
201 clock_rate, channel_count,
202 samples_per_frame,
203 bits_per_sample, 0, 0,
204 &rec_port);
205 if (status != PJ_SUCCESS) {
206 app_perror(THIS_FILE, "Unable to create WAV writer", status);
207 return 1;
208 }
209
210 pjmedia_conf_add_port(conf, pool, rec_port, NULL, NULL);
211 #endif
212
213
214 /* Create file ports. */
215 file_port = pj_pool_alloc(pool, file_count * sizeof(pjmedia_port*));
216
217 for (i=0; i<file_count; ++i) {
218
219 /* Load the WAV file to file port. */
220 status = pjmedia_wav_player_port_create(
221 pool, /* pool. */
222 argv[i+pj_optind], /* filename */
223 0, /* use default ptime */
224 0, /* flags */
225 0, /* buf size */
226 &file_port[i] /* result */
227 );
228 if (status != PJ_SUCCESS) {
229 char title[80];
230 pj_ansi_snprintf(title, sizeof(title), "Unable to use %s",
231 argv[i+pj_optind]);
232 app_perror(THIS_FILE, title, status);
233 usage();
234 return 1;
235 }
236
237 /* Add the file port to conference bridge */
238 status = pjmedia_conf_add_port( conf, /* The bridge */
239 pool, /* pool */
240 file_port[i], /* port to connect */
241 NULL, /* Use port's name */
242 NULL /* ptr for slot # */
243 );
244 if (status != PJ_SUCCESS) {
245 app_perror(THIS_FILE, "Unable to add conference port", status);
246 return 1;
247 }
248 }
249
250
251 /*
252 * All ports are set up in the conference bridge.
253 * But at this point, no media will be flowing since no ports are
254 * "connected". User must connect the port manually.
255 */
256
257
258 /* Dump memory usage */
259 dump_pool_usage(THIS_FILE, &cp);
260
261 /* Sleep to allow log messages to flush */
262 pj_thread_sleep(100);
263
264
265 /*
266 * UI Menu:
267 */
268 for (;;) {
269 char tmp1[10];
270 char tmp2[10];
271 char *err;
272 int src, dst, level, dur;
273
274 puts("");
275 conf_list(conf, 0);
276 puts("");
277 puts("Menu:");
278 puts(" s Show ports details");
279 puts(" c Connect one port to another");
280 puts(" d Disconnect port connection");
281 puts(" t Adjust signal level transmitted (tx) to a port");
282 puts(" r Adjust signal level received (rx) from a port");
283 puts(" v Display VU meter for a particular port");
284 puts(" q Quit");
285 puts("");
286
287 printf("Enter selection: "); fflush(stdout);
288
289 if (fgets(tmp, sizeof(tmp), stdin) == NULL)
290 break;
291
292 switch (tmp[0]) {
293 case 's':
294 puts("");
295 conf_list(conf, 1);
296 break;
297
298 case 'c':
299 puts("");
300 puts("Connect source port to destination port");
301 if (!input("Enter source port number", tmp1, sizeof(tmp1)) )
302 continue;
303 src = strtol(tmp1, &err, 10);
304 if (*err || src < 0 || src >= port_count) {
305 puts("Invalid slot number");
306 continue;
307 }
308
309 if (!input("Enter destination port number", tmp2, sizeof(tmp2)) )
310 continue;
311 dst = strtol(tmp2, &err, 10);
312 if (*err || dst < 0 || dst >= port_count) {
313 puts("Invalid slot number");
314 continue;
315 }
316
317 status = pjmedia_conf_connect_port(conf, src, dst, 0);
318 if (status != PJ_SUCCESS)
319 app_perror(THIS_FILE, "Error connecting port", status);
320
321 break;
322
323 case 'd':
324 puts("");
325 puts("Disconnect port connection");
326 if (!input("Enter source port number", tmp1, sizeof(tmp1)) )
327 continue;
328 src = strtol(tmp1, &err, 10);
329 if (*err || src < 0 || src >= port_count) {
330 puts("Invalid slot number");
331 continue;
332 }
333
334 if (!input("Enter destination port number", tmp2, sizeof(tmp2)) )
335 continue;
336 dst = strtol(tmp2, &err, 10);
337 if (*err || dst < 0 || dst >= port_count) {
338 puts("Invalid slot number");
339 continue;
340 }
341
342 status = pjmedia_conf_disconnect_port(conf, src, dst);
343 if (status != PJ_SUCCESS)
344 app_perror(THIS_FILE, "Error connecting port", status);
345
346
347 break;
348
349 case 't':
350 puts("");
351 puts("Adjust transmit level of a port");
352 if (!input("Enter port number", tmp1, sizeof(tmp1)) )
353 continue;
354 src = strtol(tmp1, &err, 10);
355 if (*err || src < 0 || src >= port_count) {
356 puts("Invalid slot number");
357 continue;
358 }
359
360 if (!input("Enter level (-128 to >127, 0 for normal)",
361 tmp2, sizeof(tmp2)) )
362 continue;
363 level = strtol(tmp2, &err, 10);
364 if (*err || level < -128) {
365 puts("Invalid level");
366 continue;
367 }
368
369 status = pjmedia_conf_adjust_tx_level( conf, src, level);
370 if (status != PJ_SUCCESS)
371 app_perror(THIS_FILE, "Error adjusting level", status);
372 break;
373
374
375 case 'r':
376 puts("");
377 puts("Adjust receive level of a port");
378 if (!input("Enter port number", tmp1, sizeof(tmp1)) )
379 continue;
380 src = strtol(tmp1, &err, 10);
381 if (*err || src < 0 || src >= port_count) {
382 puts("Invalid slot number");
383 continue;
384 }
385
386 if (!input("Enter level (-128 to >127, 0 for normal)",
387 tmp2, sizeof(tmp2)) )
388 continue;
389 level = strtol(tmp2, &err, 10);
390 if (*err || level < -128) {
391 puts("Invalid level");
392 continue;
393 }
394
395 status = pjmedia_conf_adjust_rx_level( conf, src, level);
396 if (status != PJ_SUCCESS)
397 app_perror(THIS_FILE, "Error adjusting level", status);
398 break;
399
400 case 'v':
401 puts("");
402 puts("Display VU meter");
403 if (!input("Enter port number to monitor", tmp1, sizeof(tmp1)) )
404 continue;
405 src = strtol(tmp1, &err, 10);
406 if (*err || src < 0 || src >= port_count) {
407 puts("Invalid slot number");
408 continue;
409 }
410
411 if (!input("Enter r for rx level or t for tx level", tmp2, sizeof(tmp2)))
412 continue;
413 if (tmp2[0] != 'r' && tmp2[0] != 't') {
414 puts("Invalid option");
415 continue;
416 }
417
418 if (!input("Duration to monitor (in seconds)", tmp1, sizeof(tmp1)) )
419 continue;
420 dur = strtol(tmp1, &err, 10);
421 if (*err) {
422 puts("Invalid duration number");
423 continue;
424 }
425
426 monitor_level(conf, src, tmp2[0], dur);
427 break;
428
429 case 'q':
430 goto on_quit;
431
432 default:
433 printf("Invalid input character '%c'\n", tmp[0]);
434 break;
435 }
436 }
437
438 on_quit:
439
440 /* Start deinitialization: */
441
442 /* Destroy conference bridge */
443 status = pjmedia_conf_destroy( conf );
444 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
445
446
447 /* Destroy file ports */
448 for (i=0; i<file_count; ++i) {
449 status = pjmedia_port_destroy( file_port[i]);
450 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
451 }
452
453 /* Destroy recorder port */
454 if (rec_port)
455 pjmedia_port_destroy(rec_port);
456
457 /* Release application pool */
458 pj_pool_release( pool );
459
460 /* Destroy media endpoint. */
461 pjmedia_endpt_destroy( med_endpt );
462
463 /* Destroy pool factory */
464 pj_caching_pool_destroy( &cp );
465
466 /* Shutdown PJLIB */
467 pj_shutdown();
468
469 /* Done. */
470 return 0;
471 }
472
473
474 /*
475 * List the ports in conference bridge
476 */
477 static void conf_list(pjmedia_conf *conf, int detail)
478 {
479 enum { MAX_PORTS = 32 };
480 unsigned i, count;
481 pjmedia_conf_port_info info[MAX_PORTS];
482
483 printf("Conference ports:\n");
484
485 count = PJ_ARRAY_SIZE(info);
486 pjmedia_conf_get_ports_info(conf, &count, info);
487
488 for (i=0; i<count; ++i) {
489 char txlist[4*MAX_PORTS];
490 unsigned j;
491 pjmedia_conf_port_info *port_info = &info[i];
492
493 txlist[0] = '\0';
494 for (j=0; j<port_info->listener_cnt; ++j) {
495 char s[10];
496 pj_ansi_snprintf(s, sizeof(s), "#%d ",
497 port_info->listener_slots[j]);
498 pj_ansi_strxcat(txlist, s, sizeof(txlist));
499
500 }
501
502 if (txlist[0] == '\0') {
503 txlist[0] = '-';
504 txlist[1] = '\0';
505 }
506
507 if (!detail) {
508 printf("Port #%02d %-25.*s transmitting to: %s\n",
509 port_info->slot,
510 (int)port_info->name.slen,
511 port_info->name.ptr,
512 txlist);
513 } else {
514 unsigned tx_level, rx_level;
515
516 pjmedia_conf_get_signal_level(conf, port_info->slot,
517 &tx_level, &rx_level);
518
519 printf("Port #%02d:\n"
520 " Name : %.*s\n"
521 " Sampling rate : %d Hz\n"
522 " Samples per frame : %d\n"
523 " Frame time : %d ms\n"
524 " Signal level adjustment : tx=%d, rx=%d\n"
525 " Current signal level : tx=%u, rx=%u\n"
526 " Transmitting to ports : %s\n\n",
527 port_info->slot,
528 (int)port_info->name.slen,
529 port_info->name.ptr,
530 port_info->clock_rate,
531 port_info->samples_per_frame,
532 port_info->samples_per_frame*1000/port_info->clock_rate,
533 port_info->tx_adj_level,
534 port_info->rx_adj_level,
535 tx_level,
536 rx_level,
537 txlist);
538 }
539
540 }
541 puts("");
542 }
543
544
545 /*
546 * Display VU meter
547 */
548 static void monitor_level(pjmedia_conf *conf, int slot, int dir, int dur)
549 {
550 enum { SLEEP = 20, SAMP_CNT = 2};
551 pj_status_t status;
552 int i, total_count;
553 unsigned level, samp_cnt;
554
555
556 puts("");
557 printf("Displaying VU meter for port %d for about %d seconds\n",
558 slot, dur);
559
560 total_count = dur * 1000 / SLEEP;
561
562 level = 0;
563 samp_cnt = 0;
564
565 for (i=0; i<total_count; ++i) {
566 unsigned tx_level, rx_level;
567 int j, length;
568 char meter[21];
569
570 /* Poll the volume every 20 msec */
571 status = pjmedia_conf_get_signal_level(conf, slot,
572 &tx_level, &rx_level);
573 if (status != PJ_SUCCESS) {
574 app_perror(THIS_FILE, "Unable to read level", status);
575 return;
576 }
577
578 level += (dir=='r' ? rx_level : tx_level);
579 ++samp_cnt;
580
581 /* Accumulate until we have enough samples */
582 if (samp_cnt < SAMP_CNT) {
583 pj_thread_sleep(SLEEP);
584 continue;
585 }
586
587 /* Get average */
588 level = level / samp_cnt;
589
590 /* Draw bar */
591 length = 20 * level / 255;
592 for (j=0; j<length; ++j)
593 meter[j] = '#';
594 for (; j<20; ++j)
595 meter[j] = ' ';
596 meter[20] = '\0';
597
598 printf("Port #%02d %cx level: [%s] %d \r",
599 slot, dir, meter, level);
600
601 /* Next.. */
602 samp_cnt = 0;
603 level = 0;
604
605 pj_thread_sleep(SLEEP);
606 }
607
608 puts("");
609 }
610