Make --with-pjproject-bundled the default for Asterisk 15
[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 #include "asterisk/channel.h"
37 #include "asterisk/module.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/acl.h"
40 #include "asterisk/app.h"
41 #include "asterisk/rtp_engine.h"
42 #include "asterisk/causes.h"
43 #include "asterisk/format_cache.h"
44 #include "asterisk/multicast_rtp.h"
45
46 /* Forward declarations */
47 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);
48 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);
49 static int rtp_call(struct ast_channel *ast, const char *dest, int timeout);
50 static int rtp_hangup(struct ast_channel *ast);
51 static struct ast_frame *rtp_read(struct ast_channel *ast);
52 static int rtp_write(struct ast_channel *ast, struct ast_frame *f);
53
54 /* Multicast channel driver declaration */
55 static struct ast_channel_tech multicast_rtp_tech = {
56         .type = "MulticastRTP",
57         .description = "Multicast RTP Paging Channel Driver",
58         .requester = multicast_rtp_request,
59         .call = rtp_call,
60         .hangup = rtp_hangup,
61         .read = rtp_read,
62         .write = rtp_write,
63 };
64
65 /* Unicast channel driver declaration */
66 static struct ast_channel_tech unicast_rtp_tech = {
67         .type = "UnicastRTP",
68         .description = "Unicast RTP Media Channel Driver",
69         .requester = unicast_rtp_request,
70         .call = rtp_call,
71         .hangup = rtp_hangup,
72         .read = rtp_read,
73         .write = rtp_write,
74 };
75
76 /*! \brief Function called when we should read a frame from the channel */
77 static struct ast_frame  *rtp_read(struct ast_channel *ast)
78 {
79         struct ast_rtp_instance *instance = ast_channel_tech_pvt(ast);
80         int fdno = ast_channel_fdno(ast);
81
82         switch (fdno) {
83         case 0:
84                 return ast_rtp_instance_read(instance, 0);
85         default:
86                 return &ast_null_frame;
87         }
88 }
89
90 /*! \brief Function called when we should write a frame to the channel */
91 static int rtp_write(struct ast_channel *ast, struct ast_frame *f)
92 {
93         struct ast_rtp_instance *instance = ast_channel_tech_pvt(ast);
94
95         return ast_rtp_instance_write(instance, f);
96 }
97
98 /*! \brief Function called when we should actually call the destination */
99 static int rtp_call(struct ast_channel *ast, const char *dest, int timeout)
100 {
101         struct ast_rtp_instance *instance = ast_channel_tech_pvt(ast);
102
103         ast_queue_control(ast, AST_CONTROL_ANSWER);
104
105         return ast_rtp_instance_activate(instance);
106 }
107
108 /*! \brief Function called when we should hang the channel up */
109 static int rtp_hangup(struct ast_channel *ast)
110 {
111         struct ast_rtp_instance *instance = ast_channel_tech_pvt(ast);
112
113         ast_rtp_instance_destroy(instance);
114
115         ast_channel_tech_pvt_set(ast, NULL);
116
117         return 0;
118 }
119
120 /*! \brief Function called when we should prepare to call the multicast destination */
121 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)
122 {
123         char *parse;
124         struct ast_rtp_instance *instance;
125         struct ast_sockaddr control_address;
126         struct ast_sockaddr destination_address;
127         struct ast_channel *chan;
128         struct ast_format_cap *caps = NULL;
129         struct ast_format *fmt = NULL;
130         AST_DECLARE_APP_ARGS(args,
131                 AST_APP_ARG(type);
132                 AST_APP_ARG(destination);
133                 AST_APP_ARG(control);
134                 AST_APP_ARG(options);
135         );
136         struct ast_multicast_rtp_options *mcast_options = NULL;
137
138         if (ast_strlen_zero(data)) {
139                 ast_log(LOG_ERROR, "A multicast type and destination must be given to the 'MulticastRTP' channel\n");
140                 goto failure;
141         }
142         parse = ast_strdupa(data);
143         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
144
145         if (ast_strlen_zero(args.type)) {
146                 ast_log(LOG_ERROR, "Type is required for the 'MulticastRTP' channel\n");
147                 goto failure;
148         }
149
150         if (ast_strlen_zero(args.destination)) {
151                 ast_log(LOG_ERROR, "Destination is required for the 'MulticastRTP' channel\n");
152                 goto failure;
153         }
154         if (!ast_sockaddr_parse(&destination_address, args.destination, PARSE_PORT_REQUIRE)) {
155                 ast_log(LOG_ERROR, "Destination address '%s' could not be parsed\n",
156                         args.destination);
157                 goto failure;
158         }
159
160         ast_sockaddr_setnull(&control_address);
161         if (!ast_strlen_zero(args.control)
162                 && !ast_sockaddr_parse(&control_address, args.control, PARSE_PORT_REQUIRE)) {
163                 ast_log(LOG_ERROR, "Control address '%s' could not be parsed\n", args.control);
164                 goto failure;
165         }
166
167         mcast_options = ast_multicast_rtp_create_options(args.type, args.options);
168         if (!mcast_options) {
169                 goto failure;
170         }
171
172         fmt = ast_multicast_rtp_options_get_format(mcast_options);
173         if (!fmt) {
174                 fmt = ast_format_cap_get_format(cap, 0);
175         }
176         if (!fmt) {
177                 ast_log(LOG_ERROR, "No codec available for sending RTP to '%s'\n",
178                         args.destination);
179                 goto failure;
180         }
181
182         caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
183         if (!caps) {
184                 goto failure;
185         }
186
187         instance = ast_rtp_instance_new("multicast", NULL, &control_address, mcast_options);
188         if (!instance) {
189                 ast_log(LOG_ERROR,
190                         "Could not create '%s' multicast RTP instance for sending media to '%s'\n",
191                         args.type, args.destination);
192                 goto failure;
193         }
194
195         chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", assignedids,
196                 requestor, 0, "MulticastRTP/%p", instance);
197         if (!chan) {
198                 ast_rtp_instance_destroy(instance);
199                 goto failure;
200         }
201         ast_rtp_instance_set_channel_id(instance, ast_channel_uniqueid(chan));
202         ast_rtp_instance_set_remote_address(instance, &destination_address);
203
204         ast_channel_tech_set(chan, &multicast_rtp_tech);
205
206         ast_format_cap_append(caps, fmt, 0);
207         ast_channel_nativeformats_set(chan, caps);
208         ast_channel_set_writeformat(chan, fmt);
209         ast_channel_set_rawwriteformat(chan, fmt);
210         ast_channel_set_readformat(chan, fmt);
211         ast_channel_set_rawreadformat(chan, fmt);
212
213         ast_channel_tech_pvt_set(chan, instance);
214
215         ast_channel_unlock(chan);
216
217         ao2_ref(fmt, -1);
218         ao2_ref(caps, -1);
219         ast_multicast_rtp_free_options(mcast_options);
220
221         return chan;
222
223 failure:
224         ao2_cleanup(fmt);
225         ao2_cleanup(caps);
226         ast_multicast_rtp_free_options(mcast_options);
227         *cause = AST_CAUSE_FAILURE;
228         return NULL;
229 }
230
231 enum {
232         OPT_RTP_CODEC =  (1 << 0),
233         OPT_RTP_ENGINE = (1 << 1),
234 };
235
236 enum {
237         OPT_ARG_RTP_CODEC,
238         OPT_ARG_RTP_ENGINE,
239         /* note: this entry _MUST_ be the last one in the enum */
240         OPT_ARG_ARRAY_SIZE
241 };
242
243 AST_APP_OPTIONS(unicast_rtp_options, BEGIN_OPTIONS
244         /*! Set the codec to be used for unicast RTP */
245         AST_APP_OPTION_ARG('c', OPT_RTP_CODEC, OPT_ARG_RTP_CODEC),
246         /*! Set the RTP engine to use for unicast RTP */
247         AST_APP_OPTION_ARG('e', OPT_RTP_ENGINE, OPT_ARG_RTP_ENGINE),
248 END_OPTIONS );
249
250 /*! \brief Function called when we should prepare to call the unicast destination */
251 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)
252 {
253         char *parse;
254         struct ast_rtp_instance *instance;
255         struct ast_sockaddr address;
256         struct ast_sockaddr local_address;
257         struct ast_channel *chan;
258         struct ast_format_cap *caps = NULL;
259         struct ast_format *fmt = NULL;
260         const char *engine_name;
261         AST_DECLARE_APP_ARGS(args,
262                 AST_APP_ARG(destination);
263                 AST_APP_ARG(options);
264         );
265         struct ast_flags opts = { 0, };
266         char *opt_args[OPT_ARG_ARRAY_SIZE];
267
268         if (ast_strlen_zero(data)) {
269                 ast_log(LOG_ERROR, "Destination is required for the 'UnicastRTP' channel\n");
270                 goto failure;
271         }
272         parse = ast_strdupa(data);
273         AST_NONSTANDARD_APP_ARGS(args, parse, '/');
274
275         if (ast_strlen_zero(args.destination)) {
276                 ast_log(LOG_ERROR, "Destination is required for the 'UnicastRTP' channel\n");
277                 goto failure;
278         }
279         if (!ast_sockaddr_parse(&address, args.destination, PARSE_PORT_REQUIRE)) {
280                 ast_log(LOG_ERROR, "Destination '%s' could not be parsed\n", args.destination);
281                 goto failure;
282         }
283
284         if (!ast_strlen_zero(args.options)
285                 && ast_app_parse_options(unicast_rtp_options, &opts, opt_args,
286                         ast_strdupa(args.options))) {
287                 ast_log(LOG_ERROR, "'UnicastRTP' channel options '%s' parse error\n",
288                         args.options);
289                 goto failure;
290         }
291
292         if (ast_test_flag(&opts, OPT_RTP_CODEC)
293                 && !ast_strlen_zero(opt_args[OPT_ARG_RTP_CODEC])) {
294                 fmt = ast_format_cache_get(opt_args[OPT_ARG_RTP_CODEC]);
295                 if (!fmt) {
296                         ast_log(LOG_ERROR, "Codec '%s' not found for sending RTP to '%s'\n",
297                                 opt_args[OPT_ARG_RTP_CODEC], args.destination);
298                         goto failure;
299                 }
300         } else {
301                 fmt = ast_format_cap_get_format(cap, 0);
302                 if (!fmt) {
303                         ast_log(LOG_ERROR, "No codec available for sending RTP to '%s'\n",
304                                 args.destination);
305                         goto failure;
306                 }
307         }
308
309         caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
310         if (!caps) {
311                 goto failure;
312         }
313
314         engine_name = S_COR(ast_test_flag(&opts, OPT_RTP_ENGINE),
315                 opt_args[OPT_ARG_RTP_ENGINE], "asterisk");
316
317         ast_sockaddr_copy(&local_address, &address);
318         if (ast_ouraddrfor(&address, &local_address)) {
319                 ast_log(LOG_ERROR, "Could not get our address for sending media to '%s'\n",
320                         args.destination);
321                 goto failure;
322         }
323         instance = ast_rtp_instance_new(engine_name, NULL, &local_address, NULL);
324         if (!instance) {
325                 ast_log(LOG_ERROR,
326                         "Could not create %s RTP instance for sending media to '%s'\n",
327                         S_OR(engine_name, "default"), args.destination);
328                 goto failure;
329         }
330
331         chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", assignedids,
332                 requestor, 0, "UnicastRTP/%s-%p", args.destination, instance);
333         if (!chan) {
334                 ast_rtp_instance_destroy(instance);
335                 goto failure;
336         }
337         ast_rtp_instance_set_channel_id(instance, ast_channel_uniqueid(chan));
338         ast_rtp_instance_set_remote_address(instance, &address);
339         ast_channel_set_fd(chan, 0, ast_rtp_instance_fd(instance, 0));
340
341         ast_channel_tech_set(chan, &unicast_rtp_tech);
342
343         ast_format_cap_append(caps, fmt, 0);
344         ast_channel_nativeformats_set(chan, caps);
345         ast_channel_set_writeformat(chan, fmt);
346         ast_channel_set_rawwriteformat(chan, fmt);
347         ast_channel_set_readformat(chan, fmt);
348         ast_channel_set_rawreadformat(chan, fmt);
349
350         ast_channel_tech_pvt_set(chan, instance);
351
352         pbx_builtin_setvar_helper(chan, "UNICASTRTP_LOCAL_ADDRESS",
353                 ast_sockaddr_stringify_addr(&local_address));
354         ast_rtp_instance_get_local_address(instance, &local_address);
355         pbx_builtin_setvar_helper(chan, "UNICASTRTP_LOCAL_PORT",
356                 ast_sockaddr_stringify_port(&local_address));
357
358         ast_channel_unlock(chan);
359
360         ao2_ref(fmt, -1);
361         ao2_ref(caps, -1);
362
363         return chan;
364
365 failure:
366         ao2_cleanup(fmt);
367         ao2_cleanup(caps);
368         *cause = AST_CAUSE_FAILURE;
369         return NULL;
370 }
371
372 /*! \brief Function called when our module is unloaded */
373 static int unload_module(void)
374 {
375         ast_channel_unregister(&multicast_rtp_tech);
376         ao2_cleanup(multicast_rtp_tech.capabilities);
377         multicast_rtp_tech.capabilities = NULL;
378
379         ast_channel_unregister(&unicast_rtp_tech);
380         ao2_cleanup(unicast_rtp_tech.capabilities);
381         unicast_rtp_tech.capabilities = NULL;
382
383         return 0;
384 }
385
386 /*! \brief Function called when our module is loaded */
387 static int load_module(void)
388 {
389         if (!(multicast_rtp_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
390                 return AST_MODULE_LOAD_DECLINE;
391         }
392         ast_format_cap_append_by_type(multicast_rtp_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
393         if (ast_channel_register(&multicast_rtp_tech)) {
394                 ast_log(LOG_ERROR, "Unable to register channel class 'MulticastRTP'\n");
395                 unload_module();
396                 return AST_MODULE_LOAD_DECLINE;
397         }
398
399         if (!(unicast_rtp_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
400                 unload_module();
401                 return AST_MODULE_LOAD_DECLINE;
402         }
403         ast_format_cap_append_by_type(unicast_rtp_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
404         if (ast_channel_register(&unicast_rtp_tech)) {
405                 ast_log(LOG_ERROR, "Unable to register channel class 'UnicastRTP'\n");
406                 unload_module();
407                 return AST_MODULE_LOAD_DECLINE;
408         }
409
410         return AST_MODULE_LOAD_SUCCESS;
411 }
412
413 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "RTP Media Channel",
414         .support_level = AST_MODULE_SUPPORT_CORE,
415         .load = load_module,
416         .unload = unload_module,
417         .load_pri = AST_MODPRI_CHANNEL_DRIVER,
418 );