From 6870292fd60f93bc92b32aa1f05708c4d263a18d Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Fri, 26 Oct 2018 15:09:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84seek=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/MediaSource.h | 19 +++++-------------- src/Device/PlayerProxy.cpp | 2 +- src/Device/PlayerProxy.h | 2 +- src/MediaFile/MediaReader.cpp | 5 +---- src/MediaFile/MediaReader.h | 3 +-- src/Rtmp/RtmpMediaSource.h | 19 ++++++++++++++++--- src/Rtmp/RtmpSession.h | 2 +- src/Rtsp/RtspMediaSource.h | 14 ++++++++++---- src/Rtsp/RtspSession.cpp | 4 ++-- src/Shell/ShellCMD.cpp | 2 +- 10 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index f705629f..8a2de2da 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -54,11 +54,7 @@ public: //拖动进度条 return false; } - virtual uint32_t getStamp() { - //获取时间戳 - return 0; - } - virtual bool shutDown() { + virtual bool close() { //通知其停止推流 return false; } @@ -143,19 +139,14 @@ public: return listener->seekTo(ui32Stamp); } - uint32_t getStamp() { - auto listener = _listener.lock(); - if(!listener){ - return 0; - } - return listener->getStamp(); - } - bool shutDown() { + virtual uint32_t getTimeStamp(TrackType trackType) = 0; + + bool close() { auto listener = _listener.lock(); if(!listener){ return false; } - return listener->shutDown(); + return listener->close(); } void setListener(const std::weak_ptr &listener){ _listener = listener; diff --git a/src/Device/PlayerProxy.cpp b/src/Device/PlayerProxy.cpp index 6fa09195..1b1cf088 100644 --- a/src/Device/PlayerProxy.cpp +++ b/src/Device/PlayerProxy.cpp @@ -192,7 +192,7 @@ void PlayerProxy::initMedia() { // _pChn->initAudio(info); // } } -bool PlayerProxy::shutDown() { +bool PlayerProxy::close() { //通知其停止推流 weak_ptr weakSlef = dynamic_pointer_cast(shared_from_this()); auto executor = getExecutor(); diff --git a/src/Device/PlayerProxy.h b/src/Device/PlayerProxy.h index d5e6bf6a..d1ca2b22 100644 --- a/src/Device/PlayerProxy.h +++ b/src/Device/PlayerProxy.h @@ -54,7 +54,7 @@ public: virtual ~PlayerProxy(); void play(const char* strUrl) override; - bool shutDown() override; + bool close() override; private: void initMedia(); void rePlay(const string &strUrl,int iFailedCnt); diff --git a/src/MediaFile/MediaReader.cpp b/src/MediaFile/MediaReader.cpp index db58eb36..653b5215 100644 --- a/src/MediaFile/MediaReader.cpp +++ b/src/MediaFile/MediaReader.cpp @@ -177,10 +177,7 @@ void MediaReader::startReadMP4() { seek(ui32Stamp); return true; } -uint32_t MediaReader::getStamp() { - return _iSeekTime + _ticker.elapsedTime(); -} -bool MediaReader::shutDown(){ +bool MediaReader::close(){ AsyncTaskThread::Instance().CancelTask(reinterpret_cast(this)); return true; } diff --git a/src/MediaFile/MediaReader.h b/src/MediaFile/MediaReader.h index e2263f2b..30ba7889 100644 --- a/src/MediaFile/MediaReader.h +++ b/src/MediaFile/MediaReader.h @@ -46,8 +46,7 @@ public: static MediaSource::Ptr onMakeMediaSource(const string &strSchema,const string &strVhost,const string &strApp, const string &strId); public: bool seekTo(uint32_t ui32Stamp) override; - uint32_t getStamp() override; - bool shutDown() override; + bool close() override; private: #ifdef ENABLE_MP4V2 diff --git a/src/Rtmp/RtmpMediaSource.h b/src/Rtmp/RtmpMediaSource.h index 784bf9ce..dc8c3415 100644 --- a/src/Rtmp/RtmpMediaSource.h +++ b/src/Rtmp/RtmpMediaSource.h @@ -93,7 +93,7 @@ public: void onWrite(const RtmpPacket::Ptr &pkt,bool isKey = true) override { lock_guard lock(_mtxMap); if (pkt->isCfgFrame()) { - _mapCfgFrame.emplace(pkt->typeId, pkt); + _mapCfgFrame[pkt->typeId] = pkt; if(_bAsyncRegist && !_bRegisted && _mapCfgFrame.size() == _iCfgFrameSize){ _bAsyncRegist = false; @@ -101,9 +101,21 @@ public: _bRegisted = true; } } - + _mapStamp[pkt->typeId] = pkt->timeStamp; _pRing->write(pkt,pkt->isVideoKeyFrame()); } + + uint32_t getTimeStamp(TrackType trackType) override { + lock_guard lock(_mtxMap); + switch (trackType){ + case TrackVideo: + return _mapStamp[MSG_VIDEO]; + case TrackAudio: + return _mapStamp[MSG_AUDIO]; + default: + return MAX(_mapStamp[MSG_VIDEO],_mapStamp[MSG_AUDIO]); + } + } private: bool ready(){ lock_guard lock(_mtxMap); @@ -111,7 +123,8 @@ private: } protected: AMFValue _metadata; - unordered_map _mapCfgFrame; + map _mapCfgFrame; + map _mapStamp; mutable recursive_mutex _mtxMap; RingBuffer::Ptr _pRing; //rtp环形缓冲 int _iCfgFrameSize = -1; diff --git a/src/Rtmp/RtmpSession.h b/src/Rtmp/RtmpSession.h index 1acc7c0e..dc5e8122 100644 --- a/src/Rtmp/RtmpSession.h +++ b/src/Rtmp/RtmpSession.h @@ -79,7 +79,7 @@ private: sendResponse(MSG_CMD, invoke.data()); } - bool shutDown() override { + bool close() override { InfoL << "kick out:" << _mediaInfo._vhost << " " << _mediaInfo._app << " " << _mediaInfo._streamid; safeShutdown(); return true; diff --git a/src/Rtsp/RtspMediaSource.h b/src/Rtsp/RtspMediaSource.h index bdc6b637..f2c8f001 100644 --- a/src/Rtsp/RtspMediaSource.h +++ b/src/Rtsp/RtspMediaSource.h @@ -84,12 +84,18 @@ public: } return track->_seq; } - virtual uint32_t getTimeStamp(TrackType trackType) { + + uint32_t getTimeStamp(TrackType trackType) override { auto track = _sdpAttr.getTrack(trackType); - if(!track){ - return 0; + if(track) { + return track->_time_stamp; + } + auto tracks = _sdpAttr.getAvailableTrack(); + switch (tracks.size()){ + case 0: return 0; + case 1: return tracks[0]->_time_stamp; + default:return MAX(tracks[0]->_time_stamp,tracks[1]->_time_stamp); } - return track->_time_stamp; } virtual void setTimeStamp(uint32_t uiStamp) { diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index b2cb9a9e..1eca3035 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -723,13 +723,13 @@ bool RtspSession::handleReq_Play() { auto iStartTime = 1000 * atof(strStart.data()); InfoL << "rtsp seekTo(ms):" << iStartTime; pMediaSrc->seekTo(iStartTime); - iStamp = pMediaSrc->getStamp(); + iStamp = pMediaSrc->getTimeStamp(TrackInvalid); }else if(pMediaSrc->getRing()->readerCount() == 1){ //第一个消费者 pMediaSrc->seekTo(0); iStamp = 0; }else{ - iStamp = pMediaSrc->getStamp(); + iStamp = pMediaSrc->getTimeStamp(TrackInvalid); } for(auto &track : _aTrackInfo){ diff --git a/src/Shell/ShellCMD.cpp b/src/Shell/ShellCMD.cpp index 83d5a298..01ad7f42 100644 --- a/src/Shell/ShellCMD.cpp +++ b/src/Shell/ShellCMD.cpp @@ -51,7 +51,7 @@ public: if(!media) { break; } - if(!media->shutDown()) { + if(!media->close()) { break; } (*stream) << "\t踢出成功:"