screencast-portal.c 24 KB

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