修复某些场景下rtmp定位关键帧不准确的bug

This commit is contained in:
xiongziliang 2020-09-06 18:22:04 +08:00
parent 51ae8d4083
commit 0878f776b3
13 changed files with 43 additions and 67 deletions

View File

@ -29,17 +29,16 @@ static string getAacCfg(const RtmpPacket &thiz) {
return ret;
}
bool AACRtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt, bool) {
void AACRtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
if (pkt->isCfgFrame()) {
_aac_cfg = getAacCfg(*pkt);
onGetAAC(nullptr, 0, 0);
return false;
return;
}
if (!_aac_cfg.empty()) {
onGetAAC(pkt->buffer.data() + 2, pkt->buffer.size() - 2, pkt->time_stamp);
}
return false;
}
void AACRtmpDecoder::onGetAAC(const char* data, int len, uint32_t stamp) {
@ -112,7 +111,7 @@ void AACRtmpEncoder::inputFrame(const Frame::Ptr &frame) {
rtmpPkt->stream_index = STREAM_MEDIA;
rtmpPkt->time_stamp = frame->dts();
rtmpPkt->type_id = MSG_AUDIO;
RtmpCodec::inputRtmp(rtmpPkt, false);
RtmpCodec::inputRtmp(rtmpPkt);
}
}
@ -133,7 +132,7 @@ void AACRtmpEncoder::makeAudioConfigPkt() {
rtmpPkt->stream_index = STREAM_MEDIA;
rtmpPkt->time_stamp = 0;
rtmpPkt->type_id = MSG_AUDIO;
RtmpCodec::inputRtmp(rtmpPkt, false);
RtmpCodec::inputRtmp(rtmpPkt);
}
}//namespace mediakit

View File

@ -28,10 +28,9 @@ public:
/**
* Rtmp并解码
* @param Rtmp Rtmp数据包
* @param key_pos false,
* @param rtmp Rtmp数据包
*/
bool inputRtmp(const RtmpPacket::Ptr &Rtmp, bool key_pos = false) override;
void inputRtmp(const RtmpPacket::Ptr &rtmp) override;
CodecId getCodecId() const override{
return CodecAAC;

View File

@ -29,7 +29,7 @@ void CommonRtmpDecoder::obtainFrame() {
_frame->_prefix_size = 0;
}
bool CommonRtmpDecoder::inputRtmp(const RtmpPacket::Ptr &rtmp, bool) {
void CommonRtmpDecoder::inputRtmp(const RtmpPacket::Ptr &rtmp) {
//拷贝负载
_frame->_buffer.assign(rtmp->buffer.data() + 1, rtmp->buffer.size() - 1);
_frame->_dts = rtmp->time_stamp;
@ -37,7 +37,6 @@ bool CommonRtmpDecoder::inputRtmp(const RtmpPacket::Ptr &rtmp, bool) {
RtmpCodec::inputFrame(_frame);
//创建下一帧
obtainFrame();
return false;
}
/////////////////////////////////////////////////////////////////////////////////////
@ -61,7 +60,7 @@ void CommonRtmpEncoder::inputFrame(const Frame::Ptr &frame) {
rtmp->stream_index = STREAM_MEDIA;
rtmp->time_stamp = frame->dts();
rtmp->type_id = MSG_AUDIO;
RtmpCodec::inputRtmp(rtmp, false);
RtmpCodec::inputRtmp(rtmp);
}
}//namespace mediakit

View File

@ -39,9 +39,8 @@ public:
/**
* Rtmp并解码
* @param rtmp Rtmp数据包
* @param key_pos false,
*/
bool inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos = false) override;
void inputRtmp(const RtmpPacket::Ptr &rtmp) override;
private:
void obtainFrame();

View File

@ -23,10 +23,6 @@ H264Frame::Ptr H264RtmpDecoder::obtainFrame() {
return frame;
}
bool H264RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos) {
return decodeRtmp(rtmp);
}
/**
* 0x00 00 00 01sps
* @return
@ -90,14 +86,14 @@ static string getH264PPS(const RtmpPacket &thiz) {
return ret;
}
bool H264RtmpDecoder::decodeRtmp(const RtmpPacket::Ptr &pkt) {
void H264RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
if (pkt->isCfgFrame()) {
//缓存sps pps后续插入到I帧之前
_sps = getH264SPS(*pkt);
_pps = getH264PPS(*pkt);
onGetH264(_sps.data(), _sps.size(), pkt->time_stamp , pkt->time_stamp);
onGetH264(_pps.data(), _pps.size(), pkt->time_stamp , pkt->time_stamp);
return false;
return;
}
if (pkt->buffer.size() > 9) {
@ -119,7 +115,6 @@ bool H264RtmpDecoder::decodeRtmp(const RtmpPacket::Ptr &pkt) {
iOffset += iFrameLen;
}
}
return pkt->isVideoKeyFrame();
}
inline void H264RtmpDecoder::onGetH264(const char* pcData, int iLen, uint32_t dts,uint32_t pts) {
@ -191,7 +186,7 @@ void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
}
if(_lastPacket && _lastPacket->time_stamp != frame->dts()) {
RtmpCodec::inputRtmp(_lastPacket, _lastPacket->isVideoKeyFrame());
RtmpCodec::inputRtmp(_lastPacket);
_lastPacket = nullptr;
}
@ -259,7 +254,7 @@ void H264RtmpEncoder::makeVideoConfigPkt() {
rtmpPkt->stream_index = STREAM_MEDIA;
rtmpPkt->time_stamp = 0;
rtmpPkt->type_id = MSG_VIDEO;
RtmpCodec::inputRtmp(rtmpPkt, false);
RtmpCodec::inputRtmp(rtmpPkt);
}
}//namespace mediakit

View File

@ -32,17 +32,17 @@ public:
/**
* 264 Rtmp包
* @param rtmp Rtmp包
* @param key_pos
*/
bool inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos = true) override;
void inputRtmp(const RtmpPacket::Ptr &rtmp) override;
CodecId getCodecId() const override{
return CodecH264;
}
protected:
bool decodeRtmp(const RtmpPacket::Ptr &Rtmp);
void onGetH264(const char *pcData, int iLen, uint32_t dts,uint32_t pts);
H264Frame::Ptr obtainFrame();
protected:
H264Frame::Ptr _h264frame;
string _sps;

View File

@ -27,10 +27,6 @@ H265Frame::Ptr H265RtmpDecoder::obtainFrame() {
return frame;
}
bool H265RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos) {
return decodeRtmp(rtmp);
}
#ifdef ENABLE_MP4
/**
* 0x00 00 00 01sps
@ -65,7 +61,7 @@ static bool getH265ConfigFrame(const RtmpPacket &thiz,string &frame) {
}
#endif
bool H265RtmpDecoder::decodeRtmp(const RtmpPacket::Ptr &pkt) {
void H265RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
if (pkt->isCfgFrame()) {
#ifdef ENABLE_MP4
string config;
@ -75,7 +71,7 @@ bool H265RtmpDecoder::decodeRtmp(const RtmpPacket::Ptr &pkt) {
#else
WarnL << "请开启MP4相关功能并使能\"ENABLE_MP4\",否则对H265-RTMP支持不完善";
#endif
return false;
return;
}
if (pkt->buffer.size() > 9) {
@ -97,7 +93,6 @@ bool H265RtmpDecoder::decodeRtmp(const RtmpPacket::Ptr &pkt) {
iOffset += iFrameLen;
}
}
return pkt->isVideoKeyFrame();
}
inline void H265RtmpDecoder::onGetH265(const char* pcData, int iLen, uint32_t dts,uint32_t pts) {
@ -177,7 +172,7 @@ void H265RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
}
if(_lastPacket && _lastPacket->time_stamp != frame->dts()) {
RtmpCodec::inputRtmp(_lastPacket, _lastPacket->isVideoKeyFrame());
RtmpCodec::inputRtmp(_lastPacket);
_lastPacket = nullptr;
}
@ -242,7 +237,7 @@ void H265RtmpEncoder::makeVideoConfigPkt() {
rtmpPkt->stream_index = STREAM_MEDIA;
rtmpPkt->time_stamp = 0;
rtmpPkt->type_id = MSG_VIDEO;
RtmpCodec::inputRtmp(rtmpPkt, false);
RtmpCodec::inputRtmp(rtmpPkt);
#else
WarnL << "请开启MP4相关功能并使能\"ENABLE_MP4\",否则对H265-RTMP支持不完善";
#endif

View File

@ -32,17 +32,17 @@ public:
/**
* 265 Rtmp包
* @param rtmp Rtmp包
* @param key_pos
*/
bool inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos = true) override;
void inputRtmp(const RtmpPacket::Ptr &rtmp) override;
CodecId getCodecId() const override{
return CodecH265;
}
protected:
bool decodeRtmp(const RtmpPacket::Ptr &Rtmp);
void onGetH265(const char *pcData, int iLen, uint32_t dts,uint32_t pts);
H265Frame::Ptr obtainFrame();
protected:
H265Frame::Ptr _h265frame;
};

View File

@ -45,14 +45,11 @@ public:
/**
* rtmp包
* @param rtmp rtmp包
* @param key_pos
* @return
*/
virtual bool inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos) {
virtual void inputRtmp(const RtmpPacket::Ptr &rtmp) {
if (_rtmpRing) {
_rtmpRing->write(rtmp, key_pos);
_rtmpRing->write(rtmp, rtmp->isVideoKeyFrame());
}
return key_pos;
}
protected:

View File

@ -65,34 +65,32 @@ bool RtmpDemuxer::loadMetaData(const AMFValue &val){
return ret;
}
bool RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) {
void RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) {
switch (pkt->type_id) {
case MSG_VIDEO: {
if(!_try_get_video_track){
if (!_try_get_video_track) {
_try_get_video_track = true;
auto codec = AMFValue(pkt->getMediaType());
makeVideoTrack(codec);
}
if(_video_rtmp_decoder){
return _video_rtmp_decoder->inputRtmp(pkt, true);
if (_video_rtmp_decoder) {
_video_rtmp_decoder->inputRtmp(pkt);
}
return false;
break;
}
case MSG_AUDIO: {
if(!_try_get_audio_track) {
if (!_try_get_audio_track) {
_try_get_audio_track = true;
auto codec = AMFValue(pkt->getMediaType());
makeAudioTrack(codec, pkt->getAudioSampleRate(), pkt->getAudioChannel(), pkt->getAudioSampleBit());
}
if(_audio_rtmp_decoder){
_audio_rtmp_decoder->inputRtmp(pkt, false);
return false;
if (_audio_rtmp_decoder) {
_audio_rtmp_decoder->inputRtmp(pkt);
}
return false;
break;
}
default:
return false;
default : break;
}
}

View File

@ -35,9 +35,8 @@ public:
/**
*
* @param pkt rtmp包
* @return true i帧
*/
bool inputRtmp(const RtmpPacket::Ptr &pkt);
void inputRtmp(const RtmpPacket::Ptr &pkt);
private:
void makeVideoTrack(const AMFValue &val);

View File

@ -117,9 +117,8 @@ public:
/**
* rtmp包
* @param pkt rtmp包
* @param key
*/
void onWrite(const RtmpPacket::Ptr &pkt, bool key = true) override {
void onWrite(const RtmpPacket::Ptr &pkt, bool = true) override {
//保存当前时间戳
switch (pkt->type_id) {
case MSG_VIDEO : _track_stamps[TrackVideo] = pkt->time_stamp, _have_video = true; break;
@ -152,7 +151,7 @@ public:
regist();
}
}
PacketCache<RtmpPacket>::inputPacket(pkt->type_id == MSG_VIDEO, pkt, key);
PacketCache<RtmpPacket>::inputPacket(pkt->type_id == MSG_VIDEO, pkt, pkt->isVideoKeyFrame());
}
/**

View File

@ -60,15 +60,12 @@ public:
/**
* rtmp并解析
*/
void onWrite(const RtmpPacket::Ptr &pkt,bool key_pos = true) override {
if (_all_track_ready && !_muxer->isEnabled()) {
//获取到所有Track后并且未开启转协议那么不需要解复用rtmp
key_pos = pkt->isVideoKeyFrame();
} else {
//需要解复用rtmp
key_pos = _demuxer->inputRtmp(pkt);
void onWrite(const RtmpPacket::Ptr &pkt, bool = true) override {
if (!_all_track_ready || _muxer->isEnabled()) {
//未获取到所有Track后或者开启转协议那么需要解复用rtmp
_demuxer->inputRtmp(pkt);
}
RtmpMediaSource::onWrite(pkt, key_pos);
RtmpMediaSource::onWrite(pkt);
}
/**