winlin

refine the statistic arch.

@@ -26,7 +26,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -26,7 +26,6 @@ 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>  
30 using namespace std; 29 using namespace std;
31 30
32 #include <srs_kernel_log.hpp> 31 #include <srs_kernel_log.hpp>
@@ -522,30 +521,14 @@ bool SrsApiVhosts::can_handle(const char* path, int length, const char** /*pchil @@ -522,30 +521,14 @@ bool SrsApiVhosts::can_handle(const char* path, int length, const char** /*pchil
522 521
523 int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) 522 int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
524 { 523 {
  524 + int ret = ERROR_SUCCESS;
  525 +
525 std::stringstream ss; 526 std::stringstream ss;
526 527
527 - std::set<std::string> vhost_set;  
528 - std::map<void*, SrsStreamInfo*>* pool = SrsStatistic::instance()->get_pool();  
529 - std::map<void*, SrsStreamInfo*>::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 -  
536 - ss << __SRS_JARRAY_START;  
537 - bool first = true;  
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) << "\""; 528 + SrsStatistic* stat = SrsStatistic::instance();
  529 + if ((ret = stat->dumps_vhosts(ss)) != ERROR_SUCCESS) {
  530 + return ret;
547 } 531 }
548 - ss << __SRS_JARRAY_END;  
549 532
550 return res_json(skt, req, ss.str()); 533 return res_json(skt, req, ss.str());
551 } 534 }
@@ -565,40 +548,13 @@ bool SrsApiStreams::can_handle(const char* path, int length, const char** /*pchi @@ -565,40 +548,13 @@ bool SrsApiStreams::can_handle(const char* path, int length, const char** /*pchi
565 548
566 int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) 549 int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
567 { 550 {
  551 + int ret = ERROR_SUCCESS;
  552 +
568 std::stringstream ss; 553 std::stringstream ss;
569 554
570 - std::string query_name = req->query_get("name");  
571 - std::string query_vhost = req->query_get("vhost");  
572 - if (query_name.size() > 0 || query_vhost.size() > 0) {  
573 - ss << __SRS_JARRAY_START;  
574 - bool first = true;  
575 - std::map<void*, SrsStreamInfo*>* pool = SrsStatistic::instance()->get_pool();  
576 - std::map<void*, SrsStreamInfo*>::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 - }  
588 -  
589 - ss << __SRS_JOBJECT_START  
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  
595 - << __SRS_JFIELD_STR("codec", "")  
596 - << __SRS_JOBJECT_END;  
597 - }  
598 - }  
599 - ss << __SRS_JARRAY_END;  
600 - } else {  
601 - return res_error(skt, req, 400, "Bad Request", "unknown query"); 555 + SrsStatistic* stat = SrsStatistic::instance();
  556 + if ((ret = stat->dumps_streams(ss)) != ERROR_SUCCESS) {
  557 + return ret;
602 } 558 }
603 559
604 return res_json(skt, req, ss.str()); 560 return res_json(skt, req, ss.str());
@@ -396,7 +396,12 @@ int SrsRtmpConn::stream_service_cycle() @@ -396,7 +396,12 @@ int SrsRtmpConn::stream_service_cycle()
396 } 396 }
397 srs_assert(source != NULL); 397 srs_assert(source != NULL);
398 398
399 - SrsStatistic::instance()->add_request_info(source, req); 399 + // update the statistic when source disconveried.
  400 + SrsStatistic* stat = SrsStatistic::instance();
  401 + if ((ret = stat->on_client(_srs_context->get_id(), req)) != ERROR_SUCCESS) {
  402 + srs_error("stat client failed. ret=%d", ret);
  403 + return ret;
  404 + }
400 405
401 // check ASAP, to fail it faster if invalid. 406 // check ASAP, to fail it faster if invalid.
402 if (type != SrsRtmpConnPlay && !vhost_is_edge) { 407 if (type != SrsRtmpConnPlay && !vhost_is_edge) {
@@ -25,17 +25,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -25,17 +25,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 25
26 #include <srs_protocol_rtmp.hpp> 26 #include <srs_protocol_rtmp.hpp>
27 27
28 -SrsStreamInfo::SrsStreamInfo()  
29 -{  
30 - _req = NULL;  
31 -}  
32 -  
33 -SrsStreamInfo::~SrsStreamInfo()  
34 -{  
35 - srs_freep(_req);  
36 -}  
37 -  
38 -SrsStatistic* SrsStatistic::_instance = NULL; 28 +SrsStatistic* SrsStatistic::_instance = new SrsStatistic();
39 29
40 SrsStatistic::SrsStatistic() 30 SrsStatistic::SrsStatistic()
41 { 31 {
@@ -43,43 +33,48 @@ SrsStatistic::SrsStatistic() @@ -43,43 +33,48 @@ SrsStatistic::SrsStatistic()
43 33
44 SrsStatistic::~SrsStatistic() 34 SrsStatistic::~SrsStatistic()
45 { 35 {
46 - std::map<void*, SrsStreamInfo*>::iterator it;  
47 - for (it = pool.begin(); it != pool.end(); it++) {  
48 - SrsStreamInfo* si = it->second;  
49 - srs_freep(si); 36 + if (true) {
  37 + std::map<std::string, SrsStatisticVhost*>::iterator it;
  38 + for (it = vhosts.begin(); it != vhosts.end(); it++) {
  39 + SrsStatisticVhost* vhost = it->second;
  40 + srs_freep(vhost);
  41 + }
  42 + }
  43 + if (true) {
  44 + std::map<std::string, SrsStatisticStream*>::iterator it;
  45 + for (it = streams.begin(); it != streams.end(); it++) {
  46 + SrsStatisticStream* stream = it->second;
  47 + srs_freep(stream);
  48 + }
  49 + }
  50 + if (true) {
  51 + std::map<int, SrsStatisticClient*>::iterator it;
  52 + for (it = clients.begin(); it != clients.end(); it++) {
  53 + SrsStatisticClient* client = it->second;
  54 + srs_freep(client);
  55 + }
50 } 56 }
51 } 57 }
52 58
53 SrsStatistic* SrsStatistic::instance() 59 SrsStatistic* SrsStatistic::instance()
54 { 60 {
55 - if (_instance == NULL) {  
56 - _instance = new SrsStatistic();  
57 - }  
58 return _instance; 61 return _instance;
59 } 62 }
60 63
61 -std::map<void*, SrsStreamInfo*>* SrsStatistic::get_pool() 64 +int SrsStatistic::on_client(int id, SrsRequest *req)
62 { 65 {
63 - return &pool; 66 + int ret = ERROR_SUCCESS;
  67 + return ret;
64 } 68 }
65 69
66 -SrsStreamInfo* SrsStatistic::get(void *p) 70 +int SrsStatistic::dumps_vhosts(std::stringstream& ss)
67 { 71 {
68 - std::map<void*, SrsStreamInfo*>::iterator it = pool.find(p);  
69 - if (it == pool.end()) {  
70 - SrsStreamInfo* si = new SrsStreamInfo();  
71 - pool[p] = si;  
72 - return si;  
73 - } else {  
74 - SrsStreamInfo* si = it->second;  
75 - return si;  
76 - } 72 + int ret = ERROR_SUCCESS;
  73 + return ret;
77 } 74 }
78 75
79 -void SrsStatistic::add_request_info(void *p, SrsRequest *req) 76 +int SrsStatistic::dumps_streams(std::stringstream& ss)
80 { 77 {
81 - SrsStreamInfo* info = get(p);  
82 - if (info->_req == NULL) {  
83 - info->_req = req->copy();  
84 - }  
85 -}  
  78 + int ret = ERROR_SUCCESS;
  79 + return ret;
  80 +}
@@ -31,33 +31,62 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,33 +31,62 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #include <srs_core.hpp> 31 #include <srs_core.hpp>
32 32
33 #include <map> 33 #include <map>
  34 +#include <string>
34 35
35 class SrsRequest; 36 class SrsRequest;
36 37
37 -class SrsStreamInfo 38 +struct SrsStatisticVhost
38 { 39 {
39 public: 40 public:
40 - SrsStreamInfo();  
41 - virtual ~SrsStreamInfo();  
42 -public:  
43 - SrsRequest *_req; 41 + std::string vhost;
44 }; 42 };
45 43
46 -class SrsStatistic 44 +struct SrsStatisticStream
47 { 45 {
48 public: 46 public:
49 - static SrsStatistic* instance(); 47 + SrsStatisticVhost* vhost;
  48 + std::string app;
  49 + std::string stream;
  50 +};
  51 +
  52 +struct SrsStatisticClient
  53 +{
50 public: 54 public:
51 - virtual std::map<void*, SrsStreamInfo*>* get_pool();  
52 - virtual void add_request_info(void *p, SrsRequest *req);  
53 -private:  
54 - SrsStatistic();  
55 - virtual ~SrsStatistic(); 55 + SrsStatisticStream* stream;
  56 + int id;
  57 +};
  58 +
  59 +class SrsStatistic
  60 +{
56 private: 61 private:
57 static SrsStatistic *_instance; 62 static SrsStatistic *_instance;
58 - std::map<void*, SrsStreamInfo*> pool; 63 + // key: vhost name, value: vhost object.
  64 + std::map<std::string, SrsStatisticVhost*> vhosts;
  65 + // key: stream name, value: stream object.
  66 + std::map<std::string, SrsStatisticStream*> streams;
  67 + // key: client id, value: stream object.
  68 + std::map<int, SrsStatisticClient*> clients;
59 private: 69 private:
60 - virtual SrsStreamInfo *get(void *p); 70 + SrsStatistic();
  71 + virtual ~SrsStatistic();
  72 +public:
  73 + static SrsStatistic* instance();
  74 +public:
  75 + /**
  76 + * when got a client to publish/play stream,
  77 + * @param id, the client srs id.
  78 + * @param req, the client request object.
  79 + */
  80 + virtual int on_client(int id, SrsRequest *req);
  81 +public:
  82 + /**
  83 + * dumps the vhosts to sstream in json.
  84 + */
  85 + virtual int dumps_vhosts(std::stringstream& ss);
  86 + /**
  87 + * dumps the streams to sstream in json.
  88 + */
  89 + virtual int dumps_streams(std::stringstream& ss);
61 }; 90 };
62 91
63 #endif 92 #endif