Skip to content

Commit

Permalink
add CodecType CBR_PRIORITY
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Nov 6, 2024
1 parent 7b5a2e7 commit 35c550b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
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

0 comments on commit 35c550b

Please sign in to comment.