[libcamera-devel,10/11] test: v4l2_device: Provide asynchronous capture test

Message ID 20190203110102.5663-11-kieran.bingham@ideasonboard.com
State Accepted
Headers show
Series
  • libcamera: V4L2 Streams
Related show

Commit Message

Kieran Bingham Feb. 3, 2019, 11:01 a.m. UTC
Utilise the event_dispatcher to create a default event loop, and process
asynchronous buffer receive events.

If no frames are captured in 5 seconds, the test will fail.
It will also fail if less than 30 frames have been captured int the same
timeout period.

Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 test/v4l2_device/capture_async.cpp | 69 ++++++++++++++++++++++++++++++
 test/v4l2_device/meson.build       |  1 +
 2 files changed, 70 insertions(+)
 create mode 100644 test/v4l2_device/capture_async.cpp

Patch

diff --git a/test/v4l2_device/capture_async.cpp b/test/v4l2_device/capture_async.cpp
new file mode 100644
index 000000000000..70f340dba3f1
--- /dev/null
+++ b/test/v4l2_device/capture_async.cpp
@@ -0,0 +1,69 @@ 
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2019, Google Inc.
+ *
+ * libcamera V4L2 API tests
+ */
+
+#include <libcamera/buffer.h>
+#include <libcamera/camera_manager.h>
+#include <libcamera/event_dispatcher.h>
+#include <libcamera/timer.h>
+
+#include <iostream>
+
+#include "v4l2_device_test.h"
+
+class CaptureAsync : public V4L2DeviceTest
+{
+public:
+	CaptureAsync()
+		: frames(0){};
+
+	void ReceiveBuffer(Buffer *buffer)
+	{
+		std::cout << "Received a Buffer" << std::endl;
+		frames++;
+	}
+
+protected:
+	int run()
+	{
+		EventDispatcher *dispatcher = CameraManager::instance()->eventDispatcher();
+		Timer timeout;
+		int ret;
+
+		BufferPool *pool = dev_->requestBuffers(8);
+		if (!pool)
+			return TestFail;
+
+		dev_->bufferReady.connect(this, &CaptureAsync::ReceiveBuffer);
+
+		ret = dev_->streamOn();
+		if (ret)
+			return ret;
+
+		timeout.start(5000);
+		while (timeout.isRunning())
+			dispatcher->processEvents();
+
+		if (frames < 1) {
+			std::cout << "Failed to capture any frames within timeout." << std::endl;
+			return TestFail;
+		}
+
+		if (frames < 30) {
+			std::cout << "Failed to capture 30 frames within timeout." << std::endl;
+			return TestFail;
+		}
+
+		std::cout << "Processed " << frames << " frames" << std::endl;
+
+		return dev_->streamOff();
+	}
+
+private:
+	unsigned int frames;
+};
+
+TEST_REGISTER(CaptureAsync);
diff --git a/test/v4l2_device/meson.build b/test/v4l2_device/meson.build
index f04c7ded009c..2a37b7b06134 100644
--- a/test/v4l2_device/meson.build
+++ b/test/v4l2_device/meson.build
@@ -5,6 +5,7 @@  v4l2_device_tests = [
   [ 'request_buffers',    'request_buffers.cpp' ],
   [ 'stream_on_off',      'stream_on_off.cpp' ],
   [ 'capture_frames',     'capture_frames.cpp' ],
+  [ 'capture_async',      'capture_async.cpp' ],
 ]
 
 foreach t : v4l2_device_tests