1
0

camera-portal.c 37 KB

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