From 66ec67bfb9c752837496c83277c1f347f8ef5bdd Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Mon, 13 Jan 2020 11:51:29 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E5=A4=8D=E7=94=9F=E6=88=90?= =?UTF-8?q?=E7=9A=84rtmp=E6=97=A0=E6=B3=95=E8=A2=ABflash=E6=92=AD=E6=94=BE?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=202=E3=80=81=E4=BF=AE=E5=A4=8DRTSP?= =?UTF-8?q?=E6=9C=89B=E5=B8=A7=E6=97=B6=EF=BC=8C=E7=9B=B8=E5=AF=B9?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=88=B3=E8=AE=A1=E7=AE=97=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/Stamp.cpp | 14 ++++++++------ src/Extension/H264.h | 1 + src/Extension/H264Rtmp.cpp | 6 +++++- src/Record/MP4Reader.cpp | 12 +++++++----- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/Common/Stamp.cpp b/src/Common/Stamp.cpp index 9d2c2d45..7330de21 100644 --- a/src/Common/Stamp.cpp +++ b/src/Common/Stamp.cpp @@ -27,25 +27,27 @@ #include "Stamp.h" #define MAX_DELTA_STAMP 300 +#define ABS(x) ((x) > 0 ? (x) : (-x)) namespace mediakit { int64_t DeltaStamp::deltaStamp(int64_t stamp) { if(!_last_stamp){ //第一次计算时间戳增量,时间戳增量为0 - _last_stamp = stamp; + if(stamp){ + _last_stamp = stamp; + } return 0; } int64_t ret = stamp - _last_stamp; - if(ret >= 0){ - //时间戳增量为正,返回之 + if(ABS(ret) < MAX_DELTA_STAMP){ + //时间戳变化不明显 _last_stamp = stamp; - //在直播情况下,时间戳增量不得大于MAX_DELTA_STAMP - return ret < MAX_DELTA_STAMP ? ret : (_playback ? ret : 0); + return ret; } - //时间戳增量为负,说明时间戳回环了或回退了 + //时间戳变化太明显,可能回环了或者seek了 _last_stamp = stamp; return _playback ? ret : 0; } diff --git a/src/Extension/H264.h b/src/Extension/H264.h index 4bb8ea1c..3ffb058d 100644 --- a/src/Extension/H264.h +++ b/src/Extension/H264.h @@ -49,6 +49,7 @@ public: NAL_SPS = 7, NAL_PPS = 8, NAL_IDR = 5, + NAL_SEI = 6, } NalType; char *data() const override{ diff --git a/src/Extension/H264Rtmp.cpp b/src/Extension/H264Rtmp.cpp index 3ab77554..9ee877e7 100644 --- a/src/Extension/H264Rtmp.cpp +++ b/src/Extension/H264Rtmp.cpp @@ -140,6 +140,10 @@ void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) { } } + if(type == H264Frame::NAL_SEI){ + return; + } + if(_lastPacket && _lastPacket->timeStamp != frame->stamp()) { RtmpCodec::inputRtmp(_lastPacket, _lastPacket->isVideoKeyFrame()); _lastPacket = nullptr; @@ -149,7 +153,7 @@ void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) { //I or P or B frame int8_t flags = 7; //h.264 bool is_config = false; - flags |= ((frame->keyFrame() ? FLV_KEY_FRAME : FLV_INTER_FRAME) << 4); + flags |= (((frame->configFrame() || frame->keyFrame()) ? FLV_KEY_FRAME : FLV_INTER_FRAME) << 4); _lastPacket = ResourcePoolHelper::obtainObj(); _lastPacket->strBuf.clear(); diff --git a/src/Record/MP4Reader.cpp b/src/Record/MP4Reader.cpp index 9bf20525..020fa6bc 100644 --- a/src/Record/MP4Reader.cpp +++ b/src/Record/MP4Reader.cpp @@ -225,8 +225,10 @@ inline bool MP4Reader::readVideoSample(int iTimeInc,bool justSeekSyncFrame) { uint32_t numBytes = _video_sample_max_size; MP4Duration pRenderingOffset; if(MP4ReadSample(_hMP4File, _video_trId, iIdx + 1, &pBytes, &numBytes,NULL,NULL,&pRenderingOffset,&_bSyncSample)){ - if (!justSeekSyncFrame) { - uint32_t iOffset = 0; + if (!justSeekSyncFrame) { + uint32_t dts = (double) _video_ms * iIdx / _video_num_samples; + uint32_t pts = dts + pRenderingOffset / 90; + uint32_t iOffset = 0; while (iOffset < numBytes) { uint32_t iFrameLen; memcpy(&iFrameLen,pBytes + iOffset,4); @@ -235,8 +237,7 @@ inline bool MP4Reader::readVideoSample(int iTimeInc,bool justSeekSyncFrame) { break; } memcpy(pBytes + iOffset, "\x0\x0\x0\x1", 4); - uint32_t dts = (double) _video_ms * iIdx / _video_num_samples; - writeH264(pBytes + iOffset, iFrameLen + 4, dts, dts + pRenderingOffset / 90); + writeH264(pBytes + iOffset, iFrameLen + 4, dts, pts); iOffset += (iFrameLen + 4); } }else if(_bSyncSample){ @@ -260,9 +261,10 @@ inline bool MP4Reader::readAudioSample(int iTimeInc,bool justSeekSyncFrame) { uint8_t *pBytes = _adts.buffer + 7; if(MP4ReadSample(_hMP4File, _audio_trId, i + 1, &pBytes, &numBytes)){ if (!justSeekSyncFrame) { + uint32_t dts = (double) _audio_ms * i / _audio_num_samples; _adts.aac_frame_length = 7 + numBytes; writeAdtsHeader(_adts, _adts.buffer); - writeAAC(_adts.buffer, _adts.aac_frame_length, (double) _audio_ms * i / _audio_num_samples); + writeAAC(_adts.buffer, _adts.aac_frame_length, dts); } }else{ ErrorL << "读取音频失败:" << i+ 1;