@@ -8,10 +8,13 @@
#include "gstlibcamera-utils.h"
+#include <libcamera/control_ids.h>
#include <libcamera/formats.h>
using namespace libcamera;
+static std::pair<int, int> framerate_container = { -1, -1 };
+
static struct {
GstVideoFormat gst_format;
PixelFormat format;
@@ -405,6 +408,66 @@ gst_libcamera_configure_stream_from_caps(StreamConfiguration &stream_cfg,
}
}
+void
+gst_libcamera_get_init_controls_from_caps(ControlList &controls, [[maybe_unused]] GstCaps *caps)
+{
+ /* read framerate from caps - convert to integer and set to frame_time. */
+ GstStructure *s = gst_caps_get_structure(caps, 0);
+ gint fps_n = -1, fps_d = -1;
+
+ if (gst_structure_has_field_typed(s, "framerate", GST_TYPE_FRACTION)) {
+ if (!gst_structure_get_fraction(s, "framerate", &fps_n, &fps_d)) {
+ GST_WARNING("invalid framerate in the caps.");
+ return;
+ }
+ }
+
+ if (fps_n < 0 || fps_d < 0)
+ return;
+
+ framerate_container = { fps_n, fps_d };
+ int64_t frame_duration = (fps_d * 1000000.0) / fps_n;
+ controls.set(controls::FrameDurationLimits,
+ Span<const int64_t, 2>({ frame_duration, frame_duration }));
+}
+
+void
+gst_libcamera_set_frame_duration_bounds(const ControlInfoMap &defaults, ControlList ¤t)
+{
+ int64_t min_frame_duration, max_frame_duration;
+
+ auto iterFrameDuration = defaults.find(controls::FrameDurationLimits.id());
+ if (!(iterFrameDuration != defaults.end() &&
+ current.contains(controls::FRAME_DURATION_LIMITS)))
+ return;
+
+ min_frame_duration = std::max(iterFrameDuration->second.min().get<int64_t>(),
+ current.get(controls::FrameDurationLimits).value()[0]);
+ max_frame_duration = std::min(iterFrameDuration->second.max().get<int64_t>(),
+ current.get(controls::FrameDurationLimits).value()[1]);
+
+ current.set(controls::FrameDurationLimits,
+ Span<const int64_t, 2>({ min_frame_duration, max_frame_duration }));
+}
+
+void
+gst_libcamera_controllist_to_caps(const ControlList &controls, GstCaps *caps)
+{
+ gint numerator, denominator;
+ if (!controls.contains(controls::FRAME_DURATION_LIMITS))
+ return;
+
+ GstStructure *s = gst_caps_get_structure(caps, 0);
+ double framerate = 1000000 / static_cast<double>(controls.get(controls::FrameDurationLimits).value()[0]);
+ gst_util_double_to_fraction(framerate, &numerator, &denominator);
+
+ if (framerate_container.first / framerate_container.second == numerator / denominator)
+ gst_structure_set(s, "framerate", GST_TYPE_FRACTION,
+ framerate_container.first, framerate_container.second, nullptr);
+ else
+ gst_structure_set(s, "framerate", GST_TYPE_FRACTION, 0, 1, nullptr);
+}
+
#if !GST_CHECK_VERSION(1, 17, 1)
gboolean
gst_task_resume(GstTask *task)
@@ -9,6 +9,7 @@
#pragma once
#include <libcamera/camera_manager.h>
+#include <libcamera/controls.h>
#include <libcamera/stream.h>
#include <gst/gst.h>
@@ -18,6 +19,10 @@ GstCaps *gst_libcamera_stream_formats_to_caps(const libcamera::StreamFormats &fo
GstCaps *gst_libcamera_stream_configuration_to_caps(const libcamera::StreamConfiguration &stream_cfg);
void gst_libcamera_configure_stream_from_caps(libcamera::StreamConfiguration &stream_cfg,
GstCaps *caps);
+void gst_libcamera_get_init_controls_from_caps(libcamera::ControlList &controls, GstCaps *caps);
+void gst_libcamera_set_frame_duration_bounds(const libcamera::ControlInfoMap &defaults, libcamera::ControlList ¤t);
+void gst_libcamera_controllist_to_caps(const libcamera::ControlList &controls, GstCaps *caps);
+
#if !GST_CHECK_VERSION(1, 17, 1)
gboolean gst_task_resume(GstTask *task);
#endif
@@ -131,6 +131,7 @@ struct GstLibcameraSrcState {
std::queue<std::unique_ptr<RequestWrap>> completedRequests_
LIBCAMERA_TSA_GUARDED_BY(lock_);
+ ControlList initControls_;
guint group_id_;
int queueRequest();
@@ -496,6 +497,7 @@ gst_libcamera_src_task_enter(GstTask *task, [[maybe_unused]] GThread *thread,
/* Retrieve the supported caps. */
g_autoptr(GstCaps) filter = gst_libcamera_stream_formats_to_caps(stream_cfg.formats());
g_autoptr(GstCaps) caps = gst_pad_peer_query_caps(srcpad, filter);
+
if (gst_caps_is_empty(caps)) {
flow_ret = GST_FLOW_NOT_NEGOTIATED;
break;
@@ -504,6 +506,7 @@ gst_libcamera_src_task_enter(GstTask *task, [[maybe_unused]] GThread *thread,
/* Fixate caps and configure the stream. */
caps = gst_caps_make_writable(caps);
gst_libcamera_configure_stream_from_caps(stream_cfg, caps);
+ gst_libcamera_get_init_controls_from_caps(state->initControls_, caps);
}
if (flow_ret != GST_FLOW_OK)
@@ -515,6 +518,16 @@ gst_libcamera_src_task_enter(GstTask *task, [[maybe_unused]] GThread *thread,
goto done;
}
+ ret = state->cam_->configure(state->config_.get());
+ if (ret) {
+ GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
+ ("Failed to configure camera: %s", g_strerror(-ret)),
+ ("Camera::configure() failed with error code %i", ret));
+ gst_task_stop(task);
+ return;
+ }
+ gst_libcamera_set_frame_duration_bounds(state->cam_->controls(), state->initControls_);
+
/*
* Regardless if it has been modified, create clean caps and push the
* caps event. Downstream will decide if the caps are acceptable.
@@ -524,6 +537,8 @@ gst_libcamera_src_task_enter(GstTask *task, [[maybe_unused]] GThread *thread,
const StreamConfiguration &stream_cfg = state->config_->at(i);
g_autoptr(GstCaps) caps = gst_libcamera_stream_configuration_to_caps(stream_cfg);
+ gst_libcamera_controllist_to_caps(state->initControls_, caps);
+
if (!gst_pad_push_event(srcpad, gst_event_new_caps(caps))) {
flow_ret = GST_FLOW_NOT_NEGOTIATED;
break;
@@ -535,15 +550,6 @@ gst_libcamera_src_task_enter(GstTask *task, [[maybe_unused]] GThread *thread,
gst_pad_push_event(srcpad, gst_event_new_segment(&segment));
}
- ret = state->cam_->configure(state->config_.get());
- if (ret) {
- GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
- ("Failed to configure camera: %s", g_strerror(-ret)),
- ("Camera::configure() failed with error code %i", ret));
- gst_task_stop(task);
- return;
- }
-
self->allocator = gst_libcamera_allocator_new(state->cam_, state->config_.get());
if (!self->allocator) {
GST_ELEMENT_ERROR(self, RESOURCE, NO_SPACE_LEFT,
@@ -566,7 +572,7 @@ gst_libcamera_src_task_enter(GstTask *task, [[maybe_unused]] GThread *thread,
gst_flow_combiner_add_pad(self->flow_combiner, srcpad);
}
- ret = state->cam_->start();
+ ret = state->cam_->start(&state->initControls_);
if (ret) {
GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
("Failed to start the camera: %s", g_strerror(-ret)),
@@ -576,6 +582,7 @@ gst_libcamera_src_task_enter(GstTask *task, [[maybe_unused]] GThread *thread,
}
done:
+ state->initControls_.clear();
switch (flow_ret) {
case GST_FLOW_NOT_NEGOTIATED:
GST_ELEMENT_FLOW_ERROR(self, flow_ret);