Debian Codegraph @

channels/chan_rtp.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2009 - 2014, Digium, Inc.
5 *
6 * Joshua Colp <[email protected]>
7 * Andreas 'MacBrody' Brodmann <[email protected]>
8 *
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
14 *
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
18 */
19
20 /*! \file
21 *
22 * \author Joshua Colp <[email protected]>
23 * \author Andreas 'MacBrody' Brodmann <[email protected]>
24 *
25 * \brief RTP (Multicast and Unicast) Media Channel
26 *
27 * \ingroup channel_drivers
28 */
29
30 /*** MODULEINFO
31 <depend>res_rtp_multicast</depend>
32 <support_level>core</support_level>
33 ***/
34
35 #include "asterisk.h"
36
37 #include "asterisk/channel.h"
38 #include "asterisk/module.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/acl.h"
41 #include "asterisk/app.h"
42 #include "asterisk/rtp_engine.h"
43 #include "asterisk/causes.h"
44 #include "asterisk/format_cache.h"
45 #include "asterisk/multicast_rtp.h"
46 #include "asterisk/dns_core.h"
47
48 /* Forward declarations */
49 static struct ast_channel *multicast_rtp_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
50 static struct ast_channel *unicast_rtp_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
51 static int rtp_call(struct ast_channel *ast, const char *dest, int timeout);
52 static int rtp_hangup(struct ast_channel *ast);
53 static struct ast_frame *rtp_read(struct ast_channel *ast);
54 static int rtp_write(struct ast_channel *ast, struct ast_frame *f);
55
56 /* Multicast channel driver declaration */
57 static struct ast_channel_tech multicast_rtp_tech = {
58 .type = "MulticastRTP",
59 .description = "Multicast RTP Paging Channel Driver",
60 .requester = multicast_rtp_request,
61 .call = rtp_call,
62 .hangup = rtp_hangup,
63 .read = rtp_read,
64 .write = rtp_write,
65 };
66
67 /* Unicast channel driver declaration */
68 static struct ast_channel_tech unicast_rtp_tech = {
69 .type = "UnicastRTP",
70 .description = "Unicast RTP Media Channel Driver",
71 .requester = unicast_rtp_request,
72 .call = rtp_call,
73 .hangup = rtp_hangup,
74 .read = rtp_read,
75 .write = rtp_write,
76 };
77
78 /*! \brief Function called when we should read a frame from the channel */
79 static struct ast_frame *rtp_read(struct ast_channel *ast)
80 {
81 struct ast_rtp_instance *instance = ast_channel_tech_pvt(ast);
82 int fdno = ast_channel_fdno(ast);
83
84 switch (fdno) {
85 case 0:
86 return ast_rtp_instance_read(instance, 0);
87 default:
88 return &ast_null_frame;
89 }
90 }
91
92 /*! \brief Function called when we should write a frame to the channel */
93 static int rtp_write(struct ast_channel *ast, struct ast_frame *f)
94 {
95 struct ast_rtp_instance *instance = ast_channel_tech_pvt(ast);
96
97 return ast_rtp_instance_write(instance, f);
98 }
99
100 /*! \brief Function called when we should actually call the destination */
101 static int rtp_call(struct ast_channel *ast, const char *dest, int timeout)
102 {
103 struct ast_rtp_instance *instance = ast_channel_tech_pvt(ast);
104
105 ast_queue_control(ast, AST_CONTROL_ANSWER);
106
107 return ast_rtp_instance_activate(instance);
108 }
109
110 /*! \brief Function called when we should hang the channel up */
111 static int rtp_hangup(struct ast_channel *ast)
112 {
113 struct ast_rtp_instance *instance = ast_channel_tech_pvt(ast);
114
115 ast_rtp_instance_destroy(instance);
116
117 ast_channel_tech_pvt_set(ast, NULL);
118
119 return 0;
120 }
121
122 static struct ast_format *derive_format_from_cap(struct ast_format_cap *cap)
123 {
124 struct ast_format *fmt = ast_format_cap_get_format(cap, 0);
125
126 if (ast_format_cap_count(cap) == 1 && fmt == ast_format_slin) {
127 /*
128 * Because we have no SDP, we must use one of the static RTP payload
129 * assignments. Signed linear @ 8kHz does not map, so if that is our
130 * only capability, we force μ-law instead.
131 */
132 ao2_ref(fmt, -1);
133 fmt = ao2_bump(ast_format_ulaw);
134 }
135
136 return fmt;
137 }
138
139 /*! \brief Function called when we should prepare to call the multicast destination */
140 static struct ast_channel *multicast_rtp_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
141 {
142 char *parse;
143 struct ast_rtp_instance *instance;
144 struct ast_sockaddr control_address;
145 struct ast_sockaddr destination_address;
146 struct ast_channel *chan;
147 struct ast_format_cap *caps = NULL;
148 struct ast_format *fmt = NULL;
149 AST_DECLARE_APP_ARGS(args,
150 AST_APP_ARG(type);
151 AST_APP_ARG(destination);
152 AST_APP_ARG(control);
153 AST_APP_ARG(options);
154 );
155 struct ast_multicast_rtp_options *mcast_options = NULL;
156
157 if (ast_strlen_zero(data)) {
158 ast_log(LOG_ERROR, "A multicast type and destination must be given to the 'MulticastRTP' channel\n");
159 goto failure;
160 }
161 parse = ast_strdupa(data);
162 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
163
164 if (ast_strlen_zero(args.type)) {
165 ast_log(LOG_ERROR, "Type is required for the 'MulticastRTP' channel\n");
166 goto failure;
167 }
168
169 if (ast_strlen_zero(args.destination)) {
170 ast_log(LOG_ERROR, "Destination is required for the 'MulticastRTP' channel\n");
171 goto failure;
172 }
173 if (!ast_sockaddr_parse(&destination_address, args.destination, PARSE_PORT_REQUIRE)) {
174 ast_log(LOG_ERROR, "Destination address '%s' could not be parsed\n",
175 args.destination);
176 goto failure;
177 }
178
179 ast_sockaddr_setnull(&control_address);
180 if (!ast_strlen_zero(args.control)
181 && !ast_sockaddr_parse(&control_address, args.control, PARSE_PORT_REQUIRE)) {
182 ast_log(LOG_ERROR, "Control address '%s' could not be parsed\n", args.control);
183 goto failure;
184 }
185
186 mcast_options = ast_multicast_rtp_create_options(args.type, args.options);
187 if (!mcast_options) {
188 goto failure;
189 }
190
191 fmt = ast_multicast_rtp_options_get_format(mcast_options);
192 if (!fmt) {
193 fmt = derive_format_from_cap(cap);
194 }
195 if (!fmt) {
196 ast_log(LOG_ERROR, "No codec available for sending RTP to '%s'\n",
197 args.destination);
198 goto failure;
199 }
200
201 caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
202 if (!caps) {
203 goto failure;
204 }
205
206 instance = ast_rtp_instance_new("multicast", NULL, &control_address, mcast_options);
207 if (!instance) {
208 ast_log(LOG_ERROR,
209 "Could not create '%s' multicast RTP instance for sending media to '%s'\n",
210 args.type, args.destination);
211 goto failure;
212 }
213
214 chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", assignedids,
215 requestor, 0, "MulticastRTP/%s-%p", args.destination, instance);
216 if (!chan) {
217 ast_rtp_instance_destroy(instance);
218 goto failure;
219 }
220 /* Prevent device state caching as this channel involves ephemeral destinations or sources */
221 ast_set_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE);
222 ast_rtp_instance_set_channel_id(instance, ast_channel_uniqueid(chan));
223 ast_rtp_instance_set_remote_address(instance, &destination_address);
224
225 ast_channel_tech_set(chan, &multicast_rtp_tech);
226
227 ast_format_cap_append(caps, fmt, 0);
228 ast_channel_nativeformats_set(chan, caps);
229 ast_channel_set_writeformat(chan, fmt);
230 ast_channel_set_rawwriteformat(chan, fmt);
231 ast_channel_set_readformat(chan, fmt);
232 ast_channel_set_rawreadformat(chan, fmt);
233
234 ast_channel_tech_pvt_set(chan, instance);
235
236 ast_channel_unlock(chan);
237
238 ao2_ref(fmt, -1);
239 ao2_ref(caps, -1);
240 ast_multicast_rtp_free_options(mcast_options);
241
242 return chan;
243
244 failure:
245 ao2_cleanup(fmt);
246 ao2_cleanup(caps);
247 ast_multicast_rtp_free_options(mcast_options);
248 *cause = AST_CAUSE_FAILURE;
249 return NULL;
250 }
251
252 enum {
253 OPT_RTP_CODEC = (1 << 0),
254 OPT_RTP_ENGINE = (1 << 1),
255 OPT_RTP_GLUE = (1 << 2),
256 };
257
258 enum {
259 OPT_ARG_RTP_CODEC,
260 OPT_ARG_RTP_ENGINE,
261 /* note: this entry _MUST_ be the last one in the enum */
262 OPT_ARG_ARRAY_SIZE
263 };
264
265 AST_APP_OPTIONS(unicast_rtp_options, BEGIN_OPTIONS
266 /*! Set the codec to be used for unicast RTP */
267 AST_APP_OPTION_ARG('c', OPT_RTP_CODEC, OPT_ARG_RTP_CODEC),
268 /*! Set the RTP engine to use for unicast RTP */
269 AST_APP_OPTION_ARG('e', OPT_RTP_ENGINE, OPT_ARG_RTP_ENGINE),
270 /*! Provide RTP glue for the channel */
271 AST_APP_OPTION('g', OPT_RTP_GLUE),
272 END_OPTIONS );
273
274 static const struct ast_datastore_info chan_rtp_datastore_info = {
275 .type = "CHAN_RTP_GLUE",
276 };
277
278 /*! \brief Function called when we should prepare to call the unicast destination */
279 static struct ast_channel *unicast_rtp_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
280 {
281 char *parse;
282 struct ast_rtp_instance *instance;
283 struct ast_sockaddr address;
284 struct ast_sockaddr local_address;
285 struct ast_channel *chan;
286 struct ast_format_cap *caps = NULL;
287 struct ast_format *fmt = NULL;
288 const char *engine_name;
289 AST_DECLARE_APP_ARGS(args,
290 AST_APP_ARG(destination);
291 AST_APP_ARG(options);
292 );
293 struct ast_flags opts = { 0, };
294 char *opt_args[OPT_ARG_ARRAY_SIZE];
295
296 if (ast_strlen_zero(data)) {
297 ast_log(LOG_ERROR, "Destination is required for the 'UnicastRTP' channel\n");
298 goto failure;
299 }
300 parse = ast_strdupa(data);
301 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
302
303 if (ast_strlen_zero(args.destination)) {
304 ast_log(LOG_ERROR, "Destination is required for the 'UnicastRTP' channel\n");
305 goto failure;
306 }
307
308 if (!ast_sockaddr_parse(&address, args.destination, PARSE_PORT_REQUIRE)) {
309 int rc;
310 char *host;
311 char *port;
312
313 rc = ast_sockaddr_split_hostport(args.destination, &host, &port, PARSE_PORT_REQUIRE);
314 if (!rc) {
315 ast_log(LOG_ERROR, "Unable to parse destination '%s' into host and port\n", args.destination);
316 goto failure;
317 }
318
319 rc = ast_dns_resolve_ipv6_and_ipv4(&address, host, port);
320 if (rc != 0) {
321 ast_log(LOG_ERROR, "Unable to resolve host '%s'\n", host);
322 goto failure;
323 }
324 }
325
326 if (!ast_strlen_zero(args.options)
327 && ast_app_parse_options(unicast_rtp_options, &opts, opt_args,
328 ast_strdupa(args.options))) {
329 ast_log(LOG_ERROR, "'UnicastRTP' channel options '%s' parse error\n",
330 args.options);
331 goto failure;
332 }
333
334 if (ast_test_flag(&opts, OPT_RTP_CODEC)
335 && !ast_strlen_zero(opt_args[OPT_ARG_RTP_CODEC])) {
336 fmt = ast_format_cache_get(opt_args[OPT_ARG_RTP_CODEC]);
337 if (!fmt) {
338 ast_log(LOG_ERROR, "Codec '%s' not found for sending RTP to '%s'\n",
339 opt_args[OPT_ARG_RTP_CODEC], args.destination);
340 goto failure;
341 }
342 } else {
343 fmt = derive_format_from_cap(cap);
344 if (!fmt) {
345 ast_log(LOG_ERROR, "No codec available for sending RTP to '%s'\n",
346 args.destination);
347 goto failure;
348 }
349 }
350
351 caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
352 if (!caps) {
353 goto failure;
354 }
355
356 engine_name = S_COR(ast_test_flag(&opts, OPT_RTP_ENGINE),
357 opt_args[OPT_ARG_RTP_ENGINE], "asterisk");
358
359 ast_sockaddr_copy(&local_address, &address);
360 if (ast_ouraddrfor(&address, &local_address)) {
361 ast_log(LOG_ERROR, "Could not get our address for sending media to '%s'\n",
362 args.destination);
363 goto failure;
364 }
365 instance = ast_rtp_instance_new(engine_name, NULL, &local_address, NULL);
366 if (!instance) {
367 ast_log(LOG_ERROR,
368 "Could not create %s RTP instance for sending media to '%s'\n",
369 S_OR(engine_name, "default"), args.destination);
370 goto failure;
371 }
372
373 chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", assignedids,
374 requestor, 0, "UnicastRTP/%s-%p", args.destination, instance);
375 if (!chan) {
376 ast_rtp_instance_destroy(instance);
377 goto failure;
378 }
379 /*Prevent device state caching as this channel involves ephemeral destinations or sources */
380 ast_set_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE);
381 ast_rtp_instance_set_channel_id(instance, ast_channel_uniqueid(chan));
382 ast_rtp_instance_set_remote_address(instance, &address);
383 ast_channel_set_fd(chan, 0, ast_rtp_instance_fd(instance, 0));
384
385 ast_channel_tech_set(chan, &unicast_rtp_tech);
386
387 if (ast_test_flag(&opts, OPT_RTP_GLUE)) {
388 struct ast_datastore *datastore;
389 if ((datastore = ast_datastore_alloc(&chan_rtp_datastore_info, NULL))) {
390 ast_channel_datastore_add(chan, datastore);
391 }
392 }
393
394 ast_format_cap_append(caps, fmt, 0);
395 ast_channel_nativeformats_set(chan, caps);
396 ast_channel_set_writeformat(chan, fmt);
397 ast_channel_set_rawwriteformat(chan, fmt);
398 ast_channel_set_readformat(chan, fmt);
399 ast_channel_set_rawreadformat(chan, fmt);
400
401 ast_channel_tech_pvt_set(chan, instance);
402
403 ast_rtp_instance_get_local_address(instance, &local_address);
404 pbx_builtin_setvar_helper(chan, "UNICASTRTP_LOCAL_ADDRESS",
405 ast_sockaddr_stringify_addr(&local_address));
406 pbx_builtin_setvar_helper(chan, "UNICASTRTP_LOCAL_PORT",
407 ast_sockaddr_stringify_port(&local_address));
408
409 ast_channel_unlock(chan);
410
411 ao2_ref(fmt, -1);
412 ao2_ref(caps, -1);
413
414 return chan;
415
416 failure:
417 ao2_cleanup(fmt);
418 ao2_cleanup(caps);
419 *cause = AST_CAUSE_FAILURE;
420 return NULL;
421 }
422
423 /*! \brief Function called by RTP engine to get peer capabilities */
424 static void chan_rtp_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
425 {
426 SCOPE_ENTER(1, "%s Native formats %s\n", ast_channel_name(chan),
427 ast_str_tmp(AST_FORMAT_CAP_NAMES_LEN, ast_format_cap_get_names(ast_channel_nativeformats(chan), &STR_TMP)));
428 ast_format_cap_append_from_cap(result, ast_channel_nativeformats(chan), AST_MEDIA_TYPE_UNKNOWN);
429 SCOPE_EXIT_RTN();
430 }
431
432 /*! \brief Function called by RTP engine to change where the remote party should send media.
433 *
434 * chan_rtp is not able to actually update the peer, so this function has no effect.
435 * */
436 static int chan_rtp_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, struct ast_rtp_instance *tpeer, const struct ast_format_cap *cap, int nat_active)
437 {
438 return -1;
439 }
440
441 /*! \brief Function called by RTP engine to get local audio RTP peer */
442 static enum ast_rtp_glue_result chan_rtp_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
443 {
444 return AST_RTP_GLUE_RESULT_FORBID;
445 }
446
447 /*! \brief Function called by RTP engine to get local audio RTP peer */
448 static enum ast_rtp_glue_result chan_rtp_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
449 {
450 struct ast_rtp_instance *rtp_instance = ast_channel_tech_pvt(chan);
451 struct ast_datastore *datastore;
452
453 if (!rtp_instance) {
454 return AST_RTP_GLUE_RESULT_FORBID;
455 }
456
457 if ((datastore = ast_channel_datastore_find(chan, &chan_rtp_datastore_info, NULL))) {
458 ao2_ref(datastore, -1);
459
460 *instance = rtp_instance;
461 ao2_ref(*instance, +1);
462
463 return AST_RTP_GLUE_RESULT_LOCAL;
464 }
465
466 return AST_RTP_GLUE_RESULT_FORBID;
467 }
468
469 /*! \brief Local glue for interacting with the RTP engine core */
470 static struct ast_rtp_glue unicast_rtp_glue = {
471 .type = "UnicastRTP",
472 .get_rtp_info = chan_rtp_get_rtp_peer,
473 .get_vrtp_info = chan_rtp_get_vrtp_peer,
474 .get_codec = chan_rtp_get_codec,
475 .update_peer = chan_rtp_set_rtp_peer,
476 };
477
478 /*! \brief Function called when our module is unloaded */
479 static int unload_module(void)
480 {
481 ast_channel_unregister(&multicast_rtp_tech);
482 ao2_cleanup(multicast_rtp_tech.capabilities);
483 multicast_rtp_tech.capabilities = NULL;
484
485 ast_channel_unregister(&unicast_rtp_tech);
486 ao2_cleanup(unicast_rtp_tech.capabilities);
487 unicast_rtp_tech.capabilities = NULL;
488
489 ast_rtp_glue_unregister(&unicast_rtp_glue);
490
491 return 0;
492 }
493
494 /*! \brief Function called when our module is loaded */
495 static int load_module(void)
496 {
497 if (!(multicast_rtp_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
498 return AST_MODULE_LOAD_DECLINE;
499 }
500
501 ast_rtp_glue_register(&unicast_rtp_glue);
502
503 ast_format_cap_append_by_type(multicast_rtp_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
504 if (ast_channel_register(&multicast_rtp_tech)) {
505 ast_log(LOG_ERROR, "Unable to register channel class 'MulticastRTP'\n");
506 unload_module();
507 return AST_MODULE_LOAD_DECLINE;
508 }
509
510 if (!(unicast_rtp_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
511 unload_module();
512 return AST_MODULE_LOAD_DECLINE;
513 }
514 ast_format_cap_append_by_type(unicast_rtp_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
515 if (ast_channel_register(&unicast_rtp_tech)) {
516 ast_log(LOG_ERROR, "Unable to register channel class 'UnicastRTP'\n");
517 unload_module();
518 return AST_MODULE_LOAD_DECLINE;
519 }
520
521 return AST_MODULE_LOAD_SUCCESS;
522 }
523
524 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "RTP Media Channel",
525 .support_level = AST_MODULE_SUPPORT_CORE,
526 .load = load_module,
527 .unload = unload_module,
528 .load_pri = AST_MODPRI_CHANNEL_DRIVER,
529 .requires = "res_rtp_multicast",
530 );