Class EncoderUtil


  • public final class EncoderUtil
    extends Object
    Utility methods for MediaCodec encoders.
    • Field Detail

      • LEVEL_UNSET

        public static final int LEVEL_UNSET
        A value to indicate the encoding level is not set.
        See Also:
        Constant Field Values
    • Method Detail

      • getSupportedEncoders

        public static ImmutableList<MediaCodecInfo> getSupportedEncoders​(String mimeType)
        Returns a list of encoders that support the given mimeType, or an empty list if there is none.
      • getSupportedVideoMimeTypes

        public static ImmutableSet<String> getSupportedVideoMimeTypes()
        Returns a list of video MIME types that can be encoded.
      • isSizeSupported

        public static boolean isSizeSupported​(MediaCodecInfo encoderInfo,
                                              String mimeType,
                                              int width,
                                              int height)
        Returns whether the encoder supports the given resolution.
      • getSupportedResolution

        @Nullable
        public static Size getSupportedResolution​(MediaCodecInfo encoderInfo,
                                                  String mimeType,
                                                  int width,
                                                  int height)
        Finds an encoder's supported resolution from a given resolution.

        The input resolution is returned, if it (after aligning to the encoder's requirement) is supported by the encoder.

        The resolution will be adjusted to be within the encoder's range of supported resolutions, and will be aligned to the encoder's alignment requirement. The adjustment process takes into account the original aspect ratio. But the fixed resolution may not preserve the original aspect ratio, depending on the encoder's required size alignment.

        Parameters:
        encoderInfo - The MediaCodecInfo of the encoder.
        mimeType - The output MIME type.
        width - The original width.
        height - The original height.
        Returns:
        A supported resolution, or null if unable to find a fallback.
      • findHighestSupportedEncodingLevel

        public static int findHighestSupportedEncodingLevel​(MediaCodecInfo encoderInfo,
                                                            String mimeType,
                                                            int profile)
        Finds the highest supported encoding level given a profile.
        Parameters:
        encoderInfo - The encoderInfo.
        mimeType - The MIME type.
        profile - The encoding profile.
        Returns:
        The highest supported encoding level, as documented in MediaCodecInfo.CodecProfileLevel, or LEVEL_UNSET if the profile is not supported.
      • findCodecForFormat

        @Nullable
        public static String findCodecForFormat​(MediaFormat format,
                                                boolean isDecoder)
        Finds a MediaCodec that supports the MediaFormat, or null if none is found.
      • isBitrateModeSupported

        public static boolean isBitrateModeSupported​(MediaCodecInfo encoderInfo,
                                                     String mimeType,
                                                     int bitrateMode)
        Returns whether the bitrate mode is supported by the encoder.
      • isHardwareAccelerated

        public static boolean isHardwareAccelerated​(MediaCodecInfo encoderInfo,
                                                    String mimeType)
        Checks if a codec is hardware-accelerated.
      • isFeatureSupported

        public static boolean isFeatureSupported​(MediaCodecInfo encoderInfo,
                                                 String mimeType,
                                                 String featureName)
        Returns whether a given feature is supported.
      • getMaxSupportedInstances

        @RequiresApi(23)
        public static int getMaxSupportedInstances​(MediaCodecInfo encoderInfo,
                                                   String mimeType)
        Returns the number of max number of the supported concurrent codec instances.