-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathV4L2Capture.cpp
More file actions
816 lines (693 loc) · 24.9 KB
/
V4L2Capture.cpp
File metadata and controls
816 lines (693 loc) · 24.9 KB
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
#include "V4L2Capture.hpp"
#include "StereoDepthPipeline.hpp"
#define SAMPLE_LOG_TAG "V4L2"
#include "sample_log.h"
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <algorithm>
#include <array>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
namespace {
bool queryControlRange(int fd, uint32_t id, struct v4l2_queryctrl& query);
int32_t normalizeControlValue(const struct v4l2_queryctrl& query, int32_t value) {
int32_t normalized = value;
if (normalized < query.minimum) {
normalized = query.minimum;
}
if (normalized > query.maximum) {
normalized = query.maximum;
}
if (query.step > 1) {
const int32_t base = query.minimum;
const int32_t offset = normalized - base;
const int32_t steps = offset / query.step;
normalized = base + steps * query.step;
if (normalized > query.maximum) {
normalized = query.maximum;
}
}
return normalized;
}
const char* controlName(uint32_t id) {
switch (id) {
case V4L2_CID_BRIGHTNESS:
return "brightness";
case V4L2_CID_CONTRAST:
return "contrast";
case V4L2_CID_SATURATION:
return "saturation";
case V4L2_CID_GAMMA:
return "gamma";
case V4L2_CID_SHARPNESS:
return "sharpness";
case V4L2_CID_AUTO_WHITE_BALANCE:
return "white_balance_auto";
case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
return "white_balance_temperature";
case V4L2_CID_POWER_LINE_FREQUENCY:
return "power_line_frequency";
case V4L2_CID_GAIN:
return "gain";
default:
return "unknown";
}
}
bool isSampleExposedControl(uint32_t id) {
switch (id) {
case V4L2_CID_BRIGHTNESS:
case V4L2_CID_CONTRAST:
case V4L2_CID_SATURATION:
case V4L2_CID_GAMMA:
case V4L2_CID_SHARPNESS:
case V4L2_CID_AUTO_WHITE_BALANCE:
case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
case V4L2_CID_POWER_LINE_FREQUENCY:
case V4L2_CID_GAIN:
return true;
default:
return false;
}
}
bool isControlAdjustableNow(const struct v4l2_queryctrl& query) {
return (query.flags &
(V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_INACTIVE)) == 0;
}
const char* controlTypeName(__u32 type) {
switch (type) {
case V4L2_CTRL_TYPE_INTEGER:
return "int";
case V4L2_CTRL_TYPE_BOOLEAN:
return "bool";
case V4L2_CTRL_TYPE_MENU:
return "menu";
case V4L2_CTRL_TYPE_BUTTON:
return "button";
case V4L2_CTRL_TYPE_INTEGER64:
return "int64";
case V4L2_CTRL_TYPE_CTRL_CLASS:
return "class";
case V4L2_CTRL_TYPE_STRING:
return "string";
case V4L2_CTRL_TYPE_BITMASK:
return "bitmask";
case V4L2_CTRL_TYPE_INTEGER_MENU:
return "int_menu";
default:
return "other";
}
}
std::string controlFlagsString(__u32 flags) {
std::string text;
const auto append = [&](const char* name) {
if (!text.empty()) {
text += ",";
}
text += name;
};
if ((flags & V4L2_CTRL_FLAG_DISABLED) != 0) {
append("disabled");
}
if ((flags & V4L2_CTRL_FLAG_READ_ONLY) != 0) {
append("readonly");
}
if ((flags & V4L2_CTRL_FLAG_VOLATILE) != 0) {
append("volatile");
}
if ((flags & V4L2_CTRL_FLAG_INACTIVE) != 0) {
append("inactive");
}
if ((flags & V4L2_CTRL_FLAG_WRITE_ONLY) != 0) {
append("writeonly");
}
if ((flags & V4L2_CTRL_FLAG_SLIDER) != 0) {
append("slider");
}
return text.empty() ? "-" : text;
}
bool isResettableControl(const struct v4l2_queryctrl& query) {
if ((query.flags & V4L2_CTRL_FLAG_DISABLED) != 0 ||
(query.flags & V4L2_CTRL_FLAG_READ_ONLY) != 0) {
return false;
}
switch (query.type) {
case V4L2_CTRL_TYPE_INTEGER:
case V4L2_CTRL_TYPE_BOOLEAN:
case V4L2_CTRL_TYPE_MENU:
case V4L2_CTRL_TYPE_INTEGER_MENU:
case V4L2_CTRL_TYPE_BITMASK:
return true;
default:
return false;
}
}
bool queryControlRange(int fd, uint32_t id, struct v4l2_queryctrl& query) {
std::memset(&query, 0, sizeof(query));
query.id = id;
if (::ioctl(fd, VIDIOC_QUERYCTRL, &query) != 0) {
return false;
}
return (query.flags & V4L2_CTRL_FLAG_DISABLED) == 0;
}
void applyOptionalControl(int fd, const std::string& device, uint32_t id,
const std::optional<int32_t>& requestedValue) {
if (!requestedValue.has_value()) {
return;
}
struct v4l2_queryctrl query = {};
if (!queryControlRange(fd, id, query)) {
ALOGW("UVC control %s is not supported by %s", controlName(id), device.c_str());
return;
}
if ((query.flags & V4L2_CTRL_FLAG_READ_ONLY) != 0) {
ALOGW("UVC control %s is read-only on %s", controlName(id), device.c_str());
return;
}
if ((query.flags & V4L2_CTRL_FLAG_INACTIVE) != 0) {
ALOGW(
"UVC control %s is currently inactive on %s and may be locked by another auto setting",
controlName(id), device.c_str());
return;
}
const int32_t requested = *requestedValue;
int32_t finalValue = normalizeControlValue(query, requested);
if (finalValue != requested) {
ALOGW("UVC control %s=%d adjusted to %d within range [%d, %d] step %d on %s",
controlName(id), requested, finalValue, query.minimum, query.maximum, query.step,
device.c_str());
}
struct v4l2_control ctrl = {};
ctrl.id = id;
ctrl.value = finalValue;
if (::ioctl(fd, VIDIOC_S_CTRL, &ctrl) != 0) {
ALOGW("Failed to set UVC control %s=%d on %s: %s", controlName(id), finalValue,
device.c_str(), strerror(errno));
return;
}
ALOGN("Set UVC control %s=%d on %s (range [%d, %d], step %d, default %d)", controlName(id),
finalValue, device.c_str(), query.minimum, query.maximum, query.step,
query.default_value);
}
void applyUvcControls(int fd, const std::string& device,
const V4L2Capture::UvcControlSettings& controls) {
applyOptionalControl(fd, device, V4L2_CID_AUTO_WHITE_BALANCE, controls.autoWhiteBalance);
applyOptionalControl(fd, device, V4L2_CID_WHITE_BALANCE_TEMPERATURE,
controls.whiteBalanceTemperature);
applyOptionalControl(fd, device, V4L2_CID_POWER_LINE_FREQUENCY, controls.powerLineFrequency);
applyOptionalControl(fd, device, V4L2_CID_BRIGHTNESS, controls.brightness);
applyOptionalControl(fd, device, V4L2_CID_CONTRAST, controls.contrast);
applyOptionalControl(fd, device, V4L2_CID_SATURATION, controls.saturation);
applyOptionalControl(fd, device, V4L2_CID_GAMMA, controls.gamma);
applyOptionalControl(fd, device, V4L2_CID_SHARPNESS, controls.sharpness);
applyOptionalControl(fd, device, V4L2_CID_GAIN, controls.gain);
}
bool resetControlToDefault(int fd, uint32_t id) {
struct v4l2_queryctrl query = {};
if (!queryControlRange(fd, id, query)) {
return true;
}
if (!isResettableControl(query)) {
ALOGN("Skip reset control %s (id=0x%08x) because it is not writable", controlName(id), id);
return true;
}
if ((query.flags & V4L2_CTRL_FLAG_INACTIVE) != 0) {
ALOGN("Skip reset control %s (id=0x%08x) because it is currently inactive", controlName(id),
id);
return true;
}
struct v4l2_control ctrl = {};
ctrl.id = id;
ctrl.value = query.default_value;
if (::ioctl(fd, VIDIOC_S_CTRL, &ctrl) != 0) {
ALOGW("Failed to reset control %s (id=0x%08x) to default=%d: %s", controlName(id), id,
query.default_value, strerror(errno));
return false;
}
ALOGN("Reset control %s (id=0x%08x) to default=%d", controlName(id), id, query.default_value);
return true;
}
std::string trimWhitespace(const std::string& value) {
const size_t first = value.find_first_not_of(" \t\r\n");
if (first == std::string::npos) {
return "";
}
const size_t last = value.find_last_not_of(" \t\r\n");
return value.substr(first, last - first + 1);
}
std::string getBaseName(const std::string& path) {
const size_t pos = path.find_last_of('/');
if (pos == std::string::npos) {
return path;
}
return path.substr(pos + 1);
}
std::string getParentPath(const std::string& path) {
const size_t pos = path.find_last_of('/');
if (pos == std::string::npos) {
return "";
}
if (pos == 0) {
return "/";
}
return path.substr(0, pos);
}
std::string readSingleLineFile(const std::string& path) {
std::ifstream file(path);
if (!file.is_open()) {
return "";
}
std::string line;
std::getline(file, line);
return trimWhitespace(line);
}
std::string readUeventValue(const std::string& path, const std::string& key) {
std::ifstream file(path);
if (!file.is_open()) {
return "";
}
const std::string prefix = key + "=";
std::string line;
while (std::getline(file, line)) {
line = trimWhitespace(line);
if (line.rfind(prefix, 0) == 0) {
return trimWhitespace(line.substr(prefix.size()));
}
}
return "";
}
bool isDirectory(const std::string& path) {
struct stat st = {};
if (::stat(path.c_str(), &st) != 0) {
return false;
}
return S_ISDIR(st.st_mode);
}
bool shouldSkipDirectoryName(const std::string& name) { return name == "." || name == ".."; }
bool isUsbDeviceNode(const std::string& path) {
const std::string baseName = getBaseName(path);
if (baseName.empty() || baseName.find(':') != std::string::npos) {
return false;
}
return !readSingleLineFile(path + "/idVendor").empty() &&
!readSingleLineFile(path + "/idProduct").empty();
}
std::string findUsbDeviceAncestor(const std::string& startPath) {
std::string currentPath = startPath;
while (!currentPath.empty() && currentPath != "/") {
if (isUsbDeviceNode(currentPath)) {
return currentPath;
}
currentPath = getParentPath(currentPath);
}
return "";
}
bool findUeventValueInSubtree(const std::string& rootPath, const std::string& key, int depth,
std::string& outValue, std::string& outPath) {
if (depth < 0 || !isDirectory(rootPath)) {
return false;
}
const std::string ueventPath = rootPath + "/uevent";
const std::string value = readUeventValue(ueventPath, key);
if (!value.empty()) {
outValue = value;
outPath = rootPath;
return true;
}
DIR* dir = ::opendir(rootPath.c_str());
if (dir == nullptr) {
return false;
}
struct dirent* entry = nullptr;
bool found = false;
while ((entry = ::readdir(dir)) != nullptr) {
const std::string name(entry->d_name);
if (shouldSkipDirectoryName(name)) {
continue;
}
const std::string childPath = rootPath + "/" + name;
if (!isDirectory(childPath)) {
continue;
}
if (findUeventValueInSubtree(childPath, key, depth - 1, outValue, outPath)) {
found = true;
break;
}
}
::closedir(dir);
return found;
}
bool findAssociatedHidUniq(const std::string& startPath, std::string& outUniq,
std::string& outPath) {
const std::string usbDevicePath = findUsbDeviceAncestor(startPath);
if (usbDevicePath.empty()) {
return false;
}
return findUeventValueInSubtree(usbDevicePath, "HID_UNIQ", 4, outUniq, outPath);
}
} // namespace
V4L2Capture::DeviceInfo V4L2Capture::probeDeviceInfo(const std::string& device) {
DeviceInfo info;
info.devicePath = device;
const std::string videoNode = getBaseName(device);
if (videoNode.empty()) {
return info;
}
const std::string sysfsPath = "/sys/class/video4linux/" + videoNode + "/device";
char resolvedPath[PATH_MAX] = {};
if (::realpath(sysfsPath.c_str(), resolvedPath) == nullptr) {
return info;
}
std::string currentPath(resolvedPath);
const std::string usbDevicePath = findUsbDeviceAncestor(currentPath);
info.isUsb = !usbDevicePath.empty();
std::string hidUniq;
std::string hidPath;
if (findAssociatedHidUniq(currentPath, hidUniq, hidPath)) {
info.serialNumber = hidUniq;
return info;
}
return info;
}
V4L2Capture::V4L2Capture(const std::string& device, int width, int height, int fps,
const UvcControlSettings& controls)
: device_(device),
width_(width),
height_(height),
fps_(fps),
fd_(-1),
is_started_(false),
buffer_count_(0),
frame_size_(0),
controls_(controls) {
std::memset(buffers_, 0, sizeof(buffers_));
std::memset(buffer_lengths_, 0, sizeof(buffer_lengths_));
device_info_ = probeDeviceInfo(device_);
}
V4L2Capture::~V4L2Capture() {
stop();
if (fd_ >= 0) {
close(fd_);
}
}
bool V4L2Capture::printSupportedModes(const std::string& device) {
int fd = open(device.c_str(), O_RDWR);
if (fd < 0) {
ALOGE("open %s failed: %s", device.c_str(), strerror(errno));
return false;
}
const DeviceInfo deviceInfo = probeDeviceInfo(device);
if (!deviceInfo.serialNumber.empty()) {
ALOGN("SerialNumber: %s", deviceInfo.serialNumber.c_str());
} else {
ALOGN("SerialNumber: (not found)");
}
ALOGN("UVC capabilities for %s", device.c_str());
ALOGN("Filtered to stereo_depth supported resolutions: 2560x720, 3840x1080");
struct v4l2_fmtdesc fmt = {};
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
for (fmt.index = 0; ioctl(fd, VIDIOC_ENUM_FMT, &fmt) == 0; ++fmt.index) {
char fourcc[5] = {static_cast<char>(fmt.pixelformat & 0xFF),
static_cast<char>((fmt.pixelformat >> 8) & 0xFF),
static_cast<char>((fmt.pixelformat >> 16) & 0xFF),
static_cast<char>((fmt.pixelformat >> 24) & 0xFF), '\0'};
bool printedFormatHeader = false;
struct v4l2_frmsizeenum fsize = {};
fsize.pixel_format = fmt.pixelformat;
for (fsize.index = 0; ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &fsize) == 0; ++fsize.index) {
if (fsize.type != V4L2_FRMSIZE_TYPE_DISCRETE) {
continue;
}
const uint32_t width = fsize.discrete.width;
const uint32_t height = fsize.discrete.height;
if (!stereo_depth::isSupportedStereoInputResolution(static_cast<int>(width),
static_cast<int>(height))) {
continue;
}
if (!printedFormatHeader) {
ALOGN("[format] %s - %s", fourcc, reinterpret_cast<const char*>(fmt.description));
printedFormatHeader = true;
}
ALOGN(" [size] %ux%u", width, height);
struct v4l2_frmivalenum fival = {};
fival.pixel_format = fmt.pixelformat;
fival.width = width;
fival.height = height;
bool foundFps = false;
for (fival.index = 0; ioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &fival) == 0;
++fival.index) {
if (fival.type != V4L2_FRMIVAL_TYPE_DISCRETE) {
continue;
}
if (fival.discrete.numerator == 0) {
continue;
}
const double fps = static_cast<double>(fival.discrete.denominator) /
static_cast<double>(fival.discrete.numerator);
ALOGN(" fps: %.3f", fps);
foundFps = true;
}
if (!foundFps) {
ALOGN(" fps: (not reported by driver)");
}
}
}
close(fd);
return true;
}
bool V4L2Capture::printAllControls(const std::string& device) {
int fd = open(device.c_str(), O_RDWR);
if (fd < 0) {
ALOGE("open %s failed: %s", device.c_str(), strerror(errno));
return false;
}
const DeviceInfo deviceInfo = probeDeviceInfo(device);
if (!deviceInfo.serialNumber.empty()) {
ALOGN("SerialNumber: %s", deviceInfo.serialNumber.c_str());
} else {
ALOGN("SerialNumber: (not found)");
}
ALOGN("All V4L2 controls for %s", device.c_str());
struct v4l2_queryctrl query = {};
query.id = V4L2_CTRL_FLAG_NEXT_CTRL;
while (::ioctl(fd, VIDIOC_QUERYCTRL, &query) == 0) {
struct v4l2_control ctrl = {};
ctrl.id = query.id;
const bool gotCurrent = ((query.flags & V4L2_CTRL_FLAG_DISABLED) == 0) &&
(::ioctl(fd, VIDIOC_G_CTRL, &ctrl) == 0);
const bool sampleExposed = isSampleExposedControl(query.id);
const bool adjustableNow = isControlAdjustableNow(query);
ALOGN(
"[control] id=0x%08x name=%s type=%s sample=%s adjustable_now=%s current=%s%d "
"range=[%d,%d] step=%d default=%d flags=%s",
query.id, reinterpret_cast<const char*>(query.name), controlTypeName(query.type),
sampleExposed ? "yes" : "no", adjustableNow ? "yes" : "no", gotCurrent ? "" : "(n/a)",
gotCurrent ? ctrl.value : 0, query.minimum, query.maximum, query.step,
query.default_value, controlFlagsString(query.flags).c_str());
query.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
}
close(fd);
return true;
}
bool V4L2Capture::resetAllControlsToDefault(const std::string& device) {
int fd = open(device.c_str(), O_RDWR);
if (fd < 0) {
ALOGE("open %s failed: %s", device.c_str(), strerror(errno));
return false;
}
const DeviceInfo deviceInfo = probeDeviceInfo(device);
if (!deviceInfo.serialNumber.empty()) {
ALOGN("SerialNumber: %s", deviceInfo.serialNumber.c_str());
} else {
ALOGN("SerialNumber: (not found)");
}
ALOGN("Reset sample-supported UVC controls to driver defaults for %s", device.c_str());
static const std::array<uint32_t, 9> kResetOrder = {
V4L2_CID_AUTO_WHITE_BALANCE,
V4L2_CID_WHITE_BALANCE_TEMPERATURE,
V4L2_CID_POWER_LINE_FREQUENCY,
V4L2_CID_BRIGHTNESS,
V4L2_CID_CONTRAST,
V4L2_CID_SATURATION,
V4L2_CID_GAMMA,
V4L2_CID_SHARPNESS,
V4L2_CID_GAIN,
};
bool success = true;
for (uint32_t id : kResetOrder) {
if (!resetControlToDefault(fd, id)) {
success = false;
}
}
close(fd);
return success;
}
size_t V4L2Capture::initialize() {
if (fd_ >= 0) {
return frame_size_;
}
fd_ = open(device_.c_str(), O_RDWR | O_NONBLOCK);
if (fd_ < 0) {
ALOGE("open %s failed: %s", device_.c_str(), strerror(errno));
return 0;
}
/* ---------- set format ---------- */
struct v4l2_format fmt = {};
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = width_;
fmt.fmt.pix.height = height_;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
fmt.fmt.pix.field = V4L2_FIELD_ANY;
if (ioctl(fd_, VIDIOC_S_FMT, &fmt) < 0) {
ALOGE("VIDIOC_S_FMT failed on %s: %s", device_.c_str(), strerror(errno));
return 0;
}
// Update actual width/height/size from driver
width_ = fmt.fmt.pix.width;
height_ = fmt.fmt.pix.height;
frame_size_ = fmt.fmt.pix.sizeimage;
/* ---------- set fps ---------- */
struct v4l2_streamparm sp = {};
sp.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
sp.parm.capture.timeperframe.numerator = 1;
sp.parm.capture.timeperframe.denominator = fps_;
const int setParmRet = ioctl(fd_, VIDIOC_S_PARM, &sp);
if (setParmRet < 0) {
ALOGW("VIDIOC_S_PARM failed on %s: %s", device_.c_str(), strerror(errno));
}
struct v4l2_streamparm actualSp = {};
actualSp.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl(fd_, VIDIOC_G_PARM, &actualSp) == 0 &&
actualSp.parm.capture.timeperframe.numerator != 0) {
const double negotiatedFps =
static_cast<double>(actualSp.parm.capture.timeperframe.denominator) /
static_cast<double>(actualSp.parm.capture.timeperframe.numerator);
if (negotiatedFps > 0.0) {
fps_ = std::max(1, static_cast<int>(negotiatedFps + 0.5));
}
} else if (setParmRet == 0 && sp.parm.capture.timeperframe.numerator != 0) {
const double negotiatedFps = static_cast<double>(sp.parm.capture.timeperframe.denominator) /
static_cast<double>(sp.parm.capture.timeperframe.numerator);
if (negotiatedFps > 0.0) {
fps_ = std::max(1, static_cast<int>(negotiatedFps + 0.5));
}
}
applyUvcControls(fd_, device_, controls_);
return frame_size_;
}
bool V4L2Capture::start() {
if (is_started_) return true;
if (initialize() == 0) {
return false;
}
/* ---------- request buffers ---------- */
struct v4l2_requestbuffers req = {};
req.count = MAX_BUFS;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
if (ioctl(fd_, VIDIOC_REQBUFS, &req) < 0) {
ALOGE("VIDIOC_REQBUFS failed: %s", strerror(errno));
return false;
}
if (req.count == 0 || req.count > MAX_BUFS) {
ALOGE("unexpected MMAP buffer count %u on %s", req.count, device_.c_str());
return false;
}
buffer_count_ = req.count;
for (uint32_t i = 0; i < buffer_count_; i++) {
struct v4l2_buffer buf = {};
buf.type = req.type;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if (ioctl(fd_, VIDIOC_QUERYBUF, &buf) < 0) {
ALOGE("VIDIOC_QUERYBUF failed for buffer %u: %s", i, strerror(errno));
return false;
}
buffers_[i] =
mmap(nullptr, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, buf.m.offset);
if (buffers_[i] == MAP_FAILED) {
buffers_[i] = nullptr;
ALOGE("mmap failed for buffer %u: %s", i, strerror(errno));
return false;
}
buffer_lengths_[i] = buf.length;
if (ioctl(fd_, VIDIOC_QBUF, &buf) < 0) {
ALOGE("VIDIOC_QBUF failed for buffer %u: %s", i, strerror(errno));
return false;
}
}
enum v4l2_buf_type type = static_cast<enum v4l2_buf_type>(req.type);
if (ioctl(fd_, VIDIOC_STREAMON, &type) < 0) {
ALOGE("VIDIOC_STREAMON failed: %s", strerror(errno));
return false;
}
is_started_ = true;
return true;
}
void V4L2Capture::stop() {
if (!is_started_) return;
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ioctl(fd_, VIDIOC_STREAMOFF, &type);
is_started_ = false;
for (uint32_t i = 0; i < buffer_count_; i++) {
if (buffers_[i] != nullptr) {
munmap(buffers_[i], buffer_lengths_[i]);
buffers_[i] = nullptr;
}
buffer_lengths_[i] = 0;
}
buffer_count_ = 0;
}
bool V4L2Capture::grab(Frame& frame) {
if (!is_started_) return false;
struct v4l2_buffer buf = {};
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
if (ioctl(fd_, VIDIOC_DQBUF, &buf) < 0) {
if (errno == EAGAIN) {
return false;
}
ALOGE("VIDIOC_DQBUF failed: %s", strerror(errno));
return false;
}
if (buf.index >= buffer_count_ || buffers_[buf.index] == nullptr) {
ALOGE("VIDIOC_DQBUF returned invalid buffer index %u", buf.index);
return false;
}
if (buf.bytesused == 0 || buf.bytesused < frame_size_) {
ALOGW("Dropping incomplete UVC frame on %s: bytesused=%u expected=%zu index=%u",
device_.c_str(), buf.bytesused, frame_size_, buf.index);
struct v4l2_buffer requeue = {};
requeue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
requeue.memory = V4L2_MEMORY_MMAP;
requeue.index = buf.index;
if (ioctl(fd_, VIDIOC_QBUF, &requeue) < 0) {
ALOGE("VIDIOC_QBUF failed while dropping incomplete frame %u: %s", buf.index,
strerror(errno));
}
return false;
}
frame.data = buffers_[buf.index];
frame.size = buf.bytesused;
frame.index = buf.index;
frame.timestampNs = static_cast<uint64_t>(buf.timestamp.tv_sec) * 1000000000ULL +
static_cast<uint64_t>(buf.timestamp.tv_usec) * 1000ULL;
return true;
}
void V4L2Capture::release(const Frame& frame) {
if (!is_started_) return;
struct v4l2_buffer buf = {};
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = frame.index;
if (ioctl(fd_, VIDIOC_QBUF, &buf) < 0) {
ALOGE("VIDIOC_QBUF failed for buffer %d: %s", frame.index, strerror(errno));
}
}