MediaCodec
用于访问底层媒体编解码器框架,编解码组件。通常与MediaExtractor(解封装,例如Mp4文件分解成 video和audio)、MediaSync、MediaMuxer(封装 例如音视频合成Mp4文件)、MediaCrypto、Image(cameraX 回调的ImageReader对象可以获取到Image帧图像,可转换成YUV420数组,传递给编码器编码),Surface(camera的预览回调)、Audio一起使用。
创建MediaCodec
public static MediaCodec createEncoderByType(@NonNull String type) //创建编码器
public static MediaCodec createDecoderByType(@NonNull String type) //创建解码器
一般type是:
- h264 MediaFormat.MIMETYPE_VIDEO_AVC(video/avc)
- h265 MediaFormat.MIMETYPE_VIDEO_HEVC(video/hevc)
public static MediaCodec createByCodecName(@NonNull String name) //通过名称创建
一般name是:
- OMX.google.h264.encoder 软编码
- OMX.google.h264.decoder 软解码
- OMX.MTK.VIDEO.DECODER.AVC 硬解码
- 等
以下代码可以检测是否支持某编解码
MediaCodecList codecList = new MediaCodecList(MediaCodecList.ALL_CODECS);
for (MediaCodecInfo codecInfo : codecList.getCodecInfos()) {
if (codecInfo.isEncoder()) {
continue; // 只检查解码器
}
String[] types = codecInfo.getSupportedTypes();
for (String type : types) {
if (type.equalsIgnoreCase("video/hevc")) {
Log.d("MediaCodec", "Device supports H265 decoding");
return; // 找到支持的解码器,可以退出
}
}
}
Log.d("MediaCodec", "Device does not support H265 decoding");
工作方式
以以下代码为例
// 假设你已经有了ByteBuffer data和BufferInfo info
codec.queueInputBuffer(inputBufferIndex, 0, data.limit(), presentationTimeUs, 0);
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int outputBufferIndex = codec.dequeueOutputBuffer(bufferInfo, 10000);
while (outputBufferIndex >= 0) {
// 处理输出帧,例如绘制到Surface或保存到文件等
codec.releaseOutputBuffer(outputBufferIndex, true); // true表示将输出帧渲染到Surface中(如果有的话)
outputBufferIndex = codec.dequeueOutputBuffer(bufferInfo, 0); // 再次尝试获取下一个输出帧
}
- queueInputBuffer 处理输入数据(如:camera/cameraX回调的视频帧数据)
- dequeueOutputBuffer 输出数据 编码后的数据(如:使用H264编码则 可直接将数据保存为H264文件)
分析: - MediaCodeC使用一组输入和输出Buffer队列。
- 数据填入设定的空输入缓冲区 ( inputBuffers = mediaCodec.getInputBuffers();),填满数据后传递给MediaCodec进行编解码
- 编解码后数据填充到一个输出Buffer中。
状态
- MediaCodec 有三种状态 Stopped Executing Released
– Stopped 包括 三种状态 Uninitialized Configured Error
– Executing 包括三种状态 Flushed Running End-of-Stream
状态
- 通过以上create工厂方法创建一个MediaCodec ,MediaCodec 处于未初始化状态
- 需要通过MediaCodec对象的configure方法配置,配置后 处于配置状态
String mimeType = "video/hevc"; // H265 MIME类型
MediaCodec codec = MediaCodec.createDecoderByType(mimeType);
MediaFormat format = MediaFormat.createVideoFormat(mimeType, width, height);
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface); // 使用Surface作为输出类型,如果是预览或显示使用
format.setInteger(MediaFormat.KEY_BIT_RATE, bitRate); // 设置比特率,如果需要的话
format.setInteger(MediaFormat.KEY_FRAME_RATE, frameRate); // 设置帧率,如果需要的话
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1); // 设置I帧间隔,通常设置为1或2
codec.configure(format, null, null, 0); // 使用null作为输出Surface,因为我们不直接处理输出数据
codec.start();
- 调用start之后 处于 Executing状态
- 调用start后,MediaCodec 会立刻刷新子状态(Flushed ) 并拥有所有的Buffer
- 第一个输入Buffer从队列中移除 MediaCodec 会花较长时间转换为 正在运行的子状态 (Running )
- 当队列输入Buffer携带End-of-Stream标记 则转换为 End-of-Stream子状态 这种状态MediaCodec不再接收输入Buffer,但是仍然会产生输出Buffer
- Executing状态下,调用flush函数可以回到 Flushed子状态
- 调用stop,MediaCodec 处于Uninitialized 状态 等待再次配置和使用
- 再次创建MediaCodec 时,上个MediaCodec 对象必须调用release函数
- 极少数情况会出现Error状态,此时需要调用reset函数让MediaCodec 再次可用
MediaCodec 从创建到start的过程
- 需要经历JNI
- 与MediaPlayer有很多相似的地方
- 我帮大家看了 想看的花去点这里
就是MediaCodec 会对应到 c++层的JMediaCodec方法
release -> native_release
reset -> native_reset
setup -> native_setup
finalize -> native_finalize
config -> native_configure
- setup -> 调用 JMediaCodec 构造方法创建JMediaCodec
- JMediaCodec的构造方法 -> createByType/CreateByComponentName 创建JMediaCodec
- 接着java 调用config -> native_configure
- native_configure -> 获取前边创建的JMediaCodec 对象 调用其configure方法 构建编解码器
- java 层调用 start -> 最终使用的还是JMediaCodec 对象 的 start方法 会直行ACodec.cpp 的start 函数
MediaCodec 到OMX过程
OpenMAX Integration Layer(OMX IL,集成层)是由Khronos Group开发的一套低层级标准接口,旨在为编解码器提供一定程度的抽象,使得嵌入式或移动设备能够统一调用音频、视频和图像编解码器,从而实现编解码器实现代码和调用代码的跨平台性。
OMX IL API由两大主要部分组成,分别是Core API和Component API。
OMX IL Component:在OMX IL中组件表示独立的功能模块,组件可能是source(源)、sinks(接收器)、codecs(编解码器)、filters(过滤器)或任何其他数据处理模块,组件需要依据Component API来实现。与组件之间的数据通信是通过称为端口的接口进行的,用户可以通过输入端口向组件发送数据,也可以通过输出端口接收数据。
OMX IL Core:Core API主要用于动态加载卸载组件,调用组件方法;
将OMX IL API封装并向上层提供高层级接口的部分被称为IL Client(客户端),IL Client使用OMX Core来加载组件,卸载组件,调用组件的方法。
- MediaCodec::init 函数
– 创建Acodec Acodec继承AHander(消息机制有了)
– 初始化ALooper AMessage
– 发送kWhatInit消息
– ACodec收到消息 调用initiateAllocateComponent(format)函数
– 发送kWhatAllocateComponent消息 - 消息中心收到消息 调用onAllocateComponent回调函数
– 通过ACodec::AllocateComponent函数判断OMXClient和Server是否正常建立连接
– 通过IOMX进行IPC通信
– 调用omx->allocateNode分配Node节点 - onConfigureComponent 函数
– 调用ACodec的configCodec函数 构建编解码器