| OLD | NEW |
| 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 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 CameraEnumerationAndroid.enumerator = enumerator; | 36 CameraEnumerationAndroid.enumerator = enumerator; |
| 37 } | 37 } |
| 38 | 38 |
| 39 public static synchronized List<CaptureFormat> getSupportedFormats(int cameraI
d) { | 39 public static synchronized List<CaptureFormat> getSupportedFormats(int cameraI
d) { |
| 40 final List<CaptureFormat> formats = enumerator.getSupportedFormats(cameraId)
; | 40 final List<CaptureFormat> formats = enumerator.getSupportedFormats(cameraId)
; |
| 41 Logging.d(TAG, "Supported formats for camera " + cameraId + ": " + formats); | 41 Logging.d(TAG, "Supported formats for camera " + cameraId + ": " + formats); |
| 42 return formats; | 42 return formats; |
| 43 } | 43 } |
| 44 | 44 |
| 45 public static class CaptureFormat { | 45 public static class CaptureFormat { |
| 46 // Class to represent a framerate range. The framerate varies because of lig
htning conditions. | |
| 47 // The values are multiplied by 1000, so 1000 represents one frame per secon
d. | |
| 48 public static class FramerateRange { | |
| 49 public int min; | |
| 50 public int max; | |
| 51 | |
| 52 public FramerateRange(int min, int max) { | |
| 53 this.min = min; | |
| 54 this.max = max; | |
| 55 } | |
| 56 | |
| 57 @Override | |
| 58 public String toString() { | |
| 59 return "[" + (min / 1000.0f) + ":" + (max / 1000.0f) + "]"; | |
| 60 } | |
| 61 | |
| 62 @Override | |
| 63 public boolean equals(Object other) { | |
| 64 if (!(other instanceof FramerateRange)) { | |
| 65 return false; | |
| 66 } | |
| 67 final FramerateRange otherFramerate = (FramerateRange) other; | |
| 68 return min == otherFramerate.min && max == otherFramerate.max; | |
| 69 } | |
| 70 | |
| 71 @Override | |
| 72 public int hashCode() { | |
| 73 // Use prime close to 2^16 to avoid collisions for normal values less th
an 2^16. | |
| 74 return 1 + 65537 * min + max; | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 public final int width; | 46 public final int width; |
| 79 public final int height; | 47 public final int height; |
| 80 public final FramerateRange framerate; | 48 public final int maxFramerate; |
| 49 public final int minFramerate; |
| 81 // TODO(hbos): If VideoCapturer.startCapture is updated to support other ima
ge formats then this | 50 // TODO(hbos): If VideoCapturer.startCapture is updated to support other ima
ge formats then this |
| 82 // needs to be updated and VideoCapturer.getSupportedFormats need to return
CaptureFormats of | 51 // needs to be updated and VideoCapturer.getSupportedFormats need to return
CaptureFormats of |
| 83 // all imageFormats. | 52 // all imageFormats. |
| 84 public final int imageFormat = ImageFormat.NV21; | 53 public final int imageFormat = ImageFormat.NV21; |
| 85 | 54 |
| 86 public CaptureFormat(int width, int height, int minFramerate, int maxFramera
te) { | 55 public CaptureFormat(int width, int height, int minFramerate, |
| 56 int maxFramerate) { |
| 87 this.width = width; | 57 this.width = width; |
| 88 this.height = height; | 58 this.height = height; |
| 89 this.framerate = new FramerateRange(minFramerate, maxFramerate); | 59 this.minFramerate = minFramerate; |
| 90 } | 60 this.maxFramerate = maxFramerate; |
| 91 | |
| 92 public CaptureFormat(int width, int height, FramerateRange framerate) { | |
| 93 this.width = width; | |
| 94 this.height = height; | |
| 95 this.framerate = framerate; | |
| 96 } | 61 } |
| 97 | 62 |
| 98 // Calculates the frame size of this capture format. | 63 // Calculates the frame size of this capture format. |
| 99 public int frameSize() { | 64 public int frameSize() { |
| 100 return frameSize(width, height, imageFormat); | 65 return frameSize(width, height, imageFormat); |
| 101 } | 66 } |
| 102 | 67 |
| 103 // Calculates the frame size of the specified image format. Currently only | 68 // Calculates the frame size of the specified image format. Currently only |
| 104 // supporting ImageFormat.NV21. | 69 // supporting ImageFormat.NV21. |
| 105 // The size is width * height * number of bytes per pixel. | 70 // The size is width * height * number of bytes per pixel. |
| 106 // http://developer.android.com/reference/android/hardware/Camera.html#addCa
llbackBuffer(byte[]) | 71 // http://developer.android.com/reference/android/hardware/Camera.html#addCa
llbackBuffer(byte[]) |
| 107 public static int frameSize(int width, int height, int imageFormat) { | 72 public static int frameSize(int width, int height, int imageFormat) { |
| 108 if (imageFormat != ImageFormat.NV21) { | 73 if (imageFormat != ImageFormat.NV21) { |
| 109 throw new UnsupportedOperationException("Don't know how to calculate " | 74 throw new UnsupportedOperationException("Don't know how to calculate " |
| 110 + "the frame size of non-NV21 image formats."); | 75 + "the frame size of non-NV21 image formats."); |
| 111 } | 76 } |
| 112 return (width * height * ImageFormat.getBitsPerPixel(imageFormat)) / 8; | 77 return (width * height * ImageFormat.getBitsPerPixel(imageFormat)) / 8; |
| 113 } | 78 } |
| 114 | 79 |
| 115 @Override | 80 @Override |
| 116 public String toString() { | 81 public String toString() { |
| 117 return width + "x" + height + "@" + framerate; | 82 return width + "x" + height + "@[" + minFramerate + ":" + maxFramerate + "
]"; |
| 118 } | 83 } |
| 119 | 84 |
| 120 public boolean isSameFormat(final CaptureFormat that) { | 85 public boolean isSameFormat(final CaptureFormat that) { |
| 121 if (that == null) { | 86 if (that == null) { |
| 122 return false; | 87 return false; |
| 123 } | 88 } |
| 124 return width == that.width && height == that.height && framerate.equals(th
at.framerate); | 89 return width == that.width && height == that.height && maxFramerate == tha
t.maxFramerate |
| 90 && minFramerate == that.minFramerate; |
| 125 } | 91 } |
| 126 } | 92 } |
| 127 | 93 |
| 128 // Returns device names that can be used to create a new VideoCapturerAndroid. | 94 // Returns device names that can be used to create a new VideoCapturerAndroid. |
| 129 public static String[] getDeviceNames() { | 95 public static String[] getDeviceNames() { |
| 130 String[] names = new String[android.hardware.Camera.getNumberOfCameras()]; | 96 String[] names = new String[android.hardware.Camera.getNumberOfCameras()]; |
| 131 for (int i = 0; i < android.hardware.Camera.getNumberOfCameras(); ++i) { | 97 for (int i = 0; i < android.hardware.Camera.getNumberOfCameras(); ++i) { |
| 132 names[i] = getDeviceName(i); | 98 names[i] = getDeviceName(i); |
| 133 } | 99 } |
| 134 return names; | 100 return names; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 161 public static String getNameOfFrontFacingDevice() { | 127 public static String getNameOfFrontFacingDevice() { |
| 162 return getNameOfDevice(android.hardware.Camera.CameraInfo.CAMERA_FACING_FRON
T); | 128 return getNameOfDevice(android.hardware.Camera.CameraInfo.CAMERA_FACING_FRON
T); |
| 163 } | 129 } |
| 164 | 130 |
| 165 // Returns the name of the back facing camera. Returns null if the | 131 // Returns the name of the back facing camera. Returns null if the |
| 166 // camera can not be used or does not exist. | 132 // camera can not be used or does not exist. |
| 167 public static String getNameOfBackFacingDevice() { | 133 public static String getNameOfBackFacingDevice() { |
| 168 return getNameOfDevice(android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK
); | 134 return getNameOfDevice(android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK
); |
| 169 } | 135 } |
| 170 | 136 |
| 171 // Helper class for finding the closest supported format for the two functions
below. It creates a | 137 // Helper class for finding the closest supported format for the two functions
below. |
| 172 // comparator based on the difference to some requested parameters, where the
element with the | |
| 173 // minimum difference is the element that is closest to the requested paramete
rs. | |
| 174 private static abstract class ClosestComparator<T> implements Comparator<T> { | 138 private static abstract class ClosestComparator<T> implements Comparator<T> { |
| 175 // Difference between supported and requested parameter. | 139 // Difference between supported and requested parameter. |
| 176 abstract int diff(T supportedParameter); | 140 abstract int diff(T supportedParameter); |
| 177 | 141 |
| 178 @Override | 142 @Override |
| 179 public int compare(T t1, T t2) { | 143 public int compare(T t1, T t2) { |
| 180 return diff(t1) - diff(t2); | 144 return diff(t1) - diff(t2); |
| 181 } | 145 } |
| 182 } | 146 } |
| 183 | 147 |
| 184 public static CaptureFormat.FramerateRange getClosestSupportedFramerateRange( | 148 public static int[] getFramerateRange(android.hardware.Camera.Parameters param
eters, |
| 185 List<CaptureFormat.FramerateRange> supportedFramerates, final int requeste
dFps) { | 149 final int framerate) { |
| 186 return Collections.min(supportedFramerates, | 150 List<int[]> listFpsRange = parameters.getSupportedPreviewFpsRange(); |
| 187 new ClosestComparator<CaptureFormat.FramerateRange>() { | 151 if (listFpsRange.isEmpty()) { |
| 188 private static final int MAX_FPS_WEIGHT = 10; | 152 Logging.w(TAG, "No supported preview fps range"); |
| 189 | 153 return new int[]{0, 0}; |
| 190 @Override | 154 } |
| 191 int diff(CaptureFormat.FramerateRange range) { | 155 return Collections.min(listFpsRange, |
| 192 return range.min + MAX_FPS_WEIGHT * abs(requestedFps * 1000 - range.
max); | 156 new ClosestComparator<int[]>() { |
| 157 @Override int diff(int[] range) { |
| 158 final int maxFpsWeight = 10; |
| 159 return range[android.hardware.Camera.Parameters.PREVIEW_FPS_MIN_INDE
X] |
| 160 + maxFpsWeight * abs(framerate |
| 161 - range[android.hardware.Camera.Parameters.PREVIEW_FPS_MAX_I
NDEX]); |
| 193 } | 162 } |
| 194 }); | 163 }); |
| 195 } | 164 } |
| 196 | 165 |
| 197 public static android.hardware.Camera.Size getClosestSupportedSize( | 166 public static android.hardware.Camera.Size getClosestSupportedSize( |
| 198 List<android.hardware.Camera.Size> supportedSizes, final int requestedWidt
h, | 167 List<android.hardware.Camera.Size> supportedSizes, final int requestedWidt
h, |
| 199 final int requestedHeight) { | 168 final int requestedHeight) { |
| 200 return Collections.min(supportedSizes, | 169 return Collections.min(supportedSizes, |
| 201 new ClosestComparator<android.hardware.Camera.Size>() { | 170 new ClosestComparator<android.hardware.Camera.Size>() { |
| 202 @Override int diff(android.hardware.Camera.Size size) { | 171 @Override int diff(android.hardware.Camera.Size size) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 213 if (info.facing == facing) { | 182 if (info.facing == facing) { |
| 214 return getDeviceName(i); | 183 return getDeviceName(i); |
| 215 } | 184 } |
| 216 } catch (Exception e) { | 185 } catch (Exception e) { |
| 217 Logging.e(TAG, "getCameraInfo() failed on index " + i, e); | 186 Logging.e(TAG, "getCameraInfo() failed on index " + i, e); |
| 218 } | 187 } |
| 219 } | 188 } |
| 220 return null; | 189 return null; |
| 221 } | 190 } |
| 222 } | 191 } |
| OLD | NEW |