Search in sources :

Example 31 with MediaCodecInfo

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

the class MediaCodecInfoBuilderTest method canCreateMediaCodecInfoForEncoder.

@Test
@Config(minSdk = Q)
public void canCreateMediaCodecInfoForEncoder() {
    CodecCapabilities codecCapabilities = MediaCodecInfoBuilder.CodecCapabilitiesBuilder.newBuilder().setMediaFormat(AAC_MEDIA_FORMAT).setIsEncoder(true).setProfileLevels(AAC_PROFILE_LEVELS).build();
    MediaCodecInfo mediaCodecInfo = MediaCodecInfoBuilder.newBuilder().setName(AAC_ENCODER_NAME).setIsEncoder(true).setIsVendor(true).setCapabilities(codecCapabilities).build();
    assertThat(mediaCodecInfo.getName()).isEqualTo(AAC_ENCODER_NAME);
    assertThat(mediaCodecInfo.isEncoder()).isTrue();
    assertThat(mediaCodecInfo.isVendor()).isTrue();
    assertThat(mediaCodecInfo.isSoftwareOnly()).isFalse();
    assertThat(mediaCodecInfo.isHardwareAccelerated()).isFalse();
    assertThat(mediaCodecInfo.getSupportedTypes()).asList().containsExactly(MIMETYPE_AUDIO_AAC);
    assertThat(mediaCodecInfo.getCapabilitiesForType(MIMETYPE_AUDIO_AAC)).isNotNull();
}
Also used : MediaCodecInfo(android.media.MediaCodecInfo) CodecCapabilities(android.media.MediaCodecInfo.CodecCapabilities) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 32 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;
            }
            // TODO: do we still need this?
            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 33 with MediaCodecInfo

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

the class AudioUtils method getAvailableEncoders.

// TODO: use MediaFormat.MIMETYPE_AUDIO_FLAC) on API>=21
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static List<String> getAvailableEncoders(String mime, int sampleRate) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        MediaFormat format = MediaFormatFactory.createMediaFormat(mime, sampleRate);
        MediaCodecList mcl = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
        String encoderAsStr = mcl.findEncoderForFormat(format);
        List<String> encoders = new ArrayList<>();
        for (MediaCodecInfo info : mcl.getCodecInfos()) {
            if (info.isEncoder()) {
                String name = info.getName();
                String infoAsStr = name + ": " + TextUtils.join(", ", info.getSupportedTypes());
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    infoAsStr += String.format(": %s/%s/%s/%s", info.isHardwareAccelerated(), info.isSoftwareOnly(), info.isAlias(), info.isVendor());
                }
                if (name.equals(encoderAsStr)) {
                    infoAsStr = '#' + infoAsStr;
                }
                encoders.add(infoAsStr);
            }
        }
        return encoders;
    }
    return Collections.emptyList();
}
Also used : MediaFormat(android.media.MediaFormat) MediaCodecInfo(android.media.MediaCodecInfo) MediaCodecList(android.media.MediaCodecList) ArrayList(java.util.ArrayList) TargetApi(android.annotation.TargetApi)

Example 34 with MediaCodecInfo

use of android.media.MediaCodecInfo in project edx-app-android by edx.

the class MediaCodecUtil method getMediaCodecInfo.

/**
 * Returns the best decoder and its capabilities for the given mimeType. If there's no decoder
 * returns null.
 */
private static synchronized Pair<MediaCodecInfo, CodecCapabilities> getMediaCodecInfo(String mimeType) {
    Pair<MediaCodecInfo, CodecCapabilities> result = codecs.get(mimeType);
    if (result != null) {
        return result;
    }
    int numberOfCodecs = MediaCodecList.getCodecCount();
    // Note: MediaCodecList is sorted by the framework such that the best decoders come first.
    for (int i = 0; i < numberOfCodecs; i++) {
        MediaCodecInfo info = MediaCodecList.getCodecInfoAt(i);
        String codecName = info.getName();
        if (!info.isEncoder() && isOmxCodec(codecName)) {
            String[] supportedTypes = info.getSupportedTypes();
            for (int j = 0; j < supportedTypes.length; j++) {
                String supportedType = supportedTypes[j];
                if (supportedType.equalsIgnoreCase(mimeType)) {
                    result = Pair.create(info, info.getCapabilitiesForType(supportedType));
                    codecs.put(mimeType, result);
                    return result;
                }
            }
        }
    }
    return null;
}
Also used : MediaCodecInfo(android.media.MediaCodecInfo) CodecCapabilities(android.media.MediaCodecInfo.CodecCapabilities)

Example 35 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<>();
    // 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<>();
                    // 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)

Aggregations

MediaCodecInfo (android.media.MediaCodecInfo)35 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