From 998de2937a4f1081ca03b7a39dfb7df04c2fdfcf Mon Sep 17 00:00:00 2001 From: Sergey Dryabzhinsky Date: Sat, 6 Aug 2016 05:23:18 +0300 Subject: [PATCH 1/4] Try to coop with playlist length and fragments duration --- hls/ngx_rtmp_hls_module.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/hls/ngx_rtmp_hls_module.c b/hls/ngx_rtmp_hls_module.c index 94e393b..31f06f5 100644 --- a/hls/ngx_rtmp_hls_module.c +++ b/hls/ngx_rtmp_hls_module.c @@ -534,7 +534,8 @@ ngx_rtmp_hls_write_playlist(ngx_rtmp_session_t *s) ssize_t n; ngx_rtmp_hls_app_conf_t *hacf; ngx_rtmp_hls_frag_t *f; - ngx_uint_t i, max_frag; + ngx_uint_t i, start_i, max_frag; + double fragments_length; ngx_str_t name_part, key_name_part; uint64_t prev_key_id; const char *sep, *key_sep; @@ -557,9 +558,29 @@ ngx_rtmp_hls_write_playlist(ngx_rtmp_session_t *s) return NGX_ERROR; } + /** + * Need to check fragments length sum and playlist max length + * Do backward search + */ + start_i = 0; + fragments_length = 0.; + for (i = ctx->nfrags-1; i >= 0; i--) { + f = ngx_rtmp_hls_get_frag(s, i); + if (f->duration) { + fragments_length += f->duration; + } + if (fragments_length >= hacf->playlen/1000.) { + start_i = i; + break; + } + } + + ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, + "hls: found starting fragment=%i", start_i); + max_frag = hacf->fraglen / 1000; - for (i = 0; i < ctx->nfrags; i++) { + for (i = start_i; i < ctx->nfrags; i++) { f = ngx_rtmp_hls_get_frag(s, i); if (f->duration > max_frag) { max_frag = (ngx_uint_t) (f->duration + .5); @@ -606,7 +627,7 @@ ngx_rtmp_hls_write_playlist(ngx_rtmp_session_t *s) prev_key_id = 0; - for (i = 0; i < ctx->nfrags; i++) { + for (i = start_i; i < ctx->nfrags; i++) { f = ngx_rtmp_hls_get_frag(s, i); if (i == 0 && f->datetime && f->datetime->len > 0) { p = ngx_snprintf(buffer, sizeof(buffer), "#EXT-X-PROGRAM-DATE-TIME:"); From a88bc391410450909af1657119acc04f4d1140b5 Mon Sep 17 00:00:00 2001 From: Sergey Dryabzhinsky Date: Sat, 6 Aug 2016 07:39:17 +0300 Subject: [PATCH 2/4] Fixes: - fix first fragment search - fix log output for discontinuety flag --- hls/ngx_rtmp_hls_module.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/hls/ngx_rtmp_hls_module.c b/hls/ngx_rtmp_hls_module.c index 31f06f5..120e085 100644 --- a/hls/ngx_rtmp_hls_module.c +++ b/hls/ngx_rtmp_hls_module.c @@ -39,8 +39,8 @@ typedef struct { uint64_t key_id; ngx_str_t *datetime; double duration; - unsigned active:1; - unsigned discont:1; /* before */ + u_char active; /* small int, 0/1 */ + u_char discont; /* small int, 0/1 */ } ngx_rtmp_hls_frag_t; @@ -51,7 +51,7 @@ typedef struct { typedef struct { - unsigned opened:1; + u_char opened; /* small int, 0/1 */ ngx_rtmp_mpegts_file_t file; @@ -534,7 +534,8 @@ ngx_rtmp_hls_write_playlist(ngx_rtmp_session_t *s) ssize_t n; ngx_rtmp_hls_app_conf_t *hacf; ngx_rtmp_hls_frag_t *f; - ngx_uint_t i, start_i, max_frag; + ngx_int_t i, start_i; + ngx_uint_t max_frag; double fragments_length; ngx_str_t name_part, key_name_part; uint64_t prev_key_id; @@ -558,6 +559,8 @@ ngx_rtmp_hls_write_playlist(ngx_rtmp_session_t *s) return NGX_ERROR; } + max_frag = hacf->fraglen / 1000; + /** * Need to check fragments length sum and playlist max length * Do backward search @@ -569,7 +572,12 @@ ngx_rtmp_hls_write_playlist(ngx_rtmp_session_t *s) if (f->duration) { fragments_length += f->duration; } - if (fragments_length >= hacf->playlen/1000.) { + /** + * Think that sum of frag length is more than playlist disired length - half minimal frag length + * XXX: sometimes sum of frag lengths are almost playlist length + * but key-frames come at random rate... + */ + if (fragments_length >= hacf->playlen/1000. - max_frag/2) { start_i = i; break; } @@ -578,8 +586,6 @@ ngx_rtmp_hls_write_playlist(ngx_rtmp_session_t *s) ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "hls: found starting fragment=%i", start_i); - max_frag = hacf->fraglen / 1000; - for (i = start_i; i < ctx->nfrags; i++) { f = ngx_rtmp_hls_get_frag(s, i); if (f->duration > max_frag) { @@ -669,7 +675,7 @@ ngx_rtmp_hls_write_playlist(ngx_rtmp_session_t *s) ngx_log_debug5(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "hls: fragment frag=%uL, n=%ui/%ui, duration=%.3f, " "discont=%i", - ctx->frag, i + 1, ctx->nfrags, f->duration, f->discont); + ctx->frag, i + 1, ctx->nfrags, f->duration, (ngx_int_t)f->discont); n = ngx_write_fd(fd, buffer, p - buffer); if (n < 0) { From 7381b66e13510af30079fa3e32aed066a1ac3b38 Mon Sep 17 00:00:00 2001 From: Sergey Dryabzhinsky Date: Sat, 6 Aug 2016 07:43:20 +0300 Subject: [PATCH 3/4] typo --- hls/ngx_rtmp_hls_module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hls/ngx_rtmp_hls_module.c b/hls/ngx_rtmp_hls_module.c index 120e085..cc95ae0 100644 --- a/hls/ngx_rtmp_hls_module.c +++ b/hls/ngx_rtmp_hls_module.c @@ -573,7 +573,7 @@ ngx_rtmp_hls_write_playlist(ngx_rtmp_session_t *s) fragments_length += f->duration; } /** - * Think that sum of frag length is more than playlist disired length - half minimal frag length + * Think that sum of frag length is more than playlist desired length - half minimal frag length * XXX: sometimes sum of frag lengths are almost playlist length * but key-frames come at random rate... */ From 14221340d396e6742c72caaa8e60b66760090c19 Mon Sep 17 00:00:00 2001 From: Sergey Dryabzhinsky Date: Tue, 1 Jun 2021 19:06:04 +0300 Subject: [PATCH 4/4] Fix miscast --- hls/ngx_rtmp_hls_module.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hls/ngx_rtmp_hls_module.c b/hls/ngx_rtmp_hls_module.c index cc95ae0..5d7dc4a 100644 --- a/hls/ngx_rtmp_hls_module.c +++ b/hls/ngx_rtmp_hls_module.c @@ -633,7 +633,7 @@ ngx_rtmp_hls_write_playlist(ngx_rtmp_session_t *s) prev_key_id = 0; - for (i = start_i; i < ctx->nfrags; i++) { + for (i = start_i; i < (ngx_int_t)ctx->nfrags; i++) { f = ngx_rtmp_hls_get_frag(s, i); if (i == 0 && f->datetime && f->datetime->len > 0) { p = ngx_snprintf(buffer, sizeof(buffer), "#EXT-X-PROGRAM-DATE-TIME:");