修复MediaSource::close线程安全问题

主动或无人观看关闭流可能会由于线程安全问题导致崩溃
This commit is contained in:
xia-chu
2025-09-09 21:59:43 +08:00
parent c89caf560a
commit 5686027fc2
9 changed files with 23 additions and 49 deletions

View File

@@ -1106,9 +1106,8 @@ void installWebApi() {
bool force = allArgs["force"].as<bool>();
for (auto &media : media_list) {
if (media->close(force)) {
++count_closed;
}
media->getOwnerPoller()->async([media, force]() { media->close(force); });
++count_closed;
}
val["count_hit"] = count_hit;
val["count_closed"] = count_closed;

View File

@@ -634,7 +634,10 @@ void installWebHook() {
// 边沿站无人观看时如果是拉流的则立即停止溯源 [AUTO-TRANSLATED:a1429c77]
// If no one is watching at the edge station, stop tracing immediately if it is pulling
if (!auto_close) {
sender.close(false);
auto ptr = sender.shared_from_this();
sender.getOwnerPoller()->async([ptr]() {
ptr->close(false);
});
WarnL << "Auto close stream when none reader: " << sender.getOriginUrl();
}
return;
@@ -661,7 +664,7 @@ void installWebHook() {
if (!flag || !err.empty() || !strongSrc) {
return;
}
strongSrc->close(false);
strongSrc->getOwnerPoller()->async([strongSrc]() { strongSrc->close(false); });
WarnL << "无人观看主动关闭流:" << strongSrc->getOriginUrl();
});
});

View File

@@ -698,13 +698,13 @@ void MediaSourceEvent::onReaderChanged(MediaSource &sender, int size){
// 此流被标记为无人观看自动关闭流 [AUTO-TRANSLATED:64a0dac3]
// This stream is marked as an automatically closed stream with no viewers.
WarnL << "Auto close stream when none reader: " << strong_sender->getUrl();
strong_sender->close(false);
strong_sender->getOwnerPoller()->async([strong_sender]() { strong_sender->close(false); });
}
} else {
// 这个是mp4点播我们自动关闭 [AUTO-TRANSLATED:8a7b9a90]
// This is an mp4 on-demand, we automatically close it.
WarnL << "MP4点播无人观看,自动关闭:" << strong_sender->getUrl();
strong_sender->close(false);
strong_sender->getOwnerPoller()->async([strong_sender]() { strong_sender->close(false); });
}
return false;
}, specified_poller);

View File

@@ -254,16 +254,9 @@ void PlayerProxy::rePlay(const string &strUrl, int iFailedCnt) {
bool PlayerProxy::close(MediaSource &sender) {
// 通知其停止推流 [AUTO-TRANSLATED:d69d10d8]
// Notify it to stop pushing the stream
weak_ptr<PlayerProxy> weakSelf = dynamic_pointer_cast<PlayerProxy>(shared_from_this());
getPoller()->async_first([weakSelf]() {
auto strongSelf = weakSelf.lock();
if (!strongSelf) {
return;
}
strongSelf->_muxer.reset();
strongSelf->setMediaSource(nullptr);
strongSelf->teardown();
});
_muxer = nullptr;
setMediaSource(nullptr);
teardown();
_on_close(SockException(Err_shutdown, "closed by user"));
WarnL << "close media: " << sender.getUrl();
return true;

View File

@@ -591,9 +591,7 @@ void RtmpSession::onSendMedia(const RtmpPacket::Ptr &pkt) {
}
bool RtmpSession::close(MediaSource &sender) {
//此回调在其他线程触发
string err = StrPrinter << "close media: " << sender.getUrl();
safeShutdown(SockException(Err_shutdown, err));
shutdown(SockException(Err_shutdown, "close media: " + sender.getUrl()));
return true;
}

View File

@@ -1175,9 +1175,7 @@ int RtspSession::getTrackIndexByInterleaved(int interleaved) {
}
bool RtspSession::close(MediaSource &sender) {
//此回调在其他线程触发
string err = StrPrinter << "close media: " << sender.getUrl();
safeShutdown(SockException(Err_shutdown,err));
shutdown(SockException(Err_shutdown,"close media: " + sender.getUrl()));
return true;
}

View File

@@ -36,9 +36,9 @@ public:
if (!media) {
break;
}
if (!media->close(true)) {
break;
}
media->getOwnerPoller()->async([media]() {
media->close(true);
});
(*stream) << "\t踢出成功:" << media->getUrl() << "\r\n";
return;
} while (0);

View File

@@ -145,16 +145,8 @@ void SrtTransportImp::onShutdown(const SockException &ex) {
}
bool SrtTransportImp::close(mediakit::MediaSource &sender) {
std::string err = StrPrinter << "close media: " << sender.getUrl();
weak_ptr<SrtTransportImp> weak_self = static_pointer_cast<SrtTransportImp>(shared_from_this());
getPoller()->async([weak_self, err]() {
auto strong_self = weak_self.lock();
if (strong_self) {
strong_self->onShutdown(SockException(Err_shutdown, err));
// 主动关闭推流,那么不延时注销
strong_self->_muxer = nullptr;
}
});
onShutdown(SockException(Err_shutdown, "close media: " + sender.getUrl()));
_muxer = nullptr;
return true;
}

View File

@@ -42,19 +42,10 @@ WebRtcPusher::WebRtcPusher(const EventPoller::Ptr &poller,
}
bool WebRtcPusher::close(MediaSource &sender) {
// 此回调在其他线程触发 [AUTO-TRANSLATED:c98e7686]
// This callback is triggered in another thread
string err = StrPrinter << "close media: " << sender.getUrl();
weak_ptr<WebRtcPusher> weak_self = static_pointer_cast<WebRtcPusher>(shared_from_this());
getPoller()->async([weak_self, err]() {
auto strong_self = weak_self.lock();
if (strong_self) {
strong_self->onShutdown(SockException(Err_shutdown, err));
// 主动关闭推流,那么不延时注销 [AUTO-TRANSLATED:ee7cc580]
// Actively close the stream, then do not delay the logout
strong_self->_push_src = nullptr;
}
});
onShutdown(SockException(Err_shutdown, "close media: " + sender.getUrl()));
// 主动关闭推流,那么不延时注销 [AUTO-TRANSLATED:ee7cc580]
// Actively close the stream, then do not delay the logout
_push_src = nullptr;
return true;
}