winlin

refine code, implements the stat.:

@@ -23,7 +23,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -23,7 +23,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 23
24 #include <srs_app_statistic.hpp> 24 #include <srs_app_statistic.hpp>
25 25
  26 +#include <sstream>
  27 +using namespace std;
  28 +
26 #include <srs_protocol_rtmp.hpp> 29 #include <srs_protocol_rtmp.hpp>
  30 +#include <srs_app_json.hpp>
27 31
28 SrsStatistic* SrsStatistic::_instance = new SrsStatistic(); 32 SrsStatistic* SrsStatistic::_instance = new SrsStatistic();
29 33
@@ -61,20 +65,63 @@ SrsStatistic* SrsStatistic::instance() @@ -61,20 +65,63 @@ SrsStatistic* SrsStatistic::instance()
61 return _instance; 65 return _instance;
62 } 66 }
63 67
64 -int SrsStatistic::on_client(int id, SrsRequest *req) 68 +int SrsStatistic::on_client(int id, SrsRequest* req)
65 { 69 {
66 int ret = ERROR_SUCCESS; 70 int ret = ERROR_SUCCESS;
  71 +
  72 + // create vhost if not exists.
  73 + SrsStatisticVhost* vhost = NULL;
  74 + if (vhosts.find(req->vhost) == vhosts.end()) {
  75 + vhost = new SrsStatisticVhost();
  76 + vhost->vhost = req->vhost;
  77 + vhosts[req->vhost] = vhost;
  78 + } else {
  79 + vhost = vhosts[req->vhost];
  80 + }
  81 +
  82 + // the url to identify the stream.
  83 + std::string url = req->get_stream_url();
  84 +
  85 + // create stream if not exists.
  86 + SrsStatisticStream* stream = NULL;
  87 + if (streams.find(url) == streams.end()) {
  88 + stream = new SrsStatisticStream();
  89 + stream->vhost = vhost;
  90 + stream->stream = url;
  91 + streams[url] = stream;
  92 + } else {
  93 + stream = streams[url];
  94 + }
  95 +
67 return ret; 96 return ret;
68 } 97 }
69 98
70 -int SrsStatistic::dumps_vhosts(std::stringstream& ss) 99 +int SrsStatistic::dumps_vhosts(stringstream& ss)
71 { 100 {
72 int ret = ERROR_SUCCESS; 101 int ret = ERROR_SUCCESS;
  102 +
  103 + std::map<std::string, SrsStatisticVhost*>::iterator it;
  104 + for (it = vhosts.begin(); it != vhosts.end(); it++) {
  105 + SrsStatisticVhost* vhost = it->second;
  106 + ss << __SRS_JOBJECT_START
  107 + << __SRS_JFIELD_STR("name", vhost->vhost)
  108 + << __SRS_JOBJECT_END;
  109 + }
  110 +
73 return ret; 111 return ret;
74 } 112 }
75 113
76 -int SrsStatistic::dumps_streams(std::stringstream& ss) 114 +int SrsStatistic::dumps_streams(stringstream& ss)
77 { 115 {
78 int ret = ERROR_SUCCESS; 116 int ret = ERROR_SUCCESS;
  117 +
  118 + std::map<std::string, SrsStatisticStream*>::iterator it;
  119 + for (it = streams.begin(); it != streams.end(); it++) {
  120 + SrsStatisticStream* stream = it->second;
  121 + ss << __SRS_JOBJECT_START
  122 + << __SRS_JFIELD_STR("url", stream->stream)
  123 + << __SRS_JOBJECT_END;
  124 + }
  125 +
79 return ret; 126 return ret;
80 } 127 }
@@ -77,7 +77,7 @@ public: @@ -77,7 +77,7 @@ public:
77 * @param id, the client srs id. 77 * @param id, the client srs id.
78 * @param req, the client request object. 78 * @param req, the client request object.
79 */ 79 */
80 - virtual int on_client(int id, SrsRequest *req); 80 + virtual int on_client(int id, SrsRequest* req);
81 public: 81 public:
82 /** 82 /**
83 * dumps the vhosts to sstream in json. 83 * dumps the vhosts to sstream in json.