From 629bce3b1f81a5a46fea5a0dd29a0dae05157a87 Mon Sep 17 00:00:00 2001 From: Jacob Su Date: Tue, 5 Nov 2024 22:12:43 +0800 Subject: [PATCH] fix rtc over tcp crash when stop rtc publish. @see https://github.com/ossrs/srs/issues/4220 --- trunk/src/app/srs_app_rtc_network.cpp | 16 ++++++++++++++++ trunk/src/app/srs_app_rtc_network.hpp | 6 +++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/trunk/src/app/srs_app_rtc_network.cpp b/trunk/src/app/srs_app_rtc_network.cpp index 216938eae1..bb28e6a489 100644 --- a/trunk/src/app/srs_app_rtc_network.cpp +++ b/trunk/src/app/srs_app_rtc_network.cpp @@ -713,10 +713,12 @@ SrsRtcTcpConn::SrsRtcTcpConn(ISrsProtocolReadWriter* skt, std::string cip, int p delta_->set_io(skt_, skt_); session_ = NULL; pkt_ = new char[SRS_RTC_TCP_PACKET_MAX]; + _srs_rtc_manager->subscribe(this); } SrsRtcTcpConn::~SrsRtcTcpConn() { + _srs_rtc_manager->unsubscribe(this); srs_freepa(pkt_); srs_freep(delta_); srs_freep(skt_); @@ -808,6 +810,20 @@ srs_error_t SrsRtcTcpConn::cycle() return srs_success; } +void SrsRtcTcpConn::on_before_dispose(ISrsResource* c) +{ + SrsRtcConnection* session = dynamic_cast(c); + if (session && session == session_ && session_->tcp()->is_establelished()) { + session_->tcp()->set_state(SrsRtcNetworkStateClosed); + session_->expire(); + session_ = NULL; + } +} + +void SrsRtcTcpConn::on_disposing(ISrsResource* c) +{ +} + srs_error_t SrsRtcTcpConn::do_cycle() { srs_error_t err = srs_success; diff --git a/trunk/src/app/srs_app_rtc_network.hpp b/trunk/src/app/srs_app_rtc_network.hpp index 8b31a36170..4c7d1a3cfb 100644 --- a/trunk/src/app/srs_app_rtc_network.hpp +++ b/trunk/src/app/srs_app_rtc_network.hpp @@ -232,7 +232,7 @@ class SrsRtcTcpNetwork: public ISrsRtcNetwork }; // For WebRTC over TCP. -class SrsRtcTcpConn : public ISrsConnection, public ISrsCoroutineHandler, public ISrsExecutorHandler +class SrsRtcTcpConn : public ISrsConnection, public ISrsCoroutineHandler, public ISrsExecutorHandler, public ISrsDisposingHandler { private: // Because session references to this object, so we should directly use the session ptr. @@ -277,6 +277,10 @@ class SrsRtcTcpConn : public ISrsConnection, public ISrsCoroutineHandler, public // Interface ISrsCoroutineHandler public: virtual srs_error_t cycle(); +// Interface ISrsDisposingHandler +public: + virtual void on_before_dispose(ISrsResource* c); + virtual void on_disposing(ISrsResource* c); private: virtual srs_error_t do_cycle(); srs_error_t handshake();