[asterisk-commits] file: branch file/bridge_native r385908 - in /team/file/bridge_native: bridge...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Apr 16 13:55:51 CDT 2013


Author: file
Date: Tue Apr 16 13:55:47 2013
New Revision: 385908

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=385908
Log:
Add a module which does native RTP bridging. Right now it's hardcoded for remote and doesn't do DTMF checks. One step at a time!

Added:
    team/file/bridge_native/bridges/bridge_native_rtp.c   (with props)
Modified:
    team/file/bridge_native/channels/chan_sip.c

Added: team/file/bridge_native/bridges/bridge_native_rtp.c
URL: http://svnview.digium.com/svn/asterisk/team/file/bridge_native/bridges/bridge_native_rtp.c?view=auto&rev=385908
==============================================================================
--- team/file/bridge_native/bridges/bridge_native_rtp.c (added)
+++ team/file/bridge_native/bridges/bridge_native_rtp.c Tue Apr 16 13:55:47 2013
@@ -1,0 +1,238 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at 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 RTP bridging module
+ *
+ * \author Joshua Colp <jcolp at digium.com>
+ *
+ * \ingroup bridges
+ */
+
+/*** MODULEINFO
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "asterisk/module.h"
+#include "asterisk/channel.h"
+#include "asterisk/bridging.h"
+#include "asterisk/bridging_technology.h"
+#include "asterisk/frame.h"
+#include "asterisk/rtp_engine.h"
+
+/*! \brief Structure which contains information about native bridged RTP capable channel */
+struct native_rtp_bridge {
+	/*! \brief What type of bridge is in progress */
+	enum ast_rtp_glue_result type;
+};
+
+static int native_rtp_bridge_compatible(struct ast_bridge *bridge)
+{
+	struct ast_channel *c0 = AST_LIST_FIRST(&bridge->channels)->chan;
+	struct ast_channel *c1 = AST_LIST_LAST(&bridge->channels)->chan;
+	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;
+	enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
+	struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL, *vinstance0 = NULL, *vinstance1 = NULL;
+	RAII_VAR(struct ast_format_cap *, cap0, ast_format_cap_alloc_nolock(), ast_format_cap_destroy);
+	RAII_VAR(struct ast_format_cap *, cap1, ast_format_cap_alloc_nolock(), ast_format_cap_destroy);
+	int read_ptime0, read_ptime1, write_ptime0, write_ptime1;
+
+	/* We require two channels before even considering native bridging */
+	if (c0 == c1) {
+		return 0;
+	}
+
+	if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) ||
+		!(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
+		return 0;
+	}
+
+	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;
+
+	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;
+
+	/* Apply any limitations on direct media bridging that may be present */
+	if (audio_glue0_res == audio_glue1_res && audio_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
+		if (glue0->allow_rtp_remote && !(glue0->allow_rtp_remote(c0, instance1))) {
+			/* If the allow_rtp_remote indicates that remote isn't allowed, revert to local bridge */
+			audio_glue0_res = audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
+		} else if (glue1->allow_rtp_remote && !(glue1->allow_rtp_remote(c1, instance0))) {
+			audio_glue0_res = audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
+		}
+	}
+	if (video_glue0_res == video_glue1_res && video_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
+		if (glue0->allow_vrtp_remote && !(glue0->allow_vrtp_remote(c0, instance1))) {
+			/* if the allow_vrtp_remote indicates that remote isn't allowed, revert to local bridge */
+			video_glue0_res = video_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
+		} else if (glue1->allow_vrtp_remote && !(glue1->allow_vrtp_remote(c1, instance0))) {
+			video_glue0_res = video_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
+		}
+	}
+
+	/* 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) {
+		return 0;
+	}
+
+	/* Make sure that codecs match */
+	if (glue0->get_codec) {
+		glue0->get_codec(c0, cap0);
+	}
+	if (glue1->get_codec) {
+		glue1->get_codec(c1, cap1);
+	}
+	if (!ast_format_cap_is_empty(cap0) && !ast_format_cap_is_empty(cap1) && !ast_format_cap_has_joint(cap0, cap1)) {
+		char tmp0[256] = { 0, }, tmp1[256] = { 0, };
+
+		ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
+			ast_getformatname_multiple(tmp0, sizeof(tmp0), cap0),
+			ast_getformatname_multiple(tmp1, sizeof(tmp1), cap1));
+		return 0;
+	}
+
+	read_ptime0 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance0)->pref, ast_channel_rawreadformat(c0))).cur_ms;
+	read_ptime1 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance1)->pref, ast_channel_rawreadformat(c1))).cur_ms;
+	write_ptime0 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance0)->pref, ast_channel_rawwriteformat(c0))).cur_ms;
+	write_ptime1 = (ast_codec_pref_getsize(&ast_rtp_instance_get_codecs(instance1)->pref, ast_channel_rawwriteformat(c1))).cur_ms;
+
+	if (read_ptime0 != write_ptime1 || read_ptime1 != write_ptime0) {
+		ast_debug(1, "Packetization differs between RTP streams (%d != %d or %d != %d). Cannot native bridge in RTP\n",
+				read_ptime0, write_ptime1, read_ptime1, write_ptime0);
+		return 0;
+	}
+
+	return 1;
+}
+
+static int native_rtp_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
+{
+	struct ast_channel *c0 = AST_LIST_FIRST(&bridge->channels)->chan;
+	struct ast_channel *c1 = AST_LIST_LAST(&bridge->channels)->chan;
+	struct ast_rtp_glue *glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type);
+	struct ast_rtp_glue *glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type);
+	enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_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;
+	struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL, *vinstance0 = NULL;
+	struct ast_rtp_instance *vinstance1 = NULL, *tinstance0 = NULL, *tinstance1 = NULL;
+	RAII_VAR(struct ast_format_cap *, cap0, ast_format_cap_alloc_nolock(), ast_format_cap_destroy);
+	RAII_VAR(struct ast_format_cap *, cap1, ast_format_cap_alloc_nolock(), ast_format_cap_destroy);
+
+	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;
+	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;
+
+	if (glue0->get_codec) {
+		glue0->get_codec(c0, cap0);
+	}
+	if (glue1->get_codec) {
+		glue1->get_codec(c1, cap1);
+	}
+
+	glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0);
+	glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0);
+
+	ast_log(LOG_NOTICE, "Res = %d %d %d %d\n", audio_glue0_res, audio_glue1_res, video_glue0_res, video_glue1_res);
+
+	return 0;
+}
+
+static void native_rtp_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
+{
+	native_rtp_bridge_join(bridge, bridge_channel);
+}
+
+static void native_rtp_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
+{
+	struct ast_channel *c0 = AST_LIST_FIRST(&bridge->channels)->chan;
+	struct ast_channel *c1 = AST_LIST_LAST(&bridge->channels)->chan;
+	struct ast_rtp_glue *glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type);
+	struct ast_rtp_glue *glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type);
+
+	glue0->update_peer(c0, NULL, NULL, NULL, 0, 0);
+	glue1->update_peer(c1, NULL, NULL, NULL, 0, 0);
+}
+
+static int native_rtp_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
+{
+	struct ast_bridge_channel *other = AST_LIST_FIRST(&bridge->channels);
+
+	/* Find the channel we actually want to write to */
+	if (other == bridge_channel) {
+		other = AST_LIST_LAST(&bridge->channels);
+	}
+
+	/* The bridging core takes care of freeing the passed in frame. */
+	ast_bridge_channel_queue_frame(other, frame);
+
+	return 0;
+}
+
+static struct ast_bridge_technology native_rtp_bridge = {
+	.name = "native_rtp",
+	.capabilities = AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE,
+	.preference = AST_BRIDGE_PREFERENCE_HIGH,
+	.join = native_rtp_bridge_join,
+	.unsuspend = native_rtp_bridge_unsuspend,
+	.leave = native_rtp_bridge_leave,
+	.suspend = native_rtp_bridge_leave,
+	.write = native_rtp_bridge_write,
+	.compatible = native_rtp_bridge_compatible,
+};
+
+static int unload_module(void)
+{
+	ast_format_cap_destroy(native_rtp_bridge.format_capabilities);
+	return ast_bridge_technology_unregister(&native_rtp_bridge);
+}
+
+static int load_module(void)
+{
+	if (!(native_rtp_bridge.format_capabilities = ast_format_cap_alloc())) {
+		return AST_MODULE_LOAD_DECLINE;
+	}
+	ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_AUDIO);
+	ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_VIDEO);
+	ast_format_cap_add_all_by_type(native_rtp_bridge.format_capabilities, AST_FORMAT_TYPE_TEXT);
+
+	return ast_bridge_technology_register(&native_rtp_bridge);
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Native RTP bridging module");

Propchange: team/file/bridge_native/bridges/bridge_native_rtp.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/file/bridge_native/bridges/bridge_native_rtp.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/file/bridge_native/bridges/bridge_native_rtp.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/file/bridge_native/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/file/bridge_native/channels/chan_sip.c?view=diff&rev=385908&r1=385907&r2=385908
==============================================================================
--- team/file/bridge_native/channels/chan_sip.c (original)
+++ team/file/bridge_native/channels/chan_sip.c Tue Apr 16 13:55:47 2013
@@ -32853,6 +32853,7 @@
 	/* Disable early RTP bridge  */
 	if ((instance || vinstance || tinstance) &&
 		!ast_bridged_channel(chan) &&
+		!ast_channel_internal_bridge(chan) &&
 		!sip_cfg.directrtpsetup) {
 		sip_pvt_unlock(p);
 		ast_channel_unlock(chan);




More information about the asterisk-commits mailing list