[Asterisk-code-review] multicast RTP: Add dialing options (asterisk[master])
Mark Michelson
asteriskteam at digium.com
Thu May 26 15:17:47 CDT 2016
Mark Michelson has uploaded a new change for review.
https://gerrit.asterisk.org/2910
Change subject: multicast RTP: Add dialing options
......................................................................
multicast RTP: Add dialing options
This adds a new parameter to the end of a multicast RTP dialing string.
This parameter defines the following options:
* i: Set the interface from which multicast RTP is sent
* l: Set whether multicast packets are looped back to the sender
* t: Set the TTL for multicast packets
* c: Set the codec to use for RTP
ASTERISK-26068 #close
Reported by Mark Michelson
Change-Id: I033b706b533f0aa635c342eb738e0bcefa07e219
---
M channels/chan_rtp.c
A include/asterisk/multicast_rtp.h
M res/res_rtp_multicast.c
A res/res_rtp_multicast.exports.in
4 files changed, 253 insertions(+), 12 deletions(-)
git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/10/2910/1
diff --git a/channels/chan_rtp.c b/channels/chan_rtp.c
index 56705b1..d15ba86 100644
--- a/channels/chan_rtp.c
+++ b/channels/chan_rtp.c
@@ -43,6 +43,7 @@
#include "asterisk/rtp_engine.h"
#include "asterisk/causes.h"
#include "asterisk/format_cache.h"
+#include "asterisk/multicast_rtp.h"
/* Forward declarations */
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);
@@ -132,7 +133,9 @@
AST_APP_ARG(type);
AST_APP_ARG(destination);
AST_APP_ARG(control);
+ AST_APP_ARG(options);
);
+ struct ast_multicast_rtp_options *mcast_options = NULL;
if (ast_strlen_zero(data)) {
ast_log(LOG_ERROR, "A multicast type and destination must be given to the 'MulticastRTP' channel\n");
@@ -163,6 +166,11 @@
goto failure;
}
+ mcast_options = ast_multicast_rtp_create_options(args.type, args.options);
+ if (!mcast_options) {
+ goto failure;
+ }
+
fmt = ast_format_cap_get_format(cap, 0);
if (!fmt) {
ast_log(LOG_ERROR, "No format available for sending RTP to '%s'\n",
@@ -175,7 +183,7 @@
goto failure;
}
- instance = ast_rtp_instance_new("multicast", NULL, &control_address, args.type);
+ instance = ast_rtp_instance_new("multicast", NULL, &control_address, mcast_options);
if (!instance) {
ast_log(LOG_ERROR,
"Could not create '%s' multicast RTP instance for sending media to '%s'\n",
@@ -194,6 +202,11 @@
ast_channel_tech_set(chan, &multicast_rtp_tech);
+ fmt = ast_multicast_rtp_options_get_format(mcast_options);
+ if (!fmt) {
+ fmt = ast_format_cap_get_format(cap, 0);
+ }
+
ast_format_cap_append(caps, fmt, 0);
ast_channel_nativeformats_set(chan, caps);
ast_channel_set_writeformat(chan, fmt);
@@ -207,12 +220,14 @@
ao2_ref(fmt, -1);
ao2_ref(caps, -1);
+ ast_multicast_rtp_free_options(mcast_options);
return chan;
failure:
ao2_cleanup(fmt);
ao2_cleanup(caps);
+ ast_multicast_rtp_free_options(mcast_options);
*cause = AST_CAUSE_FAILURE;
return NULL;
}
diff --git a/include/asterisk/multicast_rtp.h b/include/asterisk/multicast_rtp.h
new file mode 100644
index 0000000..c2bf6f0
--- /dev/null
+++ b/include/asterisk/multicast_rtp.h
@@ -0,0 +1,58 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2016, Digium, Inc.
+ *
+ * Mark Michelson <mmichelson at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+#ifndef MULTICAST_RTP_H_
+#define MULTICAST_RTP_H_
+struct ast_multicast_rtp_options;
+
+/*!
+ * \brief Create multicast RTP options.
+ *
+ * These are passed to the multicast RTP engine on its creation.
+ *
+ * \param type The type of multicast RTP, either "basic" or "linksys"
+ * \param options Miscellaneous options
+ * \retval NULL Failure
+ * \retval non-NULL success
+ */
+struct ast_multicast_rtp_options *ast_multicast_rtp_create_options(const char *type,
+ const char *options);
+
+/*!
+ * \brief Free multicast RTP options
+ *
+ * This function is NULL-tolerant
+ *
+ * \param mcast_options Options to free
+ */
+void ast_multicast_rtp_free_options(struct ast_multicast_rtp_options *mcast_options);
+
+/*!
+ * \brief Get format specified in multicast options
+ *
+ * Multicast options allow for a format to be selected.
+ * This function accesses the selected format and creates
+ * an ast_format structure for it.
+ *
+ * \param mcast_options The options where a codec was specified
+ * \retval NULL No format specified in the options
+ * \revval non-NULL The format to use for communication
+ */
+struct ast_format *ast_multicast_rtp_options_get_format(struct ast_multicast_rtp_options *mcast_options);
+
+#endif /* MULTICAST_RTP_H_ */
diff --git a/res/res_rtp_multicast.c b/res/res_rtp_multicast.c
index 192f3d1..c6c2890 100644
--- a/res/res_rtp_multicast.c
+++ b/res/res_rtp_multicast.c
@@ -54,6 +54,8 @@
#include "asterisk/module.h"
#include "asterisk/rtp_engine.h"
#include "asterisk/format_cache.h"
+#include "asterisk/multicast_rtp.h"
+#include "asterisk/app.h"
/*! Command value used for Linksys paging to indicate we are starting */
#define LINKSYS_MCAST_STARTCMD 6
@@ -63,8 +65,10 @@
/*! \brief Type of paging to do */
enum multicast_type {
+ /*! Type has not been set yet */
+ MULTICAST_TYPE_UNSPECIFIED = 0,
/*! Simple multicast enabled client/receiver paging like Snom and Barix uses */
- MULTICAST_TYPE_BASIC = 0,
+ MULTICAST_TYPE_BASIC,
/*! More advanced Linksys type paging which requires a start and stop packet */
MULTICAST_TYPE_LINKSYS,
};
@@ -95,6 +99,80 @@
struct timeval txcore;
};
+enum {
+ OPT_CODEC = (1 << 0),
+ OPT_LOOP = (1 << 1),
+ OPT_TTL = (1 << 2),
+ OPT_IF = (1 << 3),
+};
+
+enum {
+ OPT_ARG_CODEC = 0,
+ OPT_ARG_LOOP,
+ OPT_ARG_TTL,
+ OPT_ARG_IF,
+ OPT_ARG_ARRAY_SIZE,
+};
+
+AST_APP_OPTIONS(multicast_rtp_options, BEGIN_OPTIONS
+ AST_APP_OPTION_ARG('c', OPT_CODEC, OPT_ARG_CODEC),
+ AST_APP_OPTION_ARG('l', OPT_LOOP, OPT_ARG_LOOP),
+ AST_APP_OPTION_ARG('t', OPT_TTL, OPT_ARG_TTL),
+ AST_APP_OPTION_ARG('i', OPT_IF, OPT_ARG_IF),
+END_OPTIONS );
+
+struct ast_multicast_rtp_options {
+ char *type;
+ char *options;
+ struct ast_format *fmt;
+ struct ast_flags opts;
+ char *opt_args[OPT_ARG_ARRAY_SIZE];
+ char buf[0];
+};
+
+struct ast_multicast_rtp_options *ast_multicast_rtp_create_options(const char *type,
+ const char *options)
+{
+ struct ast_multicast_rtp_options *mcast_options;
+ char *pos;
+
+ mcast_options = ast_calloc(1, sizeof(*mcast_options)
+ + strlen(type)
+ + strlen(options) + 2);
+ if (!mcast_options) {
+ return NULL;
+ }
+
+ pos = mcast_options->buf;
+
+ strcpy(pos, type);
+ mcast_options->type = pos;
+ pos += strlen(type) + 1;
+
+ strcpy(pos, options);
+ mcast_options->options = pos;
+
+ ast_app_parse_options(multicast_rtp_options, &mcast_options->opts,
+ mcast_options->opt_args, mcast_options->options);
+
+ return mcast_options;
+}
+
+void ast_multicast_rtp_free_options(struct ast_multicast_rtp_options *mcast_options)
+{
+ ast_free(mcast_options);
+}
+
+struct ast_format *ast_multicast_rtp_options_get_format(struct ast_multicast_rtp_options *mcast_options)
+{
+ if (ast_test_flag(&mcast_options->opts, OPT_CODEC)
+ && !ast_strlen_zero(mcast_options->opt_args[OPT_ARG_CODEC])) {
+ return ast_format_cache_get(mcast_options->opt_args[OPT_ARG_CODEC]);
+ }
+
+ return NULL;
+}
+
/* Forward Declarations */
static int multicast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data);
static int multicast_rtp_activate(struct ast_rtp_instance *instance);
@@ -112,21 +190,93 @@
.read = multicast_rtp_read,
};
-/*! \brief Function called to create a new multicast instance */
-static int multicast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data)
+static int set_type(struct multicast_rtp *multicast, const char *type)
{
- struct multicast_rtp *multicast;
- const char *type = data;
-
- if (!(multicast = ast_calloc(1, sizeof(*multicast)))) {
- return -1;
- }
-
if (!strcasecmp(type, "basic")) {
multicast->type = MULTICAST_TYPE_BASIC;
} else if (!strcasecmp(type, "linksys")) {
multicast->type = MULTICAST_TYPE_LINKSYS;
} else {
+ ast_log(LOG_WARNING, "Unrecognized multicast type '%s' specified.\n", type);
+ return -1;
+ }
+
+ return 0;
+}
+
+static void set_ttl(int sock, const char *ttl_str)
+{
+ int ttl;
+
+ if (ast_strlen_zero(ttl_str)) {
+ return;
+ }
+
+ ast_debug(3, "Setting multicast TTL to %s\n", ttl_str);
+
+ if (sscanf(ttl_str, "%d", &ttl) < 1) {
+ ast_log(LOG_WARNING, "Inavlid multicast ttl option '%s'\n", ttl_str);
+ return;
+ }
+
+ if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
+ ast_log(LOG_WARNING, "Could not set multicast ttl to '%s': %s\n",
+ ttl_str, strerror(errno));
+ }
+}
+
+static void set_loop(int sock, const char *loop_str)
+{
+ unsigned char loop;
+
+ if (ast_strlen_zero(loop_str)) {
+ return;
+ }
+
+ ast_debug(3, "Setting multicast loop to %s\n", loop_str);
+
+ if (sscanf(loop_str, "%hhu", &loop) < 1) {
+ ast_log(LOG_WARNING, "Invalid multicast loop option '%s'\n", loop_str);
+ return;
+ }
+
+ if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0) {
+ ast_log(LOG_WARNING, "Could not set multicast loop to '%s': %s\n",
+ loop_str, strerror(errno));
+ }
+}
+
+static void set_if(int sock, const char *if_str)
+{
+ struct in_addr iface;
+
+ if (ast_strlen_zero(if_str)) {
+ return;
+ }
+
+ ast_debug(3, "Setting multicast if to %s\n", if_str);
+
+ if (!inet_aton(if_str, &iface)) {
+ ast_log(LOG_WARNING, "Cannot parse if option '%s'\n", if_str);
+ }
+
+ if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &iface, sizeof(iface)) < 0) {
+ ast_log(LOG_WARNING, "Could not set multicast if to '%s': %s\n",
+ if_str, strerror(errno));
+ }
+}
+
+/*! \brief Function called to create a new multicast instance */
+static int multicast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data)
+{
+ struct multicast_rtp *multicast;
+ struct ast_multicast_rtp_options *mcast_options = data;
+
+ if (!(multicast = ast_calloc(1, sizeof(*multicast)))) {
+ return -1;
+ }
+
+ if (set_type(multicast, mcast_options->type)) {
ast_free(multicast);
return -1;
}
@@ -134,6 +284,18 @@
if ((multicast->socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
ast_free(multicast);
return -1;
+ }
+
+ if (ast_test_flag(&mcast_options->opts, OPT_LOOP)) {
+ set_loop(multicast->socket, mcast_options->opt_args[OPT_ARG_LOOP]);
+ }
+
+ if (ast_test_flag(&mcast_options->opts, OPT_TTL)) {
+ set_ttl(multicast->socket, mcast_options->opt_args[OPT_ARG_TTL]);
+ }
+
+ if (ast_test_flag(&mcast_options->opts, OPT_IF)) {
+ set_if(multicast->socket, mcast_options->opt_args[OPT_ARG_IF]);
}
multicast->ssrc = ast_random();
@@ -316,7 +478,7 @@
return 0;
}
-AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Multicast RTP Engine",
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Multicast RTP Engine",
.support_level = AST_MODULE_SUPPORT_CORE,
.load = load_module,
.unload = unload_module,
diff --git a/res/res_rtp_multicast.exports.in b/res/res_rtp_multicast.exports.in
new file mode 100644
index 0000000..995a180
--- /dev/null
+++ b/res/res_rtp_multicast.exports.in
@@ -0,0 +1,6 @@
+{
+ global:
+ LINKER_SYMBOL_PREFIXast_multicast_rtp*;
+ local:
+ *;
+};
--
To view, visit https://gerrit.asterisk.org/2910
To unsubscribe, visit https://gerrit.asterisk.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I033b706b533f0aa635c342eb738e0bcefa07e219
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Mark Michelson <mmichelson at digium.com>
More information about the asterisk-code-review
mailing list