@@ -2799,7 +2799,10 @@ static void janus_videoroom_session_destroy(janus_videoroom_session *session) {
2799
2799
static void janus_videoroom_session_free(const janus_refcount *session_ref) {
2800
2800
janus_videoroom_session *session = janus_refcount_containerof(session_ref, janus_videoroom_session, ref);
2801
2801
/* 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
+ }
2803
2806
/* This session can be destroyed, free all the resources */
2804
2807
janus_mutex_destroy(&session->mutex);
2805
2808
g_free(session);
@@ -7290,6 +7293,7 @@ static json_t *janus_videoroom_process_synchronous_request(janus_videoroom_sessi
7290
7293
json_object_set_new(response, "id", string_ids ? json_string(publisher->user_id_str) : json_integer(publisher->user_id));
7291
7294
json_object_set_new(response, "remote_id", json_string(remote_id));
7292
7295
janus_refcount_decrease(&publisher->ref); /* This is just to handle the request for now */
7296
+ janus_refcount_decrease(&videoroom->ref);
7293
7297
goto prepare_response;
7294
7298
} else if(!strcasecmp(request_text, "unpublish_remotely")) {
7295
7299
/* 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
7833
7837
mindex++;
7834
7838
}
7835
7839
/* Done, spawn a thread for this remote publisher */
7836
- janus_refcount_increase(&publisher->ref);
7837
- janus_refcount_increase(&publisher->session->ref);
7838
7840
GError *error = NULL;
7839
7841
char tname[16];
7840
7842
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
7843
7845
/* Something went wrong */
7844
7846
janus_mutex_unlock(&videoroom->mutex);
7845
7847
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);
7848
7855
janus_refcount_decrease(&publisher->session->ref);
7856
+ janus_videoroom_publisher_destroy(publisher);
7849
7857
JANUS_LOG(LOG_ERR, "Could not spawn thread for remote publisher, %d (%s)\n",
7850
7858
errno, g_strerror(errno));
7851
7859
error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;
@@ -13491,6 +13499,11 @@ static void *janus_videoroom_remote_publisher_thread(void *user_data) {
13491
13499
JANUS_LOG(LOG_VERB, "[%s/%s] Joining remote publisher thread...\n",
13492
13500
publisher->room->room_id_str, publisher->user_id_str);
13493
13501
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
+
13494
13507
/* File descriptors */
13495
13508
socklen_t addrlen;
13496
13509
struct sockaddr_storage remote = { 0 };
@@ -13504,12 +13517,10 @@ static void *janus_videoroom_remote_publisher_thread(void *user_data) {
13504
13517
* and/or we may never be notified about sessions being closed, so give up */
13505
13518
JANUS_LOG(LOG_WARN, "[%s/%s] Leaving remote publisher thread, no pipe file descriptor...\n",
13506
13519
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;
13512
13522
}
13523
+
13513
13524
/* RTP stuff */
13514
13525
janus_rtp_header *rtp = NULL;
13515
13526
uint32_t ssrc = 0, diff = 0;
@@ -13520,10 +13531,6 @@ static void *janus_videoroom_remote_publisher_thread(void *user_data) {
13520
13531
GList *temp = NULL;
13521
13532
13522
13533
/* 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);
13527
13534
janus_mutex_lock(&videoroom->mutex);
13528
13535
g_hash_table_insert(videoroom->participants,
13529
13536
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) {
13714
13721
}
13715
13722
}
13716
13723
}
13724
+ cleanup:
13717
13725
/* If we got here, the remote publisher has been removed from the
13718
13726
* room: let's notify all other publishers in the room */
13719
13727
janus_mutex_lock(&publisher->rec_mutex);
@@ -13810,21 +13818,20 @@ static void *janus_videoroom_remote_publisher_thread(void *user_data) {
13810
13818
temp = temp->next;
13811
13819
}
13812
13820
}
13821
+ JANUS_LOG(LOG_VERB, "[%s/%s] Leaving remote publisher thread...\n",
13822
+ videoroom->room_id_str, publisher->user_id_str);
13813
13823
g_list_free(subscribers);
13814
13824
/* Free streams */
13815
- g_list_free (publisher->streams);
13825
+ g_list_free_full (publisher->streams, (GDestroyNotify)(janus_videoroom_publisher_stream_unref) );
13816
13826
publisher->streams = NULL;
13817
13827
g_hash_table_remove_all(publisher->streams_byid);
13818
13828
g_hash_table_remove_all(publisher->streams_bymid);
13819
13829
janus_mutex_unlock(&publisher->streams_mutex);
13820
13830
janus_videoroom_leave_or_unpublish(publisher, TRUE, FALSE);
13831
+ janus_refcount_decrease(&publisher->session->ref);
13821
13832
janus_videoroom_publisher_destroy(publisher);
13822
13833
/* Done */
13823
- JANUS_LOG(LOG_VERB, "[%s/%s] Leaving remote publisher thread...\n",
13824
- videoroom->room_id_str, publisher->user_id_str);
13825
13834
janus_refcount_decrease(&videoroom->ref);
13826
- janus_refcount_decrease(&publisher->session->ref);
13827
- janus_refcount_decrease(&publisher->ref);
13828
13835
g_thread_unref(g_thread_self());
13829
13836
return NULL;
13830
13837
}
0 commit comments