1
/* $Id: conf_switch.c 4443 2013-03-20 06:56: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
20
#include <pjmedia/conference.h>
21
#include <pjmedia/alaw_ulaw.h>
22
#include <pjmedia/errno.h>
23
#include <pjmedia/port.h>
24
#include <pjmedia/silencedet.h>
25
#include <pjmedia/sound_port.h>
27
#include <pj/assert.h>
31
#include <pj/string.h>
33
#if defined(PJMEDIA_CONF_USE_SWITCH_BOARD) && PJMEDIA_CONF_USE_SWITCH_BOARD!=0
35
/* CONF_DEBUG enables detailed operation of the conference bridge.
36
* Beware that it prints large amounts of logs (several lines per frame).
41
# define TRACE_(x) PJ_LOG(5,x)
46
#define THIS_FILE "conf_switch.c"
48
#define SIGNATURE PJMEDIA_CONF_SWITCH_SIGNATURE
49
#define SIGNATURE_PORT PJMEDIA_PORT_SIGNATURE('S', 'W', 'T', 'P')
50
#define NORMAL_LEVEL 128
51
#define SLOT_TYPE unsigned
52
#define INVALID_SLOT ((SLOT_TYPE)-1)
53
#define BUFFER_SIZE PJMEDIA_CONF_SWITCH_BOARD_BUF_SIZE
54
#define MAX_LEVEL (32767)
55
#define MIN_LEVEL (-32768)
58
* DON'T GET CONFUSED WITH TX/RX!!
60
* TX and RX directions are always viewed from the conference bridge's point
61
* of view, and NOT from the port's point of view. So TX means the bridge
62
* is transmitting to the port, RX means the bridge is receiving from the
68
* This is a port connected to conference bridge.
72
SLOT_TYPE slot; /**< Array of listeners. */
73
pj_str_t name; /**< Port name. */
74
pjmedia_port *port; /**< get_frame() and put_frame() */
75
pjmedia_port_op rx_setting; /**< Can we receive from this port */
76
pjmedia_port_op tx_setting; /**< Can we transmit to this port */
77
unsigned listener_cnt; /**< Number of listeners. */
78
SLOT_TYPE *listener_slots;/**< Array of listeners. */
79
unsigned transmitter_cnt;/**<Number of transmitters. */
81
/* Shortcut for port info. */
82
pjmedia_port_info *info;
83
unsigned samples_per_frame;
85
/* Calculated signal levels: */
86
unsigned tx_level; /**< Last tx level to this port. */
87
unsigned rx_level; /**< Last rx level from this port. */
89
/* The normalized signal level adjustment.
90
* A value of 128 (NORMAL_LEVEL) means there's no adjustment.
92
unsigned tx_adj_level; /**< Adjustment for TX. */
93
unsigned rx_adj_level; /**< Adjustment for RX. */
95
pj_timestamp ts_clock;
99
/* Tx buffer is a temporary buffer to be used when there's mismatch
100
* between port's ptime with conference's ptime. This buffer is used as
101
* the source to buffer the samples until there are enough samples to
102
* fulfill a complete frame to be transmitted to the port.
104
pj_uint8_t tx_buf[BUFFER_SIZE]; /**< Tx buffer. */
113
unsigned options; /**< Bitmask options. */
114
unsigned max_ports; /**< Maximum ports. */
115
unsigned port_cnt; /**< Current number of ports. */
116
unsigned connect_cnt; /**< Total number of connections */
117
pjmedia_port *master_port; /**< Port zero's port. */
118
char master_name_buf[80]; /**< Port0 name buffer. */
119
pj_mutex_t *mutex; /**< Conference mutex. */
120
struct conf_port **ports; /**< Array of ports. */
121
pj_uint8_t buf[BUFFER_SIZE]; /**< Common buffer. */
126
static pj_status_t put_frame(pjmedia_port *this_port,
127
pjmedia_frame *frame);
128
static pj_status_t get_frame(pjmedia_port *this_port,
129
pjmedia_frame *frame);
130
static pj_status_t destroy_port(pjmedia_port *this_port);
136
static pj_status_t create_conf_port( pj_pool_t *pool,
139
const pj_str_t *name,
140
struct conf_port **p_conf_port)
142
struct conf_port *conf_port;
145
PJ_ASSERT_RETURN(pool && conf && port && name && p_conf_port, PJ_EINVAL);
147
/* Check port's buffer size */
148
if (port->info.fmt.id == PJMEDIA_FORMAT_PCM &&
149
PJMEDIA_PIA_SPF(&port->info)*2 > BUFFER_SIZE - sizeof(pjmedia_frame))
151
pj_assert(!"Too small buffer size for audio switchboard. "
152
"Try increase PJMEDIA_CONF_SWITCH_BOARD_BUF_SIZE");
157
conf_port = PJ_POOL_ZALLOC_T(pool, struct conf_port);
160
pj_strdup_with_null(pool, &conf_port->name, name);
162
/* Default has tx and rx enabled. */
163
conf_port->rx_setting = PJMEDIA_PORT_ENABLE;
164
conf_port->tx_setting = PJMEDIA_PORT_ENABLE;
166
/* Default level adjustment is 128 (which means no adjustment) */
167
conf_port->tx_adj_level = NORMAL_LEVEL;
168
conf_port->rx_adj_level = NORMAL_LEVEL;
170
/* Create transmit flag array */
171
conf_port->listener_slots = (SLOT_TYPE*)
173
conf->max_ports * sizeof(SLOT_TYPE));
174
PJ_ASSERT_RETURN(conf_port->listener_slots, PJ_ENOMEM);
176
/* Save some port's infos, for convenience. */
177
conf_port->port = port;
178
conf_port->info = &port->info;
179
conf_port->samples_per_frame = PJMEDIA_PIA_SPF(&port->info);
181
/* Init pjmedia_frame structure in the TX buffer. */
182
f = (pjmedia_frame*)conf_port->tx_buf;
183
f->buf = conf_port->tx_buf + sizeof(pjmedia_frame);
186
*p_conf_port = conf_port;
191
* Create port zero for the sound device.
193
static pj_status_t create_sound_port( pj_pool_t *pool,
196
struct conf_port *conf_port;
197
pj_str_t name = { "Master/sound", 12 };
200
status = create_conf_port(pool, conf, conf->master_port, &name, &conf_port);
201
if (status != PJ_SUCCESS)
204
/* Add the port to the bridge */
206
conf->ports[0] = conf_port;
209
PJ_LOG(5,(THIS_FILE, "Sound device successfully created for port 0"));
214
* Create conference bridge.
216
PJ_DEF(pj_status_t) pjmedia_conf_create( pj_pool_t *pool,
219
unsigned channel_count,
220
unsigned samples_per_frame,
221
unsigned bits_per_sample,
223
pjmedia_conf **p_conf )
226
const pj_str_t name = { "Conf", 4 };
229
/* Can only accept 16bits per sample, for now.. */
230
PJ_ASSERT_RETURN(bits_per_sample == 16, PJ_EINVAL);
232
PJ_LOG(5,(THIS_FILE, "Creating conference bridge with %d ports",
235
/* Create and init conf structure. */
236
conf = PJ_POOL_ZALLOC_T(pool, pjmedia_conf);
237
PJ_ASSERT_RETURN(conf, PJ_ENOMEM);
239
conf->ports = (struct conf_port**)
240
pj_pool_zalloc(pool, max_ports*sizeof(void*));
241
PJ_ASSERT_RETURN(conf->ports, PJ_ENOMEM);
243
conf->options = options;
244
conf->max_ports = max_ports;
246
/* Create and initialize the master port interface. */
247
conf->master_port = PJ_POOL_ZALLOC_T(pool, pjmedia_port);
248
PJ_ASSERT_RETURN(conf->master_port, PJ_ENOMEM);
250
pjmedia_port_info_init(&conf->master_port->info, &name, SIGNATURE,
251
clock_rate, channel_count, bits_per_sample,
254
conf->master_port->port_data.pdata = conf;
255
conf->master_port->port_data.ldata = 0;
257
conf->master_port->get_frame = &get_frame;
258
conf->master_port->put_frame = &put_frame;
259
conf->master_port->on_destroy = &destroy_port;
262
/* Create port zero for sound device. */
263
status = create_sound_port(pool, conf);
264
if (status != PJ_SUCCESS)
268
status = pj_mutex_create_recursive(pool, "conf", &conf->mutex);
269
if (status != PJ_SUCCESS)
281
* Pause sound device.
283
static pj_status_t pause_sound( pjmedia_conf *conf )
291
* Resume sound device.
293
static pj_status_t resume_sound( pjmedia_conf *conf )
302
* Destroy conference bridge.
304
PJ_DEF(pj_status_t) pjmedia_conf_destroy( pjmedia_conf *conf )
306
PJ_ASSERT_RETURN(conf != NULL, PJ_EINVAL);
309
pj_mutex_destroy(conf->mutex);
316
* Destroy the master port (will destroy the conference)
318
static pj_status_t destroy_port(pjmedia_port *this_port)
320
pjmedia_conf *conf = (pjmedia_conf*) this_port->port_data.pdata;
321
return pjmedia_conf_destroy(conf);
325
* Get port zero interface.
327
PJ_DEF(pjmedia_port*) pjmedia_conf_get_master_port(pjmedia_conf *conf)
330
PJ_ASSERT_RETURN(conf != NULL, NULL);
332
/* Can only return port interface when PJMEDIA_CONF_NO_DEVICE was
333
* present in the option.
335
PJ_ASSERT_RETURN((conf->options & PJMEDIA_CONF_NO_DEVICE) != 0, NULL);
337
return conf->master_port;
342
* Set master port name.
344
PJ_DEF(pj_status_t) pjmedia_conf_set_port0_name(pjmedia_conf *conf,
345
const pj_str_t *name)
350
PJ_ASSERT_RETURN(conf != NULL && name != NULL, PJ_EINVAL);
353
if (len > sizeof(conf->master_name_buf))
354
len = sizeof(conf->master_name_buf);
356
if (len > 0) pj_memcpy(conf->master_name_buf, name->ptr, len);
358
conf->ports[0]->name.ptr = conf->master_name_buf;
359
conf->ports[0]->name.slen = len;
361
conf->master_port->info.name = conf->ports[0]->name;
367
* Add stream port to the conference bridge.
369
PJ_DEF(pj_status_t) pjmedia_conf_add_port( pjmedia_conf *conf,
371
pjmedia_port *strm_port,
372
const pj_str_t *port_name,
375
struct conf_port *conf_port;
379
PJ_ASSERT_RETURN(conf && pool && strm_port, PJ_EINVAL);
381
PJ_ASSERT_RETURN(conf->clock_rate == strm_port->info.clock_rate,
382
PJMEDIA_ENCCLOCKRATE);
383
PJ_ASSERT_RETURN(conf->channel_count == strm_port->info.channel_count,
385
PJ_ASSERT_RETURN(conf->bits_per_sample == strm_port->info.bits_per_sample,
389
/* Port's samples per frame should be equal to or multiplication of
390
* conference's samples per frame.
393
Not sure if this is needed!
394
PJ_ASSERT_RETURN((conf->samples_per_frame %
395
strm_port->info.samples_per_frame==0) ||
396
(strm_port->info.samples_per_frame %
397
conf->samples_per_frame==0),
398
PJMEDIA_ENCSAMPLESPFRAME);
401
/* If port_name is not specified, use the port's name */
403
port_name = &strm_port->info.name;
405
pj_mutex_lock(conf->mutex);
407
if (conf->port_cnt >= conf->max_ports) {
408
pj_assert(!"Too many ports");
409
pj_mutex_unlock(conf->mutex);
413
/* Find empty port in the conference bridge. */
414
for (index=0; index < conf->max_ports; ++index) {
415
if (conf->ports[index] == NULL)
419
pj_assert(index != conf->max_ports);
421
/* Create conf port structure. */
422
status = create_conf_port(pool, conf, strm_port, port_name, &conf_port);
423
if (status != PJ_SUCCESS) {
424
pj_mutex_unlock(conf->mutex);
429
conf_port->slot = index;
430
conf->ports[index] = conf_port;
438
pj_mutex_unlock(conf->mutex);
447
PJ_DEF(pj_status_t) pjmedia_conf_add_passive_port( pjmedia_conf *conf,
449
const pj_str_t *name,
451
unsigned channel_count,
452
unsigned samples_per_frame,
453
unsigned bits_per_sample,
456
pjmedia_port **p_port )
461
PJ_UNUSED_ARG(clock_rate);
462
PJ_UNUSED_ARG(channel_count);
463
PJ_UNUSED_ARG(samples_per_frame);
464
PJ_UNUSED_ARG(bits_per_sample);
465
PJ_UNUSED_ARG(options);
466
PJ_UNUSED_ARG(p_slot);
467
PJ_UNUSED_ARG(p_port);
475
* Change TX and RX settings for the port.
477
PJ_DEF(pj_status_t) pjmedia_conf_configure_port( pjmedia_conf *conf,
482
struct conf_port *conf_port;
484
/* Check arguments */
485
PJ_ASSERT_RETURN(conf && slot<conf->max_ports, PJ_EINVAL);
487
pj_mutex_lock(conf->mutex);
489
/* Port must be valid. */
490
conf_port = conf->ports[slot];
491
if (conf_port == NULL) {
492
pj_mutex_unlock(conf->mutex);
496
if (tx != PJMEDIA_PORT_NO_CHANGE)
497
conf_port->tx_setting = tx;
499
if (rx != PJMEDIA_PORT_NO_CHANGE)
500
conf_port->rx_setting = rx;
502
pj_mutex_unlock(conf->mutex);
511
PJ_DEF(pj_status_t) pjmedia_conf_connect_port( pjmedia_conf *conf,
516
struct conf_port *src_port, *dst_port;
517
pj_bool_t start_sound = PJ_FALSE;
518
pjmedia_audio_format_detail *src_afd, *dst_afd;
521
/* Check arguments */
522
PJ_ASSERT_RETURN(conf && src_slot<conf->max_ports &&
523
sink_slot<conf->max_ports, PJ_EINVAL);
525
/* For now, level MUST be zero. */
526
PJ_ASSERT_RETURN(level == 0, PJ_EINVAL);
528
pj_mutex_lock(conf->mutex);
530
/* Ports must be valid. */
531
src_port = conf->ports[src_slot];
532
dst_port = conf->ports[sink_slot];
533
if (!src_port || !dst_port) {
534
pj_mutex_unlock(conf->mutex);
538
src_afd = pjmedia_format_get_audio_format_detail(&src_port->info->fmt, 1);
539
dst_afd = pjmedia_format_get_audio_format_detail(&dst_port->info->fmt, 1);
541
/* Format must match. */
542
if (src_port->info->fmt.id != dst_port->info->fmt.id ||
543
src_afd->avg_bps != dst_afd->avg_bps)
545
pj_mutex_unlock(conf->mutex);
546
return PJMEDIA_ENOTCOMPATIBLE;
549
/* Clock rate must match. */
550
if (src_afd->clock_rate != dst_afd->clock_rate) {
551
pj_mutex_unlock(conf->mutex);
552
return PJMEDIA_ENCCLOCKRATE;
555
/* Channel count must match. */
556
if (src_afd->channel_count != dst_afd->channel_count) {
557
pj_mutex_unlock(conf->mutex);
558
return PJMEDIA_ENCCHANNEL;
561
/* Source and sink ptime must be equal or a multiplication factor. */
562
if ((src_afd->frame_time_usec % dst_afd->frame_time_usec != 0) &&
563
(dst_afd->frame_time_usec % src_afd->frame_time_usec != 0))
565
pj_mutex_unlock(conf->mutex);
566
return PJMEDIA_ENCSAMPLESPFRAME;
569
/* If sink is currently listening to other ports, it needs to be released
570
* first before the new connection made.
572
if (dst_port->transmitter_cnt > 0) {
574
pj_bool_t transmitter_found = PJ_FALSE;
576
pj_assert(dst_port->transmitter_cnt == 1);
577
for (j=0; j<conf->max_ports && !transmitter_found; ++j) {
578
if (conf->ports[j]) {
581
for (k=0; k < conf->ports[j]->listener_cnt; ++k) {
582
if (conf->ports[j]->listener_slots[k] == sink_slot) {
583
PJ_LOG(2,(THIS_FILE, "Connection [%d->%d] is "
584
"disconnected for new connection [%d->%d]",
585
j, sink_slot, src_slot, sink_slot));
586
pjmedia_conf_disconnect_port(conf, j, sink_slot);
587
transmitter_found = PJ_TRUE;
593
pj_assert(dst_port->transmitter_cnt == 0);
596
/* Check if connection has been made */
597
for (i=0; i<src_port->listener_cnt; ++i) {
598
if (src_port->listener_slots[i] == sink_slot)
602
/* Update master port info shortcut, note that application may update
603
* the master port info when the audio device needs to be reopened with
604
* a new format to match to ports connection format.
606
conf->ports[0]->samples_per_frame = PJMEDIA_PIA_SPF(conf->ports[0]->info);
608
if (i == src_port->listener_cnt) {
609
src_port->listener_slots[src_port->listener_cnt] = sink_slot;
611
++src_port->listener_cnt;
612
++dst_port->transmitter_cnt;
614
if (conf->connect_cnt == 1)
617
PJ_LOG(4,(THIS_FILE,"Port %d (%.*s) transmitting to port %d (%.*s)",
619
(int)src_port->name.slen,
622
(int)dst_port->name.slen,
623
dst_port->name.ptr));
626
pj_mutex_unlock(conf->mutex);
628
/* Sound device must be started without mutex, otherwise the
629
* sound thread will deadlock (?)
641
PJ_DEF(pj_status_t) pjmedia_conf_disconnect_port( pjmedia_conf *conf,
645
struct conf_port *src_port, *dst_port;
648
/* Check arguments */
649
PJ_ASSERT_RETURN(conf && src_slot<conf->max_ports &&
650
sink_slot<conf->max_ports, PJ_EINVAL);
652
pj_mutex_lock(conf->mutex);
654
/* Ports must be valid. */
655
src_port = conf->ports[src_slot];
656
dst_port = conf->ports[sink_slot];
657
if (!src_port || !dst_port) {
658
pj_mutex_unlock(conf->mutex);
662
/* Check if connection has been made */
663
for (i=0; i<src_port->listener_cnt; ++i) {
664
if (src_port->listener_slots[i] == sink_slot)
668
if (i != src_port->listener_cnt) {
669
pjmedia_frame_ext *f;
671
pj_assert(src_port->listener_cnt > 0 &&
672
src_port->listener_cnt < conf->max_ports);
673
pj_assert(dst_port->transmitter_cnt > 0 &&
674
dst_port->transmitter_cnt < conf->max_ports);
675
pj_array_erase(src_port->listener_slots, sizeof(SLOT_TYPE),
676
src_port->listener_cnt, i);
678
--src_port->listener_cnt;
679
--dst_port->transmitter_cnt;
681
/* Cleanup listener TX buffer. */
682
f = (pjmedia_frame_ext*)dst_port->tx_buf;
683
f->base.type = PJMEDIA_FRAME_TYPE_NONE;
689
"Port %d (%.*s) stop transmitting to port %d (%.*s)",
691
(int)src_port->name.slen,
694
(int)dst_port->name.slen,
695
dst_port->name.ptr));
698
pj_mutex_unlock(conf->mutex);
700
if (conf->connect_cnt == 0) {
708
* Get number of ports currently registered to the conference bridge.
710
PJ_DEF(unsigned) pjmedia_conf_get_port_count(pjmedia_conf *conf)
712
return conf->port_cnt;
716
* Get total number of ports connections currently set up in the bridge.
718
PJ_DEF(unsigned) pjmedia_conf_get_connect_count(pjmedia_conf *conf)
720
return conf->connect_cnt;
725
* Remove the specified port.
727
PJ_DEF(pj_status_t) pjmedia_conf_remove_port( pjmedia_conf *conf,
730
struct conf_port *conf_port;
733
/* Check arguments */
734
PJ_ASSERT_RETURN(conf && port < conf->max_ports, PJ_EINVAL);
736
/* Suspend the sound devices.
737
* Don't want to remove port while port is being accessed by sound
741
pj_mutex_lock(conf->mutex);
743
/* Port must be valid. */
744
conf_port = conf->ports[port];
745
if (conf_port == NULL) {
746
pj_mutex_unlock(conf->mutex);
750
conf_port->tx_setting = PJMEDIA_PORT_DISABLE;
751
conf_port->rx_setting = PJMEDIA_PORT_DISABLE;
753
/* Remove this port from transmit array of other ports. */
754
for (i=0; i<conf->max_ports; ++i) {
756
struct conf_port *src_port;
758
src_port = conf->ports[i];
763
if (src_port->listener_cnt == 0)
766
for (j=0; j<src_port->listener_cnt; ++j) {
767
if (src_port->listener_slots[j] == port) {
768
pj_array_erase(src_port->listener_slots, sizeof(SLOT_TYPE),
769
src_port->listener_cnt, j);
770
pj_assert(conf->connect_cnt > 0);
772
--src_port->listener_cnt;
778
/* Update transmitter_cnt of ports we're transmitting to */
779
while (conf_port->listener_cnt) {
781
struct conf_port *dst_port;
782
pjmedia_frame_ext *f;
784
dst_slot = conf_port->listener_slots[conf_port->listener_cnt-1];
785
dst_port = conf->ports[dst_slot];
786
--dst_port->transmitter_cnt;
787
--conf_port->listener_cnt;
788
pj_assert(conf->connect_cnt > 0);
791
/* Cleanup & reinit listener TX buffer. */
792
f = (pjmedia_frame_ext*)dst_port->tx_buf;
793
f->base.type = PJMEDIA_FRAME_TYPE_NONE;
799
/* Remove the port. */
800
conf->ports[port] = NULL;
803
pj_mutex_unlock(conf->mutex);
806
/* Stop sound if there's no connection. */
807
if (conf->connect_cnt == 0) {
818
PJ_DEF(pj_status_t) pjmedia_conf_enum_ports( pjmedia_conf *conf,
824
PJ_ASSERT_RETURN(conf && p_count && ports, PJ_EINVAL);
827
pj_mutex_lock(conf->mutex);
829
for (i=0; i<conf->max_ports && count<*p_count; ++i) {
837
pj_mutex_unlock(conf->mutex);
846
PJ_DEF(pj_status_t) pjmedia_conf_get_port_info( pjmedia_conf *conf,
848
pjmedia_conf_port_info *info)
850
struct conf_port *conf_port;
851
const pjmedia_audio_format_detail *afd;
853
/* Check arguments */
854
PJ_ASSERT_RETURN(conf && slot<conf->max_ports, PJ_EINVAL);
857
pj_mutex_lock(conf->mutex);
859
/* Port must be valid. */
860
conf_port = conf->ports[slot];
861
if (conf_port == NULL) {
862
pj_mutex_unlock(conf->mutex);
866
afd = pjmedia_format_get_audio_format_detail(&conf_port->info->fmt, 1);
868
pj_bzero(info, sizeof(pjmedia_conf_port_info));
871
info->name = conf_port->name;
872
info->tx_setting = conf_port->tx_setting;
873
info->rx_setting = conf_port->rx_setting;
874
info->listener_cnt = conf_port->listener_cnt;
875
info->listener_slots = conf_port->listener_slots;
876
info->transmitter_cnt = conf_port->transmitter_cnt;
877
info->clock_rate = afd->clock_rate;
878
info->channel_count = afd->channel_count;
879
info->samples_per_frame = conf_port->samples_per_frame;
880
info->bits_per_sample = afd->bits_per_sample;
881
info->format = conf_port->port->info.fmt;
882
info->tx_adj_level = conf_port->tx_adj_level - NORMAL_LEVEL;
883
info->rx_adj_level = conf_port->rx_adj_level - NORMAL_LEVEL;
886
pj_mutex_unlock(conf->mutex);
892
PJ_DEF(pj_status_t) pjmedia_conf_get_ports_info(pjmedia_conf *conf,
894
pjmedia_conf_port_info info[])
898
PJ_ASSERT_RETURN(conf && size && info, PJ_EINVAL);
901
pj_mutex_lock(conf->mutex);
903
for (i=0; i<conf->max_ports && count<*size; ++i) {
907
pjmedia_conf_get_port_info(conf, i, &info[count]);
912
pj_mutex_unlock(conf->mutex);
922
PJ_DEF(pj_status_t) pjmedia_conf_get_signal_level( pjmedia_conf *conf,
927
struct conf_port *conf_port;
929
/* Check arguments */
930
PJ_ASSERT_RETURN(conf && slot<conf->max_ports, PJ_EINVAL);
933
pj_mutex_lock(conf->mutex);
935
/* Port must be valid. */
936
conf_port = conf->ports[slot];
937
if (conf_port == NULL) {
938
pj_mutex_unlock(conf->mutex);
942
if (tx_level != NULL) {
943
*tx_level = conf_port->tx_level;
946
if (rx_level != NULL)
947
*rx_level = conf_port->rx_level;
950
pj_mutex_unlock(conf->mutex);
957
* Adjust RX level of individual port.
959
PJ_DEF(pj_status_t) pjmedia_conf_adjust_rx_level( pjmedia_conf *conf,
963
struct conf_port *conf_port;
965
/* Check arguments */
966
PJ_ASSERT_RETURN(conf && slot<conf->max_ports, PJ_EINVAL);
968
/* Value must be from -128 to +127 */
969
/* Disabled, you can put more than +127, at your own risk:
970
PJ_ASSERT_RETURN(adj_level >= -128 && adj_level <= 127, PJ_EINVAL);
972
PJ_ASSERT_RETURN(adj_level >= -128, PJ_EINVAL);
975
pj_mutex_lock(conf->mutex);
977
/* Port must be valid. */
978
conf_port = conf->ports[slot];
979
if (conf_port == NULL) {
980
pj_mutex_unlock(conf->mutex);
984
/* Level adjustment is applicable only for ports that work with raw PCM. */
985
PJ_ASSERT_RETURN(conf_port->info->fmt.id == PJMEDIA_FORMAT_L16,
988
/* Set normalized adjustment level. */
989
conf_port->rx_adj_level = adj_level + NORMAL_LEVEL;
992
pj_mutex_unlock(conf->mutex);
999
* Adjust TX level of individual port.
1001
PJ_DEF(pj_status_t) pjmedia_conf_adjust_tx_level( pjmedia_conf *conf,
1005
struct conf_port *conf_port;
1007
/* Check arguments */
1008
PJ_ASSERT_RETURN(conf && slot<conf->max_ports, PJ_EINVAL);
1010
/* Value must be from -128 to +127 */
1011
/* Disabled, you can put more than +127,, at your own risk:
1012
PJ_ASSERT_RETURN(adj_level >= -128 && adj_level <= 127, PJ_EINVAL);
1014
PJ_ASSERT_RETURN(adj_level >= -128, PJ_EINVAL);
1017
pj_mutex_lock(conf->mutex);
1019
/* Port must be valid. */
1020
conf_port = conf->ports[slot];
1021
if (conf_port == NULL) {
1022
pj_mutex_unlock(conf->mutex);
1026
/* Level adjustment is applicable only for ports that work with raw PCM. */
1027
PJ_ASSERT_RETURN(conf_port->info->fmt.id == PJMEDIA_FORMAT_L16,
1030
/* Set normalized adjustment level. */
1031
conf_port->tx_adj_level = adj_level + NORMAL_LEVEL;
1034
pj_mutex_unlock(conf->mutex);
1039
/* Deliver frm_src to a listener port, eventually call port's put_frame()
1040
* when samples count in the frm_dst are equal to port's samples_per_frame.
1042
static pj_status_t write_frame(struct conf_port *cport_dst,
1043
const pjmedia_frame *frm_src)
1045
pjmedia_frame *frm_dst = (pjmedia_frame*)cport_dst->tx_buf;
1047
PJ_TODO(MAKE_SURE_DEST_FRAME_HAS_ENOUGH_SPACE);
1049
frm_dst->type = frm_src->type;
1050
frm_dst->timestamp = cport_dst->ts_tx;
1052
if (frm_src->type == PJMEDIA_FRAME_TYPE_EXTENDED) {
1054
pjmedia_frame_ext *f_src = (pjmedia_frame_ext*)frm_src;
1055
pjmedia_frame_ext *f_dst = (pjmedia_frame_ext*)frm_dst;
1058
for (i = 0; i < f_src->subframe_cnt; ++i) {
1059
pjmedia_frame_ext_subframe *sf;
1061
/* Copy frame to listener's TX buffer. */
1062
sf = pjmedia_frame_ext_get_subframe(f_src, i);
1063
pjmedia_frame_ext_append_subframe(f_dst, sf->data, sf->bitlen,
1064
f_src->samples_cnt /
1065
f_src->subframe_cnt);
1067
/* Check if it's time to deliver the TX buffer to listener,
1068
* i.e: samples count in TX buffer equal to listener's
1069
* samples per frame.
1071
if (f_dst->samples_cnt >= cport_dst->samples_per_frame)
1073
if (cport_dst->slot) {
1074
pjmedia_port_put_frame(cport_dst->port,
1075
(pjmedia_frame*)f_dst);
1077
/* Reset TX buffer. */
1078
f_dst->subframe_cnt = 0;
1079
f_dst->samples_cnt = 0;
1082
/* Update TX timestamp. */
1083
pj_add_timestamp32(&cport_dst->ts_tx,
1084
cport_dst->samples_per_frame);
1088
} else if (frm_src->type == PJMEDIA_FRAME_TYPE_AUDIO) {
1090
pj_int16_t *f_start, *f_end;
1092
f_start = (pj_int16_t*)frm_src->buf;
1093
f_end = f_start + (frm_src->size >> 1);
1095
while (f_start < f_end) {
1096
unsigned nsamples_to_copy, nsamples_req;
1098
/* Copy frame to listener's TX buffer.
1099
* Note that if the destination is port 0, just copy the whole
1100
* available samples.
1102
nsamples_to_copy = f_end - f_start;
1103
nsamples_req = cport_dst->samples_per_frame -
1105
if (cport_dst->slot && nsamples_to_copy > nsamples_req)
1106
nsamples_to_copy = nsamples_req;
1108
/* Adjust TX level. */
1109
if (cport_dst->tx_adj_level != NORMAL_LEVEL) {
1110
pj_int16_t *p, *p_end;
1113
p_end = p + nsamples_to_copy;
1115
pj_int32_t itemp = *p;
1117
/* Adjust the level */
1118
itemp = (itemp * cport_dst->tx_adj_level) >> 7;
1120
/* Clip the signal if it's too loud */
1121
if (itemp > MAX_LEVEL) itemp = MAX_LEVEL;
1122
else if (itemp < MIN_LEVEL) itemp = MIN_LEVEL;
1124
/* Put back in the buffer. */
1125
*p = (pj_int16_t)itemp;
1130
pjmedia_copy_samples((pj_int16_t*)frm_dst->buf + (frm_dst->size>>1),
1133
frm_dst->size += nsamples_to_copy << 1;
1134
f_start += nsamples_to_copy;
1136
/* Check if it's time to deliver the TX buffer to listener,
1137
* i.e: samples count in TX buffer equal to listener's
1138
* samples per frame. Note that for destination port 0 this
1139
* function will just populate all samples in the TX buffer.
1141
if (cport_dst->slot == 0) {
1142
/* Update TX timestamp. */
1143
pj_add_timestamp32(&cport_dst->ts_tx, nsamples_to_copy);
1144
} else if ((frm_dst->size >> 1) ==
1145
cport_dst->samples_per_frame)
1147
pjmedia_port_put_frame(cport_dst->port, frm_dst);
1149
/* Reset TX buffer. */
1152
/* Update TX timestamp. */
1153
pj_add_timestamp32(&cport_dst->ts_tx,
1154
cport_dst->samples_per_frame);
1158
} else if (frm_src->type == PJMEDIA_FRAME_TYPE_NONE) {
1160
/* Check port format. */
1161
if (cport_dst->port &&
1162
cport_dst->port->info.fmt.id == PJMEDIA_FORMAT_L16)
1164
/* When there is already some samples in listener's TX buffer,
1165
* pad the buffer with "zero samples".
1167
if (frm_dst->size != 0) {
1168
pjmedia_zero_samples((pj_int16_t*)frm_dst->buf,
1169
cport_dst->samples_per_frame -
1170
(frm_dst->size>>1));
1172
frm_dst->type = PJMEDIA_FRAME_TYPE_AUDIO;
1173
frm_dst->size = cport_dst->samples_per_frame << 1;
1174
if (cport_dst->slot) {
1175
pjmedia_port_put_frame(cport_dst->port, frm_dst);
1177
/* Reset TX buffer. */
1181
/* Update TX timestamp. */
1182
pj_add_timestamp32(&cport_dst->ts_tx,
1183
cport_dst->samples_per_frame);
1186
pjmedia_frame_ext *f_dst = (pjmedia_frame_ext*)frm_dst;
1188
if (f_dst->samples_cnt != 0) {
1189
frm_dst->type = PJMEDIA_FRAME_TYPE_EXTENDED;
1190
pjmedia_frame_ext_append_subframe(f_dst, NULL, 0, (pj_uint16_t)
1191
(cport_dst->samples_per_frame - f_dst->samples_cnt));
1192
if (cport_dst->slot) {
1193
pjmedia_port_put_frame(cport_dst->port, frm_dst);
1195
/* Reset TX buffer. */
1196
f_dst->subframe_cnt = 0;
1197
f_dst->samples_cnt = 0;
1200
/* Update TX timestamp. */
1201
pj_add_timestamp32(&cport_dst->ts_tx,
1202
cport_dst->samples_per_frame);
1206
/* Synchronize clock. */
1207
while (pj_cmp_timestamp(&cport_dst->ts_clock,
1208
&cport_dst->ts_tx) > 0)
1210
frm_dst->type = PJMEDIA_FRAME_TYPE_NONE;
1211
frm_dst->timestamp = cport_dst->ts_tx;
1212
if (cport_dst->slot)
1213
pjmedia_port_put_frame(cport_dst->port, frm_dst);
1215
/* Update TX timestamp. */
1216
pj_add_timestamp32(&cport_dst->ts_tx, cport_dst->samples_per_frame);
1226
static pj_status_t get_frame(pjmedia_port *this_port,
1227
pjmedia_frame *frame)
1229
pjmedia_conf *conf = (pjmedia_conf*) this_port->port_data.pdata;
1233
pj_mutex_lock(conf->mutex);
1235
/* Call get_frame() from all ports (except port 0) that has
1236
* receiver and distribute the frame (put the frame to the destination
1237
* port's buffer to accommodate different ptime, and ultimately call
1238
* put_frame() of that port) to ports that are receiving from this port.
1240
for (i=1, ci=1; i<conf->max_ports && ci<conf->port_cnt; ++i) {
1241
struct conf_port *cport = conf->ports[i];
1242
unsigned master_samples_per_frame;
1244
/* Skip empty port. */
1248
/* Var "ci" is to count how many ports have been visited so far. */
1251
master_samples_per_frame = PJMEDIA_PIA_SPF(&conf->master_port->info);
1253
/* Update clock of the port. */
1254
pj_add_timestamp32(&cport->ts_clock, master_samples_per_frame);
1256
/* Skip if we're not allowed to receive from this port or
1257
* the port doesn't have listeners.
1259
if (cport->rx_setting == PJMEDIA_PORT_DISABLE ||
1260
cport->listener_cnt == 0)
1262
cport->rx_level = 0;
1263
pj_add_timestamp32(&cport->ts_rx, master_samples_per_frame);
1267
/* Get frame from each port, put it to the listener TX buffer,
1268
* and eventually call put_frame() of the listener. This loop
1269
* will also make sure the ptime between conf & port synchronized.
1271
while (pj_cmp_timestamp(&cport->ts_clock, &cport->ts_rx) > 0) {
1272
pjmedia_frame *f = (pjmedia_frame*)conf->buf;
1275
pj_int32_t level = 0;
1277
pj_add_timestamp32(&cport->ts_rx, cport->samples_per_frame);
1279
f->buf = &conf->buf[sizeof(pjmedia_frame)];
1280
f->size = cport->samples_per_frame<<1;
1282
/* Get frame from port. */
1283
status = pjmedia_port_get_frame(cport->port, f);
1284
if (status != PJ_SUCCESS)
1287
/* Calculate & adjust RX level. */
1288
if (f->type == PJMEDIA_FRAME_TYPE_AUDIO) {
1289
if (cport->rx_adj_level != NORMAL_LEVEL) {
1290
pj_int16_t *p = (pj_int16_t*)f->buf;
1293
end = p + (f->size >> 1);
1295
pj_int32_t itemp = *p;
1297
/* Adjust the level */
1298
itemp = (itemp * cport->rx_adj_level) >> 7;
1300
/* Clip the signal if it's too loud */
1301
if (itemp > MAX_LEVEL) itemp = MAX_LEVEL;
1302
else if (itemp < MIN_LEVEL) itemp = MIN_LEVEL;
1304
level += PJ_ABS(itemp);
1306
/* Put back in the buffer. */
1307
*p = (pj_int16_t)itemp;
1310
level /= (f->size >> 1);
1312
level = pjmedia_calc_avg_signal((const pj_int16_t*)f->buf,
1315
} else if (f->type == PJMEDIA_FRAME_TYPE_EXTENDED) {
1316
/* For extended frame, level is unknown, so we just set
1317
* it to NORMAL_LEVEL.
1319
level = NORMAL_LEVEL;
1322
cport->rx_level = pjmedia_linear2ulaw(level) ^ 0xff;
1324
/* Put the frame to all listeners. */
1325
for (j=0; j < cport->listener_cnt; ++j)
1327
struct conf_port *listener;
1329
listener = conf->ports[cport->listener_slots[j]];
1331
/* Skip if this listener doesn't want to receive audio */
1332
if (listener->tx_setting == PJMEDIA_PORT_DISABLE) {
1333
pj_add_timestamp32(&listener->ts_tx,
1334
listener->samples_per_frame);
1335
listener->tx_level = 0;
1339
status = write_frame(listener, f);
1340
if (status != PJ_SUCCESS) {
1341
listener->tx_level = 0;
1345
/* Set listener TX level based on transmitter RX level &
1346
* listener TX level.
1348
listener->tx_level = (cport->rx_level * listener->tx_adj_level)
1354
/* Keep alive. Update TX timestamp and send frame type NONE to all
1355
* underflow ports at their own clock.
1357
for (i=1, ci=1; i<conf->max_ports && ci<conf->port_cnt; ++i) {
1358
struct conf_port *cport = conf->ports[i];
1360
/* Skip empty port. */
1364
/* Var "ci" is to count how many ports have been visited so far. */
1367
if (cport->tx_setting==PJMEDIA_PORT_MUTE || cport->transmitter_cnt==0)
1369
pjmedia_frame_ext *f;
1371
/* Clear left-over samples in tx_buffer, if any, so that it won't
1372
* be transmitted next time we have audio signal.
1374
f = (pjmedia_frame_ext*)cport->tx_buf;
1375
f->base.type = PJMEDIA_FRAME_TYPE_NONE;
1378
f->subframe_cnt = 0;
1380
cport->tx_level = 0;
1382
while (pj_cmp_timestamp(&cport->ts_clock, &cport->ts_tx) > 0)
1384
if (cport->tx_setting == PJMEDIA_PORT_ENABLE) {
1385
pjmedia_frame tmp_f;
1387
tmp_f.timestamp = cport->ts_tx;
1388
tmp_f.type = PJMEDIA_FRAME_TYPE_NONE;
1392
pjmedia_port_put_frame(cport->port, &tmp_f);
1393
pj_add_timestamp32(&cport->ts_tx, cport->samples_per_frame);
1399
/* Return sound playback frame. */
1401
struct conf_port *this_cport = conf->ports[this_port->port_data.ldata];
1402
pjmedia_frame *f_src = (pjmedia_frame*) this_cport->tx_buf;
1404
frame->type = f_src->type;
1406
if (f_src->type == PJMEDIA_FRAME_TYPE_EXTENDED) {
1407
pjmedia_frame_ext *f_src_ = (pjmedia_frame_ext*)f_src;
1408
pjmedia_frame_ext *f_dst = (pjmedia_frame_ext*)frame;
1409
pjmedia_frame_ext_subframe *sf;
1410
unsigned samples_per_subframe;
1412
if (f_src_->samples_cnt < this_cport->samples_per_frame) {
1413
f_dst->base.type = PJMEDIA_FRAME_TYPE_NONE;
1414
f_dst->samples_cnt = 0;
1415
f_dst->subframe_cnt = 0;
1419
f_dst->samples_cnt = 0;
1420
f_dst->subframe_cnt = 0;
1422
samples_per_subframe = f_src_->samples_cnt / f_src_->subframe_cnt;
1425
while (f_dst->samples_cnt < this_cport->samples_per_frame) {
1426
sf = pjmedia_frame_ext_get_subframe(f_src_, i++);
1428
pjmedia_frame_ext_append_subframe(f_dst, sf->data, sf->bitlen,
1429
samples_per_subframe);
1432
/* Shift left TX buffer. */
1433
pjmedia_frame_ext_pop_subframes(f_src_, i);
1435
} else if (f_src->type == PJMEDIA_FRAME_TYPE_AUDIO) {
1436
if ((f_src->size>>1) < this_cport->samples_per_frame) {
1437
frame->type = PJMEDIA_FRAME_TYPE_NONE;
1442
pjmedia_copy_samples((pj_int16_t*)frame->buf,
1443
(pj_int16_t*)f_src->buf,
1444
this_cport->samples_per_frame);
1445
frame->size = this_cport->samples_per_frame << 1;
1447
/* Shift left TX buffer. */
1448
f_src->size -= frame->size;
1450
pjmedia_move_samples((pj_int16_t*)f_src->buf,
1451
(pj_int16_t*)f_src->buf +
1452
this_cport->samples_per_frame,
1454
} else { /* PJMEDIA_FRAME_TYPE_NONE */
1455
pjmedia_frame_ext *f_src_ = (pjmedia_frame_ext*)f_src;
1457
/* Reset source/TX buffer */
1458
f_src_->base.size = 0;
1459
f_src_->samples_cnt = 0;
1460
f_src_->subframe_cnt = 0;
1465
pj_mutex_unlock(conf->mutex);
1471
* Recorder callback.
1473
static pj_status_t put_frame(pjmedia_port *this_port,
1476
pjmedia_conf *conf = (pjmedia_conf*) this_port->port_data.pdata;
1477
struct conf_port *cport;
1479
pj_int32_t level = 0;
1482
pj_mutex_lock(conf->mutex);
1484
/* Get conf port of this port */
1485
cport = conf->ports[this_port->port_data.ldata];
1486
if (cport == NULL) {
1488
pj_mutex_unlock(conf->mutex);
1492
pj_add_timestamp32(&cport->ts_rx, cport->samples_per_frame);
1494
/* Skip if this port is muted/disabled. */
1495
if (cport->rx_setting == PJMEDIA_PORT_DISABLE) {
1496
cport->rx_level = 0;
1498
pj_mutex_unlock(conf->mutex);
1502
/* Skip if no port is listening to the microphone */
1503
if (cport->listener_cnt == 0) {
1504
cport->rx_level = 0;
1506
pj_mutex_unlock(conf->mutex);
1510
/* Calculate & adjust RX level. */
1511
if (f->type == PJMEDIA_FRAME_TYPE_AUDIO) {
1512
if (cport->rx_adj_level != NORMAL_LEVEL) {
1513
pj_int16_t *p = (pj_int16_t*)f->buf;
1516
end = p + (f->size >> 1);
1518
pj_int32_t itemp = *p;
1520
/* Adjust the level */
1521
itemp = (itemp * cport->rx_adj_level) >> 7;
1523
/* Clip the signal if it's too loud */
1524
if (itemp > MAX_LEVEL) itemp = MAX_LEVEL;
1525
else if (itemp < MIN_LEVEL) itemp = MIN_LEVEL;
1527
level += PJ_ABS(itemp);
1529
/* Put back in the buffer. */
1530
*p = (pj_int16_t)itemp;
1533
level /= (f->size >> 1);
1535
level = pjmedia_calc_avg_signal((const pj_int16_t*)f->buf,
1538
} else if (f->type == PJMEDIA_FRAME_TYPE_EXTENDED) {
1539
/* For extended frame, level is unknown, so we just set
1540
* it to NORMAL_LEVEL.
1542
level = NORMAL_LEVEL;
1545
cport->rx_level = pjmedia_linear2ulaw(level) ^ 0xff;
1547
/* Put the frame to all listeners. */
1548
for (j=0; j < cport->listener_cnt; ++j)
1550
struct conf_port *listener;
1553
listener = conf->ports[cport->listener_slots[j]];
1555
/* Skip if this listener doesn't want to receive audio */
1556
if (listener->tx_setting == PJMEDIA_PORT_DISABLE) {
1557
pj_add_timestamp32(&listener->ts_tx,
1558
listener->samples_per_frame);
1559
listener->tx_level = 0;
1563
/* Skip loopback for now. */
1564
if (listener == cport) {
1565
pj_add_timestamp32(&listener->ts_tx,
1566
listener->samples_per_frame);
1567
listener->tx_level = 0;
1571
status = write_frame(listener, f);
1572
if (status != PJ_SUCCESS) {
1573
listener->tx_level = 0;
1577
/* Set listener TX level based on transmitter RX level & listener
1580
listener->tx_level = (cport->rx_level * listener->tx_adj_level) >> 8;
1584
pj_mutex_unlock(conf->mutex);