From 35c550b3bbeaa76504e894fee689b5460055c80b Mon Sep 17 00:00:00 2001 From: pedroSG94 Date: Thu, 7 Nov 2024 00:24:15 +0100 Subject: [PATCH] add CodecType CBR_PRIORITY --- .../com/pedro/encoder/audio/AudioEncoder.java | 2 + .../com/pedro/encoder/utils/CodecUtil.java | 43 +++++++++++++++++-- .../com/pedro/encoder/video/VideoEncoder.java | 3 ++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/encoder/src/main/java/com/pedro/encoder/audio/AudioEncoder.java b/encoder/src/main/java/com/pedro/encoder/audio/AudioEncoder.java index a89b24bcb..bbdb6698a 100644 --- a/encoder/src/main/java/com/pedro/encoder/audio/AudioEncoder.java +++ b/encoder/src/main/java/com/pedro/encoder/audio/AudioEncoder.java @@ -185,6 +185,8 @@ protected MediaCodecInfo chooseEncoder(String mime) { mediaCodecInfoList = CodecUtil.getAllHardwareEncoders(CodecUtil.AAC_MIME); } else if (codecType == CodecUtil.CodecType.SOFTWARE) { mediaCodecInfoList = CodecUtil.getAllSoftwareEncoders(CodecUtil.AAC_MIME); + } else if (codecType == CodecUtil.CodecType.CBR_PRIORITY) { + mediaCodecInfoList = CodecUtil.getAllEncodersCbrPriority(mime); } else { //Priority: hardware > software mediaCodecInfoList = CodecUtil.getAllEncoders(mime, true); diff --git a/encoder/src/main/java/com/pedro/encoder/utils/CodecUtil.java b/encoder/src/main/java/com/pedro/encoder/utils/CodecUtil.java index 4bed2725b..3ccc135eb 100644 --- a/encoder/src/main/java/com/pedro/encoder/utils/CodecUtil.java +++ b/encoder/src/main/java/com/pedro/encoder/utils/CodecUtil.java @@ -43,7 +43,7 @@ public class CodecUtil { public static final String OPUS_MIME = "audio/opus"; public enum CodecType { - FIRST_COMPATIBLE_FOUND, SOFTWARE, HARDWARE + FIRST_COMPATIBLE_FOUND, SOFTWARE, HARDWARE, CBR_PRIORITY } public enum CodecTypeError { @@ -209,7 +209,7 @@ public static List getAllHardwareEncoders(String mime, boolean c for (MediaCodecInfo mediaCodecInfo : mediaCodecInfoList) { if (isHardwareAccelerated(mediaCodecInfo)) { mediaCodecInfoHardware.add(mediaCodecInfo); - if (cbrPriority &&Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP + if (cbrPriority && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && isCBRModeSupported(mediaCodecInfo, mime)) { mediaCodecInfoHardwareCBR.add(mediaCodecInfo); } @@ -301,12 +301,47 @@ public static List getAllEncoders(String mime, boolean hardwareP return mediaCodecInfoList; } + /** + * @return list of encoders -> hardware CBR > software CBR > hardware > software + */ + public static List getAllEncodersCbrPriority(String mime) { + List mediaCodecInfoList = new ArrayList<>(); + List hardwareEncoders = getAllHardwareEncoders(mime); + List softwareEncoders = getAllHardwareEncoders(mime); + + List hardwareEncodersCbr = new ArrayList<>(); + List hardwareEncodersNoCbr = new ArrayList<>(); + List softwareEncodersCbr = new ArrayList<>(); + List softwareEncodersNoCbr = new ArrayList<>(); + + for (MediaCodecInfo mediaCodecInfo: hardwareEncoders) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP + && isCBRModeSupported(mediaCodecInfo, mime)) { + hardwareEncodersCbr.add(mediaCodecInfo); + } else { + hardwareEncodersNoCbr.add(mediaCodecInfo); + } + } + + for (MediaCodecInfo mediaCodecInfo: softwareEncoders) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP + && isCBRModeSupported(mediaCodecInfo, mime)) { + softwareEncodersCbr.add(mediaCodecInfo); + } else { + softwareEncodersNoCbr.add(mediaCodecInfo); + } + } + mediaCodecInfoList.addAll(hardwareEncodersCbr); + mediaCodecInfoList.addAll(softwareEncodersCbr); + mediaCodecInfoList.addAll(hardwareEncodersNoCbr); + mediaCodecInfoList.addAll(softwareEncodersNoCbr); + return mediaCodecInfoList; + } + public static List getAllEncoders(String mime, boolean hardwarePriority) { return getAllEncoders(mime, hardwarePriority, false); } - - /** * choose decoder by mime. */ diff --git a/encoder/src/main/java/com/pedro/encoder/video/VideoEncoder.java b/encoder/src/main/java/com/pedro/encoder/video/VideoEncoder.java index e2c0cde44..2c33d94bb 100644 --- a/encoder/src/main/java/com/pedro/encoder/video/VideoEncoder.java +++ b/encoder/src/main/java/com/pedro/encoder/video/VideoEncoder.java @@ -355,6 +355,9 @@ protected MediaCodecInfo chooseEncoder(String mime) { mediaCodecInfoList = CodecUtil.getAllHardwareEncoders(mime, true); } else if (codecType == CodecUtil.CodecType.SOFTWARE) { mediaCodecInfoList = CodecUtil.getAllSoftwareEncoders(mime, true); + } else if (codecType == CodecUtil.CodecType.CBR_PRIORITY) { + //Priority: hardware CBR > software CBR > hardware > software + mediaCodecInfoList = CodecUtil.getAllEncodersCbrPriority(mime); } else { //Priority: hardware CBR > hardware > software CBR > software mediaCodecInfoList = CodecUtil.getAllEncoders(mime, true, true);