| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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; |
| 32 |
public int orient; |
| 33 |
public int[] supportedSize; |
| 34 |
public int[] supportedFps1000; |
| 35 |
public int[] supportedFormat; |
| 36 |
|
| 37 |
private static final String TAG = "PjCameraInfo2"; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private static final int[] PJ_SUPPORTED_FORMAT = { |
| 46 |
ImageFormat.YUV_420_888, |
| 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 |
|
| 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 |
|
| 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 |
|
| 160 |
pjci.supportedSize = new int[] {176, 144, 320, 240, 352, 288, 640, 480, 1280, 720, 1920, 1080}; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 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 |
|
| 181 |
if (Refresh() != 0) { |
| 182 |
return -1; |
| 183 |
} |
| 184 |
return camInfoCnt; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 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 |
} |