下面列出了怎么用android.media.MediaCodecInfo的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressLint("NewApi")
public static MediaCodecInfo selectCodec(String mimeType) {
int numCodecs = MediaCodecList.getCodecCount();
MediaCodecInfo lastCodecInfo = null;
for (int i = 0; i < numCodecs; i++) {
MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
if (!codecInfo.isEncoder()) {
continue;
}
String[] types = codecInfo.getSupportedTypes();
for (String type : types) {
if (type.equalsIgnoreCase(mimeType)) {
lastCodecInfo = codecInfo;
if (!lastCodecInfo.getName().equals("OMX.SEC.avc.enc")) {
return lastCodecInfo;
} else if (lastCodecInfo.getName().equals("OMX.SEC.AVC.Encoder")) {
return lastCodecInfo;
}
}
}
}
return lastCodecInfo;
}
/**
* select the first codec that match a specific MIME type
* @param mimeType
* @return
*/
private static final MediaCodecInfo selectAudioCodec(final String mimeType) {
if (DEBUG) Log.v(TAG, "selectAudioCodec:");
MediaCodecInfo result = null;
// get the list of available codecs
final int numCodecs = MediaCodecList.getCodecCount();
LOOP: for (int i = 0; i < numCodecs; i++) {
final MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
if (!codecInfo.isEncoder()) { // skipp decoder
continue;
}
final String[] types = codecInfo.getSupportedTypes();
for (int j = 0; j < types.length; j++) {
if (DEBUG) Log.i(TAG, "supportedType:" + codecInfo.getName() + ",MIME=" + types[j]);
if (types[j].equalsIgnoreCase(mimeType)) {
if (result == null) {
result = codecInfo;
break LOOP;
}
}
}
}
return result;
}
private void initVideoMediaCodec()throws Exception{
MediaFormat mediaFormat;
if(rotation==90 || rotation==270){
//设置视频宽高
mediaFormat = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, videoHeight, videoWidth);
}else{
mediaFormat = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, videoWidth, videoHeight);
}
//图像数据格式 YUV420
mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar);
//码率
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, videoWidth*videoHeight*3);
//每秒30帧
mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, frameRate);
//1秒一个关键帧
mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
videoMediaCodec = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_VIDEO_AVC);
videoMediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
videoMediaCodec.start();
}
/**
* Returns the first codec capable of encoding the specified MIME type, or null if no match was
* found.
*/
static MediaCodecInfo selectCodec(final String mimeType) {
final int numCodecs = MediaCodecList.getCodecCount();
for (int i = 0; i < numCodecs; i++) {
final MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
if (!codecInfo.isEncoder()) {
continue;
}
final String[] types = codecInfo.getSupportedTypes();
for (String type : types) {
if (type.equalsIgnoreCase(mimeType)) {
return codecInfo;
}
}
}
return null;
}
private static int selectColorFormat(MediaCodecInfo codecInfo, String mimeType) {
int result = 0;
final MediaCodecInfo.CodecCapabilities capabilities;
try {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
capabilities = codecInfo.getCapabilitiesForType(mimeType);
} finally {
Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
}
int colorFormat;
for (int i = 0; i < capabilities.colorFormats.length; i++) {
colorFormat = capabilities.colorFormats[i];
if (isRecognizedViewFormat(colorFormat)) {
result = colorFormat;
break;
}
}
if (result == 0) {
Log.e(TAG, "couldn't find a good color format for " + codecInfo.getName() + " / " + mimeType);
}
return result;
}
/**
* select the first codec that match a specific MIME type
* @param mimeType
* @return null if no codec matched
*/
protected static final MediaCodecInfo selectVideoCodec(final String mimeType) {
if (DEBUG) Log.v(TAG, "selectVideoCodec:");
// get the list of available codecs
final int numCodecs = MediaCodecList.getCodecCount();
for (int i = 0; i < numCodecs; i++) {
final MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
if (!codecInfo.isEncoder()) { // skipp decoder
continue;
}
// select first codec that match a specific MIME type and color format
final String[] types = codecInfo.getSupportedTypes();
for (int j = 0; j < types.length; j++) {
if (types[j].equalsIgnoreCase(mimeType)) {
if (DEBUG) Log.i(TAG, "codec:" + codecInfo.getName() + ",MIME=" + types[j]);
final int format = selectColorFormat(codecInfo, mimeType);
if (format > 0) {
return codecInfo;
}
}
}
}
return null;
}
/**
* Searches for a codec that implements the requested format conversion. Android framework encoder
* only.
*/
private static MediaCodecInfo searchAmongAndroidSupportedCodecs(String mimeType) {
int numCodecs = MediaCodecList.getCodecCount();
for (int i = 0; i < numCodecs; i++) {
MediaCodecInfo codecAndBitrate = MediaCodecList.getCodecInfoAt(i);
if (!codecAndBitrate.isEncoder()) {
continue;
}
String[] codecTypes = codecAndBitrate.getSupportedTypes();
for (int j = 0; j < codecTypes.length; j++) {
if (codecTypes[j].equalsIgnoreCase(mimeType)) {
return codecAndBitrate;
}
}
}
return null;
}
@SuppressLint("NewApi")
public static MediaCodecInfo selectCodec(String mimeType) {
int numCodecs = MediaCodecList.getCodecCount();
MediaCodecInfo lastCodecInfo = null;
for (int i = 0; i < numCodecs; i++) {
MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
if (!codecInfo.isEncoder()) {
continue;
}
String[] types = codecInfo.getSupportedTypes();
for (String type : types) {
if (type.equalsIgnoreCase(mimeType)) {
lastCodecInfo = codecInfo;
String name = lastCodecInfo.getName();
if (name != null) {
if (!name.equals("OMX.SEC.avc.enc")) {
return lastCodecInfo;
} else if (name.equals("OMX.SEC.AVC.Encoder")) {
return lastCodecInfo;
}
}
}
}
}
return lastCodecInfo;
}
/**
* select the first codec that match a specific MIME type
* @param mimeType
* @return null if no codec matched
*/
protected static final MediaCodecInfo selectVideoCodec(final String mimeType) {
if (DEBUG) Log.v(TAG, "selectVideoCodec:");
// get the list of available codecs
final int numCodecs = MediaCodecList.getCodecCount();
for (int i = 0; i < numCodecs; i++) {
final MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
if (!codecInfo.isEncoder()) { // skipp decoder
continue;
}
// select first codec that match a specific MIME type and color format
final String[] types = codecInfo.getSupportedTypes();
for (int j = 0; j < types.length; j++) {
if (types[j].equalsIgnoreCase(mimeType)) {
if (DEBUG) Log.i(TAG, "codec:" + codecInfo.getName() + ",MIME=" + types[j]);
final int format = selectColorFormat(codecInfo, mimeType);
if (format > 0) {
return codecInfo;
}
}
}
}
return null;
}
/**
* Find an encoder supported specified MIME type
*
* @return Returns empty array if not found any encoder supported specified MIME type
*/
public static MediaCodecInfo[] findEncodersByType(String mimeType) {
MediaCodecList codecList = new MediaCodecList(MediaCodecList.ALL_CODECS);
List<MediaCodecInfo> infos = new ArrayList<>();
for (MediaCodecInfo info : codecList.getCodecInfos()) {
if (!info.isEncoder()) {
continue;
}
try {
MediaCodecInfo.CodecCapabilities cap = info.getCapabilitiesForType(mimeType);
if (cap == null) continue;
} catch (IllegalArgumentException e) {
// unsupported
continue;
}
infos.add(info);
}
return infos.toArray(new MediaCodecInfo[infos.size()]);
}
/**
* Find an encoder supported specified MIME type
*
* @return Returns empty array if not found any encoder supported specified MIME type
*/
public static MediaCodecInfo[] findEncodersByType(String mimeType) {
MediaCodecList codecList = new MediaCodecList(MediaCodecList.ALL_CODECS);
List<MediaCodecInfo> infos = new ArrayList<>();
for (MediaCodecInfo info : codecList.getCodecInfos()) {
if (!info.isEncoder()) {
continue;
}
try {
MediaCodecInfo.CodecCapabilities cap = info.getCapabilitiesForType(mimeType);
if (cap == null) continue;
} catch (IllegalArgumentException e) {
// unsupported
continue;
}
infos.add(info);
}
return infos.toArray(new MediaCodecInfo[infos.size()]);
}
/**
* select the first codec that match a specific MIME type
* @param mimeType
* @return
*/
private static final MediaCodecInfo selectAudioCodec(final String mimeType) {
if (DEBUG) Log.v(TAG, "selectAudioCodec:");
MediaCodecInfo result = null;
// get the list of available codecs
final int numCodecs = MediaCodecList.getCodecCount();
LOOP: for (int i = 0; i < numCodecs; i++) {
final MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
if (!codecInfo.isEncoder()) { // skipp decoder
continue;
}
final String[] types = codecInfo.getSupportedTypes();
for (int j = 0; j < types.length; j++) {
if (DEBUG) Log.i(TAG, "supportedType:" + codecInfo.getName() + ",MIME=" + types[j]);
if (types[j].equalsIgnoreCase(mimeType)) {
if (result == null) {
result = codecInfo;
break LOOP;
}
}
}
}
return result;
}
@Override
protected void setTranscodeParameters(MediaComposer mediaComposer) throws IOException {
mediaComposer.addSourceFile(mediaUri1);
mediaComposer.setTargetFile(dstMediaPath);
configureVideoEncoder(mediaComposer, videoWidthOut, videoHeightOut);
AudioFormatAndroid audioFormat = new AudioFormatAndroid(audioMimeType, audioSampleRate, audioChannelCount);
audioFormat.setKeyMaxInputSize(48 * 1024);
audioFormat.setAudioBitrateInBytes(audioBitRate);
audioFormat.setAudioProfile(MediaCodecInfo.CodecProfileLevel.AACObjectLC);
mediaComposer.setTargetAudioFormat(audioFormat);
SubstituteAudioEffect effect = new SubstituteAudioEffect();
effect.setFileUri(this, mediaUri2, this.audioFormat);
mediaComposer.addAudioEffect(effect);
}
private MediaCodecInfo getVideoCodecInfo(String codecName) {
if (codecName == null) {
return null;
}
if (mAvcCodecInfo == null) {
mAvcCodecInfo = VideoUtils.findEncodersByType(ScreenRecorder.VIDEO_AVC);
}
MediaCodecInfo codec = null;
for (int i = 0; i < mAvcCodecInfo.length; i++) {
MediaCodecInfo info = mAvcCodecInfo[i];
if (info.getName().equals(codecName)) {
codec = info;
break;
}
}
if (codec == null) {
return null;
}
return codec;
}
@SuppressLint("NewApi")
public static MediaCodecInfo selectCodec(String mimeType) {
int numCodecs = MediaCodecList.getCodecCount();
MediaCodecInfo lastCodecInfo = null;
for (int i = 0; i < numCodecs; i++) {
MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
if (!codecInfo.isEncoder()) {
continue;
}
String[] types = codecInfo.getSupportedTypes();
for (String type : types) {
if (type.equalsIgnoreCase(mimeType)) {
lastCodecInfo = codecInfo;
String name = lastCodecInfo.getName();
if (name != null) {
if (!name.equals("OMX.SEC.avc.enc")) {
return lastCodecInfo;
} else if (name.equals("OMX.SEC.AVC.Encoder")) {
return lastCodecInfo;
}
}
}
}
}
return lastCodecInfo;
}
/**
* 录制前的准备
*
* @throws IOException
*/
@Override
public void prepare() throws IOException {
mTrackIndex = -1;
mMuxerStarted = mIsEndOfStream = false;
// mediaFormat配置
final MediaFormat audioFormat = MediaFormat.createAudioFormat(MIME_TYPE, SAMPLE_RATE, 1);
audioFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
audioFormat.setInteger(MediaFormat.KEY_CHANNEL_MASK, AudioFormat.CHANNEL_IN_MONO);
audioFormat.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);
audioFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);
//
mMediaCodec = MediaCodec.createEncoderByType(MIME_TYPE);
mMediaCodec.configure(audioFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mMediaCodec.start();
if (mMediaEncoderListener != null) {
try {
mMediaEncoderListener.onPrepared(this);
} catch (final Exception e) {
LogUtils.e(TAG, "prepare:", e);
}
}
}
/**
* select the first codec that match a specific MIME type
* @param mimeType
* @return
*/
private static final MediaCodecInfo selectAudioCodec(final String mimeType) {
if (DEBUG) Log.v(TAG, "selectAudioCodec:");
MediaCodecInfo result = null;
// get the list of available codecs
final int numCodecs = MediaCodecList.getCodecCount();
for (int i = 0; i < numCodecs; i++) {
final MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
if (!codecInfo.isEncoder()) { // skipp decoder
continue;
}
final String[] types = codecInfo.getSupportedTypes();
for (int j = 0; j < types.length; j++) {
if (DEBUG) Log.i(TAG, "supportedType:" + codecInfo.getName() + ",MIME=" + types[j]);
if (types[j].equalsIgnoreCase(mimeType)) {
if (result == null) {
result = codecInfo;
return result;
}
}
}
}
return result;
}
private static int selectColorFormat(final MediaCodecInfo codecInfo, final String mimeType) {
int result = 0;
final MediaCodecInfo.CodecCapabilities caps;
try {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
caps = codecInfo.getCapabilitiesForType(mimeType);
} finally {
Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
}
for (int colorFormat : caps.colorFormats) {
if (isRecognizedVideoFormat(colorFormat)) {
result = colorFormat;
break;
}
}
if (result == 0) {
Log.e(TAG, "couldn't find a good color format for " + codecInfo.getName() + " / " + mimeType);
}
return result;
}
/**
* select the first codec that match a specific MIME type
* @param mimeType
* @return null if no codec matched
*/
protected static final MediaCodecInfo selectVideoCodec(final String mimeType) {
if (DEBUG) Log.v(TAG, "selectVideoCodec:");
// get the list of available codecs
final int numCodecs = MediaCodecList.getCodecCount();
for (int i = 0; i < numCodecs; i++) {
final MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
if (!codecInfo.isEncoder()) { // skipp decoder
continue;
}
// select first codec that match a specific MIME type and color format
final String[] types = codecInfo.getSupportedTypes();
for (int j = 0; j < types.length; j++) {
if (types[j].equalsIgnoreCase(mimeType)) {
if (DEBUG) Log.i(TAG, "codec:" + codecInfo.getName() + ",MIME=" + types[j]);
final int format = selectColorFormat(codecInfo, mimeType);
if (format > 0) {
return codecInfo;
}
}
}
}
return null;
}
private void prepareEncoder() {
MediaFormat format = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, mWidth, mHeight);
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
format.setInteger(MediaFormat.KEY_BIT_RATE, mBitRate);
format.setInteger(MediaFormat.KEY_FRAME_RATE, FRAME_RATE);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);
Log.d(TAG, "created video format: " + format);
try {
mEncoder = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_VIDEO_AVC);
} catch (IOException e) {
e.printStackTrace();
}
mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mSurface = mEncoder.createInputSurface();
Log.d(TAG, "created input surface: " + mSurface);
mEncoder.start();
}
/**
* MediaCodec を作成します.
*
* @return MediaCodec
* @throws IOException MediaCodec の作成に失敗した場合に発生
*/
private MediaCodec createMediaCodec() throws IOException {
MediaFormat format = MediaFormat.createAudioFormat("audio/mp4a-latm",
getSamplingRate(), getChannelCount());
format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
int audioProfile = MediaCodecInfo.CodecProfileLevel.AACObjectLC;
int sampleIndex = getFreqIdx(getSamplingRate());
int channelConfig = getChannelCount();
ByteBuffer csd = ByteBuffer.allocateDirect(2);
csd.put((byte) ((audioProfile << 3) | (sampleIndex >> 1)));
csd.position(1);
csd.put((byte) ((byte) ((sampleIndex << 7) & 0x80) | (channelConfig << 3)));
csd.flip();
format.setByteBuffer("csd-0", csd);
if (DEBUG) {
Log.d(TAG, "AACDecoder::createMediaCodec: " + format);
}
MediaCodec mediaCodec = MediaCodec.createDecoderByType("audio/mp4a-latm");
mediaCodec.configure(format, null, null, 0);
mediaCodec.start();
return mediaCodec;
}
@Override
public MediaFormat createVideoOutputFormat(MediaFormat inputFormat) {
int width = inputFormat.getInteger(MediaFormat.KEY_WIDTH);
int height = inputFormat.getInteger(MediaFormat.KEY_HEIGHT);
int targetLonger = mScale * 16 * 16;
int targetShorter = mScale * 16 * 9;
int longer, shorter, outWidth, outHeight;
if (width >= height) {
longer = width;
shorter = height;
outWidth = targetLonger;
outHeight = targetShorter;
} else {
shorter = width;
longer = height;
outWidth = targetShorter;
outHeight = targetLonger;
}
if (longer * 9 != shorter * 16) {
throw new OutputFormatUnavailableException("This video is not 16:9, and is not able to transcode. (" + width + "x" + height + ")");
}
if (shorter <= targetShorter) {
Log.d(TAG, "This video's height is less or equal to " + targetShorter + ", pass-through. (" + width + "x" + height + ")");
return null;
}
MediaFormat format = MediaFormat.createVideoFormat("video/avc", outWidth, outHeight);
// From Nexus 4 Camera in 720p
format.setInteger(MediaFormat.KEY_BIT_RATE, mVideoBitrate);
format.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 3);
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
return format;
}
private static boolean isRecognizedFormat(int colorFormat) {
switch (colorFormat) {
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar:
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420PackedPlanar:
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar:
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420PackedSemiPlanar:
case MediaCodecInfo.CodecCapabilities.COLOR_TI_FormatYUV420PackedSemiPlanar:
return true;
default:
return false;
}
}
/** Creates a {@link MediaFormat} using parameters for audio from the configuration */
private MediaFormat createAudioMediaFormat() {
MediaFormat format =
MediaFormat.createAudioFormat(AUDIO_MIME_TYPE, mAudioSampleRate,
mAudioChannelCount);
format.setInteger(
MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
format.setInteger(MediaFormat.KEY_BIT_RATE, mAudioBitRate);
return format;
}
public static MediaFormat getFormat () {
MediaFormat mMediaFormat = MediaFormat.createVideoFormat(AccessConstants.FORMAT,
AccessConstants.PHONE_WIDTH, AccessConstants.PHONE_HEIGHT);
mMediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, AccessConstants.BITRATE);
mMediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, AccessConstants.FPS);
mMediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
mMediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
return mMediaFormat;
}
@Override
public MediaFormat createAudioOutputFormat(MediaFormat inputFormat) {
if (mAudioBitrate == AUDIO_BITRATE_AS_IS || mAudioChannels == AUDIO_CHANNELS_AS_IS) return null;
// Use original sample rate, as resampling is not supported yet.
final MediaFormat format = MediaFormat.createAudioFormat(MediaFormatExtraConstants.MIMETYPE_AUDIO_AAC,
inputFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE), mAudioChannels);
format.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC);
format.setInteger(MediaFormat.KEY_BIT_RATE, mAudioBitrate);
return format;
}
public static int getProfile() {
int maxProfile = 0;
Log.d(LOG_TAG, "getProfile()->Build.VERSION.SDK_INT:" + String.valueOf(VERSION.SDK_INT));
if (VERSION.SDK_INT >= 16) {
int mediaCodecListCount = MediaCodecList.getCodecCount();
for (int i = 0; i < mediaCodecListCount; i++) {
MediaCodecInfo mediaCodecInfo = MediaCodecList.getCodecInfoAt(i);
if (!(mediaCodecInfo.isEncoder() || mediaCodecInfo.getName().startsWith("OMX.google") || mediaCodecInfo.getName().startsWith("OMX.TI."))) {
Log.d(LOG_TAG, "getProfile()->name:" + mediaCodecInfo.getName());
for (String type : mediaCodecInfo.getSupportedTypes()) {
if (type.contains("avc")) {
Log.d(LOG_TAG, "getProfile()->type:" + type);
try {
CodecCapabilities codecCapabilities = mediaCodecInfo.getCapabilitiesForType(type);
for (int colorFormat : codecCapabilities.colorFormats) {
Log.d(LOG_TAG, "getProfile()->Color Format: " + colorFormat + " " + colorFormatToString(colorFormat));
}
for (CodecProfileLevel codecProfileLevel : codecCapabilities.profileLevels) {
String level = "unknown type";
String sprofile = "unknown type";
Log.d(LOG_TAG, "getProfile()->Codec Profile Level:" + avcLevelToString(codecProfileLevel.level) + " profile:" + avcProfileToString(codecProfileLevel.profile));
if (codecProfileLevel.profile > maxProfile) {
maxProfile = codecProfileLevel.profile;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
Log.d(LOG_TAG, "getProfile()->Max profile:" + maxProfile + " " + avcProfileToString(maxProfile));
}
return maxProfile;
}
/**
* Get a list of supported android codec mimes.
*/
@CalledByNative
private static CodecInfo[] getCodecsInfo() {
Map<String, CodecInfo> CodecInfoMap = new HashMap<String, CodecInfo>();
int count = MediaCodecList.getCodecCount();
for (int i = 0; i < count; ++i) {
MediaCodecInfo info = MediaCodecList.getCodecInfoAt(i);
if (info.isEncoder()) {
continue;
}
boolean secureDecoderSupported = false;
String codecString = info.getName();
// ".secure" codecs sometimes crash instead of throwing on pre-JBMR2
// platforms, but this code isn't run on them anyway (MediaDrm
// unavailable) so we side-step the issue. http://crbug.com/314868
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
String secureCodecName = codecString + ".secure";
try {
MediaCodec secureCodec = MediaCodec.createByCodecName(secureCodecName);
if (secureCodec != null) {
secureDecoderSupported = true;
secureCodec.release();
}
} catch (Exception e) {
Log.e(TAG, "Failed to create " + secureCodecName);
}
}
String[] supportedTypes = info.getSupportedTypes();
for (int j = 0; j < supportedTypes.length; ++j) {
if (!CodecInfoMap.containsKey(supportedTypes[j]) || secureDecoderSupported) {
CodecInfoMap.put(supportedTypes[j], new CodecInfo(
supportedTypes[j], codecString, secureDecoderSupported));
}
}
}
return CodecInfoMap.values().toArray(
new CodecInfo[CodecInfoMap.size()]);
}
@Override
public void start() {
if(mSurfaceTexture!=null) {
mSurface = new Surface(mSurfaceTexture);
} else if (mDisplaySurfaceView != null) {
mSurface = mDisplaySurfaceView.getHolder().getSurface();
}
synchronized (mLuck_AvcDecoderAdvance) {
mAvcDecoderAdvance.init(mFilePath, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar, mSurface, otherSurface, mFrameListener);
mAvcDecoderAdvance.start();
}
}
private String findCoderForFormat(MediaFormat format, boolean findEncoder) {
String mimeType = format.getString(MediaFormat.KEY_MIME);
Iterator<MediaCodecInfo> iterator = new MediaCodecInfoIterator();
while (iterator.hasNext()) {
MediaCodecInfo codecInfo = iterator.next();
if (codecInfo.isEncoder() != findEncoder) continue;
if (Arrays.asList(codecInfo.getSupportedTypes()).contains(mimeType)) {
return codecInfo.getName();
}
}
return null;
}