From 6e544e0b4cda0e9fd896c3aebd398f7065f9c1a8 Mon Sep 17 00:00:00 2001 From: adnanel Date: Thu, 13 Jul 2023 15:08:38 +0200 Subject: [PATCH] leave all joined rooms when destroying session --- src/plugins/janus_textroom.c | 69 ++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/plugins/janus_textroom.c b/src/plugins/janus_textroom.c index 4d9d694559..f0fadccdd7 100644 --- a/src/plugins/janus_textroom.c +++ b/src/plugins/janus_textroom.c @@ -1157,6 +1157,75 @@ void janus_textroom_destroy_session(janus_plugin_session *handle, int *error) { *error = -2; return; } + janus_mutex_lock(&session->mutex); + if(session->rooms) { + GHashTableIter iter; + gpointer value; + janus_mutex_lock(&rooms_mutex); + g_hash_table_iter_init(&iter, session->rooms); + while(g_hash_table_iter_next(&iter, NULL, &value)) { + janus_textroom_participant *participant = value; + janus_mutex_lock(&participant->mutex); + janus_refcount_increase(&participant->ref); + janus_textroom_room *textroom = participant->room; + if(textroom) { + janus_mutex_lock(&textroom->mutex); + janus_refcount_increase(&textroom->ref); + int *room_id_str = textroom->room_id_str; + int room_id = textroom->room_id; + g_hash_table_remove(session->rooms, string_ids ? (gpointer)room_id_str : (gpointer)&room_id); + g_hash_table_remove(textroom->participants, participant->username); + participant->session = NULL; + participant->room = NULL; + + /* Notify all participants */ + JANUS_LOG(LOG_VERB, "Notifying all participants about the new leave\n"); + if(textroom->participants) { + /* Prepare event */ + json_t *event = json_object(); + json_object_set_new(event, "textroom", json_string("leave")); + json_object_set_new(event, "room", string_ids ? json_string(textroom->room_id_str) : json_integer(textroom->room_id)); + json_object_set_new(event, "username", json_string(participant->username)); + char *event_text = json_dumps(event, json_format); + json_decref(event); + if(event_text != NULL) { + janus_plugin_data data = { .label = NULL, .protocol = NULL, .binary = FALSE, .buffer = event_text, .length = strlen(event_text) }; + gateway->relay_data(handle, &data); + /* Broadcast */ + GHashTableIter iter; + gpointer value; + g_hash_table_iter_init(&iter, textroom->participants); + while(g_hash_table_iter_next(&iter, NULL, &value)) { + janus_textroom_participant *top = value; + if(top == participant) + continue; /* Skip us */ + janus_refcount_increase(&top->ref); + JANUS_LOG(LOG_VERB, " >> To %s in %s\n", top->username, room_id_str); + gateway->relay_data(top->session->handle, &data); + janus_refcount_decrease(&top->ref); + } + free(event_text); + } + } + /* Also notify event handlers */ + if(notify_events && gateway->events_is_enabled()) { + json_t *info = json_object(); + json_object_set_new(info, "event", json_string("leave")); + json_object_set_new(info, "room", string_ids ? json_string(room_id_str) : json_integer(room_id)); + json_object_set_new(info, "username", json_string(participant->username)); + gateway->notify_event(&janus_textroom_plugin, session->handle, info); + } + janus_mutex_unlock(&textroom->mutex); + janus_refcount_decrease(&textroom->ref); + janus_textroom_participant_destroy(participant); + } + janus_refcount_decrease(&participant->ref); + janus_mutex_unlock(&participant->mutex); + } + janus_mutex_unlock(&rooms_mutex); + } + janus_mutex_unlock(&session->mutex); + JANUS_LOG(LOG_VERB, "Removing TextRoom session...\n"); janus_textroom_hangup_media_internal(handle); g_hash_table_remove(sessions, handle);