一、基本概念
- 编码
编码是信息从一种形式或格式转换为另一种形式的过程。用预先规定的方法将文字、数字或其他对象编成数码,或将信息、数据转换成规定的电脉冲信号。在本模块中,编码是指编码器将原始的视频信息压缩为另一种格式的过程。
- 解码
解码是一种用特定方法,把数码还原成它所代表的内容或将电脉冲信号、光信号、无线电波等转换成它所代表的信息、数据等的过程。在本模块中,解码是指解码器将接收到的数据还原为视频信息的过程,与编码过程相对应。
- 帧率
帧率是以帧称为单位的位图图像连续出现在显示器上的频率(速率),以赫兹(Hz
)为单位。该术语同样适用于胶片、摄像机、计算机图形和动作捕捉系统。
二、常用API
1、媒体编解码能力查询
CodecDescriptionList :提供设备可用的编解码器支持的媒体格式的MIME
类型列表。您可以使用此类中的方法来检查设备是否支持指定MIME
类型的编解码器
接口名 | 功能描述 |
---|---|
getSupportedMimes() | 获取某设备所支持的编解码器的MIME列表。 |
isDecodeSupportedByMime(String mime) | 判断某设备是否支持指定MIME对应的解码器。 |
isEncodeSupportedByMime(String mime) | 判断某设备是否支持指定MIME对应的编码器。 |
isDecoderSupportedByFormat(Format format) | 判断某设备是否支持指定媒体格式对应的解码器。 |
isEncoderSupportedByFormat(Format format) | 判断某设备是否支持指定媒体格式对应的编码器。 |
获取某设备所支持的编解码器的
MIME
列表
List<String> supportedMimes = CodecDescriptionList.getSupportedMimes();
for (String supportedMime : supportedMimes) {
HiLog.info(hiLogLabel,supportedMime);
}
判断某设备是否支持指定
MIME
对应的解码器
boolean flag1 = CodecDescriptionList.isDecodeSupportedByMime(Format.VIDEO_VP9);
boolean flag2 = CodecDescriptionList.isDecodeSupportedByMime(Format.VIDEO_VP8);
HiLog.info(hiLogLabel,"video/x-vnd.on2.vp8:"+flag1+",video/x-vnd.on2.vp9:"+flag2);
判断某设备是否支持指定
MIME
对应的编码器
boolean flag3 = CodecDescriptionList.isEncodeSupportedByMime(Format.VIDEO_VP9);
boolean flag4 = CodecDescriptionList.isEncodeSupportedByMime(Format.VIDEO_VP8);
HiLog.info(hiLogLabel,"video/x-vnd.on2.vp8:"+flag3+",video/x-vnd.on2.vp9:"+flag4);
判断某设备是否支持指定
Format
的编解码器
Format format1 = new Format();
Format format2 = new Format();
format1.putStringValue(Format.MIME, Format.VIDEO_AVC);
format1.putIntValue(Format.WIDTH, 2560);
format1.putIntValue(Format.HEIGHT, 1440);
format1.putIntValue(Format.FRAME_RATE, 30);
format1.putIntValue(Format.FRAME_INTERVAL, 1);
format2.putStringValue(Format.MIME, Format.VIDEO_HEVC);
format2.putIntValue(Format.WIDTH, 1280);
format2.putIntValue(Format.HEIGHT, 720);
format2.putIntValue(Format.FRAME_RATE, 20);
format2.putIntValue(Format.FRAME_INTERVAL, 1);
boolean flag5 = CodecDescriptionList.isDecoderSupportedByFormat(format1);
boolean flag6 = CodecDescriptionList.isDecoderSupportedByFormat(format2);
HiLog.info(hiLogLabel, "format1:" + flag5 + ",format2:" + flag6);
boolean flag7 = CodecDescriptionList.isEncoderSupportedByFormat(format1);
boolean flag8 = CodecDescriptionList.isEncoderSupportedByFormat(format2);
HiLog.info(hiLogLabel, "format1:" + flag7 + ",format2:" + flag8);
2、视频编码解码
2.1、普通模式
在普通模式下进行编解码,应用必须持续地传输数据到
Codec
实例。
- 编码
/**
* @description 测试普通模式视频编码
* @author PengHuAnZhi
* @date 2021/1/15 13:24
*/
private void testCodecEncode() {
//1、创建编码Codec实例,可调用createEncoder()创建。
final Codec encoder = Codec.createEncoder();
//2、构造数据源格式,并设置给Codec实例
Format format = new Format();
format.putStringValue(Format.MIME, Format.VIDEO_AVC);
format.putIntValue(Format.WIDTH, 1920);
format.putIntValue(Format.HEIGHT, 1080);
format.putIntValue(Format.BIT_RATE, 392000);
format.putIntValue(Format.FRAME_RATE, 30);
format.putIntValue(Format.FRAME_INTERVAL, -1);
encoder.setCodecFormat(format);
//3、设置监听器
encoder.registerCodecListener(new Codec.ICodecListener() {
//读到buffer时,获取buffer的format格式,异常时抛出运行时异常,代码示例如下:
@Override
public void onReadBuffer(ByteBuffer byteBuffer, BufferInfo bufferInfo, int i) {
}
@Override
public void onError(int i, int i1, int i2) {
throw new RuntimeException();
}
});
//4、调用start方法开始编码
encoder.start();
//5、调用getAvailableBuffer()取到一个可用的ByteBuffer,把数据填入ByteBuffer里,然后再调用writeBuffer()把ByteBuffer写入编码器实例
/**
* getAvailableBuffer
* 指示编解码器可以等待ByteBuffer的持续时间(以毫秒为单位)。如果在达到指定的持续时间后没有可用的ByteBuffer,
* 则返回null值。如果将此参数设置为-1,则该方法将被阻塞,直到获得字节缓冲区或发生错误为止
*/
ByteBuffer byteBuffer = encoder.getAvailableBuffer(2000