Compare commits

...

2 Commits

Author SHA1 Message Date
xia-chu
88b422db08 优化pauseRtpCheck接口,新增pause_seconds参数
Some checks are pending
Android / build (push) Waiting to run
CodeQL / Analyze (cpp) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
Docker / build (push) Waiting to run
Linux / build (push) Waiting to run
macOS / build (push) Waiting to run
Windows / build (push) Waiting to run
2025-09-09 23:11:48 +08:00
xia-chu
5686027fc2 修复MediaSource::close线程安全问题
主动或无人观看关闭流可能会由于线程安全问题导致崩溃
2025-09-09 22:02:20 +08:00
13 changed files with 51 additions and 74 deletions

View File

@ -102,7 +102,7 @@ API_EXPORT void API_CALL mk_rtp_pause_check(const char *app, const char *stream)
auto src = MediaSource::find(DEFAULT_VHOST, app, stream); auto src = MediaSource::find(DEFAULT_VHOST, app, stream);
auto process = src ? src->getRtpProcess() : nullptr; auto process = src ? src->getRtpProcess() : nullptr;
if (process) { if (process) {
process->setStopCheckRtp(true); process->pauseRtpTimeout(true);
} }
} }
@ -110,7 +110,7 @@ API_EXPORT void API_CALL mk_rtp_resume_check(const char *app, const char *stream
auto src = MediaSource::find(DEFAULT_VHOST, app, stream); auto src = MediaSource::find(DEFAULT_VHOST, app, stream);
auto process = src ? src->getRtpProcess() : nullptr; auto process = src ? src->getRtpProcess() : nullptr;
if (process) { if (process) {
process->setStopCheckRtp(false); process->pauseRtpTimeout(false);
} }
} }

View File

@ -2019,6 +2019,12 @@
"key": "stream_id", "key": "stream_id",
"value": "test", "value": "test",
"description": "该端口绑定的流id" "description": "该端口绑定的流id"
},
{
"key": "pause_seconds",
"value": "300",
"description": "暂停超时监测后将在pause_seconds时间后恢复",
"disabled": true
} }
] ]
} }

View File

@ -1106,9 +1106,8 @@ void installWebApi() {
bool force = allArgs["force"].as<bool>(); bool force = allArgs["force"].as<bool>();
for (auto &media : media_list) { for (auto &media : media_list) {
if (media->close(force)) { media->getOwnerPoller()->async([media, force]() { media->close(force); });
++count_closed; ++count_closed;
}
} }
val["count_hit"] = count_hit; val["count_hit"] = count_hit;
val["count_closed"] = count_closed; val["count_closed"] = count_closed;
@ -1735,7 +1734,7 @@ void installWebApi() {
auto src = MediaSource::find(vhost, app, allArgs["stream_id"]); auto src = MediaSource::find(vhost, app, allArgs["stream_id"]);
auto process = src ? src->getRtpProcess() : nullptr; auto process = src ? src->getRtpProcess() : nullptr;
if (process) { if (process) {
process->setStopCheckRtp(true); process->pauseRtpTimeout(true, allArgs["pause_seconds"]);
} else { } else {
val["code"] = API::NotFound; val["code"] = API::NotFound;
} }
@ -1755,7 +1754,7 @@ void installWebApi() {
auto src = MediaSource::find(vhost, app, allArgs["stream_id"]); auto src = MediaSource::find(vhost, app, allArgs["stream_id"]);
auto process = src ? src->getRtpProcess() : nullptr; auto process = src ? src->getRtpProcess() : nullptr;
if (process) { if (process) {
process->setStopCheckRtp(false); process->pauseRtpTimeout(false);
} else { } else {
val["code"] = API::NotFound; val["code"] = API::NotFound;
} }

View File

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

View File

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

View File

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

View File

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

View File

@ -203,27 +203,24 @@ void RtpProcess::doCachedFunc() {
} }
bool RtpProcess::alive() { bool RtpProcess::alive() {
if (_stop_rtp_check.load()) { if (_pause_timeout) {
if(_last_check_alive.elapsedTime() > 5 * 60 * 1000){ if (_last_check_alive.elapsedTime() < _pause_seconds * 1000) {
// 最多暂停5分钟的rtp超时检测因为NAT映射有效期一般不会太长 [AUTO-TRANSLATED:2df59aad]
// Pause the RTP timeout detection for a maximum of 5 minutes, because the NAT mapping validity period is generally not very long.
_stop_rtp_check = false;
} else {
return true; return true;
} }
// 最多暂停_pause_seconds秒的rtp超时检测因为NAT映射有效期一般不会太长
_pause_timeout = false;
} }
_last_check_alive.resetTime(); _last_check_alive.resetTime();
GET_CONFIG(uint64_t, timeoutSec, RtpProxy::kTimeoutSec) GET_CONFIG(uint64_t, timeoutSec, RtpProxy::kTimeoutSec)
if (_last_frame_time.elapsedTime() / 1000 < timeoutSec) { return _last_frame_time.elapsedTime() < timeoutSec * 1000;
return true;
}
return false;
} }
void RtpProcess::setStopCheckRtp(bool is_check){ void RtpProcess::pauseRtpTimeout(bool pause, uint32_t pause_seconds) {
_stop_rtp_check = is_check; _pause_timeout = pause;
if (!is_check) { // 默认5分钟恢复超时监测
_pause_seconds = pause_seconds ? pause_seconds : 300;
if (!pause) {
_last_frame_time.resetTime(); _last_frame_time.resetTime();
} }
} }

View File

@ -69,12 +69,11 @@ public:
void setOnDetach(onDetachCB cb); void setOnDetach(onDetachCB cb);
/** /**
* onDetach事件回调,false检查RTP超时true停止 * rtp超时监测
* Set onDetach event callback, false checks RTP timeout, true stops * @param pause
* @param pause_seconds (); 0300
* [AUTO-TRANSLATED:2780397f]
*/ */
void setStopCheckRtp(bool is_check=false); void pauseRtpTimeout(bool pause, uint32_t pause_seconds = 0);
/** /**
* track/ * track/
@ -129,10 +128,12 @@ private:
void createTimer(); void createTimer();
private: private:
OnlyTrack _only_track = kAll; bool _pause_timeout = false;
std::string _auth_err; uint32_t _pause_seconds = 5 * 60;
uint64_t _dts = 0; uint64_t _dts = 0;
uint64_t _total_bytes = 0; uint64_t _total_bytes = 0;
OnlyTrack _only_track = kAll;
std::string _auth_err;
std::unique_ptr<sockaddr_storage> _addr; std::unique_ptr<sockaddr_storage> _addr;
toolkit::Socket::Ptr _sock; toolkit::Socket::Ptr _sock;
MediaInfo _media_info; MediaInfo _media_info;
@ -142,7 +143,6 @@ private:
std::shared_ptr<FILE> _save_file_video; std::shared_ptr<FILE> _save_file_video;
ProcessInterface::Ptr _process; ProcessInterface::Ptr _process;
MultiMediaSourceMuxer::Ptr _muxer; MultiMediaSourceMuxer::Ptr _muxer;
std::atomic_bool _stop_rtp_check{false};
toolkit::Timer::Ptr _timer; toolkit::Timer::Ptr _timer;
toolkit::Ticker _last_check_alive; toolkit::Ticker _last_check_alive;
std::recursive_mutex _func_mtx; std::recursive_mutex _func_mtx;

View File

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

View File

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

View File

@ -145,16 +145,8 @@ void SrtTransportImp::onShutdown(const SockException &ex) {
} }
bool SrtTransportImp::close(mediakit::MediaSource &sender) { bool SrtTransportImp::close(mediakit::MediaSource &sender) {
std::string err = StrPrinter << "close media: " << sender.getUrl(); onShutdown(SockException(Err_shutdown, "close media: " + sender.getUrl()));
weak_ptr<SrtTransportImp> weak_self = static_pointer_cast<SrtTransportImp>(shared_from_this()); _muxer = nullptr;
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;
}
});
return true; return true;
} }

View File

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