2018-10-30 14:59:42 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2018-10-30 14:59:42 +08:00
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2018-10-30 14:59:42 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2019-06-28 17:37:11 +08:00
|
|
|
|
#include "H265Rtp.h"
|
2018-10-30 14:59:42 +08:00
|
|
|
|
|
|
|
|
|
namespace mediakit{
|
|
|
|
|
|
2018-10-30 17:11:36 +08:00
|
|
|
|
//41
|
|
|
|
|
//42 0 1
|
|
|
|
|
//43 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
|
|
|
|
|
//44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|
//45 |F| Type | LayerId | TID |
|
|
|
|
|
//46 +-------------+-----------------+
|
|
|
|
|
//48 F = 0
|
|
|
|
|
//49 Type = 49 (fragmentation unit (FU))
|
|
|
|
|
//50 LayerId = 0
|
|
|
|
|
//51 TID = 1
|
|
|
|
|
//56 /*
|
|
|
|
|
//57 create the FU header
|
|
|
|
|
//58
|
|
|
|
|
//59 0 1 2 3 4 5 6 7
|
|
|
|
|
//60 +-+-+-+-+-+-+-+-+
|
|
|
|
|
//61 |S|E| FuType |
|
|
|
|
|
//62 +---------------+
|
|
|
|
|
//63
|
|
|
|
|
//64 S = variable
|
|
|
|
|
//65 E = variable
|
|
|
|
|
//66 FuType = NAL unit type
|
|
|
|
|
//67
|
2018-10-30 14:59:42 +08:00
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
unsigned S :1;
|
|
|
|
|
unsigned E :1;
|
2018-10-30 17:11:36 +08:00
|
|
|
|
unsigned type :6;
|
2018-10-30 14:59:42 +08:00
|
|
|
|
} FU;
|
|
|
|
|
|
2018-10-30 17:11:36 +08:00
|
|
|
|
static void MakeFU(uint8_t in, FU &fu) {
|
2018-10-30 14:59:42 +08:00
|
|
|
|
fu.S = in >> 7;
|
|
|
|
|
fu.E = (in >> 6) & 0x01;
|
2018-10-30 17:11:36 +08:00
|
|
|
|
fu.type = in & 0x3f;
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
H265RtpDecoder::H265RtpDecoder() {
|
|
|
|
|
_h265frame = obtainFrame();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
H265Frame::Ptr H265RtpDecoder::obtainFrame() {
|
|
|
|
|
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
|
|
|
|
|
auto frame = ResourcePoolHelper<H265Frame>::obtainObj();
|
2020-01-14 10:04:24 +08:00
|
|
|
|
frame->_buffer.clear();
|
|
|
|
|
frame->_prefix_size = 4;
|
2018-10-30 14:59:42 +08:00
|
|
|
|
return frame;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool H265RtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
|
2019-12-26 09:43:44 +08:00
|
|
|
|
return decodeRtp(rtp);
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool H265RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
|
2019-06-24 16:07:44 +08:00
|
|
|
|
const uint8_t *frame = (uint8_t *) rtppack->data() + rtppack->offset;
|
|
|
|
|
int length = rtppack->size() - rtppack->offset;
|
2018-10-30 17:11:36 +08:00
|
|
|
|
int nal = H265_TYPE(frame[0]);
|
2018-10-30 14:59:42 +08:00
|
|
|
|
|
2018-10-30 17:11:36 +08:00
|
|
|
|
if (nal > 50){
|
|
|
|
|
WarnL << "不支持该类型的265 RTP包" << nal;
|
|
|
|
|
return false; // packet discard, Unsupported (HEVC) NAL type
|
|
|
|
|
}
|
|
|
|
|
switch (nal) {
|
|
|
|
|
case 50:
|
|
|
|
|
case 48: // aggregated packet (AP) - with two or more NAL units
|
|
|
|
|
WarnL << "不支持该类型的265 RTP包" << nal;
|
|
|
|
|
return false;
|
|
|
|
|
case 49: {
|
|
|
|
|
// fragmentation unit (FU)
|
|
|
|
|
FU fu;
|
2018-10-30 17:35:06 +08:00
|
|
|
|
MakeFU(frame[2], fu);
|
2019-08-01 18:49:04 +08:00
|
|
|
|
if (fu.S) {
|
|
|
|
|
//该帧的第一个rtp包
|
2020-01-14 10:04:24 +08:00
|
|
|
|
_h265frame->_buffer.assign("\x0\x0\x0\x1", 4);
|
|
|
|
|
_h265frame->_buffer.push_back(fu.type << 1);
|
|
|
|
|
_h265frame->_buffer.push_back(0x01);
|
|
|
|
|
_h265frame->_buffer.append((char *) frame + 3, length - 3);
|
|
|
|
|
_h265frame->_pts = rtppack->timeStamp;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
//该函数return时,保存下当前sequence,以便下次对比seq是否连续
|
|
|
|
|
_lastSeq = rtppack->sequence;
|
2018-10-30 17:11:36 +08:00
|
|
|
|
return (_h265frame->keyFrame()); //i frame
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 18:49:04 +08:00
|
|
|
|
if (rtppack->sequence != _lastSeq + 1 && rtppack->sequence != 0) {
|
|
|
|
|
//中间的或末尾的rtp包,其seq必须连续(如果回环了则判定为连续),否则说明rtp丢包,那么该帧不完整,必须得丢弃
|
2020-01-14 10:04:24 +08:00
|
|
|
|
_h265frame->_buffer.clear();
|
2019-08-01 18:49:04 +08:00
|
|
|
|
WarnL << "rtp sequence不连续: " << rtppack->sequence << " != " << _lastSeq << " + 1,该帧被废弃";
|
2018-10-30 17:11:36 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-08-01 18:49:04 +08:00
|
|
|
|
|
|
|
|
|
if (!fu.E) {
|
|
|
|
|
//该帧的中间rtp包
|
2020-01-14 10:04:24 +08:00
|
|
|
|
_h265frame->_buffer.append((char *) frame + 3, length - 3);
|
2019-08-01 18:49:04 +08:00
|
|
|
|
//该函数return时,保存下当前sequence,以便下次对比seq是否连续
|
|
|
|
|
_lastSeq = rtppack->sequence;
|
|
|
|
|
return false;
|
2018-10-30 17:11:36 +08:00
|
|
|
|
}
|
2019-08-01 18:49:04 +08:00
|
|
|
|
|
|
|
|
|
//该帧最后一个rtp包
|
2020-01-14 10:04:24 +08:00
|
|
|
|
_h265frame->_buffer.append((char *) frame + 3, length - 3);
|
|
|
|
|
_h265frame->_pts = rtppack->timeStamp;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
onGetH265(_h265frame);
|
2020-01-14 19:41:55 +08:00
|
|
|
|
return false;
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
2018-10-30 21:05:48 +08:00
|
|
|
|
|
2018-10-30 17:11:36 +08:00
|
|
|
|
default: // 4.4.1. Single NAL Unit Packets (p24)
|
|
|
|
|
//a full frame
|
2020-01-14 10:04:24 +08:00
|
|
|
|
_h265frame->_buffer.assign("\x0\x0\x0\x1", 4);
|
|
|
|
|
_h265frame->_buffer.append((char *)frame, length);
|
|
|
|
|
_h265frame->_pts = rtppack->timeStamp;
|
2019-08-01 18:49:04 +08:00
|
|
|
|
auto key = _h265frame->keyFrame();
|
2018-10-30 14:59:42 +08:00
|
|
|
|
onGetH265(_h265frame);
|
2019-08-01 18:49:04 +08:00
|
|
|
|
return key;
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void H265RtpDecoder::onGetH265(const H265Frame::Ptr &frame) {
|
2020-04-29 18:05:29 +08:00
|
|
|
|
//rtsp没有dts,那么根据pts排序算法生成dts
|
|
|
|
|
_dts_generator.getDts(frame->_pts,frame->_dts);
|
|
|
|
|
//写入环形缓存
|
|
|
|
|
RtpCodec::inputFrame(frame);
|
2018-10-30 14:59:42 +08:00
|
|
|
|
_h265frame = obtainFrame();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
H265RtpEncoder::H265RtpEncoder(uint32_t ui32Ssrc,
|
|
|
|
|
uint32_t ui32MtuSize,
|
|
|
|
|
uint32_t ui32SampleRate,
|
2020-05-25 13:51:00 +08:00
|
|
|
|
uint8_t ui8PayloadType,
|
2018-10-30 14:59:42 +08:00
|
|
|
|
uint8_t ui8Interleaved) :
|
|
|
|
|
RtpInfo(ui32Ssrc,
|
|
|
|
|
ui32MtuSize,
|
|
|
|
|
ui32SampleRate,
|
2020-05-25 13:51:00 +08:00
|
|
|
|
ui8PayloadType,
|
2018-10-30 14:59:42 +08:00
|
|
|
|
ui8Interleaved) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void H265RtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
2019-05-28 17:14:36 +08:00
|
|
|
|
GET_CONFIG(uint32_t,cycleMS,Rtp::kCycleMS);
|
2018-10-30 21:05:48 +08:00
|
|
|
|
uint8_t *pcData = (uint8_t*)frame->data() + frame->prefixSize();
|
2020-01-14 10:04:24 +08:00
|
|
|
|
auto uiStamp = frame->pts();
|
2018-10-30 14:59:42 +08:00
|
|
|
|
auto iLen = frame->size() - frame->prefixSize();
|
2018-10-30 21:05:48 +08:00
|
|
|
|
unsigned char naluType = H265_TYPE(pcData[0]); //获取NALU的5bit 帧类型
|
2018-10-30 14:59:42 +08:00
|
|
|
|
uiStamp %= cycleMS;
|
2018-10-30 21:05:48 +08:00
|
|
|
|
|
|
|
|
|
int maxSize = _ui32MtuSize - 3;
|
2019-09-06 23:22:04 +08:00
|
|
|
|
//超过MTU,按照FU方式打包
|
|
|
|
|
if (iLen > maxSize) {
|
2018-10-30 14:59:42 +08:00
|
|
|
|
//获取帧头数据,1byte
|
2019-09-06 23:23:43 +08:00
|
|
|
|
unsigned char s_e_flags;
|
2018-10-30 14:59:42 +08:00
|
|
|
|
bool bFirst = true;
|
|
|
|
|
bool mark = false;
|
2018-10-30 17:58:10 +08:00
|
|
|
|
int nOffset = 2;
|
2018-10-30 14:59:42 +08:00
|
|
|
|
while (!mark) {
|
2018-10-30 21:05:48 +08:00
|
|
|
|
if (iLen < nOffset + maxSize) { //是否拆分结束
|
|
|
|
|
maxSize = iLen - nOffset;
|
2018-10-30 14:59:42 +08:00
|
|
|
|
mark = true;
|
2019-09-06 23:22:04 +08:00
|
|
|
|
//FU end
|
2019-09-06 23:23:43 +08:00
|
|
|
|
s_e_flags = (1 << 6) | naluType;
|
2018-11-19 16:15:47 +08:00
|
|
|
|
} else if (bFirst) {
|
2019-09-06 23:22:04 +08:00
|
|
|
|
//FU start
|
2019-09-06 23:23:43 +08:00
|
|
|
|
s_e_flags = (1 << 7) | naluType;
|
2018-11-19 16:15:47 +08:00
|
|
|
|
} else {
|
2019-09-06 23:22:04 +08:00
|
|
|
|
//FU mid
|
2019-09-06 23:23:43 +08:00
|
|
|
|
s_e_flags = naluType;
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
2018-11-19 16:15:47 +08:00
|
|
|
|
|
2019-09-06 15:16:22 +08:00
|
|
|
|
{
|
|
|
|
|
//传入nullptr先不做payload的内存拷贝
|
|
|
|
|
auto rtp = makeRtp(getTrackType(), nullptr, maxSize + 3, mark, uiStamp);
|
|
|
|
|
//rtp payload 负载部分
|
|
|
|
|
uint8_t *payload = (uint8_t*)rtp->data() + rtp->offset;
|
2019-09-06 23:22:04 +08:00
|
|
|
|
//FU 第1个字节,表明为FU
|
2019-09-06 15:16:22 +08:00
|
|
|
|
payload[0] = 49 << 1;
|
2019-09-06 23:22:04 +08:00
|
|
|
|
//FU 第2个字节貌似固定为1
|
2019-09-06 15:16:22 +08:00
|
|
|
|
payload[1] = 1;
|
2019-09-06 23:22:04 +08:00
|
|
|
|
//FU 第3个字节
|
2019-09-06 23:23:43 +08:00
|
|
|
|
payload[2] = s_e_flags;
|
2019-09-06 15:16:22 +08:00
|
|
|
|
//H265 数据
|
|
|
|
|
memcpy(payload + 3,pcData + nOffset, maxSize);
|
|
|
|
|
//输入到rtp环形缓存
|
|
|
|
|
RtpCodec::inputRtp(rtp,bFirst && H265Frame::isKeyFrame(naluType));
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 21:05:48 +08:00
|
|
|
|
nOffset += maxSize;
|
2018-11-19 16:15:47 +08:00
|
|
|
|
bFirst = false;
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-11-19 16:15:47 +08:00
|
|
|
|
makeH265Rtp(naluType,pcData, iLen, true, true, uiStamp);
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-19 16:15:47 +08:00
|
|
|
|
void H265RtpEncoder::makeH265Rtp(int nal_type,const void* data, unsigned int len, bool mark, bool first_packet, uint32_t uiStamp) {
|
|
|
|
|
RtpCodec::inputRtp(makeRtp(getTrackType(),data,len,mark,uiStamp),first_packet && H265Frame::isKeyFrame(nal_type));
|
2018-10-30 14:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}//namespace mediakit
|