asterisk/channels/chan_bridge.c

235 lines
6.6 KiB
C
Raw Normal View History

/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 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
*
* \author Joshua Colp <jcolp@digium.com>
*
* \brief Bridge Interaction Channel
*
* \ingroup channel_drivers
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include <fcntl.h>
#include <sys/signal.h>
#include "asterisk/lock.h"
#include "asterisk/channel.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/sched.h"
#include "asterisk/io.h"
#include "asterisk/acl.h"
#include "asterisk/callerid.h"
#include "asterisk/file.h"
#include "asterisk/cli.h"
#include "asterisk/app.h"
#include "asterisk/bridging.h"
#include "asterisk/astobj2.h"
static struct ast_channel *bridge_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause);
static int bridge_call(struct ast_channel *ast, const char *dest, int timeout);
static int bridge_hangup(struct ast_channel *ast);
static struct ast_frame *bridge_read(struct ast_channel *ast);
static int bridge_write(struct ast_channel *ast, struct ast_frame *f);
static struct ast_channel *bridge_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge);
static struct ast_channel_tech bridge_tech = {
.type = "Bridge",
.description = "Bridge Interaction Channel",
.requester = bridge_request,
.call = bridge_call,
.hangup = bridge_hangup,
.read = bridge_read,
.write = bridge_write,
.write_video = bridge_write,
.exception = bridge_read,
.bridged_channel = bridge_bridgedchannel,
};
struct bridge_pvt {
struct ast_channel *input; /*!< Input channel - talking to source */
struct ast_channel *output; /*!< Output channel - talking to bridge */
};
/*! \brief Called when the user of this channel wants to get the actual channel in the bridge */
static struct ast_channel *bridge_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge)
{
struct bridge_pvt *p = chan->tech_pvt;
return (chan == p->input) ? p->output : bridge;
}
/*! \brief Called when a frame should be read from the channel */
static struct ast_frame *bridge_read(struct ast_channel *ast)
{
return &ast_null_frame;
}
/*! \brief Called when a frame should be written out to a channel */
static int bridge_write(struct ast_channel *ast, struct ast_frame *f)
{
struct bridge_pvt *p = ast->tech_pvt;
struct ast_channel *other = NULL;
ao2_lock(p);
/* only write frames to output. */
if (p->input == ast) {
other = p->output;
if (other) {
ast_channel_ref(other);
}
}
ao2_unlock(p);
if (other) {
ast_channel_unlock(ast);
ast_queue_frame(other, f);
ast_channel_lock(ast);
other = ast_channel_unref(other);
}
return 0;
}
/*! \brief Called when the channel should actually be dialed */
static int bridge_call(struct ast_channel *ast, const char *dest, int timeout)
{
struct bridge_pvt *p = ast->tech_pvt;
/* If no bridge has been provided on the input channel, bail out */
if (!ast->bridge) {
return -1;
}
/* Impart the output channel upon the given bridge of the input channel */
ast_bridge_impart(p->input->bridge, p->output, NULL, NULL, 0);
return 0;
}
/*! \brief Called when a channel should be hung up */
static int bridge_hangup(struct ast_channel *ast)
{
struct bridge_pvt *p = ast->tech_pvt;
if (!p) {
return 0;
}
ao2_lock(p);
if (p->input == ast) {
p->input = NULL;
} else if (p->output == ast) {
p->output = NULL;
}
ao2_unlock(p);
ast->tech_pvt = NULL;
ao2_ref(p, -1);
return 0;
}
/*! \brief Called when we want to place a call somewhere, but not actually call it... yet */
static struct ast_channel *bridge_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause)
{
struct bridge_pvt *p = NULL;
struct ast_format slin;
/* Try to allocate memory for our very minimal pvt structure */
if (!(p = ao2_alloc(sizeof(*p), NULL))) {
return NULL;
}
/* Try to grab two Asterisk channels to use as input and output channels */
if (!(p->input = ast_channel_alloc(1, AST_STATE_UP, 0, 0, "", "", "", requestor ? ast_channel_linkedid(requestor) : NULL, 0, "Bridge/%p-input", p))) {
ao2_ref(p, -1);
return NULL;
}
if (!(p->output = ast_channel_alloc(1, AST_STATE_UP, 0, 0, "", "", "", requestor ? ast_channel_linkedid(requestor) : NULL, 0, "Bridge/%p-output", p))) {
Convert the ast_channel data structure over to the astobj2 framework. There is a lot that could be said about this, but the patch is a big improvement for performance, stability, code maintainability, and ease of future code development. The channel list is no longer an unsorted linked list. The main container for channels is an astobj2 hash table. All of the code related to searching for channels or iterating active channels has been rewritten. Let n be the number of active channels. Iterating the channel list has gone from O(n^2) to O(n). Searching for a channel by name went from O(n) to O(1). Searching for a channel by extension is still O(n), but uses a new method for doing so, which is more efficient. The ast_channel object is now a reference counted object. The benefits here are plentiful. Some benefits directly related to issues in the previous code include: 1) When threads other than the channel thread owning a channel wanted access to a channel, it had to hold the lock on it to ensure that it didn't go away. This is no longer a requirement. Holding a reference is sufficient. 2) There are places that now require less dealing with channel locks. 3) There are places where channel locks are held for much shorter periods of time. 4) There are places where dealing with more than one channel at a time becomes _MUCH_ easier. ChanSpy is a great example of this. Writing code in the future that deals with multiple channels will be much easier. Some additional information regarding channel locking and reference count handling can be found in channel.h, where a new section has been added that discusses some of the rules associated with it. Mark Michelson also assisted with the development of this patch. He did the conversion of ChanSpy and introduced a new API, ast_autochan, which makes it much easier to deal with holding on to a channel pointer for an extended period of time and having it get automatically updated if the channel gets masqueraded. Mark was also a huge help in the code review process. Thanks to David Vossel for his assistance with this branch, as well. David did the conversion of the DAHDIScan application by making it become a wrapper for ChanSpy internally. The changes come from the svn/asterisk/team/russell/ast_channel_ao2 branch. Review: http://reviewboard.digium.com/r/203/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@190423 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2009-04-24 14:04:26 +00:00
p->input = ast_channel_release(p->input);
ao2_ref(p, -1);
return NULL;
}
/* Setup parameters on both new channels */
p->input->tech = p->output->tech = &bridge_tech;
ao2_ref(p, 2);
p->input->tech_pvt = p->output->tech_pvt = p;
ast_format_set(&slin, AST_FORMAT_SLINEAR, 0);
ast_format_cap_add(p->input->nativeformats, &slin);
ast_format_cap_add(p->output->nativeformats, &slin);
ast_format_copy(&p->input->readformat, &slin);
ast_format_copy(&p->output->readformat, &slin);
ast_format_copy(&p->input->rawreadformat, &slin);
ast_format_copy(&p->output->rawreadformat, &slin);
ast_format_copy(&p->input->writeformat, &slin);
ast_format_copy(&p->output->writeformat, &slin);
ast_format_copy(&p->input->rawwriteformat, &slin);
ast_format_copy(&p->output->rawwriteformat, &slin);
ast_answer(p->output);
ast_answer(p->input);
/* remove the reference from the alloc. The channels now own the pvt. */
ao2_ref(p, -1);
return p->input;
}
/*! \brief Load module into PBX, register channel */
static int load_module(void)
{
if (!(bridge_tech.capabilities = ast_format_cap_alloc())) {
return AST_MODULE_LOAD_FAILURE;
}
ast_format_cap_add_all(bridge_tech.capabilities);
/* Make sure we can register our channel type */
if (ast_channel_register(&bridge_tech)) {
ast_log(LOG_ERROR, "Unable to register channel class 'Bridge'\n");
return AST_MODULE_LOAD_FAILURE;
}
return AST_MODULE_LOAD_SUCCESS;
}
/*! \brief Unload the bridge interaction channel from Asterisk */
static int unload_module(void)
{
ast_channel_unregister(&bridge_tech);
bridge_tech.capabilities = ast_format_cap_destroy(bridge_tech.capabilities);
return 0;
}
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Bridge Interaction Channel",
.load = load_module,
.unload = unload_module,
.load_pri = AST_MODPRI_CHANNEL_DRIVER,
);