camera-portal.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  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/node/keys.h>
  29. #include <spa/pod/iter.h>
  30. #include <spa/pod/parser.h>
  31. #include <spa/param/props.h>
  32. #include <spa/utils/defs.h>
  33. #include <spa/utils/keys.h>
  34. #include <spa/utils/result.h>
  35. struct camera_portal_source {
  36. obs_source_t *source;
  37. obs_data_t *settings;
  38. obs_pipewire_stream *obs_pw_stream;
  39. char *device_id;
  40. struct {
  41. struct spa_rectangle rect;
  42. bool set;
  43. } resolution;
  44. struct {
  45. struct spa_fraction fraction;
  46. bool set;
  47. } framerate;
  48. };
  49. /* ------------------------------------------------- */
  50. struct pw_portal_connection {
  51. obs_pipewire *obs_pw;
  52. GHashTable *devices;
  53. GCancellable *cancellable;
  54. GPtrArray *sources;
  55. bool initializing;
  56. };
  57. struct pw_portal_connection *connection = NULL;
  58. static void pw_portal_connection_free(struct pw_portal_connection *connection)
  59. {
  60. if (!connection)
  61. return;
  62. g_cancellable_cancel(connection->cancellable);
  63. g_clear_pointer(&connection->devices, g_hash_table_destroy);
  64. g_clear_pointer(&connection->obs_pw, obs_pipewire_destroy);
  65. g_clear_pointer(&connection->sources, g_ptr_array_unref);
  66. g_clear_object(&connection->cancellable);
  67. bfree(connection);
  68. }
  69. static GDBusProxy *camera_proxy = NULL;
  70. static void ensure_camera_portal_proxy(void)
  71. {
  72. g_autoptr(GError) error = NULL;
  73. if (!camera_proxy) {
  74. camera_proxy = g_dbus_proxy_new_sync(
  75. portal_get_dbus_connection(), G_DBUS_PROXY_FLAGS_NONE,
  76. NULL, "org.freedesktop.portal.Desktop",
  77. "/org/freedesktop/portal/desktop",
  78. "org.freedesktop.portal.Camera", NULL, &error);
  79. if (error) {
  80. blog(LOG_WARNING,
  81. "[portals] Error retrieving D-Bus proxy: %s",
  82. error->message);
  83. return;
  84. }
  85. }
  86. }
  87. static GDBusProxy *get_camera_portal_proxy(void)
  88. {
  89. ensure_camera_portal_proxy();
  90. return camera_proxy;
  91. }
  92. static uint32_t get_camera_version(void)
  93. {
  94. g_autoptr(GVariant) cached_version = NULL;
  95. uint32_t version;
  96. ensure_camera_portal_proxy();
  97. if (!camera_proxy)
  98. return 0;
  99. cached_version =
  100. g_dbus_proxy_get_cached_property(camera_proxy, "version");
  101. version = cached_version ? g_variant_get_uint32(cached_version) : 0;
  102. return version;
  103. }
  104. /* ------------------------------------------------- */
  105. struct camera_device {
  106. uint32_t id;
  107. struct pw_properties *properties;
  108. struct pw_proxy *proxy;
  109. struct spa_hook proxy_listener;
  110. struct pw_node *node;
  111. struct spa_hook node_listener;
  112. struct pw_node_info *info;
  113. uint32_t changed;
  114. struct spa_list pending_list;
  115. struct spa_list param_list;
  116. int pending_sync;
  117. };
  118. struct param {
  119. uint32_t id;
  120. int32_t seq;
  121. struct spa_list link;
  122. struct spa_pod *param;
  123. };
  124. static uint32_t clear_params(struct spa_list *param_list, uint32_t id)
  125. {
  126. struct param *p, *t;
  127. uint32_t count = 0;
  128. spa_list_for_each_safe(p, t, param_list, link)
  129. {
  130. if (id == SPA_ID_INVALID || p->id == id) {
  131. spa_list_remove(&p->link);
  132. free(p);
  133. count++;
  134. }
  135. }
  136. return count;
  137. }
  138. static struct param *add_param(struct spa_list *params, int seq, uint32_t id,
  139. const struct spa_pod *param)
  140. {
  141. struct param *p;
  142. if (id == SPA_ID_INVALID) {
  143. if (param == NULL || !spa_pod_is_object(param)) {
  144. errno = EINVAL;
  145. return NULL;
  146. }
  147. id = SPA_POD_OBJECT_ID(param);
  148. }
  149. p = malloc(sizeof(*p) + (param != NULL ? SPA_POD_SIZE(param) : 0));
  150. if (p == NULL)
  151. return NULL;
  152. p->id = id;
  153. p->seq = seq;
  154. if (param != NULL) {
  155. p->param = SPA_PTROFF(p, sizeof(*p), struct spa_pod);
  156. memcpy(p->param, param, SPA_POD_SIZE(param));
  157. } else {
  158. clear_params(params, id);
  159. p->param = NULL;
  160. }
  161. spa_list_append(params, &p->link);
  162. return p;
  163. }
  164. static void object_update_params(struct spa_list *param_list,
  165. struct spa_list *pending_list,
  166. uint32_t n_params,
  167. struct spa_param_info *params)
  168. {
  169. struct param *p, *t;
  170. uint32_t i;
  171. for (i = 0; i < n_params; i++) {
  172. spa_list_for_each_safe(p, t, pending_list, link)
  173. {
  174. if (p->id == params[i].id && p->seq != params[i].seq &&
  175. p->param != NULL) {
  176. spa_list_remove(&p->link);
  177. free(p);
  178. }
  179. }
  180. }
  181. spa_list_consume(p, pending_list, link)
  182. {
  183. spa_list_remove(&p->link);
  184. if (p->param == NULL) {
  185. clear_params(param_list, p->id);
  186. free(p);
  187. } else {
  188. spa_list_append(param_list, &p->link);
  189. }
  190. }
  191. }
  192. static struct camera_device *
  193. camera_device_new(uint32_t id, const struct spa_dict *properties)
  194. {
  195. struct camera_device *device = bzalloc(sizeof(struct camera_device));
  196. device->id = id;
  197. device->properties = properties ? pw_properties_new_dict(properties)
  198. : NULL;
  199. spa_list_init(&device->pending_list);
  200. spa_list_init(&device->param_list);
  201. return device;
  202. }
  203. static void camera_device_free(struct camera_device *device)
  204. {
  205. if (!device)
  206. return;
  207. clear_params(&device->pending_list, SPA_ID_INVALID);
  208. clear_params(&device->param_list, SPA_ID_INVALID);
  209. g_clear_pointer(&device->proxy, pw_proxy_destroy);
  210. g_clear_pointer(&device->properties, pw_properties_free);
  211. bfree(device);
  212. }
  213. /* ------------------------------------------------- */
  214. static bool update_device_id(struct camera_portal_source *camera_source,
  215. const char *new_device_id)
  216. {
  217. if (strcmp(camera_source->device_id, new_device_id) == 0)
  218. return false;
  219. g_clear_pointer(&camera_source->device_id, bfree);
  220. camera_source->device_id = bstrdup(new_device_id);
  221. return true;
  222. }
  223. static void stream_camera(struct camera_portal_source *camera_source)
  224. {
  225. struct obs_pipwire_connect_stream_info connect_info;
  226. const struct spa_rectangle *resolution = NULL;
  227. const struct spa_fraction *framerate = NULL;
  228. struct camera_device *device;
  229. g_clear_pointer(&camera_source->obs_pw_stream,
  230. obs_pipewire_stream_destroy);
  231. device = g_hash_table_lookup(connection->devices,
  232. camera_source->device_id);
  233. if (!device)
  234. return;
  235. blog(LOG_INFO, "[camera-portal] streaming camera '%s'",
  236. camera_source->device_id);
  237. if (camera_source->resolution.set)
  238. resolution = &camera_source->resolution.rect;
  239. if (camera_source->framerate.set)
  240. framerate = &camera_source->framerate.fraction;
  241. connect_info = (struct obs_pipwire_connect_stream_info){
  242. .stream_name = "OBS PipeWire Camera",
  243. .stream_properties = pw_properties_new(
  244. PW_KEY_MEDIA_TYPE, "Video", PW_KEY_MEDIA_CATEGORY,
  245. "Capture", PW_KEY_MEDIA_ROLE, "Camera", NULL),
  246. .video = {
  247. .resolution = resolution,
  248. .framerate = framerate,
  249. }};
  250. camera_source->obs_pw_stream = obs_pipewire_connect_stream(
  251. connection->obs_pw, camera_source->source, device->id,
  252. &connect_info);
  253. }
  254. static void camera_format_list(struct camera_device *dev, obs_property_t *prop)
  255. {
  256. struct param *p;
  257. enum video_format last_format = VIDEO_FORMAT_NONE;
  258. obs_property_list_clear(prop);
  259. spa_list_for_each(p, &dev->param_list, link)
  260. {
  261. struct obs_pw_video_format obs_pw_video_format;
  262. uint32_t media_type, media_subtype, format;
  263. if (p->id != SPA_PARAM_EnumFormat || p->param == NULL)
  264. continue;
  265. if (spa_format_parse(p->param, &media_type, &media_subtype) < 0)
  266. continue;
  267. if (media_type != SPA_MEDIA_TYPE_video)
  268. continue;
  269. if (media_subtype == SPA_MEDIA_SUBTYPE_raw) {
  270. if (spa_pod_parse_object(p->param,
  271. SPA_TYPE_OBJECT_Format, NULL,
  272. SPA_FORMAT_VIDEO_format,
  273. SPA_POD_Id(&format)) < 0)
  274. continue;
  275. } else {
  276. format = SPA_VIDEO_FORMAT_ENCODED;
  277. }
  278. if (!obs_pw_video_format_from_spa_format(format,
  279. &obs_pw_video_format))
  280. continue;
  281. if (obs_pw_video_format.video_format == last_format)
  282. continue;
  283. last_format = obs_pw_video_format.video_format;
  284. obs_property_list_add_int(prop, obs_pw_video_format.pretty_name,
  285. format);
  286. }
  287. }
  288. static bool control_changed(void *data, obs_properties_t *props,
  289. obs_property_t *prop, obs_data_t *settings)
  290. {
  291. UNUSED_PARAMETER(props);
  292. struct camera_device *dev;
  293. const char *device_id;
  294. uint32_t id;
  295. char buf[1024];
  296. struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buf, sizeof(buf));
  297. struct spa_pod_frame f[1];
  298. struct spa_pod *param;
  299. device_id = obs_data_get_string(settings, "device_id");
  300. dev = g_hash_table_lookup(connection->devices, device_id);
  301. if (dev == NULL) {
  302. blog(LOG_ERROR, "unknown device %s", device_id);
  303. return false;
  304. }
  305. id = SPA_PTR_TO_UINT32(data);
  306. spa_pod_builder_push_object(&b, &f[0], SPA_TYPE_OBJECT_Props,
  307. SPA_PARAM_Props);
  308. switch (obs_property_get_type(prop)) {
  309. case OBS_PROPERTY_BOOL: {
  310. bool val = obs_data_get_bool(settings, obs_property_name(prop));
  311. spa_pod_builder_add(&b, id, SPA_POD_Bool(val), 0);
  312. break;
  313. }
  314. case OBS_PROPERTY_FLOAT: {
  315. float val =
  316. obs_data_get_double(settings, obs_property_name(prop));
  317. spa_pod_builder_add(&b, id, SPA_POD_Float(val), 0);
  318. break;
  319. }
  320. case OBS_PROPERTY_INT:
  321. case OBS_PROPERTY_LIST: {
  322. int val = obs_data_get_int(settings, obs_property_name(prop));
  323. spa_pod_builder_add(&b, id, SPA_POD_Int(val), 0);
  324. break;
  325. }
  326. default:
  327. blog(LOG_ERROR, "unknown property type for %s",
  328. obs_property_name(prop));
  329. return false;
  330. }
  331. param = spa_pod_builder_pop(&b, &f[0]);
  332. pw_node_set_param((struct pw_node *)dev->proxy, SPA_PARAM_Props, 0,
  333. param);
  334. return true;
  335. }
  336. static inline void add_control_property(obs_properties_t *props,
  337. obs_data_t *settings,
  338. struct camera_device *dev,
  339. struct param *p)
  340. {
  341. UNUSED_PARAMETER(dev);
  342. const struct spa_pod *type, *pod, *labels = NULL;
  343. uint32_t id, n_vals, choice, container = SPA_ID_INVALID;
  344. obs_property_t *prop = NULL;
  345. const char *name;
  346. if (spa_pod_parse_object(
  347. p->param, SPA_TYPE_OBJECT_PropInfo, NULL, SPA_PROP_INFO_id,
  348. SPA_POD_Id(&id), SPA_PROP_INFO_description,
  349. SPA_POD_OPT_String(&name), SPA_PROP_INFO_type,
  350. SPA_POD_PodChoice(&type), SPA_PROP_INFO_container,
  351. SPA_POD_OPT_Id(&container), SPA_PROP_INFO_labels,
  352. SPA_POD_OPT_PodStruct(&labels)) < 0)
  353. return;
  354. pod = spa_pod_get_values(type, &n_vals, &choice);
  355. container = container != SPA_ID_INVALID ? container : SPA_POD_TYPE(pod);
  356. switch (SPA_POD_TYPE(pod)) {
  357. case SPA_TYPE_Int: {
  358. int32_t *vals = SPA_POD_BODY(pod);
  359. int32_t min, max, def, step;
  360. if (n_vals < 1)
  361. return;
  362. def = vals[0];
  363. if (choice == SPA_CHOICE_Enum) {
  364. struct spa_pod_parser prs;
  365. struct spa_pod_frame f;
  366. if (labels == NULL)
  367. return;
  368. prop = obs_properties_add_list(props, (char *)name,
  369. (char *)name,
  370. OBS_COMBO_TYPE_LIST,
  371. OBS_COMBO_FORMAT_INT);
  372. spa_pod_parser_pod(&prs, (struct spa_pod *)labels);
  373. if (spa_pod_parser_push_struct(&prs, &f) < 0)
  374. return;
  375. while (1) {
  376. int32_t id;
  377. const char *desc;
  378. if (spa_pod_parser_get_int(&prs, &id) < 0 ||
  379. spa_pod_parser_get_string(&prs, &desc) < 0)
  380. break;
  381. obs_property_list_add_int(prop, (char *)desc,
  382. id);
  383. }
  384. } else {
  385. min = n_vals > 1 ? vals[1] : def;
  386. max = n_vals > 2 ? vals[2] : def;
  387. step = n_vals > 3 ? vals[3] : (max - min) / 256.0f;
  388. prop = obs_properties_add_int_slider(props,
  389. (char *)name,
  390. (char *)name, min,
  391. max, step);
  392. }
  393. obs_data_set_default_int(settings, (char *)name, def);
  394. obs_property_set_modified_callback2(prop, control_changed,
  395. SPA_UINT32_TO_PTR(id));
  396. break;
  397. }
  398. case SPA_TYPE_Bool: {
  399. int32_t *vals = SPA_POD_BODY(pod);
  400. if (n_vals < 1)
  401. return;
  402. prop = obs_properties_add_bool(props, (char *)name,
  403. (char *)name);
  404. obs_data_set_default_bool(settings, (char *)name, vals[0]);
  405. obs_property_set_modified_callback2(prop, control_changed,
  406. SPA_UINT32_TO_PTR(id));
  407. break;
  408. }
  409. case SPA_TYPE_Float: {
  410. float *vals = SPA_POD_BODY(pod);
  411. float min, max, def, step;
  412. if (n_vals < 1)
  413. return;
  414. def = vals[0];
  415. min = n_vals > 1 ? vals[1] : def;
  416. max = n_vals > 2 ? vals[2] : def;
  417. step = n_vals > 3 ? vals[3] : (max - min) / 256.0f;
  418. prop = obs_properties_add_float_slider(
  419. props, (char *)name, (char *)name, min, max, step);
  420. obs_data_set_default_double(settings, (char *)name, def);
  421. obs_property_set_modified_callback2(prop, control_changed,
  422. SPA_UINT32_TO_PTR(id));
  423. break;
  424. }
  425. default:
  426. break;
  427. }
  428. }
  429. static void camera_update_controls(struct camera_device *dev,
  430. obs_properties_t *props,
  431. obs_data_t *settings)
  432. {
  433. struct param *p;
  434. spa_list_for_each(p, &dev->param_list, link)
  435. {
  436. if (p->id != SPA_PARAM_PropInfo || p->param == NULL)
  437. continue;
  438. add_control_property(props, settings, dev, p);
  439. }
  440. }
  441. static bool device_selected(void *data, obs_properties_t *props,
  442. obs_property_t *property, obs_data_t *settings)
  443. {
  444. UNUSED_PARAMETER(props);
  445. UNUSED_PARAMETER(property);
  446. struct camera_portal_source *camera_source = data;
  447. const char *device_id;
  448. struct camera_device *device;
  449. obs_properties_t *new_control_properties;
  450. device_id = obs_data_get_string(settings, "device_id");
  451. blog(LOG_INFO, "[camera-portal] selected camera '%s'", device_id);
  452. device = g_hash_table_lookup(connection->devices, device_id);
  453. if (device == NULL)
  454. return false;
  455. if (update_device_id(camera_source, device_id))
  456. stream_camera(camera_source);
  457. blog(LOG_INFO, "[camera-portal] Updating pixel formats");
  458. property = obs_properties_get(props, "pixelformat");
  459. new_control_properties = obs_properties_create();
  460. obs_properties_remove_by_name(props, "controls");
  461. camera_format_list(device, property);
  462. camera_update_controls(device, new_control_properties, settings);
  463. obs_properties_add_group(props, "controls",
  464. obs_module_text("CameraControls"),
  465. OBS_GROUP_NORMAL, new_control_properties);
  466. obs_property_modified(property, settings);
  467. return true;
  468. }
  469. static int sort_resolutions(gconstpointer a, gconstpointer b)
  470. {
  471. const struct spa_rectangle *resolution_a = a;
  472. const struct spa_rectangle *resolution_b = b;
  473. int64_t area_a = resolution_a->width * resolution_a->height;
  474. int64_t area_b = resolution_b->width * resolution_b->height;
  475. return area_a - area_b;
  476. }
  477. static void resolution_list(struct camera_device *dev, uint32_t pixelformat,
  478. obs_property_t *prop)
  479. {
  480. struct spa_rectangle last_resolution = SPA_RECTANGLE(0, 0);
  481. g_autoptr(GArray) resolutions = NULL;
  482. struct param *p;
  483. obs_data_t *data;
  484. resolutions = g_array_new(FALSE, FALSE, sizeof(struct spa_rectangle));
  485. spa_list_for_each(p, &dev->param_list, link)
  486. {
  487. struct obs_pw_video_format obs_pw_video_format;
  488. struct spa_rectangle resolution;
  489. uint32_t media_type, media_subtype, format;
  490. if (p->id != SPA_PARAM_EnumFormat || p->param == NULL)
  491. continue;
  492. if (spa_format_parse(p->param, &media_type, &media_subtype) < 0)
  493. continue;
  494. if (media_type != SPA_MEDIA_TYPE_video)
  495. continue;
  496. if (media_subtype == SPA_MEDIA_SUBTYPE_raw) {
  497. if (spa_pod_parse_object(p->param,
  498. SPA_TYPE_OBJECT_Format, NULL,
  499. SPA_FORMAT_VIDEO_format,
  500. SPA_POD_Id(&format)) < 0)
  501. continue;
  502. } else {
  503. format = SPA_VIDEO_FORMAT_ENCODED;
  504. }
  505. if (!obs_pw_video_format_from_spa_format(format,
  506. &obs_pw_video_format))
  507. continue;
  508. if (obs_pw_video_format.video_format != pixelformat)
  509. continue;
  510. if (spa_pod_parse_object(p->param, SPA_TYPE_OBJECT_Format, NULL,
  511. SPA_FORMAT_VIDEO_size,
  512. SPA_POD_OPT_Rectangle(&resolution)) <
  513. 0)
  514. continue;
  515. if (resolution.width == last_resolution.width &&
  516. resolution.height == last_resolution.height)
  517. continue;
  518. last_resolution = resolution;
  519. g_array_append_val(resolutions, resolution);
  520. }
  521. g_array_sort(resolutions, sort_resolutions);
  522. obs_property_list_clear(prop);
  523. data = obs_data_create();
  524. for (size_t i = 0; i < resolutions->len; i++) {
  525. const struct spa_rectangle *resolution =
  526. &g_array_index(resolutions, struct spa_rectangle, i);
  527. struct dstr str = {};
  528. dstr_printf(&str, "%ux%u", resolution->width,
  529. resolution->height);
  530. obs_data_set_int(data, "width", resolution->width);
  531. obs_data_set_int(data, "height", resolution->height);
  532. obs_property_list_add_string(prop, str.array,
  533. obs_data_get_json(data));
  534. dstr_free(&str);
  535. }
  536. obs_data_release(data);
  537. }
  538. /*
  539. * Format selected callback
  540. */
  541. static bool format_selected(void *data, obs_properties_t *properties,
  542. obs_property_t *property, obs_data_t *settings)
  543. {
  544. UNUSED_PARAMETER(property);
  545. UNUSED_PARAMETER(settings);
  546. struct camera_portal_source *camera_source = data;
  547. struct camera_device *device;
  548. obs_property_t *resolution;
  549. blog(LOG_INFO, "[camera-portal] Selected format for '%s'",
  550. camera_source->device_id);
  551. device = g_hash_table_lookup(connection->devices,
  552. camera_source->device_id);
  553. if (device == NULL)
  554. return false;
  555. resolution = obs_properties_get(properties, "resolution");
  556. resolution_list(device, obs_data_get_int(settings, "pixelformat"),
  557. resolution);
  558. return true;
  559. }
  560. static int compare_framerates(gconstpointer a, gconstpointer b)
  561. {
  562. const struct spa_fraction *framerate_a = a;
  563. const struct spa_fraction *framerate_b = b;
  564. double da = framerate_a->num / (double)framerate_a->denom;
  565. double db = framerate_b->num / (double)framerate_b->denom;
  566. return da - db;
  567. }
  568. static void framerate_list(struct camera_device *dev, uint32_t pixelformat,
  569. const struct spa_rectangle *resolution,
  570. obs_property_t *prop)
  571. {
  572. g_autoptr(GHashTable) framerates_map = NULL;
  573. g_autoptr(GArray) framerates = NULL;
  574. struct param *p;
  575. obs_data_t *data;
  576. framerates = g_array_new(FALSE, FALSE, sizeof(struct spa_fraction));
  577. spa_list_for_each(p, &dev->param_list, link)
  578. {
  579. const struct spa_fraction *framerate_values;
  580. struct obs_pw_video_format obs_pw_video_format;
  581. enum spa_choice_type choice;
  582. const struct spa_pod_prop *prop;
  583. struct spa_pod_parser pod_parser;
  584. struct spa_rectangle this_resolution;
  585. struct spa_fraction framerate;
  586. struct spa_pod *framerate_pod;
  587. uint32_t media_subtype;
  588. uint32_t media_type;
  589. uint32_t n_framerates;
  590. uint32_t format;
  591. if (p->id != SPA_PARAM_EnumFormat || p->param == NULL)
  592. continue;
  593. if (spa_format_parse(p->param, &media_type, &media_subtype) < 0)
  594. continue;
  595. if (media_type != SPA_MEDIA_TYPE_video)
  596. continue;
  597. if (media_subtype == SPA_MEDIA_SUBTYPE_raw) {
  598. if (spa_pod_parse_object(p->param,
  599. SPA_TYPE_OBJECT_Format, NULL,
  600. SPA_FORMAT_VIDEO_format,
  601. SPA_POD_Id(&format)) < 0)
  602. continue;
  603. } else {
  604. format = SPA_VIDEO_FORMAT_ENCODED;
  605. }
  606. if (!obs_pw_video_format_from_spa_format(format,
  607. &obs_pw_video_format))
  608. continue;
  609. if (obs_pw_video_format.video_format != pixelformat)
  610. continue;
  611. if (spa_pod_parse_object(
  612. p->param, SPA_TYPE_OBJECT_Format, NULL,
  613. SPA_FORMAT_VIDEO_size,
  614. SPA_POD_OPT_Rectangle(&this_resolution)) < 0)
  615. continue;
  616. if (this_resolution.width != resolution->width ||
  617. this_resolution.height != resolution->height)
  618. continue;
  619. prop = spa_pod_find_prop(p->param, NULL,
  620. SPA_FORMAT_VIDEO_framerate);
  621. if (!prop)
  622. continue;
  623. framerate_pod = spa_pod_get_values(&prop->value, &n_framerates,
  624. &choice);
  625. if (framerate_pod->type != SPA_TYPE_Fraction) {
  626. blog(LOG_WARNING,
  627. "Framerate is not a fraction - something is wrong");
  628. continue;
  629. }
  630. framerate_values = SPA_POD_BODY(framerate_pod);
  631. switch (choice) {
  632. case SPA_CHOICE_None:
  633. g_array_append_val(framerates, framerate_values[0]);
  634. break;
  635. case SPA_CHOICE_Range:
  636. blog(LOG_WARNING, "Ranged framerates not supported");
  637. break;
  638. case SPA_CHOICE_Step:
  639. blog(LOG_WARNING, "Stepped framerates not supported");
  640. break;
  641. case SPA_CHOICE_Enum:
  642. /* i=0 is the default framerate, skip it */
  643. for (uint32_t i = 1; i < n_framerates; i++)
  644. g_array_append_val(framerates,
  645. framerate_values[i]);
  646. break;
  647. default:
  648. break;
  649. }
  650. }
  651. g_array_sort(framerates, compare_framerates);
  652. obs_property_list_clear(prop);
  653. data = obs_data_create();
  654. for (size_t i = 0; i < framerates->len; i++) {
  655. const struct spa_fraction *framerate =
  656. &g_array_index(framerates, struct spa_fraction, i);
  657. struct media_frames_per_second fps;
  658. struct dstr str = {};
  659. fps = (struct media_frames_per_second){
  660. .numerator = framerate->num,
  661. .denominator = framerate->denom,
  662. };
  663. obs_data_set_frames_per_second(data, "framerate", fps, NULL);
  664. dstr_printf(&str, "%.2f",
  665. framerate->num / (double)framerate->denom);
  666. obs_property_list_add_string(prop, str.array,
  667. obs_data_get_json(data));
  668. dstr_free(&str);
  669. }
  670. obs_data_release(data);
  671. }
  672. static bool parse_framerate(struct spa_fraction *dest, const char *json)
  673. {
  674. struct media_frames_per_second fps;
  675. obs_data_t *data = obs_data_create_from_json(json);
  676. if (!data)
  677. return false;
  678. if (!obs_data_get_frames_per_second(data, "framerate", &fps, NULL)) {
  679. obs_data_release(data);
  680. return false;
  681. }
  682. dest->num = fps.numerator;
  683. dest->denom = fps.denominator;
  684. obs_data_release(data);
  685. return true;
  686. }
  687. static bool framerate_selected(void *data, obs_properties_t *properties,
  688. obs_property_t *property, obs_data_t *settings)
  689. {
  690. UNUSED_PARAMETER(properties);
  691. UNUSED_PARAMETER(property);
  692. struct camera_portal_source *camera_source = data;
  693. struct camera_device *device;
  694. struct spa_fraction framerate;
  695. device = g_hash_table_lookup(connection->devices,
  696. camera_source->device_id);
  697. if (device == NULL)
  698. return false;
  699. if (!parse_framerate(&framerate,
  700. obs_data_get_string(settings, "framerate")))
  701. return false;
  702. if (camera_source->obs_pw_stream)
  703. obs_pipewire_stream_set_framerate(camera_source->obs_pw_stream,
  704. &framerate);
  705. return true;
  706. }
  707. /*
  708. * Resolution selected callback
  709. */
  710. static bool parse_resolution(struct spa_rectangle *dest, const char *json)
  711. {
  712. obs_data_t *data = obs_data_create_from_json(json);
  713. if (!data)
  714. return false;
  715. dest->width = obs_data_get_int(data, "width");
  716. dest->height = obs_data_get_int(data, "height");
  717. obs_data_release(data);
  718. return true;
  719. }
  720. static bool resolution_selected(void *data, obs_properties_t *properties,
  721. obs_property_t *property, obs_data_t *settings)
  722. {
  723. UNUSED_PARAMETER(properties);
  724. UNUSED_PARAMETER(property);
  725. UNUSED_PARAMETER(settings);
  726. struct camera_portal_source *camera_source = data;
  727. struct spa_rectangle resolution;
  728. struct camera_device *device;
  729. blog(LOG_INFO, "[camera-portal] Selected resolution for '%s'",
  730. camera_source->device_id);
  731. device = g_hash_table_lookup(connection->devices,
  732. camera_source->device_id);
  733. if (device == NULL)
  734. return false;
  735. if (!parse_resolution(&resolution,
  736. obs_data_get_string(settings, "resolution")))
  737. return false;
  738. if (camera_source->obs_pw_stream)
  739. obs_pipewire_stream_set_resolution(camera_source->obs_pw_stream,
  740. &resolution);
  741. property = obs_properties_get(properties, "framerate");
  742. framerate_list(device, obs_data_get_int(settings, "pixelformat"),
  743. &resolution, property);
  744. return true;
  745. }
  746. static void populate_cameras_list(struct camera_portal_source *camera_source,
  747. obs_property_t *device_list)
  748. {
  749. struct camera_device *device;
  750. GHashTableIter iter;
  751. const char *device_id;
  752. bool device_found;
  753. if (!connection)
  754. return;
  755. obs_property_list_clear(device_list);
  756. device_found = false;
  757. g_hash_table_iter_init(&iter, connection->devices);
  758. while (g_hash_table_iter_next(&iter, (gpointer *)&device_id,
  759. (gpointer *)&device)) {
  760. const char *device_name;
  761. device_name = pw_properties_get(device->properties,
  762. PW_KEY_NODE_DESCRIPTION);
  763. obs_property_list_add_string(device_list, device_name,
  764. device_id);
  765. device_found |= strcmp(device_id, camera_source->device_id) ==
  766. 0;
  767. }
  768. if (!device_found && camera_source->device_id) {
  769. size_t device_index;
  770. device_index = obs_property_list_add_string(
  771. device_list, camera_source->device_id,
  772. camera_source->device_id);
  773. obs_property_list_item_disable(device_list, device_index, true);
  774. }
  775. }
  776. /* ------------------------------------------------- */
  777. static void node_info(void *data, const struct pw_node_info *info)
  778. {
  779. struct camera_device *device = data;
  780. uint32_t i, changed = 0;
  781. int res;
  782. info = device->info = pw_node_info_update(device->info, info);
  783. if (info == NULL)
  784. return;
  785. if (info->change_mask & PW_NODE_CHANGE_MASK_PARAMS) {
  786. for (i = 0; i < info->n_params; i++) {
  787. uint32_t id = info->params[i].id;
  788. if (info->params[i].user == 0)
  789. continue;
  790. info->params[i].user = 0;
  791. changed++;
  792. add_param(&device->pending_list, 0, id, NULL);
  793. if (!(info->params[i].flags & SPA_PARAM_INFO_READ))
  794. continue;
  795. res = pw_node_enum_params(
  796. (struct pw_node *)device->proxy,
  797. ++info->params[i].seq, id, 0, -1, NULL);
  798. if (SPA_RESULT_IS_ASYNC(res))
  799. info->params[i].seq = res;
  800. }
  801. }
  802. if (changed) {
  803. device->changed += changed;
  804. device->pending_sync =
  805. pw_proxy_sync(device->proxy, device->pending_sync);
  806. }
  807. }
  808. static void node_param(void *data, int seq, uint32_t id, uint32_t index,
  809. uint32_t next, const struct spa_pod *param)
  810. {
  811. UNUSED_PARAMETER(index);
  812. UNUSED_PARAMETER(next);
  813. struct camera_device *device = data;
  814. add_param(&device->pending_list, seq, id, param);
  815. }
  816. static const struct pw_node_events node_events = {
  817. PW_VERSION_NODE_EVENTS,
  818. .info = node_info,
  819. .param = node_param,
  820. };
  821. static void on_proxy_removed_cb(void *data)
  822. {
  823. struct camera_device *device = data;
  824. pw_proxy_destroy(device->proxy);
  825. }
  826. static void on_destroy_proxy_cb(void *data)
  827. {
  828. struct camera_device *device = data;
  829. spa_hook_remove(&device->proxy_listener);
  830. device->proxy = NULL;
  831. }
  832. static void on_done_proxy_cb(void *data, int seq)
  833. {
  834. struct camera_device *device = data;
  835. if (device->info != NULL && device->pending_sync == seq) {
  836. object_update_params(&device->param_list, &device->pending_list,
  837. device->info->n_params,
  838. device->info->params);
  839. }
  840. }
  841. static const struct pw_proxy_events proxy_events = {
  842. PW_VERSION_PROXY_EVENTS,
  843. .removed = on_proxy_removed_cb,
  844. .destroy = on_destroy_proxy_cb,
  845. .done = on_done_proxy_cb,
  846. };
  847. static void on_registry_global_cb(void *user_data, uint32_t id,
  848. uint32_t permissions, const char *type,
  849. uint32_t version,
  850. const struct spa_dict *props)
  851. {
  852. UNUSED_PARAMETER(user_data);
  853. UNUSED_PARAMETER(permissions);
  854. UNUSED_PARAMETER(version);
  855. struct camera_device *device;
  856. struct pw_registry *registry;
  857. const char *device_id;
  858. if (strcmp(type, PW_TYPE_INTERFACE_Node) != 0)
  859. return;
  860. registry = obs_pipewire_get_registry(connection->obs_pw);
  861. device_id = spa_dict_lookup(props, SPA_KEY_NODE_NAME);
  862. blog(LOG_INFO, "[camera-portal] Found device %s", device_id);
  863. device = camera_device_new(id, props);
  864. device->proxy = pw_registry_bind(registry, id, type, version, 0);
  865. if (!device->proxy) {
  866. blog(LOG_WARNING, "[camera-portal] Failed to bind device %s",
  867. device_id);
  868. bfree(device);
  869. return;
  870. }
  871. pw_proxy_add_listener(device->proxy, &device->proxy_listener,
  872. &proxy_events, device);
  873. device->node = (struct pw_node *)device->proxy;
  874. pw_node_add_listener(device->node, &device->node_listener, &node_events,
  875. device);
  876. g_hash_table_insert(connection->devices, bstrdup(device_id), device);
  877. for (size_t i = 0; i < connection->sources->len; i++) {
  878. struct camera_portal_source *camera_source =
  879. g_ptr_array_index(connection->sources, i);
  880. obs_source_update_properties(camera_source->source);
  881. if (strcmp(camera_source->device_id, device_id) == 0)
  882. stream_camera(camera_source);
  883. }
  884. }
  885. static void on_registry_global_remove_cb(void *user_data, uint32_t id)
  886. {
  887. UNUSED_PARAMETER(user_data);
  888. struct camera_device *device;
  889. const char *device_id;
  890. GHashTableIter iter;
  891. g_hash_table_iter_init(&iter, connection->devices);
  892. while (g_hash_table_iter_next(&iter, (gpointer *)&device_id,
  893. (gpointer *)&device)) {
  894. if (device->id != id)
  895. continue;
  896. g_hash_table_iter_remove(&iter);
  897. blog(LOG_INFO, "[pipewire-camera] Removed device %s",
  898. device_id);
  899. }
  900. for (size_t i = 0; i < connection->sources->len; i++) {
  901. struct camera_portal_source *camera_source =
  902. g_ptr_array_index(connection->sources, i);
  903. obs_source_update_properties(camera_source->source);
  904. }
  905. }
  906. static const struct pw_registry_events registry_events = {
  907. PW_VERSION_REGISTRY_EVENTS,
  908. .global = on_registry_global_cb,
  909. .global_remove = on_registry_global_remove_cb,
  910. };
  911. /* ------------------------------------------------- */
  912. static void on_pipewire_remote_opened_cb(GObject *source, GAsyncResult *res,
  913. void *user_data)
  914. {
  915. UNUSED_PARAMETER(user_data);
  916. g_autoptr(GUnixFDList) fd_list = NULL;
  917. g_autoptr(GVariant) result = NULL;
  918. g_autoptr(GError) error = NULL;
  919. int pipewire_fd;
  920. int fd_index;
  921. result = g_dbus_proxy_call_with_unix_fd_list_finish(
  922. G_DBUS_PROXY(source), &fd_list, res, &error);
  923. if (error) {
  924. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  925. blog(LOG_ERROR,
  926. "[camera-portal] Error retrieving PipeWire fd: %s",
  927. error->message);
  928. return;
  929. }
  930. g_variant_get(result, "(h)", &fd_index, &error);
  931. pipewire_fd = g_unix_fd_list_get(fd_list, fd_index, &error);
  932. if (error) {
  933. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  934. blog(LOG_ERROR,
  935. "[camera-portal] Error retrieving PipeWire fd: %s",
  936. error->message);
  937. return;
  938. }
  939. connection->obs_pw = obs_pipewire_connect_fd(
  940. pipewire_fd, &registry_events, connection);
  941. obs_pipewire_roundtrip(connection->obs_pw);
  942. }
  943. static void open_pipewire_remote(void)
  944. {
  945. GVariantBuilder builder;
  946. g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
  947. g_dbus_proxy_call_with_unix_fd_list(get_camera_portal_proxy(),
  948. "OpenPipeWireRemote",
  949. g_variant_new("(a{sv})", &builder),
  950. G_DBUS_CALL_FLAGS_NONE, -1, NULL,
  951. connection->cancellable,
  952. on_pipewire_remote_opened_cb, NULL);
  953. }
  954. /* ------------------------------------------------- */
  955. static void on_access_camera_response_received_cb(GVariant *parameters,
  956. void *user_data)
  957. {
  958. UNUSED_PARAMETER(user_data);
  959. g_autoptr(GVariant) result = NULL;
  960. uint32_t response;
  961. g_variant_get(parameters, "(u@a{sv})", &response, &result);
  962. if (response != 0) {
  963. blog(LOG_WARNING,
  964. "[camera-portal] Failed to create session, denied or cancelled by user");
  965. return;
  966. }
  967. blog(LOG_INFO, "[camera-portal] Successfully accessed cameras");
  968. open_pipewire_remote();
  969. }
  970. static void on_access_camera_finished_cb(GObject *source, GAsyncResult *res,
  971. void *user_data)
  972. {
  973. UNUSED_PARAMETER(user_data);
  974. g_autoptr(GVariant) result = NULL;
  975. g_autoptr(GError) error = NULL;
  976. result = g_dbus_proxy_call_finish(G_DBUS_PROXY(source), res, &error);
  977. if (error) {
  978. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  979. blog(LOG_ERROR,
  980. "[camera-portal] Error accessing camera: %s",
  981. error->message);
  982. return;
  983. }
  984. }
  985. static void access_camera(struct camera_portal_source *camera_source)
  986. {
  987. GVariantBuilder builder;
  988. char *request_token;
  989. char *request_path;
  990. if (connection && connection->obs_pw) {
  991. stream_camera(camera_source);
  992. return;
  993. }
  994. if (!connection) {
  995. connection = bzalloc(sizeof(struct pw_portal_connection));
  996. connection->devices = g_hash_table_new_full(
  997. g_str_hash, g_str_equal, bfree,
  998. (GDestroyNotify)camera_device_free);
  999. connection->cancellable = g_cancellable_new();
  1000. connection->sources = g_ptr_array_new();
  1001. connection->initializing = false;
  1002. }
  1003. g_ptr_array_add(connection->sources, camera_source);
  1004. if (connection->initializing)
  1005. return;
  1006. portal_create_request_path(&request_path, &request_token);
  1007. portal_signal_subscribe(request_path, NULL,
  1008. on_access_camera_response_received_cb, NULL);
  1009. g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
  1010. g_variant_builder_add(&builder, "{sv}", "handle_token",
  1011. g_variant_new_string(request_token));
  1012. g_dbus_proxy_call(get_camera_portal_proxy(), "AccessCamera",
  1013. g_variant_new("(a{sv})", &builder),
  1014. G_DBUS_CALL_FLAGS_NONE, -1, connection->cancellable,
  1015. on_access_camera_finished_cb, NULL);
  1016. connection->initializing = true;
  1017. bfree(request_token);
  1018. bfree(request_path);
  1019. }
  1020. /* obs_source_info methods */
  1021. static const char *pipewire_camera_get_name(void *data)
  1022. {
  1023. UNUSED_PARAMETER(data);
  1024. return obs_module_text("PipeWireCamera");
  1025. }
  1026. static void *pipewire_camera_create(obs_data_t *settings, obs_source_t *source)
  1027. {
  1028. struct camera_portal_source *camera_source;
  1029. bool set;
  1030. camera_source = bzalloc(sizeof(struct camera_portal_source));
  1031. camera_source->source = source;
  1032. camera_source->device_id =
  1033. bstrdup(obs_data_get_string(settings, "device_id"));
  1034. camera_source->framerate.set =
  1035. parse_framerate(&camera_source->framerate.fraction,
  1036. obs_data_get_string(settings, "framerate"));
  1037. camera_source->resolution.set =
  1038. parse_resolution(&camera_source->resolution.rect,
  1039. obs_data_get_string(settings, "resolution"));
  1040. access_camera(camera_source);
  1041. return camera_source;
  1042. }
  1043. static void pipewire_camera_destroy(void *data)
  1044. {
  1045. struct camera_portal_source *camera_source = data;
  1046. if (connection)
  1047. g_ptr_array_remove(connection->sources, camera_source);
  1048. g_clear_pointer(&camera_source->obs_pw_stream,
  1049. obs_pipewire_stream_destroy);
  1050. g_clear_pointer(&camera_source->device_id, bfree);
  1051. bfree(camera_source);
  1052. }
  1053. static void pipewire_camera_get_defaults(obs_data_t *settings)
  1054. {
  1055. obs_data_set_default_string(settings, "device_id", NULL);
  1056. }
  1057. static obs_properties_t *pipewire_camera_get_properties(void *data)
  1058. {
  1059. struct camera_portal_source *camera_source = data;
  1060. obs_properties_t *controls_props;
  1061. obs_properties_t *props;
  1062. obs_property_t *resolution_list;
  1063. obs_property_t *framerate_list;
  1064. obs_property_t *device_list;
  1065. obs_property_t *format_list;
  1066. props = obs_properties_create();
  1067. device_list = obs_properties_add_list(
  1068. props, "device_id", obs_module_text("PipeWireCameraDevice"),
  1069. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  1070. format_list = obs_properties_add_list(props, "pixelformat",
  1071. obs_module_text("VideoFormat"),
  1072. OBS_COMBO_TYPE_LIST,
  1073. OBS_COMBO_FORMAT_INT);
  1074. resolution_list = obs_properties_add_list(props, "resolution",
  1075. obs_module_text("Resolution"),
  1076. OBS_COMBO_TYPE_LIST,
  1077. OBS_COMBO_FORMAT_STRING);
  1078. framerate_list = obs_properties_add_list(props, "framerate",
  1079. obs_module_text("FrameRate"),
  1080. OBS_COMBO_TYPE_LIST,
  1081. OBS_COMBO_FORMAT_STRING);
  1082. // a group to contain the camera control
  1083. controls_props = obs_properties_create();
  1084. obs_properties_add_group(props, "controls",
  1085. obs_module_text("CameraControls"),
  1086. OBS_GROUP_NORMAL, controls_props);
  1087. populate_cameras_list(camera_source, device_list);
  1088. obs_property_set_modified_callback2(device_list, device_selected,
  1089. camera_source);
  1090. obs_property_set_modified_callback2(format_list, format_selected,
  1091. camera_source);
  1092. obs_property_set_modified_callback2(resolution_list,
  1093. resolution_selected, camera_source);
  1094. obs_property_set_modified_callback2(framerate_list, framerate_selected,
  1095. camera_source);
  1096. return props;
  1097. }
  1098. static void pipewire_camera_update(void *data, obs_data_t *settings)
  1099. {
  1100. struct camera_portal_source *camera_source = data;
  1101. const char *device_id;
  1102. device_id = obs_data_get_string(settings, "device_id");
  1103. blog(LOG_INFO, "[camera-portal] Updating device %s", device_id);
  1104. if (update_device_id(camera_source, device_id))
  1105. stream_camera(camera_source);
  1106. }
  1107. static void pipewire_camera_show(void *data)
  1108. {
  1109. struct camera_portal_source *camera_source = data;
  1110. if (camera_source->obs_pw_stream)
  1111. obs_pipewire_stream_show(camera_source->obs_pw_stream);
  1112. }
  1113. static void pipewire_camera_hide(void *data)
  1114. {
  1115. struct camera_portal_source *camera_source = data;
  1116. if (camera_source->obs_pw_stream)
  1117. obs_pipewire_stream_hide(camera_source->obs_pw_stream);
  1118. }
  1119. static uint32_t pipewire_camera_get_width(void *data)
  1120. {
  1121. struct camera_portal_source *camera_source = data;
  1122. if (camera_source->obs_pw_stream)
  1123. return obs_pipewire_stream_get_width(
  1124. camera_source->obs_pw_stream);
  1125. else
  1126. return 0;
  1127. }
  1128. static uint32_t pipewire_camera_get_height(void *data)
  1129. {
  1130. struct camera_portal_source *camera_source = data;
  1131. if (camera_source->obs_pw_stream)
  1132. return obs_pipewire_stream_get_height(
  1133. camera_source->obs_pw_stream);
  1134. else
  1135. return 0;
  1136. }
  1137. void camera_portal_load(void)
  1138. {
  1139. const struct obs_source_info pipewire_camera_info = {
  1140. .id = "pipewire-camera-source",
  1141. .type = OBS_SOURCE_TYPE_INPUT,
  1142. .output_flags = OBS_SOURCE_ASYNC_VIDEO,
  1143. .get_name = pipewire_camera_get_name,
  1144. .create = pipewire_camera_create,
  1145. .destroy = pipewire_camera_destroy,
  1146. .get_defaults = pipewire_camera_get_defaults,
  1147. .get_properties = pipewire_camera_get_properties,
  1148. .update = pipewire_camera_update,
  1149. .show = pipewire_camera_show,
  1150. .hide = pipewire_camera_hide,
  1151. .get_width = pipewire_camera_get_width,
  1152. .get_height = pipewire_camera_get_height,
  1153. .icon_type = OBS_ICON_TYPE_CAMERA,
  1154. };
  1155. obs_register_source(&pipewire_camera_info);
  1156. }
  1157. void camera_portal_unload(void)
  1158. {
  1159. g_clear_pointer(&connection, pw_portal_connection_free);
  1160. }