Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate encoders using MediaCodecList #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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;
}
}