From 50fa6715644c238d1c90b68350d63fd9e1706168 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sat, 26 Nov 2022 10:14:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9E=90=E6=9E=84=E4=B8=AD?= =?UTF-8?q?=E8=B0=83=E7=94=A8getOwnerPoller=E6=8A=9B=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E5=B4=A9=E6=BA=83=E7=9A=84bug:#2117?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rdpart/ZLToolKit | 2 +- server/WebApi.cpp | 3 ++- src/Common/MediaSource.cpp | 33 +++++++++++++++++++-------------- src/Common/MediaSource.h | 1 - 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/3rdpart/ZLToolKit b/3rdpart/ZLToolKit index 617b6b1d..25ba684a 160000 --- a/3rdpart/ZLToolKit +++ b/3rdpart/ZLToolKit @@ -1 +1 @@ -Subproject commit 617b6b1db23f13e2592b29204b84b1b9dbbf81c0 +Subproject commit 25ba684a49142cc59e43719c98112031e1ec4bda diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 41b88dcd..cb3b69b0 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -344,7 +344,8 @@ Value makeMediaSourceJson(MediaSource &media){ } //getLossRate有线程安全问题;使用getMediaInfo接口才能获取丢包率;getMediaList接口将忽略丢包率 - auto current_thread = media.getOwnerPoller()->isCurrentThread(); + auto current_thread = false; + try { current_thread = media.getOwnerPoller()->isCurrentThread();} catch (...) {} float last_loss = -1; for(auto &track : media.getTracks(false)){ Value obj; diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index bcd16cb9..fa37c508 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -124,7 +124,6 @@ MediaSource::MediaSource(const string &schema, const string &vhost, const string _app = app; _stream_id = stream_id; _create_stamp = time(NULL); - _default_poller = EventPollerPool::Instance().getPoller(); } MediaSource::~MediaSource() { @@ -289,23 +288,29 @@ toolkit::EventPoller::Ptr MediaSource::getOwnerPoller() { if (listener) { return listener->getOwnerPoller(*this); } - WarnL << toolkit::demangle(typeid(*this).name()) + "::getOwnerPoller failed, now return default poller: " + getUrl(); - return _default_poller; + throw std::runtime_error(toolkit::demangle(typeid(*this).name()) + "::getOwnerPoller failed: " + getUrl()); } void MediaSource::onReaderChanged(int size) { - weak_ptr weak_self = shared_from_this(); - auto listener = _listener.lock(); - if (!listener) { - return; + try { + weak_ptr weak_self = shared_from_this(); + getOwnerPoller()->async([weak_self, size]() { + auto strong_self = weak_self.lock(); + if (!strong_self) { + return; + } + auto listener = strong_self->_listener.lock(); + if (listener) { + listener->onReaderChanged(*strong_self, size); + } + }); + } catch (MediaSourceEvent::NotImplemented &ex) { + // 未实现接口,应该打印异常 + WarnL << ex.what(); + } catch (...) { + // getOwnerPoller()接口抛异常机制应该只对外不对内 + // 所以listener已经销毁导致获取归属线程失败的异常直接忽略 } - getOwnerPoller()->async([weak_self, size, listener]() { - auto strong_self = weak_self.lock(); - if (!strong_self) { - return; - } - listener->onReaderChanged(*strong_self, size); - }); } bool MediaSource::setupRecord(Recorder::type type, bool start, const string &custom_path, size_t max_second){ diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index 698cefca..e80f1e03 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -406,7 +406,6 @@ private: std::string _app; std::string _stream_id; std::weak_ptr _listener; - toolkit::EventPoller::Ptr _default_poller; // 对象个数统计 toolkit::ObjectStatistic _statistic; };