[asterisk-commits] file: branch file/bridging r62365 - in /team/file/bridging: ./ apps/ bridges/...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Mon Apr 30 07:16:19 MST 2007


Author: file
Date: Mon Apr 30 09:16:18 2007
New Revision: 62365

URL: http://svn.digium.com/view/asterisk?view=rev&rev=62365
Log:
Hey look! It's source code for bridging stuffz.

Added:
    team/file/bridging/apps/app_bridgetest.c   (with props)
    team/file/bridging/bridges/
    team/file/bridging/bridges/Makefile   (with props)
    team/file/bridging/bridges/bridge_simple.c   (with props)
    team/file/bridging/include/asterisk/bridging.h   (with props)
    team/file/bridging/main/bridging.c   (with props)
Modified:
    team/file/bridging/Makefile
    team/file/bridging/main/Makefile

Modified: team/file/bridging/Makefile
URL: http://svn.digium.com/view/asterisk/team/file/bridging/Makefile?view=diff&rev=62365&r1=62364&r2=62365
==============================================================================
--- team/file/bridging/Makefile (original)
+++ team/file/bridging/Makefile Mon Apr 30 09:16:18 2007
@@ -242,7 +242,7 @@
 
 ASTCFLAGS+=$(MALLOC_DEBUG)$(BUSYDETECT)$(OPTIONS)
 
-MOD_SUBDIRS:=res channels pbx apps codecs formats cdr funcs main
+MOD_SUBDIRS:=res channels pbx apps codecs formats cdr bridges funcs main
 OTHER_SUBDIRS:=utils agi
 SUBDIRS:=$(OTHER_SUBDIRS) $(MOD_SUBDIRS)
 SUBDIRS_INSTALL:=$(SUBDIRS:%=%-install)

Added: team/file/bridging/apps/app_bridgetest.c
URL: http://svn.digium.com/view/asterisk/team/file/bridging/apps/app_bridgetest.c?view=auto&rev=62365
==============================================================================
--- team/file/bridging/apps/app_bridgetest.c (added)
+++ team/file/bridging/apps/app_bridgetest.c Mon Apr 30 09:16:18 2007
@@ -1,0 +1,131 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2007, 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 Bridging API Test Application
+ *
+ * \author Joshua Colp <jcolp at digium.com>
+ * 
+ * This is a test application for the new bridging API.
+ * \ingroup applications
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include "asterisk/file.h"
+#include "asterisk/logger.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/module.h"
+#include "asterisk/lock.h"
+#include "asterisk/app.h"
+#include "asterisk/dial.h"
+#include "asterisk/bridging.h"
+
+static char *app = "BridgeTest";
+static char *synopsis = 
+"Bridging API Test Application.";
+static char *descrip = "Bridging API Test Application.\n";
+
+static int bridge_test_exec(struct ast_channel *chan, void *data)
+{
+	struct ast_module_user *u;
+	struct ast_dial *dial = NULL;
+	struct ast_bridge *bridge = NULL;
+	char *tech = NULL, *resource = NULL;
+
+	if (ast_strlen_zero(data)) {
+		ast_log(LOG_WARNING, "%s requires an argument (Tech/Resource)\n", app);
+		return -1;
+	}
+
+	u = ast_module_user_add(chan);
+
+	/* Create local duplicate for manipulation */
+	tech = ast_strdupa(data);
+
+	/* Split up technology and resource */
+	if (!(resource = strchr(tech, '/'))) {
+		ast_log(LOG_WARNING, "%s requires an argument (Tech/Resource)\n", app);
+		ast_module_user_remove(u);
+		return -1;
+	}
+	*resource++ = '\0';
+
+	/* Okay... create a new dialing structure */
+	if (!(dial = ast_dial_create())) {
+		ast_module_user_remove(u);
+		return -1;
+	}
+
+	ast_dial_append(dial, tech, resource);
+
+	/* Actually dial what we want to call */
+	if (ast_dial_run(dial, chan, 0) != AST_DIAL_RESULT_ANSWERED || !ast_dial_answered(dial)) {
+		ast_log(LOG_WARNING, "Failed to call %s\n", (char*)data);
+		ast_dial_destroy(dial);
+		ast_module_user_remove(u);
+		return -1;
+	}
+
+	/* Create a new bridge to bring the two channels into */
+	if (!(bridge = ast_bridge_new(AST_BRIDGE_CAPABILITY_1TO1MIX, AST_BRIDGE_FLAG_DISSOLVE))) {
+		ast_log(LOG_WARNING, "Failed to create bridge ;(\n");
+		ast_dial_destroy(dial);
+		ast_module_user_remove(u);
+		return -1;
+	}
+
+	/* Put the dialed channel into the bridge via async */
+	ast_bridge_impart(bridge, ast_dial_answered(dial));
+
+	sleep(1);
+
+	ast_bridge_depart(bridge, ast_dial_answered(dial));
+
+	ast_hangup(ast_dial_answered(dial));
+	
+	ast_bridge_destroy(bridge);
+
+	/* Put our channel into the bridge via blocking */
+//	ast_bridge_join(bridge, chan);
+
+	ast_module_user_remove(u);
+
+	return 0;
+}
+
+static int unload_module(void)
+{
+	return ast_unregister_application(app);
+}
+
+static int load_module(void)
+{
+	return ast_register_application(app, bridge_test_exec, synopsis, descrip);
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Bridging API Test Application");

Propchange: team/file/bridging/apps/app_bridgetest.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/file/bridging/apps/app_bridgetest.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/file/bridging/apps/app_bridgetest.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/file/bridging/bridges/Makefile
URL: http://svn.digium.com/view/asterisk/team/file/bridging/bridges/Makefile?view=auto&rev=62365
==============================================================================
--- team/file/bridging/bridges/Makefile (added)
+++ team/file/bridging/bridges/Makefile Mon Apr 30 09:16:18 2007
@@ -1,0 +1,26 @@
+#
+# Asterisk -- A telephony toolkit for Linux.
+# 
+# Makefile for bridging modules
+#
+# Copyright (C) 1999-2007, Digium, Inc.
+#
+# This program is free software, distributed under the terms of
+# the GNU General Public License
+#
+
+-include ../menuselect.makeopts ../menuselect.makedeps
+
+C_MODS:=$(filter-out $(MENUSELECT_BRIDGES),$(patsubst %.c,%,$(wildcard bridge_*.c)))
+CC_MODS:=$(filter-out $(MENUSELECT_BRIDGES),$(patsubst %.cc,%,$(wildcard bridge_*.cc)))
+
+LOADABLE_MODS:=$(C_MODS) $(CC_MODS)
+
+ifneq ($(findstring funcs,$(MENUSELECT_EMBED)),)
+  EMBEDDED_MODS:=$(LOADABLE_MODS)
+  LOADABLE_MODS:=
+endif
+
+all: _all
+
+include $(ASTTOPDIR)/Makefile.moddir_rules

Propchange: team/file/bridging/bridges/Makefile
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/file/bridging/bridges/Makefile
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/file/bridging/bridges/Makefile
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/file/bridging/bridges/bridge_simple.c
URL: http://svn.digium.com/view/asterisk/team/file/bridging/bridges/bridge_simple.c?view=auto&rev=62365
==============================================================================
--- team/file/bridging/bridges/bridge_simple.c (added)
+++ team/file/bridging/bridges/bridge_simple.c Mon Apr 30 09:16:18 2007
@@ -1,0 +1,77 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, 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 Simple two channel bridging module
+ *
+ * \author Joshua Colp <jcolp at digium.com>
+ *
+ * \ingroup bridges
+ */
+
+#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/frame.h"
+
+static int simple_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
+{
+	struct ast_channel *other = NULL;
+
+	/* If this is the only channel in this bridge then immediately exit */
+	if (AST_LIST_FIRST(&bridge->channels) == AST_LIST_LAST(&bridge->channels))
+		return -1;
+
+	/* Find the channel we actually want to write to */
+	if (!(other = (AST_LIST_FIRST(&bridge->channels) == bridge_channel ? AST_LIST_LAST(&bridge->channels)->chan : AST_LIST_FIRST(&bridge->channels)->chan)))
+		return -1;
+
+	/* Write the frame out... don't worry about freeing it, the bridging core will take care of it */
+	ast_write(other, frame);
+
+	return 0;
+}
+
+static struct ast_bridge_technology simple_bridge = {
+	.name = "simple_bridge",
+	.capabilities = AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE,
+	.write = simple_bridge_write,
+};
+
+static int unload_module(void)
+{
+	return ast_bridge_technology_unregister(&simple_bridge);
+}
+
+static int load_module(void)
+{
+	return ast_bridge_technology_register(&simple_bridge);
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Simple two channel bridging module");

Propchange: team/file/bridging/bridges/bridge_simple.c
------------------------------------------------------------------------------
    svn:eol-style = native

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

Propchange: team/file/bridging/bridges/bridge_simple.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/file/bridging/include/asterisk/bridging.h
URL: http://svn.digium.com/view/asterisk/team/file/bridging/include/asterisk/bridging.h?view=auto&rev=62365
==============================================================================
--- team/file/bridging/include/asterisk/bridging.h (added)
+++ team/file/bridging/include/asterisk/bridging.h Mon Apr 30 09:16:18 2007
@@ -1,0 +1,149 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2007, 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 Channel Bridging API
+ */
+
+#ifndef _ASTERISK_BRIDGING_H
+#define _ASTERISK_BRIDGING_H
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+/*! \brief Capabilities for a bridge technology */
+enum ast_bridge_capability {
+	AST_BRIDGE_CAPABILITY_TRANSMIX = (0 << 0), /*! Move between bridge technologies as sources change to ensure the "best" use of technologies */
+	AST_BRIDGE_CAPABILITY_1TO1MIX = (1 << 0),  /*! Bridge is only capable of mixing 2 sources */
+	AST_BRIDGE_CAPABILITY_MULTIMIX = (2 << 0), /*! Bridge is capable of mixing 2+ sources */
+	AST_BRIDGE_CAPABILITY_NATIVE = (1 << 1),   /*! Bridge can natively bridge two channels */
+};
+
+/*! \brief Flags used to notify the bridge thread of things */
+enum ast_bridge_notify_flags {
+	AST_BRIDGE_NOTIFY_REBUILD = (1 << 0), /*! Someone wants us to rebuild the bridge a bit */
+};
+
+/*! \brief Phase information about a bridged channel */
+enum ast_bridge_channel_phase {
+	AST_BRIDGE_CHANNEL_PHASE_WAIT = 0,   /*! Bridged channel is waiting for a signal */
+	AST_BRIDGE_CHANNEL_PHASE_SIGNAL = 1, /*! Bridge wants us to do something in our thread */
+	AST_BRIDGE_CHANNEL_PHASE_END = 2,    /*! Bridged channel ended itself */
+	AST_BRIDGE_CHANNEL_PHASE_HANGUP = 3, /*! Bridge requested that this channel be hungup, unless otherwise instructed */
+	AST_BRIDGE_CHANNEL_PHASE_DEPART = 4, /*! Depart from the bridge */
+};
+
+/*! \brief Flags used for bridge features */
+enum ast_bridge_feature_flags {
+	AST_BRIDGE_FLAG_DISSOLVE = (1 << 0), /*! Upon hangup of any channel dissolve the bridge */
+};
+
+struct ast_bridge;
+struct ast_bridge_channel;
+
+struct ast_bridge_technology {
+	const char *name;                                                                                             /*! Unique name to this bridge technology */
+	int capabilities;                                                                                             /*! What this bridge technology is capable of */
+	int (*create)(struct ast_bridge *bridge);                                                                     /*! Callback for when a bridge is created */
+	int (*destroy)(struct ast_bridge *bridge);                                                                    /*! Callback for when a bridge is destroyed */
+	int (*join)(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel);                            /*! Callback for when a channel joins a bridge */
+	int (*leave)(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel);                           /*! Callback for when a channel leaves a bridge */
+	int (*write)(struct ast_bridge *bridge, struct ast_bridge_channel *bridged_channel, struct ast_frame *frame); /*! Callback for writing a frame to the bridge */
+	int (*signal)(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel);                          /*! Callback for a bridge channel thread signal */
+	AST_RWLIST_ENTRY(ast_bridge_technology) list;                                                                 /*! Linked list information */
+};
+
+struct ast_bridge_channel {
+	enum ast_bridge_channel_phase phase;     /*! Current bridged channel phase */
+	struct ast_channel *chan;                /*! Asterisk channel participating in this bridge */
+	struct ast_bridge *bridge;               /*! Bridge this channel is in */
+	ast_cond_t cond;                         /*! Condition, used if we want to wake up a thread waiting on the bridged channel */
+	void *bridge_pvt;                        /*! Private information unique to the bridge technology (not always needed) */
+	pthread_t thread;                        /*! Thread handling the bridged channel */
+	int fds[4];                              /*! Additional file descriptors to watch */
+	AST_LIST_ENTRY(ast_bridge_channel) list; /*! Linked list information */
+};
+
+struct ast_bridge {
+	ast_mutex_t lock;                                    /*! Lock to protect the bridge */
+	struct ast_flags notify_flags;                       /*! Notify flags, used to indicate that the bridge should do something */
+	struct ast_flags feature_flags;                      /*! Feature flags */
+	struct ast_bridge_technology *technology;            /*! Technology in use on the bridge */
+	void *bridge_pvt;                                    /*! Private information unique to the bridge technology (not always needed) */
+	pthread_t thread;                                    /*! Thread running the bridge */
+	AST_LIST_HEAD_NOLOCK(, ast_bridge_channel) channels; /*! Channels participating in this bridge */
+};
+
+/*! \brief Register a bridge technology for use
+ * \param technology The bridge technology to register
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_bridge_technology_register(struct ast_bridge_technology *technology);
+
+/*! \brief Unregister a bridge technology from use
+ * \param technology The bridge technology to unregister
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_bridge_technology_unregister(struct ast_bridge_technology *technology);
+
+/*! \brief Create a new bridge
+ * \param capabilities The capabilities that we require to be used on the bridge
+ * \return Returns bridge on success, NULL on failure
+ */
+struct ast_bridge *ast_bridge_new(int capabilities, int flags);
+
+/*! \brief Destroy a bridge
+ * \param bridge Bridge to destroy
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_bridge_destroy(struct ast_bridge *bridge);
+
+/*! \brief Join a channel to a bridge
+ * \param bridge Bridge to join
+ * \param chan Channel to join
+ * \return Returns phase that channel exited bridge on
+ */
+enum ast_bridge_channel_phase ast_bridge_join(struct ast_bridge *bridge, struct ast_channel *chan);
+
+/*! \brief Impart a channel on a bridge
+ * \param bridge Bridge to impart on
+ * \param chan Channel to impart
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_bridge_impart(struct ast_bridge *bridge, struct ast_channel *chan);
+
+/*! \brief Depart a channel from a bridge
+ * \param bridge Bridge to depart from
+ * \param chan Channel to depart
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_bridge_depart(struct ast_bridge *bridge, struct ast_channel *chan);
+
+/*! \brief Change the phase of a bridge channel
+ * \param bridge_channel Bridge channel
+ * \param new_phase Phase to change to
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_bridge_change_phase(struct ast_bridge_channel *bridge_channel, enum ast_bridge_channel_phase new_phase);
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}
+#endif
+
+#endif /* _ASTERISK_BRIDGING_H */

Propchange: team/file/bridging/include/asterisk/bridging.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/file/bridging/include/asterisk/bridging.h
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/file/bridging/include/asterisk/bridging.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/file/bridging/main/Makefile
URL: http://svn.digium.com/view/asterisk/team/file/bridging/main/Makefile?view=diff&rev=62365&r1=62364&r2=62365
==============================================================================
--- team/file/bridging/main/Makefile (original)
+++ team/file/bridging/main/Makefile Mon Apr 30 09:16:18 2007
@@ -26,7 +26,7 @@
 	utils.o plc.o jitterbuf.o dnsmgr.o devicestate.o \
 	netsock.o slinfactory.o ast_expr2.o ast_expr2f.o \
 	cryptostub.o sha1.o http.o fixedjitterbuf.o abstract_jb.o \
-	strcompat.o threadstorage.o dial.o event.o
+	strcompat.o threadstorage.o dial.o event.o bridging.o
 
 # we need to link in the objects statically, not as a library, because
 # otherwise modules will not have them available if none of the static

Added: team/file/bridging/main/bridging.c
URL: http://svn.digium.com/view/asterisk/team/file/bridging/main/bridging.c?view=auto&rev=62365
==============================================================================
--- team/file/bridging/main/bridging.c (added)
+++ team/file/bridging/main/bridging.c Mon Apr 30 09:16:18 2007
@@ -1,0 +1,555 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2007, 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 Channel Bridging API
+ *
+ * \author Joshua Colp <jcolp at digium.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/time.h>
+#include <signal.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include "asterisk/logger.h"
+#include "asterisk/channel.h"
+#include "asterisk/options.h"
+#include "asterisk/utils.h"
+#include "asterisk/lock.h"
+#include "asterisk/linkedlists.h"
+#include "asterisk/bridging.h"
+
+static AST_RWLIST_HEAD_STATIC(bridge_technologies, ast_bridge_technology);
+
+/*! \brief Register a bridge technology for use
+ * \param technology The bridge technology to register
+ * \return Returns 0 on success, -1 on failure 
+ */
+int ast_bridge_technology_register(struct ast_bridge_technology *technology)
+{
+	struct ast_bridge_technology *current = NULL;
+
+	/* Perform a sanity check to make sure the bridge technology conforms to our needed requirements */
+	if (ast_strlen_zero(technology->name) || !technology->capabilities || !technology->write) {
+		ast_log(LOG_WARNING, "Bridge technology %s failed registration sanity check.\n", technology->name);
+		return -1;
+	}
+
+	AST_RWLIST_WRLOCK(&bridge_technologies);
+
+	/* Look for duplicate bridge technology already using this name, or already registered */
+	AST_RWLIST_TRAVERSE(&bridge_technologies, current, list) {
+		if ((!strcasecmp(current->name, technology->name)) || (current == technology)) {
+			ast_log(LOG_WARNING, "A bridge technology of %s already claims to exist in our world.\n", technology->name);
+			AST_RWLIST_UNLOCK(&bridge_technologies);
+			return -1;
+		}
+	}
+
+	/* Insert our new bridge technology into the list and print out a pretty message */
+	AST_RWLIST_INSERT_TAIL(&bridge_technologies, technology, list);
+
+	AST_RWLIST_UNLOCK(&bridge_technologies);
+
+	if (option_verbose > 1)
+		ast_verbose(VERBOSE_PREFIX_2 "Registered bridge technology %s\n", technology->name);
+
+	return 0;
+}
+
+/*! \brief Unregister a bridge technology from use
+ * \param technology The bridge technology to unregister
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_bridge_technology_unregister(struct ast_bridge_technology *technology)
+{
+	struct ast_bridge_technology *current = NULL;
+	
+	AST_RWLIST_WRLOCK(&bridge_technologies);
+
+	/* Ensure the bridge technology is registered before removing it */
+	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&bridge_technologies, current, list) {
+		if (current == technology) {
+			AST_RWLIST_REMOVE_CURRENT(&bridge_technologies, list);
+			if (option_verbose > 1)
+				ast_verbose(VERBOSE_PREFIX_2 "Unregistered bridge technology %s\n", technology->name);
+			break;
+		}
+	}
+	AST_RWLIST_TRAVERSE_SAFE_END
+
+	AST_RWLIST_UNLOCK(&bridge_technologies);
+
+	return current ? 0 : -1;
+}
+
+/*! \brief Bridge thread function */
+static void *bridge_thread(void *data)
+{
+	struct ast_bridge *bridge = data;
+	struct ast_channel *cs[128] = {NULL, }, *winner = NULL;
+	int to = -1, count = 0;
+
+	ast_mutex_lock(&bridge->lock);
+
+	/* It should never be possible for us to get called without at least one channel in our bridge but just in case... */
+	if (AST_LIST_EMPTY(&bridge->channels)) {
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Unable to execute thread for bridge %p, no channels are in it.\n", bridge);
+		ast_mutex_unlock(&bridge->lock);
+		return NULL;
+	}
+
+	/* We now basically go into a giant loop handling bridged channels, handling notify flags, and general stuff like that */
+	while (bridge->thread != AST_PTHREADT_STOP) {
+		struct ast_frame *frame = NULL;
+
+		/* See if we need to rebuild the bridge array information */
+		if (ast_test_flag(&bridge->notify_flags, AST_BRIDGE_NOTIFY_REBUILD)) {
+			struct ast_bridge_channel *bridge_channel = NULL;
+			int i = 0;
+
+			/* Now we have to add each channel */
+			AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, list) {
+				if (bridge_channel->phase == AST_BRIDGE_CHANNEL_PHASE_WAIT || bridge_channel->phase == AST_BRIDGE_CHANNEL_PHASE_SIGNAL)
+					cs[i++] = bridge_channel->chan;
+			}
+
+			if (option_debug)
+				ast_log(LOG_DEBUG, "Rebuild of bridge array on %p went from %d to %d.\n", bridge, count, i);
+			count = i;
+
+			/* If no bridge channels are in this bridge any longer discontinue the thread */
+			if (!count)
+				break;
+
+			/* All done. */
+			ast_clear_flag(&bridge->notify_flags, AST_BRIDGE_NOTIFY_REBUILD);
+		} else if (count >= 2) {
+			int i = 0;
+
+			/* Move channel priorities around */
+			cs[127] = cs[0];
+			for (i = 0; i < (count-1); i++)
+				cs[i] = cs[i+1];
+			cs[count-1] = cs[127];
+		}
+
+		/* Poll on the channels and file descriptors */
+		ast_mutex_unlock(&bridge->lock);
+		winner = ast_waitfor_n(cs, count, &to);
+		ast_mutex_lock(&bridge->lock);
+
+		/* If a winner was had... read it in */
+		if (winner) {
+			struct ast_bridge_channel *bridge_channel = NULL;
+
+			/* Look for the bridge channel structure */
+			AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, list) {
+				if (bridge_channel->chan == winner)
+					break;
+			}
+
+			/* Try to read in a frame, if this fails it means they hungup */
+			if (!(frame = ast_read(winner)) || (frame->frametype == AST_FRAME_CONTROL)) {
+				/* Switch the bridged channel phase to end and signal it's thread */
+				ast_bridge_change_phase(bridge_channel, AST_BRIDGE_CHANNEL_PHASE_END);
+				/* If the bridge is set to dissolve upon anyone hanging up, due so */
+				if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE)) {
+					struct ast_bridge_channel *bridge_channel2 = NULL;
+					AST_LIST_TRAVERSE(&bridge->channels, bridge_channel2, list) {
+						if (bridge_channel2 != bridge_channel)
+							ast_bridge_change_phase(bridge_channel2, AST_BRIDGE_CHANNEL_PHASE_HANGUP);
+					}
+				}
+				/* Have ourselves rebuild next time */
+				ast_set_flag(&bridge->notify_flags, AST_BRIDGE_NOTIFY_REBUILD);
+			} else {
+				/* Write out the frame to the bridge technology */
+				bridge->technology->write(bridge, bridge_channel, frame);
+			}
+
+			/* If a frame exists, free it */
+			if (frame)
+				ast_frfree(frame);
+		}
+	}
+
+	if (option_debug)
+		ast_log(LOG_DEBUG, "Ending bridge thread for %p\n", bridge);
+
+	/* Indicate the bridge thread is no longer active */
+	bridge->thread = AST_PTHREADT_NULL;
+
+	ast_mutex_unlock(&bridge->lock);
+
+	return NULL;
+}
+
+/*! \brief Helper function used to find the "best" bridge technology given a specified capabilities */
+static struct ast_bridge_technology *find_best_technology(int capabilities)
+{
+	struct ast_bridge_technology *current = NULL, *best = NULL;
+
+	AST_RWLIST_RDLOCK(&bridge_technologies);
+	AST_RWLIST_TRAVERSE(&bridge_technologies, current, list) {
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Bridge technology %s has capabilities %d and we want %d\n", current->name, current->capabilities, capabilities);
+		if (!(current->capabilities & capabilities))
+			continue;
+		best = current;
+	}
+	AST_RWLIST_UNLOCK(&bridge_technologies);
+
+	if (option_debug && best)
+		ast_log(LOG_DEBUG, "Chose bridge technology %s\n", best->name);
+
+	return best;
+}
+
+/*! \brief Create a new bridge
+ * \param capabilities The capabilities that we require to be used on the bridge
+ * \return Returns bridge on success, NULL on failure
+ */
+struct ast_bridge *ast_bridge_new(int capabilities, int flags)
+{
+	struct ast_bridge *bridge = NULL;
+	struct ast_bridge_technology *bridge_technology = NULL;
+
+	/* If capabilities were provided use our helper function to find the "best" bridge technology, otherwise we can
+	 * just look for the most basic capability needed, single 1to1 mixing. */
+	bridge_technology = (capabilities ? find_best_technology(capabilities) : find_best_technology(AST_BRIDGE_CAPABILITY_1TO1MIX));
+
+	/* If no bridge technology was found we can't possible do bridging so fail creation of the bridge */
+	if (!bridge_technology) {
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Failed to find a bridge technology to satisfy capabilities %d\n", capabilities);
+		return NULL;
+	}
+
+	/* We have everything we need to create this bridge... so allocate the memory, link things together, and fire her up! */
+	if (!(bridge = ast_calloc(1, sizeof(*bridge))))
+		return NULL;
+
+	/* Initialize our bridge lock */
+	ast_mutex_init(&bridge->lock);
+
+	bridge->technology = bridge_technology;
+	bridge->thread = AST_PTHREADT_NULL;
+
+	ast_set_flag(&bridge->feature_flags, flags);
+
+	/* Pass off the bridge to the technology to manipulate if needed */
+	if (bridge->technology->create) {
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Giving bridge technology %s the bridge structure %p to setup\n", bridge->technology->name, bridge);
+		bridge->technology->create(bridge);
+	}
+
+	return bridge;
+}
+
+/*! \brief Destroy a bridge
+ * \param bridge Bridge to destroy
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_bridge_destroy(struct ast_bridge *bridge)
+{
+	struct ast_bridge_channel *bridge_channel = NULL;
+
+	ast_mutex_lock(&bridge->lock);
+
+	/* Drop every bridged channel */
+	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, list) {
+		ast_bridge_change_phase(bridge_channel, AST_BRIDGE_CHANNEL_PHASE_END);
+	}
+
+	/* If the thread is still running we have to nudge it to kill itself */
+	if (bridge->thread != AST_PTHREADT_NULL) {
+		pthread_t thread = bridge->thread;
+
+		/* Request that the bridge thread rebuild it's array, this will cause it to see no channels exist and the thread will exit */
+		ast_set_flag(&bridge->notify_flags, AST_BRIDGE_NOTIFY_REBUILD);
+		/* Poke the bridge thread out of it's poll if in it */
+		pthread_kill(bridge->thread, SIGURG);
+		/* Give up our lock so that the bridge thread can acquire it */
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Giving up bridge lock on %p and waiting for thread to exit\n", bridge);
+		ast_mutex_unlock(&bridge->lock);
+		/* Now we basically wait for the thread to realize it should discontinue itself */
+		pthread_join(thread, NULL);
+	} else {
+		/* Thread is not running - we don't have to worry about it */
+		ast_mutex_unlock(&bridge->lock);
+	}
+
+	if (option_debug)
+		ast_log(LOG_DEBUG, "Proceeding with bridge destruction of %p\n", bridge);
+
+	/* Pass off the bridge to the technology to destroy if needed */
+	if (bridge->technology->destroy) {
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Giving bridge technology %s the bridge structure %p to destroy\n", bridge->technology->name, bridge);
+		bridge->technology->destroy(bridge);
+	}
+
+	/* Destroy the mutex that protects the bridge */
+	ast_mutex_destroy(&bridge->lock);
+
+	/* Deallocate the bridge */
+	free(bridge);
+
+	return 0;
+}
+
+/*! \brief Join a channel to a bridge and handle anything the bridge may want us to do */
+static enum ast_bridge_channel_phase bridge_channel_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
+{
+	/* Before we actually become part of this bridge make sure we are in the signalling wait phase */
+	bridge_channel->phase = AST_BRIDGE_CHANNEL_PHASE_WAIT;
+
+	/* Make the bridged channel part of the bridge */
+	AST_LIST_INSERT_TAIL(&bridge->channels, bridge_channel, list);
+
+	/* Notify the bridge thread that a new bridged channel is part of the bridge, this will cause it to rebuild the bridge array and some critical things */
+	ast_set_flag(&bridge->notify_flags, AST_BRIDGE_NOTIFY_REBUILD);
+
+	/* Of course if this is the first channel we actually have to create the bridge thread */
+	if ((bridge->thread == AST_PTHREADT_NULL) && (ast_pthread_create(&bridge->thread, NULL, bridge_thread, bridge))) {
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Failed to create bridge thread for %p\n", bridge);
+		return -1;
+	} else {
+		/* Poke the bridge out of it's poll if there */
+		pthread_kill(bridge->thread, SIGURG);
+	}
+
+	/* If the bridge technology wants notification that this channel is joining the bridge, give it it */
+	if (bridge->technology->join) {
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Giving bridge technology %s notification that %p is joining bridge %p\n", bridge->technology->name, bridge_channel, bridge);
+		bridge->technology->join(bridge, bridge_channel);
+	}
+
+	/* Go into a loop waiting for the bridge thread to make us do something */
+	for (;;) {
+		if (bridge_channel->phase == AST_BRIDGE_CHANNEL_PHASE_WAIT) {
+			if (option_debug)
+				ast_log(LOG_DEBUG, "Bridge channel %p entering signalling wait phase.\n", bridge_channel);
+			ast_cond_wait(&bridge_channel->cond, &bridge->lock);
+		}
+		if (bridge_channel->phase == AST_BRIDGE_CHANNEL_PHASE_SIGNAL) {
+			if (option_debug)
+				ast_log(LOG_DEBUG, "Bridge channel %p entering signalling phase.\n", bridge_channel);
+			if (bridge->technology->signal)
+				bridge->technology->signal(bridge, bridge_channel);
+			/* Switch phase back to wait */
+			bridge_channel->phase = AST_BRIDGE_CHANNEL_PHASE_WAIT;
+		} else if (bridge_channel->phase == AST_BRIDGE_CHANNEL_PHASE_END) {
+			if (option_debug)
+				ast_log(LOG_DEBUG, "Bridge channel %p entering end phase.\n", bridge_channel);
+			break;
+		} else if (bridge_channel->phase == AST_BRIDGE_CHANNEL_PHASE_HANGUP) {
+			if (option_debug)
+				ast_log(LOG_DEBUG, "Bridge channel %p entering hangup phase.\n", bridge_channel);
+			break;
+		} else if (bridge_channel->phase == AST_BRIDGE_CHANNEL_PHASE_DEPART) {
+			if (option_debug)
+				ast_log(LOG_DEBUG, "Bridge channel %p entering depart phase.\n", bridge_channel);
+			break;
+		}
+	}
+
+	/* If the bridge technology wants notification that this channel is leaving the bridge, give it it */
+	if (bridge->technology->leave) {
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Giving bridge technology %p notification that %p is leaving bridge %p\n", bridge->technology->name, bridge_channel, bridge);
+		bridge->technology->leave(bridge, bridge_channel);
+	}
+
+	/* Remove ourselves from the bridge */
+	AST_LIST_REMOVE(&bridge->channels, bridge_channel, list);
+
+	return bridge_channel->phase;
+}
+
+/*! \brief Join a channel to a bridge
+ * \param bridge Bridge to join
+ * \param chan Channel to join
+ * \return Returns phase that channel exited bridge on
+ */
+enum ast_bridge_channel_phase ast_bridge_join(struct ast_bridge *bridge, struct ast_channel *chan)
+{
+	struct ast_bridge_channel bridge_channel;
+	enum ast_bridge_channel_phase phase;
+
+	/* Wipe out the bridge channel structure just in case */
+	memset(&bridge_channel, 0, sizeof(bridge_channel));
+
+	/* Setup the bridge channel with information about the channel and stuff */
+	bridge_channel.chan = chan;
+	bridge_channel.bridge = bridge;
+
+	/* Now setup our synchronization components */
+	bridge_channel.thread = pthread_self();
+	ast_cond_init(&bridge_channel.cond, NULL);
+
+	ast_mutex_lock(&bridge->lock);
+
+	/* Off to the bridge we go... */
+	phase = bridge_channel_join(bridge, &bridge_channel);
+
+	/* All done... we are out of here! */
+	ast_mutex_unlock(&bridge->lock);
+	ast_cond_destroy(&bridge_channel.cond);
+
+	return phase;
+}
+
+/*! \brief Thread responsible for imparted bridged channels */
+static void *bridge_channel_thread(void *data)
+{
+	struct ast_bridge_channel *bridge_channel = data;
+	struct ast_bridge *bridge = bridge_channel->bridge;
+	enum ast_bridge_channel_phase phase = -1;
+
+	ast_mutex_lock(&bridge->lock);
+
+	phase = bridge_channel_join(bridge, bridge_channel);
+
+	ast_mutex_unlock(&bridge->lock);
+
+	/* Self destruct the bridge channel structure */
+	ast_cond_destroy(&bridge_channel->cond);
+
+	/* Depending on the phase hangup the channel */
+	if (phase != AST_BRIDGE_CHANNEL_PHASE_DEPART) {
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Due to phase %d on bridge channel %p we are hanging it up\n", phase, bridge_channel);
+		ast_hangup(bridge_channel->chan);
+	} else {
+		if (option_debug)
+			ast_log(LOG_DEBUG, "Due to depart phase on bridge channel %p we are NOT hanging it up -- control returning to another thread\n", bridge_channel);
+	}
+
+	free(bridge_channel);
+
+	return NULL;
+}
+
+/*! \brief Impart a channel on a bridge
+ * \param bridge Bridge to impart on
+ * \param chan Channel to impart
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_bridge_impart(struct ast_bridge *bridge, struct ast_channel *chan)
+{
+	struct ast_bridge_channel *bridge_channel = NULL;
+
+	/* Create a nwe bridge channel structure... we have to allocate it since it will exist in another thread soon enough */
+	if (!(bridge_channel = ast_calloc(1, sizeof(*bridge_channel))))
+		return -1;
+
+	/* Copy over pointers to vital information */
+	bridge_channel->chan = chan;
+	bridge_channel->bridge = bridge;
+
+	/* Setup synchronization for our thread */
+	ast_cond_init(&bridge_channel->cond, NULL);
+
+	/* Now we can create the thread to handle this channel and be done with things */
+	if (ast_pthread_create(&bridge_channel->thread, NULL, bridge_channel_thread, bridge_channel)) {
+		ast_cond_destroy(&bridge_channel->cond);
+		free(bridge_channel);
+		return -1;
+	}
+
+	return 0;
+}
+
+/*! \brief Depart a channel from a bridge
+ * \param bridge Bridge to depart from
+ * \param chan Channel to depart
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_bridge_depart(struct ast_bridge *bridge, struct ast_channel *chan)
+{
+	struct ast_bridge_channel *bridge_channel = NULL;
+	pthread_t thread = AST_PTHREADT_NULL;
+
+	ast_mutex_lock(&bridge->lock);
+
+	/* Look for channel in the bridge */
+	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, list) {
+		if (bridge_channel->chan == chan)
+			break;
+	}
+
+	/* If one does not exist we can't very well depart it */
+	if (!bridge_channel) {
+		ast_mutex_unlock(&bridge->lock);
+		return -1;
+	}
+
+	/* Record thread this channel is running in... it may not be safe to access it afterwards */
+	thread = bridge_channel->thread;
+
+	/* Change the phase to depart for the channel, this will cause it's thread to exit once everything is woken up */
+	ast_bridge_change_phase(bridge_channel, AST_BRIDGE_CHANNEL_PHASE_DEPART);
+
+	/* Queue a rebuild on the bridge itself */
+	ast_set_flag(&bridge->notify_flags, AST_BRIDGE_NOTIFY_REBUILD);
+
+	/* Poke it just in case... */
+	pthread_kill(bridge->thread, SIGURG);
+
+	if (option_debug)
+		ast_log(LOG_DEBUG, "We are going to attempt to depart bridge channel %p from bridge %p in a second...\n", bridge_channel, bridge);
+
+	/* Once we let go of this lock everything is going to go wild with activity... */
+	ast_mutex_unlock(&bridge->lock);
+
+	/* Easy as pie, wait for the bridge channel thread to end */
+	pthread_join(thread, NULL);
+
+	if (option_debug)
+		ast_log(LOG_DEBUG, "Success! Bridge channel %p now belongs to this thread\n", bridge_channel);
+
+	return 0;
+}
+
+/*! \brief Change the phase of a bridge channel
+ * \param bridge_channel Bridge channel
+ * \param new_phase Phase to change to
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_bridge_change_phase(struct ast_bridge_channel *bridge_channel, enum ast_bridge_channel_phase new_phase)
+{
+	/* Change the phase on the bridge channel and signal it's thread to wakeup so it realizes the change */
+	bridge_channel->phase = new_phase;
+	return ast_cond_signal(&bridge_channel->cond);
+}

Propchange: team/file/bridging/main/bridging.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/file/bridging/main/bridging.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/file/bridging/main/bridging.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain



More information about the asterisk-commits mailing list