From fb7c051833bfdc57fd22aa7ec2b30921deb264bd Mon Sep 17 00:00:00 2001 From: winlin Date: Thu, 7 Jan 2021 16:47:49 +0800 Subject: [PATCH] Change id from int to string for the statistics. 3.0.157 --- README.md | 1 + trunk/src/app/srs_app_config.cpp | 14 ++-- trunk/src/app/srs_app_http_api.cpp | 44 ++++++------ trunk/src/app/srs_app_http_stream.cpp | 2 +- trunk/src/app/srs_app_rtmp_conn.cpp | 2 +- trunk/src/app/srs_app_server.cpp | 2 +- trunk/src/app/srs_app_source.cpp | 2 +- trunk/src/app/srs_app_statistic.cpp | 76 ++++++++++----------- trunk/src/app/srs_app_statistic.hpp | 34 ++++----- trunk/src/core/srs_core_version3.hpp | 2 +- trunk/src/protocol/srs_http_stack.hpp | 6 +- trunk/src/service/srs_service_http_conn.cpp | 8 +-- trunk/src/service/srs_service_http_conn.hpp | 2 +- trunk/src/utest/srs_utest_service.cpp | 8 ++- 14 files changed, 108 insertions(+), 95 deletions(-) diff --git a/README.md b/README.md index e08ed3e1e..7d69f7a18 100755 --- a/README.md +++ b/README.md @@ -145,6 +145,7 @@ For previous versions, please read: ## V3 changes +* v3.0, 2021-01-07, Change id from int to string for the statistics. 3.0.157 * v3.0, 2021-01-02, [3.0 release3(3.0.156)][r3.0r3] released. 122736 lines. * v3.0, 2020-12-26, For RTMP edge/forward, pass vhost in tcUrl, not in stream. 3.0.156 * v3.0, 2020-12-17, Fix [#1694][bug #1694], Support DVR 2GB+ MP4 file. 3.0.155 diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 62a324943..cebebdb43 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -2158,8 +2158,11 @@ srs_error_t SrsConfig::global_to_json(SrsJsonObject* obj) SrsJsonObject* sobj = SrsJsonAny::object(); sobjs->set(dir->arg0(), sobj); - SrsStatisticVhost* svhost = stat->find_vhost(dir->arg0()); - sobj->set("id", SrsJsonAny::integer(svhost? (double)svhost->id : 0)); + SrsStatisticVhost* svhost = stat->find_vhost_by_name(dir->arg0()); + if (!svhost) { + continue; + } + sobj->set("id", SrsJsonAny::str(svhost->id.c_str())); sobj->set("name", dir->dumps_arg0_to_str()); sobj->set("enabled", SrsJsonAny::boolean(get_vhost_enabled(dir->arg0()))); @@ -2283,8 +2286,11 @@ srs_error_t SrsConfig::vhost_to_json(SrsConfDirective* vhost, SrsJsonObject* obj // always present in vhost. SrsStatistic* stat = SrsStatistic::instance(); - SrsStatisticVhost* svhost = stat->find_vhost(vhost->arg0()); - obj->set("id", SrsJsonAny::integer(svhost? (double)svhost->id : 0)); + SrsStatisticVhost* svhost = stat->find_vhost_by_name(vhost->arg0()); + if (!svhost) { + return err; + } + obj->set("id", SrsJsonAny::str(svhost->id.c_str())); obj->set("name", vhost->dumps_arg0_to_str()); obj->set("enabled", SrsJsonAny::boolean(get_vhost_enabled(vhost))); diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index 5355068eb..3e852fdc3 100644 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -193,7 +193,7 @@ srs_error_t SrsGoApiRoot::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* SrsAutoFree(SrsJsonObject, obj); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); - obj->set("server", SrsJsonAny::integer(stat->server_id())); + obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); SrsJsonObject* urls = SrsJsonAny::object(); obj->set("urls", urls); @@ -219,7 +219,7 @@ srs_error_t SrsGoApiApi::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* SrsAutoFree(SrsJsonObject, obj); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); - obj->set("server", SrsJsonAny::integer(stat->server_id())); + obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); SrsJsonObject* urls = SrsJsonAny::object(); obj->set("urls", urls); @@ -245,7 +245,7 @@ srs_error_t SrsGoApiV1::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r SrsAutoFree(SrsJsonObject, obj); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); - obj->set("server", SrsJsonAny::integer(stat->server_id())); + obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); SrsJsonObject* urls = SrsJsonAny::object(); obj->set("urls", urls); @@ -292,7 +292,7 @@ srs_error_t SrsGoApiVersion::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa SrsAutoFree(SrsJsonObject, obj); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); - obj->set("server", SrsJsonAny::integer(stat->server_id())); + obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); SrsJsonObject* data = SrsJsonAny::object(); obj->set("data", data); @@ -321,7 +321,7 @@ srs_error_t SrsGoApiSummaries::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMes SrsAutoFree(SrsJsonObject, obj); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); - obj->set("server", SrsJsonAny::integer(stat->server_id())); + obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); srs_api_dump_summaries(obj); @@ -344,7 +344,7 @@ srs_error_t SrsGoApiRusages::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa SrsAutoFree(SrsJsonObject, obj); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); - obj->set("server", SrsJsonAny::integer(stat->server_id())); + obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); SrsJsonObject* data = SrsJsonAny::object(); obj->set("data", data); @@ -389,7 +389,7 @@ srs_error_t SrsGoApiSelfProcStats::serve_http(ISrsHttpResponseWriter* w, ISrsHtt SrsAutoFree(SrsJsonObject, obj); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); - obj->set("server", SrsJsonAny::integer(stat->server_id())); + obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); SrsJsonObject* data = SrsJsonAny::object(); obj->set("data", data); @@ -466,7 +466,7 @@ srs_error_t SrsGoApiSystemProcStats::serve_http(ISrsHttpResponseWriter* w, ISrsH SrsAutoFree(SrsJsonObject, obj); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); - obj->set("server", SrsJsonAny::integer(stat->server_id())); + obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); SrsJsonObject* data = SrsJsonAny::object(); obj->set("data", data); @@ -505,7 +505,7 @@ srs_error_t SrsGoApiMemInfos::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMess SrsAutoFree(SrsJsonObject, obj); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); - obj->set("server", SrsJsonAny::integer(stat->server_id())); + obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); SrsJsonObject* data = SrsJsonAny::object(); obj->set("data", data); @@ -545,7 +545,7 @@ srs_error_t SrsGoApiAuthors::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa SrsAutoFree(SrsJsonObject, obj); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); - obj->set("server", SrsJsonAny::integer(stat->server_id())); + obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); SrsJsonObject* data = SrsJsonAny::object(); obj->set("data", data); @@ -572,7 +572,7 @@ srs_error_t SrsGoApiFeatures::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMess SrsAutoFree(SrsJsonObject, obj); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); - obj->set("server", SrsJsonAny::integer(stat->server_id())); + obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); SrsJsonObject* data = SrsJsonAny::object(); obj->set("data", data); @@ -640,7 +640,7 @@ srs_error_t SrsGoApiRequests::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMess SrsAutoFree(SrsJsonObject, obj); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); - obj->set("server", SrsJsonAny::integer(stat->server_id())); + obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); SrsJsonObject* data = SrsJsonAny::object(); obj->set("data", data); @@ -684,10 +684,10 @@ srs_error_t SrsGoApiVhosts::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessag // path: {pattern}{vhost_id} // e.g. /api/v1/vhosts/100 pattern= /api/v1/vhosts/, vhost_id=100 - int vid = r->parse_rest_id(entry->pattern); + string vid = r->parse_rest_id(entry->pattern); SrsStatisticVhost* vhost = NULL; - if (vid > 0 && (vhost = stat->find_vhost(vid)) == NULL) { + if (!vid.empty() && (vhost = stat->find_vhost_by_id(vid)) == NULL) { return srs_api_response_code(w, r, ERROR_RTMP_VHOST_NOT_FOUND); } @@ -695,7 +695,7 @@ srs_error_t SrsGoApiVhosts::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessag SrsAutoFree(SrsJsonObject, obj); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); - obj->set("server", SrsJsonAny::integer(stat->server_id())); + obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); if (r->is_http_get()) { if (!vhost) { @@ -740,10 +740,10 @@ srs_error_t SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa // path: {pattern}{stream_id} // e.g. /api/v1/streams/100 pattern= /api/v1/streams/, stream_id=100 - int sid = r->parse_rest_id(entry->pattern); + string sid = r->parse_rest_id(entry->pattern); SrsStatisticStream* stream = NULL; - if (sid >= 0 && (stream = stat->find_stream(sid)) == NULL) { + if (!sid.empty() && (stream = stat->find_stream(sid)) == NULL) { return srs_api_response_code(w, r, ERROR_RTMP_STREAM_NOT_FOUND); } @@ -751,7 +751,7 @@ srs_error_t SrsGoApiStreams::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa SrsAutoFree(SrsJsonObject, obj); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); - obj->set("server", SrsJsonAny::integer(stat->server_id())); + obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); if (r->is_http_get()) { if (!stream) { @@ -796,10 +796,10 @@ srs_error_t SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa // path: {pattern}{client_id} // e.g. /api/v1/clients/100 pattern= /api/v1/clients/, client_id=100 - int cid = r->parse_rest_id(entry->pattern); + string cid = r->parse_rest_id(entry->pattern); SrsStatisticClient* client = NULL; - if (cid >= 0 && (client = stat->find_client(cid)) == NULL) { + if (!cid.empty() && (client = stat->find_client(cid)) == NULL) { return srs_api_response_code(w, r, ERROR_RTMP_CLIENT_NOT_FOUND); } @@ -807,7 +807,7 @@ srs_error_t SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa SrsAutoFree(SrsJsonObject, obj); obj->set("code", SrsJsonAny::integer(ERROR_SUCCESS)); - obj->set("server", SrsJsonAny::integer(stat->server_id())); + obj->set("server", SrsJsonAny::str(stat->server_id().c_str())); if (r->is_http_get()) { if (!client) { @@ -839,7 +839,7 @@ srs_error_t SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa } client->conn->expire(); - srs_warn("kickoff client id=%d ok", cid); + srs_warn("kickoff client id=%s ok", cid.c_str()); } else { return srs_go_http_error(w, SRS_CONSTS_HTTP_MethodNotAllowed); } diff --git a/trunk/src/app/srs_app_http_stream.cpp b/trunk/src/app/srs_app_http_stream.cpp index 13db329ed..d28335b5c 100755 --- a/trunk/src/app/srs_app_http_stream.cpp +++ b/trunk/src/app/srs_app_http_stream.cpp @@ -600,7 +600,7 @@ srs_error_t SrsLiveStream::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMess // update the statistic when source disconveried. SrsStatistic* stat = SrsStatistic::instance(); - if ((err = stat->on_client(_srs_context->get_id(), req, hc, SrsRtmpConnPlay)) != srs_success) { + if ((err = stat->on_client(srs_int2str(_srs_context->get_id()), req, hc, SrsRtmpConnPlay)) != srs_success) { return srs_error_wrap(err, "stat on client"); } diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index 3b3a0a4c8..699adc870 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -502,7 +502,7 @@ srs_error_t SrsRtmpConn::stream_service_cycle() // update the statistic when source disconveried. SrsStatistic* stat = SrsStatistic::instance(); - if ((err = stat->on_client(_srs_context->get_id(), req, this, info->type)) != srs_success) { + if ((err = stat->on_client(srs_int2str(_srs_context->get_id()), req, this, info->type)) != srs_success) { return srs_error_wrap(err, "rtmp: stat client"); } diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp index 381e32042..971b771c0 100644 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -1477,7 +1477,7 @@ void SrsServer::remove(ISrsConnection* c) SrsStatistic* stat = SrsStatistic::instance(); stat->kbps_add_delta(conn); - stat->on_disconnect(conn->srs_id()); + stat->on_disconnect(srs_int2str(conn->srs_id())); // use manager to free it async. conn_manager->remove(c); diff --git a/trunk/src/app/srs_app_source.cpp b/trunk/src/app/srs_app_source.cpp index 2c62fee40..44cc9fd40 100755 --- a/trunk/src/app/srs_app_source.cpp +++ b/trunk/src/app/srs_app_source.cpp @@ -2464,7 +2464,7 @@ srs_error_t SrsSource::on_publish() return srs_error_wrap(err, "handle publish"); } SrsStatistic* stat = SrsStatistic::instance(); - stat->on_stream_publish(req, _source_id); + stat->on_stream_publish(req, srs_int2str(_source_id)); return err; } diff --git a/trunk/src/app/srs_app_statistic.cpp b/trunk/src/app/srs_app_statistic.cpp index b180d20a1..827d87452 100644 --- a/trunk/src/app/srs_app_statistic.cpp +++ b/trunk/src/app/srs_app_statistic.cpp @@ -35,14 +35,14 @@ using namespace std; #include #include -int64_t srs_gvid = 0; - -int64_t srs_generate_id() +string srs_generate_id() { + static int64_t srs_gvid = 0; + if (srs_gvid == 0) { srs_gvid = getpid() * 3; } - return srs_gvid++; + return "vid-" + srs_int2str(srs_gvid++); } SrsStatisticVhost::SrsStatisticVhost() @@ -71,7 +71,7 @@ srs_error_t SrsStatisticVhost::dumps(SrsJsonObject* obj) bool hls_enabled = _srs_config->get_hls_enabled(vhost); bool enabled = _srs_config->get_vhost_enabled(vhost); - obj->set("id", SrsJsonAny::integer(id)); + obj->set("id", SrsJsonAny::str(id.c_str())); obj->set("name", SrsJsonAny::str(vhost.c_str())); obj->set("enabled", SrsJsonAny::boolean(enabled)); obj->set("clients", SrsJsonAny::integer(nb_clients)); @@ -101,7 +101,7 @@ SrsStatisticStream::SrsStatisticStream() id = srs_generate_id(); vhost = NULL; active = false; - connection_cid = -1; + connection_cid = ""; has_video = false; vcodec = SrsVideoCodecIdReserved; @@ -134,9 +134,9 @@ srs_error_t SrsStatisticStream::dumps(SrsJsonObject* obj) { srs_error_t err = srs_success; - obj->set("id", SrsJsonAny::integer(id)); + obj->set("id", SrsJsonAny::str(id.c_str())); obj->set("name", SrsJsonAny::str(stream.c_str())); - obj->set("vhost", SrsJsonAny::integer(vhost->id)); + obj->set("vhost", SrsJsonAny::str(vhost->id.c_str())); obj->set("app", SrsJsonAny::str(app.c_str())); obj->set("live_ms", SrsJsonAny::integer(srsu2ms(srs_get_system_time()))); obj->set("clients", SrsJsonAny::integer(nb_clients)); @@ -154,7 +154,7 @@ srs_error_t SrsStatisticStream::dumps(SrsJsonObject* obj) obj->set("publish", publish); publish->set("active", SrsJsonAny::boolean(active)); - publish->set("cid", SrsJsonAny::integer(connection_cid)); + publish->set("cid", SrsJsonAny::str(connection_cid.c_str())); if (!has_video) { obj->set("video", SrsJsonAny::null()); @@ -184,7 +184,7 @@ srs_error_t SrsStatisticStream::dumps(SrsJsonObject* obj) return err; } -void SrsStatisticStream::publish(int cid) +void SrsStatisticStream::publish(string cid) { connection_cid = cid; active = true; @@ -203,7 +203,7 @@ void SrsStatisticStream::close() SrsStatisticClient::SrsStatisticClient() { - id = 0; + id = srs_generate_id(); stream = NULL; conn = NULL; req = NULL; @@ -219,9 +219,9 @@ srs_error_t SrsStatisticClient::dumps(SrsJsonObject* obj) { srs_error_t err = srs_success; - obj->set("id", SrsJsonAny::integer(id)); - obj->set("vhost", SrsJsonAny::integer(stream->vhost->id)); - obj->set("stream", SrsJsonAny::integer(stream->id)); + obj->set("id", SrsJsonAny::str(id.c_str())); + obj->set("vhost", SrsJsonAny::str(stream->vhost->id.c_str())); + obj->set("stream", SrsJsonAny::str(stream->id.c_str())); obj->set("ip", SrsJsonAny::str(req->ip.c_str())); obj->set("pageUrl", SrsJsonAny::str(req->pageUrl.c_str())); obj->set("swfUrl", SrsJsonAny::str(req->swfUrl.c_str())); @@ -251,21 +251,21 @@ SrsStatistic::~SrsStatistic() srs_freep(clk); if (true) { - std::map::iterator it; + std::map::iterator it; for (it = vhosts.begin(); it != vhosts.end(); it++) { SrsStatisticVhost* vhost = it->second; srs_freep(vhost); } } if (true) { - std::map::iterator it; + std::map::iterator it; for (it = streams.begin(); it != streams.end(); it++) { SrsStatisticStream* stream = it->second; srs_freep(stream); } } if (true) { - std::map::iterator it; + std::map::iterator it; for (it = clients.begin(); it != clients.end(); it++) { SrsStatisticClient* client = it->second; srs_freep(client); @@ -286,16 +286,16 @@ SrsStatistic* SrsStatistic::instance() return _instance; } -SrsStatisticVhost* SrsStatistic::find_vhost(int vid) +SrsStatisticVhost* SrsStatistic::find_vhost_by_id(std::string vid) { - std::map::iterator it; + std::map::iterator it; if ((it = vhosts.find(vid)) != vhosts.end()) { return it->second; } return NULL; } -SrsStatisticVhost* SrsStatistic::find_vhost(string name) +SrsStatisticVhost* SrsStatistic::find_vhost_by_name(string name) { if (rvhosts.empty()) { return NULL; @@ -308,18 +308,18 @@ SrsStatisticVhost* SrsStatistic::find_vhost(string name) return NULL; } -SrsStatisticStream* SrsStatistic::find_stream(int sid) +SrsStatisticStream* SrsStatistic::find_stream(std::string sid) { - std::map::iterator it; + std::map::iterator it; if ((it = streams.find(sid)) != streams.end()) { return it->second; } return NULL; } -SrsStatisticClient* SrsStatistic::find_client(int cid) +SrsStatisticClient* SrsStatistic::find_client(std::string cid) { - std::map::iterator it; + std::map::iterator it; if ((it = clients.find(cid)) != clients.end()) { return it->second; } @@ -372,7 +372,7 @@ srs_error_t SrsStatistic::on_video_frames(SrsRequest* req, int nb_frames) return err; } -void SrsStatistic::on_stream_publish(SrsRequest* req, int cid) +void SrsStatistic::on_stream_publish(SrsRequest* req, std::string cid) { SrsStatisticVhost* vhost = create_vhost(req); SrsStatisticStream* stream = create_stream(vhost, req); @@ -388,8 +388,8 @@ void SrsStatistic::on_stream_close(SrsRequest* req) // TODO: FIXME: Should fix https://github.com/ossrs/srs/issues/803 if (true) { - std::map::iterator it; - if ((it=streams.find(stream->id)) != streams.end()) { + std::map::iterator it; + if ((it = streams.find(stream->id)) != streams.end()) { streams.erase(it); } } @@ -397,13 +397,13 @@ void SrsStatistic::on_stream_close(SrsRequest* req) // TODO: FIXME: Should fix https://github.com/ossrs/srs/issues/803 if (true) { std::map::iterator it; - if ((it=rstreams.find(stream->url)) != rstreams.end()) { + if ((it = rstreams.find(stream->url)) != rstreams.end()) { rstreams.erase(it); } } } -srs_error_t SrsStatistic::on_client(int id, SrsRequest* req, SrsConnection* conn, SrsRtmpConnType type) +srs_error_t SrsStatistic::on_client(std::string id, SrsRequest* req, SrsConnection* conn, SrsRtmpConnType type) { srs_error_t err = srs_success; @@ -431,9 +431,9 @@ srs_error_t SrsStatistic::on_client(int id, SrsRequest* req, SrsConnection* conn return err; } -void SrsStatistic::on_disconnect(int id) +void SrsStatistic::on_disconnect(std::string id) { - std::map::iterator it; + std::map::iterator it; if ((it = clients.find(id)) == clients.end()) { return; } @@ -451,7 +451,7 @@ void SrsStatistic::on_disconnect(int id) void SrsStatistic::kbps_add_delta(SrsConnection* conn) { - int id = conn->srs_id(); + string id = srs_int2str(conn->srs_id()); if (clients.find(id) == clients.end()) { return; } @@ -473,14 +473,14 @@ SrsKbps* SrsStatistic::kbps_sample() { kbps->sample(); if (true) { - std::map::iterator it; + std::map::iterator it; for (it = vhosts.begin(); it != vhosts.end(); it++) { SrsStatisticVhost* vhost = it->second; vhost->kbps->sample(); } } if (true) { - std::map::iterator it; + std::map::iterator it; for (it = streams.begin(); it != streams.end(); it++) { SrsStatisticStream* stream = it->second; stream->kbps->sample(); @@ -490,7 +490,7 @@ SrsKbps* SrsStatistic::kbps_sample() return kbps; } -int64_t SrsStatistic::server_id() +std::string SrsStatistic::server_id() { return _server_id; } @@ -499,7 +499,7 @@ srs_error_t SrsStatistic::dumps_vhosts(SrsJsonArray* arr) { srs_error_t err = srs_success; - std::map::iterator it; + std::map::iterator it; for (it = vhosts.begin(); it != vhosts.end(); it++) { SrsStatisticVhost* vhost = it->second; @@ -518,7 +518,7 @@ srs_error_t SrsStatistic::dumps_streams(SrsJsonArray* arr) { srs_error_t err = srs_success; - std::map::iterator it; + std::map::iterator it; for (it = streams.begin(); it != streams.end(); it++) { SrsStatisticStream* stream = it->second; @@ -537,7 +537,7 @@ srs_error_t SrsStatistic::dumps_clients(SrsJsonArray* arr, int start, int count) { srs_error_t err = srs_success; - std::map::iterator it = clients.begin(); + std::map::iterator it = clients.begin(); for (int i = 0; i < start + count && it != clients.end(); it++, i++) { if (i < start) { continue; diff --git a/trunk/src/app/srs_app_statistic.hpp b/trunk/src/app/srs_app_statistic.hpp index fa8835849..fe8dbeeaa 100644 --- a/trunk/src/app/srs_app_statistic.hpp +++ b/trunk/src/app/srs_app_statistic.hpp @@ -43,7 +43,7 @@ class SrsJsonArray; struct SrsStatisticVhost { public: - int64_t id; + std::string id; std::string vhost; int nb_streams; int nb_clients; @@ -61,13 +61,13 @@ public: struct SrsStatisticStream { public: - int64_t id; + std::string id; SrsStatisticVhost* vhost; std::string app; std::string stream; std::string url; bool active; - int connection_cid; + std::string connection_cid; int nb_clients; uint64_t nb_frames; public: @@ -101,7 +101,7 @@ public: virtual srs_error_t dumps(SrsJsonObject* obj); public: // Publish the stream. - virtual void publish(int cid); + virtual void publish(std::string cid); // Close the stream. virtual void close(); }; @@ -113,7 +113,7 @@ public: SrsConnection* conn; SrsRequest* req; SrsRtmpConnType type; - int id; + std::string id; srs_utime_t create; public: SrsStatisticClient(); @@ -127,22 +127,22 @@ class SrsStatistic private: static SrsStatistic *_instance; // The id to identify the sever. - int64_t _server_id; + std::string _server_id; private: // The key: vhost id, value: vhost object. - std::map vhosts; + std::map vhosts; // The key: vhost url, value: vhost Object. // @remark a fast index for vhosts. std::map rvhosts; private: // The key: stream id, value: stream Object. - std::map streams; + std::map streams; // The key: stream url, value: stream Object. // @remark a fast index for streams. std::map rstreams; private: // The key: client id, value: stream object. - std::map clients; + std::map clients; // The server total kbps. SrsKbps* kbps; SrsWallClock* clk; @@ -152,10 +152,10 @@ private: public: static SrsStatistic* instance(); public: - virtual SrsStatisticVhost* find_vhost(int vid); - virtual SrsStatisticVhost* find_vhost(std::string name); - virtual SrsStatisticStream* find_stream(int sid); - virtual SrsStatisticClient* find_client(int cid); + virtual SrsStatisticVhost* find_vhost_by_id(std::string vid); + virtual SrsStatisticVhost* find_vhost_by_name(std::string name); + virtual SrsStatisticStream* find_stream(std::string sid); + virtual SrsStatisticClient* find_client(std::string cid); public: // When got video info for stream. virtual srs_error_t on_video_info(SrsRequest* req, SrsVideoCodecId vcodec, SrsAvcProfile avc_profile, @@ -169,7 +169,7 @@ public: // When publish stream. // @param req the request object of publish connection. // @param cid the cid of publish connection. - virtual void on_stream_publish(SrsRequest* req, int cid); + virtual void on_stream_publish(SrsRequest* req, std::string cid); // When close stream. virtual void on_stream_close(SrsRequest* req); public: @@ -178,12 +178,12 @@ public: // @param req, the client request object. // @param conn, the physical absract connection object. // @param type, the type of connection. - virtual srs_error_t on_client(int id, SrsRequest* req, SrsConnection* conn, SrsRtmpConnType type); + virtual srs_error_t on_client(std::string id, SrsRequest* req, SrsConnection* conn, SrsRtmpConnType type); // Client disconnect // @remark the on_disconnect always call, while the on_client is call when // only got the request object, so the client specified by id maybe not // exists in stat. - virtual void on_disconnect(int id); + virtual void on_disconnect(std::string id); // Sample the kbps, add delta bytes of conn. // Use kbps_sample() to get all result of kbps stat. // TODO: FIXME: the add delta must use ISrsKbpsDelta interface instead. @@ -194,7 +194,7 @@ public: public: // Get the server id, used to identify the server. // For example, when restart, the server id must changed. - virtual int64_t server_id(); + virtual std::string server_id(); // Dumps the vhosts to amf0 array. virtual srs_error_t dumps_vhosts(SrsJsonArray* arr); // Dumps the streams to amf0 array. diff --git a/trunk/src/core/srs_core_version3.hpp b/trunk/src/core/srs_core_version3.hpp index 281b3955d..a3d519d3f 100644 --- a/trunk/src/core/srs_core_version3.hpp +++ b/trunk/src/core/srs_core_version3.hpp @@ -24,6 +24,6 @@ #ifndef SRS_CORE_VERSION3_HPP #define SRS_CORE_VERSION3_HPP -#define SRS_VERSION3_REVISION 156 +#define SRS_VERSION3_REVISION 157 #endif diff --git a/trunk/src/protocol/srs_http_stack.hpp b/trunk/src/protocol/srs_http_stack.hpp index 9413c32af..7615587f4 100644 --- a/trunk/src/protocol/srs_http_stack.hpp +++ b/trunk/src/protocol/srs_http_stack.hpp @@ -485,12 +485,12 @@ public: virtual std::string path() = 0; virtual std::string query() = 0; virtual std::string ext() = 0; - // Get the RESTful id, + // Get the RESTful id, in string, // for example, pattern is /api/v1/streams, path is /api/v1/streams/100, // then the rest id is 100. // @param pattern the handler pattern which will serve the request. - // @return the REST id; -1 if not matched. - virtual int parse_rest_id(std::string pattern) = 0; + // @return the REST id; "" if not matched. + virtual std::string parse_rest_id(std::string pattern) = 0; public: // The left all data is chunked body, the infinite chunked mode, // which is chunked encoding without chunked header. diff --git a/trunk/src/service/srs_service_http_conn.cpp b/trunk/src/service/srs_service_http_conn.cpp index 47fba1caa..966eda19d 100644 --- a/trunk/src/service/srs_service_http_conn.cpp +++ b/trunk/src/service/srs_service_http_conn.cpp @@ -531,19 +531,19 @@ string SrsHttpMessage::ext() return _ext; } -int SrsHttpMessage::parse_rest_id(string pattern) +string SrsHttpMessage::parse_rest_id(string pattern) { string p = _uri->get_path(); if (p.length() <= pattern.length()) { - return -1; + return ""; } string id = p.substr((int)pattern.length()); if (!id.empty()) { - return ::atoi(id.c_str()); + return id; } - return -1; + return ""; } srs_error_t SrsHttpMessage::enter_infinite_chunked() diff --git a/trunk/src/service/srs_service_http_conn.hpp b/trunk/src/service/srs_service_http_conn.hpp index 2fbba8a04..fc49d10d5 100644 --- a/trunk/src/service/srs_service_http_conn.hpp +++ b/trunk/src/service/srs_service_http_conn.hpp @@ -172,7 +172,7 @@ public: virtual std::string query(); virtual std::string ext(); // Get the RESTful matched id. - virtual int parse_rest_id(std::string pattern); + virtual std::string parse_rest_id(std::string pattern); public: virtual srs_error_t enter_infinite_chunked(); public: diff --git a/trunk/src/utest/srs_utest_service.cpp b/trunk/src/utest/srs_utest_service.cpp index 87a1c49d1..531dac18e 100644 --- a/trunk/src/utest/srs_utest_service.cpp +++ b/trunk/src/utest/srs_utest_service.cpp @@ -529,7 +529,13 @@ VOID TEST(TCPServerTest, MessageConnection) if (true) { SrsHttpMessage m; HELPER_EXPECT_SUCCESS(m.set_url("http://127.0.0.1/v1/streams/100", false)); - EXPECT_EQ(100, m.parse_rest_id("/v1/streams/")); EXPECT_FALSE(m.is_jsonp()); + EXPECT_STREQ("100", m.parse_rest_id("/v1/streams/").c_str()); EXPECT_FALSE(m.is_jsonp()); + } + + if (true) { + SrsHttpMessage m; + HELPER_EXPECT_SUCCESS(m.set_url("http://127.0.0.1/v1/streams/abc", false)); + EXPECT_STREQ("abc", m.parse_rest_id("/v1/streams/").c_str()); EXPECT_FALSE(m.is_jsonp()); } }