Java Code Examples for android.media.MediaCodecList#getCodecInfoAt()

The following examples show how to use android.media.MediaCodecList#getCodecInfoAt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MediaVideoEncoder.java    From In77Camera with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: VideoEncoder.java    From haven with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the first codec capable of encoding the specified MIME type, or
 * null if no match was found.
 */
private static MediaCodecInfo selectCodec(String mimeType)
{
    int numCodecs = MediaCodecList.getCodecCount();
    for (int i = 0; i < numCodecs; i++)
    {
        MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
        if (!codecInfo.isEncoder())
        {
            continue;
        }
        String[] types = codecInfo.getSupportedTypes();
        for (int j = 0; j < types.length; j++)
        {
            if (types[j].equalsIgnoreCase(mimeType))
            {
                return codecInfo;
            }
        }
    }
    return null;
}
 
Example 3
Source File: VideoController.java    From VideoCompressor with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: MediaController.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
@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;
}
 
Example 5
Source File: MediaAudioEncoder.java    From PLDroidShortVideo with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: Utils.java    From prettygoodmusicplayer with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Retrieve decodeable media types in the system
 * @return A set containing the supported media types
 */
private static Set<String> getSupportedTypes() {
	// These MediaCodecList methods are deprecated in API 21, but the newer
	// ones aren't supported in API < 21
	int numCodecs = MediaCodecList.getCodecCount();
	Set<String> supportedMediaTypes = new HashSet<>();

	for (int codec = 0; codec < numCodecs; codec++) {
		MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(codec);

		if (codecInfo.isEncoder()) {
			continue;
		}

		String[] codecTypes = codecInfo.getSupportedTypes();
		for (int type = 0; type < codecTypes.length; type++) {
			if (isMediaAudio(codecTypes[type]) && !supportedMediaTypes.contains(codecTypes[type])) {
				Log.d(TAG, codecTypes[type] + " is decodeable by " + codecInfo.getName());
				supportedMediaTypes.add(codecTypes[type]);
			}
		}
	}
	return supportedMediaTypes;
}
 
Example 7
Source File: MainActivity.java    From AndroidPlayground with MIT License 6 votes vote down vote up
private static MediaCodecInfo selectCodec(String mimeType) {
    int numCodecs = MediaCodecList.getCodecCount();
    for (int i = 0; i < numCodecs; i++) {
        MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);

        if (!codecInfo.isEncoder()) {
            continue;
        }

        String[] types = codecInfo.getSupportedTypes();
        for (int j = 0; j < types.length; j++) {
            if (types[j].equalsIgnoreCase(mimeType)) {
                return codecInfo;
            }
        }
    }
    return null;
}
 
Example 8
Source File: MediaCodecBridge.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 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()]);
}
 
Example 9
Source File: MediaVideoBufferEncoder.java    From AndroidUSBCamera with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
protected 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) {
               		mColorFormat = format;
               		return codecInfo;
               	}
               }
           }
       }
       return null;
   }
 
Example 10
Source File: MediaCodecUtil.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@Override
public android.media.MediaCodecInfo getCodecInfoAt(int index) {
  return MediaCodecList.getCodecInfoAt(index);
}
 
Example 11
Source File: MediaCodecListCompat.java    From Pix-Art-Messenger with GNU General Public License v3.0 4 votes vote down vote up
private static MediaCodecInfo getCodecInfoAt(int index) {
    return MediaCodecList.getCodecInfoAt(index);
}
 
Example 12
Source File: IjkMediaPlayer.java    From JZVideoDemo with MIT License 4 votes vote down vote up
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public String onMediaCodecSelect(IMediaPlayer mp, String mimeType, int profile, int level) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
        return null;

    if (TextUtils.isEmpty(mimeType))
        return null;

    Log.i(TAG, String.format(Locale.US, "onSelectCodec: mime=%s, profile=%d, level=%d", mimeType, profile, level));
    ArrayList<IjkMediaCodecInfo> candidateCodecList = new ArrayList<IjkMediaCodecInfo>();
    int numCodecs = MediaCodecList.getCodecCount();
    for (int i = 0; i < numCodecs; i++) {
        MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
        Log.d(TAG, String.format(Locale.US, "  found codec: %s", codecInfo.getName()));
        if (codecInfo.isEncoder())
            continue;

        String[] types = codecInfo.getSupportedTypes();
        if (types == null)
            continue;

        for (String type : types) {
            if (TextUtils.isEmpty(type))
                continue;

            Log.d(TAG, String.format(Locale.US, "    mime: %s", type));
            if (!type.equalsIgnoreCase(mimeType))
                continue;

            IjkMediaCodecInfo candidate = IjkMediaCodecInfo.setupCandidate(codecInfo, mimeType);
            if (candidate == null)
                continue;

            candidateCodecList.add(candidate);
            Log.i(TAG, String.format(Locale.US, "candidate codec: %s rank=%d", codecInfo.getName(), candidate.mRank));
            candidate.dumpProfileLevels(mimeType);
        }
    }

    if (candidateCodecList.isEmpty()) {
        return null;
    }

    IjkMediaCodecInfo bestCodec = candidateCodecList.get(0);

    for (IjkMediaCodecInfo codec : candidateCodecList) {
        if (codec.mRank > bestCodec.mRank) {
            bestCodec = codec;
        }
    }

    if (bestCodec.mRank < IjkMediaCodecInfo.RANK_LAST_CHANCE) {
        Log.w(TAG, String.format(Locale.US, "unaccetable codec: %s", bestCodec.mCodecInfo.getName()));
        return null;
    }

    Log.i(TAG, String.format(Locale.US, "selected codec: %s rank=%d", bestCodec.mCodecInfo.getName(), bestCodec.mRank));
    return bestCodec.mCodecInfo.getName();
}
 
Example 13
Source File: MediaCodecListCompat.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private static MediaCodecInfo getCodecInfoAt(int index) {
    return MediaCodecList.getCodecInfoAt(index);
}
 
Example 14
Source File: MediaCodecUtil.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
public android.media.MediaCodecInfo getCodecInfoAt(int index) {
  return MediaCodecList.getCodecInfoAt(index);
}
 
Example 15
Source File: CodecWrapper.java    From letv with Apache License 2.0 4 votes vote down vote up
public static int getCapbility() {
    int maxProfile = 0;
    int tsType = -1;
    Log.d(LOG_TAG, "getCapbility()->Build.VERSION.SDK_INT:" + String.valueOf(VERSION.SDK_INT));
    if (VERSION.SDK_INT < 16) {
        return -1;
    }
    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, "getCapbility()->name:" + mediaCodecInfo.getName());
            for (String type : mediaCodecInfo.getSupportedTypes()) {
                if (type.contains("avc")) {
                    Log.d(LOG_TAG, "getCapbility()->type:" + type);
                    try {
                        CodecCapabilities codecCapabilities = mediaCodecInfo.getCapabilitiesForType(type);
                        for (int colorFormat : codecCapabilities.colorFormats) {
                            Log.d(LOG_TAG, "getCapbility()->Color Format: " + colorFormat + " " + colorFormatToString(colorFormat));
                        }
                        for (CodecProfileLevel codecProfileLevel : codecCapabilities.profileLevels) {
                            String level = "unknown type";
                            String sprofile = "unknown type";
                            Log.d(LOG_TAG, "getCapbility()->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, "getCapbility()->Max profile:" + maxProfile + " " + avcProfileToString(maxProfile));
    if (maxProfile >= 8) {
        tsType = 16;
    }
    return tsType;
}
 
Example 16
Source File: IjkMediaPlayer.java    From IjkPlayerDemo with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public String onMediaCodecSelect(IMediaPlayer mp, String mimeType, int profile, int level) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
        return null;

    if (TextUtils.isEmpty(mimeType))
        return null;

    Log.i(TAG, String.format(Locale.US, "onSelectCodec: mime=%s, profile=%d, level=%d", mimeType, profile, level));
    ArrayList<IjkMediaCodecInfo> candidateCodecList = new ArrayList<IjkMediaCodecInfo>();
    int numCodecs = MediaCodecList.getCodecCount();
    for (int i = 0; i < numCodecs; i++) {
        MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
        Log.d(TAG, String.format(Locale.US, "  found codec: %s", codecInfo.getName()));
        if (codecInfo.isEncoder())
            continue;

        String[] types = codecInfo.getSupportedTypes();
        if (types == null)
            continue;

        for(String type: types) {
            if (TextUtils.isEmpty(type))
                continue;

            Log.d(TAG, String.format(Locale.US, "    mime: %s", type));
            if (!type.equalsIgnoreCase(mimeType))
                continue;

            IjkMediaCodecInfo candidate = IjkMediaCodecInfo.setupCandidate(codecInfo, mimeType);
            if (candidate == null)
                continue;

            candidateCodecList.add(candidate);
            Log.i(TAG, String.format(Locale.US, "candidate codec: %s rank=%d", codecInfo.getName(), candidate.mRank));
            candidate.dumpProfileLevels(mimeType);
        }
    }

    if (candidateCodecList.isEmpty()) {
        return null;
    }

    IjkMediaCodecInfo bestCodec = candidateCodecList.get(0);

    for (IjkMediaCodecInfo codec : candidateCodecList) {
        if (codec.mRank > bestCodec.mRank) {
            bestCodec = codec;
        }
    }

    if (bestCodec.mRank < IjkMediaCodecInfo.RANK_LAST_CHANCE) {
        Log.w(TAG, String.format(Locale.US, "unaccetable codec: %s", bestCodec.mCodecInfo.getName()));
        return null;
    }

    Log.i(TAG, String.format(Locale.US, "selected codec: %s rank=%d", bestCodec.mCodecInfo.getName(), bestCodec.mRank));
    return bestCodec.mCodecInfo.getName();
}
 
Example 17
Source File: MediaCodecVideoDecoder.java    From droidkit-webrtc with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static DecoderProperties findVp8HwDecoder() {
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
    return null; // MediaCodec.setParameters is missing.

  for (int i = 0; i < MediaCodecList.getCodecCount(); ++i) {
    MediaCodecInfo info = MediaCodecList.getCodecInfoAt(i);
    if (info.isEncoder()) {
      continue;
    }
    String name = null;
    for (String mimeType : info.getSupportedTypes()) {
      if (mimeType.equals(VP8_MIME_TYPE)) {
        name = info.getName();
        break;
      }
    }
    if (name == null) {
      continue;  // No VP8 support in this codec; try the next one.
    }
    Log.d(TAG, "Found candidate decoder " + name);
    CodecCapabilities capabilities =
        info.getCapabilitiesForType(VP8_MIME_TYPE);
    for (int colorFormat : capabilities.colorFormats) {
      Log.d(TAG, "   Color: 0x" + Integer.toHexString(colorFormat));
    }

    // Check if this is supported HW decoder
    for (String hwCodecPrefix : supportedHwCodecPrefixes) {
      if (!name.startsWith(hwCodecPrefix)) {
        continue;
      }
      // Check if codec supports either yuv420 or nv12
      for (int supportedColorFormat : supportedColorList) {
        for (int codecColorFormat : capabilities.colorFormats) {
          if (codecColorFormat == supportedColorFormat) {
            // Found supported HW VP8 decoder
            Log.d(TAG, "Found target decoder " + name +
                ". Color: 0x" + Integer.toHexString(codecColorFormat));
            return new DecoderProperties(name, codecColorFormat);
          }
        }
      }
    }
  }
  return null;  // No HW VP8 decoder.
}
 
Example 18
Source File: MediaCodecUtil.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
@Override
public android.media.MediaCodecInfo getCodecInfoAt(int index) {
  return MediaCodecList.getCodecInfoAt(index);
}
 
Example 19
Source File: IjkMediaPlayer.java    From talk-android with MIT License 4 votes vote down vote up
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public String onMediaCodecSelect(IMediaPlayer mp, String mimeType, int profile, int level) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
        return null;

    if (TextUtils.isEmpty(mimeType))
        return null;

    Log.i(TAG, String.format(Locale.US, "onSelectCodec: mime=%s, profile=%d, level=%d", mimeType, profile, level));
    ArrayList<IjkMediaCodecInfo> candidateCodecList = new ArrayList<IjkMediaCodecInfo>();
    int numCodecs = MediaCodecList.getCodecCount();
    for (int i = 0; i < numCodecs; i++) {
        MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
        Log.d(TAG, String.format(Locale.US, "  found codec: %s", codecInfo.getName()));
        if (codecInfo.isEncoder())
            continue;

        String[] types = codecInfo.getSupportedTypes();
        if (types == null)
            continue;

        for(String type: types) {
            if (TextUtils.isEmpty(type))
                continue;

            Log.d(TAG, String.format(Locale.US, "    mime: %s", type));
            if (!type.equalsIgnoreCase(mimeType))
                continue;

            IjkMediaCodecInfo candidate = IjkMediaCodecInfo.setupCandidate(codecInfo, mimeType);
            if (candidate == null)
                continue;

            candidateCodecList.add(candidate);
            Log.i(TAG, String.format(Locale.US, "candidate codec: %s rank=%d", codecInfo.getName(), candidate.mRank));
            candidate.dumpProfileLevels(mimeType);
        }
    }

    if (candidateCodecList.isEmpty()) {
        return null;
    }

    IjkMediaCodecInfo bestCodec = candidateCodecList.get(0);

    for (IjkMediaCodecInfo codec : candidateCodecList) {
        if (codec.mRank > bestCodec.mRank) {
            bestCodec = codec;
        }
    }

    if (bestCodec.mRank < IjkMediaCodecInfo.RANK_LAST_CHANCE) {
        Log.w(TAG, String.format(Locale.US, "unaccetable codec: %s", bestCodec.mCodecInfo.getName()));
        return null;
    }

    Log.i(TAG, String.format(Locale.US, "selected codec: %s rank=%d", bestCodec.mCodecInfo.getName(), bestCodec.mRank));
    return bestCodec.mCodecInfo.getName();
}
 
Example 20
Source File: MediaCodecListCompat.java    From android-transcoder with Apache License 2.0 4 votes vote down vote up
private static MediaCodecInfo getCodecInfoAt(int index) {
    return MediaCodecList.getCodecInfoAt(index);
}