整理代码

This commit is contained in:
ziyue 2021-06-28 10:35:08 +08:00
parent 430229fe94
commit 8f730b8dab
6 changed files with 179 additions and 186 deletions

View File

@ -192,7 +192,8 @@ bool FrameMerger::willFlush(const Frame::Ptr &frame) const{
switch (frame->getCodecId()) {
case CodecH264 : {
auto type = H264_TYPE(frame->data()[frame->prefixSize()]);
if ((frame->data()[frame->prefixSize()+1]&0x80) !=0 && type >=H264Frame::NAL_B_P && type<=H264Frame::NAL_IDR ) {// sei aud pps sps 不判断
if ((frame->data()[frame->prefixSize() + 1] & 0x80) != 0 && type >= H264Frame::NAL_B_P &&
type <= H264Frame::NAL_IDR) {// sei aud pps sps 不判断
//264 新一帧的开始,刷新输出
return true;
} else {
@ -203,7 +204,8 @@ bool FrameMerger::willFlush(const Frame::Ptr &frame) const{
}
case CodecH265 : {
auto type = H265_TYPE(frame->data()[frame->prefixSize()]);
if ((type>=H265Frame::NAL_TRAIL_R &&type<= H265Frame::NAL_RSV_IRAP_VCL23) && ( (frame->data()[frame->prefixSize()+2]>>7 & 0x01) != 0)) {
if ((type >= H265Frame::NAL_TRAIL_R && type <= H265Frame::NAL_RSV_IRAP_VCL23) &&
((frame->data()[frame->prefixSize() + 2] >> 7 & 0x01) != 0)) {
//first_slice_segment_in_pic_flag is frame start
return true;
} else {
@ -244,6 +246,7 @@ void FrameMerger::doMerge(BufferLikeString &merged, const Frame::Ptr &frame) con
default: /*不可达*/ assert(0); break;
}
}
bool FrameMerger::shouldDrop(const Frame::Ptr &frame) const{
switch (frame->getCodecId()) {
case CodecH264: {
@ -263,10 +266,12 @@ bool FrameMerger::shouldDrop(const Frame::Ptr &frame) const{
}
break;
}
default: break;
default:
break;
}
return false;
}
bool FrameMerger::frameCacheHasVCL(List<Frame::Ptr> &frameCached) const {
bool hasVCL = false;
bool isH264OrH265 = false;
@ -291,15 +296,16 @@ bool FrameMerger::frameCacheHasVCL(List<Frame::Ptr> &frameCached) const{
isH264OrH265 = true;
break;
}
default: break;
default:
break;
}
});
if (isH264OrH265) {
return hasVCL;
}
return true;
}
void FrameMerger::inputFrame(const Frame::Ptr &frame, const onOutput &cb) {
if (shouldDrop(frame)) {
return;

View File

@ -95,22 +95,21 @@ void H264RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
}
if (pkt->buffer.size() > 9) {
auto iTotalLen = pkt->buffer.size();
size_t iOffset = 5;
auto total_len = pkt->buffer.size();
size_t offset = 5;
uint8_t *cts_ptr = (uint8_t *) (pkt->buffer.data() + 2);
int32_t cts = (((cts_ptr[0] << 16) | (cts_ptr[1] << 8) | (cts_ptr[2])) + 0xff800000) ^ 0xff800000;
auto pts = pkt->time_stamp + cts;
while(iOffset + 4 < iTotalLen){
uint32_t iFrameLen;
memcpy(&iFrameLen, pkt->buffer.data() + iOffset, 4);
iFrameLen = ntohl(iFrameLen);
iOffset += 4;
if(iFrameLen + iOffset > iTotalLen){
while (offset + 4 < total_len) {
uint32_t frame_len;
memcpy(&frame_len, pkt->buffer.data() + offset, 4);
frame_len = ntohl(frame_len);
offset += 4;
if (frame_len + offset > total_len) {
break;
}
onGetH264(pkt->buffer.data() + iOffset, iFrameLen, pkt->time_stamp , pts);
iOffset += iFrameLen;
onGetH264(pkt->buffer.data() + offset, frame_len, pkt->time_stamp, pts);
offset += frame_len;
}
}
}
@ -151,79 +150,73 @@ void H264RtmpEncoder::makeConfigPacket(){
if (!_sps.empty() && !_pps.empty()) {
//获取到sps/pps
makeVideoConfigPkt();
_gotSpsPps = true;
_got_config_frame = true;
}
}
void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
auto pcData = frame->data() + frame->prefixSize();
auto iLen = frame->size() - frame->prefixSize();
auto type = H264_TYPE(((uint8_t*)pcData)[0]);
if(type == H264Frame::NAL_SEI || type == H264Frame::NAL_AUD){
return;
}
if (!_gotSpsPps) {
//尝试从frame中获取sps pps
auto data = frame->data() + frame->prefixSize();
auto len = frame->size() - frame->prefixSize();
auto type = H264_TYPE(((uint8_t*)data)[0]);
switch (type) {
case H264Frame::NAL_SEI:
case H264Frame::NAL_AUD: return;
case H264Frame::NAL_SPS: {
//sps
_sps = string(pcData, iLen);
if (!_got_config_frame) {
_sps = string(data, len);
makeConfigPacket();
}
break;
}
case H264Frame::NAL_PPS: {
//pps
_pps = string(pcData, iLen);
if (!_got_config_frame) {
_pps = string(data, len);
makeConfigPacket();
}
break;
}
default:
break;
}
}
if(frame->configFrame() && _lastPacket &&_lastPacketHasVCL){
//sps pps flush frame
RtmpCodec::inputRtmp(_lastPacket);
_lastPacket = nullptr;
_lastPacketHasVCL = false;
default : break;
}
if(_lastPacket && (_lastPacket->time_stamp != frame->dts() || ((pcData[1]&0x80) != 0 && type>=H264Frame::NAL_B_P && type<=H264Frame::NAL_IDR && _lastPacketHasVCL))) {
RtmpCodec::inputRtmp(_lastPacket);
_lastPacket = nullptr;
_lastPacketHasVCL = false;
if (frame->configFrame() && _rtmp_packet && _has_vcl) {
//sps pps flush frame
RtmpCodec::inputRtmp(_rtmp_packet);
_has_vcl = false;
_rtmp_packet = nullptr;
}
if (_rtmp_packet && (_rtmp_packet->time_stamp != frame->dts() || ((data[1] & 0x80) != 0 && type >= H264Frame::NAL_B_P && type <= H264Frame::NAL_IDR && _has_vcl))) {
RtmpCodec::inputRtmp(_rtmp_packet);
_has_vcl = false;
_rtmp_packet = nullptr;
}
if (type >= H264Frame::NAL_B_P && type <= H264Frame::NAL_IDR) {
_lastPacketHasVCL = true;
_has_vcl = true;
}
if(!_lastPacket) {
if (!_rtmp_packet) {
//I or P or B frame
int8_t flags = FLV_CODEC_H264;
bool is_config = false;
flags |= (((frame->configFrame() || frame->keyFrame()) ? FLV_KEY_FRAME : FLV_INTER_FRAME) << 4);
_lastPacket = RtmpPacket::create();
_lastPacket->buffer.push_back(flags);
_lastPacket->buffer.push_back(!is_config);
_rtmp_packet = RtmpPacket::create();
_rtmp_packet->buffer.push_back(flags);
_rtmp_packet->buffer.push_back(!is_config);
int32_t cts = frame->pts() - frame->dts();
if (cts < 0) {
cts = 0;
}
cts = htonl(cts);
_lastPacket->buffer.append((char *)&cts + 1, 3);
_lastPacket->chunk_id = CHUNK_VIDEO;
_lastPacket->stream_index = STREAM_MEDIA;
_lastPacket->time_stamp = frame->dts();
_lastPacket->type_id = MSG_VIDEO;
_rtmp_packet->buffer.append((char *) &cts + 1, 3);
_rtmp_packet->chunk_id = CHUNK_VIDEO;
_rtmp_packet->stream_index = STREAM_MEDIA;
_rtmp_packet->time_stamp = frame->dts();
_rtmp_packet->type_id = MSG_VIDEO;
}
uint32_t size = htonl((uint32_t)iLen);
_lastPacket->buffer.append((char *) &size, 4);
_lastPacket->buffer.append(pcData, iLen);
_lastPacket->body_size = _lastPacket->buffer.size();
uint32_t size = htonl((uint32_t) len);
_rtmp_packet->buffer.append((char *) &size, 4);
_rtmp_packet->buffer.append(data, len);
_rtmp_packet->body_size = _rtmp_packet->buffer.size();
}
void H264RtmpEncoder::makeVideoConfigPkt() {

View File

@ -78,10 +78,10 @@ public:
private:
void makeVideoConfigPkt();
private:
bool _has_vcl = false;
bool _got_config_frame = false;
H264Track::Ptr _track;
bool _gotSpsPps = false;
bool _lastPacketHasVCL = false;
RtmpPacket::Ptr _lastPacket;
RtmpPacket::Ptr _rtmp_packet;
};
}//namespace mediakit

View File

@ -73,22 +73,21 @@ void H265RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
}
if (pkt->buffer.size() > 9) {
auto iTotalLen = pkt->buffer.size();
size_t iOffset = 5;
auto total_len = pkt->buffer.size();
size_t offset = 5;
uint8_t *cts_ptr = (uint8_t *) (pkt->buffer.data() + 2);
int32_t cts = (((cts_ptr[0] << 16) | (cts_ptr[1] << 8) | (cts_ptr[2])) + 0xff800000) ^ 0xff800000;
auto pts = pkt->time_stamp + cts;
while(iOffset + 4 < iTotalLen){
uint32_t iFrameLen;
memcpy(&iFrameLen, pkt->buffer.data() + iOffset, 4);
iFrameLen = ntohl(iFrameLen);
iOffset += 4;
if(iFrameLen + iOffset > iTotalLen){
while (offset + 4 < total_len) {
uint32_t frame_len;
memcpy(&frame_len, pkt->buffer.data() + offset, 4);
frame_len = ntohl(frame_len);
offset += 4;
if (frame_len + offset > total_len) {
break;
}
onGetH265(pkt->buffer.data() + iOffset, iFrameLen, pkt->time_stamp , pts);
iOffset += iFrameLen;
onGetH265(pkt->buffer.data() + offset, frame_len, pkt->time_stamp, pts);
offset += frame_len;
}
}
}
@ -130,87 +129,85 @@ void H265RtmpEncoder::makeConfigPacket(){
if (!_sps.empty() && !_pps.empty() && !_vps.empty()) {
//获取到sps/pps
makeVideoConfigPkt();
_gotSpsPps = true;
_got_config_frame = true;
}
}
void H265RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
auto pcData = frame->data() + frame->prefixSize();
auto iLen = frame->size() - frame->prefixSize();
auto type = H265_TYPE(((uint8_t*)pcData)[0]);
auto data = frame->data() + frame->prefixSize();
auto len = frame->size() - frame->prefixSize();
auto type = H265_TYPE(((uint8_t*)data)[0]);
if (!_gotSpsPps) {
//尝试从frame中获取sps pps
switch (type) {
case H265Frame::NAL_SPS: {
//sps
_sps = string(pcData, iLen);
if (!_got_config_frame) {
_sps = string(data, len);
makeConfigPacket();
}
break;
}
case H265Frame::NAL_PPS: {
//pps
_pps = string(pcData, iLen);
if (!_got_config_frame) {
_pps = string(data, len);
makeConfigPacket();
}
break;
}
case H265Frame::NAL_VPS: {
//vps
_vps = string(pcData, iLen);
if (!_got_config_frame) {
_vps = string(data, len);
makeConfigPacket();
break;
}
default:
break;
}
case H265Frame::NAL_AUD:
case H265Frame::NAL_SEI_PREFIX:
case H265Frame::NAL_SEI_SUFFIX: return;
default: break;
}
if(type == H265Frame::NAL_SEI_PREFIX || type == H265Frame::NAL_SEI_SUFFIX || type == H265Frame::NAL_AUD){
return;// 防止sei aud 作为一帧
}
if(frame->configFrame() && _lastPacket &&_lastPacketHasVCL){
if(frame->configFrame() && _rtmp_packet && _has_vcl){
// sps pps flush frame
RtmpCodec::inputRtmp(_lastPacket);
_lastPacket = nullptr;
_lastPacketHasVCL = false;
RtmpCodec::inputRtmp(_rtmp_packet);
_rtmp_packet = nullptr;
_has_vcl = false;
}
if (_lastPacket && (_lastPacket->time_stamp != frame->dts() || (_lastPacketHasVCL &&type>=H265Frame::NAL_TRAIL_R &&type<= H265Frame::NAL_RSV_IRAP_VCL23 && (pcData[2]>>7 &0x01) !=0))) {
RtmpCodec::inputRtmp(_lastPacket);
_lastPacket = nullptr;
_lastPacketHasVCL = false;
if (_rtmp_packet && (_rtmp_packet->time_stamp != frame->dts() || (_has_vcl && type >= H265Frame::NAL_TRAIL_R && type <= H265Frame::NAL_RSV_IRAP_VCL23 && (data[2] >> 7 & 0x01) != 0))) {
RtmpCodec::inputRtmp(_rtmp_packet);
_has_vcl = false;
_rtmp_packet = nullptr;
}
if (type >= H265Frame::NAL_TRAIL_R && type <= H265Frame::NAL_RSV_IRAP_VCL23) {
_lastPacketHasVCL = true;
_has_vcl = true;
}
if(!_lastPacket) {
if (!_rtmp_packet) {
//I or P or B frame
int8_t flags = FLV_CODEC_H265;
bool is_config = false;
flags |= (((frame->configFrame() || frame->keyFrame()) ? FLV_KEY_FRAME : FLV_INTER_FRAME) << 4);
// to do
// 必须是IDR帧才能是关键帧否则有可能开始帧会花屏 SPS PPS VPS 打头的是一般I帧但不一定是IDR帧
// todo 必须是IDR帧才能是关键帧否则有可能开始帧会花屏 SPS PPS VPS 打头的是一般I帧但不一定是IDR帧
// RtmpCodec::inputRtmp 时需要判断 是否是IDR帧,做出相应的修改
_lastPacket = RtmpPacket::create();
_lastPacket->buffer.push_back(flags);
_lastPacket->buffer.push_back(!is_config);
auto cts = frame->pts() - frame->dts();
cts = htonl(cts);
_lastPacket->buffer.append((char *)&cts + 1, 3);
_lastPacket->chunk_id = CHUNK_VIDEO;
_lastPacket->stream_index = STREAM_MEDIA;
_lastPacket->time_stamp = frame->dts();
_lastPacket->type_id = MSG_VIDEO;
_rtmp_packet = RtmpPacket::create();
_rtmp_packet->buffer.push_back(flags);
_rtmp_packet->buffer.push_back(!is_config);
int32_t cts = frame->pts() - frame->dts();
if (cts < 0) {
cts = 0;
}
uint32_t size = htonl((uint32_t)iLen);
_lastPacket->buffer.append((char *) &size, 4);
_lastPacket->buffer.append(pcData, iLen);
_lastPacket->body_size = _lastPacket->buffer.size();
cts = htonl(cts);
_rtmp_packet->buffer.append((char *) &cts + 1, 3);
_rtmp_packet->chunk_id = CHUNK_VIDEO;
_rtmp_packet->stream_index = STREAM_MEDIA;
_rtmp_packet->time_stamp = frame->dts();
_rtmp_packet->type_id = MSG_VIDEO;
}
uint32_t size = htonl((uint32_t) len);
_rtmp_packet->buffer.append((char *) &size, 4);
_rtmp_packet->buffer.append(data, len);
_rtmp_packet->body_size = _rtmp_packet->buffer.size();
}
void H265RtmpEncoder::makeVideoConfigPkt() {
@ -218,7 +215,6 @@ void H265RtmpEncoder::makeVideoConfigPkt() {
int8_t flags = FLV_CODEC_H265;
flags |= (FLV_KEY_FRAME << 4);
bool is_config = true;
auto rtmpPkt = RtmpPacket::create();
//header
rtmpPkt->buffer.push_back(flags);
@ -237,10 +233,8 @@ void H265RtmpEncoder::makeVideoConfigPkt() {
WarnL << "生成H265 extra_data 失败";
return;
}
//HEVCDecoderConfigurationRecord
rtmpPkt->buffer.append((char *)extra_data, extra_data_size);
rtmpPkt->body_size = rtmpPkt->buffer.size();
rtmpPkt->chunk_id = CHUNK_VIDEO;
rtmpPkt->stream_index = STREAM_MEDIA;

View File

@ -78,13 +78,13 @@ private:
void makeVideoConfigPkt();
private:
bool _gotSpsPps = false;
bool _has_vcl = false;
bool _got_config_frame = false;
string _vps;
string _sps;
string _pps;
H265Track::Ptr _track;
RtmpPacket::Ptr _lastPacket;
bool _lastPacketHasVCL = false;
RtmpPacket::Ptr _rtmp_packet;
};
}//namespace mediakit