diff --git a/config b/config index cc05185bc..51176f318 100644 --- a/config +++ b/config @@ -15,6 +15,7 @@ RTMP_CORE_MODULES=" \ ngx_rtmp_relay_module \ ngx_rtmp_exec_module \ ngx_rtmp_auto_push_module \ + ngx_rtmp_auto_push_index_module \ ngx_rtmp_notify_module \ ngx_rtmp_log_module \ ngx_rtmp_limit_module \ diff --git a/dash/ngx_rtmp_dash_module.c b/dash/ngx_rtmp_dash_module.c index 88f55ef0d..81bc34351 100644 --- a/dash/ngx_rtmp_dash_module.c +++ b/dash/ngx_rtmp_dash_module.c @@ -52,7 +52,7 @@ typedef struct { ngx_str_t playlist_bak; ngx_str_t name; ngx_str_t stream; - ngx_time_t start_time; + time_t start_time; ngx_uint_t nfrags; ngx_uint_t frag; @@ -228,8 +228,8 @@ ngx_rtmp_dash_write_playlist(ngx_rtmp_session_t *s) ngx_rtmp_dash_app_conf_t *dacf; static u_char buffer[NGX_RTMP_DASH_BUFSIZE]; - static u_char start_time[sizeof("1970-09-28T12:00:00+06:00")]; - static u_char end_time[sizeof("1970-09-28T12:00:00+06:00")]; + static u_char start_time[sizeof("1970-09-28T12:00:00Z")]; + static u_char pub_time[sizeof("1970-09-28T12:00:00Z")]; dacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_dash_module); ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_dash_module); @@ -252,18 +252,16 @@ ngx_rtmp_dash_write_playlist(ngx_rtmp_session_t *s) return NGX_ERROR; } - #define NGX_RTMP_DASH_MANIFEST_HEADER \ "\n" \ "\n" \ " \n" \ @@ -323,7 +319,6 @@ ngx_rtmp_dash_write_playlist(ngx_rtmp_session_t *s) " startWithSAP=\"1\"\n" \ " bandwidth=\"%ui\">\n" \ " \n" \ @@ -341,38 +336,33 @@ ngx_rtmp_dash_write_playlist(ngx_rtmp_session_t *s) " \n" \ "\n" - ngx_libc_localtime(ctx->start_time.sec + - ngx_rtmp_dash_get_frag(s, 0)->timestamp / 1000, &tm); - - *ngx_sprintf(start_time, "%4d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d", - tm.tm_year + 1900, tm.tm_mon + 1, - tm.tm_mday, tm.tm_hour, - tm.tm_min, tm.tm_sec, - ctx->start_time.gmtoff < 0 ? '-' : '+', - ngx_abs(ctx->start_time.gmtoff / 60), - ngx_abs(ctx->start_time.gmtoff % 60)) = 0; - - ngx_libc_localtime(ctx->start_time.sec + - (ngx_rtmp_dash_get_frag(s, ctx->nfrags - 1)->timestamp + - ngx_rtmp_dash_get_frag(s, ctx->nfrags - 1)->duration) / - 1000, &tm); - - *ngx_sprintf(end_time, "%4d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d", - tm.tm_year + 1900, tm.tm_mon + 1, - tm.tm_mday, tm.tm_hour, - tm.tm_min, tm.tm_sec, - ctx->start_time.gmtoff < 0 ? '-' : '+', - ngx_abs(ctx->start_time.gmtoff / 60), - ngx_abs(ctx->start_time.gmtoff % 60)) = 0; + ngx_libc_gmtime(ctx->start_time, &tm); + + ngx_sprintf(start_time, "%4d-%02d-%02dT%02d:%02d:%02dZ%Z", + tm.tm_year + 1900, tm.tm_mon + 1, + tm.tm_mday, tm.tm_hour, + tm.tm_min, tm.tm_sec); + + ngx_libc_gmtime(ngx_time(), &tm); + + ngx_sprintf(pub_time, "%4d-%02d-%02dT%02d:%02d:%02dZ%Z", + tm.tm_year + 1900, tm.tm_mon + 1, + tm.tm_mday, tm.tm_hour, + tm.tm_min, tm.tm_sec); last = buffer + sizeof(buffer); p = ngx_slprintf(buffer, last, NGX_RTMP_DASH_MANIFEST_HEADER, start_time, - end_time, + pub_time, (ngx_uint_t) (dacf->fraglen / 1000), (ngx_uint_t) (dacf->fraglen / 1000), - (ngx_uint_t) (dacf->fraglen / 500)); + (ngx_uint_t) (dacf->fraglen / 250 + 1)); + + /* + * timeShiftBufferDepth formula: + * 2 * minBufferTime + max_fragment_length + 1 + */ n = ngx_write_fd(fd, buffer, p - buffer); @@ -952,7 +942,7 @@ ngx_rtmp_dash_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v) "dash: playlist='%V' playlist_bak='%V' stream_pattern='%V'", &ctx->playlist, &ctx->playlist_bak, &ctx->stream); - ctx->start_time = *ngx_cached_time; + ctx->start_time = ngx_time(); if (ngx_rtmp_dash_ensure_directory(s) != NGX_OK) { return NGX_ERROR; @@ -1008,6 +998,11 @@ ngx_rtmp_dash_update_fragments(ngx_rtmp_session_t *s, ngx_int_t boundary, f->duration = timestamp - f->timestamp; hit = (f->duration >= dacf->fraglen); + /* keep fragment lengths within 2x factor for dash.js */ + if (f->duration >= dacf->fraglen * 2) { + boundary = 1; + } + } else { /* sometimes clients generate slightly unordered frames */ @@ -1413,14 +1408,22 @@ ngx_rtmp_dash_cleanup_dir(ngx_str_t *ppath, ngx_msec_t playlen) } +#if (nginx_version >= 1011005) +static ngx_msec_t +#else static time_t +#endif ngx_rtmp_dash_cleanup(void *data) { ngx_rtmp_dash_cleanup_t *cleanup = data; ngx_rtmp_dash_cleanup_dir(&cleanup->path, cleanup->playlen); +#if (nginx_version >= 1011005) + return cleanup->playlen * 2; +#else return cleanup->playlen / 500; +#endif } diff --git a/hls/ngx_rtmp_hls_module.c b/hls/ngx_rtmp_hls_module.c index 7c573a35c..25166cbdc 100644 --- a/hls/ngx_rtmp_hls_module.c +++ b/hls/ngx_rtmp_hls_module.c @@ -1951,6 +1951,7 @@ ngx_rtmp_hls_video(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, "hls: error appending AUD NAL"); } + /* fall through */ case 9: aud_sent = 1; break; @@ -2210,14 +2211,22 @@ ngx_rtmp_hls_cleanup_dir(ngx_str_t *ppath, ngx_msec_t playlen) } +#if (nginx_version >= 1011005) +static ngx_msec_t +#else static time_t +#endif ngx_rtmp_hls_cleanup(void *data) { ngx_rtmp_hls_cleanup_t *cleanup = data; ngx_rtmp_hls_cleanup_dir(&cleanup->path, cleanup->playlen); +#if (nginx_version >= 1011005) + return cleanup->playlen * 2; +#else return cleanup->playlen / 500; +#endif } diff --git a/ngx_rtmp.c b/ngx_rtmp.c index 192b6c949..cde743202 100644 --- a/ngx_rtmp.c +++ b/ngx_rtmp.c @@ -87,6 +87,7 @@ ngx_rtmp_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) ngx_uint_t i, m, mi, s; ngx_conf_t pcf; ngx_array_t ports; + ngx_module_t **modules; ngx_rtmp_listen_t *listen; ngx_rtmp_module_t *module; ngx_rtmp_conf_ctx_t *ctx; @@ -102,6 +103,12 @@ ngx_rtmp_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) /* count the number of the rtmp modules and set up their indices */ +#if (nginx_version >= 1009011) + + ngx_rtmp_max_module = ngx_count_modules(cf->cycle, NGX_RTMP_MODULE); + +#else + ngx_rtmp_max_module = 0; for (m = 0; ngx_modules[m]; m++) { if (ngx_modules[m]->type != NGX_RTMP_MODULE) { @@ -111,6 +118,8 @@ ngx_rtmp_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) ngx_modules[m]->ctx_index = ngx_rtmp_max_module++; } +#endif + /* the rtmp main_conf context, it is the same in the all rtmp contexts */ @@ -148,13 +157,19 @@ ngx_rtmp_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) * of the all rtmp modules */ - for (m = 0; ngx_modules[m]; m++) { - if (ngx_modules[m]->type != NGX_RTMP_MODULE) { +#if (nginx_version >= 1009011) + modules = cf->cycle->modules; +#else + modules = ngx_modules; +#endif + + for (m = 0; modules[m]; m++) { + if (modules[m]->type != NGX_RTMP_MODULE) { continue; } - module = ngx_modules[m]->ctx; - mi = ngx_modules[m]->ctx_index; + module = modules[m]->ctx; + mi = modules[m]->ctx_index; if (module->create_main_conf) { ctx->main_conf[mi] = module->create_main_conf(cf); @@ -181,12 +196,12 @@ ngx_rtmp_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) pcf = *cf; cf->ctx = ctx; - for (m = 0; ngx_modules[m]; m++) { - if (ngx_modules[m]->type != NGX_RTMP_MODULE) { + for (m = 0; modules[m]; m++) { + if (modules[m]->type != NGX_RTMP_MODULE) { continue; } - module = ngx_modules[m]->ctx; + module = modules[m]->ctx; if (module->preconfiguration) { if (module->preconfiguration(cf) != NGX_OK) { @@ -212,13 +227,13 @@ ngx_rtmp_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) cmcf = ctx->main_conf[ngx_rtmp_core_module.ctx_index]; cscfp = cmcf->servers.elts; - for (m = 0; ngx_modules[m]; m++) { - if (ngx_modules[m]->type != NGX_RTMP_MODULE) { + for (m = 0; modules[m]; m++) { + if (modules[m]->type != NGX_RTMP_MODULE) { continue; } - module = ngx_modules[m]->ctx; - mi = ngx_modules[m]->ctx_index; + module = modules[m]->ctx; + mi = modules[m]->ctx_index; /* init rtmp{} main_conf's */ @@ -283,12 +298,12 @@ ngx_rtmp_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } - for (m = 0; ngx_modules[m]; m++) { - if (ngx_modules[m]->type != NGX_RTMP_MODULE) { + for (m = 0; modules[m]; m++) { + if (modules[m]->type != NGX_RTMP_MODULE) { continue; } - module = ngx_modules[m]->ctx; + module = modules[m]->ctx; if (module->postconfiguration) { if (module->postconfiguration(cf) != NGX_OK) { @@ -630,6 +645,8 @@ ngx_rtmp_optimize_servers(ngx_conf_t *cf, ngx_array_t *ports) ls->ipv6only = addr[i].ipv6only; #endif + ls->wildcard = addr[i].wildcard; + mport = ngx_palloc(cf->pool, sizeof(ngx_rtmp_port_t)); if (mport == NULL) { return NGX_CONF_ERROR; diff --git a/ngx_rtmp.h b/ngx_rtmp.h index e6c34d988..cbe6a93e3 100644 --- a/ngx_rtmp.h +++ b/ngx_rtmp.h @@ -135,6 +135,8 @@ typedef struct { #define NGX_RTMP_MSG_AGGREGATE 22 #define NGX_RTMP_MSG_MAX 22 +#define NGX_RTMP_MAX_CHUNK_SIZE 10485760 + #define NGX_RTMP_CONNECT NGX_RTMP_MSG_MAX + 1 #define NGX_RTMP_DISCONNECT NGX_RTMP_MSG_MAX + 2 #define NGX_RTMP_HANDSHAKE_DONE NGX_RTMP_MSG_MAX + 3 diff --git a/ngx_rtmp_access_module.c b/ngx_rtmp_access_module.c index 06d3bc20b..c4dbb19a6 100644 --- a/ngx_rtmp_access_module.c +++ b/ngx_rtmp_access_module.c @@ -410,8 +410,8 @@ ngx_rtmp_access_rule(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) break; } - /* "all" passes through */ #endif + /* fall through */ default: /* AF_INET */ diff --git a/ngx_rtmp_amf.c b/ngx_rtmp_amf.c index f133d0ec1..3fe3ccf6f 100644 --- a/ngx_rtmp_amf.c +++ b/ngx_rtmp_amf.c @@ -328,9 +328,10 @@ ngx_rtmp_amf_read(ngx_rtmp_amf_ctx_t *ctx, ngx_rtmp_amf_elt_t *elts, } else { switch (ngx_rtmp_amf_get(ctx, &type8, 1)) { case NGX_DONE: - if (elts->type & NGX_RTMP_AMF_OPTIONAL) { + if (elts && elts->type & NGX_RTMP_AMF_OPTIONAL) { return NGX_OK; } + /* fall through */ case NGX_ERROR: return NGX_ERROR; } @@ -398,6 +399,7 @@ ngx_rtmp_amf_read(ngx_rtmp_amf_ctx_t *ctx, ngx_rtmp_amf_elt_t *elts, if (ngx_rtmp_amf_get(ctx, &max_index, 4) != NGX_OK) { return NGX_ERROR; } + /* fall through */ case NGX_RTMP_AMF_OBJECT: if (ngx_rtmp_amf_read_object(ctx, data, @@ -592,6 +594,7 @@ ngx_rtmp_amf_write(ngx_rtmp_amf_ctx_t *ctx, if (ngx_rtmp_amf_put(ctx, &max_index, 4) != NGX_OK) { return NGX_ERROR; } + /* fall through */ case NGX_RTMP_AMF_OBJECT: type8 = NGX_RTMP_AMF_END; diff --git a/ngx_rtmp_auto_push_module.c b/ngx_rtmp_auto_push_module.c index 2f3533db0..60c85d77b 100644 --- a/ngx_rtmp_auto_push_module.c +++ b/ngx_rtmp_auto_push_module.c @@ -93,6 +93,34 @@ ngx_module_t ngx_rtmp_auto_push_module = { }; +static ngx_rtmp_module_t ngx_rtmp_auto_push_index_module_ctx = { + NULL, /* preconfiguration */ + NULL, /* postconfiguration */ + NULL, /* create main configuration */ + NULL, /* init main configuration */ + NULL, /* create server configuration */ + NULL, /* merge server configuration */ + NULL, /* create app configuration */ + NULL /* merge app configuration */ +}; + + +ngx_module_t ngx_rtmp_auto_push_index_module = { + NGX_MODULE_V1, + &ngx_rtmp_auto_push_index_module_ctx, /* module context */ + NULL, /* module directives */ + NGX_RTMP_MODULE, /* module type */ + NULL, /* init master */ + NULL, /* init module */ + NULL, /* init process */ + NULL, /* init thread */ + NULL, /* exit thread */ + NULL, /* exit process */ + NULL, /* exit master */ + NGX_MODULE_V1_PADDING +}; + + #define NGX_RTMP_AUTO_PUSH_SOCKNAME "nginx-rtmp" @@ -324,7 +352,7 @@ ngx_rtmp_auto_push_reconnect(ngx_event_t *ev) apcf = (ngx_rtmp_auto_push_conf_t *) ngx_get_conf(ngx_cycle->conf_ctx, ngx_rtmp_auto_push_module); - ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_auto_push_module); + ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_auto_push_index_module); if (ctx == NULL) { return; } @@ -461,14 +489,14 @@ ngx_rtmp_auto_push_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v) goto next; } - ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_auto_push_module); + ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_auto_push_index_module); if (ctx == NULL) { ctx = ngx_palloc(s->connection->pool, sizeof(ngx_rtmp_auto_push_ctx_t)); if (ctx == NULL) { goto next; } - ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_auto_push_module); + ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_auto_push_index_module); } ngx_memzero(ctx, sizeof(*ctx)); @@ -508,7 +536,7 @@ ngx_rtmp_auto_push_delete_stream(ngx_rtmp_session_t *s, goto next; } - ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_auto_push_module); + ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_auto_push_index_module); if (ctx) { if (ctx->push_evt.timer_set) { ngx_del_timer(&ctx->push_evt); @@ -532,7 +560,7 @@ ngx_rtmp_auto_push_delete_stream(ngx_rtmp_session_t *s, slot, &rctx->app, &rctx->name); pctx = ngx_rtmp_get_module_ctx(rctx->publish->session, - ngx_rtmp_auto_push_module); + ngx_rtmp_auto_push_index_module); if (pctx == NULL) { goto next; } diff --git a/ngx_rtmp_core_module.c b/ngx_rtmp_core_module.c index 01303c650..a33fa169f 100644 --- a/ngx_rtmp_core_module.c +++ b/ngx_rtmp_core_module.c @@ -332,6 +332,7 @@ ngx_rtmp_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) void *mconf; ngx_uint_t m; ngx_conf_t pcf; + ngx_module_t **modules; ngx_rtmp_module_t *module; ngx_rtmp_conf_ctx_t *ctx, *rtmp_ctx; ngx_rtmp_core_srv_conf_t *cscf, **cscfp; @@ -357,12 +358,18 @@ ngx_rtmp_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } - for (m = 0; ngx_modules[m]; m++) { - if (ngx_modules[m]->type != NGX_RTMP_MODULE) { +#if (nginx_version >= 1009011) + modules = cf->cycle->modules; +#else + modules = ngx_modules; +#endif + + for (m = 0; modules[m]; m++) { + if (modules[m]->type != NGX_RTMP_MODULE) { continue; } - module = ngx_modules[m]->ctx; + module = modules[m]->ctx; if (module->create_srv_conf) { mconf = module->create_srv_conf(cf); @@ -370,7 +377,7 @@ ngx_rtmp_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } - ctx->srv_conf[ngx_modules[m]->ctx_index] = mconf; + ctx->srv_conf[modules[m]->ctx_index] = mconf; } if (module->create_app_conf) { @@ -379,7 +386,7 @@ ngx_rtmp_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } - ctx->app_conf[ngx_modules[m]->ctx_index] = mconf; + ctx->app_conf[modules[m]->ctx_index] = mconf; } } @@ -419,6 +426,7 @@ ngx_rtmp_core_application(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) ngx_int_t i; ngx_str_t *value; ngx_conf_t save; + ngx_module_t **modules; ngx_rtmp_module_t *module; ngx_rtmp_conf_ctx_t *ctx, *pctx; ngx_rtmp_core_srv_conf_t *cscf; @@ -438,17 +446,22 @@ ngx_rtmp_core_application(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } - for (i = 0; ngx_modules[i]; i++) { - if (ngx_modules[i]->type != NGX_RTMP_MODULE) { +#if (nginx_version >= 1009011) + modules = cf->cycle->modules; +#else + modules = ngx_modules; +#endif + + for (i = 0; modules[i]; i++) { + if (modules[i]->type != NGX_RTMP_MODULE) { continue; } - module = ngx_modules[i]->ctx; + module = modules[i]->ctx; if (module->create_app_conf) { - ctx->app_conf[ngx_modules[i]->ctx_index] = - module->create_app_conf(cf); - if (ctx->app_conf[ngx_modules[i]->ctx_index] == NULL) { + ctx->app_conf[modules[i]->ctx_index] = module->create_app_conf(cf); + if (ctx->app_conf[modules[i]->ctx_index] == NULL) { return NGX_CONF_ERROR; } } @@ -489,6 +502,7 @@ ngx_rtmp_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) ngx_str_t *value; ngx_url_t u; ngx_uint_t i, m; + ngx_module_t **modules; struct sockaddr *sa; ngx_rtmp_listen_t *ls; struct sockaddr_in *sin; @@ -545,7 +559,9 @@ ngx_rtmp_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) break; } - if (ngx_memcmp(ls[i].sockaddr + off, u.sockaddr + off, len) != 0) { + if (ngx_memcmp(ls[i].sockaddr + off, (u_char *) &u.sockaddr + off, len) + != 0) + { continue; } @@ -565,14 +581,20 @@ ngx_rtmp_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) ngx_memzero(ls, sizeof(ngx_rtmp_listen_t)); - ngx_memcpy(ls->sockaddr, u.sockaddr, u.socklen); + ngx_memcpy(ls->sockaddr, (u_char *) &u.sockaddr, u.socklen); ls->socklen = u.socklen; ls->wildcard = u.wildcard; ls->ctx = cf->ctx; - for (m = 0; ngx_modules[m]; m++) { - if (ngx_modules[m]->type != NGX_RTMP_MODULE) { +#if (nginx_version >= 1009011) + modules = cf->cycle->modules; +#else + modules = ngx_modules; +#endif + + for (m = 0; modules[m]; m++) { + if (modules[m]->type != NGX_RTMP_MODULE) { continue; } } diff --git a/ngx_rtmp_eval.c b/ngx_rtmp_eval.c index 24e1f8072..6c2143f07 100644 --- a/ngx_rtmp_eval.c +++ b/ngx_rtmp_eval.c @@ -84,12 +84,11 @@ static void ngx_rtmp_eval_append_var(void *ctx, ngx_buf_t *b, ngx_rtmp_eval_t **e, ngx_str_t *name, ngx_log_t *log) { - ngx_uint_t k; ngx_str_t v; ngx_rtmp_eval_t *ee; for (; *e; ++e) { - for (k = 0, ee = *e; ee->handler; ++k, ++ee) { + for (ee = *e; ee->handler; ++ee) { if (ee->name.len == name->len && ngx_memcmp(ee->name.data, name->data, name->len) == 0) { @@ -154,6 +153,7 @@ ngx_rtmp_eval(void *ctx, ngx_str_t *in, ngx_rtmp_eval_t **e, ngx_str_t *out, name.len = p - name.data; ngx_rtmp_eval_append_var(ctx, &b, e, &name, log); + /* fall through */ case NORMAL: switch (c) { @@ -166,6 +166,8 @@ ngx_rtmp_eval(void *ctx, ngx_str_t *in, ngx_rtmp_eval_t **e, ngx_str_t *out, continue; } + /* fall through */ + case ESCAPE: ngx_rtmp_eval_append(&b, &c, 1, log); state = NORMAL; diff --git a/ngx_rtmp_handler.c b/ngx_rtmp_handler.c index ac78a6fde..b3f12e046 100644 --- a/ngx_rtmp_handler.c +++ b/ngx_rtmp_handler.c @@ -241,7 +241,9 @@ ngx_rtmp_recv(ngx_event_t *rev) "reusing formerly read data: %d", old_size); b->pos = b->start; - b->last = ngx_movemem(b->pos, old_pos, old_size); + + size = ngx_min((size_t) (b->end - b->start), old_size); + b->last = ngx_movemem(b->pos, old_pos, size); if (s->in_chunk_size_changing) { ngx_rtmp_finalize_set_chunk_size(s); @@ -568,7 +570,10 @@ ngx_rtmp_prepare_message(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, { ngx_chain_t *l; u_char *p, *pp; - ngx_int_t hsize, thsize, nbufs; + ngx_int_t hsize, thsize; +#if (NGX_DEBUG) + ngx_int_t nbufs; +#endif uint32_t mlen, timestamp, ext_timestamp; static uint8_t hdrsize[] = { 12, 8, 4, 1 }; u_char th[7]; @@ -589,10 +594,14 @@ ngx_rtmp_prepare_message(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, /* detect packet size */ mlen = 0; +#if (NGX_DEBUG) nbufs = 0; +#endif for(l = out; l; l = l->next) { mlen += (l->buf->last - l->buf->pos); +#if (NGX_DEBUG) ++nbufs; +#endif } fmt = 0; @@ -821,6 +830,12 @@ ngx_rtmp_set_chunk_size(ngx_rtmp_session_t *s, ngx_uint_t size) ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "setting chunk_size=%ui", size); + if (size > NGX_RTMP_MAX_CHUNK_SIZE) { + ngx_log_error(NGX_LOG_ALERT, s->connection->log, 0, + "too big RTMP chunk size:%ui", size); + return NGX_ERROR; + } + cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module); s->in_old_pool = s->in_pool; diff --git a/ngx_rtmp_handshake.c b/ngx_rtmp_handshake.c index d58fcff47..409d9a0dd 100644 --- a/ngx_rtmp_handshake.c +++ b/ngx_rtmp_handshake.c @@ -104,30 +104,37 @@ static ngx_int_t ngx_rtmp_make_digest(ngx_str_t *key, ngx_buf_t *src, u_char *skip, u_char *dst, ngx_log_t *log) { - static HMAC_CTX hmac; - static unsigned hmac_initialized; + static HMAC_CTX *hmac; unsigned int len; - if (!hmac_initialized) { - HMAC_CTX_init(&hmac); - hmac_initialized = 1; + if (hmac == NULL) { +#if OPENSSL_VERSION_NUMBER < 0x10100000L + static HMAC_CTX shmac; + hmac = &shmac; + HMAC_CTX_init(hmac); +#else + hmac = HMAC_CTX_new(); + if (hmac == NULL) { + return NGX_ERROR; + } +#endif } - HMAC_Init_ex(&hmac, key->data, key->len, EVP_sha256(), NULL); + HMAC_Init_ex(hmac, key->data, key->len, EVP_sha256(), NULL); if (skip && src->pos <= skip && skip <= src->last) { if (skip != src->pos) { - HMAC_Update(&hmac, src->pos, skip - src->pos); + HMAC_Update(hmac, src->pos, skip - src->pos); } if (src->last != skip + NGX_RTMP_HANDSHAKE_KEYLEN) { - HMAC_Update(&hmac, skip + NGX_RTMP_HANDSHAKE_KEYLEN, + HMAC_Update(hmac, skip + NGX_RTMP_HANDSHAKE_KEYLEN, src->last - skip - NGX_RTMP_HANDSHAKE_KEYLEN); } } else { - HMAC_Update(&hmac, src->pos, src->last - src->pos); + HMAC_Update(hmac, src->pos, src->last - src->pos); } - HMAC_Final(&hmac, dst, &len); + HMAC_Final(hmac, dst, &len); return NGX_OK; } diff --git a/ngx_rtmp_init.c b/ngx_rtmp_init.c index 97c7e9a84..459406d30 100644 --- a/ngx_rtmp_init.c +++ b/ngx_rtmp_init.c @@ -79,6 +79,7 @@ ngx_rtmp_init_connection(ngx_connection_t *c) case AF_UNIX: unix_socket = 1; + /* fall through */ default: /* AF_INET */ sin = (struct sockaddr_in *) sa; @@ -110,6 +111,7 @@ ngx_rtmp_init_connection(ngx_connection_t *c) case AF_UNIX: unix_socket = 1; + /* fall through */ default: /* AF_INET */ addr = port->addrs; @@ -256,6 +258,17 @@ ngx_rtmp_close_connection(ngx_connection_t *c) ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, "close connection"); +#if (NGX_SSL) + + if (c->ssl) { + if (ngx_ssl_shutdown(c) == NGX_AGAIN) { + c->ssl->handler = ngx_rtmp_close_connection; + return; + } + } + +#endif + #if (NGX_STAT_STUB) (void) ngx_atomic_fetch_add(ngx_stat_active, -1); #endif diff --git a/ngx_rtmp_notify_module.c b/ngx_rtmp_notify_module.c index 2fcfffb1a..dd65e4991 100644 --- a/ngx_rtmp_notify_module.c +++ b/ngx_rtmp_notify_module.c @@ -892,6 +892,7 @@ ngx_rtmp_notify_parse_http_header(ngx_rtmp_session_t *s, n = 0; state = parse_name; + /* fall through */ case parse_name: switch (c) { @@ -919,6 +920,7 @@ ngx_rtmp_notify_parse_http_header(ngx_rtmp_session_t *s, break; } state = parse_value; + /* fall through */ case parse_value: if (c == '\n') { diff --git a/ngx_rtmp_record_module.c b/ngx_rtmp_record_module.c index e54a30b41..dc2de12c4 100644 --- a/ngx_rtmp_record_module.c +++ b/ngx_rtmp_record_module.c @@ -1195,6 +1195,7 @@ ngx_rtmp_record_recorder(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) ngx_int_t i; ngx_str_t *value; ngx_conf_t save; + ngx_module_t **modules; ngx_rtmp_module_t *module; ngx_rtmp_core_app_conf_t *cacf, **pcacf, *rcacf; ngx_rtmp_record_app_conf_t *racf, **pracf, *rracf; @@ -1221,17 +1222,22 @@ ngx_rtmp_record_recorder(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) return NGX_CONF_ERROR; } - for (i = 0; ngx_modules[i]; i++) { - if (ngx_modules[i]->type != NGX_RTMP_MODULE) { +#if (nginx_version >= 1009011) + modules = cf->cycle->modules; +#else + modules = ngx_modules; +#endif + + for (i = 0; modules[i]; i++) { + if (modules[i]->type != NGX_RTMP_MODULE) { continue; } - module = ngx_modules[i]->ctx; + module = modules[i]->ctx; if (module->create_app_conf) { - ctx->app_conf[ngx_modules[i]->ctx_index] = - module->create_app_conf(cf); - if (ctx->app_conf[ngx_modules[i]->ctx_index] == NULL) { + ctx->app_conf[modules[i]->ctx_index] = module->create_app_conf(cf); + if (ctx->app_conf[modules[i]->ctx_index] == NULL) { return NGX_CONF_ERROR; } }