Wed May 16 06:33:24 2012

Asterisk developer's documentation


chan_bridge.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2008, Digium, Inc.
00005  *
00006  * Joshua Colp <jcolp@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \author Joshua Colp <jcolp@digium.com>
00022  *
00023  * \brief Bridge Interaction Channel
00024  *
00025  * \ingroup channel_drivers
00026  */
00027 
00028 /*** MODULEINFO
00029    <support_level>core</support_level>
00030  ***/
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 358907 $")
00035 
00036 #include <fcntl.h>
00037 #include <sys/signal.h>
00038 
00039 #include "asterisk/lock.h"
00040 #include "asterisk/channel.h"
00041 #include "asterisk/config.h"
00042 #include "asterisk/module.h"
00043 #include "asterisk/pbx.h"
00044 #include "asterisk/sched.h"
00045 #include "asterisk/io.h"
00046 #include "asterisk/acl.h"
00047 #include "asterisk/callerid.h"
00048 #include "asterisk/file.h"
00049 #include "asterisk/cli.h"
00050 #include "asterisk/app.h"
00051 #include "asterisk/bridging.h"
00052 #include "asterisk/astobj2.h"
00053 
00054 static struct ast_channel *bridge_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause);
00055 static int bridge_call(struct ast_channel *ast, const char *dest, int timeout);
00056 static int bridge_hangup(struct ast_channel *ast);
00057 static struct ast_frame *bridge_read(struct ast_channel *ast);
00058 static int bridge_write(struct ast_channel *ast, struct ast_frame *f);
00059 static struct ast_channel *bridge_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge);
00060 
00061 static struct ast_channel_tech bridge_tech = {
00062    .type = "Bridge",
00063    .description = "Bridge Interaction Channel",
00064    .requester = bridge_request,
00065    .call = bridge_call,
00066    .hangup = bridge_hangup,
00067    .read = bridge_read,
00068    .write = bridge_write,
00069    .write_video = bridge_write,
00070    .exception = bridge_read,
00071    .bridged_channel = bridge_bridgedchannel,
00072 };
00073 
00074 struct bridge_pvt {
00075    struct ast_channel *input;  /*!< Input channel - talking to source */
00076    struct ast_channel *output; /*!< Output channel - talking to bridge */
00077 };
00078 
00079 /*! \brief Called when the user of this channel wants to get the actual channel in the bridge */
00080 static struct ast_channel *bridge_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge)
00081 {
00082    struct bridge_pvt *p = ast_channel_tech_pvt(chan);
00083    return (chan == p->input) ? p->output : bridge;
00084 }
00085 
00086 /*! \brief Called when a frame should be read from the channel */
00087 static struct ast_frame  *bridge_read(struct ast_channel *ast)
00088 {
00089    return &ast_null_frame;
00090 }
00091 
00092 /*! \brief Called when a frame should be written out to a channel */
00093 static int bridge_write(struct ast_channel *ast, struct ast_frame *f)
00094 {
00095    struct bridge_pvt *p = ast_channel_tech_pvt(ast);
00096    struct ast_channel *other = NULL;
00097 
00098    ao2_lock(p);
00099    /* only write frames to output. */
00100    if (p->input == ast) {
00101       other = p->output;
00102       if (other) {
00103          ast_channel_ref(other);
00104       }
00105    }
00106    ao2_unlock(p);
00107 
00108    if (other) {
00109       ast_channel_unlock(ast);
00110       ast_queue_frame(other, f);
00111       ast_channel_lock(ast);
00112       other = ast_channel_unref(other);
00113    }
00114 
00115    return 0;
00116 }
00117 
00118 /*! \brief Called when the channel should actually be dialed */
00119 static int bridge_call(struct ast_channel *ast, const char *dest, int timeout)
00120 {
00121    struct bridge_pvt *p = ast_channel_tech_pvt(ast);
00122 
00123    /* If no bridge has been provided on the input channel, bail out */
00124    if (!ast_channel_internal_bridge(ast)) {
00125       return -1;
00126    }
00127 
00128    /* Impart the output channel upon the given bridge of the input channel */
00129    ast_bridge_impart(ast_channel_internal_bridge(p->input), p->output, NULL, NULL, 0);
00130 
00131    return 0;
00132 }
00133 
00134 /*! \brief Called when a channel should be hung up */
00135 static int bridge_hangup(struct ast_channel *ast)
00136 {
00137    struct bridge_pvt *p = ast_channel_tech_pvt(ast);
00138 
00139    if (!p) {
00140       return 0;
00141    }
00142 
00143    ao2_lock(p);
00144    if (p->input == ast) {
00145       p->input = NULL;
00146    } else if (p->output == ast) {
00147       p->output = NULL;
00148    }
00149    ao2_unlock(p);
00150 
00151    ast_channel_tech_pvt_set(ast, NULL);
00152    ao2_ref(p, -1);
00153 
00154    return 0;
00155 }
00156 
00157 /*! \brief Called when we want to place a call somewhere, but not actually call it... yet */
00158 static struct ast_channel *bridge_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause)
00159 {
00160    struct bridge_pvt *p = NULL;
00161    struct ast_format slin;
00162 
00163    /* Try to allocate memory for our very minimal pvt structure */
00164    if (!(p = ao2_alloc(sizeof(*p), NULL))) {
00165       return NULL;
00166    }
00167 
00168    /* Try to grab two Asterisk channels to use as input and output channels */
00169    if (!(p->input = ast_channel_alloc(1, AST_STATE_UP, 0, 0, "", "", "", requestor ? ast_channel_linkedid(requestor) : NULL, 0, "Bridge/%p-input", p))) {
00170       ao2_ref(p, -1);
00171       return NULL;
00172    }
00173    if (!(p->output = ast_channel_alloc(1, AST_STATE_UP, 0, 0, "", "", "", requestor ? ast_channel_linkedid(requestor) : NULL, 0, "Bridge/%p-output", p))) {
00174       p->input = ast_channel_release(p->input);
00175       ao2_ref(p, -1);
00176       return NULL;
00177    }
00178 
00179    /* Setup parameters on both new channels */
00180    ast_channel_tech_set(p->input, &bridge_tech);
00181    ast_channel_tech_set(p->output, &bridge_tech);
00182 
00183    ao2_ref(p, 2);
00184    ast_channel_tech_pvt_set(p->input, p);
00185    ast_channel_tech_pvt_set(p->output, p);
00186 
00187    ast_format_set(&slin, AST_FORMAT_SLINEAR, 0);
00188 
00189    ast_format_cap_add(ast_channel_nativeformats(p->input), &slin);
00190    ast_format_cap_add(ast_channel_nativeformats(p->output), &slin);
00191    ast_format_copy(ast_channel_readformat(p->input), &slin);
00192    ast_format_copy(ast_channel_readformat(p->output), &slin);
00193    ast_format_copy(ast_channel_rawreadformat(p->input), &slin);
00194    ast_format_copy(ast_channel_rawreadformat(p->output), &slin);
00195    ast_format_copy(ast_channel_writeformat(p->input), &slin);
00196    ast_format_copy(ast_channel_writeformat(p->output), &slin);
00197    ast_format_copy(ast_channel_rawwriteformat(p->input), &slin);
00198    ast_format_copy(ast_channel_rawwriteformat(p->output), &slin);
00199 
00200    ast_answer(p->output);
00201    ast_answer(p->input);
00202 
00203    /* remove the reference from the alloc. The channels now own the pvt. */
00204    ao2_ref(p, -1);
00205    return p->input;
00206 }
00207 
00208 /*! \brief Load module into PBX, register channel */
00209 static int load_module(void)
00210 {
00211    if (!(bridge_tech.capabilities = ast_format_cap_alloc())) {
00212       return AST_MODULE_LOAD_FAILURE;
00213    }
00214 
00215    ast_format_cap_add_all(bridge_tech.capabilities);
00216    /* Make sure we can register our channel type */
00217    if (ast_channel_register(&bridge_tech)) {
00218       ast_log(LOG_ERROR, "Unable to register channel class 'Bridge'\n");
00219       return AST_MODULE_LOAD_FAILURE;
00220    }
00221    return AST_MODULE_LOAD_SUCCESS;
00222 }
00223 
00224 /*! \brief Unload the bridge interaction channel from Asterisk */
00225 static int unload_module(void)
00226 {
00227    ast_channel_unregister(&bridge_tech);
00228    bridge_tech.capabilities = ast_format_cap_destroy(bridge_tech.capabilities);
00229    return 0;
00230 }
00231 
00232 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Bridge Interaction Channel",
00233    .load = load_module,
00234    .unload = unload_module,
00235    .load_pri = AST_MODPRI_CHANNEL_DRIVER,
00236 );

Generated on Wed May 16 06:33:24 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6