[RFC,v1] libcamera: pipeline: simple: Handle failed capture better
diff mbox series

Message ID 20261126132649.1928406-1-barnabas.pocze@ideasonboard.com
State New
Headers show
Series
  • [RFC,v1] libcamera: pipeline: simple: Handle failed capture better
Related show

Commit Message

Barnabás Pőcze Nov. 26, 2026, 1:26 p.m. UTC
The handling of failed frame captures from the video device was not entirely
correct.

For example, assume (raw, processed) streams, and consider that all captures
fail with an error. In that case, the "raw" buffer will be completed in the
request, but since `tryCompleteRequest()` returns early due to the pending
"processed" buffer, the request will never be completed and processing stops.

Furthermore, there is an issue of uninitialized metadata. When the "raw"
buffer fails, then the metadata of the "processed" buffer will not be filled,
not even the `status` field. Since it starts out as uninitialized, it will
stay as such.

Fix both issues by always unconditionally completing the associated request
(if any) when the "raw" capture fails for some reason, and ensure that the
other buffers of the request have valid metadata.

Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
---
 src/libcamera/pipeline/simple/simple.cpp | 71 ++++++++++++++++--------
 1 file changed, 49 insertions(+), 22 deletions(-)

Patch
diff mbox series

diff --git a/src/libcamera/pipeline/simple/simple.cpp b/src/libcamera/pipeline/simple/simple.cpp
index 2f7f4c893f..9e9874f56f 100644
--- a/src/libcamera/pipeline/simple/simple.cpp
+++ b/src/libcamera/pipeline/simple/simple.cpp
@@ -41,6 +41,7 @@ 
 #include "libcamera/internal/delayed_controls.h"
 #include "libcamera/internal/device_enumerator.h"
 #include "libcamera/internal/formats.h"
+#include "libcamera/internal/framebuffer.h"
 #include "libcamera/internal/global_configuration.h"
 #include "libcamera/internal/media_device.h"
 #include "libcamera/internal/pipeline_handler.h"
@@ -889,36 +890,62 @@  void SimpleCameraData::imageBufferReady(FrameBuffer *buffer)
 	 * point converting an erroneous buffer.
 	 */
 	if (buffer->metadata().status != FrameMetadata::FrameSuccess) {
+		Request *request = buffer->request();
+		const auto &metadata = buffer->metadata();
+		const bool cancelled = metadata.status == FrameMetadata::FrameCancelled;
+
 		if (!useConversion_ || rawStream_) {
-			/* No conversion, just complete the request. */
-			Request *request = buffer->request();
+			/* The buffer is part of the request, complete it. */
 			pipe->completeBuffer(request, buffer);
-			SimpleFrameInfo *info = frameInfo_.find(request->sequence());
-			if (info)
-				info->metadataRequired = false;
-			tryCompleteRequest(request);
-			return;
+		} else if (!cancelled) {
+			/*
+			 * The converter or Software ISP is in use. Requeue the internal
+			 * buffer for capture (unless the stream is being stopped), and
+			 * complete the request with all the user-facing buffers.
+			 */
+			 video_->queueBuffer(buffer);
 		}
 
 		/*
-		 * The converter or Software ISP is in use. Requeue the internal
-		 * buffer for capture (unless the stream is being stopped), and
-		 * complete the request with all the user-facing buffers.
+		 * \todo Due to the various queues here, it is assumed that buffers
+		 * are returned in-order. But `V4L2VideoDevice::streamOff()` does not
+		 * guarantee that. So it is assumed that cancellation is only possible
+		 * due to that, and processing the `conversionQueue_` is skipped as
+		 * it should be handled in `clearIncompleteRequests()` during the stop
+		 * procedure.
 		 */
-		if (buffer->metadata().status != FrameMetadata::FrameCancelled)
-			video_->queueBuffer(buffer);
+		if (!cancelled && !conversionQueue_.empty()) {
+			const RequestOutputs &outputs = conversionQueue_.front();
+			ASSERT(!request || request == outputs.request);
+			request = outputs.request;
 
-		if (conversionQueue_.empty())
-			return;
+			/*
+			 * \todo There should probably be some common code to propagate
+			 * metadata, as appropriate for the given status.
+			 */
+			for (auto &[stream, buf] : outputs.outputs) {
+				auto &m = buf->_d()->metadata();
 
-		const RequestOutputs &outputs = conversionQueue_.front();
-		for (auto &[stream, buf] : outputs.outputs)
-			pipe->completeBuffer(outputs.request, buf);
-		SimpleFrameInfo *info = frameInfo_.find(outputs.request->sequence());
-		if (info)
-			info->metadataRequired = false;
-		tryCompleteRequest(outputs.request);
-		conversionQueue_.pop();
+				m.status = metadata.status;
+				m.sequence = metadata.sequence;
+				m.timestamp = metadata.timestamp;
+
+				pipe->completeBuffer(request, buf);
+			}
+
+			conversionQueue_.pop();
+		}
+
+		/*
+		 * \todo The `hasPendingBuffers()` check is only needed
+		 * due to the out-of-order cancellation of buffers by
+		 * `V4L2VideoDevice::streamOff()`, as described above.
+		 */
+		ASSERT(cancelled || !request->hasPendingBuffers());
+		if (request && !request->hasPendingBuffers()) {
+			frameInfo_.destroy(request->sequence());
+			pipe->completeRequest(request);
+		}
 
 		return;
 	}