Skip to content

Commit

Permalink
Added DSCP support for RTP to NoSIP plugin too (see #2150)
Browse files Browse the repository at this point in the history
  • Loading branch information
lminiero committed May 11, 2020
1 parent ddbe10c commit 415e025
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 17 deletions.
8 changes: 8 additions & 0 deletions conf/janus.plugin.nosip.jcfg.sample
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ general: {

# Whether events should be sent to event handlers (default=true)
#events = false

# If you need DSCP packet marking and prioritization, you can configure
# the 'dscp_audio_rtp' and/or 'dscp_video_rtp' property to specific values,
# and the plugin will set it on all outgoing audio/video RTP packets.
# No packet marking is done if this parameter is undefined or equal to 0
#dscp_audio_rtp = 46
#dscp_video_rtp = 26

}
6 changes: 3 additions & 3 deletions conf/janus.plugin.sip.jcfg.sample
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ general: {

# Whether events should be sent to event handlers (default=true)
#events = false

# If you need DSCP packet marking and prioritization, you can configure
# the 'dscp_audio_rtp' and/or 'dscp_video_rtp' property to specific values,
# and Janus SIP plugin will set it on all outgoing audio/video RTP packets.
# and the plugin will set it on all outgoing audio/video RTP packets.
# No packet marking is done if this parameter is undefined or equal to 0
#dscp_audio_rtp = 46
#dscp_video_rtp = 26

}
50 changes: 44 additions & 6 deletions plugins/janus_nosip.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ static char *local_ip = NULL, *sdp_ip = NULL;
static uint16_t rtp_range_min = DEFAULT_RTP_RANGE_MIN;
static uint16_t rtp_range_max = DEFAULT_RTP_RANGE_MAX;
static uint16_t rtp_range_slider = DEFAULT_RTP_RANGE_MIN;
static int dscp_audio_rtp = 0;
static int dscp_video_rtp = 0;

static GThread *handler_thread;
static void *janus_nosip_handler(void *data);
Expand Down Expand Up @@ -724,6 +726,26 @@ int janus_nosip_init(janus_callbacks *callback, const char *config_path) {
JANUS_LOG(LOG_WARN, "Notification of events to handlers disabled for %s\n", JANUS_NOSIP_NAME);
}

/* Is there any DSCP TOS to apply? */
item = janus_config_get(config, config_general, janus_config_type_item, "dscp_audio_rtp");
if(item && item->value) {
int val = atoi(item->value);
if(val < 0) {
JANUS_LOG(LOG_WARN, "Ignoring dscp_audio_rtp value as it's not a positive integer\n");
} else {
dscp_audio_rtp = val;
}
}
item = janus_config_get(config, config_general, janus_config_type_item, "dscp_video_rtp");
if(item && item->value) {
int val = atoi(item->value);
if(val < 0) {
JANUS_LOG(LOG_WARN, "Ignoring dscp_video_rtp value as it's not a positive integer\n");
} else {
dscp_video_rtp = val;
}
}

janus_config_destroy(config);
}
config = NULL;
Expand Down Expand Up @@ -1883,26 +1905,42 @@ static int janus_nosip_bind_socket(int fd, int port) {
}

/* Bind RTP/RTCP port pair */
static int janus_nosip_allocate_port_pair(int fds[2], int ports[2]) {
static int janus_nosip_allocate_port_pair(gboolean video, int fds[2], int ports[2]) {
uint16_t rtp_port_next = rtp_range_slider; /* Read global slider */
uint16_t rtp_port_start = rtp_port_next;
gboolean rtp_port_wrap = FALSE;

int rtp_fd = -1, rtcp_fd = -1;
while(1) {
if(rtp_port_wrap && rtp_port_next >= rtp_port_start) { /* Full range scanned */
JANUS_LOG(LOG_ERR, "No ports available for audio/video channel in range: %u -- %u\n",
rtp_range_min, rtp_range_max);
JANUS_LOG(LOG_ERR, "No ports available for %s channel in range: %u -- %u\n",
video ? "video" : "audio", rtp_range_min, rtp_range_max);
break;
}
if(rtp_fd == -1) {
rtp_fd = socket(AF_INET, SOCK_DGRAM, 0);
/* Set the DSCP value if set in the config file */
if(!video && dscp_audio_rtp > 0) {
int optval = dscp_audio_rtp << 2;
int ret = setsockopt(rtp_fd, IPPROTO_IP, IP_TOS, &optval, sizeof(optval));
if(ret < 0) {
JANUS_LOG(LOG_WARN, "Error setting IP_TOS %d on audio RTP socket (error=%s)\n",
optval, strerror(errno));
}
} else if(video && dscp_video_rtp > 0) {
int optval = dscp_video_rtp << 2;
int ret = setsockopt(rtp_fd, IPPROTO_IP, IP_TOS, &optval, sizeof(optval));
if(ret < 0) {
JANUS_LOG(LOG_WARN, "Error setting IP_TOS %d on video RTP socket (error=%s)\n",
optval, strerror(errno));
}
}
}
if(rtcp_fd == -1) {
rtcp_fd = socket(AF_INET, SOCK_DGRAM, 0);
}
if(rtp_fd == -1 || rtcp_fd == -1) {
JANUS_LOG(LOG_ERR, "Error creating audio/video sockets...\n");
JANUS_LOG(LOG_ERR, "Error creating %s sockets...\n", video ? "video" : "audio");
break;
}
int rtp_port = rtp_port_next;
Expand Down Expand Up @@ -1991,7 +2029,7 @@ static int janus_nosip_allocate_local_ports(janus_nosip_session *session, gboole
}
JANUS_LOG(LOG_VERB, "Allocating audio ports:\n");
int fds[2], ports[2];
if(janus_nosip_allocate_port_pair(fds, ports)) {
if(janus_nosip_allocate_port_pair(FALSE, fds, ports)) {
return -1;
}
JANUS_LOG(LOG_VERB, "Audio RTP listener bound to port %d\n", ports[0]);
Expand All @@ -2015,7 +2053,7 @@ static int janus_nosip_allocate_local_ports(janus_nosip_session *session, gboole
}
JANUS_LOG(LOG_VERB, "Allocating video ports:\n");
int fds[2], ports[2];
if(janus_nosip_allocate_port_pair(fds, ports)) {
if(janus_nosip_allocate_port_pair(TRUE, fds, ports)) {
return -1;
}
JANUS_LOG(LOG_VERB, "Video RTP listener bound to port %d\n", ports[0]);
Expand Down
18 changes: 10 additions & 8 deletions plugins/janus_sip.c
Original file line number Diff line number Diff line change
Expand Up @@ -5707,12 +5707,13 @@ static int janus_sip_allocate_local_ports(janus_sip_session *session) {
return -1;
if(session->media.audio_rtp_fd == -1) {
session->media.audio_rtp_fd = socket(AF_INET, SOCK_DGRAM, 0);
/* Set the DSCP value if set in the config file */
if (dscp_audio_rtp > 0) {
/* Set the DSCP value if set in the config file */
if(dscp_audio_rtp > 0) {
int optval = dscp_audio_rtp << 2;
int ret = setsockopt(session->media.audio_rtp_fd, IPPROTO_IP, IP_TOS, &optval, sizeof(optval));
if (ret < 0) {
JANUS_LOG(LOG_WARN, "Error setting IP_TOS option on audio RTP socket, value=%d, error=%s\n",optval, strerror(errno));
if(ret < 0) {
JANUS_LOG(LOG_WARN, "Error setting IP_TOS %d on audio RTP socket (error=%s)\n",
optval, strerror(errno));
}
}
}
Expand Down Expand Up @@ -5764,12 +5765,13 @@ static int janus_sip_allocate_local_ports(janus_sip_session *session) {
return -1;
if(session->media.video_rtp_fd == -1) {
session->media.video_rtp_fd = socket(AF_INET, SOCK_DGRAM, 0);
/* Set the DSCP value if set in the config file */
if (dscp_video_rtp > 0) {
/* Set the DSCP value if set in the config file */
if(dscp_video_rtp > 0) {
int optval = dscp_video_rtp << 2;
int ret = setsockopt(session->media.video_rtp_fd, IPPROTO_IP, IP_TOS, &optval, sizeof(optval));
if (ret < 0) {
JANUS_LOG(LOG_WARN, "Error setting IP_TOS option on video RTP socket, value=%d, error=%s\n",optval, strerror(errno));
if(ret < 0) {
JANUS_LOG(LOG_WARN, "Error setting IP_TOS %d on video RTP socket (error=%s)\n",
optval, strerror(errno));
}
}
}
Expand Down

0 comments on commit 415e025

Please sign in to comment.