From 3a6c4bd146cc6647daf458bbc048bd861e702f62 Mon Sep 17 00:00:00 2001 From: Jacopo Mondi Date: Wed, 12 Jun 2019 13:09:57 +0100 Subject: libcamera: Rename V4L2Device to V4L2VideoDevice MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In preparation of creating a new V4L2Device base class, rename V4L2Device to V4L2VideoDevice. This is a project wide rename without any intended functional change. Signed-off-by: Jacopo Mondi Signed-off-by: Kieran Bingham Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- test/v4l2_videodevice/buffer_sharing.cpp | 186 ++++++++++++++++++++++++ test/v4l2_videodevice/capture_async.cpp | 89 ++++++++++++ test/v4l2_videodevice/double_open.cpp | 41 ++++++ test/v4l2_videodevice/formats.cpp | 54 +++++++ test/v4l2_videodevice/meson.build | 18 +++ test/v4l2_videodevice/request_buffers.cpp | 36 +++++ test/v4l2_videodevice/stream_on_off.cpp | 38 +++++ test/v4l2_videodevice/v4l2_videodevice_test.cpp | 87 +++++++++++ test/v4l2_videodevice/v4l2_videodevice_test.h | 42 ++++++ 9 files changed, 591 insertions(+) create mode 100644 test/v4l2_videodevice/buffer_sharing.cpp create mode 100644 test/v4l2_videodevice/capture_async.cpp create mode 100644 test/v4l2_videodevice/double_open.cpp create mode 100644 test/v4l2_videodevice/formats.cpp create mode 100644 test/v4l2_videodevice/meson.build create mode 100644 test/v4l2_videodevice/request_buffers.cpp create mode 100644 test/v4l2_videodevice/stream_on_off.cpp create mode 100644 test/v4l2_videodevice/v4l2_videodevice_test.cpp create mode 100644 test/v4l2_videodevice/v4l2_videodevice_test.h (limited to 'test/v4l2_videodevice') diff --git a/test/v4l2_videodevice/buffer_sharing.cpp b/test/v4l2_videodevice/buffer_sharing.cpp new file mode 100644 index 00000000..1bc478fe --- /dev/null +++ b/test/v4l2_videodevice/buffer_sharing.cpp @@ -0,0 +1,186 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * libcamera V4L2 API tests + * + * Validate the function of exporting buffers from a V4L2VideoDevice and + * the ability to import them to another V4L2VideoDevice instance. + * Ensure that the Buffers can successfully be queued and dequeued + * between both devices. + */ + +#include + +#include +#include +#include +#include + +#include "v4l2_videodevice_test.h" + +class BufferSharingTest : public V4L2VideoDeviceTest +{ +public: + BufferSharingTest() + : V4L2VideoDeviceTest("vivid", "vivid-000-vid-cap"), + output_(nullptr), framesCaptured_(0), framesOutput_(0) {} + +protected: + int init() + { + int ret = V4L2VideoDeviceTest::init(); + if (ret) + return ret; + + /* media_ already represents VIVID */ + MediaEntity *entity = media_->getEntityByName("vivid-000-vid-out"); + if (!entity) + return TestSkip; + + output_ = new V4L2VideoDevice(entity); + if (!output_) { + std::cout << "Failed to create output device" << std::endl; + return TestFail; + } + + ret = output_->open(); + if (ret) { + std::cout << "Failed to open output device" << std::endl; + return TestFail; + } + + V4L2DeviceFormat format = {}; + + ret = capture_->getFormat(&format); + if (ret) { + std::cout << "Failed to get capture format" << std::endl; + return TestFail; + } + + ret = output_->setFormat(&format); + if (ret) { + std::cout << "Failed to set output format" << std::endl; + return TestFail; + } + + pool_.createBuffers(bufferCount); + + ret = capture_->exportBuffers(&pool_); + if (ret) { + std::cout << "Failed to export buffers" << std::endl; + return TestFail; + } + + ret = output_->importBuffers(&pool_); + if (ret) { + std::cout << "Failed to import buffers" << std::endl; + return TestFail; + } + + return 0; + } + + void captureBufferReady(Buffer *buffer) + { + std::cout << "Received capture buffer: " << buffer->index() + << " sequence " << buffer->sequence() << std::endl; + + output_->queueBuffer(buffer); + framesCaptured_++; + } + + void outputBufferReady(Buffer *buffer) + { + std::cout << "Received output buffer: " << buffer->index() + << " sequence " << buffer->sequence() << std::endl; + + capture_->queueBuffer(buffer); + framesOutput_++; + } + + int run() + { + EventDispatcher *dispatcher = CameraManager::instance()->eventDispatcher(); + Timer timeout; + int ret; + + capture_->bufferReady.connect(this, &BufferSharingTest::captureBufferReady); + output_->bufferReady.connect(this, &BufferSharingTest::outputBufferReady); + + /* Queue all the buffers to the capture device. */ + for (Buffer &buffer : pool_.buffers()) { + if (capture_->queueBuffer(&buffer)) + return TestFail; + } + + ret = capture_->streamOn(); + if (ret) { + std::cout << "Failed to start streaming on the capture device" << std::endl; + return TestFail; + } + + ret = output_->streamOn(); + if (ret) { + std::cout << "Failed to start streaming on the output device" << std::endl; + return TestFail; + } + + timeout.start(10000); + while (timeout.isRunning()) { + dispatcher->processEvents(); + if (framesCaptured_ > 30 && framesOutput_ > 30) + break; + } + + if ((framesCaptured_ < 1) || (framesOutput_ < 1)) { + std::cout << "Failed to process any frames within timeout." << std::endl; + return TestFail; + } + + if ((framesCaptured_ < 30) || (framesOutput_ < 30)) { + std::cout << "Failed to process 30 frames within timeout." << std::endl; + return TestFail; + } + + ret = capture_->streamOff(); + if (ret) { + std::cout << "Failed to stop streaming on the capture device" << std::endl; + return TestFail; + } + + ret = output_->streamOff(); + if (ret) { + std::cout << "Failed to stop streaming on the output device" << std::endl; + return TestFail; + } + + return TestPass; + } + + void cleanup() + { + std::cout + << "Captured " << framesCaptured_ << " frames and " + << "output " << framesOutput_ << " frames" + << std::endl; + + output_->streamOff(); + output_->releaseBuffers(); + output_->close(); + + delete output_; + + V4L2VideoDeviceTest::cleanup(); + } + +private: + const unsigned int bufferCount = 4; + + V4L2VideoDevice *output_; + + unsigned int framesCaptured_; + unsigned int framesOutput_; +}; + +TEST_REGISTER(BufferSharingTest); diff --git a/test/v4l2_videodevice/capture_async.cpp b/test/v4l2_videodevice/capture_async.cpp new file mode 100644 index 00000000..cea4fffb --- /dev/null +++ b/test/v4l2_videodevice/capture_async.cpp @@ -0,0 +1,89 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * libcamera V4L2 API tests + */ + +#include +#include +#include +#include + +#include + +#include "v4l2_videodevice_test.h" + +class CaptureAsyncTest : public V4L2VideoDeviceTest +{ +public: + CaptureAsyncTest() + : V4L2VideoDeviceTest("vimc", "Raw Capture 0"), frames(0) {} + + void receiveBuffer(Buffer *buffer) + { + std::cout << "Received buffer " << buffer->index() << std::endl; + frames++; + + /* Requeue the buffer for further use. */ + capture_->queueBuffer(buffer); + } + +protected: + int run() + { + const unsigned int bufferCount = 8; + + EventDispatcher *dispatcher = CameraManager::instance()->eventDispatcher(); + Timer timeout; + int ret; + + pool_.createBuffers(bufferCount); + + ret = capture_->exportBuffers(&pool_); + if (ret) + return TestFail; + + capture_->bufferReady.connect(this, &CaptureAsyncTest::receiveBuffer); + + /* Queue all the buffers to the device. */ + for (Buffer &b : pool_.buffers()) { + if (capture_->queueBuffer(&b)) + return TestFail; + } + + ret = capture_->streamOn(); + if (ret) + return TestFail; + + timeout.start(10000); + while (timeout.isRunning()) { + dispatcher->processEvents(); + if (frames > 30) + break; + } + + 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; + + ret = capture_->streamOff(); + if (ret) + return TestFail; + + return TestPass; + } + +private: + unsigned int frames; +}; + +TEST_REGISTER(CaptureAsyncTest); diff --git a/test/v4l2_videodevice/double_open.cpp b/test/v4l2_videodevice/double_open.cpp new file mode 100644 index 00000000..5768d404 --- /dev/null +++ b/test/v4l2_videodevice/double_open.cpp @@ -0,0 +1,41 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * libcamera V4L2 API tests + */ + +#include + +#include "v4l2_videodevice_test.h" + +namespace { + +class DoubleOpen : public V4L2VideoDeviceTest +{ +public: + DoubleOpen() + : V4L2VideoDeviceTest("vimc", "Raw Capture 0") {} +protected: + int run() + { + int ret; + + /* + * Expect failure: The device has already been opened by the + * V4L2VideoDeviceTest base class + */ + ret = capture_->open(); + if (!ret) { + std::cout << "Double open erroneously succeeded" << std::endl; + capture_->close(); + return TestFail; + } + + return TestPass; + } +}; + +} /* namespace */ + +TEST_REGISTER(DoubleOpen); diff --git a/test/v4l2_videodevice/formats.cpp b/test/v4l2_videodevice/formats.cpp new file mode 100644 index 00000000..ee7d357d --- /dev/null +++ b/test/v4l2_videodevice/formats.cpp @@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * libcamera V4L2 device format handling test + */ + +#include +#include + +#include "v4l2_videodevice.h" + +#include "v4l2_videodevice_test.h" + +using namespace std; +using namespace libcamera; + +class Format : public V4L2VideoDeviceTest +{ +public: + Format() + : V4L2VideoDeviceTest("vimc", "Raw Capture 0") {} +protected: + int run() + { + V4L2DeviceFormat format = {}; + + int ret = capture_->getFormat(&format); + if (ret) { + cerr << "Failed to get format" << endl; + return TestFail; + } + + format.size = { UINT_MAX, UINT_MAX }; + ret = capture_->setFormat(&format); + if (ret) { + cerr << "Failed to set format: image resolution is invalid: " + << "(UINT_MAX x UINT_MAX) but setFormat() should not fail." + << endl; + return TestFail; + } + + if (format.size.width == UINT_MAX || + format.size.height == UINT_MAX) { + cerr << "Failed to update image format = (UINT_MAX x UINT_MAX)" + << endl; + return TestFail; + } + + return TestPass; + } +}; + +TEST_REGISTER(Format); diff --git a/test/v4l2_videodevice/meson.build b/test/v4l2_videodevice/meson.build new file mode 100644 index 00000000..76be5e14 --- /dev/null +++ b/test/v4l2_videodevice/meson.build @@ -0,0 +1,18 @@ +# Tests are listed in order of complexity. +# They are not alphabetically sorted. +v4l2_videodevice_tests = [ + [ 'double_open', 'double_open.cpp' ], + [ 'formats', 'formats.cpp' ], + [ 'request_buffers', 'request_buffers.cpp' ], + [ 'stream_on_off', 'stream_on_off.cpp' ], + [ 'capture_async', 'capture_async.cpp' ], + [ 'buffer_sharing', 'buffer_sharing.cpp' ], +] + +foreach t : v4l2_videodevice_tests + exe = executable(t[0], [t[1], 'v4l2_videodevice_test.cpp'], + dependencies : libcamera_dep, + link_with : test_libraries, + include_directories : test_includes_internal) + test(t[0], exe, suite : 'v4l2_videodevice', is_parallel : false) +endforeach diff --git a/test/v4l2_videodevice/request_buffers.cpp b/test/v4l2_videodevice/request_buffers.cpp new file mode 100644 index 00000000..c4aedf7b --- /dev/null +++ b/test/v4l2_videodevice/request_buffers.cpp @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * libcamera V4L2 API tests + */ + +#include "v4l2_videodevice_test.h" + +class RequestBuffersTest : public V4L2VideoDeviceTest +{ +public: + RequestBuffersTest() + : V4L2VideoDeviceTest("vimc", "Raw Capture 0") {} + +protected: + int run() + { + /* + * TODO: + * Test invalid requests + * Test different buffer allocations + */ + const unsigned int bufferCount = 8; + + pool_.createBuffers(bufferCount); + + int ret = capture_->exportBuffers(&pool_); + if (ret) + return TestFail; + + return TestPass; + } +}; + +TEST_REGISTER(RequestBuffersTest); diff --git a/test/v4l2_videodevice/stream_on_off.cpp b/test/v4l2_videodevice/stream_on_off.cpp new file mode 100644 index 00000000..7664adc4 --- /dev/null +++ b/test/v4l2_videodevice/stream_on_off.cpp @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * libcamera V4L2 API tests + */ + +#include "v4l2_videodevice_test.h" + +class StreamOnStreamOffTest : public V4L2VideoDeviceTest +{ +public: + StreamOnStreamOffTest() + : V4L2VideoDeviceTest("vimc", "Raw Capture 0") {} +protected: + int run() + { + const unsigned int bufferCount = 8; + + pool_.createBuffers(bufferCount); + + int ret = capture_->exportBuffers(&pool_); + if (ret) + return TestFail; + + ret = capture_->streamOn(); + if (ret) + return TestFail; + + ret = capture_->streamOff(); + if (ret) + return TestFail; + + return TestPass; + } +}; + +TEST_REGISTER(StreamOnStreamOffTest); diff --git a/test/v4l2_videodevice/v4l2_videodevice_test.cpp b/test/v4l2_videodevice/v4l2_videodevice_test.cpp new file mode 100644 index 00000000..b26d06ad --- /dev/null +++ b/test/v4l2_videodevice/v4l2_videodevice_test.cpp @@ -0,0 +1,87 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * libcamera V4L2 API tests + */ + +#include +#include + +#include "v4l2_videodevice_test.h" + +#include "device_enumerator.h" +#include "media_device.h" + +using namespace std; +using namespace libcamera; + +bool exists(const std::string &path) +{ + struct stat sb; + + if (stat(path.c_str(), &sb) == 0) + return true; + + return false; +} + +int V4L2VideoDeviceTest::init() +{ + enumerator_ = DeviceEnumerator::create(); + if (!enumerator_) { + cerr << "Failed to create device enumerator" << endl; + return TestFail; + } + + if (enumerator_->enumerate()) { + cerr << "Failed to enumerate media devices" << endl; + return TestFail; + } + + DeviceMatch dm(driver_); + dm.add(entity_); + + media_ = enumerator_->search(dm); + if (!media_) + return TestSkip; + + MediaEntity *entity = media_->getEntityByName(entity_); + if (!entity) + return TestSkip; + + capture_ = new V4L2VideoDevice(entity); + if (!capture_) + return TestFail; + + if (!media_->acquire()) + return TestFail; + + int ret = media_->disableLinks(); + media_->release(); + if (ret) + return TestFail; + + if (capture_->open()) + return TestFail; + + V4L2DeviceFormat format = {}; + if (capture_->getFormat(&format)) + return TestFail; + + format.size.width = 640; + format.size.height = 480; + if (capture_->setFormat(&format)) + return TestFail; + + return TestPass; +} + +void V4L2VideoDeviceTest::cleanup() +{ + capture_->streamOff(); + capture_->releaseBuffers(); + capture_->close(); + + delete capture_; +}; diff --git a/test/v4l2_videodevice/v4l2_videodevice_test.h b/test/v4l2_videodevice/v4l2_videodevice_test.h new file mode 100644 index 00000000..3321b5a4 --- /dev/null +++ b/test/v4l2_videodevice/v4l2_videodevice_test.h @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * vl42device_test.h - libcamera v4l2device test base class + */ +#ifndef __LIBCAMERA_V4L2_DEVICE_TEST_H_ +#define __LIBCAMERA_V4L2_DEVICE_TEST_H_ + +#include + +#include + +#include "test.h" + +#include "device_enumerator.h" +#include "media_device.h" +#include "v4l2_videodevice.h" + +using namespace libcamera; + +class V4L2VideoDeviceTest : public Test +{ +public: + V4L2VideoDeviceTest(const char *driver, const char *entity) + : driver_(driver), entity_(entity), capture_(nullptr) + { + } + +protected: + int init(); + void cleanup(); + + std::string driver_; + std::string entity_; + std::unique_ptr enumerator_; + std::shared_ptr media_; + V4L2VideoDevice *capture_; + BufferPool pool_; +}; + +#endif /* __LIBCAMERA_V4L2_DEVICE_TEST_H_ */ -- cgit v1.2.1