Sat Apr 26 2014 22:01:29

Asterisk developer's documentation


bridge_simple.c
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2007, 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  * \brief Simple two channel bridging module
00022  *
00023  * \author Joshua Colp <jcolp@digium.com>
00024  *
00025  * \ingroup bridges
00026  */
00027 
00028 /*** MODULEINFO
00029    <support_level>core</support_level>
00030  ***/
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 356573 $")
00035 
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <sys/types.h>
00040 #include <sys/stat.h>
00041 
00042 #include "asterisk/module.h"
00043 #include "asterisk/channel.h"
00044 #include "asterisk/bridging.h"
00045 #include "asterisk/bridging_technology.h"
00046 #include "asterisk/frame.h"
00047 
00048 static int simple_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
00049 {
00050    struct ast_channel *c0 = AST_LIST_FIRST(&bridge->channels)->chan, *c1 = AST_LIST_LAST(&bridge->channels)->chan;
00051 
00052    /* If this is the first channel we can't make it compatible... unless we make it compatible with itself O.o */
00053    if (AST_LIST_FIRST(&bridge->channels) == AST_LIST_LAST(&bridge->channels)) {
00054       return 0;
00055    }
00056 
00057    /* See if we need to make these compatible */
00058    if ((ast_format_cmp(ast_channel_writeformat(c0), ast_channel_readformat(c1)) == AST_FORMAT_CMP_EQUAL) &&
00059       (ast_format_cmp(ast_channel_readformat(c0), ast_channel_writeformat(c1)) == AST_FORMAT_CMP_EQUAL) &&
00060       (ast_format_cap_identical(ast_channel_nativeformats(c0), ast_channel_nativeformats(c1)))) {
00061       return 0;
00062    }
00063 
00064    /* BOOM! We do. */
00065    return ast_channel_make_compatible(c0, c1);
00066 }
00067 
00068 static enum ast_bridge_write_result simple_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
00069 {
00070    struct ast_bridge_channel *other = NULL;
00071 
00072    /* If this is the only channel in this bridge then immediately exit */
00073    if (AST_LIST_FIRST(&bridge->channels) == AST_LIST_LAST(&bridge->channels)) {
00074       return AST_BRIDGE_WRITE_FAILED;
00075    }
00076 
00077    /* Find the channel we actually want to write to */
00078    if (!(other = (AST_LIST_FIRST(&bridge->channels) == bridge_channel ? AST_LIST_LAST(&bridge->channels) : AST_LIST_FIRST(&bridge->channels)))) {
00079       return AST_BRIDGE_WRITE_FAILED;
00080    }
00081 
00082    /* 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 */
00083    if (other->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
00084       ast_write(other->chan, frame);
00085    }
00086 
00087    return AST_BRIDGE_WRITE_SUCCESS;
00088 }
00089 
00090 static struct ast_bridge_technology simple_bridge = {
00091    .name = "simple_bridge",
00092    .capabilities = AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_THREAD,
00093    .preference = AST_BRIDGE_PREFERENCE_MEDIUM,
00094    .join = simple_bridge_join,
00095    .write = simple_bridge_write,
00096 };
00097 
00098 static int unload_module(void)
00099 {
00100    ast_format_cap_destroy(simple_bridge.format_capabilities);
00101    return ast_bridge_technology_unregister(&simple_bridge);
00102 }
00103 
00104 static int load_module(void)
00105 {
00106    if (!(simple_bridge.format_capabilities = ast_format_cap_alloc())) {
00107       return AST_MODULE_LOAD_DECLINE;
00108    }
00109    ast_format_cap_add_all_by_type(simple_bridge.format_capabilities, AST_FORMAT_TYPE_AUDIO);
00110    ast_format_cap_add_all_by_type(simple_bridge.format_capabilities, AST_FORMAT_TYPE_VIDEO);
00111    ast_format_cap_add_all_by_type(simple_bridge.format_capabilities, AST_FORMAT_TYPE_TEXT);
00112 
00113    return ast_bridge_technology_register(&simple_bridge);
00114 }
00115 
00116 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Simple two channel bridging module");