3e53b31c0e1849b7f361927228954c54e6e8d921
[asterisk/asterisk.git] / bridges / bridge_simple.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Simple two channel bridging module
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  *
25  * \ingroup bridges
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 #include "asterisk/module.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/bridging.h"
45 #include "asterisk/bridging_technology.h"
46 #include "asterisk/frame.h"
47
48 static int simple_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
49 {
50         struct ast_channel *c0 = AST_LIST_FIRST(&bridge->channels)->chan;
51         struct ast_channel *c1 = AST_LIST_LAST(&bridge->channels)->chan;
52
53         /* If this is the first channel we can't make it compatible... unless we make it compatible with itself O.o */
54         if (AST_LIST_FIRST(&bridge->channels) == AST_LIST_LAST(&bridge->channels)) {
55                 return 0;
56         }
57
58         /* See if we need to make these compatible */
59         if ((ast_format_cmp(ast_channel_writeformat(c0), ast_channel_readformat(c1)) == AST_FORMAT_CMP_EQUAL) &&
60                 (ast_format_cmp(ast_channel_readformat(c0), ast_channel_writeformat(c1)) == AST_FORMAT_CMP_EQUAL) &&
61                 (ast_format_cap_identical(ast_channel_nativeformats(c0), ast_channel_nativeformats(c1)))) {
62                 return 0;
63         }
64
65         /* BOOM! We do. */
66         return ast_channel_make_compatible(c0, c1);
67 }
68
69 static int simple_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
70 {
71         struct ast_bridge_channel *other;
72
73         /* Find the channel we actually want to write to */
74         other = ast_bridge_channel_peer(bridge_channel);
75         if (!other) {
76                 return -1;
77         }
78
79         /* The bridging core takes care of freeing the passed in frame. */
80         ast_bridge_channel_queue_frame(other, frame);
81
82         return 0;
83 }
84
85 static struct ast_bridge_technology simple_bridge = {
86         .name = "simple_bridge",
87         .capabilities = AST_BRIDGE_CAPABILITY_1TO1MIX,
88         .preference = AST_BRIDGE_PREFERENCE_BASE_1TO1MIX,
89         .join = simple_bridge_join,
90         .write = simple_bridge_write,
91 };
92
93 static int unload_module(void)
94 {
95         ast_format_cap_destroy(simple_bridge.format_capabilities);
96         return ast_bridge_technology_unregister(&simple_bridge);
97 }
98
99 static int load_module(void)
100 {
101         if (!(simple_bridge.format_capabilities = ast_format_cap_alloc())) {
102                 return AST_MODULE_LOAD_DECLINE;
103         }
104         ast_format_cap_add_all_by_type(simple_bridge.format_capabilities, AST_FORMAT_TYPE_AUDIO);
105         ast_format_cap_add_all_by_type(simple_bridge.format_capabilities, AST_FORMAT_TYPE_VIDEO);
106         ast_format_cap_add_all_by_type(simple_bridge.format_capabilities, AST_FORMAT_TYPE_TEXT);
107
108         return ast_bridge_technology_register(&simple_bridge);
109 }
110
111 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Simple two channel bridging module");