From 8762648f8722c3e8ab18bc38e0a574fa732add44 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Tue, 26 Mar 2019 15:34:07 +0100 Subject: [PATCH] rtp: port to C++ --- include/villas/nodes/{rtp.h => rtp.hpp} | 39 +++++++++------- lib/nodes/CMakeLists.txt | 2 +- lib/nodes/{rtp.c => rtp.cpp} | 60 +++++++++++-------------- 3 files changed, 51 insertions(+), 50 deletions(-) rename include/villas/nodes/{rtp.h => rtp.hpp} (85%) rename lib/nodes/{rtp.c => rtp.cpp} (93%) diff --git a/include/villas/nodes/rtp.h b/include/villas/nodes/rtp.hpp similarity index 85% rename from include/villas/nodes/rtp.h rename to include/villas/nodes/rtp.hpp index a1e85c557..01be5803c 100644 --- a/include/villas/nodes/rtp.h +++ b/include/villas/nodes/rtp.hpp @@ -32,23 +32,33 @@ #include +extern "C" { #include +} #include #include #include #include +#include +#include -#ifdef __cplusplus -extern "C" { -#endif +/* Forward declarations */ +struct format_type; /** The maximum length of a packet which contains rtp data. */ #define RTP_INITIAL_BUFFER_LEN 1500 #define RTP_PACKET_TYPE 21 -/* Forward declarations */ -struct format_type; +enum rtp_throttle_mode { + RTCP_THROTTLE_DISABLED, + RTCP_THROTTLE_HOOK_DECIMATE, + RTCP_THROTTLE_HOOK_LIMIT_RATE +}; + +enum rtp_rtcp_mode { + RTCP_MODE_AIMD +}; struct rtp { struct rtp_sock *rs; /**< RTP socket */ @@ -68,17 +78,14 @@ struct rtp { int num_rrs; - enum { - RTCP_MODE_AIMD, - } mode; + enum rtp_rtcp_mode mode; - enum { - RTCP_THROTTLE_DISABLED, - RTCP_THROTTLE_HOOK_DECIMATE, - RTCP_THROTTLE_HOOK_LIMIT_RATE - } throttle_mode; + enum rtp_throttle_mode throttle_mode; - struct hook *throttle_hook; + union { + villas::node::DecimateHook *decimate; + villas::node::LimitRateHook *limit_rate; + } throttle_hook; } rtcp; struct { @@ -94,6 +101,8 @@ struct rtp { struct mbuf *send_mb; }; +extern "C" { + /** @see node_type::print */ char * rtp_print(struct node *n); @@ -112,8 +121,6 @@ int rtp_read(struct node *n, struct sample *smps[], unsigned cnt, unsigned *rele /** @see node_type::write */ int rtp_write(struct node *n, struct sample *smps[], unsigned cnt, unsigned *release); -#ifdef __cplusplus } -#endif /** @} */ diff --git a/lib/nodes/CMakeLists.txt b/lib/nodes/CMakeLists.txt index 49651392c..b9b269ceb 100644 --- a/lib/nodes/CMakeLists.txt +++ b/lib/nodes/CMakeLists.txt @@ -126,7 +126,7 @@ endif() # Enable RTP node type when libre is available if(RE_FOUND AND WITH_IO) - list(APPEND NODE_SRC rtp.c) + list(APPEND NODE_SRC rtp.cpp) list(APPEND INCLUDE_DIRS ${RE_INCLUDE_DIRS}) list(APPEND LIBRARIES PkgConfig::RE) endif() diff --git a/lib/nodes/rtp.c b/lib/nodes/rtp.cpp similarity index 93% rename from lib/nodes/rtp.c rename to lib/nodes/rtp.cpp index 2f207ce9f..59054181f 100644 --- a/lib/nodes/rtp.c +++ b/lib/nodes/rtp.cpp @@ -27,6 +27,7 @@ #include #include +extern "C" { #include #include #include @@ -35,12 +36,11 @@ #include #include #undef ALIGN_MASK +} #include #include -#include -#include -#include +#include #include #include #include @@ -52,8 +52,12 @@ static pthread_t re_pthread; +using namespace villas::node; + +extern "C" { + /* Forward declarations */ -static struct plugin p; +extern struct plugin p; static int rtp_set_rate(struct node *n, double rate) { @@ -62,14 +66,15 @@ static int rtp_set_rate(struct node *n, double rate) switch (r->rtcp.throttle_mode) { case RTCP_THROTTLE_HOOK_LIMIT_RATE: - limit_rate_set_rate(r->rtcp.throttle_hook, rate); + r->rtcp.throttle_hook.limit_rate->setRate(rate); break; case RTCP_THROTTLE_HOOK_DECIMATE: ratio = r->rate / rate; if (ratio == 0) ratio = 1; - decimate_set_ratio(r->rtcp.throttle_hook, ratio); + + r->rtcp.throttle_hook.decimate->setRatio(ratio); break; case RTCP_THROTTLE_DISABLED: @@ -329,7 +334,7 @@ static void rtcp_handler(const struct sa *src, struct rtcp_msg *msg, void *arg) /* source not used */ (void) src; - debug(5, "RTCP: recv %s", rtcp_type_name(msg->hdr.pt)); + debug(5, "RTCP: recv %s", rtcp_type_name((enum rtcp_type) msg->hdr.pt)); if (msg->hdr.pt == RTCP_SR) { if (msg->hdr.count > 0) { @@ -374,35 +379,20 @@ int rtp_start(struct node *n) /* Initialize throttle hook */ if (r->rtcp.throttle_mode != RTCP_THROTTLE_DISABLED) { - struct hook_type *throttle_hook_type; - - r->rtcp.throttle_hook = alloc(sizeof(struct hook)); - if (!r->rtcp.throttle_hook) - return -1; - - r->rtcp.throttle_hook->state = STATE_DESTROYED; - switch (r->rtcp.throttle_mode) { case RTCP_THROTTLE_HOOK_DECIMATE: - throttle_hook_type = hook_type_lookup("decimate"); + r->rtcp.throttle_hook.decimate = new DecimateHook(nullptr, n, 0, 0); break; case RTCP_THROTTLE_HOOK_LIMIT_RATE: - throttle_hook_type = hook_type_lookup("limit_rate"); + r->rtcp.throttle_hook.limit_rate = new LimitRateHook(nullptr, n, 0, 0); break; default: - throttle_hook_type = NULL; + return -1; } - if (!throttle_hook_type) - return -1; - - ret = hook_init(r->rtcp.throttle_hook, throttle_hook_type, NULL, n); - if (ret) - return ret; - - vlist_push(&n->out.hooks, r->rtcp.throttle_hook); + vlist_push(&n->out.hooks, (void *) r->rtcp.throttle_hook.limit_rate); } ret = rtp_set_rate(n, r->aimd.last_rate); @@ -511,12 +501,12 @@ int rtp_type_start(struct super_node *sn) for (size_t i = 0; i < vlist_length(&p.node.instances); i++) { struct node *n = (struct node *) vlist_at(&p.node.instances, i); struct rtp *r = (struct rtp *) n->_vd; - struct interface *i = if_get_egress(&r->out.saddr_rtp.u.sa, interfaces); + struct interface *j = if_get_egress(&r->out.saddr_rtp.u.sa, interfaces); - if (!i) + if (!j) error("Failed to find egress interface for node: %s", node_name(n)); - vlist_push(&i->nodes, n); + vlist_push(&j->nodes, n); } #endif /* WITH_NETEM */ @@ -624,7 +614,7 @@ int rtp_netem_fds(struct node *n, int fds[]) return m; } -static struct plugin p = { +struct plugin p = { .name = "rtp", #ifdef WITH_NETEM .description = "real-time transport protocol (libre, libnl3 netem support)", @@ -635,20 +625,24 @@ static struct plugin p = { .node = { .vectorize = 0, .size = sizeof(struct rtp), - .type.start = rtp_type_start, - .type.stop = rtp_type_stop, + .type = { + .start = rtp_type_start, + .stop = rtp_type_stop + }, .init = rtp_init, - .reverse = rtp_reverse, .parse = rtp_parse, .print = rtp_print, .start = rtp_start, .stop = rtp_stop, .read = rtp_read, .write = rtp_write, + .reverse = rtp_reverse, .poll_fds = rtp_poll_fds, .netem_fds = rtp_netem_fds } }; +} /* extern C */ + REGISTER_PLUGIN(&p) LIST_INIT_STATIC(&p.node.instances)