2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2009, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
7 * Andreas 'MacBrody' Brodmann <andreas.brodmann@gmail.com>
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.
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.
22 * \author Joshua Colp <jcolp@digium.com>
23 * \author Andreas 'MacBrody' Broadmann <andreas.brodmann@gmail.com>
25 * \brief Multicast RTP Paging Channel
27 * \ingroup channel_drivers
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35 #include <sys/signal.h>
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"
52 static const char tdesc[] = "Multicast RTP Paging Channel Driver";
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);
61 /* Channel driver declaration */
62 static const struct ast_channel_tech multicast_rtp_tech = {
63 .type = "MulticastRTP",
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,
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)
76 return &ast_null_frame;
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)
82 struct ast_rtp_instance *instance = ast->tech_pvt;
84 return ast_rtp_instance_write(instance, f);
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)
90 struct ast_rtp_instance *instance = ast->tech_pvt;
92 ast_queue_control(ast, AST_CONTROL_ANSWER);
94 return ast_rtp_instance_activate(instance);
97 /*! \brief Function called when we should hang the channel up */
98 static int multicast_rtp_hangup(struct ast_channel *ast)
100 struct ast_rtp_instance *instance = ast->tech_pvt;
102 ast_rtp_instance_destroy(instance);
104 ast->tech_pvt = NULL;
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)
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);
118 /* If no type was given we can't do anything */
119 if (ast_strlen_zero(multicast_type)) {
123 if (!(destination = strchr(tmp, '/'))) {
126 *destination++ = '\0';
128 if (ast_parse_arg(destination, PARSE_INADDR | PARSE_PORT_REQUIRE, &destination_address)) {
132 if ((control = strchr(destination, '/'))) {
134 if (ast_parse_arg(control, PARSE_INADDR | PARSE_PORT_REQUIRE, &control_address)) {
139 if (!(instance = ast_rtp_instance_new("multicast", NULL, &control_address, multicast_type))) {
143 if (!(chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", 0, "MulticastRTP/%p", instance))) {
144 ast_rtp_instance_destroy(instance);
148 ast_rtp_instance_set_remote_address(instance, &destination_address);
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;
161 *cause = AST_CAUSE_FAILURE;
165 /*! \brief Function called when our module is loaded */
166 static int load_module(void)
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;
173 return AST_MODULE_LOAD_SUCCESS;
176 /*! \brief Function called when our module is unloaded */
177 static int unload_module(void)
179 ast_channel_unregister(&multicast_rtp_tech);
184 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multicast RTP Paging Channel");