1
/* $Id: confsample.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
22
#include <pjlib-util.h> /* pj_getopt */
25
#include <stdlib.h> /* atoi() */
31
* \page page_pjmedia_samples_confsample_c Samples: Using Conference Bridge
33
* Sample to mix multiple files in the conference bridge and play the
34
* result to sound device.
36
* This file is pjsip-apps/src/samples/confsample.c
38
* \includelineno confsample.c
42
/* For logging purpose. */
43
#define THIS_FILE "confsample.c"
46
/* Shall we put recorder in the conference */
50
static const char *desc =
57
" Demonstrate how to use conference bridge. \n"
61
" confsample [options] [file1.wav] [file2.wav] ... \n"
66
" fileN.wav are optional WAV files to be connected to the conference \n"
67
" bridge. The WAV files MUST have single channel (mono) and 16 bit PCM \n"
68
" samples. It can have arbitrary sampling rate. \n"
72
" Here we create a conference bridge, with at least one port (port zero \n"
73
" is always created for the sound device). \n"
75
" If WAV files are specified, the WAV file player ports will be connected \n"
76
" to slot starting from number one in the bridge. The WAV files can have \n"
77
" arbitrary sampling rate; the bridge will convert it to its clock rate. \n"
78
" However, the files MUST have a single audio channel only (i.e. mono). \n";
86
/* List the ports in the conference bridge */
87
static void conf_list(pjmedia_conf *conf, pj_bool_t detail);
89
/* Display VU meter */
90
static void monitor_level(pjmedia_conf *conf, int slot, int dir, int dur);
94
static void usage(void)
102
/* Input simple string */
103
static pj_bool_t input(const char *title, char *buf, pj_size_t len)
107
printf("%s (empty to cancel): ", title); fflush(stdout);
108
if (fgets(buf, len, stdin) == NULL)
111
/* Remove trailing newlines. */
113
if (*p=='\r' || *p=='\n') *p='\0';
124
/*****************************************************************************
127
int main(int argc, char *argv[])
130
int clock_rate = CLOCK_RATE;
131
int channel_count = NCHANNELS;
132
int samples_per_frame = NSAMPLES;
133
int bits_per_sample = NBITS;
136
pjmedia_endpt *med_endpt;
140
int i, port_count, file_count;
141
pjmedia_port **file_port; /* Array of file ports */
142
pjmedia_port *rec_port = NULL; /* Wav writer port */
148
/* Must init PJLIB first: */
150
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
152
/* Get command line options. */
153
if (get_snd_options(THIS_FILE, argc, argv, &dev_id, &clock_rate,
154
&channel_count, &samples_per_frame, &bits_per_sample))
160
/* Must create a pool factory before we can allocate any memory. */
161
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
164
* Initialize media endpoint.
165
* This will implicitly initialize PJMEDIA too.
167
status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
168
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
170
/* Create memory pool to allocate memory */
171
pool = pj_pool_create( &cp.factory, /* pool factory */
172
"wav", /* pool name. */
173
4000, /* init size */
174
4000, /* increment size */
175
NULL /* callback on error */
179
file_count = argc - pj_optind;
180
port_count = file_count + 1 + RECORDER;
182
/* Create the conference bridge.
183
* With default options (zero), the bridge will create an instance of
184
* sound capture and playback device and connect them to slot zero.
186
status = pjmedia_conf_create( pool, /* pool to use */
187
port_count,/* number of ports */
195
if (status != PJ_SUCCESS) {
196
app_perror(THIS_FILE, "Unable to create conference bridge", status);
201
status = pjmedia_wav_writer_port_create( pool, "confrecord.wav",
202
clock_rate, channel_count,
204
bits_per_sample, 0, 0,
206
if (status != PJ_SUCCESS) {
207
app_perror(THIS_FILE, "Unable to create WAV writer", status);
211
pjmedia_conf_add_port(conf, pool, rec_port, NULL, NULL);
215
/* Create file ports. */
216
file_port = pj_pool_alloc(pool, file_count * sizeof(pjmedia_port*));
218
for (i=0; i<file_count; ++i) {
220
/* Load the WAV file to file port. */
221
status = pjmedia_wav_player_port_create(
223
argv[i+pj_optind], /* filename */
224
0, /* use default ptime */
227
&file_port[i] /* result */
229
if (status != PJ_SUCCESS) {
231
pj_ansi_sprintf(title, "Unable to use %s", argv[i+pj_optind]);
232
app_perror(THIS_FILE, title, status);
237
/* Add the file port to conference bridge */
238
status = pjmedia_conf_add_port( conf, /* The bridge */
240
file_port[i], /* port to connect */
241
NULL, /* Use port's name */
242
NULL /* ptr for slot # */
244
if (status != PJ_SUCCESS) {
245
app_perror(THIS_FILE, "Unable to add conference port", status);
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.
258
/* Dump memory usage */
259
dump_pool_usage(THIS_FILE, &cp);
261
/* Sleep to allow log messages to flush */
262
pj_thread_sleep(100);
272
int src, dst, level, dur;
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");
287
printf("Enter selection: "); fflush(stdout);
289
if (fgets(tmp, sizeof(tmp), stdin) == NULL)
300
puts("Connect source port to destination port");
301
if (!input("Enter source port number", tmp1, sizeof(tmp1)) )
303
src = strtol(tmp1, &err, 10);
304
if (*err || src < 0 || src >= port_count) {
305
puts("Invalid slot number");
309
if (!input("Enter destination port number", tmp2, sizeof(tmp2)) )
311
dst = strtol(tmp2, &err, 10);
312
if (*err || dst < 0 || dst >= port_count) {
313
puts("Invalid slot number");
317
status = pjmedia_conf_connect_port(conf, src, dst, 0);
318
if (status != PJ_SUCCESS)
319
app_perror(THIS_FILE, "Error connecting port", status);
325
puts("Disconnect port connection");
326
if (!input("Enter source port number", tmp1, sizeof(tmp1)) )
328
src = strtol(tmp1, &err, 10);
329
if (*err || src < 0 || src >= port_count) {
330
puts("Invalid slot number");
334
if (!input("Enter destination port number", tmp2, sizeof(tmp2)) )
336
dst = strtol(tmp2, &err, 10);
337
if (*err || dst < 0 || dst >= port_count) {
338
puts("Invalid slot number");
342
status = pjmedia_conf_disconnect_port(conf, src, dst);
343
if (status != PJ_SUCCESS)
344
app_perror(THIS_FILE, "Error connecting port", status);
351
puts("Adjust transmit level of a port");
352
if (!input("Enter port number", tmp1, sizeof(tmp1)) )
354
src = strtol(tmp1, &err, 10);
355
if (*err || src < 0 || src >= port_count) {
356
puts("Invalid slot number");
360
if (!input("Enter level (-128 to >127, 0 for normal)",
361
tmp2, sizeof(tmp2)) )
363
level = strtol(tmp2, &err, 10);
364
if (*err || level < -128) {
365
puts("Invalid level");
369
status = pjmedia_conf_adjust_tx_level( conf, src, level);
370
if (status != PJ_SUCCESS)
371
app_perror(THIS_FILE, "Error adjusting level", status);
377
puts("Adjust receive level of a port");
378
if (!input("Enter port number", tmp1, sizeof(tmp1)) )
380
src = strtol(tmp1, &err, 10);
381
if (*err || src < 0 || src >= port_count) {
382
puts("Invalid slot number");
386
if (!input("Enter level (-128 to >127, 0 for normal)",
387
tmp2, sizeof(tmp2)) )
389
level = strtol(tmp2, &err, 10);
390
if (*err || level < -128) {
391
puts("Invalid level");
395
status = pjmedia_conf_adjust_rx_level( conf, src, level);
396
if (status != PJ_SUCCESS)
397
app_perror(THIS_FILE, "Error adjusting level", status);
402
puts("Display VU meter");
403
if (!input("Enter port number to monitor", tmp1, sizeof(tmp1)) )
405
src = strtol(tmp1, &err, 10);
406
if (*err || src < 0 || src >= port_count) {
407
puts("Invalid slot number");
411
if (!input("Enter r for rx level or t for tx level", tmp2, sizeof(tmp2)))
413
if (tmp2[0] != 'r' && tmp2[0] != 't') {
414
puts("Invalid option");
418
if (!input("Duration to monitor (in seconds)", tmp1, sizeof(tmp1)) )
420
dur = strtol(tmp1, &err, 10);
422
puts("Invalid duration number");
426
monitor_level(conf, src, tmp2[0], dur);
433
printf("Invalid input character '%c'\n", tmp[0]);
440
/* Start deinitialization: */
442
/* Destroy conference bridge */
443
status = pjmedia_conf_destroy( conf );
444
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
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);
453
/* Destroy recorder port */
455
pjmedia_port_destroy(rec_port);
457
/* Release application pool */
458
pj_pool_release( pool );
460
/* Destroy media endpoint. */
461
pjmedia_endpt_destroy( med_endpt );
463
/* Destroy pool factory */
464
pj_caching_pool_destroy( &cp );
475
* List the ports in conference bridge
477
static void conf_list(pjmedia_conf *conf, int detail)
479
enum { MAX_PORTS = 32 };
481
pjmedia_conf_port_info info[MAX_PORTS];
483
printf("Conference ports:\n");
485
count = PJ_ARRAY_SIZE(info);
486
pjmedia_conf_get_ports_info(conf, &count, info);
488
for (i=0; i<count; ++i) {
489
char txlist[4*MAX_PORTS];
491
pjmedia_conf_port_info *port_info = &info[i];
494
for (j=0; j<port_info->listener_cnt; ++j) {
496
pj_ansi_sprintf(s, "#%d ", port_info->listener_slots[j]);
497
pj_ansi_strcat(txlist, s);
501
if (txlist[0] == '\0') {
507
printf("Port #%02d %-25.*s transmitting to: %s\n",
509
(int)port_info->name.slen,
513
unsigned tx_level, rx_level;
515
pjmedia_conf_get_signal_level(conf, port_info->slot,
516
&tx_level, &rx_level);
518
printf("Port #%02d:\n"
520
" Sampling rate : %d Hz\n"
521
" Samples per frame : %d\n"
522
" Frame time : %d ms\n"
523
" Signal level adjustment : tx=%d, rx=%d\n"
524
" Current signal level : tx=%u, rx=%u\n"
525
" Transmitting to ports : %s\n\n",
527
(int)port_info->name.slen,
529
port_info->clock_rate,
530
port_info->samples_per_frame,
531
port_info->samples_per_frame*1000/port_info->clock_rate,
532
port_info->tx_adj_level,
533
port_info->rx_adj_level,
547
static void monitor_level(pjmedia_conf *conf, int slot, int dir, int dur)
549
enum { SLEEP = 20, SAMP_CNT = 2};
552
unsigned level, samp_cnt;
556
printf("Displaying VU meter for port %d for about %d seconds\n",
559
total_count = dur * 1000 / SLEEP;
564
for (i=0; i<total_count; ++i) {
565
unsigned tx_level, rx_level;
569
/* Poll the volume every 20 msec */
570
status = pjmedia_conf_get_signal_level(conf, slot,
571
&tx_level, &rx_level);
572
if (status != PJ_SUCCESS) {
573
app_perror(THIS_FILE, "Unable to read level", status);
577
level += (dir=='r' ? rx_level : tx_level);
580
/* Accumulate until we have enough samples */
581
if (samp_cnt < SAMP_CNT) {
582
pj_thread_sleep(SLEEP);
587
level = level / samp_cnt;
590
length = 20 * level / 255;
591
for (j=0; j<length; ++j)
597
printf("Port #%02d %cx level: [%s] %d \r",
598
slot, dir, meter, level);
604
pj_thread_sleep(SLEEP);