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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* libcamera Camera API tests
*
* Test importing buffers exported from the VIVID output device into a Camera
*/
#include <algorithm>
#include <iostream>
#include <numeric>
#include <random>
#include <vector>
#include "device_enumerator.h"
#include "media_device.h"
#include "v4l2_videodevice.h"
#include "camera_test.h"
using namespace libcamera;
/* Keep SINK_BUFFER_COUNT > CAMERA_BUFFER_COUNT + 1 */
static constexpr unsigned int SINK_BUFFER_COUNT = 8;
static constexpr unsigned int CAMERA_BUFFER_COUNT = 4;
class FrameSink
{
public:
int init()
{
int ret;
/* 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;
}
video_ = V4L2VideoDevice::fromEntityName(media_.get(), videoDeviceName);
if (!video_) {
std::cout << "Unable to open " << videoDeviceName << std::endl;
return TestFail;
}
if (video_->open())
return TestFail;
/* Configure the format. */
ret = video_->getFormat(&format_);
if (ret) {
std::cout << "Failed to get format on output device" << std::endl;
return ret;
}
format_.size.width = 1920;
format_.size.height = 1080;
format_.fourcc = V4L2_PIX_FMT_RGB24;
format_.planesCount = 1;
format_.planes[0].size = 1920 * 1080 * 3;
format_.planes[0].bpl = 1920 * 3;
if (video_->setFormat(&format_)) {
cleanup();
return TestFail;
}
/* Export the buffers to a pool. */
pool_.createBuffers(SINK_BUFFER_COUNT);
ret = video_->exportBuffers(&pool_);
if (ret) {
std::cout << "Failed to export buffers" << std::endl;
cleanup();
return TestFail;
}
/* Only use the first CAMERA_BUFFER_COUNT buffers to start with. */
availableBuffers_.resize(CAMERA_BUFFER_COUNT);
std::iota(availableBuffers_.begin(), availableBuffers_.end(), 0);
/* Connect the buffer ready signal. */
video_->bufferReady.connect(this, &FrameSink::bufferComplete);
return TestPass;
}
void cleanup()
{
if (video_) {
video_->streamOff();
video_->releaseBuffers();
video_->close();
delete video_;
video_ = nullptr;
}
if (media_)
media_->release();
}
int start()
{
requestsCount_ = 0;
done_ = false;
int ret = video_->streamOn();
if (ret < 0)
return ret;
/* Queue all the initial requests. */
for (unsigned int index = 0; index < CAMERA_BUFFER_COUNT; ++index)
queueRequest(index);
return 0;
}
int stop()
{
return video_->streamOff();
}
void requestComplete(uint64_t cookie, const Buffer *metadata)
{
unsigned int index = cookie;
Buffer *buffer = new Buffer(index, metadata);
int ret = video_->queueBuffer(buffer);
if (ret < 0)
std::cout << "Failed to queue buffer to sink" << std::endl;
}
bool done() const { return done_; }
const V4L2DeviceFormat &format() const { return format_; }
Signal<uint64_t, int> requestReady;
private:
void queueRequest(unsigned int index)
{
auto it = std::find(availableBuffers_.begin(),
availableBuffers_.end(), index);
availableBuffers_.erase(it);
uint64_t cookie = index;
BufferMemory &mem = pool_.buffers()[index];
int dmabuf = mem.planes()[0].dmabuf();
requestReady.emit(cookie, dmabuf);
requestsCount_++;
}
void bufferComplete(Buffer *buffer)
{
availableBuffers_.push_back(buffer->index());
/*
* Pick the buffer for the next request among the available
* buffers.
*
* For the first 20 frames, select the buffer that has just
* completed to keep the mapping of dmabuf fds to buffers
* unchanged in the camera.
*
* For the next 20 frames, cycle randomly over the available
* buffers. The mapping should still be kept unchanged, as the
* camera should map using the cached fds.
*
* For the last 20 frames, cycles through all buffers, which
* should trash the mappings.
*/
unsigned int index = buffer->index();
delete buffer;
std::cout << "Completed buffer, request=" << requestsCount_
<< ", available buffers=" << availableBuffers_.size()
<< std::endl;
if (requestsCount_ >= 60) {
if (availableBuffers_.size() == SINK_BUFFER_COUNT)
done_ = true;
return;
}
if (requestsCount_ == 40) {
/* Add the remaining of the buffers. */
for (unsigned int i = CAMERA_BUFFER_COUNT;
i < SINK_BUFFER_COUNT; ++i)
availableBuffers_.push_back(i);
}
if (requestsCount_ >= 20) {
/*
* Wait until we have enough buffers to make this
* meaningful. Preferably half of the camera buffers,
* but no less than 2 in any case.
*/
const unsigned int min_pool_size =
std::min(CAMERA_BUFFER_COUNT / 2, 2U);
if (availableBuffers_.size() < min_pool_size)
return;
/* Pick a buffer at random. */
unsigned int pos = random_() % availableBuffers_.size();
index = availableBuffers_[pos];
}
queueRequest(index);
}
std::shared_ptr<MediaDevice> media_;
V4L2VideoDevice *video_;
BufferPool pool_;
V4L2DeviceFormat format_;
unsigned int requestsCount_;
std::vector<int> availableBuffers_;
std::random_device random_;
bool done_;
};
class BufferImportTest : public CameraTest
{
public:
BufferImportTest()
: CameraTest()
{
}
void queueRequest(uint64_t cookie, int dmabuf)
{
Request *request = camera_->createRequest(cookie);
std::unique_ptr<Buffer> buffer = stream_->createBuffer({ dmabuf, -1, -1 });
request->addBuffer(move(buffer));
camera_->queueRequest(request);
}
protected:
void bufferComplete(Request *request, Buffer *buffer)
{
if (buffer->status() != Buffer::BufferSuccess)
return;
unsigned int index = buffer->index();
int dmabuf = buffer->dmabufs()[0];
/* Record dmabuf to index remappings. */
bool remapped = false;
if (bufferMappings_.find(index) != bufferMappings_.end()) {
if (bufferMappings_[index] != dmabuf)
remapped = true;
}
std::cout << "Completed request " << framesCaptured_
<< ": dmabuf fd " << dmabuf
<< " -> index " << index
<< " (" << (remapped ? 'R' : '-') << ")"
<< std::endl;
if (remapped)
bufferRemappings_.push_back(framesCaptured_);
bufferMappings_[index] = dmabuf;
framesCaptured_++;
sink_.requestComplete(request->cookie(), buffer);
if (framesCaptured_ == 60)
sink_.stop();
}
int initCamera()
{
if (camera_->acquire()) {
std::cout << "Failed to acquire the camera" << std::endl;
return TestFail;
}
/*
* Configure the Stream to work with externally allocated
* buffers by setting the memoryType to ExternalMemory.
*/
std::unique_ptr<CameraConfiguration> config;
config = camera_->generateConfiguration({ StreamRole::VideoRecording });
if (!config || config->size() != 1) {
std::cout << "Failed to generate configuration" << std::endl;
return TestFail;
}
const V4L2DeviceFormat &format = sink_.format();
StreamConfiguration &cfg = config->at(0);
cfg.size = format.size;
cfg.pixelFormat = format.fourcc;
cfg.bufferCount = CAMERA_BUFFER_COUNT;
cfg.memoryType = ExternalMemory;
if (camera_->configure(config.get())) {
std::cout << "Failed to set configuration" << std::endl;
return TestFail;
}
stream_ = cfg.stream();
/* Allocate buffers. */
if (camera_->allocateBuffers()) {
std::cout << "Failed to allocate buffers" << std::endl;
return TestFail;
}
/* Connect the buffer completed signal. */
camera_->bufferCompleted.connect(this, &BufferImportTest::bufferComplete);
return TestPass;
}
int init()
{
int ret = CameraTest::init();
if (ret)
return ret;
ret = sink_.init();
if (ret != TestPass) {
cleanup();
return ret;
}
ret = initCamera();
if (ret != TestPass) {
cleanup();
return ret;
}
sink_.requestReady.connect(this, &BufferImportTest::queueRequest);
return TestPass;
}
int run()
{
int ret;
framesCaptured_ = 0;
if (camera_->start()) {
std::cout << "Failed to start camera" << std::endl;
return TestFail;
}
ret = sink_.start();
if (ret < 0) {
std::cout << "Failed to start sink" << std::endl;
return TestFail;
}
EventDispatcher *dispatcher = CameraManager::instance()->eventDispatcher();
Timer timer;
timer.start(5000);
while (timer.isRunning() && !sink_.done())
dispatcher->processEvents();
std::cout << framesCaptured_ << " frames captured, "
<< bufferRemappings_.size() << " buffers remapped"
<< std::endl;
if (framesCaptured_ < 60) {
std::cout << "Too few frames captured" << std::endl;
return TestFail;
}
if (bufferRemappings_.empty()) {
std::cout << "No buffer remappings" << std::endl;
return TestFail;
}
if (bufferRemappings_[0] < 40) {
std::cout << "Early buffer remapping" << std::endl;
return TestFail;
}
return TestPass;
}
void cleanup()
{
sink_.cleanup();
camera_->stop();
camera_->freeBuffers();
CameraTest::cleanup();
}
private:
Stream *stream_;
std::map<unsigned int, int> bufferMappings_;
std::vector<unsigned int> bufferRemappings_;
unsigned int framesCaptured_;
FrameSink sink_;
};
TEST_REGISTER(BufferImportTest);
|