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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2024, Google Inc.
*
* Pipeline handler for virtual cameras
*/
#include "virtual.h"
#include <algorithm>
#include <array>
#include <chrono>
#include <errno.h>
#include <map>
#include <memory>
#include <ostream>
#include <set>
#include <stdint.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#include <libcamera/base/flags.h>
#include <libcamera/base/log.h>
#include <libcamera/control_ids.h>
#include <libcamera/controls.h>
#include <libcamera/formats.h>
#include <libcamera/pixel_format.h>
#include <libcamera/property_ids.h>
#include <libcamera/request.h>
#include "libcamera/internal/camera.h"
#include "libcamera/internal/dma_buf_allocator.h"
#include "libcamera/internal/formats.h"
#include "libcamera/internal/framebuffer.h"
#include "libcamera/internal/pipeline_handler.h"
namespace libcamera {
LOG_DEFINE_CATEGORY(Virtual)
namespace {
uint64_t currentTimestamp()
{
const auto now = std::chrono::steady_clock::now();
auto nsecs = std::chrono::duration_cast<std::chrono::nanoseconds>(
now.time_since_epoch());
return nsecs.count();
}
} /* namespace */
class VirtualCameraConfiguration : public CameraConfiguration
{
public:
static constexpr unsigned int kBufferCount = 4;
VirtualCameraConfiguration(VirtualCameraData *data);
Status validate() override;
private:
const VirtualCameraData *data_;
};
class PipelineHandlerVirtual : public PipelineHandler
{
public:
PipelineHandlerVirtual(CameraManager *manager);
~PipelineHandlerVirtual();
std::unique_ptr<CameraConfiguration> generateConfiguration(Camera *camera,
Span<const StreamRole> roles) override;
int configure(Camera *camera, CameraConfiguration *config) override;
int exportFrameBuffers(Camera *camera, Stream *stream,
std::vector<std::unique_ptr<FrameBuffer>> *buffers) override;
int start(Camera *camera, const ControlList *controls) override;
void stopDevice(Camera *camera) override;
int queueRequestDevice(Camera *camera, Request *request) override;
bool match(DeviceEnumerator *enumerator) override;
private:
static bool created_;
VirtualCameraData *cameraData(Camera *camera)
{
return static_cast<VirtualCameraData *>(camera->_d());
}
void initFrameGenerator(Camera *camera);
DmaBufAllocator dmaBufAllocator_;
bool resetCreated_ = false;
};
VirtualCameraData::VirtualCameraData(PipelineHandler *pipe,
const std::vector<Resolution> &supportedResolutions)
: Camera::Private(pipe), supportedResolutions_(supportedResolutions)
{
for (const auto &resolution : supportedResolutions_) {
if (minResolutionSize_.isNull() || minResolutionSize_ > resolution.size)
minResolutionSize_ = resolution.size;
maxResolutionSize_ = std::max(maxResolutionSize_, resolution.size);
}
/* \todo Support multiple streams and pass multi_stream_test */
streamConfigs_.resize(kMaxStream);
}
VirtualCameraConfiguration::VirtualCameraConfiguration(VirtualCameraData *data)
: CameraConfiguration(), data_(data)
{
}
CameraConfiguration::Status VirtualCameraConfiguration::validate()
{
Status status = Valid;
if (config_.empty()) {
LOG(Virtual, Error) << "Empty config";
return Invalid;
}
/* Only one stream is supported */
if (config_.size() > VirtualCameraData::kMaxStream) {
config_.resize(VirtualCameraData::kMaxStream);
status = Adjusted;
}
for (StreamConfiguration &cfg : config_) {
bool adjusted = false;
bool found = false;
for (const auto &resolution : data_->supportedResolutions_) {
if (resolution.size.width == cfg.size.width &&
resolution.size.height == cfg.size.height) {
found = true;
break;
}
}
if (!found) {
/*
* \todo It's a pipeline's decision to choose a
* resolution when the exact one is not supported.
* Defining the default logic in PipelineHandler to
* find the closest resolution would be nice.
*/
cfg.size = data_->maxResolutionSize_;
status = Adjusted;
adjusted = true;
}
if (cfg.pixelFormat != formats::NV12) {
cfg.pixelFormat = formats::NV12;
status = Adjusted;
adjusted = true;
}
if (adjusted)
LOG(Virtual, Info)
<< "Stream configuration adjusted to " << cfg.toString();
const PixelFormatInfo &info = PixelFormatInfo::info(cfg.pixelFormat);
cfg.stride = info.stride(cfg.size.width, 0, 1);
cfg.frameSize = info.frameSize(cfg.size, 1);
cfg.bufferCount = VirtualCameraConfiguration::kBufferCount;
}
return status;
}
/* static */
bool PipelineHandlerVirtual::created_ = false;
PipelineHandlerVirtual::PipelineHandlerVirtual(CameraManager *manager)
: PipelineHandler(manager),
dmaBufAllocator_(DmaBufAllocator::DmaBufAllocatorFlag::CmaHeap |
DmaBufAllocator::DmaBufAllocatorFlag::SystemHeap |
DmaBufAllocator::DmaBufAllocatorFlag::UDmaBuf)
{
}
PipelineHandlerVirtual::~PipelineHandlerVirtual()
{
if (resetCreated_)
created_ = false;
}
std::unique_ptr<CameraConfiguration>
PipelineHandlerVirtual::generateConfiguration(Camera *camera,
Span<const StreamRole> roles)
{
VirtualCameraData *data = cameraData(camera);
auto config = std::make_unique<VirtualCameraConfiguration>(data);
if (roles.empty())
return config;
for (const StreamRole role : roles) {
switch (role) {
case StreamRole::StillCapture:
case StreamRole::VideoRecording:
case StreamRole::Viewfinder:
break;
case StreamRole::Raw:
default:
LOG(Virtual, Error)
<< "Requested stream role not supported: " << role;
config.reset();
return config;
}
std::map<PixelFormat, std::vector<SizeRange>> streamFormats;
PixelFormat pixelFormat = formats::NV12;
streamFormats[pixelFormat] = { { data->minResolutionSize_,
data->maxResolutionSize_ } };
StreamFormats formats(streamFormats);
StreamConfiguration cfg(formats);
cfg.pixelFormat = pixelFormat;
cfg.size = data->maxResolutionSize_;
cfg.bufferCount = VirtualCameraConfiguration::kBufferCount;
config->addConfiguration(cfg);
}
ASSERT(config->validate() != CameraConfiguration::Invalid);
return config;
}
int PipelineHandlerVirtual::configure(Camera *camera,
CameraConfiguration *config)
{
VirtualCameraData *data = cameraData(camera);
for (auto [i, c] : utils::enumerate(*config)) {
c.setStream(&data->streamConfigs_[i].stream);
data->streamConfigs_[i].frameGenerator->configure(c.size);
}
return 0;
}
int PipelineHandlerVirtual::exportFrameBuffers([[maybe_unused]] Camera *camera,
Stream *stream,
std::vector<std::unique_ptr<FrameBuffer>> *buffers)
{
if (!dmaBufAllocator_.isValid())
return -ENOBUFS;
const StreamConfiguration &config = stream->configuration();
auto info = PixelFormatInfo::info(config.pixelFormat);
std::vector<unsigned int> planeSizes;
for (size_t i = 0; i < info.planes.size(); ++i)
planeSizes.push_back(info.planeSize(config.size, i));
return dmaBufAllocator_.exportBuffers(config.bufferCount, planeSizes, buffers);
}
int PipelineHandlerVirtual::start([[maybe_unused]] Camera *camera,
[[maybe_unused]] const ControlList *controls)
{
return 0;
}
void PipelineHandlerVirtual::stopDevice([[maybe_unused]] Camera *camera)
{
}
int PipelineHandlerVirtual::queueRequestDevice([[maybe_unused]] Camera *camera,
Request *request)
{
VirtualCameraData *data = cameraData(camera);
for (auto const &[stream, buffer] : request->buffers()) {
bool found = false;
/* map buffer and fill test patterns */
for (auto &streamConfig : data->streamConfigs_) {
if (stream == &streamConfig.stream) {
found = true;
if (streamConfig.frameGenerator->generateFrame(
stream->configuration().size, buffer))
buffer->_d()->cancel();
completeBuffer(request, buffer);
break;
}
}
ASSERT(found);
}
request->metadata().set(controls::SensorTimestamp, currentTimestamp());
completeRequest(request);
return 0;
}
bool PipelineHandlerVirtual::match([[maybe_unused]] DeviceEnumerator *enumerator)
{
if (created_)
return false;
created_ = true;
/* \todo Add virtual cameras according to a config file. */
std::vector<VirtualCameraData::Resolution> supportedResolutions;
supportedResolutions.resize(2);
supportedResolutions[0] = { .size = Size(1920, 1080), .frameRates = { 30 } };
supportedResolutions[1] = { .size = Size(1280, 720), .frameRates = { 30 } };
std::unique_ptr<VirtualCameraData> data =
std::make_unique<VirtualCameraData>(this, supportedResolutions);
data->properties_.set(properties::Location, properties::CameraLocationFront);
data->properties_.set(properties::Model, "Virtual Video Device");
data->properties_.set(properties::PixelArrayActiveAreas, { Rectangle(Size(1920, 1080)) });
/* \todo Set FrameDurationLimits based on config. */
ControlInfoMap::Map controls;
int64_t min_frame_duration = 33333, max_frame_duration = 33333;
controls[&controls::FrameDurationLimits] = ControlInfo(min_frame_duration, max_frame_duration);
std::vector<ControlValue> supportedFaceDetectModes{
static_cast<int32_t>(controls::draft::FaceDetectModeOff),
};
controls[&controls::draft::FaceDetectMode] = ControlInfo(supportedFaceDetectModes);
data->controlInfo_ = ControlInfoMap(std::move(controls), controls::controls);
/* Create and register the camera. */
std::set<Stream *> streams;
for (auto &streamConfig : data->streamConfigs_)
streams.insert(&streamConfig.stream);
const std::string id = "Virtual0";
std::shared_ptr<Camera> camera = Camera::create(std::move(data), id, streams);
initFrameGenerator(camera.get());
registerCamera(std::move(camera));
resetCreated_ = true;
return true;
}
void PipelineHandlerVirtual::initFrameGenerator(Camera *camera)
{
auto data = cameraData(camera);
for (auto &streamConfig : data->streamConfigs_) {
if (data->testPattern_ == TestPattern::DiagonalLines)
streamConfig.frameGenerator = std::make_unique<DiagonalLinesGenerator>();
else
streamConfig.frameGenerator = std::make_unique<ColorBarsGenerator>();
}
}
REGISTER_PIPELINE_HANDLER(PipelineHandlerVirtual, "virtual")
} /* namespace libcamera */
|