camera-portal.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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(GArray) framerates = NULL;
  573. struct param *p;
  574. obs_data_t *data;
  575. framerates = g_array_new(FALSE, FALSE, sizeof(struct spa_fraction));
  576. spa_list_for_each(p, &dev->param_list, link)
  577. {
  578. const struct spa_fraction *framerate_values;
  579. struct obs_pw_video_format obs_pw_video_format;
  580. enum spa_choice_type choice;
  581. const struct spa_pod_prop *prop;
  582. struct spa_rectangle this_resolution;
  583. struct spa_pod *framerate_pod;
  584. uint32_t media_subtype;
  585. uint32_t media_type;
  586. uint32_t n_framerates;
  587. uint32_t format;
  588. if (p->id != SPA_PARAM_EnumFormat || p->param == NULL)
  589. continue;
  590. if (spa_format_parse(p->param, &media_type, &media_subtype) < 0)
  591. continue;
  592. if (media_type != SPA_MEDIA_TYPE_video)
  593. continue;
  594. if (media_subtype == SPA_MEDIA_SUBTYPE_raw) {
  595. if (spa_pod_parse_object(p->param,
  596. SPA_TYPE_OBJECT_Format, NULL,
  597. SPA_FORMAT_VIDEO_format,
  598. SPA_POD_Id(&format)) < 0)
  599. continue;
  600. } else {
  601. format = SPA_VIDEO_FORMAT_ENCODED;
  602. }
  603. if (!obs_pw_video_format_from_spa_format(format,
  604. &obs_pw_video_format))
  605. continue;
  606. if (obs_pw_video_format.video_format != pixelformat)
  607. continue;
  608. if (spa_pod_parse_object(
  609. p->param, SPA_TYPE_OBJECT_Format, NULL,
  610. SPA_FORMAT_VIDEO_size,
  611. SPA_POD_OPT_Rectangle(&this_resolution)) < 0)
  612. continue;
  613. if (this_resolution.width != resolution->width ||
  614. this_resolution.height != resolution->height)
  615. continue;
  616. prop = spa_pod_find_prop(p->param, NULL,
  617. SPA_FORMAT_VIDEO_framerate);
  618. if (!prop)
  619. continue;
  620. framerate_pod = spa_pod_get_values(&prop->value, &n_framerates,
  621. &choice);
  622. if (framerate_pod->type != SPA_TYPE_Fraction) {
  623. blog(LOG_WARNING,
  624. "Framerate is not a fraction - something is wrong");
  625. continue;
  626. }
  627. framerate_values = SPA_POD_BODY(framerate_pod);
  628. switch (choice) {
  629. case SPA_CHOICE_None:
  630. g_array_append_val(framerates, framerate_values[0]);
  631. break;
  632. case SPA_CHOICE_Range:
  633. blog(LOG_WARNING, "Ranged framerates not supported");
  634. break;
  635. case SPA_CHOICE_Step:
  636. blog(LOG_WARNING, "Stepped framerates not supported");
  637. break;
  638. case SPA_CHOICE_Enum:
  639. /* i=0 is the default framerate, skip it */
  640. for (uint32_t i = 1; i < n_framerates; i++)
  641. g_array_append_val(framerates,
  642. framerate_values[i]);
  643. break;
  644. default:
  645. break;
  646. }
  647. }
  648. g_array_sort(framerates, compare_framerates);
  649. obs_property_list_clear(prop);
  650. data = obs_data_create();
  651. for (size_t i = 0; i < framerates->len; i++) {
  652. const struct spa_fraction *framerate =
  653. &g_array_index(framerates, struct spa_fraction, i);
  654. struct media_frames_per_second fps;
  655. struct dstr str = {};
  656. fps = (struct media_frames_per_second){
  657. .numerator = framerate->num,
  658. .denominator = framerate->denom,
  659. };
  660. obs_data_set_frames_per_second(data, "framerate", fps, NULL);
  661. dstr_printf(&str, "%.2f",
  662. framerate->num / (double)framerate->denom);
  663. obs_property_list_add_string(prop, str.array,
  664. obs_data_get_json(data));
  665. dstr_free(&str);
  666. }
  667. obs_data_release(data);
  668. }
  669. static bool parse_framerate(struct spa_fraction *dest, const char *json)
  670. {
  671. struct media_frames_per_second fps;
  672. obs_data_t *data = obs_data_create_from_json(json);
  673. if (!data)
  674. return false;
  675. if (!obs_data_get_frames_per_second(data, "framerate", &fps, NULL)) {
  676. obs_data_release(data);
  677. return false;
  678. }
  679. dest->num = fps.numerator;
  680. dest->denom = fps.denominator;
  681. obs_data_release(data);
  682. return true;
  683. }
  684. static bool framerate_selected(void *data, obs_properties_t *properties,
  685. obs_property_t *property, obs_data_t *settings)
  686. {
  687. UNUSED_PARAMETER(properties);
  688. UNUSED_PARAMETER(property);
  689. struct camera_portal_source *camera_source = data;
  690. struct camera_device *device;
  691. struct spa_fraction framerate;
  692. device = g_hash_table_lookup(connection->devices,
  693. camera_source->device_id);
  694. if (device == NULL)
  695. return false;
  696. if (!parse_framerate(&framerate,
  697. obs_data_get_string(settings, "framerate")))
  698. return false;
  699. if (camera_source->obs_pw_stream)
  700. obs_pipewire_stream_set_framerate(camera_source->obs_pw_stream,
  701. &framerate);
  702. return true;
  703. }
  704. /*
  705. * Resolution selected callback
  706. */
  707. static bool parse_resolution(struct spa_rectangle *dest, const char *json)
  708. {
  709. obs_data_t *data = obs_data_create_from_json(json);
  710. if (!data)
  711. return false;
  712. dest->width = obs_data_get_int(data, "width");
  713. dest->height = obs_data_get_int(data, "height");
  714. obs_data_release(data);
  715. return true;
  716. }
  717. static bool resolution_selected(void *data, obs_properties_t *properties,
  718. obs_property_t *property, obs_data_t *settings)
  719. {
  720. UNUSED_PARAMETER(properties);
  721. UNUSED_PARAMETER(property);
  722. UNUSED_PARAMETER(settings);
  723. struct camera_portal_source *camera_source = data;
  724. struct spa_rectangle resolution;
  725. struct camera_device *device;
  726. blog(LOG_INFO, "[camera-portal] Selected resolution for '%s'",
  727. camera_source->device_id);
  728. device = g_hash_table_lookup(connection->devices,
  729. camera_source->device_id);
  730. if (device == NULL)
  731. return false;
  732. if (!parse_resolution(&resolution,
  733. obs_data_get_string(settings, "resolution")))
  734. return false;
  735. if (camera_source->obs_pw_stream)
  736. obs_pipewire_stream_set_resolution(camera_source->obs_pw_stream,
  737. &resolution);
  738. property = obs_properties_get(properties, "framerate");
  739. framerate_list(device, obs_data_get_int(settings, "pixelformat"),
  740. &resolution, property);
  741. return true;
  742. }
  743. static void populate_cameras_list(struct camera_portal_source *camera_source,
  744. obs_property_t *device_list)
  745. {
  746. struct camera_device *device;
  747. GHashTableIter iter;
  748. const char *device_id;
  749. bool device_found;
  750. if (!connection)
  751. return;
  752. obs_property_list_clear(device_list);
  753. device_found = false;
  754. g_hash_table_iter_init(&iter, connection->devices);
  755. while (g_hash_table_iter_next(&iter, (gpointer *)&device_id,
  756. (gpointer *)&device)) {
  757. const char *device_name;
  758. device_name = pw_properties_get(device->properties,
  759. PW_KEY_NODE_DESCRIPTION);
  760. obs_property_list_add_string(device_list, device_name,
  761. device_id);
  762. device_found |= strcmp(device_id, camera_source->device_id) ==
  763. 0;
  764. }
  765. if (!device_found && camera_source->device_id) {
  766. size_t device_index;
  767. device_index = obs_property_list_add_string(
  768. device_list, camera_source->device_id,
  769. camera_source->device_id);
  770. obs_property_list_item_disable(device_list, device_index, true);
  771. }
  772. }
  773. /* ------------------------------------------------- */
  774. static void node_info(void *data, const struct pw_node_info *info)
  775. {
  776. struct camera_device *device = data;
  777. uint32_t i, changed = 0;
  778. int res;
  779. info = device->info = pw_node_info_update(device->info, info);
  780. if (info == NULL)
  781. return;
  782. if (info->change_mask & PW_NODE_CHANGE_MASK_PARAMS) {
  783. for (i = 0; i < info->n_params; i++) {
  784. uint32_t id = info->params[i].id;
  785. if (info->params[i].user == 0)
  786. continue;
  787. info->params[i].user = 0;
  788. changed++;
  789. add_param(&device->pending_list, 0, id, NULL);
  790. if (!(info->params[i].flags & SPA_PARAM_INFO_READ))
  791. continue;
  792. res = pw_node_enum_params(
  793. (struct pw_node *)device->proxy,
  794. ++info->params[i].seq, id, 0, -1, NULL);
  795. if (SPA_RESULT_IS_ASYNC(res))
  796. info->params[i].seq = res;
  797. }
  798. }
  799. if (changed) {
  800. device->changed += changed;
  801. device->pending_sync =
  802. pw_proxy_sync(device->proxy, device->pending_sync);
  803. }
  804. }
  805. static void node_param(void *data, int seq, uint32_t id, uint32_t index,
  806. uint32_t next, const struct spa_pod *param)
  807. {
  808. UNUSED_PARAMETER(index);
  809. UNUSED_PARAMETER(next);
  810. struct camera_device *device = data;
  811. add_param(&device->pending_list, seq, id, param);
  812. }
  813. static const struct pw_node_events node_events = {
  814. PW_VERSION_NODE_EVENTS,
  815. .info = node_info,
  816. .param = node_param,
  817. };
  818. static void on_proxy_removed_cb(void *data)
  819. {
  820. struct camera_device *device = data;
  821. pw_proxy_destroy(device->proxy);
  822. }
  823. static void on_destroy_proxy_cb(void *data)
  824. {
  825. struct camera_device *device = data;
  826. spa_hook_remove(&device->proxy_listener);
  827. device->proxy = NULL;
  828. }
  829. static void on_done_proxy_cb(void *data, int seq)
  830. {
  831. struct camera_device *device = data;
  832. if (device->info != NULL && device->pending_sync == seq) {
  833. object_update_params(&device->param_list, &device->pending_list,
  834. device->info->n_params,
  835. device->info->params);
  836. }
  837. }
  838. static const struct pw_proxy_events proxy_events = {
  839. PW_VERSION_PROXY_EVENTS,
  840. .removed = on_proxy_removed_cb,
  841. .destroy = on_destroy_proxy_cb,
  842. .done = on_done_proxy_cb,
  843. };
  844. static void on_registry_global_cb(void *user_data, uint32_t id,
  845. uint32_t permissions, const char *type,
  846. uint32_t version,
  847. const struct spa_dict *props)
  848. {
  849. UNUSED_PARAMETER(user_data);
  850. UNUSED_PARAMETER(permissions);
  851. UNUSED_PARAMETER(version);
  852. struct camera_device *device;
  853. struct pw_registry *registry;
  854. const char *device_id;
  855. if (strcmp(type, PW_TYPE_INTERFACE_Node) != 0)
  856. return;
  857. registry = obs_pipewire_get_registry(connection->obs_pw);
  858. device_id = spa_dict_lookup(props, SPA_KEY_NODE_NAME);
  859. blog(LOG_INFO, "[camera-portal] Found device %s", device_id);
  860. device = camera_device_new(id, props);
  861. device->proxy = pw_registry_bind(registry, id, type, version, 0);
  862. if (!device->proxy) {
  863. blog(LOG_WARNING, "[camera-portal] Failed to bind device %s",
  864. device_id);
  865. bfree(device);
  866. return;
  867. }
  868. pw_proxy_add_listener(device->proxy, &device->proxy_listener,
  869. &proxy_events, device);
  870. device->node = (struct pw_node *)device->proxy;
  871. pw_node_add_listener(device->node, &device->node_listener, &node_events,
  872. device);
  873. g_hash_table_insert(connection->devices, bstrdup(device_id), device);
  874. for (size_t i = 0; i < connection->sources->len; i++) {
  875. struct camera_portal_source *camera_source =
  876. g_ptr_array_index(connection->sources, i);
  877. obs_source_update_properties(camera_source->source);
  878. if (strcmp(camera_source->device_id, device_id) == 0)
  879. stream_camera(camera_source);
  880. }
  881. }
  882. static void on_registry_global_remove_cb(void *user_data, uint32_t id)
  883. {
  884. UNUSED_PARAMETER(user_data);
  885. struct camera_device *device;
  886. const char *device_id;
  887. GHashTableIter iter;
  888. g_hash_table_iter_init(&iter, connection->devices);
  889. while (g_hash_table_iter_next(&iter, (gpointer *)&device_id,
  890. (gpointer *)&device)) {
  891. if (device->id != id)
  892. continue;
  893. g_hash_table_iter_remove(&iter);
  894. blog(LOG_INFO, "[pipewire-camera] Removed device %s",
  895. device_id);
  896. }
  897. for (size_t i = 0; i < connection->sources->len; i++) {
  898. struct camera_portal_source *camera_source =
  899. g_ptr_array_index(connection->sources, i);
  900. obs_source_update_properties(camera_source->source);
  901. }
  902. }
  903. static const struct pw_registry_events registry_events = {
  904. PW_VERSION_REGISTRY_EVENTS,
  905. .global = on_registry_global_cb,
  906. .global_remove = on_registry_global_remove_cb,
  907. };
  908. /* ------------------------------------------------- */
  909. static void on_pipewire_remote_opened_cb(GObject *source, GAsyncResult *res,
  910. void *user_data)
  911. {
  912. UNUSED_PARAMETER(user_data);
  913. g_autoptr(GUnixFDList) fd_list = NULL;
  914. g_autoptr(GVariant) result = NULL;
  915. g_autoptr(GError) error = NULL;
  916. int pipewire_fd;
  917. int fd_index;
  918. result = g_dbus_proxy_call_with_unix_fd_list_finish(
  919. G_DBUS_PROXY(source), &fd_list, res, &error);
  920. if (error) {
  921. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  922. blog(LOG_ERROR,
  923. "[camera-portal] Error retrieving PipeWire fd: %s",
  924. error->message);
  925. return;
  926. }
  927. g_variant_get(result, "(h)", &fd_index, &error);
  928. pipewire_fd = g_unix_fd_list_get(fd_list, fd_index, &error);
  929. if (error) {
  930. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  931. blog(LOG_ERROR,
  932. "[camera-portal] Error retrieving PipeWire fd: %s",
  933. error->message);
  934. return;
  935. }
  936. connection->obs_pw = obs_pipewire_connect_fd(
  937. pipewire_fd, &registry_events, connection);
  938. obs_pipewire_roundtrip(connection->obs_pw);
  939. }
  940. static void open_pipewire_remote(void)
  941. {
  942. GVariantBuilder builder;
  943. g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
  944. g_dbus_proxy_call_with_unix_fd_list(get_camera_portal_proxy(),
  945. "OpenPipeWireRemote",
  946. g_variant_new("(a{sv})", &builder),
  947. G_DBUS_CALL_FLAGS_NONE, -1, NULL,
  948. connection->cancellable,
  949. on_pipewire_remote_opened_cb, NULL);
  950. }
  951. /* ------------------------------------------------- */
  952. static void on_access_camera_response_received_cb(GVariant *parameters,
  953. void *user_data)
  954. {
  955. UNUSED_PARAMETER(user_data);
  956. g_autoptr(GVariant) result = NULL;
  957. uint32_t response;
  958. g_variant_get(parameters, "(u@a{sv})", &response, &result);
  959. if (response != 0) {
  960. blog(LOG_WARNING,
  961. "[camera-portal] Failed to create session, denied or cancelled by user");
  962. return;
  963. }
  964. blog(LOG_INFO, "[camera-portal] Successfully accessed cameras");
  965. open_pipewire_remote();
  966. }
  967. static void on_access_camera_finished_cb(GObject *source, GAsyncResult *res,
  968. void *user_data)
  969. {
  970. UNUSED_PARAMETER(user_data);
  971. g_autoptr(GVariant) result = NULL;
  972. g_autoptr(GError) error = NULL;
  973. result = g_dbus_proxy_call_finish(G_DBUS_PROXY(source), res, &error);
  974. if (error) {
  975. if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
  976. blog(LOG_ERROR,
  977. "[camera-portal] Error accessing camera: %s",
  978. error->message);
  979. return;
  980. }
  981. }
  982. static void access_camera(struct camera_portal_source *camera_source)
  983. {
  984. GVariantBuilder builder;
  985. char *request_token;
  986. char *request_path;
  987. if (connection && connection->obs_pw) {
  988. stream_camera(camera_source);
  989. return;
  990. }
  991. if (!connection) {
  992. connection = bzalloc(sizeof(struct pw_portal_connection));
  993. connection->devices = g_hash_table_new_full(
  994. g_str_hash, g_str_equal, bfree,
  995. (GDestroyNotify)camera_device_free);
  996. connection->cancellable = g_cancellable_new();
  997. connection->sources = g_ptr_array_new();
  998. connection->initializing = false;
  999. }
  1000. g_ptr_array_add(connection->sources, camera_source);
  1001. if (connection->initializing)
  1002. return;
  1003. portal_create_request_path(&request_path, &request_token);
  1004. portal_signal_subscribe(request_path, NULL,
  1005. on_access_camera_response_received_cb, NULL);
  1006. g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
  1007. g_variant_builder_add(&builder, "{sv}", "handle_token",
  1008. g_variant_new_string(request_token));
  1009. g_dbus_proxy_call(get_camera_portal_proxy(), "AccessCamera",
  1010. g_variant_new("(a{sv})", &builder),
  1011. G_DBUS_CALL_FLAGS_NONE, -1, connection->cancellable,
  1012. on_access_camera_finished_cb, NULL);
  1013. connection->initializing = true;
  1014. bfree(request_token);
  1015. bfree(request_path);
  1016. }
  1017. /* obs_source_info methods */
  1018. static const char *pipewire_camera_get_name(void *data)
  1019. {
  1020. UNUSED_PARAMETER(data);
  1021. return obs_module_text("PipeWireCamera");
  1022. }
  1023. static void *pipewire_camera_create(obs_data_t *settings, obs_source_t *source)
  1024. {
  1025. struct camera_portal_source *camera_source;
  1026. camera_source = bzalloc(sizeof(struct camera_portal_source));
  1027. camera_source->source = source;
  1028. camera_source->device_id =
  1029. bstrdup(obs_data_get_string(settings, "device_id"));
  1030. camera_source->framerate.set =
  1031. parse_framerate(&camera_source->framerate.fraction,
  1032. obs_data_get_string(settings, "framerate"));
  1033. camera_source->resolution.set =
  1034. parse_resolution(&camera_source->resolution.rect,
  1035. obs_data_get_string(settings, "resolution"));
  1036. access_camera(camera_source);
  1037. return camera_source;
  1038. }
  1039. static void pipewire_camera_destroy(void *data)
  1040. {
  1041. struct camera_portal_source *camera_source = data;
  1042. if (connection)
  1043. g_ptr_array_remove(connection->sources, camera_source);
  1044. g_clear_pointer(&camera_source->obs_pw_stream,
  1045. obs_pipewire_stream_destroy);
  1046. g_clear_pointer(&camera_source->device_id, bfree);
  1047. bfree(camera_source);
  1048. }
  1049. static void pipewire_camera_get_defaults(obs_data_t *settings)
  1050. {
  1051. obs_data_set_default_string(settings, "device_id", NULL);
  1052. }
  1053. static obs_properties_t *pipewire_camera_get_properties(void *data)
  1054. {
  1055. struct camera_portal_source *camera_source = data;
  1056. obs_properties_t *controls_props;
  1057. obs_properties_t *props;
  1058. obs_property_t *resolution_list;
  1059. obs_property_t *framerate_list;
  1060. obs_property_t *device_list;
  1061. obs_property_t *format_list;
  1062. props = obs_properties_create();
  1063. device_list = obs_properties_add_list(
  1064. props, "device_id", obs_module_text("PipeWireCameraDevice"),
  1065. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  1066. format_list = obs_properties_add_list(props, "pixelformat",
  1067. obs_module_text("VideoFormat"),
  1068. OBS_COMBO_TYPE_LIST,
  1069. OBS_COMBO_FORMAT_INT);
  1070. resolution_list = obs_properties_add_list(props, "resolution",
  1071. obs_module_text("Resolution"),
  1072. OBS_COMBO_TYPE_LIST,
  1073. OBS_COMBO_FORMAT_STRING);
  1074. framerate_list = obs_properties_add_list(props, "framerate",
  1075. obs_module_text("FrameRate"),
  1076. OBS_COMBO_TYPE_LIST,
  1077. OBS_COMBO_FORMAT_STRING);
  1078. // a group to contain the camera control
  1079. controls_props = obs_properties_create();
  1080. obs_properties_add_group(props, "controls",
  1081. obs_module_text("CameraControls"),
  1082. OBS_GROUP_NORMAL, controls_props);
  1083. populate_cameras_list(camera_source, device_list);
  1084. obs_property_set_modified_callback2(device_list, device_selected,
  1085. camera_source);
  1086. obs_property_set_modified_callback2(format_list, format_selected,
  1087. camera_source);
  1088. obs_property_set_modified_callback2(resolution_list,
  1089. resolution_selected, camera_source);
  1090. obs_property_set_modified_callback2(framerate_list, framerate_selected,
  1091. camera_source);
  1092. return props;
  1093. }
  1094. static void pipewire_camera_update(void *data, obs_data_t *settings)
  1095. {
  1096. struct camera_portal_source *camera_source = data;
  1097. const char *device_id;
  1098. device_id = obs_data_get_string(settings, "device_id");
  1099. blog(LOG_INFO, "[camera-portal] Updating device %s", device_id);
  1100. if (update_device_id(camera_source, device_id))
  1101. stream_camera(camera_source);
  1102. }
  1103. static void pipewire_camera_show(void *data)
  1104. {
  1105. struct camera_portal_source *camera_source = data;
  1106. if (camera_source->obs_pw_stream)
  1107. obs_pipewire_stream_show(camera_source->obs_pw_stream);
  1108. }
  1109. static void pipewire_camera_hide(void *data)
  1110. {
  1111. struct camera_portal_source *camera_source = data;
  1112. if (camera_source->obs_pw_stream)
  1113. obs_pipewire_stream_hide(camera_source->obs_pw_stream);
  1114. }
  1115. static uint32_t pipewire_camera_get_width(void *data)
  1116. {
  1117. struct camera_portal_source *camera_source = data;
  1118. if (camera_source->obs_pw_stream)
  1119. return obs_pipewire_stream_get_width(
  1120. camera_source->obs_pw_stream);
  1121. else
  1122. return 0;
  1123. }
  1124. static uint32_t pipewire_camera_get_height(void *data)
  1125. {
  1126. struct camera_portal_source *camera_source = data;
  1127. if (camera_source->obs_pw_stream)
  1128. return obs_pipewire_stream_get_height(
  1129. camera_source->obs_pw_stream);
  1130. else
  1131. return 0;
  1132. }
  1133. void camera_portal_load(void)
  1134. {
  1135. const struct obs_source_info pipewire_camera_info = {
  1136. .id = "pipewire-camera-source",
  1137. .type = OBS_SOURCE_TYPE_INPUT,
  1138. .output_flags = OBS_SOURCE_ASYNC_VIDEO,
  1139. .get_name = pipewire_camera_get_name,
  1140. .create = pipewire_camera_create,
  1141. .destroy = pipewire_camera_destroy,
  1142. .get_defaults = pipewire_camera_get_defaults,
  1143. .get_properties = pipewire_camera_get_properties,
  1144. .update = pipewire_camera_update,
  1145. .show = pipewire_camera_show,
  1146. .hide = pipewire_camera_hide,
  1147. .get_width = pipewire_camera_get_width,
  1148. .get_height = pipewire_camera_get_height,
  1149. .icon_type = OBS_ICON_TYPE_CAMERA,
  1150. };
  1151. obs_register_source(&pipewire_camera_info);
  1152. }
  1153. void camera_portal_unload(void)
  1154. {
  1155. g_clear_pointer(&connection, pw_portal_connection_free);
  1156. }