Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CodecType CBR_PRIORITY #1633

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
43 changes: 39 additions & 4 deletions encoder/src/main/java/com/pedro/encoder/utils/CodecUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -209,7 +209,7 @@ public static List<MediaCodecInfo> 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);
}
Expand Down Expand Up @@ -301,12 +301,47 @@ public static List<MediaCodecInfo> getAllEncoders(String mime, boolean hardwareP
return mediaCodecInfoList;
}

/**
* @return list of encoders -> hardware CBR > software CBR > hardware > software
*/
public static List<MediaCodecInfo> getAllEncodersCbrPriority(String mime) {
List<MediaCodecInfo> mediaCodecInfoList = new ArrayList<>();
List<MediaCodecInfo> hardwareEncoders = getAllHardwareEncoders(mime);
List<MediaCodecInfo> softwareEncoders = getAllHardwareEncoders(mime);

List<MediaCodecInfo> hardwareEncodersCbr = new ArrayList<>();
List<MediaCodecInfo> hardwareEncodersNoCbr = new ArrayList<>();
List<MediaCodecInfo> softwareEncodersCbr = new ArrayList<>();
List<MediaCodecInfo> 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<MediaCodecInfo> getAllEncoders(String mime, boolean hardwarePriority) {
return getAllEncoders(mime, hardwarePriority, false);
}



/**
* choose decoder by mime.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading