From 359706e54f3e9588a469e194ef3472099753efb2 Mon Sep 17 00:00:00 2001 From: Alessandro Toppi Date: Thu, 7 Sep 2023 14:57:48 +0200 Subject: [PATCH] Extend sessions_mutex range for audiobridge message handler --- src/plugins/janus_audiobridge.c | 43 ++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/plugins/janus_audiobridge.c b/src/plugins/janus_audiobridge.c index 72434659dc..ee933de4ad 100644 --- a/src/plugins/janus_audiobridge.c +++ b/src/plugins/janus_audiobridge.c @@ -5811,11 +5811,11 @@ static void *janus_audiobridge_handler(void *data) { janus_audiobridge_message_free(msg); continue; } - janus_mutex_unlock(&sessions_mutex); /* Handle request */ error_code = 0; root = NULL; if(msg->message == NULL) { + janus_mutex_unlock(&sessions_mutex); JANUS_LOG(LOG_ERR, "No message??\n"); error_code = JANUS_AUDIOBRIDGE_ERROR_NO_MESSAGE; g_snprintf(error_cause, 512, "%s", "No message??"); @@ -5826,8 +5826,10 @@ static void *janus_audiobridge_handler(void *data) { JANUS_VALIDATE_JSON_OBJECT(root, request_parameters, error_code, error_cause, TRUE, JANUS_AUDIOBRIDGE_ERROR_MISSING_ELEMENT, JANUS_AUDIOBRIDGE_ERROR_INVALID_ELEMENT); - if(error_code != 0) + if(error_code != 0) { + janus_mutex_unlock(&sessions_mutex); goto error; + } json_t *request = json_object_get(root, "request"); const char *request_text = json_string_value(request); json_t *event = NULL; @@ -5841,6 +5843,7 @@ static void *janus_audiobridge_handler(void *data) { got_offer = !strcasecmp(msg_sdp_type, "offer"); got_answer = !strcasecmp(msg_sdp_type, "answer"); if(!got_offer && !got_answer) { + janus_mutex_unlock(&sessions_mutex); JANUS_LOG(LOG_ERR, "Unsupported SDP type '%s'\n", msg_sdp_type); error_code = JANUS_AUDIOBRIDGE_ERROR_INVALID_SDP; g_snprintf(error_cause, 512, "Unsupported SDP type '%s'\n", msg_sdp_type); @@ -5851,6 +5854,7 @@ static void *janus_audiobridge_handler(void *data) { JANUS_LOG(LOG_VERB, "Configuring new participant\n"); janus_audiobridge_participant *participant = session->participant; if(participant != NULL && participant->room != NULL) { + janus_mutex_unlock(&sessions_mutex); JANUS_LOG(LOG_ERR, "Already in a room (use changeroom to join another one)\n"); error_code = JANUS_AUDIOBRIDGE_ERROR_ALREADY_JOINED; g_snprintf(error_cause, 512, "Already in a room (use changeroom to join another one)"); @@ -5859,15 +5863,19 @@ static void *janus_audiobridge_handler(void *data) { JANUS_VALIDATE_JSON_OBJECT(root, join_parameters, error_code, error_cause, TRUE, JANUS_AUDIOBRIDGE_ERROR_MISSING_ELEMENT, JANUS_AUDIOBRIDGE_ERROR_INVALID_ELEMENT); - if(error_code != 0) + if(error_code != 0) { + janus_mutex_unlock(&sessions_mutex); goto error; + } json_t *rtp = json_object_get(root, "rtp"); if(rtp != NULL) { JANUS_VALIDATE_JSON_OBJECT(root, rtp_parameters, error_code, error_cause, TRUE, JANUS_AUDIOBRIDGE_ERROR_MISSING_ELEMENT, JANUS_AUDIOBRIDGE_ERROR_INVALID_ELEMENT); - if(error_code != 0) + if(error_code != 0) { + janus_mutex_unlock(&sessions_mutex); goto error; + } if(msg_sdp != NULL) { JANUS_LOG(LOG_WARN, "Added plain RTP details but negotiating a WebRTC PeerConnection: plain RTP will be ignored\n"); rtp = NULL; @@ -5883,8 +5891,10 @@ static void *janus_audiobridge_handler(void *data) { error_code, error_cause, TRUE, JANUS_AUDIOBRIDGE_ERROR_MISSING_ELEMENT, JANUS_AUDIOBRIDGE_ERROR_INVALID_ELEMENT); } - if(error_code != 0) + if(error_code != 0) { + janus_mutex_unlock(&sessions_mutex); goto error; + } if(!string_ids) { JANUS_VALIDATE_JSON_OBJECT(root, idopt_parameters, error_code, error_cause, TRUE, @@ -5894,8 +5904,10 @@ static void *janus_audiobridge_handler(void *data) { error_code, error_cause, TRUE, JANUS_AUDIOBRIDGE_ERROR_MISSING_ELEMENT, JANUS_AUDIOBRIDGE_ERROR_INVALID_ELEMENT); } - if(error_code != 0) + if(error_code != 0) { + janus_mutex_unlock(&sessions_mutex); goto error; + } json_t *room = json_object_get(root, "room"); guint64 room_id = 0; char room_id_num[30], *room_id_str = NULL; @@ -5911,6 +5923,7 @@ static void *janus_audiobridge_handler(void *data) { string_ids ? (gpointer)room_id_str : (gpointer)&room_id); if(audiobridge == NULL) { janus_mutex_unlock(&rooms_mutex); + janus_mutex_unlock(&sessions_mutex); error_code = JANUS_AUDIOBRIDGE_ERROR_NO_SUCH_ROOM; JANUS_LOG(LOG_ERR, "No such room (%s)\n", room_id_str); g_snprintf(error_cause, 512, "No such room (%s)", room_id_str); @@ -5923,6 +5936,7 @@ static void *janus_audiobridge_handler(void *data) { /* Plain RTP participants are not allowed in this room */ janus_mutex_unlock(&audiobridge->mutex); janus_refcount_decrease(&audiobridge->ref); + janus_mutex_unlock(&sessions_mutex); error_code = JANUS_AUDIOBRIDGE_ERROR_UNAUTHORIZED; JANUS_LOG(LOG_ERR, "Plain RTP participants not allowed in this room\n"); g_snprintf(error_cause, 512, "Plain RTP participants not allowed in this room"); @@ -5934,6 +5948,7 @@ static void *janus_audiobridge_handler(void *data) { if(error_code != 0) { janus_mutex_unlock(&audiobridge->mutex); janus_refcount_decrease(&audiobridge->ref); + janus_mutex_unlock(&sessions_mutex); goto error; } /* A token might be required too */ @@ -5946,6 +5961,7 @@ static void *janus_audiobridge_handler(void *data) { g_snprintf(error_cause, 512, "Unauthorized (not in the allowed list)"); janus_mutex_unlock(&audiobridge->mutex); janus_refcount_decrease(&audiobridge->ref); + janus_mutex_unlock(&sessions_mutex); goto error; } } @@ -5957,6 +5973,7 @@ static void *janus_audiobridge_handler(void *data) { if(error_code != 0) { janus_mutex_unlock(&audiobridge->mutex); janus_refcount_decrease(&audiobridge->ref); + janus_mutex_unlock(&sessions_mutex); goto error; } admin = TRUE; @@ -5970,6 +5987,7 @@ static void *janus_audiobridge_handler(void *data) { if(error_code != 0) { janus_mutex_unlock(&audiobridge->mutex); janus_refcount_decrease(&audiobridge->ref); + janus_mutex_unlock(&sessions_mutex); goto error; } const char *group_name = json_string_value(json_object_get(root, "group")); @@ -5977,6 +5995,7 @@ static void *janus_audiobridge_handler(void *data) { if(group == 0) { janus_mutex_unlock(&audiobridge->mutex); janus_refcount_decrease(&audiobridge->ref); + janus_mutex_unlock(&sessions_mutex); JANUS_LOG(LOG_ERR, "No such group (%s)\n", group_name); error_code = JANUS_AUDIOBRIDGE_ERROR_NO_SUCH_GROUP; g_snprintf(error_cause, 512, "No such group (%s)", group_name); @@ -6011,6 +6030,7 @@ static void *janus_audiobridge_handler(void *data) { if(complexity < 1 || complexity > 10) { janus_mutex_unlock(&audiobridge->mutex); janus_refcount_decrease(&audiobridge->ref); + janus_mutex_unlock(&sessions_mutex); JANUS_LOG(LOG_ERR, "Invalid element (quality should be a positive integer between 1 and 10)\n"); error_code = JANUS_AUDIOBRIDGE_ERROR_INVALID_ELEMENT; g_snprintf(error_cause, 512, "Invalid element (quality should be a positive integer between 1 and 10)"); @@ -6020,6 +6040,7 @@ static void *janus_audiobridge_handler(void *data) { if(expected_loss > 20) { janus_mutex_unlock(&audiobridge->mutex); janus_refcount_decrease(&audiobridge->ref); + janus_mutex_unlock(&sessions_mutex); JANUS_LOG(LOG_ERR, "Invalid element (expected_loss should be a positive integer between 0 and 20)\n"); error_code = JANUS_AUDIOBRIDGE_ERROR_INVALID_ELEMENT; g_snprintf(error_cause, 512, "Invalid element (expected_loss should be a positive integer between 0 and 20)"); @@ -6031,6 +6052,7 @@ static void *janus_audiobridge_handler(void *data) { if(codec != JANUS_AUDIOCODEC_OPUS && codec != JANUS_AUDIOCODEC_PCMA && codec != JANUS_AUDIOCODEC_PCMU) { janus_mutex_unlock(&audiobridge->mutex); janus_refcount_decrease(&audiobridge->ref); + janus_mutex_unlock(&sessions_mutex); JANUS_LOG(LOG_ERR, "Invalid element (codec must opus, pcmu or pcma)\n"); error_code = JANUS_AUDIOBRIDGE_ERROR_INVALID_ELEMENT; g_snprintf(error_cause, 512, "Invalid element (codec must opus, pcmu or pcma)"); @@ -6054,6 +6076,7 @@ static void *janus_audiobridge_handler(void *data) { /* User ID already taken */ janus_mutex_unlock(&audiobridge->mutex); janus_refcount_decrease(&audiobridge->ref); + janus_mutex_unlock(&sessions_mutex); error_code = JANUS_AUDIOBRIDGE_ERROR_ID_EXISTS; JANUS_LOG(LOG_ERR, "User ID %s already exists\n", user_id_str); g_snprintf(error_cause, 512, "User ID %s already exists", user_id_str); @@ -6156,6 +6179,7 @@ static void *janus_audiobridge_handler(void *data) { } janus_mutex_unlock(&audiobridge->mutex); janus_refcount_decrease(&audiobridge->ref); + janus_mutex_unlock(&sessions_mutex); g_free(participant->display); g_free(participant); JANUS_LOG(LOG_ERR, "Error creating Opus encoder\n"); @@ -6196,6 +6220,7 @@ static void *janus_audiobridge_handler(void *data) { } janus_mutex_unlock(&audiobridge->mutex); janus_refcount_decrease(&audiobridge->ref); + janus_mutex_unlock(&sessions_mutex); g_free(participant->display); if(participant->encoder) opus_encoder_destroy(participant->encoder); @@ -6346,6 +6371,7 @@ static void *janus_audiobridge_handler(void *data) { g_hash_table_insert(audiobridge->participants, string_ids ? (gpointer)g_strdup(participant->user_id_str) : (gpointer)janus_uint64_dup(participant->user_id), participant); + janus_mutex_unlock(&sessions_mutex); /* Notify the other participants */ json_t *newuser = json_object(); json_object_set_new(newuser, "audiobridge", json_string("joined")); @@ -6433,6 +6459,7 @@ static void *janus_audiobridge_handler(void *data) { if(generate_offer) session->plugin_offer = generate_offer; } else if(!strcasecmp(request_text, "configure")) { + janus_mutex_unlock(&sessions_mutex); /* Handle this participant */ janus_audiobridge_participant *participant = (janus_audiobridge_participant *)session->participant; if(participant == NULL || participant->room == NULL) { @@ -6648,6 +6675,7 @@ static void *janus_audiobridge_handler(void *data) { session->plugin_offer = generate_offer; } } else if(!strcasecmp(request_text, "changeroom")) { + janus_mutex_unlock(&sessions_mutex); /* The participant wants to leave the current room and join another one without reconnecting (e.g., a sidebar) */ JANUS_VALIDATE_JSON_OBJECT(root, join_parameters, error_code, error_cause, TRUE, @@ -7087,11 +7115,13 @@ static void *janus_audiobridge_handler(void *data) { janus_mutex_unlock(&audiobridge->mutex); janus_mutex_unlock(&rooms_mutex); } else if(!strcasecmp(request_text, "hangup")) { + janus_mutex_unlock(&sessions_mutex); /* Get rid of an ongoing session */ gateway->close_pc(session->handle); event = json_object(); json_object_set_new(event, "audiobridge", json_string("hangingup")); } else if(!strcasecmp(request_text, "leave")) { + janus_mutex_unlock(&sessions_mutex); /* This participant is leaving */ janus_audiobridge_participant *participant = (janus_audiobridge_participant *)session->participant; if(participant == NULL || participant->room == NULL) { @@ -7182,6 +7212,7 @@ static void *janus_audiobridge_handler(void *data) { janus_refcount_decrease(&audiobridge->ref); } } else { + janus_mutex_unlock(&sessions_mutex); JANUS_LOG(LOG_ERR, "Unknown request '%s'\n", request_text); error_code = JANUS_AUDIOBRIDGE_ERROR_INVALID_REQUEST; g_snprintf(error_cause, 512, "Unknown request '%s'", request_text);