umlaeute / v4l2loopback

v4l2-loopback device
GNU General Public License v2.0
3.73k stars 531 forks source link

Allow repeat REQBUF when buffers not mapped #599

Open stephematician opened 1 month ago

stephematician commented 1 month ago

Allow repeated VIDIOC_REQBUF when no buffers are mapped (fix #598) by:

  1. Setting ready_for_output back to 1 in vidioc_streamoff.
  2. Checking that buffers are not mapped in vidioc_reqbufs when ready-for-output is true.

I hope I haven't messed up the logic for dev->ready_for_output. The comments suggest:

set to true when no writer is currently attached

More precisely, it looked to me like it was initialised as true and set to false (only) once vidioc_streamon is called - i.e. when the output process is started in streaming (memory mapping) I/O. Thus it should be safe to return it to true once vidioc_streamoff is called.

I contemplated whether I also needed to add a check that the opener is assigned WRITER type in stream-off, but have left it out for now, e.g.:

--- a/v4l2loopback.c
+++ b/v4l2loopback.c
@@ -1964,7 +1964,8 @@ static int vidioc_streamoff(struct file *file, void *fh,
        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
                if (dev->ready_for_capture > 0)
                        dev->ready_for_capture--;
-                dev->ready_for_output = 1;
+                if (opener->type == WRITER)
+                        dev->ready_for_output = 1;
                return 0;
stephematician commented 2 weeks ago

I added the test of opener->type when setting ready-for-output (and decrementing ready-for-capture) in STREAMOFF.

I wouldn't mind some feedback about what the expected behaviour of ready-for-output and ready-for-capture are in order to tidy up the logic. Otherwise, happy to leave things as they are.