qiang.li

add statistic for stream

@@ -389,7 +389,7 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then @@ -389,7 +389,7 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then
389 "srs_app_pithy_print" "srs_app_reload" "srs_app_http_api" "srs_app_http_conn" "srs_app_http_hooks" 389 "srs_app_pithy_print" "srs_app_reload" "srs_app_http_api" "srs_app_http_conn" "srs_app_http_hooks"
390 "srs_app_json" "srs_app_ingest" "srs_app_ffmpeg" "srs_app_utility" "srs_app_dvr" "srs_app_edge" 390 "srs_app_json" "srs_app_ingest" "srs_app_ffmpeg" "srs_app_utility" "srs_app_dvr" "srs_app_edge"
391 "srs_app_kbps" "srs_app_heartbeat" "srs_app_empty" "srs_app_http_client" "srs_app_avc_aac" 391 "srs_app_kbps" "srs_app_heartbeat" "srs_app_empty" "srs_app_http_client" "srs_app_avc_aac"
392 - "srs_app_recv_thread") 392 + "srs_app_recv_thread" "srs_app_statistic")
393 APP_INCS="src/app"; MODULE_DIR=${APP_INCS} . auto/modules.sh 393 APP_INCS="src/app"; MODULE_DIR=${APP_INCS} . auto/modules.sh
394 APP_OBJS="${MODULE_OBJS[@]}" 394 APP_OBJS="${MODULE_OBJS[@]}"
395 fi 395 fi
@@ -188,10 +188,6 @@ public: @@ -188,10 +188,6 @@ public:
188 * when ingester start to play stream. 188 * when ingester start to play stream.
189 */ 189 */
190 virtual int on_ingest_play(); 190 virtual int on_ingest_play();
191 - /**  
192 - * get state info.  
193 - */  
194 - virtual int get_state() { return state; }  
195 }; 191 };
196 192
197 /** 193 /**
@@ -223,10 +219,6 @@ public: @@ -223,10 +219,6 @@ public:
223 * proxy unpublish stream to edge. 219 * proxy unpublish stream to edge.
224 */ 220 */
225 virtual void on_proxy_unpublish(); 221 virtual void on_proxy_unpublish();
226 - /**  
227 - * get state info.  
228 - */  
229 - virtual int get_state() { return state; }  
230 }; 222 };
231 223
232 #endif 224 #endif
@@ -26,6 +26,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -26,6 +26,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #ifdef SRS_AUTO_HTTP_API 26 #ifdef SRS_AUTO_HTTP_API
27 27
28 #include <sstream> 28 #include <sstream>
  29 +#include <set>
29 using namespace std; 30 using namespace std;
30 31
31 #include <srs_kernel_log.hpp> 32 #include <srs_kernel_log.hpp>
@@ -35,7 +36,7 @@ using namespace std; @@ -35,7 +36,7 @@ using namespace std;
35 #include <srs_app_json.hpp> 36 #include <srs_app_json.hpp>
36 #include <srs_kernel_utility.hpp> 37 #include <srs_kernel_utility.hpp>
37 #include <srs_app_utility.hpp> 38 #include <srs_app_utility.hpp>
38 -#include <srs_app_source.hpp> 39 +#include <srs_app_statistic.hpp>
39 #include <srs_protocol_rtmp.hpp> 40 #include <srs_protocol_rtmp.hpp>
40 41
41 SrsApiRoot::SrsApiRoot() 42 SrsApiRoot::SrsApiRoot()
@@ -153,7 +154,7 @@ int SrsApiV1::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) @@ -153,7 +154,7 @@ int SrsApiV1::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
153 << __SRS_JFIELD_STR("authors", "the primary authors and contributors") << __SRS_JFIELD_CONT 154 << __SRS_JFIELD_STR("authors", "the primary authors and contributors") << __SRS_JFIELD_CONT
154 << __SRS_JFIELD_STR("requests", "the request itself, for http debug") << __SRS_JFIELD_CONT 155 << __SRS_JFIELD_STR("requests", "the request itself, for http debug") << __SRS_JFIELD_CONT
155 << __SRS_JFIELD_STR("vhosts", "list all vhosts") << __SRS_JFIELD_CONT 156 << __SRS_JFIELD_STR("vhosts", "list all vhosts") << __SRS_JFIELD_CONT
156 - << __SRS_JFIELD_STR("streams?(name/vhost)=xxx", "list streams that match the name or vhost") 157 + << __SRS_JFIELD_STR("streams", "list streams that match the name or vhost")
157 << __SRS_JOBJECT_END 158 << __SRS_JOBJECT_END
158 << __SRS_JOBJECT_END; 159 << __SRS_JOBJECT_END;
159 160
@@ -523,16 +524,26 @@ int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) @@ -523,16 +524,26 @@ int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
523 { 524 {
524 std::stringstream ss; 525 std::stringstream ss;
525 526
  527 + std::set<std::string> vhost_set;
  528 + SrsStreamInfoMap* pool = SrsStatistic::instance()->get_pool();
  529 + SrsStreamInfoMap::iterator it;
  530 + for (it = pool->begin(); it != pool->end(); it++) {
  531 + if (it->second->_req == NULL)
  532 + continue;
  533 + vhost_set.insert(it->second->_req->vhost);
  534 + }
  535 +
526 ss << __SRS_JARRAY_START; 536 ss << __SRS_JARRAY_START;
527 bool first = true; 537 bool first = true;
528 - std::map<std::string, SrsSource*> *source_pool = SrsSource::get_source_pool();  
529 - std::map<std::string, SrsSource*>::iterator it;  
530 - for (it=source_pool->begin(); it!=source_pool->end(); it++) {  
531 - SrsRequest* source_req = it->second->get_reqinfo();  
532 - if (first) first = false;  
533 - else ss << __SRS_JFIELD_CONT;  
534 -  
535 - ss << "\"" << source_req->vhost << "\""; 538 + std::set<std::string>::iterator it_set;
  539 + for (it_set = vhost_set.begin(); it_set != vhost_set.end(); it_set++) {
  540 + if (first) {
  541 + first = false;
  542 + } else {
  543 + ss << __SRS_JFIELD_CONT;
  544 + }
  545 +
  546 + ss << "\"" << (*it_set) << "\"";
536 } 547 }
537 ss << __SRS_JARRAY_END; 548 ss << __SRS_JARRAY_END;
538 549
@@ -558,24 +569,29 @@ int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) @@ -558,24 +569,29 @@ int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
558 569
559 std::string query_name = req->query_get("name"); 570 std::string query_name = req->query_get("name");
560 std::string query_vhost = req->query_get("vhost"); 571 std::string query_vhost = req->query_get("vhost");
561 - if (query_name.size()>0 || query_vhost.size()>0) { 572 + if (query_name.size() > 0 || query_vhost.size() > 0) {
562 ss << __SRS_JARRAY_START; 573 ss << __SRS_JARRAY_START;
563 bool first = true; 574 bool first = true;
564 - std::map<std::string, SrsSource*> *source_pool = SrsSource::get_source_pool();  
565 - std::map<std::string, SrsSource*>::iterator it;  
566 - for (it=source_pool->begin(); it!=source_pool->end(); it++) {  
567 - SrsSource* source = it->second;  
568 - SrsRequest* source_req = source->get_reqinfo();  
569 - if (source_req->stream==query_name || source_req->vhost==query_vhost) {  
570 - if (first) first = false;  
571 - else ss << __SRS_JFIELD_CONT; 575 + SrsStreamInfoMap* pool = SrsStatistic::instance()->get_pool();
  576 + SrsStreamInfoMap::iterator it;
  577 + for (it = pool->begin(); it != pool->end(); it++) {
  578 + SrsRequest* reqinfo = it->second->_req;
  579 + if (reqinfo == NULL)
  580 + continue;
  581 +
  582 + if (reqinfo->stream == query_name || reqinfo->vhost == query_vhost) {
  583 + if (first) {
  584 + first = false;
  585 + } else {
  586 + ss << __SRS_JFIELD_CONT;
  587 + }
572 588
573 ss << __SRS_JOBJECT_START 589 ss << __SRS_JOBJECT_START
574 - << __SRS_JFIELD_STR("name", source_req->stream) << __SRS_JFIELD_CONT  
575 - << __SRS_JFIELD_STR("url", source_req->tcUrl) << __SRS_JFIELD_CONT  
576 - << __SRS_JFIELD_ORG("clients", source->get_consumers_size()) << __SRS_JFIELD_CONT  
577 - << __SRS_JFIELD_STR("status", (source->can_publish()?"idle":"streaming")) << __SRS_JFIELD_CONT  
578 - << __SRS_JFIELD_STR("type", source->get_source_type()) << __SRS_JFIELD_CONT 590 + << __SRS_JFIELD_STR("name", reqinfo->stream) << __SRS_JFIELD_CONT
  591 + << __SRS_JFIELD_STR("url", reqinfo->tcUrl) << __SRS_JFIELD_CONT
  592 + << __SRS_JFIELD_ORG("clients", 0) << __SRS_JFIELD_CONT
  593 + << __SRS_JFIELD_STR("status", "idle") << __SRS_JFIELD_CONT
  594 + << __SRS_JFIELD_STR("type", "") << __SRS_JFIELD_CONT
579 << __SRS_JFIELD_STR("codec", "") 595 << __SRS_JFIELD_STR("codec", "")
580 << __SRS_JOBJECT_END; 596 << __SRS_JOBJECT_END;
581 } 597 }
@@ -51,6 +51,7 @@ using namespace std; @@ -51,6 +51,7 @@ using namespace std;
51 #include <srs_app_recv_thread.hpp> 51 #include <srs_app_recv_thread.hpp>
52 #include <srs_core_performance.hpp> 52 #include <srs_core_performance.hpp>
53 #include <srs_kernel_utility.hpp> 53 #include <srs_kernel_utility.hpp>
  54 +#include <srs_app_statistic.hpp>
54 55
55 // when stream is busy, for example, streaming is already 56 // when stream is busy, for example, streaming is already
56 // publishing, when a new client to request to publish, 57 // publishing, when a new client to request to publish,
@@ -382,6 +383,8 @@ int SrsRtmpConn::stream_service_cycle() @@ -382,6 +383,8 @@ int SrsRtmpConn::stream_service_cycle()
382 } 383 }
383 srs_assert(source != NULL); 384 srs_assert(source != NULL);
384 385
  386 + SrsStatistic::instance()->add_request_info(source, req);
  387 +
385 // check ASAP, to fail it faster if invalid. 388 // check ASAP, to fail it faster if invalid.
386 if (type != SrsRtmpConnPlay && !vhost_is_edge) { 389 if (type != SrsRtmpConnPlay && !vhost_is_edge) {
387 // check publish available 390 // check publish available
@@ -1807,13 +1807,3 @@ void SrsSource::destroy_forwarders() @@ -1807,13 +1807,3 @@ void SrsSource::destroy_forwarders()
1807 forwarders.clear(); 1807 forwarders.clear();
1808 } 1808 }
1809 1809
1810 -std::string SrsSource::get_source_type()  
1811 -{  
1812 - if (play_edge->get_state() == SrsEdgeStateIngestConnected) {  
1813 - return "origin pull";  
1814 - } else if (publish_edge->get_state() == SrsEdgeStatePublish) {  
1815 - return "edge publish";  
1816 - } else {  
1817 - return "normal publish";  
1818 - }  
1819 -}  
@@ -486,12 +486,6 @@ public: @@ -486,12 +486,6 @@ public:
486 private: 486 private:
487 virtual int create_forwarders(); 487 virtual int create_forwarders();
488 virtual void destroy_forwarders(); 488 virtual void destroy_forwarders();
489 -//get information  
490 -public:  
491 - static std::map<std::string, SrsSource*>* get_source_pool() { return &pool; }  
492 - virtual SrsRequest* get_reqinfo() { return _req; }  
493 - virtual std::size_t get_consumers_size() { return consumers.size(); }  
494 - virtual std::string get_source_type();  
495 }; 489 };
496 490
497 #endif 491 #endif
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013-2015 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +
  24 +#include <srs_app_statistic.hpp>
  25 +
  26 +#include <srs_protocol_rtmp.hpp>
  27 +
  28 +SrsStreamInfo::SrsStreamInfo()
  29 +{
  30 + _req = NULL;
  31 +}
  32 +
  33 +SrsStreamInfo::~SrsStreamInfo()
  34 +{
  35 + if (_req != NULL)
  36 + delete _req;
  37 +}
  38 +
  39 +SrsStatistic *SrsStatistic::_instance = NULL;
  40 +
  41 +SrsStatistic::SrsStatistic()
  42 +{
  43 +
  44 +}
  45 +
  46 +SrsStatistic::~SrsStatistic()
  47 +{
  48 + SrsStreamInfoMap::iterator it;
  49 + for (it = pool.begin(); it != pool.end(); it++) {
  50 + delete it->second;
  51 + }
  52 +}
  53 +
  54 +SrsStreamInfoMap* SrsStatistic::get_pool()
  55 +{
  56 + return &pool;
  57 +}
  58 +
  59 +SrsStreamInfo* SrsStatistic::get(void *p)
  60 +{
  61 + SrsStreamInfoMap::iterator it = pool.find(p);
  62 + if (it == pool.end()) {
  63 + pool[p] = new SrsStreamInfo();
  64 + return pool[p];
  65 + } else {
  66 + return it->second;
  67 + }
  68 +}
  69 +
  70 +void SrsStatistic::add_request_info(void *p, SrsRequest *req)
  71 +{
  72 + SrsStreamInfo *info = get(p);
  73 + if (info->_req == NULL)
  74 + info->_req = req->copy();
  75 +}
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013-2015 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +
  24 +#ifndef SRS_APP_STATISTIC_HPP
  25 +#define SRS_APP_STATISTIC_HPP
  26 +
  27 +/*
  28 +#include <srs_app_statistic.hpp>
  29 +*/
  30 +
  31 +#include <srs_core.hpp>
  32 +
  33 +#include <map>
  34 +
  35 +class SrsRequest;
  36 +
  37 +class SrsStreamInfo
  38 +{
  39 +public:
  40 + SrsStreamInfo();
  41 + virtual ~SrsStreamInfo();
  42 +
  43 + SrsRequest *_req;
  44 +};
  45 +typedef std::map<void*, SrsStreamInfo*> SrsStreamInfoMap;
  46 +
  47 +class SrsStatistic
  48 +{
  49 +public:
  50 + static SrsStatistic *instance()
  51 + {
  52 + if (_instance == NULL) {
  53 + _instance = new SrsStatistic();
  54 + }
  55 + return _instance;
  56 + }
  57 +
  58 + virtual SrsStreamInfoMap* get_pool();
  59 +
  60 + virtual void add_request_info(void *p, SrsRequest *req);
  61 +
  62 +private:
  63 + SrsStatistic();
  64 + virtual ~SrsStatistic();
  65 + static SrsStatistic *_instance;
  66 + SrsStreamInfoMap pool;
  67 + virtual SrsStreamInfo *get(void *p);
  68 +};
  69 +
  70 +#endif