From 48d55ce59b3258397794b1e4d1e07265401273b7 Mon Sep 17 00:00:00 2001 From: Haibo Chen Date: Fri, 3 Sep 2021 15:55:43 +0800 Subject: [PATCH 1/4] Enhance HLS: support http callback on_play/stop, support statistic --- trunk/src/app/srs_app_http_api.cpp | 9 +- trunk/src/app/srs_app_http_static.cpp | 171 +++++++++++++++++++++++++- trunk/src/app/srs_app_http_static.hpp | 15 ++- trunk/src/protocol/srs_http_stack.cpp | 16 ++- trunk/src/protocol/srs_http_stack.hpp | 11 ++ trunk/src/utest/srs_utest_http.cpp | 41 ++++++ trunk/src/utest/srs_utest_http.hpp | 4 + 7 files changed, 262 insertions(+), 5 deletions(-) diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index ded8c3c5bd..2392da45b0 100644 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -836,8 +836,13 @@ srs_error_t SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa return srs_api_response_code(w, r, ERROR_RTMP_CLIENT_NOT_FOUND); } - client->conn->expire(); - srs_warn("kickoff client id=%s ok", client_id.c_str()); + if (client->conn) { + client->conn->expire(); + srs_warn("kickoff client id=%s ok", client_id.c_str()); + } else { + srs_error("kickoff client id=%s error", client_id.c_str()); + return srs_api_response_code(w, r, SRS_CONSTS_HTTP_BadRequest); + } } else { return srs_go_http_error(w, SRS_CONSTS_HTTP_MethodNotAllowed); } diff --git a/trunk/src/app/srs_app_http_static.cpp b/trunk/src/app/srs_app_http_static.cpp index 89fe6136ff..ffbfb02cfc 100644 --- a/trunk/src/app/srs_app_http_static.cpp +++ b/trunk/src/app/srs_app_http_static.cpp @@ -33,15 +33,28 @@ using namespace std; #include #include #include +#include +#include +#include + +#define SRS_SECRET_IN_HLS "srs_secret" SrsVodStream::SrsVodStream(string root_dir) : SrsHttpFileServer(root_dir) { + _srs_hybrid->timer5s()->subscribe(this); } SrsVodStream::~SrsVodStream() { + _srs_hybrid->timer5s()->unsubscribe(this); + std::map::iterator it; + for (it = map_secret_req_.begin(); it != map_secret_req_.end(); ++it) { + srs_freep(it->second); + } + map_secret_req_.clear(); + map_secret_validity_.clear(); } - + srs_error_t SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, string fullpath, int offset) { srs_error_t err = srs_success; @@ -171,6 +184,162 @@ srs_error_t SrsVodStream::serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMe } return err; +} + +srs_error_t SrsVodStream::serve_m3u8_secret(ISrsHttpResponseWriter * w, ISrsHttpMessage * r, std::string fullpath) +{ + srs_error_t err = srs_success; + + SrsHttpMessage* hr = dynamic_cast(r); + srs_assert(hr); + + SrsRequest* req = hr->to_request(hr->host())->as_http(); + SrsAutoFree(SrsRequest, req); + + string secret = r->query_get(SRS_SECRET_IN_HLS); + if (!secret.empty() && secret_is_exist(secret)) { + alive(secret); + return SrsHttpFileServer::serve_m3u8_secret(w, r, fullpath); + } + + if ((err = http_hooks_on_play(req)) != srs_success) { + return srs_error_wrap(err, "HLS: http_hooks_on_play"); + } + + if (secret.empty()) { + // make sure unique + do { + secret = srs_random_str(8); + } while (secret_is_exist(secret)); + } + + std::string res = "#EXTM3U\r"; + res += "#EXT-X-STREAM-INF:BANDWIDTH=1,AVERAGE-BANDWIDTH=1\r"; + res += hr->path() + "?" + SRS_SECRET_IN_HLS + "=" + secret; + + int length = res.length(); + + w->header()->set_content_length(length); + w->header()->set_content_type("application/vnd.apple.mpegurl"); + w->write_header(SRS_CONSTS_HTTP_OK); + + if ((err = w->write((char*)res.c_str(), length)) != srs_success) { + return srs_error_wrap(err, "write bytes=%d", length); + } + + if ((err = w->final_request()) != srs_success) { + return srs_error_wrap(err, "final request"); + } + + // update the statistic when source disconveried. + SrsStatistic* stat = SrsStatistic::instance(); + if ((err = stat->on_client(secret, req, NULL, SrsRtmpConnPlay)) != srs_success) { + return srs_error_wrap(err, "stat on client"); + } + + // save req for on_disconnect when timeout + map_secret_req_.insert(make_pair(secret, req->copy())); + alive(secret); + + return err; +} + +bool SrsVodStream::secret_is_exist(std::string secret) +{ + return (map_secret_validity_.find(secret) != map_secret_validity_.end()); +} + +void SrsVodStream::alive(std::string secret) +{ + map_secret_validity_[secret] = srs_get_system_time(); +} + +srs_error_t SrsVodStream::http_hooks_on_play(SrsRequest* req) +{ + srs_error_t err = srs_success; + + if (!_srs_config->get_vhost_http_hooks_enabled(req->vhost)) { + return err; + } + + // the http hooks will cause context switch, + // so we must copy all hooks for the on_connect may freed. + // @see https://github.com/ossrs/srs/issues/475 + vector hooks; + + if (true) { + SrsConfDirective* conf = _srs_config->get_vhost_on_play(req->vhost); + + if (!conf) { + return err; + } + + hooks = conf->args; + } + + for (int i = 0; i < (int)hooks.size(); i++) { + std::string url = hooks.at(i); + if ((err = SrsHttpHooks::on_play(url, req)) != srs_success) { + return srs_error_wrap(err, "http on_play %s", url.c_str()); + } + } + + return err; +} + +void SrsVodStream::http_hooks_on_stop(SrsRequest* req) +{ + if (!_srs_config->get_vhost_http_hooks_enabled(req->vhost)) { + return; + } + + // the http hooks will cause context switch, + // so we must copy all hooks for the on_connect may freed. + // @see https://github.com/ossrs/srs/issues/475 + vector hooks; + + if (true) { + SrsConfDirective* conf = _srs_config->get_vhost_on_stop(req->vhost); + + if (!conf) { + srs_info("ignore the empty http callback: on_stop"); + return; + } + + hooks = conf->args; + } + + for (int i = 0; i < (int)hooks.size(); i++) { + std::string url = hooks.at(i); + SrsHttpHooks::on_stop(url, req); + } + + return; +} + +srs_error_t SrsVodStream::on_timer(srs_utime_t interval) +{ + srs_error_t err = srs_success; + + std::map::iterator it; + for (it = map_secret_validity_.begin(); it != map_secret_validity_.end(); ++it) { + string secret = it->first; + SrsRequest* req = map_secret_req_[secret]; + srs_utime_t hls_window = _srs_config->get_hls_window(req->vhost); + if (it->second + (2 * hls_window) < srs_get_system_time()) { + http_hooks_on_stop(req); + srs_freep(req); + map_secret_req_.erase(secret); + + SrsStatistic* stat = SrsStatistic::instance(); + stat->on_disconnect(secret); + map_secret_validity_.erase(it); + + break; + } + } + + return err; } SrsHttpStaticServer::SrsHttpStaticServer(SrsServer* svr) diff --git a/trunk/src/app/srs_app_http_static.hpp b/trunk/src/app/srs_app_http_static.hpp index 5215ef2355..36f22e704f 100644 --- a/trunk/src/app/srs_app_http_static.hpp +++ b/trunk/src/app/srs_app_http_static.hpp @@ -15,14 +15,27 @@ // For example, http://server/file.flv?start=10240 // server will write flv header and sequence header, // then seek(10240) and response flv tag data. -class SrsVodStream : public SrsHttpFileServer +class SrsVodStream : public SrsHttpFileServer, public ISrsFastTimer { +private: + // The period of validity of the secret + std::map map_secret_validity_; + std::map map_secret_req_; public: SrsVodStream(std::string root_dir); virtual ~SrsVodStream(); protected: virtual srs_error_t serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath, int offset); virtual srs_error_t serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath, int start, int end); + virtual srs_error_t serve_m3u8_secret(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath); +private: + virtual bool secret_is_exist(std::string secret); + virtual void alive(std::string secret); + virtual srs_error_t http_hooks_on_play(SrsRequest* req); + virtual void http_hooks_on_stop(SrsRequest* req); +// interface ISrsFastTimer +private: + srs_error_t on_timer(srs_utime_t interval); }; // The http static server instance, diff --git a/trunk/src/protocol/srs_http_stack.cpp b/trunk/src/protocol/srs_http_stack.cpp index 210e014d68..a013389e0d 100644 --- a/trunk/src/protocol/srs_http_stack.cpp +++ b/trunk/src/protocol/srs_http_stack.cpp @@ -364,7 +364,7 @@ srs_error_t SrsHttpFileServer::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMes string upath = r->path(); string fullpath = srs_http_fs_fullpath(dir, entry->pattern, upath); - + // stat current dir, if exists, return error. if (!_srs_path_exists(fullpath)) { srs_warn("http miss file=%s, pattern=%s, upath=%s", @@ -380,6 +380,8 @@ srs_error_t SrsHttpFileServer::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMes return serve_flv_file(w, r, fullpath); } else if (srs_string_ends_with(fullpath, ".mp4")) { return serve_mp4_file(w, r, fullpath); + } else if (srs_string_ends_with(upath, ".m3u8")) { + return serve_m3u8_file(w, r, fullpath); } // serve common static file. @@ -522,6 +524,11 @@ srs_error_t SrsHttpFileServer::serve_mp4_file(ISrsHttpResponseWriter* w, ISrsHtt } return serve_mp4_stream(w, r, fullpath, start, end); +} + +srs_error_t SrsHttpFileServer::serve_m3u8_file(ISrsHttpResponseWriter * w, ISrsHttpMessage * r, std::string fullpath) +{ + return serve_m3u8_secret(w, r, fullpath); } srs_error_t SrsHttpFileServer::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, string fullpath, int offset) @@ -536,6 +543,13 @@ srs_error_t SrsHttpFileServer::serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsH // @remark For common http file server, we don't support stream request, please use SrsVodStream instead. // TODO: FIXME: Support range in header https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Range_requests return serve_file(w, r, fullpath); +} + +srs_error_t SrsHttpFileServer::serve_m3u8_secret(ISrsHttpResponseWriter * w, ISrsHttpMessage * r, std::string fullpath) +{ + // @remark For common http file server, we don't support stream request, please use SrsVodStream instead. + // TODO: FIXME: Support range in header https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Range_requests + return serve_file(w, r, fullpath); } srs_error_t SrsHttpFileServer::copy(ISrsHttpResponseWriter* w, SrsFileReader* fs, ISrsHttpMessage* r, int size) diff --git a/trunk/src/protocol/srs_http_stack.hpp b/trunk/src/protocol/srs_http_stack.hpp index e128e22526..6d4d4147d1 100644 --- a/trunk/src/protocol/srs_http_stack.hpp +++ b/trunk/src/protocol/srs_http_stack.hpp @@ -285,6 +285,7 @@ class SrsHttpFileServer : public ISrsHttpHandler virtual srs_error_t serve_file(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath); virtual srs_error_t serve_flv_file(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath); virtual srs_error_t serve_mp4_file(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath); + virtual srs_error_t serve_m3u8_file(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath); protected: // When access flv file with x.flv?start=xxx virtual srs_error_t serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath, int offset); @@ -293,6 +294,16 @@ class SrsHttpFileServer : public ISrsHttpHandler // @param end the end offset in bytes. -1 to end of file. // @remark response data in [start, end]. virtual srs_error_t serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath, int start, int end); + // For HLS protocol. + // When the request url, like as "http://127.0.0.1:8080/live/livestream.m3u8", + // returns the response like as "http://127.0.0.1:8080/live/livestream.m3u8?srs_secret=12345678" . + // SRS use "srs_secret" to keep track of subsequent requests that is short-connection. + // Remark 1: + // Fill the parameter "srs_secret" by yourself in the first request is allowed, SRS will use it. + // And MUST make sure it is unique. + // Remark 2: + // If use two same "srs_secret" in different requests, SRS cannot detect so that they will be treated as one. + virtual srs_error_t serve_m3u8_secret(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath); protected: // Copy the fs to response writer in size bytes. virtual srs_error_t copy(ISrsHttpResponseWriter* w, SrsFileReader* fs, ISrsHttpMessage* r, int size); diff --git a/trunk/src/utest/srs_utest_http.cpp b/trunk/src/utest/srs_utest_http.cpp index de0aa0a37a..84473b54ac 100644 --- a/trunk/src/utest/srs_utest_http.cpp +++ b/trunk/src/utest/srs_utest_http.cpp @@ -143,6 +143,11 @@ string mock_http_response2(int status, string content) return ss.str(); } +bool is_string_contain(string substr, string str) +{ + return (string::npos != str.find(substr)); +} + class MockFileReaderFactory : public ISrsFileReaderFactory { public: @@ -1183,6 +1188,42 @@ VOID TEST(ProtocolHTTPTest, VodStreamHandlers) HELPER_ASSERT_SUCCESS(h.serve_http(&w, &r)); __MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w); } + + // should return "srs_secret" + if (true) { + SrsHttpMuxEntry e; + e.pattern = "/"; + + SrsVodStream h("/tmp"); + h.set_fs_factory(new MockFileReaderFactory("Hello, world!")); + h.set_path_check(_mock_srs_path_always_exists); + h.entry = &e; + + MockResponseWriter w; + SrsHttpMessage r(NULL, NULL); + HELPER_ASSERT_SUCCESS(r.set_url("/index.m3u8", false)); + + HELPER_ASSERT_SUCCESS(h.serve_http(&w, &r)); + __MOCK_HTTP_EXPECT_STRCT(200, "index.m3u8?srs_secret=", w); + } + + // should return "srs_secret" + if (true) { + SrsHttpMuxEntry e; + e.pattern = "/"; + + SrsVodStream h("/tmp"); + h.set_fs_factory(new MockFileReaderFactory("Hello, world!")); + h.set_path_check(_mock_srs_path_always_exists); + h.entry = &e; + + MockResponseWriter w; + SrsHttpMessage r(NULL, NULL); + HELPER_ASSERT_SUCCESS(r.set_url("/index.m3u8?srs_secret=123456", false)); + + HELPER_ASSERT_SUCCESS(h.serve_http(&w, &r)); + __MOCK_HTTP_EXPECT_STRCT(200, "index.m3u8?srs_secret=123456", w); + } } VOID TEST(ProtocolHTTPTest, BasicHandlers) diff --git a/trunk/src/utest/srs_utest_http.hpp b/trunk/src/utest/srs_utest_http.hpp index 945fad1892..e8172b1c00 100644 --- a/trunk/src/utest/srs_utest_http.hpp +++ b/trunk/src/utest/srs_utest_http.hpp @@ -39,6 +39,7 @@ class MockResponseWriter : public ISrsHttpResponseWriter, public ISrsHttpHeaderF string mock_http_response(int status, string content); string mock_http_response2(int status, string content); +bool is_string_contain(string substr, string str); #define __MOCK_HTTP_EXPECT_STREQ(status, text, w) \ EXPECT_STREQ(mock_http_response(status, text).c_str(), HELPER_BUFFER2STR(&w.io.out_buffer).c_str()) @@ -46,5 +47,8 @@ string mock_http_response2(int status, string content); #define __MOCK_HTTP_EXPECT_STREQ2(status, text, w) \ EXPECT_STREQ(mock_http_response2(status, text).c_str(), HELPER_BUFFER2STR(&w.io.out_buffer).c_str()) +#define __MOCK_HTTP_EXPECT_STRCT(status, text, w) \ + EXPECT_PRED2(is_string_contain, text, HELPER_BUFFER2STR(&w.io.out_buffer).c_str()) + #endif From edce8541bea9e1c6a1efdb7cac51de9bc42ba77e Mon Sep 17 00:00:00 2001 From: Haibo Chen Date: Wed, 15 Sep 2021 14:20:38 +0800 Subject: [PATCH 2/4] make code readable --- trunk/src/app/srs_app_http_static.cpp | 72 +++++++++++++++------------ trunk/src/app/srs_app_http_static.hpp | 15 ++++-- trunk/src/protocol/srs_http_stack.cpp | 4 +- trunk/src/protocol/srs_http_stack.hpp | 10 ++-- trunk/src/utest/srs_utest_http.cpp | 10 ++-- 5 files changed, 63 insertions(+), 48 deletions(-) diff --git a/trunk/src/app/srs_app_http_static.cpp b/trunk/src/app/srs_app_http_static.cpp index ffbfb02cfc..9cb0ad6007 100644 --- a/trunk/src/app/srs_app_http_static.cpp +++ b/trunk/src/app/srs_app_http_static.cpp @@ -37,7 +37,7 @@ using namespace std; #include #include -#define SRS_SECRET_IN_HLS "srs_secret" +#define SRS_CONTEXT_IN_HLS "hls_ctx" SrsVodStream::SrsVodStream(string root_dir) : SrsHttpFileServer(root_dir) { @@ -47,12 +47,11 @@ SrsVodStream::SrsVodStream(string root_dir) : SrsHttpFileServer(root_dir) SrsVodStream::~SrsVodStream() { _srs_hybrid->timer5s()->unsubscribe(this); - std::map::iterator it; - for (it = map_secret_req_.begin(); it != map_secret_req_.end(); ++it) { - srs_freep(it->second); + std::map::iterator it; + for (it = map_ctx_info_.begin(); it != map_ctx_info_.end(); ++it) { + srs_freep(it->second.req); } - map_secret_req_.clear(); - map_secret_validity_.clear(); + map_ctx_info_.clear(); } srs_error_t SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, string fullpath, int offset) @@ -186,7 +185,7 @@ srs_error_t SrsVodStream::serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMe return err; } -srs_error_t SrsVodStream::serve_m3u8_secret(ISrsHttpResponseWriter * w, ISrsHttpMessage * r, std::string fullpath) +srs_error_t SrsVodStream::serve_m3u8_ctx(ISrsHttpResponseWriter * w, ISrsHttpMessage * r, std::string fullpath) { srs_error_t err = srs_success; @@ -196,27 +195,33 @@ srs_error_t SrsVodStream::serve_m3u8_secret(ISrsHttpResponseWriter * w, ISrsHttp SrsRequest* req = hr->to_request(hr->host())->as_http(); SrsAutoFree(SrsRequest, req); - string secret = r->query_get(SRS_SECRET_IN_HLS); - if (!secret.empty() && secret_is_exist(secret)) { - alive(secret); - return SrsHttpFileServer::serve_m3u8_secret(w, r, fullpath); + string ctx = hr->query_get(SRS_CONTEXT_IN_HLS); + if (!ctx.empty() && ctx_is_exist(ctx)) { + alive(ctx, NULL); + return SrsHttpFileServer::serve_m3u8_ctx(w, r, fullpath); } if ((err = http_hooks_on_play(req)) != srs_success) { return srs_error_wrap(err, "HLS: http_hooks_on_play"); } - if (secret.empty()) { + if (ctx.empty()) { // make sure unique do { - secret = srs_random_str(8); - } while (secret_is_exist(secret)); + ctx = srs_random_str(8); // the same as cid + } while (ctx_is_exist(ctx)); } - std::string res = "#EXTM3U\r"; - res += "#EXT-X-STREAM-INF:BANDWIDTH=1,AVERAGE-BANDWIDTH=1\r"; - res += hr->path() + "?" + SRS_SECRET_IN_HLS + "=" + secret; + std::stringstream ss; + ss << "#EXTM3U" << SRS_CONSTS_LF; + ss << "#EXT-X-STREAM-INF:BANDWIDTH=1,AVERAGE-BANDWIDTH=1" << SRS_CONSTS_LF; + ss << hr->path() << "?" << SRS_CONTEXT_IN_HLS << "=" << ctx; + if (!hr->query().empty() && !srs_string_contains(hr->query(), "hls_ctx")) + { + ss << "&" << hr->query(); + } + std::string res = ss.str(); int length = res.length(); w->header()->set_content_length(length); @@ -233,25 +238,31 @@ srs_error_t SrsVodStream::serve_m3u8_secret(ISrsHttpResponseWriter * w, ISrsHttp // update the statistic when source disconveried. SrsStatistic* stat = SrsStatistic::instance(); - if ((err = stat->on_client(secret, req, NULL, SrsRtmpConnPlay)) != srs_success) { + if ((err = stat->on_client(ctx, req, NULL, SrsRtmpConnPlay)) != srs_success) { return srs_error_wrap(err, "stat on client"); } - // save req for on_disconnect when timeout - map_secret_req_.insert(make_pair(secret, req->copy())); - alive(secret); + alive(ctx, req->copy()); return err; } -bool SrsVodStream::secret_is_exist(std::string secret) +bool SrsVodStream::ctx_is_exist(std::string secret) { - return (map_secret_validity_.find(secret) != map_secret_validity_.end()); + return (map_ctx_info_.find(secret) != map_ctx_info_.end()); } -void SrsVodStream::alive(std::string secret) +void SrsVodStream::alive(std::string secret, SrsRequest* req) { - map_secret_validity_[secret] = srs_get_system_time(); + std::map::iterator it; + if ((it = map_ctx_info_.find(secret)) != map_ctx_info_.end()) { + it->second.request_time = srs_get_system_time(); + } else { + SrsM3u8CtxInfo info; + info.req = req; + info.request_time = srs_get_system_time(); + map_ctx_info_.insert(make_pair(secret, info)); + } } srs_error_t SrsVodStream::http_hooks_on_play(SrsRequest* req) @@ -321,19 +332,18 @@ srs_error_t SrsVodStream::on_timer(srs_utime_t interval) { srs_error_t err = srs_success; - std::map::iterator it; - for (it = map_secret_validity_.begin(); it != map_secret_validity_.end(); ++it) { + std::map::iterator it; + for (it = map_ctx_info_.begin(); it != map_ctx_info_.end(); ++it) { string secret = it->first; - SrsRequest* req = map_secret_req_[secret]; + SrsRequest* req = it->second.req; srs_utime_t hls_window = _srs_config->get_hls_window(req->vhost); - if (it->second + (2 * hls_window) < srs_get_system_time()) { + if (it->second.request_time + (2 * hls_window) < srs_get_system_time()) { http_hooks_on_stop(req); srs_freep(req); - map_secret_req_.erase(secret); SrsStatistic* stat = SrsStatistic::instance(); stat->on_disconnect(secret); - map_secret_validity_.erase(it); + map_ctx_info_.erase(it); break; } diff --git a/trunk/src/app/srs_app_http_static.hpp b/trunk/src/app/srs_app_http_static.hpp index 36f22e704f..8e0603d8d2 100644 --- a/trunk/src/app/srs_app_http_static.hpp +++ b/trunk/src/app/srs_app_http_static.hpp @@ -11,6 +11,12 @@ #include +struct SrsM3u8CtxInfo +{ + srs_utime_t request_time; + SrsRequest* req; +}; + // The flv vod stream supports flv?start=offset-bytes. // For example, http://server/file.flv?start=10240 // server will write flv header and sequence header, @@ -19,18 +25,17 @@ class SrsVodStream : public SrsHttpFileServer, public ISrsFastTimer { private: // The period of validity of the secret - std::map map_secret_validity_; - std::map map_secret_req_; + std::map map_ctx_info_; public: SrsVodStream(std::string root_dir); virtual ~SrsVodStream(); protected: virtual srs_error_t serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath, int offset); virtual srs_error_t serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath, int start, int end); - virtual srs_error_t serve_m3u8_secret(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath); + virtual srs_error_t serve_m3u8_ctx(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath); private: - virtual bool secret_is_exist(std::string secret); - virtual void alive(std::string secret); + virtual bool ctx_is_exist(std::string secret); + virtual void alive(std::string secret, SrsRequest* req); virtual srs_error_t http_hooks_on_play(SrsRequest* req); virtual void http_hooks_on_stop(SrsRequest* req); // interface ISrsFastTimer diff --git a/trunk/src/protocol/srs_http_stack.cpp b/trunk/src/protocol/srs_http_stack.cpp index a013389e0d..a9af10c087 100644 --- a/trunk/src/protocol/srs_http_stack.cpp +++ b/trunk/src/protocol/srs_http_stack.cpp @@ -528,7 +528,7 @@ srs_error_t SrsHttpFileServer::serve_mp4_file(ISrsHttpResponseWriter* w, ISrsHtt srs_error_t SrsHttpFileServer::serve_m3u8_file(ISrsHttpResponseWriter * w, ISrsHttpMessage * r, std::string fullpath) { - return serve_m3u8_secret(w, r, fullpath); + return serve_m3u8_ctx(w, r, fullpath); } srs_error_t SrsHttpFileServer::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, string fullpath, int offset) @@ -545,7 +545,7 @@ srs_error_t SrsHttpFileServer::serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsH return serve_file(w, r, fullpath); } -srs_error_t SrsHttpFileServer::serve_m3u8_secret(ISrsHttpResponseWriter * w, ISrsHttpMessage * r, std::string fullpath) +srs_error_t SrsHttpFileServer::serve_m3u8_ctx(ISrsHttpResponseWriter * w, ISrsHttpMessage * r, std::string fullpath) { // @remark For common http file server, we don't support stream request, please use SrsVodStream instead. // TODO: FIXME: Support range in header https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Range_requests diff --git a/trunk/src/protocol/srs_http_stack.hpp b/trunk/src/protocol/srs_http_stack.hpp index 6d4d4147d1..c676812199 100644 --- a/trunk/src/protocol/srs_http_stack.hpp +++ b/trunk/src/protocol/srs_http_stack.hpp @@ -296,14 +296,14 @@ class SrsHttpFileServer : public ISrsHttpHandler virtual srs_error_t serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath, int start, int end); // For HLS protocol. // When the request url, like as "http://127.0.0.1:8080/live/livestream.m3u8", - // returns the response like as "http://127.0.0.1:8080/live/livestream.m3u8?srs_secret=12345678" . - // SRS use "srs_secret" to keep track of subsequent requests that is short-connection. + // returns the response like as "http://127.0.0.1:8080/live/livestream.m3u8?hls_ctx=12345678" . + // SRS use "hls_ctx" to keep track of subsequent requests that is short-connection. // Remark 1: - // Fill the parameter "srs_secret" by yourself in the first request is allowed, SRS will use it. + // Fill the parameter "hls_ctx" by yourself in the first request is allowed, SRS will use it. // And MUST make sure it is unique. // Remark 2: - // If use two same "srs_secret" in different requests, SRS cannot detect so that they will be treated as one. - virtual srs_error_t serve_m3u8_secret(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath); + // If use two same "hls_ctx" in different requests, SRS cannot detect so that they will be treated as one. + virtual srs_error_t serve_m3u8_ctx(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath); protected: // Copy the fs to response writer in size bytes. virtual srs_error_t copy(ISrsHttpResponseWriter* w, SrsFileReader* fs, ISrsHttpMessage* r, int size); diff --git a/trunk/src/utest/srs_utest_http.cpp b/trunk/src/utest/srs_utest_http.cpp index 84473b54ac..5b6d70b349 100644 --- a/trunk/src/utest/srs_utest_http.cpp +++ b/trunk/src/utest/srs_utest_http.cpp @@ -1189,7 +1189,7 @@ VOID TEST(ProtocolHTTPTest, VodStreamHandlers) __MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w); } - // should return "srs_secret" + // should return "hls_ctx" if (true) { SrsHttpMuxEntry e; e.pattern = "/"; @@ -1204,10 +1204,10 @@ VOID TEST(ProtocolHTTPTest, VodStreamHandlers) HELPER_ASSERT_SUCCESS(r.set_url("/index.m3u8", false)); HELPER_ASSERT_SUCCESS(h.serve_http(&w, &r)); - __MOCK_HTTP_EXPECT_STRCT(200, "index.m3u8?srs_secret=", w); + __MOCK_HTTP_EXPECT_STRCT(200, "index.m3u8?hls_ctx=", w); } - // should return "srs_secret" + // should return "hls_ctx" if (true) { SrsHttpMuxEntry e; e.pattern = "/"; @@ -1219,10 +1219,10 @@ VOID TEST(ProtocolHTTPTest, VodStreamHandlers) MockResponseWriter w; SrsHttpMessage r(NULL, NULL); - HELPER_ASSERT_SUCCESS(r.set_url("/index.m3u8?srs_secret=123456", false)); + HELPER_ASSERT_SUCCESS(r.set_url("/index.m3u8?hls_ctx=123456", false)); HELPER_ASSERT_SUCCESS(h.serve_http(&w, &r)); - __MOCK_HTTP_EXPECT_STRCT(200, "index.m3u8?srs_secret=123456", w); + __MOCK_HTTP_EXPECT_STRCT(200, "index.m3u8?hls_ctx=123456", w); } } From 1789f549f55801b0af5ca1c757a06909e5405a9c Mon Sep 17 00:00:00 2001 From: Haibo Chen Date: Wed, 15 Sep 2021 15:10:09 +0800 Subject: [PATCH 3/4] make code readable --- trunk/src/app/srs_app_http_static.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trunk/src/app/srs_app_http_static.cpp b/trunk/src/app/srs_app_http_static.cpp index 9cb0ad6007..6c57a19be6 100644 --- a/trunk/src/app/srs_app_http_static.cpp +++ b/trunk/src/app/srs_app_http_static.cpp @@ -216,7 +216,7 @@ srs_error_t SrsVodStream::serve_m3u8_ctx(ISrsHttpResponseWriter * w, ISrsHttpMes ss << "#EXTM3U" << SRS_CONSTS_LF; ss << "#EXT-X-STREAM-INF:BANDWIDTH=1,AVERAGE-BANDWIDTH=1" << SRS_CONSTS_LF; ss << hr->path() << "?" << SRS_CONTEXT_IN_HLS << "=" << ctx; - if (!hr->query().empty() && !srs_string_contains(hr->query(), "hls_ctx")) + if (!hr->query().empty() && hr->query_get(SRS_CONTEXT_IN_HLS).empty()) { ss << "&" << hr->query(); } From a00554a7693a9f5ede6ae3a7d578108deb8e4149 Mon Sep 17 00:00:00 2001 From: Haibo Chen Date: Wed, 15 Sep 2021 15:16:48 +0800 Subject: [PATCH 4/4] rename secret --- trunk/src/app/srs_app_http_static.cpp | 18 +++++++++--------- trunk/src/app/srs_app_http_static.hpp | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/trunk/src/app/srs_app_http_static.cpp b/trunk/src/app/srs_app_http_static.cpp index 6c57a19be6..dd8b4e9696 100644 --- a/trunk/src/app/srs_app_http_static.cpp +++ b/trunk/src/app/srs_app_http_static.cpp @@ -236,32 +236,32 @@ srs_error_t SrsVodStream::serve_m3u8_ctx(ISrsHttpResponseWriter * w, ISrsHttpMes return srs_error_wrap(err, "final request"); } + alive(ctx, req->copy()); + // update the statistic when source disconveried. SrsStatistic* stat = SrsStatistic::instance(); if ((err = stat->on_client(ctx, req, NULL, SrsRtmpConnPlay)) != srs_success) { return srs_error_wrap(err, "stat on client"); } - alive(ctx, req->copy()); - return err; } -bool SrsVodStream::ctx_is_exist(std::string secret) +bool SrsVodStream::ctx_is_exist(std::string ctx) { - return (map_ctx_info_.find(secret) != map_ctx_info_.end()); + return (map_ctx_info_.find(ctx) != map_ctx_info_.end()); } -void SrsVodStream::alive(std::string secret, SrsRequest* req) +void SrsVodStream::alive(std::string ctx, SrsRequest* req) { std::map::iterator it; - if ((it = map_ctx_info_.find(secret)) != map_ctx_info_.end()) { + if ((it = map_ctx_info_.find(ctx)) != map_ctx_info_.end()) { it->second.request_time = srs_get_system_time(); } else { SrsM3u8CtxInfo info; info.req = req; info.request_time = srs_get_system_time(); - map_ctx_info_.insert(make_pair(secret, info)); + map_ctx_info_.insert(make_pair(ctx, info)); } } @@ -334,7 +334,7 @@ srs_error_t SrsVodStream::on_timer(srs_utime_t interval) std::map::iterator it; for (it = map_ctx_info_.begin(); it != map_ctx_info_.end(); ++it) { - string secret = it->first; + string ctx = it->first; SrsRequest* req = it->second.req; srs_utime_t hls_window = _srs_config->get_hls_window(req->vhost); if (it->second.request_time + (2 * hls_window) < srs_get_system_time()) { @@ -342,7 +342,7 @@ srs_error_t SrsVodStream::on_timer(srs_utime_t interval) srs_freep(req); SrsStatistic* stat = SrsStatistic::instance(); - stat->on_disconnect(secret); + stat->on_disconnect(ctx); map_ctx_info_.erase(it); break; diff --git a/trunk/src/app/srs_app_http_static.hpp b/trunk/src/app/srs_app_http_static.hpp index 8e0603d8d2..8af98cdaa5 100644 --- a/trunk/src/app/srs_app_http_static.hpp +++ b/trunk/src/app/srs_app_http_static.hpp @@ -24,7 +24,7 @@ struct SrsM3u8CtxInfo class SrsVodStream : public SrsHttpFileServer, public ISrsFastTimer { private: - // The period of validity of the secret + // The period of validity of the ctx std::map map_ctx_info_; public: SrsVodStream(std::string root_dir); @@ -34,8 +34,8 @@ class SrsVodStream : public SrsHttpFileServer, public ISrsFastTimer virtual srs_error_t serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath, int start, int end); virtual srs_error_t serve_m3u8_ctx(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath); private: - virtual bool ctx_is_exist(std::string secret); - virtual void alive(std::string secret, SrsRequest* req); + virtual bool ctx_is_exist(std::string ctx); + virtual void alive(std::string ctx, SrsRequest* req); virtual srs_error_t http_hooks_on_play(SrsRequest* req); virtual void http_hooks_on_stop(SrsRequest* req); // interface ISrsFastTimer