From 06adb9dc42fbf41bbd9fbe909d0bd78901ac9e3b Mon Sep 17 00:00:00 2001 From: winlin Date: Thu, 14 Jan 2021 19:12:01 +0800 Subject: [PATCH] Fix #2142, Async release bridger when unpublish. 4.0.60 --- README.md | 2 ++ trunk/src/app/srs_app_rtc_source.cpp | 28 ++++++++++++++++++++++++++-- trunk/src/app/srs_app_rtc_source.hpp | 8 ++++++++ trunk/src/app/srs_app_source.cpp | 16 ++++++++++++++++ trunk/src/app/srs_app_source.hpp | 9 +++++++-- trunk/src/core/srs_core_version4.hpp | 2 +- 6 files changed, 60 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bdd34cbfc..5cc643c3f 100755 --- a/README.md +++ b/README.md @@ -155,6 +155,7 @@ For previous versions, please read: ## V4 changes +* v4.0, 2021-01-14, For [#2142][bug #2142], Async release bridger when unpublish. 4.0.60 * v4.0, 2021-01-08, HTML5 video tag resolution adaptive. 4.0.59 * v4.0, 2021-01-08, Fix memory leak and bugs for RTC. 4.0.58 * v4.0, 2021-01-06, Merge #2109, Refine srs_string_split. @@ -1795,6 +1796,7 @@ Winlin [bug #1987]: https://github.com/ossrs/srs/issues/1987 [bug #1548]: https://github.com/ossrs/srs/issues/1548 [bug #1694]: https://github.com/ossrs/srs/issues/1694 +[bug #2142]: https://github.com/ossrs/srs/issues/2142 [bug #yyyyyyyyyyyyy]: https://github.com/ossrs/srs/issues/yyyyyyyyyyyyy [bug #1631]: https://github.com/ossrs/srs/issues/1631 diff --git a/trunk/src/app/srs_app_rtc_source.cpp b/trunk/src/app/srs_app_rtc_source.cpp index 39696a1ad..d53a50751 100644 --- a/trunk/src/app/srs_app_rtc_source.cpp +++ b/trunk/src/app/srs_app_rtc_source.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #ifdef SRS_FFMPEG_FIT #include @@ -323,8 +324,10 @@ SrsRtcStream::~SrsRtcStream() consumers.clear(); srs_freep(req); - srs_freep(bridger_); srs_freep(stream_desc_); + + // Async remove the bridger, to notify the sources to clear it. + _srs_rtc_manager->remove(bridger_); } srs_error_t SrsRtcStream::initialize(SrsRequest* r) @@ -483,7 +486,8 @@ void SrsRtcStream::on_unpublish() // release unpublish stream description. set_stream_desc(NULL); - srs_freep(bridger_); + // Async remove the bridger, to notify the sources to clear it. + _srs_rtc_manager->remove(bridger_); // TODO: FIXME: Handle by statistic. } @@ -717,6 +721,16 @@ srs_error_t SrsRtcFromRtmpBridger::on_audio(SrsSharedPtrMessage* msg) return err; } +const SrsContextId& SrsRtcFromRtmpBridger::get_id() +{ + return _srs_context->get_id(); +} + +std::string SrsRtcFromRtmpBridger::desc() +{ + return "Rtmp2Rtc"; +} + srs_error_t SrsRtcFromRtmpBridger::transcode(char* adts_audio, int nn_adts_audio) { srs_error_t err = srs_success; @@ -1175,6 +1189,16 @@ void SrsRtcDummyBridger::on_unpublish() { } +const SrsContextId& SrsRtcDummyBridger::get_id() +{ + return _srs_context->get_id(); +} + +std::string SrsRtcDummyBridger::desc() +{ + return "Rtmp2RtcDummy"; +} + SrsCodecPayload::SrsCodecPayload() { pt_of_publisher_ = pt_ = 0; diff --git a/trunk/src/app/srs_app_rtc_source.hpp b/trunk/src/app/srs_app_rtc_source.hpp index e2b7d5f2b..f6ed07562 100644 --- a/trunk/src/app/srs_app_rtc_source.hpp +++ b/trunk/src/app/srs_app_rtc_source.hpp @@ -247,6 +247,10 @@ public: virtual srs_error_t on_publish(); virtual void on_unpublish(); virtual srs_error_t on_audio(SrsSharedPtrMessage* msg); +// interface ISrsResource +public: + virtual const SrsContextId& get_id(); + virtual std::string desc(); private: srs_error_t transcode(char* adts_audio, int nn_adts_audio); srs_error_t package_opus(char* data, int size, SrsRtpPacket2** ppkt); @@ -272,6 +276,10 @@ public: virtual srs_error_t on_audio(SrsSharedPtrMessage* audio); virtual srs_error_t on_video(SrsSharedPtrMessage* video); virtual void on_unpublish(); +// interface ISrsResource +public: + virtual const SrsContextId& get_id(); + virtual std::string desc(); }; // TODO: FIXME: Rename it. diff --git a/trunk/src/app/srs_app_source.cpp b/trunk/src/app/srs_app_source.cpp index 3d317eb2d..0d952e003 100755 --- a/trunk/src/app/srs_app_source.cpp +++ b/trunk/src/app/srs_app_source.cpp @@ -51,6 +51,7 @@ using namespace std; #include #include #include +#include #define CONST_MAX_JITTER_MS 250 #define CONST_MAX_JITTER_MS_NEG -250 @@ -1881,10 +1882,13 @@ SrsSource::SrsSource() _srs_config->subscribe(this); atc = false; + + _srs_rtc_manager->subscribe(this); } SrsSource::~SrsSource() { + _srs_rtc_manager->unsubscribe(this); _srs_config->unsubscribe(this); // never free the consumers, @@ -1980,6 +1984,18 @@ void SrsSource::bridge_to(ISrsSourceBridger* v) bridger = v; } +void SrsSource::on_before_dispose(ISrsResource* c) +{ + ISrsSourceBridger* pb = dynamic_cast(c); + if (bridger == pb) { + bridge_to(NULL); + } +} + +void SrsSource::on_disposing(ISrsResource* c) +{ +} + srs_error_t SrsSource::on_reload_vhost_play(string vhost) { srs_error_t err = srs_success; diff --git a/trunk/src/app/srs_app_source.hpp b/trunk/src/app/srs_app_source.hpp index 4a58eb954..652e0c14d 100644 --- a/trunk/src/app/srs_app_source.hpp +++ b/trunk/src/app/srs_app_source.hpp @@ -34,6 +34,7 @@ #include #include #include +#include class SrsFormat; class SrsRtmpFormat; @@ -484,7 +485,7 @@ public: extern SrsSourceManager* _srs_sources; // For two sources to bridge with each other. -class ISrsSourceBridger +class ISrsSourceBridger : public ISrsResource { public: ISrsSourceBridger(); @@ -497,7 +498,7 @@ public: }; // live streaming source. -class SrsSource : public ISrsReloadHandler +class SrsSource : virtual public ISrsReloadHandler, virtual public ISrsDisposingHandler { friend class SrsOriginHub; private: @@ -559,6 +560,10 @@ public: virtual srs_error_t initialize(SrsRequest* r, ISrsSourceHandler* h); // Bridge to other source, forward packets to it. void bridge_to(ISrsSourceBridger* v); +// interface ISrsDisposingHandler +public: + virtual void on_before_dispose(ISrsResource* c); + virtual void on_disposing(ISrsResource* c); // Interface ISrsReloadHandler public: virtual srs_error_t on_reload_vhost_play(std::string vhost); diff --git a/trunk/src/core/srs_core_version4.hpp b/trunk/src/core/srs_core_version4.hpp index 603d1bab4..36f154c90 100644 --- a/trunk/src/core/srs_core_version4.hpp +++ b/trunk/src/core/srs_core_version4.hpp @@ -24,6 +24,6 @@ #ifndef SRS_CORE_VERSION4_HPP #define SRS_CORE_VERSION4_HPP -#define SRS_VERSION4_REVISION 59 +#define SRS_VERSION4_REVISION 60 #endif