diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index 9881e4e01..e9f9e429a 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -200,6 +200,7 @@ int SrsRtmpConn::do_cycle() ret = service_cycle(); http_hooks_on_close(); + SrsStatistic::instance()->on_close(_srs_context->get_id()); return ret; } @@ -398,8 +399,7 @@ int SrsRtmpConn::stream_service_cycle() // update the statistic when source disconveried. SrsStatistic* stat = SrsStatistic::instance(); - int client_id = _srs_context->get_id(); - if ((ret = stat->on_client(client_id, req)) != ERROR_SUCCESS) { + if ((ret = stat->on_client(_srs_context->get_id(), req)) != ERROR_SUCCESS) { srs_error("stat client failed. ret=%d", ret); return ret; } @@ -446,15 +446,9 @@ int SrsRtmpConn::stream_service_cycle() return ret; } - if ((ret = stat->on_client_play_start(client_id)) != ERROR_SUCCESS) { - srs_error("stat client play start failed. ret=%d", ret); - return ret; - } - srs_info("start to play stream %s success", req->stream.c_str()); ret = playing(source); http_hooks_on_stop(); - stat->on_client_play_stop(client_id); return ret; } diff --git a/trunk/src/app/srs_app_statistic.cpp b/trunk/src/app/srs_app_statistic.cpp index f03b78fbc..49eef587f 100644 --- a/trunk/src/app/srs_app_statistic.cpp +++ b/trunk/src/app/srs_app_statistic.cpp @@ -50,7 +50,6 @@ SrsStatisticStream::SrsStatisticStream() { id = __srs_generate_id(); vhost = NULL; - clients = 0; } SrsStatisticStream::~SrsStatisticStream() @@ -133,30 +132,22 @@ int SrsStatistic::on_client(int id, SrsRequest* req) client = clients[id]; } + stream->clients[id] = client; + return ret; } -int SrsStatistic::on_client_play_start(int id) +int SrsStatistic::on_close(int id) { int ret = ERROR_SUCCESS; std::map::iterator it; it = clients.find(id); if (it != clients.end()) { - it->second->stream->clients++; - } - - return ret; -} - -int SrsStatistic::on_client_play_stop(int id) -{ - int ret = ERROR_SUCCESS; - - std::map::iterator it; - it = clients.find(id); - if (it != clients.end()) { - it->second->stream->clients--; + SrsStatisticClient* client = it->second; + client->stream->clients.erase(id); + srs_freep(client); + clients.erase(it); } return ret; @@ -172,13 +163,10 @@ int SrsStatistic::dumps_vhosts(stringstream& ss) int ret = ERROR_SUCCESS; ss << __SRS_JARRAY_START; - bool first = true; std::map::iterator it; for (it = vhosts.begin(); it != vhosts.end(); it++) { SrsStatisticVhost* vhost = it->second; - if (first) { - first = false; - } else { + if (it != vhosts.begin()) { ss << __SRS_JFIELD_CONT; } @@ -197,13 +185,10 @@ int SrsStatistic::dumps_streams(stringstream& ss) int ret = ERROR_SUCCESS; ss << __SRS_JARRAY_START; - bool first = true; std::map::iterator it; for (it = streams.begin(); it != streams.end(); it++) { SrsStatisticStream* stream = it->second; - if (first) { - first = false; - } else { + if (it != streams.begin()) { ss << __SRS_JFIELD_CONT; } @@ -211,7 +196,7 @@ int SrsStatistic::dumps_streams(stringstream& ss) << __SRS_JFIELD_ORG("id", stream->id) << __SRS_JFIELD_CONT << __SRS_JFIELD_STR("name", stream->stream) << __SRS_JFIELD_CONT << __SRS_JFIELD_ORG("vhost", stream->vhost->id) << __SRS_JFIELD_CONT - << __SRS_JFIELD_ORG("clients", stream->clients) + << __SRS_JFIELD_ORG("clients", stream->clients.size()) << __SRS_JOBJECT_END; } ss << __SRS_JARRAY_END; diff --git a/trunk/src/app/srs_app_statistic.hpp b/trunk/src/app/srs_app_statistic.hpp index 452254bde..0d380549d 100644 --- a/trunk/src/app/srs_app_statistic.hpp +++ b/trunk/src/app/srs_app_statistic.hpp @@ -34,6 +34,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include class SrsRequest; +struct SrsStatisticClient; struct SrsStatisticVhost { @@ -53,7 +54,7 @@ public: std::string app; std::string stream; std::string url; - int64_t clients; + std::map clients; public: SrsStatisticStream(); virtual ~SrsStatisticStream(); @@ -91,13 +92,9 @@ public: */ virtual int on_client(int id, SrsRequest* req); /** - * client start play + * client close */ - virtual int on_client_play_start(int id); - /** - * client stop play - */ - virtual int on_client_play_stop(int id); + virtual int on_close(int id); public: /** * get the server id, used to identify the server.