1.根据info创建对应的硬软编码器(vp8还是H264),优先选择硬编码
public VideoEncoder createEncoder(VideoCodecInfo info) {
final VideoEncoder softwareEncoder = softwareVideoEncoderFactory.createEncoder(info);
final VideoEncoder hardwareEncoder = hardwareVideoEncoderFactory.createEncoder(info);
if (hardwareEncoder != null && softwareEncoder != null) {
// Both hardware and software supported, wrap it in a software fallback
return new VideoEncoderFallback(
/* fallback= */ softwareEncoder, /* primary= */ hardwareEncoder);
}
return hardwareEncoder != null ? hardwareEncoder : softwareEncoder;
}
public VideoEncoder createEncoder(VideoCodecInfo input) {
// HW encoding is not supported below Android Kitkat.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return null;
}
VideoCodecMimeType type = VideoCodecMimeType.valueOf(input.getName());
MediaCodecInfo info = findCodecForType(type);
if (info == null) {
return null;
}
}
2.判断是否VideoCodecMimeType type类型的编码器
enum VideoCodecMimeType {
VP8("video/x-vnd.on2.vp8"),
VP9("video/x-vnd.on2.vp9"),
H264("video/avc"),
AV1("video/av01");
}
private MediaCodecInfo findCodecForType(VideoCodecMimeType type) {
for (int i = 0; i < MediaCodecList.getCodecCount(); ++i) {
MediaCodecInfo info = null;
try {
info = MediaCodecList.getCodecInfoAt(i);
} catch (IllegalArgumentException e) {
Logging.e(TAG, "Cannot retrieve encoder codec info", e);
}
if (info == null || !info.isEncoder()) {
continue;
}
if (isSupportedCodec(info, type)) {
return info;
}
}
return null; // No support for this type.
}
private boolean isSupportedCodec(MediaCodecInfo info, VideoCodecMimeType type) {
if (!MediaCodecUtils.codecSupportsType(info, type)) {
return false;
}
// Check for a supported color format.
if (MediaCodecUtils.selectColorFormat(
MediaCodecUtils.ENCODER_COLOR_FORMATS, info.getCapabilitiesForType(type.mimeType()))
== null) {
return false;
}
return isHardwareSupportedInCurrentSdk(info, type) && isMediaCodecAllowed(info);
}
3.根据supportedH264HwCodecPrefixes里面的型号判断当前设备是否支持硬编码
static final String[] supportedH264HwCodecPrefixes = new String[]{"OMX.qcom.",
"OMX.Exynos.", "OMX.MTK.", "OMX.IMG.TOPAZ.", "OMX.k3.", "OMX.amlogic.",
"OMX.rk.", "OMX.MS."};
private boolean isHardwareSupportedInCurrentSdkH264(MediaCodecInfo info) {
// First, H264 hardware might perform poorly on this model.
if (H264_HW_EXCEPTION_MODELS.contains(Build.MODEL)) {
Logging.e("Log","isHardwareSupportedInCurrentSdkH264 HardwareSupported false");
return false;
}
String name = info.getName();
Logging.i("Log","isHardwareSupportedInCurrentSdkH264 HardwareSupported:" + name);
for (int i =0;i<MediaCodecUtils.supportedH264HwCodecPrefixes.length;i++){
if (name.startsWith(MediaCodecUtils.supportedH264HwCodecPrefixes[i])){
Logging.i("_Log","isHardwareSupportedInCurrentSdkH264 HardwareSupported true");
return true;
}
}
Logging.i("Log","isHardwareSupportedInCurrentSdkH264 HardwareSupported false");
return false;
}
|