完善主动关断流机制

This commit is contained in:
xiongziliang 2019-05-27 14:14:42 +08:00
parent 3f211d1653
commit f563274cda
6 changed files with 50 additions and 3 deletions

View File

@ -338,7 +338,8 @@ void installWebApi() {
}); });
}); });
API_REGIST(api,kick_pusher,{ //主动关断流,包括关断拉流、推流
API_REGIST(api,close_stream,{
CHECK_SECRET(); CHECK_SECRET();
CHECK_ARGS("schema","vhost","app","stream"); CHECK_ARGS("schema","vhost","app","stream");
//踢掉推流器 //踢掉推流器
@ -405,6 +406,12 @@ void installWebApi() {
const_cast<PlayerProxy::Ptr &>(player).reset(); const_cast<PlayerProxy::Ptr &>(player).reset();
invoker("200 OK", headerOut, val.toStyledString()); invoker("200 OK", headerOut, val.toStyledString());
}); });
//被主动关闭拉流
player->setOnClose([key](){
lock_guard<recursive_mutex> lck(s_proxyMapMtx);
s_proxyMap.erase(key);
});
player->play(allArgs["url"]); player->play(allArgs["url"]);
}); });

View File

@ -148,7 +148,7 @@ public:
} }
return listener->close(); return listener->close();
} }
void setListener(const std::weak_ptr<MediaSourceEvent> &listener){ virtual void setListener(const std::weak_ptr<MediaSourceEvent> &listener){
_listener = listener; _listener = listener;
} }

View File

@ -79,6 +79,11 @@ PlayerProxy::PlayerProxy(const string &strVhost,
void PlayerProxy::setPlayCallbackOnce(const function<void(const SockException &ex)> &cb){ void PlayerProxy::setPlayCallbackOnce(const function<void(const SockException &ex)> &cb){
_playCB = cb; _playCB = cb;
} }
void PlayerProxy::setOnClose(const function<void()> &cb){
_onClose = cb;
}
void PlayerProxy::play(const string &strUrlTmp) { void PlayerProxy::play(const string &strUrlTmp) {
weak_ptr<PlayerProxy> weakSelf = shared_from_this(); weak_ptr<PlayerProxy> weakSelf = shared_from_this();
std::shared_ptr<int> piFailedCnt(new int(0)); //连续播放失败次数 std::shared_ptr<int> piFailedCnt(new int(0)); //连续播放失败次数
@ -148,6 +153,9 @@ bool PlayerProxy::close() {
if (stronSelf) { if (stronSelf) {
stronSelf->_mediaMuxer.reset(); stronSelf->_mediaMuxer.reset();
stronSelf->teardown(); stronSelf->teardown();
if(stronSelf->_onClose){
stronSelf->_onClose();
}
} }
}); });
return true; return true;

View File

@ -62,8 +62,23 @@ public:
*/ */
void setPlayCallbackOnce(const function<void(const SockException &ex)> &cb); void setPlayCallbackOnce(const function<void(const SockException &ex)> &cb);
/**
*
* @param cb
*/
void setOnClose(const function<void()> &cb);
/**
*
* @param strUrl
*/
void play(const string &strUrl) override; void play(const string &strUrl) override;
/**
*
* @return
*/
bool close() override; bool close() override;
private: private:
void rePlay(const string &strUrl,int iFailedCnt); void rePlay(const string &strUrl,int iFailedCnt);
@ -78,6 +93,7 @@ private:
string _strSrc; string _strSrc;
Timer::Ptr _timer; Timer::Ptr _timer;
function<void(const SockException &ex)> _playCB; function<void(const SockException &ex)> _playCB;
function<void()> _onClose;
}; };
} /* namespace mediakit */ } /* namespace mediakit */

View File

@ -78,9 +78,17 @@ public:
track->addDelegate(_rtspMuxer); track->addDelegate(_rtspMuxer);
track->addDelegate(_recorder); track->addDelegate(_recorder);
} }
_rtspMuxer->setListener(_listener);
} }
RtmpMediaSource::onWrite(pkt,key_pos); RtmpMediaSource::onWrite(pkt,key_pos);
} }
void setListener(const std::weak_ptr<MediaSourceEvent> &listener) override {
RtmpMediaSource::setListener(listener);
if(_rtspMuxer){
_rtspMuxer->setListener(listener);
}
}
private: private:
RtmpDemuxer::Ptr _rtmpDemuxer; RtmpDemuxer::Ptr _rtmpDemuxer;
RtspMediaSourceMuxer::Ptr _rtspMuxer; RtspMediaSourceMuxer::Ptr _rtspMuxer;

View File

@ -73,10 +73,18 @@ public:
track->addDelegate(_rtmpMuxer); track->addDelegate(_rtmpMuxer);
track->addDelegate(_recorder); track->addDelegate(_recorder);
} }
_rtmpMuxer->setListener(_listener);
} }
} }
RtspMediaSource::onWrite(rtp, bKeyPos); RtspMediaSource::onWrite(rtp, bKeyPos);
} }
void setListener(const std::weak_ptr<MediaSourceEvent> &listener) override {
RtspMediaSource::setListener(listener);
if(_rtmpMuxer){
_rtmpMuxer->setListener(listener);
}
}
private: private:
RtspDemuxer::Ptr _rtspDemuxer; RtspDemuxer::Ptr _rtspDemuxer;
RtmpMediaSourceMuxer::Ptr _rtmpMuxer; RtmpMediaSourceMuxer::Ptr _rtmpMuxer;