Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: webrtc/api/java/android/org/webrtc/Camera2Enumerator.java

Issue 2013433003: WIP: Android Camera2 capture implementation (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fix CaptureFormat jni parsing Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 package org.webrtc; 11 package org.webrtc;
12 12
13 import android.annotation.TargetApi; 13 import android.annotation.TargetApi;
14 import android.content.Context; 14 import android.content.Context;
15 15
16 import android.graphics.ImageFormat; 16 import android.graphics.ImageFormat;
17 import android.graphics.SurfaceTexture;
18 import android.hardware.camera2.CameraAccessException;
17 import android.hardware.camera2.CameraCharacteristics; 19 import android.hardware.camera2.CameraCharacteristics;
18 import android.hardware.camera2.CameraManager; 20 import android.hardware.camera2.CameraManager;
19 import android.hardware.camera2.params.StreamConfigurationMap; 21 import android.hardware.camera2.params.StreamConfigurationMap;
20 import android.os.Build; 22 import android.os.Build;
21 import android.os.SystemClock; 23 import android.os.SystemClock;
22 import android.util.Range; 24 import android.util.Range;
23 import android.util.Size; 25 import android.util.Size;
24 26
25 import org.webrtc.CameraEnumerationAndroid.CaptureFormat; 27 import org.webrtc.CameraEnumerationAndroid.CaptureFormat;
26 import org.webrtc.Logging; 28 import org.webrtc.Logging;
(...skipping 27 matching lines...) Expand all
54 synchronized (cachedSupportedFormats) { 56 synchronized (cachedSupportedFormats) {
55 if (cachedSupportedFormats.containsKey(cameraId)) { 57 if (cachedSupportedFormats.containsKey(cameraId)) {
56 return cachedSupportedFormats.get(cameraId); 58 return cachedSupportedFormats.get(cameraId);
57 } 59 }
58 Logging.d(TAG, "Get supported formats for camera index " + cameraId + ".") ; 60 Logging.d(TAG, "Get supported formats for camera index " + cameraId + ".") ;
59 final long startTimeMs = SystemClock.elapsedRealtime(); 61 final long startTimeMs = SystemClock.elapsedRealtime();
60 62
61 final CameraCharacteristics cameraCharacteristics; 63 final CameraCharacteristics cameraCharacteristics;
62 try { 64 try {
63 cameraCharacteristics = cameraManager.getCameraCharacteristics(Integer.t oString(cameraId)); 65 cameraCharacteristics = cameraManager.getCameraCharacteristics(Integer.t oString(cameraId));
64 } catch (Exception ex) { 66 } catch (CameraAccessException e) {
65 Logging.e(TAG, "getCameraCharacteristics(): " + ex); 67 Logging.e(TAG, "getCameraCharacteristics(): " + e);
66 return new ArrayList<CaptureFormat>(); 68 return new ArrayList<CaptureFormat>();
67 } 69 }
68 70
69 // Calculate default max fps from auto-exposure ranges in case getOutputMi nFrameDuration() is
70 // not supported.
71 final Range<Integer>[] fpsRanges = 71 final Range<Integer>[] fpsRanges =
72 cameraCharacteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_T ARGET_FPS_RANGES); 72 cameraCharacteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_T ARGET_FPS_RANGES);
73 int defaultMaxFps = 0; 73 int maxFps = 0;
74 for (Range<Integer> fpsRange : fpsRanges) { 74 for (Range<Integer> fpsRange : fpsRanges) {
75 defaultMaxFps = Math.max(defaultMaxFps, fpsRange.getUpper()); 75 maxFps = Math.max(maxFps, fpsRange.getUpper());
76 } 76 }
77 77 if (maxFps < 1000) {
78 // Some LEGACY camera implementations use fps rates that are multiplied with 1000. Make sure
79 // all values are multiplied with 1000 for consistency.
80 maxFps *= 1000;
81 }
78 final StreamConfigurationMap streamMap = 82 final StreamConfigurationMap streamMap =
79 cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGUR ATION_MAP); 83 cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGUR ATION_MAP);
80 final Size[] sizes = streamMap.getOutputSizes(ImageFormat.YUV_420_888); 84 final Size[] sizes = streamMap.getOutputSizes(SurfaceTexture.class);
81 if (sizes == null) { 85 if (sizes == null) {
82 throw new RuntimeException("ImageFormat.YUV_420_888 not supported."); 86 Logging.e(TAG, "No supported camera output sizes.");
87 return new ArrayList<CaptureFormat>();
83 } 88 }
84 89
85 final List<CaptureFormat> formatList = new ArrayList<CaptureFormat>(); 90 final List<CaptureFormat> formatList = new ArrayList<CaptureFormat>();
86 for (Size size : sizes) { 91 for (Size size : sizes) {
87 long minFrameDurationNs = 0; 92 long minFrameDurationNs = 0;
88 try { 93 formatList.add(new CaptureFormat(size.getWidth(), size.getHeight(), 0, m axFps));
89 minFrameDurationNs = streamMap.getOutputMinFrameDuration(ImageFormat.Y UV_420_888, size);
90 } catch (Exception e) {
91 // getOutputMinFrameDuration() is not supported on all devices. Ignore silently.
92 }
93 final int maxFps = (minFrameDurationNs == 0)
94 ? defaultMaxFps
95 : (int) Math.round(NANO_SECONDS_PER_SECOND / minF rameDurationNs);
96 formatList.add(new CaptureFormat(size.getWidth(), size.getHeight(), 0, m axFps * 1000));
97 } 94 }
98 cachedSupportedFormats.put(cameraId, formatList); 95 cachedSupportedFormats.put(cameraId, formatList);
99 final long endTimeMs = SystemClock.elapsedRealtime(); 96 final long endTimeMs = SystemClock.elapsedRealtime();
100 Logging.d(TAG, "Get supported formats for camera index " + cameraId + " do ne." 97 Logging.d(TAG, "Get supported formats for camera index " + cameraId + " do ne."
101 + " Time spent: " + (endTimeMs - startTimeMs) + " ms."); 98 + " Time spent: " + (endTimeMs - startTimeMs) + " ms.");
102 return formatList; 99 return formatList;
103 } 100 }
104 } 101 }
102
103 // Convert from android.util.Size to CaptureFormat.Size.
104 public static List<CaptureFormat.Size> convertSizes(Size[] cameraSizes) {
105 final List<CaptureFormat.Size> sizes = new ArrayList<CaptureFormat.Size>();
106 for (Size size : cameraSizes) {
107 sizes.add(new CaptureFormat.Size(size.getWidth(), size.getHeight()));
108 }
109 return sizes;
110 }
111
112 // Convert from android.util.Range<Integer> to CaptureFormat.FramerateRange.
113 public static List<CaptureFormat.FramerateRange> convertFramerates(
114 Range<Integer>[] arrayRanges, int unitFactor) {
115 final List<CaptureFormat.FramerateRange> ranges = new ArrayList<CaptureForma t.FramerateRange>();
116 for (Range<Integer> range : arrayRanges) {
117 ranges.add(new CaptureFormat.FramerateRange(
118 range.getLower() * unitFactor,
119 range.getUpper() * unitFactor));
120 }
121 return ranges;
122 }
105 } 123 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698