Index: webrtc/api/java/android/org/webrtc/CameraEnumerationAndroid.java |
diff --git a/webrtc/api/java/android/org/webrtc/CameraEnumerationAndroid.java b/webrtc/api/java/android/org/webrtc/CameraEnumerationAndroid.java |
index c5c8be3a5d8e62443fb28ce70c9a8dc82fd0d1c5..87f2adc1431e8376310999a342cf837182893c77 100644 |
--- a/webrtc/api/java/android/org/webrtc/CameraEnumerationAndroid.java |
+++ b/webrtc/api/java/android/org/webrtc/CameraEnumerationAndroid.java |
@@ -23,7 +23,7 @@ import java.util.List; |
public class CameraEnumerationAndroid { |
private final static String TAG = "CameraEnumerationAndroid"; |
// Synchronized on |CameraEnumerationAndroid.this|. |
- private static Enumerator enumerator = new CameraEnumerator(); |
+ private static Enumerator enumerator = new Camera1Enumerator(); |
public interface Enumerator { |
/** |
@@ -43,26 +43,90 @@ public class CameraEnumerationAndroid { |
} |
public static class CaptureFormat { |
- public final int width; |
- public final int height; |
- public final int maxFramerate; |
- public final int minFramerate; |
+ // Class for describing width and height dimensions in pixels. |
+ public static class Size { |
+ public int width; |
+ public int height; |
+ |
+ public Size(int width, int height) { |
+ this.width = width; |
+ this.height = height; |
+ } |
+ |
+ @Override |
+ public String toString() { |
+ return width + "x" + height; |
+ } |
+ |
+ @Override |
+ public boolean equals(Object other) { |
+ if (!(other instanceof Size)) { |
+ return false; |
+ } |
+ final Size otherSize = (Size) other; |
+ return width == otherSize.width && height == otherSize.height; |
+ } |
+ |
+ @Override |
+ public int hashCode() { |
+ // Use prime close to 2^16 to avoid collisions for normal values less than 2^16. |
+ return 1 + 65537 * width + height; |
+ } |
+ } |
+ |
+ // Class to represent a framerate range. The framerate varies because of lightning conditions. |
+ // The values are multiplied by 1000, so 1000 represents one frame per second. |
+ public static class FramerateRange { |
+ public int min; |
+ public int max; |
+ |
+ public FramerateRange(int min, int max) { |
+ this.min = min; |
+ this.max = max; |
+ } |
+ |
+ @Override |
+ public String toString() { |
+ return "[" + min + ":" + max + "]"; |
+ } |
+ |
+ @Override |
+ public boolean equals(Object other) { |
+ if (!(other instanceof FramerateRange)) { |
+ return false; |
+ } |
+ final FramerateRange otherFramerate = (FramerateRange) other; |
+ return min == otherFramerate.min && max == otherFramerate.max; |
+ } |
+ |
+ @Override |
+ public int hashCode() { |
+ // Use prime close to 2^16 to avoid collisions for normal values less than 2^16. |
+ return 1 + 65537 * min + max; |
+ } |
+ } |
+ |
+ public final Size size; |
+ public final FramerateRange framerate; |
+ |
// TODO(hbos): If VideoCapturer.startCapture is updated to support other image formats then this |
// needs to be updated and VideoCapturer.getSupportedFormats need to return CaptureFormats of |
// all imageFormats. |
public final int imageFormat = ImageFormat.NV21; |
- public CaptureFormat(int width, int height, int minFramerate, |
- int maxFramerate) { |
- this.width = width; |
- this.height = height; |
- this.minFramerate = minFramerate; |
- this.maxFramerate = maxFramerate; |
+ public CaptureFormat(int width, int height, int minFramerate, int maxFramerate) { |
+ this.size = new Size(width, height); |
+ this.framerate = new FramerateRange(minFramerate, maxFramerate); |
+ } |
+ |
+ public CaptureFormat(Size size, FramerateRange framerate) { |
+ this.size = size; |
+ this.framerate = framerate; |
} |
// Calculates the frame size of this capture format. |
public int frameSize() { |
- return frameSize(width, height, imageFormat); |
+ return frameSize(size.width, size.height, imageFormat); |
} |
// Calculates the frame size of the specified image format. Currently only |
@@ -79,15 +143,21 @@ public class CameraEnumerationAndroid { |
@Override |
public String toString() { |
- return width + "x" + height + "@[" + minFramerate + ":" + maxFramerate + "]"; |
+ return size.toString() + "@" + framerate.toString(); |
} |
- public boolean isSameFormat(final CaptureFormat that) { |
- if (that == null) { |
+ @Override |
+ public boolean equals(Object other) { |
+ if (!(other instanceof CaptureFormat)) { |
return false; |
} |
- return width == that.width && height == that.height && maxFramerate == that.maxFramerate |
- && minFramerate == that.minFramerate; |
+ final CaptureFormat otherFormat = (CaptureFormat) other; |
+ return size.equals(otherFormat.size) && framerate.equals(otherFormat.framerate); |
+ } |
+ |
+ @Override |
+ public int hashCode() { |
+ return 1 + 31 * size.hashCode() + framerate.hashCode(); |
} |
} |
@@ -134,7 +204,9 @@ public class CameraEnumerationAndroid { |
return getNameOfDevice(android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK); |
} |
- // Helper class for finding the closest supported format for the two functions below. |
+ // Helper class for finding the closest supported format for the two functions below. It creates a |
+ // comparator based on the difference to some requested parameters, where the element with the |
+ // minimum difference is the element that is closest to the requested parameters. |
private static abstract class ClosestComparator<T> implements Comparator<T> { |
// Difference between supported and requested parameter. |
abstract int diff(T supportedParameter); |
@@ -145,30 +217,27 @@ public class CameraEnumerationAndroid { |
} |
} |
- public static int[] getFramerateRange(android.hardware.Camera.Parameters parameters, |
- final int framerate) { |
- List<int[]> listFpsRange = parameters.getSupportedPreviewFpsRange(); |
- if (listFpsRange.isEmpty()) { |
- Logging.w(TAG, "No supported preview fps range"); |
- return new int[]{0, 0}; |
- } |
- return Collections.min(listFpsRange, |
- new ClosestComparator<int[]>() { |
- @Override int diff(int[] range) { |
+ // Prefer a fps range with an upper bound close to |framerate|. Also prefer a fps range with a low |
+ // lower bound, to allow the framerate to fluctuate based on lightning conditions. |
+ public static CaptureFormat.FramerateRange getClosestSupportedFramerate( |
+ List<CaptureFormat.FramerateRange> supportedFramerates, final int requestedFps) { |
+ return Collections.min(supportedFramerates, |
+ new ClosestComparator<CaptureFormat.FramerateRange>() { |
+ @Override |
+ int diff(CaptureFormat.FramerateRange range) { |
final int maxFpsWeight = 10; |
- return range[android.hardware.Camera.Parameters.PREVIEW_FPS_MIN_INDEX] |
- + maxFpsWeight * abs(framerate |
- - range[android.hardware.Camera.Parameters.PREVIEW_FPS_MAX_INDEX]); |
+ return range.min + maxFpsWeight * abs(1000 * requestedFps - range.max); |
} |
}); |
} |
- public static android.hardware.Camera.Size getClosestSupportedSize( |
- List<android.hardware.Camera.Size> supportedSizes, final int requestedWidth, |
+ public static CaptureFormat.Size getClosestSupportedSize( |
+ List<CaptureFormat.Size> supportedSizes, final int requestedWidth, |
final int requestedHeight) { |
return Collections.min(supportedSizes, |
- new ClosestComparator<android.hardware.Camera.Size>() { |
- @Override int diff(android.hardware.Camera.Size size) { |
+ new ClosestComparator<CaptureFormat.Size>() { |
+ @Override |
+ int diff(CaptureFormat.Size size) { |
return abs(requestedWidth - size.width) + abs(requestedHeight - size.height); |
} |
}); |