Skip to content

Commit d353e9b

Browse files
committed
Fix rare deadlock in AudioBridge plugin when closing connections (#3387)
1 parent 56cfe33 commit d353e9b

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

plugins/janus_audiobridge.c

+11-15
Original file line numberDiff line numberDiff line change
@@ -4283,16 +4283,16 @@ static json_t *janus_audiobridge_process_synchronous_request(janus_audiobridge_s
42834283
goto prepare_response;
42844284
}
42854285

4286+
janus_mutex_lock(&participant->qmutex);
42864287
participant->muted = muted;
42874288
if(participant->muted) {
42884289
JANUS_LOG(LOG_VERB, "Setting muted property: %s (room %s, user %s)\n",
42894290
participant->muted ? "true" : "false", participant->room->room_id_str, participant->user_id_str);
42904291
/* Clear the queued packets waiting to be handled */
4291-
janus_mutex_lock(&participant->qmutex);
42924292
janus_audiobridge_participant_clear_jitter_buffer(participant);
42934293
janus_audiobridge_participant_clear_inbuf(participant);
4294-
janus_mutex_unlock(&participant->qmutex);
42954294
}
4295+
janus_mutex_unlock(&participant->qmutex);
42964296

42974297
if(audiobridge != NULL) {
42984298
json_t *list = json_array();
@@ -6963,16 +6963,16 @@ static void *janus_audiobridge_handler(void *data) {
69636963
}
69646964
if(muted || display || (participant->stereo && spatial) || denoise) {
69656965
if(muted) {
6966+
janus_mutex_lock(&participant->qmutex);
69666967
participant->muted = json_is_true(muted);
69676968
JANUS_LOG(LOG_VERB, "Setting muted property: %s (room %s, user %s)\n",
69686969
participant->muted ? "true" : "false", participant->room->room_id_str, participant->user_id_str);
69696970
if(participant->muted) {
69706971
/* Clear the queued packets waiting to be handled */
6971-
janus_mutex_lock(&participant->qmutex);
69726972
janus_audiobridge_participant_clear_jitter_buffer(participant);
69736973
janus_audiobridge_participant_clear_inbuf(participant);
6974-
janus_mutex_unlock(&participant->qmutex);
69756974
}
6975+
janus_mutex_unlock(&participant->qmutex);
69766976
}
69776977
if(display) {
69786978
char *old_display = participant->display;
@@ -8708,7 +8708,6 @@ static void *janus_audiobridge_participant_thread(void *data) {
87088708
/* Start with packets to decode and queue for the mixer */
87098709
now = janus_get_monotonic_time();
87108710
janus_mutex_lock(&participant->qmutex);
8711-
gboolean locked = TRUE;
87128711
/* Start by reading packets to decode from the jitter buffer on a clock */
87138712
if(now - before >= 18000) {
87148713
before += 20000;
@@ -8727,9 +8726,8 @@ static void *janus_audiobridge_participant_thread(void *data) {
87278726
} else {
87288727
/* Decode the audio packet */
87298728
bpkt = (janus_audiobridge_buffer_packet *)jbp.data;
8730-
janus_mutex_unlock(&participant->qmutex);
8731-
locked = FALSE;
87328729
if(!g_atomic_int_compare_and_exchange(&participant->decoding, 0, 1)) {
8730+
janus_mutex_unlock(&participant->qmutex);
87338731
/* This means we're cleaning up, so don't try to decode */
87348732
janus_audiobridge_buffer_packet_destroy(bpkt);
87358733
break;
@@ -8740,9 +8738,10 @@ static void *janus_audiobridge_participant_thread(void *data) {
87408738
int plen = 0;
87418739
const unsigned char *payload = (const unsigned char *)janus_rtp_payload(buffer, len, &plen);
87428740
if(!payload) {
8743-
g_atomic_int_set(&participant->decoding, 0);
87448741
JANUS_LOG(LOG_ERR, "[%s] Ops! got an error accessing the RTP payload\n",
87458742
participant->codec == JANUS_AUDIOCODEC_OPUS ? "Opus" : "G.711");
8743+
g_atomic_int_set(&participant->decoding, 0);
8744+
janus_mutex_unlock(&participant->qmutex);
87468745
janus_audiobridge_buffer_packet_destroy(bpkt);
87478746
break;
87488747
}
@@ -8776,9 +8775,7 @@ static void *janus_audiobridge_participant_thread(void *data) {
87768775
janus_audiobridge_participant_denoise(participant, (char *)pkt->data, pkt->length);
87778776
#endif
87788777
/* Queue the decoded redundant packet for the mixer */
8779-
janus_mutex_lock(&participant->qmutex);
87808778
participant->inbuf = g_list_append(participant->inbuf, pkt);
8781-
janus_mutex_unlock(&participant->qmutex);
87828779
/* Now we can process the next packet */
87838780
}
87848781
/* Decode the packet */
@@ -8797,8 +8794,9 @@ static void *janus_audiobridge_participant_thread(void *data) {
87978794
} else if(participant->codec == JANUS_AUDIOCODEC_PCMA || participant->codec == JANUS_AUDIOCODEC_PCMU) {
87988795
/* G.711 */
87998796
if(plen != 160) {
8800-
g_atomic_int_set(&participant->decoding, 0);
88018797
JANUS_LOG(LOG_WARN, "[G.711] Wrong packet size (expected 160, got %d), skipping audio packet\n", plen);
8798+
g_atomic_int_set(&participant->decoding, 0);
8799+
janus_mutex_unlock(&participant->qmutex);
88028800
janus_audiobridge_buffer_packet_destroy(bpkt);
88038801
break;
88048802
}
@@ -8832,13 +8830,12 @@ static void *janus_audiobridge_participant_thread(void *data) {
88328830
} else {
88338831
JANUS_LOG(LOG_ERR, "[G.711] Ops! got an error decoding the audio frame\n");
88348832
}
8833+
janus_mutex_unlock(&participant->qmutex);
88358834
g_free(pkt->data);
88368835
g_free(pkt);
88378836
break;
88388837
}
88398838
/* Queue the decoded packet for the mixer */
8840-
janus_mutex_lock(&participant->qmutex);
8841-
locked = TRUE;
88428839
/* Do not let queue-in grow too much */
88438840
guint count = g_list_length(participant->inbuf);
88448841
if(count > QUEUE_IN_MAX_PACKETS) {
@@ -8849,8 +8846,7 @@ static void *janus_audiobridge_participant_thread(void *data) {
88498846
}
88508847
}
88518848
}
8852-
if(locked)
8853-
janus_mutex_unlock(&participant->qmutex);
8849+
janus_mutex_unlock(&participant->qmutex);
88548850
/* Now check if there's packets to encode */
88558851
mixedpkt = g_async_queue_try_pop(participant->outbuf);
88568852
if(mixedpkt != NULL && g_atomic_int_get(&session->destroyed) == 0 && g_atomic_int_get(&session->started)) {

0 commit comments

Comments
 (0)