Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-videodev/android/PjCameraInfo2.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2021 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 package org.pjsip;
19
20 import java.util.Arrays;
21 import android.hardware.camera2.CameraAccessException;
22 import android.hardware.camera2.CameraCharacteristics;
23 import android.hardware.camera2.CameraManager;
24 import android.hardware.camera2.params.StreamConfigurationMap;
25 import android.util.Log;
26 import android.util.Size;
27 import android.graphics.ImageFormat;
28
29 public class PjCameraInfo2 {
30 public String id;
31 public int facing; // 0: back, 1: front, 2: external
32 public int orient;
33 public int[] supportedSize; // [w1, h1, w2, h2, ...]
34 public int[] supportedFps1000; // [min1, max1, min2, max2, ...]
35 public int[] supportedFormat; // [fmt1, fmt2, ...]
36
37 private static final String TAG = "PjCameraInfo2";
38
39 /* The camera2 is a bit tricky with format, for example it reports
40 * for I420 support (and no NV21 support), however the incoming frame
41 * buffers are actually in NV21 format (e.g: pixel stride is 2).
42 *
43 * For now, we only support I420.
44 */
45 private static final int[] PJ_SUPPORTED_FORMAT = {
46 ImageFormat.YUV_420_888, // PJMEDIA_FORMAT_I420
47 };
48
49 private static boolean is_inited = false;
50 private static PjCameraInfo2[] camInfo = null;
51 private static int camInfoCnt = 0;
52 private static CameraManager cameraManager = null;
53
54 public static void SetCameraManager(CameraManager cm) {
55 cameraManager = cm;
56 }
57
58 public static CameraManager GetCameraManager() {
59 return cameraManager;
60 }
61
62 private static int Refresh() {
63 CameraManager cm = GetCameraManager();
64 if (cm==null) {
65 Log.e(TAG, "Need camera manager instance for enumerating camera");
66 return -1;
67 }
68
69 is_inited = false;
70
71 String[] camIds;
72 try {
73 camIds = cm.getCameraIdList();
74 } catch (CameraAccessException e) {
75 Log.d(TAG, e.getMessage());
76 e.printStackTrace();
77 return -1;
78 }
79
80 camInfo = new PjCameraInfo2[camIds.length];
81 camInfoCnt = 0;
82
83 Log.i(TAG, "Found " + camIds.length + " cameras:");
84 for (int i = 0; i < camIds.length; ++i) {
85 Integer facing;
86 CameraCharacteristics cc;
87 StreamConfigurationMap scm;
88 int[] outFmts;
89
90 /* Query basic info */
91 try {
92 cc = cm.getCameraCharacteristics(camIds[i]);
93 facing = cc.get(CameraCharacteristics.LENS_FACING);
94 scm = cc.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
95 outFmts = scm.getOutputFormats();
96 } catch (Exception e) {
97 Log.d(TAG, e.getMessage());
98 Log.w(TAG, String.format("%3d: id=%s skipped due to failure in querying info",
99 i, camIds[i]));
100 continue;
101 }
102
103 PjCameraInfo2 pjci = new PjCameraInfo2();
104 pjci.id = camIds[i];
105 switch(facing) {
106 case CameraCharacteristics.LENS_FACING_BACK:
107 pjci.facing = 0;
108 break;
109 case CameraCharacteristics.LENS_FACING_FRONT:
110 pjci.facing = 1;
111 break;
112 default:
113 pjci.facing = 2;
114 break;
115 }
116
117 int[] fmts = new int[outFmts.length];
118 int fmtCnt = 0;
119 for (int pjFmt : PJ_SUPPORTED_FORMAT) {
120 for (int outFmt : outFmts) {
121 if (outFmt == pjFmt) {
122 fmts[fmtCnt++] = pjFmt;
123 break;
124 }
125 }
126 }
127 Log.i(TAG, String.format("%3d: id=%s formats=%s",
128 i, pjci.id, Arrays.toString(outFmts)));
129 if (fmtCnt == 0) {
130 Log.w(TAG, String.format("%3d: id=%s skipped due to no PJSIP compatible format",
131 i, pjci.id));
132 continue;
133 }
134
135 pjci.supportedFormat = new int[fmtCnt];
136 System.arraycopy(fmts, 0, pjci.supportedFormat, 0, fmtCnt);
137
138 /* Query additional info */
139 try {
140 pjci.orient = cc.get(CameraCharacteristics.SENSOR_ORIENTATION);
141 } catch (Exception e) {
142 Log.d(TAG, e.getMessage());
143 Log.w(TAG, String.format("%3d: id=%s failed in getting orient",
144 i, camIds[i]));
145 }
146
147 try {
148 Size[] suppSize = scm.getOutputSizes(pjci.supportedFormat[0]);
149 pjci.supportedSize = new int[suppSize.length * 2];
150 for (int j = 0; j < suppSize.length; ++j) {
151 pjci.supportedSize[j*2] = suppSize[j].getWidth();
152 pjci.supportedSize[j*2 + 1] = suppSize[j].getHeight();
153 }
154 } catch (Exception e) {
155 Log.d(TAG, e.getMessage());
156 Log.w(TAG, String.format("%3d: id=%s failed in getting sizes",
157 i, camIds[i]));
158
159 /* Query failed, let's just hardcode the supported sizes */
160 pjci.supportedSize = new int[] {176, 144, 320, 240, 352, 288, 640, 480, 1280, 720, 1920, 1080};
161 }
162
163 /* FPS info is not really used for now, let's just hardcode it */
164 pjci.supportedFps1000 = new int[] { 1000, 30000 };
165
166 camInfo[camInfoCnt++] = pjci;
167
168 Log.i(TAG, String.format("%3d: id=%s facing=%d orient=%d formats=%s sizes=%s",
169 i, pjci.id, pjci.facing, pjci.orient,
170 Arrays.toString(pjci.supportedFormat),
171 Arrays.toString(pjci.supportedSize)));
172 }
173
174 is_inited = true;
175 return 0;
176 }
177
178 public static int GetCameraCount()
179 {
180 /* Always refresh */
181 if (Refresh() != 0) {
182 return -1;
183 }
184 return camInfoCnt;
185 }
186
187 /* Get camera info: facing, orientation, supported size/fps/format. */
188 public static PjCameraInfo2 GetCameraInfo(int idx)
189 {
190 if (!is_inited) {
191 Log.e(TAG, "Not initalized");
192 return null;
193 }
194
195 if (idx < 0 || idx >= camInfoCnt) {
196 Log.e(TAG, "Invalid camera ID");
197 return null;
198 }
199
200 return camInfo[idx];
201 }
202 }