From c2d5f5abed390630fc213a13c900690f59145957 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 17 May 2017 11:52:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3rtmp=E7=BB=9D=E5=AF=B9?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=88=B3=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Rtmp/RtmpProtocol.cpp | 4 ++-- src/Rtmp/RtmpSession.cpp | 15 ++++++++++++++- src/Rtmp/RtmpSession.h | 1 + src/Rtsp/RtspToRtmpMediaSource.cpp | 30 ++---------------------------- src/Rtsp/RtspToRtmpMediaSource.h | 2 -- 5 files changed, 19 insertions(+), 33 deletions(-) diff --git a/src/Rtmp/RtmpProtocol.cpp b/src/Rtmp/RtmpProtocol.cpp index d90237c7..8006e8f6 100644 --- a/src/Rtmp/RtmpProtocol.cpp +++ b/src/Rtmp/RtmpProtocol.cpp @@ -210,7 +210,7 @@ void RtmpProtocol::startClientSession(const function &callBack) { //发送 C0C1 char handshake_head = HANDSHAKE_PLAINTEXT; onSendRawData(&handshake_head, 1); - RtmpHandshake c1(::time(NULL)); + RtmpHandshake c1(0); onSendRawData((char *) (&c1), sizeof(c1)); m_nextHandle = [this,callBack]() { //等待 S0+S1+S2 @@ -388,7 +388,7 @@ void RtmpProtocol::send_complex_S0S1S2(int schemeType,const string &digest){ char handshake_head = HANDSHAKE_PLAINTEXT; onSendRawData(&handshake_head, 1); //S1 - RtmpHandshake s1(time(NULL)); + RtmpHandshake s1(0); memcpy(s1.zero,"\x04\x05\x00\x01",4); char *digestPos; if(schemeType == 0){ diff --git a/src/Rtmp/RtmpSession.cpp b/src/Rtmp/RtmpSession.cpp index 0e5d7085..4bb31fb8 100644 --- a/src/Rtmp/RtmpSession.cpp +++ b/src/Rtmp/RtmpSession.cpp @@ -342,7 +342,20 @@ void RtmpSession::onCmd_seek(AMFDecoder &dec) { } void RtmpSession::onSendMedia(const RtmpPacket& pkt) { - sendRtmp(pkt.typeId, pkt.streamId, pkt.strBuf, pkt.timeStamp, pkt.chunkId); + auto modifiedStamp = pkt.timeStamp; + auto &firstStamp = m_aui32FirstStamp[pkt.typeId % 2]; + if(!firstStamp){ + firstStamp = modifiedStamp; + } + if(modifiedStamp >= firstStamp){ + //计算时间戳增量 + modifiedStamp -= firstStamp; + }else{ + //发生回环,重新计算时间戳增量 + CLEAR_ARR(m_aui32FirstStamp); + modifiedStamp = 0; + } + sendRtmp(pkt.typeId, pkt.streamId, pkt.strBuf, modifiedStamp, pkt.chunkId); } } /* namespace Rtmp */ diff --git a/src/Rtmp/RtmpSession.h b/src/Rtmp/RtmpSession.h index 1ee4fd18..1b984378 100644 --- a/src/Rtmp/RtmpSession.h +++ b/src/Rtmp/RtmpSession.h @@ -46,6 +46,7 @@ private: std::shared_ptr m_pPublisherSrc; bool m_bPublisherSrcRegisted = false; std::weak_ptr m_pPlayerSrc; + uint32_t m_aui32FirstStamp[2] = {0}; void onProcessCmd(AMFDecoder &dec); void onCmd_connect(AMFDecoder &dec); diff --git a/src/Rtsp/RtspToRtmpMediaSource.cpp b/src/Rtsp/RtspToRtmpMediaSource.cpp index 65b093b1..dd930b5b 100644 --- a/src/Rtsp/RtspToRtmpMediaSource.cpp +++ b/src/Rtsp/RtspToRtmpMediaSource.cpp @@ -118,20 +118,7 @@ void RtspToRtmpMediaSource::onGetH264(const H264Frame& frame) { m_rtmpPkt.bodySize = m_rtmpPkt.strBuf.size(); m_rtmpPkt.chunkId = CHUNK_MEDIA; m_rtmpPkt.streamId = STREAM_MEDIA; - - if(!m_aui32FirstStamp[0]){ - //记录首次时间戳 - m_aui32FirstStamp[0] = frame.timeStamp; - } - if(frame.timeStamp >= m_aui32FirstStamp[0]){ - //计算时间戳增量 - m_rtmpPkt.timeStamp = frame.timeStamp - m_aui32FirstStamp[0]; - }else{ - //发生回环,重新计算时间戳增量 - CLEAR_ARR(m_aui32FirstStamp); - m_rtmpPkt.timeStamp = 0; - } - + m_rtmpPkt.timeStamp = frame.timeStamp; m_rtmpPkt.typeId = MSG_VIDEO; m_pRtmpSrc->onGetMedia(m_rtmpPkt); } @@ -150,20 +137,7 @@ void RtspToRtmpMediaSource::onGetAdts(const AdtsFrame& frame) { m_rtmpPkt.bodySize = m_rtmpPkt.strBuf.size(); m_rtmpPkt.chunkId = CHUNK_MEDIA; m_rtmpPkt.streamId = STREAM_MEDIA; - - if(!m_aui32FirstStamp[1]){ - //记录首次时间戳 - m_aui32FirstStamp[1] = frame.timeStamp; - } - if(frame.timeStamp >= m_aui32FirstStamp[1]){ - //计算时间戳增量 - m_rtmpPkt.timeStamp = frame.timeStamp - m_aui32FirstStamp[1]; - }else{ - //发生回环,重新计算时间戳增量 - CLEAR_ARR(m_aui32FirstStamp); - m_rtmpPkt.timeStamp = 0; - } - + m_rtmpPkt.timeStamp = frame.timeStamp; m_rtmpPkt.typeId = MSG_AUDIO; m_pRtmpSrc->onGetMedia(m_rtmpPkt); } diff --git a/src/Rtsp/RtspToRtmpMediaSource.h b/src/Rtsp/RtspToRtmpMediaSource.h index 096f1418..2f2e0df5 100644 --- a/src/Rtsp/RtspToRtmpMediaSource.h +++ b/src/Rtsp/RtspToRtmpMediaSource.h @@ -84,8 +84,6 @@ private: RtmpMediaSource::Ptr m_pRtmpSrc; RtmpPacket m_rtmpPkt; uint8_t m_ui8AudioFlags = 0; - //首次时间戳,m_aui32FirstStamp[0]:视频,m_aui32FirstStamp[1]:音频 - uint32_t m_aui32FirstStamp[2] = {0,0}; MediaRecorder::Ptr m_pRecorder; bool m_bEnableFile = true; void onGetH264(const H264Frame &frame);