Ensure thread safety of player and pusher

This commit is contained in:
xiongziliang 2024-02-07 23:06:42 +08:00
parent 306a32131c
commit 81b08af0ce
2 changed files with 33 additions and 22 deletions

View File

@ -23,11 +23,16 @@ namespace mediakit {
PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &in_poller, const string &url_in) { PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &in_poller, const string &url_in) {
auto poller = in_poller ? in_poller : EventPollerPool::Instance().getPoller(); auto poller = in_poller ? in_poller : EventPollerPool::Instance().getPoller();
static auto releasePlayer = [poller](PlayerBase *ptr) { std::weak_ptr<EventPoller> weak_poller = poller;
static auto release_func = [weak_poller](PlayerBase *ptr) {
if (auto poller = weak_poller.lock()) {
poller->async([ptr]() { poller->async([ptr]() {
onceToken token(nullptr, [&]() { delete ptr; }); onceToken token(nullptr, [&]() { delete ptr; });
ptr->teardown(); ptr->teardown();
}); });
} else {
delete ptr;
}
}; };
string url = url_in; string url = url_in;
string prefix = findSubString(url.data(), NULL, "://"); string prefix = findSubString(url.data(), NULL, "://");
@ -38,29 +43,29 @@ PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &in_poller, cons
} }
if (strcasecmp("rtsps", prefix.data()) == 0) { if (strcasecmp("rtsps", prefix.data()) == 0) {
return PlayerBase::Ptr(new TcpClientWithSSL<RtspPlayerImp>(poller), releasePlayer); return PlayerBase::Ptr(new TcpClientWithSSL<RtspPlayerImp>(poller), release_func);
} }
if (strcasecmp("rtsp", prefix.data()) == 0) { if (strcasecmp("rtsp", prefix.data()) == 0) {
return PlayerBase::Ptr(new RtspPlayerImp(poller), releasePlayer); return PlayerBase::Ptr(new RtspPlayerImp(poller), release_func);
} }
if (strcasecmp("rtmps", prefix.data()) == 0) { if (strcasecmp("rtmps", prefix.data()) == 0) {
return PlayerBase::Ptr(new TcpClientWithSSL<RtmpPlayerImp>(poller), releasePlayer); return PlayerBase::Ptr(new TcpClientWithSSL<RtmpPlayerImp>(poller), release_func);
} }
if (strcasecmp("rtmp", prefix.data()) == 0) { if (strcasecmp("rtmp", prefix.data()) == 0) {
return PlayerBase::Ptr(new RtmpPlayerImp(poller), releasePlayer); return PlayerBase::Ptr(new RtmpPlayerImp(poller), release_func);
} }
if ((strcasecmp("http", prefix.data()) == 0 || strcasecmp("https", prefix.data()) == 0)) { if ((strcasecmp("http", prefix.data()) == 0 || strcasecmp("https", prefix.data()) == 0)) {
if (end_with(url, ".m3u8") || end_with(url_in, ".m3u8")) { if (end_with(url, ".m3u8") || end_with(url_in, ".m3u8")) {
return PlayerBase::Ptr(new HlsPlayerImp(poller), releasePlayer); return PlayerBase::Ptr(new HlsPlayerImp(poller), release_func);
} }
if (end_with(url, ".ts") || end_with(url_in, ".ts")) { if (end_with(url, ".ts") || end_with(url_in, ".ts")) {
return PlayerBase::Ptr(new TsPlayerImp(poller), releasePlayer); return PlayerBase::Ptr(new TsPlayerImp(poller), release_func);
} }
if (end_with(url, ".flv") || end_with(url_in, ".flv")) { if (end_with(url, ".flv") || end_with(url_in, ".flv")) {
return PlayerBase::Ptr(new FlvPlayerImp(poller), releasePlayer); return PlayerBase::Ptr(new FlvPlayerImp(poller), release_func);
} }
} }

View File

@ -17,31 +17,37 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
PusherBase::Ptr PusherBase::createPusher(const EventPoller::Ptr &poller, PusherBase::Ptr PusherBase::createPusher(const EventPoller::Ptr &in_poller,
const MediaSource::Ptr &src, const MediaSource::Ptr &src,
const std::string & url) { const std::string & url) {
static auto releasePusher = [](PusherBase *ptr){ auto poller = in_poller ? in_poller : EventPollerPool::Instance().getPoller();
onceToken token(nullptr,[&](){ std::weak_ptr<EventPoller> weak_poller = poller;
delete ptr; static auto release_func = [weak_poller](PusherBase *ptr) {
}); if (auto poller = weak_poller.lock()) {
poller->async([ptr]() {
onceToken token(nullptr, [&]() { delete ptr; });
ptr->teardown(); ptr->teardown();
});
} else {
delete ptr;
}
}; };
std::string prefix = findSubString(url.data(), NULL, "://"); std::string prefix = findSubString(url.data(), NULL, "://");
if (strcasecmp("rtsps",prefix.data()) == 0) { if (strcasecmp("rtsps",prefix.data()) == 0) {
return PusherBase::Ptr(new TcpClientWithSSL<RtspPusherImp>(poller, std::dynamic_pointer_cast<RtspMediaSource>(src)), releasePusher); return PusherBase::Ptr(new TcpClientWithSSL<RtspPusherImp>(poller, std::dynamic_pointer_cast<RtspMediaSource>(src)), release_func);
} }
if (strcasecmp("rtsp",prefix.data()) == 0) { if (strcasecmp("rtsp",prefix.data()) == 0) {
return PusherBase::Ptr(new RtspPusherImp(poller, std::dynamic_pointer_cast<RtspMediaSource>(src)), releasePusher); return PusherBase::Ptr(new RtspPusherImp(poller, std::dynamic_pointer_cast<RtspMediaSource>(src)), release_func);
} }
if (strcasecmp("rtmps",prefix.data()) == 0) { if (strcasecmp("rtmps",prefix.data()) == 0) {
return PusherBase::Ptr(new TcpClientWithSSL<RtmpPusherImp>(poller, std::dynamic_pointer_cast<RtmpMediaSource>(src)), releasePusher); return PusherBase::Ptr(new TcpClientWithSSL<RtmpPusherImp>(poller, std::dynamic_pointer_cast<RtmpMediaSource>(src)), release_func);
} }
if (strcasecmp("rtmp",prefix.data()) == 0) { if (strcasecmp("rtmp",prefix.data()) == 0) {
return PusherBase::Ptr(new RtmpPusherImp(poller, std::dynamic_pointer_cast<RtmpMediaSource>(src)), releasePusher); return PusherBase::Ptr(new RtmpPusherImp(poller, std::dynamic_pointer_cast<RtmpMediaSource>(src)), release_func);
} }
throw std::invalid_argument("not supported push schema:" + url); throw std::invalid_argument("not supported push schema:" + url);