From 6a649174275a31cfb745e5351efade5b5a892f3b Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 8 May 2019 15:27:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=92=AD=E6=94=BE=E6=81=A2?= =?UTF-8?q?=E5=A4=8D=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Player/MediaPlayer.cpp | 1 + src/Player/PlayerBase.h | 29 +++++++++++++++-- src/Rtmp/RtmpPlayer.cpp | 52 ++++++++++++++++++------------ src/Rtsp/RtspPlayer.cpp | 66 +++++++++++++++++++++----------------- 4 files changed, 96 insertions(+), 52 deletions(-) diff --git a/src/Player/MediaPlayer.cpp b/src/Player/MediaPlayer.cpp index 3b74a152..719e4510 100644 --- a/src/Player/MediaPlayer.cpp +++ b/src/Player/MediaPlayer.cpp @@ -45,6 +45,7 @@ void MediaPlayer::play(const string &strUrl) { _parser = PlayerBase::createPlayer(_poller,strUrl); _parser->setOnShutdown(_shutdownCB); _parser->setOnPlayResult(_playResultCB); + _parser->setOnResume(_resumeCB); _parser->setMediaSouce(_pMediaSrc); _parser->mINI::operator=(*this); _parser->play(strUrl); diff --git a/src/Player/PlayerBase.h b/src/Player/PlayerBase.h index d88fe286..57db2796 100644 --- a/src/Player/PlayerBase.h +++ b/src/Player/PlayerBase.h @@ -120,6 +120,12 @@ public: */ virtual void setOnPlayResult( const function &cb) {} + /** + * 设置播放恢复回调 + * @param cb + */ + virtual void setOnResume( const function &cb) {} + /** * 获取播放进度,取值 0.0 ~ 1.0 * @return @@ -147,6 +153,10 @@ public: protected: virtual void onShutdown(const SockException &ex) {} virtual void onPlayResult(const SockException &ex) {} + /** + * 暂停后恢复播放时间 + */ + virtual void onResume(){}; }; template @@ -172,6 +182,13 @@ public: _playResultCB = cb; } + void setOnResume(const function &cb) override { + if (_parser) { + _parser->setOnResume(cb); + } + _resumeCB = cb; + } + bool isInited(int analysisMs) override{ if (_parser) { return _parser->isInited(analysisMs); @@ -237,7 +254,14 @@ protected: } //播放成功却未初始化完毕,这个时候不回调汇报播放成功 } - void checkInited(int analysisMs){ + + void onResume() override{ + if(_resumeCB){ + _resumeCB(); + } + } + + void checkInited(int analysisMs){ if(!_playResultCB){ return; } @@ -249,7 +273,8 @@ protected: protected: function _shutdownCB; function _playResultCB; - std::shared_ptr _parser; + function _resumeCB; + std::shared_ptr _parser; MediaSource::Ptr _pMediaSrc; }; diff --git a/src/Rtmp/RtmpPlayer.cpp b/src/Rtmp/RtmpPlayer.cpp index e201d352..b740262f 100644 --- a/src/Rtmp/RtmpPlayer.cpp +++ b/src/Rtmp/RtmpPlayer.cpp @@ -117,30 +117,40 @@ void RtmpPlayer::onErr(const SockException &ex){ void RtmpPlayer::onPlayResult_l(const SockException &ex) { WarnL << ex.getErrCode() << " " << ex.what(); + + if(!ex){ + //恢复rtmp接收超时定时器 + _mediaTicker.resetTime(); + weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); + int timeoutMS = (*this)[kMediaTimeoutMS].as(); + _pMediaTimer.reset( new Timer(timeoutMS / 2000.0, [weakSelf,timeoutMS]() { + auto strongSelf=weakSelf.lock(); + if(!strongSelf) { + return false; + } + if(strongSelf->_mediaTicker.elapsedTime()> timeoutMS) { + //recv media timeout! + strongSelf->onPlayResult_l(SockException(Err_timeout,"recv rtmp timeout")); + return false; + } + return true; + },getPoller())); + } + if (_pPlayTimer) { + //开始播放阶段 _pPlayTimer.reset(); onPlayResult(ex); - if(!ex){ - _mediaTicker.resetTime(); - weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); - int timeoutMS = (*this)[kMediaTimeoutMS].as(); - _pMediaTimer.reset( new Timer(timeoutMS / 2000.0, [weakSelf,timeoutMS]() { - auto strongSelf=weakSelf.lock(); - if(!strongSelf) { - return false; - } - if(strongSelf->_mediaTicker.elapsedTime()> timeoutMS) { - //recv media timeout! - strongSelf->onPlayResult_l(SockException(Err_timeout,"recv rtmp timeout")); - return false; - } - return true; - },getPoller())); - } - }else if(ex){ - //播放成功后异常断开回调 - onShutdown(ex); - } + }else { + //播放中途阶段 + if (ex) { + //播放成功后异常断开回调 + onShutdown(ex); + }else{ + //恢复播放 + onResume(); + } + } if(ex){ teardown(); diff --git a/src/Rtsp/RtspPlayer.cpp b/src/Rtsp/RtspPlayer.cpp index cb281b42..1500cd70 100644 --- a/src/Rtsp/RtspPlayer.cpp +++ b/src/Rtsp/RtspPlayer.cpp @@ -614,36 +614,44 @@ void RtspPlayer::onRecvRTP_l(const RtpPacket::Ptr &pRtppt, const SdpTrack::Ptr & } void RtspPlayer::onPlayResult_l(const SockException &ex) { WarnL << ex.getErrCode() << " " << ex.what(); - if(_pPlayTimer){ - //播放结果回调 - _pPlayTimer.reset(); - onPlayResult(ex); - if(!ex){ - //播放成功 - _rtpTicker.resetTime(); - weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); - int timeoutMS = (*this)[kMediaTimeoutMS].as(); - _pRtpTimer.reset( new Timer(timeoutMS / 2000.0, [weakSelf,timeoutMS]() { - auto strongSelf=weakSelf.lock(); - if(!strongSelf) { - return false; - } - if(strongSelf->_rtpTicker.elapsedTime()> timeoutMS) { - //recv rtp timeout! - strongSelf->onPlayResult_l(SockException(Err_timeout,"recv rtp timeout")); - return false; - } - return true; - },getPoller())); - } - } else if(ex){ - //播放成功后异常断开回调 - onShutdown(ex); - } - if(ex){ - teardown(); - } + if(!ex){ + //播放成功,恢复rtp接收超时定时器 + _rtpTicker.resetTime(); + weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); + int timeoutMS = (*this)[kMediaTimeoutMS].as(); + _pRtpTimer.reset( new Timer(timeoutMS / 2000.0, [weakSelf,timeoutMS]() { + auto strongSelf=weakSelf.lock(); + if(!strongSelf) { + return false; + } + if(strongSelf->_rtpTicker.elapsedTime()> timeoutMS) { + //recv rtp timeout! + strongSelf->onPlayResult_l(SockException(Err_timeout,"recv rtp timeout")); + return false; + } + return true; + },getPoller())); + } + + if (_pPlayTimer) { + //开始播放阶段 + _pPlayTimer.reset(); + onPlayResult(ex); + }else { + //播放中途阶段 + if (ex) { + //播放成功后异常断开回调 + onShutdown(ex); + }else{ + //恢复播放 + onResume(); + } + } + + if(ex){ + teardown(); + } } int RtspPlayer::getTrackIndexByControlSuffix(const string &controlSuffix) const{