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