screencast-portal.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /* screencast-portal.c
  2. *
  3. * Copyright 2022 Georges Basile Stavracas Neto <[email protected]>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * SPDX-License-Identifier: GPL-2.0-or-later
  19. */
  20. #include "pipewire.h"
  21. #include "portal.h"
  22. #include <gio/gunixfdlist.h>
  23. enum portal_capture_type {
  24. PORTAL_CAPTURE_TYPE_MONITOR = 1 << 0,
  25. PORTAL_CAPTURE_TYPE_WINDOW = 1 << 1,
  26. PORTAL_CAPTURE_TYPE_VIRTUAL = 1 << 2,
  27. };
  28. enum portal_cursor_mode {
  29. PORTAL_CURSOR_MODE_HIDDEN = 1 << 0,
  30. PORTAL_CURSOR_MODE_EMBEDDED = 1 << 1,
  31. PORTAL_CURSOR_MODE_METADATA = 1 << 2,
  32. };
  33. enum obs_portal_capture_type {
  34. OBS_PORTAL_CAPTURE_TYPE_MONITOR = PORTAL_CAPTURE_TYPE_MONITOR,
  35. OBS_PORTAL_CAPTURE_TYPE_WINDOW = PORTAL_CAPTURE_TYPE_WINDOW,
  36. OBS_PORTAL_CAPTURE_TYPE_UNIFIED = PORTAL_CAPTURE_TYPE_MONITOR | PORTAL_CAPTURE_TYPE_WINDOW,
  37. };
  38. struct screencast_portal_capture {
  39. enum obs_portal_capture_type capture_type;
  40. GCancellable *cancellable;
  41. char *session_handle;
  42. char *restore_token;
  43. obs_source_t *source;
  44. obs_data_t *settings;
  45. uint32_t pipewire_node;
  46. bool cursor_visible;
  47. obs_pipewire *obs_pw;
  48. obs_pipewire_stream *obs_pw_stream;
  49. };
  50. /* ------------------------------------------------- */
  51. static GDBusProxy *screencast_proxy = NULL;
  52. static void ensure_screencast_portal_proxy(void)
  53. {
  54. g_autoptr(GError) error = NULL;
  55. if (!screencast_proxy) {
  56. screencast_proxy = g_dbus_proxy_new_sync(portal_get_dbus_connection(), G_DBUS_PROXY_FLAGS_NONE, NULL,
  57. "org.freedesktop.portal.Desktop",
  58. "/org/freedesktop/portal/desktop",
  59. "org.freedesktop.portal.ScreenCast", NULL, &error);
  60. if (error) {
  61. blog(LOG_WARNING, "[portals] Error retrieving D-Bus proxy: %s", error->message);
  62. return;
  63. }
  64. }
  65. }
  66. static GDBusProxy *get_screencast_portal_proxy(void)
  67. {
  68. ensure_screencast_portal_proxy();
  69. return screencast_proxy;
  70. }
  71. static uint32_t get_available_capture_types(void)
  72. {
  73. g_autoptr(GVariant) cached_source_types = NULL;
  74. uint32_t available_source_types;
  75. ensure_screencast_portal_proxy();
  76. if (!screencast_proxy)
  77. return 0;
  78. cached_source_types = g_dbus_proxy_get_cached_property(screencast_proxy, "AvailableSourceTypes");
  79. available_source_types = cached_source_types ? g_variant_get_uint32(cached_source_types) : 0;
  80. return available_source_types;
  81. }
  82. static uint32_t get_available_cursor_modes(void)
  83. {
  84. g_autoptr(GVariant) cached_cursor_modes = NULL;
  85. uint32_t available_cursor_modes;
  86. ensure_screencast_portal_proxy();
  87. if (!screencast_proxy)
  88. return 0;
  89. cached_cursor_modes = g_dbus_proxy_get_cached_property(screencast_proxy, "AvailableCursorModes");
  90. available_cursor_modes = cached_cursor_modes ? g_variant_get_uint32(cached_cursor_modes) : 0;
  91. return available_cursor_modes;
  92. }
  93. static uint32_t get_screencast_version(void)
  94. {
  95. g_autoptr(GVariant) cached_version = NULL;
  96. uint32_t version;
  97. ensure_screencast_portal_proxy();
  98. if (!screencast_proxy)
  99. return 0;
  100. cached_version = g_dbus_proxy_get_cached_property(screencast_proxy, "version");
  101. version = cached_version ? g_variant_get_uint32(cached_version) : 0;
  102. return version;
  103. }
  104. /* ------------------------------------------------- */
  105. static const char *capture_type_to_string(enum obs_portal_capture_type capture_type)
  106. {
  107. switch (capture_type) {
  108. case OBS_PORTAL_CAPTURE_TYPE_MONITOR:
  109. return "monitor";
  110. case OBS_PORTAL_CAPTURE_TYPE_WINDOW:
  111. return "window";
  112. case OBS_PORTAL_CAPTURE_TYPE_UNIFIED:
  113. return "monitor and window";
  114. default:
  115. return "unknown";
  116. }
  117. }
  118. /* ------------------------------------------------- */
  119. static void on_pipewire_remote_opened_cb(GObject *source, GAsyncResult *res, void *user_data)
  120. {
  121. struct obs_pipwire_connect_stream_info connect_info;
  122. struct screencast_portal_capture *capture;
  123. g_autoptr(GUnixFDList) fd_list = NULL;
  124. g_autoptr(GVariant) result = NULL;
  125. g_autoptr(GError) error = NULL;
  126. int pipewire_fd;
  127. int fd_index;
  128. capture = user_data;
  129. result = g_dbus_proxy_call_with_unix_fd_list_finish(G_DBUS_PROXY(source), &fd_list, res, &error);
  130. if (error) {
  131. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  132. blog(LOG_ERROR, "[pipewire] Error retrieving pipewire fd: %s", error->message);
  133. return;
  134. }
  135. g_variant_get(result, "(h)", &fd_index, &error);
  136. pipewire_fd = g_unix_fd_list_get(fd_list, fd_index, &error);
  137. if (error) {
  138. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  139. blog(LOG_ERROR, "[pipewire] Error retrieving pipewire fd: %s", error->message);
  140. return;
  141. }
  142. capture->obs_pw = obs_pipewire_connect_fd(pipewire_fd, NULL, NULL);
  143. if (!capture->obs_pw)
  144. return;
  145. connect_info = (struct obs_pipwire_connect_stream_info){
  146. .stream_name = "OBS Studio",
  147. .stream_properties = pw_properties_new(PW_KEY_MEDIA_TYPE, "Video", PW_KEY_MEDIA_CATEGORY, "Capture",
  148. PW_KEY_MEDIA_ROLE, "Screen", NULL),
  149. .screencast =
  150. {
  151. .cursor_visible = capture->cursor_visible,
  152. },
  153. };
  154. capture->obs_pw_stream =
  155. obs_pipewire_connect_stream(capture->obs_pw, capture->source, capture->pipewire_node, &connect_info);
  156. }
  157. static void open_pipewire_remote(struct screencast_portal_capture *capture)
  158. {
  159. GVariantBuilder builder;
  160. g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
  161. g_dbus_proxy_call_with_unix_fd_list(get_screencast_portal_proxy(), "OpenPipeWireRemote",
  162. g_variant_new("(oa{sv})", capture->session_handle, &builder),
  163. G_DBUS_CALL_FLAGS_NONE, -1, NULL, capture->cancellable,
  164. on_pipewire_remote_opened_cb, capture);
  165. }
  166. /* ------------------------------------------------- */
  167. static void on_start_response_received_cb(GVariant *parameters, void *user_data)
  168. {
  169. struct screencast_portal_capture *capture = user_data;
  170. g_autoptr(GVariant) stream_properties = NULL;
  171. g_autoptr(GVariant) streams = NULL;
  172. g_autoptr(GVariant) result = NULL;
  173. GVariantIter iter;
  174. uint32_t response;
  175. size_t n_streams;
  176. g_variant_get(parameters, "(u@a{sv})", &response, &result);
  177. if (response != 0) {
  178. blog(LOG_WARNING, "[pipewire] Failed to start screencast, denied or cancelled by user");
  179. return;
  180. }
  181. streams = g_variant_lookup_value(result, "streams", G_VARIANT_TYPE_ARRAY);
  182. g_variant_iter_init(&iter, streams);
  183. n_streams = g_variant_iter_n_children(&iter);
  184. if (n_streams != 1) {
  185. blog(LOG_WARNING, "[pipewire] Received more than one stream when only one was expected. "
  186. "This is probably a bug in the desktop portal implementation you are "
  187. "using.");
  188. // The KDE Desktop portal implementation sometimes sends an invalid
  189. // response where more than one stream is attached, and only the
  190. // last one is the one we're looking for. This is the only known
  191. // buggy implementation, so let's at least try to make it work here.
  192. for (size_t i = 0; i < n_streams - 1; i++) {
  193. g_autoptr(GVariant) throwaway_properties = NULL;
  194. uint32_t throwaway_pipewire_node;
  195. g_variant_iter_loop(&iter, "(u@a{sv})", &throwaway_pipewire_node, &throwaway_properties);
  196. }
  197. }
  198. g_variant_iter_loop(&iter, "(u@a{sv})", &capture->pipewire_node, &stream_properties);
  199. if (get_screencast_version() >= 4) {
  200. g_autoptr(GVariant) restore_token = NULL;
  201. g_clear_pointer(&capture->restore_token, bfree);
  202. restore_token = g_variant_lookup_value(result, "restore_token", G_VARIANT_TYPE_STRING);
  203. if (restore_token)
  204. capture->restore_token = bstrdup(g_variant_get_string(restore_token, NULL));
  205. obs_source_save(capture->source);
  206. }
  207. blog(LOG_INFO, "[pipewire] source selected, setting up screencast");
  208. open_pipewire_remote(capture);
  209. }
  210. static void on_started_cb(GObject *source, GAsyncResult *res, void *user_data)
  211. {
  212. UNUSED_PARAMETER(user_data);
  213. g_autoptr(GVariant) result = NULL;
  214. g_autoptr(GError) error = NULL;
  215. result = g_dbus_proxy_call_finish(G_DBUS_PROXY(source), res, &error);
  216. if (error) {
  217. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  218. blog(LOG_ERROR, "[pipewire] Error selecting screencast source: %s", error->message);
  219. return;
  220. }
  221. }
  222. static void start(struct screencast_portal_capture *capture)
  223. {
  224. GVariantBuilder builder;
  225. char *request_token;
  226. char *request_path;
  227. portal_create_request_path(&request_path, &request_token);
  228. blog(LOG_INFO, "[pipewire] Asking for %s", capture_type_to_string(capture->capture_type));
  229. portal_signal_subscribe(request_path, capture->cancellable, on_start_response_received_cb, capture);
  230. g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
  231. g_variant_builder_add(&builder, "{sv}", "handle_token", g_variant_new_string(request_token));
  232. g_dbus_proxy_call(get_screencast_portal_proxy(), "Start",
  233. g_variant_new("(osa{sv})", capture->session_handle, "", &builder), G_DBUS_CALL_FLAGS_NONE, -1,
  234. capture->cancellable, on_started_cb, NULL);
  235. bfree(request_token);
  236. bfree(request_path);
  237. }
  238. /* ------------------------------------------------- */
  239. static void on_select_source_response_received_cb(GVariant *parameters, void *user_data)
  240. {
  241. struct screencast_portal_capture *capture = user_data;
  242. g_autoptr(GVariant) ret = NULL;
  243. uint32_t response;
  244. blog(LOG_DEBUG, "[pipewire] Response to select source received");
  245. g_variant_get(parameters, "(u@a{sv})", &response, &ret);
  246. if (response != 0) {
  247. blog(LOG_WARNING, "[pipewire] Failed to select source, denied or cancelled by user");
  248. return;
  249. }
  250. start(capture);
  251. }
  252. static void on_source_selected_cb(GObject *source, GAsyncResult *res, void *user_data)
  253. {
  254. UNUSED_PARAMETER(user_data);
  255. g_autoptr(GVariant) result = NULL;
  256. g_autoptr(GError) error = NULL;
  257. result = g_dbus_proxy_call_finish(G_DBUS_PROXY(source), res, &error);
  258. if (error) {
  259. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  260. blog(LOG_ERROR, "[pipewire] Error selecting screencast source: %s", error->message);
  261. return;
  262. }
  263. }
  264. static void select_source(struct screencast_portal_capture *capture)
  265. {
  266. GVariantBuilder builder;
  267. uint32_t available_cursor_modes;
  268. char *request_token;
  269. char *request_path;
  270. portal_create_request_path(&request_path, &request_token);
  271. portal_signal_subscribe(request_path, capture->cancellable, on_select_source_response_received_cb, capture);
  272. g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
  273. g_variant_builder_add(&builder, "{sv}", "types", g_variant_new_uint32(capture->capture_type));
  274. g_variant_builder_add(&builder, "{sv}", "multiple", g_variant_new_boolean(FALSE));
  275. g_variant_builder_add(&builder, "{sv}", "handle_token", g_variant_new_string(request_token));
  276. available_cursor_modes = get_available_cursor_modes();
  277. if (available_cursor_modes & PORTAL_CURSOR_MODE_METADATA)
  278. g_variant_builder_add(&builder, "{sv}", "cursor_mode",
  279. g_variant_new_uint32(PORTAL_CURSOR_MODE_METADATA));
  280. else if ((available_cursor_modes & PORTAL_CURSOR_MODE_EMBEDDED) && capture->cursor_visible)
  281. g_variant_builder_add(&builder, "{sv}", "cursor_mode",
  282. g_variant_new_uint32(PORTAL_CURSOR_MODE_EMBEDDED));
  283. else
  284. g_variant_builder_add(&builder, "{sv}", "cursor_mode", g_variant_new_uint32(PORTAL_CURSOR_MODE_HIDDEN));
  285. if (get_screencast_version() >= 4) {
  286. g_variant_builder_add(&builder, "{sv}", "persist_mode", g_variant_new_uint32(2));
  287. if (capture->restore_token && *capture->restore_token) {
  288. g_variant_builder_add(&builder, "{sv}", "restore_token",
  289. g_variant_new_string(capture->restore_token));
  290. }
  291. }
  292. g_dbus_proxy_call(get_screencast_portal_proxy(), "SelectSources",
  293. g_variant_new("(oa{sv})", capture->session_handle, &builder), G_DBUS_CALL_FLAGS_NONE, -1,
  294. capture->cancellable, on_source_selected_cb, NULL);
  295. bfree(request_token);
  296. bfree(request_path);
  297. }
  298. /* ------------------------------------------------- */
  299. static void on_create_session_response_received_cb(GVariant *parameters, void *user_data)
  300. {
  301. struct screencast_portal_capture *capture = user_data;
  302. g_autoptr(GVariant) session_handle_variant = NULL;
  303. g_autoptr(GVariant) result = NULL;
  304. uint32_t response;
  305. g_variant_get(parameters, "(u@a{sv})", &response, &result);
  306. if (response != 0) {
  307. blog(LOG_WARNING, "[pipewire] Failed to create session, denied or cancelled by user");
  308. return;
  309. }
  310. blog(LOG_INFO, "[pipewire] Screencast session created");
  311. session_handle_variant = g_variant_lookup_value(result, "session_handle", NULL);
  312. capture->session_handle = g_variant_dup_string(session_handle_variant, NULL);
  313. select_source(capture);
  314. }
  315. static void on_session_created_cb(GObject *source, GAsyncResult *res, void *user_data)
  316. {
  317. UNUSED_PARAMETER(user_data);
  318. g_autoptr(GVariant) result = NULL;
  319. g_autoptr(GError) error = NULL;
  320. result = g_dbus_proxy_call_finish(G_DBUS_PROXY(source), res, &error);
  321. if (error) {
  322. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  323. blog(LOG_ERROR, "[pipewire] Error creating screencast session: %s", error->message);
  324. return;
  325. }
  326. }
  327. static void create_session(struct screencast_portal_capture *capture)
  328. {
  329. GVariantBuilder builder;
  330. char *session_token;
  331. char *request_token;
  332. char *request_path;
  333. portal_create_request_path(&request_path, &request_token);
  334. portal_create_session_path(NULL, &session_token);
  335. portal_signal_subscribe(request_path, capture->cancellable, on_create_session_response_received_cb, capture);
  336. g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
  337. g_variant_builder_add(&builder, "{sv}", "handle_token", g_variant_new_string(request_token));
  338. g_variant_builder_add(&builder, "{sv}", "session_handle_token", g_variant_new_string(session_token));
  339. g_dbus_proxy_call(get_screencast_portal_proxy(), "CreateSession", g_variant_new("(a{sv})", &builder),
  340. G_DBUS_CALL_FLAGS_NONE, -1, capture->cancellable, on_session_created_cb, NULL);
  341. bfree(session_token);
  342. bfree(request_token);
  343. bfree(request_path);
  344. }
  345. /* ------------------------------------------------- */
  346. static gboolean init_screencast_capture(struct screencast_portal_capture *capture)
  347. {
  348. GDBusConnection *connection;
  349. GDBusProxy *proxy;
  350. capture->cancellable = g_cancellable_new();
  351. connection = portal_get_dbus_connection();
  352. if (!connection)
  353. return FALSE;
  354. proxy = get_screencast_portal_proxy();
  355. if (!proxy)
  356. return FALSE;
  357. blog(LOG_INFO, "PipeWire initialized");
  358. create_session(capture);
  359. return TRUE;
  360. }
  361. static bool reload_session_cb(obs_properties_t *properties, obs_property_t *property, void *data)
  362. {
  363. UNUSED_PARAMETER(properties);
  364. UNUSED_PARAMETER(property);
  365. struct screencast_portal_capture *capture = data;
  366. g_clear_pointer(&capture->restore_token, bfree);
  367. g_clear_pointer(&capture->obs_pw_stream, obs_pipewire_stream_destroy);
  368. g_clear_pointer(&capture->obs_pw, obs_pipewire_destroy);
  369. if (capture->session_handle) {
  370. blog(LOG_DEBUG, "[pipewire] Cleaning previous session %s", capture->session_handle);
  371. g_dbus_connection_call(portal_get_dbus_connection(), "org.freedesktop.portal.Desktop",
  372. capture->session_handle, "org.freedesktop.portal.Session", "Close", NULL, NULL,
  373. G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
  374. g_clear_pointer(&capture->session_handle, g_free);
  375. }
  376. init_screencast_capture(capture);
  377. return false;
  378. }
  379. /* obs_source_info methods */
  380. static const char *screencast_portal_desktop_capture_get_name(void *data)
  381. {
  382. UNUSED_PARAMETER(data);
  383. return obs_module_text("PipeWireDesktopCapture");
  384. }
  385. static const char *screencast_portal_window_capture_get_name(void *data)
  386. {
  387. UNUSED_PARAMETER(data);
  388. return obs_module_text("PipeWireWindowCapture");
  389. }
  390. static void *screencast_portal_desktop_capture_create(obs_data_t *settings, obs_source_t *source)
  391. {
  392. struct screencast_portal_capture *capture;
  393. capture = bzalloc(sizeof(struct screencast_portal_capture));
  394. capture->capture_type = OBS_PORTAL_CAPTURE_TYPE_MONITOR;
  395. capture->cursor_visible = obs_data_get_bool(settings, "ShowCursor");
  396. capture->restore_token = bstrdup(obs_data_get_string(settings, "RestoreToken"));
  397. capture->source = source;
  398. init_screencast_capture(capture);
  399. return capture;
  400. }
  401. static void *screencast_portal_window_capture_create(obs_data_t *settings, obs_source_t *source)
  402. {
  403. struct screencast_portal_capture *capture;
  404. capture = bzalloc(sizeof(struct screencast_portal_capture));
  405. capture->capture_type = OBS_PORTAL_CAPTURE_TYPE_WINDOW;
  406. capture->cursor_visible = obs_data_get_bool(settings, "ShowCursor");
  407. capture->restore_token = bstrdup(obs_data_get_string(settings, "RestoreToken"));
  408. capture->source = source;
  409. init_screencast_capture(capture);
  410. return capture;
  411. }
  412. static void *screencast_portal_capture_create(obs_data_t *settings, obs_source_t *source)
  413. {
  414. struct screencast_portal_capture *capture;
  415. capture = bzalloc(sizeof(struct screencast_portal_capture));
  416. capture->capture_type = OBS_PORTAL_CAPTURE_TYPE_UNIFIED;
  417. capture->cursor_visible = obs_data_get_bool(settings, "ShowCursor");
  418. capture->restore_token = bstrdup(obs_data_get_string(settings, "RestoreToken"));
  419. capture->source = source;
  420. init_screencast_capture(capture);
  421. return capture;
  422. }
  423. static void screencast_portal_capture_destroy(void *data)
  424. {
  425. struct screencast_portal_capture *capture = data;
  426. if (!capture)
  427. return;
  428. if (capture->session_handle) {
  429. g_dbus_connection_call(portal_get_dbus_connection(), "org.freedesktop.portal.Desktop",
  430. capture->session_handle, "org.freedesktop.portal.Session", "Close", NULL, NULL,
  431. G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
  432. g_clear_pointer(&capture->session_handle, g_free);
  433. }
  434. g_clear_pointer(&capture->restore_token, bfree);
  435. g_clear_pointer(&capture->obs_pw_stream, obs_pipewire_stream_destroy);
  436. obs_pipewire_destroy(capture->obs_pw);
  437. g_cancellable_cancel(capture->cancellable);
  438. g_clear_object(&capture->cancellable);
  439. bfree(capture);
  440. }
  441. static void screencast_portal_capture_save(void *data, obs_data_t *settings)
  442. {
  443. struct screencast_portal_capture *capture = data;
  444. obs_data_set_string(settings, "RestoreToken", capture->restore_token);
  445. }
  446. static void screencast_portal_capture_get_defaults(obs_data_t *settings)
  447. {
  448. obs_data_set_default_bool(settings, "ShowCursor", true);
  449. obs_data_set_default_string(settings, "RestoreToken", NULL);
  450. }
  451. static obs_properties_t *screencast_portal_capture_get_properties(void *data)
  452. {
  453. struct screencast_portal_capture *capture = data;
  454. const char *reload_string_id;
  455. obs_properties_t *properties;
  456. switch (capture->capture_type) {
  457. case OBS_PORTAL_CAPTURE_TYPE_MONITOR:
  458. reload_string_id = "PipeWireSelectMonitor";
  459. break;
  460. case OBS_PORTAL_CAPTURE_TYPE_WINDOW:
  461. reload_string_id = "PipeWireSelectWindow";
  462. break;
  463. case OBS_PORTAL_CAPTURE_TYPE_UNIFIED:
  464. reload_string_id = "PipeWireSelectScreenCast";
  465. break;
  466. default:
  467. return NULL;
  468. }
  469. properties = obs_properties_create();
  470. obs_properties_add_button2(properties, "Reload", obs_module_text(reload_string_id), reload_session_cb, capture);
  471. obs_properties_add_bool(properties, "ShowCursor", obs_module_text("ShowCursor"));
  472. return properties;
  473. }
  474. static void screencast_portal_capture_update(void *data, obs_data_t *settings)
  475. {
  476. struct screencast_portal_capture *capture = data;
  477. capture->cursor_visible = obs_data_get_bool(settings, "ShowCursor");
  478. if (capture->obs_pw_stream)
  479. obs_pipewire_stream_set_cursor_visible(capture->obs_pw_stream, capture->cursor_visible);
  480. }
  481. static void screencast_portal_capture_show(void *data)
  482. {
  483. struct screencast_portal_capture *capture = data;
  484. if (capture->obs_pw_stream)
  485. obs_pipewire_stream_show(capture->obs_pw_stream);
  486. }
  487. static void screencast_portal_capture_hide(void *data)
  488. {
  489. struct screencast_portal_capture *capture = data;
  490. if (capture->obs_pw_stream)
  491. obs_pipewire_stream_hide(capture->obs_pw_stream);
  492. }
  493. static uint32_t screencast_portal_capture_get_width(void *data)
  494. {
  495. struct screencast_portal_capture *capture = data;
  496. if (capture->obs_pw_stream)
  497. return obs_pipewire_stream_get_width(capture->obs_pw_stream);
  498. else
  499. return 0;
  500. }
  501. static uint32_t screencast_portal_capture_get_height(void *data)
  502. {
  503. struct screencast_portal_capture *capture = data;
  504. if (capture->obs_pw_stream)
  505. return obs_pipewire_stream_get_height(capture->obs_pw_stream);
  506. else
  507. return 0;
  508. }
  509. static void screencast_portal_capture_video_render(void *data, gs_effect_t *effect)
  510. {
  511. struct screencast_portal_capture *capture = data;
  512. if (capture->obs_pw_stream)
  513. obs_pipewire_stream_video_render(capture->obs_pw_stream, effect);
  514. }
  515. void screencast_portal_load(void)
  516. {
  517. uint32_t available_capture_types = get_available_capture_types();
  518. bool desktop_capture_available = (available_capture_types & PORTAL_CAPTURE_TYPE_MONITOR) != 0;
  519. bool window_capture_available = (available_capture_types & PORTAL_CAPTURE_TYPE_WINDOW) != 0;
  520. if (available_capture_types == 0) {
  521. blog(LOG_INFO, "[pipewire] No capture sources available");
  522. return;
  523. }
  524. blog(LOG_INFO, "[pipewire] Available capture sources:");
  525. if (desktop_capture_available)
  526. blog(LOG_INFO, "[pipewire] - Monitor source");
  527. if (window_capture_available)
  528. blog(LOG_INFO, "[pipewire] - Window source");
  529. // Desktop capture
  530. const struct obs_source_info screencast_portal_desktop_capture_info = {
  531. .id = "pipewire-desktop-capture-source",
  532. .type = OBS_SOURCE_TYPE_INPUT,
  533. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CAP_OBSOLETE,
  534. .get_name = screencast_portal_desktop_capture_get_name,
  535. .create = screencast_portal_desktop_capture_create,
  536. .destroy = screencast_portal_capture_destroy,
  537. .save = screencast_portal_capture_save,
  538. .get_defaults = screencast_portal_capture_get_defaults,
  539. .get_properties = screencast_portal_capture_get_properties,
  540. .update = screencast_portal_capture_update,
  541. .show = screencast_portal_capture_show,
  542. .hide = screencast_portal_capture_hide,
  543. .get_width = screencast_portal_capture_get_width,
  544. .get_height = screencast_portal_capture_get_height,
  545. .video_render = screencast_portal_capture_video_render,
  546. .icon_type = OBS_ICON_TYPE_DESKTOP_CAPTURE,
  547. };
  548. if (desktop_capture_available)
  549. obs_register_source(&screencast_portal_desktop_capture_info);
  550. // Window capture
  551. const struct obs_source_info screencast_portal_window_capture_info = {
  552. .id = "pipewire-window-capture-source",
  553. .type = OBS_SOURCE_TYPE_INPUT,
  554. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CAP_OBSOLETE,
  555. .get_name = screencast_portal_window_capture_get_name,
  556. .create = screencast_portal_window_capture_create,
  557. .destroy = screencast_portal_capture_destroy,
  558. .save = screencast_portal_capture_save,
  559. .get_defaults = screencast_portal_capture_get_defaults,
  560. .get_properties = screencast_portal_capture_get_properties,
  561. .update = screencast_portal_capture_update,
  562. .show = screencast_portal_capture_show,
  563. .hide = screencast_portal_capture_hide,
  564. .get_width = screencast_portal_capture_get_width,
  565. .get_height = screencast_portal_capture_get_height,
  566. .video_render = screencast_portal_capture_video_render,
  567. .icon_type = OBS_ICON_TYPE_WINDOW_CAPTURE,
  568. };
  569. if (window_capture_available)
  570. obs_register_source(&screencast_portal_window_capture_info);
  571. // Screen capture (monitor and window)
  572. const struct obs_source_info screencast_portal_capture_info = {
  573. .id = "pipewire-screen-capture-source",
  574. .type = OBS_SOURCE_TYPE_INPUT,
  575. .output_flags = OBS_SOURCE_VIDEO,
  576. .get_name = screencast_portal_desktop_capture_get_name,
  577. .create = screencast_portal_capture_create,
  578. .destroy = screencast_portal_capture_destroy,
  579. .save = screencast_portal_capture_save,
  580. .get_defaults = screencast_portal_capture_get_defaults,
  581. .get_properties = screencast_portal_capture_get_properties,
  582. .update = screencast_portal_capture_update,
  583. .show = screencast_portal_capture_show,
  584. .hide = screencast_portal_capture_hide,
  585. .get_width = screencast_portal_capture_get_width,
  586. .get_height = screencast_portal_capture_get_height,
  587. .video_render = screencast_portal_capture_video_render,
  588. .icon_type = OBS_ICON_TYPE_DESKTOP_CAPTURE,
  589. };
  590. obs_register_source(&screencast_portal_capture_info);
  591. }
  592. void screencast_portal_unload(void)
  593. {
  594. g_clear_object(&screencast_proxy);
  595. }