ac15e7d7002e63ad92141d7e8f5f440d233bd197
[asterisk/asterisk.git] / channels / chan_bridge_media.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013 Digium, Inc.
5  *
6  * Jonathan Rose <jrose@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 /*!
20  * \file
21  * \brief Bridge Media Channels driver
22  *
23  * \author Jonathan Rose <jrose@digium.com>
24  * \author Richard Mudgett <rmudgett@digium.com>
25  *
26  * \brief Bridge Media Channels
27  *
28  * \ingroup channel_drivers
29  */
30
31 /*** MODULEINFO
32         <support_level>core</support_level>
33  ***/
34
35 #include "asterisk.h"
36
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38
39 #include "asterisk/channel.h"
40 #include "asterisk/bridge.h"
41 #include "asterisk/core_unreal.h"
42 #include "asterisk/module.h"
43
44 static int media_call(struct ast_channel *chan, const char *addr, int timeout)
45 {
46         /* ast_call() will fail unconditionally against channels provided by this driver */
47         return -1;
48 }
49
50 static int media_hangup(struct ast_channel *ast)
51 {
52         struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
53         int res;
54
55         if (!p) {
56                 return -1;
57         }
58
59         /* Give the pvt a ref to fulfill calling requirements. */
60         ao2_ref(p, +1);
61         res = ast_unreal_hangup(p, ast);
62         ao2_ref(p, -1);
63
64         return res;
65 }
66
67 static struct ast_channel *announce_request(const char *type, struct ast_format_cap *cap,
68         const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
69
70 static struct ast_channel *record_request(const char *type, struct ast_format_cap *cap,
71         const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
72
73 static struct ast_channel_tech announce_tech = {
74         .type = "Announcer",
75         .description = "Bridge Media Announcing Channel Driver",
76         .requester = announce_request,
77         .call = media_call,
78         .hangup = media_hangup,
79
80         .send_digit_begin = ast_unreal_digit_begin,
81         .send_digit_end = ast_unreal_digit_end,
82         .read = ast_unreal_read,
83         .write = ast_unreal_write,
84         .write_video = ast_unreal_write,
85         .exception = ast_unreal_read,
86         .indicate = ast_unreal_indicate,
87         .fixup = ast_unreal_fixup,
88         .send_html = ast_unreal_sendhtml,
89         .send_text = ast_unreal_sendtext,
90         .queryoption = ast_unreal_queryoption,
91         .setoption = ast_unreal_setoption,
92         .properties = AST_CHAN_TP_INTERNAL,
93 };
94
95 static struct ast_channel_tech record_tech = {
96         .type = "Recorder",
97         .description = "Bridge Media Recording Channel Driver",
98         .requester = record_request,
99         .call = media_call,
100         .hangup = media_hangup,
101
102         .send_digit_begin = ast_unreal_digit_begin,
103         .send_digit_end = ast_unreal_digit_end,
104         .read = ast_unreal_read,
105         .write = ast_unreal_write,
106         .write_video = ast_unreal_write,
107         .exception = ast_unreal_read,
108         .indicate = ast_unreal_indicate,
109         .fixup = ast_unreal_fixup,
110         .send_html = ast_unreal_sendhtml,
111         .send_text = ast_unreal_sendtext,
112         .queryoption = ast_unreal_queryoption,
113         .setoption = ast_unreal_setoption,
114         .properties = AST_CHAN_TP_INTERNAL,
115 };
116
117 static struct ast_channel *media_request_helper(struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids,
118         const struct ast_channel *requestor, const char *data, struct ast_channel_tech *tech, const char *role)
119 {
120         struct ast_channel *chan;
121
122         RAII_VAR(struct ast_callid *, callid, NULL, ast_callid_cleanup);
123         RAII_VAR(struct ast_unreal_pvt *, pvt, NULL, ao2_cleanup);
124
125         if (!(pvt = ast_unreal_alloc(sizeof(*pvt), ast_unreal_destructor, cap))) {
126                 return NULL;
127         }
128
129         ast_copy_string(pvt->name, data, sizeof(pvt->name));
130
131         ast_set_flag(pvt, AST_UNREAL_NO_OPTIMIZATION);
132
133         callid = ast_read_threadstorage_callid();
134
135         chan = ast_unreal_new_channels(pvt, tech,
136                 AST_STATE_UP, AST_STATE_UP, NULL, NULL, assignedids, requestor, callid);
137         if (!chan) {
138                 return NULL;
139         }
140
141         ast_answer(pvt->owner);
142         ast_answer(pvt->chan);
143
144         if (ast_channel_add_bridge_role(pvt->chan, role)) {
145                 ast_hangup(chan);
146                 return NULL;
147         }
148
149         return chan;
150 }
151
152 static struct ast_channel *announce_request(const char *type, struct ast_format_cap *cap,
153         const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
154 {
155         return media_request_helper(cap, assignedids, requestor, data, &announce_tech, "announcer");
156 }
157
158 static struct ast_channel *record_request(const char *type, struct ast_format_cap *cap,
159         const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
160 {
161         return media_request_helper(cap, assignedids, requestor, data, &record_tech, "recorder");
162 }
163
164 static void cleanup_capabilities(void)
165 {
166         if (announce_tech.capabilities) {
167                 announce_tech.capabilities = ast_format_cap_destroy(announce_tech.capabilities);
168         }
169
170         if (record_tech.capabilities) {
171                 record_tech.capabilities = ast_format_cap_destroy(record_tech.capabilities);
172         }
173 }
174
175 static int unload_module(void)
176 {
177         ast_channel_unregister(&announce_tech);
178         ast_channel_unregister(&record_tech);
179         cleanup_capabilities();
180         return 0;
181 }
182
183 static int load_module(void)
184 {
185         announce_tech.capabilities = ast_format_cap_alloc(0);
186         if (!announce_tech.capabilities) {
187                 return AST_MODULE_LOAD_DECLINE;
188         }
189
190         record_tech.capabilities = ast_format_cap_alloc(0);
191         if (!record_tech.capabilities) {
192                 return AST_MODULE_LOAD_DECLINE;
193         }
194
195         ast_format_cap_add_all(announce_tech.capabilities);
196         ast_format_cap_add_all(record_tech.capabilities);
197
198         if (ast_channel_register(&announce_tech)) {
199                 ast_log(LOG_ERROR, "Unable to register channel technology %s(%s).\n",
200                         announce_tech.type, announce_tech.description);
201                 cleanup_capabilities();
202                 return AST_MODULE_LOAD_DECLINE;
203         }
204
205         if (ast_channel_register(&record_tech)) {
206                 ast_log(LOG_ERROR, "Unable to register channel technology %s(%s).\n",
207                         record_tech.type, record_tech.description);
208                 cleanup_capabilities();
209                 return AST_MODULE_LOAD_DECLINE;
210         }
211
212         return AST_MODULE_LOAD_SUCCESS;
213 }
214
215 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Bridge Media Channel Driver",
216     .load = load_module,
217     .unload = unload_module,
218 );