Wed Jan 8 2020 09:49:44

Asterisk developer's documentation


chan_multicast_rtp.c
Go to the documentation of this file.
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 /*** MODULEINFO
31  <support_level>core</support_level>
32  ***/
33 
34 #include "asterisk.h"
35 
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 385683 $")
37 
38 #include <fcntl.h>
39 #include <sys/signal.h>
40 
41 #include "asterisk/lock.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/config.h"
44 #include "asterisk/module.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/sched.h"
47 #include "asterisk/io.h"
48 #include "asterisk/acl.h"
49 #include "asterisk/callerid.h"
50 #include "asterisk/file.h"
51 #include "asterisk/cli.h"
52 #include "asterisk/app.h"
53 #include "asterisk/rtp_engine.h"
54 #include "asterisk/causes.h"
55 
56 static const char tdesc[] = "Multicast RTP Paging Channel Driver";
57 
58 /* Forward declarations */
59 static struct ast_channel *multicast_rtp_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause);
60 static int multicast_rtp_call(struct ast_channel *ast, char *dest, int timeout);
61 static int multicast_rtp_hangup(struct ast_channel *ast);
62 static struct ast_frame *multicast_rtp_read(struct ast_channel *ast);
63 static int multicast_rtp_write(struct ast_channel *ast, struct ast_frame *f);
64 
65 /* Channel driver declaration */
66 static const struct ast_channel_tech multicast_rtp_tech = {
67  .type = "MulticastRTP",
68  .description = tdesc,
69  .capabilities = -1,
70  .requester = multicast_rtp_request,
71  .call = multicast_rtp_call,
72  .hangup = multicast_rtp_hangup,
73  .read = multicast_rtp_read,
74  .write = multicast_rtp_write,
75 };
76 
77 /*! \brief Function called when we should read a frame from the channel */
78 static struct ast_frame *multicast_rtp_read(struct ast_channel *ast)
79 {
80  return &ast_null_frame;
81 }
82 
83 /*! \brief Function called when we should write a frame to the channel */
84 static int multicast_rtp_write(struct ast_channel *ast, struct ast_frame *f)
85 {
86  struct ast_rtp_instance *instance = ast->tech_pvt;
87 
88  return ast_rtp_instance_write(instance, f);
89 }
90 
91 /*! \brief Function called when we should actually call the destination */
92 static int multicast_rtp_call(struct ast_channel *ast, char *dest, int timeout)
93 {
94  struct ast_rtp_instance *instance = ast->tech_pvt;
95 
97 
98  return ast_rtp_instance_activate(instance);
99 }
100 
101 /*! \brief Function called when we should hang the channel up */
102 static int multicast_rtp_hangup(struct ast_channel *ast)
103 {
104  struct ast_rtp_instance *instance = ast->tech_pvt;
105 
106  ast_rtp_instance_destroy(instance);
107 
108  ast->tech_pvt = NULL;
109 
110  return 0;
111 }
112 
113 /*! \brief Function called when we should prepare to call the destination */
114 static struct ast_channel *multicast_rtp_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
115 {
116  char *tmp = ast_strdupa(data), *multicast_type = tmp, *destination, *control;
117  struct ast_rtp_instance *instance;
118  struct ast_sockaddr control_address;
119  struct ast_sockaddr destination_address;
120  struct ast_channel *chan;
121  format_t fmt = ast_best_codec(format);
122 
123  ast_sockaddr_setnull(&control_address);
124 
125  /* If no type was given we can't do anything */
127  goto failure;
128  }
129 
130  if (!(destination = strchr(tmp, '/'))) {
131  goto failure;
132  }
133  *destination++ = '\0';
134 
135  if ((control = strchr(destination, '/'))) {
136  *control++ = '\0';
137  if (!ast_sockaddr_parse(&control_address, control,
139  goto failure;
140  }
141  }
142 
143  if (!ast_sockaddr_parse(&destination_address, destination,
145  goto failure;
146  }
147 
148  if (!(instance = ast_rtp_instance_new("multicast", NULL, &control_address, multicast_type))) {
149  goto failure;
150  }
151 
152  if (!(chan = ast_channel_alloc(1, AST_STATE_DOWN, "", "", "", "", "", requestor ? requestor->linkedid : "", 0, "MulticastRTP/%p", instance))) {
153  ast_rtp_instance_destroy(instance);
154  goto failure;
155  }
156 
157  ast_rtp_instance_set_remote_address(instance, &destination_address);
158 
159  chan->tech = &multicast_rtp_tech;
160  chan->nativeformats = fmt;
161  chan->writeformat = fmt;
162  chan->readformat = fmt;
163  chan->rawwriteformat = fmt;
164  chan->rawreadformat = fmt;
165  chan->tech_pvt = instance;
166 
167  return chan;
168 
169 failure:
170  *cause = AST_CAUSE_FAILURE;
171  return NULL;
172 }
173 
174 /*! \brief Function called when our module is loaded */
175 static int load_module(void)
176 {
177  if (ast_channel_register(&multicast_rtp_tech)) {
178  ast_log(LOG_ERROR, "Unable to register channel class 'MulticastRTP'\n");
180  }
181 
183 }
184 
185 /*! \brief Function called when our module is unloaded */
186 static int unload_module(void)
187 {
188  ast_channel_unregister(&multicast_rtp_tech);
189 
190  return 0;
191 }
192 
193 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Multicast RTP Paging Channel",
194  .load = load_module,
195  .unload = unload_module,
196  .load_pri = AST_MODPRI_CHANNEL_DRIVER,
197 );
int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
Indicate to the RTP engine that packets are now expected to be sent/received on the RTP instance...
Definition: rtp_engine.c:1744
static int load_module(void)
Function called when our module is loaded.
Main Channel structure associated with a channel.
Definition: channel.h:742
const char *const type
Definition: channel.h:508
Asterisk locking-related definitions:
multicast_type
Type of paging to do.
Asterisk main include file. File version handling, generic pbx functions.
int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
Queue a control frame with payload.
Definition: channel.c:1601
struct ast_frame ast_null_frame
Definition: frame.c:131
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
format_t writeformat
Definition: channel.h:854
int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags)
Parse an IPv4 or IPv6 address string.
Definition: netsock2.c:198
static struct ast_frame * multicast_rtp_read(struct ast_channel *ast)
Function called when we should read a frame from the channel.
void ast_channel_unregister(const struct ast_channel_tech *tech)
Unregister a channel technology.
Definition: channel.c:938
void * tech_pvt
Definition: channel.h:744
int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
Send a frame out over RTP.
Definition: rtp_engine.c:374
format_t rawwriteformat
Definition: channel.h:856
Configuration File Parser.
static struct ast_channel_tech multicast_rtp_tech
format_t ast_best_codec(format_t fmts)
Pick the best audio codec.
Definition: channel.c:1062
int ast_channel_register(const struct ast_channel_tech *tech)
Register a channel technology (a new channel driver) Called by a channel module to register the kind ...
Definition: channel.c:907
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
format_t nativeformats
Definition: channel.h:852
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
const char * data
Definition: channel.h:755
I/O Management (derived from Cheops-NG)
const ast_string_field linkedid
Definition: channel.h:787
format_t rawreadformat
Definition: channel.h:855
Socket address structure.
Definition: netsock2.h:63
struct ast_channel * ast_channel_alloc(int needqueue, int state, const char *cid_num, const char *cid_name, const char *acctcode, const char *exten, const char *context, const char *linkedid, const int amaflag, const char *name_fmt,...)
Definition: channel.c:9825
static void ast_sockaddr_setnull(struct ast_sockaddr *addr)
Sets address addr to null.
Definition: netsock2.h:106
static struct ast_channel * multicast_rtp_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
Function called when we should prepare to call the destination.
General Asterisk PBX channel definitions.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
Access Control of various sorts.
Scheduler Routines (derived from cheops)
int ast_rtp_instance_set_remote_address(struct ast_rtp_instance *instance, const struct ast_sockaddr *address)
Set the address of the remote endpoint that we are sending RTP to.
Definition: rtp_engine.c:391
Structure to describe a channel &quot;technology&quot;, ie a channel driver See for examples: ...
Definition: channel.h:507
Core PBX routines and definitions.
#define AST_CAUSE_FAILURE
Definition: causes.h:149
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
static int multicast_rtp_hangup(struct ast_channel *ast)
Function called when we should hang the channel up.
#define LOG_ERROR
Definition: logger.h:155
int64_t format_t
Definition: frame_defs.h:32
static int unload_module(void)
Function called when our module is unloaded.
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static const char tdesc[]
static int multicast_rtp_write(struct ast_channel *ast, struct ast_frame *f)
Function called when we should write a frame to the channel.
static struct ast_format f[]
Definition: format_g726.c:181
struct ast_rtp_instance * ast_rtp_instance_new(const char *engine_name, struct sched_context *sched, const struct ast_sockaddr *sa, void *data)
Create a new RTP instance.
Definition: rtp_engine.c:308
static const char type[]
Definition: chan_nbs.c:57
int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
Destroy an RTP instance.
Definition: rtp_engine.c:301
Standard Command Line Interface.
format_t readformat
Definition: channel.h:853
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
static int multicast_rtp_call(struct ast_channel *ast, char *dest, int timeout)
Function called when we should actually call the destination.
Data structure associated with a single frame of data.
Definition: frame.h:142
Internal Asterisk hangup causes.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Pluggable RTP Architecture.
Asterisk module definitions.
static snd_pcm_format_t format
Definition: chan_alsa.c:93
struct ast_channel_tech * tech
Definition: channel.h:743
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180