Android: Add FramerateRange class

The Camera1 and Camera2 API use different framerate range types. Camera1
uses int[2] and Camera2 uses Range<Integer>. Range<Integer> is
unfortunately only available on Lollipop and later, so this CL adds a
similar FramerateRange class in CaptureFormat.

The purpose with this CL is to have a common framerate range type that can
be reused from both Camera1 and Camera2 in helper functions such as
CameraEnumerationAndroid.getClosestSupportedFramerateRange().

BUG=webrtc:5519
R=sakal@webrtc.org

Review URL: https://codereview.webrtc.org/2010763003 .

Cr-Commit-Position: refs/heads/master@{#12942}
This commit is contained in:
Magnus Jedvert
2016-05-27 10:35:51 +02:00
parent a44e72c44f
commit 94cb67d6df
6 changed files with 101 additions and 47 deletions

View File

@ -43,21 +43,56 @@ public class CameraEnumerationAndroid {
}
public static class CaptureFormat {
// 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 / 1000.0f) + ":" + (max / 1000.0f) + "]";
}
@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 int width;
public final int height;
public final int maxFramerate;
public final int minFramerate;
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) {
public CaptureFormat(int width, int height, int minFramerate, int maxFramerate) {
this.width = width;
this.height = height;
this.minFramerate = minFramerate;
this.maxFramerate = maxFramerate;
this.framerate = new FramerateRange(minFramerate, maxFramerate);
}
public CaptureFormat(int width, int height, FramerateRange framerate) {
this.width = width;
this.height = height;
this.framerate = framerate;
}
// Calculates the frame size of this capture format.
@ -79,15 +114,14 @@ public class CameraEnumerationAndroid {
@Override
public String toString() {
return width + "x" + height + "@[" + minFramerate + ":" + maxFramerate + "]";
return width + "x" + height + "@" + framerate;
}
public boolean isSameFormat(final CaptureFormat that) {
if (that == null) {
return false;
}
return width == that.width && height == that.height && maxFramerate == that.maxFramerate
&& minFramerate == that.minFramerate;
return width == that.width && height == that.height && framerate.equals(that.framerate);
}
}
@ -134,7 +168,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,20 +181,15 @@ 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) {
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]);
public static CaptureFormat.FramerateRange getClosestSupportedFramerateRange(
List<CaptureFormat.FramerateRange> supportedFramerates, final int requestedFps) {
return Collections.min(supportedFramerates,
new ClosestComparator<CaptureFormat.FramerateRange>() {
private static final int MAX_FPS_WEIGHT = 10;
@Override
int diff(CaptureFormat.FramerateRange range) {
return range.min + MAX_FPS_WEIGHT * abs(requestedFps * 1000 - range.max);
}
});
}

View File

@ -81,4 +81,16 @@ public class CameraEnumerator implements CameraEnumerationAndroid.Enumerator {
+ " Time spent: " + (endTimeMs - startTimeMs) + " ms.");
return formatList;
}
// Convert from int[2] to CaptureFormat.FramerateRange.
public static List<CaptureFormat.FramerateRange> convertFramerates(
List<int[]> arrayRanges) {
final List<CaptureFormat.FramerateRange> ranges = new ArrayList<CaptureFormat.FramerateRange>();
for (int[] range : arrayRanges) {
ranges.add(new CaptureFormat.FramerateRange(
range[android.hardware.Camera.Parameters.PREVIEW_FPS_MIN_INDEX],
range[android.hardware.Camera.Parameters.PREVIEW_FPS_MAX_INDEX]));
}
return ranges;
}
}

View File

@ -397,19 +397,24 @@ public class VideoCapturerAndroid implements
// Find closest supported format for |width| x |height| @ |framerate|.
final android.hardware.Camera.Parameters parameters = camera.getParameters();
for (int[] fpsRange : parameters.getSupportedPreviewFpsRange()) {
Logging.d(TAG, "Available fps range: " +
fpsRange[android.hardware.Camera.Parameters.PREVIEW_FPS_MIN_INDEX] + ":" +
fpsRange[android.hardware.Camera.Parameters.PREVIEW_FPS_MAX_INDEX]);
final List<CaptureFormat.FramerateRange> supportedFramerates =
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 int[] range = CameraEnumerationAndroid.getFramerateRange(parameters, framerate * 1000);
final android.hardware.Camera.Size previewSize =
CameraEnumerationAndroid.getClosestSupportedSize(
parameters.getSupportedPreviewSizes(), width, height);
final CaptureFormat captureFormat = new CaptureFormat(
previewSize.width, previewSize.height,
range[android.hardware.Camera.Parameters.PREVIEW_FPS_MIN_INDEX],
range[android.hardware.Camera.Parameters.PREVIEW_FPS_MAX_INDEX]);
previewSize.width, previewSize.height, bestFpsRange);
// Check if we are already using this capture format, then we don't need to do anything.
if (captureFormat.isSameFormat(this.captureFormat)) {
@ -424,8 +429,8 @@ public class VideoCapturerAndroid implements
}
// Note: setRecordingHint(true) actually decrease frame rate on N5.
// parameters.setRecordingHint(true);
if (captureFormat.maxFramerate > 0) {
parameters.setPreviewFpsRange(captureFormat.minFramerate, captureFormat.maxFramerate);
if (captureFormat.framerate.max > 0) {
parameters.setPreviewFpsRange(captureFormat.framerate.min, captureFormat.framerate.max);
}
parameters.setPreviewSize(captureFormat.width, captureFormat.height);