Drop bad frames on nvv4l2camerasrc

Hi,
This is a follow up on

As summary, Xavier NX JP5.1.3 with an unstable hw setup that is prone to cause issues during capture, and want to try and have software ways to handle the capture issues gracefully.

We found a possible way to limit bad frames using v4l2 to capture with another cam that uses v4l2 for capture using the buffer flags. Like the attached patch. It works on v4l2src but not on nvv4l2camerasrc. Attached is the patch.
The error we get on nvv4l2camerasrc after triggering capture issues is:

0:00:06.307238068 122602 0xaaaaf009aaa0 ERROR        nvv4l2camerasrc gstnvv4l2camerasrc.cpp:383:gst_nvv4l2camera_buffer_pool_acquire_buffer:<nvv4l2camerabufferpool0> Corrupted frame detected (index=4) - requeueing and dequeueing next

0:00:06.346651980 122602 0xaaaaf009aaa0 WARN         nvv4l2camerasrc gstnvv4l2camerasrc.cpp:406:gst_nvv4l2camera_buffer_pool_acquire_buffer:<nvv4l2camerabufferpool0> Search camera output buffer. Driver out fd: -1610579168 camera buffer fd: 24

0:00:06.346827948 122602 0xaaaaf009aaa0 WARN         nvv4l2camerasrc gstnvv4l2camerasrc.cpp:406:gst_nvv4l2camera_buffer_pool_acquire_buffer:<nvv4l2camerabufferpool0> Search camera output buffer. Driver out fd: -1610579168 camera buffer fd: 24

[…]

0:00:06.347559920 122602 0xaaaaf009aaa0 WARN         nvv4l2camerasrc gstnvv4l2camerasrc.cpp:406:gst_nvv4l2camera_buffer_pool_acquire_buffer:<nvv4l2camerabufferpool0> Search camera output buffer. Driver out fd: -1610579168 camera buffer fd: 24

0:00:06.348687765 122602 0xaaaaf009aaa0 ERROR        nvv4l2camerasrc gstnvv4l2camerasrc.cpp:455:gst_nvv4l2camera_buffer_pool_acquire_buffer:<nvv4l2camerabufferpool0> Not found buffer which driver returned

Is there something we might be missing before the VIDIOC_QBUF or VIDIOC_DQBUF call?

0002-Add-v4l2-error-flag-check-to-discard-frame-if-marked.txt (1.8 KB)

Regards,
Andres
Embedded SW Engineer at RidgeRun
Contact us: support@ridgerun.com
Developers wiki: https://developer.ridgerun.com/
Website: www.ridgerun.com

hello andres.artavia,

we should to emphasize that..
it’s unstable hardware setup, and you’re trying to resolve it through software approach.

let me review that.. will update the thread.
I’ve slightly revise your while loop, please try below..

+  while (nv_mem->nvcam_buf->buffer->flags & V4L2_BUF_FLAG_ERROR) {
+    GST_ERROR_OBJECT (pool, "Corrupted frame detected (index=%d) - requeueing and dequeueing next", nv_mem->nvcam_buf->buffer->index);
+    if (nv_mem && (nv_mem->nvcam_buf->buffer != NULL)) {
+      if (-1 == ioctl(pool->video_fd, VIDIOC_QBUF, nv_mem->nvcam_buf->buffer)) {
+        GST_ERROR_OBJECT (pool, "Failed to re-queue corrupted buffer %d", nv_mem->nvcam_buf->buffer->index);
+      }
+    }
+    nv_mem->nvcam_buf->buffer->type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+    nv_mem->nvcam_buf->buffer->memory      = V4L2_MEMORY_DMABUF;
+    if (-1 == ioctl(pool->video_fd, VIDIOC_DQBUF, nv_mem->nvcam_buf->buffer)) {
+      GST_ERROR_OBJECT (pool, "Failed to dequeue after corrupted buffer");
+      goto dqbuf_error;
+    }
+  }