From de455f1850bbbfcff58e09cfc5e5f8ae9fc97017 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Fri, 10 Jan 2020 12:32:07 +0100 Subject: [PATCH] Improve DeviceEncoders to respect max block count --- .../cameraview/internal/DeviceEncoders.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/internal/DeviceEncoders.java b/cameraview/src/main/java/com/otaliastudios/cameraview/internal/DeviceEncoders.java index 14efcfdc..3180f81b 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/internal/DeviceEncoders.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/internal/DeviceEncoders.java @@ -7,6 +7,7 @@ import android.media.MediaCodecInfo; import android.media.MediaCodecList; import android.media.MediaFormat; import android.os.Build; +import android.util.Range; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -262,6 +263,27 @@ public class DeviceEncoders { // It's still possible that we're unsupported for other reasons. if (!mVideoCapabilities.isSizeSupported(width, height)) { + try { + if (!mVideoCapabilities.getSupportedHeightsFor(width).contains(height)) { + // We cannot change the aspect ratio, but the max block count might also be the + // issue. Try to find a width that contains a height that would accept our AR. + int candidateWidth = width; + int minWidth = mVideoCapabilities.getSupportedWidths().getLower(); + int widthAlignment = mVideoCapabilities.getWidthAlignment(); + while (candidateWidth >= minWidth) { + // Reduce by 32 and realign just in case, then check if our AR is now + // supported. If it is, restart from scratch to go through the other checks. + candidateWidth -= 32; + while (candidateWidth % widthAlignment != 0) candidateWidth--; + int candidateHeight = (int) Math.round(candidateWidth / aspect); + if (mVideoCapabilities.getSupportedHeightsFor(candidateWidth) + .contains(candidateHeight)) { + LOG.w("getSupportedVideoSize - restarting with smaller size."); + return getSupportedVideoSize(new Size(candidateWidth, candidateHeight)); + } + } + } + } catch (IllegalArgumentException ignore) {} throw new VideoException("Size not supported for unknown reason." + " Might be an aspect ratio issue." + " Desired size:" + new Size(width, height));