Skip to content

Commit

Permalink
HTTP-FLV: Notify connection to expire.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Aug 31, 2024
1 parent f8319d6 commit f6736af
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
28 changes: 23 additions & 5 deletions trunk/src/app/srs_app_http_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,13 +583,13 @@ 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_);
viewers_.clear();
}

srs_error_t SrsLiveStream::update_auth(SrsRequest* r)
Expand Down Expand Up @@ -634,18 +634,35 @@ 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;
}

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)
Expand Down Expand Up @@ -1075,6 +1092,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.
Expand Down
9 changes: 7 additions & 2 deletions trunk/src/app/srs_app_http_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <srs_app_security.hpp>
#include <srs_app_http_conn.hpp>

#include <vector>

class SrsAacTransmuxer;
class SrsMp3Transmuxer;
class SrsFlvTransmuxer;
Expand Down Expand Up @@ -176,7 +178,7 @@ class SrsBufferWriter : public SrsFileWriter

// HTTP Live Streaming, to transmux RTMP to HTTP FLV or other format.
// TODO: FIXME: Rename to SrsHttpLive
class SrsLiveStream : public ISrsHttpHandler
class SrsLiveStream : public ISrsHttpHandler, public ISrsExpire
{
private:
SrsRequest* req;
Expand All @@ -185,14 +187,17 @@ class SrsLiveStream : public ISrsHttpHandler
// For multiple viewers, which means there will more than one alive viewers for a live stream, so we must
// use an int value to represent if there is any viewer is alive. We should never do cleanup unless all
// viewers closed the connection.
int alive_viewers_;
std::vector<ISrsExpire*> viewers_;
public:
SrsLiveStream(SrsRequest* r, SrsBufferCache* c);
virtual ~SrsLiveStream();
virtual srs_error_t update_auth(SrsRequest* r);
public:
virtual srs_error_t serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
virtual bool alive();
// Interface ISrsExpire
public:
virtual void expire();
private:
virtual srs_error_t do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
virtual srs_error_t http_hooks_on_play(ISrsHttpMessage* r);
Expand Down

0 comments on commit f6736af

Please sign in to comment.