summaryrefslogtreecommitdiff
path: root/src/v4l2/v4l2_compat_manager.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/v4l2/v4l2_compat_manager.h')
0 files changed, 0 insertions, 0 deletions
/a> 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * uvcvideo.cpp - Pipeline handler for uvcvideo devices
 */

#include <libcamera/camera.h>
#include <libcamera/request.h>
#include <libcamera/stream.h>

#include "device_enumerator.h"
#include "log.h"
#include "media_device.h"
#include "pipeline_handler.h"
#include "utils.h"
#include "v4l2_device.h"

namespace libcamera {

LOG_DEFINE_CATEGORY(UVC)

class PipelineHandlerUVC : public PipelineHandler
{
public:
	PipelineHandlerUVC(CameraManager *manager);
	~PipelineHandlerUVC();

	CameraConfiguration
	streamConfiguration(Camera *camera,
			    const std::vector<StreamUsage> &usages) override;
	int configureStreams(Camera *camera,
			     const CameraConfiguration &config) override;

	int allocateBuffers(Camera *camera,
			    const std::set<Stream *> &streams) override;
	int freeBuffers(Camera *camera,
			const std::set<Stream *> &streams) override;

	int start(Camera *camera) override;
	void stop(Camera *camera) override;

	int queueRequest(Camera *camera, Request *request) override;

	bool match(DeviceEnumerator *enumerator) override;

private:
	class UVCCameraData : public CameraData
	{
	public:
		UVCCameraData(PipelineHandler *pipe)
			: CameraData(pipe), video_(nullptr)
		{
		}

		~UVCCameraData()
		{
			delete video_;
		}

		void bufferReady(Buffer *buffer);

		V4L2Device *video_;
		Stream stream_;
	};

	UVCCameraData *cameraData(const Camera *camera)
	{
		return static_cast<UVCCameraData *>(
			PipelineHandler::cameraData(camera));
	}

	std::shared_ptr<MediaDevice> media_;
};

PipelineHandlerUVC::PipelineHandlerUVC(CameraManager *manager)
	: PipelineHandler(manager), media_(nullptr)
{
}

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

CameraConfiguration
PipelineHandlerUVC::streamConfiguration(Camera *camera,
					const std::vector<StreamUsage> &usages)
{
	UVCCameraData *data = cameraData(camera);
	CameraConfiguration config;
	StreamConfiguration cfg{};

	cfg.pixelFormat = V4L2_PIX_FMT_YUYV;
	cfg.size = { 640, 480 };
	cfg.bufferCount = 4;

	config[&data->stream_] = cfg;

	return config;
}

int PipelineHandlerUVC::configureStreams(Camera *camera,
					 const CameraConfiguration &config)
{
	UVCCameraData *data = cameraData(camera);
	const StreamConfiguration *cfg = &config[&data->stream_];
	int ret;

	V4L2DeviceFormat format = {};
	format.fourcc = cfg->pixelFormat;
	format.size = cfg->size;

	ret = data->video_->setFormat(&format);
	if (ret)
		return ret;

	if (format.size != cfg->size ||
	    format.fourcc != cfg->pixelFormat)
		return -EINVAL;

	return 0;
}

int PipelineHandlerUVC::allocateBuffers(Camera *camera,
					const std::set<Stream *> &streams)
{
	UVCCameraData *data = cameraData(camera);
	Stream *stream = *streams.begin();
	const StreamConfiguration &cfg = stream->configuration();

	LOG(UVC, Debug) << "Requesting " << cfg.bufferCount << " buffers";

	return data->video_->exportBuffers(&stream->bufferPool());
}

int PipelineHandlerUVC::freeBuffers(Camera *camera,
				    const std::set<Stream *> &streams)
{
	UVCCameraData *data = cameraData(camera);
	return data->video_->releaseBuffers();
}

int PipelineHandlerUVC::start(Camera *camera)
{
	UVCCameraData *data = cameraData(camera);
	return data->video_->streamOn();
}

void PipelineHandlerUVC::stop(Camera *camera)
{
	UVCCameraData *data = cameraData(camera);
	data->video_->streamOff();
	PipelineHandler::stop(camera);
}

int PipelineHandlerUVC::queueRequest(Camera *camera, Request *request)
{
	UVCCameraData *data = cameraData(camera);
	Buffer *buffer = request->findBuffer(&data->stream_);
	if (!buffer) {
		LOG(UVC, Error)
			<< "Attempt to queue request with invalid stream";

		return -ENOENT;
	}

	int ret = data->video_->queueBuffer(buffer);
	if (ret < 0)
		return ret;

	PipelineHandler::queueRequest(camera, request);

	return 0;
}

bool PipelineHandlerUVC::match(DeviceEnumerator *enumerator)
{
	DeviceMatch dm("uvcvideo");

	media_ = enumerator->search(dm);
	if (!media_)
		return false;

	if (!media_->acquire())
		return false;

	std::unique_ptr<UVCCameraData> data = utils::make_unique<UVCCameraData>(this);

	/* Locate and open the default video node. */
	for (MediaEntity *entity : media_->entities()) {
		if (entity->flags() & MEDIA_ENT_FL_DEFAULT) {
			data->video_ = new V4L2Device(entity);
			break;
		}
	}

	if (!data->video_) {
		LOG(UVC, Error) << "Could not find a default video device";
		return false;
	}

	if (data->video_->open())
		return false;

	data->video_->bufferReady.connect(data.get(), &UVCCameraData::bufferReady);

	/* Create and register the camera. */
	std::set<Stream *> streams{ &data->stream_ };
	std::shared_ptr<Camera> camera = Camera::create(this, media_->model(), streams);
	registerCamera(std::move(camera), std::move(data));

	/* Enable hot-unplug notifications. */
	hotplugMediaDevice(media_.get());

	return true;
}

void PipelineHandlerUVC::UVCCameraData::bufferReady(Buffer *buffer)
{
	Request *request = queuedRequests_.front();

	pipe_->completeBuffer(camera_, request, buffer);
	pipe_->completeRequest(camera_, request);
}

REGISTER_PIPELINE_HANDLER(PipelineHandlerUVC);

} /* namespace libcamera */