Revert AndroidTestUtil.canDecode to use EncoderUtil.findCodecForFormat

208eefc0fd introduced using `DefaultDecoderFactory.getDecoderInfo(format) != null` caused certain tests not to be skipped when they were expected to be, creating more mh failures.

PiperOrigin-RevId: 537820370
(cherry picked from commit 2af57527853beb7ee40e5b70efde74e72f8d60f1)
This commit is contained in:
tofunmi 2023-06-05 10:08:26 +00:00 committed by Tofunmi Adigun-Hameed
parent 83957eb67f
commit a8dbea74ce
2 changed files with 35 additions and 2 deletions

View File

@ -834,7 +834,7 @@ public final class AndroidTestUtil {
} }
} }
private static boolean canDecode(Format format) throws MediaCodecUtil.DecoderQueryException { private static boolean canDecode(Format format) {
// Check decoding capability in the same way as the default decoder factory. // Check decoding capability in the same way as the default decoder factory.
MediaFormat mediaFormat = MediaFormatUtil.createMediaFormatFromFormat(format); MediaFormat mediaFormat = MediaFormatUtil.createMediaFormatFromFormat(format);
@Nullable @Nullable
@ -843,7 +843,7 @@ public final class AndroidTestUtil {
MediaFormatUtil.maybeSetInteger( MediaFormatUtil.maybeSetInteger(
mediaFormat, MediaFormat.KEY_PROFILE, codecProfileAndLevel.first); mediaFormat, MediaFormat.KEY_PROFILE, codecProfileAndLevel.first);
} }
return DefaultDecoderFactory.getDecoderInfo(format) != null; return EncoderUtil.findCodecForFormat(mediaFormat, /* isDecoder= */ true) != null;
} }
private static boolean canEncode(Format format) { private static boolean canEncode(Format format) {

View File

@ -24,6 +24,7 @@ import android.media.CamcorderProfile;
import android.media.MediaCodec; import android.media.MediaCodec;
import android.media.MediaCodecInfo; import android.media.MediaCodecInfo;
import android.media.MediaCodecList; import android.media.MediaCodecList;
import android.media.MediaFormat;
import android.util.Pair; import android.util.Pair;
import android.util.Range; import android.util.Range;
import android.util.Size; import android.util.Size;
@ -37,6 +38,7 @@ import androidx.media3.common.C.ColorTransfer;
import androidx.media3.common.ColorInfo; import androidx.media3.common.ColorInfo;
import androidx.media3.common.Format; import androidx.media3.common.Format;
import androidx.media3.common.MimeTypes; import androidx.media3.common.MimeTypes;
import androidx.media3.common.util.MediaFormatUtil;
import androidx.media3.common.util.UnstableApi; import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util; import androidx.media3.common.util.Util;
import com.google.common.base.Ascii; import com.google.common.base.Ascii;
@ -301,6 +303,37 @@ public final class EncoderUtil {
return maxSupportedLevel; return maxSupportedLevel;
} }
/**
* Finds a {@link MediaCodec} that supports the {@link MediaFormat}, or {@code null} if none is
* found.
*/
@Nullable
public static String findCodecForFormat(MediaFormat format, boolean isDecoder) {
MediaCodecList mediaCodecList = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
// Format must not include KEY_FRAME_RATE on API21.
// https://developer.android.com/reference/android/media/MediaCodecList#findDecoderForFormat(android.media.MediaFormat)
float frameRate = Format.NO_VALUE;
if (Util.SDK_INT == 21 && format.containsKey(MediaFormat.KEY_FRAME_RATE)) {
try {
frameRate = format.getFloat(MediaFormat.KEY_FRAME_RATE);
} catch (ClassCastException e) {
frameRate = format.getInteger(MediaFormat.KEY_FRAME_RATE);
}
// Clears the frame rate field.
format.setString(MediaFormat.KEY_FRAME_RATE, null);
}
String mediaCodecName =
isDecoder
? mediaCodecList.findDecoderForFormat(format)
: mediaCodecList.findEncoderForFormat(format);
if (Util.SDK_INT == 21) {
MediaFormatUtil.maybeSetInteger(format, MediaFormat.KEY_FRAME_RATE, round(frameRate));
}
return mediaCodecName;
}
/** Returns the range of supported bitrates for the given {@linkplain MimeTypes MIME type}. */ /** Returns the range of supported bitrates for the given {@linkplain MimeTypes MIME type}. */
public static Range<Integer> getSupportedBitrateRange( public static Range<Integer> getSupportedBitrateRange(
MediaCodecInfo encoderInfo, String mimeType) { MediaCodecInfo encoderInfo, String mimeType) {