[asterisk-commits] file: branch file/rtp_engine r130782 - in /team/file/rtp_engine: channels/ in...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Mon Jul 14 12:30:36 CDT 2008
Author: file
Date: Mon Jul 14 12:30:35 2008
New Revision: 130782
URL: http://svn.digium.com/view/asterisk?view=rev&rev=130782
Log:
Add core RTP engine components for doing both local and remote bridging.
Modified:
team/file/rtp_engine/channels/chan_sip.c
team/file/rtp_engine/include/asterisk/rtp_engine.h
team/file/rtp_engine/main/rtp_engine.c
Modified: team/file/rtp_engine/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/file/rtp_engine/channels/chan_sip.c?view=diff&rev=130782&r1=130781&r2=130782
==============================================================================
--- team/file/rtp_engine/channels/chan_sip.c (original)
+++ team/file/rtp_engine/channels/chan_sip.c Mon Jul 14 12:30:35 2008
@@ -2165,7 +2165,6 @@
static enum st_mode st_get_mode(struct sip_pvt *);
static struct sip_st_dlg* sip_st_alloc(struct sip_pvt *const p);
-
/*! \brief Definition of this channel for PBX channel registration */
static const struct ast_channel_tech sip_tech = {
.type = "SIP",
@@ -2187,7 +2186,7 @@
.fixup = sip_fixup, /* called with chan locked */
.send_digit_begin = sip_senddigit_begin, /* called with chan unlocked */
.send_digit_end = sip_senddigit_end,
-// .bridge = ast_rtp_bridge, /* XXX chan unlocked ? */
+ .bridge = ast_rtp_instance_bridge, /* XXX chan unlocked ? */
// .early_bridge = ast_rtp_early_bridge,
.send_text = sip_sendtext, /* called with chan locked */
.func_channel_read = acf_channel_read,
@@ -21586,6 +21585,173 @@
}
}
+static enum ast_rtp_glue_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
+{
+ struct sip_pvt *p = NULL;
+ enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_LOCAL;
+
+ if (!(p = chan->tech_pvt)) {
+ return AST_RTP_GLUE_RESULT_FORBID;
+ }
+
+ sip_pvt_lock(p);
+ if (!(p->rtp)) {
+ sip_pvt_unlock(p);
+ return AST_RTP_GLUE_RESULT_FORBID;
+ }
+
+ *instance = p->rtp;
+
+ if (!ast_test_flag(&p->flags[0], SIP_CAN_REINVITE_NAT)) {
+ res = AST_RTP_GLUE_RESULT_LOCAL;
+ } else if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE)) {
+ res = AST_RTP_GLUE_RESULT_REMOTE;
+ } else if (ast_test_flag(&global_jbconf, AST_JB_FORCED)) {
+ res = AST_RTP_GLUE_RESULT_FORBID;
+ }
+
+ sip_pvt_unlock(p);
+
+ return res;
+}
+
+static enum ast_rtp_glue_result sip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
+{
+ struct sip_pvt *p = NULL;
+ enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_FORBID;
+
+ if (!(p = chan->tech_pvt)) {
+ return AST_RTP_GLUE_RESULT_FORBID;
+ }
+
+ sip_pvt_lock(p);
+ if (!(p->vrtp)) {
+ sip_pvt_unlock(p);
+ return AST_RTP_GLUE_RESULT_FORBID;
+ }
+
+ *instance = p->vrtp;
+
+ if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE)) {
+ res = AST_RTP_GLUE_RESULT_REMOTE;
+ }
+
+ sip_pvt_unlock(p);
+
+ return res;
+}
+
+static enum ast_rtp_glue_result sip_get_trtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
+{
+ struct sip_pvt *p = NULL;
+ enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_FORBID;
+
+ if (!(p = chan->tech_pvt)) {
+ return AST_RTP_GLUE_RESULT_FORBID;
+ }
+
+ sip_pvt_lock(p);
+ if (!(p->trtp)) {
+ sip_pvt_unlock(p);
+ return AST_RTP_GLUE_RESULT_FORBID;
+ }
+
+ *instance = p->trtp;
+
+ if (ast_test_flag(&p->flags[0], SIP_CAN_REINVITE)) {
+ res = AST_RTP_GLUE_RESULT_REMOTE;
+ }
+
+ sip_pvt_unlock(p);
+
+ return res;
+}
+
+static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_rtp_instance *vinstance, struct ast_rtp_instance *tinstance, int codecs, int nat_active)
+{
+ struct sip_pvt *p;
+ int changed = 0;
+
+ p = chan->tech_pvt;
+ if (!p)
+ return -1;
+
+ /* Disable early RTP bridge */
+ if (chan->_state != AST_STATE_UP && !global_directrtpsetup) /* We are in early state */
+ return 0;
+
+ sip_pvt_lock(p);
+ if (p->alreadygone) {
+ /* If we're destroyed, don't bother */
+ sip_pvt_unlock(p);
+ return 0;
+ }
+
+ /* if this peer cannot handle reinvites of the media stream to devices
+ that are known to be behind a NAT, then stop the process now
+ */
+ if (nat_active && !ast_test_flag(&p->flags[0], SIP_CAN_REINVITE_NAT)) {
+ sip_pvt_unlock(p);
+ return 0;
+ }
+
+ if (instance) {
+ changed |= ast_rtp_instance_get_remote_address(instance, &p->redirip);
+ } else if (p->redirip.sin_addr.s_addr || ntohs(p->redirip.sin_port) != 0) {
+ memset(&p->redirip, 0, sizeof(p->redirip));
+ changed = 1;
+ }
+ if (vinstance) {
+ changed |= ast_rtp_instance_get_remote_address(vinstance, &p->vredirip);
+ } else if (p->vredirip.sin_addr.s_addr || ntohs(p->vredirip.sin_port) != 0) {
+ memset(&p->vredirip, 0, sizeof(p->vredirip));
+ changed = 1;
+ }
+ if (tinstance) {
+ changed |= ast_rtp_instance_get_remote_address(tinstance, &p->tredirip);
+ } else if (p->tredirip.sin_addr.s_addr || ntohs(p->tredirip.sin_port) != 0) {
+ memset(&p->tredirip, 0, sizeof(p->tredirip));
+ changed = 1;
+ }
+ if (codecs && (p->redircodecs != codecs)) {
+ p->redircodecs = codecs;
+ changed = 1;
+ }
+ if (changed && !ast_test_flag(&p->flags[0], SIP_GOTREFER) && !ast_test_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER)) {
+ if (chan->_state != AST_STATE_UP) { /* We are in early state */
+ if (p->do_history)
+ append_history(p, "ExtInv", "Initial invite sent with remote bridge proposal.");
+ ast_debug(1, "Early remote bridge setting SIP '%s' - Sending media to %s\n", p->callid, ast_inet_ntoa(instance ? p->redirip.sin_addr : p->ourip.sin_addr));
+ } else if (!p->pendinginvite) { /* We are up, and have no outstanding invite */
+ ast_debug(3, "Sending reinvite on SIP '%s' - It's audio soon redirected to IP %s\n", p->callid, ast_inet_ntoa(instance ? p->redirip.sin_addr : p->ourip.sin_addr));
+ transmit_reinvite_with_sdp(p, FALSE, FALSE);
+ } else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
+ ast_debug(3, "Deferring reinvite on SIP '%s' - It's audio will be redirected to IP %s\n", p->callid, ast_inet_ntoa(instance ? p->redirip.sin_addr : p->ourip.sin_addr));
+ /* We have a pending Invite. Send re-invite when we're done with the invite */
+ ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
+ }
+ }
+ /* Reset lastrtprx timer */
+ p->lastrtprx = p->lastrtptx = time(NULL);
+ sip_pvt_unlock(p);
+ return 0;
+}
+
+static int sip_get_codec(struct ast_channel *chan)
+{
+ struct sip_pvt *p = chan->tech_pvt;
+ return p->peercapability ? p->peercapability : p->capability;
+}
+
+static struct ast_rtp_glue sip_rtp_glue = {
+ .type = "SIP",
+ .get_rtp_info = sip_get_rtp_peer,
+ .get_vrtp_info = sip_get_vrtp_peer,
+ .get_trtp_info = sip_get_trtp_peer,
+ .update_peer = sip_set_rtp_peer,
+ .get_codec = sip_get_codec,
+};
+
static char *synopsis_dtmfmode = "Change the dtmfmode for a SIP call";
static char *descrip_dtmfmode = " SIPDtmfMode(inband|info|rfc2833): Changes the dtmfmode for a SIP call\n";
static char *app_dtmfmode = "SIPDtmfMode";
@@ -21939,6 +22105,9 @@
/* Tell the UDPTL subdriver that we're here */
ast_udptl_proto_register(&sip_udptl);
+ /* Tell the RTP engine about our RTP glue */
+ ast_rtp_glue_register(&sip_rtp_glue);
+
/* Register dialplan applications */
ast_register_application(app_dtmfmode, sip_dtmfmode, synopsis_dtmfmode, descrip_dtmfmode);
ast_register_application(app_sipaddheader, sip_addheader, synopsis_sipaddheader, descrip_sipaddheader);
@@ -22009,6 +22178,9 @@
/* Disconnect from UDPTL */
ast_udptl_proto_unregister(&sip_udptl);
+ /* Disconnect from RTP engine */
+ ast_rtp_glue_unregister(&sip_rtp_glue);
+
/* Unregister AMI actions */
ast_manager_unregister("SIPpeers");
ast_manager_unregister("SIPshowpeer");
Modified: team/file/rtp_engine/include/asterisk/rtp_engine.h
URL: http://svn.digium.com/view/asterisk/team/file/rtp_engine/include/asterisk/rtp_engine.h?view=diff&rev=130782&r1=130781&r2=130782
==============================================================================
--- team/file/rtp_engine/include/asterisk/rtp_engine.h (original)
+++ team/file/rtp_engine/include/asterisk/rtp_engine.h Mon Jul 14 12:30:35 2008
@@ -50,7 +50,7 @@
enum ast_rtp_glue_result {
AST_RTP_GLUE_RESULT_FORBID = 0,
- AST_RTP_GLUE_RESULT_DIRECT,
+ AST_RTP_GLUE_RESULT_REMOTE,
AST_RTP_GLUE_RESULT_LOCAL,
};
@@ -92,6 +92,7 @@
int (*red_init)(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations); /*!< Callback for initializing RED support */
int (*red_buffer)(struct ast_rtp_instance *instance, struct ast_frame *frame); /*!< Callback for buffering a frame over RED */
struct ast_frame *(*read)(struct ast_rtp_instance *instance, int rtcp); /*!< Callback for reading a frame in */
+ int (*local_bridge)(struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1); /*!< Locally bridge two RTP instances together */
AST_RWLIST_ENTRY(ast_rtp_engine) entry; /*!< Linked list information */
};
@@ -115,7 +116,14 @@
};
struct ast_rtp_glue {
- const char *name; /*!< Name of the channel driver this glue works with */
+ const char *type;
+ struct ast_module *mod;
+ enum ast_rtp_glue_result (*get_rtp_info)(struct ast_channel *chan, struct ast_rtp_instance **instance);
+ enum ast_rtp_glue_result (*get_vrtp_info)(struct ast_channel *chan, struct ast_rtp_instance **instance);
+ enum ast_rtp_glue_result (*get_trtp_info)(struct ast_channel *chan, struct ast_rtp_instance **instance);
+ int (*update_peer)(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_rtp_instance *vinstance, struct ast_rtp_instance *tinstance, int codecs, int nat_active);
+ int (*get_codec)(struct ast_channel *chan);
+ AST_RWLIST_ENTRY(ast_rtp_glue) entry; /*!< Linked list information */
};
#define ast_rtp_engine_register(engine) ast_rtp_engine_register2(engine, ast_module_info->self)
@@ -138,6 +146,27 @@
* \retval -1 failure
*/
int ast_rtp_engine_unregister(struct ast_rtp_engine *engine);
+
+#define ast_rtp_glue_register(glue) ast_rtp_glue_register2(glue, ast_module_info->self)
+
+/*! \brief Register RTP glue
+ *
+ * \param glue The glue to register
+ * \param module Module that the RTP glue is part of
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module);
+
+/*! \brief Unregister RTP glue
+ *
+ * \param glue The glue to unregister
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+int ast_rtp_glue_unregister(struct ast_rtp_glue *glue);
/*! \brief Create a new RTP instance
*
@@ -189,6 +218,9 @@
*/
int ast_rtp_instance_set_remote_address(struct ast_rtp_instance *instance, struct sockaddr_in *address);
+/*! \brief Get the address of the remote endpoint that we are sending RTP to */
+int ast_rtp_instance_get_remote_address(struct ast_rtp_instance *instance, struct sockaddr_in *address);
+
/*! \brief Set the value of an RTP instance property
*
* \param instance The RTP instance to set the property on
@@ -264,6 +296,9 @@
/*! \brief Get the file descriptor for an RTP session (or RTCP) */
int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp);
+/*! \brief Get the RTP glue that binds a channel to the RTP engine */
+struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type);
+
/*! \brief Bridge two channels that use RTP instances */
enum ast_bridge_result ast_rtp_instance_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms);
Modified: team/file/rtp_engine/main/rtp_engine.c
URL: http://svn.digium.com/view/asterisk/team/file/rtp_engine/main/rtp_engine.c?view=diff&rev=130782&r1=130781&r2=130782
==============================================================================
--- team/file/rtp_engine/main/rtp_engine.c (original)
+++ team/file/rtp_engine/main/rtp_engine.c Mon Jul 14 12:30:35 2008
@@ -34,6 +34,9 @@
/* List of RTP engines that are currently registered */
static AST_RWLIST_HEAD_STATIC(engines, ast_rtp_engine);
+
+/* List of RTP glues */
+static AST_RWLIST_HEAD_STATIC(glues, ast_rtp_glue);
/* The following array defines the MIME Media type (and subtype) for each
of our codecs, or RTP-specific data type. */
@@ -180,6 +183,62 @@
return current_engine ? 0 : -1;
}
+int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
+{
+ struct ast_rtp_glue *current_glue = NULL;
+
+ if (ast_strlen_zero(glue->type)) {
+ return -1;
+ }
+
+ glue->mod = module;
+
+ AST_RWLIST_WRLOCK(&glues);
+
+ AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
+ if (!strcasecmp(current_glue->type, glue->type)) {
+ ast_log(LOG_WARNING, "RTP glue with the name '%s' has already been registered.\n", glue->type);
+ AST_RWLIST_UNLOCK(&glues);
+ return -1;
+ }
+ }
+
+ AST_RWLIST_INSERT_TAIL(&glues, glue, entry);
+
+ AST_RWLIST_UNLOCK(&glues);
+
+ if (option_verbose > 1) {
+ ast_verbose(VERBOSE_PREFIX_2 "Registered RTP glue '%s'\n", glue->type);
+ }
+
+ return 0;
+}
+
+int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
+{
+ struct ast_rtp_glue *current_glue = NULL;
+
+ AST_RWLIST_WRLOCK(&glues);
+
+ AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
+ if (current_glue == glue) {
+ break;
+ }
+ }
+
+ if (current_glue) {
+ AST_LIST_REMOVE(&glues, current_glue, entry);
+ }
+
+ AST_RWLIST_UNLOCK(&glues);
+
+ if (current_glue && option_verbose > 1) {
+ ast_verbose(VERBOSE_PREFIX_2 "Unregistered RTP glue '%s'\n", glue->type);
+ }
+
+ return current_glue ? 0 : -1;
+}
+
struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name, struct ast_rtp_glue *glue, struct sched_context *sched, struct sockaddr_in *sin)
{
struct ast_rtp_instance *instance = NULL;
@@ -268,6 +327,18 @@
if (instance->engine->remote_address_set) {
instance->engine->remote_address_set(instance, address);
+ }
+
+ return 0;
+}
+
+int ast_rtp_instance_get_remote_address(struct ast_rtp_instance *instance, struct sockaddr_in *address)
+{
+ if ((address->sin_family != AF_INET) ||
+ (address->sin_port != instance->remote_address.sin_port) ||
+ (address->sin_addr.s_addr != instance->remote_address.sin_addr.s_addr)) {
+ memcpy(address, &instance->remote_address, sizeof(address));
+ return 1;
}
return 0;
@@ -560,9 +631,489 @@
return instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
}
+struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
+{
+ struct ast_rtp_glue *glue = NULL;
+
+ AST_RWLIST_RDLOCK(&glues);
+
+ AST_RWLIST_TRAVERSE(&glues, glue, entry) {
+ if (!strcasecmp(glue->type, type)) {
+ break;
+ }
+ }
+
+ AST_RWLIST_UNLOCK(&glues);
+
+ return glue;
+}
+
+static enum ast_bridge_result local_bridge_loop(struct ast_channel *c0, struct ast_channel *c1, struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1, int timeoutms, int flags, struct ast_frame **fo, struct ast_channel **rc, void *pvt0, void *pvt1)
+{
+ enum ast_bridge_result res = AST_BRIDGE_FAILED;
+ struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
+ struct ast_frame *fr = NULL;
+
+ /* Start locally bridging both instances */
+ if (instance0->engine->local_bridge && instance0->engine->local_bridge(instance0, instance1)) {
+ ast_debug(3, "Failed to locally bridge %s to %s, backing out.\n", c0->name, c1->name);
+ ast_channel_unlock(c0);
+ ast_channel_unlock(c1);
+ return AST_BRIDGE_FAILED_NOWARN;
+ }
+ if (instance1->engine->local_bridge && instance1->engine->local_bridge(instance1, instance0)) {
+ ast_debug(3, "Failed to locally bridge %s to %s, backing out.\n", c1->name, c0->name);
+ if (instance0->engine->local_bridge) {
+ instance0->engine->local_bridge(instance0, NULL);
+ }
+ ast_channel_unlock(c0);
+ ast_channel_unlock(c1);
+ return AST_BRIDGE_FAILED_NOWARN;
+ }
+
+ ast_channel_unlock(c0);
+ ast_channel_unlock(c1);
+
+ ast_poll_channel_add(c0, c1);
+
+ instance0->bridged = instance1;
+ instance1->bridged = instance0;
+
+ /* Hop into a loop waiting for a frame from either channel */
+ cs[0] = c0;
+ cs[1] = c1;
+ cs[2] = NULL;
+ for (;;) {
+ /* If the underlying formats have changed force this bridge to break */
+ if ((c0->rawreadformat != c1->rawwriteformat) || (c1->rawreadformat != c0->rawwriteformat)) {
+ ast_debug(3, "rtp-engine-local-bridge: Oooh, formats changed, backing out\n");
+ res = AST_BRIDGE_FAILED_NOWARN;
+ break;
+ }
+ /* Check if anything changed */
+ if ((c0->tech_pvt != pvt0) ||
+ (c1->tech_pvt != pvt1) ||
+ (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
+ (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks)) {
+ ast_debug(3, "rtp-engine-local-bridge: Oooh, something is weird, backing out\n");
+ /* If a masquerade needs to happen we have to try to read in a frame so that it actually happens. Without this we risk being called again and going into a loop */
+ if ((c0->masq || c0->masqr) && (fr = ast_read(c0)))
+ ast_frfree(fr);
+ if ((c1->masq || c1->masqr) && (fr = ast_read(c1)))
+ ast_frfree(fr);
+ res = AST_BRIDGE_RETRY;
+ break;
+ }
+ /* Wait on a channel to feed us a frame */
+ if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
+ if (!timeoutms) {
+ res = AST_BRIDGE_RETRY;
+ break;
+ }
+ if (option_debug > 2)
+ ast_log(LOG_NOTICE, "rtp-engine-local-bridge: Ooh, empty read...\n");
+ if (ast_check_hangup(c0) || ast_check_hangup(c1))
+ break;
+ continue;
+ }
+ /* Read in frame from channel */
+ fr = ast_read(who);
+ other = (who == c0) ? c1 : c0;
+ /* Depending on the frame we may need to break out of our bridge */
+ if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
+ ((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) |
+ ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)))) {
+ /* Record received frame and who */
+ *fo = fr;
+ *rc = who;
+ ast_debug(3, "rtp-engine-local-bridge: Ooh, got a %s\n", fr ? "digit" : "hangup");
+ res = AST_BRIDGE_COMPLETE;
+ break;
+ } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
+ if ((fr->subclass == AST_CONTROL_HOLD) ||
+ (fr->subclass == AST_CONTROL_UNHOLD) ||
+ (fr->subclass == AST_CONTROL_VIDUPDATE) ||
+ (fr->subclass == AST_CONTROL_T38) ||
+ (fr->subclass == AST_CONTROL_SRCUPDATE)) {
+ /* If we are going on hold, then break callback mode and P2P bridging */
+ if (fr->subclass == AST_CONTROL_HOLD) {
+ if (instance0->engine->local_bridge) {
+ instance0->engine->local_bridge(instance0, NULL);
+ }
+ if (instance1->engine->local_bridge) {
+ instance1->engine->local_bridge(instance1, NULL);
+ }
+ instance0->bridged = NULL;
+ instance1->bridged = NULL;
+ } else if (fr->subclass == AST_CONTROL_UNHOLD) {
+ if (instance0->engine->local_bridge) {
+ instance0->engine->local_bridge(instance0, instance1);
+ }
+ if (instance1->engine->local_bridge) {
+ instance1->engine->local_bridge(instance1, instance0);
+ }
+ instance0->bridged = instance1;
+ instance1->bridged = instance0;
+ }
+ ast_indicate_data(other, fr->subclass, fr->data.ptr, fr->datalen);
+ ast_frfree(fr);
+ } else {
+ *fo = fr;
+ *rc = who;
+ ast_debug(3, "rtp-engine-local-bridge: Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass, who->name);
+ res = AST_BRIDGE_COMPLETE;
+ break;
+ }
+ } else {
+ if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
+ (fr->frametype == AST_FRAME_DTMF_END) ||
+ (fr->frametype == AST_FRAME_VOICE) ||
+ (fr->frametype == AST_FRAME_VIDEO) ||
+ (fr->frametype == AST_FRAME_IMAGE) ||
+ (fr->frametype == AST_FRAME_HTML) ||
+ (fr->frametype == AST_FRAME_MODEM) ||
+ (fr->frametype == AST_FRAME_TEXT)) {
+ ast_write(other, fr);
+ }
+
+ ast_frfree(fr);
+ }
+ /* Swap priority */
+ cs[2] = cs[0];
+ cs[0] = cs[1];
+ cs[1] = cs[2];
+ }
+
+ /* Stop locally bridging both instances */
+ if (instance0->engine->local_bridge) {
+ instance0->engine->local_bridge(instance0, NULL);
+ }
+ if (instance1->engine->local_bridge) {
+ instance1->engine->local_bridge(instance1, NULL);
+ }
+
+ instance0->bridged = NULL;
+ instance1->bridged = NULL;
+
+ ast_poll_channel_del(c0, c1);
+
+ return res;
+}
+
+static enum ast_bridge_result remote_bridge_loop(struct ast_channel *c0, struct ast_channel *c1, struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1, struct ast_rtp_instance *vinstance0, struct ast_rtp_instance *vinstance1, struct ast_rtp_instance *tinstance0, struct ast_rtp_instance *tinstance1, struct ast_rtp_glue *glue0, struct ast_rtp_glue *glue1, int codec0, int codec1, int timeoutms, int flags, struct ast_frame **fo, struct ast_channel **rc, void *pvt0, void *pvt1)
+{
+ enum ast_bridge_result res = AST_BRIDGE_FAILED;
+ struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
+ int oldcodec0 = codec0, oldcodec1 = codec1;
+ struct sockaddr_in ac1 = {0,}, vac1 = {0,}, tac1 = {0,}, ac0 = {0,}, vac0 = {0,}, tac0 = {0,};
+ struct sockaddr_in t1 = {0,}, vt1 = {0,}, tt1 = {0,}, t0 = {0,}, vt0 = {0,}, tt0 = {0,};
+ struct ast_frame *fr = NULL;
+
+ /* Test the first channel */
+ if (!(glue0->update_peer(c0, instance1, vinstance1, tinstance1, codec1, 0))) {
+ ast_rtp_instance_get_remote_address(instance1, &ac1);
+ if (vinstance1) {
+ ast_rtp_instance_get_remote_address(vinstance1, &vac1);
+ }
+ if (tinstance1) {
+ ast_rtp_instance_get_remote_address(tinstance1, &tac1);
+ }
+ } else {
+ ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
+ }
+
+ /* Test the second channel */
+ if (!(glue1->update_peer(c1, instance0, vinstance0, tinstance0, codec0, 0))) {
+ ast_rtp_instance_get_remote_address(instance0, &ac0);
+ if (vinstance0) {
+ ast_rtp_instance_get_remote_address(instance0, &vac0);
+ }
+ if (tinstance0) {
+ ast_rtp_instance_get_remote_address(instance0, &tac0);
+ }
+ } else {
+ ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c1->name, c0->name);
+ }
+
+ ast_channel_unlock(c0);
+ ast_channel_unlock(c1);
+
+ ast_poll_channel_add(c0, c1);
+
+ instance0->bridged = instance1;
+ instance1->bridged = instance0;
+
+ /* Go into a loop handling any stray frames that may come in */
+ cs[0] = c0;
+ cs[1] = c1;
+ cs[2] = NULL;
+ for (;;) {
+ /* Check if anything changed */
+ if ((c0->tech_pvt != pvt0) ||
+ (c1->tech_pvt != pvt1) ||
+ (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
+ (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks)) {
+ ast_debug(1, "Oooh, something is weird, backing out\n");
+ res = AST_BRIDGE_RETRY;
+ break;
+ }
+
+ /* Check if they have changed their address */
+ ast_rtp_instance_get_remote_address(instance1, &t1);
+ if (vinstance1) {
+ ast_rtp_instance_get_remote_address(vinstance1, &vt1);
+ }
+ if (tinstance1) {
+ ast_rtp_instance_get_remote_address(tinstance1, &tt1);
+ }
+ if (glue1->get_codec) {
+ codec1 = glue1->get_codec(c1);
+ }
+
+ ast_rtp_instance_get_remote_address(instance0, &t0);
+ if (vinstance0) {
+ ast_rtp_instance_get_remote_address(vinstance0, &vt0);
+ }
+ if (tinstance0) {
+ ast_rtp_instance_get_remote_address(tinstance0, &tt0);
+ }
+ if (glue0->get_codec) {
+ codec0 = glue0->get_codec(c0);
+ }
+
+ if ((inaddrcmp(&t1, &ac1)) ||
+ (vinstance1 && inaddrcmp(&vt1, &vac1)) ||
+ (tinstance1 && inaddrcmp(&tt1, &tac1)) ||
+ (codec1 != oldcodec1)) {
+ ast_debug(2, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
+ c1->name, ast_inet_ntoa(t1.sin_addr), ntohs(t1.sin_port), codec1);
+ ast_debug(2, "Oooh, '%s' changed end vaddress to %s:%d (format %d)\n",
+ c1->name, ast_inet_ntoa(vt1.sin_addr), ntohs(vt1.sin_port), codec1);
+ ast_debug(2, "Oooh, '%s' changed end taddress to %s:%d (format %d)\n",
+ c1->name, ast_inet_ntoa(tt1.sin_addr), ntohs(tt1.sin_port), codec1);
+ ast_debug(2, "Oooh, '%s' was %s:%d/(format %d)\n",
+ c1->name, ast_inet_ntoa(ac1.sin_addr), ntohs(ac1.sin_port), oldcodec1);
+ ast_debug(2, "Oooh, '%s' was %s:%d/(format %d)\n",
+ c1->name, ast_inet_ntoa(vac1.sin_addr), ntohs(vac1.sin_port), oldcodec1);
+ ast_debug(2, "Oooh, '%s' was %s:%d/(format %d)\n",
+ c1->name, ast_inet_ntoa(tac1.sin_addr), ntohs(tac1.sin_port), oldcodec1);
+ if (glue0->update_peer(c0, t1.sin_addr.s_addr ? instance1 : NULL, vt1.sin_addr.s_addr ? vinstance1 : NULL, tt1.sin_addr.s_addr ? tinstance1 : NULL, codec1, 0))
+ ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
+ memcpy(&ac1, &t1, sizeof(ac1));
+ memcpy(&vac1, &vt1, sizeof(vac1));
+ memcpy(&tac1, &tt1, sizeof(tac1));
+ oldcodec1 = codec1;
+ }
+ if ((inaddrcmp(&t0, &ac0)) ||
+ (vinstance0 && inaddrcmp(&vt0, &vac0)) ||
+ (tinstance0 && inaddrcmp(&tt0, &tac0))) {
+ ast_debug(2, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
+ c0->name, ast_inet_ntoa(t0.sin_addr), ntohs(t0.sin_port), codec0);
+ ast_debug(2, "Oooh, '%s' was %s:%d/(format %d)\n",
+ c0->name, ast_inet_ntoa(ac0.sin_addr), ntohs(ac0.sin_port), oldcodec0);
+ if (glue1->update_peer(c1, t0.sin_addr.s_addr ? instance0 : NULL, vt0.sin_addr.s_addr ? vinstance0 : NULL, tt0.sin_addr.s_addr ? tinstance0 : NULL, codec0, 0))
+ ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
+ memcpy(&ac0, &t0, sizeof(ac0));
+ memcpy(&vac0, &vt0, sizeof(vac0));
+ memcpy(&tac0, &tt0, sizeof(tac0));
+ oldcodec0 = codec0;
+ }
+
+ /* Wait for frame to come in on the channels */
+ if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
+ if (!timeoutms) {
+ res = AST_BRIDGE_RETRY;
+ break;
+ }
+ ast_debug(1, "Ooh, empty read...\n");
+ if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
+ break;
+ }
+ continue;
+ }
+ fr = ast_read(who);
+ other = (who == c0) ? c1 : c0;
+ if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
+ (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
+ ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
+ /* Break out of bridge */
+ *fo = fr;
+ *rc = who;
+ ast_debug(1, "Oooh, got a %s\n", fr ? "digit" : "hangup");
+ res = AST_BRIDGE_COMPLETE;
+ break;
+ } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
+ if ((fr->subclass == AST_CONTROL_HOLD) ||
+ (fr->subclass == AST_CONTROL_UNHOLD) ||
+ (fr->subclass == AST_CONTROL_VIDUPDATE) ||
+ (fr->subclass == AST_CONTROL_T38) ||
+ (fr->subclass == AST_CONTROL_SRCUPDATE)) {
+ if (fr->subclass == AST_CONTROL_HOLD) {
+ /* If we someone went on hold we want the other side to reinvite back to us */
+ if (who == c0)
+ glue1->update_peer(c1, NULL, NULL, NULL, 0, 0);
+ else
+ glue0->update_peer(c0, NULL, NULL, NULL, 0, 0);
+ } else if (fr->subclass == AST_CONTROL_UNHOLD) {
+ /* If they went off hold they should go back to being direct */
+ if (who == c0)
+ glue1->update_peer(c1, instance0, vinstance0, tinstance0, codec0, 0);
+ else
+ glue0->update_peer(c0, instance1, vinstance1, tinstance1, codec1, 0);
+ }
+ /* Update local address information */
+ ast_rtp_instance_get_remote_address(instance0, &t0);
+ memcpy(&ac0, &t0, sizeof(ac0));
+ ast_rtp_instance_get_remote_address(instance1, &t1);
+ memcpy(&ac1, &t1, sizeof(ac1));
+ /* Update codec information */
+ if (glue0->get_codec && c0->tech_pvt) {
+ oldcodec0 = codec0 = glue0->get_codec(c0);
+ }
+ if (glue1->get_codec && c1->tech_pvt) {
+ oldcodec1 = codec1 = glue1->get_codec(c1);
+ }
+ ast_indicate_data(other, fr->subclass, fr->data.ptr, fr->datalen);
+ ast_frfree(fr);
+ } else {
+ *fo = fr;
+ *rc = who;
+ ast_debug(1, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass, who->name);
+ return AST_BRIDGE_COMPLETE;
+ }
+ } else {
+ if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
+ (fr->frametype == AST_FRAME_DTMF_END) ||
+ (fr->frametype == AST_FRAME_VOICE) ||
+ (fr->frametype == AST_FRAME_VIDEO) ||
+ (fr->frametype == AST_FRAME_IMAGE) ||
+ (fr->frametype == AST_FRAME_HTML) ||
+ (fr->frametype == AST_FRAME_MODEM) ||
+ (fr->frametype == AST_FRAME_TEXT)) {
+ ast_write(other, fr);
+ }
+ ast_frfree(fr);
+ }
+ /* Swap priority */
+ cs[2] = cs[0];
+ cs[0] = cs[1];
+ cs[1] = cs[2];
+ }
+
+ if (glue0->update_peer(c0, NULL, NULL, NULL, 0, 0)) {
+ ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
+ }
+ if (glue1->update_peer(c1, NULL, NULL, NULL, 0, 0)) {
+ ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
+ }
+
+ instance0->bridged = NULL;
+ instance1->bridged = NULL;
+
+ ast_poll_channel_del(c0, c1);
+
+ return res;
+}
+
enum ast_bridge_result ast_rtp_instance_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms)
{
- return AST_BRIDGE_FAILED;
+ struct ast_rtp_instance *instance0, *instance1, *vinstance0, *vinstance1, *tinstance0, *tinstance1;
+ struct ast_rtp_glue *glue0, *glue1;
+ enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID, text_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
+ enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID, text_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
+ enum ast_bridge_result res = AST_BRIDGE_FAILED;
+ int codec0 = 0, codec1 = 0;
+
+ /* Lock both channels so we can look for the glue that binds them together */
+ ast_channel_lock(c0);
+ while (ast_channel_trylock(c1)) {
+ ast_channel_unlock(c0);
+ usleep(1);
+ ast_channel_lock(c0);
+ }
+
+ /* Ensure neither channel got hungup during lock avoidance */
+ if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
+ ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", c0->name, c1->name);
+ ast_channel_unlock(c0);
+ ast_channel_unlock(c1);
+ return AST_BRIDGE_FAILED;
+ }
+
+ /* Grab glue that binds each channel to something using the RTP engine */
+ if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
+ ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
+ ast_channel_unlock(c0);
+ ast_channel_unlock(c1);
+ return AST_BRIDGE_FAILED;
+ }
+
+ audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
+ video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
+ text_glue0_res = glue0->get_trtp_info ? glue0->get_trtp_info(c0, &tinstance0) : AST_RTP_GLUE_RESULT_FORBID;
+
+ audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
+ video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
+ text_glue1_res = glue1->get_trtp_info ? glue1->get_trtp_info(c1, &tinstance1) : AST_RTP_GLUE_RESULT_FORBID;
+
+ /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
+ if (video_glue0_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue0_res != AST_RTP_GLUE_RESULT_REMOTE)) {
+ audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
+ }
+ if (video_glue1_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue1_res != AST_RTP_GLUE_RESULT_REMOTE)) {
+ audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
+ }
+
+ /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
+ if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID || audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
+ ast_channel_unlock(c0);
+ ast_channel_unlock(c1);
+ return AST_BRIDGE_FAILED_NOWARN;
+ }
+
+ /* If both sides are not using the same method of DTMF transmission
+ * (ie: one is RFC2833, other is INFO... then we can not do direct media.
+ * --------------------------------------------------
+ * | DTMF Mode | HAS_DTMF | Accepts Begin Frames |
+ * |-----------|------------|-----------------------|
+ * | Inband | False | True |
+ * | RFC2833 | True | True |
+ * | SIP INFO | False | False |
+ * --------------------------------------------------
+ */
+ if ((instance0->properties[AST_RTP_PROPERTY_DTMF] != instance1->properties[AST_RTP_PROPERTY_DTMF]) || (!c0->tech->send_digit_begin != !c1->tech->send_digit_begin)) {
+ ast_channel_unlock(c0);
+ ast_channel_unlock(c1);
+ return AST_BRIDGE_FAILED_NOWARN;
+ }
+
+ /* If we have gotten to a local bridge make sure that both sides have the same local bridge callback */
+ if ((audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) && (instance0->engine->local_bridge != instance1->engine->local_bridge)) {
+ ast_channel_unlock(c0);
+ ast_channel_unlock(c1);
+ return AST_BRIDGE_FAILED_NOWARN;
+ }
+
+ /* Make sure that codecs match */
+ codec0 = glue0->get_codec ? glue0->get_codec(c0) : 0;
+ codec1 = glue1->get_codec ? glue1->get_codec(c1) : 0;
+ if (codec0 && codec1 && !(codec0 & codec1)) {
+ ast_debug(3, "Channel codec0 = %d is not codec1 = %d, cannot native bridge in RTP.\n", codec0, codec1);
+ ast_channel_unlock(c0);
+ ast_channel_unlock(c1);
+ return AST_BRIDGE_FAILED_NOWARN;
+ }
+
+ /* Depending on the end result for bridging either do a local bridge or remote bridge */
+ if (audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) {
+ ast_verb(3, "Locally bridging %s and %s\n", c0->name, c1->name);
+ res = local_bridge_loop(c0, c1, instance0, instance1, timeoutms, flags, fo, rc, c0->tech_pvt, c1->tech_pvt);
+ } else {
+ ast_verb(3, "Remotely bridging %s and %s\n", c0->name, c1->name);
+ res = remote_bridge_loop(c0, c1, instance0, instance1, vinstance0, vinstance1, tinstance0, tinstance1, glue0, glue1, codec0, codec1, timeoutms, flags, fo, rc, c0->tech_pvt, c1->tech_pvt);
+ }
+
+ return res;
}
int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
More information about the asterisk-commits
mailing list