Skip to content

Commit 05c3a42

Browse files
winlinvipsuzp1984
andauthored
HTTP-FLV: Notify connection to expire when unpublishing. v6.0.152 v7.0.11 (#4164)
When stopping the stream, it will wait for the HTTP Streaming to exit. If the HTTP Streaming goroutine hangs, it will not exit automatically. ```cpp void SrsHttpStreamServer::http_unmount(SrsRequest* r) { SrsUniquePtr<SrsLiveStream> stream(entry->stream); if (stream->entry) stream->entry->enabled = false; srs_usleep(...); // Wait for about 120s. mux.unhandle(entry->mount, stream.get()); // Free stream. } srs_error_t SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) { err = do_serve_http(w, r); // If stuck in here for 120s+ alive_viewers_--; // Crash at here, because stream has been deleted. ``` We should notify http stream connection to interrupt(expire): ```cpp void SrsHttpStreamServer::http_unmount(SrsRequest* r) { SrsUniquePtr<SrsLiveStream> stream(entry->stream); if (stream->entry) stream->entry->enabled = false; stream->expire(); // Notify http stream to interrupt. ``` Note that we should notify all viewers pulling stream from this http stream. Note that we have tried to fix this issue, but only try to wait for all viewers to quit, without interrupting the viewers, see #4144 --------- Co-authored-by: Jacob Su <[email protected]>
1 parent f8319d6 commit 05c3a42

6 files changed

+36
-11
lines changed

trunk/doc/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The changelog for SRS.
77
<a name="v7-changes"></a>
88

99
## SRS 7.0 Changelog
10+
* v7.0, 2024-08-31, Merge [#4164](https://github.com/ossrs/srs/pull/4164): HTTP-FLV: Notify connection to expire when unpublishing. v7.0.11 (#4164)
1011
* v7.0, 2024-08-24, Merge [#4157](https://github.com/ossrs/srs/pull/4157): Fix crash when quiting. v7.0.10 (#4157)
1112
* v7.0, 2024-08-24, Merge [#4156](https://github.com/ossrs/srs/pull/4156): Build: Fix srs_mp4_parser compiling error. v7.0.9 (#4156)
1213
* v7.0, 2024-08-22, Merge [#4154](https://github.com/ossrs/srs/pull/4154): ASAN: Disable memory leak detection by default. v7.0.8 (#4154)
@@ -22,6 +23,7 @@ The changelog for SRS.
2223
<a name="v6-changes"></a>
2324

2425
## SRS 6.0 Changelog
26+
* v6.0, 2024-08-31, Merge [#4164](https://github.com/ossrs/srs/pull/4164): HTTP-FLV: Notify connection to expire when unpublishing. v6.0.152 (#4164)
2527
* v6.0, 2024-08-24, Merge [#4157](https://github.com/ossrs/srs/pull/4157): Fix crash when quiting. v6.0.151 (#4157)
2628
* v6.0, 2024-08-24, Merge [#4156](https://github.com/ossrs/srs/pull/4156): Build: Fix srs_mp4_parser compiling error. v6.0.150 (#4156)
2729
* v6.0, 2024-08-21, Merge [#4150](https://github.com/ossrs/srs/pull/4150): API: Support new HTTP API for VALGRIND. v6.0.149 (#4150)

trunk/src/app/srs_app_http_api.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -1206,8 +1206,6 @@ SrsGoApiSignal::~SrsGoApiSignal()
12061206

12071207
srs_error_t SrsGoApiSignal::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
12081208
{
1209-
srs_error_t err = srs_success;
1210-
12111209
std::string signal = r->query_get("signo");
12121210
srs_trace("query signo=%s", signal.c_str());
12131211

trunk/src/app/srs_app_http_stream.cpp

+25-5
Original file line numberDiff line numberDiff line change
@@ -583,13 +583,15 @@ SrsLiveStream::SrsLiveStream(SrsRequest* r, SrsBufferCache* c)
583583
cache = c;
584584
req = r->copy()->as_http();
585585
security_ = new SrsSecurity();
586-
alive_viewers_ = 0;
587586
}
588587

589588
SrsLiveStream::~SrsLiveStream()
590589
{
591590
srs_freep(req);
592591
srs_freep(security_);
592+
593+
// The live stream should never be destroyed when it's serving any viewers.
594+
srs_assert(viewers_.empty());
593595
}
594596

595597
srs_error_t SrsLiveStream::update_auth(SrsRequest* r)
@@ -634,18 +636,35 @@ srs_error_t SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage
634636
return srs_error_wrap(err, "http hook");
635637
}
636638

637-
alive_viewers_++;
639+
// Add the viewer to the viewers list.
640+
viewers_.push_back(hc);
641+
642+
// Serve the viewer connection.
638643
err = do_serve_http(w, r);
639-
alive_viewers_--;
640-
644+
645+
// Remove viewer from the viewers list.
646+
vector<ISrsExpire*>::iterator it = std::find(viewers_.begin(), viewers_.end(), hc);
647+
srs_assert (it != viewers_.end());
648+
viewers_.erase(it);
649+
650+
// Do hook after serving.
641651
http_hooks_on_stop(r);
642652

643653
return err;
644654
}
645655

646656
bool SrsLiveStream::alive()
647657
{
648-
return alive_viewers_ > 0;
658+
return !viewers_.empty();
659+
}
660+
661+
void SrsLiveStream::expire()
662+
{
663+
vector<ISrsExpire*>::iterator it;
664+
for (it = viewers_.begin(); it != viewers_.end(); ++it) {
665+
ISrsExpire* conn = *it;
666+
conn->expire();
667+
}
649668
}
650669

651670
srs_error_t SrsLiveStream::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
@@ -1075,6 +1094,7 @@ void SrsHttpStreamServer::http_unmount(SrsRequest* r)
10751094

10761095
// Notify cache and stream to stop.
10771096
if (stream->entry) stream->entry->enabled = false;
1097+
stream->expire();
10781098
cache->stop();
10791099

10801100
// Wait for cache and stream to stop.

trunk/src/app/srs_app_http_stream.hpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <srs_app_security.hpp>
1212
#include <srs_app_http_conn.hpp>
1313

14+
#include <vector>
15+
1416
class SrsAacTransmuxer;
1517
class SrsMp3Transmuxer;
1618
class SrsFlvTransmuxer;
@@ -176,7 +178,7 @@ class SrsBufferWriter : public SrsFileWriter
176178

177179
// HTTP Live Streaming, to transmux RTMP to HTTP FLV or other format.
178180
// TODO: FIXME: Rename to SrsHttpLive
179-
class SrsLiveStream : public ISrsHttpHandler
181+
class SrsLiveStream : public ISrsHttpHandler, public ISrsExpire
180182
{
181183
private:
182184
SrsRequest* req;
@@ -185,14 +187,17 @@ class SrsLiveStream : public ISrsHttpHandler
185187
// For multiple viewers, which means there will more than one alive viewers for a live stream, so we must
186188
// use an int value to represent if there is any viewer is alive. We should never do cleanup unless all
187189
// viewers closed the connection.
188-
int alive_viewers_;
190+
std::vector<ISrsExpire*> viewers_;
189191
public:
190192
SrsLiveStream(SrsRequest* r, SrsBufferCache* c);
191193
virtual ~SrsLiveStream();
192194
virtual srs_error_t update_auth(SrsRequest* r);
193195
public:
194196
virtual srs_error_t serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
195197
virtual bool alive();
198+
// Interface ISrsExpire
199+
public:
200+
virtual void expire();
196201
private:
197202
virtual srs_error_t do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
198203
virtual srs_error_t http_hooks_on_play(ISrsHttpMessage* r);

trunk/src/core/srs_core_version6.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
#define VERSION_MAJOR 6
1111
#define VERSION_MINOR 0
12-
#define VERSION_REVISION 151
12+
#define VERSION_REVISION 152
1313

1414
#endif

trunk/src/core/srs_core_version7.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
#define VERSION_MAJOR 7
1111
#define VERSION_MINOR 0
12-
#define VERSION_REVISION 10
12+
#define VERSION_REVISION 11
1313

1414
#endif

0 commit comments

Comments
 (0)