| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <pjmedia.h> |
| 21 |
#include <pjlib-util.h> |
| 22 |
#include <pjlib.h> |
| 23 |
|
| 24 |
#include <stdlib.h> |
| 25 |
#include <stdio.h> |
| 26 |
|
| 27 |
#include "util.h" |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#define THIS_FILE "confsample.c" |
| 43 |
|
| 44 |
|
| 45 |
|
| 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 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
static void conf_list(pjmedia_conf *conf, pj_bool_t detail); |
| 87 |
|
| 88 |
|
| 89 |
static void monitor_level(pjmedia_conf *conf, int slot, int dir, int dur); |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
static void usage(void) |
| 94 |
{ |
| 95 |
puts(""); |
| 96 |
puts(desc); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 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 |
|
| 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 |
|
| 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; |
| 141 |
pjmedia_port *rec_port = NULL; |
| 142 |
|
| 143 |
char tmp[10]; |
| 144 |
pj_status_t status; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
status = pj_init(); |
| 149 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 150 |
|
| 151 |
|
| 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 |
|
| 160 |
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); |
| 167 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 168 |
|
| 169 |
|
| 170 |
pool = pj_pool_create( &cp.factory, |
| 171 |
"wav", |
| 172 |
4000, |
| 173 |
4000, |
| 174 |
NULL |
| 175 |
); |
| 176 |
|
| 177 |
|
| 178 |
file_count = argc - pj_optind; |
| 179 |
port_count = file_count + 1 + RECORDER; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
status = pjmedia_conf_create( pool, |
| 186 |
port_count, |
| 187 |
clock_rate, |
| 188 |
channel_count, |
| 189 |
samples_per_frame, |
| 190 |
bits_per_sample, |
| 191 |
0, |
| 192 |
&conf |
| 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 |
|
| 215 |
file_port = pj_pool_alloc(pool, file_count * sizeof(pjmedia_port*)); |
| 216 |
|
| 217 |
for (i=0; i<file_count; ++i) { |
| 218 |
|
| 219 |
|
| 220 |
status = pjmedia_wav_player_port_create( |
| 221 |
pool, |
| 222 |
argv[i+pj_optind], |
| 223 |
0, |
| 224 |
0, |
| 225 |
0, |
| 226 |
&file_port[i] |
| 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 |
|
| 238 |
status = pjmedia_conf_add_port( conf, |
| 239 |
pool, |
| 240 |
file_port[i], |
| 241 |
NULL, |
| 242 |
NULL |
| 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 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
dump_pool_usage(THIS_FILE, &cp); |
| 260 |
|
| 261 |
|
| 262 |
pj_thread_sleep(100); |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 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 |
|
| 441 |
|
| 442 |
|
| 443 |
status = pjmedia_conf_destroy( conf ); |
| 444 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 445 |
|
| 446 |
|
| 447 |
|
| 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 |
|
| 454 |
if (rec_port) |
| 455 |
pjmedia_port_destroy(rec_port); |
| 456 |
|
| 457 |
|
| 458 |
pj_pool_release( pool ); |
| 459 |
|
| 460 |
|
| 461 |
pjmedia_endpt_destroy( med_endpt ); |
| 462 |
|
| 463 |
|
| 464 |
pj_caching_pool_destroy( &cp ); |
| 465 |
|
| 466 |
|
| 467 |
pj_shutdown(); |
| 468 |
|
| 469 |
|
| 470 |
return 0; |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 582 |
if (samp_cnt < SAMP_CNT) { |
| 583 |
pj_thread_sleep(SLEEP); |
| 584 |
continue; |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
level = level / samp_cnt; |
| 589 |
|
| 590 |
|
| 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 |
|
| 602 |
samp_cnt = 0; |
| 603 |
level = 0; |
| 604 |
|
| 605 |
pj_thread_sleep(SLEEP); |
| 606 |
} |
| 607 |
|
| 608 |
puts(""); |
| 609 |
} |
| 610 |
|