Skip to content

Commit d7f7a57

Browse files
authored
Dilute frequency of PLI in the VideoRoom (#3423)
1 parent 02cbae1 commit d7f7a57

File tree

2 files changed

+39
-43
lines changed

2 files changed

+39
-43
lines changed

src/plugins/janus_videoroom.c

+35-40
Original file line numberDiff line numberDiff line change
@@ -2949,13 +2949,19 @@ static void janus_videoroom_codecstr(janus_videoroom *videoroom, char *audio_cod
29492949
}
29502950
}
29512951

2952-
/* Helper method to send an RTCP PLI to a remote publisher */
2953-
static void janus_videoroom_rtcp_pli_send(janus_videoroom_publisher_stream *ps) {
2954-
if(ps == NULL || ps->publisher == NULL)
2955-
return;
2956-
janus_videoroom_publisher *publisher = ps->publisher;
2957-
if(publisher->remote_rtcp_fd < 0 || publisher->rtcp_addr.ss_family == 0)
2952+
/* Helper method to send PLI to publishers.
2953+
* Send an PLI to local publisher and RTCP PLI to a remote publishers */
2954+
static void janus_videoroom_reqpli(janus_videoroom_publisher_stream *ps, const char *reason) {
2955+
if(ps == NULL || g_atomic_int_get(&ps->destroyed))
29582956
return;
2957+
janus_videoroom_publisher *remote_publisher = NULL;
2958+
if(ps->publisher->remote) {
2959+
if(ps->publisher == NULL || g_atomic_int_get(&ps->publisher->destroyed))
2960+
return;
2961+
remote_publisher = ps->publisher;
2962+
if(remote_publisher->remote_rtcp_fd < 0 || remote_publisher->rtcp_addr.ss_family == 0)
2963+
return;
2964+
}
29592965
if(!g_atomic_int_compare_and_exchange(&ps->sending_pli, 0, 1))
29602966
return;
29612967
gint64 now = janus_get_monotonic_time();
@@ -2965,44 +2971,33 @@ static void janus_videoroom_rtcp_pli_send(janus_videoroom_publisher_stream *ps)
29652971
g_atomic_int_set(&ps->sending_pli, 0);
29662972
return;
29672973
}
2968-
/* Update the time of when we last sent a keyframe request */
2974+
JANUS_LOG(LOG_VERB, "%s, sending PLI to %s (#%d, %s)\n", reason,
2975+
ps->publisher->user_id_str, ps->mindex, ps->publisher->display ? ps->publisher->display : "??");
29692976
g_atomic_int_set(&ps->need_pli, 0);
29702977
ps->pli_latest = janus_get_monotonic_time();
2971-
JANUS_LOG(LOG_HUGE, "Sending PLI\n");
2972-
/* Generate a PLI */
2973-
char rtcp_buf[12];
2974-
int rtcp_len = 12;
2975-
janus_rtcp_pli((char *)&rtcp_buf, rtcp_len);
2976-
uint32_t ssrc = REMOTE_PUBLISHER_BASE_SSRC + (ps->mindex*REMOTE_PUBLISHER_SSRC_STEP);
2977-
janus_rtcp_fix_ssrc(NULL, rtcp_buf, rtcp_len, 1, 1, ssrc);
2978-
/* Send the packet */
2979-
socklen_t addrlen = publisher->rtcp_addr.ss_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
2980-
int sent = 0;
2981-
if((sent = sendto(publisher->remote_rtcp_fd, rtcp_buf, rtcp_len, 0,
2982-
(struct sockaddr *)&publisher->rtcp_addr, addrlen)) < 0) {
2983-
JANUS_LOG(LOG_ERR, "Error in sendto... %d (%s)\n", errno, g_strerror(errno));
2984-
} else {
2985-
JANUS_LOG(LOG_HUGE, "Sent %d/%d bytes\n", sent, rtcp_len);
2986-
}
2987-
g_atomic_int_set(&ps->sending_pli, 0);
2988-
}
2989-
2990-
static void janus_videoroom_reqpli(janus_videoroom_publisher_stream *ps, const char *reason) {
2991-
if(ps == NULL || g_atomic_int_get(&ps->destroyed) || ps->publisher == NULL || g_atomic_int_get(&ps->publisher->destroyed))
2992-
return;
2993-
/* Send a PLI */
2994-
JANUS_LOG(LOG_VERB, "%s sending PLI to %s (#%d, %s)\n", reason,
2995-
ps->publisher->user_id_str, ps->mindex, ps->publisher->display ? ps->publisher->display : "??");
2996-
if(!ps->publisher->remote) {
2997-
/* Easy enough, local publisher so we ask the Janus core to send a PLI */
2978+
/* Update the time of when we last sent a keyframe request */
2979+
ps->fir_latest = ps->pli_latest;
2980+
if(remote_publisher == NULL) {
2981+
/* Local publisher so we ask the Janus core to send a PLI */
29982982
gateway->send_pli_stream(ps->publisher->session->handle, ps->mindex);
29992983
} else {
3000-
/* This is a remote publisher, so we'll need to send a PLI to the remote RTCP address */
3001-
JANUS_LOG(LOG_VERB, "Sending PLI to remote publisher\n");
3002-
janus_videoroom_rtcp_pli_send(ps);
2984+
/* Generate a PLI */
2985+
char rtcp_buf[12];
2986+
int rtcp_len = 12;
2987+
janus_rtcp_pli((char *)&rtcp_buf, rtcp_len);
2988+
uint32_t ssrc = REMOTE_PUBLISHER_BASE_SSRC + (ps->mindex*REMOTE_PUBLISHER_SSRC_STEP);
2989+
janus_rtcp_fix_ssrc(NULL, rtcp_buf, rtcp_len, 1, 1, ssrc);
2990+
/* Send the packet */
2991+
socklen_t addrlen = remote_publisher->rtcp_addr.ss_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
2992+
int sent = 0;
2993+
if((sent = sendto(remote_publisher->remote_rtcp_fd, rtcp_buf, rtcp_len, 0,
2994+
(struct sockaddr *)&remote_publisher->rtcp_addr, addrlen)) < 0) {
2995+
JANUS_LOG(LOG_ERR, "Error in sendto... %d (%s)\n", errno, g_strerror(errno));
2996+
} else {
2997+
JANUS_LOG(LOG_HUGE, "Sent %d/%d bytes\n", sent, rtcp_len);
2998+
}
30032999
}
3004-
/* Update the time of when we last sent a keyframe request */
3005-
ps->fir_latest = janus_get_monotonic_time();
3000+
g_atomic_int_set(&ps->sending_pli, 0);
30063001
}
30073002

30083003
/* Error codes */
@@ -13803,7 +13798,7 @@ static void *janus_videoroom_remote_publisher_thread(void *user_data) {
1380313798
ps = (janus_videoroom_publisher_stream *)temp->data;
1380413799
/* Any PLI and/or REMB we should send back to the source? */
1380513800
if(ps->type == JANUS_VIDEOROOM_MEDIA_VIDEO && g_atomic_int_get(&ps->need_pli))
13806-
janus_videoroom_rtcp_pli_send(ps);
13801+
janus_videoroom_reqpli(ps, "Delayed PLI request");
1380713802
temp = temp->next;
1380813803
}
1380913804
janus_mutex_unlock(&publisher->streams_mutex);

src/rtp.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,8 @@ gboolean janus_rtp_simulcasting_context_process_rtp(janus_rtp_simulcasting_conte
12181218
context->last_relayed = now;
12191219
} else if(context->substream > 0) {
12201220
/* Check if too much time went by with no packet relayed */
1221-
if((now - context->last_relayed) > (context->drop_trigger ? context->drop_trigger : 250000)) {
1221+
gint64 delay_us = (now - context->last_relayed);
1222+
if(delay_us > (context->drop_trigger ? context->drop_trigger : 250000)) {
12221223
context->last_relayed = now;
12231224
if(context->substream != substream && context->substream_target_temp != 0) {
12241225
if(context->substream_target > substream) {
@@ -1230,8 +1231,8 @@ gboolean janus_rtp_simulcasting_context_process_rtp(janus_rtp_simulcasting_conte
12301231
if(context->substream_target_temp < 0)
12311232
context->substream_target_temp = 0;
12321233
if(context->substream_target_temp != prev_target) {
1233-
JANUS_LOG(LOG_WARN, "No packet received on substream %d for a while, falling back to %d\n",
1234-
context->substream, context->substream_target_temp);
1234+
JANUS_LOG(LOG_WARN, "No packet received on substream %d for %"SCNi64"ms, falling back to %d\n",
1235+
context->substream, (delay_us / 1000), context->substream_target_temp);
12351236
/* Notify the caller that we (still) need a PLI */
12361237
context->need_pli = TRUE;
12371238
}

0 commit comments

Comments
 (0)