| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 package org.webrtc; | |
| 12 | |
| 13 import android.os.SystemClock; | |
| 14 import java.util.ArrayList; | |
| 15 import java.util.List; | |
| 16 import org.webrtc.CameraEnumerationAndroid.CaptureFormat; | |
| 17 | |
| 18 @SuppressWarnings("deprecation") | |
| 19 public class Camera1Enumerator implements CameraEnumerator { | |
| 20 private final static String TAG = "Camera1Enumerator"; | |
| 21 // Each entry contains the supported formats for corresponding camera index. T
he formats for all | |
| 22 // cameras are enumerated on the first call to getSupportedFormats(), and cach
ed for future | |
| 23 // reference. | |
| 24 private static List<List<CaptureFormat>> cachedSupportedFormats; | |
| 25 | |
| 26 private final boolean captureToTexture; | |
| 27 | |
| 28 public Camera1Enumerator() { | |
| 29 this(true /* captureToTexture */); | |
| 30 } | |
| 31 | |
| 32 public Camera1Enumerator(boolean captureToTexture) { | |
| 33 this.captureToTexture = captureToTexture; | |
| 34 } | |
| 35 | |
| 36 // Returns device names that can be used to create a new VideoCapturerAndroid. | |
| 37 @Override | |
| 38 public String[] getDeviceNames() { | |
| 39 ArrayList<String> namesList = new ArrayList<>(); | |
| 40 for (int i = 0; i < android.hardware.Camera.getNumberOfCameras(); ++i) { | |
| 41 String name = getDeviceName(i); | |
| 42 if (name != null) { | |
| 43 namesList.add(name); | |
| 44 Logging.d(TAG, "Index: " + i + ". " + name); | |
| 45 } else { | |
| 46 Logging.e(TAG, "Index: " + i + ". Failed to query camera name."); | |
| 47 } | |
| 48 } | |
| 49 String[] namesArray = new String[namesList.size()]; | |
| 50 return namesList.toArray(namesArray); | |
| 51 } | |
| 52 | |
| 53 @Override | |
| 54 public boolean isFrontFacing(String deviceName) { | |
| 55 android.hardware.Camera.CameraInfo info = getCameraInfo(getCameraIndex(devic
eName)); | |
| 56 return info != null && info.facing == android.hardware.Camera.CameraInfo.CAM
ERA_FACING_FRONT; | |
| 57 } | |
| 58 | |
| 59 @Override | |
| 60 public boolean isBackFacing(String deviceName) { | |
| 61 android.hardware.Camera.CameraInfo info = getCameraInfo(getCameraIndex(devic
eName)); | |
| 62 return info != null && info.facing == android.hardware.Camera.CameraInfo.CAM
ERA_FACING_BACK; | |
| 63 } | |
| 64 | |
| 65 @Override | |
| 66 public List<CaptureFormat> getSupportedFormats(String deviceName) { | |
| 67 return getSupportedFormats(getCameraIndex(deviceName)); | |
| 68 } | |
| 69 | |
| 70 @Override | |
| 71 public CameraVideoCapturer createCapturer( | |
| 72 String deviceName, CameraVideoCapturer.CameraEventsHandler eventsHandler)
{ | |
| 73 return new Camera1Capturer(deviceName, eventsHandler, captureToTexture); | |
| 74 } | |
| 75 | |
| 76 private static android.hardware.Camera.CameraInfo getCameraInfo(int index) { | |
| 77 android.hardware.Camera.CameraInfo info = new android.hardware.Camera.Camera
Info(); | |
| 78 try { | |
| 79 android.hardware.Camera.getCameraInfo(index, info); | |
| 80 } catch (Exception e) { | |
| 81 Logging.e(TAG, "getCameraInfo failed on index " + index, e); | |
| 82 return null; | |
| 83 } | |
| 84 return info; | |
| 85 } | |
| 86 | |
| 87 static synchronized List<CaptureFormat> getSupportedFormats(int cameraId) { | |
| 88 if (cachedSupportedFormats == null) { | |
| 89 cachedSupportedFormats = new ArrayList<List<CaptureFormat>>(); | |
| 90 for (int i = 0; i < android.hardware.Camera.getNumberOfCameras(); ++i) { | |
| 91 cachedSupportedFormats.add(enumerateFormats(i)); | |
| 92 } | |
| 93 } | |
| 94 return cachedSupportedFormats.get(cameraId); | |
| 95 } | |
| 96 | |
| 97 private static List<CaptureFormat> enumerateFormats(int cameraId) { | |
| 98 Logging.d(TAG, "Get supported formats for camera index " + cameraId + "."); | |
| 99 final long startTimeMs = SystemClock.elapsedRealtime(); | |
| 100 final android.hardware.Camera.Parameters parameters; | |
| 101 android.hardware.Camera camera = null; | |
| 102 try { | |
| 103 Logging.d(TAG, "Opening camera with index " + cameraId); | |
| 104 camera = android.hardware.Camera.open(cameraId); | |
| 105 parameters = camera.getParameters(); | |
| 106 } catch (RuntimeException e) { | |
| 107 Logging.e(TAG, "Open camera failed on camera index " + cameraId, e); | |
| 108 return new ArrayList<CaptureFormat>(); | |
| 109 } finally { | |
| 110 if (camera != null) { | |
| 111 camera.release(); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 final List<CaptureFormat> formatList = new ArrayList<CaptureFormat>(); | |
| 116 try { | |
| 117 int minFps = 0; | |
| 118 int maxFps = 0; | |
| 119 final List<int[]> listFpsRange = parameters.getSupportedPreviewFpsRange(); | |
| 120 if (listFpsRange != null) { | |
| 121 // getSupportedPreviewFpsRange() returns a sorted list. Take the fps ran
ge | |
| 122 // corresponding to the highest fps. | |
| 123 final int[] range = listFpsRange.get(listFpsRange.size() - 1); | |
| 124 minFps = range[android.hardware.Camera.Parameters.PREVIEW_FPS_MIN_INDEX]
; | |
| 125 maxFps = range[android.hardware.Camera.Parameters.PREVIEW_FPS_MAX_INDEX]
; | |
| 126 } | |
| 127 for (android.hardware.Camera.Size size : parameters.getSupportedPreviewSiz
es()) { | |
| 128 formatList.add(new CaptureFormat(size.width, size.height, minFps, maxFps
)); | |
| 129 } | |
| 130 } catch (Exception e) { | |
| 131 Logging.e(TAG, "getSupportedFormats() failed on camera index " + cameraId,
e); | |
| 132 } | |
| 133 | |
| 134 final long endTimeMs = SystemClock.elapsedRealtime(); | |
| 135 Logging.d(TAG, "Get supported formats for camera index " + cameraId + " done
." | |
| 136 + " Time spent: " + (endTimeMs - startTimeMs) + " ms."); | |
| 137 return formatList; | |
| 138 } | |
| 139 | |
| 140 // Convert from android.hardware.Camera.Size to Size. | |
| 141 static List<Size> convertSizes(List<android.hardware.Camera.Size> cameraSizes)
{ | |
| 142 final List<Size> sizes = new ArrayList<Size>(); | |
| 143 for (android.hardware.Camera.Size size : cameraSizes) { | |
| 144 sizes.add(new Size(size.width, size.height)); | |
| 145 } | |
| 146 return sizes; | |
| 147 } | |
| 148 | |
| 149 // Convert from int[2] to CaptureFormat.FramerateRange. | |
| 150 static List<CaptureFormat.FramerateRange> convertFramerates(List<int[]> arrayR
anges) { | |
| 151 final List<CaptureFormat.FramerateRange> ranges = new ArrayList<CaptureForma
t.FramerateRange>(); | |
| 152 for (int[] range : arrayRanges) { | |
| 153 ranges.add(new CaptureFormat.FramerateRange( | |
| 154 range[android.hardware.Camera.Parameters.PREVIEW_FPS_MIN_INDEX], | |
| 155 range[android.hardware.Camera.Parameters.PREVIEW_FPS_MAX_INDEX])); | |
| 156 } | |
| 157 return ranges; | |
| 158 } | |
| 159 | |
| 160 // Returns the camera index for camera with name |deviceName|, or throws Illeg
alArgumentException | |
| 161 // if no such camera can be found. | |
| 162 static int getCameraIndex(String deviceName) { | |
| 163 Logging.d(TAG, "getCameraIndex: " + deviceName); | |
| 164 for (int i = 0; i < android.hardware.Camera.getNumberOfCameras(); ++i) { | |
| 165 if (deviceName.equals(getDeviceName(i))) { | |
| 166 return i; | |
| 167 } | |
| 168 } | |
| 169 throw new IllegalArgumentException("No such camera: " + deviceName); | |
| 170 } | |
| 171 | |
| 172 // Returns the name of the camera with camera index. Returns null if the | |
| 173 // camera can not be used. | |
| 174 static String getDeviceName(int index) { | |
| 175 android.hardware.Camera.CameraInfo info = getCameraInfo(index); | |
| 176 if (info == null) { | |
| 177 return null; | |
| 178 } | |
| 179 | |
| 180 String facing = | |
| 181 (info.facing == android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT)
? "front" : "back"; | |
| 182 return "Camera " + index + ", Facing " + facing + ", Orientation " + info.or
ientation; | |
| 183 } | |
| 184 } | |
| OLD | NEW |