diff --git a/webrtc/api/java/android/org/webrtc/CameraEnumerationAndroid.java b/webrtc/api/java/android/org/webrtc/CameraEnumerationAndroid.java index ed93bd12a0..f3e08f6f33 100644 --- a/webrtc/api/java/android/org/webrtc/CameraEnumerationAndroid.java +++ b/webrtc/api/java/android/org/webrtc/CameraEnumerationAndroid.java @@ -12,8 +12,6 @@ package org.webrtc; import static java.lang.Math.abs; -import org.webrtc.Logging; - import android.graphics.ImageFormat; import java.util.Collections; @@ -60,6 +58,7 @@ public class CameraEnumerationAndroid { public final int width; public final int height; 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. @@ -99,11 +98,19 @@ public class CameraEnumerationAndroid { return width + "x" + height + "@" + framerate; } - 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 && framerate.equals(that.framerate); + final CaptureFormat otherFormat = (CaptureFormat) other; + return width == otherFormat.width && height == otherFormat.height + && framerate.equals(otherFormat.framerate); + } + + @Override + public int hashCode() { + return 1 + (width * 65497 + height) * 251 + framerate.hashCode(); } } @@ -210,12 +217,13 @@ public class CameraEnumerationAndroid { }); } - public static android.hardware.Camera.Size getClosestSupportedSize( - List supportedSizes, final int requestedWidth, + public static Size getClosestSupportedSize( + List supportedSizes, final int requestedWidth, final int requestedHeight) { return Collections.min(supportedSizes, - new ClosestComparator() { - @Override int diff(android.hardware.Camera.Size size) { + new ClosestComparator() { + @Override + int diff(Size size) { return abs(requestedWidth - size.width) + abs(requestedHeight - size.height); } }); diff --git a/webrtc/api/java/android/org/webrtc/CameraEnumerator.java b/webrtc/api/java/android/org/webrtc/CameraEnumerator.java index 90d9487ba3..203c1954b7 100644 --- a/webrtc/api/java/android/org/webrtc/CameraEnumerator.java +++ b/webrtc/api/java/android/org/webrtc/CameraEnumerator.java @@ -79,6 +79,16 @@ public class CameraEnumerator { return formatList; } + // Convert from android.hardware.Camera.Size to Size. + public static List convertSizes( + List cameraSizes) { + final List sizes = new ArrayList(); + for (android.hardware.Camera.Size size : cameraSizes) { + sizes.add(new Size(size.width, size.height)); + } + return sizes; + } + // Convert from int[2] to CaptureFormat.FramerateRange. public static List convertFramerates( List arrayRanges) { diff --git a/webrtc/api/java/android/org/webrtc/VideoCapturerAndroid.java b/webrtc/api/java/android/org/webrtc/VideoCapturerAndroid.java index 0690a4cfa3..5e9a5a6f2f 100644 --- a/webrtc/api/java/android/org/webrtc/VideoCapturerAndroid.java +++ b/webrtc/api/java/android/org/webrtc/VideoCapturerAndroid.java @@ -394,23 +394,17 @@ public class VideoCapturerAndroid implements CameraEnumerator.convertFramerates(parameters.getSupportedPreviewFpsRange()); Logging.d(TAG, "Available fps ranges: " + supportedFramerates); - final CaptureFormat.FramerateRange bestFpsRange; - if (supportedFramerates.isEmpty()) { - Logging.w(TAG, "No supported preview fps range"); - bestFpsRange = new CaptureFormat.FramerateRange(0, 0); - } else { - bestFpsRange = CameraEnumerationAndroid.getClosestSupportedFramerateRange( - supportedFramerates, framerate); - } + final CaptureFormat.FramerateRange fpsRange = + CameraEnumerationAndroid.getClosestSupportedFramerateRange(supportedFramerates, framerate); - final android.hardware.Camera.Size previewSize = - CameraEnumerationAndroid.getClosestSupportedSize( - parameters.getSupportedPreviewSizes(), width, height); - final CaptureFormat captureFormat = new CaptureFormat( - previewSize.width, previewSize.height, bestFpsRange); + final Size previewSize = CameraEnumerationAndroid.getClosestSupportedSize( + CameraEnumerator.convertSizes(parameters.getSupportedPreviewSizes()), width, height); + + final CaptureFormat captureFormat = + new CaptureFormat(previewSize.width, previewSize.height, fpsRange); // Check if we are already using this capture format, then we don't need to do anything. - if (captureFormat.isSameFormat(this.captureFormat)) { + if (captureFormat.equals(this.captureFormat)) { return; } @@ -425,16 +419,15 @@ public class VideoCapturerAndroid implements if (captureFormat.framerate.max > 0) { parameters.setPreviewFpsRange(captureFormat.framerate.min, captureFormat.framerate.max); } - parameters.setPreviewSize(captureFormat.width, captureFormat.height); + parameters.setPreviewSize(previewSize.width, previewSize.height); if (!isCapturingToTexture) { parameters.setPreviewFormat(captureFormat.imageFormat); } // Picture size is for taking pictures and not for preview/video, but we need to set it anyway // as a workaround for an aspect ratio problem on Nexus 7. - final android.hardware.Camera.Size pictureSize = - CameraEnumerationAndroid.getClosestSupportedSize( - parameters.getSupportedPictureSizes(), width, height); + final Size pictureSize = CameraEnumerationAndroid.getClosestSupportedSize( + CameraEnumerator.convertSizes(parameters.getSupportedPictureSizes()), width, height); parameters.setPictureSize(pictureSize.width, pictureSize.height); // Temporarily stop preview if it's already running. diff --git a/webrtc/base/java/src/org/webrtc/Size.java b/webrtc/base/java/src/org/webrtc/Size.java new file mode 100644 index 0000000000..a711b5d2ca --- /dev/null +++ b/webrtc/base/java/src/org/webrtc/Size.java @@ -0,0 +1,45 @@ +/* + * Copyright 2016 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +package org.webrtc; + +/** + * Class for representing size of an object. Very similar to android.util.Size but available on all + * devices. + */ +public 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; + } +}