Android H265投屏
H265特点
- H265将宏块的大小从H264的16x16扩展到了64x64,以便于高分辨率视频的压缩
- H265采用了更加灵活的编码结构来提高编码效率
包括编码单元(类似H264宏块,用于编码)、预测单元和变换单 - H265帧内预测
- H265:所有的CU块,亮度有35种预测方向,色度5种
- H264:亮度4x4和8x8块都是9个方向,16x16是4种方向,色度4种方向
H265码流分析
-
H264的帧类型,因为H264是后5位保存帧类型数据,所以与1F即可
-
H265的帧类型:将value&7E>>1就可以得到帧类型
以40 01为例
0100 0000 40 & 0111 1110 7E = 0100 0000 40 >>1 0010 0000 =32 //vps
42 01为例
0100 0010 42 & 0111 1110 7E = 0100 0010 42 >>1 0010 0001 =33 //sps
- 推送端
public void startLive() {
try {
//服务器端编码H264通过socket发送给客户端
MediaFormat format = MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_HEVC, mWidth, mHeight);
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
format.setInteger(KEY_BIT_RATE, mWidth * mHeight);
format.setInteger(KEY_FRAME_RATE, 20);
mMediaCodec = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_VIDEO_HEVC);
mMediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
//创建场地
Surface surface = mMediaCodec.createInputSurface();
mVirtualDisplay = mMediaProjection.createVirtualDisplay("CodecLiveH265",
mWidth, mHeight, 1, DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC, surface, null, null);
mHandler.post(this);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
mMediaCodec.start();
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
while (true) {
//取出数据发送给客户端
int outIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, 1000);
if (outIndex >= 0) {
ByteBuffer buffer = mMediaCodec.getOutputBuffer(outIndex);
dealFrame(buffer, bufferInfo);
mMediaCodec.releaseOutputBuffer(outIndex, false);
}
}
}
public static final int NAL_I = 19;
public static final int NAL_VPS = 32;
//vps+sps+pps是一帧,所以只需要获取vps
private byte[] vps_sps_pps_buffer;
private void dealFrame(ByteBuffer buffer, MediaCodec.BufferInfo bufferInfo) {
//过滤掉第一个0x00 00 00 01 或者0x 00 00 01
int offset = 4;
if (buffer.get(2) == 0x01) {
offset = 3;
}
//获取帧类型
int type = (buffer.get(offset) & 0x7E) >> 1;
if (type == NAL_VPS) {
vps_sps_pps_buffer = new byte[bufferInfo.size];
buffer.get(vps_sps_pps_buffer);
} else if (type == NAL_I) {
//I帧
final byte[] bytes = new byte[bufferInfo.size];
buffer.get(bytes);
//vps_pps_sps+I帧的数据
byte[] newBuffer = new byte[vps_sps_pps_buffer.length + bytes.length];
System.arraycopy(vps_sps_pps_buffer, 0, newBuffer, 0, vps_sps_pps_buffer.length);
System.arraycopy(bytes, 0, newBuffer, vps_sps_pps_buffer.length, bytes.length);
mWebSocketSendLive.sendData(newBuffer);
}else{
//P帧 B帧直接发送就可以了
final byte[] bytes = new byte[bufferInfo.size];
buffer.get(bytes);
mWebSocketSendLive.sendData(bytes);
}
}
接收端
//初始化解码器
private fun initDecoder(surface: Surface?) {
mMediaCodec = MediaCodec.createDecoderByType(MediaFormat.MIMETYPE_VIDEO_HEVC)
val format =
MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_HEVC, mWidth, mHeight)
format.setInteger(MediaFormat.KEY_BIT_RATE, mWidth * mHeight)
format.setInteger(MediaFormat.KEY_FRAME_RATE, 20)
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1)
mMediaCodec.configure(
format,
surface,
null, 0
)
mMediaCodec.start()
}
override fun callBack(data: ByteArray?) {
//回调
LogUtils.e("接收到数据的长度:${data?.size}")
//客户端主要将获取到的数据进行解码,首先需要通过dsp进行解码
val index = mMediaCodec.dequeueInputBuffer(10000)
if (index >= 0) {
val inputBuffer = mMediaCodec.getInputBuffer(index)
inputBuffer.clear()
inputBuffer.put(data, 0, data!!.size)
//通知dsp芯片帮忙解码
mMediaCodec.queueInputBuffer(index, 0, data.size, System.currentTimeMillis(), 0)
}
//取出数据
val bufferInfo = MediaCodec.BufferInfo()
var outIndex: Int = mMediaCodec.dequeueOutputBuffer(bufferInfo, 10000)
while (outIndex > 0) {
mMediaCodec.releaseOutputBuffer(outIndex, true)
outIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, 10000)
}
}