Skip to content

Commit 9dbd049

Browse files
committed
For #1568, extract SrsSourceManager from SrsSource.
1 parent fea293d commit 9dbd049

File tree

5 files changed

+49
-23
lines changed

5 files changed

+49
-23
lines changed

trunk/src/app/srs_app_http_stream.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ srs_error_t SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandle
10891089
}
10901090

10911091
SrsSource* s = NULL;
1092-
if ((err = SrsSource::fetch_or_create(r, server, &s)) != srs_success) {
1092+
if ((err = _srs_sources->fetch_or_create(r, server, &s)) != srs_success) {
10931093
return srs_error_wrap(err, "source create");
10941094
}
10951095
srs_assert(s != NULL);

trunk/src/app/srs_app_rtmp_conn.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ srs_error_t SrsRtmpConn::stream_service_cycle()
496496

497497
// find a source to serve.
498498
SrsSource* source = NULL;
499-
if ((err = SrsSource::fetch_or_create(req, server, &source)) != srs_success) {
499+
if ((err = _srs_sources->fetch_or_create(req, server, &source)) != srs_success) {
500500
return srs_error_wrap(err, "rtmp: fetch source");
501501
}
502502
srs_assert(source != NULL);

trunk/src/app/srs_app_server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ void SrsServer::dispose()
523523
// @remark don't dispose ingesters, for too slow.
524524

525525
// dispose the source for hls and dvr.
526-
SrsSource::dispose_all();
526+
_srs_sources->dispose();
527527

528528
// @remark don't dispose all connections, for too slow.
529529

@@ -952,7 +952,7 @@ srs_error_t SrsServer::do_cycle()
952952
}
953953

954954
// notice the stream sources to cycle.
955-
if ((err = SrsSource::cycle_all()) != srs_success) {
955+
if ((err = _srs_sources->cycle()) != srs_success) {
956956
return srs_error_wrap(err, "source cycle");
957957
}
958958

trunk/src/app/srs_app_source.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,9 +1635,17 @@ srs_error_t SrsMetaCache::update_vsh(SrsSharedPtrMessage* msg)
16351635
return vformat->on_video(msg);
16361636
}
16371637

1638-
std::map<std::string, SrsSource*> SrsSource::pool;
1638+
SrsSourceManager* _srs_sources = new SrsSourceManager();
16391639

1640-
srs_error_t SrsSource::fetch_or_create(SrsRequest* r, ISrsSourceHandler* h, SrsSource** pps)
1640+
SrsSourceManager::SrsSourceManager()
1641+
{
1642+
}
1643+
1644+
SrsSourceManager::~SrsSourceManager()
1645+
{
1646+
}
1647+
1648+
srs_error_t SrsSourceManager::fetch_or_create(SrsRequest* r, ISrsSourceHandler* h, SrsSource** pps)
16411649
{
16421650
srs_error_t err = srs_success;
16431651

@@ -1665,7 +1673,7 @@ srs_error_t SrsSource::fetch_or_create(SrsRequest* r, ISrsSourceHandler* h, SrsS
16651673
return err;
16661674
}
16671675

1668-
SrsSource* SrsSource::fetch(SrsRequest* r)
1676+
SrsSource* SrsSourceManager::fetch(SrsRequest* r)
16691677
{
16701678
SrsSource* source = NULL;
16711679

@@ -1679,12 +1687,12 @@ SrsSource* SrsSource::fetch(SrsRequest* r)
16791687
// we always update the request of resource,
16801688
// for origin auth is on, the token in request maybe invalid,
16811689
// and we only need to update the token of request, it's simple.
1682-
source->req->update_auth(r);
1690+
source->update_auth(r);
16831691

16841692
return source;
16851693
}
16861694

1687-
void SrsSource::dispose_all()
1695+
void SrsSourceManager::dispose()
16881696
{
16891697
std::map<std::string, SrsSource*>::iterator it;
16901698
for (it = pool.begin(); it != pool.end(); ++it) {
@@ -1694,16 +1702,16 @@ void SrsSource::dispose_all()
16941702
return;
16951703
}
16961704

1697-
srs_error_t SrsSource::cycle_all()
1705+
srs_error_t SrsSourceManager::cycle()
16981706
{
16991707
int cid = _srs_context->get_id();
1700-
srs_error_t err = do_cycle_all();
1708+
srs_error_t err = do_cycle();
17011709
_srs_context->set_id(cid);
17021710

17031711
return err;
17041712
}
17051713

1706-
srs_error_t SrsSource::do_cycle_all()
1714+
srs_error_t SrsSourceManager::do_cycle()
17071715
{
17081716
srs_error_t err = srs_success;
17091717

@@ -1744,7 +1752,7 @@ srs_error_t SrsSource::do_cycle_all()
17441752
return err;
17451753
}
17461754

1747-
void SrsSource::destroy()
1755+
void SrsSourceManager::destroy()
17481756
{
17491757
std::map<std::string, SrsSource*>::iterator it;
17501758
for (it = pool.begin(); it != pool.end(); ++it) {
@@ -1994,6 +2002,11 @@ bool SrsSource::inactive()
19942002
return _can_publish;
19952003
}
19962004

2005+
void SrsSource::update_auth(SrsRequest* r)
2006+
{
2007+
req->update_auth(r);
2008+
}
2009+
19972010
bool SrsSource::can_publish(bool is_edge)
19982011
{
19992012
if (is_edge) {

trunk/src/app/srs_app_source.hpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -438,32 +438,43 @@ class SrsMetaCache
438438
virtual srs_error_t update_vsh(SrsSharedPtrMessage* msg);
439439
};
440440

441-
// live streaming source.
442-
class SrsSource : public ISrsReloadHandler
441+
// The source manager to create and refresh all stream sources.
442+
class SrsSourceManager
443443
{
444-
friend class SrsOriginHub;
445444
private:
446-
static std::map<std::string, SrsSource*> pool;
445+
std::map<std::string, SrsSource*> pool;
446+
public:
447+
SrsSourceManager();
448+
virtual ~SrsSourceManager();
447449
public:
448450
// create source when fetch from cache failed.
449451
// @param r the client request.
450452
// @param h the event handler for source.
451453
// @param pps the matched source, if success never be NULL.
452-
static srs_error_t fetch_or_create(SrsRequest* r, ISrsSourceHandler* h, SrsSource** pps);
454+
virtual srs_error_t fetch_or_create(SrsRequest* r, ISrsSourceHandler* h, SrsSource** pps);
453455
private:
454456
// Get the exists source, NULL when not exists.
455457
// update the request and return the exists source.
456-
static SrsSource* fetch(SrsRequest* r);
458+
virtual SrsSource* fetch(SrsRequest* r);
457459
public:
458460
// dispose and cycle all sources.
459-
static void dispose_all();
460-
static srs_error_t cycle_all();
461+
virtual void dispose();
462+
virtual srs_error_t cycle();
461463
private:
462-
static srs_error_t do_cycle_all();
464+
virtual srs_error_t do_cycle();
463465
public:
464466
// when system exit, destroy the sources,
465467
// For gmc to analysis mem leaks.
466-
static void destroy();
468+
virtual void destroy();
469+
};
470+
471+
// Global singleton instance.
472+
extern SrsSourceManager* _srs_sources;
473+
474+
// live streaming source.
475+
class SrsSource : public ISrsReloadHandler
476+
{
477+
friend class SrsOriginHub;
467478
private:
468479
// For publish, it's the publish client id.
469480
// For edge, it's the edge ingest id.
@@ -531,6 +542,8 @@ class SrsSource : public ISrsReloadHandler
531542
// Whether source is inactive, which means there is no publishing stream source.
532543
// @remark For edge, it's inactive util stream has been pulled from origin.
533544
virtual bool inactive();
545+
// Update the authentication information in request.
546+
virtual void update_auth(SrsRequest* r);
534547
public:
535548
virtual bool can_publish(bool is_edge);
536549
virtual srs_error_t on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata);

0 commit comments

Comments
 (0)