Wed Jan 8 2020 09:49:50

Asterisk developer's documentation


res_rtp_multicast.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 /*!
21  * \file
22  *
23  * \brief Multicast RTP Engine
24  *
25  * \author Joshua Colp <jcolp@digium.com>
26  * \author Andreas 'MacBrody' Brodmann <andreas.brodmann@gmail.com>
27  *
28  * \ingroup rtp_engines
29  */
30 
31 /*** MODULEINFO
32  <support_level>core</support_level>
33  ***/
34 
35 #include "asterisk.h"
36 
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 400393 $")
38 
39 #include <sys/time.h>
40 #include <signal.h>
41 #include <fcntl.h>
42 #include <math.h>
43 
44 #include "asterisk/pbx.h"
45 #include "asterisk/frame.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/acl.h"
48 #include "asterisk/config.h"
49 #include "asterisk/lock.h"
50 #include "asterisk/utils.h"
51 #include "asterisk/cli.h"
52 #include "asterisk/manager.h"
53 #include "asterisk/unaligned.h"
54 #include "asterisk/module.h"
55 #include "asterisk/rtp_engine.h"
56 
57 /*! Command value used for Linksys paging to indicate we are starting */
58 #define LINKSYS_MCAST_STARTCMD 6
59 
60 /*! Command value used for Linksys paging to indicate we are stopping */
61 #define LINKSYS_MCAST_STOPCMD 7
62 
63 /*! \brief Type of paging to do */
65  /*! Simple multicast enabled client/receiver paging like Snom and Barix uses */
67  /*! More advanced Linksys type paging which requires a start and stop packet */
69 };
70 
71 /*! \brief Structure for a Linksys control packet */
73  /*! Unique identifier for the control packet */
74  uint32_t unique_id;
75  /*! Actual command in the control packet */
76  uint32_t command;
77  /*! IP address for the RTP */
78  uint32_t ip;
79  /*! Port for the RTP */
80  uint32_t port;
81 };
82 
83 /*! \brief Structure for a multicast paging instance */
84 struct multicast_rtp {
85  /*! TYpe of multicast paging this instance is doing */
87  /*! Socket used for sending the audio on */
88  int socket;
89  /*! Synchronization source value, used when creating/sending the RTP packet */
90  unsigned int ssrc;
91  /*! Sequence number, used when creating/sending the RTP packet */
92  uint16_t seqno;
93  unsigned int lastts;
94  struct timeval txcore;
95 };
96 
97 /* Forward Declarations */
98 static int multicast_rtp_new(struct ast_rtp_instance *instance, struct sched_context *sched, struct ast_sockaddr *addr, void *data);
99 static int multicast_rtp_activate(struct ast_rtp_instance *instance);
100 static int multicast_rtp_destroy(struct ast_rtp_instance *instance);
101 static int multicast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame);
102 static struct ast_frame *multicast_rtp_read(struct ast_rtp_instance *instance, int rtcp);
103 
104 /* RTP Engine Declaration */
106  .name = "multicast",
107  .new = multicast_rtp_new,
108  .activate = multicast_rtp_activate,
109  .destroy = multicast_rtp_destroy,
110  .write = multicast_rtp_write,
111  .read = multicast_rtp_read,
112 };
113 
114 /*! \brief Function called to create a new multicast instance */
115 static int multicast_rtp_new(struct ast_rtp_instance *instance, struct sched_context *sched, struct ast_sockaddr *addr, void *data)
116 {
117  struct multicast_rtp *multicast;
118  const char *type = data;
119 
120  if (!(multicast = ast_calloc(1, sizeof(*multicast)))) {
121  return -1;
122  }
123 
124  if (!strcasecmp(type, "basic")) {
125  multicast->type = MULTICAST_TYPE_BASIC;
126  } else if (!strcasecmp(type, "linksys")) {
127  multicast->type = MULTICAST_TYPE_LINKSYS;
128  } else {
129  ast_free(multicast);
130  return -1;
131  }
132 
133  if ((multicast->socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
134  ast_free(multicast);
135  return -1;
136  }
137 
138  multicast->ssrc = ast_random();
139 
140  ast_rtp_instance_set_data(instance, multicast);
141 
142  return 0;
143 }
144 
146 {
147  return (format == AST_FORMAT_G722) ? 8000 : ast_format_rate(format);
148 }
149 
150 static unsigned int calc_txstamp(struct multicast_rtp *rtp, struct timeval *delivery)
151 {
152  struct timeval t;
153  long ms;
154 
155  if (ast_tvzero(rtp->txcore)) {
156  rtp->txcore = ast_tvnow();
157  rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
158  }
159 
160  t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow();
161  if ((ms = ast_tvdiff_ms(t, rtp->txcore)) < 0) {
162  ms = 0;
163  }
164  rtp->txcore = t;
165 
166  return (unsigned int) ms;
167 }
168 
169 /*! \brief Helper function which populates a control packet with useful information and sends it */
170 static int multicast_send_control_packet(struct ast_rtp_instance *instance, struct multicast_rtp *multicast, int command)
171 {
172  struct multicast_control_packet control_packet = { .unique_id = htonl((u_long)time(NULL)),
173  .command = htonl(command),
174  };
175  struct ast_sockaddr control_address, remote_address;
176 
177  ast_rtp_instance_get_local_address(instance, &control_address);
178  ast_rtp_instance_get_remote_address(instance, &remote_address);
179 
180  /* Ensure the user of us have given us both the control address and destination address */
181  if (ast_sockaddr_isnull(&control_address) ||
182  ast_sockaddr_isnull(&remote_address)) {
183  return -1;
184  }
185 
186  /* The protocol only supports IPv4. */
187  if (ast_sockaddr_is_ipv6(&remote_address)) {
188  ast_log(LOG_WARNING, "Cannot send control packet for IPv6 "
189  "remote address.\n");
190  return -1;
191  }
192 
193  control_packet.ip = htonl(ast_sockaddr_ipv4(&remote_address));
194  control_packet.port = htonl(ast_sockaddr_port(&remote_address));
195 
196  /* Based on a recommendation by Brian West who did the FreeSWITCH implementation we send control packets twice */
197  ast_sendto(multicast->socket, &control_packet, sizeof(control_packet), 0, &control_address);
198  ast_sendto(multicast->socket, &control_packet, sizeof(control_packet), 0, &control_address);
199 
200  return 0;
201 }
202 
203 /*! \brief Function called to indicate that audio is now going to flow */
204 static int multicast_rtp_activate(struct ast_rtp_instance *instance)
205 {
206  struct multicast_rtp *multicast = ast_rtp_instance_get_data(instance);
207 
208  if (multicast->type != MULTICAST_TYPE_LINKSYS) {
209  return 0;
210  }
211 
212  return multicast_send_control_packet(instance, multicast, LINKSYS_MCAST_STARTCMD);
213 }
214 
215 /*! \brief Function called to destroy a multicast instance */
216 static int multicast_rtp_destroy(struct ast_rtp_instance *instance)
217 {
218  struct multicast_rtp *multicast = ast_rtp_instance_get_data(instance);
219 
220  if (multicast->type == MULTICAST_TYPE_LINKSYS) {
222  }
223 
224  close(multicast->socket);
225 
226  ast_free(multicast);
227 
228  return 0;
229 }
230 
231 /*! \brief Function called to broadcast some audio on a multicast instance */
232 static int multicast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
233 {
234  struct multicast_rtp *multicast = ast_rtp_instance_get_data(instance);
235  struct ast_frame *f = frame;
236  struct ast_sockaddr remote_address;
237  int hdrlen = 12, res = 0, codec;
238  int rate;
239  unsigned char *rtpheader;
240  unsigned int ms = calc_txstamp(multicast, &frame->delivery);
241 
242  /* We only accept audio, nothing else */
243  if (frame->frametype != AST_FRAME_VOICE) {
244  return 0;
245  }
246  rate = rtp_get_rate(frame->subclass.codec) / 1000;
247 
248  /* Grab the actual payload number for when we create the RTP packet */
249  if ((codec = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(instance), 1, frame->subclass.codec)) < 0) {
250  return -1;
251  }
252 
253  /* If we do not have space to construct an RTP header duplicate the frame so we get some */
254  if (frame->offset < hdrlen) {
255  f = ast_frdup(frame);
256  }
257 
258  /* Calucate last TS */
259  multicast->lastts = multicast->lastts + ms * rate;
260 
261  /* Construct an RTP header for our packet */
262  rtpheader = (unsigned char *)(f->data.ptr - hdrlen);
263  put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (multicast->seqno)));
264 
266  put_unaligned_uint32(rtpheader + 4, htonl(f->ts * 8));
267  } else {
268  put_unaligned_uint32(rtpheader + 4, htonl(multicast->lastts));
269  }
270 
271  put_unaligned_uint32(rtpheader + 8, htonl(multicast->ssrc));
272 
273  /* Increment sequence number and wrap to 0 if it overflows 16 bits. */
274  multicast->seqno = 0xFFFF & (multicast->seqno + 1);
275 
276  /* Finally send it out to the eager phones listening for us */
277  ast_rtp_instance_get_remote_address(instance, &remote_address);
278 
279  if (ast_sendto(multicast->socket, (void *) rtpheader, f->datalen + hdrlen, 0, &remote_address) < 0) {
280  ast_log(LOG_ERROR, "Multicast RTP Transmission error to %s: %s\n",
281  ast_sockaddr_stringify(&remote_address),
282  strerror(errno));
283  res = -1;
284  }
285 
286  /* If we were forced to duplicate the frame free the new one */
287  if (frame != f) {
288  ast_frfree(f);
289  }
290 
291  return res;
292 }
293 
294 /*! \brief Function called to read from a multicast instance */
295 static struct ast_frame *multicast_rtp_read(struct ast_rtp_instance *instance, int rtcp)
296 {
297  return &ast_null_frame;
298 }
299 
300 static int load_module(void)
301 {
302  if (ast_rtp_engine_register(&multicast_rtp_engine)) {
304  }
305 
307 }
308 
309 static int unload_module(void)
310 {
311  ast_rtp_engine_unregister(&multicast_rtp_engine);
312 
313  return 0;
314 }
315 
317  .load = load_module,
318  .unload = unload_module,
319  .load_pri = AST_MODPRI_CHANNEL_DEPEND,
320 );
union ast_frame_subclass subclass
Definition: frame.h:146
static int rtp_get_rate(format_t format)
static int multicast_send_control_packet(struct ast_rtp_instance *instance, struct multicast_rtp *multicast, int command)
Helper function which populates a control packet with useful information and sends it...
Structure for a Linksys control packet.
ssize_t ast_sendto(int sockfd, const void *buf, size_t len, int flags, const struct ast_sockaddr *dest_addr)
Wrapper around sendto(2) that uses ast_sockaddr.
Definition: netsock2.c:486
static int unload_module(void)
Asterisk locking-related definitions:
multicast_type
Type of paging to do.
Asterisk main include file. File version handling, generic pbx functions.
#define ast_rtp_engine_register(engine)
Definition: rtp_engine.h:423
int offset
Definition: frame.h:156
struct ast_frame ast_null_frame
Definition: frame.c:131
static int multicast_rtp_new(struct ast_rtp_instance *instance, struct sched_context *sched, struct ast_sockaddr *addr, void *data)
Function called to create a new multicast instance.
#define ast_test_flag(p, flag)
Definition: utils.h:63
void * ptr
Definition: frame.h:160
struct ast_rtp_codecs * ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
Get the codecs structure of an RTP instance.
Definition: rtp_engine.c:483
#define LOG_WARNING
Definition: logger.h:144
enum multicast_type type
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
Definition: time.h:100
static unsigned int calc_txstamp(struct multicast_rtp *rtp, struct timeval *delivery)
static void put_unaligned_uint32(void *p, unsigned int datum)
Definition: unaligned.h:58
Definition: sched.c:57
long ts
Definition: frame.h:168
Configuration File Parser.
uint32_t ast_sockaddr_ipv4(const struct ast_sockaddr *addr)
Get an IPv4 address of an ast_sockaddr.
Definition: netsock2.c:394
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
static force_inline int ast_format_rate(format_t format)
Get the sample rate for a given format.
Definition: frame.h:809
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:90
format_t codec
Definition: frame.h:137
Socket address structure.
Definition: netsock2.h:63
Utility functions.
static int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
Checks if the ast_sockaddr is null. &quot;null&quot; in this sense essentially means uninitialized, or having a 0 length.
Definition: netsock2.h:93
#define ast_sockaddr_port(addr)
Get the port number of a socket address.
Definition: netsock2.h:406
Structure for a multicast paging instance.
unsigned int lastts
Handle unaligned data access.
General Asterisk PBX channel definitions.
int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
Unregister an RTP engine.
Definition: rtp_engine.c:222
void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
Set the data portion of an RTP instance.
Definition: rtp_engine.c:364
unsigned int ssrc
Access Control of various sorts.
Asterisk internal frame definitions.
int datalen
Definition: frame.h:148
long int ast_random(void)
Definition: utils.c:1640
#define LINKSYS_MCAST_STARTCMD
Core PBX routines and definitions.
static int multicast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
Function called to broadcast some audio on a multicast instance.
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
#define LOG_ERROR
Definition: logger.h:155
static struct ast_rtp_engine multicast_rtp_engine
int64_t format_t
Definition: frame_defs.h:32
static int multicast_rtp_activate(struct ast_rtp_instance *instance)
Function called to indicate that audio is now going to flow.
#define LINKSYS_MCAST_STOPCMD
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
Definition: netsock2.h:210
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
int errno
#define ast_free(a)
Definition: astmm.h:97
void * ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
Get the data portion of an RTP instance.
Definition: rtp_engine.c:369
static struct ast_frame * multicast_rtp_read(struct ast_rtp_instance *instance, int rtcp)
Function called to read from a multicast instance.
void ast_rtp_instance_get_remote_address(struct ast_rtp_instance *instance, struct ast_sockaddr *address)
Get the address of the remote endpoint that we are sending RTP to.
Definition: rtp_engine.c:447
static struct ast_format f[]
Definition: format_g726.c:181
const char * name
Definition: rtp_engine.h:312
static const char type[]
Definition: chan_nbs.c:57
void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance, struct ast_sockaddr *address)
Get the local address that we are expecting RTP on.
Definition: rtp_engine.c:430
struct timeval delivery
Definition: frame.h:162
static int multicast_rtp_destroy(struct ast_rtp_instance *instance)
Function called to destroy a multicast instance.
struct timeval txcore
static int load_module(void)
Standard Command Line Interface.
#define ast_calloc(a, b)
Definition: astmm.h:82
Data structure associated with a single frame of data.
Definition: frame.h:142
#define AST_FORMAT_G722
Definition: frame.h:266
enum ast_frame_type frametype
Definition: frame.h:144
#define ast_frfree(fr)
Definition: frame.h:583
#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
union ast_frame::@172 data
int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, const int asterisk_format, const format_t code)
Retrieve a payload based on whether it is an Asterisk format and the code.
Definition: rtp_engine.c:654
int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr)
Determine if this is an IPv6 address.
Definition: netsock2.c:418
struct ast_frame * ast_frdup(const struct ast_frame *fr)
Copies a frame.
Definition: frame.c:474
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180