OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * libjingle |
| 3 * Copyright 2015 Google Inc. |
| 4 * |
| 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are met: |
| 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright notice, |
| 9 * this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 * this list of conditions and the following disclaimer in the documentation |
| 12 * and/or other materials provided with the distribution. |
| 13 * 3. The name of the author may not be used to endorse or promote products |
| 14 * derived from this software without specific prior written permission. |
| 15 * |
| 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 */ |
| 27 |
| 28 package org.webrtc; |
| 29 |
| 30 import android.content.Context; |
| 31 import android.graphics.ImageFormat; |
| 32 import android.hardware.camera2.CameraCharacteristics; |
| 33 import android.hardware.camera2.CameraManager; |
| 34 import android.hardware.camera2.params.StreamConfigurationMap; |
| 35 import android.os.Build; |
| 36 import android.os.SystemClock; |
| 37 import android.util.Log; |
| 38 import android.util.Range; |
| 39 import android.util.Size; |
| 40 |
| 41 import org.webrtc.CameraEnumerationAndroid.CaptureFormat; |
| 42 |
| 43 import java.util.ArrayList; |
| 44 import java.util.HashMap; |
| 45 import java.util.List; |
| 46 import java.util.Map; |
| 47 |
| 48 public class Camera2Enumerator implements CameraEnumerationAndroid.Enumerator { |
| 49 private final static String TAG = "Camera2Enumerator"; |
| 50 private final static double NANO_SECONDS_PER_SECOND = 1.0e9; |
| 51 |
| 52 private final CameraManager cameraManager; |
| 53 // Each entry contains the supported formats for a given camera index. The for
mats are enumerated |
| 54 // lazily in getSupportedFormats(), and cached for future reference. |
| 55 private final Map<Integer, List<CaptureFormat>> cachedSupportedFormats = |
| 56 new HashMap<Integer, List<CaptureFormat>>(); |
| 57 |
| 58 public static boolean isSupported() { |
| 59 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; |
| 60 } |
| 61 |
| 62 public Camera2Enumerator(Context context) { |
| 63 cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERV
ICE); |
| 64 } |
| 65 |
| 66 @Override |
| 67 public List<CaptureFormat> getSupportedFormats(int cameraId) { |
| 68 synchronized (cachedSupportedFormats) { |
| 69 if (cachedSupportedFormats.containsKey(cameraId)) { |
| 70 return cachedSupportedFormats.get(cameraId); |
| 71 } |
| 72 Log.d(TAG, "Get supported formats for camera index " + cameraId + "."); |
| 73 final long startTimeMs = SystemClock.elapsedRealtime(); |
| 74 |
| 75 final CameraCharacteristics cameraCharacteristics; |
| 76 try { |
| 77 cameraCharacteristics = cameraManager.getCameraCharacteristics(Integer.t
oString(cameraId)); |
| 78 } catch (Exception ex) { |
| 79 Log.e(TAG, "getCameraCharacteristics(): " + ex); |
| 80 return new ArrayList<CaptureFormat>(); |
| 81 } |
| 82 |
| 83 // Calculate default max fps from auto-exposure ranges in case getOutputMi
nFrameDuration() is |
| 84 // not supported. |
| 85 final Range<Integer>[] fpsRanges = |
| 86 cameraCharacteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_T
ARGET_FPS_RANGES); |
| 87 int defaultMaxFps = 0; |
| 88 for (Range<Integer> fpsRange : fpsRanges) { |
| 89 defaultMaxFps = Math.max(defaultMaxFps, fpsRange.getUpper()); |
| 90 } |
| 91 |
| 92 final StreamConfigurationMap streamMap = |
| 93 cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGUR
ATION_MAP); |
| 94 final Size[] sizes = streamMap.getOutputSizes(ImageFormat.YUV_420_888); |
| 95 if (sizes == null) { |
| 96 throw new RuntimeException("ImageFormat.YUV_420_888 not supported."); |
| 97 } |
| 98 |
| 99 final List<CaptureFormat> formatList = new ArrayList<CaptureFormat>(); |
| 100 for (Size size : sizes) { |
| 101 long minFrameDurationNs = 0; |
| 102 try { |
| 103 minFrameDurationNs = streamMap.getOutputMinFrameDuration(ImageFormat.Y
UV_420_888, size); |
| 104 } catch (Exception e) { |
| 105 // getOutputMinFrameDuration() is not supported on all devices. Ignore
silently. |
| 106 } |
| 107 final int maxFps = (minFrameDurationNs == 0) |
| 108 ? defaultMaxFps |
| 109 : (int) Math.round(NANO_SECONDS_PER_SECOND / minF
rameDurationNs); |
| 110 formatList.add(new CaptureFormat(size.getWidth(), size.getHeight(), 0, m
axFps * 1000)); |
| 111 } |
| 112 cachedSupportedFormats.put(cameraId, formatList); |
| 113 final long endTimeMs = SystemClock.elapsedRealtime(); |
| 114 Log.d(TAG, "Get supported formats for camera index " + cameraId + " done." |
| 115 + " Time spent: " + (endTimeMs - startTimeMs) + " ms."); |
| 116 return formatList; |
| 117 } |
| 118 } |
| 119 } |
OLD | NEW |