1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

HTTP-FLV: Notify connection to expire when unpublishing. v6.0.152 (#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
https://github.com/ossrs/srs/pull/4144

---------

Co-authored-by: Jacob Su <suzp1984@gmail.com>
This commit is contained in:
Winlin 2024-08-31 23:15:51 +08:00 committed by winlin
parent da5683e478
commit d27e530f84
5 changed files with 34 additions and 10 deletions

View file

@ -583,13 +583,15 @@ SrsLiveStream::SrsLiveStream(SrsRequest* r, SrsBufferCache* c)
cache = c;
req = r->copy()->as_http();
security_ = new SrsSecurity();
alive_viewers_ = 0;
}
SrsLiveStream::~SrsLiveStream()
{
srs_freep(req);
srs_freep(security_);
// The live stream should never be destroyed when it's serving any viewers.
srs_assert(viewers_.empty());
}
srs_error_t SrsLiveStream::update_auth(SrsRequest* r)
@ -634,10 +636,18 @@ srs_error_t SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage
return srs_error_wrap(err, "http hook");
}
alive_viewers_++;
// Add the viewer to the viewers list.
viewers_.push_back(hc);
// Serve the viewer connection.
err = do_serve_http(w, r);
alive_viewers_--;
// Remove viewer from the viewers list.
vector<ISrsExpire*>::iterator it = std::find(viewers_.begin(), viewers_.end(), hc);
srs_assert (it != viewers_.end());
viewers_.erase(it);
// Do hook after serving.
http_hooks_on_stop(r);
return err;
@ -645,7 +655,16 @@ srs_error_t SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage
bool SrsLiveStream::alive()
{
return alive_viewers_ > 0;
return !viewers_.empty();
}
void SrsLiveStream::expire()
{
vector<ISrsExpire*>::iterator it;
for (it = viewers_.begin(); it != viewers_.end(); ++it) {
ISrsExpire* conn = *it;
conn->expire();
}
}
srs_error_t SrsLiveStream::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
@ -1075,6 +1094,7 @@ void SrsHttpStreamServer::http_unmount(SrsRequest* r)
// Notify cache and stream to stop.
if (stream->entry) stream->entry->enabled = false;
stream->expire();
cache->stop();
// Wait for cache and stream to stop.