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

support reload the gop_cache

This commit is contained in:
winlin 2013-12-15 12:04:28 +08:00
parent e2bb38c483
commit 2f397d0460
8 changed files with 95 additions and 25 deletions

View file

@ -198,7 +198,7 @@ usr sys idl wai hiq siq| read writ| recv send| in out | int csw
* nginx v1.5.0: 139524 lines <br/> * nginx v1.5.0: 139524 lines <br/>
### History ### History
* v0.9, 2013-12-14, support reload the hls/forwarder/transcoder. * v0.9, 2013-12-15, support reload the hls/forwarder/transcoder.
* v0.9, 2013-12-14, refine the thread model for the retry threads. * v0.9, 2013-12-14, refine the thread model for the retry threads.
* v0.9, 2013-12-10, auto install depends tools/libs on centos/ubuntu. * v0.9, 2013-12-10, auto install depends tools/libs on centos/ubuntu.
* v0.8, 2013-12-08, v0.8 released. 19186 lines. * v0.8, 2013-12-08, v0.8 released. 19186 lines.

View file

@ -26,6 +26,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <arpa/inet.h> #include <arpa/inet.h>
#include <stdlib.h> #include <stdlib.h>
using namespace std;
#include <srs_core_error.hpp> #include <srs_core_error.hpp>
#include <srs_core_log.hpp> #include <srs_core_log.hpp>
#include <srs_core_rtmp.hpp> #include <srs_core_rtmp.hpp>
@ -118,15 +120,20 @@ int SrsClient::do_cycle()
return ret; return ret;
} }
int SrsClient::on_reload_vhost_removed(SrsConfDirective* vhost) int SrsClient::on_reload_vhost_removed(string vhost)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
// if the vhost connected is removed, disconnect the client. if (req->vhost != vhost) {
if (req->vhost == vhost->arg0()) { return ret;
srs_close_stfd(stfd);
} }
// if the vhost connected is removed, disconnect the client.
srs_trace("vhost %s removed/disabled, close client url=%s",
vhost.c_str(), req->get_stream_url().c_str());
srs_close_stfd(stfd);
return ret; return ret;
} }
@ -174,7 +181,7 @@ int SrsClient::service_cycle()
srs_trace("set chunk_size=%d success", chunk_size); srs_trace("set chunk_size=%d success", chunk_size);
// find a source to publish. // find a source to publish.
SrsSource* source = SrsSource::find(req->get_stream_url()); SrsSource* source = SrsSource::find(req->get_stream_url(), req->vhost);
srs_assert(source != NULL); srs_assert(source != NULL);
// check publish available. // check publish available.

View file

@ -65,7 +65,7 @@ protected:
virtual int do_cycle(); virtual int do_cycle();
// interface ISrsReloadHandler // interface ISrsReloadHandler
public: public:
virtual int on_reload_vhost_removed(SrsConfDirective* vhost); virtual int on_reload_vhost_removed(std::string vhost);
private: private:
// when valid and connected to vhost/app, service the client. // when valid and connected to vhost/app, service the client.
virtual int service_cycle(); virtual int service_cycle();

View file

@ -500,39 +500,60 @@ int SrsConfig::reload()
if (old_vhost->name != "vhost") { if (old_vhost->name != "vhost") {
continue; continue;
} }
std::string vhost = old_vhost->arg0();
SrsConfDirective* new_vhost = root->get("vhost", old_vhost->arg0()); SrsConfDirective* new_vhost = root->get("vhost", vhost);
// ignore if absolutely equal // ignore if absolutely equal
if (new_vhost && srs_directive_equals(old_vhost, new_vhost)) { if (new_vhost && srs_directive_equals(old_vhost, new_vhost)) {
srs_trace("vhost %s absolutely equal, ignore.", vhost.c_str());
continue; continue;
} }
// ignore if enable the new vhost when old vhost is disabled. // ignore if enable the new vhost when old vhost is disabled.
if (get_vhost_enabled(new_vhost) && !get_vhost_enabled(old_vhost)) { if (get_vhost_enabled(new_vhost) && !get_vhost_enabled(old_vhost)) {
srs_trace("vhost %s disabled=>enabled, ignore.", vhost.c_str());
continue; continue;
} }
// ignore if both old and new vhost are disabled. // ignore if both old and new vhost are disabled.
if (!get_vhost_enabled(new_vhost) && !get_vhost_enabled(old_vhost)) { if (!get_vhost_enabled(new_vhost) && !get_vhost_enabled(old_vhost)) {
srs_trace("vhost %s disabled=>disabled, ignore.", vhost.c_str());
continue; continue;
} }
// merge config: vhost removed/disabled. // merge config: vhost removed/disabled.
if (!get_vhost_enabled(new_vhost) && get_vhost_enabled(old_vhost)) { if (!get_vhost_enabled(new_vhost) && get_vhost_enabled(old_vhost)) {
srs_trace("vhost %s disabled, reload it.", old_vhost->name.c_str()); srs_trace("vhost %s disabled, reload it.", vhost.c_str());
for (it = subscribes.begin(); it != subscribes.end(); ++it) { for (it = subscribes.begin(); it != subscribes.end(); ++it) {
ISrsReloadHandler* subscribe = *it; ISrsReloadHandler* subscribe = *it;
if ((ret = subscribe->on_reload_vhost_removed(old_vhost)) != ERROR_SUCCESS) { if ((ret = subscribe->on_reload_vhost_removed(vhost)) != ERROR_SUCCESS) {
srs_error("notify subscribes pithy_print remove vhost failed. ret=%d", ret); srs_error("notify subscribes pithy_print remove "
"vhost %s failed. ret=%d", vhost.c_str(), ret);
return ret; return ret;
} }
} }
srs_trace("reload remove vhost success."); srs_trace("reload remove vhost %s success.", vhost.c_str());
} }
// merge config: vhost modified. // merge config: vhost modified.
srs_trace("vhost %s modified, reload its detail.", vhost.c_str());
if (get_vhost_enabled(new_vhost) && get_vhost_enabled(old_vhost)) {
if (!srs_directive_equals(new_vhost->get("gop_cache"), old_vhost->get("gop_cache"))) {
for (it = subscribes.begin(); it != subscribes.end(); ++it) {
ISrsReloadHandler* subscribe = *it;
if ((ret = subscribe->on_reload_gop_cache(vhost)) != ERROR_SUCCESS) {
srs_error("vhost %s notify subscribes gop_cache failed. ret=%d", vhost.c_str(), ret);
return ret;
}
}
srs_trace("vhost %s reload gop_cache success.", vhost.c_str());
}
// TODO: suppor reload hls/forward/ffmpeg/http
continue;
}
srs_warn("invalid reload path, enabled old: %d, new: %d",
get_vhost_enabled(old_vhost), get_vhost_enabled(new_vhost));
} }
// TODO: suppor reload hls/forward/ffmpeg/http
return ret; return ret;
} }
@ -1440,6 +1461,11 @@ int SrsConfig::get_pithy_print_play()
bool srs_directive_equals(SrsConfDirective* a, SrsConfDirective* b) bool srs_directive_equals(SrsConfDirective* a, SrsConfDirective* b)
{ {
// both NULL, equal.
if (!a && !b) {
return true;
}
if (!a || !b) { if (!a || !b) {
return false; return false;
} }

View file

@ -23,6 +23,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_core_reload.hpp> #include <srs_core_reload.hpp>
using namespace std;
#include <srs_core_error.hpp> #include <srs_core_error.hpp>
ISrsReloadHandler::ISrsReloadHandler() ISrsReloadHandler::ISrsReloadHandler()
@ -43,7 +45,12 @@ int ISrsReloadHandler::on_reload_pithy_print()
return ERROR_SUCCESS; return ERROR_SUCCESS;
} }
int ISrsReloadHandler::on_reload_vhost_removed(SrsConfDirective* /*vhost*/) int ISrsReloadHandler::on_reload_vhost_removed(string /*vhost*/)
{
return ERROR_SUCCESS;
}
int ISrsReloadHandler::on_reload_gop_cache(string /*vhost*/)
{ {
return ERROR_SUCCESS; return ERROR_SUCCESS;
} }

View file

@ -29,7 +29,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <srs_core.hpp> #include <srs_core.hpp>
class SrsConfDirective; #include <string>
/** /**
* the handler for config reload. * the handler for config reload.
@ -42,7 +42,8 @@ public:
public: public:
virtual int on_reload_listen(); virtual int on_reload_listen();
virtual int on_reload_pithy_print(); virtual int on_reload_pithy_print();
virtual int on_reload_vhost_removed(SrsConfDirective* vhost); virtual int on_reload_vhost_removed(std::string vhost);
virtual int on_reload_gop_cache(std::string vhost);
}; };
#endif #endif

View file

@ -24,6 +24,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_core_source.hpp> #include <srs_core_source.hpp>
#include <algorithm> #include <algorithm>
using namespace std;
#include <srs_core_log.hpp> #include <srs_core_log.hpp>
#include <srs_core_protocol.hpp> #include <srs_core_protocol.hpp>
@ -344,19 +345,21 @@ int SrsGopCache::dump(SrsConsumer* consumer, int tba, int tbv)
std::map<std::string, SrsSource*> SrsSource::pool; std::map<std::string, SrsSource*> SrsSource::pool;
SrsSource* SrsSource::find(std::string stream_url) SrsSource* SrsSource::find(string stream_url, string vhost)
{ {
if (pool.find(stream_url) == pool.end()) { if (pool.find(stream_url) == pool.end()) {
pool[stream_url] = new SrsSource(stream_url); pool[stream_url] = new SrsSource(stream_url, vhost);
srs_verbose("create new source for url=%s", stream_url.c_str()); srs_verbose("create new source for "
"url=%s, vhost=%s", stream_url.c_str(), vhost.c_str());
} }
return pool[stream_url]; return pool[stream_url];
} }
SrsSource::SrsSource(std::string _stream_url) SrsSource::SrsSource(string _stream_url, string _vhost)
{ {
stream_url = _stream_url; stream_url = _stream_url;
vhost = _vhost;
#ifdef SRS_HLS #ifdef SRS_HLS
hls = new SrsHls(); hls = new SrsHls();
@ -407,6 +410,25 @@ SrsSource::~SrsSource()
#endif #endif
} }
int SrsSource::on_reload_gop_cache(string _vhost)
{
int ret = ERROR_SUCCESS;
if (vhost != _vhost) {
return ret;
}
// gop cache changed.
bool enabled_cache = config->get_gop_cache(vhost);
srs_trace("vhost %s gop_cache changed to %d, source url=%s",
vhost.c_str(), enabled_cache, stream_url.c_str());
set_cache(enabled_cache);
return ret;
}
bool SrsSource::can_publish() bool SrsSource::can_publish()
{ {
return _can_publish; return _can_publish;

View file

@ -34,6 +34,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <vector> #include <vector>
#include <string> #include <string>
#include <srs_core_reload.hpp>
class SrsSource; class SrsSource;
class SrsCommonMessage; class SrsCommonMessage;
class SrsOnMetaDataPacket; class SrsOnMetaDataPacket;
@ -158,19 +160,21 @@ public:
/** /**
* live streaming source. * live streaming source.
*/ */
class SrsSource class SrsSource : public ISrsReloadHandler
{ {
private: private:
static std::map<std::string, SrsSource*> pool; static std::map<std::string, SrsSource*> pool;
public: public:
/** /**
* find stream by vhost/app/stream. * find stream by vhost/app/stream.
* @stream_url the stream url, for example, myserver.xxx.com/app/stream * @param stream_url the stream url, for example, myserver.xxx.com/app/stream
* @param vhost the vhost to constructor the object.
* @return the matched source, never be NULL. * @return the matched source, never be NULL.
* @remark stream_url should without port and schema. * @remark stream_url should without port and schema.
*/ */
static SrsSource* find(std::string stream_url); static SrsSource* find(std::string stream_url, std::string vhost);
private: private:
std::string vhost;
std::string stream_url; std::string stream_url;
// to delivery stream to clients. // to delivery stream to clients.
std::vector<SrsConsumer*> consumers; std::vector<SrsConsumer*> consumers;
@ -206,8 +210,11 @@ private:
// the cached audio sequence header. // the cached audio sequence header.
SrsSharedPtrMessage* cache_sh_audio; SrsSharedPtrMessage* cache_sh_audio;
public: public:
SrsSource(std::string _stream_url); SrsSource(std::string _stream_url, std::string _vhost);
virtual ~SrsSource(); virtual ~SrsSource();
// interface ISrsReloadHandler
public:
virtual int on_reload_gop_cache(std::string _vhost);
public: public:
virtual bool can_publish(); virtual bool can_publish();
virtual int on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata); virtual int on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata);