Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qv4l2camera.cpp
Go to the documentation of this file.
1
// Copyright (C) 2021 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4
#
include
"qv4l2camera_p.h"
5
6
#
include
<
QtMultimedia
/
private
/
qcameradevice_p
.
h
>
7
#
include
<
QtMultimedia
/
private
/
qmultimediautils_p
.
h
>
8
#
include
<
QtMultimedia
/
private
/
qv4l2filedescriptor_p
.
h
>
9
#
include
<
QtMultimedia
/
private
/
qv4l2formats_p
.
h
>
10
#
include
<
QtMultimedia
/
private
/
qv4l2memorytransfer_p
.
h
>
11
#
include
<
QtMultimedia
/
private
/
qvideoframe_p
.
h
>
12
#
include
<
QtCore
/
qloggingcategory
.
h
>
13
#
include
<
QtCore
/
qsocketnotifier
.
h
>
14
#
include
<
QtCore
/
private
/
qcore_unix_p
.
h
>
15
16
#
include
<
sys
/
time
.
h
>
17
18
#
include
<
chrono
>
19
#
include
<
system_error
>
20
21
QT_BEGIN_NAMESPACE
22
23
namespace
QtV4L2
{
24
25
using
namespace
Qt
::
Literals
;
26
27
Q_STATIC_LOGGING_CATEGORY
(
qLcV4L2Camera
,
"qt.multimedia.v4l2.camera"
);
28
29
static
std
::chrono::microseconds
toMicroseconds
(
const
timeval &tv)
30
{
31
return
std::chrono::seconds{ tv.tv_sec } + std::chrono::microseconds{ tv.tv_usec };
32
}
33
34
static
QString
v4l2ErrorMessage
(
std
::error_code error)
35
{
36
if
(error == std::errc::device_or_resource_busy)
37
return
u"Camera is in use"_s;
38
if
(error == std::errc::no_such_device)
39
return
u"Camera has been removed"_s;
40
if
(error == std::errc::not_supported)
41
return
u"Unsupported V4L2 camera format"_s;
42
return
u"V4L2 camera error: " + qt_error_string(error.value());
43
}
44
45
QV4L2Camera
::
QV4L2Camera
(
QCamera
*
camera
) :
QPlatformCamera
(
camera
) { }
46
47
QV4L2Camera
::~
QV4L2Camera
()
48
{
49
stopCapturing
();
50
closeV4L2Fd
();
51
}
52
53
bool
QV4L2Camera
::
isActive
()
const
54
{
55
return
m_active
;
56
}
57
58
void
QV4L2Camera
::
setActive
(
bool
active
)
59
{
60
if
(
m_active
==
active
)
61
return
;
62
if
(
m_cameraDevice
.
isNull
() &&
active
)
63
return
;
64
65
if
(
m_cameraFormat
.
isNull
())
66
resolveCameraFormat
({});
67
68
m_active
=
active
;
69
if
(
m_active
) {
70
if
(
auto
result
=
startCapturing
(); !
result
)
71
reportError
(
result
.
error
());
72
73
Q_EMIT
newVideoFrame
({});
74
}
else
{
75
stopCapturing
();
76
emitEndOfStreamFrame
(
m_lastFrameStart
.
value_or
(
std
::
chrono
::
microseconds
{}),
77
m_lastFrameEnd
);
78
}
79
80
Q_EMIT
activeChanged
(
active
);
81
}
82
83
void
QV4L2Camera
::
setCamera
(
const
QCameraDevice
&
camera
)
84
{
85
if
(
m_cameraDevice
==
camera
)
86
return
;
87
88
stopCapturing
();
89
closeV4L2Fd
();
90
91
m_cameraDevice
=
camera
;
92
resolveCameraFormat
({});
93
94
if
(
auto
result
=
initV4L2Controls
(); !
result
) {
95
reportError
(
result
.
error
());
96
return
;
97
}
98
99
if
(
m_active
) {
100
if
(
auto
result
=
startCapturing
(); !
result
)
101
reportError
(
result
.
error
());
102
}
103
}
104
105
bool
QV4L2Camera
::
setCameraFormat
(
const
QCameraFormat
&
format
)
106
{
107
if
(!
format
.
isNull
() && !
m_cameraDevice
.
videoFormats
().
contains
(
format
))
108
return
false
;
109
110
if
(!
resolveCameraFormat
(
format
))
111
return
true
;
112
113
if
(
m_active
) {
114
stopCapturing
();
115
closeV4L2Fd
();
116
117
if
(
auto
result
=
initV4L2Controls
(); !
result
) {
118
reportError
(
result
.
error
());
119
return
true
;
120
}
121
122
if
(
auto
result
=
startCapturing
(); !
result
)
123
reportError
(
result
.
error
());
124
}
125
126
return
true
;
127
}
128
129
bool
QV4L2Camera
::
resolveCameraFormat
(
const
QCameraFormat
&
format
)
130
{
131
auto
fmt
=
format
;
132
if
(
fmt
.
isNull
())
133
fmt
=
findBestCameraFormat
(
m_cameraDevice
);
134
135
if
(
fmt
==
m_cameraFormat
)
136
return
false
;
137
138
m_cameraFormat
=
fmt
;
139
// The device must be reconfigured with the new format before the next stream start.
140
m_negotiatedFormat
.
reset
();
141
return
true
;
142
}
143
144
void
QV4L2Camera
::
setFocusMode
(
QCamera
::
FocusMode
mode
)
145
{
146
if
(
mode
==
focusMode
())
147
return
;
148
149
bool
focusDist
=
supportedFeatures
() &
QCamera
::
Feature
::
FocusDistance
;
150
if
(!
focusDist
&& !
m_v4l2Info
.
rangedFocus
)
151
return
;
152
153
switch
(
mode
) {
154
default
:
155
case
QCamera
::
FocusModeAuto
:
156
setV4L2Parameter
(V4L2_CID_FOCUS_AUTO, 1);
157
if
(
m_v4l2Info
.
rangedFocus
)
158
setV4L2Parameter
(V4L2_CID_AUTO_FOCUS_RANGE,
V4L2_AUTO_FOCUS_RANGE_AUTO
);
159
break
;
160
case
QCamera
::
FocusModeAutoNear
:
161
setV4L2Parameter
(V4L2_CID_FOCUS_AUTO, 1);
162
if
(
m_v4l2Info
.
rangedFocus
)
163
setV4L2Parameter
(V4L2_CID_AUTO_FOCUS_RANGE,
V4L2_AUTO_FOCUS_RANGE_MACRO
);
164
else
if
(
focusDist
)
165
setV4L2Parameter
(V4L2_CID_FOCUS_ABSOLUTE,
m_v4l2Info
.
minFocus
);
166
break
;
167
case
QCamera
::
FocusModeAutoFar
:
168
setV4L2Parameter
(V4L2_CID_FOCUS_AUTO, 1);
169
if
(
m_v4l2Info
.
rangedFocus
)
170
setV4L2Parameter
(V4L2_CID_AUTO_FOCUS_RANGE,
V4L2_AUTO_FOCUS_RANGE_INFINITY
);
171
break
;
172
case
QCamera
::
FocusModeInfinity
:
173
setV4L2Parameter
(V4L2_CID_FOCUS_AUTO, 0);
174
setV4L2Parameter
(V4L2_CID_FOCUS_ABSOLUTE,
m_v4l2Info
.
maxFocus
);
175
break
;
176
case
QCamera
::
FocusModeManual
:
177
setV4L2Parameter
(V4L2_CID_FOCUS_AUTO, 0);
178
setFocusDistance
(
focusDistance
());
179
break
;
180
}
181
focusModeChanged
(
mode
);
182
}
183
184
void
QV4L2Camera
::
setFocusDistance
(
float
d
)
185
{
186
int
distance
=
m_v4l2Info
.
minFocus
+
int
((
m_v4l2Info
.
maxFocus
-
m_v4l2Info
.
minFocus
) *
d
);
187
setV4L2Parameter
(V4L2_CID_FOCUS_ABSOLUTE,
distance
);
188
focusDistanceChanged
(
d
);
189
}
190
191
void
QV4L2Camera
::
zoomTo
(
float
factor
,
float
)
192
{
193
if
(
m_v4l2Info
.
maxZoom
==
m_v4l2Info
.
minZoom
)
194
return
;
195
factor
=
qBound
(1.,
factor
, 2.);
196
int
zoom
=
m_v4l2Info
.
minZoom
+ (
factor
- 1.) * (
m_v4l2Info
.
maxZoom
-
m_v4l2Info
.
minZoom
);
197
setV4L2Parameter
(V4L2_CID_ZOOM_ABSOLUTE,
zoom
);
198
zoomFactorChanged
(
factor
);
199
}
200
201
bool
QV4L2Camera
::
isFocusModeSupported
(
QCamera
::
FocusMode
mode
)
const
202
{
203
if
(
supportedFeatures
() &
QCamera
::
Feature
::
FocusDistance
204
&& (
mode
==
QCamera
::
FocusModeManual
||
mode
==
QCamera
::
FocusModeAutoNear
205
||
mode
==
QCamera
::
FocusModeInfinity
))
206
return
true
;
207
208
return
mode
==
QCamera
::
FocusModeAuto
;
209
}
210
211
void
QV4L2Camera
::
setFlashMode
(
QCamera
::
FlashMode
mode
)
212
{
213
if
(!
m_v4l2Info
.
flashSupported
||
mode
==
QCamera
::
FlashOn
)
214
return
;
215
setV4L2Parameter
(V4L2_CID_FLASH_LED_MODE,
216
mode
==
QCamera
::
FlashAuto
?
V4L2_FLASH_LED_MODE_FLASH
217
:
V4L2_FLASH_LED_MODE_NONE
);
218
flashModeChanged
(
mode
);
219
}
220
221
bool
QV4L2Camera
::
isFlashModeSupported
(
QCamera
::
FlashMode
mode
)
const
222
{
223
if
(
m_v4l2Info
.
flashSupported
&&
mode
==
QCamera
::
FlashAuto
)
224
return
true
;
225
return
mode
==
QCamera
::
FlashOff
;
226
}
227
228
bool
QV4L2Camera
::
isFlashReady
()
const
229
{
230
return
getV4L2Parameter
(V4L2_CID_FLASH_READY).
value_or
(0) != 0;
231
}
232
233
void
QV4L2Camera
::
setTorchMode
(
QCamera
::
TorchMode
mode
)
234
{
235
if
(!
m_v4l2Info
.
torchSupported
||
mode
==
QCamera
::
TorchAuto
)
236
return
;
237
setV4L2Parameter
(V4L2_CID_FLASH_LED_MODE,
238
mode
==
QCamera
::
TorchOn
?
V4L2_FLASH_LED_MODE_TORCH
239
:
V4L2_FLASH_LED_MODE_NONE
);
240
torchModeChanged
(
mode
);
241
}
242
243
bool
QV4L2Camera
::
isTorchModeSupported
(
QCamera
::
TorchMode
mode
)
const
244
{
245
if
(
mode
==
QCamera
::
TorchOn
)
246
return
m_v4l2Info
.
torchSupported
;
247
return
mode
==
QCamera
::
TorchOff
;
248
}
249
250
void
QV4L2Camera
::
setExposureMode
(
QCamera
::
ExposureMode
mode
)
251
{
252
if
(
m_v4l2Info
.
autoExposureSupported
&&
m_v4l2Info
.
manualExposureSupported
) {
253
if
(
mode
!=
QCamera
::
ExposureAuto
&&
mode
!=
QCamera
::
ExposureManual
)
254
return
;
255
int
value
=
mode
==
QCamera
::
ExposureAuto
?
V4L2_EXPOSURE_AUTO
:
V4L2_EXPOSURE_MANUAL
;
256
setV4L2Parameter
(V4L2_CID_EXPOSURE_AUTO,
value
);
257
exposureModeChanged
(
mode
);
258
return
;
259
}
260
}
261
262
bool
QV4L2Camera
::
isExposureModeSupported
(
QCamera
::
ExposureMode
mode
)
const
263
{
264
if
(
mode
==
QCamera
::
ExposureAuto
)
265
return
true
;
266
if
(
m_v4l2Info
.
manualExposureSupported
&&
m_v4l2Info
.
autoExposureSupported
)
267
return
mode
==
QCamera
::
ExposureManual
;
268
return
false
;
269
}
270
271
void
QV4L2Camera
::
setExposureCompensation
(
float
compensation
)
272
{
273
if
((
m_v4l2Info
.
minExposureAdjustment
!= 0 ||
m_v4l2Info
.
maxExposureAdjustment
!= 0)) {
274
int
value
=
qBound
(
m_v4l2Info
.
minExposureAdjustment
, (
int
)(
compensation
* 1000),
275
m_v4l2Info
.
maxExposureAdjustment
);
276
setV4L2Parameter
(V4L2_CID_AUTO_EXPOSURE_BIAS,
value
);
277
exposureCompensationChanged
(
value
/ 1000.);
278
return
;
279
}
280
}
281
282
void
QV4L2Camera
::
setManualIsoSensitivity
(
int
iso
)
283
{
284
if
(!(
supportedFeatures
() &
QCamera
::
Feature
::
IsoSensitivity
))
285
return
;
286
setV4L2Parameter
(V4L2_CID_ISO_SENSITIVITY_AUTO,
287
iso
<= 0 ?
V4L2_ISO_SENSITIVITY_AUTO
:
V4L2_ISO_SENSITIVITY_MANUAL
);
288
if
(
iso
> 0) {
289
iso
=
qBound
(
minIso
(),
iso
,
maxIso
());
290
setV4L2Parameter
(V4L2_CID_ISO_SENSITIVITY,
iso
);
291
}
292
}
293
294
int
QV4L2Camera
::
isoSensitivity
()
const
295
{
296
if
(!(
supportedFeatures
() &
QCamera
::
Feature
::
IsoSensitivity
))
297
return
-1;
298
return
getV4L2Parameter
(V4L2_CID_ISO_SENSITIVITY).
value_or
(-1);
299
}
300
301
void
QV4L2Camera
::
setManualExposureTime
(
float
secs
)
302
{
303
if
(
m_v4l2Info
.
manualExposureSupported
&&
m_v4l2Info
.
autoExposureSupported
) {
304
int
exposure
=
305
qBound
(
m_v4l2Info
.
minExposure
,
qRound
(
secs
* 10000.),
m_v4l2Info
.
maxExposure
);
306
setV4L2Parameter
(V4L2_CID_EXPOSURE_ABSOLUTE,
exposure
);
307
exposureTimeChanged
(
exposure
/ 10000.);
308
return
;
309
}
310
}
311
312
float
QV4L2Camera
::
exposureTime
()
const
313
{
314
return
getV4L2Parameter
(V4L2_CID_EXPOSURE_ABSOLUTE).
value_or
(-10000) / 10000.;
315
}
316
317
bool
QV4L2Camera
::
isWhiteBalanceModeSupported
(
QCamera
::
WhiteBalanceMode
mode
)
const
318
{
319
if
(
m_v4l2Info
.
autoWhiteBalanceSupported
&&
m_v4l2Info
.
colorTemperatureSupported
)
320
return
true
;
321
322
return
mode
==
QCamera
::
WhiteBalanceAuto
;
323
}
324
325
void
QV4L2Camera
::
setWhiteBalanceMode
(
QCamera
::
WhiteBalanceMode
mode
)
326
{
327
Q_ASSERT
(
isWhiteBalanceModeSupported
(
mode
));
328
329
int
temperature
=
colorTemperatureForWhiteBalance
(
mode
);
330
int
t
=
setV4L2ColorTemperature
(
temperature
);
331
if
(
t
== 0)
332
mode
=
QCamera
::
WhiteBalanceAuto
;
333
whiteBalanceModeChanged
(
mode
);
334
}
335
336
void
QV4L2Camera
::
setColorTemperature
(
int
temperature
)
337
{
338
if
(
temperature
== 0) {
339
setWhiteBalanceMode
(
QCamera
::
WhiteBalanceAuto
);
340
return
;
341
}
342
343
Q_ASSERT
(
isWhiteBalanceModeSupported
(
QCamera
::
WhiteBalanceManual
));
344
345
int
t
=
setV4L2ColorTemperature
(
temperature
);
346
if
(
t
)
347
colorTemperatureChanged
(
t
);
348
}
349
350
void
QV4L2Camera
::
readFrame
()
351
{
352
Q_ASSERT
(
m_memoryTransfer
);
353
354
auto
dequeued
=
m_memoryTransfer
->
dequeueFrame
();
355
if
(!
dequeued
) {
356
if
(
dequeued
.
error
() ==
std
::
errc
::
no_such_device
) {
357
// camera got removed while being active
358
stopCapturing
();
359
closeV4L2Fd
();
360
}
else
if
(
dequeued
.
error
() !=
std
::
errc
::
resource_unavailable_try_again
) {
361
qCWarning
(
qLcV4L2Camera
)
362
<<
"Cannot take buffer:"
<<
qt_error_string
(
dequeued
.
error
().
value
());
363
}
364
365
return
;
366
}
367
368
Q_ASSERT
(
m_negotiatedFormat
);
369
370
QVideoFrame
frame
=
371
QVideoFramePrivate
::
createFrame
(
std
::
move
(
dequeued
->
videoBuffer
),
frameFormat
());
372
373
const
auto
timestamp
=
toMicroseconds
(
dequeued
->
timestamp
);
374
if
(!
m_firstFrameTime
)
375
m_firstFrameTime
=
timestamp
;
376
const
auto
frameTime
=
timestamp
- *
m_firstFrameTime
;
377
m_lastFrameStart
=
frameTime
;
378
m_lastFrameEnd
=
frameTime
+
m_negotiatedFormat
->
frameDuration
;
379
frame
.
setStartTime
(
frameTime
.
count
());
380
frame
.
setEndTime
(
m_lastFrameEnd
->
count
());
381
382
Q_EMIT
newVideoFrame
(
frame
);
383
}
384
385
void
QV4L2Camera
::
reportError
(
std
::
error_code
error
)
386
{
387
qCWarning
(
qLcV4L2Camera
) <<
"V4L2 camera error:"
<<
qt_error_string
(
error
.
value
());
388
updateError
(
QCamera
::
CameraError
,
v4l2ErrorMessage
(
error
));
389
}
390
391
std
::
optional
<
v4l2_queryctrl
>
QV4L2Camera
::
query
(
uint32_t
id
)
const
392
{
393
auto
result
=
m_v4l2FileDescriptor
->
queryControl
(
id
);
394
if
(!
result
) {
395
qCWarning
(
qLcV4L2Camera
) <<
"Unable to query the V4L2 parameter"
<<
Qt
::
hex
<<
id
396
<<
result
.
error
();
397
return
std
::
nullopt
;
398
}
399
return
*
result
;
400
}
401
402
q23
::
expected
<
void
,
std
::
error_code
>
QV4L2Camera
::
initV4L2Controls
()
403
{
404
m_v4l2Info
= {};
405
supportedFeaturesChanged
({});
406
407
QCamera
::
Features
features
;
408
409
const
QByteArray
deviceName
=
m_cameraDevice
.
id
();
410
Q_ASSERT
(!
deviceName
.
isEmpty
());
411
412
closeV4L2Fd
();
413
414
const
int
descriptor
=
qt_safe_open
(
deviceName
.
constData
(),
O_RDWR
);
415
if
(
descriptor
== -1) {
416
const
auto
error
=
std
::
error_code
{ errno,
std
::
system_category
() };
417
qCWarning
(
qLcV4L2Camera
) <<
"Unable to open the camera"
<<
deviceName
418
<<
"for read to query the parameter info:"
419
<<
qt_error_string
(
error
.
value
());
420
return
q23
::
unexpected
(
error
);
421
}
422
423
m_v4l2FileDescriptor
=
424
std
::
make_shared
<
QV4L2FileDescriptor
>(
QUniqueFileDescriptorHandle
{
descriptor
});
425
426
qCDebug
(
qLcV4L2Camera
) <<
"FD="
<<
descriptor
;
427
428
if
(
query
(V4L2_CID_AUTO_WHITE_BALANCE)) {
429
m_v4l2Info
.
autoWhiteBalanceSupported
=
true
;
430
setV4L2Parameter
(V4L2_CID_AUTO_WHITE_BALANCE,
true
);
431
}
432
433
if
(
const
auto
control
=
query
(V4L2_CID_WHITE_BALANCE_TEMPERATURE)) {
434
m_v4l2Info
.
minColorTemp
=
control
->
minimum
;
435
m_v4l2Info
.
maxColorTemp
=
control
->
maximum
;
436
m_v4l2Info
.
colorTemperatureSupported
=
true
;
437
features
|=
QCamera
::
Feature
::
ColorTemperature
;
438
}
439
440
if
(
query
(V4L2_CID_EXPOSURE_AUTO))
441
m_v4l2Info
.
autoExposureSupported
=
true
;
442
443
if
(
const
auto
control
=
query
(V4L2_CID_EXPOSURE_ABSOLUTE)) {
444
m_v4l2Info
.
manualExposureSupported
=
true
;
445
m_v4l2Info
.
minExposure
=
control
->
minimum
;
446
m_v4l2Info
.
maxExposure
=
control
->
maximum
;
447
features
|=
QCamera
::
Feature
::
ManualExposureTime
;
448
}
449
450
if
(
const
auto
control
=
query
(V4L2_CID_AUTO_EXPOSURE_BIAS)) {
451
m_v4l2Info
.
minExposureAdjustment
=
control
->
minimum
;
452
m_v4l2Info
.
maxExposureAdjustment
=
control
->
maximum
;
453
features
|=
QCamera
::
Feature
::
ExposureCompensation
;
454
}
455
456
if
(
query
(V4L2_CID_ISO_SENSITIVITY_AUTO)) {
457
if
(
const
auto
control
=
query
(V4L2_CID_ISO_SENSITIVITY)) {
458
features
|=
QCamera
::
Feature
::
IsoSensitivity
;
459
minIsoChanged
(
control
->
minimum
);
460
maxIsoChanged
(
control
->
maximum
);
461
}
462
}
463
464
if
(
const
auto
control
=
query
(V4L2_CID_FOCUS_ABSOLUTE)) {
465
m_v4l2Info
.
minFocus
=
control
->
minimum
;
466
m_v4l2Info
.
maxFocus
=
control
->
maximum
;
467
features
|=
QCamera
::
Feature
::
FocusDistance
;
468
}
469
470
if
(
query
(V4L2_CID_AUTO_FOCUS_RANGE))
471
m_v4l2Info
.
rangedFocus
=
true
;
472
473
if
(
const
auto
control
=
query
(V4L2_CID_FLASH_LED_MODE)) {
474
m_v4l2Info
.
flashSupported
=
control
->
minimum
<=
V4L2_FLASH_LED_MODE_FLASH
475
&&
control
->
maximum
>=
V4L2_FLASH_LED_MODE_FLASH
;
476
m_v4l2Info
.
torchSupported
=
control
->
minimum
<=
V4L2_FLASH_LED_MODE_TORCH
477
&&
control
->
maximum
>=
V4L2_FLASH_LED_MODE_TORCH
;
478
}
479
480
if
(
const
auto
control
=
query
(V4L2_CID_ZOOM_ABSOLUTE)) {
481
m_v4l2Info
.
minZoom
=
control
->
minimum
;
482
m_v4l2Info
.
maxZoom
=
control
->
maximum
;
483
}
484
// zoom factors are in arbitrary units, so we simply normalize them to go from 1 to 2
485
// if they are different
486
minimumZoomFactorChanged
(1);
487
maximumZoomFactorChanged
(
m_v4l2Info
.
minZoom
!=
m_v4l2Info
.
maxZoom
? 2 : 1);
488
489
supportedFeaturesChanged
(
features
);
490
return
{};
491
}
492
493
void
QV4L2Camera
::
closeV4L2Fd
()
494
{
495
Q_ASSERT
(!
m_memoryTransfer
);
496
497
m_v4l2Info
= {};
498
m_negotiatedFormat
.
reset
();
499
m_v4l2FileDescriptor
=
nullptr
;
500
}
501
502
int
QV4L2Camera
::
setV4L2ColorTemperature
(
int
temperature
)
503
{
504
if
(
m_v4l2Info
.
autoWhiteBalanceSupported
) {
505
setV4L2Parameter
(V4L2_CID_AUTO_WHITE_BALANCE,
temperature
== 0 ?
true
:
false
);
506
}
else
if
(
temperature
== 0) {
507
temperature
= 5600;
508
}
509
510
if
(
temperature
!= 0 &&
m_v4l2Info
.
colorTemperatureSupported
) {
511
temperature
=
qBound
(
m_v4l2Info
.
minColorTemp
,
temperature
,
m_v4l2Info
.
maxColorTemp
);
512
if
(!
setV4L2Parameter
(
513
V4L2_CID_WHITE_BALANCE_TEMPERATURE,
514
qBound
(
m_v4l2Info
.
minColorTemp
,
temperature
,
m_v4l2Info
.
maxColorTemp
)))
515
temperature
= 0;
516
}
else
{
517
temperature
= 0;
518
}
519
520
return
temperature
;
521
}
522
523
q23
::
expected
<
void
,
std
::
error_code
>
QV4L2Camera
::
setV4L2Parameter
(
quint32
id
,
qint32
value
)
524
{
525
v4l2_control
control
{
id
,
value
};
526
auto
result
=
m_v4l2FileDescriptor
->
call
(VIDIOC_S_CTRL, &
control
);
527
if
(!
result
) {
528
qCWarning
(
qLcV4L2Camera
) <<
"Unable to set the V4L2 parameter"
<<
Qt
::
hex
<<
id
<<
"to"
529
<<
value
<<
result
.
error
();
530
return
q23
::
unexpected
(
result
.
error
());
531
}
532
return
{};
533
}
534
535
std
::
optional
<
qint32
>
QV4L2Camera
::
getV4L2Parameter
(
quint32
id
)
const
536
{
537
v4l2_control
control
{
id
, 0 };
538
auto
result
=
m_v4l2FileDescriptor
->
call
(VIDIOC_G_CTRL, &
control
);
539
if
(!
result
) {
540
qCWarning
(
qLcV4L2Camera
) <<
"Unable to get the V4L2 parameter"
<<
Qt
::
hex
<<
id
541
<<
result
.
error
();
542
return
std
::
nullopt
;
543
}
544
return
control
.
value
;
545
}
546
547
q23
::
expected
<
void
,
std
::
error_code
>
QV4L2Camera
::
setV4L2CameraFormat
()
548
{
549
if
(
m_negotiatedFormat
)
550
return
{};
551
552
Q_ASSERT
(
m_v4l2FileDescriptor
);
553
554
Q_ASSERT
(!
m_cameraFormat
.
isNull
());
555
qCDebug
(
qLcV4L2Camera
) <<
"XXXXX"
<<
this
<<
m_cameraDevice
.
id
() <<
m_cameraFormat
.
pixelFormat
()
556
<<
m_cameraFormat
.
resolution
();
557
558
const
auto
v4l2Format
=
v4l2FormatForPixelFormat
(
m_cameraFormat
.
pixelFormat
());
559
if
(!
v4l2Format
) {
560
qCWarning
(
qLcV4L2Camera
) <<
"Unsupported camera pixel format"
561
<<
m_cameraFormat
.
pixelFormat
();
562
return
q23
::
unexpected
(
std
::
make_error_code
(
std
::
errc
::
not_supported
));
563
}
564
565
v4l2_format
fmt
= {};
566
fmt
.
type
=
V4L2_BUF_TYPE_VIDEO_CAPTURE
;
567
568
auto
size
=
m_cameraFormat
.
resolution
();
569
fmt
.
fmt
.
pix
.
width
=
size
.
width
();
570
fmt
.
fmt
.
pix
.
height
=
size
.
height
();
571
fmt
.
fmt
.
pix
.
pixelformat
= *
v4l2Format
;
572
fmt
.
fmt
.
pix
.
field
=
V4L2_FIELD_ANY
;
573
574
qCDebug
(
qLcV4L2Camera
) <<
"setting camera format to"
<<
size
<<
fmt
.
fmt
.
pix
.
pixelformat
;
575
576
auto
setFormatResult
=
m_v4l2FileDescriptor
->
call
(VIDIOC_S_FMT, &
fmt
);
577
if
(!
setFormatResult
) {
578
qCWarning
(
qLcV4L2Camera
) <<
"Couldn't set video format on v4l2 camera"
579
<<
setFormatResult
.
error
();
580
return
q23
::
unexpected
(
setFormatResult
.
error
());
581
}
582
583
// The driver may have adjusted the requested format, so keep what it negotiated.
584
const
auto
pixFormat
=
fmt
.
fmt
.
pix
;
585
586
const
auto
negotiatedPixelFormat
=
formatForV4L2Format
(
pixFormat
.
pixelformat
);
587
const
bool
isCompressed
=
negotiatedPixelFormat
==
QVideoFrameFormat
::
Format_Jpeg
;
588
589
if
(!
negotiatedPixelFormat
||
pixFormat
.
sizeimage
== 0
590
|| (
pixFormat
.
bytesperline
== 0 && !
isCompressed
)) {
591
qCWarning
(
qLcV4L2Camera
) <<
"Driver negotiated an unusable camera format"
592
<<
pixFormat
.
pixelformat
;
593
return
q23
::
unexpected
(
std
::
make_error_code
(
std
::
errc
::
not_supported
));
594
}
595
596
v4l2_streamparm
streamParam
= {};
597
streamParam
.
type
=
V4L2_BUF_TYPE_VIDEO_CAPTURE
;
598
599
streamParam
.
parm
.
capture
.
capability
= V4L2_CAP_TIMEPERFRAME;
600
qreal
frameDuration
= 1. /
m_cameraFormat
.
maxFrameRate
();
601
auto
[
num
,
den
] =
qRealToFraction
(
frameDuration
);
602
streamParam
.
parm
.
capture
.
timeperframe
= { (
uint
)
num
, (
uint
)
den
};
603
if
(
auto
result
=
m_v4l2FileDescriptor
->
call
(VIDIOC_S_PARM, &
streamParam
); !
result
)
604
qCWarning
(
qLcV4L2Camera
) <<
"Couldn't set camera frame rate:"
<<
result
.
error
();
605
606
using
namespace
std
::
chrono
;
607
m_negotiatedFormat
=
NegotiatedFormat
{
608
pixFormat
,
609
duration_cast
<
microseconds
>(
duration
<
qreal
>{
frameDuration
}),
610
};
611
return
{};
612
}
613
614
q23
::
expected
<
void
,
std
::
error_code
>
615
QV4L2Camera
::
initV4L2MemoryTransfer
(
const
v4l2_pix_format
&
format
)
616
{
617
Q_ASSERT
(!
m_memoryTransfer
);
618
619
// QT_V4L2_CAMERA_MEMORY=dmabuf|userptr|mmap selects one strategy, with no fallback.
620
const
QByteArray
forcedStrategy
=
qgetenv
(
"QT_V4L2_CAMERA_MEMORY"
);
621
const
auto
isAllowed
= [&](
QByteArrayView
strategy
) {
622
return
forcedStrategy
.
isEmpty
() ||
forcedStrategy
==
strategy
;
623
};
624
625
std
::
error_code
lastError
;
626
627
#
if
QT_CONFIG
(
linux_dmabuf
)
628
if
(
isAllowed
(
"dmabuf"
)) {
629
auto
transfer
=
makeDmaBufMemoryTransfer
(
m_v4l2FileDescriptor
,
format
);
630
if
(
transfer
) {
631
m_memoryTransfer
=
std
::
move
(*
transfer
);
632
return
{};
633
}
634
lastError
=
transfer
.
error
();
635
}
636
#
endif
637
638
if
(
isAllowed
(
"userptr"
)) {
639
auto
transfer
=
makeUserPtrMemoryTransfer
(
m_v4l2FileDescriptor
,
format
);
640
if
(
transfer
) {
641
m_memoryTransfer
=
std
::
move
(*
transfer
);
642
return
{};
643
}
644
lastError
=
transfer
.
error
();
645
if
(
lastError
==
std
::
errc
::
device_or_resource_busy
)
646
return
q23
::
unexpected
(
lastError
);
647
}
648
649
if
(
isAllowed
(
"mmap"
)) {
650
qCDebug
(
qLcV4L2Camera
) <<
"Trying V4L2_MEMORY_MMAP"
;
651
652
auto
transfer
=
makeMMapMemoryTransfer
(
m_v4l2FileDescriptor
,
format
);
653
if
(
transfer
) {
654
m_memoryTransfer
=
std
::
move
(*
transfer
);
655
return
{};
656
}
657
lastError
=
transfer
.
error
();
658
}
659
660
qCWarning
(
qLcV4L2Camera
) <<
"Cannot init V4L2 memory transfer:"
661
<<
qt_error_string
(
lastError
.
value
());
662
return
q23
::
unexpected
(
lastError
);
663
}
664
665
void
QV4L2Camera
::
stopCapturing
()
666
{
667
if
(!
m_memoryTransfer
|| !
m_v4l2FileDescriptor
)
668
return
;
669
670
m_notifier
=
nullptr
;
671
672
if
(
auto
result
=
m_v4l2FileDescriptor
->
stopStream
(); !
result
) {
673
// TODO: handle the case carefully to avoid possible memory corruption
674
if
(
result
.
error
() !=
std
::
errc
::
no_such_device
)
675
qCWarning
(
qLcV4L2Camera
) <<
"failed to stop capture:"
<<
result
.
error
();
676
}
677
678
m_memoryTransfer
=
nullptr
;
679
}
680
681
void
QV4L2Camera
::
resetCaptureState
()
682
{
683
m_notifier
=
nullptr
;
684
m_memoryTransfer
=
nullptr
;
685
m_negotiatedFormat
.
reset
();
686
m_firstFrameTime
.
reset
();
687
m_lastFrameStart
.
reset
();
688
m_lastFrameEnd
.
reset
();
689
}
690
691
q23
::
expected
<
void
,
std
::
error_code
>
QV4L2Camera
::
startCapturing
()
692
{
693
if
(!
m_v4l2FileDescriptor
)
694
return
q23
::
unexpected
(
std
::
make_error_code
(
std
::
errc
::
bad_file_descriptor
));
695
696
if
(
auto
result
=
setV4L2CameraFormat
(); !
result
) {
697
resetCaptureState
();
698
return
q23
::
unexpected
(
result
.
error
());
699
}
700
701
Q_ASSERT
(
m_negotiatedFormat
);
702
703
if
(
auto
result
=
initV4L2MemoryTransfer
(
m_negotiatedFormat
->
pixFormat
); !
result
) {
704
resetCaptureState
();
705
return
q23
::
unexpected
(
result
.
error
());
706
}
707
708
if
(
auto
result
=
m_v4l2FileDescriptor
->
startStream
(); !
result
) {
709
qCWarning
(
qLcV4L2Camera
) <<
"Couldn't start v4l2 camera stream:"
<<
result
.
error
();
710
resetCaptureState
();
711
return
q23
::
unexpected
(
result
.
error
());
712
}
713
714
m_notifier
=
715
std
::
make_unique
<
QSocketNotifier
>(
m_v4l2FileDescriptor
->
get
(),
QSocketNotifier
::
Read
);
716
connect
(
m_notifier
.
get
(), &
QSocketNotifier
::
activated
,
m_notifier
.
get
(), [
this
] {
717
this
->
readFrame
();
718
});
719
720
m_firstFrameTime
.
reset
();
721
m_lastFrameStart
.
reset
();
722
m_lastFrameEnd
.
reset
();
723
return
{};
724
}
725
726
QVideoFrameFormat
QV4L2Camera
::
frameFormat
()
const
727
{
728
auto
result
=
QPlatformCamera
::
frameFormat
();
729
if
(!
m_negotiatedFormat
)
730
return
result
;
731
732
const
auto
&
pixFormat
=
m_negotiatedFormat
->
pixFormat
;
733
734
result
=
QVideoFrameFormat
(
sizeForV4L2Format
(
pixFormat
),
735
formatForV4L2Format
(
pixFormat
.
pixelformat
));
736
result
.
setStreamFrameRate
(
m_cameraFormat
.
maxFrameRate
());
737
738
result
.
setColorSpace
(
colorSpaceForV4L2Colorspace
(
v4l2_colorspace
(
pixFormat
.
colorspace
)));
739
return
result
;
740
}
741
742
}
// namespace QtV4L2
743
744
QT_END_NAMESPACE
745
746
#
include
"moc_qv4l2camera_p.cpp"
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
QtV4L2
Definition
qv4l2camera.cpp:23
QtV4L2::v4l2ErrorMessage
static QString v4l2ErrorMessage(std::error_code error)
Definition
qv4l2camera.cpp:34
QtV4L2::toMicroseconds
static std::chrono::microseconds toMicroseconds(const timeval &tv)
Definition
qv4l2camera.cpp:29
QtV4L2::Q_STATIC_LOGGING_CATEGORY
Q_STATIC_LOGGING_CATEGORY(qLcV4L2Camera, "qt.multimedia.v4l2.camera")
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qtmultimedia
src
multimedia
v4l2
qv4l2camera.cpp
Generated on
for Qt by
1.18.0