Search in sources :

Example 1 with CodecCapabilities

use of android.media.MediaCodecInfo.CodecCapabilities 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 2 with CodecCapabilities

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

the class MediaCodecUtil method isH264ProfileSupported.

/**
 * @param profile An AVC profile constant from {@link CodecProfileLevel}.
 * @param level An AVC profile level from {@link CodecProfileLevel}.
 * @return Whether the specified profile is supported at the specified level.
 */
public static boolean isH264ProfileSupported(int profile, int level) {
    Pair<MediaCodecInfo, CodecCapabilities> info = getMediaCodecInfo(MimeTypes.VIDEO_H264);
    if (info == null) {
        return false;
    }
    CodecCapabilities capabilities = info.second;
    for (int i = 0; i < capabilities.profileLevels.length; i++) {
        CodecProfileLevel profileLevel = capabilities.profileLevels[i];
        if (profileLevel.profile == profile && profileLevel.level >= level) {
            return true;
        }
    }
    return false;
}
Also used : CodecProfileLevel(android.media.MediaCodecInfo.CodecProfileLevel) MediaCodecInfo(android.media.MediaCodecInfo) CodecCapabilities(android.media.MediaCodecInfo.CodecCapabilities)

Example 3 with CodecCapabilities

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

the class MediaCodecUtil method maxH264DecodableFrameSize.

/**
 * @return the maximum frame size for an H264 stream that can be decoded on the device.
 */
public static int maxH264DecodableFrameSize() {
    Pair<MediaCodecInfo, CodecCapabilities> info = getMediaCodecInfo(MimeTypes.VIDEO_H264);
    if (info == null) {
        return 0;
    }
    int maxH264DecodableFrameSize = 0;
    CodecCapabilities capabilities = info.second;
    for (int i = 0; i < capabilities.profileLevels.length; i++) {
        CodecProfileLevel profileLevel = capabilities.profileLevels[i];
        maxH264DecodableFrameSize = Math.max(avcLevelToMaxFrameSize(profileLevel.level), maxH264DecodableFrameSize);
    }
    return maxH264DecodableFrameSize;
}
Also used : CodecProfileLevel(android.media.MediaCodecInfo.CodecProfileLevel) MediaCodecInfo(android.media.MediaCodecInfo) CodecCapabilities(android.media.MediaCodecInfo.CodecCapabilities)

Example 4 with CodecCapabilities

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

the class MediaCodecInfoBuilderTest method canCreateMediaCodecInfoForDecoder.

@Test
@Config(minSdk = Q)
public void canCreateMediaCodecInfoForDecoder() {
    CodecCapabilities codecCapabilities = MediaCodecInfoBuilder.CodecCapabilitiesBuilder.newBuilder().setMediaFormat(VP9_MEDIA_FORMAT).setProfileLevels(VP9_PROFILE_LEVELS).setColorFormats(VP9_COLOR_FORMATS).build();
    MediaCodecInfo mediaCodecInfo = MediaCodecInfoBuilder.newBuilder().setName(VP9_DECODER_NAME).setIsSoftwareOnly(true).setIsHardwareAccelerated(true).setCapabilities(codecCapabilities).build();
    assertThat(mediaCodecInfo.getName()).isEqualTo(VP9_DECODER_NAME);
    assertThat(mediaCodecInfo.isEncoder()).isFalse();
    assertThat(mediaCodecInfo.isVendor()).isFalse();
    assertThat(mediaCodecInfo.isSoftwareOnly()).isTrue();
    assertThat(mediaCodecInfo.isHardwareAccelerated()).isTrue();
    assertThat(mediaCodecInfo.getSupportedTypes()).asList().containsExactly(MIMETYPE_VIDEO_VP9);
    assertThat(mediaCodecInfo.getCapabilitiesForType(MIMETYPE_VIDEO_VP9)).isNotNull();
}
Also used : MediaCodecInfo(android.media.MediaCodecInfo) CodecCapabilities(android.media.MediaCodecInfo.CodecCapabilities) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 5 with CodecCapabilities

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

the class MediaCodecInfoBuilderTest method canCreateAudioEncoderCapabilities.

@Test
@Config(minSdk = Q)
public void canCreateAudioEncoderCapabilities() {
    CodecCapabilities codecCapabilities = MediaCodecInfoBuilder.CodecCapabilitiesBuilder.newBuilder().setMediaFormat(AAC_MEDIA_FORMAT).setIsEncoder(true).setProfileLevels(AAC_PROFILE_LEVELS).build();
    assertThat(codecCapabilities.getMimeType()).isEqualTo(MIMETYPE_AUDIO_AAC);
    assertThat(codecCapabilities.getAudioCapabilities()).isNotNull();
    assertThat(codecCapabilities.getVideoCapabilities()).isNull();
    assertThat(codecCapabilities.getEncoderCapabilities()).isNotNull();
    assertThat(codecCapabilities.isFeatureSupported(CodecCapabilities.FEATURE_DynamicTimestamp)).isTrue();
    assertThat(codecCapabilities.isFeatureSupported(CodecCapabilities.FEATURE_FrameParsing)).isFalse();
    assertThat(codecCapabilities.profileLevels).hasLength(AAC_PROFILE_LEVELS.length);
    assertThat(codecCapabilities.profileLevels).isEqualTo(AAC_PROFILE_LEVELS);
}
Also used : CodecCapabilities(android.media.MediaCodecInfo.CodecCapabilities) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Aggregations

CodecCapabilities (android.media.MediaCodecInfo.CodecCapabilities)20 MediaCodecInfo (android.media.MediaCodecInfo)11 Test (org.junit.Test)10 Config (org.robolectric.annotation.Config)8 CodecProfileLevel (android.media.MediaCodecInfo.CodecProfileLevel)6 SparseIntArray (android.util.SparseIntArray)4 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 TreeMap (java.util.TreeMap)4 Nullable (androidx.annotation.Nullable)2 MediaCodecInfo (com.google.android.exoplayer2.mediacodec.MediaCodecInfo)2 SuppressLint (android.annotation.SuppressLint)1 TargetApi (android.annotation.TargetApi)1 SurfaceTexture (android.graphics.SurfaceTexture)1 MediaFormat (android.media.MediaFormat)1 Handler (android.os.Handler)1 Looper (android.os.Looper)1 SystemClock (android.os.SystemClock)1 Surface (android.view.Surface)1 ApplicationProvider (androidx.test.core.app.ApplicationProvider)1