Add support for multicast RTP paging.
[asterisk/asterisk.git] / channels / chan_multicast_rtp.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2009, 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 Multicast RTP Paging Channel
26  *
27  * \ingroup channel_drivers
28  */
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <fcntl.h>
35 #include <sys/signal.h>
36
37 #include "asterisk/lock.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/config.h"
40 #include "asterisk/module.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/sched.h"
43 #include "asterisk/io.h"
44 #include "asterisk/acl.h"
45 #include "asterisk/callerid.h"
46 #include "asterisk/file.h"
47 #include "asterisk/cli.h"
48 #include "asterisk/app.h"
49 #include "asterisk/rtp_engine.h"
50 #include "asterisk/causes.h"
51
52 static const char tdesc[] = "Multicast RTP Paging Channel Driver";
53
54 /* Forward declarations */
55 static struct ast_channel *multicast_rtp_request(const char *type, int format, void *data, int *cause);
56 static int multicast_rtp_call(struct ast_channel *ast, char *dest, int timeout);
57 static int multicast_rtp_hangup(struct ast_channel *ast);
58 static struct ast_frame *multicast_rtp_read(struct ast_channel *ast);
59 static int multicast_rtp_write(struct ast_channel *ast, struct ast_frame *f);
60
61 /* Channel driver declaration */
62 static const struct ast_channel_tech multicast_rtp_tech = {
63         .type = "MulticastRTP",
64         .description = tdesc,
65         .capabilities = -1,
66         .requester = multicast_rtp_request,
67         .call = multicast_rtp_call,
68         .hangup = multicast_rtp_hangup,
69         .read = multicast_rtp_read,
70         .write = multicast_rtp_write,
71 };
72
73 /*! \brief Function called when we should read a frame from the channel */
74 static struct ast_frame  *multicast_rtp_read(struct ast_channel *ast)
75 {
76         return &ast_null_frame;
77 }
78
79 /*! \brief Function called when we should write a frame to the channel */
80 static int multicast_rtp_write(struct ast_channel *ast, struct ast_frame *f)
81 {
82         struct ast_rtp_instance *instance = ast->tech_pvt;
83
84         return ast_rtp_instance_write(instance, f);
85 }
86
87 /*! \brief Function called when we should actually call the destination */
88 static int multicast_rtp_call(struct ast_channel *ast, char *dest, int timeout)
89 {
90         struct ast_rtp_instance *instance = ast->tech_pvt;
91
92         ast_queue_control(ast, AST_CONTROL_ANSWER);
93
94         return ast_rtp_instance_activate(instance);
95 }
96
97 /*! \brief Function called when we should hang the channel up */
98 static int multicast_rtp_hangup(struct ast_channel *ast)
99 {
100         struct ast_rtp_instance *instance = ast->tech_pvt;
101
102         ast_rtp_instance_destroy(instance);
103
104         ast->tech_pvt = NULL;
105
106         return 0;
107 }
108
109 /*! \brief Function called when we should prepare to call the destination */
110 static struct ast_channel *multicast_rtp_request(const char *type, int format, void *data, int *cause)
111 {
112         char *tmp = ast_strdupa(data), *multicast_type = tmp, *destination, *control;
113         struct ast_rtp_instance *instance;
114         struct sockaddr_in control_address = { .sin_family = AF_INET, }, destination_address = { .sin_family = AF_INET, };
115         struct ast_channel *chan;
116         int fmt = ast_best_codec(format);
117
118         /* If no type was given we can't do anything */
119         if (ast_strlen_zero(multicast_type)) {
120                 goto failure;
121         }
122
123         if (!(destination = strchr(tmp, '/'))) {
124                 goto failure;
125         }
126         *destination++ = '\0';
127
128         if (ast_parse_arg(destination, PARSE_INADDR | PARSE_PORT_REQUIRE, &destination_address)) {
129                 goto failure;
130         }
131
132         if ((control = strchr(destination, '/'))) {
133                 *control++ = '\0';
134                 if (ast_parse_arg(control, PARSE_INADDR | PARSE_PORT_REQUIRE, &control_address)) {
135                         goto failure;
136                 }
137         }
138
139         if (!(instance = ast_rtp_instance_new("multicast", NULL, &control_address, multicast_type))) {
140                 goto failure;
141         }
142
143         if (!(chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", 0, "MulticastRTP/%p", instance))) {
144                 ast_rtp_instance_destroy(instance);
145                 goto failure;
146         }
147
148         ast_rtp_instance_set_remote_address(instance, &destination_address);
149
150         chan->tech = &multicast_rtp_tech;
151         chan->nativeformats = fmt;
152         chan->writeformat = fmt;
153         chan->readformat = fmt;
154         chan->rawwriteformat = fmt;
155         chan->rawreadformat = fmt;
156         chan->tech_pvt = instance;
157
158         return chan;
159
160 failure:
161         *cause = AST_CAUSE_FAILURE;
162         return NULL;
163 }
164
165 /*! \brief Function called when our module is loaded */
166 static int load_module(void)
167 {
168         if (ast_channel_register(&multicast_rtp_tech)) {
169                 ast_log(LOG_ERROR, "Unable to register channel class 'MulticastRTP'\n");
170                 return AST_MODULE_LOAD_DECLINE;
171         }
172
173         return AST_MODULE_LOAD_SUCCESS;
174 }
175
176 /*! \brief Function called when our module is unloaded */
177 static int unload_module(void)
178 {
179         ast_channel_unregister(&multicast_rtp_tech);
180
181         return 0;
182 }
183
184 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multicast RTP Paging Channel");