summaryrefslogtreecommitdiff
path: root/test/libtest/buffer_source.cpp
blob: 1b261697279a4a58d3070ffe271092f888e84bef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2020, Google Inc.
 *
 * A provider of external buffers, suitable for use in tests.
 */

#include "buffer_source.h"

#include <iostream>
#include <memory>

#include "libcamera/internal/device_enumerator.h"

#include "test.h"

using namespace libcamera;

BufferSource::BufferSource()
{
}

BufferSource::~BufferSource()
{
	if (media_)
		media_->release();
}

int BufferSource::allocate(const StreamConfiguration &config)
{
	/* Locate and open the video device. */
	std::string videoDeviceName = "vivid-000-vid-out";

	std::unique_ptr<DeviceEnumerator> enumerator =
		DeviceEnumerator::create();
	if (!enumerator) {
		std::cout << "Failed to create device enumerator" << std::endl;
		return TestFail;
	}

	if (enumerator->enumerate()) {
		std::cout << "Failed to enumerate media devices" << std::endl;
		return TestFail;
	}

	DeviceMatch dm("vivid");
	dm.add(videoDeviceName);

	media_ = enumerator->search(dm);
	if (!media_) {
		std::cout << "No vivid output device available" << std::endl;
		return TestSkip;
	}

	std::unique_ptr<V4L2VideoDevice> video = V4L2VideoDevice::fromEntityName(media_.get(), videoDeviceName);
	if (!video) {
		std::cout << "Failed to get video device from entity "
			  << videoDeviceName << std::endl;
		return TestFail;
	}

	if (video->open()) {
		std::cout << "Unable to open " << videoDeviceName << std::endl;
		return TestFail;
	}

	/* Configure the format. */
	V4L2DeviceFormat format;
	if (video->getFormat(&format)) {
		std::cout << "Failed to get format on output device" << std::endl;
		return TestFail;
	}

	format.size = config.size;
	format.fourcc = V4L2PixelFormat::fromPixelFormat(config.pixelFormat);
	if (video->setFormat(&format)) {
		std::cout << "Failed to set format on output device" << std::endl;
		return TestFail;
	}

	if (video->allocateBuffers(config.bufferCount, &buffers_) < 0) {
		std::cout << "Failed to allocate buffers" << std::endl;
		return TestFail;
	}

	video->close();

	return TestPass;
}

const std::vector<std::unique_ptr<FrameBuffer>> &BufferSource::buffers()
{
	return buffers_;
}
t">} void SimpleCapture::stop() { if (!config_ || !allocator_->allocated()) return; camera_->stop(); camera_->requestCompleted.disconnect(this, &SimpleCapture::requestComplete); Stream *stream = config_->at(0).stream(); allocator_->free(stream); } /* SimpleCaptureBalanced */ SimpleCaptureBalanced::SimpleCaptureBalanced(std::shared_ptr<Camera> camera) : SimpleCapture(camera) { } void SimpleCaptureBalanced::capture(unsigned int numRequests) { start(); Stream *stream = config_->at(0).stream(); const std::vector<std::unique_ptr<FrameBuffer>> &buffers = allocator_->buffers(stream); /* No point in testing less requests then the camera depth. */ if (buffers.size() > numRequests) { std::cout << "Camera needs " + std::to_string(buffers.size()) + " requests, can't test only " + std::to_string(numRequests) << std::endl; GTEST_SKIP(); } queueCount_ = 0; captureCount_ = 0; captureLimit_ = numRequests; /* Queue the recommended number of reqeuests. */ std::vector<std::unique_ptr<libcamera::Request>> requests; for (const std::unique_ptr<FrameBuffer> &buffer : buffers) { std::unique_ptr<Request> request = camera_->createRequest(); ASSERT_TRUE(request) << "Can't create request"; ASSERT_EQ(request->addBuffer(stream, buffer.get()), 0) << "Can't set buffer for request"; ASSERT_EQ(queueRequest(request.get()), 0) << "Failed to queue request"; requests.push_back(std::move(request)); } /* Run capture session. */ loop_ = new EventLoop(); loop_->exec(); stop(); delete loop_; ASSERT_EQ(captureCount_, captureLimit_); } int SimpleCaptureBalanced::queueRequest(Request *request) { queueCount_++; if (queueCount_ > captureLimit_) return 0; return camera_->queueRequest(request); } void SimpleCaptureBalanced::requestComplete(Request *request) { captureCount_++; if (captureCount_ >= captureLimit_) { loop_->exit(0); return; } request->reuse(Request::ReuseBuffers); if (queueRequest(request)) loop_->exit(-EINVAL); } /* SimpleCaptureUnbalanced */ SimpleCaptureUnbalanced::SimpleCaptureUnbalanced(std::shared_ptr<Camera> camera) : SimpleCapture(camera) { } void SimpleCaptureUnbalanced::capture(unsigned int numRequests) { start(); Stream *stream = config_->at(0).stream(); const std::vector<std::unique_ptr<FrameBuffer>> &buffers = allocator_->buffers(stream); captureCount_ = 0; captureLimit_ = numRequests; /* Queue the recommended number of reqeuests. */ std::vector<std::unique_ptr<libcamera::Request>> requests; for (const std::unique_ptr<FrameBuffer> &buffer : buffers) { std::unique_ptr<Request> request = camera_->createRequest(); ASSERT_TRUE(request) << "Can't create request"; ASSERT_EQ(request->addBuffer(stream, buffer.get()), 0) << "Can't set buffer for request"; ASSERT_EQ(camera_->queueRequest(request.get()), 0) << "Failed to queue request"; requests.push_back(std::move(request)); } /* Run capture session. */ loop_ = new EventLoop(); int status = loop_->exec(); stop(); delete loop_; ASSERT_EQ(status, 0); } void SimpleCaptureUnbalanced::requestComplete(Request *request) { captureCount_++; if (captureCount_ >= captureLimit_) { loop_->exit(0); return; } request->reuse(Request::ReuseBuffers); if (camera_->queueRequest(request)) loop_->exit(-EINVAL); }