Rabbit-R1/original r1/java/sources/org/webrtc/HardwareVideoEncoderFactory.java
2024-05-21 17:08:36 -04:00

209 lines
9.7 KiB
Java

package org.webrtc;
import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.os.Build;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.webrtc.EglBase;
import org.webrtc.EglBase14;
/* loaded from: classes3.dex */
public class HardwareVideoEncoderFactory implements VideoEncoderFactory {
private static final List<String> H264_HW_EXCEPTION_MODELS = Arrays.asList("SAMSUNG-SGH-I337", "Nexus 7", "Nexus 4");
private static final int QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_L_MS = 15000;
private static final int QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_M_MS = 20000;
private static final int QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_N_MS = 15000;
private static final String TAG = "HardwareVideoEncoderFactory";
private final Predicate<MediaCodecInfo> codecAllowedPredicate;
private final boolean enableH264HighProfile;
private final boolean enableIntelVp8Encoder;
private final EglBase14.Context sharedContext;
public HardwareVideoEncoderFactory(EglBase.Context context, boolean z, boolean z2) {
this(context, z, z2, null);
}
public HardwareVideoEncoderFactory(EglBase.Context context, boolean z, boolean z2, Predicate<MediaCodecInfo> predicate) {
if (context instanceof EglBase14.Context) {
this.sharedContext = (EglBase14.Context) context;
} else {
Logging.w(TAG, "No shared EglBase.Context. Encoders will not use texture mode.");
this.sharedContext = null;
}
this.enableIntelVp8Encoder = z;
this.enableH264HighProfile = z2;
this.codecAllowedPredicate = predicate;
}
@Deprecated
public HardwareVideoEncoderFactory(boolean z, boolean z2) {
this(null, z, z2);
}
@Override // org.webrtc.VideoEncoderFactory
public VideoEncoder createEncoder(VideoCodecInfo videoCodecInfo) {
VideoCodecMimeType fromSdpCodecName = VideoCodecMimeType.fromSdpCodecName(videoCodecInfo.getName());
MediaCodecInfo findCodecForType = findCodecForType(fromSdpCodecName);
if (findCodecForType == null) {
return null;
}
String name = findCodecForType.getName();
String mimeType = fromSdpCodecName.mimeType();
Integer selectColorFormat = MediaCodecUtils.selectColorFormat(MediaCodecUtils.TEXTURE_COLOR_FORMATS, findCodecForType.getCapabilitiesForType(mimeType));
Integer selectColorFormat2 = MediaCodecUtils.selectColorFormat(MediaCodecUtils.ENCODER_COLOR_FORMATS, findCodecForType.getCapabilitiesForType(mimeType));
if (fromSdpCodecName == VideoCodecMimeType.H264) {
boolean isSameH264Profile = H264Utils.isSameH264Profile(videoCodecInfo.params, MediaCodecUtils.getCodecProperties(fromSdpCodecName, true));
boolean isSameH264Profile2 = H264Utils.isSameH264Profile(videoCodecInfo.params, MediaCodecUtils.getCodecProperties(fromSdpCodecName, false));
if (!isSameH264Profile && !isSameH264Profile2) {
return null;
}
if (isSameH264Profile && !isH264HighProfileSupported(findCodecForType)) {
return null;
}
}
return new HardwareVideoEncoder(new MediaCodecWrapperFactoryImpl(), name, fromSdpCodecName, selectColorFormat, selectColorFormat2, videoCodecInfo.params, getKeyFrameIntervalSec(fromSdpCodecName), getForcedKeyFrameIntervalMs(fromSdpCodecName, name), createBitrateAdjuster(fromSdpCodecName, name), this.sharedContext);
}
@Override // org.webrtc.VideoEncoderFactory
public VideoCodecInfo[] getSupportedCodecs() {
ArrayList arrayList = new ArrayList();
VideoCodecMimeType[] videoCodecMimeTypeArr = {VideoCodecMimeType.VP8, VideoCodecMimeType.VP9, VideoCodecMimeType.H264, VideoCodecMimeType.AV1};
for (int i = 0; i < 4; i++) {
VideoCodecMimeType videoCodecMimeType = videoCodecMimeTypeArr[i];
MediaCodecInfo findCodecForType = findCodecForType(videoCodecMimeType);
if (findCodecForType != null) {
String sdpCodecName = videoCodecMimeType.toSdpCodecName();
if (videoCodecMimeType == VideoCodecMimeType.H264 && isH264HighProfileSupported(findCodecForType)) {
arrayList.add(new VideoCodecInfo(sdpCodecName, MediaCodecUtils.getCodecProperties(videoCodecMimeType, true)));
}
arrayList.add(new VideoCodecInfo(sdpCodecName, MediaCodecUtils.getCodecProperties(videoCodecMimeType, false)));
}
}
return (VideoCodecInfo[]) arrayList.toArray(new VideoCodecInfo[arrayList.size()]);
}
private MediaCodecInfo findCodecForType(VideoCodecMimeType videoCodecMimeType) {
int i = 0;
while (true) {
MediaCodecInfo mediaCodecInfo = null;
if (i >= MediaCodecList.getCodecCount()) {
return null;
}
try {
mediaCodecInfo = MediaCodecList.getCodecInfoAt(i);
} catch (IllegalArgumentException e) {
Logging.e(TAG, "Cannot retrieve encoder codec info", e);
}
if (mediaCodecInfo != null && mediaCodecInfo.isEncoder() && isSupportedCodec(mediaCodecInfo, videoCodecMimeType)) {
return mediaCodecInfo;
}
i++;
}
}
private boolean isSupportedCodec(MediaCodecInfo mediaCodecInfo, VideoCodecMimeType videoCodecMimeType) {
return MediaCodecUtils.codecSupportsType(mediaCodecInfo, videoCodecMimeType) && MediaCodecUtils.selectColorFormat(MediaCodecUtils.ENCODER_COLOR_FORMATS, mediaCodecInfo.getCapabilitiesForType(videoCodecMimeType.mimeType())) != null && isHardwareSupportedInCurrentSdk(mediaCodecInfo, videoCodecMimeType) && isMediaCodecAllowed(mediaCodecInfo);
}
/* JADX INFO: Access modifiers changed from: package-private */
/* renamed from: org.webrtc.HardwareVideoEncoderFactory$1, reason: invalid class name */
/* loaded from: classes3.dex */
public static /* synthetic */ class AnonymousClass1 {
static final /* synthetic */ int[] $SwitchMap$org$webrtc$VideoCodecMimeType;
static {
int[] iArr = new int[VideoCodecMimeType.values().length];
$SwitchMap$org$webrtc$VideoCodecMimeType = iArr;
try {
iArr[VideoCodecMimeType.VP8.ordinal()] = 1;
} catch (NoSuchFieldError unused) {
}
try {
$SwitchMap$org$webrtc$VideoCodecMimeType[VideoCodecMimeType.VP9.ordinal()] = 2;
} catch (NoSuchFieldError unused2) {
}
try {
$SwitchMap$org$webrtc$VideoCodecMimeType[VideoCodecMimeType.H264.ordinal()] = 3;
} catch (NoSuchFieldError unused3) {
}
try {
$SwitchMap$org$webrtc$VideoCodecMimeType[VideoCodecMimeType.AV1.ordinal()] = 4;
} catch (NoSuchFieldError unused4) {
}
}
}
private boolean isHardwareSupportedInCurrentSdk(MediaCodecInfo mediaCodecInfo, VideoCodecMimeType videoCodecMimeType) {
int i = AnonymousClass1.$SwitchMap$org$webrtc$VideoCodecMimeType[videoCodecMimeType.ordinal()];
if (i == 1) {
return isHardwareSupportedInCurrentSdkVp8(mediaCodecInfo);
}
if (i == 2) {
return isHardwareSupportedInCurrentSdkVp9(mediaCodecInfo);
}
if (i != 3) {
return false;
}
return isHardwareSupportedInCurrentSdkH264(mediaCodecInfo);
}
private boolean isHardwareSupportedInCurrentSdkVp8(MediaCodecInfo mediaCodecInfo) {
String name = mediaCodecInfo.getName();
return name.startsWith("OMX.qcom.") || name.startsWith("OMX.Exynos.") || (name.startsWith("OMX.Intel.") && this.enableIntelVp8Encoder);
}
private boolean isHardwareSupportedInCurrentSdkVp9(MediaCodecInfo mediaCodecInfo) {
String name = mediaCodecInfo.getName();
return name.startsWith("OMX.qcom.") || name.startsWith("OMX.Exynos.");
}
private boolean isHardwareSupportedInCurrentSdkH264(MediaCodecInfo mediaCodecInfo) {
if (H264_HW_EXCEPTION_MODELS.contains(Build.MODEL)) {
return false;
}
String name = mediaCodecInfo.getName();
return name.startsWith("OMX.qcom.") || name.startsWith("OMX.Exynos.");
}
private boolean isMediaCodecAllowed(MediaCodecInfo mediaCodecInfo) {
Predicate<MediaCodecInfo> predicate = this.codecAllowedPredicate;
if (predicate == null) {
return true;
}
return predicate.test(mediaCodecInfo);
}
private int getKeyFrameIntervalSec(VideoCodecMimeType videoCodecMimeType) {
int i = AnonymousClass1.$SwitchMap$org$webrtc$VideoCodecMimeType[videoCodecMimeType.ordinal()];
if (i == 1 || i == 2) {
return 100;
}
if (i == 3) {
return 20;
}
if (i == 4) {
return 100;
}
throw new IllegalArgumentException("Unsupported VideoCodecMimeType " + videoCodecMimeType);
}
private int getForcedKeyFrameIntervalMs(VideoCodecMimeType videoCodecMimeType, String str) {
return (videoCodecMimeType == VideoCodecMimeType.VP8 && str.startsWith("OMX.qcom.")) ? 15000 : 0;
}
private BitrateAdjuster createBitrateAdjuster(VideoCodecMimeType videoCodecMimeType, String str) {
if (str.startsWith("OMX.Exynos.")) {
if (videoCodecMimeType == VideoCodecMimeType.VP8) {
return new DynamicBitrateAdjuster();
}
return new FramerateBitrateAdjuster();
}
return new BaseBitrateAdjuster();
}
private boolean isH264HighProfileSupported(MediaCodecInfo mediaCodecInfo) {
return this.enableH264HighProfile && mediaCodecInfo.getName().startsWith("OMX.Exynos.");
}
}