From 15cc5d02264e8d400d6108084b59838e429f1c60 Mon Sep 17 00:00:00 2001 From: Brad Murray Date: Mon, 16 Apr 2018 16:54:59 -0400 Subject: [PATCH 1/8] Increase length for flashver. It was truncated for wirecast. --- ngx_rtmp_cmd_module.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ngx_rtmp_cmd_module.h b/ngx_rtmp_cmd_module.h index a419158..09bf4c0 100644 --- a/ngx_rtmp_cmd_module.h +++ b/ngx_rtmp_cmd_module.h @@ -25,7 +25,7 @@ typedef struct { double trans; u_char app[NGX_RTMP_MAX_NAME]; u_char args[NGX_RTMP_MAX_ARGS]; - u_char flashver[32]; + u_char flashver[64]; u_char swf_url[NGX_RTMP_MAX_URL]; u_char tc_url[NGX_RTMP_MAX_URL]; double acodecs; From ff3536996c13c5909f79f608f4aeb4a56bb2b797 Mon Sep 17 00:00:00 2001 From: Brad Murray Date: Mon, 16 Apr 2018 17:03:53 -0400 Subject: [PATCH 2/8] Add metadata audio+video data rate to stats --- ngx_rtmp_stat_module.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ngx_rtmp_stat_module.c b/ngx_rtmp_stat_module.c index ba1c646..325a2f1 100644 --- a/ngx_rtmp_stat_module.c +++ b/ngx_rtmp_stat_module.c @@ -555,7 +555,10 @@ ngx_rtmp_stat_live(ngx_http_request_t *r, ngx_chain_t ***lll, NGX_RTMP_STAT_L(""); NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf), "%.3f", codec->frame_rate) - buf); - NGX_RTMP_STAT_L(""); + NGX_RTMP_STAT_L(""); + NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf), + "%.0f", codec->video_data_rate) - buf); + NGX_RTMP_STAT_L(""); cname = ngx_rtmp_get_video_codec_name(codec->video_codec_id); if (*cname) { @@ -615,6 +618,12 @@ ngx_rtmp_stat_live(ngx_http_request_t *r, ngx_chain_t ***lll, "%ui", codec->sample_rate) - buf); NGX_RTMP_STAT_L(""); } + if (codec->audio_data_rate) { + NGX_RTMP_STAT_L(""); + NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf), + "%0.2f", codec->audio_data_rate) - buf); + NGX_RTMP_STAT_L(""); + } NGX_RTMP_STAT_L(""); NGX_RTMP_STAT_L("\r\n"); From 669059f41b0fc9946a00fc3bc3e7d4ced5428c30 Mon Sep 17 00:00:00 2001 From: Brad Murray Date: Mon, 16 Apr 2018 17:54:22 -0400 Subject: [PATCH 3/8] track meta videokeyframe_frequency, add to stats --- ngx_rtmp_codec_module.c | 23 ++++++++++++++++++----- ngx_rtmp_codec_module.h | 1 + ngx_rtmp_stat_module.c | 7 +++++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/ngx_rtmp_codec_module.c b/ngx_rtmp_codec_module.c index 73d31a1..7c377ca 100644 --- a/ngx_rtmp_codec_module.c +++ b/ngx_rtmp_codec_module.c @@ -306,7 +306,7 @@ ngx_rtmp_codec_parse_aac_header(ngx_rtmp_session_t *s, ngx_chain_t *in) ctx->aac_chan_conf = (ngx_uint_t) ngx_rtmp_bit_read(&br, 4); if (ctx->aac_profile == 5 || ctx->aac_profile == 29) { - + if (ctx->aac_profile == 29) { ctx->aac_ps = 1; } @@ -343,7 +343,7 @@ ngx_rtmp_codec_parse_aac_header(ngx_rtmp_session_t *s, ngx_chain_t *in) 5 bits: object type if (object type == 31) 6 bits + 32: object type - + var bits: AOT Specific Config */ @@ -415,7 +415,7 @@ ngx_rtmp_codec_parse_avc_header(ngx_rtmp_session_t *s, ngx_chain_t *in) { /* chroma format idc */ cf_idc = (ngx_uint_t) ngx_rtmp_bit_read_golomb(&br); - + if (cf_idc == 3) { /* separate color plane */ @@ -595,6 +595,7 @@ ngx_rtmp_codec_reconstruct_meta(ngx_rtmp_session_t *s) double duration; double frame_rate; double video_data_rate; + double video_keyframe_frequency; double video_codec_id; double audio_data_rate; double audio_codec_id; @@ -640,6 +641,10 @@ ngx_rtmp_codec_reconstruct_meta(ngx_rtmp_session_t *s) ngx_string("videodatarate"), &v.video_data_rate, 0 }, + { NGX_RTMP_AMF_NUMBER, + ngx_string("videokeyframe_frequency"), + &v.video_keyframe_frequency, 0 }, + { NGX_RTMP_AMF_NUMBER, ngx_string("videocodecid"), &v.video_codec_id, 0 }, @@ -689,6 +694,7 @@ ngx_rtmp_codec_reconstruct_meta(ngx_rtmp_session_t *s) v.duration = ctx->duration; v.frame_rate = ctx->frame_rate; v.video_data_rate = ctx->video_data_rate; + v.video_keyframe_frequency = ctx->video_keyframe_frequency; v.video_codec_id = ctx->video_codec_id; v.audio_data_rate = ctx->audio_data_rate; v.audio_codec_id = ctx->audio_codec_id; @@ -765,6 +771,7 @@ ngx_rtmp_codec_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, double duration; double frame_rate; double video_data_rate; + double video_keyframe_frequency; double video_codec_id_n; u_char video_codec_id_s[32]; double audio_data_rate; @@ -822,6 +829,10 @@ ngx_rtmp_codec_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_string("videodatarate"), &v.video_data_rate, 0 }, + { NGX_RTMP_AMF_NUMBER, + ngx_string("videokeyframe_frequency"), + &v.video_keyframe_frequency, 0 }, + { NGX_RTMP_AMF_VARIANT, ngx_string("videocodecid"), in_video_codec_id, sizeof(in_video_codec_id) }, @@ -871,6 +882,7 @@ ngx_rtmp_codec_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, v.duration = -1; v.frame_rate = -1; v.video_data_rate = -1; + v.video_keyframe_frequency = -1; v.video_codec_id_n = -1; v.audio_data_rate = -1; v.audio_codec_id_n = -1; @@ -895,6 +907,7 @@ ngx_rtmp_codec_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, if (v.video_data_rate != -1) ctx->video_data_rate = v.video_data_rate; if (v.video_codec_id_n != -1) ctx->video_codec_id = (ngx_uint_t) v.video_codec_id_n; if (v.audio_data_rate != -1) ctx->audio_data_rate = v.audio_data_rate; + if (v.video_keyframe_frequency != -1) ctx->video_keyframe_frequency = v.video_keyframe_frequency; if (v.audio_codec_id_n != -1) ctx->audio_codec_id = (v.audio_codec_id_n == 0 ? NGX_RTMP_AUDIO_UNCOMPRESSED : (ngx_uint_t) v.audio_codec_id_n); if (v.profile[0] != '\0') ngx_memcpy(ctx->profile, v.profile, sizeof(v.profile)); @@ -982,7 +995,7 @@ ngx_rtmp_codec_postconfiguration(ngx_conf_t *cf) } ngx_str_set(&ch->name, "@setDataFrame"); ch->handler = ngx_rtmp_codec_meta_data; - + // some encoders send setDataFrame instead of @setDataFrame ch = ngx_array_push(&cmcf->amf); if (ch == NULL) { @@ -990,7 +1003,7 @@ ngx_rtmp_codec_postconfiguration(ngx_conf_t *cf) } ngx_str_set(&ch->name, "setDataFrame"); ch->handler = ngx_rtmp_codec_meta_data; - + ch = ngx_array_push(&cmcf->amf); if (ch == NULL) { return NGX_ERROR; diff --git a/ngx_rtmp_codec_module.h b/ngx_rtmp_codec_module.h index b8de10c..768c0f8 100644 --- a/ngx_rtmp_codec_module.h +++ b/ngx_rtmp_codec_module.h @@ -55,6 +55,7 @@ typedef struct { double duration; double frame_rate; double video_data_rate; + double video_keyframe_frequency; ngx_uint_t video_codec_id; double audio_data_rate; ngx_uint_t audio_codec_id; diff --git a/ngx_rtmp_stat_module.c b/ngx_rtmp_stat_module.c index 325a2f1..6aeac0f 100644 --- a/ngx_rtmp_stat_module.c +++ b/ngx_rtmp_stat_module.c @@ -560,6 +560,13 @@ ngx_rtmp_stat_live(ngx_http_request_t *r, ngx_chain_t ***lll, "%.0f", codec->video_data_rate) - buf); NGX_RTMP_STAT_L(""); + if(codec->video_keyframe_frequency) { + NGX_RTMP_STAT_L(""); + NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf), + "%.0f", codec->video_keyframe_frequency) - buf); + NGX_RTMP_STAT_L(""); + } + cname = ngx_rtmp_get_video_codec_name(codec->video_codec_id); if (*cname) { NGX_RTMP_STAT_L(""); From a65297410ec8bcca30a4ce58dbe114f1ba0381f6 Mon Sep 17 00:00:00 2001 From: Brad Murray Date: Mon, 16 Apr 2018 17:54:22 -0400 Subject: [PATCH 4/8] track meta videokeyframe_frequency, add to stats --- ngx_rtmp_codec_module.c | 23 ++++++++++++++++++----- ngx_rtmp_codec_module.h | 1 + ngx_rtmp_stat_module.c | 9 ++++++++- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/ngx_rtmp_codec_module.c b/ngx_rtmp_codec_module.c index 73d31a1..7c377ca 100644 --- a/ngx_rtmp_codec_module.c +++ b/ngx_rtmp_codec_module.c @@ -306,7 +306,7 @@ ngx_rtmp_codec_parse_aac_header(ngx_rtmp_session_t *s, ngx_chain_t *in) ctx->aac_chan_conf = (ngx_uint_t) ngx_rtmp_bit_read(&br, 4); if (ctx->aac_profile == 5 || ctx->aac_profile == 29) { - + if (ctx->aac_profile == 29) { ctx->aac_ps = 1; } @@ -343,7 +343,7 @@ ngx_rtmp_codec_parse_aac_header(ngx_rtmp_session_t *s, ngx_chain_t *in) 5 bits: object type if (object type == 31) 6 bits + 32: object type - + var bits: AOT Specific Config */ @@ -415,7 +415,7 @@ ngx_rtmp_codec_parse_avc_header(ngx_rtmp_session_t *s, ngx_chain_t *in) { /* chroma format idc */ cf_idc = (ngx_uint_t) ngx_rtmp_bit_read_golomb(&br); - + if (cf_idc == 3) { /* separate color plane */ @@ -595,6 +595,7 @@ ngx_rtmp_codec_reconstruct_meta(ngx_rtmp_session_t *s) double duration; double frame_rate; double video_data_rate; + double video_keyframe_frequency; double video_codec_id; double audio_data_rate; double audio_codec_id; @@ -640,6 +641,10 @@ ngx_rtmp_codec_reconstruct_meta(ngx_rtmp_session_t *s) ngx_string("videodatarate"), &v.video_data_rate, 0 }, + { NGX_RTMP_AMF_NUMBER, + ngx_string("videokeyframe_frequency"), + &v.video_keyframe_frequency, 0 }, + { NGX_RTMP_AMF_NUMBER, ngx_string("videocodecid"), &v.video_codec_id, 0 }, @@ -689,6 +694,7 @@ ngx_rtmp_codec_reconstruct_meta(ngx_rtmp_session_t *s) v.duration = ctx->duration; v.frame_rate = ctx->frame_rate; v.video_data_rate = ctx->video_data_rate; + v.video_keyframe_frequency = ctx->video_keyframe_frequency; v.video_codec_id = ctx->video_codec_id; v.audio_data_rate = ctx->audio_data_rate; v.audio_codec_id = ctx->audio_codec_id; @@ -765,6 +771,7 @@ ngx_rtmp_codec_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, double duration; double frame_rate; double video_data_rate; + double video_keyframe_frequency; double video_codec_id_n; u_char video_codec_id_s[32]; double audio_data_rate; @@ -822,6 +829,10 @@ ngx_rtmp_codec_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_string("videodatarate"), &v.video_data_rate, 0 }, + { NGX_RTMP_AMF_NUMBER, + ngx_string("videokeyframe_frequency"), + &v.video_keyframe_frequency, 0 }, + { NGX_RTMP_AMF_VARIANT, ngx_string("videocodecid"), in_video_codec_id, sizeof(in_video_codec_id) }, @@ -871,6 +882,7 @@ ngx_rtmp_codec_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, v.duration = -1; v.frame_rate = -1; v.video_data_rate = -1; + v.video_keyframe_frequency = -1; v.video_codec_id_n = -1; v.audio_data_rate = -1; v.audio_codec_id_n = -1; @@ -895,6 +907,7 @@ ngx_rtmp_codec_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, if (v.video_data_rate != -1) ctx->video_data_rate = v.video_data_rate; if (v.video_codec_id_n != -1) ctx->video_codec_id = (ngx_uint_t) v.video_codec_id_n; if (v.audio_data_rate != -1) ctx->audio_data_rate = v.audio_data_rate; + if (v.video_keyframe_frequency != -1) ctx->video_keyframe_frequency = v.video_keyframe_frequency; if (v.audio_codec_id_n != -1) ctx->audio_codec_id = (v.audio_codec_id_n == 0 ? NGX_RTMP_AUDIO_UNCOMPRESSED : (ngx_uint_t) v.audio_codec_id_n); if (v.profile[0] != '\0') ngx_memcpy(ctx->profile, v.profile, sizeof(v.profile)); @@ -982,7 +995,7 @@ ngx_rtmp_codec_postconfiguration(ngx_conf_t *cf) } ngx_str_set(&ch->name, "@setDataFrame"); ch->handler = ngx_rtmp_codec_meta_data; - + // some encoders send setDataFrame instead of @setDataFrame ch = ngx_array_push(&cmcf->amf); if (ch == NULL) { @@ -990,7 +1003,7 @@ ngx_rtmp_codec_postconfiguration(ngx_conf_t *cf) } ngx_str_set(&ch->name, "setDataFrame"); ch->handler = ngx_rtmp_codec_meta_data; - + ch = ngx_array_push(&cmcf->amf); if (ch == NULL) { return NGX_ERROR; diff --git a/ngx_rtmp_codec_module.h b/ngx_rtmp_codec_module.h index b8de10c..768c0f8 100644 --- a/ngx_rtmp_codec_module.h +++ b/ngx_rtmp_codec_module.h @@ -55,6 +55,7 @@ typedef struct { double duration; double frame_rate; double video_data_rate; + double video_keyframe_frequency; ngx_uint_t video_codec_id; double audio_data_rate; ngx_uint_t audio_codec_id; diff --git a/ngx_rtmp_stat_module.c b/ngx_rtmp_stat_module.c index 325a2f1..8d21c8b 100644 --- a/ngx_rtmp_stat_module.c +++ b/ngx_rtmp_stat_module.c @@ -560,6 +560,13 @@ ngx_rtmp_stat_live(ngx_http_request_t *r, ngx_chain_t ***lll, "%.0f", codec->video_data_rate) - buf); NGX_RTMP_STAT_L(""); + if(codec->video_keyframe_frequency) { + NGX_RTMP_STAT_L(""); + NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf), + "%.0f", codec->video_keyframe_frequency) - buf); + NGX_RTMP_STAT_L(""); + } + cname = ngx_rtmp_get_video_codec_name(codec->video_codec_id); if (*cname) { NGX_RTMP_STAT_L(""); @@ -621,7 +628,7 @@ ngx_rtmp_stat_live(ngx_http_request_t *r, ngx_chain_t ***lll, if (codec->audio_data_rate) { NGX_RTMP_STAT_L(""); NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf), - "%0.2f", codec->audio_data_rate) - buf); + "%0.0f", codec->audio_data_rate) - buf); NGX_RTMP_STAT_L(""); } NGX_RTMP_STAT_L(""); From 7b7d30f36ccadc7a64a59caa64c36b5494068a44 Mon Sep 17 00:00:00 2001 From: Brad Murray Date: Thu, 19 Apr 2018 15:02:03 -0400 Subject: [PATCH 5/8] Intellegent variant playlist entries. Auto stream inf, skip if not live --- hls/ngx_rtmp_hls_module.c | 81 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 4 deletions(-) diff --git a/hls/ngx_rtmp_hls_module.c b/hls/ngx_rtmp_hls_module.c index fbf2bcb..467a33f 100644 --- a/hls/ngx_rtmp_hls_module.c +++ b/hls/ngx_rtmp_hls_module.c @@ -9,9 +9,11 @@ #include #include #include +#include #include "ngx_rtmp_mpegts.h" + static ngx_rtmp_publish_pt next_publish; static ngx_rtmp_close_stream_pt next_close_stream; static ngx_rtmp_stream_begin_pt next_stream_begin; @@ -296,7 +298,7 @@ static ngx_command_t ngx_rtmp_hls_commands[] = { ngx_conf_set_enum_slot, NGX_RTMP_APP_CONF_OFFSET, offsetof(ngx_rtmp_hls_app_conf_t, allow_client_cache), - &ngx_rtmp_hls_cache }, + &ngx_rtmp_hls_cache }, { ngx_string("hls_variant"), NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_1MORE, @@ -381,6 +383,36 @@ ngx_module_t ngx_rtmp_hls_module = { NGX_MODULE_V1_PADDING }; +static ngx_rtmp_codec_ctx_t * ngx_rtmp_hls_get_stream(ngx_rtmp_session_t *s, u_char *name) +{ + ngx_rtmp_core_app_conf_t *cacf; + ngx_rtmp_live_app_conf_t *lacf; + ngx_int_t n; + ngx_rtmp_live_stream_t *stream; + ngx_rtmp_codec_ctx_t *codec; + ngx_rtmp_live_ctx_t *ctx; + ngx_rtmp_session_t *s2; + + cacf = *s->app_conf; + lacf = cacf->app_conf[ngx_rtmp_live_module.ctx_index]; + + for (n = 0; n < lacf->nbuckets; ++n) { + for (stream = lacf->streams[n]; stream; stream = stream->next) { + if (ngx_strcmp(name, stream->name) == 0) { + codec = NULL; + for (ctx = stream->ctx; ctx; ctx = ctx->next) { + if (ctx->publishing) { + s2 = ctx->session; + codec = ngx_rtmp_get_module_ctx(s2, ngx_rtmp_codec_module); + return codec; + } + } + } + } + } + + return NULL; +} static ngx_rtmp_hls_frag_t * ngx_rtmp_hls_get_frag(ngx_rtmp_session_t *s, ngx_int_t n) @@ -431,6 +463,8 @@ ngx_rtmp_hls_write_variant_playlist(ngx_rtmp_session_t *s) static u_char buffer[1024]; u_char *p, *last; + u_char variant_name[NGX_RTMP_MAX_NAME]; + ssize_t rc; ngx_fd_t fd; ngx_str_t *arg; @@ -438,6 +472,8 @@ ngx_rtmp_hls_write_variant_playlist(ngx_rtmp_session_t *s) ngx_rtmp_hls_ctx_t *ctx; ngx_rtmp_hls_variant_t *var; ngx_rtmp_hls_app_conf_t *hacf; + ngx_rtmp_codec_ctx_t *codec_ctx; + double total_data_rate; ngx_rtmp_playlist_t v; @@ -475,9 +511,46 @@ ngx_rtmp_hls_write_variant_playlist(ngx_rtmp_session_t *s) p = ngx_slprintf(p, last, "#EXT-X-STREAM-INF:PROGRAM-ID=1,CLOSED-CAPTIONS=NONE"); - arg = var->args.elts; - for (k = 0; k < var->args.nelts; k++, arg++) { - p = ngx_slprintf(p, last, ",%V", arg); + // not sure why this is necessary. snprintf wasn't null terminating + ngx_memzero(variant_name, NGX_RTMP_MAX_NAME); + + ngx_snprintf(variant_name, NGX_RTMP_MAX_NAME, "%*s%V", + ctx->name.len - ctx->var->suffix.len, ctx->name.data, + &var->suffix); + + if(var->args.nelts == 0) { + if (ngx_strcmp(ctx->name.data, variant_name) == 0) { + codec_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module); + } + else { + codec_ctx = ngx_rtmp_hls_get_stream(s, variant_name); + } + + if(codec_ctx) { + // meta bandwidth is in kbps, output is in bps. Add 10% overhead + total_data_rate = (codec_ctx->video_data_rate + codec_ctx->audio_data_rate)*1100; + p = ngx_slprintf(p, last, ",BANDWIDTH=%.0f", total_data_rate); + if(codec_ctx->width) { + p = ngx_slprintf(p, last, ",CODECS=\"avc1.%02uxi%02uxi%02uxi", codec_ctx->avc_profile, codec_ctx->avc_compat, codec_ctx->avc_level); + if(codec_ctx->audio_codec_id) { + p = ngx_slprintf(p, last, ",mp4a.%s\"", codec_ctx->audio_codec_id == NGX_RTMP_AUDIO_AAC ? + (codec_ctx->aac_sbr ? "40.5" : "40.2") : "6b"); + } + else *p++ = '"'; + p = ngx_slprintf(p, last, ",RESOLUTION=%uix%ui", codec_ctx->width, codec_ctx->height); + + } + } + else { + // skip the non-broadcasting rendition + continue; + } + } + else { + arg = var->args.elts; + for (k = 0; k < var->args.nelts; k++, arg++) { + p = ngx_slprintf(p, last, ",%V", arg); + } } if (p < last) { From e2a626ac0414706ff1787d82fe656884c97af1a7 Mon Sep 17 00:00:00 2001 From: Brad Murray Date: Thu, 19 Apr 2018 23:51:20 -0400 Subject: [PATCH 6/8] Code cleanup, change codec string for mp3 audio --- hls/ngx_rtmp_hls_module.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/hls/ngx_rtmp_hls_module.c b/hls/ngx_rtmp_hls_module.c index 467a33f..3b9f5a8 100644 --- a/hls/ngx_rtmp_hls_module.c +++ b/hls/ngx_rtmp_hls_module.c @@ -389,9 +389,7 @@ static ngx_rtmp_codec_ctx_t * ngx_rtmp_hls_get_stream(ngx_rtmp_session_t *s, u_c ngx_rtmp_live_app_conf_t *lacf; ngx_int_t n; ngx_rtmp_live_stream_t *stream; - ngx_rtmp_codec_ctx_t *codec; ngx_rtmp_live_ctx_t *ctx; - ngx_rtmp_session_t *s2; cacf = *s->app_conf; lacf = cacf->app_conf[ngx_rtmp_live_module.ctx_index]; @@ -399,12 +397,9 @@ static ngx_rtmp_codec_ctx_t * ngx_rtmp_hls_get_stream(ngx_rtmp_session_t *s, u_c for (n = 0; n < lacf->nbuckets; ++n) { for (stream = lacf->streams[n]; stream; stream = stream->next) { if (ngx_strcmp(name, stream->name) == 0) { - codec = NULL; for (ctx = stream->ctx; ctx; ctx = ctx->next) { if (ctx->publishing) { - s2 = ctx->session; - codec = ngx_rtmp_get_module_ctx(s2, ngx_rtmp_codec_module); - return codec; + return ngx_rtmp_get_module_ctx(ctx->session, ngx_rtmp_codec_module); } } } @@ -533,8 +528,11 @@ ngx_rtmp_hls_write_variant_playlist(ngx_rtmp_session_t *s) if(codec_ctx->width) { p = ngx_slprintf(p, last, ",CODECS=\"avc1.%02uxi%02uxi%02uxi", codec_ctx->avc_profile, codec_ctx->avc_compat, codec_ctx->avc_level); if(codec_ctx->audio_codec_id) { + // apple says mp4a.40.34 for mp3. Other sources say mp4a.6b or mp4a.69 + // https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/StreamingMediaGuide/FrequentlyAskedQuestions/FrequentlyAskedQuestions.html + // nginx-rtmp dash module uses 6b. p = ngx_slprintf(p, last, ",mp4a.%s\"", codec_ctx->audio_codec_id == NGX_RTMP_AUDIO_AAC ? - (codec_ctx->aac_sbr ? "40.5" : "40.2") : "6b"); + (codec_ctx->aac_sbr ? "40.5" : "40.2") : "40.34"); } else *p++ = '"'; p = ngx_slprintf(p, last, ",RESOLUTION=%uix%ui", codec_ctx->width, codec_ctx->height); From 59c2454b23395662199d34048c7b79e33860a0a8 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 30 Dec 2020 04:01:12 +0000 Subject: [PATCH 7/8] Revert "Intellegent variant playlist entries. Auto stream inf, skip if not live" This reverts commit 7b7d30f36ccadc7a64a59caa64c36b5494068a44. Conflicts: hls/ngx_rtmp_hls_module.c --- hls/ngx_rtmp_hls_module.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/hls/ngx_rtmp_hls_module.c b/hls/ngx_rtmp_hls_module.c index 2e1a8bf..fe9abac 100644 --- a/hls/ngx_rtmp_hls_module.c +++ b/hls/ngx_rtmp_hls_module.c @@ -9,11 +9,9 @@ #include #include #include -#include #include "ngx_rtmp_mpegts.h" - static ngx_rtmp_publish_pt next_publish; static ngx_rtmp_close_stream_pt next_close_stream; static ngx_rtmp_stream_begin_pt next_stream_begin; @@ -298,7 +296,7 @@ static ngx_command_t ngx_rtmp_hls_commands[] = { ngx_conf_set_enum_slot, NGX_RTMP_APP_CONF_OFFSET, offsetof(ngx_rtmp_hls_app_conf_t, allow_client_cache), - &ngx_rtmp_hls_cache }, + &ngx_rtmp_hls_cache }, { ngx_string("hls_variant"), NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_1MORE, @@ -383,6 +381,7 @@ ngx_module_t ngx_rtmp_hls_module = { NGX_MODULE_V1_PADDING }; +<<<<<<< HEAD static ngx_rtmp_codec_ctx_t * ngx_rtmp_hls_get_stream(ngx_rtmp_session_t *s, u_char *name) { ngx_rtmp_core_app_conf_t *cacf; @@ -408,6 +407,8 @@ static ngx_rtmp_codec_ctx_t * ngx_rtmp_hls_get_stream(ngx_rtmp_session_t *s, u_c return NULL; } +======= +>>>>>>> parent of 7b7d30f... Intellegent variant playlist entries. Auto stream inf, skip if not live static ngx_rtmp_hls_frag_t * ngx_rtmp_hls_get_frag(ngx_rtmp_session_t *s, ngx_int_t n) @@ -458,8 +459,6 @@ ngx_rtmp_hls_write_variant_playlist(ngx_rtmp_session_t *s) static u_char buffer[1024]; u_char *p, *last; - u_char variant_name[NGX_RTMP_MAX_NAME]; - ssize_t rc; ngx_fd_t fd; ngx_str_t *arg; @@ -467,8 +466,6 @@ ngx_rtmp_hls_write_variant_playlist(ngx_rtmp_session_t *s) ngx_rtmp_hls_ctx_t *ctx; ngx_rtmp_hls_variant_t *var; ngx_rtmp_hls_app_conf_t *hacf; - ngx_rtmp_codec_ctx_t *codec_ctx; - double total_data_rate; ngx_rtmp_playlist_t v; @@ -506,6 +503,7 @@ ngx_rtmp_hls_write_variant_playlist(ngx_rtmp_session_t *s) p = ngx_slprintf(p, last, "#EXT-X-STREAM-INF:PROGRAM-ID=1,CLOSED-CAPTIONS=NONE"); +<<<<<<< HEAD // not sure why this is necessary. snprintf wasn't null terminating ngx_memzero(variant_name, NGX_RTMP_MAX_NAME); @@ -549,6 +547,11 @@ ngx_rtmp_hls_write_variant_playlist(ngx_rtmp_session_t *s) for (k = 0; k < var->args.nelts; k++, arg++) { p = ngx_slprintf(p, last, ",%V", arg); } +======= + arg = var->args.elts; + for (k = 0; k < var->args.nelts; k++, arg++) { + p = ngx_slprintf(p, last, ",%V", arg); +>>>>>>> parent of 7b7d30f... Intellegent variant playlist entries. Auto stream inf, skip if not live } if (p < last) { From d743c3699611fef7b2589cad99a2b240bf21e60d Mon Sep 17 00:00:00 2001 From: root Date: Wed, 30 Dec 2020 18:19:23 +0000 Subject: [PATCH 8/8] revert variant playlist code --- hls/ngx_rtmp_hls_module.c | 74 --------------------------------------- 1 file changed, 74 deletions(-) diff --git a/hls/ngx_rtmp_hls_module.c b/hls/ngx_rtmp_hls_module.c index fe9abac..48e8e3b 100644 --- a/hls/ngx_rtmp_hls_module.c +++ b/hls/ngx_rtmp_hls_module.c @@ -381,34 +381,6 @@ ngx_module_t ngx_rtmp_hls_module = { NGX_MODULE_V1_PADDING }; -<<<<<<< HEAD -static ngx_rtmp_codec_ctx_t * ngx_rtmp_hls_get_stream(ngx_rtmp_session_t *s, u_char *name) -{ - ngx_rtmp_core_app_conf_t *cacf; - ngx_rtmp_live_app_conf_t *lacf; - ngx_int_t n; - ngx_rtmp_live_stream_t *stream; - ngx_rtmp_live_ctx_t *ctx; - - cacf = *s->app_conf; - lacf = cacf->app_conf[ngx_rtmp_live_module.ctx_index]; - - for (n = 0; n < lacf->nbuckets; ++n) { - for (stream = lacf->streams[n]; stream; stream = stream->next) { - if (ngx_strcmp(name, stream->name) == 0) { - for (ctx = stream->ctx; ctx; ctx = ctx->next) { - if (ctx->publishing) { - return ngx_rtmp_get_module_ctx(ctx->session, ngx_rtmp_codec_module); - } - } - } - } - } - - return NULL; -} -======= ->>>>>>> parent of 7b7d30f... Intellegent variant playlist entries. Auto stream inf, skip if not live static ngx_rtmp_hls_frag_t * ngx_rtmp_hls_get_frag(ngx_rtmp_session_t *s, ngx_int_t n) @@ -503,55 +475,9 @@ ngx_rtmp_hls_write_variant_playlist(ngx_rtmp_session_t *s) p = ngx_slprintf(p, last, "#EXT-X-STREAM-INF:PROGRAM-ID=1,CLOSED-CAPTIONS=NONE"); -<<<<<<< HEAD - // not sure why this is necessary. snprintf wasn't null terminating - ngx_memzero(variant_name, NGX_RTMP_MAX_NAME); - - ngx_snprintf(variant_name, NGX_RTMP_MAX_NAME, "%*s%V", - ctx->name.len - ctx->var->suffix.len, ctx->name.data, - &var->suffix); - - if(var->args.nelts == 0) { - if (ngx_strcmp(ctx->name.data, variant_name) == 0) { - codec_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module); - } - else { - codec_ctx = ngx_rtmp_hls_get_stream(s, variant_name); - } - - if(codec_ctx) { - // meta bandwidth is in kbps, output is in bps. Add 10% overhead - total_data_rate = (codec_ctx->video_data_rate + codec_ctx->audio_data_rate)*1100; - p = ngx_slprintf(p, last, ",BANDWIDTH=%.0f", total_data_rate); - if(codec_ctx->width) { - p = ngx_slprintf(p, last, ",CODECS=\"avc1.%02uxi%02uxi%02uxi", codec_ctx->avc_profile, codec_ctx->avc_compat, codec_ctx->avc_level); - if(codec_ctx->audio_codec_id) { - // apple says mp4a.40.34 for mp3. Other sources say mp4a.6b or mp4a.69 - // https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/StreamingMediaGuide/FrequentlyAskedQuestions/FrequentlyAskedQuestions.html - // nginx-rtmp dash module uses 6b. - p = ngx_slprintf(p, last, ",mp4a.%s\"", codec_ctx->audio_codec_id == NGX_RTMP_AUDIO_AAC ? - (codec_ctx->aac_sbr ? "40.5" : "40.2") : "40.34"); - } - else *p++ = '"'; - p = ngx_slprintf(p, last, ",RESOLUTION=%uix%ui", codec_ctx->width, codec_ctx->height); - - } - } - else { - // skip the non-broadcasting rendition - continue; - } - } - else { - arg = var->args.elts; - for (k = 0; k < var->args.nelts; k++, arg++) { - p = ngx_slprintf(p, last, ",%V", arg); - } -======= arg = var->args.elts; for (k = 0; k < var->args.nelts; k++, arg++) { p = ngx_slprintf(p, last, ",%V", arg); ->>>>>>> parent of 7b7d30f... Intellegent variant playlist entries. Auto stream inf, skip if not live } if (p < last) {