Files
asterisk/channels/dahdi/bridge_native_dahdi.c
Matthew Jordan a2c912e997 media formats: re-architect handling of media for performance improvements
In the old times media formats were represented using a bit field. This was
fast but had a few limitations.
 1. Asterisk was limited in how many formats it could handle.
 2. Formats, being a bit field, could not include any attribute information.
    A format was strictly its type, e.g., "this is ulaw".
This was changed in Asterisk 10 (see
https://wiki.asterisk.org/wiki/display/AST/Media+Architecture+Proposal for
notes on that work) which led to the creation of the ast_format structure.
This structure allowed Asterisk to handle attributes and bundle information
with a format.

Additionally, ast_format_cap was created to act as a container for multiple
formats that, together, formed the capability of some entity. Another
mechanism was added to allow logic to be registered which performed format
attribute negotiation. Everywhere throughout the codebase Asterisk was
changed to use this strategy.

Unfortunately, in software, there is no free lunch. These new capabilities
came at a cost.

Performance analysis and profiling showed that we spend an inordinate
amount of time comparing, copying, and generally manipulating formats and
their related structures. Basic prototyping has shown that a reasonably
large performance improvement could be made in this area. This patch is the
result of that project, which overhauled the media format architecture
and its usage in Asterisk to improve performance.

Generally, the new philosophy for handling formats is as follows:
 * The ast_format structure is reference counted. This removed a large amount
   of the memory allocations and copying that was done in prior versions.
 * In order to prevent race conditions while keeping things performant, the
   ast_format structure is immutable by convention and lock-free. Violate this
   tenet at your peril!
 * Because formats are reference counted, codecs are also reference counted.
   The Asterisk core generally provides built-in codecs and caches the
   ast_format structures created to represent them. Generally, to prevent
   inordinate amounts of module reference bumping, codecs and formats can be
   added at run-time but cannot be removed.
 * All compatibility with the bit field representation of codecs/formats has
   been moved to a compatibility API. The primary user of this representation
   is chan_iax2, which must continue to maintain its bit-field usage of formats
   for interoperability concerns.
 * When a format is negotiated with attributes, or when a format cannot be
   represented by one of the cached formats, a new format object is created or
   cloned from an existing format. That format may have the same codec
   underlying it, but is a different format than a version of the format with
   different attributes or without attributes.
 * While formats are reference counted objects, the reference count maintained
   on the format should be manipulated with care. Formats are generally cached
   and will persist for the lifetime of Asterisk and do not explicitly need
   to have their lifetime modified. An exception to this is when the user of a
   format does not know where the format came from *and* the user may outlive
   the provider of the format. This occurs, for example, when a format is read
   from a channel: the channel may have a format with attributes (hence,
   non-cached) and the user of the format may last longer than the channel (if
   the reference to the channel is released prior to the format's reference).

For more information on this work, see the API design notes:
  https://wiki.asterisk.org/wiki/display/AST/Media+Format+Rewrite

Finally, this work was the culmination of a large number of developer's
efforts. Extra thanks goes to Corey Farrell, who took on a large amount of the
work in the Asterisk core, chan_sip, and was an invaluable resource in peer
reviews throughout this project.

There were a substantial number of patches contributed during this work; the
following issues/patch names simply reflect some of the work (and will cause
the release scripts to give attribution to the individuals who work on them).

Reviews:
 https://reviewboard.asterisk.org/r/3814
 https://reviewboard.asterisk.org/r/3808
 https://reviewboard.asterisk.org/r/3805
 https://reviewboard.asterisk.org/r/3803
 https://reviewboard.asterisk.org/r/3801
 https://reviewboard.asterisk.org/r/3798
 https://reviewboard.asterisk.org/r/3800
 https://reviewboard.asterisk.org/r/3794
 https://reviewboard.asterisk.org/r/3793
 https://reviewboard.asterisk.org/r/3792
 https://reviewboard.asterisk.org/r/3791
 https://reviewboard.asterisk.org/r/3790
 https://reviewboard.asterisk.org/r/3789
 https://reviewboard.asterisk.org/r/3788
 https://reviewboard.asterisk.org/r/3787
 https://reviewboard.asterisk.org/r/3786
 https://reviewboard.asterisk.org/r/3784
 https://reviewboard.asterisk.org/r/3783
 https://reviewboard.asterisk.org/r/3778
 https://reviewboard.asterisk.org/r/3774
 https://reviewboard.asterisk.org/r/3775
 https://reviewboard.asterisk.org/r/3772
 https://reviewboard.asterisk.org/r/3761
 https://reviewboard.asterisk.org/r/3754
 https://reviewboard.asterisk.org/r/3753
 https://reviewboard.asterisk.org/r/3751
 https://reviewboard.asterisk.org/r/3750
 https://reviewboard.asterisk.org/r/3748
 https://reviewboard.asterisk.org/r/3747
 https://reviewboard.asterisk.org/r/3746
 https://reviewboard.asterisk.org/r/3742
 https://reviewboard.asterisk.org/r/3740
 https://reviewboard.asterisk.org/r/3739
 https://reviewboard.asterisk.org/r/3738
 https://reviewboard.asterisk.org/r/3737
 https://reviewboard.asterisk.org/r/3736
 https://reviewboard.asterisk.org/r/3734
 https://reviewboard.asterisk.org/r/3722
 https://reviewboard.asterisk.org/r/3713
 https://reviewboard.asterisk.org/r/3703
 https://reviewboard.asterisk.org/r/3689
 https://reviewboard.asterisk.org/r/3687
 https://reviewboard.asterisk.org/r/3674
 https://reviewboard.asterisk.org/r/3671
 https://reviewboard.asterisk.org/r/3667
 https://reviewboard.asterisk.org/r/3665
 https://reviewboard.asterisk.org/r/3625
 https://reviewboard.asterisk.org/r/3602
 https://reviewboard.asterisk.org/r/3519
 https://reviewboard.asterisk.org/r/3518
 https://reviewboard.asterisk.org/r/3516
 https://reviewboard.asterisk.org/r/3515
 https://reviewboard.asterisk.org/r/3512
 https://reviewboard.asterisk.org/r/3506
 https://reviewboard.asterisk.org/r/3413
 https://reviewboard.asterisk.org/r/3410
 https://reviewboard.asterisk.org/r/3387
 https://reviewboard.asterisk.org/r/3388
 https://reviewboard.asterisk.org/r/3389
 https://reviewboard.asterisk.org/r/3390
 https://reviewboard.asterisk.org/r/3321
 https://reviewboard.asterisk.org/r/3320
 https://reviewboard.asterisk.org/r/3319
 https://reviewboard.asterisk.org/r/3318
 https://reviewboard.asterisk.org/r/3266
 https://reviewboard.asterisk.org/r/3265
 https://reviewboard.asterisk.org/r/3234
 https://reviewboard.asterisk.org/r/3178

ASTERISK-23114 #close
Reported by: mjordan
  media_formats_translation_core.diff uploaded by kharwell (License 6464)
  rb3506.diff uploaded by mjordan (License 6283)
  media_format_app_file.diff uploaded by kharwell (License 6464) 
  misc-2.diff uploaded by file (License 5000)
  chan_mild-3.diff uploaded by file (License 5000) 
  chan_obscure.diff uploaded by file (License 5000) 
  jingle.diff uploaded by file (License 5000) 
  funcs.diff uploaded by file (License 5000) 
  formats.diff uploaded by file (License 5000) 
  core.diff uploaded by file (License 5000) 
  bridges.diff uploaded by file (License 5000) 
  mf-codecs-2.diff uploaded by file (License 5000) 
  mf-app_fax.diff uploaded by file (License 5000) 
  mf-apps-3.diff uploaded by file (License 5000) 
  media-formats-3.diff uploaded by file (License 5000) 

ASTERISK-23715
  rb3713.patch uploaded by coreyfarrell (License 5909)
  rb3689.patch uploaded by mjordan (License 6283)
  
ASTERISK-23957
  rb3722.patch uploaded by mjordan (License 6283) 
  mf-attributes-3.diff uploaded by file (License 5000) 

ASTERISK-23958
Tested by: jrose
  rb3822.patch uploaded by coreyfarrell (License 5909) 
  rb3800.patch uploaded by jrose (License 6182)
  chan_sip.diff uploaded by mjordan (License 6283) 
  rb3747.patch uploaded by jrose (License 6182)

ASTERISK-23959 #close
Tested by: sgriepentrog, mjordan, coreyfarrell
  sip_cleanup.diff uploaded by opticron (License 6273)
  chan_sip_caps.diff uploaded by mjordan (License 6283) 
  rb3751.patch uploaded by coreyfarrell (License 5909) 
  chan_sip-3.diff uploaded by file (License 5000) 

ASTERISK-23960 #close
Tested by: opticron
  direct_media.diff uploaded by opticron (License 6273) 
  pjsip-direct-media.diff uploaded by file (License 5000) 
  format_cap_remove.diff uploaded by opticron (License 6273) 
  media_format_fixes.diff uploaded by opticron (License 6273) 
  chan_pjsip-2.diff uploaded by file (License 5000) 

ASTERISK-23966 #close
Tested by: rmudgett
  rb3803.patch uploaded by rmudgetti (License 5621)
  chan_dahdi.diff uploaded by file (License 5000) 
  
ASTERISK-24064 #close
Tested by: coreyfarrell, mjordan, opticron, file, rmudgett, sgriepentrog, jrose
  rb3814.patch uploaded by rmudgett (License 5621) 
  moh_cleanup.diff uploaded by opticron (License 6273) 
  bridge_leak.diff uploaded by opticron (License 6273) 
  translate.diff uploaded by file (License 5000) 
  rb3795.patch uploaded by rmudgett (License 5621) 
  tls_fix.diff uploaded by mjordan (License 6283) 
  fax-mf-fix-2.diff uploaded by file (License 5000) 
  rtp_transfer_stuff uploaded by mjordan (License 6283) 
  rb3787.patch uploaded by rmudgett (License 5621) 
  media-formats-explicit-translate-format-3.diff uploaded by file (License 5000) 
  format_cache_case_fix.diff uploaded by opticron (License 6273) 
  rb3774.patch uploaded by rmudgett (License 5621) 
  rb3775.patch uploaded by rmudgett (License 5621) 
  rtp_engine_fix.diff uploaded by opticron (License 6273) 
  rtp_crash_fix.diff uploaded by opticron (License 6273) 
  rb3753.patch uploaded by mjordan (License 6283) 
  rb3750.patch uploaded by mjordan (License 6283) 
  rb3748.patch uploaded by rmudgett (License 5621) 
  media_format_fixes.diff uploaded by opticron (License 6273) 
  rb3740.patch uploaded by mjordan (License 6283) 
  rb3739.patch uploaded by mjordan (License 6283) 
  rb3734.patch uploaded by mjordan (License 6283) 
  rb3689.patch uploaded by mjordan (License 6283) 
  rb3674.patch uploaded by coreyfarrell (License 5909) 
  rb3671.patch uploaded by coreyfarrell (License 5909) 
  rb3667.patch uploaded by coreyfarrell (License 5909) 
  rb3665.patch uploaded by mjordan (License 6283) 
  rb3625.patch uploaded by coreyfarrell (License 5909) 
  rb3602.patch uploaded by coreyfarrell (License 5909) 
  format_compatibility-2.diff uploaded by file (License 5000) 
  core.diff uploaded by file (License 5000) 
  


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@419044 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2014-07-20 22:06:33 +00:00

926 lines
24 KiB
C

/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2013 Digium, Inc.
*
* Richard Mudgett <rmudgett@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*!
* \file
* \brief Native DAHDI bridging support.
*
* \author Richard Mudgett <rmudgett@digium.com>
*
* See Also:
* \arg \ref AstCREDITS
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "../sig_analog.h"
#if defined(HAVE_PRI)
#include "../sig_pri.h"
#endif /* defined(HAVE_PRI) */
#include "../chan_dahdi.h"
#include "bridge_native_dahdi.h"
#include "asterisk/bridge.h"
#include "asterisk/bridge_technology.h"
#include "asterisk/frame.h"
#include "asterisk/format_cache.h"
/* ------------------------------------------------------------------- */
static const struct ast_channel_tech *dahdi_tech;
struct native_pvt_chan {
/*! Original private. */
struct dahdi_pvt *pvt;
/*! Original private owner. */
struct ast_channel *owner;
/*! Original owner index. */
int index;
/*! Original file descriptor 0. */
int fd0;
/*! Original channel state. */
int state;
/*! Original inthreeway. */
unsigned int inthreeway:1;
};
struct native_pvt_bridge {
/*! Master channel in the native bridge. */
struct dahdi_pvt *master;
/*! Slave channel in the native bridge. */
struct dahdi_pvt *slave;
/*! TRUE if the bridge can start when ready. */
unsigned int saw_start:1;
/*! TRUE if the channels are connected in a conference. */
unsigned int connected:1;
#if defined(HAVE_PRI) && defined(PRI_2BCT)
/*!
* \brief TRUE if tried to eliminate possible PRI tromboned call.
*
* \note A tromboned call uses two B channels of the same ISDN
* span. One leg comes into Asterisk, the other leg goes out of
* Asterisk, and Asterisk is natively bridging the two legs.
*/
unsigned int tried_trombone_removal:1;
#endif /* defined(HAVE_PRI) && defined(PRI_2BCT) */
};
/*!
* \internal
* \brief Create a bridge technology instance for a bridge.
* \since 12.0.0
*
* \retval 0 on success
* \retval -1 on failure
*
* \note On entry, bridge may or may not already be locked.
* However, it can be accessed as if it were locked.
*/
static int native_bridge_create(struct ast_bridge *bridge)
{
struct native_pvt_bridge *tech_pvt;
ast_assert(!bridge->tech_pvt);
tech_pvt = ast_calloc(1, sizeof(*tech_pvt));
if (!tech_pvt) {
return -1;
}
bridge->tech_pvt = tech_pvt;
return 0;
}
/*!
* \internal
* \brief Destroy a bridging technology instance for a bridge.
* \since 12.0.0
*
* \note On entry, bridge must NOT be locked.
*/
static void native_bridge_destroy(struct ast_bridge *bridge)
{
struct native_pvt_bridge *tech_pvt;
tech_pvt = bridge->tech_pvt;
bridge->tech_pvt = NULL;
ast_free(tech_pvt);
}
/*!
* \internal
* \brief Stop native bridging activity.
* \since 12.0.0
*
* \param bridge What to operate upon.
*
* \return Nothing
*
* \note On entry, bridge is already locked.
*/
static void native_stop(struct ast_bridge *bridge)
{
struct native_pvt_bridge *bridge_tech_pvt;
struct ast_bridge_channel *cur;
ast_assert(bridge->tech_pvt != NULL);
AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
struct native_pvt_chan *chan_tech_pvt;
chan_tech_pvt = cur->tech_pvt;
if (!chan_tech_pvt) {
continue;
}
ast_mutex_lock(&chan_tech_pvt->pvt->lock);
if (chan_tech_pvt->pvt == ast_channel_tech_pvt(cur->chan)) {
dahdi_ec_enable(chan_tech_pvt->pvt);
}
if (chan_tech_pvt->index == SUB_REAL) {
dahdi_dtmf_detect_enable(chan_tech_pvt->pvt);
}
ast_mutex_unlock(&chan_tech_pvt->pvt->lock);
}
bridge_tech_pvt = bridge->tech_pvt;
dahdi_master_slave_unlink(bridge_tech_pvt->slave, bridge_tech_pvt->master, 1);
ast_debug(2, "Stop native bridging %s and %s\n",
ast_channel_name(AST_LIST_FIRST(&bridge->channels)->chan),
ast_channel_name(AST_LIST_LAST(&bridge->channels)->chan));
}
/*!
* \internal
* \brief Request to stop native bridging activity.
* \since 12.0.0
*
* \param bridge What to operate upon.
*
* \return Nothing
*
* \note On entry, bridge is already locked.
*/
static void native_request_stop(struct ast_bridge *bridge)
{
struct native_pvt_bridge *tech_pvt;
ast_assert(bridge->tech_pvt != NULL);
tech_pvt = bridge->tech_pvt;
if (!tech_pvt->connected) {
return;
}
tech_pvt->connected = 0;
/* Now to actually stop the bridge. */
native_stop(bridge);
}
/*!
* \internal
* \brief Start native bridging activity.
* \since 12.0.0
*
* \param bridge What to operate upon.
*
* \retval 0 on success.
* \retval -1 on error. Could not start the bridge.
*
* \note On entry, bridge may or may not already be locked.
* However, it can be accessed as if it were locked.
*/
static int native_start(struct ast_bridge *bridge)
{
struct native_pvt_bridge *tech_pvt;
struct ast_bridge_channel *bc0;
struct ast_bridge_channel *bc1;
struct native_pvt_chan *npc0;
struct native_pvt_chan *npc1;
struct ast_channel *c0;
struct ast_channel *c1;
struct dahdi_pvt *p0;
struct dahdi_pvt *p1;
struct dahdi_pvt *master;
struct dahdi_pvt *slave;
int inconf;
int nothing_ok;
ast_assert(bridge->tech_pvt != NULL);
bc0 = AST_LIST_FIRST(&bridge->channels);
bc1 = AST_LIST_LAST(&bridge->channels);
c0 = bc0->chan;
c1 = bc1->chan;
/* Lock channels and privates */
for (;;) {
ast_channel_lock(c0);
if (!ast_channel_trylock(c1)) {
p0 = ast_channel_tech_pvt(c0);
if (!ast_mutex_trylock(&p0->lock)) {
p1 = ast_channel_tech_pvt(c1);
if (!ast_mutex_trylock(&p1->lock)) {
/* Got all locks */
break;
}
ast_mutex_unlock(&p0->lock);
}
ast_channel_unlock(c1);
}
ast_channel_unlock(c0);
sched_yield();
}
npc0 = bc0->tech_pvt;
ast_assert(npc0 != NULL);
npc0->pvt = p0;
npc0->owner = p0->owner;
npc0->index = dahdi_get_index(c0, p0, 0);
npc0->fd0 = ast_channel_fd(c0, 0);
npc0->state = -1;
npc0->inthreeway = p0->subs[SUB_REAL].inthreeway;
npc1 = bc1->tech_pvt;
ast_assert(npc1 != NULL);
npc1->pvt = p1;
npc1->owner = p1->owner;
npc1->index = dahdi_get_index(c1, p1, 0);
npc1->fd0 = ast_channel_fd(c1, 0);
npc1->state = -1;
npc1->inthreeway = p1->subs[SUB_REAL].inthreeway;
/*
* Check things that can change on the privates while in native
* bridging and cause native to not activate.
*/
if (npc0->index < 0 || npc1->index < 0
#if defined(HAVE_PRI)
/*
* PRI nobch channels (hold and call waiting) are equivalent to
* pseudo channels and cannot be nativly bridged.
*/
|| (dahdi_sig_pri_lib_handles(p0->sig)
&& ((struct sig_pri_chan *) p0->sig_pvt)->no_b_channel)
|| (dahdi_sig_pri_lib_handles(p1->sig)
&& ((struct sig_pri_chan *) p1->sig_pvt)->no_b_channel)
#endif /* defined(HAVE_PRI) */
) {
ast_mutex_unlock(&p0->lock);
ast_mutex_unlock(&p1->lock);
ast_channel_unlock(c0);
ast_channel_unlock(c1);
return -1;
}
inconf = 0;
nothing_ok = 1;
master = NULL;
slave = NULL;
if (npc0->index == SUB_REAL && npc1->index == SUB_REAL) {
if (p0->owner && p1->owner) {
/*
* If we don't have a call-wait in a 3-way, and we aren't in a
* 3-way, we can be master.
*/
if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
master = p0;
slave = p1;
inconf = 1;
} else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
master = p1;
slave = p0;
inconf = 1;
} else {
ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
ast_log(LOG_WARNING, "p0: chan %d/%d/CW%d/3W%d, p1: chan %d/%d/CW%d/3W%d\n",
p0->channel,
npc0->index, (p0->subs[SUB_CALLWAIT].dfd > -1) ? 1 : 0,
p0->subs[SUB_REAL].inthreeway,
p0->channel,
npc0->index, (p1->subs[SUB_CALLWAIT].dfd > -1) ? 1 : 0,
p1->subs[SUB_REAL].inthreeway);
}
nothing_ok = 0;
}
} else if (npc0->index == SUB_REAL && npc1->index == SUB_THREEWAY) {
if (p1->subs[SUB_THREEWAY].inthreeway) {
master = p1;
slave = p0;
nothing_ok = 0;
}
} else if (npc0->index == SUB_THREEWAY && npc1->index == SUB_REAL) {
if (p0->subs[SUB_THREEWAY].inthreeway) {
master = p0;
slave = p1;
nothing_ok = 0;
}
} else if (npc0->index == SUB_REAL && npc1->index == SUB_CALLWAIT) {
/*
* We have a real and a call wait. If we're in a three way
* call, put us in it, otherwise, don't put us in anything.
*/
if (p1->subs[SUB_CALLWAIT].inthreeway) {
master = p1;
slave = p0;
nothing_ok = 0;
}
} else if (npc0->index == SUB_CALLWAIT && npc1->index == SUB_REAL) {
/* Same as previous */
if (p0->subs[SUB_CALLWAIT].inthreeway) {
master = p0;
slave = p1;
nothing_ok = 0;
}
}
ast_debug(3, "master: %d, slave: %d, nothing_ok: %d\n",
master ? master->channel : 0,
slave ? slave->channel : 0,
nothing_ok);
if (master && slave) {
/*
* Stop any tones, or play ringtone as appropriate. If they are
* bridged in an active threeway call with a channel that is
* ringing, we should indicate ringing.
*/
if (npc1->index == SUB_THREEWAY
&& p1->subs[SUB_THREEWAY].inthreeway
&& p1->subs[SUB_REAL].owner
&& p1->subs[SUB_REAL].inthreeway
&& ast_channel_state(p1->subs[SUB_REAL].owner) == AST_STATE_RINGING) {
ast_debug(2,
"Playing ringback on %d/%d(%s) since %d/%d(%s) is in a ringing three-way\n",
p0->channel, npc0->index, ast_channel_name(c0),
p1->channel, npc1->index, ast_channel_name(c1));
tone_zone_play_tone(p0->subs[npc0->index].dfd, DAHDI_TONE_RINGTONE);
npc1->state = ast_channel_state(p1->subs[SUB_REAL].owner);
} else {
ast_debug(2, "Stopping tones on %d/%d(%s) talking to %d/%d(%s)\n",
p0->channel, npc0->index, ast_channel_name(c0),
p1->channel, npc1->index, ast_channel_name(c1));
tone_zone_play_tone(p0->subs[npc0->index].dfd, -1);
}
if (npc0->index == SUB_THREEWAY
&& p0->subs[SUB_THREEWAY].inthreeway
&& p0->subs[SUB_REAL].owner
&& p0->subs[SUB_REAL].inthreeway
&& ast_channel_state(p0->subs[SUB_REAL].owner) == AST_STATE_RINGING) {
ast_debug(2,
"Playing ringback on %d/%d(%s) since %d/%d(%s) is in a ringing three-way\n",
p1->channel, npc1->index, ast_channel_name(c1),
p0->channel, npc0->index, ast_channel_name(c0));
tone_zone_play_tone(p1->subs[npc1->index].dfd, DAHDI_TONE_RINGTONE);
npc0->state = ast_channel_state(p0->subs[SUB_REAL].owner);
} else {
ast_debug(2, "Stopping tones on %d/%d(%s) talking to %d/%d(%s)\n",
p1->channel, npc1->index, ast_channel_name(c1),
p0->channel, npc0->index, ast_channel_name(c0));
tone_zone_play_tone(p1->subs[npc1->index].dfd, -1);
}
if (npc0->index == SUB_REAL && npc1->index == SUB_REAL) {
if (!p0->echocanbridged || !p1->echocanbridged) {
/* Disable echo cancellation if appropriate */
dahdi_ec_disable(p0);
dahdi_ec_disable(p1);
}
}
dahdi_master_slave_link(slave, master);
master->inconference = inconf;
} else if (!nothing_ok) {
ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n",
p0->channel, subnames[npc0->index],
p1->channel, subnames[npc1->index]);
}
dahdi_conf_update(p0);
dahdi_conf_update(p1);
ast_channel_unlock(c0);
ast_channel_unlock(c1);
/* Native bridge failed */
if ((!master || !slave) && !nothing_ok) {
ast_mutex_unlock(&p0->lock);
ast_mutex_unlock(&p1->lock);
return -1;
}
if (npc0->index == SUB_REAL) {
dahdi_dtmf_detect_disable(p0);
}
if (npc1->index == SUB_REAL) {
dahdi_dtmf_detect_disable(p1);
}
ast_mutex_unlock(&p0->lock);
ast_mutex_unlock(&p1->lock);
tech_pvt = bridge->tech_pvt;
tech_pvt->master = master;
tech_pvt->slave = slave;
ast_debug(2, "Start native bridging %s and %s\n",
ast_channel_name(c0), ast_channel_name(c1));
#if defined(HAVE_PRI) && defined(PRI_2BCT)
if (!tech_pvt->tried_trombone_removal) {
tech_pvt->tried_trombone_removal = 1;
if (p0->pri && p0->pri == p1->pri && p0->pri->transfer) {
q931_call *q931_c0;
q931_call *q931_c1;
/* Try to eliminate the tromboned call. */
ast_mutex_lock(&p0->pri->lock);
ast_assert(dahdi_sig_pri_lib_handles(p0->sig));
ast_assert(dahdi_sig_pri_lib_handles(p1->sig));
q931_c0 = ((struct sig_pri_chan *) (p0->sig_pvt))->call;
q931_c1 = ((struct sig_pri_chan *) (p1->sig_pvt))->call;
if (q931_c0 && q931_c1) {
pri_channel_bridge(q931_c0, q931_c1);
ast_debug(2, "Attempt to eliminate tromboned call with %s and %s\n",
ast_channel_name(c0), ast_channel_name(c1));
}
ast_mutex_unlock(&p0->pri->lock);
}
}
#endif /* defined(HAVE_PRI) && defined(PRI_2BCT) */
return 0;
}
/*!
* \internal
* \brief Request to start native bridging activity.
* \since 12.0.0
*
* \param bridge What to operate upon.
*
* \return Nothing
*
* \note On entry, bridge may or may not already be locked.
* However, it can be accessed as if it were locked.
*/
static void native_request_start(struct ast_bridge *bridge)
{
struct native_pvt_bridge *tech_pvt;
struct ast_bridge_channel *cur;
ast_assert(bridge->tech_pvt != NULL);
tech_pvt = bridge->tech_pvt;
if (bridge->num_channels != 2 || !tech_pvt->saw_start || tech_pvt->connected) {
return;
}
AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
if (cur->suspended || !cur->tech_pvt) {
return;
}
}
/* Actually try starting the native bridge. */
if (native_start(bridge)) {
return;
}
tech_pvt->connected = 1;
}
/*!
* \internal
* \brief Request a bridge technology instance start operations.
* \since 12.0.0
*
* \retval 0 on success
* \retval -1 on failure
*
* \note On entry, bridge may or may not already be locked.
* However, it can be accessed as if it were locked.
*/
static int native_bridge_start(struct ast_bridge *bridge)
{
struct native_pvt_bridge *tech_pvt;
ast_assert(bridge->tech_pvt != NULL);
tech_pvt = bridge->tech_pvt;
tech_pvt->saw_start = 1;
native_request_start(bridge);
return 0;
}
/*!
* \internal
* \brief Request a bridge technology instance stop in preparation for being destroyed.
* \since 12.0.0
*
* \note On entry, bridge is already locked.
*/
static void native_bridge_stop(struct ast_bridge *bridge)
{
struct native_pvt_bridge *tech_pvt;
tech_pvt = bridge->tech_pvt;
if (!tech_pvt) {
return;
}
tech_pvt->saw_start = 0;
native_request_stop(bridge);
}
/*!
* \internal
* \brief Add a channel to a bridging technology instance for a bridge.
* \since 12.0.0
*
* \retval 0 on success
* \retval -1 on failure
*
* \note On entry, bridge is already locked.
*/
static int native_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
{
struct native_pvt_chan *tech_pvt;
struct ast_channel *c0;
struct ast_channel *c1;
ast_assert(!bridge_channel->tech_pvt);
tech_pvt = ast_calloc(1, sizeof(*tech_pvt));
if (!tech_pvt) {
return -1;
}
bridge_channel->tech_pvt = tech_pvt;
native_request_start(bridge);
/*
* Make the channels compatible in case the native bridge did
* not start for some reason and we need to fallback to 1-1
* bridging.
*/
c0 = AST_LIST_FIRST(&bridge->channels)->chan;
c1 = AST_LIST_LAST(&bridge->channels)->chan;
if (c0 == c1) {
return 0;
}
return ast_channel_make_compatible(c0, c1);
}
/*!
* \internal
* \brief Remove a channel from a bridging technology instance for a bridge.
* \since 12.0.0
*
* \note On entry, bridge is already locked.
*/
static void native_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
{
struct native_pvt_chan *tech_pvt;
native_request_stop(bridge);
tech_pvt = bridge_channel->tech_pvt;
bridge_channel->tech_pvt = NULL;
ast_free(tech_pvt);
}
/*!
* \internal
* \brief Suspend a channel on a bridging technology instance for a bridge.
* \since 12.0.0
*
* \note On entry, bridge is already locked.
*/
static void native_bridge_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
{
native_request_stop(bridge);
}
/*!
* \internal
* \brief Unsuspend a channel on a bridging technology instance for a bridge.
* \since 12.0.0
*
* \note On entry, bridge is already locked.
*/
static void native_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
{
native_request_start(bridge);
}
/*!
* \internal
* \brief Check if channel is compatible.
* \since 12.0.0
*
* \param bridge_channel Is this channel compatible.
*
* \retval TRUE if channel is compatible with native DAHDI bridge.
*/
static int native_bridge_is_capable(struct ast_bridge_channel *bridge_channel)
{
struct ast_channel *chan = bridge_channel->chan;
struct dahdi_pvt *pvt;
int is_capable;
if (ao2_container_count(bridge_channel->features->dtmf_hooks)) {
ast_debug(2, "Channel '%s' has DTMF hooks.\n", ast_channel_name(chan));
return 0;
}
ast_channel_lock(chan);
if (dahdi_tech != ast_channel_tech(chan)) {
ast_debug(2, "Channel '%s' is not %s.\n",
ast_channel_name(chan), dahdi_tech->type);
ast_channel_unlock(chan);
return 0;
}
if (ast_channel_has_audio_frame_or_monitor(chan)) {
ast_debug(2, "Channel '%s' has an active monitor, audiohook, or framehook.\n",
ast_channel_name(chan));
ast_channel_unlock(chan);
return 0;
}
pvt = ast_channel_tech_pvt(chan);
if (!pvt || !pvt->sig) {
/* No private; or signaling is for a pseudo channel. */
ast_channel_unlock(chan);
return 0;
}
is_capable = 1;
ast_mutex_lock(&pvt->lock);
if (pvt->callwaiting && pvt->callwaitingcallerid) {
/*
* Call Waiting Caller ID requires DTMF detection to know if it
* can send the CID spill.
*/
ast_debug(2, "Channel '%s' has call waiting caller ID enabled.\n",
ast_channel_name(chan));
is_capable = 0;
}
ast_mutex_unlock(&pvt->lock);
ast_channel_unlock(chan);
return is_capable;
}
/*!
* \internal
* \brief Check if a bridge is compatible with the bridging technology.
* \since 12.0.0
*
* \retval 0 if not compatible
* \retval non-zero if compatible
*
* \note On entry, bridge may or may not already be locked.
* However, it can be accessed as if it were locked.
*/
static int native_bridge_compatible(struct ast_bridge *bridge)
{
struct ast_bridge_channel *cur;
/* We require two channels before even considering native bridging. */
if (bridge->num_channels != 2) {
ast_debug(1, "Bridge %s: Cannot use native DAHDI. Must have two channels.\n",
bridge->uniqueid);
return 0;
}
AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
if (!native_bridge_is_capable(cur)) {
ast_debug(1, "Bridge %s: Cannot use native DAHDI. Channel '%s' not compatible.\n",
bridge->uniqueid, ast_channel_name(cur->chan));
return 0;
}
}
return -1;
}
/*!
* \internal
* \brief Check if something changed on the channel.
* \since 12.0.0
*
* \param bridge_channel What to operate upon.
*
* \retval 0 Nothing changed.
* \retval -1 Something changed.
*
* \note On entry, bridge_channel->bridge is already locked.
*/
static int native_chan_changed(struct ast_bridge_channel *bridge_channel)
{
struct native_pvt_chan *tech_pvt;
struct ast_channel *chan;
struct dahdi_pvt *pvt;
int idx = -1;
ast_assert(bridge_channel->tech_pvt != NULL);
tech_pvt = bridge_channel->tech_pvt;
chan = bridge_channel->chan;
ast_channel_lock(chan);
pvt = ast_channel_tech_pvt(chan);
if (tech_pvt->pvt == pvt) {
idx = dahdi_get_index(chan, pvt, 1);
}
ast_channel_unlock(chan);
if (/* Did chan get masqueraded or PRI change associated B channel? */
tech_pvt->pvt != pvt
/* Did the pvt active owner change? */
|| tech_pvt->owner != pvt->owner
/* Did the pvt three way call status change? */
|| tech_pvt->inthreeway != pvt->subs[SUB_REAL].inthreeway
/* Did the owner index change? */
|| tech_pvt->index != idx
/*
* Did chan file descriptor change? (This seems redundant with
* masquerade and active owner change checks.)
*/
|| tech_pvt->fd0 != ast_channel_fd(chan, 0)
/* Did chan state change? i.e. Did it stop ringing? */
|| (pvt->subs[SUB_REAL].owner
&& tech_pvt->state > -1
&& tech_pvt->state != ast_channel_state(pvt->subs[SUB_REAL].owner))) {
return -1;
}
return 0;
}
/*!
* \internal
* \brief Check if something changed on the bridge channels.
* \since 12.0.0
*
* \param bridge What to operate upon.
*
* \retval 0 Nothing changed.
* \retval -1 Something changed.
*
* \note On entry, bridge is already locked.
*/
static int native_bridge_changed(struct ast_bridge *bridge)
{
struct ast_bridge_channel *cur;
AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
if (native_chan_changed(cur)) {
ast_debug(1, "Bridge %s: Something changed on channel '%s'.\n",
bridge->uniqueid, ast_channel_name(cur->chan));
return -1;
}
}
return 0;
}
/*!
* \internal
* \brief Write a frame into the bridging technology instance for a bridge.
* \since 12.0.0
*
* \note The bridge must be tolerant of bridge_channel being NULL.
*
* \retval 0 Frame accepted into the bridge.
* \retval -1 Frame needs to be deferred.
*
* \note On entry, bridge is already locked.
*/
static int native_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
{
struct native_pvt_bridge *tech_pvt;
/*
* When we are not native bridged by DAHDI, we are like a normal
* 1-1 bridge.
*/
ast_assert(bridge->tech_pvt != NULL);
/* Recheck native bridging validity. */
tech_pvt = bridge->tech_pvt;
switch (frame->frametype) {
case AST_FRAME_VOICE:
case AST_FRAME_VIDEO:
if (!tech_pvt->connected) {
/* Don't try to start native mode on media frames. */
break;
}
if (native_bridge_changed(bridge)) {
native_request_stop(bridge);
native_request_start(bridge);
if (!tech_pvt->connected) {
break;
}
}
/*
* Native bridge handles voice frames in hardware. However, it
* also passes the frames up to Asterisk anyway. Discard the
* media frames.
*/
return 0;
default:
if (!tech_pvt->connected) {
native_request_start(bridge);
break;
}
if (native_bridge_changed(bridge)) {
native_request_stop(bridge);
native_request_start(bridge);
}
break;
}
return ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
}
static struct ast_bridge_technology native_bridge = {
.name = "native_dahdi",
.capabilities = AST_BRIDGE_CAPABILITY_NATIVE,
.preference = AST_BRIDGE_PREFERENCE_BASE_NATIVE,
.create = native_bridge_create,
.start = native_bridge_start,
.stop = native_bridge_stop,
.destroy = native_bridge_destroy,
.join = native_bridge_join,
.leave = native_bridge_leave,
.suspend = native_bridge_suspend,
.unsuspend = native_bridge_unsuspend,
.compatible = native_bridge_compatible,
.write = native_bridge_write,
};
/*!
* \internal
* \brief Destroy the DAHDI native bridge support.
* \since 12.0.0
*
* \return Nothing
*/
void dahdi_native_unload(void)
{
ast_bridge_technology_unregister(&native_bridge);
ao2_cleanup(native_bridge.format_capabilities);
}
/*!
* \internal
* \brief Initialize the DAHDI native bridge support.
* \since 12.0.0
*
* \retval 0 on success.
* \retval -1 on error.
*/
int dahdi_native_load(struct ast_module *mod, const struct ast_channel_tech *tech)
{
dahdi_tech = tech;
native_bridge.format_capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
if (!native_bridge.format_capabilities) {
return -1;
}
/*
* This is used to make channels compatible with the bridge
* itself not with each other.
*/
ast_format_cap_append(native_bridge.format_capabilities, ast_format_slin, 0);
ast_format_cap_append(native_bridge.format_capabilities, ast_format_ulaw, 0);
ast_format_cap_append(native_bridge.format_capabilities, ast_format_alaw, 0);
return __ast_bridge_technology_register(&native_bridge, mod);
}