1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

HLS: Enable hls_ctx by default.

This commit is contained in:
winlin 2022-08-27 19:27:08 +08:00
parent ca7b5a1c4e
commit d39995cc31
4 changed files with 23 additions and 17 deletions

View file

@ -1371,6 +1371,19 @@ vhost hls.srs.com {
# if off, do not write hls(ts and m3u8) when publish. # if off, do not write hls(ts and m3u8) when publish.
# default: off # default: off
enabled on; 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. # the hls fragment in seconds, the duration of a piece of ts.
# default: 10 # default: 10
hls_fragment 10; hls_fragment 10;
@ -1499,14 +1512,6 @@ vhost hls.srs.com {
# the key root URL, use this can support https. # the key root URL, use this can support https.
# @remark It's optional. # @remark It's optional.
hls_key_url https://localhost:8080; 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. # Special control controls.
########################################### ###########################################

View file

@ -6390,7 +6390,7 @@ bool SrsConfig::get_vhost_hls_dts_directly(string vhost)
bool SrsConfig::get_hls_ctx_enabled(std::string vhost) bool SrsConfig::get_hls_ctx_enabled(std::string vhost)
{ {
static bool DEFAULT = false; static bool DEFAULT = true;
SrsConfDirective* conf = get_hls(vhost); SrsConfDirective* conf = get_hls(vhost);
if (!conf) { if (!conf) {

View file

@ -195,18 +195,20 @@ srs_error_t SrsVodStream::serve_m3u8_ctx(ISrsHttpResponseWriter * w, ISrsHttpMes
srs_assert(hr); srs_assert(hr);
SrsRequest* req = hr->to_request(hr->host())->as_http(); SrsRequest* req = hr->to_request(hr->host())->as_http();
SrsAutoFree(SrsRequest, req);
// discovery vhost, resolve the vhost from config // discovery vhost, resolve the vhost from config
SrsConfDirective* parsed_vhost = _srs_config->get_vhost(req->vhost); SrsConfDirective* parsed_vhost = _srs_config->get_vhost(req->vhost);
if (parsed_vhost) { if (parsed_vhost) {
req->vhost = parsed_vhost->arg0(); 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)) { if (!_srs_config->get_hls_ctx_enabled(req->vhost)) {
return SrsHttpFileServer::serve_m3u8_ctx(w, r, fullpath); 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); string ctx = hr->query_get(SRS_CONTEXT_IN_HLS);
if (!ctx.empty() && ctx_is_exist(ctx)) { if (!ctx.empty() && ctx_is_exist(ctx)) {
alive(ctx, NULL); alive(ctx, NULL);
@ -231,8 +233,7 @@ srs_error_t SrsVodStream::serve_m3u8_ctx(ISrsHttpResponseWriter * w, ISrsHttpMes
ss << "#EXTM3U" << SRS_CONSTS_LF; ss << "#EXTM3U" << SRS_CONSTS_LF;
ss << "#EXT-X-STREAM-INF:BANDWIDTH=1,AVERAGE-BANDWIDTH=1" << SRS_CONSTS_LF; ss << "#EXT-X-STREAM-INF:BANDWIDTH=1,AVERAGE-BANDWIDTH=1" << SRS_CONSTS_LF;
ss << hr->path() << "?" << SRS_CONTEXT_IN_HLS << "=" << ctx; 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(); ss << "&" << hr->query();
} }

View file

@ -1210,8 +1210,8 @@ VOID TEST(ProtocolHTTPTest, VodStreamHandlers)
__MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w); __MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w);
} }
// TODO: should return "hls_ctx" // Should return "hls_ctx"
if (false) { if (true) {
SrsHttpMuxEntry e; SrsHttpMuxEntry e;
e.pattern = "/"; e.pattern = "/";
@ -1228,8 +1228,8 @@ VOID TEST(ProtocolHTTPTest, VodStreamHandlers)
__MOCK_HTTP_EXPECT_STRCT(200, "index.m3u8?hls_ctx=", w); __MOCK_HTTP_EXPECT_STRCT(200, "index.m3u8?hls_ctx=", w);
} }
// TODO: should return "hls_ctx" // Should return "hls_ctx"
if (false) { if (true) {
SrsHttpMuxEntry e; SrsHttpMuxEntry e;
e.pattern = "/"; e.pattern = "/";