Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
18QT_BEGIN_NAMESPACE
19
20using namespace Qt::StringLiterals;
21
22namespace QFFmpeg {
23
24Q_STATIC_LOGGING_CATEGORY(qLCAndroidVideoDevices, "qt.multimedia.ffmpeg.android.videoDevices");
25
26// Can be called from any thread
34
39
40QCameraFormat 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
61{
63
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
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) {
97 info->description = QStringLiteral(u"External Camera: %1").arg(cameraIndex);
98 break;
101 info->description = QStringLiteral(u"Rear Camera: %1").arg(cameraIndex);
102 break;
105 info->description = QStringLiteral(u"Front Camera: %1").arg(cameraIndex);
106 break;
107 default:
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");
134 "getStreamConfigurationsSizes", cameraId, yuvFormat);
135
136 const static int jpegFormat = QtJniTypes::ImageFormat::getStaticField<jint>("JPEG");
138 "getStreamConfigurationsSizes", cameraId, jpegFormat);
139
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) {
154 }
155
156 for (const auto &sizeString : photoSizes)
158
159
161 }
162
163 return devices;
164}
165
166} // namespace QFFmpeg
167
168// Called from background looper thread in Android
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}
179Q_DECLARE_JNI_NATIVE_METHOD(onCameraAvailableNative)
180
181// Called from background looper thread in Android
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}
192Q_DECLARE_JNI_NATIVE_METHOD(onCameraUnavailableNative)
193
194bool QFFmpeg::QAndroidVideoDevices::registerNativeMethods() {
195 return QJavaCameraAvailabilityListener::registerNativeMethods({
196 Q_JNI_NATIVE_METHOD(onCameraAvailableNative),
197 Q_JNI_NATIVE_METHOD(onCameraUnavailableNative),
198 });
199}
200
201QT_END_NAMESPACE
Q_STATIC_LOGGING_CATEGORY(qLCAndroidCaCertificates, "qt.multimedia.ffmpeg.android.caCertificates")
QCameraFormat createCameraFormat(int width, int height, int fpsMin, int fpsMax)
static void onCameraUnavailableNative(JNIEnv *, jobject, jlong nativePtr)
static void onCameraAvailableNative(JNIEnv *, jobject, jlong nativePtr)