Skip to content

Commit

Permalink
修复兼容部分机型硬编码失败的问题。
Browse files Browse the repository at this point in the history
Change-Id: Ib418740ff192f4daaa9c87e535bb4d5490987066
  • Loading branch information
SundoggyNew committed Jan 16, 2024
1 parent f191d86 commit 2f4abe6
Showing 1 changed file with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.annotation.TargetApi;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.media.MediaFormat;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -27,17 +28,24 @@ public class VideoEncoder {
private int MAX_BITRATE_LENGTH = 1000000;
private int beginBitRate = 0;

private String firstSupportColorFormatCodecName = ""; // OMX.qcom.video.encoder.avc 和 c2.android.avc.encoder 过滤,这两个h264编码性能好一些。如果都不支持COLOR_FormatYUV420Planar,就用默认的方式。

public VideoEncoder(VideoEncodeParam param) {
this.videoEncodeParam = param;
checkSupportedColorFormats();
initMediaCodec();
}

private void initMediaCodec() {
try {
mediaCodec = MediaCodec.createEncoderByType("video/avc");
if (!firstSupportColorFormatCodecName.isEmpty()) {
mediaCodec = MediaCodec.createByCodecName(firstSupportColorFormatCodecName);
} else {
mediaCodec = MediaCodec.createEncoderByType("video/avc");
}
//height和width一般都是照相机的height和width。
//TODO 因为获取到的视频帧数据是逆时针旋转了90度的,所以这里宽高需要对调
MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", videoEncodeParam.getHeight(), videoEncodeParam.getWidth());
MediaFormat mediaFormat = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_AVC, videoEncodeParam.getHeight(), videoEncodeParam.getWidth());
//描述平均位速率(以位/秒为单位)的键。 关联的值是一个整数
int bitRate = videoEncodeParam.getBitRate();
if (bitRate > MAX_BITRATE_LENGTH) {
Expand Down Expand Up @@ -327,4 +335,36 @@ public void setEncoderListener(OnEncodeListener listener) {
public void stop() {
executor.shutdown();
}

private void checkSupportedColorFormats() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
MediaCodecList codecList = new MediaCodecList(MediaCodecList.ALL_CODECS);
MediaCodecInfo[] codecInfos = codecList.getCodecInfos();

for (MediaCodecInfo codecInfo : codecInfos) {
if (!codecInfo.isEncoder()) {
continue;
}

if (codecInfo.getName().equals("OMX.qcom.video.encoder.avc") || codecInfo.getName().equals("c2.android.avc.encoder")) {

String[] supportedTypes = codecInfo.getSupportedTypes();
for (String type : supportedTypes) {
if (type.startsWith("video/")) {
MediaCodecInfo.CodecCapabilities capabilities = codecInfo.getCapabilitiesForType(type);
int[] colorFormats = capabilities.colorFormats;
for (int colorFormat : colorFormats) {

if (colorFormat == MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar) {
Log.d("TAG", "Video encoder: " + codecInfo.getName() + ", supported color format: " + colorFormat);
firstSupportColorFormatCodecName = codecInfo.getName();
return;
}
}
}
}
}
}
}
}
}

0 comments on commit 2f4abe6

Please sign in to comment.