diff options
Diffstat (limited to 'src/gstreamer/gstlibcameraprovider.cpp')
-rw-r--r-- | src/gstreamer/gstlibcameraprovider.cpp | 57 |
1 files changed, 26 insertions, 31 deletions
diff --git a/src/gstreamer/gstlibcameraprovider.cpp b/src/gstreamer/gstlibcameraprovider.cpp index 914ed4fb..5da96ea3 100644 --- a/src/gstreamer/gstlibcameraprovider.cpp +++ b/src/gstreamer/gstlibcameraprovider.cpp @@ -3,9 +3,11 @@ * Copyright (C) 2020, Collabora Ltd. * Author: Nicolas Dufresne <nicolas.dufresne@collabora.com> * - * gstlibcameraprovider.c - GStreamer Device Provider + * GStreamer Device Provider */ +#include <array> + #include "gstlibcameraprovider.h" #include <libcamera/camera.h> @@ -35,14 +37,14 @@ enum { #define GST_TYPE_LIBCAMERA_DEVICE gst_libcamera_device_get_type() G_DECLARE_FINAL_TYPE(GstLibcameraDevice, gst_libcamera_device, - GST_LIBCAMERA, DEVICE, GstDevice); + GST_LIBCAMERA, DEVICE, GstDevice) struct _GstLibcameraDevice { GstDevice parent; gchar *name; }; -G_DEFINE_TYPE(GstLibcameraDevice, gst_libcamera_device, GST_TYPE_DEVICE); +G_DEFINE_TYPE(GstLibcameraDevice, gst_libcamera_device, GST_TYPE_DEVICE) static GstElement * gst_libcamera_device_create_element(GstDevice *device, const gchar *name) @@ -89,7 +91,7 @@ gst_libcamera_device_set_property(GObject *object, guint prop_id, } static void -gst_libcamera_device_init(GstLibcameraDevice *self) +gst_libcamera_device_init([[maybe_unused]] GstLibcameraDevice *self) { } @@ -101,7 +103,7 @@ gst_libcamera_device_finalize(GObject *object) g_free(self->name); - G_OBJECT_GET_CLASS(klass)->finalize(object); + G_OBJECT_CLASS(klass)->finalize(object); } static void @@ -126,12 +128,15 @@ gst_libcamera_device_class_init(GstLibcameraDeviceClass *klass) static GstDevice * gst_libcamera_device_new(const std::shared_ptr<Camera> &camera) { + static const std::array roles{ StreamRole::VideoRecording }; g_autoptr(GstCaps) caps = gst_caps_new_empty(); - const gchar *name = camera->name().c_str(); - StreamRoles roles; + const gchar *name = camera->id().c_str(); - roles.push_back(StreamRole::VideoRecording); std::unique_ptr<CameraConfiguration> config = camera->generateConfiguration(roles); + if (!config || config->size() != roles.size()) { + GST_ERROR("Failed to generate a default configuration for %s", name); + return nullptr; + } for (const StreamConfiguration &stream_cfg : *config) { GstCaps *sub_caps = gst_libcamera_stream_formats_to_caps(stream_cfg.formats()); @@ -158,19 +163,18 @@ gst_libcamera_device_new(const std::shared_ptr<Camera> &camera) struct _GstLibcameraProvider { GstDeviceProvider parent; - CameraManager *cm; }; G_DEFINE_TYPE_WITH_CODE(GstLibcameraProvider, gst_libcamera_provider, GST_TYPE_DEVICE_PROVIDER, GST_DEBUG_CATEGORY_INIT(provider_debug, "libcamera-provider", 0, - "libcamera Device Provider")); + "libcamera Device Provider")) static GList * gst_libcamera_provider_probe(GstDeviceProvider *provider) { GstLibcameraProvider *self = GST_LIBCAMERA_PROVIDER(provider); - CameraManager *cm = self->cm; + std::shared_ptr<CameraManager> cm; GList *devices = nullptr; gint ret; @@ -181,7 +185,7 @@ gst_libcamera_provider_probe(GstDeviceProvider *provider) * gains monitoring support. Meanwhile we need to cycle start()/stop() * to ensure every probe() calls return the latest list. */ - ret = cm->start(); + cm = gst_libcamera_get_camera_manager(ret); if (ret) { GST_ERROR_OBJECT(self, "Failed to retrieve device list: %s", g_strerror(-ret)); @@ -189,13 +193,19 @@ gst_libcamera_provider_probe(GstDeviceProvider *provider) } for (const std::shared_ptr<Camera> &camera : cm->cameras()) { - GST_INFO_OBJECT(self, "Found camera '%s'", camera->name().c_str()); + GST_INFO_OBJECT(self, "Found camera '%s'", camera->id().c_str()); + + GstDevice *dev = gst_libcamera_device_new(camera); + if (!dev) { + GST_ERROR_OBJECT(self, "Failed to add camera '%s'", + camera->id().c_str()); + return nullptr; + } + devices = g_list_append(devices, - g_object_ref_sink(gst_libcamera_device_new(camera))); + g_object_ref_sink(dev)); } - cm->stop(); - return devices; } @@ -204,31 +214,16 @@ gst_libcamera_provider_init(GstLibcameraProvider *self) { GstDeviceProvider *provider = GST_DEVICE_PROVIDER(self); - self->cm = new CameraManager(); - /* Avoid devices being duplicated. */ gst_device_provider_hide_provider(provider, "v4l2deviceprovider"); } static void -gst_libcamera_provider_finalize(GObject *object) -{ - GstLibcameraProvider *self = GST_LIBCAMERA_PROVIDER(object); - gpointer klass = gst_libcamera_provider_parent_class; - - delete self->cm; - - return G_OBJECT_GET_CLASS(klass)->finalize(object); -} - -static void gst_libcamera_provider_class_init(GstLibcameraProviderClass *klass) { GstDeviceProviderClass *provider_class = GST_DEVICE_PROVIDER_CLASS(klass); - GObjectClass *object_class = G_OBJECT_CLASS(klass); provider_class->probe = gst_libcamera_provider_probe; - object_class->finalize = gst_libcamera_provider_finalize; gst_device_provider_class_set_metadata(provider_class, "libcamera Device Provider", |