From 879c03ff882422549a7974b6e2826e2ceb419b06 Mon Sep 17 00:00:00 2001 From: Sam Elsamman Date: Fri, 12 Apr 2019 08:37:01 +0100 Subject: [PATCH] Validate encoders using MediaCodecList --- .../engine/MediaFormatValidator.java | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/src/main/java/net/ypresto/androidtranscoder/engine/MediaFormatValidator.java b/lib/src/main/java/net/ypresto/androidtranscoder/engine/MediaFormatValidator.java index 67ea5ba8..584ba1d5 100644 --- a/lib/src/main/java/net/ypresto/androidtranscoder/engine/MediaFormatValidator.java +++ b/lib/src/main/java/net/ypresto/androidtranscoder/engine/MediaFormatValidator.java @@ -15,9 +15,10 @@ */ package net.ypresto.androidtranscoder.engine; +import android.media.MediaCodecInfo; +import android.media.MediaCodecList; import android.media.MediaFormat; -import net.ypresto.androidtranscoder.format.MediaFormatExtraConstants; import net.ypresto.androidtranscoder.utils.AvcCsdUtils; import net.ypresto.androidtranscoder.utils.AvcSpsUtils; @@ -27,13 +28,14 @@ class MediaFormatValidator { // Refer: http://en.wikipedia.org/wiki/H.264/MPEG-4_AVC#Profiles private static final byte PROFILE_IDC_BASELINE = 66; + public static void validateVideoOutputFormat(MediaFormat format) { String mime = format.getString(MediaFormat.KEY_MIME); // Refer: http://developer.android.com/guide/appendix/media-formats.html#core // Refer: http://en.wikipedia.org/wiki/MPEG-4_Part_14#Data_streams - if (!MediaFormatExtraConstants.MIMETYPE_VIDEO_AVC.equals(mime)) { - throw new InvalidOutputFormatException("Video codecs other than AVC is not supported, actual mime type: " + mime); - } + if (!validateEncoderMimeType(mime)) + throw new InvalidOutputFormatException("Video codecs not supported, actual mime type: " + mime); + ByteBuffer spsBuffer = AvcCsdUtils.getSpsBuffer(format); byte profileIdc = AvcSpsUtils.getProfileIdc(spsBuffer); if (profileIdc != PROFILE_IDC_BASELINE) { @@ -43,8 +45,25 @@ public static void validateVideoOutputFormat(MediaFormat format) { public static void validateAudioOutputFormat(MediaFormat format) { String mime = format.getString(MediaFormat.KEY_MIME); - if (!MediaFormatExtraConstants.MIMETYPE_AUDIO_AAC.equals(mime)) { - throw new InvalidOutputFormatException("Audio codecs other than AAC is not supported, actual mime type: " + mime); + if (!validateEncoderMimeType(mime)) + throw new InvalidOutputFormatException("Audio codecs not supported, actual mime type: " + mime); + } + + private static boolean validateEncoderMimeType(String mime) { + // See https://developer.android.com/reference/android/media/MediaCodecInfo + // Code on that page was updated to use getCodecInfos rather than deprecated getCodeInfoAt() + MediaCodecList list = new MediaCodecList(MediaCodecList.ALL_CODECS); + MediaCodecInfo[] codecInfos = list.getCodecInfos(); + for (MediaCodecInfo info : codecInfos) { + if (info.isEncoder()) { + String[] types = info.getSupportedTypes(); + for (int j = 0; j < types.length; j++) { + if (types[j].equalsIgnoreCase(mime)) { + return true; + } + } + } } + return false; } }