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 process = src ? src->getRtpProcess() : nullptr;
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 process = src ? src->getRtpProcess() : nullptr;
if (process) {
process->setStopCheckRtp(false);
process->pauseRtpTimeout(false);
}
}

View File

@ -2019,6 +2019,12 @@
"key": "stream_id",
"value": "test",
"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>();
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;
@ -1735,7 +1734,7 @@ void installWebApi() {
auto src = MediaSource::find(vhost, app, allArgs["stream_id"]);
auto process = src ? src->getRtpProcess() : nullptr;
if (process) {
process->setStopCheckRtp(true);
process->pauseRtpTimeout(true, allArgs["pause_seconds"]);
} else {
val["code"] = API::NotFound;
}
@ -1755,7 +1754,7 @@ void installWebApi() {
auto src = MediaSource::find(vhost, app, allArgs["stream_id"]);
auto process = src ? src->getRtpProcess() : nullptr;
if (process) {
process->setStopCheckRtp(false);
process->pauseRtpTimeout(false);
} else {
val["code"] = API::NotFound;
}

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

@ -203,27 +203,24 @@ void RtpProcess::doCachedFunc() {
}
bool RtpProcess::alive() {
if (_stop_rtp_check.load()) {
if(_last_check_alive.elapsedTime() > 5 * 60 * 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 {
if (_pause_timeout) {
if (_last_check_alive.elapsedTime() < _pause_seconds * 1000) {
return true;
}
// 最多暂停_pause_seconds秒的rtp超时检测因为NAT映射有效期一般不会太长
_pause_timeout = false;
}
_last_check_alive.resetTime();
GET_CONFIG(uint64_t, timeoutSec, RtpProxy::kTimeoutSec)
if (_last_frame_time.elapsedTime() / 1000 < timeoutSec) {
return true;
}
return false;
return _last_frame_time.elapsedTime() < timeoutSec * 1000;
}
void RtpProcess::setStopCheckRtp(bool is_check){
_stop_rtp_check = is_check;
if (!is_check) {
void RtpProcess::pauseRtpTimeout(bool pause, uint32_t pause_seconds) {
_pause_timeout = pause;
// 默认5分钟恢复超时监测
_pause_seconds = pause_seconds ? pause_seconds : 300;
if (!pause) {
_last_frame_time.resetTime();
}
}

View File

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

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;
}