支持客户端自定义设置EventPoller对象,提高线程安全性

This commit is contained in:
xiongziliang
2019-04-01 10:16:15 +08:00
parent 6e2002e451
commit 9247cb9571
20 changed files with 70 additions and 57 deletions

View File

@@ -32,13 +32,17 @@ using namespace toolkit;
namespace mediakit {
MediaPlayer::MediaPlayer() {
MediaPlayer::MediaPlayer(const EventPoller::Ptr &poller) {
_poller = poller;
if(!_poller){
_poller = EventPollerPool::Instance().getPoller();
}
}
MediaPlayer::~MediaPlayer() {
}
void MediaPlayer::play(const string &strUrl) {
_parser = PlayerBase::createPlayer(strUrl);
_parser = PlayerBase::createPlayer(_poller,strUrl);
_parser->setOnShutdown(_shutdownCB);
_parser->setOnPlayResult(_playResultCB);
_parser->setMediaSouce(_pMediaSrc);
@@ -47,11 +51,7 @@ void MediaPlayer::play(const string &strUrl) {
}
EventPoller::Ptr MediaPlayer::getPoller(){
auto parser = dynamic_pointer_cast<SocketHelper>(_parser);
if(!parser){
return nullptr;
}
return parser->getPoller();
return _poller;
}
void MediaPlayer::pause(bool bPause) {

View File

@@ -41,13 +41,14 @@ class MediaPlayer : public PlayerImp<PlayerBase,PlayerBase> {
public:
typedef std::shared_ptr<MediaPlayer> Ptr;
MediaPlayer();
MediaPlayer(const EventPoller::Ptr &poller = nullptr);
virtual ~MediaPlayer();
void play(const string &strUrl) override;
void pause(bool bPause) override;
void teardown() override;
EventPoller::Ptr getPoller();
private:
EventPoller::Ptr _poller;
};
} /* namespace mediakit */

View File

@@ -33,7 +33,7 @@ using namespace toolkit;
namespace mediakit {
PlayerBase::Ptr PlayerBase::createPlayer(const string &strUrl) {
PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &poller,const string &strUrl) {
static auto releasePlayer = [](PlayerBase *ptr){
onceToken token(nullptr,[&](){
delete ptr;
@@ -42,12 +42,12 @@ PlayerBase::Ptr PlayerBase::createPlayer(const string &strUrl) {
};
string prefix = FindField(strUrl.data(), NULL, "://");
if (strcasecmp("rtsp",prefix.data()) == 0) {
return PlayerBase::Ptr(new RtspPlayerImp(),releasePlayer);
return PlayerBase::Ptr(new RtspPlayerImp(poller),releasePlayer);
}
if (strcasecmp("rtmp",prefix.data()) == 0) {
return PlayerBase::Ptr(new RtmpPlayerImp(),releasePlayer);
return PlayerBase::Ptr(new RtmpPlayerImp(poller),releasePlayer);
}
return PlayerBase::Ptr(new RtspPlayerImp(),releasePlayer);
return PlayerBase::Ptr(new RtspPlayerImp(poller),releasePlayer);
}
PlayerBase::PlayerBase() {

View File

@@ -86,7 +86,7 @@ public:
class PlayerBase : public DemuxerBase, public mINI{
public:
typedef std::shared_ptr<PlayerBase> Ptr;
static Ptr createPlayer(const string &strUrl);
static Ptr createPlayer(const EventPoller::Ptr &poller,const string &strUrl);
PlayerBase();
virtual ~PlayerBase(){}
@@ -154,7 +154,10 @@ class PlayerImp : public Parent
{
public:
typedef std::shared_ptr<PlayerImp> Ptr;
PlayerImp(){}
template<typename ...ArgsType>
PlayerImp(ArgsType &&...args):Parent(std::forward<ArgsType>(args)...){}
virtual ~PlayerImp(){}
void setOnShutdown(const function<void(const SockException &)> &cb) override {
if (_parser) {

View File

@@ -66,7 +66,8 @@ PlayerProxy::PlayerProxy(const string &strVhost,
const string &strSrc,
bool bEnableHls,
bool bEnableMp4,
int iRetryCount){
int iRetryCount,
const EventPoller::Ptr &poller) : MediaPlayer(poller){
_strVhost = strVhost;
_strApp = strApp;
_strSrc = strSrc;
@@ -127,21 +128,18 @@ void PlayerProxy::rePlay(const string &strUrl,int iFailedCnt){
WarnL << "重试播放[" << iFailedCnt << "]:" << strUrl;
strongPlayer->MediaPlayer::play(strUrl);
return false;
}, nullptr);
}, getPoller());
}
bool PlayerProxy::close() {
//通知其停止推流
weak_ptr<PlayerProxy> weakSlef = dynamic_pointer_cast<PlayerProxy>(shared_from_this());
auto poller = getPoller();
if(poller) {
poller->async_first([weakSlef]() {
auto stronSelf = weakSlef.lock();
if (stronSelf) {
stronSelf->_mediaMuxer.reset();
stronSelf->teardown();
}
});
}
//通知其停止推流
weak_ptr<PlayerProxy> weakSlef = dynamic_pointer_cast<PlayerProxy>(shared_from_this());
getPoller()->async_first([weakSlef]() {
auto stronSelf = weakSlef.lock();
if (stronSelf) {
stronSelf->_mediaMuxer.reset();
stronSelf->teardown();
}
});
return true;
}

View File

@@ -51,7 +51,8 @@ public:
const string &strSrc,
bool bEnableHls = true,
bool bEnableMp4 = false,
int iRetryCount = -1);
int iRetryCount = -1,
const EventPoller::Ptr &poller = nullptr);
virtual ~PlayerProxy();