qiang.li

add client num for stream in statistic

@@ -200,6 +200,7 @@ int SrsRtmpConn::do_cycle() @@ -200,6 +200,7 @@ int SrsRtmpConn::do_cycle()
200 200
201 ret = service_cycle(); 201 ret = service_cycle();
202 http_hooks_on_close(); 202 http_hooks_on_close();
  203 + SrsStatistic::instance()->on_close(_srs_context->get_id());
203 204
204 return ret; 205 return ret;
205 } 206 }
@@ -398,8 +399,7 @@ int SrsRtmpConn::stream_service_cycle() @@ -398,8 +399,7 @@ int SrsRtmpConn::stream_service_cycle()
398 399
399 // update the statistic when source disconveried. 400 // update the statistic when source disconveried.
400 SrsStatistic* stat = SrsStatistic::instance(); 401 SrsStatistic* stat = SrsStatistic::instance();
401 - int client_id = _srs_context->get_id();  
402 - if ((ret = stat->on_client(client_id, req)) != ERROR_SUCCESS) { 402 + if ((ret = stat->on_client(_srs_context->get_id(), req)) != ERROR_SUCCESS) {
403 srs_error("stat client failed. ret=%d", ret); 403 srs_error("stat client failed. ret=%d", ret);
404 return ret; 404 return ret;
405 } 405 }
@@ -446,15 +446,9 @@ int SrsRtmpConn::stream_service_cycle() @@ -446,15 +446,9 @@ int SrsRtmpConn::stream_service_cycle()
446 return ret; 446 return ret;
447 } 447 }
448 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 -  
454 srs_info("start to play stream %s success", req->stream.c_str()); 449 srs_info("start to play stream %s success", req->stream.c_str());
455 ret = playing(source); 450 ret = playing(source);
456 http_hooks_on_stop(); 451 http_hooks_on_stop();
457 - stat->on_client_play_stop(client_id);  
458 452
459 return ret; 453 return ret;
460 } 454 }
@@ -50,7 +50,6 @@ SrsStatisticStream::SrsStatisticStream() @@ -50,7 +50,6 @@ SrsStatisticStream::SrsStatisticStream()
50 { 50 {
51 id = __srs_generate_id(); 51 id = __srs_generate_id();
52 vhost = NULL; 52 vhost = NULL;
53 - clients = 0;  
54 } 53 }
55 54
56 SrsStatisticStream::~SrsStatisticStream() 55 SrsStatisticStream::~SrsStatisticStream()
@@ -133,30 +132,22 @@ int SrsStatistic::on_client(int id, SrsRequest* req) @@ -133,30 +132,22 @@ int SrsStatistic::on_client(int id, SrsRequest* req)
133 client = clients[id]; 132 client = clients[id];
134 } 133 }
135 134
136 - return ret;  
137 -}  
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 - } 135 + stream->clients[id] = client;
148 136
149 return ret; 137 return ret;
150 } 138 }
151 139
152 -int SrsStatistic::on_client_play_stop(int id) 140 +int SrsStatistic::on_close(int id)
153 { 141 {
154 int ret = ERROR_SUCCESS; 142 int ret = ERROR_SUCCESS;
155 143
156 std::map<int, SrsStatisticClient*>::iterator it; 144 std::map<int, SrsStatisticClient*>::iterator it;
157 it = clients.find(id); 145 it = clients.find(id);
158 if (it != clients.end()) { 146 if (it != clients.end()) {
159 - it->second->stream->clients--; 147 + SrsStatisticClient* client = it->second;
  148 + client->stream->clients.erase(id);
  149 + srs_freep(client);
  150 + clients.erase(it);
160 } 151 }
161 152
162 return ret; 153 return ret;
@@ -172,13 +163,10 @@ int SrsStatistic::dumps_vhosts(stringstream& ss) @@ -172,13 +163,10 @@ int SrsStatistic::dumps_vhosts(stringstream& ss)
172 int ret = ERROR_SUCCESS; 163 int ret = ERROR_SUCCESS;
173 164
174 ss << __SRS_JARRAY_START; 165 ss << __SRS_JARRAY_START;
175 - bool first = true;  
176 std::map<std::string, SrsStatisticVhost*>::iterator it; 166 std::map<std::string, SrsStatisticVhost*>::iterator it;
177 for (it = vhosts.begin(); it != vhosts.end(); it++) { 167 for (it = vhosts.begin(); it != vhosts.end(); it++) {
178 SrsStatisticVhost* vhost = it->second; 168 SrsStatisticVhost* vhost = it->second;
179 - if (first) {  
180 - first = false;  
181 - } else { 169 + if (it != vhosts.begin()) {
182 ss << __SRS_JFIELD_CONT; 170 ss << __SRS_JFIELD_CONT;
183 } 171 }
184 172
@@ -197,13 +185,10 @@ int SrsStatistic::dumps_streams(stringstream& ss) @@ -197,13 +185,10 @@ int SrsStatistic::dumps_streams(stringstream& ss)
197 int ret = ERROR_SUCCESS; 185 int ret = ERROR_SUCCESS;
198 186
199 ss << __SRS_JARRAY_START; 187 ss << __SRS_JARRAY_START;
200 - bool first = true;  
201 std::map<std::string, SrsStatisticStream*>::iterator it; 188 std::map<std::string, SrsStatisticStream*>::iterator it;
202 for (it = streams.begin(); it != streams.end(); it++) { 189 for (it = streams.begin(); it != streams.end(); it++) {
203 SrsStatisticStream* stream = it->second; 190 SrsStatisticStream* stream = it->second;
204 - if (first) {  
205 - first = false;  
206 - } else { 191 + if (it != streams.begin()) {
207 ss << __SRS_JFIELD_CONT; 192 ss << __SRS_JFIELD_CONT;
208 } 193 }
209 194
@@ -211,7 +196,7 @@ int SrsStatistic::dumps_streams(stringstream& ss) @@ -211,7 +196,7 @@ int SrsStatistic::dumps_streams(stringstream& ss)
211 << __SRS_JFIELD_ORG("id", stream->id) << __SRS_JFIELD_CONT 196 << __SRS_JFIELD_ORG("id", stream->id) << __SRS_JFIELD_CONT
212 << __SRS_JFIELD_STR("name", stream->stream) << __SRS_JFIELD_CONT 197 << __SRS_JFIELD_STR("name", stream->stream) << __SRS_JFIELD_CONT
213 << __SRS_JFIELD_ORG("vhost", stream->vhost->id) << __SRS_JFIELD_CONT 198 << __SRS_JFIELD_ORG("vhost", stream->vhost->id) << __SRS_JFIELD_CONT
214 - << __SRS_JFIELD_ORG("clients", stream->clients) 199 + << __SRS_JFIELD_ORG("clients", stream->clients.size())
215 << __SRS_JOBJECT_END; 200 << __SRS_JOBJECT_END;
216 } 201 }
217 ss << __SRS_JARRAY_END; 202 ss << __SRS_JARRAY_END;
@@ -34,6 +34,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -34,6 +34,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 #include <string> 34 #include <string>
35 35
36 class SrsRequest; 36 class SrsRequest;
  37 +struct SrsStatisticClient;
37 38
38 struct SrsStatisticVhost 39 struct SrsStatisticVhost
39 { 40 {
@@ -53,7 +54,7 @@ public: @@ -53,7 +54,7 @@ public:
53 std::string app; 54 std::string app;
54 std::string stream; 55 std::string stream;
55 std::string url; 56 std::string url;
56 - int64_t clients; 57 + std::map<int, SrsStatisticClient*> clients;
57 public: 58 public:
58 SrsStatisticStream(); 59 SrsStatisticStream();
59 virtual ~SrsStatisticStream(); 60 virtual ~SrsStatisticStream();
@@ -91,13 +92,9 @@ public: @@ -91,13 +92,9 @@ public:
91 */ 92 */
92 virtual int on_client(int id, SrsRequest* req); 93 virtual int on_client(int id, SrsRequest* req);
93 /** 94 /**
94 - * client start play  
95 - */  
96 - virtual int on_client_play_start(int id);  
97 - /**  
98 - * client stop play 95 + * client close
99 */ 96 */
100 - virtual int on_client_play_stop(int id); 97 + virtual int on_close(int id);
101 public: 98 public:
102 /** 99 /**
103 * get the server id, used to identify the server. 100 * get the server id, used to identify the server.