aboutsummaryrefslogtreecommitdiffstats
path: root/bridges
diff options
context:
space:
mode:
authorfile <file@f38db490-d61c-443f-a65b-d21fe96a405b>2009-03-05 18:18:27 +0000
committerfile <file@f38db490-d61c-443f-a65b-d21fe96a405b>2009-03-05 18:18:27 +0000
commit78d18e52a495a2b1f964a2dceeb7fcc705a79d73 (patch)
treecc242551fdaa17b1537490b8ec0621d2c3fa2c66 /bridges
parent62297fe334460eaa96bc7a2a2c39d9714c77c868 (diff)
Merge phase 1 support for the new bridging architecture.
This commit brings in the bridging core, bridging technologies, and the ConfBridge application. For usage information on the ConfBridge application please see the output of "core show application ConfBridge" from the CLI. For API documentation please see the doxygen page describing the architecture and the documentation for each API call. Review: http://reviewboard.digium.com/r/93/ git-svn-id: http://svn.digium.com/svn/asterisk/trunk@180369 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'bridges')
-rw-r--r--bridges/Makefile20
-rw-r--r--bridges/bridge_builtin_features.c257
-rw-r--r--bridges/bridge_multiplexed.c404
-rw-r--r--bridges/bridge_simple.c103
-rw-r--r--bridges/bridge_softmix.c303
5 files changed, 1087 insertions, 0 deletions
diff --git a/bridges/Makefile b/bridges/Makefile
new file mode 100644
index 000000000..90ed6c204
--- /dev/null
+++ b/bridges/Makefile
@@ -0,0 +1,20 @@
+#
+# Asterisk -- A telephony toolkit for Linux.
+#
+# Makefile for bridging modules
+#
+# Copyright (C) 2005-2007, Digium, Inc.
+#
+# This program is free software, distributed under the terms of
+# the GNU General Public License
+#
+
+-include $(ASTTOPDIR)/menuselect.makeopts $(ASTTOPDIR)/menuselect.makedeps
+
+MODULE_PREFIX=bridge
+MENUSELECT_CATEGORY=BRIDGES
+MENUSELECT_DESCRIPTION=Bridging Modules
+
+all: _all
+
+include $(ASTTOPDIR)/Makefile.moddir_rules
diff --git a/bridges/bridge_builtin_features.c b/bridges/bridge_builtin_features.c
new file mode 100644
index 000000000..a61421c79
--- /dev/null
+++ b/bridges/bridge_builtin_features.c
@@ -0,0 +1,257 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2009, Digium, Inc.
+ *
+ * Joshua Colp <jcolp@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 Built in bridging features
+ *
+ * \author Joshua Colp <jcolp@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/bridging_technology.h"
+#include "asterisk/frame.h"
+#include "asterisk/file.h"
+#include "asterisk/app.h"
+#include "asterisk/astobj2.h"
+
+/*! \brief Helper function that presents dialtone and grabs extension */
+static int grab_transfer(struct ast_channel *chan, char *exten, size_t exten_len, const char *context)
+{
+ int res;
+
+ /* Play the simple "transfer" prompt out and wait */
+ res = ast_stream_and_wait(chan, "pbx-transfer", AST_DIGIT_ANY);
+ ast_stopstream(chan);
+
+ /* If the person hit a DTMF digit while the above played back stick it into the buffer */
+ if (res) {
+ exten[0] = (char)res;
+ }
+
+ /* Drop to dialtone so they can enter the extension they want to transfer to */
+ res = ast_app_dtget(chan, context, exten, exten_len, 100, 1000);
+
+ return res;
+}
+
+/*! \brief Helper function that creates an outgoing channel and returns it immediately */
+static struct ast_channel *dial_transfer(const struct ast_channel *caller, const char *exten, const char *context)
+{
+ char destination[AST_MAX_EXTENSION+AST_MAX_CONTEXT+1] = "";
+ struct ast_channel *chan = NULL;
+ int cause;
+
+ /* Fill the variable with the extension and context we want to call */
+ snprintf(destination, sizeof(destination), "%s@%s", exten, context);
+
+ /* Now we request that chan_local prepare to call the destination */
+ if (!(chan = ast_request("Local", caller->nativeformats, destination, &cause))) {
+ return NULL;
+ }
+
+ /* Before we actually dial out let's inherit the appropriate dialplan variables */
+ ast_channel_inherit_variables(caller, chan);
+
+ /* Since the above worked fine now we actually call it and return the channel */
+ if (ast_call(chan, destination, 0)) {
+ ast_hangup(chan);
+ return NULL;
+ }
+
+ return chan;
+}
+
+/*! \brief Internal built in feature for blind transfers */
+static int feature_blind_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
+{
+ char exten[AST_MAX_EXTENSION] = "";
+ struct ast_channel *chan = NULL;
+ struct ast_bridge_features_blind_transfer *blind_transfer = hook_pvt;
+ const char *context = (blind_transfer && !ast_strlen_zero(blind_transfer->context) ? blind_transfer->context : bridge_channel->chan->context);
+
+ /* Grab the extension to transfer to */
+ if (!grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
+ ast_stream_and_wait(bridge_channel->chan, "pbx-invalid", AST_DIGIT_ANY);
+ ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
+ return 0;
+ }
+
+ /* Get a channel that is the destination we wish to call */
+ if (!(chan = dial_transfer(bridge_channel->chan, exten, context))) {
+ ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_ANY);
+ ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
+ return 0;
+ }
+
+ /* This is sort of the fun part. We impart the above channel onto the bridge, and have it take our place. */
+ ast_bridge_impart(bridge, chan, bridge_channel->chan, NULL);
+
+ return 0;
+}
+
+/*! \brief Attended transfer feature to turn it into a threeway call */
+static int attended_threeway_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
+{
+ /* This is sort of abusing the depart state but in this instance it is only going to be handled in the below function so it is okay */
+ ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_DEPART);
+ return 0;
+}
+
+/*! \brief Attended transfer abort feature */
+static int attended_abort_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
+{
+ struct ast_bridge_channel *called_bridge_channel = NULL;
+
+ /* It is possible (albeit unlikely) that the bridge channels list may change, so we have to ensure we do all of our magic while locked */
+ ao2_lock(bridge);
+
+ if (AST_LIST_FIRST(&bridge->channels) != bridge_channel) {
+ called_bridge_channel = AST_LIST_FIRST(&bridge->channels);
+ } else {
+ called_bridge_channel = AST_LIST_LAST(&bridge->channels);
+ }
+
+ /* Now we basically eject the other channel from the bridge. This will cause their thread to hang them up, and our own code to consider the transfer failed. */
+ if (called_bridge_channel) {
+ ast_bridge_change_state(called_bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
+ }
+
+ ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
+
+ ao2_unlock(bridge);
+
+ return 0;
+}
+
+/*! \brief Internal built in feature for attended transfers */
+static int feature_attended_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
+{
+ char exten[AST_MAX_EXTENSION] = "";
+ struct ast_channel *chan = NULL;
+ struct ast_bridge *attended_bridge = NULL;
+ struct ast_bridge_features caller_features, called_features;
+ enum ast_bridge_channel_state attended_bridge_result;
+ struct ast_bridge_features_attended_transfer *attended_transfer = hook_pvt;
+ const char *context = (attended_transfer && !ast_strlen_zero(attended_transfer->context) ? attended_transfer->context : bridge_channel->chan->context);
+
+ /* Grab the extension to transfer to */
+ if (!grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
+ ast_stream_and_wait(bridge_channel->chan, "pbx-invalid", AST_DIGIT_ANY);
+ ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
+ return 0;
+ }
+
+ /* Get a channel that is the destination we wish to call */
+ if (!(chan = dial_transfer(bridge_channel->chan, exten, context))) {
+ ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_ANY);
+ ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
+ return 0;
+ }
+
+ /* Create a bridge to use to talk to the person we are calling */
+ if (!(attended_bridge = ast_bridge_new(AST_BRIDGE_CAPABILITY_1TO1MIX, 0))) {
+ ast_hangup(chan);
+ ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_ANY);
+ ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
+ return 0;
+ }
+
+ /* Setup our called features structure so that if they hang up we immediately get thrown out of the bridge */
+ ast_bridge_features_init(&called_features);
+ ast_bridge_features_set_flag(&called_features, AST_BRIDGE_FLAG_DISSOLVE);
+
+ /* This is how this is going down, we are imparting the channel we called above into this bridge first */
+ ast_bridge_impart(attended_bridge, chan, NULL, &called_features);
+
+ /* Before we join setup a features structure with the hangup option, just in case they want to use DTMF */
+ ast_bridge_features_init(&caller_features);
+ ast_bridge_features_enable(&caller_features, AST_BRIDGE_BUILTIN_HANGUP,
+ (attended_transfer && !ast_strlen_zero(attended_transfer->complete) ? attended_transfer->complete : "*1"), NULL);
+ ast_bridge_features_hook(&caller_features, (attended_transfer && !ast_strlen_zero(attended_transfer->threeway) ? attended_transfer->threeway : "*2"),
+ attended_threeway_transfer, NULL);
+ ast_bridge_features_hook(&caller_features, (attended_transfer && !ast_strlen_zero(attended_transfer->abort) ? attended_transfer->abort : "*3"),
+ attended_abort_transfer, NULL);
+
+ /* But for the caller we want to join the bridge in a blocking fashion so we don't spin around in this function doing nothing while waiting */
+ attended_bridge_result = ast_bridge_join(attended_bridge, bridge_channel->chan, NULL, &caller_features);
+
+ /* Since the above returned the caller features structure is of no more use */
+ ast_bridge_features_cleanup(&caller_features);
+
+ /* Drop the channel we are transferring to out of the above bridge since it has ended */
+ if ((attended_bridge_result != AST_BRIDGE_CHANNEL_STATE_HANGUP) && !ast_bridge_depart(attended_bridge, chan)) {
+ /* If the user wants to turn this into a threeway transfer then do so, otherwise they take our place */
+ if (attended_bridge_result == AST_BRIDGE_CHANNEL_STATE_DEPART) {
+ /* We want to impart them upon the bridge and just have us return to it as normal */
+ ast_bridge_impart(bridge, chan, NULL, NULL);
+ } else {
+ ast_bridge_impart(bridge, chan, bridge_channel->chan, NULL);
+ }
+ } else {
+ ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_ANY);
+ ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
+ }
+
+ /* Now that all channels are out of it we can destroy the bridge and the called features structure */
+ ast_bridge_features_cleanup(&called_features);
+ ast_bridge_destroy(attended_bridge);
+
+ return 0;
+}
+
+/*! \brief Internal built in feature for hangup */
+static int feature_hangup(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
+{
+ /* This is very simple, we basically change the state on the bridge channel to end and the core takes care of the rest */
+ ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
+ return 0;
+}
+
+static int unload_module(void)
+{
+ return 0;
+}
+
+static int load_module(void)
+{
+ ast_bridge_features_register(AST_BRIDGE_BUILTIN_BLINDTRANSFER, feature_blind_transfer, NULL);
+ ast_bridge_features_register(AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER, feature_attended_transfer, NULL);
+ ast_bridge_features_register(AST_BRIDGE_BUILTIN_HANGUP, feature_hangup, NULL);
+
+ /* Bump up our reference count so we can't be unloaded */
+ ast_module_ref(ast_module_info->self);
+
+ return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Built in bridging features");
diff --git a/bridges/bridge_multiplexed.c b/bridges/bridge_multiplexed.c
new file mode 100644
index 000000000..520673134
--- /dev/null
+++ b/bridges/bridge_multiplexed.c
@@ -0,0 +1,404 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2008, Digium, Inc.
+ *
+ * Joshua Colp <jcolp@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 Two channel bridging module which groups bridges into batches of threads
+ *
+ * \author Joshua Colp <jcolp@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 <fcntl.h>
+
+#include "asterisk/module.h"
+#include "asterisk/channel.h"
+#include "asterisk/bridging.h"
+#include "asterisk/bridging_technology.h"
+#include "asterisk/frame.h"
+#include "asterisk/astobj2.h"
+
+/*! \brief Number of buckets our multiplexed thread container can have */
+#define MULTIPLEXED_BUCKETS 53
+
+/*! \brief Number of channels we handle in a single thread */
+#define MULTIPLEXED_MAX_CHANNELS 8
+
+/*! \brief Structure which represents a single thread handling multiple 2 channel bridges */
+struct multiplexed_thread {
+ /*! Thread itself */
+ pthread_t thread;
+ /*! Pipe used to wake up the multiplexed thread */
+ int pipe[2];
+ /*! Channels in this thread */
+ struct ast_channel *chans[MULTIPLEXED_MAX_CHANNELS];
+ /*! Number of channels in this thread */
+ unsigned int count;
+ /*! Bit used to indicate that the thread is waiting on channels */
+ unsigned int waiting:1;
+ /*! Number of channels actually being serviced by this thread */
+ unsigned int service_count;
+};
+
+/*! \brief Container of all operating multiplexed threads */
+static struct ao2_container *multiplexed_threads;
+
+/*! \brief Callback function for finding a free multiplexed thread */
+static int find_multiplexed_thread(void *obj, void *arg, int flags)
+{
+ struct multiplexed_thread *multiplexed_thread = obj;
+ return (multiplexed_thread->count <= (MULTIPLEXED_MAX_CHANNELS - 2)) ? CMP_MATCH | CMP_STOP : 0;
+}
+
+/*! \brief Destroy callback for a multiplexed thread structure */
+static void destroy_multiplexed_thread(void *obj)
+{
+ struct multiplexed_thread *multiplexed_thread = obj;
+
+ if (multiplexed_thread->pipe[0] > -1) {
+ close(multiplexed_thread->pipe[0]);
+ }
+ if (multiplexed_thread->pipe[1] > -1) {
+ close(multiplexed_thread->pipe[1]);
+ }
+
+ return;
+}
+
+/*! \brief Create function which finds/reserves/references a multiplexed thread structure */
+static int multiplexed_bridge_create(struct ast_bridge *bridge)
+{
+ struct multiplexed_thread *multiplexed_thread;
+
+ ao2_lock(multiplexed_threads);
+
+ /* Try to find an existing thread to handle our additional channels */
+ if (!(multiplexed_thread = ao2_callback(multiplexed_threads, 0, find_multiplexed_thread, NULL))) {
+ int flags;
+
+ /* If we failed we will have to create a new one from scratch */
+ if (!(multiplexed_thread = ao2_alloc(sizeof(*multiplexed_thread), destroy_multiplexed_thread))) {
+ ast_debug(1, "Failed to find or create a new multiplexed thread for bridge '%p'\n", bridge);
+ ao2_unlock(multiplexed_threads);
+ return -1;
+ }
+
+ multiplexed_thread->pipe[0] = multiplexed_thread->pipe[1] = -1;
+ /* Setup a pipe so we can poke the thread itself when needed */
+ if (pipe(multiplexed_thread->pipe)) {
+ ast_debug(1, "Failed to create a pipe for poking a multiplexed thread for bridge '%p'\n", bridge);
+ ao2_ref(multiplexed_thread, -1);
+ ao2_unlock(multiplexed_threads);
+ return -1;
+ }
+
+ /* Setup each pipe for non-blocking operation */
+ flags = fcntl(multiplexed_thread->pipe[0], F_GETFL);
+ if (fcntl(multiplexed_thread->pipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
+ ast_log(LOG_WARNING, "Failed to setup first nudge pipe for non-blocking operation on %p (%d: %s)\n", bridge, errno, strerror(errno));
+ ao2_ref(multiplexed_thread, -1);
+ ao2_unlock(multiplexed_threads);
+ return -1;
+ }
+ flags = fcntl(multiplexed_thread->pipe[1], F_GETFL);
+ if (fcntl(multiplexed_thread->pipe[1], F_SETFL, flags | O_NONBLOCK) < 0) {
+ ast_log(LOG_WARNING, "Failed to setup second nudge pipe for non-blocking operation on %p (%d: %s)\n", bridge, errno, strerror(errno));
+ ao2_ref(multiplexed_thread, -1);
+ ao2_unlock(multiplexed_threads);
+ return -1;
+ }
+
+ /* Set up default parameters */
+ multiplexed_thread->thread = AST_PTHREADT_NULL;
+
+ /* Finally link us into the container so others may find us */
+ ao2_link(multiplexed_threads, multiplexed_thread);
+ ast_debug(1, "Created multiplexed thread '%p' for bridge '%p'\n", multiplexed_thread, bridge);
+ } else {
+ ast_debug(1, "Found multiplexed thread '%p' for bridge '%p'\n", multiplexed_thread, bridge);
+ }
+
+ /* Bump the count of the thread structure up by two since the channels for this bridge will be joining shortly */
+ multiplexed_thread->count += 2;
+
+ ao2_unlock(multiplexed_threads);
+
+ bridge->bridge_pvt = multiplexed_thread;
+
+ return 0;
+}
+
+/*! \brief Internal function which nudges the thread */
+static void multiplexed_nudge(struct multiplexed_thread *multiplexed_thread)
+{
+ int nudge = 0;
+
+ if (multiplexed_thread->thread == AST_PTHREADT_NULL) {
+ return;
+ }
+
+ if (write(multiplexed_thread->pipe[1], &nudge, sizeof(nudge)) != sizeof(nudge)) {
+ ast_log(LOG_ERROR, "We couldn't poke multiplexed thread '%p'... something is VERY wrong\n", multiplexed_thread);
+ }
+
+ while (multiplexed_thread->waiting) {
+ sched_yield();
+ }
+
+ return;
+}
+
+/*! \brief Destroy function which unreserves/unreferences/removes a multiplexed thread structure */
+static int multiplexed_bridge_destroy(struct ast_bridge *bridge)
+{
+ struct multiplexed_thread *multiplexed_thread = bridge->bridge_pvt;
+
+ ao2_lock(multiplexed_threads);
+
+ multiplexed_thread->count -= 2;
+
+ if (!multiplexed_thread->count) {
+ ast_debug(1, "Unlinking multiplexed thread '%p' since nobody is using it anymore\n", multiplexed_thread);
+ ao2_unlink(multiplexed_threads, multiplexed_thread);
+ }
+
+ multiplexed_nudge(multiplexed_thread);
+
+ ao2_unlock(multiplexed_threads);
+
+ ao2_ref(multiplexed_thread, -1);
+
+ return 0;
+}
+
+/*! \brief Thread function that executes for multiplexed threads */
+static void *multiplexed_thread_function(void *data)
+{
+ struct multiplexed_thread *multiplexed_thread = data;
+ int fds = multiplexed_thread->pipe[0];
+
+ ao2_lock(multiplexed_thread);
+
+ ast_debug(1, "Starting actual thread for multiplexed thread '%p'\n", multiplexed_thread);
+
+ while (multiplexed_thread->thread != AST_PTHREADT_STOP) {
+ struct ast_channel *winner = NULL, *first = multiplexed_thread->chans[0];
+ int to = -1, outfd = -1;
+
+ /* Move channels around so not just the first one gets priority */
+ memmove(multiplexed_thread->chans, multiplexed_thread->chans + 1, sizeof(struct ast_channel *) * (multiplexed_thread->service_count - 1));
+ multiplexed_thread->chans[multiplexed_thread->service_count - 1] = first;
+
+ multiplexed_thread->waiting = 1;
+ ao2_unlock(multiplexed_thread);
+ winner = ast_waitfor_nandfds(multiplexed_thread->chans, multiplexed_thread->service_count, &fds, 1, NULL, &outfd, &to);
+ multiplexed_thread->waiting = 0;
+ ao2_lock(multiplexed_thread);
+
+ if (outfd > -1) {
+ int nudge;
+
+ if (read(multiplexed_thread->pipe[0], &nudge, sizeof(nudge)) < 0) {
+ if (errno != EINTR && errno != EAGAIN) {
+ ast_log(LOG_WARNING, "read() failed for pipe on multiplexed thread '%p': %s\n", multiplexed_thread, strerror(errno));
+ }
+ }
+ }
+ if (winner && winner->bridge) {
+ ast_bridge_handle_trip(winner->bridge, NULL, winner, -1);
+ }
+ }
+
+ multiplexed_thread->thread = AST_PTHREADT_NULL;
+
+ ast_debug(1, "Stopping actual thread for multiplexed thread '%p'\n", multiplexed_thread);
+
+ ao2_unlock(multiplexed_thread);
+ ao2_ref(multiplexed_thread, -1);
+
+ return NULL;
+}
+
+/*! \brief Helper function which adds or removes a channel and nudges the thread */
+static void multiplexed_add_or_remove(struct multiplexed_thread *multiplexed_thread, struct ast_channel *chan, int add)
+{
+ int i, removed = 0;
+ pthread_t thread = AST_PTHREADT_NULL;
+
+ ao2_lock(multiplexed_thread);
+
+ multiplexed_nudge(multiplexed_thread);
+
+ for (i = 0; i < MULTIPLEXED_MAX_CHANNELS; i++) {
+ if (multiplexed_thread->chans[i] == chan) {
+ if (!add) {
+ multiplexed_thread->chans[i] = NULL;
+ multiplexed_thread->service_count--;
+ removed = 1;
+ }
+ break;
+ } else if (!multiplexed_thread->chans[i] && add) {
+ multiplexed_thread->chans[i] = chan;
+ multiplexed_thread->service_count++;
+ break;
+ }
+ }
+
+ if (multiplexed_thread->service_count && multiplexed_thread->thread == AST_PTHREADT_NULL) {
+ ao2_ref(multiplexed_thread, +1);
+ if (ast_pthread_create(&multiplexed_thread->thread, NULL, multiplexed_thread_function, multiplexed_thread)) {
+ ao2_ref(multiplexed_thread, -1);
+ ast_debug(1, "Failed to create an actual thread for multiplexed thread '%p', trying next time\n", multiplexed_thread);
+ }
+ } else if (!multiplexed_thread->service_count && multiplexed_thread->thread != AST_PTHREADT_NULL) {
+ thread = multiplexed_thread->thread;
+ multiplexed_thread->thread = AST_PTHREADT_STOP;
+ } else if (!add && removed) {
+ memmove(multiplexed_thread->chans + i, multiplexed_thread->chans + i + 1, sizeof(struct ast_channel *) * (MULTIPLEXED_MAX_CHANNELS - (i + 1)));
+ }
+
+ ao2_unlock(multiplexed_thread);
+
+ if (thread != AST_PTHREADT_NULL) {
+ pthread_join(thread, NULL);
+ }
+
+ return;
+}
+
+/*! \brief Join function which actually adds the channel into the array to be monitored */
+static int multiplexed_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
+{
+ struct ast_channel *c0 = AST_LIST_FIRST(&bridge->channels)->chan, *c1 = AST_LIST_LAST(&bridge->channels)->chan;
+ struct multiplexed_thread *multiplexed_thread = bridge->bridge_pvt;
+
+ ast_debug(1, "Adding channel '%s' to multiplexed thread '%p' for monitoring\n", bridge_channel->chan->name, multiplexed_thread);
+
+ multiplexed_add_or_remove(multiplexed_thread, bridge_channel->chan, 1);
+
+ /* If the second channel has not yet joined do not make things compatible */
+ if (c0 == c1) {
+ return 0;
+ }
+
+ if (((c0->writeformat == c1->readformat) && (c0->readformat == c1->writeformat) && (c0->nativeformats == c1->nativeformats))) {
+ return 0;
+ }
+
+ return ast_channel_make_compatible(c0, c1);
+}
+
+/*! \brief Leave function which actually removes the channel from the array */
+static int multiplexed_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
+{
+ struct multiplexed_thread *multiplexed_thread = bridge->bridge_pvt;
+
+ ast_debug(1, "Removing channel '%s' from multiplexed thread '%p'\n", bridge_channel->chan->name, multiplexed_thread);
+
+ multiplexed_add_or_remove(multiplexed_thread, bridge_channel->chan, 0);
+
+ return 0;
+}
+
+/*! \brief Suspend function which means control of the channel is going elsewhere */
+static void multiplexed_bridge_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
+{
+ struct multiplexed_thread *multiplexed_thread = bridge->bridge_pvt;
+
+ ast_debug(1, "Suspending channel '%s' from multiplexed thread '%p'\n", bridge_channel->chan->name, multiplexed_thread);
+
+ multiplexed_add_or_remove(multiplexed_thread, bridge_channel->chan, 0);
+
+ return;
+}
+
+/*! \brief Unsuspend function which means control of the channel is coming back to us */
+static void multiplexed_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
+{
+ struct multiplexed_thread *multiplexed_thread = bridge->bridge_pvt;
+
+ ast_debug(1, "Unsuspending channel '%s' from multiplexed thread '%p'\n", bridge_channel->chan->name, multiplexed_thread);
+
+ multiplexed_add_or_remove(multiplexed_thread, bridge_channel->chan, 1);
+
+ return;
+}
+
+/*! \brief Write function for writing frames into the bridge */
+static enum ast_bridge_write_result multiplexed_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
+{
+ struct ast_bridge_channel *other;
+
+ if (AST_LIST_FIRST(&bridge->channels) == AST_LIST_LAST(&bridge->channels)) {
+ return AST_BRIDGE_WRITE_FAILED;
+ }
+
+ if (!(other = (AST_LIST_FIRST(&bridge->channels) == bridge_channel ? AST_LIST_LAST(&bridge->channels) : AST_LIST_FIRST(&bridge->channels)))) {
+ return AST_BRIDGE_WRITE_FAILED;
+ }
+
+ if (other->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
+ ast_write(other->chan, frame);
+ }
+
+ return AST_BRIDGE_WRITE_SUCCESS;
+}
+
+static struct ast_bridge_technology multiplexed_bridge = {
+ .name = "multiplexed_bridge",
+ .capabilities = AST_BRIDGE_CAPABILITY_1TO1MIX,
+ .preference = AST_BRIDGE_PREFERENCE_HIGH,
+ .formats = AST_FORMAT_AUDIO_MASK | AST_FORMAT_VIDEO_MASK | AST_FORMAT_TEXT_MASK,
+ .create = multiplexed_bridge_create,
+ .destroy = multiplexed_bridge_destroy,
+ .join = multiplexed_bridge_join,
+ .leave = multiplexed_bridge_leave,
+ .suspend = multiplexed_bridge_suspend,
+ .unsuspend = multiplexed_bridge_unsuspend,
+ .write = multiplexed_bridge_write,
+};
+
+static int unload_module(void)
+{
+ int res = ast_bridge_technology_unregister(&multiplexed_bridge);
+
+ ao2_ref(multiplexed_threads, -1);
+
+ return res;
+}
+
+static int load_module(void)
+{
+ if (!(multiplexed_threads = ao2_container_alloc(MULTIPLEXED_BUCKETS, NULL, NULL))) {
+ return AST_MODULE_LOAD_DECLINE;
+ }
+
+ return ast_bridge_technology_register(&multiplexed_bridge);
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multiplexed two channel bridging module");
diff --git a/bridges/bridge_simple.c b/bridges/bridge_simple.c
new file mode 100644
index 000000000..0dbcd25d2
--- /dev/null
+++ b/bridges/bridge_simple.c
@@ -0,0 +1,103 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Joshua Colp <jcolp@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@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/bridging_technology.h"
+#include "asterisk/frame.h"
+
+static int simple_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
+{
+ struct ast_channel *c0 = AST_LIST_FIRST(&bridge->channels)->chan, *c1 = AST_LIST_LAST(&bridge->channels)->chan;
+
+ /* If this is the first channel we can't make it compatible... unless we make it compatible with itself O.o */
+ if (AST_LIST_FIRST(&bridge->channels) == AST_LIST_LAST(&bridge->channels)) {
+ return 0;
+ }
+
+ /* See if we need to make these compatible */
+ if (((c0->writeformat == c1->readformat) && (c0->readformat == c1->writeformat) && (c0->nativeformats == c1->nativeformats))) {
+ return 0;
+ }
+
+ /* BOOM! We do. */
+ return ast_channel_make_compatible(c0, c1);
+}
+
+static enum ast_bridge_write_result simple_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
+{
+ struct ast_bridge_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 AST_BRIDGE_WRITE_FAILED;
+ }
+
+ /* Find the channel we actually want to write to */
+ if (!(other = (AST_LIST_FIRST(&bridge->channels) == bridge_channel ? AST_LIST_LAST(&bridge->channels) : AST_LIST_FIRST(&bridge->channels)))) {
+ return AST_BRIDGE_WRITE_FAILED;
+ }
+
+ /* Write the frame out if they are in the waiting state... don't worry about freeing it, the bridging core will take care of it */
+ if (other->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
+ ast_write(other->chan, frame);
+ }
+
+ return AST_BRIDGE_WRITE_SUCCESS;
+}
+
+static struct ast_bridge_technology simple_bridge = {
+ .name = "simple_bridge",
+ .capabilities = AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_THREAD,
+ .preference = AST_BRIDGE_PREFERENCE_MEDIUM,
+ .formats = AST_FORMAT_AUDIO_MASK | AST_FORMAT_VIDEO_MASK | AST_FORMAT_TEXT_MASK,
+ .join = simple_bridge_join,
+ .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");
diff --git a/bridges/bridge_softmix.c b/bridges/bridge_softmix.c
new file mode 100644
index 000000000..4f1e4d76f
--- /dev/null
+++ b/bridges/bridge_softmix.c
@@ -0,0 +1,303 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Joshua Colp <jcolp@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 Multi-party software based channel mixing
+ *
+ * \author Joshua Colp <jcolp@digium.com>
+ *
+ * \ingroup bridges
+ *
+ * \todo This bridge operates in 8 kHz mode unless a define is uncommented.
+ * This needs to be improved so the bridge moves between the dominant codec as needed depending
+ * on channels present in the bridge and transcoding capabilities.
+ */
+
+#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/module.h"
+#include "asterisk/channel.h"
+#include "asterisk/bridging.h"
+#include "asterisk/bridging_technology.h"
+#include "asterisk/frame.h"
+#include "asterisk/options.h"
+#include "asterisk/logger.h"
+#include "asterisk/slinfactory.h"
+#include "asterisk/astobj2.h"
+#include "asterisk/timing.h"
+
+/*! \brief Interval at which mixing will take place. Valid options are 10, 20, and 40. */
+#define SOFTMIX_INTERVAL 20
+
+/*! \brief Size of the buffer used for sample manipulation */
+#define SOFTMIX_DATALEN (160 * (SOFTMIX_INTERVAL / 10))
+
+/*! \brief Number of samples we are dealing with */
+#define SOFTMIX_SAMPLES (SOFTMIX_DATALEN / 2)
+
+/*! \brief Define used to turn on 16 kHz audio support */
+/* #define SOFTMIX_16_SUPPORT */
+
+/*! \brief Structure which contains per-channel mixing information */
+struct softmix_channel {
+ /*! Lock to protect this structure */
+ ast_mutex_t lock;
+ /*! Factory which contains audio read in from the channel */
+ struct ast_slinfactory factory;
+ /*! Frame that contains mixed audio to be written out to the channel */
+ struct ast_frame frame;
+ /*! Bit used to indicate that the channel provided audio for this mixing interval */
+ int have_audio:1;
+ /*! Bit used to indicate that a frame is available to be written out to the channel */
+ int have_frame:1;
+ /*! Buffer containing final mixed audio from all sources */
+ short final_buf[SOFTMIX_DATALEN];
+ /*! Buffer containing only the audio from the channel */
+ short our_buf[SOFTMIX_DATALEN];
+};
+
+/*! \brief Function called when a bridge is created */
+static int softmix_bridge_create(struct ast_bridge *bridge)
+{
+ int timingfd;
+
+ if ((timingfd = ast_timer_open()) < 0) {
+ return -1;
+ }
+
+ ast_timer_close(timingfd);
+
+ return 0;
+}
+
+/*! \brief Function called when a channel is joined into the bridge */
+static int softmix_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
+{
+ struct softmix_channel *sc = NULL;
+
+ /* Create a new softmix_channel structure and allocate various things on it */
+ if (!(sc = ast_calloc(1, sizeof(*sc)))) {
+ return -1;
+ }
+
+ /* Can't forget the lock */
+ ast_mutex_init(&sc->lock);
+
+ /* Setup smoother */
+ ast_slinfactory_init(&sc->factory);
+
+ /* Setup frame parameters */
+ sc->frame.frametype = AST_FRAME_VOICE;
+#ifdef SOFTMIX_16_SUPPORT
+ sc->frame.subclass = AST_FORMAT_SLINEAR16;
+#else
+ sc->frame.subclass = AST_FORMAT_SLINEAR;
+#endif
+ sc->frame.data.ptr = sc->final_buf;
+ sc->frame.datalen = SOFTMIX_DATALEN;
+ sc->frame.samples = SOFTMIX_SAMPLES;
+
+ /* Can't forget to record our pvt structure within the bridged channel structure */
+ bridge_channel->bridge_pvt = sc;
+
+ return 0;
+}
+
+/*! \brief Function called when a channel leaves the bridge */
+static int softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
+{
+ struct softmix_channel *sc = bridge_channel->bridge_pvt;
+
+ /* Drop mutex lock */
+ ast_mutex_destroy(&sc->lock);
+
+ /* Drop the factory */
+ ast_slinfactory_destroy(&sc->factory);
+
+ /* Eep! drop ourselves */
+ ast_free(sc);
+
+ return 0;
+}
+
+/*! \brief Function called when a channel writes a frame into the bridge */
+static enum ast_bridge_write_result softmix_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
+{
+ struct softmix_channel *sc = bridge_channel->bridge_pvt;
+
+ /* Only accept audio frames, all others are unsupported */
+ if (frame->frametype != AST_FRAME_VOICE) {
+ return AST_BRIDGE_WRITE_UNSUPPORTED;
+ }
+
+ ast_mutex_lock(&sc->lock);
+
+ /* If a frame was provided add it to the smoother */
+#ifdef SOFTMIX_16_SUPPORT
+ if (frame->frametype == AST_FRAME_VOICE && frame->subclass == AST_FORMAT_SLINEAR16) {
+#else
+ if (frame->frametype == AST_FRAME_VOICE && frame->subclass == AST_FORMAT_SLINEAR) {
+#endif
+ ast_slinfactory_feed(&sc->factory, frame);
+ }
+
+ /* If a frame is ready to be written out, do so */
+ if (sc->have_frame) {
+ ast_write(bridge_channel->chan, &sc->frame);
+ sc->have_frame = 0;
+ }
+
+ /* Alllll done */
+ ast_mutex_unlock(&sc->lock);
+
+ return AST_BRIDGE_WRITE_SUCCESS;
+}
+
+/*! \brief Function called when the channel's thread is poked */
+static int softmix_bridge_poke(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
+{
+ struct softmix_channel *sc = bridge_channel->bridge_pvt;
+
+ ast_mutex_lock(&sc->lock);
+
+ if (sc->have_frame) {
+ ast_write(bridge_channel->chan, &sc->frame);
+ sc->have_frame = 0;
+ }
+
+ ast_mutex_unlock(&sc->lock);
+
+ return 0;
+}
+
+/*! \brief Function which acts as the mixing thread */
+static int softmix_bridge_thread(struct ast_bridge *bridge)
+{
+ int timingfd;
+
+ if ((timingfd = ast_timer_open()) < 0) {
+ return -1;
+ }
+
+ ast_timer_set_rate(timingfd, (1000 / SOFTMIX_INTERVAL));
+
+ while (!bridge->stop && !bridge->refresh && bridge->array_num) {
+ struct ast_bridge_channel *bridge_channel = NULL;
+ short buf[SOFTMIX_DATALEN] = {0, };
+ int timeout = -1;
+
+ /* Go through pulling audio from each factory that has it available */
+ AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
+ struct softmix_channel *sc = bridge_channel->bridge_pvt;
+
+ ast_mutex_lock(&sc->lock);
+
+ /* Try to get audio from the factory if available */
+ if (ast_slinfactory_available(&sc->factory) >= SOFTMIX_SAMPLES && ast_slinfactory_read(&sc->factory, sc->our_buf, SOFTMIX_SAMPLES)) {
+ short *data1, *data2;
+ int i;
+
+ /* Put into the local final buffer */
+ for (i = 0, data1 = buf, data2 = sc->our_buf; i < SOFTMIX_DATALEN; i++, data1++, data2++)
+ ast_slinear_saturated_add(data1, data2);
+ /* Yay we have our own audio */
+ sc->have_audio = 1;
+ } else {
+ /* Awww we don't have audio ;( */
+ sc->have_audio = 0;
+ }
+ ast_mutex_unlock(&sc->lock);
+ }
+
+ /* Next step go through removing the channel's own audio and creating a good frame... */
+ AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
+ struct softmix_channel *sc = bridge_channel->bridge_pvt;
+ int i = 0;
+
+ /* Copy from local final buffer to our final buffer */
+ memcpy(sc->final_buf, buf, sizeof(sc->final_buf));
+
+ /* If we provided audio then take it out */
+ if (sc->have_audio) {
+ for (i = 0; i < SOFTMIX_DATALEN; i++) {
+ ast_slinear_saturated_subtract(&sc->final_buf[i], &sc->our_buf[i]);
+ }
+ }
+
+ /* The frame is now ready for use... */
+ sc->have_frame = 1;
+
+ /* Poke bridged channel thread just in case */
+ pthread_kill(bridge_channel->thread, SIGURG);
+ }
+
+ ao2_unlock(bridge);
+
+ /* Wait for the timing source to tell us to wake up and get things done */
+ ast_waitfor_n_fd(&timingfd, 1, &timeout, NULL);
+
+ ast_timer_ack(timingfd, 1);
+
+ ao2_lock(bridge);
+ }
+
+ ast_timer_set_rate(timingfd, 0);
+ ast_timer_close(timingfd);
+
+ return 0;
+}
+
+static struct ast_bridge_technology softmix_bridge = {
+ .name = "softmix",
+ .capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX | AST_BRIDGE_CAPABILITY_THREAD | AST_BRIDGE_CAPABILITY_MULTITHREADED,
+ .preference = AST_BRIDGE_PREFERENCE_LOW,
+#ifdef SOFTMIX_16_SUPPORT
+ .formats = AST_FORMAT_SLINEAR16,
+#else
+ .formats = AST_FORMAT_SLINEAR,
+#endif
+ .create = softmix_bridge_create,
+ .join = softmix_bridge_join,
+ .leave = softmix_bridge_leave,
+ .write = softmix_bridge_write,
+ .thread = softmix_bridge_thread,
+ .poke = softmix_bridge_poke,
+};
+
+static int unload_module(void)
+{
+ return ast_bridge_technology_unregister(&softmix_bridge);
+}
+
+static int load_module(void)
+{
+ return ast_bridge_technology_register(&softmix_bridge);
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multi-party software based channel mixing");