[asterisk-commits] file: branch file/bridge_native r385989 - /team/file/bridge_native/bridges/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Apr 18 05:51:06 CDT 2013


Author: file
Date: Thu Apr 18 05:51:01 2013
New Revision: 385989

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=385989
Log:
Add a frame hook which returns media to Asterisk and re-establishes native bridging on certain control frames.

Modified:
    team/file/bridge_native/bridges/bridge_native_rtp.c

Modified: 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=diff&rev=385989&r1=385988&r2=385989
==============================================================================
--- team/file/bridge_native/bridges/bridge_native_rtp.c (original)
+++ team/file/bridge_native/bridges/bridge_native_rtp.c Thu Apr 18 05:51:01 2013
@@ -45,6 +45,34 @@
 #include "asterisk/bridging_technology.h"
 #include "asterisk/frame.h"
 #include "asterisk/rtp_engine.h"
+
+/*! \brief Forward declarations for frame hook usage */
+static int native_rtp_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel);
+static void native_rtp_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel);
+
+/*! \brief Internal structure which contains information about bridged RTP channels */
+struct native_rtp_bridge_data {
+	/*! \brief Framehook used to intercept certain control frames */
+	int id;
+};
+
+/*! \brief Frame hook that is called to intercept hold/unhold */
+static struct ast_frame *native_rtp_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data)
+{
+	if (!f || (event != AST_FRAMEHOOK_EVENT_WRITE)) {
+		return f;
+	}
+
+	/* It's safe for NULL to be passed to both of these, bridge_channel isn't used at all */
+	if (f->subclass.integer == AST_CONTROL_HOLD) {
+		native_rtp_bridge_leave(ast_channel_internal_bridge(chan), NULL);
+	} else if ((f->subclass.integer == AST_CONTROL_UNHOLD) ||
+				(f->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
+		native_rtp_bridge_join(ast_channel_internal_bridge(chan), NULL);
+	}
+
+	return f;
+}
 
 /*! \brief Internal helper function which checks whether the channels are compatible with our native bridging */
 static int native_rtp_bridge_capable(struct ast_channel *chan)
@@ -197,10 +225,46 @@
 	return 1;
 }
 
+/*! \brief Helper function which adds frame hook to bridge channel */
+static int native_rtp_bridge_framehook_attach(struct ast_bridge_channel *bridge_channel)
+{
+	struct native_rtp_bridge_data *data = ao2_alloc(sizeof(*data), NULL);
+	struct ast_framehook_interface hook = {
+		.version = AST_FRAMEHOOK_INTERFACE_VERSION,
+		.event_cb = native_rtp_framehook,
+	};
+
+	if (!data) {
+		return -1;
+	}
+
+	if (!(data->id = ast_framehook_attach(bridge_channel->chan, &hook)) < 0) {
+		ao2_cleanup(data);
+		return -1;
+	}
+
+	bridge_channel->bridge_pvt = data;
+
+	return 0;
+}
+
+/*! \brief Helper function which removes frame hook from bridge channel */
+static void native_rtp_bridge_framehook_detach(struct ast_bridge_channel *bridge_channel)
+{
+	RAII_VAR(struct native_rtp_bridge_data *, data, bridge_channel->bridge_pvt, ao2_cleanup);
+
+	if (!data) {
+		return;
+	}
+
+	ast_framehook_detach(bridge_channel->chan, data->id);
+	bridge_channel->bridge_pvt = NULL;
+}
+
 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_bridge_channel *c0 = AST_LIST_FIRST(&bridge->channels);
+	struct ast_bridge_channel *c1 = AST_LIST_LAST(&bridge->channels);
 	enum ast_rtp_glue_result native_type;
 	struct ast_rtp_glue *glue0, *glue1;
 	struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL, *vinstance0 = NULL;
@@ -208,13 +272,22 @@
 	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);
 
-	native_type = native_rtp_bridge_get(c0, c1, &glue0, &glue1, &instance0, &instance1, &vinstance0, &vinstance1);
+	if (native_rtp_bridge_framehook_attach(c0)) {
+		return -1;
+	}
+
+	if (native_rtp_bridge_framehook_attach(c1)) {
+		native_rtp_bridge_framehook_detach(c0);
+		return -1;
+	}
+
+	native_type = native_rtp_bridge_get(c0->chan, c1->chan, &glue0, &glue1, &instance0, &instance1, &vinstance0, &vinstance1);
 
 	if (glue0->get_codec) {
-		glue0->get_codec(c0, cap0);
+		glue0->get_codec(c0->chan, cap0);
 	}
 	if (glue1->get_codec) {
-		glue1->get_codec(c1, cap1);
+		glue1->get_codec(c1->chan, cap1);
 	}
 
 	if (native_type == AST_RTP_GLUE_RESULT_LOCAL) {
@@ -227,8 +300,8 @@
 		ast_rtp_instance_set_bridged(instance0, instance1);
 		ast_rtp_instance_set_bridged(instance1, instance0);
 	} else {
-		glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0);
-		glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0);
+		glue0->update_peer(c0->chan, instance1, vinstance1, tinstance1, cap1, 0);
+		glue1->update_peer(c1->chan, instance0, vinstance0, tinstance0, cap0, 0);
 	}
 
 	return 0;
@@ -241,13 +314,16 @@
 
 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_bridge_channel *c0 = AST_LIST_FIRST(&bridge->channels);
+	struct ast_bridge_channel *c1 = AST_LIST_LAST(&bridge->channels);
 	enum ast_rtp_glue_result native_type;
 	struct ast_rtp_glue *glue0, *glue1;
 	struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL, *vinstance0 = NULL, *vinstance1 = NULL;
 
-	native_type = native_rtp_bridge_get(c0, c1, &glue0, &glue1, &instance0, &instance1, &vinstance0, &vinstance1);
+	native_rtp_bridge_framehook_detach(c0);
+	native_rtp_bridge_framehook_detach(c1);
+
+	native_type = native_rtp_bridge_get(c0->chan, c1->chan, &glue0, &glue1, &instance0, &instance1, &vinstance0, &vinstance1);
 
 	if (native_type == AST_RTP_GLUE_RESULT_LOCAL) {
 		if (ast_rtp_instance_get_engine(instance0)->local_bridge) {
@@ -259,8 +335,8 @@
 		ast_rtp_instance_set_bridged(instance0, instance1);
 		ast_rtp_instance_set_bridged(instance1, instance0);
 	} else {
-		glue0->update_peer(c0, NULL, NULL, NULL, NULL, 0);
-		glue1->update_peer(c1, NULL, NULL, NULL, NULL, 0);
+		glue0->update_peer(c0->chan, NULL, NULL, NULL, NULL, 0);
+		glue1->update_peer(c1->chan, NULL, NULL, NULL, NULL, 0);
 	}
 }
 




More information about the asterisk-commits mailing list