Browse Source

linux-pipewire: Shuffle some code around

This is the start of what will be a rather incisive surgery on
pipewire.c. Move a couple of functions to portal.c, since they're
not really related to the PipeWire code.
Georges Basile Stavracas Neto 3 years ago
parent
commit
5ffc8ef663
3 changed files with 86 additions and 64 deletions
  1. 5 64
      plugins/linux-pipewire/pipewire.c
  2. 78 0
      plugins/linux-pipewire/portal.c
  3. 3 0
      plugins/linux-pipewire/portal.h

+ 5 - 64
plugins/linux-pipewire/pipewire.c

@@ -42,9 +42,6 @@
 #define SPA_POD_PROP_FLAG_DONT_FIXATE (1 << 4)
 #endif
 
-#define REQUEST_PATH "/org/freedesktop/portal/desktop/request/%s/obs%u"
-#define SESSION_PATH "/org/freedesktop/portal/desktop/session/%s/obs%u"
-
 #define CURSOR_META_SIZE(width, height)                                    \
 	(sizeof(struct spa_meta_cursor) + sizeof(struct spa_meta_bitmap) + \
 	 width * height * 4)
@@ -64,7 +61,6 @@ struct format_info {
 struct _obs_pipewire_data {
 	GCancellable *cancellable;
 
-	char *sender_name;
 	char *session_handle;
 	char *restore_token;
 
@@ -165,52 +161,6 @@ static const char *capture_type_to_string(enum portal_capture_type capture_type)
 	return "unknown";
 }
 
-static void new_request_path(obs_pipewire_data *data, char **out_path,
-			     char **out_token)
-{
-	static uint32_t request_token_count = 0;
-
-	request_token_count++;
-
-	if (out_token) {
-		struct dstr str;
-		dstr_init(&str);
-		dstr_printf(&str, "obs%u", request_token_count);
-		*out_token = str.array;
-	}
-
-	if (out_path) {
-		struct dstr str;
-		dstr_init(&str);
-		dstr_printf(&str, REQUEST_PATH, data->sender_name,
-			    request_token_count);
-		*out_path = str.array;
-	}
-}
-
-static void new_session_path(obs_pipewire_data *data, char **out_path,
-			     char **out_token)
-{
-	static uint32_t session_token_count = 0;
-
-	session_token_count++;
-
-	if (out_token) {
-		struct dstr str;
-		dstr_init(&str);
-		dstr_printf(&str, "obs%u", session_token_count);
-		*out_token = str.array;
-	}
-
-	if (out_path) {
-		struct dstr str;
-		dstr_init(&str);
-		dstr_printf(&str, SESSION_PATH, data->sender_name,
-			    session_token_count);
-		*out_path = str.array;
-	}
-}
-
 static void on_cancelled_cb(GCancellable *cancellable, void *data)
 {
 	UNUSED_PARAMETER(cancellable);
@@ -298,7 +248,6 @@ static void destroy_session(obs_pipewire_data *obs_pw)
 		g_clear_pointer(&obs_pw->session_handle, g_free);
 	}
 
-	g_clear_pointer(&obs_pw->sender_name, bfree);
 	obs_enter_graphics();
 	g_clear_pointer(&obs_pw->cursor.texture, gs_texture_destroy);
 	g_clear_pointer(&obs_pw->texture, gs_texture_destroy);
@@ -1135,7 +1084,7 @@ static void start(obs_pipewire_data *obs_pw)
 	char *request_token;
 	char *request_path;
 
-	new_request_path(obs_pw, &request_path, &request_token);
+	portal_create_request_path(&request_path, &request_token);
 
 	blog(LOG_INFO, "[pipewire] Asking for %s",
 	     capture_type_to_string(obs_pw->capture_type));
@@ -1216,7 +1165,7 @@ static void select_source(obs_pipewire_data *obs_pw)
 	char *request_token;
 	char *request_path;
 
-	new_request_path(obs_pw, &request_path, &request_token);
+	portal_create_request_path(&request_path, &request_token);
 
 	call = subscribe_to_signal(obs_pw, request_path,
 				   on_select_source_response_received_cb);
@@ -1330,8 +1279,8 @@ static void create_session(obs_pipewire_data *obs_pw)
 	char *request_token;
 	char *request_path;
 
-	new_request_path(obs_pw, &request_path, &request_token);
-	new_session_path(obs_pw, NULL, &session_token);
+	portal_create_request_path(&request_path, &request_token);
+	portal_create_session_path(NULL, &session_token);
 
 	call = subscribe_to_signal(obs_pw, request_path,
 				   on_create_session_response_received_cb);
@@ -1368,15 +1317,7 @@ static gboolean init_obs_pipewire(obs_pipewire_data *obs_pw)
 	if (!proxy)
 		return FALSE;
 
-	obs_pw->sender_name =
-		bstrdup(g_dbus_connection_get_unique_name(connection) + 1);
-
-	/* Replace dots by underscores */
-	while ((aux = strstr(obs_pw->sender_name, ".")) != NULL)
-		*aux = '_';
-
-	blog(LOG_INFO, "PipeWire initialized (sender name: %s)",
-	     obs_pw->sender_name);
+	blog(LOG_INFO, "PipeWire initialized");
 
 	create_session(obs_pw);
 

+ 78 - 0
plugins/linux-pipewire/portal.c

@@ -21,6 +21,11 @@
 #include "portal.h"
 #include "pipewire.h"
 
+#include <util/dstr.h>
+
+#define REQUEST_PATH "/org/freedesktop/portal/desktop/request/%s/obs%u"
+#define SESSION_PATH "/org/freedesktop/portal/desktop/session/%s/obs%u"
+
 static GDBusConnection *connection = NULL;
 static GDBusProxy *proxy = NULL;
 
@@ -54,6 +59,23 @@ static void ensure_proxy(void)
 	}
 }
 
+char *get_sender_name(void)
+{
+	char *sender_name;
+	char *aux;
+
+	ensure_proxy();
+
+	sender_name =
+		bstrdup(g_dbus_connection_get_unique_name(connection) + 1);
+
+	/* Replace dots by underscores */
+	while ((aux = strstr(sender_name, ".")) != NULL)
+		*aux = '_';
+
+	return sender_name;
+}
+
 uint32_t portal_get_available_capture_types(void)
 {
 	g_autoptr(GVariant) cached_source_types = NULL;
@@ -119,3 +141,59 @@ GDBusProxy *portal_get_dbus_proxy(void)
 	ensure_proxy();
 	return proxy;
 }
+
+void portal_create_request_path(char **out_path, char **out_token)
+{
+	static uint32_t request_token_count = 0;
+
+	request_token_count++;
+
+	if (out_token) {
+		struct dstr str;
+		dstr_init(&str);
+		dstr_printf(&str, "obs%u", request_token_count);
+		*out_token = str.array;
+	}
+
+	if (out_path) {
+		char *sender_name;
+		struct dstr str;
+
+		sender_name = get_sender_name();
+
+		dstr_init(&str);
+		dstr_printf(&str, REQUEST_PATH, sender_name,
+			    request_token_count);
+		*out_path = str.array;
+
+		bfree(sender_name);
+	}
+}
+
+void portal_create_session_path(char **out_path, char **out_token)
+{
+	static uint32_t session_token_count = 0;
+
+	session_token_count++;
+
+	if (out_token) {
+		struct dstr str;
+		dstr_init(&str);
+		dstr_printf(&str, "obs%u", session_token_count);
+		*out_token = str.array;
+	}
+
+	if (out_path) {
+		char *sender_name;
+		struct dstr str;
+
+		sender_name = get_sender_name();
+
+		dstr_init(&str);
+		dstr_printf(&str, SESSION_PATH, sender_name,
+			    session_token_count);
+		*out_path = str.array;
+
+		bfree(sender_name);
+	}
+}

+ 3 - 0
plugins/linux-pipewire/portal.h

@@ -41,3 +41,6 @@ uint32_t portal_get_screencast_version(void);
 
 GDBusConnection *portal_get_dbus_connection(void);
 GDBusProxy *portal_get_dbus_proxy(void);
+
+void portal_create_request_path(char **out_path, char **out_token);
+void portal_create_session_path(char **out_path, char **out_token);