Android MediaCodec 简明教程(一):使用 MediaCodecList 查询 Codec 信息,并创建 MediaCodec 编解码器
系列文章目录
文章目录
前言
最近在学习 Android MediaCodec 相关的知识,准备开个新坑把学习过程记录下来,总结成 MediaCodec 教程。在介绍 MediaCodec 编解码之前,让我们学习一些其他与之配套的组件,今天要讲的是 MediaCodecList。
提示:以下是本篇文章正文内容,下面案例可供参考
一、MediaCodecList 是什么?
MediaCodecList 是 Android 提供的一个类,用于查询设备上可用的编解码器(codecs)。编解码器是用于编码和解码数据的软件组件,例如音频或视频数据。
MediaCodecList 提供了一种方法来获取设备上所有可用的编解码器的信息,包括它们的名称、支持的数据格式等。这对于开发需要处理音频或视频数据的应用程序非常有用。
例如,你可以使用 MediaCodecList 来查找设备上是否有支持特定视频格式的编解码器,如果有,你可以使用这个编解码器来处理这种格式的视频数据。
二、MediaCodecList 使用方法
1. 获取 MediaCodecList 实例
可以通过以下方式获取 MediaCodecList 实例:
MediaCodecList codecList = new MediaCodecList(MediaCodecList.ALL_CODECS);
其中,ALL_CODECS 表示列出所有编解码器,也可以使用 REGULAR_CODECS 表示只列出正常编解码器。在笔者的华为测试机上,ALL_CODECS 与 REGULAR_CODECS 有部分差别,在 ALL_CODECS 中有一些带 .secure 的解码器类型 ,例如 OMX.hisi.video.decoder.avc.secure
2. 列出设备上支持的编解码器
可以通过以下方式列出设备上支持的编解码器:
MediaCodecInfo[] codecInfos = codecList.getCodecInfos();
其中,codecInfos 是一个包含所有支持的编解码器信息的数组。
3. 获取编解码器详细信息
可以通过以下方式获取编解码器的详细信息:
MediaCodecInfo codecInfo = codecInfos[i];
String name = codecInfo.getName(); // 编解码器名称
String canonicalName = codecInfo.getCanonicalName(); // 编解码器规范名称
boolean isEncoder = codecInfo.isEncoder(); // 是编码器吗?
boolean isVendor = codecInfo.isVendor(); // 是厂商提供的吗?
boolean isSoftwareOnly = codecInfo.isSoftwareOnly(); // 纯软件实现的编解码器?
boolean isHardwareAccelerated = codecINfo.isHardwareAccelerated(); // 有硬件加速?
String[] types = codecInfo.getSupportedTypes(); // 支持的媒体类型
MediaCodecInfo.CodecCapabilities capabilities = codecInfo.getCapabilitiesForType(type); // 编解码器的配置参数
canonicalName
是编码器规范名称,在 Android 中,一个编解码器可能有多个别名,为了保持向后兼容性,设备实现可能会为同一个编解码器提供多个别名。getCanonicalName() 方法返回的是编解码器的规范名称,它必须是唯一的,不能是别名。
isHardwareAccelerated
查询是否有硬件加速,很多厂商提供编解码器都有硬件加速。
下面是笔者机器上的编解码器信息:
codec: c2.android.aac.decoder, canonicalName: c2.android.aac.decoder, types: [audio/mp4a-latm], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.aac.decoder, canonicalName: c2.android.aac.decoder, types: [audio/mp4a-latm], isEncoder = false, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.aac.encoder, canonicalName: c2.android.aac.encoder, types: [audio/mp4a-latm], isEncoder = true, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.aac.encoder, canonicalName: c2.android.aac.encoder, types: [audio/mp4a-latm], isEncoder = true, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.amrnb.decoder, canonicalName: c2.android.amrnb.decoder, types: [audio/3gpp], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.amrnb.decoder, canonicalName: c2.android.amrnb.decoder, types: [audio/3gpp], isEncoder = false, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.amrnb.encoder, canonicalName: c2.android.amrnb.encoder, types: [audio/3gpp], isEncoder = true, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.amrnb.encoder, canonicalName: c2.android.amrnb.encoder, types: [audio/3gpp], isEncoder = true, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.amrwb.decoder, canonicalName: c2.android.amrwb.decoder, types: [audio/amr-wb], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.amrwb.decoder, canonicalName: c2.android.amrwb.decoder, types: [audio/amr-wb], isEncoder = false, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.amrwb.encoder, canonicalName: c2.android.amrwb.encoder, types: [audio/amr-wb], isEncoder = true, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.amrwb.encoder, canonicalName: c2.android.amrwb.encoder, types: [audio/amr-wb], isEncoder = true, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.flac.decoder, canonicalName: c2.android.flac.decoder, types: [audio/flac], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.flac.decoder, canonicalName: c2.android.flac.decoder, types: [audio/flac], isEncoder = false, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.flac.encoder, canonicalName: c2.android.flac.encoder, types: [audio/flac], isEncoder = true, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.flac.encoder, canonicalName: c2.android.flac.encoder, types: [audio/flac], isEncoder = true, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.g711.alaw.decoder, canonicalName: c2.android.g711.alaw.decoder, types: [audio/g711-alaw], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.g711.alaw.decoder, canonicalName: c2.android.g711.alaw.decoder, types: [audio/g711-alaw], isEncoder = false, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.g711.mlaw.decoder, canonicalName: c2.android.g711.mlaw.decoder, types: [audio/g711-mlaw], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.g711.mlaw.decoder, canonicalName: c2.android.g711.mlaw.decoder, types: [audio/g711-mlaw], isEncoder = false, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.gsm.decoder, canonicalName: c2.android.gsm.decoder, types: [audio/gsm], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.gsm.decoder, canonicalName: c2.android.gsm.decoder, types: [audio/gsm], isEncoder = false, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.mp3.decoder, canonicalName: c2.android.mp3.decoder, types: [audio/mpeg], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.mp3.decoder, canonicalName: c2.android.mp3.decoder, types: [audio/mpeg], isEncoder = false, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.opus.decoder, canonicalName: c2.android.opus.decoder, types: [audio/opus], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.opus.decoder, canonicalName: c2.android.opus.decoder, types: [audio/opus], isEncoder = false, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.opus.encoder, canonicalName: c2.android.opus.encoder, types: [audio/opus], isEncoder = true, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.raw.decoder, canonicalName: c2.android.raw.decoder, types: [audio/raw], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.raw.decoder, canonicalName: c2.android.raw.decoder, types: [audio/raw], isEncoder = false, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.vorbis.decoder, canonicalName: c2.android.vorbis.decoder, types: [audio/vorbis], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.vorbis.decoder, canonicalName: c2.android.vorbis.decoder, types: [audio/vorbis], isEncoder = false, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.dolby.ac3.decoder, canonicalName: OMX.dolby.ac3.decoder, types: [audio/ac3], isEncoder = false, isVendor = true, isAlias = false, isSoftwareOnly = false, isHardwareAccelerated = true
codec: OMX.dolby.ac4.decoder, canonicalName: OMX.dolby.ac4.decoder, types: [audio/ac4], isEncoder = false, isVendor = true, isAlias = false, isSoftwareOnly = false, isHardwareAccelerated = true
codec: OMX.dolby.eac3.decoder, canonicalName: OMX.dolby.eac3.decoder, types: [audio/eac3], isEncoder = false, isVendor = true, isAlias = false, isSoftwareOnly = false, isHardwareAccelerated = true
codec: OMX.dolby.eac3_joc.decoder, canonicalName: OMX.dolby.eac3_joc.decoder, types: [audio/eac3-joc], isEncoder = false, isVendor = true, isAlias = false, isSoftwareOnly = false, isHardwareAccelerated = true
codec: OMX.hisi.video.decoder.avc, canonicalName: OMX.hisi.video.decoder.avc, types: [video/avc], isEncoder = false, isVendor = true, isAlias = false, isSoftwareOnly = false, isHardwareAccelerated = true
codec: OMX.hisi.video.decoder.avc.secure, canonicalName: OMX.hisi.video.decoder.avc.secure, types: [video/avc], isEncoder = false, isVendor = true, isAlias = false, isSoftwareOnly = false, isHardwareAccelerated = true
codec: OMX.hisi.video.decoder.hevc, canonicalName: OMX.hisi.video.decoder.hevc, types: [video/hevc], isEncoder = false, isVendor = true, isAlias = false, isSoftwareOnly = false, isHardwareAccelerated = true
codec: OMX.hisi.video.decoder.hevc.secure, canonicalName: OMX.hisi.video.decoder.hevc.secure, types: [video/hevc], isEncoder = false, isVendor = true, isAlias = false, isSoftwareOnly = false, isHardwareAccelerated = true
codec: OMX.hisi.video.decoder.mpeg2, canonicalName: OMX.hisi.video.decoder.mpeg2, types: [video/mpeg2], isEncoder = false, isVendor = true, isAlias = false, isSoftwareOnly = false, isHardwareAccelerated = true
codec: OMX.hisi.video.decoder.mpeg4, canonicalName: OMX.hisi.video.decoder.mpeg4, types: [video/mp4v-es], isEncoder = false, isVendor = true, isAlias = false, isSoftwareOnly = false, isHardwareAccelerated = true
codec: OMX.hisi.video.decoder.vp8, canonicalName: OMX.hisi.video.decoder.vp8, types: [video/x-vnd.on2.vp8], isEncoder = false, isVendor = true, isAlias = false, isSoftwareOnly = false, isHardwareAccelerated = true
codec: OMX.hisi.video.decoder.vp9, canonicalName: OMX.hisi.video.decoder.vp9, types: [video/x-vnd.on2.vp9], isEncoder = false, isVendor = true, isAlias = false, isSoftwareOnly = false, isHardwareAccelerated = true
codec: OMX.hisi.video.decoder.vp9.secure, canonicalName: OMX.hisi.video.decoder.vp9.secure, types: [video/x-vnd.on2.vp9], isEncoder = false, isVendor = true, isAlias = false, isSoftwareOnly = false, isHardwareAccelerated = true
codec: OMX.hisi.video.encoder.avc, canonicalName: OMX.hisi.video.encoder.avc, types: [video/avc], isEncoder = true, isVendor = true, isAlias = false, isSoftwareOnly = false, isHardwareAccelerated = true
codec: OMX.hisi.video.encoder.hevc, canonicalName: OMX.hisi.video.encoder.hevc, types: [video/hevc], isEncoder = true, isVendor = true, isAlias = false, isSoftwareOnly = false, isHardwareAccelerated = true
codec: c2.android.av1.decoder, canonicalName: c2.android.av1.decoder, types: [video/av01], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.avc.decoder, canonicalName: c2.android.avc.decoder, types: [video/avc], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.h264.decoder, canonicalName: c2.android.avc.decoder, types: [video/avc], isEncoder = false, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.avc.encoder, canonicalName: c2.android.avc.encoder, types: [video/avc], isEncoder = true, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.h264.encoder, canonicalName: c2.android.avc.encoder, types: [video/avc], isEncoder = true, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.h263.decoder, canonicalName: c2.android.h263.decoder, types: [video/3gpp], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.h263.decoder, canonicalName: c2.android.h263.decoder, types: [video/3gpp], isEncoder = false, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.h263.encoder, canonicalName: c2.android.h263.encoder, types: [video/3gpp], isEncoder = true, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.h263.encoder, canonicalName: c2.android.h263.encoder, types: [video/3gpp], isEncoder = true, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.hevc.decoder, canonicalName: c2.android.hevc.decoder, types: [video/hevc], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.hevc.decoder, canonicalName: c2.android.hevc.decoder, types: [video/hevc], isEncoder = false, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.hevc.encoder, canonicalName: c2.android.hevc.encoder, types: [video/hevc], isEncoder = true, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.mpeg4.decoder, canonicalName: c2.android.mpeg4.decoder, types: [video/mp4v-es], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.mpeg4.decoder, canonicalName: c2.android.mpeg4.decoder, types: [video/mp4v-es], isEncoder = false, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.mpeg4.encoder, canonicalName: c2.android.mpeg4.encoder, types: [video/mp4v-es], isEncoder = true, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.mpeg4.encoder, canonicalName: c2.android.mpeg4.encoder, types: [video/mp4v-es], isEncoder = true, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.vp8.decoder, canonicalName: c2.android.vp8.decoder, types: [video/x-vnd.on2.vp8], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.vp8.decoder, canonicalName: c2.android.vp8.decoder, types: [video/x-vnd.on2.vp8], isEncoder = false, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.vp8.encoder, canonicalName: c2.android.vp8.encoder, types: [video/x-vnd.on2.vp8], isEncoder = true, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.vp8.encoder, canonicalName: c2.android.vp8.encoder, types: [video/x-vnd.on2.vp8], isEncoder = true, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.vp9.decoder, canonicalName: c2.android.vp9.decoder, types: [video/x-vnd.on2.vp9], isEncoder = false, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.vp9.decoder, canonicalName: c2.android.vp9.decoder, types: [video/x-vnd.on2.vp9], isEncoder = false, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
codec: c2.android.vp9.encoder, canonicalName: c2.android.vp9.encoder, types: [video/x-vnd.on2.vp9], isEncoder = true, isVendor = false, isAlias = false, isSoftwareOnly = true, isHardwareAccelerated = false
codec: OMX.google.vp9.encoder, canonicalName: c2.android.vp9.encoder, types: [video/x-vnd.on2.vp9], isEncoder = true, isVendor = false, isAlias = true, isSoftwareOnly = true, isHardwareAccelerated = false
4. 判断设备是否支持某种编解码器
可以使用以下方式判断设备是否支持某种编解码器:
boolean isEncoder = true; // 是否为编码器
String mimeType = "video/avc"; // 媒体类型
MediaCodecInfo codecInfo = codecList.findCodecForType(mimeType);
if (codecInfo != null) {
String name = codecInfo.getName();
if (isEncoder) {
MediaCodecInfo.CodecCapabilities capabilities = codecInfo.getCapabilitiesForType(mimeType);
if (capabilities != null && capabilities.isEncoder()) {
// 设备支持该编码器
}
} else {
// 设备支持该解码器
}
}
其中,isEncoder 表示是否为编码器,mimeType 表示媒体类型。
5. 查询默认的编解码器
对于某种 MediaFormat,MediaCodecList 支持查询哪种 Codec 支持这种 Format:
MediaCodecInfo codecInfo = codecList.findEncoderForFormat(format);
if (codecInfo != null) {
String name = codecInfo.getName();
// 使用默认编码器
}
其中,format 表示媒体格式。
三、通过 MediaCodecList 创建 MediaCodec
3.1 创建解码器
想要解码某个文件,首先要创建 MediaCodec 解码器,基本步骤是
- 创建 MediaCodecList 实例
- 使用 MediaExtractor 获取 Track 的 Format 信息
- 根据 Format 找到默认的解码器信息 MediaCodecInfo
- 根据 MediaCodecInfo 中的信息创建 MediaCodec
代码如下:
public class Decoder {
public void decodeFile(String filePath) {
MediaExtractor extractor = new MediaExtractor();
try {
extractor.setDataSource(filePath);
} catch (IOException e) {
e.printStackTrace();
}
// 获取文件中的第一个轨道
MediaFormat format = extractor.getTrackFormat(0);
String mime = format.getString(MediaFormat.KEY_MIME);
// 使用 MediaCodecList 来找到支持这个媒体类型的解码器
MediaCodecList codecList = new MediaCodecList(MediaCodecList.ALL_CODECS);
String decoderName = codecList.findDecoderForFormat(format);
// 创建 MediaCodec 解码器
MediaCodec decoder = null;
try {
decoder = MediaCodec.createByCodecName(decoderName);
} catch (IOException e) {
e.printStackTrace();
}
// 配置解码器
decoder.configure(format, null, null, 0);
decoder.start();
// ... 这里可以开始解码操作
}
}
3.2 创建编码器
- 创建 MediaCodecList 实例
- 构建 MediaFormat 用于描述目标编码的格式
- 通过 MediaCodecList 查询 MediaFormat 的默认编码器信息 MediaCodecInfo
- 根据 MediaCodecInfo 中的信息创建 MediaCodec
代码如下:
public class Encoder {
public void encodeToFormat(String mimeType) {
// 创建 MediaCodecList 实例
MediaCodecList codecList = new MediaCodecList(MediaCodecList.ALL_CODECS);
// 构建 MediaFormat 用于描述目标编码的格式
MediaFormat format = MediaFormat.createVideoFormat(mimeType, 640, 480);
// 通过 MediaCodecList 查询 MediaFormat 的默认编码器信息 MediaCodecInfo
String encoderName = codecList.findEncoderForFormat(format);
// 根据 MediaCodecInfo 中的信息创建 MediaCodec
MediaCodec encoder = null;
try {
encoder = MediaCodec.createByCodecName(encoderName);
} catch (IOException e) {
e.printStackTrace();
}
// 配置编码器
encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
encoder.start();
// ... 这里可以开始编码操作
}
}
总结
本文介绍了 MediaCodecList 的基本使用方法,并展示了如何使用 MediaCodecList 来创建 MediaCodec 编解码器。
参考
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!