From d39995cc31524d844e896ba687f91c451034243d Mon Sep 17 00:00:00 2001 From: winlin Date: Sat, 27 Aug 2022 19:27:08 +0800 Subject: [PATCH] HLS: Enable hls_ctx by default. --- trunk/conf/full.conf | 21 +++++++++++++-------- trunk/src/app/srs_app_config.cpp | 2 +- trunk/src/app/srs_app_http_static.cpp | 9 +++++---- trunk/src/utest/srs_utest_http.cpp | 8 ++++---- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index c42726e60..b877241d8 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -1371,6 +1371,19 @@ vhost hls.srs.com { # if off, do not write hls(ts and m3u8) when publish. # default: off enabled on; + + # Whether enable hls_ctx for HLS streaming, for which we create a "fake" connection for HTTP API and callback. + # For each HLS streaming session, we use a child m3u8 with a session identified by query "hls_ctx", it simply + # work as the session id. + # Once the HLS streaming session is created, we will cleanup it when timeout in 2*hls_window seconds. So it + # takes a long time period to identify the timeout. + # Now we got a HLS stremaing session, just like RTMP/WebRTC/HTTP-FLV streaming, we're able to stat the session + # as a "fake" connection, do HTTP callback when start playing the HLS streaming. You're able to do querying and + # authentication. + # Note that it will make NGINX edge cache always missed, so never enable HLS streaming if use NGINX edges. + # Default: on + hls_ctx on; + # the hls fragment in seconds, the duration of a piece of ts. # default: 10 hls_fragment 10; @@ -1499,14 +1512,6 @@ vhost hls.srs.com { # the key root URL, use this can support https. # @remark It's optional. hls_key_url https://localhost:8080; - # Whether enable hls_ctx. - # hls_ctx used to keep track of subsequent m3u8 requests. - # if on, hls client info can be collected in stat module, - # if on, on_play/on_stop can be called in http hook module. - # otherwise not; - # When request stopped, http hook or stat with special time effected, that is 2*hls_window, - # Default: off - hls_ctx on; # Special control controls. ########################################### diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index ba388b57d..8f76bbe1b 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -6390,7 +6390,7 @@ bool SrsConfig::get_vhost_hls_dts_directly(string vhost) bool SrsConfig::get_hls_ctx_enabled(std::string vhost) { - static bool DEFAULT = false; + static bool DEFAULT = true; SrsConfDirective* conf = get_hls(vhost); if (!conf) { diff --git a/trunk/src/app/srs_app_http_static.cpp b/trunk/src/app/srs_app_http_static.cpp index 563bcc47a..460bbebad 100644 --- a/trunk/src/app/srs_app_http_static.cpp +++ b/trunk/src/app/srs_app_http_static.cpp @@ -195,18 +195,20 @@ srs_error_t SrsVodStream::serve_m3u8_ctx(ISrsHttpResponseWriter * w, ISrsHttpMes srs_assert(hr); SrsRequest* req = hr->to_request(hr->host())->as_http(); + SrsAutoFree(SrsRequest, req); + // discovery vhost, resolve the vhost from config SrsConfDirective* parsed_vhost = _srs_config->get_vhost(req->vhost); if (parsed_vhost) { req->vhost = parsed_vhost->arg0(); } - SrsAutoFree(SrsRequest, req); - + // If HLS stream is disabled, use SrsHttpFileServer to serve HLS, which is normal file server. if (!_srs_config->get_hls_ctx_enabled(req->vhost)) { return SrsHttpFileServer::serve_m3u8_ctx(w, r, fullpath); } + // Serve as HLS stream, create a HLS session to serve it. string ctx = hr->query_get(SRS_CONTEXT_IN_HLS); if (!ctx.empty() && ctx_is_exist(ctx)) { alive(ctx, NULL); @@ -231,8 +233,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() && hr->query_get(SRS_CONTEXT_IN_HLS).empty()) - { + if (!hr->query().empty() && hr->query_get(SRS_CONTEXT_IN_HLS).empty()) { ss << "&" << hr->query(); } diff --git a/trunk/src/utest/srs_utest_http.cpp b/trunk/src/utest/srs_utest_http.cpp index c2c598c55..7ad432836 100644 --- a/trunk/src/utest/srs_utest_http.cpp +++ b/trunk/src/utest/srs_utest_http.cpp @@ -1210,8 +1210,8 @@ VOID TEST(ProtocolHTTPTest, VodStreamHandlers) __MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w); } - // TODO: should return "hls_ctx" - if (false) { + // Should return "hls_ctx" + if (true) { SrsHttpMuxEntry e; e.pattern = "/"; @@ -1228,8 +1228,8 @@ VOID TEST(ProtocolHTTPTest, VodStreamHandlers) __MOCK_HTTP_EXPECT_STRCT(200, "index.m3u8?hls_ctx=", w); } - // TODO: should return "hls_ctx" - if (false) { + // Should return "hls_ctx" + if (true) { SrsHttpMuxEntry e; e.pattern = "/";