| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995 |
- /* camera-portal.c
- *
- * Copyright 2021 Georges Basile Stavracas Neto <[email protected]>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * SPDX-License-Identifier: GPL-2.0-or-later
- */
- #include "pipewire.h"
- #include "formats.h"
- #include "portal.h"
- #include <util/dstr.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <gio/gio.h>
- #include <gio/gunixfdlist.h>
- #include <spa/debug/dict.h>
- #include <spa/node/keys.h>
- #include <spa/pod/iter.h>
- #include <spa/pod/parser.h>
- #include <spa/param/props.h>
- #include <spa/utils/defs.h>
- #include <spa/utils/keys.h>
- #include <spa/utils/result.h>
- struct camera_portal_source {
- obs_source_t *source;
- obs_data_t *settings;
- obs_pipewire_stream *obs_pw_stream;
- char *device_id;
- };
- /* ------------------------------------------------- */
- struct pw_portal_connection {
- obs_pipewire *obs_pw;
- GHashTable *devices;
- GCancellable *cancellable;
- GPtrArray *sources;
- bool initializing;
- };
- struct pw_portal_connection *connection = NULL;
- static void pw_portal_connection_free(struct pw_portal_connection *connection)
- {
- if (!connection)
- return;
- g_cancellable_cancel(connection->cancellable);
- g_clear_pointer(&connection->devices, g_hash_table_destroy);
- g_clear_pointer(&connection->obs_pw, obs_pipewire_destroy);
- g_clear_pointer(&connection->sources, g_ptr_array_unref);
- g_clear_object(&connection->cancellable);
- bfree(connection);
- }
- static GDBusProxy *camera_proxy = NULL;
- static void ensure_camera_portal_proxy(void)
- {
- g_autoptr(GError) error = NULL;
- if (!camera_proxy) {
- camera_proxy = g_dbus_proxy_new_sync(
- portal_get_dbus_connection(), G_DBUS_PROXY_FLAGS_NONE,
- NULL, "org.freedesktop.portal.Desktop",
- "/org/freedesktop/portal/desktop",
- "org.freedesktop.portal.Camera", NULL, &error);
- if (error) {
- blog(LOG_WARNING,
- "[portals] Error retrieving D-Bus proxy: %s",
- error->message);
- return;
- }
- }
- }
- static GDBusProxy *get_camera_portal_proxy(void)
- {
- ensure_camera_portal_proxy();
- return camera_proxy;
- }
- static uint32_t get_camera_version(void)
- {
- g_autoptr(GVariant) cached_version = NULL;
- uint32_t version;
- ensure_camera_portal_proxy();
- if (!camera_proxy)
- return 0;
- cached_version =
- g_dbus_proxy_get_cached_property(camera_proxy, "version");
- version = cached_version ? g_variant_get_uint32(cached_version) : 0;
- return version;
- }
- /* ------------------------------------------------- */
- struct camera_device {
- uint32_t id;
- struct pw_properties *properties;
- struct pw_proxy *proxy;
- struct spa_hook proxy_listener;
- struct pw_node *node;
- struct spa_hook node_listener;
- struct pw_node_info *info;
- uint32_t changed;
- struct spa_list pending_list;
- struct spa_list param_list;
- int pending_sync;
- };
- struct param {
- uint32_t id;
- int32_t seq;
- struct spa_list link;
- struct spa_pod *param;
- };
- static uint32_t clear_params(struct spa_list *param_list, uint32_t id)
- {
- struct param *p, *t;
- uint32_t count = 0;
- spa_list_for_each_safe(p, t, param_list, link)
- {
- if (id == SPA_ID_INVALID || p->id == id) {
- spa_list_remove(&p->link);
- free(p);
- count++;
- }
- }
- return count;
- }
- static struct param *add_param(struct spa_list *params, int seq, uint32_t id,
- const struct spa_pod *param)
- {
- struct param *p;
- if (id == SPA_ID_INVALID) {
- if (param == NULL || !spa_pod_is_object(param)) {
- errno = EINVAL;
- return NULL;
- }
- id = SPA_POD_OBJECT_ID(param);
- }
- p = malloc(sizeof(*p) + (param != NULL ? SPA_POD_SIZE(param) : 0));
- if (p == NULL)
- return NULL;
- p->id = id;
- p->seq = seq;
- if (param != NULL) {
- p->param = SPA_PTROFF(p, sizeof(*p), struct spa_pod);
- memcpy(p->param, param, SPA_POD_SIZE(param));
- } else {
- clear_params(params, id);
- p->param = NULL;
- }
- spa_list_append(params, &p->link);
- return p;
- }
- static void object_update_params(struct spa_list *param_list,
- struct spa_list *pending_list,
- uint32_t n_params,
- struct spa_param_info *params)
- {
- struct param *p, *t;
- uint32_t i;
- for (i = 0; i < n_params; i++) {
- spa_list_for_each_safe(p, t, pending_list, link)
- {
- if (p->id == params[i].id && p->seq != params[i].seq &&
- p->param != NULL) {
- spa_list_remove(&p->link);
- free(p);
- }
- }
- }
- spa_list_consume(p, pending_list, link)
- {
- spa_list_remove(&p->link);
- if (p->param == NULL) {
- clear_params(param_list, p->id);
- free(p);
- } else {
- spa_list_append(param_list, &p->link);
- }
- }
- }
- static struct camera_device *
- camera_device_new(uint32_t id, const struct spa_dict *properties)
- {
- struct camera_device *device = bzalloc(sizeof(struct camera_device));
- device->id = id;
- device->properties = properties ? pw_properties_new_dict(properties)
- : NULL;
- spa_list_init(&device->pending_list);
- spa_list_init(&device->param_list);
- return device;
- }
- static void camera_device_free(struct camera_device *device)
- {
- if (!device)
- return;
- clear_params(&device->pending_list, SPA_ID_INVALID);
- clear_params(&device->param_list, SPA_ID_INVALID);
- g_clear_pointer(&device->proxy, pw_proxy_destroy);
- g_clear_pointer(&device->properties, pw_properties_free);
- bfree(device);
- }
- /* ------------------------------------------------- */
- static bool update_device_id(struct camera_portal_source *camera_source,
- const char *new_device_id)
- {
- if (strcmp(camera_source->device_id, new_device_id) == 0)
- return false;
- g_clear_pointer(&camera_source->device_id, bfree);
- camera_source->device_id = bstrdup(new_device_id);
- return true;
- }
- static void stream_camera(struct camera_portal_source *camera_source)
- {
- struct obs_pipwire_connect_stream_info connect_info;
- struct camera_device *device;
- g_clear_pointer(&camera_source->obs_pw_stream,
- obs_pipewire_stream_destroy);
- device = g_hash_table_lookup(connection->devices,
- camera_source->device_id);
- if (!device)
- return;
- blog(LOG_INFO, "[camera-portal] streaming camera '%s'",
- camera_source->device_id);
- connect_info = (struct obs_pipwire_connect_stream_info){
- .stream_name = "OBS PipeWire Camera",
- .stream_properties = pw_properties_new(
- PW_KEY_MEDIA_TYPE, "Video", PW_KEY_MEDIA_CATEGORY,
- "Capture", PW_KEY_MEDIA_ROLE, "Camera", NULL),
- };
- camera_source->obs_pw_stream = obs_pipewire_connect_stream(
- connection->obs_pw, camera_source->source, device->id,
- &connect_info);
- }
- static void camera_format_list(struct camera_device *dev, obs_property_t *prop)
- {
- struct param *p;
- enum video_format last_format = VIDEO_FORMAT_NONE;
- obs_property_list_clear(prop);
- spa_list_for_each(p, &dev->param_list, link)
- {
- struct obs_pw_video_format obs_pw_video_format;
- uint32_t media_type, media_subtype, format;
- if (p->id != SPA_PARAM_EnumFormat || p->param == NULL)
- continue;
- if (spa_format_parse(p->param, &media_type, &media_subtype) < 0)
- continue;
- if (media_type != SPA_MEDIA_TYPE_video)
- continue;
- if (media_subtype == SPA_MEDIA_SUBTYPE_raw) {
- if (spa_pod_parse_object(p->param,
- SPA_TYPE_OBJECT_Format, NULL,
- SPA_FORMAT_VIDEO_format,
- SPA_POD_Id(&format)) < 0)
- continue;
- } else {
- format = SPA_VIDEO_FORMAT_ENCODED;
- }
- if (!obs_pw_video_format_from_spa_format(format,
- &obs_pw_video_format))
- continue;
- if (obs_pw_video_format.video_format == last_format)
- continue;
- last_format = obs_pw_video_format.video_format;
- obs_property_list_add_int(prop, obs_pw_video_format.pretty_name,
- format);
- }
- }
- static inline void add_control_property(obs_properties_t *props,
- obs_data_t *settings,
- struct camera_device *dev,
- struct param *p)
- {
- UNUSED_PARAMETER(dev);
- const struct spa_pod *type, *pod, *labels = NULL;
- uint32_t n_vals, choice, container = SPA_ID_INVALID;
- obs_property_t *prop = NULL;
- const char *name;
- if (spa_pod_parse_object(
- p->param, SPA_TYPE_OBJECT_PropInfo, NULL,
- SPA_PROP_INFO_description, SPA_POD_OPT_String(&name),
- SPA_PROP_INFO_type, SPA_POD_PodChoice(&type),
- SPA_PROP_INFO_container, SPA_POD_OPT_Id(&container),
- SPA_PROP_INFO_labels, SPA_POD_OPT_PodStruct(&labels)) < 0)
- return;
- pod = spa_pod_get_values(type, &n_vals, &choice);
- container = container != SPA_ID_INVALID ? container : SPA_POD_TYPE(pod);
- switch (SPA_POD_TYPE(pod)) {
- case SPA_TYPE_Int: {
- int32_t *vals = SPA_POD_BODY(pod);
- if (n_vals < 1)
- return;
- if (choice == SPA_CHOICE_Enum) {
- struct spa_pod_parser prs;
- struct spa_pod_frame f;
- if (labels == NULL)
- return;
- prop = obs_properties_add_list(props, (char *)name,
- (char *)name,
- OBS_COMBO_TYPE_LIST,
- OBS_COMBO_FORMAT_INT);
- spa_pod_parser_pod(&prs, (struct spa_pod *)labels);
- if (spa_pod_parser_push_struct(&prs, &f) < 0)
- return;
- while (1) {
- int32_t id;
- const char *desc;
- if (spa_pod_parser_get_int(&prs, &id) < 0 ||
- spa_pod_parser_get_string(&prs, &desc) < 0)
- break;
- obs_property_list_add_int(prop, (char *)desc,
- id);
- }
- } else {
- prop = obs_properties_add_int_slider(
- props, (char *)name, (char *)name,
- n_vals > 1 ? vals[1] : vals[0],
- n_vals > 2 ? vals[2] : vals[0],
- n_vals > 3 ? vals[3] : 1);
- }
- obs_data_set_default_int(settings, (char *)name, vals[0]);
- break;
- }
- case SPA_TYPE_Bool: {
- int32_t *vals = SPA_POD_BODY(pod);
- if (n_vals < 1)
- return;
- prop = obs_properties_add_bool(props, (char *)name,
- (char *)name);
- obs_data_set_default_bool(settings, (char *)name, vals[0]);
- break;
- }
- default:
- break;
- }
- }
- static void camera_update_controls(struct camera_device *dev,
- obs_properties_t *props,
- obs_data_t *settings)
- {
- struct param *p;
- spa_list_for_each(p, &dev->param_list, link)
- {
- if (p->id != SPA_PARAM_PropInfo || p->param == NULL)
- continue;
- add_control_property(props, settings, dev, p);
- }
- }
- static bool device_selected(void *data, obs_properties_t *props,
- obs_property_t *property, obs_data_t *settings)
- {
- UNUSED_PARAMETER(props);
- UNUSED_PARAMETER(property);
- struct camera_portal_source *camera_source = data;
- const char *device_id;
- struct camera_device *device;
- obs_properties_t *new_control_properties;
- device_id = obs_data_get_string(settings, "device_id");
- blog(LOG_INFO, "[camera-portal] selected camera '%s'", device_id);
- device = g_hash_table_lookup(connection->devices, device_id);
- if (device == NULL)
- return false;
- if (update_device_id(camera_source, device_id))
- stream_camera(camera_source);
- blog(LOG_INFO, "[camera-portal] Updating pixel formats");
- property = obs_properties_get(props, "pixelformat");
- new_control_properties = obs_properties_create();
- obs_properties_remove_by_name(props, "controls");
- camera_format_list(device, property);
- camera_update_controls(device, new_control_properties, settings);
- obs_properties_add_group(props, "controls",
- obs_module_text("CameraControls"),
- OBS_GROUP_NORMAL, new_control_properties);
- obs_property_modified(property, settings);
- return true;
- }
- /*
- * Format selected callback
- */
- static bool format_selected(void *data, obs_properties_t *properties,
- obs_property_t *property, obs_data_t *settings)
- {
- UNUSED_PARAMETER(properties);
- UNUSED_PARAMETER(property);
- UNUSED_PARAMETER(settings);
- struct camera_portal_source *camera_source = data;
- blog(LOG_INFO, "[camera-portal] Selected format for '%s'",
- camera_source->device_id);
- return true;
- }
- /*
- * Resolution selected callback
- */
- static bool resolution_selected(void *data, obs_properties_t *properties,
- obs_property_t *property, obs_data_t *settings)
- {
- UNUSED_PARAMETER(properties);
- UNUSED_PARAMETER(property);
- UNUSED_PARAMETER(settings);
- struct camera_portal_source *camera_source = data;
- blog(LOG_INFO, "[camera-portal] Selected resolution for '%s'",
- camera_source->device_id);
- return true;
- }
- static void populate_cameras_list(struct camera_portal_source *camera_source,
- obs_property_t *device_list)
- {
- struct camera_device *device;
- GHashTableIter iter;
- const char *device_id;
- bool device_found;
- if (!connection)
- return;
- obs_property_list_clear(device_list);
- device_found = false;
- g_hash_table_iter_init(&iter, connection->devices);
- while (g_hash_table_iter_next(&iter, (gpointer *)&device_id,
- (gpointer *)&device)) {
- const char *device_name;
- device_name = pw_properties_get(device->properties,
- PW_KEY_NODE_DESCRIPTION);
- obs_property_list_add_string(device_list, device_name,
- device_id);
- device_found |= strcmp(device_id, camera_source->device_id) ==
- 0;
- }
- if (!device_found && camera_source->device_id) {
- size_t device_index;
- device_index = obs_property_list_add_string(
- device_list, camera_source->device_id,
- camera_source->device_id);
- obs_property_list_item_disable(device_list, device_index, true);
- }
- }
- /* ------------------------------------------------- */
- static void node_info(void *data, const struct pw_node_info *info)
- {
- struct camera_device *device = data;
- uint32_t i, changed = 0;
- int res;
- info = device->info = pw_node_info_update(device->info, info);
- if (info == NULL)
- return;
- if (info->change_mask & PW_NODE_CHANGE_MASK_PARAMS) {
- for (i = 0; i < info->n_params; i++) {
- uint32_t id = info->params[i].id;
- if (info->params[i].user == 0)
- continue;
- info->params[i].user = 0;
- changed++;
- add_param(&device->pending_list, 0, id, NULL);
- if (!(info->params[i].flags & SPA_PARAM_INFO_READ))
- continue;
- res = pw_node_enum_params(
- (struct pw_node *)device->proxy,
- ++info->params[i].seq, id, 0, -1, NULL);
- if (SPA_RESULT_IS_ASYNC(res))
- info->params[i].seq = res;
- }
- }
- if (changed) {
- device->changed += changed;
- device->pending_sync =
- pw_proxy_sync(device->proxy, device->pending_sync);
- }
- }
- static void node_param(void *data, int seq, uint32_t id, uint32_t index,
- uint32_t next, const struct spa_pod *param)
- {
- UNUSED_PARAMETER(index);
- UNUSED_PARAMETER(next);
- struct camera_device *device = data;
- add_param(&device->pending_list, seq, id, param);
- }
- static const struct pw_node_events node_events = {
- PW_VERSION_NODE_EVENTS,
- .info = node_info,
- .param = node_param,
- };
- static void on_proxy_removed_cb(void *data)
- {
- struct camera_device *device = data;
- pw_proxy_destroy(device->proxy);
- }
- static void on_destroy_proxy_cb(void *data)
- {
- struct camera_device *device = data;
- spa_hook_remove(&device->proxy_listener);
- device->proxy = NULL;
- }
- static void on_done_proxy_cb(void *data, int seq)
- {
- struct camera_device *device = data;
- if (device->info != NULL && device->pending_sync == seq) {
- object_update_params(&device->param_list, &device->pending_list,
- device->info->n_params,
- device->info->params);
- }
- }
- static const struct pw_proxy_events proxy_events = {
- PW_VERSION_PROXY_EVENTS,
- .removed = on_proxy_removed_cb,
- .destroy = on_destroy_proxy_cb,
- .done = on_done_proxy_cb,
- };
- static void on_registry_global_cb(void *user_data, uint32_t id,
- uint32_t permissions, const char *type,
- uint32_t version,
- const struct spa_dict *props)
- {
- UNUSED_PARAMETER(user_data);
- UNUSED_PARAMETER(permissions);
- UNUSED_PARAMETER(version);
- struct camera_device *device;
- struct pw_registry *registry;
- const char *device_id;
- if (strcmp(type, PW_TYPE_INTERFACE_Node) != 0)
- return;
- registry = obs_pipewire_get_registry(connection->obs_pw);
- device_id = spa_dict_lookup(props, SPA_KEY_NODE_NAME);
- blog(LOG_INFO, "[camera-portal] Found device %s", device_id);
- device = camera_device_new(id, props);
- device->proxy = pw_registry_bind(registry, id, type, version, 0);
- if (!device->proxy) {
- blog(LOG_WARNING, "[camera-portal] Failed to bind device %s",
- device_id);
- bfree(device);
- return;
- }
- pw_proxy_add_listener(device->proxy, &device->proxy_listener,
- &proxy_events, device);
- device->node = (struct pw_node *)device->proxy;
- pw_node_add_listener(device->node, &device->node_listener, &node_events,
- device);
- g_hash_table_insert(connection->devices, bstrdup(device_id), device);
- for (size_t i = 0; i < connection->sources->len; i++) {
- struct camera_portal_source *camera_source =
- g_ptr_array_index(connection->sources, i);
- if (strcmp(camera_source->device_id, device_id) == 0)
- stream_camera(camera_source);
- }
- }
- static void on_registry_global_remove_cb(void *user_data, uint32_t id)
- {
- UNUSED_PARAMETER(user_data);
- struct camera_device *device;
- const char *device_id;
- GHashTableIter iter;
- g_hash_table_iter_init(&iter, connection->devices);
- while (g_hash_table_iter_next(&iter, (gpointer *)&device_id,
- (gpointer *)&device)) {
- if (device->id != id)
- continue;
- g_hash_table_iter_remove(&iter);
- }
- }
- static const struct pw_registry_events registry_events = {
- PW_VERSION_REGISTRY_EVENTS,
- .global = on_registry_global_cb,
- .global_remove = on_registry_global_remove_cb,
- };
- /* ------------------------------------------------- */
- static void on_pipewire_remote_opened_cb(GObject *source, GAsyncResult *res,
- void *user_data)
- {
- UNUSED_PARAMETER(user_data);
- g_autoptr(GUnixFDList) fd_list = NULL;
- g_autoptr(GVariant) result = NULL;
- g_autoptr(GError) error = NULL;
- int pipewire_fd;
- int fd_index;
- result = g_dbus_proxy_call_with_unix_fd_list_finish(
- G_DBUS_PROXY(source), &fd_list, res, &error);
- if (error) {
- if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
- blog(LOG_ERROR,
- "[camera-portal] Error retrieving PipeWire fd: %s",
- error->message);
- return;
- }
- g_variant_get(result, "(h)", &fd_index, &error);
- pipewire_fd = g_unix_fd_list_get(fd_list, fd_index, &error);
- if (error) {
- if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
- blog(LOG_ERROR,
- "[camera-portal] Error retrieving PipeWire fd: %s",
- error->message);
- return;
- }
- connection->obs_pw = obs_pipewire_connect_fd(
- pipewire_fd, ®istry_events, connection);
- obs_pipewire_roundtrip(connection->obs_pw);
- }
- static void open_pipewire_remote(void)
- {
- GVariantBuilder builder;
- g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
- g_dbus_proxy_call_with_unix_fd_list(get_camera_portal_proxy(),
- "OpenPipeWireRemote",
- g_variant_new("(a{sv})", &builder),
- G_DBUS_CALL_FLAGS_NONE, -1, NULL,
- connection->cancellable,
- on_pipewire_remote_opened_cb, NULL);
- }
- /* ------------------------------------------------- */
- static void on_access_camera_response_received_cb(GVariant *parameters,
- void *user_data)
- {
- UNUSED_PARAMETER(user_data);
- g_autoptr(GVariant) result = NULL;
- uint32_t response;
- g_variant_get(parameters, "(u@a{sv})", &response, &result);
- if (response != 0) {
- blog(LOG_WARNING,
- "[camera-portal] Failed to create session, denied or cancelled by user");
- return;
- }
- blog(LOG_INFO, "[camera-portal] Successfully accessed cameras");
- open_pipewire_remote();
- }
- static void on_access_camera_finished_cb(GObject *source, GAsyncResult *res,
- void *user_data)
- {
- UNUSED_PARAMETER(user_data);
- g_autoptr(GVariant) result = NULL;
- g_autoptr(GError) error = NULL;
- result = g_dbus_proxy_call_finish(G_DBUS_PROXY(source), res, &error);
- if (error) {
- if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
- blog(LOG_ERROR,
- "[camera-portal] Error accessing camera: %s",
- error->message);
- return;
- }
- }
- static void access_camera(struct camera_portal_source *camera_source)
- {
- GVariantBuilder builder;
- char *request_token;
- char *request_path;
- if (connection && connection->obs_pw) {
- stream_camera(camera_source);
- return;
- }
- if (!connection) {
- connection = bzalloc(sizeof(struct pw_portal_connection));
- connection->devices = g_hash_table_new_full(
- g_str_hash, g_str_equal, bfree,
- (GDestroyNotify)camera_device_free);
- connection->cancellable = g_cancellable_new();
- connection->sources = g_ptr_array_new();
- connection->initializing = false;
- }
- g_ptr_array_add(connection->sources, camera_source);
- if (connection->initializing)
- return;
- portal_create_request_path(&request_path, &request_token);
- portal_signal_subscribe(request_path, NULL,
- on_access_camera_response_received_cb, NULL);
- g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
- g_variant_builder_add(&builder, "{sv}", "handle_token",
- g_variant_new_string(request_token));
- g_dbus_proxy_call(get_camera_portal_proxy(), "AccessCamera",
- g_variant_new("(a{sv})", &builder),
- G_DBUS_CALL_FLAGS_NONE, -1, connection->cancellable,
- on_access_camera_finished_cb, NULL);
- connection->initializing = true;
- bfree(request_token);
- bfree(request_path);
- }
- /* obs_source_info methods */
- static const char *pipewire_camera_get_name(void *data)
- {
- UNUSED_PARAMETER(data);
- return obs_module_text("PipeWireCamera");
- }
- static void *pipewire_camera_create(obs_data_t *settings, obs_source_t *source)
- {
- struct camera_portal_source *camera_source;
- camera_source = bzalloc(sizeof(struct camera_portal_source));
- camera_source->source = source;
- camera_source->device_id =
- bstrdup(obs_data_get_string(settings, "device_id"));
- access_camera(camera_source);
- return camera_source;
- }
- static void pipewire_camera_destroy(void *data)
- {
- struct camera_portal_source *camera_source = data;
- if (connection)
- g_ptr_array_remove(connection->sources, camera_source);
- g_clear_pointer(&camera_source->obs_pw_stream,
- obs_pipewire_stream_destroy);
- g_clear_pointer(&camera_source->device_id, bfree);
- bfree(camera_source);
- }
- static void pipewire_camera_get_defaults(obs_data_t *settings)
- {
- obs_data_set_default_string(settings, "device_id", NULL);
- }
- static obs_properties_t *pipewire_camera_get_properties(void *data)
- {
- struct camera_portal_source *camera_source = data;
- obs_properties_t *controls_props;
- obs_properties_t *props;
- obs_property_t *resolution_list;
- obs_property_t *device_list;
- obs_property_t *format_list;
- props = obs_properties_create();
- device_list = obs_properties_add_list(
- props, "device_id", obs_module_text("PipeWireCameraDevice"),
- OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
- format_list = obs_properties_add_list(props, "pixelformat",
- obs_module_text("VideoFormat"),
- OBS_COMBO_TYPE_LIST,
- OBS_COMBO_FORMAT_INT);
- resolution_list = obs_properties_add_list(props, "resolution",
- obs_module_text("Resolution"),
- OBS_COMBO_TYPE_LIST,
- OBS_COMBO_FORMAT_INT);
- obs_properties_add_list(props, "framerate",
- obs_module_text("FrameRate"),
- OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
- // a group to contain the camera control
- controls_props = obs_properties_create();
- obs_properties_add_group(props, "controls",
- obs_module_text("CameraControls"),
- OBS_GROUP_NORMAL, controls_props);
- populate_cameras_list(camera_source, device_list);
- obs_property_set_modified_callback2(device_list, device_selected,
- camera_source);
- obs_property_set_modified_callback2(format_list, format_selected,
- camera_source);
- obs_property_set_modified_callback2(resolution_list,
- resolution_selected, camera_source);
- return props;
- }
- static void pipewire_camera_update(void *data, obs_data_t *settings)
- {
- struct camera_portal_source *camera_source = data;
- const char *device_id;
- device_id = obs_data_get_string(settings, "device_id");
- blog(LOG_INFO, "[camera-portal] Updating device %s", device_id);
- if (update_device_id(camera_source, device_id))
- stream_camera(camera_source);
- }
- static void pipewire_camera_show(void *data)
- {
- struct camera_portal_source *camera_source = data;
- if (camera_source->obs_pw_stream)
- obs_pipewire_stream_show(camera_source->obs_pw_stream);
- }
- static void pipewire_camera_hide(void *data)
- {
- struct camera_portal_source *camera_source = data;
- if (camera_source->obs_pw_stream)
- obs_pipewire_stream_hide(camera_source->obs_pw_stream);
- }
- static uint32_t pipewire_camera_get_width(void *data)
- {
- struct camera_portal_source *camera_source = data;
- if (camera_source->obs_pw_stream)
- return obs_pipewire_stream_get_width(
- camera_source->obs_pw_stream);
- else
- return 0;
- }
- static uint32_t pipewire_camera_get_height(void *data)
- {
- struct camera_portal_source *camera_source = data;
- if (camera_source->obs_pw_stream)
- return obs_pipewire_stream_get_height(
- camera_source->obs_pw_stream);
- else
- return 0;
- }
- void camera_portal_load(void)
- {
- const struct obs_source_info pipewire_camera_info = {
- .id = "pipewire-camera-source",
- .type = OBS_SOURCE_TYPE_INPUT,
- .output_flags = OBS_SOURCE_ASYNC_VIDEO,
- .get_name = pipewire_camera_get_name,
- .create = pipewire_camera_create,
- .destroy = pipewire_camera_destroy,
- .get_defaults = pipewire_camera_get_defaults,
- .get_properties = pipewire_camera_get_properties,
- .update = pipewire_camera_update,
- .show = pipewire_camera_show,
- .hide = pipewire_camera_hide,
- .get_width = pipewire_camera_get_width,
- .get_height = pipewire_camera_get_height,
- .icon_type = OBS_ICON_TYPE_CAMERA,
- };
- obs_register_source(&pipewire_camera_info);
- }
- void camera_portal_unload(void)
- {
- g_clear_pointer(&connection, pw_portal_connection_free);
- }
|