完善seek相关的逻辑

This commit is contained in:
xiongziliang 2018-10-26 15:09:08 +08:00
parent 52f0d89d7a
commit 6870292fd6
10 changed files with 39 additions and 33 deletions

View File

@ -54,11 +54,7 @@ public:
//拖动进度条 //拖动进度条
return false; return false;
} }
virtual uint32_t getStamp() { virtual bool close() {
//获取时间戳
return 0;
}
virtual bool shutDown() {
//通知其停止推流 //通知其停止推流
return false; return false;
} }
@ -143,19 +139,14 @@ public:
return listener->seekTo(ui32Stamp); return listener->seekTo(ui32Stamp);
} }
uint32_t getStamp() { virtual uint32_t getTimeStamp(TrackType trackType) = 0;
auto listener = _listener.lock();
if(!listener){ bool close() {
return 0;
}
return listener->getStamp();
}
bool shutDown() {
auto listener = _listener.lock(); auto listener = _listener.lock();
if(!listener){ if(!listener){
return false; return false;
} }
return listener->shutDown(); return listener->close();
} }
void setListener(const std::weak_ptr<MediaSourceEvent> &listener){ void setListener(const std::weak_ptr<MediaSourceEvent> &listener){
_listener = listener; _listener = listener;

View File

@ -192,7 +192,7 @@ void PlayerProxy::initMedia() {
// _pChn->initAudio(info); // _pChn->initAudio(info);
// } // }
} }
bool PlayerProxy::shutDown() { bool PlayerProxy::close() {
//通知其停止推流 //通知其停止推流
weak_ptr<PlayerProxy> weakSlef = dynamic_pointer_cast<PlayerProxy>(shared_from_this()); weak_ptr<PlayerProxy> weakSlef = dynamic_pointer_cast<PlayerProxy>(shared_from_this());
auto executor = getExecutor(); auto executor = getExecutor();

View File

@ -54,7 +54,7 @@ public:
virtual ~PlayerProxy(); virtual ~PlayerProxy();
void play(const char* strUrl) override; void play(const char* strUrl) override;
bool shutDown() override; bool close() override;
private: private:
void initMedia(); void initMedia();
void rePlay(const string &strUrl,int iFailedCnt); void rePlay(const string &strUrl,int iFailedCnt);

View File

@ -177,10 +177,7 @@ void MediaReader::startReadMP4() {
seek(ui32Stamp); seek(ui32Stamp);
return true; return true;
} }
uint32_t MediaReader::getStamp() { bool MediaReader::close(){
return _iSeekTime + _ticker.elapsedTime();
}
bool MediaReader::shutDown(){
AsyncTaskThread::Instance().CancelTask(reinterpret_cast<uint64_t>(this)); AsyncTaskThread::Instance().CancelTask(reinterpret_cast<uint64_t>(this));
return true; return true;
} }

View File

@ -46,8 +46,7 @@ public:
static MediaSource::Ptr onMakeMediaSource(const string &strSchema,const string &strVhost,const string &strApp, const string &strId); static MediaSource::Ptr onMakeMediaSource(const string &strSchema,const string &strVhost,const string &strApp, const string &strId);
public: public:
bool seekTo(uint32_t ui32Stamp) override; bool seekTo(uint32_t ui32Stamp) override;
uint32_t getStamp() override; bool close() override;
bool shutDown() override;
private: private:
#ifdef ENABLE_MP4V2 #ifdef ENABLE_MP4V2

View File

@ -93,7 +93,7 @@ public:
void onWrite(const RtmpPacket::Ptr &pkt,bool isKey = true) override { void onWrite(const RtmpPacket::Ptr &pkt,bool isKey = true) override {
lock_guard<recursive_mutex> lock(_mtxMap); lock_guard<recursive_mutex> lock(_mtxMap);
if (pkt->isCfgFrame()) { if (pkt->isCfgFrame()) {
_mapCfgFrame.emplace(pkt->typeId, pkt); _mapCfgFrame[pkt->typeId] = pkt;
if(_bAsyncRegist && !_bRegisted && _mapCfgFrame.size() == _iCfgFrameSize){ if(_bAsyncRegist && !_bRegisted && _mapCfgFrame.size() == _iCfgFrameSize){
_bAsyncRegist = false; _bAsyncRegist = false;
@ -101,9 +101,21 @@ public:
_bRegisted = true; _bRegisted = true;
} }
} }
_mapStamp[pkt->typeId] = pkt->timeStamp;
_pRing->write(pkt,pkt->isVideoKeyFrame()); _pRing->write(pkt,pkt->isVideoKeyFrame());
} }
uint32_t getTimeStamp(TrackType trackType) override {
lock_guard<recursive_mutex> 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: private:
bool ready(){ bool ready(){
lock_guard<recursive_mutex> lock(_mtxMap); lock_guard<recursive_mutex> lock(_mtxMap);
@ -111,7 +123,8 @@ private:
} }
protected: protected:
AMFValue _metadata; AMFValue _metadata;
unordered_map<int, RtmpPacket::Ptr> _mapCfgFrame; map<int, RtmpPacket::Ptr> _mapCfgFrame;
map<int,uint32_t> _mapStamp;
mutable recursive_mutex _mtxMap; mutable recursive_mutex _mtxMap;
RingBuffer<RtmpPacket::Ptr>::Ptr _pRing; //rtp环形缓冲 RingBuffer<RtmpPacket::Ptr>::Ptr _pRing; //rtp环形缓冲
int _iCfgFrameSize = -1; int _iCfgFrameSize = -1;

View File

@ -79,7 +79,7 @@ private:
sendResponse(MSG_CMD, invoke.data()); sendResponse(MSG_CMD, invoke.data());
} }
bool shutDown() override { bool close() override {
InfoL << "kick out:" << _mediaInfo._vhost << " " << _mediaInfo._app << " " << _mediaInfo._streamid; InfoL << "kick out:" << _mediaInfo._vhost << " " << _mediaInfo._app << " " << _mediaInfo._streamid;
safeShutdown(); safeShutdown();
return true; return true;

View File

@ -84,12 +84,18 @@ public:
} }
return track->_seq; return track->_seq;
} }
virtual uint32_t getTimeStamp(TrackType trackType) {
uint32_t getTimeStamp(TrackType trackType) override {
auto track = _sdpAttr.getTrack(trackType); auto track = _sdpAttr.getTrack(trackType);
if(!track){ if(track) {
return 0; 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) { virtual void setTimeStamp(uint32_t uiStamp) {

View File

@ -723,13 +723,13 @@ bool RtspSession::handleReq_Play() {
auto iStartTime = 1000 * atof(strStart.data()); auto iStartTime = 1000 * atof(strStart.data());
InfoL << "rtsp seekTo(ms):" << iStartTime; InfoL << "rtsp seekTo(ms):" << iStartTime;
pMediaSrc->seekTo(iStartTime); pMediaSrc->seekTo(iStartTime);
iStamp = pMediaSrc->getStamp(); iStamp = pMediaSrc->getTimeStamp(TrackInvalid);
}else if(pMediaSrc->getRing()->readerCount() == 1){ }else if(pMediaSrc->getRing()->readerCount() == 1){
//第一个消费者 //第一个消费者
pMediaSrc->seekTo(0); pMediaSrc->seekTo(0);
iStamp = 0; iStamp = 0;
}else{ }else{
iStamp = pMediaSrc->getStamp(); iStamp = pMediaSrc->getTimeStamp(TrackInvalid);
} }
for(auto &track : _aTrackInfo){ for(auto &track : _aTrackInfo){

View File

@ -51,7 +51,7 @@ public:
if(!media) { if(!media) {
break; break;
} }
if(!media->shutDown()) { if(!media->close()) {
break; break;
} }
(*stream) << "\t踢出成功:" (*stream) << "\t踢出成功:"