From 47f3f4da5ca0e52a927442e15765471376f20610 Mon Sep 17 00:00:00 2001 From: winlin Date: Wed, 12 May 2021 21:50:57 +0800 Subject: [PATCH 1/2] SquashSRS4: fix bugs --- README.md | 2 ++ trunk/src/app/srs_app_http_stream.cpp | 9 +++++---- trunk/src/app/srs_app_http_stream.hpp | 4 ++-- trunk/src/app/srs_app_statistic.cpp | 6 +++++- trunk/src/core/srs_core_version3.hpp | 2 +- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 89777056c..cd836db78 100755 --- a/README.md +++ b/README.md @@ -276,6 +276,7 @@ The ports used by SRS: ## V3 changes +* v3.0, 2021-05-12, Fix [#2311][bug #2311], Copy the request for stat client. 3.0.162 * v3.0, 2021-04-28, [3.0 release5(3.0.161)][r3.0r5] released. 122750 lines. * v3.0, 2021-04-28, Upgrade players. 3.0.161 * v3.0, 2021-04-24, [3.0 release4(3.0.160)][r3.0r4] released. 122750 lines. @@ -1912,6 +1913,7 @@ Winlin [bug #1987]: https://github.com/ossrs/srs/issues/1987 [bug #1548]: https://github.com/ossrs/srs/issues/1548 [bug #1694]: https://github.com/ossrs/srs/issues/1694 +[bug #2311]: https://github.com/ossrs/srs/issues/2311 [bug #413]: https://github.com/ossrs/srs/issues/413 [bug #2091]: https://github.com/ossrs/srs/issues/2091 [bug #1342]: https://github.com/ossrs/srs/issues/1342 diff --git a/trunk/src/app/srs_app_http_stream.cpp b/trunk/src/app/srs_app_http_stream.cpp index 5b32215ec..04a3f7a5a 100755 --- a/trunk/src/app/srs_app_http_stream.cpp +++ b/trunk/src/app/srs_app_http_stream.cpp @@ -76,7 +76,7 @@ SrsBufferCache::~SrsBufferCache() srs_freep(req); } -srs_error_t SrsBufferCache::update(SrsSource* s, SrsRequest* r) +srs_error_t SrsBufferCache::update_auth(SrsSource* s, SrsRequest* r) { srs_freep(req); req = r->copy(); @@ -527,7 +527,7 @@ SrsLiveStream::~SrsLiveStream() srs_freep(req); } -srs_error_t SrsLiveStream::update(SrsSource* s, SrsRequest* r) +srs_error_t SrsLiveStream::update_auth(SrsSource* s, SrsRequest* r) { source = s; @@ -953,9 +953,10 @@ srs_error_t SrsHttpStreamServer::http_mount(SrsSource* s, SrsRequest* r) } srs_trace("http: mount flv stream for sid=%s, mount=%s", sid.c_str(), mount.c_str()); } else { + // The entry exists, we reuse it and update the request of stream and cache. entry = sflvs[sid]; - entry->stream->update(s, r); - entry->cache->update(s, r); + entry->stream->update_auth(s, r); + entry->cache->update_auth(s, r); } if (entry->stream) { diff --git a/trunk/src/app/srs_app_http_stream.hpp b/trunk/src/app/srs_app_http_stream.hpp index 1ba6c1e5a..82d62db39 100755 --- a/trunk/src/app/srs_app_http_stream.hpp +++ b/trunk/src/app/srs_app_http_stream.hpp @@ -46,7 +46,7 @@ private: public: SrsBufferCache(SrsSource* s, SrsRequest* r); virtual ~SrsBufferCache(); - virtual srs_error_t update(SrsSource* s, SrsRequest* r); + virtual srs_error_t update_auth(SrsSource* s, SrsRequest* r); public: virtual srs_error_t start(); virtual srs_error_t dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter); @@ -189,7 +189,7 @@ private: public: SrsLiveStream(SrsSource* s, SrsRequest* r, SrsBufferCache* c); virtual ~SrsLiveStream(); - virtual srs_error_t update(SrsSource* s, SrsRequest* r); + virtual srs_error_t update_auth(SrsSource* s, SrsRequest* r); public: virtual srs_error_t serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r); private: diff --git a/trunk/src/app/srs_app_statistic.cpp b/trunk/src/app/srs_app_statistic.cpp index e637e7199..4e58e9511 100644 --- a/trunk/src/app/srs_app_statistic.cpp +++ b/trunk/src/app/srs_app_statistic.cpp @@ -460,10 +460,14 @@ srs_error_t SrsStatistic::on_client(SrsContextId cid, SrsRequest* req, ISrsExpir // got client. client->conn = conn; - client->req = req; client->type = type; stream->nb_clients++; vhost->nb_clients++; + + // The req might be freed, in such as SrsLiveStream::update, so we must copy it. + // @see https://github.com/ossrs/srs/issues/2311 + srs_freep(client->req); + client->req = req->copy(); return err; } diff --git a/trunk/src/core/srs_core_version3.hpp b/trunk/src/core/srs_core_version3.hpp index dce1ed4d0..de1a7a578 100644 --- a/trunk/src/core/srs_core_version3.hpp +++ b/trunk/src/core/srs_core_version3.hpp @@ -26,6 +26,6 @@ #define VERSION_MAJOR 3 #define VERSION_MINOR 0 -#define SRS_VERSION3_REVISION 161 +#define SRS_VERSION3_REVISION 162 #endif From f711eb79ed692f450c0f166da38705598ae72caa Mon Sep 17 00:00:00 2001 From: winlin Date: Fri, 14 May 2021 08:26:07 +0800 Subject: [PATCH 2/2] SquashSRS4: Refine stat id. Fix SRT build bug --- README.md | 1 + trunk/configure | 2 +- 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 | 6 ++-- trunk/src/app/srs_app_source.cpp | 2 +- trunk/src/app/srs_app_statistic.cpp | 44 +++++++++------------------ trunk/src/app/srs_app_statistic.hpp | 19 ++++++------ trunk/src/core/srs_core_version4.hpp | 2 +- 9 files changed, 33 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index cd836db78..7788883f0 100755 --- a/README.md +++ b/README.md @@ -182,6 +182,7 @@ The ports used by SRS: ## V4 changes +* v4.0, 2021-05-14, Refine id and vid for statistic. 4.0.108 * v4.0, 2021-05-09, Refine tid for sdk and demos. 4.0.106 * v4.0, 2021-05-08, Refine shared fast timer. 4.0.105 * v4.0, 2021-05-08, Refine global or thread-local variables initialize. 4.0.104 diff --git a/trunk/configure b/trunk/configure index 945359482..4fbe83dfa 100755 --- a/trunk/configure +++ b/trunk/configure @@ -244,7 +244,7 @@ PROTOCOL_OBJS="${MODULE_OBJS[@]}" if [ $SRS_SRT = YES ]; then MODULE_ID="SRT" MODULE_DEPENDS=("CORE" "KERNEL" "PROTOCOL" "APP") - ModuleLibIncs=(${SRS_OBJS_DIR}) + ModuleLibIncs=(${SRS_OBJS_DIR} ${LibSSLRoot}) MODULE_FILES=("srt_server" "srt_handle" "srt_conn" "srt_to_rtmp" "ts_demux" "srt_data") SRT_INCS=${LibSRTRoot}; MODULE_DIR=${LibSRTRoot} . auto/modules.sh SRT_OBJS="${MODULE_OBJS[@]}" diff --git a/trunk/src/app/srs_app_http_stream.cpp b/trunk/src/app/srs_app_http_stream.cpp index 04a3f7a5a..eb4e372e9 100755 --- a/trunk/src/app/srs_app_http_stream.cpp +++ b/trunk/src/app/srs_app_http_stream.cpp @@ -606,7 +606,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_context->get_id().c_str(), 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 d6fe9d630..b2dac498e 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -534,7 +534,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_context->get_id().c_str(), 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 f33b081e4..b06612a6f 100644 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -1555,7 +1555,7 @@ void SrsServer::resample_kbps() // add delta of connection to server kbps., // for next sample() of server kbps can get the stat. - stat->kbps_add_delta(c->get_id(), conn); + stat->kbps_add_delta(c->get_id().c_str(), conn); } // TODO: FXME: support all other connections. @@ -1662,8 +1662,8 @@ void SrsServer::remove(ISrsResource* c) ISrsStartableConneciton* conn = dynamic_cast(c); SrsStatistic* stat = SrsStatistic::instance(); - stat->kbps_add_delta(c->get_id(), conn); - stat->on_disconnect(c->get_id()); + stat->kbps_add_delta(c->get_id().c_str(), conn); + stat->on_disconnect(c->get_id().c_str()); // 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 5f0ce3313..ed737f1cc 100755 --- a/trunk/src/app/srs_app_source.cpp +++ b/trunk/src/app/srs_app_source.cpp @@ -2518,7 +2518,7 @@ srs_error_t SrsSource::on_publish() } SrsStatistic* stat = SrsStatistic::instance(); - stat->on_stream_publish(req, _source_id); + stat->on_stream_publish(req, _source_id.c_str()); return err; } diff --git a/trunk/src/app/srs_app_statistic.cpp b/trunk/src/app/srs_app_statistic.cpp index 4e58e9511..37dbab504 100644 --- a/trunk/src/app/srs_app_statistic.cpp +++ b/trunk/src/app/srs_app_statistic.cpp @@ -34,23 +34,16 @@ using namespace std; #include #include #include +#include -string srs_generate_id() +string srs_generate_stat_vid() { - static int64_t srs_gvid = 0; - - if (srs_gvid == 0) { - srs_gvid = getpid(); - } - - string prefix = "vid"; - string rand_id = srs_int2str(srs_get_system_time() % 1000); - return prefix + "-" + srs_int2str(srs_gvid++) + "-" + rand_id; + return "vid-" + srs_random_str(7); } SrsStatisticVhost::SrsStatisticVhost() { - id = srs_generate_id(); + id = srs_generate_stat_vid(); clk = new SrsWallClock(); kbps = new SrsKbps(clk); @@ -101,7 +94,7 @@ srs_error_t SrsStatisticVhost::dumps(SrsJsonObject* obj) SrsStatisticStream::SrsStatisticStream() { - id = srs_generate_id(); + id = srs_generate_stat_vid(); vhost = NULL; active = false; @@ -156,7 +149,7 @@ srs_error_t SrsStatisticStream::dumps(SrsJsonObject* obj) obj->set("publish", publish); publish->set("active", SrsJsonAny::boolean(active)); - publish->set("cid", SrsJsonAny::str(connection_cid.c_str())); + publish->set("cid", SrsJsonAny::str(publisher_id.c_str())); if (!has_video) { obj->set("video", SrsJsonAny::null()); @@ -186,9 +179,9 @@ srs_error_t SrsStatisticStream::dumps(SrsJsonObject* obj) return err; } -void SrsStatisticStream::publish(SrsContextId cid) +void SrsStatisticStream::publish(std::string id) { - connection_cid = cid; + publisher_id = id; active = true; vhost->nb_streams++; @@ -214,6 +207,7 @@ SrsStatisticClient::SrsStatisticClient() SrsStatisticClient::~SrsStatisticClient() { + srs_freep(req); } srs_error_t SrsStatisticClient::dumps(SrsJsonObject* obj) @@ -260,7 +254,7 @@ SrsStatistic* SrsStatistic::_instance = NULL; SrsStatistic::SrsStatistic() { - _server_id = srs_generate_id(); + _server_id = srs_generate_stat_vid(); clk = new SrsWallClock(); kbps = new SrsKbps(clk); @@ -406,12 +400,12 @@ srs_error_t SrsStatistic::on_video_frames(SrsRequest* req, int nb_frames) return err; } -void SrsStatistic::on_stream_publish(SrsRequest* req, SrsContextId cid) +void SrsStatistic::on_stream_publish(SrsRequest* req, std::string publisher_id) { SrsStatisticVhost* vhost = create_vhost(req); SrsStatisticStream* stream = create_stream(vhost, req); - stream->publish(cid); + stream->publish(publisher_id); } void SrsStatistic::on_stream_close(SrsRequest* req) @@ -437,13 +431,10 @@ void SrsStatistic::on_stream_close(SrsRequest* req) } } -srs_error_t SrsStatistic::on_client(SrsContextId cid, SrsRequest* req, ISrsExpire* conn, SrsRtmpConnType type) +srs_error_t SrsStatistic::on_client(std::string id, SrsRequest* req, ISrsExpire* conn, SrsRtmpConnType type) { srs_error_t err = srs_success; - // TODO: FIXME: We should use UUID for client ID. - std::string id = cid.c_str(); - SrsStatisticVhost* vhost = create_vhost(req); SrsStatisticStream* stream = create_stream(vhost, req); @@ -472,11 +463,8 @@ srs_error_t SrsStatistic::on_client(SrsContextId cid, SrsRequest* req, ISrsExpir return err; } -void SrsStatistic::on_disconnect(const SrsContextId& cid) +void SrsStatistic::on_disconnect(std::string id) { - // TODO: FIXME: We should use UUID for client ID. - std::string id = cid.c_str(); - std::map::iterator it; if ((it = clients.find(id)) == clients.end()) { return; @@ -493,10 +481,8 @@ void SrsStatistic::on_disconnect(const SrsContextId& cid) vhost->nb_clients--; } -void SrsStatistic::kbps_add_delta(const SrsContextId& cid, ISrsKbpsDelta* delta) +void SrsStatistic::kbps_add_delta(std::string id, ISrsKbpsDelta* delta) { - // TODO: FIXME: Should not use context id as connection id. - std::string id = cid.c_str(); if (clients.find(id) == clients.end()) { return; } diff --git a/trunk/src/app/srs_app_statistic.hpp b/trunk/src/app/srs_app_statistic.hpp index 7ec90a2ac..1441cd57e 100644 --- a/trunk/src/app/srs_app_statistic.hpp +++ b/trunk/src/app/srs_app_statistic.hpp @@ -68,7 +68,8 @@ public: std::string stream; std::string url; bool active; - SrsContextId connection_cid; + // The publisher connection id. + std::string publisher_id; int nb_clients; uint64_t nb_frames; public: @@ -101,8 +102,8 @@ public: public: virtual srs_error_t dumps(SrsJsonObject* obj); public: - // Publish the stream. - virtual void publish(SrsContextId cid); + // Publish the stream, id is the publisher. + virtual void publish(std::string id); // Close the stream. virtual void close(); }; @@ -196,8 +197,8 @@ public: virtual srs_error_t on_video_frames(SrsRequest* req, int nb_frames); // 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, SrsContextId cid); + // @param publisher_id The id of publish connection. + virtual void on_stream_publish(SrsRequest* req, std::string publisher_id); // When close stream. virtual void on_stream_close(SrsRequest* req); public: @@ -206,17 +207,15 @@ public: // @param req, the client request object. // @param conn, the physical absract connection object. // @param type, the type of connection. - // TODO: FIXME: We should not use context id as client id. - virtual srs_error_t on_client(SrsContextId id, SrsRequest* req, ISrsExpire* conn, SrsRtmpConnType type); + virtual srs_error_t on_client(std::string id, SrsRequest* req, ISrsExpire* 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. - // TODO: FIXME: We should not use context id as client id. - virtual void on_disconnect(const SrsContextId& 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. - virtual void kbps_add_delta(const SrsContextId& cid, ISrsKbpsDelta* delta); + virtual void kbps_add_delta(std::string id, ISrsKbpsDelta* delta); // Calc the result for all kbps. // @return the server kbps. virtual SrsKbps* kbps_sample(); diff --git a/trunk/src/core/srs_core_version4.hpp b/trunk/src/core/srs_core_version4.hpp index e8f5529dc..35a083367 100644 --- a/trunk/src/core/srs_core_version4.hpp +++ b/trunk/src/core/srs_core_version4.hpp @@ -26,6 +26,6 @@ #define VERSION_MAJOR 4 #define VERSION_MINOR 0 -#define VERSION_REVISION 107 +#define VERSION_REVISION 108 #endif