Search in sources :

Example 6 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 7 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 8 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)

Example 9 with MediaCodecInfo

use of android.media.MediaCodecInfo in project libstreaming by fyhertz.

the class CodecManager method findEncodersForMimeType.

/**
	 * Lists all encoders that claim to support a color format that we know how to use.
	 * @return A list of those encoders
	 */
@SuppressLint("NewApi")
public static synchronized Codec[] findEncodersForMimeType(String mimeType) {
    if (sEncoders != null)
        return sEncoders;
    ArrayList<Codec> encoders = new ArrayList<Codec>();
    // We loop through the encoders, apparently this can take up to a sec (testes on a GS3)
    for (int j = MediaCodecList.getCodecCount() - 1; j >= 0; j--) {
        MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(j);
        if (!codecInfo.isEncoder())
            continue;
        String[] types = codecInfo.getSupportedTypes();
        for (int i = 0; i < types.length; i++) {
            if (types[i].equalsIgnoreCase(mimeType)) {
                try {
                    MediaCodecInfo.CodecCapabilities capabilities = codecInfo.getCapabilitiesForType(mimeType);
                    Set<Integer> formats = new HashSet<Integer>();
                    // And through the color formats supported
                    for (int k = 0; k < capabilities.colorFormats.length; k++) {
                        int format = capabilities.colorFormats[k];
                        for (int l = 0; l < SUPPORTED_COLOR_FORMATS.length; l++) {
                            if (format == SUPPORTED_COLOR_FORMATS[l]) {
                                formats.add(format);
                            }
                        }
                    }
                    Codec codec = new Codec(codecInfo.getName(), (Integer[]) formats.toArray(new Integer[formats.size()]));
                    encoders.add(codec);
                } catch (Exception e) {
                    Log.wtf(TAG, e);
                }
            }
        }
    }
    sEncoders = (Codec[]) encoders.toArray(new Codec[encoders.size()]);
    return sEncoders;
}
Also used : MediaCodecInfo(android.media.MediaCodecInfo) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint) HashSet(java.util.HashSet) SuppressLint(android.annotation.SuppressLint)

Example 10 with MediaCodecInfo

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

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