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.hardware.Camera; |
| 31 import android.os.SystemClock; |
| 32 import android.util.Log; |
| 33 |
| 34 import org.webrtc.CameraEnumerationAndroid.CaptureFormat; |
| 35 |
| 36 import java.util.ArrayList; |
| 37 import java.util.List; |
| 38 |
| 39 @SuppressWarnings("deprecation") |
| 40 public class CameraEnumerator implements CameraEnumerationAndroid.Enumerator { |
| 41 private final static String TAG = "CameraEnumerator"; |
| 42 // Each entry contains the supported formats for corresponding camera index. T
he formats for all |
| 43 // cameras are enumerated on the first call to getSupportedFormats(), and cach
ed for future |
| 44 // reference. |
| 45 private List<List<CaptureFormat>> cachedSupportedFormats; |
| 46 |
| 47 @Override |
| 48 public List<CaptureFormat> getSupportedFormats(int cameraId) { |
| 49 synchronized (this) { |
| 50 if (cachedSupportedFormats == null) { |
| 51 cachedSupportedFormats = new ArrayList<List<CaptureFormat>>(); |
| 52 for (int i = 0; i < CameraEnumerationAndroid.getDeviceCount(); ++i) { |
| 53 cachedSupportedFormats.add(enumerateFormats(i)); |
| 54 } |
| 55 } |
| 56 } |
| 57 return cachedSupportedFormats.get(cameraId); |
| 58 } |
| 59 |
| 60 private List<CaptureFormat> enumerateFormats(int cameraId) { |
| 61 Log.d(TAG, "Get supported formats for camera index " + cameraId + "."); |
| 62 final long startTimeMs = SystemClock.elapsedRealtime(); |
| 63 final Camera.Parameters parameters; |
| 64 Camera camera = null; |
| 65 try { |
| 66 Log.d(TAG, "Opening camera with index " + cameraId); |
| 67 camera = Camera.open(cameraId); |
| 68 parameters = camera.getParameters(); |
| 69 } catch (RuntimeException e) { |
| 70 Log.e(TAG, "Open camera failed on camera index " + cameraId, e); |
| 71 return new ArrayList<CaptureFormat>(); |
| 72 } finally { |
| 73 if (camera != null) { |
| 74 camera.release(); |
| 75 } |
| 76 } |
| 77 |
| 78 final List<CaptureFormat> formatList = new ArrayList<CaptureFormat>(); |
| 79 try { |
| 80 int minFps = 0; |
| 81 int maxFps = 0; |
| 82 final List<int[]> listFpsRange = parameters.getSupportedPreviewFpsRange(); |
| 83 if (listFpsRange != null) { |
| 84 // getSupportedPreviewFpsRange() returns a sorted list. Take the fps ran
ge |
| 85 // corresponding to the highest fps. |
| 86 final int[] range = listFpsRange.get(listFpsRange.size() - 1); |
| 87 minFps = range[Camera.Parameters.PREVIEW_FPS_MIN_INDEX]; |
| 88 maxFps = range[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]; |
| 89 } |
| 90 for (Camera.Size size : parameters.getSupportedPreviewSizes()) { |
| 91 formatList.add(new CaptureFormat(size.width, size.height, minFps, maxFps
)); |
| 92 } |
| 93 } catch (Exception e) { |
| 94 Log.e(TAG, "getSupportedFormats() failed on camera index " + cameraId, e); |
| 95 } |
| 96 |
| 97 final long endTimeMs = SystemClock.elapsedRealtime(); |
| 98 Log.d(TAG, "Get supported formats for camera index " + cameraId + " done." |
| 99 + " Time spent: " + (endTimeMs - startTimeMs) + " ms."); |
| 100 return formatList; |
| 101 } |
| 102 } |
OLD | NEW |