Search in sources :

Example 11 with MediaCodecInfo

use of android.media.MediaCodecInfo in project platform_frameworks_base by android.

the class MediaCodecList method initCodecList.

private static final void initCodecList() {
    synchronized (sInitLock) {
        if (sRegularCodecInfos == null) {
            int count = native_getCodecCount();
            ArrayList<MediaCodecInfo> regulars = new ArrayList<MediaCodecInfo>();
            ArrayList<MediaCodecInfo> all = new ArrayList<MediaCodecInfo>();
            for (int index = 0; index < count; index++) {
                try {
                    MediaCodecInfo info = getNewCodecInfoAt(index);
                    all.add(info);
                    info = info.makeRegular();
                    if (info != null) {
                        regulars.add(info);
                    }
                } catch (Exception e) {
                    Log.e(TAG, "Could not get codec capabilities", e);
                }
            }
            sRegularCodecInfos = regulars.toArray(new MediaCodecInfo[regulars.size()]);
            sAllCodecInfos = all.toArray(new MediaCodecInfo[all.size()]);
        }
    }
}
Also used : MediaCodecInfo(android.media.MediaCodecInfo) ArrayList(java.util.ArrayList)

Example 12 with MediaCodecInfo

use of android.media.MediaCodecInfo in project speechutils by Kaljurand.

the class AudioUtils method getEncoderNamesForType.

/**
     * Maps the given mime type to a list of names of suitable codecs.
     * Only OMX-codecs are considered.
     */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static List<String> getEncoderNamesForType(String mime) {
    LinkedList<String> names = new LinkedList<>();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        int n = MediaCodecList.getCodecCount();
        for (int i = 0; i < n; ++i) {
            MediaCodecInfo info = MediaCodecList.getCodecInfoAt(i);
            if (!info.isEncoder()) {
                continue;
            }
            if (!info.getName().startsWith("OMX.")) {
                // Unfortunately for legacy reasons, "AACEncoder", a
                // non OMX component had to be in this list for the video
                // editor code to work... but it cannot actually be instantiated
                // using MediaCodec.
                Log.i("skipping '" + info.getName() + "'.");
                continue;
            }
            String[] supportedTypes = info.getSupportedTypes();
            for (String type : supportedTypes) {
                if (type.equalsIgnoreCase(mime)) {
                    names.push(info.getName());
                    break;
                }
            }
        }
    }
    // TODO: maybe return null or throw exception
    return names;
}
Also used : MediaCodecInfo(android.media.MediaCodecInfo) LinkedList(java.util.LinkedList) TargetApi(android.annotation.TargetApi)

Example 13 with MediaCodecInfo

use of android.media.MediaCodecInfo in project android_frameworks_base by ResurrectionRemix.

the class MediaCodecList method initCodecList.

private static final void initCodecList() {
    synchronized (sInitLock) {
        if (sRegularCodecInfos == null) {
            int count = native_getCodecCount();
            ArrayList<MediaCodecInfo> regulars = new ArrayList<MediaCodecInfo>();
            ArrayList<MediaCodecInfo> all = new ArrayList<MediaCodecInfo>();
            for (int index = 0; index < count; index++) {
                try {
                    MediaCodecInfo info = getNewCodecInfoAt(index);
                    all.add(info);
                    info = info.makeRegular();
                    if (info != null) {
                        regulars.add(info);
                    }
                } catch (Exception e) {
                    Log.e(TAG, "Could not get codec capabilities", e);
                }
            }
            sRegularCodecInfos = regulars.toArray(new MediaCodecInfo[regulars.size()]);
            sAllCodecInfos = all.toArray(new MediaCodecInfo[all.size()]);
        }
    }
}
Also used : MediaCodecInfo(android.media.MediaCodecInfo) ArrayList(java.util.ArrayList)

Example 14 with MediaCodecInfo

use of android.media.MediaCodecInfo in project android_frameworks_base by ResurrectionRemix.

the class CpuVideoTrackDecoder method findDecoderCodec.

/**
     * Looks for a codec with the specified requirements.
     *
     * The set of codecs will be filtered down to those that meet the following requirements:
     * <ol>
     *   <li>The codec is a decoder.</li>
     *   <li>The codec can decode a video of the specified format.</li>
     *   <li>The codec can decode to one of the specified color formats.</li>
     * </ol>
     * If multiple codecs are found, the one with the preferred color-format is taken. Color format
     * preference is determined by the order of their appearance in the color format array.
     *
     * @param format The format the codec must decode.
     * @param requiredColorFormats Array of target color spaces ordered by preference.
     * @return A codec that meets the requirements, or null if no such codec was found.
     */
private static MediaCodec findDecoderCodec(MediaFormat format, int[] requiredColorFormats) {
    TreeMap<Integer, String> candidateCodecs = new TreeMap<Integer, String>();
    SparseIntArray colorPriorities = intArrayToPriorityMap(requiredColorFormats);
    for (int i = 0; i < MediaCodecList.getCodecCount(); ++i) {
        // Get next codec
        MediaCodecInfo info = MediaCodecList.getCodecInfoAt(i);
        // Check that this is a decoder
        if (info.isEncoder()) {
            continue;
        }
        // Check if this codec can decode the video in question
        String requiredType = format.getString(MediaFormat.KEY_MIME);
        String[] supportedTypes = info.getSupportedTypes();
        Set<String> typeSet = new HashSet<String>(Arrays.asList(supportedTypes));
        // Check if it can decode to one of the required color formats
        if (typeSet.contains(requiredType)) {
            CodecCapabilities capabilities = info.getCapabilitiesForType(requiredType);
            for (int supportedColorFormat : capabilities.colorFormats) {
                if (colorPriorities.indexOfKey(supportedColorFormat) >= 0) {
                    int priority = colorPriorities.get(supportedColorFormat);
                    candidateCodecs.put(priority, info.getName());
                }
            }
        }
    }
    // Pick the best codec (with the highest color priority)
    if (candidateCodecs.isEmpty()) {
        return null;
    } else {
        String bestCodec = candidateCodecs.firstEntry().getValue();
        try {
            return MediaCodec.createByCodecName(bestCodec);
        } catch (IOException e) {
            throw new RuntimeException("failed to create codec for " + bestCodec, e);
        }
    }
}
Also used : IOException(java.io.IOException) TreeMap(java.util.TreeMap) SparseIntArray(android.util.SparseIntArray) MediaCodecInfo(android.media.MediaCodecInfo) CodecCapabilities(android.media.MediaCodecInfo.CodecCapabilities) HashSet(java.util.HashSet)

Example 15 with MediaCodecInfo

use of android.media.MediaCodecInfo in project android_frameworks_base by crdroidandroid.

the class CpuVideoTrackDecoder method findDecoderCodec.

/**
     * Looks for a codec with the specified requirements.
     *
     * The set of codecs will be filtered down to those that meet the following requirements:
     * <ol>
     *   <li>The codec is a decoder.</li>
     *   <li>The codec can decode a video of the specified format.</li>
     *   <li>The codec can decode to one of the specified color formats.</li>
     * </ol>
     * If multiple codecs are found, the one with the preferred color-format is taken. Color format
     * preference is determined by the order of their appearance in the color format array.
     *
     * @param format The format the codec must decode.
     * @param requiredColorFormats Array of target color spaces ordered by preference.
     * @return A codec that meets the requirements, or null if no such codec was found.
     */
private static MediaCodec findDecoderCodec(MediaFormat format, int[] requiredColorFormats) {
    TreeMap<Integer, String> candidateCodecs = new TreeMap<Integer, String>();
    SparseIntArray colorPriorities = intArrayToPriorityMap(requiredColorFormats);
    for (int i = 0; i < MediaCodecList.getCodecCount(); ++i) {
        // Get next codec
        MediaCodecInfo info = MediaCodecList.getCodecInfoAt(i);
        // Check that this is a decoder
        if (info.isEncoder()) {
            continue;
        }
        // Check if this codec can decode the video in question
        String requiredType = format.getString(MediaFormat.KEY_MIME);
        String[] supportedTypes = info.getSupportedTypes();
        Set<String> typeSet = new HashSet<String>(Arrays.asList(supportedTypes));
        // Check if it can decode to one of the required color formats
        if (typeSet.contains(requiredType)) {
            CodecCapabilities capabilities = info.getCapabilitiesForType(requiredType);
            for (int supportedColorFormat : capabilities.colorFormats) {
                if (colorPriorities.indexOfKey(supportedColorFormat) >= 0) {
                    int priority = colorPriorities.get(supportedColorFormat);
                    candidateCodecs.put(priority, info.getName());
                }
            }
        }
    }
    // Pick the best codec (with the highest color priority)
    if (candidateCodecs.isEmpty()) {
        return null;
    } else {
        String bestCodec = candidateCodecs.firstEntry().getValue();
        try {
            return MediaCodec.createByCodecName(bestCodec);
        } catch (IOException e) {
            throw new RuntimeException("failed to create codec for " + bestCodec, e);
        }
    }
}
Also used : IOException(java.io.IOException) TreeMap(java.util.TreeMap) SparseIntArray(android.util.SparseIntArray) MediaCodecInfo(android.media.MediaCodecInfo) CodecCapabilities(android.media.MediaCodecInfo.CodecCapabilities) HashSet(java.util.HashSet)

Aggregations

MediaCodecInfo (android.media.MediaCodecInfo)15 ArrayList (java.util.ArrayList)8 HashSet (java.util.HashSet)6 TargetApi (android.annotation.TargetApi)4 CodecCapabilities (android.media.MediaCodecInfo.CodecCapabilities)4 SparseIntArray (android.util.SparseIntArray)4 IOException (java.io.IOException)4 TreeMap (java.util.TreeMap)4 SuppressLint (android.annotation.SuppressLint)2 MediaCodecList (android.media.MediaCodecList)2 NonNull (android.support.annotation.NonNull)2 JSONObject (org.json.JSONObject)2 MediaFormat (android.media.MediaFormat)1 LinkedList (java.util.LinkedList)1 ComplexElement (org.acra.model.ComplexElement)1 JSONArray (org.json.JSONArray)1