summaryrefslogtreecommitdiff
path: root/src/libcamera/pipeline/uvcvideo.cpp
blob: 74bdf5c5aea519763215cd060d1f968f2536fc55 (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
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
/* 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/stream.h>

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

namespace libcamera {

LOG_DEFINE_CATEGORY(UVC)

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

	std::map<Stream *, StreamConfiguration>
	streamConfiguration(Camera *camera,
			    std::vector<Stream *> &streams) override;
	int configureStreams(Camera *camera,
			     std::map<Stream *, StreamConfiguration> &config) override;

	int allocateBuffers(Camera *camera, Stream *stream) override;
	int freeBuffers(Camera *camera, Stream *stream) override;

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

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

	bool match(DeviceEnumerator *enumerator);

private:
	std::shared_ptr<MediaDevice> media_;
	V4L2Device *video_;
	Stream stream_;
};

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

PipelineHandlerUVC::~PipelineHandlerUVC()
{
	if (video_)
		delete video_;

	if (media_)
		media_->release();
}

std::map<Stream *, StreamConfiguration>
PipelineHandlerUVC::streamConfiguration(Camera *camera,
					std::vector<Stream *> &streams)
{
	std::map<Stream *, StreamConfiguration> configs;

	StreamConfiguration config{};

	LOG(UVC, Debug) << "Retrieving default format";
	config.width = 640;
	config.height = 480;
	config.pixelFormat = V4L2_PIX_FMT_YUYV;
	config.bufferCount = 4;

	configs[&stream_] = config;

	return configs;
}

int PipelineHandlerUVC::configureStreams(Camera *camera,
					 std::map<Stream *, StreamConfiguration> &config)
{
	StreamConfiguration *cfg = &config[&stream_];

	LOG(UVC, Info) << "TODO: Configure the camera for resolution "
		       << cfg->width << "x" << cfg->height;

	return 0;
}

int PipelineHandlerUVC::allocateBuffers(Camera *camera, Stream *stream)
{
	return -ENOTRECOVERABLE;
}

int PipelineHandlerUVC::freeBuffers(Camera *camera, Stream *stream)
{
	return 0;
}

int PipelineHandlerUVC::start(const Camera *camera)
{
	LOG(UVC, Error) << "TODO: start camera";
	return 0;
}

void PipelineHandlerUVC::stop(const Camera *camera)
{
	LOG(UVC, Error) << "TODO: stop camera";
}

int PipelineHandlerUVC::queueRequest(const Camera *camera, Request *request)
{
	return 0;
}

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

	media_ = std::move(enumerator->search(dm));

	if (!media_)
		return false;

	media_->acquire();

	for (MediaEntity *entity : media_->entities()) {
		if (entity->flags() & MEDIA_ENT_FL_DEFAULT) {
			video_ = new V4L2Device(entity);
			break;
		}
	}

	if (!video_ || video_->open()) {
		if (!video_)
			LOG(UVC, Error) << "Could not find a default video device";

		media_->release();
		return false;
	}

	std::vector<Stream *> streams{ &stream_ };
	std::shared_ptr<Camera> camera = Camera::create(this, media_->model(), streams);
	registerCamera(std::move(camera));
	hotplugMediaDevice(media_.get());

	return true;
}

REGISTER_PIPELINE_HANDLER(PipelineHandlerUVC);

} /* namespace libcamera */