Skip to content

Commit dadc2ab

Browse files
authored
videoroom: always take room mutex before streams mutex (see #3463) (#3474)
1 parent 51fe38a commit dadc2ab

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/plugins/janus_videoroom.c

+25-10
Original file line numberDiff line numberDiff line change
@@ -4318,9 +4318,7 @@ static void janus_videoroom_notify_about_publisher(janus_videoroom_publisher *p,
43184318
janus_videoroom *room = p->room;
43194319
if(room && !g_atomic_int_get(&room->destroyed)) {
43204320
janus_refcount_increase(&room->ref);
4321-
janus_mutex_lock(&room->mutex);
43224321
janus_videoroom_notify_participants(p, pub, FALSE);
4323-
janus_mutex_unlock(&room->mutex);
43244322
janus_refcount_decrease(&room->ref);
43254323
}
43264324
json_decref(pub);
@@ -7975,7 +7973,6 @@ static json_t *janus_videoroom_process_synchronous_request(janus_videoroom_sessi
79757973
publisher->display = new_display;
79767974
g_free(old_display);
79777975
}
7978-
janus_mutex_unlock(&videoroom->mutex);
79797976
janus_mutex_lock(&publisher->streams_mutex);
79807977
janus_videoroom_publisher_stream *ps = NULL;
79817978
int changes = FALSE;
@@ -8115,6 +8112,7 @@ static json_t *janus_videoroom_process_synchronous_request(janus_videoroom_sessi
81158112
janus_videoroom_notify_about_publisher(publisher, TRUE);
81168113
}
81178114
janus_mutex_unlock(&publisher->streams_mutex);
8115+
janus_mutex_unlock(&videoroom->mutex);
81188116
/* Done */
81198117
janus_refcount_decrease(&publisher->ref);
81208118
janus_refcount_decrease(&videoroom->ref);
@@ -8380,9 +8378,16 @@ void janus_videoroom_setup_media(janus_plugin_session *handle) {
83808378
if(session->participant_type == janus_videoroom_p_type_publisher) {
83818379
janus_videoroom_publisher *participant = janus_videoroom_session_get_publisher(session);
83828380
/* Notify all other participants that there's a new boy in town */
8381+
janus_videoroom *room = participant->room;
8382+
if(room && !g_atomic_int_get(&room->destroyed)) {
8383+
janus_refcount_increase(&room->ref);
8384+
janus_mutex_lock(&room->mutex);
8385+
}
83838386
janus_mutex_lock(&participant->rec_mutex);
83848387
janus_mutex_lock(&participant->streams_mutex);
8385-
janus_videoroom_notify_about_publisher(participant, FALSE);
8388+
if(room) {
8389+
janus_videoroom_notify_about_publisher(participant, FALSE);
8390+
}
83868391
/* Check if we need to start recording */
83878392
if((participant->room && participant->room->record) || participant->recording_active) {
83888393
GList *temp = participant->streams;
@@ -8395,6 +8400,10 @@ void janus_videoroom_setup_media(janus_plugin_session *handle) {
83958400
}
83968401
janus_mutex_unlock(&participant->streams_mutex);
83978402
janus_mutex_unlock(&participant->rec_mutex);
8403+
if(room) {
8404+
janus_mutex_unlock(&room->mutex);
8405+
janus_refcount_decrease(&room->ref);
8406+
}
83988407
janus_refcount_decrease(&participant->ref);
83998408
} else if(session->participant_type == janus_videoroom_p_type_subscriber) {
84008409
janus_videoroom_subscriber *s = janus_videoroom_session_get_subscriber(session);
@@ -10811,6 +10820,7 @@ static void *janus_videoroom_handler(void *data) {
1081110820
* a renegotiation is involved, descriptions are updated later */
1081210821
gboolean desc_updated = FALSE;
1081310822
size_t i = 0;
10823+
janus_mutex_lock(&participant->room->mutex);
1081410824
janus_mutex_lock(&participant->streams_mutex);
1081510825
for(i=0; i<json_array_size(descriptions); i++) {
1081610826
json_t *d = json_array_get(descriptions, i);
@@ -10829,6 +10839,7 @@ static void *janus_videoroom_handler(void *data) {
1082910839
if(desc_updated)
1083010840
janus_videoroom_notify_about_publisher(participant, TRUE);
1083110841
janus_mutex_unlock(&participant->streams_mutex);
10842+
janus_mutex_unlock(&participant->room->mutex);
1083210843
}
1083310844
/* Done */
1083410845
event = json_object();
@@ -11138,6 +11149,7 @@ static void *janus_videoroom_handler(void *data) {
1113811149
* handle the unsubscribe first, and the subscribe only after that */
1113911150
int changes = 0;
1114011151
size_t i = 0;
11152+
janus_mutex_lock(&subscriber->room->mutex);
1114111153
janus_mutex_lock(&subscriber->streams_mutex);
1114211154
if(unsubscribe) {
1114311155
/* Remove the specified subscriptions */
@@ -11165,10 +11177,8 @@ static void *janus_videoroom_handler(void *data) {
1116511177
janus_videoroom_subscriber_stream_remove(stream, NULL, TRUE);
1116611178
changes++;
1116711179
} else if(feed_id_str != NULL) {
11168-
janus_mutex_lock(&subscriber->room->mutex);
1116911180
janus_videoroom_publisher *publisher = g_hash_table_lookup(subscriber->room->participants,
1117011181
string_ids ? (gpointer)feed_id_str : (gpointer)&feed_id);
11171-
janus_mutex_unlock(&subscriber->room->mutex);
1117211182
if(publisher == NULL || g_atomic_int_get(&publisher->destroyed) ||
1117311183
!g_atomic_int_get(&publisher->session->started)) {
1117411184
JANUS_LOG(LOG_WARN, "Publisher '%s' not found, not unsubscribing...\n", feed_id_str);
@@ -11219,10 +11229,8 @@ static void *janus_videoroom_handler(void *data) {
1121911229
} else {
1122011230
feed_id_str = (char *)json_string_value(feed);
1122111231
}
11222-
janus_mutex_lock(&subscriber->room->mutex);
1122311232
janus_videoroom_publisher *publisher = g_hash_table_lookup(subscriber->room->participants,
1122411233
string_ids ? (gpointer)feed_id_str : (gpointer)&feed_id);
11225-
janus_mutex_unlock(&subscriber->room->mutex);
1122611234
if(publisher == NULL || g_atomic_int_get(&publisher->destroyed) ||
1122711235
!g_atomic_int_get(&publisher->session->started)) {
1122811236
JANUS_LOG(LOG_WARN, "Publisher '%s' not found, not subscribing...\n", feed_id_str);
@@ -11379,6 +11387,7 @@ static void *janus_videoroom_handler(void *data) {
1137911387
changes++;
1138011388
if(changes == 0) {
1138111389
janus_mutex_unlock(&subscriber->streams_mutex);
11390+
janus_mutex_unlock(&subscriber->room->mutex);
1138211391
/* Nothing changed, just ack and don't do anything else */
1138311392
JANUS_LOG(LOG_VERB, "No change made, skipping renegotiation\n");
1138411393
event = json_object();
@@ -11406,6 +11415,7 @@ static void *janus_videoroom_handler(void *data) {
1140611415
/* We're still waiting for an answer to a previous offer, postpone this */
1140711416
g_atomic_int_set(&subscriber->pending_offer, 1);
1140811417
janus_mutex_unlock(&subscriber->streams_mutex);
11418+
janus_mutex_unlock(&subscriber->room->mutex);
1140911419
JANUS_LOG(LOG_VERB, "Post-poning new offer, waiting for previous answer\n");
1141011420
/* Send a temporary event */
1141111421
event = json_object();
@@ -11437,6 +11447,7 @@ static void *janus_videoroom_handler(void *data) {
1143711447
/* Generate a new offer */
1143811448
json_t *jsep = janus_videoroom_subscriber_offer(subscriber);
1143911449
janus_mutex_unlock(&subscriber->streams_mutex);
11450+
janus_mutex_unlock(&subscriber->room->mutex);
1144011451
/* How long will the Janus core take to push the event? */
1144111452
gint64 start = janus_get_monotonic_time();
1144211453
int res = gateway->push_event(msg->handle, &janus_videoroom_plugin, msg->transaction, event, jsep);
@@ -12023,6 +12034,7 @@ static void *janus_videoroom_handler(void *data) {
1202312034
* notice that no renegotiation happens, we just switch the sources */
1202412035
int changes = 0;
1202512036
gboolean update = FALSE;
12037+
janus_mutex_lock(&subscriber->room->mutex);
1202612038
janus_mutex_lock(&subscriber->streams_mutex);
1202712039
for(i=0; i<json_array_size(feeds); i++) {
1202812040
json_t *s = json_array_get(feeds, i);
@@ -12045,10 +12057,8 @@ static void *janus_videoroom_handler(void *data) {
1204512057
feed_id_str = (char *)json_string_value(feed);
1204612058
}
1204712059
const char *mid = json_string_value(json_object_get(s, "mid"));
12048-
janus_mutex_lock(&subscriber->room->mutex);
1204912060
janus_videoroom_publisher *publisher = g_hash_table_lookup(subscriber->room->participants,
1205012061
string_ids ? (gpointer)feed_id_str : (gpointer)&feed_id);
12051-
janus_mutex_unlock(&subscriber->room->mutex);
1205212062
if(publisher == NULL || g_atomic_int_get(&publisher->destroyed) ||
1205312063
!g_atomic_int_get(&publisher->session->started)) {
1205412064
JANUS_LOG(LOG_WARN, "Publisher '%s' not found, not switching...\n", feed_id_str);
@@ -12197,6 +12207,7 @@ static void *janus_videoroom_handler(void *data) {
1219712207
janus_refcount_decrease(&stream->ref);
1219812208
}
1219912209
janus_mutex_unlock(&subscriber->streams_mutex);
12210+
janus_mutex_unlock(&subscriber->room->mutex);
1220012211
/* Decrease the references we took before */
1220112212
while(publishers) {
1220212213
janus_videoroom_publisher *publisher = (janus_videoroom_publisher *)publishers->data;
@@ -12952,9 +12963,11 @@ static void *janus_videoroom_handler(void *data) {
1295212963
/* If this is an update/renegotiation, notify participants about this */
1295312964
if(sdp_update && g_atomic_int_get(&session->started)) {
1295412965
/* Notify all other participants this publisher's media has changed */
12966+
janus_mutex_lock(&videoroom->mutex);
1295512967
janus_mutex_lock(&participant->streams_mutex);
1295612968
janus_videoroom_notify_about_publisher(participant, TRUE);
1295712969
janus_mutex_unlock(&participant->streams_mutex);
12970+
janus_mutex_unlock(&videoroom->mutex);
1295812971
}
1295912972
/* Done */
1296012973
if(res != JANUS_OK) {
@@ -13511,13 +13524,15 @@ static void *janus_videoroom_remote_publisher_thread(void *user_data) {
1351113524
janus_refcount_increase(&publisher->session->ref);
1351213525
janus_videoroom *videoroom = publisher->room;
1351313526
janus_refcount_increase(&videoroom->ref);
13527+
janus_mutex_lock(&videoroom->mutex);
1351413528
g_hash_table_insert(videoroom->participants,
1351513529
string_ids ? (gpointer)g_strdup(publisher->user_id_str) : (gpointer)janus_uint64_dup(publisher->user_id),
1351613530
publisher);
1351713531
/* Let's also notify all other participants that the publisher is here */
1351813532
janus_mutex_lock(&publisher->streams_mutex);
1351913533
janus_videoroom_notify_about_publisher(publisher, FALSE);
1352013534
janus_mutex_unlock(&publisher->streams_mutex);
13535+
janus_mutex_unlock(&videoroom->mutex);
1352113536

1352213537
/* Loop */
1352313538
int num = 0, i = 0;

0 commit comments

Comments
 (0)