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
qandroidvideodevices.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
<
QtFFmpegMediaPluginImpl
/
private
/
qandroidvideodevices_p
.
h
>
5
6
#
include
<
QtCore
/
private
/
qandroidextras_p
.
h
>
7
#
include
<
QtCore
/
qcoreapplication
.
h
>
8
#
include
<
QtCore
/
qcoreapplication_platform
.
h
>
9
#
include
<
QtCore
/
qjnienvironment
.
h
>
10
#
include
<
QtCore
/
qloggingcategory
.
h
>
11
12
#
include
<
QtFFmpegMediaPluginImpl
/
private
/
qandroidvideoframebuffer_p
.
h
>
13
14
#
include
<
QtMultimedia
/
private
/
qcameradevice_p
.
h
>
15
16
#
include
<
jni
.
h
>
17
18
QT_BEGIN_NAMESPACE
19
20
using
namespace
Qt::StringLiterals;
21
22
namespace
QFFmpeg
{
23
24
Q_STATIC_LOGGING_CATEGORY
(
qLCAndroidVideoDevices
,
"qt.multimedia.ffmpeg.android.videoDevices"
);
25
26
// Can be called from any thread
27
QAndroidVideoDevices
::
QAndroidVideoDevices
(
QPlatformMediaIntegration
*
integration
)
28
:
QPlatformVideoDevices
(
integration
)
29
{
30
m_javaCameraAvailabilityListener
=
QJavaCameraAvailabilityListener
(
31
QtAndroidPrivate
::
activity
(),
32
static_cast
<
jlong
>(
reinterpret_cast
<
size_t
>(
this
)));
33
}
34
35
QAndroidVideoDevices
::~
QAndroidVideoDevices
()
36
{
37
m_javaCameraAvailabilityListener
.
callMethod
<
void
>(
"cleanup"
);
38
}
39
40
QCameraFormat
createCameraFormat
(
int
width,
int
height,
int
fpsMin,
int
fpsMax)
41
{
42
QCameraFormatPrivate *format =
new
QCameraFormatPrivate();
43
44
format->resolution = { width, height };
45
46
format->minFrameRate = fpsMin;
47
format->maxFrameRate = fpsMax;
48
49
// Limitation: In Camera2 API, formats are enumerated for YUV_420_888. This is a flexible
50
// format whose memory layout is chosen by the camera HAL and is only known once the frame
51
// is actually delivered. Depending on the device, frames arrive as YUV420P, NV12 or NV21,
52
// and Camera2 has no API to query the layout ahead of time. We advertise YUV420P, but
53
// frames may use one of the other layouts.
54
format->pixelFormat = QVideoFrameFormat::PixelFormat::Format_YUV420P;
55
56
return
format->create();
57
}
58
59
// Can be called from any thread
60
QList
<
QCameraDevice
>
QAndroidVideoDevices
::
findVideoInputs
()
const
61
{
62
QList
<
QCameraDevice
>
devices
;
63
64
QtJniTypes
::
QtVideoDeviceManager
deviceManager
{
65
QNativeInterface
::
QAndroidApplication
::
context
() };
66
67
if
(!
deviceManager
.
isValid
()) {
68
qCWarning
(
qLCAndroidVideoDevices
) <<
"Failed to connect to Qt Video Device Manager."
;
69
return
devices
;
70
}
71
72
const
QJniArray
cameraIdList
=
deviceManager
.
callMethod
<
QtJniTypes
::
String
[]>(
"getCameraIdList"
);
73
if
(!
cameraIdList
.
isValid
())
74
return
devices
;
75
76
int
cameraIndex
= 0;
77
for
(
const
auto
&
cameraId
:
cameraIdList
) {
78
if
(!
cameraId
.
isValid
())
79
continue
;
80
81
auto
info
=
std
::
make_unique
<
QCameraDevicePrivate
>();
82
info
->
id
=
cameraId
.
toString
().
toUtf8
();
83
84
info
->
orientation
=
deviceManager
.
callMethod
<
jint
>(
"getSensorOrientation"
,
cameraId
);
85
86
// Will be set to -1 if facing can not be determined.
87
const
int
facing
=
deviceManager
.
callMethod
<
jint
>(
"getLensFacing"
,
cameraId
);
88
89
// Values grabbed from Android docs CameraCharacteristics.LENS_FACING
90
constexpr
int
LENS_FACING_FRONT
= 0;
91
constexpr
int
LENS_FACING_BACK
= 1;
92
constexpr
int
LENS_FACING_EXTERNAL
= 2;
93
94
switch
(
facing
) {
95
case
LENS_FACING_EXTERNAL
:
96
info
->
position
=
QCameraDevice
::
Position
::
UnspecifiedPosition
;
97
info
->
description
=
QStringLiteral
(u"External Camera: %1").
arg
(
cameraIndex
);
98
break
;
99
case
LENS_FACING_BACK
:
100
info
->
position
=
QCameraDevice
::
Position
::
BackFace
;
101
info
->
description
=
QStringLiteral
(u"Rear Camera: %1").
arg
(
cameraIndex
);
102
break
;
103
case
LENS_FACING_FRONT
:
104
info
->
position
=
QCameraDevice
::
Position
::
FrontFace
;
105
info
->
description
=
QStringLiteral
(u"Front Camera: %1").
arg
(
cameraIndex
);
106
break
;
107
default
:
108
info
->
position
=
QCameraDevice
::
Position
::
UnspecifiedPosition
;
109
info
->
description
=
QStringLiteral
(u"Camera: %1").
arg
(
cameraIndex
);
110
break
;
111
}
112
++
cameraIndex
;
113
114
const
auto
fpsRanges
=
deviceManager
.
callMethod
<
QStringList
>(
"getFpsRange"
,
cameraId
);
115
116
int
maxFps
= 0,
minFps
= 0;
117
for
(
auto
range
:
fpsRanges
) {
118
range
=
range
.
remove
(u"["_s);
119
range
=
range
.
remove
(u"]"_s);
120
121
const
auto
split
=
range
.
split
(u","_s);
122
123
int
min
=
split
.
at
(0).
toInt
();
124
int
max
=
split
.
at
(1).
toInt
();
125
126
if
(
max
>
maxFps
) {
127
maxFps
=
max
;
128
minFps
=
min
;
129
}
130
}
131
132
const
static
int
yuvFormat
=
QtJniTypes
::
ImageFormat
::
getStaticField
<
jint
>(
"YUV_420_888"
);
133
const
QStringList
videoSizes
=
deviceManager
.
callMethod
<
QStringList
>(
134
"getStreamConfigurationsSizes"
,
cameraId
,
yuvFormat
);
135
136
const
static
int
jpegFormat
=
QtJniTypes
::
ImageFormat
::
getStaticField
<
jint
>(
"JPEG"
);
137
const
QStringList
photoSizes
=
deviceManager
.
callMethod
<
QStringList
>(
138
"getStreamConfigurationsSizes"
,
cameraId
,
jpegFormat
);
139
140
if
(
videoSizes
.
isEmpty
() &&
photoSizes
.
isEmpty
())
141
continue
;
142
143
auto
sizeFromString
= [](
const
QString
&
sizeString
) ->
QSize
{
144
const
auto
split
=
sizeString
.
split
(u"x"_s);
145
int
width
=
split
.
at
(0).
toInt
();
146
int
height
=
split
.
at
(1).
toInt
();
147
return
{
width
,
height
};
148
};
149
150
for
(
const
auto
&
sizeString
:
videoSizes
) {
151
auto
size
=
sizeFromString
(
sizeString
);
152
info
->
videoFormats
.
append
(
153
createCameraFormat
(
size
.
width
(),
size
.
height
(),
minFps
,
maxFps
));
154
}
155
156
for
(
const
auto
&
sizeString
:
photoSizes
)
157
info
->
photoResolutions
.
append
(
sizeFromString
(
sizeString
));
158
159
160
devices
.
push_back
(
info
.
release
()->
create
());
161
}
162
163
return
devices
;
164
}
165
166
}
// namespace QFFmpeg
167
168
// Called from background looper thread in Android
169
static
void
onCameraAvailableNative
(
170
JNIEnv*,
171
jobject
,
172
jlong nativePtr)
173
{
174
using
namespace
QFFmpeg
;
175
auto
*videoDevices =
reinterpret_cast
<QAndroidVideoDevices*>(
static_cast
<size_t>(nativePtr));
176
Q_ASSERT(!videoDevices->thread()->isCurrentThread());
177
videoDevices->onVideoInputsChanged();
178
}
179
Q_DECLARE_JNI_NATIVE_METHOD(onCameraAvailableNative)
180
181
// Called from background looper thread in Android
182
static
void
onCameraUnavailableNative
(
183
JNIEnv*,
184
jobject
,
185
jlong nativePtr)
186
{
187
using
namespace
QFFmpeg
;
188
auto
*videoDevices =
reinterpret_cast
<QAndroidVideoDevices*>(
static_cast
<size_t>(nativePtr));
189
Q_ASSERT(!videoDevices->thread()->isCurrentThread());
190
videoDevices->onVideoInputsChanged();
191
}
192
Q_DECLARE_JNI_NATIVE_METHOD(onCameraUnavailableNative)
193
194
bool
QFFmpeg::QAndroidVideoDevices::registerNativeMethods() {
195
return
QJavaCameraAvailabilityListener::registerNativeMethods({
196
Q_JNI_NATIVE_METHOD(onCameraAvailableNative),
197
Q_JNI_NATIVE_METHOD(onCameraUnavailableNative),
198
});
199
}
200
201
QT_END_NAMESPACE
QFFmpeg
Definition
qandroidcacertificates.cpp:26
QFFmpeg::Q_STATIC_LOGGING_CATEGORY
Q_STATIC_LOGGING_CATEGORY(qLCAndroidCaCertificates, "qt.multimedia.ffmpeg.android.caCertificates")
QFFmpeg::createCameraFormat
QCameraFormat createCameraFormat(int width, int height, int fpsMin, int fpsMax)
Definition
qandroidvideodevices.cpp:40
onCameraUnavailableNative
static void onCameraUnavailableNative(JNIEnv *, jobject, jlong nativePtr)
Definition
qandroidvideodevices.cpp:182
onCameraAvailableNative
static void onCameraAvailableNative(JNIEnv *, jobject, jlong nativePtr)
Definition
qandroidvideodevices.cpp:169
qtmultimedia
src
plugins
multimedia
ffmpeg
android
qandroidvideodevices.cpp
Generated on
for Qt by
1.18.0