From 49be2a1c5214c82b7c0a9ab7ab07593c5902bbd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Fri, 25 Jul 2025 15:52:59 +0200 Subject: [PATCH] spa: libcamera: source: generate camera config right away Currently the plugin uses a single configuration during its entire lifetime. So generate that configuration during initialization and hold on to it. This makes the code a bit simpler, and also fixes issues stemming from missed calls to `spa_libcamera_get_config()` as well as no error checking of `libcamera::Camera::generateConfiguration()`. --- spa/plugins/libcamera/libcamera-source.cpp | 31 +++++++++++----------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/spa/plugins/libcamera/libcamera-source.cpp b/spa/plugins/libcamera/libcamera-source.cpp index 40b6dfceb..2861661af 100644 --- a/spa/plugins/libcamera/libcamera-source.cpp +++ b/spa/plugins/libcamera/libcamera-source.cpp @@ -149,6 +149,7 @@ struct impl { std::shared_ptr manager; std::shared_ptr camera; + const std::unique_ptr config; FrameBufferAllocator *allocator = nullptr; std::vector> requestPool; @@ -156,8 +157,6 @@ struct impl { void requestComplete(libcamera::Request *request); - std::unique_ptr config; - struct spa_source source = {}; ControlList ctrls; @@ -166,7 +165,8 @@ struct impl { bool acquired = false; impl(spa_log *log, spa_loop *data_loop, spa_system *system, - std::shared_ptr manager, std::shared_ptr camera); + std::shared_ptr manager, std::shared_ptr camera, + std::unique_ptr config); struct spa_dll dll; }; @@ -237,14 +237,6 @@ int spa_libcamera_close(struct impl *impl) return 0; } -void spa_libcamera_get_config(struct impl *impl) -{ - if (impl->config) - return; - - impl->config = impl->camera->generateConfiguration({ StreamRole::VideoRecording }); -} - int spa_libcamera_buffer_recycle(struct impl *impl, struct port *port, uint32_t buffer_id) { struct buffer *b = &port->buffers[buffer_id]; @@ -507,8 +499,6 @@ spa_libcamera_enum_format(struct impl *impl, struct port *port, int seq, struct spa_result_node_params result; uint32_t count = 0; - spa_libcamera_get_config(impl); - const StreamConfiguration& streamConfig = impl->config->at(0); const StreamFormats &formats = streamConfig.formats(); const auto &pixel_formats = formats.pixelformats(); @@ -2066,14 +2056,16 @@ int impl_clear(struct spa_handle *handle) } impl::impl(spa_log *log, spa_loop *data_loop, spa_system *system, - std::shared_ptr manager, std::shared_ptr camera) + std::shared_ptr manager, std::shared_ptr camera, + std::unique_ptr config) : handle({ SPA_VERSION_HANDLE, impl_get_interface, impl_clear }), log(log), data_loop(data_loop), system(system), out_ports{{this}}, manager(std::move(manager)), - camera(std::move(camera)) + camera(std::move(camera)), + config(std::move(config)) { libcamera_log_topic_init(log); @@ -2147,8 +2139,15 @@ impl_init(const struct spa_handle_factory *factory, return -ENOENT; } + auto config = camera->generateConfiguration({ libcamera::StreamRole::VideoRecording }); + if (!config) { + spa_log_error(log, "cannot generate configuration for camera"); + return -EINVAL; + } + new (handle) impl(log, data_loop, system, - std::move(manager), std::move(camera)); + std::move(manager), std::move(camera), + std::move(config)); return 0; }