From 15affeff1d2d843da074734f4e1a1fec9c5b66e9 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Sun, 18 Sep 2022 20:36:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=85=B3=E9=97=AD=E5=AA=92?= =?UTF-8?q?=E4=BD=93=E6=BA=90=E7=9B=B8=E5=85=B3=E9=80=BB=E8=BE=91:=20#1963?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/source/mk_media.cpp | 10 +++------- server/FFmpegSource.cpp | 6 +++--- server/FFmpegSource.h | 2 +- src/Common/MediaSource.cpp | 12 ++++++++---- src/Common/MediaSource.h | 4 ++-- src/Player/PlayerProxy.cpp | 8 ++------ src/Player/PlayerProxy.h | 2 +- src/Record/MP4Reader.cpp | 9 +++------ src/Record/MP4Reader.h | 2 +- src/Rtmp/RtmpSession.cpp | 9 +++------ src/Rtmp/RtmpSession.h | 2 +- src/Rtp/RtpSelector.cpp | 7 ++----- src/Rtp/RtpSelector.h | 2 +- src/Rtp/RtpSession.cpp | 9 +++------ src/Rtp/RtpSession.h | 2 +- src/Rtsp/RtspSession.cpp | 7 ++----- src/Rtsp/RtspSession.h | 2 +- srt/SrtTransportImp.cpp | 7 ++----- srt/SrtTransportImp.hpp | 2 +- webrtc/WebRtcPusher.cpp | 7 ++----- webrtc/WebRtcPusher.h | 2 +- 21 files changed, 44 insertions(+), 69 deletions(-) diff --git a/api/source/mk_media.cpp b/api/source/mk_media.cpp index a3462b25..49f65342 100755 --- a/api/source/mk_media.cpp +++ b/api/source/mk_media.cpp @@ -60,19 +60,15 @@ public: protected: // 通知其停止推流 - bool close(MediaSource &sender,bool force) override{ - if(!force && _channel->totalReaderCount()){ - //非强制关闭且正有人在观看该视频 - return false; - } - if(!_on_close){ + bool close(MediaSource &sender) override { + if (!_on_close) { //未设置回调,没法关闭 WarnL << "请使用mk_media_set_on_close函数设置回调函数!"; return false; } //请在回调中调用mk_media_release函数释放资源,否则MediaSource::close()操作不会生效 _on_close(_on_close_data); - WarnL << "close media:" << sender.getUrl() << " " << force; + WarnL << "close media: " << sender.getUrl(); return true; } diff --git a/server/FFmpegSource.cpp b/server/FFmpegSource.cpp index abb950bc..1392d9dc 100644 --- a/server/FFmpegSource.cpp +++ b/server/FFmpegSource.cpp @@ -273,14 +273,14 @@ void FFmpegSource::setOnClose(const function &cb){ _onClose = cb; } -bool FFmpegSource::close(MediaSource &sender, bool force) { +bool FFmpegSource::close(MediaSource &sender) { auto listener = getDelegate(); - if(listener && !listener->close(sender,force)){ + if (listener && !listener->close(sender)) { //关闭失败 return false; } //该流无人观看,我们停止吧 - if(_onClose){ + if (_onClose) { _onClose(); } return true; diff --git a/server/FFmpegSource.h b/server/FFmpegSource.h index 3bf5fe19..3f28f228 100644 --- a/server/FFmpegSource.h +++ b/server/FFmpegSource.h @@ -75,7 +75,7 @@ private: ///////MediaSourceEvent override/////// // 关闭 - bool close(mediakit::MediaSource &sender,bool force) override; + bool close(mediakit::MediaSource &sender) override; // 获取媒体源类型 mediakit::MediaOriginType getOriginType(mediakit::MediaSource &sender) const override; //获取媒体源url或者文件路径 diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index ea2ee033..d1b18f52 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -211,10 +211,14 @@ bool MediaSource::speed(float speed) { bool MediaSource::close(bool force) { auto listener = _listener.lock(); - if(!listener){ + if (!listener) { return false; } - return listener->close(*this,force); + if (!force && totalReaderCount()) { + //有人观看,不强制关闭 + return false; + } + return listener->close(*this); } float MediaSource::getLossRate(mediakit::TrackType type) { @@ -680,12 +684,12 @@ bool MediaSourceEventInterceptor::speed(MediaSource &sender, float speed) { return listener->speed(sender, speed); } -bool MediaSourceEventInterceptor::close(MediaSource &sender, bool force) { +bool MediaSourceEventInterceptor::close(MediaSource &sender) { auto listener = _listener.lock(); if (!listener) { return false; } - return listener->close(sender, force); + return listener->close(sender); } int MediaSourceEventInterceptor::totalReaderCount(MediaSource &sender) { diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index 2b67773b..8c9eb220 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -74,7 +74,7 @@ public: // 通知倍数 virtual bool speed(MediaSource &sender, float speed) { return false; } // 通知其停止产生流 - virtual bool close(MediaSource &sender, bool force) { return false; } + virtual bool close(MediaSource &sender) { return false; } // 获取观看总人数,此函数一般强制重载 virtual int totalReaderCount(MediaSource &sender) { throw NotImplemented(toolkit::demangle(typeid(*this).name()) + "::totalReaderCount not implemented"); } // 通知观看人数变化 @@ -150,7 +150,7 @@ public: bool seekTo(MediaSource &sender, uint32_t stamp) override; bool pause(MediaSource &sender, bool pause) override; bool speed(MediaSource &sender, float speed) override; - bool close(MediaSource &sender, bool force) override; + bool close(MediaSource &sender) override; int totalReaderCount(MediaSource &sender) override; void onReaderChanged(MediaSource &sender, int size) override; void onRegist(MediaSource &sender, bool regist) override; diff --git a/src/Player/PlayerProxy.cpp b/src/Player/PlayerProxy.cpp index d3f51d54..477b83f0 100644 --- a/src/Player/PlayerProxy.cpp +++ b/src/Player/PlayerProxy.cpp @@ -143,11 +143,7 @@ void PlayerProxy::rePlay(const string &strUrl, int iFailedCnt) { }, getPoller()); } -bool PlayerProxy::close(MediaSource &sender, bool force) { - if (!force && totalReaderCount()) { - return false; - } - +bool PlayerProxy::close(MediaSource &sender) { //通知其停止推流 weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); getPoller()->async_first([weakSelf]() { @@ -160,7 +156,7 @@ bool PlayerProxy::close(MediaSource &sender, bool force) { strongSelf->teardown(); }); _on_close(SockException(Err_shutdown, "closed by user")); - WarnL << sender.getUrl() << " " << force; + WarnL << "close media: " << sender.getUrl(); return true; } diff --git a/src/Player/PlayerProxy.h b/src/Player/PlayerProxy.h index 6e3a2401..970c620a 100644 --- a/src/Player/PlayerProxy.h +++ b/src/Player/PlayerProxy.h @@ -54,7 +54,7 @@ public: private: //MediaSourceEvent override - bool close(MediaSource &sender,bool force) override; + bool close(MediaSource &sender) override; int totalReaderCount(MediaSource &sender) override; MediaOriginType getOriginType(MediaSource &sender) const override; std::string getOriginUrl(MediaSource &sender) const override; diff --git a/src/Record/MP4Reader.cpp b/src/Record/MP4Reader.cpp index 10377092..66160e9b 100644 --- a/src/Record/MP4Reader.cpp +++ b/src/Record/MP4Reader.cpp @@ -230,12 +230,9 @@ bool MP4Reader::seekTo(uint32_t stamp_seek) { return false; } -bool MP4Reader::close(MediaSource &sender, bool force) { - if (!_muxer || (!force && _muxer->totalReaderCount())) { - return false; - } - _timer.reset(); - WarnL << sender.getUrl() << " " << force; +bool MP4Reader::close(MediaSource &sender) { + _timer = nullptr; + WarnL << "close media: " << sender.getUrl(); return true; } diff --git a/src/Record/MP4Reader.h b/src/Record/MP4Reader.h index 10a7ff35..b2cb87ed 100644 --- a/src/Record/MP4Reader.h +++ b/src/Record/MP4Reader.h @@ -55,7 +55,7 @@ private: bool pause(MediaSource &sender, bool pause) override; bool speed(MediaSource &sender, float speed) override; - bool close(MediaSource &sender,bool force) override; + bool close(MediaSource &sender) override; MediaOriginType getOriginType(MediaSource &sender) const override; std::string getOriginUrl(MediaSource &sender) const override; toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) override; diff --git a/src/Rtmp/RtmpSession.cpp b/src/Rtmp/RtmpSession.cpp index a5323853..1e1e86ae 100644 --- a/src/Rtmp/RtmpSession.cpp +++ b/src/Rtmp/RtmpSession.cpp @@ -569,13 +569,10 @@ void RtmpSession::onSendMedia(const RtmpPacket::Ptr &pkt) { sendRtmp(pkt->type_id, pkt->stream_index, pkt, pkt->time_stamp, pkt->chunk_id); } -bool RtmpSession::close(MediaSource &sender,bool force) { +bool RtmpSession::close(MediaSource &sender) { //此回调在其他线程触发 - if(!_push_src || (!force && _push_src->totalReaderCount())){ - return false; - } - string err = StrPrinter << "close media:" << sender.getUrl() << " " << force; - safeShutdown(SockException(Err_shutdown,err)); + string err = StrPrinter << "close media: " << sender.getUrl(); + safeShutdown(SockException(Err_shutdown, err)); return true; } diff --git a/src/Rtmp/RtmpSession.h b/src/Rtmp/RtmpSession.h index 7bce876c..0fbd4eaa 100644 --- a/src/Rtmp/RtmpSession.h +++ b/src/Rtmp/RtmpSession.h @@ -71,7 +71,7 @@ private: ///////MediaSourceEvent override/////// // 关闭 - bool close(MediaSource &sender, bool force) override; + bool close(MediaSource &sender) override; // 播放总人数 int totalReaderCount(MediaSource &sender) override; // 获取媒体源类型 diff --git a/src/Rtp/RtpSelector.cpp b/src/Rtp/RtpSelector.cpp index e60e7e10..cf9ae26d 100644 --- a/src/Rtp/RtpSelector.cpp +++ b/src/Rtp/RtpSelector.cpp @@ -134,17 +134,14 @@ void RtpProcessHelper::attachEvent() { _process->setDelegate(shared_from_this()); } -bool RtpProcessHelper::close(MediaSource &sender, bool force) { +bool RtpProcessHelper::close(MediaSource &sender) { //此回调在其他线程触发 - if (!_process || (!force && _process->totalReaderCount(sender))) { - return false; - } auto parent = _parent.lock(); if (!parent) { return false; } parent->delProcess(_stream_id, _process.get()); - WarnL << "close media:" << sender.getUrl() << " " << force; + WarnL << "close media: " << sender.getUrl(); return true; } diff --git a/src/Rtp/RtpSelector.h b/src/Rtp/RtpSelector.h index ae796ac6..746abacb 100644 --- a/src/Rtp/RtpSelector.h +++ b/src/Rtp/RtpSelector.h @@ -31,7 +31,7 @@ public: protected: // 通知其停止推流 - bool close(MediaSource &sender,bool force) override; + bool close(MediaSource &sender) override; private: std::string _stream_id; diff --git a/src/Rtp/RtpSession.cpp b/src/Rtp/RtpSession.cpp index 8c9de0c3..8b0de2bb 100644 --- a/src/Rtp/RtpSession.cpp +++ b/src/Rtp/RtpSession.cpp @@ -124,13 +124,10 @@ void RtpSession::onRtpPacket(const char *data, size_t len) { _ticker.resetTime(); } -bool RtpSession::close(MediaSource &sender, bool force) { +bool RtpSession::close(MediaSource &sender) { //此回调在其他线程触发 - if(!_process || (!force && static_pointer_cast(_process)->totalReaderCount(sender))){ - return false; - } - string err = StrPrinter << "close media:" << sender.getUrl() << " " << force; - safeShutdown(SockException(Err_shutdown,err)); + string err = StrPrinter << "close media: " << sender.getUrl(); + safeShutdown(SockException(Err_shutdown, err)); return true; } diff --git a/src/Rtp/RtpSession.h b/src/Rtp/RtpSession.h index 842ea53c..62f65443 100644 --- a/src/Rtp/RtpSession.h +++ b/src/Rtp/RtpSession.h @@ -35,7 +35,7 @@ public: protected: // 通知其停止推流 - bool close(MediaSource &sender,bool force) override; + bool close(MediaSource &sender) override; // 收到rtp回调 void onRtpPacket(const char *data, size_t len) override; // RtpSplitter override diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index e5d2d9db..9321215c 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -1136,12 +1136,9 @@ int RtspSession::getTrackIndexByInterleaved(int interleaved) { throw SockException(Err_shutdown, StrPrinter << "no such track with interleaved:" << interleaved); } -bool RtspSession::close(MediaSource &sender, bool force) { +bool RtspSession::close(MediaSource &sender) { //此回调在其他线程触发 - if(!_push_src || (!force && _push_src->totalReaderCount())){ - return false; - } - string err = StrPrinter << "close media:" << sender.getUrl() << " " << force; + string err = StrPrinter << "close media: " << sender.getUrl(); safeShutdown(SockException(Err_shutdown,err)); return true; } diff --git a/src/Rtsp/RtspSession.h b/src/Rtsp/RtspSession.h index a4efabe5..b5e50a2a 100644 --- a/src/Rtsp/RtspSession.h +++ b/src/Rtsp/RtspSession.h @@ -82,7 +82,7 @@ protected: ///////MediaSourceEvent override/////// // 关闭 - bool close(MediaSource &sender, bool force) override; + bool close(MediaSource &sender) override; // 播放总人数 int totalReaderCount(MediaSource &sender) override; // 获取媒体源类型 diff --git a/srt/SrtTransportImp.cpp b/srt/SrtTransportImp.cpp index 9147f573..8464fb5e 100644 --- a/srt/SrtTransportImp.cpp +++ b/srt/SrtTransportImp.cpp @@ -109,11 +109,8 @@ void SrtTransportImp::onShutdown(const SockException &ex) { SrtTransport::onShutdown(ex); } -bool SrtTransportImp::close(mediakit::MediaSource &sender, bool force) { - if (!force && totalReaderCount(sender)) { - return false; - } - std::string err = StrPrinter << "close media:" << sender.getUrl() << " " << force; +bool SrtTransportImp::close(mediakit::MediaSource &sender) { + std::string err = StrPrinter << "close media: " << sender.getUrl(); weak_ptr weak_self = static_pointer_cast(shared_from_this()); getPoller()->async([weak_self, err]() { auto strong_self = weak_self.lock(); diff --git a/srt/SrtTransportImp.hpp b/srt/SrtTransportImp.hpp index 987dee8e..e33156ca 100644 --- a/srt/SrtTransportImp.hpp +++ b/srt/SrtTransportImp.hpp @@ -50,7 +50,7 @@ protected: ///////MediaSourceEvent override/////// // 关闭 - bool close(mediakit::MediaSource &sender, bool force) override; + bool close(mediakit::MediaSource &sender) override; // 获取媒体源类型 mediakit::MediaOriginType getOriginType(mediakit::MediaSource &sender) const override; // 获取媒体源url或者文件路径 diff --git a/webrtc/WebRtcPusher.cpp b/webrtc/WebRtcPusher.cpp index 30182039..ebb7b6d8 100644 --- a/webrtc/WebRtcPusher.cpp +++ b/webrtc/WebRtcPusher.cpp @@ -38,12 +38,9 @@ WebRtcPusher::WebRtcPusher(const EventPoller::Ptr &poller, CHECK(_push_src); } -bool WebRtcPusher::close(MediaSource &sender, bool force) { +bool WebRtcPusher::close(MediaSource &sender) { //此回调在其他线程触发 - if (!force && totalReaderCount(sender)) { - return false; - } - string err = StrPrinter << "close media:" << sender.getUrl() << " " << force; + string err = StrPrinter << "close media: " << sender.getUrl(); weak_ptr weak_self = static_pointer_cast(shared_from_this()); getPoller()->async([weak_self, err]() { auto strong_self = weak_self.lock(); diff --git a/webrtc/WebRtcPusher.h b/webrtc/WebRtcPusher.h index f064bd9a..41122bc1 100644 --- a/webrtc/WebRtcPusher.h +++ b/webrtc/WebRtcPusher.h @@ -33,7 +33,7 @@ protected: protected: ///////MediaSourceEvent override/////// // 关闭 - bool close(mediakit::MediaSource &sender, bool force) override; + bool close(mediakit::MediaSource &sender) override; // 播放总人数 int totalReaderCount(mediakit::MediaSource &sender) override; // 获取媒体源类型