summaryrefslogtreecommitdiffstats
path: root/spa/plugins/libcamera
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:28:17 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:28:17 +0000
commit7a46c07230b8d8108c0e8e80df4522d0ac116538 (patch)
treed483300dab478b994fe199a5d19d18d74153718a /spa/plugins/libcamera
parentInitial commit. (diff)
downloadpipewire-upstream.tar.xz
pipewire-upstream.zip
Adding upstream version 0.3.65.upstream/0.3.65upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'spa/plugins/libcamera')
-rw-r--r--spa/plugins/libcamera/libcamera-client.c247
-rw-r--r--spa/plugins/libcamera/libcamera-device.cpp332
-rw-r--r--spa/plugins/libcamera/libcamera-manager.cpp488
-rw-r--r--spa/plugins/libcamera/libcamera-manager.hpp29
-rw-r--r--spa/plugins/libcamera/libcamera-source.cpp1073
-rw-r--r--spa/plugins/libcamera/libcamera-utils.cpp990
-rw-r--r--spa/plugins/libcamera/libcamera.c57
-rw-r--r--spa/plugins/libcamera/libcamera.h51
-rw-r--r--spa/plugins/libcamera/meson.build17
9 files changed, 3284 insertions, 0 deletions
diff --git a/spa/plugins/libcamera/libcamera-client.c b/spa/plugins/libcamera/libcamera-client.c
new file mode 100644
index 0000000..a31a41f
--- /dev/null
+++ b/spa/plugins/libcamera/libcamera-client.c
@@ -0,0 +1,247 @@
+/* Spa libcamera client
+ *
+ * Copyright (C) 2020, Collabora Ltd.
+ * Author: Raghavendra Rao Sidlagatta <raghavendra.rao@collabora.com>
+ *
+ * libcamera-client.c
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <errno.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include <spa/support/log.h>
+#include <spa/support/loop.h>
+#include <spa/support/plugin.h>
+#include <spa/utils/type.h>
+#include <spa/utils/keys.h>
+#include <spa/utils/names.h>
+#include <spa/utils/string.h>
+#include <spa/monitor/device.h>
+#include <spa/monitor/utils.h>
+
+#include "libcamera.h"
+
+struct impl {
+ struct spa_handle handle;
+ struct spa_device device;
+
+ struct spa_log *log;
+ struct spa_loop *main_loop;
+
+ struct spa_hook_list hooks;
+
+ uint64_t info_all;
+ struct spa_device_info info;
+
+ struct spa_source source;
+};
+
+static int emit_object_info(struct impl *this, uint32_t id)
+{
+ struct spa_device_object_info info;
+ struct spa_dict_item items[20];
+ uint32_t n_items = 0;
+
+ info = SPA_DEVICE_OBJECT_INFO_INIT();
+
+ info.type = SPA_TYPE_INTERFACE_Device;
+ info.factory_name = SPA_NAME_API_LIBCAMERA_DEVICE;
+ info.change_mask = (SPA_DEVICE_OBJECT_CHANGE_MASK_FLAGS |
+ SPA_DEVICE_OBJECT_CHANGE_MASK_PROPS);
+ info.flags = 0;
+
+ items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_ENUM_API,"libcamera-client");
+ items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_DEVICE_API, "libcamera");
+ items[n_items++] = SPA_DICT_ITEM_INIT(SPA_KEY_MEDIA_CLASS, "Video/Device");
+
+ info.props = &SPA_DICT_INIT(items, n_items);
+ spa_device_emit_object_info(&this->hooks, id, &info);
+
+ return 1;
+}
+
+static const struct spa_dict_item device_info_items[] = {
+ { SPA_KEY_DEVICE_API, "libcamera" },
+ { SPA_KEY_DEVICE_NICK, "libcamera-client" },
+ { SPA_KEY_API_UDEV_MATCH, "libcamera" },
+};
+
+
+static void emit_device_info(struct impl *this, bool full)
+{
+ uint64_t old = full ? this->info.change_mask : 0;
+ if (full)
+ this->info.change_mask = this->info_all;
+ if (this->info.change_mask) {
+ this->info.props = &SPA_DICT_INIT_ARRAY(device_info_items);
+ spa_device_emit_info(&this->hooks, &this->info);
+ this->info.change_mask = old;
+ }
+}
+
+static void impl_hook_removed(struct spa_hook *hook)
+{
+ return;
+}
+
+static int
+impl_device_add_listener(void *object, struct spa_hook *listener,
+ const struct spa_device_events *events, void *data)
+{
+ struct impl *this = object;
+ struct spa_hook_list save;
+
+ spa_return_val_if_fail(this != NULL, -EINVAL);
+ spa_return_val_if_fail(events != NULL, -EINVAL);
+
+ spa_hook_list_isolate(&this->hooks, &save, listener, events, data);
+
+ emit_device_info(this, true);
+
+ emit_object_info(this, 0);
+
+ spa_hook_list_join(&this->hooks, &save);
+
+ listener->removed = impl_hook_removed;
+ listener->priv = this;
+
+ return 0;
+}
+
+static const struct spa_device_methods impl_device = {
+ SPA_VERSION_DEVICE_METHODS,
+ .add_listener = impl_device_add_listener,
+};
+
+static int impl_get_interface(struct spa_handle *handle, const char *type, void **interface)
+{
+ struct impl *this;
+
+ spa_return_val_if_fail(handle != NULL, -EINVAL);
+ spa_return_val_if_fail(interface != NULL, -EINVAL);
+
+ this = (struct impl *) handle;
+
+ if (spa_streq(type, SPA_TYPE_INTERFACE_Device))
+ *interface = &this->device;
+ else
+ return -ENOENT;
+
+ return 0;
+}
+
+static int impl_clear(struct spa_handle *handle)
+{
+ struct impl *this = (struct impl *) handle;
+
+ if(this->dev.camera) {
+ deleteLibCamera(this->dev.camera);
+ this->dev.camera = NULL;
+ }
+ return 0;
+}
+
+static size_t
+impl_get_size(const struct spa_handle_factory *factory,
+ const struct spa_dict *params)
+{
+ return sizeof(struct impl);
+}
+
+static int
+impl_init(const struct spa_handle_factory *factory,
+ struct spa_handle *handle,
+ const struct spa_dict *info,
+ const struct spa_support *support,
+ uint32_t n_support)
+{
+ struct impl *this;
+
+ spa_return_val_if_fail(factory != NULL, -EINVAL);
+ spa_return_val_if_fail(handle != NULL, -EINVAL);
+
+ handle->get_interface = impl_get_interface;
+ handle->clear = impl_clear;
+
+ this = (struct impl *) handle;
+
+ this->log = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log);
+ libcamera_log_topic_init(this->log);
+
+ this->main_loop = spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Loop);
+
+ if (this->main_loop == NULL) {
+ spa_log_error(this->log, "a main-loop is needed");
+ return -EINVAL;
+ }
+ spa_hook_list_init(&this->hooks);
+
+ this->device.iface = SPA_INTERFACE_INIT(
+ SPA_TYPE_INTERFACE_Device,
+ SPA_VERSION_DEVICE,
+ &impl_device, this);
+
+ this->info = SPA_DEVICE_INFO_INIT();
+ this->info_all = SPA_DEVICE_CHANGE_MASK_FLAGS |
+ SPA_DEVICE_CHANGE_MASK_PROPS;
+ this->info.flags = 0;
+
+ if(this->dev.camera == NULL)
+ this->dev.camera = (LibCamera*)newLibCamera();
+ if(this->dev.camera != NULL)
+ libcamera_set_log(this->dev.camera, this->dev.log);
+
+ return 0;
+}
+
+static const struct spa_interface_info impl_interfaces[] = {
+ {SPA_TYPE_INTERFACE_Device,},
+};
+
+static int
+impl_enum_interface_info(const struct spa_handle_factory *factory,
+ const struct spa_interface_info **info,
+ uint32_t *index)
+{
+ spa_return_val_if_fail(factory != NULL, -EINVAL);
+ spa_return_val_if_fail(info != NULL, -EINVAL);
+ spa_return_val_if_fail(index != NULL, -EINVAL);
+
+ if (*index >= SPA_N_ELEMENTS(impl_interfaces))
+ return 0;
+
+ *info = &impl_interfaces[(*index)++];
+ return 1;
+}
+
+const struct spa_handle_factory spa_libcamera_client_factory = {
+ SPA_VERSION_HANDLE_FACTORY,
+ SPA_NAME_API_LIBCAMERA_ENUM_CLIENT,
+ NULL,
+ impl_get_size,
+ impl_init,
+ impl_enum_interface_info,
+};
diff --git a/spa/plugins/libcamera/libcamera-device.cpp b/spa/plugins/libcamera/libcamera-device.cpp
new file mode 100644
index 0000000..ff206a4
--- /dev/null
+++ b/spa/plugins/libcamera/libcamera-device.cpp
@@ -0,0 +1,332 @@
+/* Spa libcamera Device
+ *
+ * Copyright (C) 2020, Collabora Ltd.
+ * Author: Raghavendra Rao Sidlagatta <raghavendra.rao@collabora.com>
+ * Copyright (C) 2021 Wim Taymans <wim.taymans@gmail.com>
+ *
+ * libcamera-device.cpp
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <stddef.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include <sys/ioctl.h>
+
+#include <spa/support/plugin.h>
+#include <spa/support/log.h>
+#include <spa/support/loop.h>
+#include <spa/utils/keys.h>
+#include <spa/utils/result.h>
+#include <spa/utils/names.h>
+#include <spa/utils/string.h>
+#include <spa/node/node.h>
+#include <spa/pod/builder.h>
+#include <spa/monitor/device.h>
+#include <spa/monitor/utils.h>
+#include <spa/param/param.h>
+
+#include "libcamera.h"
+#include "libcamera-manager.hpp"
+
+#include <libcamera/camera.h>
+#include <libcamera/property_ids.h>
+
+using namespace libcamera;
+
+namespace {
+
+struct impl {
+ struct spa_handle handle;
+ struct spa_device device = {};
+
+ struct spa_log *log;
+
+ std::string device_id;
+
+ struct spa_hook_list hooks;
+
+ std::shared_ptr<CameraManager> manager;
+ std::shared_ptr<Camera> camera;
+
+ impl(spa_log *log,
+ std::shared_ptr<CameraManager> manager,
+ std::shared_ptr<Camera> camera,
+ std::string device_id);
+};
+
+}
+
+static std::string cameraModel(const Camera *camera)
+{
+ const ControlList &props = camera->properties();
+
+ if (auto model = props.get(properties::Model))
+ return std::move(model.value());
+
+ return camera->id();
+}
+
+static const char *cameraLoc(const Camera *camera)
+{
+ const ControlList &props = camera->properties();
+
+ if (auto location = props.get(properties::Location)) {
+ switch (location.value()) {
+ case properties::CameraLocationFront:
+ return "front";
+ case properties::CameraLocationBack:
+ return "back";
+ case properties::CameraLocationExternal:
+ return "external";
+ }
+ }
+
+ return nullptr;
+}
+
+static int emit_info(struct impl *impl, bool full)
+{
+ struct spa_dict_item items[10];
+ struct spa_dict dict;
+ uint32_t n_items = 0;
+ struct spa_device_info info;
+ struct spa_param_info params[2];
+ char path[256], model[256], name[256];
+
+ info = SPA_DEVICE_INFO_INIT();
+
+ info.change_mask = SPA_DEVICE_CHANGE_MASK_PROPS;
+
+#define ADD_ITEM(key, value) items[n_items++] = SPA_DICT_ITEM_INIT(key, value)
+ snprintf(path, sizeof(path), "libcamera:%s", impl->device_id.c_str());
+ ADD_ITEM(SPA_KEY_OBJECT_PATH, path);
+ ADD_ITEM(SPA_KEY_DEVICE_API, "libcamera");
+ ADD_ITEM(SPA_KEY_MEDIA_CLASS, "Video/Device");
+ ADD_ITEM(SPA_KEY_API_LIBCAMERA_PATH, impl->device_id.c_str());
+
+ if (auto location = cameraLoc(impl->camera.get()))
+ ADD_ITEM(SPA_KEY_API_LIBCAMERA_LOCATION, location);
+
+ snprintf(model, sizeof(model), "%s", cameraModel(impl->camera.get()).c_str());
+ ADD_ITEM(SPA_KEY_DEVICE_PRODUCT_NAME, model);
+ ADD_ITEM(SPA_KEY_DEVICE_DESCRIPTION, model);
+ snprintf(name, sizeof(name), "libcamera_device.%s", impl->device_id.c_str());
+ ADD_ITEM(SPA_KEY_DEVICE_NAME, name);
+#undef ADD_ITEM
+
+ dict = SPA_DICT_INIT(items, n_items);
+ info.props = &dict;
+
+ info.change_mask |= SPA_DEVICE_CHANGE_MASK_PARAMS;
+ params[0] = SPA_PARAM_INFO(SPA_PARAM_EnumProfile, SPA_PARAM_INFO_READ);
+ params[1] = SPA_PARAM_INFO(SPA_PARAM_Profile, SPA_PARAM_INFO_WRITE);
+ info.n_params = SPA_N_ELEMENTS(params);
+ info.params = params;
+
+ spa_device_emit_info(&impl->hooks, &info);
+
+ if (true) {
+ struct spa_device_object_info oinfo;
+
+ oinfo = SPA_DEVICE_OBJECT_INFO_INIT();
+ oinfo.type = SPA_TYPE_INTERFACE_Node;
+ oinfo.factory_name = SPA_NAME_API_LIBCAMERA_SOURCE;
+ oinfo.change_mask = SPA_DEVICE_OBJECT_CHANGE_MASK_PROPS;
+ oinfo.props = &dict;
+
+ spa_device_emit_object_info(&impl->hooks, 0, &oinfo);
+ }
+ return 0;
+}
+
+static int impl_add_listener(void *object,
+ struct spa_hook *listener,
+ const struct spa_device_events *events,
+ void *data)
+{
+ struct impl *impl = (struct impl*)object;
+ struct spa_hook_list save;
+ int res = 0;
+
+ spa_return_val_if_fail(impl != NULL, -EINVAL);
+ spa_return_val_if_fail(events != NULL, -EINVAL);
+
+ spa_hook_list_isolate(&impl->hooks, &save, listener, events, data);
+
+ if (events->info || events->object_info)
+ res = emit_info(impl, true);
+
+ spa_hook_list_join(&impl->hooks, &save);
+
+ return res;
+}
+
+static int impl_sync(void *object, int seq)
+{
+ struct impl *impl = (struct impl*) object;
+
+ spa_return_val_if_fail(impl != NULL, -EINVAL);
+
+ spa_device_emit_result(&impl->hooks, seq, 0, 0, NULL);
+
+ return 0;
+}
+
+static int impl_enum_params(void *object, int seq,
+ uint32_t id, uint32_t start, uint32_t num,
+ const struct spa_pod *filter)
+{
+ return -ENOTSUP;
+}
+
+static int impl_set_param(void *object,
+ uint32_t id, uint32_t flags,
+ const struct spa_pod *param)
+{
+ return -ENOTSUP;
+}
+
+static const struct spa_device_methods impl_device = {
+ SPA_VERSION_DEVICE_METHODS,
+ .add_listener = impl_add_listener,
+ .sync = impl_sync,
+ .enum_params = impl_enum_params,
+ .set_param = impl_set_param,
+};
+
+static int impl_get_interface(struct spa_handle *handle, const char *type, void **interface)
+{
+ struct impl *impl;
+
+ spa_return_val_if_fail(handle != NULL, -EINVAL);
+ spa_return_val_if_fail(interface != NULL, -EINVAL);
+
+ impl = (struct impl *) handle;
+
+ if (spa_streq(type, SPA_TYPE_INTERFACE_Device))
+ *interface = &impl->device;
+ else
+ return -ENOENT;
+
+ return 0;
+}
+
+static int impl_clear(struct spa_handle *handle)
+{
+ std::destroy_at(reinterpret_cast<impl *>(handle));
+ return 0;
+}
+
+impl::impl(spa_log *log,
+ std::shared_ptr<CameraManager> manager,
+ std::shared_ptr<Camera> camera,
+ std::string device_id)
+ : handle({ SPA_VERSION_HANDLE, impl_get_interface, impl_clear }),
+ log(log),
+ device_id(std::move(device_id)),
+ manager(std::move(manager)),
+ camera(std::move(camera))
+{
+ libcamera_log_topic_init(log);
+
+ spa_hook_list_init(&hooks);
+
+ device.iface = SPA_INTERFACE_INIT(
+ SPA_TYPE_INTERFACE_Device,
+ SPA_VERSION_DEVICE,
+ &impl_device, this);
+}
+
+static size_t
+impl_get_size(const struct spa_handle_factory *factory,
+ const struct spa_dict *params)
+{
+ return sizeof(struct impl);
+}
+
+static int
+impl_init(const struct spa_handle_factory *factory,
+ struct spa_handle *handle,
+ const struct spa_dict *info,
+ const struct spa_support *support,
+ uint32_t n_support)
+{
+ const char *str;
+ int res;
+
+ spa_return_val_if_fail(factory != NULL, -EINVAL);
+ spa_return_val_if_fail(handle != NULL, -EINVAL);
+
+ auto log = static_cast<spa_log *>(spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log));
+
+ auto manager = libcamera_manager_acquire(res);
+ if (!manager) {
+ spa_log_error(log, "can't start camera manager: %s", spa_strerror(res));
+ return res;
+ }
+
+ std::string device_id;
+ if (info && (str = spa_dict_lookup(info, SPA_KEY_API_LIBCAMERA_PATH)))
+ device_id = str;
+
+ auto camera = manager->get(device_id);
+ if (!camera) {
+ spa_log_error(log, "unknown camera id %s", device_id.c_str());
+ return -ENOENT;
+ }
+
+ new (handle) impl(log, std::move(manager), std::move(camera), std::move(device_id));
+
+ return 0;
+}
+
+static const struct spa_interface_info impl_interfaces[] = {
+ {SPA_TYPE_INTERFACE_Device,},
+};
+
+static int impl_enum_interface_info(const struct spa_handle_factory *factory,
+ const struct spa_interface_info **info,
+ uint32_t *index)
+{
+ spa_return_val_if_fail(factory != NULL, -EINVAL);
+ spa_return_val_if_fail(info != NULL, -EINVAL);
+ spa_return_val_if_fail(index != NULL, -EINVAL);
+
+ if (*index >= SPA_N_ELEMENTS(impl_interfaces))
+ return 0;
+
+ *info = &impl_interfaces[(*index)++];
+ return 1;
+}
+
+extern "C" {
+const struct spa_handle_factory spa_libcamera_device_factory = {
+ SPA_VERSION_HANDLE_FACTORY,
+ SPA_NAME_API_LIBCAMERA_DEVICE,
+ NULL,
+ impl_get_size,
+ impl_init,
+ impl_enum_interface_info,
+};
+}
diff --git a/spa/plugins/libcamera/libcamera-manager.cpp b/spa/plugins/libcamera/libcamera-manager.cpp
new file mode 100644
index 0000000..afba403
--- /dev/null
+++ b/spa/plugins/libcamera/libcamera-manager.cpp
@@ -0,0 +1,488 @@
+/* Spa libcamera manager
+ *
+ * Copyright © 2021 Wim Taymans
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <stddef.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <limits.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <utility>
+#include <mutex>
+#include <optional>
+#include <queue>
+
+#include <libcamera/camera.h>
+#include <libcamera/camera_manager.h>
+
+#include <linux/videodev2.h>
+
+using namespace libcamera;
+
+#include <spa/support/log.h>
+#include <spa/utils/type.h>
+#include <spa/utils/keys.h>
+#include <spa/utils/names.h>
+#include <spa/utils/result.h>
+#include <spa/utils/string.h>
+#include <spa/support/loop.h>
+#include <spa/support/plugin.h>
+#include <spa/monitor/device.h>
+#include <spa/monitor/utils.h>
+
+#include "libcamera.h"
+#include "libcamera-manager.hpp"
+
+#define MAX_DEVICES 64
+
+namespace {
+
+struct device {
+ uint32_t id;
+ std::shared_ptr<Camera> camera;
+};
+
+struct impl {
+ struct spa_handle handle;
+ struct spa_device device = {};
+
+ struct spa_log *log;
+ struct spa_loop_utils *loop_utils;
+
+ struct spa_hook_list hooks;
+
+ static constexpr uint64_t info_all = SPA_DEVICE_CHANGE_MASK_FLAGS | SPA_DEVICE_CHANGE_MASK_PROPS;
+ struct spa_device_info info = SPA_DEVICE_INFO_INIT();
+
+ std::shared_ptr<CameraManager> manager;
+ void addCamera(std::shared_ptr<libcamera::Camera> camera);
+ void removeCamera(std::shared_ptr<libcamera::Camera> camera);
+
+ struct device devices[MAX_DEVICES];
+ uint32_t n_devices = 0;
+
+ struct hotplug_event {
+ enum class type { add, remove } type;
+ std::shared_ptr<Camera> camera;
+ };
+
+ std::mutex hotplug_events_lock;
+ std::queue<hotplug_event> hotplug_events;
+ struct spa_source *hotplug_event_source;
+
+ impl(spa_log *log, spa_loop_utils *loop_utils, spa_source *hotplug_event_source);
+
+ ~impl()
+ {
+ spa_loop_utils_destroy_source(loop_utils, hotplug_event_source);
+ }
+};
+
+}
+
+static std::weak_ptr<CameraManager> global_manager;
+
+std::shared_ptr<CameraManager> libcamera_manager_acquire(int& res)
+{
+ if (auto manager = global_manager.lock())
+ return manager;
+
+ auto manager = std::make_shared<CameraManager>();
+ if ((res = manager->start()) < 0)
+ return {};
+
+ global_manager = manager;
+
+ return manager;
+}
+static uint32_t get_free_id(struct impl *impl)
+{
+ for (std::size_t i = 0; i < MAX_DEVICES; i++)
+ if (impl->devices[i].camera == nullptr)
+ return i;
+ return 0;
+}
+
+static struct device *add_device(struct impl *impl, std::shared_ptr<Camera> camera)
+{
+ struct device *device;
+ uint32_t id;
+
+ if (impl->n_devices >= MAX_DEVICES)
+ return NULL;
+ id = get_free_id(impl);;
+ device = &impl->devices[id];
+ device->id = get_free_id(impl);;
+ device->camera = std::move(camera);
+ impl->n_devices++;
+ return device;
+}
+
+static struct device *find_device(struct impl *impl, const Camera *camera)
+{
+ uint32_t i;
+ for (i = 0; i < impl->n_devices; i++) {
+ if (impl->devices[i].camera.get() == camera)
+ return &impl->devices[i];
+ }
+ return NULL;
+}
+
+static void remove_device(struct impl *impl, struct device *device)
+{
+ uint32_t old = --impl->n_devices;
+ device->camera.reset();
+ *device = std::move(impl->devices[old]);
+ impl->devices[old].camera = nullptr;
+}
+
+static void clear_devices(struct impl *impl)
+{
+ while (impl->n_devices > 0)
+ impl->devices[--impl->n_devices].camera.reset();
+}
+
+static int emit_object_info(struct impl *impl, struct device *device)
+{
+ struct spa_device_object_info info;
+ uint32_t id = device->id;
+ struct spa_dict_item items[20];
+ struct spa_dict dict;
+ uint32_t n_items = 0;
+ char path[256];
+
+ info = SPA_DEVICE_OBJECT_INFO_INIT();
+
+ info.type = SPA_TYPE_INTERFACE_Device;
+ info.factory_name = SPA_NAME_API_LIBCAMERA_DEVICE;
+ info.change_mask = SPA_DEVICE_OBJECT_CHANGE_MASK_FLAGS |
+ SPA_DEVICE_OBJECT_CHANGE_MASK_PROPS;
+ info.flags = 0;
+
+#define ADD_ITEM(key, value) items[n_items++] = SPA_DICT_ITEM_INIT(key, value)
+ ADD_ITEM(SPA_KEY_DEVICE_ENUM_API,"libcamera.manager");
+ ADD_ITEM(SPA_KEY_DEVICE_API, "libcamera");
+ ADD_ITEM(SPA_KEY_MEDIA_CLASS, "Video/Device");
+ snprintf(path, sizeof(path), "%s", device->camera->id().c_str());
+ ADD_ITEM(SPA_KEY_API_LIBCAMERA_PATH, path);
+#undef ADD_ITEM
+
+ dict = SPA_DICT_INIT(items, n_items);
+ info.props = &dict;
+ spa_device_emit_object_info(&impl->hooks, id, &info);
+
+ return 1;
+}
+
+static void try_add_camera(struct impl *impl, std::shared_ptr<Camera> camera)
+{
+ struct device *device;
+
+ if ((device = find_device(impl, camera.get())) != NULL)
+ return;
+
+ if ((device = add_device(impl, std::move(camera))) == NULL)
+ return;
+
+ spa_log_info(impl->log, "camera added: id:%d %s", device->id,
+ device->camera->id().c_str());
+ emit_object_info(impl, device);
+}
+
+static void try_remove_camera(struct impl *impl, const Camera *camera)
+{
+ struct device *device;
+
+ if ((device = find_device(impl, camera)) == NULL)
+ return;
+
+ spa_log_info(impl->log, "camera removed: id:%d %s", device->id,
+ device->camera->id().c_str());
+ spa_device_emit_object_info(&impl->hooks, device->id, NULL);
+ remove_device(impl, device);
+}
+
+static void consume_hotplug_event(struct impl *impl, impl::hotplug_event& event)
+{
+ auto& [ type, camera ] = event;
+
+ switch (type) {
+ case impl::hotplug_event::type::add:
+ spa_log_info(impl->log, "camera appeared: %s", camera->id().c_str());
+ try_add_camera(impl, std::move(camera));
+ break;
+ case impl::hotplug_event::type::remove:
+ spa_log_info(impl->log, "camera disappeared: %s", camera->id().c_str());
+ try_remove_camera(impl, camera.get());
+ break;
+ }
+}
+
+static void on_hotplug_event(void *data, std::uint64_t)
+{
+ auto impl = static_cast<struct impl *>(data);
+
+ for (;;) {
+ std::optional<impl::hotplug_event> event;
+
+ {
+ std::unique_lock guard(impl->hotplug_events_lock);
+
+ if (!impl->hotplug_events.empty()) {
+ event = std::move(impl->hotplug_events.front());
+ impl->hotplug_events.pop();
+ }
+ }
+
+ if (!event)
+ break;
+
+ consume_hotplug_event(impl, *event);
+ }
+}
+
+void impl::addCamera(std::shared_ptr<Camera> camera)
+{
+ {
+ std::unique_lock guard(hotplug_events_lock);
+ hotplug_events.push({ hotplug_event::type::add, std::move(camera) });
+ }
+
+ spa_loop_utils_signal_event(loop_utils, hotplug_event_source);
+}
+
+void impl::removeCamera(std::shared_ptr<Camera> camera)
+{
+ {
+ std::unique_lock guard(hotplug_events_lock);
+ hotplug_events.push({ hotplug_event::type::remove, std::move(camera) });
+ }
+
+ spa_loop_utils_signal_event(loop_utils, hotplug_event_source);
+}
+
+static void start_monitor(struct impl *impl)
+{
+ impl->manager->cameraAdded.connect(impl, &impl::addCamera);
+ impl->manager->cameraRemoved.connect(impl, &impl::removeCamera);
+}
+
+static int stop_monitor(struct impl *impl)
+{
+ if (impl->manager) {
+ impl->manager->cameraAdded.disconnect(impl, &impl::addCamera);
+ impl->manager->cameraRemoved.disconnect(impl, &impl::removeCamera);
+ }
+ clear_devices (impl);
+ return 0;
+}
+
+static void collect_existing_devices(struct impl *impl)
+{
+ auto cameras = impl->manager->cameras();
+
+ for (std::shared_ptr<Camera>& camera : cameras)
+ try_add_camera(impl, std::move(camera));
+}
+
+static const struct spa_dict_item device_info_items[] = {
+ { SPA_KEY_DEVICE_API, "libcamera" },
+ { SPA_KEY_DEVICE_NICK, "libcamera-manager" },
+};
+
+static void emit_device_info(struct impl *impl, bool full)
+{
+ uint64_t old = full ? impl->info.change_mask : 0;
+ if (full)
+ impl->info.change_mask = impl->info_all;
+ if (impl->info.change_mask) {
+ struct spa_dict dict;
+ dict = SPA_DICT_INIT_ARRAY(device_info_items);
+ impl->info.props = &dict;
+ spa_device_emit_info(&impl->hooks, &impl->info);
+ impl->info.change_mask = old;
+ }
+}
+
+static void impl_hook_removed(struct spa_hook *hook)
+{
+ struct impl *impl = (struct impl*)hook->priv;
+ if (spa_hook_list_is_empty(&impl->hooks)) {
+ stop_monitor(impl);
+ impl->manager.reset();
+ }
+}
+
+static int
+impl_device_add_listener(void *object, struct spa_hook *listener,
+ const struct spa_device_events *events, void *data)
+{
+ int res;
+ struct impl *impl = (struct impl*) object;
+ struct spa_hook_list save;
+ bool had_manager = !!impl->manager;
+
+ spa_return_val_if_fail(impl != NULL, -EINVAL);
+ spa_return_val_if_fail(events != NULL, -EINVAL);
+
+ if (!impl->manager && !(impl->manager = libcamera_manager_acquire(res)))
+ return res;
+
+ spa_hook_list_isolate(&impl->hooks, &save, listener, events, data);
+
+ emit_device_info(impl, true);
+
+ if (had_manager) {
+ for (std::size_t i = 0; i < impl->n_devices; i++)
+ emit_object_info(impl, &impl->devices[i]);
+ }
+ else {
+ collect_existing_devices(impl);
+ start_monitor(impl);
+ }
+
+ spa_hook_list_join(&impl->hooks, &save);
+
+ listener->removed = impl_hook_removed;
+ listener->priv = impl;
+
+ return 0;
+}
+
+static const struct spa_device_methods impl_device = {
+ SPA_VERSION_DEVICE_METHODS,
+ .add_listener = impl_device_add_listener,
+};
+
+static int impl_get_interface(struct spa_handle *handle, const char *type, void **interface)
+{
+ struct impl *impl;
+
+ spa_return_val_if_fail(handle != NULL, -EINVAL);
+ spa_return_val_if_fail(interface != NULL, -EINVAL);
+
+ impl = (struct impl *) handle;
+
+ if (spa_streq(type, SPA_TYPE_INTERFACE_Device))
+ *interface = &impl->device;
+ else
+ return -ENOENT;
+
+ return 0;
+}
+
+static int impl_clear(struct spa_handle *handle)
+{
+ auto impl = reinterpret_cast<struct impl *>(handle);
+
+ stop_monitor(impl);
+ std::destroy_at(impl);
+
+ return 0;
+}
+
+impl::impl(spa_log *log, spa_loop_utils *loop_utils, spa_source *hotplug_event_source)
+ : handle({ SPA_VERSION_HANDLE, impl_get_interface, impl_clear }),
+ log(log),
+ loop_utils(loop_utils),
+ hotplug_event_source(hotplug_event_source)
+{
+ libcamera_log_topic_init(log);
+
+ spa_hook_list_init(&hooks);
+
+ device.iface = SPA_INTERFACE_INIT(
+ SPA_TYPE_INTERFACE_Device,
+ SPA_VERSION_DEVICE,
+ &impl_device, this);
+}
+
+static size_t
+impl_get_size(const struct spa_handle_factory *factory,
+ const struct spa_dict *params)
+{
+ return sizeof(struct impl);
+}
+
+static int
+impl_init(const struct spa_handle_factory *factory,
+ struct spa_handle *handle,
+ const struct spa_dict *info,
+ const struct spa_support *support,
+ uint32_t n_support)
+{
+ spa_return_val_if_fail(factory != NULL, -EINVAL);
+ spa_return_val_if_fail(handle != NULL, -EINVAL);
+
+ auto log = static_cast<spa_log *>(spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log));
+
+ auto loop_utils = static_cast<spa_loop_utils *>(spa_support_find(support, n_support, SPA_TYPE_INTERFACE_LoopUtils));
+ if (!loop_utils) {
+ spa_log_error(log, "a " SPA_TYPE_INTERFACE_LoopUtils " is needed");
+ return -EINVAL;
+ }
+
+ auto hotplug_event_source = spa_loop_utils_add_event(loop_utils, on_hotplug_event, handle);
+ if (!hotplug_event_source) {
+ int res = -errno;
+ spa_log_error(log, "failed to create hotplug event: %m");
+ return res;
+ }
+
+ new (handle) impl(log, loop_utils, hotplug_event_source);
+
+ return 0;
+}
+
+static const struct spa_interface_info impl_interfaces[] = {
+ {SPA_TYPE_INTERFACE_Device,},
+};
+
+static int
+impl_enum_interface_info(const struct spa_handle_factory *factory,
+ const struct spa_interface_info **info,
+ uint32_t *index)
+{
+ spa_return_val_if_fail(factory != NULL, -EINVAL);
+ spa_return_val_if_fail(info != NULL, -EINVAL);
+ spa_return_val_if_fail(index != NULL, -EINVAL);
+
+ if (*index >= SPA_N_ELEMENTS(impl_interfaces))
+ return 0;
+
+ *info = &impl_interfaces[(*index)++];
+ return 1;
+}
+
+extern "C" {
+const struct spa_handle_factory spa_libcamera_manager_factory = {
+ SPA_VERSION_HANDLE_FACTORY,
+ SPA_NAME_API_LIBCAMERA_ENUM_MANAGER,
+ NULL,
+ impl_get_size,
+ impl_init,
+ impl_enum_interface_info,
+};
+}
diff --git a/spa/plugins/libcamera/libcamera-manager.hpp b/spa/plugins/libcamera/libcamera-manager.hpp
new file mode 100644
index 0000000..4336a39
--- /dev/null
+++ b/spa/plugins/libcamera/libcamera-manager.hpp
@@ -0,0 +1,29 @@
+/* Spa libcamera support
+ *
+ * Copyright © 2021 wim Taymans <wim.taymans@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <memory>
+
+#include <libcamera/camera_manager.h>
+
+std::shared_ptr<libcamera::CameraManager> libcamera_manager_acquire(int& res);
diff --git a/spa/plugins/libcamera/libcamera-source.cpp b/spa/plugins/libcamera/libcamera-source.cpp
new file mode 100644
index 0000000..d2ca670
--- /dev/null
+++ b/spa/plugins/libcamera/libcamera-source.cpp
@@ -0,0 +1,1073 @@
+/* Spa libcamera Source
+ *
+ * Copyright (C) 2020, Collabora Ltd.
+ * Author: Raghavendra Rao Sidlagatta <raghavendra.rao@collabora.com>
+ * Copyright (C) 2021 Wim Taymans <wim.taymans@gmail.com>
+ *
+ * libcamera-source.cpp
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <stddef.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <deque>
+#include <optional>
+
+#include <spa/support/plugin.h>
+#include <spa/support/log.h>
+#include <spa/support/loop.h>
+#include <spa/utils/list.h>
+#include <spa/utils/keys.h>
+#include <spa/utils/names.h>
+#include <spa/utils/result.h>
+#include <spa/utils/string.h>
+#include <spa/utils/ringbuffer.h>
+#include <spa/monitor/device.h>
+#include <spa/node/node.h>
+#include <spa/node/io.h>
+#include <spa/node/utils.h>
+#include <spa/node/keys.h>
+#include <spa/param/video/format-utils.h>
+#include <spa/param/param.h>
+#include <spa/control/control.h>
+#include <spa/pod/filter.h>
+
+#include <libcamera/camera.h>
+#include <libcamera/stream.h>
+#include <libcamera/formats.h>
+#include <libcamera/framebuffer.h>
+#include <libcamera/framebuffer_allocator.h>
+
+#include "libcamera.h"
+#include "libcamera-manager.hpp"
+
+using namespace libcamera;
+
+namespace {
+
+#define MAX_BUFFERS 32
+#define MASK_BUFFERS 31
+
+#define BUFFER_FLAG_OUTSTANDING (1<<0)
+#define BUFFER_FLAG_ALLOCATED (1<<1)
+#define BUFFER_FLAG_MAPPED (1<<2)
+
+struct buffer {
+ uint32_t id;
+ uint32_t flags;
+ struct spa_list link;
+ struct spa_buffer *outbuf;
+ struct spa_meta_header *h;
+ struct spa_meta_videotransform *videotransform;
+ void *ptr;
+};
+
+#define MAX_CONTROLS 64
+
+struct control {
+ uint32_t id;
+ uint32_t ctrl_id;
+ double value;
+};
+
+struct port {
+ struct impl *impl;
+
+ std::optional<spa_video_info> current_format;
+
+ struct spa_fraction rate = {};
+ StreamConfiguration streamConfig;
+
+ uint32_t memtype = 0;
+
+ struct control controls[MAX_CONTROLS];
+ uint32_t n_controls = 0;
+
+ struct buffer buffers[MAX_BUFFERS];
+ uint32_t n_buffers = 0;
+ struct spa_list queue;
+ struct spa_ringbuffer ring = SPA_RINGBUFFER_INIT();
+ uint32_t ring_ids[MAX_BUFFERS];
+
+ static constexpr uint64_t info_all = SPA_PORT_CHANGE_MASK_FLAGS | SPA_PORT_CHANGE_MASK_PARAMS;
+ struct spa_port_info info = SPA_PORT_INFO_INIT();
+ struct spa_io_buffers *io = nullptr;
+ struct spa_io_sequence *control = nullptr;
+#define PORT_PropInfo 0
+#define PORT_EnumFormat 1
+#define PORT_Meta 2
+#define PORT_IO 3
+#define PORT_Format 4
+#define PORT_Buffers 5
+#define N_PORT_PARAMS 6
+ struct spa_param_info params[N_PORT_PARAMS];
+
+ uint32_t fmt_index = 0;
+ PixelFormat enum_fmt;
+ uint32_t size_index = 0;
+
+ port(struct impl *impl)
+ : impl(impl)
+ {
+ spa_list_init(&queue);
+
+ params[PORT_PropInfo] = SPA_PARAM_INFO(SPA_PARAM_PropInfo, SPA_PARAM_INFO_READ);
+ params[PORT_EnumFormat] = SPA_PARAM_INFO(SPA_PARAM_EnumFormat, SPA_PARAM_INFO_READ);
+ params[PORT_Meta] = SPA_PARAM_INFO(SPA_PARAM_Meta, SPA_PARAM_INFO_READ);
+ params[PORT_IO] = SPA_PARAM_INFO(SPA_PARAM_IO, SPA_PARAM_INFO_READ);
+ params[PORT_Format] = SPA_PARAM_INFO(SPA_PARAM_Format, SPA_PARAM_INFO_WRITE);
+ params[PORT_Buffers] = SPA_PARAM_INFO(SPA_PARAM_Buffers, 0);
+
+ info.flags = SPA_PORT_FLAG_LIVE | SPA_PORT_FLAG_PHYSICAL | SPA_PORT_FLAG_TERMINAL;
+ info.params = params;
+ info.n_params = N_PORT_PARAMS;
+ }
+};
+
+struct impl {
+ struct spa_handle handle;
+ struct spa_node node = {};
+
+ struct spa_log *log;
+ struct spa_loop *data_loop;
+ struct spa_system *system;
+
+ static constexpr uint64_t info_all =
+ SPA_NODE_CHANGE_MASK_FLAGS |
+ SPA_NODE_CHANGE_MASK_PROPS |
+ SPA_NODE_CHANGE_MASK_PARAMS;
+ struct spa_node_info info = SPA_NODE_INFO_INIT();
+#define NODE_PropInfo 0
+#define NODE_Props 1
+#define NODE_EnumFormat 2
+#define NODE_Format 3
+#define N_NODE_PARAMS 4
+ struct spa_param_info params[N_NODE_PARAMS];
+
+ std::string device_id;
+ std::string device_name;
+
+ struct spa_hook_list hooks;
+ struct spa_callbacks callbacks = {};
+
+ std::array<port, 1> out_ports;
+
+ struct spa_io_position *position = nullptr;
+ struct spa_io_clock *clock = nullptr;
+
+ std::shared_ptr<CameraManager> manager;
+ std::shared_ptr<Camera> camera;
+
+ FrameBufferAllocator *allocator = nullptr;
+ std::vector<std::unique_ptr<libcamera::Request>> requestPool;
+ std::deque<libcamera::Request *> pendingRequests;
+
+ void requestComplete(libcamera::Request *request);
+
+ std::unique_ptr<CameraConfiguration> config;
+
+ struct spa_source source = {};
+
+ ControlList ctrls;
+ bool active = false;
+ bool acquired = false;
+
+ impl(spa_log *log, spa_loop *data_loop, spa_system *system,
+ std::shared_ptr<CameraManager> manager, std::shared_ptr<Camera> camera, std::string device_id);
+};
+
+}
+
+#define CHECK_PORT(impl,direction,port_id) ((direction) == SPA_DIRECTION_OUTPUT && (port_id) == 0)
+
+#define GET_OUT_PORT(impl,p) (&impl->out_ports[p])
+#define GET_PORT(impl,d,p) GET_OUT_PORT(impl,p)
+
+#include "libcamera-utils.cpp"
+
+static int port_get_format(struct impl *impl, struct port *port,
+ uint32_t index,
+ const struct spa_pod *filter,
+ struct spa_pod **param,
+ struct spa_pod_builder *builder)
+{
+ struct spa_pod_frame f;
+
+ if (!port->current_format)
+ return -EIO;
+ if (index > 0)
+ return 0;
+
+ spa_pod_builder_push_object(builder, &f, SPA_TYPE_OBJECT_Format, SPA_PARAM_Format);
+ spa_pod_builder_add(builder,
+ SPA_FORMAT_mediaType, SPA_POD_Id(port->current_format->media_type),
+ SPA_FORMAT_mediaSubtype, SPA_POD_Id(port->current_format->media_subtype),
+ 0);
+
+ switch (port->current_format->media_subtype) {
+ case SPA_MEDIA_SUBTYPE_raw:
+ spa_pod_builder_add(builder,
+ SPA_FORMAT_VIDEO_format, SPA_POD_Id(port->current_format->info.raw.format),
+ SPA_FORMAT_VIDEO_size, SPA_POD_Rectangle(&port->current_format->info.raw.size),
+ SPA_FORMAT_VIDEO_framerate, SPA_POD_Fraction(&port->current_format->info.raw.framerate),
+ 0);
+ break;
+ case SPA_MEDIA_SUBTYPE_mjpg:
+ case SPA_MEDIA_SUBTYPE_jpeg:
+ spa_pod_builder_add(builder,
+ SPA_FORMAT_VIDEO_size, SPA_POD_Rectangle(&port->current_format->info.mjpg.size),
+ SPA_FORMAT_VIDEO_framerate, SPA_POD_Fraction(&port->current_format->info.mjpg.framerate),
+ 0);
+ break;
+ case SPA_MEDIA_SUBTYPE_h264:
+ spa_pod_builder_add(builder,
+ SPA_FORMAT_VIDEO_size, SPA_POD_Rectangle(&port->current_format->info.h264.size),
+ SPA_FORMAT_VIDEO_framerate, SPA_POD_Fraction(&port->current_format->info.h264.framerate),
+ 0);
+ break;
+ default:
+ return -EIO;
+ }
+
+ *param = (struct spa_pod*)spa_pod_builder_pop(builder, &f);
+
+ return 1;
+}
+
+static int impl_node_enum_params(void *object, int seq,
+ uint32_t id, uint32_t start, uint32_t num,
+ const struct spa_pod *filter)
+{
+ struct impl *impl = (struct impl*)object;
+ struct spa_pod *param;
+ struct spa_pod_builder b = { 0 };
+ uint8_t buffer[1024];
+ struct spa_result_node_params result;
+ uint32_t count = 0;
+ int res;
+
+ spa_return_val_if_fail(impl != NULL, -EINVAL);
+ spa_return_val_if_fail(num != 0, -EINVAL);
+
+ result.id = id;
+ result.next = start;
+next:
+ result.index = result.next++;
+
+ spa_pod_builder_init(&b, buffer, sizeof(buffer));
+
+ switch (id) {
+ case SPA_PARAM_PropInfo:
+ {
+ switch (result.index) {
+ case 0:
+ param = (struct spa_pod*)spa_pod_builder_add_object(&b,
+ SPA_TYPE_OBJECT_PropInfo, id,
+ SPA_PROP_INFO_id, SPA_POD_Id(SPA_PROP_device),
+ SPA_PROP_INFO_description, SPA_POD_String("The libcamera device"),
+ SPA_PROP_INFO_type, SPA_POD_String(impl->device_id.c_str()));
+ break;
+ case 1:
+ param = (struct spa_pod*)spa_pod_builder_add_object(&b,
+ SPA_TYPE_OBJECT_PropInfo, id,
+ SPA_PROP_INFO_id, SPA_POD_Id(SPA_PROP_deviceName),
+ SPA_PROP_INFO_description, SPA_POD_String("The libcamera device name"),
+ SPA_PROP_INFO_type, SPA_POD_String(impl->device_name.c_str()));
+ break;
+ default:
+ return spa_libcamera_enum_controls(impl,
+ GET_OUT_PORT(impl, 0),
+ seq, result.index - 2, num, filter);
+ }
+ break;
+ }
+ case SPA_PARAM_Props:
+ {
+ switch (result.index) {
+ case 0:
+ param = (struct spa_pod*)spa_pod_builder_add_object(&b,
+ SPA_TYPE_OBJECT_Props, id,
+ SPA_PROP_device, SPA_POD_String(impl->device_id.c_str()),
+ SPA_PROP_deviceName, SPA_POD_String(impl->device_name.c_str()));
+ break;
+ default:
+ return 0;
+ }
+ break;
+ }
+ case SPA_PARAM_EnumFormat:
+ return spa_libcamera_enum_format(impl, GET_OUT_PORT(impl, 0),
+ seq, start, num, filter);
+ case SPA_PARAM_Format:
+ if ((res = port_get_format(impl, GET_OUT_PORT(impl, 0), result.index, filter, &param, &b)) <= 0)
+ return res;
+ break;
+ default:
+ return -ENOENT;
+ }
+
+ if (spa_pod_filter(&b, &result.param, param, filter) < 0)
+ goto next;
+
+ spa_node_emit_result(&impl->hooks, seq, 0, SPA_RESULT_TYPE_NODE_PARAMS, &result);
+
+ if (++count != num)
+ goto next;
+
+ return 0;
+}
+
+static int impl_node_set_param(void *object,
+ uint32_t id, uint32_t flags,
+ const struct spa_pod *param)
+{
+ struct impl *impl = (struct impl*)object;
+
+ spa_return_val_if_fail(impl != NULL, -EINVAL);
+
+ switch (id) {
+ case SPA_PARAM_Props:
+ {
+ struct spa_pod_object *obj = (struct spa_pod_object *) param;
+ struct spa_pod_prop *prop;
+
+ if (param == NULL) {
+ impl->device_id.clear();
+ impl->device_name.clear();
+ return 0;
+ }
+ SPA_POD_OBJECT_FOREACH(obj, prop) {
+ char device[128];
+
+ switch (prop->key) {
+ case SPA_PROP_device:
+ strncpy(device, (char *)SPA_POD_CONTENTS(struct spa_pod_string, &prop->value),
+ sizeof(device)-1);
+ impl->device_id = device;
+ break;
+ default:
+ spa_libcamera_set_control(impl, prop);
+ break;
+ }
+ }
+ break;
+ }
+ default:
+ return -ENOENT;
+ }
+ return 0;
+}
+
+static int impl_node_set_io(void *object, uint32_t id, void *data, size_t size)
+{
+ struct impl *impl = (struct impl*)object;
+
+ spa_return_val_if_fail(impl != NULL, -EINVAL);
+
+ switch (id) {
+ case SPA_IO_Clock:
+ impl->clock = (struct spa_io_clock*)data;
+ break;
+ case SPA_IO_Position:
+ impl->position = (struct spa_io_position*)data;
+ break;
+ default:
+ return -ENOENT;
+ }
+ return 0;
+}
+
+static int impl_node_send_command(void *object, const struct spa_command *command)
+{
+ struct impl *impl = (struct impl*)object;
+ int res;
+
+ spa_return_val_if_fail(impl != NULL, -EINVAL);
+ spa_return_val_if_fail(command != NULL, -EINVAL);
+
+ switch (SPA_NODE_COMMAND_ID(command)) {
+ case SPA_NODE_COMMAND_Start:
+ {
+ struct port *port = GET_OUT_PORT(impl, 0);
+
+ if (!port->current_format)
+ return -EIO;
+ if (port->n_buffers == 0)
+ return -EIO;
+
+ if ((res = spa_libcamera_stream_on(impl)) < 0)
+ return res;
+ break;
+ }
+ case SPA_NODE_COMMAND_Pause:
+ case SPA_NODE_COMMAND_Suspend:
+ if ((res = spa_libcamera_stream_off(impl)) < 0)
+ return res;
+ break;
+ default:
+ return -ENOTSUP;
+ }
+
+ return 0;
+}
+
+static const struct spa_dict_item info_items[] = {
+ { SPA_KEY_DEVICE_API, "libcamera" },
+ { SPA_KEY_MEDIA_CLASS, "Video/Source" },
+ { SPA_KEY_MEDIA_ROLE, "Camera" },
+ { SPA_KEY_NODE_DRIVER, "true" },
+};
+
+static void emit_node_info(struct impl *impl, bool full)
+{
+ uint64_t old = full ? impl->info.change_mask : 0;
+ if (full)
+ impl->info.change_mask = impl->info_all;
+ if (impl->info.change_mask) {
+ struct spa_dict dict = SPA_DICT_INIT_ARRAY(info_items);
+ impl->info.props = &dict;
+ spa_node_emit_info(&impl->hooks, &impl->info);
+ impl->info.change_mask = old;
+ }
+}
+
+static void emit_port_info(struct impl *impl, struct port *port, bool full)
+{
+ uint64_t old = full ? port->info.change_mask : 0;
+ if (full)
+ port->info.change_mask = port->info_all;
+ if (port->info.change_mask) {
+ spa_node_emit_port_info(&impl->hooks,
+ SPA_DIRECTION_OUTPUT, 0, &port->info);
+ port->info.change_mask = old;
+ }
+}
+
+static int
+impl_node_add_listener(void *object,
+ struct spa_hook *listener,
+ const struct spa_node_events *events,
+ void *data)
+{
+ struct impl *impl = (struct impl*)object;
+ struct spa_hook_list save;
+
+ spa_return_val_if_fail(impl != NULL, -EINVAL);
+
+ spa_hook_list_isolate(&impl->hooks, &save, listener, events, data);
+
+ emit_node_info(impl, true);
+ emit_port_info(impl, GET_OUT_PORT(impl, 0), true);
+
+ spa_hook_list_join(&impl->hooks, &save);
+
+ return 0;
+}
+
+static int impl_node_set_callbacks(void *object,
+ const struct spa_node_callbacks *callbacks,
+ void *data)
+{
+ struct impl *impl = (struct impl*)object;
+
+ spa_return_val_if_fail(impl != NULL, -EINVAL);
+
+ impl->callbacks = SPA_CALLBACKS_INIT(callbacks, data);
+
+ return 0;
+}
+
+static int impl_node_sync(void *object, int seq)
+{
+ struct impl *impl = (struct impl*)object;
+
+ spa_return_val_if_fail(impl != NULL, -EINVAL);
+
+ spa_node_emit_result(&impl->hooks, seq, 0, 0, NULL);
+
+ return 0;
+}
+
+static int impl_node_add_port(void *object,
+ enum spa_direction direction,
+ uint32_t port_id, const struct spa_dict *props)
+{
+ return -ENOTSUP;
+}
+
+static int impl_node_remove_port(void *object,
+ enum spa_direction direction,
+ uint32_t port_id)
+{
+ return -ENOTSUP;
+}
+
+static int impl_node_port_enum_params(void *object, int seq,
+ enum spa_direction direction,
+ uint32_t port_id,
+ uint32_t id, uint32_t start, uint32_t num,
+ const struct spa_pod *filter)
+{
+
+ struct impl *impl = (struct impl*)object;
+ struct port *port;
+ struct spa_pod *param;
+ struct spa_pod_builder b = { 0 };
+ uint8_t buffer[1024];
+ struct spa_result_node_params result;
+ uint32_t count = 0;
+ int res;
+
+ spa_return_val_if_fail(impl != NULL, -EINVAL);
+ spa_return_val_if_fail(num != 0, -EINVAL);
+ spa_return_val_if_fail(CHECK_PORT(impl, direction, port_id), -EINVAL);
+
+ port = GET_PORT(impl, direction, port_id);
+
+ result.id = id;
+ result.next = start;
+next:
+ result.index = result.next++;
+
+ spa_pod_builder_init(&b, buffer, sizeof(buffer));
+
+ switch (id) {
+ case SPA_PARAM_PropInfo:
+ return spa_libcamera_enum_controls(impl, port, seq, start, num, filter);
+
+ case SPA_PARAM_EnumFormat:
+ return spa_libcamera_enum_format(impl, port, seq, start, num, filter);
+
+ case SPA_PARAM_Format:
+ if((res = port_get_format(impl, port, result.index, filter, &param, &b)) <= 0)
+ return res;
+ break;
+ case SPA_PARAM_Buffers:
+ {
+ if (!port->current_format)
+ return -EIO;
+ if (result.index > 0)
+ return 0;
+
+ /* Get the number of buffers to be used from libcamera and send the same to pipewire
+ * so that exact number of buffers are allocated
+ */
+ uint32_t n_buffers = port->streamConfig.bufferCount;
+
+ param = (struct spa_pod*)spa_pod_builder_add_object(&b,
+ SPA_TYPE_OBJECT_ParamBuffers, id,
+ SPA_PARAM_BUFFERS_buffers, SPA_POD_CHOICE_RANGE_Int(n_buffers, n_buffers, n_buffers),
+ SPA_PARAM_BUFFERS_blocks, SPA_POD_Int(1),
+ SPA_PARAM_BUFFERS_size, SPA_POD_Int(port->streamConfig.frameSize),
+ SPA_PARAM_BUFFERS_stride, SPA_POD_Int(port->streamConfig.stride));
+ break;
+ }
+ case SPA_PARAM_Meta:
+ switch (result.index) {
+ case 0:
+ param = (struct spa_pod*)spa_pod_builder_add_object(&b,
+ SPA_TYPE_OBJECT_ParamMeta, id,
+ SPA_PARAM_META_type, SPA_POD_Id(SPA_META_Header),
+ SPA_PARAM_META_size, SPA_POD_Int(sizeof(struct spa_meta_header)));
+ break;
+ case 1:
+ param = (struct spa_pod*)spa_pod_builder_add_object(&b,
+ SPA_TYPE_OBJECT_ParamMeta, id,
+ SPA_PARAM_META_type, SPA_POD_Id(SPA_META_VideoTransform),
+ SPA_PARAM_META_size, SPA_POD_Int(sizeof(struct spa_meta_videotransform)));
+ break;
+ default:
+ return 0;
+ }
+ break;
+ case SPA_PARAM_IO:
+ switch (result.index) {
+ case 0:
+ param = (struct spa_pod*)spa_pod_builder_add_object(&b,
+ SPA_TYPE_OBJECT_ParamIO, id,
+ SPA_PARAM_IO_id, SPA_POD_Id(SPA_IO_Buffers),
+ SPA_PARAM_IO_size, SPA_POD_Int(sizeof(struct spa_io_buffers)));
+ break;
+ case 1:
+ param = (struct spa_pod*)spa_pod_builder_add_object(&b,
+ SPA_TYPE_OBJECT_ParamIO, id,
+ SPA_PARAM_IO_id, SPA_POD_Id(SPA_IO_Clock),
+ SPA_PARAM_IO_size, SPA_POD_Int(sizeof(struct spa_io_clock)));
+ break;
+ case 2:
+ param = (struct spa_pod*)spa_pod_builder_add_object(&b,
+ SPA_TYPE_OBJECT_ParamIO, id,
+ SPA_PARAM_IO_id, SPA_POD_Id(SPA_IO_Control),
+ SPA_PARAM_IO_size, SPA_POD_Int(sizeof(struct spa_io_sequence)));
+ break;
+ default:
+ return 0;
+ }
+ break;
+ default:
+ return -ENOENT;
+ }
+
+ if (spa_pod_filter(&b, &result.param, param, filter) < 0)
+ goto next;
+
+ spa_node_emit_result(&impl->hooks, seq, 0, SPA_RESULT_TYPE_NODE_PARAMS, &result);
+
+ if (++count != num)
+ goto next;
+
+ return 0;
+}
+
+static int port_set_format(struct impl *impl, struct port *port,
+ uint32_t flags, const struct spa_pod *format)
+{
+ struct spa_video_info info;
+ int res;
+
+ if (format == NULL) {
+ if (!port->current_format)
+ return 0;
+
+ spa_libcamera_stream_off(impl);
+ spa_libcamera_clear_buffers(impl, port);
+ port->current_format.reset();
+
+ spa_libcamera_close(impl);
+ goto done;
+ } else {
+ spa_zero(info);
+ if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
+ return res;
+
+ if (info.media_type != SPA_MEDIA_TYPE_video) {
+ spa_log_error(impl->log, "media type must be video");
+ return -EINVAL;
+ }
+
+ switch (info.media_subtype) {
+ case SPA_MEDIA_SUBTYPE_raw:
+ if (spa_format_video_raw_parse(format, &info.info.raw) < 0) {
+ spa_log_error(impl->log, "can't parse video raw");
+ return -EINVAL;
+ }
+
+ if (port->current_format && info.media_type == port->current_format->media_type &&
+ info.media_subtype == port->current_format->media_subtype &&
+ info.info.raw.format == port->current_format->info.raw.format &&
+ info.info.raw.size.width == port->current_format->info.raw.size.width &&
+ info.info.raw.size.height == port->current_format->info.raw.size.height &&
+ info.info.raw.flags == port->current_format->info.raw.flags &&
+ (!(info.info.raw.flags & SPA_VIDEO_FLAG_MODIFIER) ||
+ (info.info.raw.modifier == port->current_format->info.raw.modifier)))
+ return 0;
+ break;
+ case SPA_MEDIA_SUBTYPE_mjpg:
+ if (spa_format_video_mjpg_parse(format, &info.info.mjpg) < 0)
+ return -EINVAL;
+
+ if (port->current_format && info.media_type == port->current_format->media_type &&
+ info.media_subtype == port->current_format->media_subtype &&
+ info.info.mjpg.size.width == port->current_format->info.mjpg.size.width &&
+ info.info.mjpg.size.height == port->current_format->info.mjpg.size.height)
+ return 0;
+ break;
+ case SPA_MEDIA_SUBTYPE_h264:
+ if (spa_format_video_h264_parse(format, &info.info.h264) < 0)
+ return -EINVAL;
+
+ if (port->current_format && info.media_type == port->current_format->media_type &&
+ info.media_subtype == port->current_format->media_subtype &&
+ info.info.h264.size.width == port->current_format->info.h264.size.width &&
+ info.info.h264.size.height == port->current_format->info.h264.size.height)
+ return 0;
+ break;
+ default:
+ return -EINVAL;
+ }
+ }
+
+ if (port->current_format && !(flags & SPA_NODE_PARAM_FLAG_TEST_ONLY)) {
+ spa_libcamera_use_buffers(impl, port, NULL, 0);
+ port->current_format.reset();
+ }
+
+ if (spa_libcamera_set_format(impl, port, &info, flags & SPA_NODE_PARAM_FLAG_TEST_ONLY) < 0)
+ return -EINVAL;
+
+ if (!(flags & SPA_NODE_PARAM_FLAG_TEST_ONLY)) {
+ port->current_format = info;
+ }
+
+ done:
+ impl->info.change_mask |= SPA_NODE_CHANGE_MASK_PARAMS;
+ port->info.change_mask |= SPA_PORT_CHANGE_MASK_PARAMS;
+ if (port->current_format) {
+ impl->params[NODE_Format] = SPA_PARAM_INFO(SPA_PARAM_Format, SPA_PARAM_INFO_READWRITE);
+ port->params[PORT_Format] = SPA_PARAM_INFO(SPA_PARAM_Format, SPA_PARAM_INFO_READWRITE);
+ port->params[PORT_Buffers] = SPA_PARAM_INFO(SPA_PARAM_Buffers, SPA_PARAM_INFO_READ);
+ } else {
+ impl->params[NODE_Format] = SPA_PARAM_INFO(SPA_PARAM_Format, SPA_PARAM_INFO_WRITE);
+ port->params[PORT_Format] = SPA_PARAM_INFO(SPA_PARAM_Format, SPA_PARAM_INFO_WRITE);
+ port->params[PORT_Buffers] = SPA_PARAM_INFO(SPA_PARAM_Buffers, 0);
+ }
+ emit_port_info(impl, port, false);
+ emit_node_info(impl, false);
+
+ return 0;
+}
+
+static int impl_node_port_set_param(void *object,
+ enum spa_direction direction, uint32_t port_id,
+ uint32_t id, uint32_t flags,
+ const struct spa_pod *param)
+{
+ struct impl *impl = (struct impl*)object;
+ struct port *port;
+ int res;
+
+ spa_return_val_if_fail(object != NULL, -EINVAL);
+ spa_return_val_if_fail(CHECK_PORT(impl, direction, port_id), -EINVAL);
+
+ port = GET_PORT(impl, direction, port_id);
+
+ switch (id) {
+ case SPA_PARAM_Format:
+ res = port_set_format(impl, port, flags, param);
+ break;
+ default:
+ res = -ENOENT;
+ }
+ return res;
+}
+
+static int impl_node_port_use_buffers(void *object,
+ enum spa_direction direction,
+ uint32_t port_id,
+ uint32_t flags,
+ struct spa_buffer **buffers,
+ uint32_t n_buffers)
+{
+ struct impl *impl = (struct impl*)object;
+ struct port *port;
+ int res;
+
+ spa_return_val_if_fail(impl != NULL, -EINVAL);
+ spa_return_val_if_fail(CHECK_PORT(impl, direction, port_id), -EINVAL);
+
+ port = GET_PORT(impl, direction, port_id);
+
+ if (port->n_buffers) {
+ spa_libcamera_stream_off(impl);
+ if ((res = spa_libcamera_clear_buffers(impl, port)) < 0)
+ return res;
+ }
+ if (n_buffers > 0 && !port->current_format)
+ return -EIO;
+ if (n_buffers > MAX_BUFFERS)
+ return -ENOSPC;
+ if (buffers == NULL)
+ return 0;
+
+ if (flags & SPA_NODE_BUFFERS_FLAG_ALLOC) {
+ res = spa_libcamera_alloc_buffers(impl, port, buffers, n_buffers);
+ } else {
+ res = spa_libcamera_use_buffers(impl, port, buffers, n_buffers);
+ }
+ return res;
+}
+
+static int impl_node_port_set_io(void *object,
+ enum spa_direction direction,
+ uint32_t port_id,
+ uint32_t id,
+ void *data, size_t size)
+{
+ struct impl *impl = (struct impl*)object;
+ struct port *port;
+
+ spa_return_val_if_fail(impl != NULL, -EINVAL);
+ spa_return_val_if_fail(CHECK_PORT(impl, direction, port_id), -EINVAL);
+
+ port = GET_PORT(impl, direction, port_id);
+
+ switch (id) {
+ case SPA_IO_Buffers:
+ port->io = (struct spa_io_buffers*)data;
+ break;
+ case SPA_IO_Control:
+ port->control = (struct spa_io_sequence*)data;
+ break;
+ default:
+ return -ENOENT;
+ }
+ return 0;
+}
+
+static int impl_node_port_reuse_buffer(void *object,
+ uint32_t port_id,
+ uint32_t buffer_id)
+{
+ struct impl *impl = (struct impl*)object;
+ struct port *port;
+ int res;
+
+ spa_return_val_if_fail(impl != NULL, -EINVAL);
+ spa_return_val_if_fail(port_id == 0, -EINVAL);
+
+ port = GET_OUT_PORT(impl, port_id);
+
+ spa_return_val_if_fail(buffer_id < port->n_buffers, -EINVAL);
+
+ res = spa_libcamera_buffer_recycle(impl, port, buffer_id);
+
+ return res;
+}
+
+static int process_control(struct impl *impl, struct spa_pod_sequence *control)
+{
+ struct spa_pod_control *c;
+
+ SPA_POD_SEQUENCE_FOREACH(control, c) {
+ switch (c->type) {
+ case SPA_CONTROL_Properties:
+ {
+ struct spa_pod_prop *prop;
+ struct spa_pod_object *obj = (struct spa_pod_object *) &c->value;
+
+ SPA_POD_OBJECT_FOREACH(obj, prop) {
+ spa_libcamera_set_control(impl, prop);
+ }
+ break;
+ }
+ default:
+ break;
+ }
+ }
+ return 0;
+}
+
+static int impl_node_process(void *object)
+{
+ struct impl *impl = (struct impl*)object;
+ int res;
+ struct spa_io_buffers *io;
+ struct port *port;
+ struct buffer *b;
+
+ spa_return_val_if_fail(impl != NULL, -EINVAL);
+
+ port = GET_OUT_PORT(impl, 0);
+ if ((io = port->io) == NULL)
+ return -EIO;
+
+ if (port->control)
+ process_control(impl, &port->control->sequence);
+
+ spa_log_trace(impl->log, "%p; status %d", impl, io->status);
+
+ if (io->status == SPA_STATUS_HAVE_DATA) {
+ return SPA_STATUS_HAVE_DATA;
+ }
+
+ if (io->buffer_id < port->n_buffers) {
+ if ((res = spa_libcamera_buffer_recycle(impl, port, io->buffer_id)) < 0)
+ return res;
+
+ io->buffer_id = SPA_ID_INVALID;
+ }
+
+ if (spa_list_is_empty(&port->queue)) {
+ return SPA_STATUS_OK;
+ }
+
+ b = spa_list_first(&port->queue, struct buffer, link);
+ spa_list_remove(&b->link);
+ SPA_FLAG_SET(b->flags, BUFFER_FLAG_OUTSTANDING);
+
+ spa_log_trace(impl->log, "%p: dequeue buffer %d", impl, b->id);
+
+ io->buffer_id = b->id;
+ io->status = SPA_STATUS_HAVE_DATA;
+
+ return SPA_STATUS_HAVE_DATA;
+}
+
+static const struct spa_node_methods impl_node = {
+ SPA_VERSION_NODE_METHODS,
+ .add_listener = impl_node_add_listener,
+ .set_callbacks = impl_node_set_callbacks,
+ .sync = impl_node_sync,
+ .enum_params = impl_node_enum_params,
+ .set_param = impl_node_set_param,
+ .set_io = impl_node_set_io,
+ .send_command = impl_node_send_command,
+ .add_port = impl_node_add_port,
+ .remove_port = impl_node_remove_port,
+ .port_enum_params = impl_node_port_enum_params,
+ .port_set_param = impl_node_port_set_param,
+ .port_use_buffers = impl_node_port_use_buffers,
+ .port_set_io = impl_node_port_set_io,
+ .port_reuse_buffer = impl_node_port_reuse_buffer,
+ .process = impl_node_process,
+};
+
+static int impl_get_interface(struct spa_handle *handle, const char *type, void **interface)
+{
+ struct impl *impl;
+
+ spa_return_val_if_fail(handle != NULL, -EINVAL);
+ spa_return_val_if_fail(interface != NULL, -EINVAL);
+
+ impl = (struct impl *) handle;
+
+ if (spa_streq(type, SPA_TYPE_INTERFACE_Node))
+ *interface = &impl->node;
+ else
+ return -ENOENT;
+
+ return 0;
+}
+
+static int impl_clear(struct spa_handle *handle)
+{
+ std::destroy_at(reinterpret_cast<impl *>(handle));
+ return 0;
+}
+
+impl::impl(spa_log *log, spa_loop *data_loop, spa_system *system,
+ std::shared_ptr<CameraManager> manager, std::shared_ptr<Camera> camera, std::string device_id)
+ : handle({ SPA_VERSION_HANDLE, impl_get_interface, impl_clear }),
+ log(log),
+ data_loop(data_loop),
+ system(system),
+ device_id(std::move(device_id)),
+ out_ports{{this}},
+ manager(std::move(manager)),
+ camera(std::move(camera))
+{
+ libcamera_log_topic_init(log);
+
+ spa_hook_list_init(&hooks);
+
+ node.iface = SPA_INTERFACE_INIT(
+ SPA_TYPE_INTERFACE_Node,
+ SPA_VERSION_NODE,
+ &impl_node, this);
+
+ params[NODE_PropInfo] = SPA_PARAM_INFO(SPA_PARAM_PropInfo, SPA_PARAM_INFO_READ);
+ params[NODE_Props] = SPA_PARAM_INFO(SPA_PARAM_Props, SPA_PARAM_INFO_READWRITE);
+ params[NODE_EnumFormat] = SPA_PARAM_INFO(SPA_PARAM_EnumFormat, SPA_PARAM_INFO_READ);
+ params[NODE_Format] = SPA_PARAM_INFO(SPA_PARAM_Format, SPA_PARAM_INFO_WRITE);
+
+ info.max_output_ports = 1;
+ info.flags = SPA_NODE_FLAG_RT;
+ info.params = params;
+ info.n_params = N_NODE_PARAMS;
+}
+
+static size_t
+impl_get_size(const struct spa_handle_factory *factory,
+ const struct spa_dict *params)
+{
+ return sizeof(struct impl);
+}
+
+static int
+impl_init(const struct spa_handle_factory *factory,
+ struct spa_handle *handle,
+ const struct spa_dict *info,
+ const struct spa_support *support,
+ uint32_t n_support)
+{
+ const char *str;
+ int res;
+
+ spa_return_val_if_fail(factory != NULL, -EINVAL);
+ spa_return_val_if_fail(handle != NULL, -EINVAL);
+
+ auto log = static_cast<spa_log *>(spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log));
+ auto data_loop = static_cast<spa_loop *>(spa_support_find(support, n_support, SPA_TYPE_INTERFACE_DataLoop));
+ auto system = static_cast<spa_system *>(spa_support_find(support, n_support, SPA_TYPE_INTERFACE_System));
+
+ if (!data_loop) {
+ spa_log_error(log, "a data_loop is needed");
+ return -EINVAL;
+ }
+
+ if (!system) {
+ spa_log_error(log, "a system is needed");
+ return -EINVAL;
+ }
+
+ auto manager = libcamera_manager_acquire(res);
+ if (!manager) {
+ spa_log_error(log, "can't start camera manager: %s", spa_strerror(res));
+ return res;
+ }
+
+ std::string device_id;
+ if (info && (str = spa_dict_lookup(info, SPA_KEY_API_LIBCAMERA_PATH)))
+ device_id = str;
+
+ auto camera = manager->get(device_id);
+ if (!camera) {
+ spa_log_error(log, "unknown camera id %s", device_id.c_str());
+ return -ENOENT;
+ }
+
+ new (handle) impl(log, data_loop, system,
+ std::move(manager), std::move(camera), std::move(device_id));
+
+ return 0;
+}
+
+static const struct spa_interface_info impl_interfaces[] = {
+ {SPA_TYPE_INTERFACE_Node,},
+};
+
+static int impl_enum_interface_info(const struct spa_handle_factory *factory,
+ const struct spa_interface_info **info,
+ uint32_t *index)
+{
+ spa_return_val_if_fail(factory != NULL, -EINVAL);
+ spa_return_val_if_fail(info != NULL, -EINVAL);
+ spa_return_val_if_fail(index != NULL, -EINVAL);
+
+ if (*index >= SPA_N_ELEMENTS(impl_interfaces))
+ return 0;
+
+ *info = &impl_interfaces[(*index)++];
+ return 1;
+}
+
+extern "C" {
+const struct spa_handle_factory spa_libcamera_source_factory = {
+ SPA_VERSION_HANDLE_FACTORY,
+ SPA_NAME_API_LIBCAMERA_SOURCE,
+ NULL,
+ impl_get_size,
+ impl_init,
+ impl_enum_interface_info,
+};
+}
diff --git a/spa/plugins/libcamera/libcamera-utils.cpp b/spa/plugins/libcamera/libcamera-utils.cpp
new file mode 100644
index 0000000..af07484
--- /dev/null
+++ b/spa/plugins/libcamera/libcamera-utils.cpp
@@ -0,0 +1,990 @@
+/* Spa
+ *
+ * Copyright (C) 2020, Collabora Ltd.
+ * Author: Raghavendra Rao Sidlagatta <raghavendra.rao@collabora.com>
+ * Copyright (C) 2021 Wim Taymans <wim.taymans@gmail.com>
+ *
+ * libcamera-utils.cpp
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sched.h>
+#include <errno.h>
+#include <sys/mman.h>
+#include <poll.h>
+#include <limits.h>
+
+#include <linux/media.h>
+
+int spa_libcamera_open(struct impl *impl)
+{
+ if (impl->acquired)
+ return 0;
+
+ spa_log_info(impl->log, "open camera %s", impl->device_id.c_str());
+ impl->camera->acquire();
+
+ impl->allocator = new FrameBufferAllocator(impl->camera);
+
+ impl->acquired = true;
+ return 0;
+}
+
+int spa_libcamera_close(struct impl *impl)
+{
+ struct port *port = &impl->out_ports[0];
+ if (!impl->acquired)
+ return 0;
+ if (impl->active || port->current_format)
+ return 0;
+
+ spa_log_info(impl->log, "close camera %s", impl->device_id.c_str());
+ delete impl->allocator;
+ impl->allocator = nullptr;
+
+ impl->camera->release();
+
+ impl->acquired = false;
+ return 0;
+}
+
+static void spa_libcamera_get_config(struct impl *impl)
+{
+ if (impl->config)
+ return;
+
+ StreamRoles roles;
+ roles.push_back(StreamRole::VideoRecording);
+ impl->config = impl->camera->generateConfiguration(roles);
+}
+
+static int spa_libcamera_buffer_recycle(struct impl *impl, struct port *port, uint32_t buffer_id)
+{
+ struct buffer *b = &port->buffers[buffer_id];
+ int res;
+
+ if (!SPA_FLAG_IS_SET(b->flags, BUFFER_FLAG_OUTSTANDING))
+ return 0;
+
+ SPA_FLAG_CLEAR(b->flags, BUFFER_FLAG_OUTSTANDING);
+
+ if (buffer_id >= impl->requestPool.size()) {
+ spa_log_warn(impl->log, "invalid buffer_id %u >= %zu",
+ buffer_id, impl->requestPool.size());
+ return -EINVAL;
+ }
+ Request *request = impl->requestPool[buffer_id].get();
+ Stream *stream = port->streamConfig.stream();
+ FrameBuffer *buffer = impl->allocator->buffers(stream)[buffer_id].get();
+ if ((res = request->addBuffer(stream, buffer)) < 0) {
+ spa_log_warn(impl->log, "can't add buffer %u for request: %s",
+ buffer_id, spa_strerror(res));
+ return -ENOMEM;
+ }
+ if (!impl->active) {
+ impl->pendingRequests.push_back(request);
+ return 0;
+ } else {
+ request->controls().merge(impl->ctrls);
+ impl->ctrls.clear();
+ if ((res = impl->camera->queueRequest(request)) < 0) {
+ spa_log_warn(impl->log, "can't queue buffer %u: %s",
+ buffer_id, spa_strerror(res));
+ return res == -EACCES ? -EBUSY : res;
+ }
+ }
+ return 0;
+}
+
+static int allocBuffers(struct impl *impl, struct port *port, unsigned int count)
+{
+ int res;
+
+ if ((res = impl->allocator->allocate(port->streamConfig.stream())) < 0)
+ return res;
+
+ for (unsigned int i = 0; i < count; i++) {
+ std::unique_ptr<Request> request = impl->camera->createRequest(i);
+ if (!request) {
+ impl->requestPool.clear();
+ return -ENOMEM;
+ }
+ impl->requestPool.push_back(std::move(request));
+ }
+ return res;
+}
+
+static void freeBuffers(struct impl *impl, struct port *port)
+{
+ impl->pendingRequests.clear();
+ impl->requestPool.clear();
+ impl->allocator->free(port->streamConfig.stream());
+}
+
+static int spa_libcamera_clear_buffers(struct impl *impl, struct port *port)
+{
+ uint32_t i;
+
+ if (port->n_buffers == 0)
+ return 0;
+
+ for (i = 0; i < port->n_buffers; i++) {
+ struct buffer *b;
+ struct spa_data *d;
+
+ b = &port->buffers[i];
+ d = b->outbuf->datas;
+
+ if (SPA_FLAG_IS_SET(b->flags, BUFFER_FLAG_OUTSTANDING)) {
+ spa_log_debug(impl->log, "queueing outstanding buffer %p", b);
+ spa_libcamera_buffer_recycle(impl, port, i);
+ }
+ if (SPA_FLAG_IS_SET(b->flags, BUFFER_FLAG_MAPPED)) {
+ munmap(SPA_PTROFF(b->ptr, -d[0].mapoffset, void),
+ d[0].maxsize - d[0].mapoffset);
+ }
+ if (SPA_FLAG_IS_SET(b->flags, BUFFER_FLAG_ALLOCATED)) {
+ close(d[0].fd);
+ }
+ d[0].type = SPA_ID_INVALID;
+ }
+
+ freeBuffers(impl, port);
+ port->n_buffers = 0;
+
+ return 0;
+}
+
+struct format_info {
+ PixelFormat pix;
+ uint32_t format;
+ uint32_t media_type;
+ uint32_t media_subtype;
+};
+
+#define MAKE_FMT(pix,fmt,mt,mst) { pix, SPA_VIDEO_FORMAT_ ##fmt, SPA_MEDIA_TYPE_ ##mt, SPA_MEDIA_SUBTYPE_ ##mst }
+static const struct format_info format_info[] = {
+ /* RGB formats */
+ MAKE_FMT(formats::RGB565, RGB16, video, raw),
+ MAKE_FMT(formats::RGB565_BE, RGB16, video, raw),
+ MAKE_FMT(formats::RGB888, RGB, video, raw),
+ MAKE_FMT(formats::BGR888, BGR, video, raw),
+ MAKE_FMT(formats::XRGB8888, xRGB, video, raw),
+ MAKE_FMT(formats::XBGR8888, xBGR, video, raw),
+ MAKE_FMT(formats::RGBX8888, RGBx, video, raw),
+ MAKE_FMT(formats::BGRX8888, BGRx, video, raw),
+ MAKE_FMT(formats::ARGB8888, ARGB, video, raw),
+ MAKE_FMT(formats::ABGR8888, ABGR, video, raw),
+ MAKE_FMT(formats::RGBA8888, RGBA, video, raw),
+ MAKE_FMT(formats::BGRA8888, BGRA, video, raw),
+
+ MAKE_FMT(formats::YUYV, YUY2, video, raw),
+ MAKE_FMT(formats::YVYU, YVYU, video, raw),
+ MAKE_FMT(formats::UYVY, UYVY, video, raw),
+ MAKE_FMT(formats::VYUY, VYUY, video, raw),
+
+ MAKE_FMT(formats::NV12, NV12, video, raw),
+ MAKE_FMT(formats::NV21, NV21, video, raw),
+ MAKE_FMT(formats::NV16, NV16, video, raw),
+ MAKE_FMT(formats::NV61, NV61, video, raw),
+ MAKE_FMT(formats::NV24, NV24, video, raw),
+
+ MAKE_FMT(formats::YUV420, I420, video, raw),
+ MAKE_FMT(formats::YVU420, YV12, video, raw),
+ MAKE_FMT(formats::YUV422, Y42B, video, raw),
+
+ MAKE_FMT(formats::MJPEG, ENCODED, video, mjpg),
+#undef MAKE_FMT
+};
+
+static const struct format_info *video_format_to_info(const PixelFormat &pix) {
+ size_t i;
+
+ for (i = 0; i < SPA_N_ELEMENTS(format_info); i++) {
+ if (format_info[i].pix == pix)
+ return &format_info[i];
+ }
+ return NULL;
+}
+
+static const struct format_info *find_format_info_by_media_type(uint32_t type,
+ uint32_t subtype, uint32_t format, int startidx)
+{
+ size_t i;
+
+ for (i = startidx; i < SPA_N_ELEMENTS(format_info); i++) {
+ if ((format_info[i].media_type == type) &&
+ (format_info[i].media_subtype == subtype) &&
+ (format == 0 || format_info[i].format == format))
+ return &format_info[i];
+ }
+ return NULL;
+}
+
+static int score_size(Size &a, Size &b)
+{
+ int x, y;
+ x = (int)a.width - (int)b.width;
+ y = (int)a.height - (int)b.height;
+ return x * x + y * y;
+}
+
+static int
+spa_libcamera_enum_format(struct impl *impl, struct port *port, int seq,
+ uint32_t start, uint32_t num, const struct spa_pod *filter)
+{
+ int res;
+ const struct format_info *info;
+ uint8_t buffer[1024];
+ struct spa_pod_builder b = { 0 };
+ struct spa_pod_frame f[2];
+ struct spa_result_node_params result;
+ struct spa_pod *fmt;
+ uint32_t i, count = 0, num_sizes;
+ PixelFormat format;
+ Size frameSize;
+ SizeRange sizeRange = SizeRange();
+
+ spa_libcamera_get_config(impl);
+
+ const StreamConfiguration& streamConfig = impl->config->at(0);
+ const StreamFormats &formats = streamConfig.formats();
+
+ result.id = SPA_PARAM_EnumFormat;
+ result.next = start;
+
+ if (result.next == 0) {
+ port->fmt_index = 0;
+ port->size_index = 0;
+ }
+next:
+ result.index = result.next++;
+
+next_fmt:
+ if (port->fmt_index >= formats.pixelformats().size())
+ goto enum_end;
+
+ format = formats.pixelformats()[port->fmt_index];
+
+ spa_log_debug(impl->log, "format: %s", format.toString().c_str());
+
+ info = video_format_to_info(format);
+ if (info == NULL) {
+ spa_log_debug(impl->log, "unknown format");
+ port->fmt_index++;
+ goto next_fmt;
+ }
+
+ num_sizes = formats.sizes(format).size();
+ if (num_sizes > 0 && port->size_index <= num_sizes) {
+ if (port->size_index == 0) {
+ Size wanted = Size(640, 480), test;
+ int score, best = INT_MAX;
+ for (i = 0; i < num_sizes; i++) {
+ test = formats.sizes(format)[i];
+ score = score_size(wanted, test);
+ if (score < best) {
+ best = score;
+ frameSize = test;
+ }
+ }
+ }
+ else {
+ frameSize = formats.sizes(format)[port->size_index - 1];
+ }
+ } else if (port->size_index < 1) {
+ sizeRange = formats.range(format);
+ if (sizeRange.hStep == 0 || sizeRange.vStep == 0) {
+ port->size_index = 0;
+ port->fmt_index++;
+ goto next_fmt;
+ }
+ } else {
+ port->size_index = 0;
+ port->fmt_index++;
+ goto next_fmt;
+ }
+ port->size_index++;
+
+ spa_pod_builder_init(&b, buffer, sizeof(buffer));
+ spa_pod_builder_push_object(&b, &f[0], SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat);
+ spa_pod_builder_add(&b,
+ SPA_FORMAT_mediaType, SPA_POD_Id(info->media_type),
+ SPA_FORMAT_mediaSubtype, SPA_POD_Id(info->media_subtype),
+ 0);
+
+ if (info->media_subtype == SPA_MEDIA_SUBTYPE_raw) {
+ spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_format, 0);
+ spa_pod_builder_id(&b, info->format);
+ }
+ if (info->pix.modifier()) {
+ spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_modifier, 0);
+ spa_pod_builder_long(&b, info->pix.modifier());
+ }
+ spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_size, 0);
+
+ if (sizeRange.hStep != 0 && sizeRange.vStep != 0) {
+ spa_pod_builder_push_choice(&b, &f[1], SPA_CHOICE_Step, 0);
+ spa_pod_builder_frame(&b, &f[1]);
+ spa_pod_builder_rectangle(&b,
+ sizeRange.min.width,
+ sizeRange.min.height);
+ spa_pod_builder_rectangle(&b,
+ sizeRange.min.width,
+ sizeRange.min.height);
+ spa_pod_builder_rectangle(&b,
+ sizeRange.max.width,
+ sizeRange.max.height);
+ spa_pod_builder_rectangle(&b,
+ sizeRange.hStep,
+ sizeRange.vStep);
+ spa_pod_builder_pop(&b, &f[1]);
+
+ } else {
+ spa_pod_builder_rectangle(&b, frameSize.width, frameSize.height);
+ }
+
+ fmt = (struct spa_pod*) spa_pod_builder_pop(&b, &f[0]);
+
+ if (spa_pod_filter(&b, &result.param, fmt, filter) < 0)
+ goto next;
+
+ spa_node_emit_result(&impl->hooks, seq, 0, SPA_RESULT_TYPE_NODE_PARAMS, &result);
+
+ if (++count != num)
+ goto next;
+
+ enum_end:
+ res = 0;
+ return res;
+}
+
+static int spa_libcamera_set_format(struct impl *impl, struct port *port,
+ struct spa_video_info *format, bool try_only)
+{
+ const struct format_info *info = NULL;
+ uint32_t video_format;
+ struct spa_rectangle *size = NULL;
+ struct spa_fraction *framerate = NULL;
+ CameraConfiguration::Status validation;
+ int res;
+
+ switch (format->media_subtype) {
+ case SPA_MEDIA_SUBTYPE_raw:
+ video_format = format->info.raw.format;
+ size = &format->info.raw.size;
+ framerate = &format->info.raw.framerate;
+ break;
+ case SPA_MEDIA_SUBTYPE_mjpg:
+ case SPA_MEDIA_SUBTYPE_jpeg:
+ video_format = SPA_VIDEO_FORMAT_ENCODED;
+ size = &format->info.mjpg.size;
+ framerate = &format->info.mjpg.framerate;
+ break;
+ case SPA_MEDIA_SUBTYPE_h264:
+ video_format = SPA_VIDEO_FORMAT_ENCODED;
+ size = &format->info.h264.size;
+ framerate = &format->info.h264.framerate;
+ break;
+ default:
+ video_format = SPA_VIDEO_FORMAT_ENCODED;
+ break;
+ }
+
+ info = find_format_info_by_media_type(format->media_type,
+ format->media_subtype, video_format, 0);
+ if (info == NULL || size == NULL || framerate == NULL) {
+ spa_log_error(impl->log, "unknown media type %d %d %d", format->media_type,
+ format->media_subtype, video_format);
+ return -EINVAL;
+ }
+ StreamConfiguration& streamConfig = impl->config->at(0);
+
+ streamConfig.pixelFormat = info->pix;
+ streamConfig.size.width = size->width;
+ streamConfig.size.height = size->height;
+ streamConfig.bufferCount = 8;
+
+ validation = impl->config->validate();
+ if (validation == CameraConfiguration::Invalid)
+ return -EINVAL;
+
+ if (try_only)
+ return 0;
+
+ if ((res = spa_libcamera_open(impl)) < 0)
+ return res;
+
+ res = impl->camera->configure(impl->config.get());
+ if (res != 0)
+ goto error;
+
+ port->streamConfig = impl->config->at(0);
+
+ if ((res = allocBuffers(impl, port, port->streamConfig.bufferCount)) < 0)
+ goto error;
+
+ port->info.change_mask |= SPA_PORT_CHANGE_MASK_FLAGS | SPA_PORT_CHANGE_MASK_RATE;
+ port->info.flags = SPA_PORT_FLAG_CAN_ALLOC_BUFFERS |
+ SPA_PORT_FLAG_LIVE |
+ SPA_PORT_FLAG_PHYSICAL |
+ SPA_PORT_FLAG_TERMINAL;
+ port->info.rate = SPA_FRACTION(port->rate.num, port->rate.denom);
+
+ return 0;
+error:
+ spa_libcamera_close(impl);
+ return res;
+
+}
+
+static int
+spa_libcamera_enum_controls(struct impl *impl, struct port *port, int seq,
+ uint32_t start, uint32_t num,
+ const struct spa_pod *filter)
+{
+ const ControlInfoMap &info = impl->camera->controls();
+ uint8_t buffer[1024];
+ struct spa_pod_builder b = { 0 };
+ struct spa_pod_frame f[2];
+ struct spa_result_node_params result;
+ struct spa_pod *ctrl;
+ uint32_t count = 0, skip;
+ int res;
+ const ControlId *ctrl_id;
+ ControlInfo ctrl_info;
+
+ result.id = SPA_PARAM_PropInfo;
+ result.next = start;
+
+ auto it = info.begin();
+ for (skip = result.next; skip; skip--)
+ it++;
+
+ if (false) {
+next:
+ it++;
+ }
+ result.index = result.next++;
+ if (it == info.end())
+ goto enum_end;
+
+ ctrl_id = it->first;
+ ctrl_info = it->second;
+
+ spa_pod_builder_init(&b, buffer, sizeof(buffer));
+ spa_pod_builder_push_object(&b, &f[0], SPA_TYPE_OBJECT_PropInfo, SPA_PARAM_PropInfo);
+ spa_pod_builder_add(&b,
+ SPA_PROP_INFO_id, SPA_POD_Id(ctrl_id->id()),
+ SPA_PROP_INFO_description, SPA_POD_String(ctrl_id->name().c_str()),
+ 0);
+
+ switch (ctrl_id->type()) {
+ case ControlTypeBool: {
+ bool def;
+ if (ctrl_info.def().isNone())
+ def = ctrl_info.min().get<bool>();
+ else
+ def = ctrl_info.def().get<bool>();
+
+ spa_pod_builder_add(&b,
+ SPA_PROP_INFO_type, SPA_POD_CHOICE_Bool(
+ def),
+ 0);
+ } break;
+ case ControlTypeFloat: {
+ float min = ctrl_info.min().get<float>();
+ float max = ctrl_info.max().get<float>();
+ float def;
+
+ if (ctrl_info.def().isNone())
+ def = (min + max) / 2;
+ else
+ def = ctrl_info.def().get<float>();
+
+ spa_pod_builder_add(&b,
+ SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Float(
+ def, min, max),
+ 0);
+ } break;
+ case ControlTypeInteger32: {
+ int32_t min = ctrl_info.min().get<int32_t>();
+ int32_t max = ctrl_info.max().get<int32_t>();
+ int32_t def;
+
+ if (ctrl_info.def().isNone())
+ def = (min + max) / 2;
+ else
+ def = ctrl_info.def().get<int32_t>();
+
+ spa_pod_builder_add(&b,
+ SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Int(
+ def, min, max),
+ 0);
+ } break;
+ default:
+ goto next;
+ }
+
+ ctrl = (struct spa_pod*) spa_pod_builder_pop(&b, &f[0]);
+
+ if (spa_pod_filter(&b, &result.param, ctrl, filter) < 0)
+ goto next;
+
+ spa_node_emit_result(&impl->hooks, seq, 0, SPA_RESULT_TYPE_NODE_PARAMS, &result);
+
+ if (++count != num)
+ goto next;
+
+enum_end:
+ res = 0;
+ return res;
+}
+
+struct val {
+ uint32_t type;
+ float f_val;
+ int32_t i_val;
+ bool b_val;
+ uint32_t id;
+};
+
+static int do_update_ctrls(struct spa_loop *loop,
+ bool async,
+ uint32_t seq,
+ const void *data,
+ size_t size,
+ void *user_data)
+{
+ struct impl *impl = (struct impl *)user_data;
+ const struct val *d = (const struct val *)data;
+ switch (d->type) {
+ case ControlTypeBool:
+ impl->ctrls.set(d->id, d->b_val);
+ break;
+ case ControlTypeFloat:
+ impl->ctrls.set(d->id, d->f_val);
+ break;
+ case ControlTypeInteger32:
+ //impl->ctrls.set(d->id, (int32_t)d->i_val);
+ break;
+ default:
+ break;
+ }
+ return 0;
+}
+
+static int
+spa_libcamera_set_control(struct impl *impl, const struct spa_pod_prop *prop)
+{
+ const ControlInfoMap &info = impl->camera->controls();
+ const ControlId *ctrl_id;
+ int res;
+ struct val d;
+
+ auto v = info.idmap().find(prop->key);
+ if (v == info.idmap().end())
+ return -ENOENT;
+
+ ctrl_id = v->second;
+
+ d.type = ctrl_id->type();
+ d.id = ctrl_id->id();
+
+ switch (d.type) {
+ case ControlTypeBool:
+ if ((res = spa_pod_get_bool(&prop->value, &d.b_val)) < 0)
+ goto done;
+ break;
+ case ControlTypeFloat:
+ if ((res = spa_pod_get_float(&prop->value, &d.f_val)) < 0)
+ goto done;
+ break;
+ case ControlTypeInteger32:
+ if ((res = spa_pod_get_int(&prop->value, &d.i_val)) < 0)
+ goto done;
+ break;
+ default:
+ res = -EINVAL;
+ goto done;
+ }
+ spa_loop_invoke(impl->data_loop, do_update_ctrls, 0, &d, sizeof(d), true, impl);
+ res = 0;
+done:
+ return res;
+}
+
+
+static void libcamera_on_fd_events(struct spa_source *source)
+{
+ struct impl *impl = (struct impl*) source->data;
+ struct spa_io_buffers *io;
+ struct port *port = &impl->out_ports[0];
+ uint32_t index, buffer_id;
+ struct buffer *b;
+ uint64_t cnt;
+
+ if (source->rmask & SPA_IO_ERR) {
+ spa_log_error(impl->log, "libcamera %p: error %08x", impl, source->rmask);
+ if (impl->source.loop)
+ spa_loop_remove_source(impl->data_loop, &impl->source);
+ return;
+ }
+
+ if (!(source->rmask & SPA_IO_IN)) {
+ spa_log_warn(impl->log, "libcamera %p: spurious wakeup %d", impl, source->rmask);
+ return;
+ }
+
+ if (spa_system_eventfd_read(impl->system, impl->source.fd, &cnt) < 0) {
+ spa_log_error(impl->log, "Failed to read on event fd");
+ return;
+ }
+
+ if (spa_ringbuffer_get_read_index(&port->ring, &index) < 1) {
+ spa_log_error(impl->log, "nothing is queued");
+ return;
+ }
+ buffer_id = port->ring_ids[index & MASK_BUFFERS];
+ spa_ringbuffer_read_update(&port->ring, index + 1);
+
+ b = &port->buffers[buffer_id];
+ spa_list_append(&port->queue, &b->link);
+
+ io = port->io;
+ if (io == NULL) {
+ b = spa_list_first(&port->queue, struct buffer, link);
+ spa_list_remove(&b->link);
+ SPA_FLAG_SET(b->flags, BUFFER_FLAG_OUTSTANDING);
+ spa_libcamera_buffer_recycle(impl, port, b->id);
+ } else if (io->status != SPA_STATUS_HAVE_DATA) {
+ if (io->buffer_id < port->n_buffers)
+ spa_libcamera_buffer_recycle(impl, port, io->buffer_id);
+
+ b = spa_list_first(&port->queue, struct buffer, link);
+ spa_list_remove(&b->link);
+ SPA_FLAG_SET(b->flags, BUFFER_FLAG_OUTSTANDING);
+
+ io->buffer_id = b->id;
+ io->status = SPA_STATUS_HAVE_DATA;
+ spa_log_trace(impl->log, "libcamera %p: now queued %d", impl, b->id);
+ }
+ spa_node_call_ready(&impl->callbacks, SPA_STATUS_HAVE_DATA);
+}
+
+static int spa_libcamera_use_buffers(struct impl *impl, struct port *port,
+ struct spa_buffer **buffers, uint32_t n_buffers)
+{
+ return -ENOTSUP;
+}
+
+static const struct {
+ Transform libcamera_transform;
+ uint32_t spa_transform_value;
+} transform_map[] = {
+ { Transform::Identity, SPA_META_TRANSFORMATION_None },
+ { Transform::Rot0, SPA_META_TRANSFORMATION_None },
+ { Transform::HFlip, SPA_META_TRANSFORMATION_Flipped },
+ { Transform::VFlip, SPA_META_TRANSFORMATION_Flipped180 },
+ { Transform::HVFlip, SPA_META_TRANSFORMATION_180 },
+ { Transform::Rot180, SPA_META_TRANSFORMATION_180 },
+ { Transform::Transpose, SPA_META_TRANSFORMATION_Flipped90 },
+ { Transform::Rot90, SPA_META_TRANSFORMATION_90 },
+ { Transform::Rot270, SPA_META_TRANSFORMATION_270 },
+ { Transform::Rot180Transpose, SPA_META_TRANSFORMATION_Flipped270 },
+};
+
+static uint32_t libcamera_transform_to_spa_transform_value(Transform transform)
+{
+ for (const auto& t : transform_map) {
+ if (t.libcamera_transform == transform)
+ return t.spa_transform_value;
+ }
+ return SPA_META_TRANSFORMATION_None;
+}
+
+static int
+mmap_init(struct impl *impl, struct port *port,
+ struct spa_buffer **buffers, uint32_t n_buffers)
+{
+ unsigned int i, j;
+ struct spa_data *d;
+ Stream *stream = impl->config->at(0).stream();
+ const std::vector<std::unique_ptr<FrameBuffer>> &bufs =
+ impl->allocator->buffers(stream);
+
+ if (n_buffers > 0) {
+ if (bufs.size() != n_buffers)
+ return -EINVAL;
+
+ d = buffers[0]->datas;
+
+ if (d[0].type != SPA_ID_INVALID &&
+ d[0].type & (1u << SPA_DATA_DmaBuf)) {
+ port->memtype = SPA_DATA_DmaBuf;
+ } else if (d[0].type != SPA_ID_INVALID &&
+ d[0].type & (1u << SPA_DATA_MemFd)) {
+ port->memtype = SPA_DATA_MemFd;
+ } else if (d[0].type & (1u << SPA_DATA_MemPtr)) {
+ port->memtype = SPA_DATA_MemPtr;
+ } else {
+ spa_log_error(impl->log, "v4l2: can't use buffers of type %d", d[0].type);
+ return -EINVAL;
+ }
+ }
+
+ for (i = 0; i < n_buffers; i++) {
+ struct buffer *b;
+
+ if (buffers[i]->n_datas < 1) {
+ spa_log_error(impl->log, "invalid buffer data");
+ return -EINVAL;
+ }
+
+ b = &port->buffers[i];
+ b->id = i;
+ b->outbuf = buffers[i];
+ b->flags = BUFFER_FLAG_OUTSTANDING;
+ b->h = (struct spa_meta_header*)spa_buffer_find_meta_data(buffers[i], SPA_META_Header, sizeof(*b->h));
+
+ b->videotransform = (struct spa_meta_videotransform*)spa_buffer_find_meta_data(
+ buffers[i], SPA_META_VideoTransform, sizeof(*b->videotransform));
+ if (b->videotransform) {
+ b->videotransform->transform =
+ libcamera_transform_to_spa_transform_value(impl->config->transform);
+ spa_log_debug(impl->log, "Setting videotransform for buffer %d to %u (from %s)",
+ i, b->videotransform->transform, transformToString(impl->config->transform));
+
+ }
+
+ d = buffers[i]->datas;
+ for(j = 0; j < buffers[i]->n_datas; ++j) {
+ d[j].type = port->memtype;
+ d[j].flags = SPA_DATA_FLAG_READABLE;
+ d[j].mapoffset = 0;
+ d[j].maxsize = port->streamConfig.frameSize;
+ d[j].chunk->offset = 0;
+ d[j].chunk->size = port->streamConfig.frameSize;
+ d[j].chunk->stride = port->streamConfig.stride;
+ d[j].chunk->flags = 0;
+
+ if (port->memtype == SPA_DATA_DmaBuf ||
+ port->memtype == SPA_DATA_MemFd) {
+ d[j].fd = bufs[i]->planes()[j].fd.get();
+ spa_log_debug(impl->log, "Got fd = %ld for buffer: #%d", d[j].fd, i);
+ d[j].data = NULL;
+ SPA_FLAG_SET(b->flags, BUFFER_FLAG_ALLOCATED);
+ }
+ else if(port->memtype == SPA_DATA_MemPtr) {
+ d[j].fd = -1;
+ d[j].data = mmap(NULL,
+ d[j].maxsize + d[j].mapoffset,
+ PROT_READ, MAP_SHARED,
+ bufs[i]->planes()[j].fd.get(),
+ 0);
+ if (d[j].data == MAP_FAILED) {
+ spa_log_error(impl->log, "mmap: %m");
+ continue;
+ }
+ b->ptr = d[j].data;
+ SPA_FLAG_SET(b->flags, BUFFER_FLAG_MAPPED);
+ spa_log_debug(impl->log, "mmap ptr:%p", d[j].data);
+ } else {
+ spa_log_error(impl->log, "invalid buffer type");
+ return -EIO;
+ }
+ }
+ spa_libcamera_buffer_recycle(impl, port, i);
+ }
+ port->n_buffers = n_buffers;
+ spa_log_debug(impl->log, "we have %d buffers", n_buffers);
+
+ return 0;
+}
+
+static int
+spa_libcamera_alloc_buffers(struct impl *impl, struct port *port,
+ struct spa_buffer **buffers,
+ uint32_t n_buffers)
+{
+ int res;
+
+ if (port->n_buffers > 0)
+ return -EIO;
+
+ if ((res = mmap_init(impl, port, buffers, n_buffers)) < 0)
+ return res;
+
+ return 0;
+}
+
+
+void impl::requestComplete(libcamera::Request *request)
+{
+ struct impl *impl = this;
+ struct port *port = &impl->out_ports[0];
+ Stream *stream = port->streamConfig.stream();
+ uint32_t index, buffer_id;
+ struct buffer *b;
+
+ spa_log_debug(impl->log, "request complete");
+
+ buffer_id = request->cookie();
+ b = &port->buffers[buffer_id];
+
+ if ((request->status() == Request::RequestCancelled)) {
+ spa_log_debug(impl->log, "Request was cancelled");
+ request->reuse();
+ SPA_FLAG_SET(b->flags, BUFFER_FLAG_OUTSTANDING);
+ spa_libcamera_buffer_recycle(impl, port, b->id);
+ return;
+ }
+ FrameBuffer *buffer = request->findBuffer(stream);
+ if (buffer == nullptr) {
+ spa_log_warn(impl->log, "unknown buffer");
+ return;
+ }
+ const FrameMetadata &fmd = buffer->metadata();
+
+
+
+ if (impl->clock) {
+ impl->clock->nsec = fmd.timestamp;
+ impl->clock->rate = port->rate;
+ impl->clock->position = fmd.sequence;
+ impl->clock->duration = 1;
+ impl->clock->delay = 0;
+ impl->clock->rate_diff = 1.0;
+ impl->clock->next_nsec = fmd.timestamp;
+ }
+ if (b->h) {
+ b->h->flags = 0;
+ b->h->offset = 0;
+ b->h->seq = fmd.sequence;
+ b->h->pts = fmd.timestamp;
+ b->h->dts_offset = 0;
+ }
+ request->reuse();
+
+ spa_ringbuffer_get_write_index(&port->ring, &index);
+ port->ring_ids[index & MASK_BUFFERS] = buffer_id;
+ spa_ringbuffer_write_update(&port->ring, index + 1);
+
+ if (spa_system_eventfd_write(impl->system, impl->source.fd, 1) < 0)
+ spa_log_error(impl->log, "Failed to write on event fd");
+
+}
+
+static int spa_libcamera_stream_on(struct impl *impl)
+{
+ struct port *port = &impl->out_ports[0];
+ int res;
+
+ if (!port->current_format) {
+ spa_log_error(impl->log, "Exting %s with -EIO", __FUNCTION__);
+ return -EIO;
+ }
+
+ if (impl->active)
+ return 0;
+
+ impl->camera->requestCompleted.connect(impl, &impl::requestComplete);
+
+ spa_log_info(impl->log, "starting camera %s", impl->device_id.c_str());
+ if ((res = impl->camera->start()) < 0)
+ goto error;
+
+ for (Request *req : impl->pendingRequests) {
+ if ((res = impl->camera->queueRequest(req)) < 0)
+ goto error_stop;
+ }
+ impl->pendingRequests.clear();
+
+ impl->source.func = libcamera_on_fd_events;
+ impl->source.data = impl;
+ impl->source.fd = spa_system_eventfd_create(impl->system, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
+ impl->source.mask = SPA_IO_IN | SPA_IO_ERR;
+ impl->source.rmask = 0;
+ if (impl->source.fd < 0) {
+ spa_log_error(impl->log, "Failed to create eventfd: %s", spa_strerror(impl->source.fd));
+ res = impl->source.fd;
+ goto error_stop;
+ }
+ spa_loop_add_source(impl->data_loop, &impl->source);
+
+ impl->active = true;
+
+ return 0;
+
+error_stop:
+ impl->camera->stop();
+error:
+ impl->camera->requestCompleted.disconnect(impl, &impl::requestComplete);
+ return res == -EACCES ? -EBUSY : res;
+}
+
+static int do_remove_source(struct spa_loop *loop,
+ bool async,
+ uint32_t seq,
+ const void *data,
+ size_t size,
+ void *user_data)
+{
+ struct impl *impl = (struct impl *)user_data;
+ if (impl->source.loop)
+ spa_loop_remove_source(loop, &impl->source);
+ return 0;
+}
+
+static int spa_libcamera_stream_off(struct impl *impl)
+{
+ struct port *port = &impl->out_ports[0];
+ int res;
+
+ if (!impl->active) {
+ for (std::unique_ptr<Request> &req : impl->requestPool)
+ req->reuse();
+ return 0;
+ }
+
+ impl->active = false;
+ spa_log_info(impl->log, "stopping camera %s", impl->device_id.c_str());
+ impl->pendingRequests.clear();
+
+ if ((res = impl->camera->stop()) < 0) {
+ spa_log_warn(impl->log, "error stopping camera %s: %s",
+ impl->device_id.c_str(), spa_strerror(res));
+ }
+
+ impl->camera->requestCompleted.disconnect(impl, &impl::requestComplete);
+
+ spa_loop_invoke(impl->data_loop, do_remove_source, 0, NULL, 0, true, impl);
+ if (impl->source.fd >= 0) {
+ spa_system_close(impl->system, impl->source.fd);
+ impl->source.fd = -1;
+ }
+
+ spa_list_init(&port->queue);
+
+ return 0;
+}
diff --git a/spa/plugins/libcamera/libcamera.c b/spa/plugins/libcamera/libcamera.c
new file mode 100644
index 0000000..bcf24d2
--- /dev/null
+++ b/spa/plugins/libcamera/libcamera.c
@@ -0,0 +1,57 @@
+/* Spa libcamera support
+ *
+ * Copyright © 2020 collabora
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <errno.h>
+
+#include <spa/support/plugin.h>
+#include <spa/support/log.h>
+
+#include "libcamera.h"
+
+struct spa_log_topic log_topic = SPA_LOG_TOPIC(0, "spa.libcamera");
+struct spa_log_topic *libcamera_log_topic = &log_topic;
+
+SPA_EXPORT
+int spa_handle_factory_enum(const struct spa_handle_factory **factory,
+ uint32_t *index)
+{
+ spa_return_val_if_fail(factory != NULL, -EINVAL);
+ spa_return_val_if_fail(index != NULL, -EINVAL);
+
+ switch (*index) {
+ case 0:
+ *factory = &spa_libcamera_manager_factory;
+ break;
+ case 1:
+ *factory = &spa_libcamera_device_factory;
+ break;
+ case 2:
+ *factory = &spa_libcamera_source_factory;
+ break;
+ default:
+ return 0;
+ }
+ (*index)++;
+ return 1;
+}
diff --git a/spa/plugins/libcamera/libcamera.h b/spa/plugins/libcamera/libcamera.h
new file mode 100644
index 0000000..1a4d618
--- /dev/null
+++ b/spa/plugins/libcamera/libcamera.h
@@ -0,0 +1,51 @@
+/* Spa libcamera support
+ *
+ * Copyright © 2020 collabora
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <errno.h>
+
+#include <linux/media.h>
+
+#include <spa/support/log.h>
+#include <spa/support/system.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+extern const struct spa_handle_factory spa_libcamera_source_factory;
+extern const struct spa_handle_factory spa_libcamera_manager_factory;
+extern const struct spa_handle_factory spa_libcamera_device_factory;
+
+#undef SPA_LOG_TOPIC_DEFAULT
+#define SPA_LOG_TOPIC_DEFAULT libcamera_log_topic
+extern struct spa_log_topic *libcamera_log_topic;
+
+static inline void libcamera_log_topic_init(struct spa_log *log)
+{
+ spa_log_topic_init(log, libcamera_log_topic);
+}
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
diff --git a/spa/plugins/libcamera/meson.build b/spa/plugins/libcamera/meson.build
new file mode 100644
index 0000000..abb1a42
--- /dev/null
+++ b/spa/plugins/libcamera/meson.build
@@ -0,0 +1,17 @@
+libcamera_sources = [
+ 'libcamera.c',
+ 'libcamera-manager.cpp',
+ 'libcamera-device.cpp',
+ 'libcamera-source.cpp'
+]
+
+libdrm_dep = dependency('libdrm', version : '>= 2.4.98',
+ required : get_option('libcamera'))
+summary({'libdrm': libdrm_dep.found()}, bool_yn: true, section: 'Backend')
+if libdrm_dep.found()
+ libcameralib = shared_library('spa-libcamera',
+ libcamera_sources,
+ dependencies : [ spa_dep, libudev_dep, libcamera_dep, pthread_lib, libdrm_dep ],
+ install : true,
+ install_dir : spa_plugindir / 'libcamera')
+endif