From 8d534d34709cf343535b5a0d17fa1aaab425d25c Mon Sep 17 00:00:00 2001 From: "qiang.li" Date: Sat, 3 Jan 2015 12:57:13 +0800 Subject: [PATCH 1/9] get stream info use http api #227 --- trunk/src/app/srs_app_edge.hpp | 8 +++ trunk/src/app/srs_app_http_api.cpp | 90 +++++++++++++++++++++++++++++- trunk/src/app/srs_app_http_api.hpp | 22 ++++++++ trunk/src/app/srs_app_source.cpp | 11 +++- trunk/src/app/srs_app_source.hpp | 6 ++ 5 files changed, 135 insertions(+), 2 deletions(-) diff --git a/trunk/src/app/srs_app_edge.hpp b/trunk/src/app/srs_app_edge.hpp index 6a7329cfb..285806a95 100644 --- a/trunk/src/app/srs_app_edge.hpp +++ b/trunk/src/app/srs_app_edge.hpp @@ -188,6 +188,10 @@ public: * when ingester start to play stream. */ virtual int on_ingest_play(); + /** + * get state info. + */ + virtual int get_state() { return state; } }; /** @@ -219,6 +223,10 @@ public: * proxy unpublish stream to edge. */ virtual void on_proxy_unpublish(); + /** + * get state info. + */ + virtual int get_state() { return state; } }; #endif diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index 3958fd838..54e824843 100644 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -35,6 +35,8 @@ using namespace std; #include #include #include +#include +#include SrsApiRoot::SrsApiRoot() { @@ -122,6 +124,8 @@ SrsApiV1::SrsApiV1() handlers.push_back(new SrsApiMemInfos()); handlers.push_back(new SrsApiAuthors()); handlers.push_back(new SrsApiRequests()); + handlers.push_back(new SrsApiVhosts()); + handlers.push_back(new SrsApiStreams()); } SrsApiV1::~SrsApiV1() @@ -147,7 +151,9 @@ int SrsApiV1::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) << __SRS_JFIELD_STR("system_proc_stats", "the system process stats") << __SRS_JFIELD_CONT << __SRS_JFIELD_STR("meminfos", "the meminfo of system") << __SRS_JFIELD_CONT << __SRS_JFIELD_STR("authors", "the primary authors and contributors") << __SRS_JFIELD_CONT - << __SRS_JFIELD_STR("requests", "the request itself, for http debug") + << __SRS_JFIELD_STR("requests", "the request itself, for http debug") << __SRS_JFIELD_CONT + << __SRS_JFIELD_STR("vhosts", "list all vhosts") << __SRS_JFIELD_CONT + << __SRS_JFIELD_STR("streams?(name/vhost)=xxx", "list streams that match the name or vhost") << __SRS_JOBJECT_END << __SRS_JOBJECT_END; @@ -500,6 +506,88 @@ int SrsApiAuthors::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) return res_json(skt, req, ss.str()); } +SrsApiVhosts::SrsApiVhosts() +{ +} + +SrsApiVhosts::~SrsApiVhosts() +{ +} + +bool SrsApiVhosts::can_handle(const char* path, int length, const char** /*pchild*/) +{ + return srs_path_equals("/vhosts", path, length); +} + +int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) +{ + std::stringstream ss; + + ss << __SRS_JARRAY_START; + bool first = true; + std::map *source_pool = SrsSource::get_source_pool(); + std::map::iterator it; + for (it=source_pool->begin(); it!=source_pool->end(); it++) { + SrsRequest* source_req = it->second->get_reqinfo(); + if (first) first = false; + else ss << __SRS_JFIELD_CONT; + + ss << "\"" << source_req->vhost << "\""; + } + ss << __SRS_JARRAY_END; + + return res_json(skt, req, ss.str()); +} + +SrsApiStreams::SrsApiStreams() +{ +} + +SrsApiStreams::~SrsApiStreams() +{ +} + +bool SrsApiStreams::can_handle(const char* path, int length, const char** /*pchild*/) +{ + return srs_path_equals("/streams", path, length); +} + +int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) +{ + std::stringstream ss; + + std::string query_name = req->query_get("name"); + std::string query_vhost = req->query_get("vhost"); + if (query_name.size()>0 || query_vhost.size()>0) { + ss << __SRS_JARRAY_START; + bool first = true; + std::map *source_pool = SrsSource::get_source_pool(); + std::map::iterator it; + for (it=source_pool->begin(); it!=source_pool->end(); it++) { + SrsSource* source = it->second; + SrsRequest* source_req = source->get_reqinfo(); + if (source_req->stream==query_name || source_req->vhost==query_vhost) { + if (first) first = false; + else ss << __SRS_JFIELD_CONT; + + ss << __SRS_JOBJECT_START + << __SRS_JFIELD_STR("name", source_req->stream) << __SRS_JFIELD_CONT + << __SRS_JFIELD_STR("url", source_req->tcUrl) << __SRS_JFIELD_CONT + << __SRS_JFIELD_ORG("clients", source->get_consumers_size()) << __SRS_JFIELD_CONT + << __SRS_JFIELD_STR("status", (source->can_publish()?"idle":"streaming")) << __SRS_JFIELD_CONT + << __SRS_JFIELD_STR("type", source->get_source_type()) << __SRS_JFIELD_CONT + << __SRS_JFIELD_STR("codec", "") + << __SRS_JOBJECT_END; + } + } + ss << __SRS_JARRAY_END; + } else { + return res_error(skt, req, 400, "Bad Request", "unknown query"); + } + + return res_json(skt, req, ss.str()); +} + SrsHttpApi::SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHandler* _handler) : SrsConnection(srs_server, client_stfd) { diff --git a/trunk/src/app/srs_app_http_api.hpp b/trunk/src/app/srs_app_http_api.hpp index ea12ffcf1..a29e46066 100644 --- a/trunk/src/app/srs_app_http_api.hpp +++ b/trunk/src/app/srs_app_http_api.hpp @@ -164,6 +164,28 @@ protected: virtual int do_process_request(SrsStSocket* skt, SrsHttpMessage* req); }; +class SrsApiVhosts : public SrsHttpHandler +{ +public: + SrsApiVhosts(); + virtual ~SrsApiVhosts(); +public: + virtual bool can_handle(const char* path, int length, const char** pchild); +protected: + virtual int do_process_request(SrsStSocket* skt, SrsHttpMessage* req); +}; + +class SrsApiStreams : public SrsHttpHandler +{ +public: + SrsApiStreams(); + virtual ~SrsApiStreams(); +public: + virtual bool can_handle(const char* path, int length, const char** pchild); +protected: + virtual int do_process_request(SrsStSocket* skt, SrsHttpMessage* req); +}; + class SrsHttpApi : public SrsConnection { private: diff --git a/trunk/src/app/srs_app_source.cpp b/trunk/src/app/srs_app_source.cpp index 6003c8883..e788fb465 100644 --- a/trunk/src/app/srs_app_source.cpp +++ b/trunk/src/app/srs_app_source.cpp @@ -1807,4 +1807,13 @@ void SrsSource::destroy_forwarders() forwarders.clear(); } - +std::string SrsSource::get_source_type() +{ + if (play_edge->get_state() == SrsEdgeStateIngestConnected) { + return "origin pull"; + } else if (publish_edge->get_state() == SrsEdgeStatePublish) { + return "edge publish"; + } else { + return "normal publish"; + } +} diff --git a/trunk/src/app/srs_app_source.hpp b/trunk/src/app/srs_app_source.hpp index 145adb8eb..7a6ce1f2a 100644 --- a/trunk/src/app/srs_app_source.hpp +++ b/trunk/src/app/srs_app_source.hpp @@ -486,6 +486,12 @@ public: private: virtual int create_forwarders(); virtual void destroy_forwarders(); +//get information +public: + static std::map* get_source_pool() { return &pool; } + virtual SrsRequest* get_reqinfo() { return _req; } + virtual std::size_t get_consumers_size() { return consumers.size(); } + virtual std::string get_source_type(); }; #endif From a4a93613d0cda57f51ea4f4cb8a60f2965d9c100 Mon Sep 17 00:00:00 2001 From: "qiang.li" Date: Sun, 4 Jan 2015 22:47:12 +0800 Subject: [PATCH 2/9] add statistic for stream --- trunk/configure | 2 +- trunk/src/app/srs_app_edge.hpp | 8 --- trunk/src/app/srs_app_http_api.cpp | 62 +++++++++++++++--------- trunk/src/app/srs_app_rtmp_conn.cpp | 3 ++ trunk/src/app/srs_app_source.cpp | 10 ---- trunk/src/app/srs_app_source.hpp | 6 --- trunk/src/app/srs_app_statistic.cpp | 75 +++++++++++++++++++++++++++++ trunk/src/app/srs_app_statistic.hpp | 70 +++++++++++++++++++++++++++ 8 files changed, 188 insertions(+), 48 deletions(-) create mode 100644 trunk/src/app/srs_app_statistic.cpp create mode 100644 trunk/src/app/srs_app_statistic.hpp diff --git a/trunk/configure b/trunk/configure index dd6d5f4e3..c543d150c 100755 --- a/trunk/configure +++ b/trunk/configure @@ -389,7 +389,7 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then "srs_app_pithy_print" "srs_app_reload" "srs_app_http_api" "srs_app_http_conn" "srs_app_http_hooks" "srs_app_json" "srs_app_ingest" "srs_app_ffmpeg" "srs_app_utility" "srs_app_dvr" "srs_app_edge" "srs_app_kbps" "srs_app_heartbeat" "srs_app_empty" "srs_app_http_client" "srs_app_avc_aac" - "srs_app_recv_thread") + "srs_app_recv_thread" "srs_app_statistic") APP_INCS="src/app"; MODULE_DIR=${APP_INCS} . auto/modules.sh APP_OBJS="${MODULE_OBJS[@]}" fi diff --git a/trunk/src/app/srs_app_edge.hpp b/trunk/src/app/srs_app_edge.hpp index 285806a95..6a7329cfb 100644 --- a/trunk/src/app/srs_app_edge.hpp +++ b/trunk/src/app/srs_app_edge.hpp @@ -188,10 +188,6 @@ public: * when ingester start to play stream. */ virtual int on_ingest_play(); - /** - * get state info. - */ - virtual int get_state() { return state; } }; /** @@ -223,10 +219,6 @@ public: * proxy unpublish stream to edge. */ virtual void on_proxy_unpublish(); - /** - * get state info. - */ - virtual int get_state() { return state; } }; #endif diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index 54e824843..a0c43492e 100644 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -26,6 +26,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #ifdef SRS_AUTO_HTTP_API #include +#include using namespace std; #include @@ -35,7 +36,7 @@ using namespace std; #include #include #include -#include +#include #include SrsApiRoot::SrsApiRoot() @@ -153,7 +154,7 @@ int SrsApiV1::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) << __SRS_JFIELD_STR("authors", "the primary authors and contributors") << __SRS_JFIELD_CONT << __SRS_JFIELD_STR("requests", "the request itself, for http debug") << __SRS_JFIELD_CONT << __SRS_JFIELD_STR("vhosts", "list all vhosts") << __SRS_JFIELD_CONT - << __SRS_JFIELD_STR("streams?(name/vhost)=xxx", "list streams that match the name or vhost") + << __SRS_JFIELD_STR("streams", "list streams that match the name or vhost") << __SRS_JOBJECT_END << __SRS_JOBJECT_END; @@ -523,16 +524,26 @@ int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) { std::stringstream ss; + std::set vhost_set; + SrsStreamInfoMap* pool = SrsStatistic::instance()->get_pool(); + SrsStreamInfoMap::iterator it; + for (it = pool->begin(); it != pool->end(); it++) { + if (it->second->_req == NULL) + continue; + vhost_set.insert(it->second->_req->vhost); + } + ss << __SRS_JARRAY_START; bool first = true; - std::map *source_pool = SrsSource::get_source_pool(); - std::map::iterator it; - for (it=source_pool->begin(); it!=source_pool->end(); it++) { - SrsRequest* source_req = it->second->get_reqinfo(); - if (first) first = false; - else ss << __SRS_JFIELD_CONT; + std::set::iterator it_set; + for (it_set = vhost_set.begin(); it_set != vhost_set.end(); it_set++) { + if (first) { + first = false; + } else { + ss << __SRS_JFIELD_CONT; + } - ss << "\"" << source_req->vhost << "\""; + ss << "\"" << (*it_set) << "\""; } ss << __SRS_JARRAY_END; @@ -558,24 +569,29 @@ int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) std::string query_name = req->query_get("name"); std::string query_vhost = req->query_get("vhost"); - if (query_name.size()>0 || query_vhost.size()>0) { + if (query_name.size() > 0 || query_vhost.size() > 0) { ss << __SRS_JARRAY_START; bool first = true; - std::map *source_pool = SrsSource::get_source_pool(); - std::map::iterator it; - for (it=source_pool->begin(); it!=source_pool->end(); it++) { - SrsSource* source = it->second; - SrsRequest* source_req = source->get_reqinfo(); - if (source_req->stream==query_name || source_req->vhost==query_vhost) { - if (first) first = false; - else ss << __SRS_JFIELD_CONT; + SrsStreamInfoMap* pool = SrsStatistic::instance()->get_pool(); + SrsStreamInfoMap::iterator it; + for (it = pool->begin(); it != pool->end(); it++) { + SrsRequest* reqinfo = it->second->_req; + if (reqinfo == NULL) + continue; + + if (reqinfo->stream == query_name || reqinfo->vhost == query_vhost) { + if (first) { + first = false; + } else { + ss << __SRS_JFIELD_CONT; + } ss << __SRS_JOBJECT_START - << __SRS_JFIELD_STR("name", source_req->stream) << __SRS_JFIELD_CONT - << __SRS_JFIELD_STR("url", source_req->tcUrl) << __SRS_JFIELD_CONT - << __SRS_JFIELD_ORG("clients", source->get_consumers_size()) << __SRS_JFIELD_CONT - << __SRS_JFIELD_STR("status", (source->can_publish()?"idle":"streaming")) << __SRS_JFIELD_CONT - << __SRS_JFIELD_STR("type", source->get_source_type()) << __SRS_JFIELD_CONT + << __SRS_JFIELD_STR("name", reqinfo->stream) << __SRS_JFIELD_CONT + << __SRS_JFIELD_STR("url", reqinfo->tcUrl) << __SRS_JFIELD_CONT + << __SRS_JFIELD_ORG("clients", 0) << __SRS_JFIELD_CONT + << __SRS_JFIELD_STR("status", "idle") << __SRS_JFIELD_CONT + << __SRS_JFIELD_STR("type", "") << __SRS_JFIELD_CONT << __SRS_JFIELD_STR("codec", "") << __SRS_JOBJECT_END; } diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index 87eef5c4a..43feda5a7 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -51,6 +51,7 @@ using namespace std; #include #include #include +#include // when stream is busy, for example, streaming is already // publishing, when a new client to request to publish, @@ -382,6 +383,8 @@ int SrsRtmpConn::stream_service_cycle() } srs_assert(source != NULL); + SrsStatistic::instance()->add_request_info(source, req); + // check ASAP, to fail it faster if invalid. if (type != SrsRtmpConnPlay && !vhost_is_edge) { // check publish available diff --git a/trunk/src/app/srs_app_source.cpp b/trunk/src/app/srs_app_source.cpp index e788fb465..a7cbea204 100644 --- a/trunk/src/app/srs_app_source.cpp +++ b/trunk/src/app/srs_app_source.cpp @@ -1807,13 +1807,3 @@ void SrsSource::destroy_forwarders() forwarders.clear(); } -std::string SrsSource::get_source_type() -{ - if (play_edge->get_state() == SrsEdgeStateIngestConnected) { - return "origin pull"; - } else if (publish_edge->get_state() == SrsEdgeStatePublish) { - return "edge publish"; - } else { - return "normal publish"; - } -} diff --git a/trunk/src/app/srs_app_source.hpp b/trunk/src/app/srs_app_source.hpp index 7a6ce1f2a..145adb8eb 100644 --- a/trunk/src/app/srs_app_source.hpp +++ b/trunk/src/app/srs_app_source.hpp @@ -486,12 +486,6 @@ public: private: virtual int create_forwarders(); virtual void destroy_forwarders(); -//get information -public: - static std::map* get_source_pool() { return &pool; } - virtual SrsRequest* get_reqinfo() { return _req; } - virtual std::size_t get_consumers_size() { return consumers.size(); } - virtual std::string get_source_type(); }; #endif diff --git a/trunk/src/app/srs_app_statistic.cpp b/trunk/src/app/srs_app_statistic.cpp new file mode 100644 index 000000000..d93dfe737 --- /dev/null +++ b/trunk/src/app/srs_app_statistic.cpp @@ -0,0 +1,75 @@ +/* +The MIT License (MIT) + +Copyright (c) 2013-2015 winlin + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include + +#include + +SrsStreamInfo::SrsStreamInfo() +{ + _req = NULL; +} + +SrsStreamInfo::~SrsStreamInfo() +{ + if (_req != NULL) + delete _req; +} + +SrsStatistic *SrsStatistic::_instance = NULL; + +SrsStatistic::SrsStatistic() +{ + +} + +SrsStatistic::~SrsStatistic() +{ + SrsStreamInfoMap::iterator it; + for (it = pool.begin(); it != pool.end(); it++) { + delete it->second; + } +} + +SrsStreamInfoMap* SrsStatistic::get_pool() +{ + return &pool; +} + +SrsStreamInfo* SrsStatistic::get(void *p) +{ + SrsStreamInfoMap::iterator it = pool.find(p); + if (it == pool.end()) { + pool[p] = new SrsStreamInfo(); + return pool[p]; + } else { + return it->second; + } +} + +void SrsStatistic::add_request_info(void *p, SrsRequest *req) +{ + SrsStreamInfo *info = get(p); + if (info->_req == NULL) + info->_req = req->copy(); +} \ No newline at end of file diff --git a/trunk/src/app/srs_app_statistic.hpp b/trunk/src/app/srs_app_statistic.hpp new file mode 100644 index 000000000..42e0d81d2 --- /dev/null +++ b/trunk/src/app/srs_app_statistic.hpp @@ -0,0 +1,70 @@ +/* +The MIT License (MIT) + +Copyright (c) 2013-2015 winlin + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#ifndef SRS_APP_STATISTIC_HPP +#define SRS_APP_STATISTIC_HPP + +/* +#include +*/ + +#include + +#include + +class SrsRequest; + +class SrsStreamInfo +{ +public: + SrsStreamInfo(); + virtual ~SrsStreamInfo(); + + SrsRequest *_req; +}; +typedef std::map SrsStreamInfoMap; + +class SrsStatistic +{ +public: + static SrsStatistic *instance() + { + if (_instance == NULL) { + _instance = new SrsStatistic(); + } + return _instance; + } + + virtual SrsStreamInfoMap* get_pool(); + + virtual void add_request_info(void *p, SrsRequest *req); + +private: + SrsStatistic(); + virtual ~SrsStatistic(); + static SrsStatistic *_instance; + SrsStreamInfoMap pool; + virtual SrsStreamInfo *get(void *p); +}; + +#endif \ No newline at end of file From 344ff24f66e87cb134e29edd67a5c44d0da90ae8 Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 5 Jan 2015 12:12:21 +0800 Subject: [PATCH 3/9] add srs_app_statistic to upp project. --- trunk/src/app/srs_app_statistic.hpp | 1 + trunk/src/srs/srs.upp | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/trunk/src/app/srs_app_statistic.hpp b/trunk/src/app/srs_app_statistic.hpp index 42e0d81d2..82b5d7aab 100644 --- a/trunk/src/app/srs_app_statistic.hpp +++ b/trunk/src/app/srs_app_statistic.hpp @@ -42,6 +42,7 @@ public: SrsRequest *_req; }; + typedef std::map SrsStreamInfoMap; class SrsStatistic diff --git a/trunk/src/srs/srs.upp b/trunk/src/srs/srs.upp index b6712314a..661b19f5c 100755 --- a/trunk/src/srs/srs.upp +++ b/trunk/src/srs/srs.upp @@ -37,8 +37,8 @@ file rtmp-protocol readonly separator, ..\rtmp\srs_protocol_amf0.hpp, ..\rtmp\srs_protocol_amf0.cpp, - ..\rtmp\srs_protocol_buffer.hpp, - ..\rtmp\srs_protocol_buffer.cpp, + ..\rtmp\srs_protocol_buffer.hpp, + ..\rtmp\srs_protocol_buffer.cpp, ..\rtmp\srs_protocol_handshake.hpp, ..\rtmp\srs_protocol_handshake.cpp, ..\rtmp\srs_protocol_io.hpp, @@ -112,6 +112,8 @@ file ..\app\srs_app_st.cpp, ..\app\srs_app_st_socket.hpp, ..\app\srs_app_st_socket.cpp, + ..\app\srs_app_statistic.hpp, + ..\app\srs_app_statistic.cpp, ..\app\srs_app_source.hpp, ..\app\srs_app_source.cpp, ..\app\srs_app_thread.hpp, From 4bb17f0c81d819c6a6b676b855d8cdea060261dd Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 5 Jan 2015 12:14:02 +0800 Subject: [PATCH 4/9] replace the TAB with 4spaces --- trunk/src/app/srs_app_statistic.cpp | 36 ++-- trunk/src/app/srs_app_statistic.hpp | 2 +- trunk/src/srs/srs.upp | 298 ++++++++++++++-------------- 3 files changed, 168 insertions(+), 168 deletions(-) diff --git a/trunk/src/app/srs_app_statistic.cpp b/trunk/src/app/srs_app_statistic.cpp index d93dfe737..fea7a9635 100644 --- a/trunk/src/app/srs_app_statistic.cpp +++ b/trunk/src/app/srs_app_statistic.cpp @@ -27,13 +27,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. SrsStreamInfo::SrsStreamInfo() { - _req = NULL; + _req = NULL; } SrsStreamInfo::~SrsStreamInfo() { - if (_req != NULL) - delete _req; + if (_req != NULL) + delete _req; } SrsStatistic *SrsStatistic::_instance = NULL; @@ -45,31 +45,31 @@ SrsStatistic::SrsStatistic() SrsStatistic::~SrsStatistic() { - SrsStreamInfoMap::iterator it; - for (it = pool.begin(); it != pool.end(); it++) { - delete it->second; - } + SrsStreamInfoMap::iterator it; + for (it = pool.begin(); it != pool.end(); it++) { + delete it->second; + } } SrsStreamInfoMap* SrsStatistic::get_pool() { - return &pool; + return &pool; } SrsStreamInfo* SrsStatistic::get(void *p) { - SrsStreamInfoMap::iterator it = pool.find(p); - if (it == pool.end()) { - pool[p] = new SrsStreamInfo(); - return pool[p]; - } else { - return it->second; - } + SrsStreamInfoMap::iterator it = pool.find(p); + if (it == pool.end()) { + pool[p] = new SrsStreamInfo(); + return pool[p]; + } else { + return it->second; + } } void SrsStatistic::add_request_info(void *p, SrsRequest *req) { - SrsStreamInfo *info = get(p); - if (info->_req == NULL) - info->_req = req->copy(); + SrsStreamInfo *info = get(p); + if (info->_req == NULL) + info->_req = req->copy(); } \ No newline at end of file diff --git a/trunk/src/app/srs_app_statistic.hpp b/trunk/src/app/srs_app_statistic.hpp index 82b5d7aab..132b78ff4 100644 --- a/trunk/src/app/srs_app_statistic.hpp +++ b/trunk/src/app/srs_app_statistic.hpp @@ -68,4 +68,4 @@ private: virtual SrsStreamInfo *get(void *p); }; -#endif \ No newline at end of file +#endif diff --git a/trunk/src/srs/srs.upp b/trunk/src/srs/srs.upp index 661b19f5c..b267c032a 100755 --- a/trunk/src/srs/srs.upp +++ b/trunk/src/srs/srs.upp @@ -1,155 +1,155 @@ file - main readonly separator, - ..\main\srs_main_server.cpp, - auto readonly separator, - ..\..\objs\srs_auto_headers.hpp, - libs readonly separator, - ..\libs\srs_librtmp.hpp, - ..\libs\srs_librtmp.cpp, - ..\libs\srs_lib_bandwidth.hpp, - ..\libs\srs_lib_bandwidth.cpp, - ..\libs\srs_lib_simple_socket.hpp, - ..\libs\srs_lib_simple_socket.cpp, - core readonly separator, - ..\core\srs_core.hpp, - ..\core\srs_core.cpp, - ..\core\srs_core_autofree.hpp, - ..\core\srs_core_autofree.cpp, - ..\core\srs_core_performance.hpp, - ..\core\srs_core_performance.cpp, - kernel readonly separator, - ..\kernel\srs_kernel_codec.hpp, - ..\kernel\srs_kernel_codec.cpp, - ..\kernel\srs_kernel_consts.hpp, - ..\kernel\srs_kernel_consts.cpp, - ..\kernel\srs_kernel_error.hpp, - ..\kernel\srs_kernel_error.cpp, - ..\kernel\srs_kernel_file.hpp, - ..\kernel\srs_kernel_file.cpp, - ..\kernel\srs_kernel_flv.hpp, - ..\kernel\srs_kernel_flv.cpp, - ..\kernel\srs_kernel_log.hpp, - ..\kernel\srs_kernel_log.cpp, - ..\kernel\srs_kernel_stream.hpp, - ..\kernel\srs_kernel_stream.cpp, - ..\kernel\srs_kernel_utility.hpp, - ..\kernel\srs_kernel_utility.cpp, - rtmp-protocol readonly separator, - ..\rtmp\srs_protocol_amf0.hpp, - ..\rtmp\srs_protocol_amf0.cpp, + main readonly separator, + ..\main\srs_main_server.cpp, + auto readonly separator, + ..\..\objs\srs_auto_headers.hpp, + libs readonly separator, + ..\libs\srs_librtmp.hpp, + ..\libs\srs_librtmp.cpp, + ..\libs\srs_lib_bandwidth.hpp, + ..\libs\srs_lib_bandwidth.cpp, + ..\libs\srs_lib_simple_socket.hpp, + ..\libs\srs_lib_simple_socket.cpp, + core readonly separator, + ..\core\srs_core.hpp, + ..\core\srs_core.cpp, + ..\core\srs_core_autofree.hpp, + ..\core\srs_core_autofree.cpp, + ..\core\srs_core_performance.hpp, + ..\core\srs_core_performance.cpp, + kernel readonly separator, + ..\kernel\srs_kernel_codec.hpp, + ..\kernel\srs_kernel_codec.cpp, + ..\kernel\srs_kernel_consts.hpp, + ..\kernel\srs_kernel_consts.cpp, + ..\kernel\srs_kernel_error.hpp, + ..\kernel\srs_kernel_error.cpp, + ..\kernel\srs_kernel_file.hpp, + ..\kernel\srs_kernel_file.cpp, + ..\kernel\srs_kernel_flv.hpp, + ..\kernel\srs_kernel_flv.cpp, + ..\kernel\srs_kernel_log.hpp, + ..\kernel\srs_kernel_log.cpp, + ..\kernel\srs_kernel_stream.hpp, + ..\kernel\srs_kernel_stream.cpp, + ..\kernel\srs_kernel_utility.hpp, + ..\kernel\srs_kernel_utility.cpp, + rtmp-protocol readonly separator, + ..\rtmp\srs_protocol_amf0.hpp, + ..\rtmp\srs_protocol_amf0.cpp, ..\rtmp\srs_protocol_buffer.hpp, ..\rtmp\srs_protocol_buffer.cpp, - ..\rtmp\srs_protocol_handshake.hpp, - ..\rtmp\srs_protocol_handshake.cpp, - ..\rtmp\srs_protocol_io.hpp, - ..\rtmp\srs_protocol_io.cpp, - ..\rtmp\srs_protocol_msg_array.hpp, - ..\rtmp\srs_protocol_msg_array.cpp, - ..\rtmp\srs_protocol_rtmp.hpp, - ..\rtmp\srs_protocol_rtmp.cpp, - ..\rtmp\srs_protocol_stack.hpp, - ..\rtmp\srs_protocol_stack.cpp, - ..\rtmp\srs_protocol_utility.hpp, - ..\rtmp\srs_protocol_utility.cpp, - app readonly separator, - ..\app\srs_app_avc_aac.hpp, - ..\app\srs_app_avc_aac.cpp, - ..\app\srs_app_bandwidth.hpp, - ..\app\srs_app_bandwidth.cpp, - ..\app\srs_app_conn.hpp, - ..\app\srs_app_conn.cpp, - ..\app\srs_app_config.hpp, - ..\app\srs_app_config.cpp, - ..\app\srs_app_dvr.hpp, - ..\app\srs_app_dvr.cpp, - ..\app\srs_app_edge.hpp, - ..\app\srs_app_edge.cpp, - ..\app\srs_app_empty.hpp, - ..\app\srs_app_empty.cpp, - ..\app\srs_app_encoder.hpp, - ..\app\srs_app_encoder.cpp, - ..\app\srs_app_ffmpeg.hpp, - ..\app\srs_app_ffmpeg.cpp, - ..\app\srs_app_forward.hpp, - ..\app\srs_app_forward.cpp, - ..\app\srs_app_heartbeat.hpp, - ..\app\srs_app_heartbeat.cpp, - ..\app\srs_app_hls.hpp, - ..\app\srs_app_hls.cpp, - ..\app\srs_app_http.hpp, - ..\app\srs_app_http.cpp, - ..\app\srs_app_http_api.hpp, - ..\app\srs_app_http_api.cpp, - ..\app\srs_app_http_client.hpp, - ..\app\srs_app_http_client.cpp, - ..\app\srs_app_http_conn.hpp, - ..\app\srs_app_http_conn.cpp, - ..\app\srs_app_http_hooks.hpp, - ..\app\srs_app_http_hooks.cpp, - ..\app\srs_app_ingest.hpp, - ..\app\srs_app_ingest.cpp, - ..\app\srs_app_json.hpp, - ..\app\srs_app_json.cpp, - ..\app\srs_app_kbps.hpp, - ..\app\srs_app_kbps.cpp, - ..\app\srs_app_log.hpp, - ..\app\srs_app_log.cpp, - ..\app\srs_app_recv_thread.hpp, - ..\app\srs_app_recv_thread.cpp, - ..\app\srs_app_refer.hpp, - ..\app\srs_app_refer.cpp, - ..\app\srs_app_reload.hpp, - ..\app\srs_app_reload.cpp, - ..\app\srs_app_rtmp_conn.hpp, - ..\app\srs_app_rtmp_conn.cpp, - ..\app\srs_app_pithy_print.hpp, - ..\app\srs_app_pithy_print.cpp, - ..\app\srs_app_security.hpp, - ..\app\srs_app_security.cpp, - ..\app\srs_app_server.hpp, - ..\app\srs_app_server.cpp, - ..\app\srs_app_st.hpp, - ..\app\srs_app_st.cpp, - ..\app\srs_app_st_socket.hpp, - ..\app\srs_app_st_socket.cpp, - ..\app\srs_app_statistic.hpp, - ..\app\srs_app_statistic.cpp, - ..\app\srs_app_source.hpp, - ..\app\srs_app_source.cpp, - ..\app\srs_app_thread.hpp, - ..\app\srs_app_thread.cpp, - ..\app\srs_app_utility.hpp, - ..\app\srs_app_utility.cpp, - utest readonly separator, - ..\utest\srs_utest.hpp, - ..\utest\srs_utest.cpp, - ..\utest\srs_utest_amf0.hpp, - ..\utest\srs_utest_amf0.cpp, - ..\utest\srs_utest_config.hpp, - ..\utest\srs_utest_config.cpp, - ..\utest\srs_utest_core.hpp, - ..\utest\srs_utest_core.cpp, - ..\utest\srs_utest_kernel.hpp, - ..\utest\srs_utest_kernel.cpp, - ..\utest\srs_utest_protocol.hpp, - ..\utest\srs_utest_protocol.cpp, - ..\utest\srs_utest_reload.hpp, - ..\utest\srs_utest_reload.cpp, - research readonly separator, - ..\..\research\librtmp\srs_aac_raw_publish.c, - ..\..\research\librtmp\srs_audio_raw_publish.c, - ..\..\research\librtmp\srs_bandwidth_check.c, - ..\..\research\librtmp\srs_detect_rtmp.c, - ..\..\research\librtmp\srs_flv_injecter.c, - ..\..\research\librtmp\srs_flv_parser.c, - ..\..\research\librtmp\srs_h264_raw_publish.c, - ..\..\research\librtmp\srs_ingest_flv.c, - ..\..\research\librtmp\srs_ingest_rtmp.c, - ..\..\research\librtmp\srs_play.c, - ..\..\research\librtmp\srs_publish.c, - ..\..\research\librtmp\srs_rtmp_dump.c, - ..\..\research\hls\ts_info.cc; + ..\rtmp\srs_protocol_handshake.hpp, + ..\rtmp\srs_protocol_handshake.cpp, + ..\rtmp\srs_protocol_io.hpp, + ..\rtmp\srs_protocol_io.cpp, + ..\rtmp\srs_protocol_msg_array.hpp, + ..\rtmp\srs_protocol_msg_array.cpp, + ..\rtmp\srs_protocol_rtmp.hpp, + ..\rtmp\srs_protocol_rtmp.cpp, + ..\rtmp\srs_protocol_stack.hpp, + ..\rtmp\srs_protocol_stack.cpp, + ..\rtmp\srs_protocol_utility.hpp, + ..\rtmp\srs_protocol_utility.cpp, + app readonly separator, + ..\app\srs_app_avc_aac.hpp, + ..\app\srs_app_avc_aac.cpp, + ..\app\srs_app_bandwidth.hpp, + ..\app\srs_app_bandwidth.cpp, + ..\app\srs_app_conn.hpp, + ..\app\srs_app_conn.cpp, + ..\app\srs_app_config.hpp, + ..\app\srs_app_config.cpp, + ..\app\srs_app_dvr.hpp, + ..\app\srs_app_dvr.cpp, + ..\app\srs_app_edge.hpp, + ..\app\srs_app_edge.cpp, + ..\app\srs_app_empty.hpp, + ..\app\srs_app_empty.cpp, + ..\app\srs_app_encoder.hpp, + ..\app\srs_app_encoder.cpp, + ..\app\srs_app_ffmpeg.hpp, + ..\app\srs_app_ffmpeg.cpp, + ..\app\srs_app_forward.hpp, + ..\app\srs_app_forward.cpp, + ..\app\srs_app_heartbeat.hpp, + ..\app\srs_app_heartbeat.cpp, + ..\app\srs_app_hls.hpp, + ..\app\srs_app_hls.cpp, + ..\app\srs_app_http.hpp, + ..\app\srs_app_http.cpp, + ..\app\srs_app_http_api.hpp, + ..\app\srs_app_http_api.cpp, + ..\app\srs_app_http_client.hpp, + ..\app\srs_app_http_client.cpp, + ..\app\srs_app_http_conn.hpp, + ..\app\srs_app_http_conn.cpp, + ..\app\srs_app_http_hooks.hpp, + ..\app\srs_app_http_hooks.cpp, + ..\app\srs_app_ingest.hpp, + ..\app\srs_app_ingest.cpp, + ..\app\srs_app_json.hpp, + ..\app\srs_app_json.cpp, + ..\app\srs_app_kbps.hpp, + ..\app\srs_app_kbps.cpp, + ..\app\srs_app_log.hpp, + ..\app\srs_app_log.cpp, + ..\app\srs_app_recv_thread.hpp, + ..\app\srs_app_recv_thread.cpp, + ..\app\srs_app_refer.hpp, + ..\app\srs_app_refer.cpp, + ..\app\srs_app_reload.hpp, + ..\app\srs_app_reload.cpp, + ..\app\srs_app_rtmp_conn.hpp, + ..\app\srs_app_rtmp_conn.cpp, + ..\app\srs_app_pithy_print.hpp, + ..\app\srs_app_pithy_print.cpp, + ..\app\srs_app_security.hpp, + ..\app\srs_app_security.cpp, + ..\app\srs_app_server.hpp, + ..\app\srs_app_server.cpp, + ..\app\srs_app_st.hpp, + ..\app\srs_app_st.cpp, + ..\app\srs_app_st_socket.hpp, + ..\app\srs_app_st_socket.cpp, + ..\app\srs_app_statistic.hpp, + ..\app\srs_app_statistic.cpp, + ..\app\srs_app_source.hpp, + ..\app\srs_app_source.cpp, + ..\app\srs_app_thread.hpp, + ..\app\srs_app_thread.cpp, + ..\app\srs_app_utility.hpp, + ..\app\srs_app_utility.cpp, + utest readonly separator, + ..\utest\srs_utest.hpp, + ..\utest\srs_utest.cpp, + ..\utest\srs_utest_amf0.hpp, + ..\utest\srs_utest_amf0.cpp, + ..\utest\srs_utest_config.hpp, + ..\utest\srs_utest_config.cpp, + ..\utest\srs_utest_core.hpp, + ..\utest\srs_utest_core.cpp, + ..\utest\srs_utest_kernel.hpp, + ..\utest\srs_utest_kernel.cpp, + ..\utest\srs_utest_protocol.hpp, + ..\utest\srs_utest_protocol.cpp, + ..\utest\srs_utest_reload.hpp, + ..\utest\srs_utest_reload.cpp, + research readonly separator, + ..\..\research\librtmp\srs_aac_raw_publish.c, + ..\..\research\librtmp\srs_audio_raw_publish.c, + ..\..\research\librtmp\srs_bandwidth_check.c, + ..\..\research\librtmp\srs_detect_rtmp.c, + ..\..\research\librtmp\srs_flv_injecter.c, + ..\..\research\librtmp\srs_flv_parser.c, + ..\..\research\librtmp\srs_h264_raw_publish.c, + ..\..\research\librtmp\srs_ingest_flv.c, + ..\..\research\librtmp\srs_ingest_rtmp.c, + ..\..\research\librtmp\srs_play.c, + ..\..\research\librtmp\srs_publish.c, + ..\..\research\librtmp\srs_rtmp_dump.c, + ..\..\research\hls\ts_info.cc; mainconfig - "" = "MAIN"; + "" = "MAIN"; From 40ed2249e8e9453371c844e38e22d206913c8ae3 Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 5 Jan 2015 12:20:46 +0800 Subject: [PATCH 5/9] refine code to use the one coding style. --- trunk/src/app/srs_app_http_api.cpp | 8 +++---- trunk/src/app/srs_app_statistic.cpp | 36 ++++++++++++++++++----------- trunk/src/app/srs_app_statistic.hpp | 22 ++++++------------ 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index a0c43492e..a9e775db5 100644 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -525,8 +525,8 @@ int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) std::stringstream ss; std::set vhost_set; - SrsStreamInfoMap* pool = SrsStatistic::instance()->get_pool(); - SrsStreamInfoMap::iterator it; + std::map* pool = SrsStatistic::instance()->get_pool(); + std::map::iterator it; for (it = pool->begin(); it != pool->end(); it++) { if (it->second->_req == NULL) continue; @@ -572,8 +572,8 @@ int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) if (query_name.size() > 0 || query_vhost.size() > 0) { ss << __SRS_JARRAY_START; bool first = true; - SrsStreamInfoMap* pool = SrsStatistic::instance()->get_pool(); - SrsStreamInfoMap::iterator it; + std::map* pool = SrsStatistic::instance()->get_pool(); + std::map::iterator it; for (it = pool->begin(); it != pool->end(); it++) { SrsRequest* reqinfo = it->second->_req; if (reqinfo == NULL) diff --git a/trunk/src/app/srs_app_statistic.cpp b/trunk/src/app/srs_app_statistic.cpp index fea7a9635..331672adb 100644 --- a/trunk/src/app/srs_app_statistic.cpp +++ b/trunk/src/app/srs_app_statistic.cpp @@ -32,44 +32,54 @@ SrsStreamInfo::SrsStreamInfo() SrsStreamInfo::~SrsStreamInfo() { - if (_req != NULL) - delete _req; + srs_freep(_req); } -SrsStatistic *SrsStatistic::_instance = NULL; +SrsStatistic* SrsStatistic::_instance = NULL; SrsStatistic::SrsStatistic() { - } SrsStatistic::~SrsStatistic() { - SrsStreamInfoMap::iterator it; + std::map::iterator it; for (it = pool.begin(); it != pool.end(); it++) { - delete it->second; + SrsStreamInfo* si = it->second; + srs_freep(si); } } -SrsStreamInfoMap* SrsStatistic::get_pool() +SrsStatistic* SrsStatistic::instance() +{ + if (_instance == NULL) { + _instance = new SrsStatistic(); + } + return _instance; +} + +std::map* SrsStatistic::get_pool() { return &pool; } SrsStreamInfo* SrsStatistic::get(void *p) { - SrsStreamInfoMap::iterator it = pool.find(p); + std::map::iterator it = pool.find(p); if (it == pool.end()) { - pool[p] = new SrsStreamInfo(); - return pool[p]; + SrsStreamInfo* si = new SrsStreamInfo(); + pool[p] = si; + return si; } else { - return it->second; + SrsStreamInfo* si = it->second; + return si; } } void SrsStatistic::add_request_info(void *p, SrsRequest *req) { - SrsStreamInfo *info = get(p); - if (info->_req == NULL) + SrsStreamInfo* info = get(p); + if (info->_req == NULL) { info->_req = req->copy(); + } } \ No newline at end of file diff --git a/trunk/src/app/srs_app_statistic.hpp b/trunk/src/app/srs_app_statistic.hpp index 132b78ff4..41cbbeace 100644 --- a/trunk/src/app/srs_app_statistic.hpp +++ b/trunk/src/app/srs_app_statistic.hpp @@ -39,32 +39,24 @@ class SrsStreamInfo public: SrsStreamInfo(); virtual ~SrsStreamInfo(); - +public: SrsRequest *_req; }; -typedef std::map SrsStreamInfoMap; - class SrsStatistic { public: - static SrsStatistic *instance() - { - if (_instance == NULL) { - _instance = new SrsStatistic(); - } - return _instance; - } - - virtual SrsStreamInfoMap* get_pool(); - + static SrsStatistic* instance(); +public: + virtual std::map* get_pool(); virtual void add_request_info(void *p, SrsRequest *req); - private: SrsStatistic(); virtual ~SrsStatistic(); +private: static SrsStatistic *_instance; - SrsStreamInfoMap pool; + std::map pool; +private: virtual SrsStreamInfo *get(void *p); }; From cc796a433a7c6bfb5a00a1a28c13fac1848283a6 Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 5 Jan 2015 12:40:38 +0800 Subject: [PATCH 6/9] refine the statistic arch. --- trunk/src/app/srs_app_http_api.cpp | 64 +++++---------------------- trunk/src/app/srs_app_rtmp_conn.cpp | 7 ++- trunk/src/app/srs_app_statistic.cpp | 67 +++++++++++++---------------- trunk/src/app/srs_app_statistic.hpp | 57 ++++++++++++++++++------ 4 files changed, 90 insertions(+), 105 deletions(-) diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index a9e775db5..eb751852d 100644 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -26,7 +26,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #ifdef SRS_AUTO_HTTP_API #include -#include using namespace std; #include @@ -522,30 +521,14 @@ bool SrsApiVhosts::can_handle(const char* path, int length, const char** /*pchil int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) { + int ret = ERROR_SUCCESS; + std::stringstream ss; - std::set vhost_set; - std::map* pool = SrsStatistic::instance()->get_pool(); - std::map::iterator it; - for (it = pool->begin(); it != pool->end(); it++) { - if (it->second->_req == NULL) - continue; - vhost_set.insert(it->second->_req->vhost); + SrsStatistic* stat = SrsStatistic::instance(); + if ((ret = stat->dumps_vhosts(ss)) != ERROR_SUCCESS) { + return ret; } - - ss << __SRS_JARRAY_START; - bool first = true; - std::set::iterator it_set; - for (it_set = vhost_set.begin(); it_set != vhost_set.end(); it_set++) { - if (first) { - first = false; - } else { - ss << __SRS_JFIELD_CONT; - } - - ss << "\"" << (*it_set) << "\""; - } - ss << __SRS_JARRAY_END; return res_json(skt, req, ss.str()); } @@ -565,40 +548,13 @@ bool SrsApiStreams::can_handle(const char* path, int length, const char** /*pchi int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) { + int ret = ERROR_SUCCESS; + std::stringstream ss; - std::string query_name = req->query_get("name"); - std::string query_vhost = req->query_get("vhost"); - if (query_name.size() > 0 || query_vhost.size() > 0) { - ss << __SRS_JARRAY_START; - bool first = true; - std::map* pool = SrsStatistic::instance()->get_pool(); - std::map::iterator it; - for (it = pool->begin(); it != pool->end(); it++) { - SrsRequest* reqinfo = it->second->_req; - if (reqinfo == NULL) - continue; - - if (reqinfo->stream == query_name || reqinfo->vhost == query_vhost) { - if (first) { - first = false; - } else { - ss << __SRS_JFIELD_CONT; - } - - ss << __SRS_JOBJECT_START - << __SRS_JFIELD_STR("name", reqinfo->stream) << __SRS_JFIELD_CONT - << __SRS_JFIELD_STR("url", reqinfo->tcUrl) << __SRS_JFIELD_CONT - << __SRS_JFIELD_ORG("clients", 0) << __SRS_JFIELD_CONT - << __SRS_JFIELD_STR("status", "idle") << __SRS_JFIELD_CONT - << __SRS_JFIELD_STR("type", "") << __SRS_JFIELD_CONT - << __SRS_JFIELD_STR("codec", "") - << __SRS_JOBJECT_END; - } - } - ss << __SRS_JARRAY_END; - } else { - return res_error(skt, req, 400, "Bad Request", "unknown query"); + SrsStatistic* stat = SrsStatistic::instance(); + if ((ret = stat->dumps_streams(ss)) != ERROR_SUCCESS) { + return ret; } return res_json(skt, req, ss.str()); diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index 9cac70327..b7bb4d815 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -396,7 +396,12 @@ int SrsRtmpConn::stream_service_cycle() } srs_assert(source != NULL); - SrsStatistic::instance()->add_request_info(source, req); + // update the statistic when source disconveried. + SrsStatistic* stat = SrsStatistic::instance(); + if ((ret = stat->on_client(_srs_context->get_id(), req)) != ERROR_SUCCESS) { + srs_error("stat client failed. ret=%d", ret); + return ret; + } // check ASAP, to fail it faster if invalid. if (type != SrsRtmpConnPlay && !vhost_is_edge) { diff --git a/trunk/src/app/srs_app_statistic.cpp b/trunk/src/app/srs_app_statistic.cpp index 331672adb..7b5c60944 100644 --- a/trunk/src/app/srs_app_statistic.cpp +++ b/trunk/src/app/srs_app_statistic.cpp @@ -25,17 +25,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include -SrsStreamInfo::SrsStreamInfo() -{ - _req = NULL; -} - -SrsStreamInfo::~SrsStreamInfo() -{ - srs_freep(_req); -} - -SrsStatistic* SrsStatistic::_instance = NULL; +SrsStatistic* SrsStatistic::_instance = new SrsStatistic(); SrsStatistic::SrsStatistic() { @@ -43,43 +33,48 @@ SrsStatistic::SrsStatistic() SrsStatistic::~SrsStatistic() { - std::map::iterator it; - for (it = pool.begin(); it != pool.end(); it++) { - SrsStreamInfo* si = it->second; - srs_freep(si); + if (true) { + std::map::iterator it; + for (it = vhosts.begin(); it != vhosts.end(); it++) { + SrsStatisticVhost* vhost = it->second; + srs_freep(vhost); + } + } + if (true) { + std::map::iterator it; + for (it = streams.begin(); it != streams.end(); it++) { + SrsStatisticStream* stream = it->second; + srs_freep(stream); + } + } + if (true) { + std::map::iterator it; + for (it = clients.begin(); it != clients.end(); it++) { + SrsStatisticClient* client = it->second; + srs_freep(client); + } } } SrsStatistic* SrsStatistic::instance() { - if (_instance == NULL) { - _instance = new SrsStatistic(); - } return _instance; } -std::map* SrsStatistic::get_pool() +int SrsStatistic::on_client(int id, SrsRequest *req) { - return &pool; + int ret = ERROR_SUCCESS; + return ret; } -SrsStreamInfo* SrsStatistic::get(void *p) +int SrsStatistic::dumps_vhosts(std::stringstream& ss) { - std::map::iterator it = pool.find(p); - if (it == pool.end()) { - SrsStreamInfo* si = new SrsStreamInfo(); - pool[p] = si; - return si; - } else { - SrsStreamInfo* si = it->second; - return si; - } + int ret = ERROR_SUCCESS; + return ret; } -void SrsStatistic::add_request_info(void *p, SrsRequest *req) +int SrsStatistic::dumps_streams(std::stringstream& ss) { - SrsStreamInfo* info = get(p); - if (info->_req == NULL) { - info->_req = req->copy(); - } -} \ No newline at end of file + int ret = ERROR_SUCCESS; + return ret; +} diff --git a/trunk/src/app/srs_app_statistic.hpp b/trunk/src/app/srs_app_statistic.hpp index 41cbbeace..e7ec72ed6 100644 --- a/trunk/src/app/srs_app_statistic.hpp +++ b/trunk/src/app/srs_app_statistic.hpp @@ -31,33 +31,62 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include +#include class SrsRequest; -class SrsStreamInfo +struct SrsStatisticVhost { public: - SrsStreamInfo(); - virtual ~SrsStreamInfo(); + std::string vhost; +}; + +struct SrsStatisticStream +{ public: - SrsRequest *_req; + SrsStatisticVhost* vhost; + std::string app; + std::string stream; +}; + +struct SrsStatisticClient +{ +public: + SrsStatisticStream* stream; + int id; }; class SrsStatistic { -public: - static SrsStatistic* instance(); -public: - virtual std::map* get_pool(); - virtual void add_request_info(void *p, SrsRequest *req); +private: + static SrsStatistic *_instance; + // key: vhost name, value: vhost object. + std::map vhosts; + // key: stream name, value: stream object. + std::map streams; + // key: client id, value: stream object. + std::map clients; private: SrsStatistic(); virtual ~SrsStatistic(); -private: - static SrsStatistic *_instance; - std::map pool; -private: - virtual SrsStreamInfo *get(void *p); +public: + static SrsStatistic* instance(); +public: + /** + * when got a client to publish/play stream, + * @param id, the client srs id. + * @param req, the client request object. + */ + virtual int on_client(int id, SrsRequest *req); +public: + /** + * dumps the vhosts to sstream in json. + */ + virtual int dumps_vhosts(std::stringstream& ss); + /** + * dumps the streams to sstream in json. + */ + virtual int dumps_streams(std::stringstream& ss); }; #endif From 97bea514992b51afb4ce5fcffa0988b080a27e42 Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 5 Jan 2015 12:49:00 +0800 Subject: [PATCH 7/9] refine the api, response the error code and data. --- trunk/src/app/srs_app_http_api.cpp | 32 +++++++++++++++++++----------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index eb751852d..d0c914ef6 100644 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -152,8 +152,8 @@ int SrsApiV1::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) << __SRS_JFIELD_STR("meminfos", "the meminfo of system") << __SRS_JFIELD_CONT << __SRS_JFIELD_STR("authors", "the primary authors and contributors") << __SRS_JFIELD_CONT << __SRS_JFIELD_STR("requests", "the request itself, for http debug") << __SRS_JFIELD_CONT - << __SRS_JFIELD_STR("vhosts", "list all vhosts") << __SRS_JFIELD_CONT - << __SRS_JFIELD_STR("streams", "list streams that match the name or vhost") + << __SRS_JFIELD_STR("vhosts", "dumps vhost to json") << __SRS_JFIELD_CONT + << __SRS_JFIELD_STR("streams", "dumps streams to json") << __SRS_JOBJECT_END << __SRS_JOBJECT_END; @@ -521,14 +521,18 @@ bool SrsApiVhosts::can_handle(const char* path, int length, const char** /*pchil int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) { - int ret = ERROR_SUCCESS; + std::stringstream data; + SrsStatistic* stat = SrsStatistic::instance(); + int ret = stat->dumps_vhosts(data); std::stringstream ss; - SrsStatistic* stat = SrsStatistic::instance(); - if ((ret = stat->dumps_vhosts(ss)) != ERROR_SUCCESS) { - return ret; - } + ss << __SRS_JOBJECT_START + << __SRS_JFIELD_ERROR(ret) << __SRS_JFIELD_CONT + << __SRS_JFIELD_ORG("vhosts", __SRS_JARRAY_START) + << data.str() + << __SRS_JARRAY_END + << __SRS_JOBJECT_END; return res_json(skt, req, ss.str()); } @@ -548,14 +552,18 @@ bool SrsApiStreams::can_handle(const char* path, int length, const char** /*pchi int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) { - int ret = ERROR_SUCCESS; + std::stringstream data; + SrsStatistic* stat = SrsStatistic::instance(); + int ret = stat->dumps_streams(data); std::stringstream ss; - SrsStatistic* stat = SrsStatistic::instance(); - if ((ret = stat->dumps_streams(ss)) != ERROR_SUCCESS) { - return ret; - } + ss << __SRS_JOBJECT_START + << __SRS_JFIELD_ERROR(ret) << __SRS_JFIELD_CONT + << __SRS_JFIELD_ORG("streams", __SRS_JARRAY_START) + << data.str() + << __SRS_JARRAY_END + << __SRS_JOBJECT_END; return res_json(skt, req, ss.str()); } From ae63af6a4adb2a788aaff184b1617d8d82639eb7 Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 5 Jan 2015 12:57:48 +0800 Subject: [PATCH 8/9] refine code, implements the stat.: --- trunk/src/app/srs_app_statistic.cpp | 53 +++++++++++++++++++++++++++-- trunk/src/app/srs_app_statistic.hpp | 2 +- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/trunk/src/app/srs_app_statistic.cpp b/trunk/src/app/srs_app_statistic.cpp index 7b5c60944..5c0f1aad3 100644 --- a/trunk/src/app/srs_app_statistic.cpp +++ b/trunk/src/app/srs_app_statistic.cpp @@ -23,7 +23,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include +#include +using namespace std; + #include +#include SrsStatistic* SrsStatistic::_instance = new SrsStatistic(); @@ -61,20 +65,63 @@ SrsStatistic* SrsStatistic::instance() return _instance; } -int SrsStatistic::on_client(int id, SrsRequest *req) +int SrsStatistic::on_client(int id, SrsRequest* req) { int ret = ERROR_SUCCESS; + + // create vhost if not exists. + SrsStatisticVhost* vhost = NULL; + if (vhosts.find(req->vhost) == vhosts.end()) { + vhost = new SrsStatisticVhost(); + vhost->vhost = req->vhost; + vhosts[req->vhost] = vhost; + } else { + vhost = vhosts[req->vhost]; + } + + // the url to identify the stream. + std::string url = req->get_stream_url(); + + // create stream if not exists. + SrsStatisticStream* stream = NULL; + if (streams.find(url) == streams.end()) { + stream = new SrsStatisticStream(); + stream->vhost = vhost; + stream->stream = url; + streams[url] = stream; + } else { + stream = streams[url]; + } + return ret; } -int SrsStatistic::dumps_vhosts(std::stringstream& ss) +int SrsStatistic::dumps_vhosts(stringstream& ss) { int ret = ERROR_SUCCESS; + + std::map::iterator it; + for (it = vhosts.begin(); it != vhosts.end(); it++) { + SrsStatisticVhost* vhost = it->second; + ss << __SRS_JOBJECT_START + << __SRS_JFIELD_STR("name", vhost->vhost) + << __SRS_JOBJECT_END; + } + return ret; } -int SrsStatistic::dumps_streams(std::stringstream& ss) +int SrsStatistic::dumps_streams(stringstream& ss) { int ret = ERROR_SUCCESS; + + std::map::iterator it; + for (it = streams.begin(); it != streams.end(); it++) { + SrsStatisticStream* stream = it->second; + ss << __SRS_JOBJECT_START + << __SRS_JFIELD_STR("url", stream->stream) + << __SRS_JOBJECT_END; + } + return ret; } diff --git a/trunk/src/app/srs_app_statistic.hpp b/trunk/src/app/srs_app_statistic.hpp index e7ec72ed6..fa14e4b41 100644 --- a/trunk/src/app/srs_app_statistic.hpp +++ b/trunk/src/app/srs_app_statistic.hpp @@ -77,7 +77,7 @@ public: * @param id, the client srs id. * @param req, the client request object. */ - virtual int on_client(int id, SrsRequest *req); + virtual int on_client(int id, SrsRequest* req); public: /** * dumps the vhosts to sstream in json. From f1efdcd00005a5fe10c3cc05b6853cddbaa1e321 Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 5 Jan 2015 13:08:11 +0800 Subject: [PATCH 9/9] refine code, use global virtual id to generate the id of vhost and stream. --- trunk/src/app/srs_app_http_api.cpp | 2 ++ trunk/src/app/srs_app_statistic.cpp | 41 +++++++++++++++++++++++++++-- trunk/src/app/srs_app_statistic.hpp | 16 +++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index d0c914ef6..41c752dde 100644 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -529,6 +529,7 @@ int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) ss << __SRS_JOBJECT_START << __SRS_JFIELD_ERROR(ret) << __SRS_JFIELD_CONT + << __SRS_JFIELD_ORG("server", stat->server_id()) << __SRS_JFIELD_CONT << __SRS_JFIELD_ORG("vhosts", __SRS_JARRAY_START) << data.str() << __SRS_JARRAY_END @@ -560,6 +561,7 @@ int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) ss << __SRS_JOBJECT_START << __SRS_JFIELD_ERROR(ret) << __SRS_JFIELD_CONT + << __SRS_JFIELD_ORG("server", stat->server_id()) << __SRS_JFIELD_CONT << __SRS_JFIELD_ORG("streams", __SRS_JARRAY_START) << data.str() << __SRS_JARRAY_END diff --git a/trunk/src/app/srs_app_statistic.cpp b/trunk/src/app/srs_app_statistic.cpp index 5c0f1aad3..45e0019c7 100644 --- a/trunk/src/app/srs_app_statistic.cpp +++ b/trunk/src/app/srs_app_statistic.cpp @@ -23,16 +23,44 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include +#include #include using namespace std; #include #include +int64_t __srs_gvid = getpid(); + +int64_t __srs_generate_id() +{ + return __srs_gvid++; +} + +SrsStatisticVhost::SrsStatisticVhost() +{ + id = __srs_generate_id(); +} + +SrsStatisticVhost::~SrsStatisticVhost() +{ +} + +SrsStatisticStream::SrsStatisticStream() +{ + id = __srs_generate_id(); + vhost = NULL; +} + +SrsStatisticStream::~SrsStatisticStream() +{ +} + SrsStatistic* SrsStatistic::_instance = new SrsStatistic(); SrsStatistic::SrsStatistic() { + _server_id = __srs_generate_id(); } SrsStatistic::~SrsStatistic() @@ -87,7 +115,8 @@ int SrsStatistic::on_client(int id, SrsRequest* req) if (streams.find(url) == streams.end()) { stream = new SrsStatisticStream(); stream->vhost = vhost; - stream->stream = url; + stream->stream = req->stream; + stream->url = url; streams[url] = stream; } else { stream = streams[url]; @@ -96,6 +125,11 @@ int SrsStatistic::on_client(int id, SrsRequest* req) return ret; } +int64_t SrsStatistic::server_id() +{ + return _server_id; +} + int SrsStatistic::dumps_vhosts(stringstream& ss) { int ret = ERROR_SUCCESS; @@ -104,6 +138,7 @@ int SrsStatistic::dumps_vhosts(stringstream& ss) for (it = vhosts.begin(); it != vhosts.end(); it++) { SrsStatisticVhost* vhost = it->second; ss << __SRS_JOBJECT_START + << __SRS_JFIELD_ORG("id", vhost->id) << __SRS_JFIELD_CONT << __SRS_JFIELD_STR("name", vhost->vhost) << __SRS_JOBJECT_END; } @@ -119,7 +154,9 @@ int SrsStatistic::dumps_streams(stringstream& ss) for (it = streams.begin(); it != streams.end(); it++) { SrsStatisticStream* stream = it->second; ss << __SRS_JOBJECT_START - << __SRS_JFIELD_STR("url", stream->stream) + << __SRS_JFIELD_ORG("id", stream->id) << __SRS_JFIELD_CONT + << __SRS_JFIELD_STR("name", stream->stream) << __SRS_JFIELD_CONT + << __SRS_JFIELD_ORG("vhost", stream->vhost->id) << __SRS_JOBJECT_END; } diff --git a/trunk/src/app/srs_app_statistic.hpp b/trunk/src/app/srs_app_statistic.hpp index fa14e4b41..f261962ea 100644 --- a/trunk/src/app/srs_app_statistic.hpp +++ b/trunk/src/app/srs_app_statistic.hpp @@ -38,15 +38,24 @@ class SrsRequest; struct SrsStatisticVhost { public: + int64_t id; std::string vhost; +public: + SrsStatisticVhost(); + virtual ~SrsStatisticVhost(); }; struct SrsStatisticStream { public: + int64_t id; SrsStatisticVhost* vhost; std::string app; std::string stream; + std::string url; +public: + SrsStatisticStream(); + virtual ~SrsStatisticStream(); }; struct SrsStatisticClient @@ -60,6 +69,8 @@ class SrsStatistic { private: static SrsStatistic *_instance; + // the id to identify the sever. + int64_t _server_id; // key: vhost name, value: vhost object. std::map vhosts; // key: stream name, value: stream object. @@ -79,6 +90,11 @@ public: */ virtual int on_client(int id, SrsRequest* req); public: + /** + * get the server id, used to identify the server. + * for example, when restart, the server id must changed. + */ + virtual int64_t server_id(); /** * dumps the vhosts to sstream in json. */