Chromium Code Reviews| Index: media/base/android/java/src/org/chromium/media/VideoCaptureAndroid2.java |
| diff --git a/media/base/android/java/src/org/chromium/media/VideoCaptureAndroid2.java b/media/base/android/java/src/org/chromium/media/VideoCaptureAndroid2.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..28cc0a09a297ac4d6954443b5ca2a72aeb6f0cfc |
| --- /dev/null |
| +++ b/media/base/android/java/src/org/chromium/media/VideoCaptureAndroid2.java |
| @@ -0,0 +1,105 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.media; |
| + |
| +import android.content.Context; |
| +import android.hardware.camera2.CameraAccessException; |
| +import android.hardware.camera2.CameraCharacteristics; |
| +import android.hardware.camera2.CameraManager; |
| +import android.hardware.camera2.params.StreamConfigurationMap; |
| +import android.util.Log; |
| +import android.util.Size; |
| + |
| +import org.chromium.base.JNINamespace; |
| + |
| +import java.util.ArrayList; |
| +import java.util.Arrays; |
| + |
| +/** |
| + * Static methods to retrieve information on current system cameras and their |
| + * capabilities using the Camera2 API introduced in Android SDK 21 (L Release). |
| + * For this we interact with an android.hardware.camera2.CameraManager. |
| + **/ |
| +@JNINamespace("media") |
| +public class VideoCaptureAndroid2 { |
| + private static final double kNanoSecondsToFps = 1.0E-9; |
| + private static final String TAG = "VideoCaptureAndroid2"; |
| + |
| + // Service function to grab CameraCharacteristics and handle exceptions. |
| + private static CameraCharacteristics getCameraCharacteristics(Context appContext, int id) { |
| + final CameraManager manager = |
| + (CameraManager) appContext.getSystemService(Context.CAMERA_SERVICE); |
| + 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.
|
| + try { |
| + cameraCharacteristics = manager.getCameraCharacteristics(Integer.toString(id)); |
| + } catch (CameraAccessException ex) { |
| + Log.e(TAG, "getNumberOfCameras: getCameraIdList(): " + ex); |
| + return null; |
| + } |
| + return cameraCharacteristics; |
| + } |
| + |
| + static int getNumberOfCameras(Context appContext) { |
| + final CameraManager manager = |
| + (CameraManager) appContext.getSystemService(Context.CAMERA_SERVICE); |
| + try { |
| + return manager.getCameraIdList().length; |
| + } catch (CameraAccessException ex) { |
| + Log.e(TAG, "getNumberOfCameras: getCameraIdList(): " + ex); |
| + return 0; |
| + } |
| + } |
| + |
| + static String getName(int id, Context appContext) { |
| + final CameraCharacteristics cameraCharacteristics = |
| + getCameraCharacteristics(appContext, id); |
| + if (cameraCharacteristics == null) return null; |
| + int facing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING); |
| + |
| + return "camera2 " + id + ", facing " |
| + + ((facing == CameraCharacteristics.LENS_FACING_FRONT) ? "front" : "back"); |
| + } |
| + |
| + static VideoCapture.CaptureFormat[] getDeviceSupportedFormats(Context appContext, int id) { |
| + CameraCharacteristics cameraCharacteristics = getCameraCharacteristics(appContext, id); |
|
qinmin
2014/10/24 22:53:22
final
mcasas
2014/10/25 17:46:15
Done.
|
| + if (cameraCharacteristics == null) return null; |
| + |
| + int[] capabilities = cameraCharacteristics.get( |
| + CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES); |
| + // Per-format frame rate via getOutputMinFrameDuration() is only available if the |
| + // property REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR is set. |
| + final boolean minFrameDurationAvailable = Arrays.asList(capabilities).contains( |
| + CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR); |
| + |
| + ArrayList<VideoCapture.CaptureFormat> formatList = |
| + new ArrayList<VideoCapture.CaptureFormat>(); |
| + StreamConfigurationMap streamMap = |
| + cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); |
| + final int[] formats = streamMap.getOutputFormats(); |
| + for (int format : formats) { |
| + final Size[] sizes = streamMap.getOutputSizes(format); |
| + for (Size size : sizes) { |
| + double minFrameRate = 0.0f; |
| + if (minFrameDurationAvailable) { |
| + final long minFrameDuration = |
| + streamMap.getOutputMinFrameDuration(format, size); |
| + minFrameRate = (minFrameDuration == 0) ? 0.0f : |
| + (1.0 / kNanoSecondsToFps * minFrameDuration); |
| + } else { |
| + // TODO(mcasas): find out where to get the info from in this case. |
| + // Hint: perhaps using SCALER_AVAILABLE_PROCESSED_MIN_DURATIONS. |
| + minFrameRate = 0.0; |
| + } |
| + formatList.add(new VideoCapture.CaptureFormat(size.getWidth(), |
| + size.getHeight(), |
| + (int) minFrameRate, |
| + 0)); |
| + } |
| + } |
| + return formatList.toArray(new VideoCapture.CaptureFormat[formatList.size()]); |
| + } |
| + |
| + VideoCaptureAndroid2() {} |
| +} |