logger.conf.sample: add missing comment mark
[asterisk/asterisk.git] / channels / chan_rtp.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2009 - 2014, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  * Andreas 'MacBrody' Brodmann <andreas.brodmann@gmail.com>
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 <jcolp@digium.com>
23  * \author Andreas 'MacBrody' Broadmann <andreas.brodmann@gmail.com>
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                 fmt = ast_format_ulaw;
133         }
134
135         return fmt;
136 }
137
138 /*! \brief Function called when we should prepare to call the multicast destination */
139 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)
140 {
141         char *parse;
142         struct ast_rtp_instance *instance;
143         struct ast_sockaddr control_address;
144         struct ast_sockaddr destination_address;
145         struct ast_channel *chan;
146         struct ast_format_cap *caps = NULL;
147         struct ast_format *fmt = NULL;
148         AST_DECLARE_APP_ARGS(args,
149                 AST_APP_ARG(type);
150                 AST_APP_ARG(destination);
151                 AST_APP_ARG(control);
152                 AST_APP_ARG(options);
153         );
154         struct ast_multicast_rtp_options *mcast_options = NULL;
155
156         if (ast_strlen_zero(data)) {
157                 ast_log(LOG_ERROR, "A multicast type and destination must be given to the 'MulticastRTP' channel\n");
158                 goto failure;
159         }
160         parse = ast_strdupa(data);
161         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
162
163         if (ast_strlen_zero(args.type)) {
164                 ast_log(LOG_ERROR, "Type is required for the 'MulticastRTP' channel\n");
165                 goto failure;
166         }
167
168         if (ast_strlen_zero(args.destination)) {
169                 ast_log(LOG_ERROR, "Destination is required for the 'MulticastRTP' channel\n");
170                 goto failure;
171         }
172         if (!ast_sockaddr_parse(&destination_address, args.destination, PARSE_PORT_REQUIRE)) {
173                 ast_log(LOG_ERROR, "Destination address '%s' could not be parsed\n",
174                         args.destination);
175                 goto failure;
176         }
177
178         ast_sockaddr_setnull(&control_address);
179         if (!ast_strlen_zero(args.control)
180                 && !ast_sockaddr_parse(&control_address, args.control, PARSE_PORT_REQUIRE)) {
181                 ast_log(LOG_ERROR, "Control address '%s' could not be parsed\n", args.control);
182                 goto failure;
183         }
184
185         mcast_options = ast_multicast_rtp_create_options(args.type, args.options);
186         if (!mcast_options) {
187                 goto failure;
188         }
189
190         fmt = ast_multicast_rtp_options_get_format(mcast_options);
191         if (!fmt) {
192                 fmt = derive_format_from_cap(cap);
193         }
194         if (!fmt) {
195                 ast_log(LOG_ERROR, "No codec available for sending RTP to '%s'\n",
196                         args.destination);
197                 goto failure;
198         }
199
200         caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
201         if (!caps) {
202                 goto failure;
203         }
204
205         instance = ast_rtp_instance_new("multicast", NULL, &control_address, mcast_options);
206         if (!instance) {
207                 ast_log(LOG_ERROR,
208                         "Could not create '%s' multicast RTP instance for sending media to '%s'\n",
209                         args.type, args.destination);
210                 goto failure;
211         }
212
213         chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", assignedids,
214                 requestor, 0, "MulticastRTP/%p", instance);
215         if (!chan) {
216                 ast_rtp_instance_destroy(instance);
217                 goto failure;
218         }
219         ast_rtp_instance_set_channel_id(instance, ast_channel_uniqueid(chan));
220         ast_rtp_instance_set_remote_address(instance, &destination_address);
221
222         ast_channel_tech_set(chan, &multicast_rtp_tech);
223
224         ast_format_cap_append(caps, fmt, 0);
225         ast_channel_nativeformats_set(chan, caps);
226         ast_channel_set_writeformat(chan, fmt);
227         ast_channel_set_rawwriteformat(chan, fmt);
228         ast_channel_set_readformat(chan, fmt);
229         ast_channel_set_rawreadformat(chan, fmt);
230
231         ast_channel_tech_pvt_set(chan, instance);
232
233         ast_channel_unlock(chan);
234
235         ao2_ref(fmt, -1);
236         ao2_ref(caps, -1);
237         ast_multicast_rtp_free_options(mcast_options);
238
239         return chan;
240
241 failure:
242         ao2_cleanup(fmt);
243         ao2_cleanup(caps);
244         ast_multicast_rtp_free_options(mcast_options);
245         *cause = AST_CAUSE_FAILURE;
246         return NULL;
247 }
248
249 enum {
250         OPT_RTP_CODEC =  (1 << 0),
251         OPT_RTP_ENGINE = (1 << 1),
252 };
253
254 enum {
255         OPT_ARG_RTP_CODEC,
256         OPT_ARG_RTP_ENGINE,
257         /* note: this entry _MUST_ be the last one in the enum */
258         OPT_ARG_ARRAY_SIZE
259 };
260
261 AST_APP_OPTIONS(unicast_rtp_options, BEGIN_OPTIONS
262         /*! Set the codec to be used for unicast RTP */
263         AST_APP_OPTION_ARG('c', OPT_RTP_CODEC, OPT_ARG_RTP_CODEC),
264         /*! Set the RTP engine to use for unicast RTP */
265         AST_APP_OPTION_ARG('e', OPT_RTP_ENGINE, OPT_ARG_RTP_ENGINE),
266 END_OPTIONS );
267
268 /*! \brief Function called when we should prepare to call the unicast destination */
269 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)
270 {
271         char *parse;
272         struct ast_rtp_instance *instance;
273         struct ast_sockaddr address;
274         struct ast_sockaddr local_address;
275         struct ast_channel *chan;
276         struct ast_format_cap *caps = NULL;
277         struct ast_format *fmt = NULL;
278         const char *engine_name;
279         AST_DECLARE_APP_ARGS(args,
280                 AST_APP_ARG(destination);
281                 AST_APP_ARG(options);
282         );
283         struct ast_flags opts = { 0, };
284         char *opt_args[OPT_ARG_ARRAY_SIZE];
285
286         if (ast_strlen_zero(data)) {
287                 ast_log(LOG_ERROR, "Destination is required for the 'UnicastRTP' channel\n");
288                 goto failure;
289         }
290         parse = ast_strdupa(data);
291         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
292
293         if (ast_strlen_zero(args.destination)) {
294                 ast_log(LOG_ERROR, "Destination is required for the 'UnicastRTP' channel\n");
295                 goto failure;
296         }
297
298         if (!ast_sockaddr_parse(&address, args.destination, PARSE_PORT_REQUIRE)) {
299             int rc;
300             char *host;
301             char *port;
302
303             rc = ast_sockaddr_split_hostport(args.destination, &host, &port, PARSE_PORT_REQUIRE);
304             if (!rc) {
305                 ast_log(LOG_ERROR, "Unable to parse destination '%s' into host and port\n", args.destination);
306                 goto failure;
307             }
308
309             rc = ast_dns_resolve_ipv6_and_ipv4(&address, host, port);
310             if (rc != 0) {
311                 ast_log(LOG_ERROR, "Unable to resolve host '%s'\n", host);
312                 goto failure;
313             }
314         }
315
316         if (!ast_strlen_zero(args.options)
317                 && ast_app_parse_options(unicast_rtp_options, &opts, opt_args,
318                         ast_strdupa(args.options))) {
319                 ast_log(LOG_ERROR, "'UnicastRTP' channel options '%s' parse error\n",
320                         args.options);
321                 goto failure;
322         }
323
324         if (ast_test_flag(&opts, OPT_RTP_CODEC)
325                 && !ast_strlen_zero(opt_args[OPT_ARG_RTP_CODEC])) {
326                 fmt = ast_format_cache_get(opt_args[OPT_ARG_RTP_CODEC]);
327                 if (!fmt) {
328                         ast_log(LOG_ERROR, "Codec '%s' not found for sending RTP to '%s'\n",
329                                 opt_args[OPT_ARG_RTP_CODEC], args.destination);
330                         goto failure;
331                 }
332         } else {
333                 fmt = derive_format_from_cap(cap);
334                 if (!fmt) {
335                         ast_log(LOG_ERROR, "No codec available for sending RTP to '%s'\n",
336                                 args.destination);
337                         goto failure;
338                 }
339         }
340
341         caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
342         if (!caps) {
343                 goto failure;
344         }
345
346         engine_name = S_COR(ast_test_flag(&opts, OPT_RTP_ENGINE),
347                 opt_args[OPT_ARG_RTP_ENGINE], "asterisk");
348
349         ast_sockaddr_copy(&local_address, &address);
350         if (ast_ouraddrfor(&address, &local_address)) {
351                 ast_log(LOG_ERROR, "Could not get our address for sending media to '%s'\n",
352                         args.destination);
353                 goto failure;
354         }
355         instance = ast_rtp_instance_new(engine_name, NULL, &local_address, NULL);
356         if (!instance) {
357                 ast_log(LOG_ERROR,
358                         "Could not create %s RTP instance for sending media to '%s'\n",
359                         S_OR(engine_name, "default"), args.destination);
360                 goto failure;
361         }
362
363         chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", assignedids,
364                 requestor, 0, "UnicastRTP/%s-%p", args.destination, instance);
365         if (!chan) {
366                 ast_rtp_instance_destroy(instance);
367                 goto failure;
368         }
369         ast_rtp_instance_set_channel_id(instance, ast_channel_uniqueid(chan));
370         ast_rtp_instance_set_remote_address(instance, &address);
371         ast_channel_set_fd(chan, 0, ast_rtp_instance_fd(instance, 0));
372
373         ast_channel_tech_set(chan, &unicast_rtp_tech);
374
375         ast_format_cap_append(caps, fmt, 0);
376         ast_channel_nativeformats_set(chan, caps);
377         ast_channel_set_writeformat(chan, fmt);
378         ast_channel_set_rawwriteformat(chan, fmt);
379         ast_channel_set_readformat(chan, fmt);
380         ast_channel_set_rawreadformat(chan, fmt);
381
382         ast_channel_tech_pvt_set(chan, instance);
383
384         pbx_builtin_setvar_helper(chan, "UNICASTRTP_LOCAL_ADDRESS",
385                 ast_sockaddr_stringify_addr(&local_address));
386         ast_rtp_instance_get_local_address(instance, &local_address);
387         pbx_builtin_setvar_helper(chan, "UNICASTRTP_LOCAL_PORT",
388                 ast_sockaddr_stringify_port(&local_address));
389
390         ast_channel_unlock(chan);
391
392         ao2_ref(fmt, -1);
393         ao2_ref(caps, -1);
394
395         return chan;
396
397 failure:
398         ao2_cleanup(fmt);
399         ao2_cleanup(caps);
400         *cause = AST_CAUSE_FAILURE;
401         return NULL;
402 }
403
404 /*! \brief Function called when our module is unloaded */
405 static int unload_module(void)
406 {
407         ast_channel_unregister(&multicast_rtp_tech);
408         ao2_cleanup(multicast_rtp_tech.capabilities);
409         multicast_rtp_tech.capabilities = NULL;
410
411         ast_channel_unregister(&unicast_rtp_tech);
412         ao2_cleanup(unicast_rtp_tech.capabilities);
413         unicast_rtp_tech.capabilities = NULL;
414
415         return 0;
416 }
417
418 /*! \brief Function called when our module is loaded */
419 static int load_module(void)
420 {
421         if (!(multicast_rtp_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
422                 return AST_MODULE_LOAD_DECLINE;
423         }
424         ast_format_cap_append_by_type(multicast_rtp_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
425         if (ast_channel_register(&multicast_rtp_tech)) {
426                 ast_log(LOG_ERROR, "Unable to register channel class 'MulticastRTP'\n");
427                 unload_module();
428                 return AST_MODULE_LOAD_DECLINE;
429         }
430
431         if (!(unicast_rtp_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
432                 unload_module();
433                 return AST_MODULE_LOAD_DECLINE;
434         }
435         ast_format_cap_append_by_type(unicast_rtp_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
436         if (ast_channel_register(&unicast_rtp_tech)) {
437                 ast_log(LOG_ERROR, "Unable to register channel class 'UnicastRTP'\n");
438                 unload_module();
439                 return AST_MODULE_LOAD_DECLINE;
440         }
441
442         return AST_MODULE_LOAD_SUCCESS;
443 }
444
445 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "RTP Media Channel",
446         .support_level = AST_MODULE_SUPPORT_CORE,
447         .load = load_module,
448         .unload = unload_module,
449         .load_pri = AST_MODPRI_CHANNEL_DRIVER,
450         .requires = "res_rtp_multicast",
451 );