Debian Codegraph @

channels/chan_audiosocket.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2019, CyCore Systems, Inc
5 *
6 * Seán C McCord <[email protected]>
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 * \author Seán C McCord <[email protected]>
22 *
23 * \brief AudioSocket Channel
24 *
25 * \ingroup channel_drivers
26 */
27
28 /*** MODULEINFO
29 <depend>res_audiosocket</depend>
30 <support_level>extended</support_level>
31 ***/
32
33
34 #include "asterisk.h"
35 #include <uuid/uuid.h>
36
37 #include "asterisk/channel.h"
38 #include "asterisk/module.h"
39 #include "asterisk/res_audiosocket.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/acl.h"
42 #include "asterisk/app.h"
43 #include "asterisk/causes.h"
44 #include "asterisk/format_cache.h"
45
46 #define FD_OUTPUT 1 /* A fd of -1 means an error, 0 is stdin */
47
48 struct audiosocket_instance {
49 int svc; /* The file descriptor for the AudioSocket instance */
50 char server[50]; /* The IP/port of the server the instance is connected to */
51 char id[38]; /* The UUID identifying this AudioSocket instance */
52 } audiosocket_instance;
53
54 /* Forward declarations */
55 static struct ast_channel *audiosocket_request(const char *type,
56 struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids,
57 const struct ast_channel *requestor, const char *data, int *cause);
58 static int audiosocket_call(struct ast_channel *ast, const char *dest, int timeout);
59 static int audiosocket_hangup(struct ast_channel *ast);
60 static struct ast_frame *audiosocket_read(struct ast_channel *ast);
61 static int audiosocket_write(struct ast_channel *ast, struct ast_frame *f);
62 static int audiosocket_send_dtmf(struct ast_channel *ast, char digit, unsigned int duration);
63
64 /* AudioSocket channel driver declaration */
65 static struct ast_channel_tech audiosocket_channel_tech = {
66 .type = "AudioSocket",
67 .description = "AudioSocket Channel Driver",
68 .requester = audiosocket_request,
69 .call = audiosocket_call,
70 .hangup = audiosocket_hangup,
71 .read = audiosocket_read,
72 .write = audiosocket_write,
73 .send_digit_end = audiosocket_send_dtmf,
74 };
75
76 /*! \brief Function called when we should read a frame from the channel */
77 static struct ast_frame *audiosocket_read(struct ast_channel *ast)
78 {
79 struct audiosocket_instance *instance;
80 struct ast_frame *frame;
81 int hangup;
82
83
84 /* The channel should always be present from the API */
85 instance = ast_channel_tech_pvt(ast);
86 if (instance == NULL || instance->svc < FD_OUTPUT) {
87 return NULL;
88 }
89
90 frame = ast_audiosocket_receive_frame_with_hangup(instance->svc, &hangup);
91 if (!frame && !hangup) {
92 ast_log(LOG_ERROR, "Failed to receive frame from AudioSocket server '%s'"
93 " connected to channel '%s'\n", instance->server, ast_channel_name(ast));
94 }
95 return frame;
96 }
97
98 /*! \brief Function called when we should write a frame to the channel */
99 static int audiosocket_write(struct ast_channel *ast, struct ast_frame *f)
100 {
101 struct audiosocket_instance *instance;
102
103 /* The channel should always be present from the API */
104 instance = ast_channel_tech_pvt(ast);
105 if (instance == NULL || instance->svc < 1) {
106 return -1;
107 }
108
109 if (ast_audiosocket_send_frame(instance->svc, f)) {
110 ast_log(LOG_ERROR, "Failed to forward frame from channel '%s' to AudioSocket server '%s'\n",
111 ast_channel_name(ast), instance->server);
112 return -1;
113 }
114 return 0;
115 }
116
117 /*! \brief Function called when we should write a DTMF frame to the channel */
118 static int audiosocket_send_dtmf(struct ast_channel *ast, char digit, unsigned int duration)
119 {
120 struct audiosocket_instance *instance;
121 struct ast_frame f;
122
123 /* The channel should always be present from the API */
124 instance = ast_channel_tech_pvt(ast);
125 if (instance == NULL || instance->svc < 1) {
126 return -1;
127 }
128
129 f.frametype = AST_FRAME_DTMF;
130 f.subclass.integer = digit;
131 f.len = duration;
132
133 if (ast_audiosocket_send_frame(instance->svc, &f)) {
134 ast_log(LOG_ERROR, "Failed to forward DTMF frame from channel '%s' to AudioSocket server '%s'\n",
135 ast_channel_name(ast), instance->server);
136 return -1;
137 }
138 return 0;
139 }
140
141 /*! \brief Function called when we should actually call the destination */
142 static int audiosocket_call(struct ast_channel *ast, const char *dest, int timeout)
143 {
144 struct audiosocket_instance *instance = ast_channel_tech_pvt(ast);
145
146 ast_queue_control(ast, AST_CONTROL_ANSWER);
147
148 return ast_audiosocket_init(instance->svc, instance->id);
149 }
150
151 /*! \brief Function called when we should hang the channel up */
152 static int audiosocket_hangup(struct ast_channel *ast)
153 {
154 struct audiosocket_instance *instance;
155
156 /* The channel should always be present from the API */
157 instance = ast_channel_tech_pvt(ast);
158 if (instance != NULL && instance->svc > 0) {
159 close(instance->svc);
160 }
161
162 ast_channel_tech_pvt_set(ast, NULL);
163 ast_free(instance);
164
165 return 0;
166 }
167
168 enum {
169 OPT_AUDIOSOCKET_CODEC = (1 << 0),
170 };
171
172 enum {
173 OPT_ARG_AUDIOSOCKET_CODEC = (1 << 0),
174 OPT_ARG_ARRAY_SIZE
175 };
176
177 AST_APP_OPTIONS(audiosocket_options, BEGIN_OPTIONS
178 AST_APP_OPTION_ARG('c', OPT_AUDIOSOCKET_CODEC, OPT_ARG_AUDIOSOCKET_CODEC),
179 END_OPTIONS );
180
181 /*! \brief Function called when we should prepare to call the unicast destination */
182 static struct ast_channel *audiosocket_request(const char *type,
183 struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids,
184 const struct ast_channel *requestor, const char *data, int *cause)
185 {
186 char *parse;
187 struct audiosocket_instance *instance = NULL;
188 struct ast_sockaddr address;
189 struct ast_channel *chan;
190 struct ast_format_cap *caps = NULL;
191 struct ast_format *fmt = NULL;
192 uuid_t uu;
193 int fd = -1;
194 AST_DECLARE_APP_ARGS(args,
195 AST_APP_ARG(destination);
196 AST_APP_ARG(idStr);
197 AST_APP_ARG(options);
198 );
199 struct ast_flags opts = { 0, };
200 char *opt_args[OPT_ARG_ARRAY_SIZE];
201
202 if (ast_strlen_zero(data)) {
203 ast_log(LOG_ERROR, "Destination is required for the 'AudioSocket' channel\n");
204 goto failure;
205 }
206 parse = ast_strdupa(data);
207 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
208
209 if (ast_strlen_zero(args.destination)) {
210 ast_log(LOG_ERROR, "Destination is required for the 'AudioSocket' channel\n");
211 goto failure;
212 }
213 if (ast_sockaddr_resolve_first_af
214 (&address, args.destination, PARSE_PORT_REQUIRE, AST_AF_UNSPEC)) {
215 ast_log(LOG_ERROR, "Destination '%s' could not be parsed\n", args.destination);
216 goto failure;
217 }
218
219 if (ast_strlen_zero(args.idStr)) {
220 ast_log(LOG_ERROR, "UUID is required for the 'AudioSocket' channel\n");
221 goto failure;
222 }
223 if (uuid_parse(args.idStr, uu)) {
224 ast_log(LOG_ERROR, "Failed to parse UUID '%s'\n", args.idStr);
225 goto failure;
226 }
227
228 if (!ast_strlen_zero(args.options)
229 && ast_app_parse_options(audiosocket_options, &opts, opt_args,
230 ast_strdupa(args.options))) {
231 ast_log(LOG_ERROR, "'AudioSocket' channel options '%s' parse error\n",
232 args.options);
233 goto failure;
234 }
235
236 if (ast_test_flag(&opts, OPT_AUDIOSOCKET_CODEC)
237 && !ast_strlen_zero(opt_args[OPT_ARG_AUDIOSOCKET_CODEC])) {
238 fmt = ast_format_cache_get(opt_args[OPT_ARG_AUDIOSOCKET_CODEC]);
239 if (!fmt) {
240 ast_log(LOG_ERROR, "Codec '%s' not found for AudioSocket connection to '%s'\n",
241 opt_args[OPT_ARG_AUDIOSOCKET_CODEC], args.destination);
242 goto failure;
243 }
244 } else {
245 fmt = ast_format_cap_get_format(cap, 0);
246 if (!fmt) {
247 ast_log(LOG_ERROR, "No codec available for AudioSocket connection to '%s'\n",
248 args.destination);
249 goto failure;
250 }
251 }
252
253 caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
254 if (!caps) {
255 goto failure;
256 }
257
258 instance = ast_calloc(1, sizeof(*instance));
259 if (!instance) {
260 ast_log(LOG_ERROR, "Failed to allocate AudioSocket channel pvt\n");
261 goto failure;
262 }
263 ast_copy_string(instance->id, args.idStr, sizeof(instance->id));
264
265 if ((fd = ast_audiosocket_connect(args.destination, NULL)) < 0) {
266 goto failure;
267 }
268 /* Make a copy of the server address for display in error messages. */
269 ast_copy_string(instance->server, args.destination, sizeof(instance->server));
270 instance->svc = fd;
271
272 chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", assignedids,
273 requestor, 0, "AudioSocket/%s-%s", args.destination, args.idStr);
274 if (!chan) {
275 goto failure;
276 }
277 ast_channel_set_fd(chan, 0, fd);
278 /* Prevent device state caching as this channel involves ephemeral destinations or sources */
279 ast_set_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE);
280
281 ast_channel_tech_set(chan, &audiosocket_channel_tech);
282
283 ast_format_cap_append(caps, fmt, 0);
284 ast_channel_nativeformats_set(chan, caps);
285 ast_channel_set_writeformat(chan, fmt);
286 ast_channel_set_rawwriteformat(chan, fmt);
287 ast_channel_set_readformat(chan, fmt);
288 ast_channel_set_rawreadformat(chan, fmt);
289
290 ast_channel_tech_pvt_set(chan, instance);
291
292 pbx_builtin_setvar_helper(chan, "AUDIOSOCKET_UUID", args.idStr);
293 pbx_builtin_setvar_helper(chan, "AUDIOSOCKET_SERVICE", args.destination);
294
295 ast_channel_unlock(chan);
296
297 ao2_ref(fmt, -1);
298 ao2_ref(caps, -1);
299 return chan;
300
301 failure:
302 *cause = AST_CAUSE_FAILURE;
303 ao2_cleanup(fmt);
304 ao2_cleanup(caps);
305 if (instance != NULL) {
306 ast_free(instance);
307 if (fd >= 0) {
308 close(fd);
309 }
310 }
311
312 return NULL;
313 }
314
315 /*! \brief Function called when our module is unloaded */
316 static int unload_module(void)
317 {
318 ast_channel_unregister(&audiosocket_channel_tech);
319 ao2_cleanup(audiosocket_channel_tech.capabilities);
320 audiosocket_channel_tech.capabilities = NULL;
321
322 return 0;
323 }
324
325 /*! \brief Function called when our module is loaded */
326 static int load_module(void)
327 {
328 if (!(audiosocket_channel_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
329 return AST_MODULE_LOAD_DECLINE;
330 }
331 ast_format_cap_append_by_type(audiosocket_channel_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
332 if (ast_channel_register(&audiosocket_channel_tech)) {
333 ast_log(LOG_ERROR, "Unable to register channel class AudioSocket");
334 ao2_ref(audiosocket_channel_tech.capabilities, -1);
335 audiosocket_channel_tech.capabilities = NULL;
336 return AST_MODULE_LOAD_DECLINE;
337 }
338
339 return AST_MODULE_LOAD_SUCCESS;
340 }
341
342 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "AudioSocket Channel",
343 .support_level = AST_MODULE_SUPPORT_EXTENDED,
344 .load = load_module,
345 .unload = unload_module,
346 .load_pri = AST_MODPRI_CHANNEL_DRIVER,
347 .requires = "res_audiosocket",
348 );