screencast-portal.c 22 KB

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