camera-portal.c 38 KB

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