camera-portal.c 36 KB

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