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
format->pixelFormat = QVideoFrameFormat::PixelFormat::Format_YUV420P;
50
51
return
format->create();
52
}
53
54
// Can be called from any thread
55
QList
<
QCameraDevice
>
QAndroidVideoDevices
::
findVideoInputs
()
const
56
{
57
QList
<
QCameraDevice
>
devices
;
58
59
QtJniTypes
::
QtVideoDeviceManager
deviceManager
{
60
QNativeInterface
::
QAndroidApplication
::
context
() };
61
62
if
(!
deviceManager
.
isValid
()) {
63
qCWarning
(
qLCAndroidVideoDevices
) <<
"Failed to connect to Qt Video Device Manager."
;
64
return
devices
;
65
}
66
67
const
QJniArray
cameraIdList
=
deviceManager
.
callMethod
<
QtJniTypes
::
String
[]>(
"getCameraIdList"
);
68
if
(!
cameraIdList
.
isValid
())
69
return
devices
;
70
71
int
cameraIndex
= 0;
72
for
(
const
auto
&
cameraId
:
cameraIdList
) {
73
if
(!
cameraId
.
isValid
())
74
continue
;
75
76
auto
info
=
std
::
make_unique
<
QCameraDevicePrivate
>();
77
info
->
id
=
cameraId
.
toString
().
toUtf8
();
78
79
info
->
orientation
=
deviceManager
.
callMethod
<
jint
>(
"getSensorOrientation"
,
cameraId
);
80
81
// Will be set to -1 if facing can not be determined.
82
const
int
facing
=
deviceManager
.
callMethod
<
jint
>(
"getLensFacing"
,
cameraId
);
83
84
// Values grabbed from Android docs CameraCharacteristics.LENS_FACING
85
constexpr
int
LENS_FACING_FRONT
= 0;
86
constexpr
int
LENS_FACING_BACK
= 1;
87
constexpr
int
LENS_FACING_EXTERNAL
= 2;
88
89
switch
(
facing
) {
90
case
LENS_FACING_EXTERNAL
:
91
info
->
position
=
QCameraDevice
::
Position
::
UnspecifiedPosition
;
92
info
->
description
=
QStringLiteral
(u"External Camera: %1").
arg
(
cameraIndex
);
93
break
;
94
case
LENS_FACING_BACK
:
95
info
->
position
=
QCameraDevice
::
Position
::
BackFace
;
96
info
->
description
=
QStringLiteral
(u"Rear Camera: %1").
arg
(
cameraIndex
);
97
break
;
98
case
LENS_FACING_FRONT
:
99
info
->
position
=
QCameraDevice
::
Position
::
FrontFace
;
100
info
->
description
=
QStringLiteral
(u"Front Camera: %1").
arg
(
cameraIndex
);
101
break
;
102
default
:
103
info
->
position
=
QCameraDevice
::
Position
::
UnspecifiedPosition
;
104
info
->
description
=
QStringLiteral
(u"Camera: %1").
arg
(
cameraIndex
);
105
break
;
106
}
107
++
cameraIndex
;
108
109
const
auto
fpsRanges
=
deviceManager
.
callMethod
<
QStringList
>(
"getFpsRange"
,
cameraId
);
110
111
int
maxFps
= 0,
minFps
= 0;
112
for
(
auto
range
:
fpsRanges
) {
113
range
=
range
.
remove
(u"["_s);
114
range
=
range
.
remove
(u"]"_s);
115
116
const
auto
split
=
range
.
split
(u","_s);
117
118
int
min
=
split
.
at
(0).
toInt
();
119
int
max
=
split
.
at
(1).
toInt
();
120
121
if
(
max
>
maxFps
) {
122
maxFps
=
max
;
123
minFps
=
min
;
124
}
125
}
126
127
const
static
int
yuvFormat
=
QtJniTypes
::
ImageFormat
::
getStaticField
<
jint
>(
"YUV_420_888"
);
128
const
QStringList
videoSizes
=
deviceManager
.
callMethod
<
QStringList
>(
129
"getStreamConfigurationsSizes"
,
cameraId
,
yuvFormat
);
130
131
const
static
int
jpegFormat
=
QtJniTypes
::
ImageFormat
::
getStaticField
<
jint
>(
"JPEG"
);
132
const
QStringList
photoSizes
=
deviceManager
.
callMethod
<
QStringList
>(
133
"getStreamConfigurationsSizes"
,
cameraId
,
jpegFormat
);
134
135
if
(
videoSizes
.
isEmpty
() &&
photoSizes
.
isEmpty
())
136
continue
;
137
138
auto
sizeFromString
= [](
const
QString
&
sizeString
) ->
QSize
{
139
const
auto
split
=
sizeString
.
split
(u"x"_s);
140
int
width
=
split
.
at
(0).
toInt
();
141
int
height
=
split
.
at
(1).
toInt
();
142
return
{
width
,
height
};
143
};
144
145
for
(
const
auto
&
sizeString
:
videoSizes
) {
146
auto
size
=
sizeFromString
(
sizeString
);
147
info
->
videoFormats
.
append
(
148
createCameraFormat
(
size
.
width
(),
size
.
height
(),
minFps
,
maxFps
));
149
}
150
151
for
(
const
auto
&
sizeString
:
photoSizes
)
152
info
->
photoResolutions
.
append
(
sizeFromString
(
sizeString
));
153
154
155
devices
.
push_back
(
info
.
release
()->
create
());
156
}
157
158
return
devices
;
159
}
160
161
}
// namespace QFFmpeg
162
163
// Called from background looper thread in Android
164
static
void
onCameraAvailableNative
(
165
JNIEnv*,
166
jobject
,
167
jlong nativePtr)
168
{
169
using
namespace
QFFmpeg
;
170
auto
*videoDevices =
reinterpret_cast
<QAndroidVideoDevices*>(
static_cast
<size_t>(nativePtr));
171
Q_ASSERT(!videoDevices->thread()->isCurrentThread());
172
videoDevices->onVideoInputsChanged();
173
}
174
Q_DECLARE_JNI_NATIVE_METHOD(onCameraAvailableNative)
175
176
// Called from background looper thread in Android
177
static
void
onCameraUnavailableNative
(
178
JNIEnv*,
179
jobject
,
180
jlong nativePtr)
181
{
182
using
namespace
QFFmpeg
;
183
auto
*videoDevices =
reinterpret_cast
<QAndroidVideoDevices*>(
static_cast
<size_t>(nativePtr));
184
Q_ASSERT(!videoDevices->thread()->isCurrentThread());
185
videoDevices->onVideoInputsChanged();
186
}
187
Q_DECLARE_JNI_NATIVE_METHOD(onCameraUnavailableNative)
188
189
bool
QFFmpeg::QAndroidVideoDevices::registerNativeMethods() {
190
return
QJavaCameraAvailabilityListener::registerNativeMethods({
191
Q_JNI_NATIVE_METHOD(onCameraAvailableNative),
192
Q_JNI_NATIVE_METHOD(onCameraUnavailableNative),
193
});
194
}
195
196
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:177
onCameraAvailableNative
static void onCameraAvailableNative(JNIEnv *, jobject, jlong nativePtr)
Definition
qandroidvideodevices.cpp:164
qtmultimedia
src
plugins
multimedia
ffmpeg
android
qandroidvideodevices.cpp
Generated on
for Qt by
1.18.0