From ba3b47086d13cdef5cec27c204654f3fe2883e8d Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Mon, 29 Oct 2018 17:19:14 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3gcc4.8=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98=20=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E9=83=A8=E5=88=86rtsp=E6=9C=8D=E5=8A=A1=E5=99=A8seek?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ZLToolKit | 2 +- src/Rtsp/RtspSession.cpp | 82 ++++++++++++++++++---------------------- src/Rtsp/RtspSession.h | 4 +- 3 files changed, 39 insertions(+), 49 deletions(-) diff --git a/ZLToolKit b/ZLToolKit index 3a9c9164..109c994d 160000 --- a/ZLToolKit +++ b/ZLToolKit @@ -1 +1 @@ -Subproject commit 3a9c916454897ad02c6ee4cba89f5e370718cd91 +Subproject commit 109c994dc84f1c9ce9caf7199dd905086f54881d diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index 413d4419..e723f970 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -541,13 +541,6 @@ bool RtspSession::handleReq_Setup() { } trackRef->_inited = true; //现在初始化 - auto strongRing = _pWeakRing.lock(); - if (!strongRing) { - //the media source is released! - send_NotAcceptable(); - return false; - } - if(!_bSetUped){ _bSetUped = true; auto strTransport = _parser["Transport"]; @@ -560,18 +553,6 @@ bool RtspSession::handleReq_Setup() { } } - if (!_pRtpReader && _rtpType != PlayerBase::RTP_MULTICAST) { - _pRtpReader = strongRing->attach(); - weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); - _pRtpReader->setDetachCB([weakSelf]() { - auto strongSelf = weakSelf.lock(); - if(!strongSelf) { - return; - } - strongSelf->safeShutdown(); - }); - } - switch (_rtpType) { case PlayerBase::RTP_TCP: { int iLen = sprintf(_pcBuf, @@ -723,6 +704,9 @@ bool RtspSession::handleReq_Play() { return; } + bool useBuf = true; + _enableSendRtp = false; + if (strRange.size() && !_bFirstPlay) { //这个是seek操作 auto strStart = FindField(strRange.data(), "npt=", "-"); @@ -731,8 +715,8 @@ bool RtspSession::handleReq_Play() { } auto iStartTime = 1000 * atof(strStart.data()); InfoL << "rtsp seekTo(ms):" << iStartTime; - pMediaSrc->seekTo(iStartTime); - }else if(pMediaSrc->getRing()->readerCount() == 1){ + useBuf = !pMediaSrc->seekTo(iStartTime); + }else if(pMediaSrc->getRing()->readerCount() == 0){ //第一个消费者 pMediaSrc->seekTo(0); } @@ -769,28 +753,41 @@ bool RtspSession::handleReq_Play() { iLen += sprintf(_pcBuf + iLen, "\r\n\r\n"); SocketHelper::send(_pcBuf, iLen); + _enableSendRtp = true; + //提高发送性能 (*this) << SocketFlags(kSockFlags); SockUtil::setNoDelay(_pSender->rawFD(),false); - if(_pRtpReader){ - weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); - SockUtil::setNoDelay(_pSender->rawFD(), false); - _pRtpReader->setReadCB([weakSelf](const RtpPacket::Ptr &pack) { - auto strongSelf = weakSelf.lock(); - if(!strongSelf) { - return; - } - strongSelf->async([weakSelf,pack](){ - auto strongSelf = weakSelf.lock(); - if(!strongSelf) { - return; - } - strongSelf->sendRtpPacket(pack); - }); - - }); - } + if (!_pRtpReader && _rtpType != PlayerBase::RTP_MULTICAST) { + weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); + _pRtpReader = pMediaSrc->getRing()->attach(useBuf); + _pRtpReader->setDetachCB([weakSelf]() { + auto strongSelf = weakSelf.lock(); + if(!strongSelf) { + return; + } + strongSelf->safeShutdown(); + }); + _pRtpReader->setReadCB([weakSelf](const RtpPacket::Ptr &pack) { + auto strongSelf = weakSelf.lock(); + if(!strongSelf) { + return; + } + if(!strongSelf->_enableSendRtp) { + return; + } + strongSelf->async([weakSelf,pack](){ + auto strongSelf = weakSelf.lock(); + if(!strongSelf) { + return; + } + if(strongSelf->_enableSendRtp) { + strongSelf->sendRtpPacket(pack); + } + }); + }); + } }; weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); @@ -834,11 +831,8 @@ bool RtspSession::handleReq_Pause() { "Session: %s\r\n\r\n", _iCseq, SERVER_NAME, RTSP_VERSION, RTSP_BUILDTIME, dateHeader().data(), _strSession.data()); SocketHelper::send(_pcBuf, n); - if (_pRtpReader) { - _pRtpReader->setReadCB(nullptr); - } + _enableSendRtp = false; return true; - } bool RtspSession::handleReq_Teardown() { @@ -992,8 +986,6 @@ inline bool RtspSession::findStream() { return false; } _strSdp = pMediaSrc->getSdp(); - _pWeakRing = pMediaSrc->getRing(); - _sdpAttr.load(_strSdp); _aTrackInfo = _sdpAttr.getAvailableTrack(); diff --git a/src/Rtsp/RtspSession.h b/src/Rtsp/RtspSession.h index 5b7b6848..0979c34c 100644 --- a/src/Rtsp/RtspSession.h +++ b/src/Rtsp/RtspSession.h @@ -156,9 +156,6 @@ private: bool _bFirstPlay = true; MediaInfo _mediaInfo; std::weak_ptr _pMediaSrc; - - //RTP缓冲 - weak_ptr > _pWeakRing; RingBuffer::RingReader::Ptr _pRtpReader; PlayerBase::eRtpType _rtpType = PlayerBase::RTP_UDP; @@ -204,6 +201,7 @@ private: std::function _delayTask; uint32_t _iTaskTimeLine = 0; + atomic _enableSendRtp; }; } /* namespace mediakit */