Skip to content

Commit c917ee6

Browse files
authored
Fix references counting for remote publisher and streams (see #3359) (#3475)
1 parent 2357864 commit c917ee6

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

src/plugins/janus_videoroom.c

+26-19
Original file line numberDiff line numberDiff line change
@@ -2799,7 +2799,10 @@ static void janus_videoroom_session_destroy(janus_videoroom_session *session) {
27992799
static void janus_videoroom_session_free(const janus_refcount *session_ref) {
28002800
janus_videoroom_session *session = janus_refcount_containerof(session_ref, janus_videoroom_session, ref);
28012801
/* Remove the reference to the core plugin session */
2802-
janus_refcount_decrease(&session->handle->ref);
2802+
if(session->handle) {
2803+
/* Could be NULL for dummy publishers */
2804+
janus_refcount_decrease(&session->handle->ref);
2805+
}
28032806
/* This session can be destroyed, free all the resources */
28042807
janus_mutex_destroy(&session->mutex);
28052808
g_free(session);
@@ -7290,6 +7293,7 @@ static json_t *janus_videoroom_process_synchronous_request(janus_videoroom_sessi
72907293
json_object_set_new(response, "id", string_ids ? json_string(publisher->user_id_str) : json_integer(publisher->user_id));
72917294
json_object_set_new(response, "remote_id", json_string(remote_id));
72927295
janus_refcount_decrease(&publisher->ref); /* This is just to handle the request for now */
7296+
janus_refcount_decrease(&videoroom->ref);
72937297
goto prepare_response;
72947298
} else if(!strcasecmp(request_text, "unpublish_remotely")) {
72957299
/* Configure a local publisher to stop restreaming to a remote VideoRomm instance */
@@ -7833,8 +7837,6 @@ static json_t *janus_videoroom_process_synchronous_request(janus_videoroom_sessi
78337837
mindex++;
78347838
}
78357839
/* Done, spawn a thread for this remote publisher */
7836-
janus_refcount_increase(&publisher->ref);
7837-
janus_refcount_increase(&publisher->session->ref);
78387840
GError *error = NULL;
78397841
char tname[16];
78407842
g_snprintf(tname, sizeof(tname), "vremote %s", publisher->user_id_str);
@@ -7843,9 +7845,15 @@ static json_t *janus_videoroom_process_synchronous_request(janus_videoroom_sessi
78437845
/* Something went wrong */
78447846
janus_mutex_unlock(&videoroom->mutex);
78457847
janus_refcount_decrease(&videoroom->ref);
7846-
janus_videoroom_publisher_destroy(publisher);
7847-
janus_refcount_decrease(&publisher->ref);
7848+
janus_mutex_lock(&publisher->streams_mutex);
7849+
g_list_free_full(publisher->streams, (GDestroyNotify)(janus_videoroom_publisher_stream_unref));
7850+
publisher->streams = NULL;
7851+
g_hash_table_remove_all(publisher->streams_byid);
7852+
g_hash_table_remove_all(publisher->streams_bymid);
7853+
janus_mutex_unlock(&publisher->streams_mutex);
7854+
janus_videoroom_leave_or_unpublish(publisher, TRUE, FALSE);
78487855
janus_refcount_decrease(&publisher->session->ref);
7856+
janus_videoroom_publisher_destroy(publisher);
78497857
JANUS_LOG(LOG_ERR, "Could not spawn thread for remote publisher, %d (%s)\n",
78507858
errno, g_strerror(errno));
78517859
error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;
@@ -13491,6 +13499,11 @@ static void *janus_videoroom_remote_publisher_thread(void *user_data) {
1349113499
JANUS_LOG(LOG_VERB, "[%s/%s] Joining remote publisher thread...\n",
1349213500
publisher->room->room_id_str, publisher->user_id_str);
1349313501

13502+
janus_videoroom *videoroom = publisher->room;
13503+
janus_refcount_increase(&videoroom->ref);
13504+
janus_refcount_increase(&publisher->ref);
13505+
janus_refcount_increase(&publisher->session->ref);
13506+
1349413507
/* File descriptors */
1349513508
socklen_t addrlen;
1349613509
struct sockaddr_storage remote = { 0 };
@@ -13504,12 +13517,10 @@ static void *janus_videoroom_remote_publisher_thread(void *user_data) {
1350413517
* and/or we may never be notified about sessions being closed, so give up */
1350513518
JANUS_LOG(LOG_WARN, "[%s/%s] Leaving remote publisher thread, no pipe file descriptor...\n",
1350613519
publisher->room->room_id_str, publisher->user_id_str);
13507-
janus_videoroom_publisher_destroy(publisher);
13508-
janus_refcount_decrease(&publisher->session->ref);
13509-
janus_refcount_decrease(&publisher->ref);
13510-
g_thread_unref(g_thread_self());
13511-
return NULL;
13520+
janus_videoroom_publisher_dereference(publisher);
13521+
goto cleanup;
1351213522
}
13523+
1351313524
/* RTP stuff */
1351413525
janus_rtp_header *rtp = NULL;
1351513526
uint32_t ssrc = 0, diff = 0;
@@ -13520,10 +13531,6 @@ static void *janus_videoroom_remote_publisher_thread(void *user_data) {
1352013531
GList *temp = NULL;
1352113532

1352213533
/* As the first thing, we add the remote publisher to the list */
13523-
janus_refcount_increase(&publisher->ref);
13524-
janus_refcount_increase(&publisher->session->ref);
13525-
janus_videoroom *videoroom = publisher->room;
13526-
janus_refcount_increase(&videoroom->ref);
1352713534
janus_mutex_lock(&videoroom->mutex);
1352813535
g_hash_table_insert(videoroom->participants,
1352913536
string_ids ? (gpointer)g_strdup(publisher->user_id_str) : (gpointer)janus_uint64_dup(publisher->user_id),
@@ -13714,6 +13721,7 @@ static void *janus_videoroom_remote_publisher_thread(void *user_data) {
1371413721
}
1371513722
}
1371613723
}
13724+
cleanup:
1371713725
/* If we got here, the remote publisher has been removed from the
1371813726
* room: let's notify all other publishers in the room */
1371913727
janus_mutex_lock(&publisher->rec_mutex);
@@ -13810,21 +13818,20 @@ static void *janus_videoroom_remote_publisher_thread(void *user_data) {
1381013818
temp = temp->next;
1381113819
}
1381213820
}
13821+
JANUS_LOG(LOG_VERB, "[%s/%s] Leaving remote publisher thread...\n",
13822+
videoroom->room_id_str, publisher->user_id_str);
1381313823
g_list_free(subscribers);
1381413824
/* Free streams */
13815-
g_list_free(publisher->streams);
13825+
g_list_free_full(publisher->streams, (GDestroyNotify)(janus_videoroom_publisher_stream_unref));
1381613826
publisher->streams = NULL;
1381713827
g_hash_table_remove_all(publisher->streams_byid);
1381813828
g_hash_table_remove_all(publisher->streams_bymid);
1381913829
janus_mutex_unlock(&publisher->streams_mutex);
1382013830
janus_videoroom_leave_or_unpublish(publisher, TRUE, FALSE);
13831+
janus_refcount_decrease(&publisher->session->ref);
1382113832
janus_videoroom_publisher_destroy(publisher);
1382213833
/* Done */
13823-
JANUS_LOG(LOG_VERB, "[%s/%s] Leaving remote publisher thread...\n",
13824-
videoroom->room_id_str, publisher->user_id_str);
1382513834
janus_refcount_decrease(&videoroom->ref);
13826-
janus_refcount_decrease(&publisher->session->ref);
13827-
janus_refcount_decrease(&publisher->ref);
1382813835
g_thread_unref(g_thread_self());
1382913836
return NULL;
1383013837
}

0 commit comments

Comments
 (0)