Skip to content

Commit

Permalink
Extend sessions_mutex range for audiobridge message handler
Browse files Browse the repository at this point in the history
  • Loading branch information
atoppi committed Sep 7, 2023
1 parent f936e77 commit 359706e
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions src/plugins/janus_audiobridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -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??");
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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)");
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -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 */
Expand All @@ -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;
}
}
Expand All @@ -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;
Expand All @@ -5970,13 +5987,15 @@ 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"));
group = GPOINTER_TO_UINT(g_hash_table_lookup(audiobridge->groups, group_name));
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);
Expand Down Expand Up @@ -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)");
Expand All @@ -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)");
Expand All @@ -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)");
Expand All @@ -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);
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 359706e

Please sign in to comment.