From 988ec3f417886cc8202ccc7932ac2f95e89db061 Mon Sep 17 00:00:00 2001
From: Naushir Patuck <naush@raspberrypi.com>
Date: Fri, 25 Mar 2022 09:09:00 +0000
Subject: libcamera: v4l2_videodevice: Better tracking of the device state

Replace the existing streaming_ state variable with an enum to track the
following three state: Streaming, Stopping, and Stopped. The alternate states
will be used in a subsequent commit.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 include/libcamera/internal/v4l2_videodevice.h |  8 +++++++-
 src/libcamera/v4l2_videodevice.cpp            | 10 ++++++----
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h
index 2d2ccc47..d7a81e08 100644
--- a/include/libcamera/internal/v4l2_videodevice.h
+++ b/include/libcamera/internal/v4l2_videodevice.h
@@ -225,6 +225,12 @@ protected:
 private:
 	LIBCAMERA_DISABLE_COPY(V4L2VideoDevice)
 
+	enum class State {
+		Streaming,
+		Stopping,
+		Stopped,
+	};
+
 	int getFormatMeta(V4L2DeviceFormat *format);
 	int trySetFormatMeta(V4L2DeviceFormat *format, bool set);
 
@@ -258,7 +264,7 @@ private:
 
 	EventNotifier *fdBufferNotifier_;
 
-	bool streaming_;
+	State state_;
 };
 
 class V4L2M2MDevice
diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
index 95b9c38b..c016e6ad 100644
--- a/src/libcamera/v4l2_videodevice.cpp
+++ b/src/libcamera/v4l2_videodevice.cpp
@@ -507,7 +507,7 @@ const std::string V4L2DeviceFormat::toString() const
  */
 V4L2VideoDevice::V4L2VideoDevice(const std::string &deviceNode)
 	: V4L2Device(deviceNode), formatInfo_(nullptr), cache_(nullptr),
-	  fdBufferNotifier_(nullptr), streaming_(false)
+	  fdBufferNotifier_(nullptr), state_(State::Stopped)
 {
 	/*
 	 * We default to an MMAP based CAPTURE video device, however this will
@@ -1803,7 +1803,7 @@ int V4L2VideoDevice::streamOn()
 		return ret;
 	}
 
-	streaming_ = true;
+	state_ = State::Streaming;
 
 	return 0;
 }
@@ -1825,7 +1825,7 @@ int V4L2VideoDevice::streamOff()
 {
 	int ret;
 
-	if (!streaming_ && queuedBuffers_.empty())
+	if (state_ != State::Streaming && queuedBuffers_.empty())
 		return 0;
 
 	ret = ioctl(VIDIOC_STREAMOFF, &bufferType_);
@@ -1835,6 +1835,8 @@ int V4L2VideoDevice::streamOff()
 		return ret;
 	}
 
+	state_ = State::Stopping;
+
 	/* Send back all queued buffers. */
 	for (auto it : queuedBuffers_) {
 		FrameBuffer *buffer = it.second;
@@ -1845,7 +1847,7 @@ int V4L2VideoDevice::streamOff()
 
 	queuedBuffers_.clear();
 	fdBufferNotifier_->setEnabled(false);
-	streaming_ = false;
+	state_ = State::Stopped;
 
 	return 0;
 }
-- 
cgit v1.2.1