[Pkg-voip-commits] [janus] 18/282: Use mqueue to make sure libre calls are done on the loop thread

Jonas Smedegaard dr at jones.dk
Wed Dec 20 21:53:24 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag debian/0.2.6-1
in repository janus.

commit 68a7a91073c0746a27287ed0fbb227ba89f9fc92
Author: Lorenzo Miniero <lminiero at gmail.com>
Date:   Sat Mar 25 15:28:31 2017 +0100

    Use mqueue to make sure libre calls are done on the loop thread
---
 plugins/janus_sipre.c | 65 +++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 53 insertions(+), 12 deletions(-)

diff --git a/plugins/janus_sipre.c b/plugins/janus_sipre.c
index 1125748..1d1d2f5 100644
--- a/plugins/janus_sipre.c
+++ b/plugins/janus_sipre.c
@@ -49,6 +49,7 @@
 #include <re_sa.h>
 #include <re_main.h>
 #include <re_mem.h>
+#include <re_mqueue.h>
 #include <re_sdp.h>
 #include <re_uri.h>
 #include <re_sip.h>
@@ -214,6 +215,15 @@ static struct sip *sipstack;
 static struct tls *tls = NULL;
 GThread *sipstack_thread = NULL;
 
+/* Message queue */
+typedef enum janus_sipre_mqueue_event {
+	janus_sipre_mqueue_event_do_register,
+	/* TODO Add other events here */
+	janus_sipre_mqueue_event_do_exit
+} janus_sipre_mqueue_event;
+static struct mqueue *mq = NULL;
+void janus_sipre_mqueue_handler(int id, void *data, void *arg);
+
 /* Registration info */
 typedef enum {
 	janus_sipre_registration_status_disabled = -2,
@@ -776,6 +786,12 @@ int janus_sipre_init(janus_callbacks *callback, const char *config_path) {
 		return -1;
 	}
 	mem_deref(tls);
+	err = mqueue_alloc(&mq, janus_sipre_mqueue_handler, NULL);
+	if(err) {
+		mem_deref(sipstack);
+		JANUS_LOG(LOG_ERR, "Failed to initialize message queue: %d (%s)\n", err, strerror(err));
+		return -1;
+	}
 
 	sessions = g_hash_table_new(NULL, NULL);
 	callids = g_hash_table_new(g_str_hash, g_str_equal);
@@ -823,8 +839,7 @@ void janus_sipre_destroy(void) {
 		g_thread_join(handler_thread);
 		handler_thread = NULL;
 	}
-	re_cancel();
-	JANUS_LOG(LOG_ERR, "re_cancel() called\n");
+
 	if(sipstack_thread != NULL) {
 		g_thread_join(sipstack_thread);
 		sipstack_thread = NULL;
@@ -1487,16 +1502,11 @@ static void *janus_sipre_handler(void *data) {
 				char ttl_text[20];
 				g_snprintf(ttl_text, sizeof(ttl_text), "%d", ttl);
 					/* TODO Any way to specify TTL in sipreg_register? */
-				int err = sipreg_register(&session->stack.reg, sipstack,
-					session->account.proxy,
-					session->account.identity, session->account.identity, 3600,
-					session->account.display_name ? session->account.display_name : session->account.username, NULL, 0, 0,
-					janus_sipre_cb_auth, session, FALSE,
-					janus_sipre_cb_register, session, NULL, NULL);
-				if(err < 0) {
-					/* TODO Handle accordingly */
-					JANUS_LOG(LOG_ERR, "Error attempting to REGISTER...\n");
-				}
+				/* We enqueue this REGISTER attempt, to be sure it's done in the re_main loop thread
+				 * FIXME Maybe passing a key to the session is better than passing the session object
+				 * itself? it may be gone when it gets handled... won't be an issue with the
+				 * reference counter branch but needs to be taken into account until then */
+				mqueue_push(mq, janus_sipre_mqueue_event_do_register, session);
 				result = json_object();
 				json_object_set_new(result, "event", json_string("registering"));
 			} else {
@@ -2891,3 +2901,34 @@ void janus_sipre_cb_exit(void *arg) {
 	/* Stop libre main loop */
 	re_cancel();
 }
+
+/* Callback to implement SIP requests in the re_main loop thread */
+void janus_sipre_mqueue_handler(int id, void *data, void *arg) {
+	janus_sipre_mqueue_event event = (janus_sipre_mqueue_event)id;
+	janus_sipre_session *session = (janus_sipre_session *)data;
+	JANUS_LOG(LOG_INFO, "[SIPre-%s] event %d\n", session->account.username, id);
+
+	int err = 0;
+
+	switch(event) {
+		case janus_sipre_mqueue_event_do_register:
+			err = sipreg_register(&session->stack.reg, sipstack,
+				session->account.proxy,
+				session->account.identity, session->account.identity, 3600,
+				session->account.display_name ? session->account.display_name : session->account.username, NULL, 0, 0,
+				janus_sipre_cb_auth, session, FALSE,
+				janus_sipre_cb_register, session, NULL, NULL);
+			if(err < 0) {
+				/* TODO Send event and handle accordingly */
+				JANUS_LOG(LOG_ERR, "Error attempting to REGISTER...\n");
+			}
+			break;
+		case janus_sipre_mqueue_event_do_exit:
+			/* We're done, here, break the loop */
+			re_cancel();
+			break;
+		default:
+			/* Shouldn't happen */
+			break;
+	}
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-voip/janus.git



More information about the Pkg-voip-commits mailing list