[asterisk-commits] file: branch file/bridging r115590 - /team/file/bridging/main/bridging.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri May 9 16:25:33 CDT 2008


Author: file
Date: Fri May  9 16:25:32 2008
New Revision: 115590

URL: http://svn.digium.com/view/asterisk?view=rev&rev=115590
Log:
Add contents of ast_bridge_merge back in.

Modified:
    team/file/bridging/main/bridging.c

Modified: team/file/bridging/main/bridging.c
URL: http://svn.digium.com/view/asterisk/team/file/bridging/main/bridging.c?view=diff&rev=115590&r1=115589&r2=115590
==============================================================================
--- team/file/bridging/main/bridging.c (original)
+++ team/file/bridging/main/bridging.c Fri May  9 16:25:32 2008
@@ -441,7 +441,7 @@
 }
 
 /*! \brief Perform the smart bridge operation. Basically sees if a new bridge technology should be used instead of the current one. */
-static int smart_bridge_operation(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
+static int smart_bridge_operation(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, int count)
 {
 	int new_capabilities = 0;
 	struct ast_bridge_technology *new_technology = NULL, *old_technology = bridge->technology;
@@ -453,14 +453,14 @@
 
 	/* Based on current feature determine whether we want to change bridge technologies or not */
 	if (bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_1TO1MIX) {
-		if (bridge->num <= 2) {
-			ast_debug(1, "Bridge %p channel count (%d) is within limits for bridge technology %s, not performing smart bridge operation.\n", bridge, bridge->num, bridge->technology->name);
+		if (count <= 2) {
+			ast_debug(1, "Bridge %p channel count (%d) is within limits for bridge technology %s, not performing smart bridge operation.\n", bridge, count, bridge->technology->name);
 			return 0;
 		}
 		new_capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX;
 	} else if (bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) {
-		if (bridge->num > 2) {
-			ast_debug(1, "Bridge %p channel count (%d) is within limits for bridge technology %s, not performing smart bridge operation.\n", bridge, bridge->num, bridge->technology->name);
+		if (count > 2) {
+			ast_debug(1, "Bridge %p channel count (%d) is within limits for bridge technology %s, not performing smart bridge operation.\n", bridge, count, bridge->technology->name);
 			return 0;
 		}
 		new_capabilities = AST_BRIDGE_CAPABILITY_1TO1MIX;
@@ -675,7 +675,7 @@
 		bridge_channel->swap = NULL;
 	} else if (ast_test_flag(&bridge_channel->bridge->feature_flags, AST_BRIDGE_FLAG_SMART)) {
 		/* Perform the smart bridge operation, basically see if we need to move around between technologies */
-		smart_bridge_operation(bridge_channel->bridge, bridge_channel);
+		smart_bridge_operation(bridge_channel->bridge, bridge_channel, bridge_channel->bridge->num);
 	}
 	
 	/* Tell the bridge technology we are joining so they set us up */
@@ -722,7 +722,7 @@
 
 	/* Perform the smart bridge operation if needed since a channel has left */
 	if (ast_test_flag(&bridge_channel->bridge->feature_flags, AST_BRIDGE_FLAG_SMART)) {
-		smart_bridge_operation(bridge_channel->bridge, NULL);
+		smart_bridge_operation(bridge_channel->bridge, NULL, bridge_channel->bridge->num);
 	}
 
 	ao2_unlock(bridge_channel->bridge);
@@ -899,6 +899,82 @@
 
 int ast_bridge_merge(struct ast_bridge *bridge0, struct ast_bridge *bridge1)
 {
+	struct ast_bridge_channel *bridge_channel = NULL;
+
+	ao2_lock(bridge0);
+	ao2_lock(bridge1);
+
+	/* If the first bridge currently has 2 channels and is not capable of becoming a multimixing bridge we can not merge */
+	if ((bridge0->num + bridge1->num) > 2 && (!(bridge0->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) && !ast_test_flag(&bridge0->feature_flags, AST_BRIDGE_FLAG_SMART))) {
+		ao2_unlock(bridge0);
+		ao2_unlock(bridge1);
+		ast_debug(1, "Can't merge bridge %p into bridge %p, multimix is needed and it could not be acquired.\n", bridge1, bridge0);
+		return -1;
+	}
+
+	ast_debug(1, "Merging channels from bridge %p into bridge %p\n", bridge1, bridge0);
+
+	/* Perform smart bridge operation on bridge we are merging into so it can change bridge technology if needed */
+	if (smart_bridge_operation(bridge0, NULL, bridge0->num + bridge1->num)) {
+		ao2_unlock(bridge0);
+		ao2_unlock(bridge1);
+		ast_debug(1, "Can't merge bridge %p into bridge %p, tried to perform smart bridge operation and failed.\n", bridge1, bridge0);
+		return -1;
+	}
+
+	/* If a thread is currently executing on bridge1 tell it to stop */
+	if (bridge1->thread) {
+		ast_debug(1, "Telling bridge thread on bridge %p to stop as it is being merged into %p\n", bridge1, bridge0);
+		bridge1->thread = AST_PTHREADT_STOP;
+	}
+
+	/* Tell each bridge that something is going to happen */
+	ast_bridge_rebuild(bridge0);
+	ast_bridge_rebuild(bridge1);
+
+	/* Move channels from bridge1 over to bridge0 */
+	while ((bridge_channel = AST_LIST_REMOVE_HEAD(&bridge1->channels, entry))) {
+		/* Tell the technology handling bridge1 that the bridge channel is leaving */
+		if (bridge1->technology->leave) {
+			ast_debug(1, "Giving bridge technology %s notification that %p is leaving bridge %p\n", bridge1->technology->name, bridge_channel, bridge1);
+			if (bridge1->technology->leave(bridge1, bridge_channel)) {
+				ast_debug(1, "Bridge technology %s failed to allow %p to leave bridge %p\n", bridge1->technology->name, bridge_channel, bridge1);
+			}
+		}
+
+		/* Drop channel count and reference count on the bridge they are leaving */
+		bridge1->num--;
+		ao2_ref(bridge1, -1);
+
+		/* Now add them into the bridge they are joining, increase channel count, and bump up reference count */
+		bridge_channel->bridge = bridge0;
+		AST_LIST_INSERT_TAIL(&bridge0->channels, bridge_channel, entry);
+		bridge0->num++;
+		ao2_ref(bridge0, +1);
+
+		/* Make the channel compatible with the new bridge it is joining or else formats would go amuck */
+		bridge_make_compatible(bridge0, bridge_channel);
+
+		/* Tell the technology handling bridge0 that the bridge channel is joining */
+		if (bridge0->technology->join) {
+			ast_debug(1, "Giving bridge technology %s notification that %p is joining bridge %p\n", bridge0->technology->name, bridge_channel, bridge0);
+			if (bridge0->technology->join(bridge0, bridge_channel)) {
+				ast_debug(1, "Bridge technology %s failed to join %p to bridge %p\n", bridge0->technology->name, bridge_channel, bridge0);
+			}
+		}
+
+		/* Poke the bridge channel, this will cause it to wake up and execute the proper threading model for the new bridge it is in */
+		pthread_kill(bridge_channel->thread, SIGURG);
+		ast_mutex_lock(&bridge_channel->lock);
+		ast_cond_signal(&bridge_channel->cond);
+		ast_mutex_unlock(&bridge_channel->lock);
+	}
+
+	ast_debug(1, "Merged channels from bridge %p into bridge %p\n", bridge1, bridge0);
+
+	ao2_unlock(bridge0);
+	ao2_unlock(bridge1);
+
 	return 0;
 }
 




More information about the asterisk-commits mailing list