Merge "chan_dahdi: Add faxdetect_timeout option."
[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         <support_level>core</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 ASTERISK_REGISTER_FILE()
37
38 #include "asterisk/channel.h"
39 #include "asterisk/module.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/acl.h"
42 #include "asterisk/app.h"
43 #include "asterisk/rtp_engine.h"
44 #include "asterisk/causes.h"
45 #include "asterisk/format_cache.h"
46 #include "asterisk/multicast_rtp.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 /*! \brief Function called when we should prepare to call the multicast destination */
123 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)
124 {
125         char *parse;
126         struct ast_rtp_instance *instance;
127         struct ast_sockaddr control_address;
128         struct ast_sockaddr destination_address;
129         struct ast_channel *chan;
130         struct ast_format_cap *caps = NULL;
131         struct ast_format *fmt = NULL;
132         AST_DECLARE_APP_ARGS(args,
133                 AST_APP_ARG(type);
134                 AST_APP_ARG(destination);
135                 AST_APP_ARG(control);
136                 AST_APP_ARG(options);
137         );
138         struct ast_multicast_rtp_options *mcast_options = NULL;
139
140         if (ast_strlen_zero(data)) {
141                 ast_log(LOG_ERROR, "A multicast type and destination must be given to the 'MulticastRTP' channel\n");
142                 goto failure;
143         }
144         parse = ast_strdupa(data);
145         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
146
147         if (ast_strlen_zero(args.type)) {
148                 ast_log(LOG_ERROR, "Type is required for the 'MulticastRTP' channel\n");
149                 goto failure;
150         }
151
152         if (ast_strlen_zero(args.destination)) {
153                 ast_log(LOG_ERROR, "Destination is required for the 'MulticastRTP' channel\n");
154                 goto failure;
155         }
156         if (!ast_sockaddr_parse(&destination_address, args.destination, PARSE_PORT_REQUIRE)) {
157                 ast_log(LOG_ERROR, "Destination address '%s' could not be parsed\n",
158                         args.destination);
159                 goto failure;
160         }
161
162         ast_sockaddr_setnull(&control_address);
163         if (!ast_strlen_zero(args.control)
164                 && !ast_sockaddr_parse(&control_address, args.control, PARSE_PORT_REQUIRE)) {
165                 ast_log(LOG_ERROR, "Control address '%s' could not be parsed\n", args.control);
166                 goto failure;
167         }
168
169         mcast_options = ast_multicast_rtp_create_options(args.type, args.options);
170         if (!mcast_options) {
171                 goto failure;
172         }
173
174         fmt = ast_multicast_rtp_options_get_format(mcast_options);
175         if (!fmt) {
176                 fmt = ast_format_cap_get_format(cap, 0);
177         }
178         if (!fmt) {
179                 ast_log(LOG_ERROR, "No codec available for sending RTP to '%s'\n",
180                         args.destination);
181                 goto failure;
182         }
183
184         caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
185         if (!caps) {
186                 goto failure;
187         }
188
189         instance = ast_rtp_instance_new("multicast", NULL, &control_address, mcast_options);
190         if (!instance) {
191                 ast_log(LOG_ERROR,
192                         "Could not create '%s' multicast RTP instance for sending media to '%s'\n",
193                         args.type, args.destination);
194                 goto failure;
195         }
196
197         chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", assignedids,
198                 requestor, 0, "MulticastRTP/%p", instance);
199         if (!chan) {
200                 ast_rtp_instance_destroy(instance);
201                 goto failure;
202         }
203         ast_rtp_instance_set_channel_id(instance, ast_channel_uniqueid(chan));
204         ast_rtp_instance_set_remote_address(instance, &destination_address);
205
206         ast_channel_tech_set(chan, &multicast_rtp_tech);
207
208         ast_format_cap_append(caps, fmt, 0);
209         ast_channel_nativeformats_set(chan, caps);
210         ast_channel_set_writeformat(chan, fmt);
211         ast_channel_set_rawwriteformat(chan, fmt);
212         ast_channel_set_readformat(chan, fmt);
213         ast_channel_set_rawreadformat(chan, fmt);
214
215         ast_channel_tech_pvt_set(chan, instance);
216
217         ast_channel_unlock(chan);
218
219         ao2_ref(fmt, -1);
220         ao2_ref(caps, -1);
221         ast_multicast_rtp_free_options(mcast_options);
222
223         return chan;
224
225 failure:
226         ao2_cleanup(fmt);
227         ao2_cleanup(caps);
228         ast_multicast_rtp_free_options(mcast_options);
229         *cause = AST_CAUSE_FAILURE;
230         return NULL;
231 }
232
233 enum {
234         OPT_RTP_CODEC =  (1 << 0),
235         OPT_RTP_ENGINE = (1 << 1),
236 };
237
238 enum {
239         OPT_ARG_RTP_CODEC,
240         OPT_ARG_RTP_ENGINE,
241         /* note: this entry _MUST_ be the last one in the enum */
242         OPT_ARG_ARRAY_SIZE
243 };
244
245 AST_APP_OPTIONS(unicast_rtp_options, BEGIN_OPTIONS
246         /*! Set the codec to be used for unicast RTP */
247         AST_APP_OPTION_ARG('c', OPT_RTP_CODEC, OPT_ARG_RTP_CODEC),
248         /*! Set the RTP engine to use for unicast RTP */
249         AST_APP_OPTION_ARG('e', OPT_RTP_ENGINE, OPT_ARG_RTP_ENGINE),
250 END_OPTIONS );
251
252 /*! \brief Function called when we should prepare to call the unicast destination */
253 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)
254 {
255         char *parse;
256         struct ast_rtp_instance *instance;
257         struct ast_sockaddr address;
258         struct ast_sockaddr local_address;
259         struct ast_channel *chan;
260         struct ast_format_cap *caps = NULL;
261         struct ast_format *fmt = NULL;
262         const char *engine_name;
263         AST_DECLARE_APP_ARGS(args,
264                 AST_APP_ARG(destination);
265                 AST_APP_ARG(options);
266         );
267         struct ast_flags opts = { 0, };
268         char *opt_args[OPT_ARG_ARRAY_SIZE];
269
270         if (ast_strlen_zero(data)) {
271                 ast_log(LOG_ERROR, "Destination is required for the 'UnicastRTP' channel\n");
272                 goto failure;
273         }
274         parse = ast_strdupa(data);
275         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
276
277         if (ast_strlen_zero(args.destination)) {
278                 ast_log(LOG_ERROR, "Destination is required for the 'UnicastRTP' channel\n");
279                 goto failure;
280         }
281         if (!ast_sockaddr_parse(&address, args.destination, PARSE_PORT_REQUIRE)) {
282                 ast_log(LOG_ERROR, "Destination '%s' could not be parsed\n", args.destination);
283                 goto failure;
284         }
285
286         if (!ast_strlen_zero(args.options)
287                 && ast_app_parse_options(unicast_rtp_options, &opts, opt_args,
288                         ast_strdupa(args.options))) {
289                 ast_log(LOG_ERROR, "'UnicastRTP' channel options '%s' parse error\n",
290                         args.options);
291                 goto failure;
292         }
293
294         if (ast_test_flag(&opts, OPT_RTP_CODEC)
295                 && !ast_strlen_zero(opt_args[OPT_ARG_RTP_CODEC])) {
296                 fmt = ast_format_cache_get(opt_args[OPT_ARG_RTP_CODEC]);
297                 if (!fmt) {
298                         ast_log(LOG_ERROR, "Codec '%s' not found for sending RTP to '%s'\n",
299                                 opt_args[OPT_ARG_RTP_CODEC], args.destination);
300                         goto failure;
301                 }
302         } else {
303                 fmt = ast_format_cap_get_format(cap, 0);
304                 if (!fmt) {
305                         ast_log(LOG_ERROR, "No codec available for sending RTP to '%s'\n",
306                                 args.destination);
307                         goto failure;
308                 }
309         }
310
311         caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
312         if (!caps) {
313                 goto failure;
314         }
315
316         engine_name = S_COR(ast_test_flag(&opts, OPT_RTP_ENGINE),
317                 opt_args[OPT_ARG_RTP_ENGINE], NULL);
318
319         ast_ouraddrfor(&address, &local_address);
320         instance = ast_rtp_instance_new(engine_name, NULL, &local_address, NULL);
321         if (!instance) {
322                 ast_log(LOG_ERROR,
323                         "Could not create %s RTP instance for sending media to '%s'\n",
324                         S_OR(engine_name, "default"), args.destination);
325                 goto failure;
326         }
327
328         chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", assignedids,
329                 requestor, 0, "UnicastRTP/%s-%p", args.destination, instance);
330         if (!chan) {
331                 ast_rtp_instance_destroy(instance);
332                 goto failure;
333         }
334         ast_rtp_instance_set_channel_id(instance, ast_channel_uniqueid(chan));
335         ast_rtp_instance_set_remote_address(instance, &address);
336         ast_channel_set_fd(chan, 0, ast_rtp_instance_fd(instance, 0));
337
338         ast_channel_tech_set(chan, &unicast_rtp_tech);
339
340         ast_format_cap_append(caps, fmt, 0);
341         ast_channel_nativeformats_set(chan, caps);
342         ast_channel_set_writeformat(chan, fmt);
343         ast_channel_set_rawwriteformat(chan, fmt);
344         ast_channel_set_readformat(chan, fmt);
345         ast_channel_set_rawreadformat(chan, fmt);
346
347         ast_channel_tech_pvt_set(chan, instance);
348
349         pbx_builtin_setvar_helper(chan, "UNICASTRTP_LOCAL_ADDRESS",
350                 ast_sockaddr_stringify_addr(&local_address));
351         ast_rtp_instance_get_local_address(instance, &local_address);
352         pbx_builtin_setvar_helper(chan, "UNICASTRTP_LOCAL_PORT",
353                 ast_sockaddr_stringify_port(&local_address));
354
355         ast_channel_unlock(chan);
356
357         ao2_ref(fmt, -1);
358         ao2_ref(caps, -1);
359
360         return chan;
361
362 failure:
363         ao2_cleanup(fmt);
364         ao2_cleanup(caps);
365         *cause = AST_CAUSE_FAILURE;
366         return NULL;
367 }
368
369 /*! \brief Function called when our module is unloaded */
370 static int unload_module(void)
371 {
372         ast_channel_unregister(&multicast_rtp_tech);
373         ao2_cleanup(multicast_rtp_tech.capabilities);
374         multicast_rtp_tech.capabilities = NULL;
375
376         ast_channel_unregister(&unicast_rtp_tech);
377         ao2_cleanup(unicast_rtp_tech.capabilities);
378         unicast_rtp_tech.capabilities = NULL;
379
380         return 0;
381 }
382
383 /*! \brief Function called when our module is loaded */
384 static int load_module(void)
385 {
386         if (!(multicast_rtp_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
387                 return AST_MODULE_LOAD_DECLINE;
388         }
389         ast_format_cap_append_by_type(multicast_rtp_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
390         if (ast_channel_register(&multicast_rtp_tech)) {
391                 ast_log(LOG_ERROR, "Unable to register channel class 'MulticastRTP'\n");
392                 unload_module();
393                 return AST_MODULE_LOAD_DECLINE;
394         }
395
396         if (!(unicast_rtp_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
397                 unload_module();
398                 return AST_MODULE_LOAD_DECLINE;
399         }
400         ast_format_cap_append_by_type(unicast_rtp_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
401         if (ast_channel_register(&unicast_rtp_tech)) {
402                 ast_log(LOG_ERROR, "Unable to register channel class 'UnicastRTP'\n");
403                 unload_module();
404                 return AST_MODULE_LOAD_DECLINE;
405         }
406
407         return AST_MODULE_LOAD_SUCCESS;
408 }
409
410 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "RTP Media Channel",
411         .support_level = AST_MODULE_SUPPORT_CORE,
412         .load = load_module,
413         .unload = unload_module,
414         .load_pri = AST_MODPRI_CHANNEL_DRIVER,
415 );