diff --git a/src/Rtmp/RtmpPusher.cpp b/src/Rtmp/RtmpPusher.cpp index aade6a63..5ec122de 100644 --- a/src/Rtmp/RtmpPusher.cpp +++ b/src/Rtmp/RtmpPusher.cpp @@ -63,6 +63,20 @@ void RtmpPusher::teardown() { } } +void RtmpPusher::onPublishResult(const SockException &ex) { + _pPublishTimer.reset(); + if(_onPublished){ + _onPublished(ex); + _onPublished = nullptr; + }else if(_onShutdown){ + _onShutdown(ex); + } + + if(ex){ + teardown(); + } +} + void RtmpPusher::publish(const string &strUrl) { teardown(); string strHost = FindField(strUrl.data(), "://", "/"); @@ -93,7 +107,6 @@ void RtmpPusher::publish(const string &strUrl) { return false; } strongSelf->onPublishResult(SockException(Err_timeout,"publish rtmp timeout")); - strongSelf->teardown(); return false; },getPoller())); @@ -105,7 +118,7 @@ void RtmpPusher::publish(const string &strUrl) { } void RtmpPusher::onErr(const SockException &ex){ - onShutdown(ex); + onPublishResult(ex); } void RtmpPusher::onConnect(const SockException &err){ if(err) { @@ -129,8 +142,6 @@ void RtmpPusher::onRecv(const Buffer::Ptr &pBuf){ } catch (exception &e) { SockException ex(Err_other, e.what()); onPublishResult(ex); - onShutdown(ex); - teardown(); } } @@ -207,8 +218,7 @@ inline void RtmpPusher::send_metaData(){ _pRtmpReader->setDetachCB([weakSelf](){ auto strongSelf = weakSelf.lock(); if(strongSelf){ - strongSelf->onShutdown(SockException(Err_other,"媒体源被释放")); - strongSelf->teardown(); + strongSelf->onPublishResult(SockException(Err_other,"媒体源被释放")); } }); onPublishResult(SockException(Err_success,"success")); diff --git a/src/Rtmp/RtmpPusher.h b/src/Rtmp/RtmpPusher.h index d36457b9..ae5d8db7 100644 --- a/src/Rtmp/RtmpPusher.h +++ b/src/Rtmp/RtmpPusher.h @@ -63,19 +63,7 @@ protected: send(buffer); } private: - void onShutdown(const SockException &ex) { - _pPublishTimer.reset(); - if(_onShutdown){ - _onShutdown(ex); - } - _pRtmpReader.reset(); - } - void onPublishResult(const SockException &ex) { - _pPublishTimer.reset(); - if(_onPublished){ - _onPublished(ex); - } - } + void onPublishResult(const SockException &ex); template inline void addOnResultCB(const FUN &fun) { diff --git a/src/Rtsp/RtspPusher.cpp b/src/Rtsp/RtspPusher.cpp index 86ae8bfa..718fe13f 100644 --- a/src/Rtsp/RtspPusher.cpp +++ b/src/Rtsp/RtspPusher.cpp @@ -62,17 +62,15 @@ void RtspPusher::publish(const string &strUrl) { publish(url,user,pwd,eType); } -void RtspPusher::onShutdown(const SockException &ex) { - if(_onShutdown){ - _onShutdown(ex); - } - teardown(); -} void RtspPusher::onPublishResult(const SockException &ex) { _pPublishTimer.reset(); if(_onPublished){ _onPublished(ex); + _onPublished = nullptr; + }else if(_onShutdown){ + _onShutdown(ex); } + if(ex){ teardown(); } @@ -128,7 +126,7 @@ void RtspPusher::publish(const string & strUrl, const string &strUser, const str } void RtspPusher::onErr(const SockException &ex) { - onShutdown(ex); + onPublishResult(ex); } void RtspPusher::onConnect(const SockException &err) { @@ -145,7 +143,6 @@ void RtspPusher::onRecv(const Buffer::Ptr &pBuf){ } catch (exception &e) { SockException ex(Err_other, e.what()); onPublishResult(ex); - onShutdown(ex); } } @@ -366,7 +363,7 @@ void RtspPusher::sendRecord() { _pRtspReader->setDetachCB([weakSelf](){ auto strongSelf = weakSelf.lock(); if(strongSelf){ - strongSelf->onShutdown(SockException(Err_other,"媒体源被释放")); + strongSelf->onPublishResult(SockException(Err_other,"媒体源被释放")); } }); if(_eType != Rtsp::RTP_TCP){ diff --git a/src/Rtsp/RtspPusher.h b/src/Rtsp/RtspPusher.h index 0bab5603..3ef8fb2b 100644 --- a/src/Rtsp/RtspPusher.h +++ b/src/Rtsp/RtspPusher.h @@ -50,7 +50,6 @@ protected: void onRtpPacket(const char *data,uint64_t len) override {}; private: void publish(const string &strUrl, const string &strUser, const string &strPwd, Rtsp::eRtpType eType ); - void onShutdown(const SockException &ex); void onPublishResult(const SockException &ex); void sendAnnounce();