camera-portal.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /* camera-portal.c
  2. *
  3. * Copyright 2021 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 "formats.h"
  22. #include "portal.h"
  23. #include <util/dstr.h>
  24. #include <fcntl.h>
  25. #include <unistd.h>
  26. #include <gio/gio.h>
  27. #include <gio/gunixfdlist.h>
  28. #include <spa/debug/dict.h>
  29. #include <spa/node/keys.h>
  30. #include <spa/pod/iter.h>
  31. #include <spa/pod/parser.h>
  32. #include <spa/param/props.h>
  33. #include <spa/utils/defs.h>
  34. #include <spa/utils/keys.h>
  35. #include <spa/utils/result.h>
  36. struct camera_portal_source {
  37. obs_source_t *source;
  38. obs_data_t *settings;
  39. obs_pipewire_stream *obs_pw_stream;
  40. char *device_id;
  41. };
  42. /* ------------------------------------------------- */
  43. struct pw_portal_connection {
  44. obs_pipewire *obs_pw;
  45. GHashTable *devices;
  46. GCancellable *cancellable;
  47. GPtrArray *sources;
  48. bool initializing;
  49. };
  50. struct pw_portal_connection *connection = NULL;
  51. static void pw_portal_connection_free(struct pw_portal_connection *connection)
  52. {
  53. if (!connection)
  54. return;
  55. g_cancellable_cancel(connection->cancellable);
  56. g_clear_pointer(&connection->devices, g_hash_table_destroy);
  57. g_clear_pointer(&connection->obs_pw, obs_pipewire_destroy);
  58. g_clear_pointer(&connection->sources, g_ptr_array_unref);
  59. g_clear_object(&connection->cancellable);
  60. bfree(connection);
  61. }
  62. static GDBusProxy *camera_proxy = NULL;
  63. static void ensure_camera_portal_proxy(void)
  64. {
  65. g_autoptr(GError) error = NULL;
  66. if (!camera_proxy) {
  67. camera_proxy = g_dbus_proxy_new_sync(
  68. portal_get_dbus_connection(), G_DBUS_PROXY_FLAGS_NONE,
  69. NULL, "org.freedesktop.portal.Desktop",
  70. "/org/freedesktop/portal/desktop",
  71. "org.freedesktop.portal.Camera", NULL, &error);
  72. if (error) {
  73. blog(LOG_WARNING,
  74. "[portals] Error retrieving D-Bus proxy: %s",
  75. error->message);
  76. return;
  77. }
  78. }
  79. }
  80. static GDBusProxy *get_camera_portal_proxy(void)
  81. {
  82. ensure_camera_portal_proxy();
  83. return camera_proxy;
  84. }
  85. static uint32_t get_camera_version(void)
  86. {
  87. g_autoptr(GVariant) cached_version = NULL;
  88. uint32_t version;
  89. ensure_camera_portal_proxy();
  90. if (!camera_proxy)
  91. return 0;
  92. cached_version =
  93. g_dbus_proxy_get_cached_property(camera_proxy, "version");
  94. version = cached_version ? g_variant_get_uint32(cached_version) : 0;
  95. return version;
  96. }
  97. /* ------------------------------------------------- */
  98. struct camera_device {
  99. uint32_t id;
  100. struct pw_properties *properties;
  101. struct pw_proxy *proxy;
  102. struct spa_hook proxy_listener;
  103. struct pw_node *node;
  104. struct spa_hook node_listener;
  105. struct pw_node_info *info;
  106. uint32_t changed;
  107. struct spa_list pending_list;
  108. struct spa_list param_list;
  109. int pending_sync;
  110. };
  111. struct param {
  112. uint32_t id;
  113. int32_t seq;
  114. struct spa_list link;
  115. struct spa_pod *param;
  116. };
  117. static uint32_t clear_params(struct spa_list *param_list, uint32_t id)
  118. {
  119. struct param *p, *t;
  120. uint32_t count = 0;
  121. spa_list_for_each_safe(p, t, param_list, link)
  122. {
  123. if (id == SPA_ID_INVALID || p->id == id) {
  124. spa_list_remove(&p->link);
  125. free(p);
  126. count++;
  127. }
  128. }
  129. return count;
  130. }
  131. static struct param *add_param(struct spa_list *params, int seq, uint32_t id,
  132. const struct spa_pod *param)
  133. {
  134. struct param *p;
  135. if (id == SPA_ID_INVALID) {
  136. if (param == NULL || !spa_pod_is_object(param)) {
  137. errno = EINVAL;
  138. return NULL;
  139. }
  140. id = SPA_POD_OBJECT_ID(param);
  141. }
  142. p = malloc(sizeof(*p) + (param != NULL ? SPA_POD_SIZE(param) : 0));
  143. if (p == NULL)
  144. return NULL;
  145. p->id = id;
  146. p->seq = seq;
  147. if (param != NULL) {
  148. p->param = SPA_PTROFF(p, sizeof(*p), struct spa_pod);
  149. memcpy(p->param, param, SPA_POD_SIZE(param));
  150. } else {
  151. clear_params(params, id);
  152. p->param = NULL;
  153. }
  154. spa_list_append(params, &p->link);
  155. return p;
  156. }
  157. static void object_update_params(struct spa_list *param_list,
  158. struct spa_list *pending_list,
  159. uint32_t n_params,
  160. struct spa_param_info *params)
  161. {
  162. struct param *p, *t;
  163. uint32_t i;
  164. for (i = 0; i < n_params; i++) {
  165. spa_list_for_each_safe(p, t, pending_list, link)
  166. {
  167. if (p->id == params[i].id && p->seq != params[i].seq &&
  168. p->param != NULL) {
  169. spa_list_remove(&p->link);
  170. free(p);
  171. }
  172. }
  173. }
  174. spa_list_consume(p, pending_list, link)
  175. {
  176. spa_list_remove(&p->link);
  177. if (p->param == NULL) {
  178. clear_params(param_list, p->id);
  179. free(p);
  180. } else {
  181. spa_list_append(param_list, &p->link);
  182. }
  183. }
  184. }
  185. static struct camera_device *
  186. camera_device_new(uint32_t id, const struct spa_dict *properties)
  187. {
  188. struct camera_device *device = bzalloc(sizeof(struct camera_device));
  189. device->id = id;
  190. device->properties = properties ? pw_properties_new_dict(properties)
  191. : NULL;
  192. spa_list_init(&device->pending_list);
  193. spa_list_init(&device->param_list);
  194. return device;
  195. }
  196. static void camera_device_free(struct camera_device *device)
  197. {
  198. if (!device)
  199. return;
  200. clear_params(&device->pending_list, SPA_ID_INVALID);
  201. clear_params(&device->param_list, SPA_ID_INVALID);
  202. g_clear_pointer(&device->proxy, pw_proxy_destroy);
  203. g_clear_pointer(&device->properties, pw_properties_free);
  204. bfree(device);
  205. }
  206. /* ------------------------------------------------- */
  207. static bool update_device_id(struct camera_portal_source *camera_source,
  208. const char *new_device_id)
  209. {
  210. if (strcmp(camera_source->device_id, new_device_id) == 0)
  211. return false;
  212. g_clear_pointer(&camera_source->device_id, bfree);
  213. camera_source->device_id = bstrdup(new_device_id);
  214. return true;
  215. }
  216. static void stream_camera(struct camera_portal_source *camera_source)
  217. {
  218. struct obs_pipwire_connect_stream_info connect_info;
  219. struct camera_device *device;
  220. g_clear_pointer(&camera_source->obs_pw_stream,
  221. obs_pipewire_stream_destroy);
  222. device = g_hash_table_lookup(connection->devices,
  223. camera_source->device_id);
  224. if (!device)
  225. return;
  226. blog(LOG_INFO, "[camera-portal] streaming camera '%s'",
  227. camera_source->device_id);
  228. connect_info = (struct obs_pipwire_connect_stream_info){
  229. .stream_name = "OBS PipeWire Camera",
  230. .stream_properties = pw_properties_new(
  231. PW_KEY_MEDIA_TYPE, "Video", PW_KEY_MEDIA_CATEGORY,
  232. "Capture", PW_KEY_MEDIA_ROLE, "Camera", NULL),
  233. };
  234. camera_source->obs_pw_stream = obs_pipewire_connect_stream(
  235. connection->obs_pw, camera_source->source, device->id,
  236. &connect_info);
  237. }
  238. static void camera_format_list(struct camera_device *dev, obs_property_t *prop)
  239. {
  240. struct param *p;
  241. enum video_format last_format = VIDEO_FORMAT_NONE;
  242. obs_property_list_clear(prop);
  243. spa_list_for_each(p, &dev->param_list, link)
  244. {
  245. struct obs_pw_video_format obs_pw_video_format;
  246. uint32_t media_type, media_subtype, format;
  247. if (p->id != SPA_PARAM_EnumFormat || p->param == NULL)
  248. continue;
  249. if (spa_format_parse(p->param, &media_type, &media_subtype) < 0)
  250. continue;
  251. if (media_type != SPA_MEDIA_TYPE_video)
  252. continue;
  253. if (media_subtype == SPA_MEDIA_SUBTYPE_raw) {
  254. if (spa_pod_parse_object(p->param,
  255. SPA_TYPE_OBJECT_Format, NULL,
  256. SPA_FORMAT_VIDEO_format,
  257. SPA_POD_Id(&format)) < 0)
  258. continue;
  259. } else {
  260. format = SPA_VIDEO_FORMAT_ENCODED;
  261. }
  262. if (!obs_pw_video_format_from_spa_format(format,
  263. &obs_pw_video_format))
  264. continue;
  265. if (obs_pw_video_format.video_format == last_format)
  266. continue;
  267. last_format = obs_pw_video_format.video_format;
  268. obs_property_list_add_int(prop, obs_pw_video_format.pretty_name,
  269. format);
  270. }
  271. }
  272. static inline void add_control_property(obs_properties_t *props,
  273. obs_data_t *settings,
  274. struct camera_device *dev,
  275. struct param *p)
  276. {
  277. UNUSED_PARAMETER(dev);
  278. const struct spa_pod *type, *pod, *labels = NULL;
  279. uint32_t n_vals, choice, container = SPA_ID_INVALID;
  280. obs_property_t *prop = NULL;
  281. const char *name;
  282. if (spa_pod_parse_object(
  283. p->param, SPA_TYPE_OBJECT_PropInfo, NULL,
  284. SPA_PROP_INFO_description, SPA_POD_OPT_String(&name),
  285. SPA_PROP_INFO_type, SPA_POD_PodChoice(&type),
  286. SPA_PROP_INFO_container, SPA_POD_OPT_Id(&container),
  287. SPA_PROP_INFO_labels, SPA_POD_OPT_PodStruct(&labels)) < 0)
  288. return;
  289. pod = spa_pod_get_values(type, &n_vals, &choice);
  290. container = container != SPA_ID_INVALID ? container : SPA_POD_TYPE(pod);
  291. switch (SPA_POD_TYPE(pod)) {
  292. case SPA_TYPE_Int: {
  293. int32_t *vals = SPA_POD_BODY(pod);
  294. if (n_vals < 1)
  295. return;
  296. if (choice == SPA_CHOICE_Enum) {
  297. struct spa_pod_parser prs;
  298. struct spa_pod_frame f;
  299. if (labels == NULL)
  300. return;
  301. prop = obs_properties_add_list(props, (char *)name,
  302. (char *)name,
  303. OBS_COMBO_TYPE_LIST,
  304. OBS_COMBO_FORMAT_INT);
  305. spa_pod_parser_pod(&prs, (struct spa_pod *)labels);
  306. if (spa_pod_parser_push_struct(&prs, &f) < 0)
  307. return;
  308. while (1) {
  309. int32_t id;
  310. const char *desc;
  311. if (spa_pod_parser_get_int(&prs, &id) < 0 ||
  312. spa_pod_parser_get_string(&prs, &desc) < 0)
  313. break;
  314. obs_property_list_add_int(prop, (char *)desc,
  315. id);
  316. }
  317. } else {
  318. prop = obs_properties_add_int_slider(
  319. props, (char *)name, (char *)name,
  320. n_vals > 1 ? vals[1] : vals[0],
  321. n_vals > 2 ? vals[2] : vals[0],
  322. n_vals > 3 ? vals[3] : 1);
  323. }
  324. obs_data_set_default_int(settings, (char *)name, vals[0]);
  325. break;
  326. }
  327. case SPA_TYPE_Bool: {
  328. int32_t *vals = SPA_POD_BODY(pod);
  329. if (n_vals < 1)
  330. return;
  331. prop = obs_properties_add_bool(props, (char *)name,
  332. (char *)name);
  333. obs_data_set_default_bool(settings, (char *)name, vals[0]);
  334. break;
  335. }
  336. default:
  337. break;
  338. }
  339. }
  340. static void camera_update_controls(struct camera_device *dev,
  341. obs_properties_t *props,
  342. obs_data_t *settings)
  343. {
  344. struct param *p;
  345. spa_list_for_each(p, &dev->param_list, link)
  346. {
  347. if (p->id != SPA_PARAM_PropInfo || p->param == NULL)
  348. continue;
  349. add_control_property(props, settings, dev, p);
  350. }
  351. }
  352. static bool device_selected(void *data, obs_properties_t *props,
  353. obs_property_t *property, obs_data_t *settings)
  354. {
  355. UNUSED_PARAMETER(props);
  356. UNUSED_PARAMETER(property);
  357. struct camera_portal_source *camera_source = data;
  358. const char *device_id;
  359. struct camera_device *device;
  360. obs_properties_t *new_control_properties;
  361. device_id = obs_data_get_string(settings, "device_id");
  362. blog(LOG_INFO, "[camera-portal] selected camera '%s'", device_id);
  363. device = g_hash_table_lookup(connection->devices, device_id);
  364. if (device == NULL)
  365. return false;
  366. if (update_device_id(camera_source, device_id))
  367. stream_camera(camera_source);
  368. blog(LOG_INFO, "[camera-portal] Updating pixel formats");
  369. property = obs_properties_get(props, "pixelformat");
  370. new_control_properties = obs_properties_create();
  371. obs_properties_remove_by_name(props, "controls");
  372. camera_format_list(device, property);
  373. camera_update_controls(device, new_control_properties, settings);
  374. obs_properties_add_group(props, "controls",
  375. obs_module_text("CameraControls"),
  376. OBS_GROUP_NORMAL, new_control_properties);
  377. obs_property_modified(property, settings);
  378. return true;
  379. }
  380. /*
  381. * Format selected callback
  382. */
  383. static bool format_selected(void *data, obs_properties_t *properties,
  384. obs_property_t *property, obs_data_t *settings)
  385. {
  386. UNUSED_PARAMETER(properties);
  387. UNUSED_PARAMETER(property);
  388. UNUSED_PARAMETER(settings);
  389. struct camera_portal_source *camera_source = data;
  390. blog(LOG_INFO, "[camera-portal] Selected format for '%s'",
  391. camera_source->device_id);
  392. return true;
  393. }
  394. /*
  395. * Resolution selected callback
  396. */
  397. static bool resolution_selected(void *data, obs_properties_t *properties,
  398. obs_property_t *property, obs_data_t *settings)
  399. {
  400. UNUSED_PARAMETER(properties);
  401. UNUSED_PARAMETER(property);
  402. UNUSED_PARAMETER(settings);
  403. struct camera_portal_source *camera_source = data;
  404. blog(LOG_INFO, "[camera-portal] Selected resolution for '%s'",
  405. camera_source->device_id);
  406. return true;
  407. }
  408. static void populate_cameras_list(struct camera_portal_source *camera_source,
  409. obs_property_t *device_list)
  410. {
  411. struct camera_device *device;
  412. GHashTableIter iter;
  413. const char *device_id;
  414. bool device_found;
  415. if (!connection)
  416. return;
  417. obs_property_list_clear(device_list);
  418. device_found = false;
  419. g_hash_table_iter_init(&iter, connection->devices);
  420. while (g_hash_table_iter_next(&iter, (gpointer *)&device_id,
  421. (gpointer *)&device)) {
  422. const char *device_name;
  423. device_name = pw_properties_get(device->properties,
  424. PW_KEY_NODE_DESCRIPTION);
  425. obs_property_list_add_string(device_list, device_name,
  426. device_id);
  427. device_found |= strcmp(device_id, camera_source->device_id) ==
  428. 0;
  429. }
  430. if (!device_found && camera_source->device_id) {
  431. size_t device_index;
  432. device_index = obs_property_list_add_string(
  433. device_list, camera_source->device_id,
  434. camera_source->device_id);
  435. obs_property_list_item_disable(device_list, device_index, true);
  436. }
  437. }
  438. /* ------------------------------------------------- */
  439. static void node_info(void *data, const struct pw_node_info *info)
  440. {
  441. struct camera_device *device = data;
  442. uint32_t i, changed = 0;
  443. int res;
  444. info = device->info = pw_node_info_update(device->info, info);
  445. if (info == NULL)
  446. return;
  447. if (info->change_mask & PW_NODE_CHANGE_MASK_PARAMS) {
  448. for (i = 0; i < info->n_params; i++) {
  449. uint32_t id = info->params[i].id;
  450. if (info->params[i].user == 0)
  451. continue;
  452. info->params[i].user = 0;
  453. changed++;
  454. add_param(&device->pending_list, 0, id, NULL);
  455. if (!(info->params[i].flags & SPA_PARAM_INFO_READ))
  456. continue;
  457. res = pw_node_enum_params(
  458. (struct pw_node *)device->proxy,
  459. ++info->params[i].seq, id, 0, -1, NULL);
  460. if (SPA_RESULT_IS_ASYNC(res))
  461. info->params[i].seq = res;
  462. }
  463. }
  464. if (changed) {
  465. device->changed += changed;
  466. device->pending_sync =
  467. pw_proxy_sync(device->proxy, device->pending_sync);
  468. }
  469. }
  470. static void node_param(void *data, int seq, uint32_t id, uint32_t index,
  471. uint32_t next, const struct spa_pod *param)
  472. {
  473. UNUSED_PARAMETER(index);
  474. UNUSED_PARAMETER(next);
  475. struct camera_device *device = data;
  476. add_param(&device->pending_list, seq, id, param);
  477. }
  478. static const struct pw_node_events node_events = {
  479. PW_VERSION_NODE_EVENTS,
  480. .info = node_info,
  481. .param = node_param,
  482. };
  483. static void on_proxy_removed_cb(void *data)
  484. {
  485. struct camera_device *device = data;
  486. pw_proxy_destroy(device->proxy);
  487. }
  488. static void on_destroy_proxy_cb(void *data)
  489. {
  490. struct camera_device *device = data;
  491. spa_hook_remove(&device->proxy_listener);
  492. device->proxy = NULL;
  493. }
  494. static void on_done_proxy_cb(void *data, int seq)
  495. {
  496. struct camera_device *device = data;
  497. if (device->info != NULL && device->pending_sync == seq) {
  498. object_update_params(&device->param_list, &device->pending_list,
  499. device->info->n_params,
  500. device->info->params);
  501. }
  502. }
  503. static const struct pw_proxy_events proxy_events = {
  504. PW_VERSION_PROXY_EVENTS,
  505. .removed = on_proxy_removed_cb,
  506. .destroy = on_destroy_proxy_cb,
  507. .done = on_done_proxy_cb,
  508. };
  509. static void on_registry_global_cb(void *user_data, uint32_t id,
  510. uint32_t permissions, const char *type,
  511. uint32_t version,
  512. const struct spa_dict *props)
  513. {
  514. UNUSED_PARAMETER(user_data);
  515. UNUSED_PARAMETER(permissions);
  516. UNUSED_PARAMETER(version);
  517. struct camera_device *device;
  518. struct pw_registry *registry;
  519. const char *device_id;
  520. if (strcmp(type, PW_TYPE_INTERFACE_Node) != 0)
  521. return;
  522. registry = obs_pipewire_get_registry(connection->obs_pw);
  523. device_id = spa_dict_lookup(props, SPA_KEY_NODE_NAME);
  524. blog(LOG_INFO, "[camera-portal] Found device %s", device_id);
  525. device = camera_device_new(id, props);
  526. device->proxy = pw_registry_bind(registry, id, type, version, 0);
  527. if (!device->proxy) {
  528. blog(LOG_WARNING, "[camera-portal] Failed to bind device %s",
  529. device_id);
  530. bfree(device);
  531. return;
  532. }
  533. pw_proxy_add_listener(device->proxy, &device->proxy_listener,
  534. &proxy_events, device);
  535. device->node = (struct pw_node *)device->proxy;
  536. pw_node_add_listener(device->node, &device->node_listener, &node_events,
  537. device);
  538. g_hash_table_insert(connection->devices, bstrdup(device_id), device);
  539. for (size_t i = 0; i < connection->sources->len; i++) {
  540. struct camera_portal_source *camera_source =
  541. g_ptr_array_index(connection->sources, i);
  542. if (strcmp(camera_source->device_id, device_id) == 0)
  543. stream_camera(camera_source);
  544. }
  545. }
  546. static void on_registry_global_remove_cb(void *user_data, uint32_t id)
  547. {
  548. UNUSED_PARAMETER(user_data);
  549. struct camera_device *device;
  550. const char *device_id;
  551. GHashTableIter iter;
  552. g_hash_table_iter_init(&iter, connection->devices);
  553. while (g_hash_table_iter_next(&iter, (gpointer *)&device_id,
  554. (gpointer *)&device)) {
  555. if (device->id != id)
  556. continue;
  557. g_hash_table_iter_remove(&iter);
  558. }
  559. }
  560. static const struct pw_registry_events registry_events = {
  561. PW_VERSION_REGISTRY_EVENTS,
  562. .global = on_registry_global_cb,
  563. .global_remove = on_registry_global_remove_cb,
  564. };
  565. /* ------------------------------------------------- */
  566. static void on_pipewire_remote_opened_cb(GObject *source, GAsyncResult *res,
  567. void *user_data)
  568. {
  569. UNUSED_PARAMETER(user_data);
  570. g_autoptr(GUnixFDList) fd_list = NULL;
  571. g_autoptr(GVariant) result = NULL;
  572. g_autoptr(GError) error = NULL;
  573. int pipewire_fd;
  574. int fd_index;
  575. result = g_dbus_proxy_call_with_unix_fd_list_finish(
  576. G_DBUS_PROXY(source), &fd_list, res, &error);
  577. if (error) {
  578. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  579. blog(LOG_ERROR,
  580. "[camera-portal] Error retrieving PipeWire fd: %s",
  581. error->message);
  582. return;
  583. }
  584. g_variant_get(result, "(h)", &fd_index, &error);
  585. pipewire_fd = g_unix_fd_list_get(fd_list, fd_index, &error);
  586. if (error) {
  587. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  588. blog(LOG_ERROR,
  589. "[camera-portal] Error retrieving PipeWire fd: %s",
  590. error->message);
  591. return;
  592. }
  593. connection->obs_pw = obs_pipewire_connect_fd(
  594. pipewire_fd, &registry_events, connection);
  595. obs_pipewire_roundtrip(connection->obs_pw);
  596. }
  597. static void open_pipewire_remote(void)
  598. {
  599. GVariantBuilder builder;
  600. g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
  601. g_dbus_proxy_call_with_unix_fd_list(get_camera_portal_proxy(),
  602. "OpenPipeWireRemote",
  603. g_variant_new("(a{sv})", &builder),
  604. G_DBUS_CALL_FLAGS_NONE, -1, NULL,
  605. connection->cancellable,
  606. on_pipewire_remote_opened_cb, NULL);
  607. }
  608. /* ------------------------------------------------- */
  609. static void on_access_camera_response_received_cb(GVariant *parameters,
  610. void *user_data)
  611. {
  612. UNUSED_PARAMETER(user_data);
  613. g_autoptr(GVariant) result = NULL;
  614. uint32_t response;
  615. g_variant_get(parameters, "(u@a{sv})", &response, &result);
  616. if (response != 0) {
  617. blog(LOG_WARNING,
  618. "[camera-portal] Failed to create session, denied or cancelled by user");
  619. return;
  620. }
  621. blog(LOG_INFO, "[camera-portal] Successfully accessed cameras");
  622. open_pipewire_remote();
  623. }
  624. static void on_access_camera_finished_cb(GObject *source, GAsyncResult *res,
  625. void *user_data)
  626. {
  627. UNUSED_PARAMETER(user_data);
  628. g_autoptr(GVariant) result = NULL;
  629. g_autoptr(GError) error = NULL;
  630. result = g_dbus_proxy_call_finish(G_DBUS_PROXY(source), res, &error);
  631. if (error) {
  632. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  633. blog(LOG_ERROR,
  634. "[camera-portal] Error accessing camera: %s",
  635. error->message);
  636. return;
  637. }
  638. }
  639. static void access_camera(struct camera_portal_source *camera_source)
  640. {
  641. GVariantBuilder builder;
  642. char *request_token;
  643. char *request_path;
  644. if (connection && connection->obs_pw) {
  645. stream_camera(camera_source);
  646. return;
  647. }
  648. if (!connection) {
  649. connection = bzalloc(sizeof(struct pw_portal_connection));
  650. connection->devices = g_hash_table_new_full(
  651. g_str_hash, g_str_equal, bfree,
  652. (GDestroyNotify)camera_device_free);
  653. connection->cancellable = g_cancellable_new();
  654. connection->sources = g_ptr_array_new();
  655. connection->initializing = false;
  656. }
  657. g_ptr_array_add(connection->sources, camera_source);
  658. if (connection->initializing)
  659. return;
  660. portal_create_request_path(&request_path, &request_token);
  661. portal_signal_subscribe(request_path, NULL,
  662. on_access_camera_response_received_cb, NULL);
  663. g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
  664. g_variant_builder_add(&builder, "{sv}", "handle_token",
  665. g_variant_new_string(request_token));
  666. g_dbus_proxy_call(get_camera_portal_proxy(), "AccessCamera",
  667. g_variant_new("(a{sv})", &builder),
  668. G_DBUS_CALL_FLAGS_NONE, -1, connection->cancellable,
  669. on_access_camera_finished_cb, NULL);
  670. connection->initializing = true;
  671. bfree(request_token);
  672. bfree(request_path);
  673. }
  674. /* obs_source_info methods */
  675. static const char *pipewire_camera_get_name(void *data)
  676. {
  677. UNUSED_PARAMETER(data);
  678. return obs_module_text("PipeWireCamera");
  679. }
  680. static void *pipewire_camera_create(obs_data_t *settings, obs_source_t *source)
  681. {
  682. struct camera_portal_source *camera_source;
  683. camera_source = bzalloc(sizeof(struct camera_portal_source));
  684. camera_source->source = source;
  685. camera_source->device_id =
  686. bstrdup(obs_data_get_string(settings, "device_id"));
  687. access_camera(camera_source);
  688. return camera_source;
  689. }
  690. static void pipewire_camera_destroy(void *data)
  691. {
  692. struct camera_portal_source *camera_source = data;
  693. if (connection)
  694. g_ptr_array_remove(connection->sources, camera_source);
  695. g_clear_pointer(&camera_source->obs_pw_stream,
  696. obs_pipewire_stream_destroy);
  697. g_clear_pointer(&camera_source->device_id, bfree);
  698. bfree(camera_source);
  699. }
  700. static void pipewire_camera_get_defaults(obs_data_t *settings)
  701. {
  702. obs_data_set_default_string(settings, "device_id", NULL);
  703. }
  704. static obs_properties_t *pipewire_camera_get_properties(void *data)
  705. {
  706. struct camera_portal_source *camera_source = data;
  707. obs_properties_t *controls_props;
  708. obs_properties_t *props;
  709. obs_property_t *resolution_list;
  710. obs_property_t *device_list;
  711. obs_property_t *format_list;
  712. props = obs_properties_create();
  713. device_list = obs_properties_add_list(
  714. props, "device_id", obs_module_text("PipeWireCameraDevice"),
  715. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  716. format_list = obs_properties_add_list(props, "pixelformat",
  717. obs_module_text("VideoFormat"),
  718. OBS_COMBO_TYPE_LIST,
  719. OBS_COMBO_FORMAT_INT);
  720. resolution_list = obs_properties_add_list(props, "resolution",
  721. obs_module_text("Resolution"),
  722. OBS_COMBO_TYPE_LIST,
  723. OBS_COMBO_FORMAT_INT);
  724. obs_properties_add_list(props, "framerate",
  725. obs_module_text("FrameRate"),
  726. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  727. // a group to contain the camera control
  728. controls_props = obs_properties_create();
  729. obs_properties_add_group(props, "controls",
  730. obs_module_text("CameraControls"),
  731. OBS_GROUP_NORMAL, controls_props);
  732. populate_cameras_list(camera_source, device_list);
  733. obs_property_set_modified_callback2(device_list, device_selected,
  734. camera_source);
  735. obs_property_set_modified_callback2(format_list, format_selected,
  736. camera_source);
  737. obs_property_set_modified_callback2(resolution_list,
  738. resolution_selected, camera_source);
  739. return props;
  740. }
  741. static void pipewire_camera_update(void *data, obs_data_t *settings)
  742. {
  743. struct camera_portal_source *camera_source = data;
  744. const char *device_id;
  745. device_id = obs_data_get_string(settings, "device_id");
  746. blog(LOG_INFO, "[camera-portal] Updating device %s", device_id);
  747. if (update_device_id(camera_source, device_id))
  748. stream_camera(camera_source);
  749. }
  750. static void pipewire_camera_show(void *data)
  751. {
  752. struct camera_portal_source *camera_source = data;
  753. if (camera_source->obs_pw_stream)
  754. obs_pipewire_stream_show(camera_source->obs_pw_stream);
  755. }
  756. static void pipewire_camera_hide(void *data)
  757. {
  758. struct camera_portal_source *camera_source = data;
  759. if (camera_source->obs_pw_stream)
  760. obs_pipewire_stream_hide(camera_source->obs_pw_stream);
  761. }
  762. static uint32_t pipewire_camera_get_width(void *data)
  763. {
  764. struct camera_portal_source *camera_source = data;
  765. if (camera_source->obs_pw_stream)
  766. return obs_pipewire_stream_get_width(
  767. camera_source->obs_pw_stream);
  768. else
  769. return 0;
  770. }
  771. static uint32_t pipewire_camera_get_height(void *data)
  772. {
  773. struct camera_portal_source *camera_source = data;
  774. if (camera_source->obs_pw_stream)
  775. return obs_pipewire_stream_get_height(
  776. camera_source->obs_pw_stream);
  777. else
  778. return 0;
  779. }
  780. void camera_portal_load(void)
  781. {
  782. const struct obs_source_info pipewire_camera_info = {
  783. .id = "pipewire-camera-source",
  784. .type = OBS_SOURCE_TYPE_INPUT,
  785. .output_flags = OBS_SOURCE_ASYNC_VIDEO,
  786. .get_name = pipewire_camera_get_name,
  787. .create = pipewire_camera_create,
  788. .destroy = pipewire_camera_destroy,
  789. .get_defaults = pipewire_camera_get_defaults,
  790. .get_properties = pipewire_camera_get_properties,
  791. .update = pipewire_camera_update,
  792. .show = pipewire_camera_show,
  793. .hide = pipewire_camera_hide,
  794. .get_width = pipewire_camera_get_width,
  795. .get_height = pipewire_camera_get_height,
  796. .icon_type = OBS_ICON_TYPE_CAMERA,
  797. };
  798. obs_register_source(&pipewire_camera_info);
  799. }
  800. void camera_portal_unload(void)
  801. {
  802. g_clear_pointer(&connection, pw_portal_connection_free);
  803. }