Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.media; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.hardware.camera2.CameraAccessException; | |
| 9 import android.hardware.camera2.CameraCharacteristics; | |
| 10 import android.hardware.camera2.CameraManager; | |
| 11 import android.hardware.camera2.params.StreamConfigurationMap; | |
| 12 import android.util.Log; | |
| 13 import android.util.Size; | |
| 14 | |
| 15 import org.chromium.base.JNINamespace; | |
| 16 | |
| 17 import java.util.ArrayList; | |
| 18 import java.util.Arrays; | |
| 19 | |
| 20 /** | |
| 21 * Static methods to retrieve information on current system cameras and their | |
| 22 * capabilities using the Camera2 API introduced in Android SDK 21 (L Release). | |
| 23 * For this we interact with an android.hardware.camera2.CameraManager. | |
| 24 **/ | |
| 25 @JNINamespace("media") | |
| 26 public class VideoCaptureAndroid2 { | |
| 27 private static final double kNanoSecondsToFps = 1.0E-9; | |
| 28 private static final String TAG = "VideoCaptureAndroid2"; | |
| 29 | |
| 30 // Service function to grab CameraCharacteristics and handle exceptions. | |
| 31 private static CameraCharacteristics getCameraCharacteristics(Context appCon text, int id) { | |
| 32 final CameraManager manager = | |
| 33 (CameraManager) appContext.getSystemService(Context.CAMERA_SERVI CE); | |
| 34 CameraCharacteristics cameraCharacteristics; | |
|
qinmin
2014/10/24 22:53:22
can we just do the following:
try {
return man
mcasas
2014/10/25 17:46:16
Done.
| |
| 35 try { | |
| 36 cameraCharacteristics = manager.getCameraCharacteristics(Integer.toS tring(id)); | |
| 37 } catch (CameraAccessException ex) { | |
| 38 Log.e(TAG, "getNumberOfCameras: getCameraIdList(): " + ex); | |
| 39 return null; | |
| 40 } | |
| 41 return cameraCharacteristics; | |
| 42 } | |
| 43 | |
| 44 static int getNumberOfCameras(Context appContext) { | |
| 45 final CameraManager manager = | |
| 46 (CameraManager) appContext.getSystemService(Context.CAMERA_SERVI CE); | |
| 47 try { | |
| 48 return manager.getCameraIdList().length; | |
| 49 } catch (CameraAccessException ex) { | |
| 50 Log.e(TAG, "getNumberOfCameras: getCameraIdList(): " + ex); | |
| 51 return 0; | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 static String getName(int id, Context appContext) { | |
| 56 final CameraCharacteristics cameraCharacteristics = | |
| 57 getCameraCharacteristics(appContext, id); | |
| 58 if (cameraCharacteristics == null) return null; | |
| 59 int facing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING ); | |
| 60 | |
| 61 return "camera2 " + id + ", facing " | |
| 62 + ((facing == CameraCharacteristics.LENS_FACING_FRONT) ? "front" : "back"); | |
| 63 } | |
| 64 | |
| 65 static VideoCapture.CaptureFormat[] getDeviceSupportedFormats(Context appCon text, int id) { | |
| 66 CameraCharacteristics cameraCharacteristics = getCameraCharacteristics(a ppContext, id); | |
|
qinmin
2014/10/24 22:53:22
final
mcasas
2014/10/25 17:46:15
Done.
| |
| 67 if (cameraCharacteristics == null) return null; | |
| 68 | |
| 69 int[] capabilities = cameraCharacteristics.get( | |
| 70 CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES); | |
| 71 // Per-format frame rate via getOutputMinFrameDuration() is only availab le if the | |
| 72 // property REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR is set. | |
| 73 final boolean minFrameDurationAvailable = Arrays.asList(capabilities).co ntains( | |
| 74 CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENS OR); | |
| 75 | |
| 76 ArrayList<VideoCapture.CaptureFormat> formatList = | |
| 77 new ArrayList<VideoCapture.CaptureFormat>(); | |
| 78 StreamConfigurationMap streamMap = | |
| 79 cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CO NFIGURATION_MAP); | |
| 80 final int[] formats = streamMap.getOutputFormats(); | |
| 81 for (int format : formats) { | |
| 82 final Size[] sizes = streamMap.getOutputSizes(format); | |
| 83 for (Size size : sizes) { | |
| 84 double minFrameRate = 0.0f; | |
| 85 if (minFrameDurationAvailable) { | |
| 86 final long minFrameDuration = | |
| 87 streamMap.getOutputMinFrameDuration(format, size); | |
| 88 minFrameRate = (minFrameDuration == 0) ? 0.0f : | |
| 89 (1.0 / kNanoSecondsToFps * minFrameDuration); | |
| 90 } else { | |
| 91 // TODO(mcasas): find out where to get the info from in this case. | |
| 92 // Hint: perhaps using SCALER_AVAILABLE_PROCESSED_MIN_DURATI ONS. | |
| 93 minFrameRate = 0.0; | |
| 94 } | |
| 95 formatList.add(new VideoCapture.CaptureFormat(size.getWidth(), | |
| 96 size.getHeight(), | |
| 97 (int) minFrameRate , | |
| 98 0)); | |
| 99 } | |
| 100 } | |
| 101 return formatList.toArray(new VideoCapture.CaptureFormat[formatList.size ()]); | |
| 102 } | |
| 103 | |
| 104 VideoCaptureAndroid2() {} | |
| 105 } | |
| OLD | NEW |