Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QtVideoDeviceManager.java
Go to the documentation of this file.
1// Copyright (C) 2022 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
4package org.qtproject.qt.android.multimedia;
5
6import android.annotation.TargetApi;
7import android.content.Context;
8import android.graphics.ImageFormat;
9import android.graphics.Rect;
10import android.hardware.camera2.CameraCharacteristics;
11import android.hardware.camera2.CameraManager;
12import android.hardware.camera2.CaptureRequest;
13import android.hardware.camera2.params.StreamConfigurationMap;
14import android.media.MediaCodecList;
15import android.media.MediaCodecInfo;
16import android.util.Range;
17import android.util.Size;
18import android.util.Log;
19
20import java.lang.String;
21import java.util.ArrayList;
22import java.util.HashSet;
23import java.util.Map;
24import java.util.Set;
25import java.util.WeakHashMap;
26
28
29 CameraManager mCameraManager;
30 Map<String, CameraCharacteristics> cache;
31
32 public QtVideoDeviceManager(Context context) {
33 mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
34 cache = new WeakHashMap<String, CameraCharacteristics>();
35 }
36
37 public CameraCharacteristics getCameraCharacteristics(String cameraId) {
38
39 if (cache.containsKey(cameraId))
40 return cache.get(cameraId);
41
42 try {
43 CameraCharacteristics cameraCharacteristics = mCameraManager.getCameraCharacteristics(cameraId);
44 cache.put(cameraId, cameraCharacteristics);
45 return cameraCharacteristics;
46 } catch (Exception e) {
47 e.printStackTrace();
48 }
49 return null;
50 }
51
52 static private boolean isSoftwareCodec(String longCodecName) {
53 longCodecName = longCodecName.toLowerCase();
54 return longCodecName.startsWith("omx.google.") || longCodecName.startsWith("c2.android.")
55 || !(longCodecName.startsWith("omx.") || longCodecName.startsWith("c2."));
56 }
57
58 private enum CODEC {
60 ENCODER
61 }
62 static private String[] getHWVideoCodecs(CODEC expectedType) {
63 MediaCodecList mediaCodecList = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
64 MediaCodecInfo[] mediaCodecInfo = mediaCodecList.getCodecInfos();
65 Set<String> codecs = new HashSet<String>();
66
67 for (MediaCodecInfo codecInfo : mediaCodecInfo) {
68 CODEC currentType = codecInfo.isEncoder() ? CODEC.ENCODER : CODEC.DECODER;
69 if (currentType == expectedType && !isSoftwareCodec(codecInfo.getName())) {
70 String[] supportedTypes = codecInfo.getSupportedTypes();
71 for (String type : supportedTypes) {
72 if (type.startsWith("video/"))
73 codecs.add(type.substring(6));
74 }
75 }
76 }
77 return codecs.toArray(new String[codecs.size()]);
78 }
79
80 static public String[] getHWVideoDecoders() { return getHWVideoCodecs(CODEC.DECODER); }
81 static public String[] getHWVideoEncoders() { return getHWVideoCodecs(CODEC.ENCODER); }
82
83 public String[] getCameraIdList() {
84 try {
85 return mCameraManager.getCameraIdList();
86 } catch (Exception e) {
87 e.printStackTrace();
88 }
89 return null;
90 }
91
92 public int getSensorOrientation(String cameraId) {
93 CameraCharacteristics characteristics = getCameraCharacteristics(cameraId);
94 if (characteristics == null)
95 return 0;
96 return characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
97 }
98
99 public int getLensFacing(String cameraId) {
100 CameraCharacteristics characteristics = getCameraCharacteristics(cameraId);
101 if (characteristics == null)
102 return 0;
103 return characteristics.get(CameraCharacteristics.LENS_FACING);
104 }
105
106 public String[] getFpsRange(String cameraId) {
107
108 CameraCharacteristics characteristics = getCameraCharacteristics(cameraId);
109 if (characteristics == null)
110 return new String[0];
111
112 Range<Integer>[] ranges = characteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES);
113
114 String[] fps = new String[ranges.length];
115
116 for (int index = 0; index < ranges.length; index++) {
117 fps[index] = ranges[index].toString();
118 }
119
120 return fps;
121 }
122
123 public float getMaxZoom(String cameraId) {
124
125 float maxZoom = 1.0f;
126 final CameraCharacteristics characteristics = getCameraCharacteristics(cameraId);
127 if (characteristics != null)
128 maxZoom = characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM);
129 return maxZoom;
130 }
131
132 public Rect getActiveArraySize(String cameraId) {
133 Rect activeArraySize = new Rect();
134 final CameraCharacteristics characteristics = getCameraCharacteristics(cameraId);
135 if (characteristics != null)
136 activeArraySize = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
137 return activeArraySize;
138 }
139
140 static final int maxResolution = 3840*2160; // 4k resolution
141 public String[] getStreamConfigurationsSizes(String cameraId, int imageFormat) {
142
143 CameraCharacteristics characteristics = getCameraCharacteristics(cameraId);
144 if (characteristics == null)
145 return new String[0];
146
147 StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
148 Size[] sizes = map.getOutputSizes(imageFormat);
149 if (sizes == null)
150 return new String[0];
151
152 ArrayList<String> stream = new ArrayList<>();
153
154 for (int index = 0; index < sizes.length; index++) {
155 if (sizes[index].getWidth() * sizes[index].getHeight() <= maxResolution)
156 stream.add(sizes[index].toString());
157 }
158
159 return stream.toArray(new String[0]);
160 }
161
162 public int stringToControlAEMode(String mode) {
163 switch (mode) {
164 case "off":
165 return CaptureRequest.CONTROL_AE_MODE_ON;
166 case "auto":
167 return CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH;
168 case "on":
169 return CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH;
170 case "redeye":
171 return CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE;
172 case "external":
173 return CaptureRequest.CONTROL_AE_MODE_ON_EXTERNAL_FLASH;
174 default:
175 return -1;
176 }
177 }
178
179 public String controlAEModeToString(int mode) {
180 switch (mode) {
181 case CaptureRequest.CONTROL_AE_MODE_ON:
182 return "off";
183 case CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH:
184 return "auto";
185 case CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH:
186 return "on";
187 case CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE:
188 return "redeye";
189 case CaptureRequest.CONTROL_AE_MODE_ON_EXTERNAL_FLASH:
190 return "external";
191 case CaptureRequest.CONTROL_AE_MODE_OFF:
192 default:
193 return "unknown";
194 }
195 }
196
197 public int[] getSupportedAfModes(String cameraId) {
198
199 CameraCharacteristics characteristics = getCameraCharacteristics(cameraId);
200 if (characteristics == null)
201 return new int[0];
202
203 return characteristics.get(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES);
204 }
205
206 public String[] getSupportedFlashModes(String cameraId) {
207
208 CameraCharacteristics characteristics = getCameraCharacteristics(cameraId);
209 if (characteristics == null)
210 return new String[0];
211
212 int supportedFlashModes[] = characteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_MODES);
213 ArrayList<String> supportedFlashModesList = new ArrayList<String>();
214 for (int index = 0; index < supportedFlashModes.length; index++) {
215 supportedFlashModesList.add(controlAEModeToString(supportedFlashModes[index]));
216 }
217
218 String[] ret = new String[ supportedFlashModesList.size() ];
219 return supportedFlashModesList.toArray(ret);
220 }
221
222 public boolean isTorchModeSupported(String cameraId) {
223 boolean ret = false;
224 final CameraCharacteristics characteristics = getCameraCharacteristics(cameraId);
225 if (characteristics != null)
226 ret = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
227 return ret;
228 }
229}
String[] getStreamConfigurationsSizes(String cameraId, int imageFormat)
CameraCharacteristics getCameraCharacteristics(String cameraId)
QMap< QString, QString > map
[6]
QCache< int, Employee > cache
[0]
static void * context
EGLStreamKHR stream
return ret
GLenum mode
GLuint index
[2]
GLenum type
GLuint GLsizei const GLuint const GLintptr const GLsizeiptr * sizes
char * toString(const MyType &t)
[31]