/* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.media; import android.util.Log; import android.media.MediaCodecInfo; import java.util.ArrayList; import java.util.Arrays; import java.util.Map; /** * Allows you to enumerate available codecs, each specified as a {@link MediaCodecInfo} object, * find a codec supporting a given format and query the capabilities * of a given codec. *
See {@link MediaCodecInfo} for sample usage.
*/
final public class MediaCodecList {
private static final String TAG = "MediaCodecList";
/**
* Count the number of available (regular) codecs.
*
* @deprecated Use {@link #getCodecInfos} instead.
*
* @see #REGULAR_CODECS
*/
public static final int getCodecCount() {
initCodecList();
return sRegularCodecInfos.length;
}
private static native final int native_getCodecCount();
/**
* Return the {@link MediaCodecInfo} object for the codec at
* the given {@code index} in the regular list.
*
* @deprecated Use {@link #getCodecInfos} instead.
*
* @see #REGULAR_CODECS
*/
public static final MediaCodecInfo getCodecInfoAt(int index) {
initCodecList();
if (index < 0 || index > sRegularCodecInfos.length) {
throw new IllegalArgumentException();
}
return sRegularCodecInfos[index];
}
/* package private */ static final Map
* Note: On {@link android.os.Build.VERSION_CODES#LOLLIPOP},
* {@code format} must not contain a {@linkplain MediaFormat#KEY_FRAME_RATE
* frame rate}. Use
*
* Note: On {@link android.os.Build.VERSION_CODES#LOLLIPOP},
* {@code format} must not contain a {@linkplain MediaFormat#KEY_FRAME_RATE
* frame rate}. Use
* format.setString(MediaFormat.KEY_FRAME_RATE, null)
* to clear any existing frame rate setting in the format.
*
* @see MediaCodecList.CodecCapabilities.isFormatSupported for format keys
* considered per android versions when evaluating suitable codecs.
*
* @param format A decoder media format with optional feature directives.
* @throws IllegalArgumentException if format is not a valid media format.
* @throws NullPointerException if format is null.
* @return the name of a decoder that supports the given format and feature
* requests, or {@code null} if no such codec has been found.
*/
public final String findDecoderForFormat(MediaFormat format) {
return findCodecForFormat(false /* encoder */, format);
}
/**
* Find an encoder supporting a given {@link MediaFormat} in the list
* of media-codecs.
*
* format.setString(MediaFormat.KEY_FRAME_RATE, null)
* to clear any existing frame rate setting in the format.
*
* @see MediaCodecList.CodecCapabilities.isFormatSupported for format keys
* considered per android versions when evaluating suitable codecs.
*
* @param format An encoder media format with optional feature directives.
* @throws IllegalArgumentException if format is not a valid media format.
* @throws NullPointerException if format is null.
* @return the name of an encoder that supports the given format and feature
* requests, or {@code null} if no such codec has been found.
*/
public final String findEncoderForFormat(MediaFormat format) {
return findCodecForFormat(true /* encoder */, format);
}
private String findCodecForFormat(boolean encoder, MediaFormat format) {
String mime = format.getString(MediaFormat.KEY_MIME);
for (MediaCodecInfo info: mCodecInfos) {
if (info.isEncoder() != encoder) {
continue;
}
try {
MediaCodecInfo.CodecCapabilities caps = info.getCapabilitiesForType(mime);
if (caps != null && caps.isFormatSupported(format)) {
return info.getName();
}
} catch (IllegalArgumentException e) {
// type is not supported
}
}
return null;
}
}