1
0

camera-portal.c 35 KB

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