Search in sources :

Example 1 with MediaCodecInfo

use of android.media.MediaCodecInfo in project acra by ACRA.

the class MediaCodecListCollector method collectCapabilitiesForType.

/**
     * Retrieve capabilities (ColorFormats and CodecProfileLevels) for a
     * specific codec type.
     *
     * @param codecInfo the currently inspected codec
     * @param type      supported type to collect
     * @return the color formats and codec profile levels
     * available for a specific codec type.
     */
@NonNull
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private JSONObject collectCapabilitiesForType(@NonNull final MediaCodecInfo codecInfo, @NonNull String type) throws JSONException {
    final JSONObject result = new JSONObject();
    final MediaCodecInfo.CodecCapabilities codecCapabilities = codecInfo.getCapabilitiesForType(type);
    // Color Formats
    final int[] colorFormats = codecCapabilities.colorFormats;
    if (colorFormats.length > 0) {
        JSONArray colorFormatsJson = new JSONArray();
        for (int colorFormat : colorFormats) {
            colorFormatsJson.put(mColorFormatValues.get(colorFormat));
        }
        result.put("colorFormats", colorFormatsJson);
    }
    final CodecType codecType = identifyCodecType(codecInfo);
    // Profile Levels
    final MediaCodecInfo.CodecProfileLevel[] codecProfileLevels = codecCapabilities.profileLevels;
    if (codecProfileLevels.length > 0) {
        JSONArray profileLevels = new JSONArray();
        for (MediaCodecInfo.CodecProfileLevel codecProfileLevel : codecProfileLevels) {
            final int profileValue = codecProfileLevel.profile;
            final int levelValue = codecProfileLevel.level;
            if (codecType == null) {
                // Unknown codec
                profileLevels.put(profileValue + '-' + levelValue);
                break;
            }
            switch(codecType) {
                case AVC:
                    profileLevels.put(profileValue + mAVCProfileValues.get(profileValue) + '-' + mAVCLevelValues.get(levelValue));
                    break;
                case H263:
                    profileLevels.put(mH263ProfileValues.get(profileValue) + '-' + mH263LevelValues.get(levelValue));
                    break;
                case MPEG4:
                    profileLevels.put(mMPEG4ProfileValues.get(profileValue) + '-' + mMPEG4LevelValues.get(levelValue));
                    break;
                case AAC:
                    profileLevels.put(mAACProfileValues.get(profileValue));
                    break;
                default:
                    break;
            }
        }
        result.put("profileLevels", profileLevels);
    }
    return result;
}
Also used : JSONObject(org.json.JSONObject) MediaCodecInfo(android.media.MediaCodecInfo) JSONArray(org.json.JSONArray) NonNull(android.support.annotation.NonNull) TargetApi(android.annotation.TargetApi)

Example 2 with MediaCodecInfo

use of android.media.MediaCodecInfo in project acra by ACRA.

the class MediaCodecListCollector method collectMediaCodecList.

/**
     * Builds an Element describing the list of available codecs on the device
     * with their capabilities (supported Color Formats, Codec Profiles et
     * Levels).
     *
     * @return The media codecs information
     */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@NonNull
private Element collectMediaCodecList() throws JSONException {
    prepare();
    final MediaCodecInfo[] infos;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        //noinspection deprecation
        final int codecCount = MediaCodecList.getCodecCount();
        infos = new MediaCodecInfo[codecCount];
        for (int codecIdx = 0; codecIdx < codecCount; codecIdx++) {
            //noinspection deprecation
            infos[codecIdx] = MediaCodecList.getCodecInfoAt(codecIdx);
        }
    } else {
        infos = new MediaCodecList(MediaCodecList.ALL_CODECS).getCodecInfos();
    }
    final ComplexElement result = new ComplexElement();
    for (int i = 0; i < infos.length; i++) {
        final MediaCodecInfo codecInfo = infos[i];
        JSONObject codec = new JSONObject();
        final String[] supportedTypes = codecInfo.getSupportedTypes();
        codec.put("name", codecInfo.getName()).put("isEncoder", codecInfo.isEncoder());
        JSONObject supportedTypesJson = new JSONObject();
        for (String type : supportedTypes) {
            supportedTypesJson.put(type, collectCapabilitiesForType(codecInfo, type));
        }
        codec.put("supportedTypes", supportedTypesJson);
        result.put(String.valueOf(i), codec);
    }
    return result;
}
Also used : JSONObject(org.json.JSONObject) MediaCodecInfo(android.media.MediaCodecInfo) MediaCodecList(android.media.MediaCodecList) ComplexElement(org.acra.model.ComplexElement) NonNull(android.support.annotation.NonNull) TargetApi(android.annotation.TargetApi)

Example 3 with MediaCodecInfo

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

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 4 with MediaCodecInfo

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

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 5 with MediaCodecInfo

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

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)

Aggregations

MediaCodecInfo (android.media.MediaCodecInfo)33 CodecCapabilities (android.media.MediaCodecInfo.CodecCapabilities)11 ArrayList (java.util.ArrayList)10 HashSet (java.util.HashSet)6 TargetApi (android.annotation.TargetApi)5 Test (org.junit.Test)5 Config (org.robolectric.annotation.Config)5 MediaCodecList (android.media.MediaCodecList)4 SparseIntArray (android.util.SparseIntArray)4 IOException (java.io.IOException)4 TreeMap (java.util.TreeMap)4 SuppressLint (android.annotation.SuppressLint)3 MediaFormat (android.media.MediaFormat)3 CodecProfileLevel (android.media.MediaCodecInfo.CodecProfileLevel)2 NonNull (android.support.annotation.NonNull)2 JSONObject (org.json.JSONObject)2 Nullable (androidx.annotation.Nullable)1 Format (com.google.android.exoplayer2.Format)1 ImmutableList (com.google.common.collect.ImmutableList)1 LinkedList (java.util.LinkedList)1