qiang.li

add play client num in statistic

@@ -530,9 +530,7 @@ int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) @@ -530,9 +530,7 @@ int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
530 ss << __SRS_JOBJECT_START 530 ss << __SRS_JOBJECT_START
531 << __SRS_JFIELD_ERROR(ret) << __SRS_JFIELD_CONT 531 << __SRS_JFIELD_ERROR(ret) << __SRS_JFIELD_CONT
532 << __SRS_JFIELD_ORG("server", stat->server_id()) << __SRS_JFIELD_CONT 532 << __SRS_JFIELD_ORG("server", stat->server_id()) << __SRS_JFIELD_CONT
533 - << __SRS_JFIELD_ORG("vhosts", __SRS_JARRAY_START)  
534 - << data.str()  
535 - << __SRS_JARRAY_END 533 + << __SRS_JFIELD_ORG("vhosts", data.str())
536 << __SRS_JOBJECT_END; 534 << __SRS_JOBJECT_END;
537 535
538 return res_json(skt, req, ss.str()); 536 return res_json(skt, req, ss.str());
@@ -562,9 +560,7 @@ int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) @@ -562,9 +560,7 @@ int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
562 ss << __SRS_JOBJECT_START 560 ss << __SRS_JOBJECT_START
563 << __SRS_JFIELD_ERROR(ret) << __SRS_JFIELD_CONT 561 << __SRS_JFIELD_ERROR(ret) << __SRS_JFIELD_CONT
564 << __SRS_JFIELD_ORG("server", stat->server_id()) << __SRS_JFIELD_CONT 562 << __SRS_JFIELD_ORG("server", stat->server_id()) << __SRS_JFIELD_CONT
565 - << __SRS_JFIELD_ORG("streams", __SRS_JARRAY_START)  
566 - << data.str()  
567 - << __SRS_JARRAY_END 563 + << __SRS_JFIELD_ORG("streams", data.str())
568 << __SRS_JOBJECT_END; 564 << __SRS_JOBJECT_END;
569 565
570 return res_json(skt, req, ss.str()); 566 return res_json(skt, req, ss.str());
@@ -398,7 +398,8 @@ int SrsRtmpConn::stream_service_cycle() @@ -398,7 +398,8 @@ int SrsRtmpConn::stream_service_cycle()
398 398
399 // update the statistic when source disconveried. 399 // update the statistic when source disconveried.
400 SrsStatistic* stat = SrsStatistic::instance(); 400 SrsStatistic* stat = SrsStatistic::instance();
401 - if ((ret = stat->on_client(_srs_context->get_id(), req)) != ERROR_SUCCESS) { 401 + int client_id = _srs_context->get_id();
  402 + if ((ret = stat->on_client(client_id, req)) != ERROR_SUCCESS) {
402 srs_error("stat client failed. ret=%d", ret); 403 srs_error("stat client failed. ret=%d", ret);
403 return ret; 404 return ret;
404 } 405 }
@@ -445,9 +446,15 @@ int SrsRtmpConn::stream_service_cycle() @@ -445,9 +446,15 @@ int SrsRtmpConn::stream_service_cycle()
445 return ret; 446 return ret;
446 } 447 }
447 448
  449 + if ((ret = stat->on_client_play_start(client_id)) != ERROR_SUCCESS) {
  450 + srs_error("stat client play start failed. ret=%d", ret);
  451 + return ret;
  452 + }
  453 +
448 srs_info("start to play stream %s success", req->stream.c_str()); 454 srs_info("start to play stream %s success", req->stream.c_str());
449 ret = playing(source); 455 ret = playing(source);
450 http_hooks_on_stop(); 456 http_hooks_on_stop();
  457 + stat->on_client_play_stop(client_id);
451 458
452 return ret; 459 return ret;
453 } 460 }
@@ -50,6 +50,7 @@ SrsStatisticStream::SrsStatisticStream() @@ -50,6 +50,7 @@ SrsStatisticStream::SrsStatisticStream()
50 { 50 {
51 id = __srs_generate_id(); 51 id = __srs_generate_id();
52 vhost = NULL; 52 vhost = NULL;
  53 + clients = 0;
53 } 54 }
54 55
55 SrsStatisticStream::~SrsStatisticStream() 56 SrsStatisticStream::~SrsStatisticStream()
@@ -121,10 +122,46 @@ int SrsStatistic::on_client(int id, SrsRequest* req) @@ -121,10 +122,46 @@ int SrsStatistic::on_client(int id, SrsRequest* req)
121 } else { 122 } else {
122 stream = streams[url]; 123 stream = streams[url];
123 } 124 }
  125 +
  126 + // create client if not exists
  127 + SrsStatisticClient* client = NULL;
  128 + if (clients.find(id) == clients.end()) {
  129 + client = new SrsStatisticClient();
  130 + client->stream = stream;
  131 + clients[id] = client;
  132 + } else {
  133 + client = clients[id];
  134 + }
124 135
125 return ret; 136 return ret;
126 } 137 }
127 138
  139 +int SrsStatistic::on_client_play_start(int id)
  140 +{
  141 + int ret = ERROR_SUCCESS;
  142 +
  143 + std::map<int, SrsStatisticClient*>::iterator it;
  144 + it = clients.find(id);
  145 + if (it != clients.end()) {
  146 + it->second->stream->clients++;
  147 + }
  148 +
  149 + return ret;
  150 +}
  151 +
  152 +int SrsStatistic::on_client_play_stop(int id)
  153 +{
  154 + int ret = ERROR_SUCCESS;
  155 +
  156 + std::map<int, SrsStatisticClient*>::iterator it;
  157 + it = clients.find(id);
  158 + if (it != clients.end()) {
  159 + it->second->stream->clients--;
  160 + }
  161 +
  162 + return ret;
  163 +}
  164 +
128 int64_t SrsStatistic::server_id() 165 int64_t SrsStatistic::server_id()
129 { 166 {
130 return _server_id; 167 return _server_id;
@@ -134,15 +171,24 @@ int SrsStatistic::dumps_vhosts(stringstream& ss) @@ -134,15 +171,24 @@ int SrsStatistic::dumps_vhosts(stringstream& ss)
134 { 171 {
135 int ret = ERROR_SUCCESS; 172 int ret = ERROR_SUCCESS;
136 173
  174 + ss << __SRS_JARRAY_START;
  175 + bool first = true;
137 std::map<std::string, SrsStatisticVhost*>::iterator it; 176 std::map<std::string, SrsStatisticVhost*>::iterator it;
138 for (it = vhosts.begin(); it != vhosts.end(); it++) { 177 for (it = vhosts.begin(); it != vhosts.end(); it++) {
139 SrsStatisticVhost* vhost = it->second; 178 SrsStatisticVhost* vhost = it->second;
  179 + if (first) {
  180 + first = false;
  181 + } else {
  182 + ss << __SRS_JFIELD_CONT;
  183 + }
  184 +
140 ss << __SRS_JOBJECT_START 185 ss << __SRS_JOBJECT_START
141 << __SRS_JFIELD_ORG("id", vhost->id) << __SRS_JFIELD_CONT 186 << __SRS_JFIELD_ORG("id", vhost->id) << __SRS_JFIELD_CONT
142 << __SRS_JFIELD_STR("name", vhost->vhost) 187 << __SRS_JFIELD_STR("name", vhost->vhost)
143 << __SRS_JOBJECT_END; 188 << __SRS_JOBJECT_END;
144 } 189 }
145 - 190 + ss << __SRS_JARRAY_END;
  191 +
146 return ret; 192 return ret;
147 } 193 }
148 194
@@ -150,15 +196,25 @@ int SrsStatistic::dumps_streams(stringstream& ss) @@ -150,15 +196,25 @@ int SrsStatistic::dumps_streams(stringstream& ss)
150 { 196 {
151 int ret = ERROR_SUCCESS; 197 int ret = ERROR_SUCCESS;
152 198
  199 + ss << __SRS_JARRAY_START;
  200 + bool first = true;
153 std::map<std::string, SrsStatisticStream*>::iterator it; 201 std::map<std::string, SrsStatisticStream*>::iterator it;
154 for (it = streams.begin(); it != streams.end(); it++) { 202 for (it = streams.begin(); it != streams.end(); it++) {
155 SrsStatisticStream* stream = it->second; 203 SrsStatisticStream* stream = it->second;
  204 + if (first) {
  205 + first = false;
  206 + } else {
  207 + ss << __SRS_JFIELD_CONT;
  208 + }
  209 +
156 ss << __SRS_JOBJECT_START 210 ss << __SRS_JOBJECT_START
157 << __SRS_JFIELD_ORG("id", stream->id) << __SRS_JFIELD_CONT 211 << __SRS_JFIELD_ORG("id", stream->id) << __SRS_JFIELD_CONT
158 << __SRS_JFIELD_STR("name", stream->stream) << __SRS_JFIELD_CONT 212 << __SRS_JFIELD_STR("name", stream->stream) << __SRS_JFIELD_CONT
159 - << __SRS_JFIELD_ORG("vhost", stream->vhost->id) 213 + << __SRS_JFIELD_ORG("vhost", stream->vhost->id) << __SRS_JFIELD_CONT
  214 + << __SRS_JFIELD_ORG("clients", stream->clients)
160 << __SRS_JOBJECT_END; 215 << __SRS_JOBJECT_END;
161 } 216 }
  217 + ss << __SRS_JARRAY_END;
162 218
163 return ret; 219 return ret;
164 } 220 }
@@ -53,6 +53,7 @@ public: @@ -53,6 +53,7 @@ public:
53 std::string app; 53 std::string app;
54 std::string stream; 54 std::string stream;
55 std::string url; 55 std::string url;
  56 + int64_t clients;
56 public: 57 public:
57 SrsStatisticStream(); 58 SrsStatisticStream();
58 virtual ~SrsStatisticStream(); 59 virtual ~SrsStatisticStream();
@@ -89,6 +90,14 @@ public: @@ -89,6 +90,14 @@ public:
89 * @param req, the client request object. 90 * @param req, the client request object.
90 */ 91 */
91 virtual int on_client(int id, SrsRequest* req); 92 virtual int on_client(int id, SrsRequest* req);
  93 + /**
  94 + * client start play
  95 + */
  96 + virtual int on_client_play_start(int id);
  97 + /**
  98 + * client stop play
  99 + */
  100 + virtual int on_client_play_stop(int id);
92 public: 101 public:
93 /** 102 /**
94 * get the server id, used to identify the server. 103 * get the server id, used to identify the server.