Skip to content

Commit

Permalink
nginx-rtmp-module: apply fixes from open PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
erankor committed Sep 15, 2019
1 parent 0f335f5 commit b0aec7a
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 18 deletions.
6 changes: 4 additions & 2 deletions nginx-rtmp-module/ngx_rtmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ typedef struct {
} ngx_rtmp_addr_conf_t;

typedef struct {
in_addr_t addr;
ngx_rtmp_addr_conf_t conf;
in_addr_t addr;
} ngx_rtmp_in_addr_t;


#if (NGX_HAVE_INET6)

typedef struct {
struct in6_addr addr6;
ngx_rtmp_addr_conf_t conf;
struct in6_addr addr6;
} ngx_rtmp_in6_addr_t;

#endif
Expand Down Expand Up @@ -267,6 +267,7 @@ typedef struct {
ngx_int_t in_chunk_size_changing;

ngx_connection_t *connection;
ngx_fd_t dump_fd;

/* circular buffer of RTMP message pointers */
ngx_msec_t timeout;
Expand Down Expand Up @@ -339,6 +340,7 @@ typedef struct ngx_rtmp_core_srv_conf_s {
size_t out_queue;
size_t out_cork;
ngx_msec_t buflen;
ngx_str_t dump_folder;

ngx_rtmp_conf_ctx_t *ctx;
} ngx_rtmp_core_srv_conf_t;
Expand Down
4 changes: 2 additions & 2 deletions nginx-rtmp-module/ngx_rtmp_amf.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ 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 */
Expand Down Expand Up @@ -373,7 +373,7 @@ ngx_rtmp_amf_read(ngx_rtmp_amf_ctx_t *ctx, ngx_rtmp_amf_elt_t *elts,
if (data == NULL) {
rc = ngx_rtmp_amf_get(ctx, data, len);

} else if (elts->len <= len) {
} else if (elts && elts->len <= len) {
rc = ngx_rtmp_amf_get(ctx, data, elts->len - 1);
if (rc != NGX_OK)
return NGX_ERROR;
Expand Down
2 changes: 1 addition & 1 deletion nginx-rtmp-module/ngx_rtmp_codec_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ ngx_rtmp_codec_dump_header(ngx_rtmp_session_t *s, const char *type,
u_char hex[] = "0123456789abcdef";

for (pp = buf, p = in->buf->pos;
p < in->buf->last && pp < buf + sizeof(buf) - 1;
p < in->buf->last && pp < buf + sizeof(buf) - 2;
++p)
{
*pp++ = hex[*p >> 4];
Expand Down
8 changes: 8 additions & 0 deletions nginx-rtmp-module/ngx_rtmp_core_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ static ngx_command_t ngx_rtmp_core_commands[] = {
offsetof(ngx_rtmp_core_srv_conf_t, buflen),
NULL },

{ ngx_string("dump_folder"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_RTMP_SRV_CONF_OFFSET,
offsetof(ngx_rtmp_core_srv_conf_t, dump_folder),
NULL },

ngx_null_command
};

Expand Down Expand Up @@ -277,6 +284,7 @@ ngx_rtmp_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_value(conf->publish_time_fix, prev->publish_time_fix, 1);
ngx_conf_merge_msec_value(conf->buflen, prev->buflen, 1000);
ngx_conf_merge_value(conf->busy, prev->busy, 0);
ngx_conf_merge_str_value(conf->dump_folder, prev->dump_folder, "");

if (prev->pool == NULL) {
prev->pool = ngx_create_pool(4096, &cf->cycle->new_log);
Expand Down
28 changes: 21 additions & 7 deletions nginx-rtmp-module/ngx_rtmp_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,15 @@ ngx_rtmp_recv(ngx_event_t *rev)
return;
}

if (s->dump_fd != NGX_INVALID_FILE) {
if (ngx_write_fd(s->dump_fd, b->last, n) == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, s->connection->log, ngx_errno,
"failed to write to rtmp dump file");
ngx_close_file(s->dump_fd);
s->dump_fd = NGX_INVALID_FILE;
}
}

s->ping_reset = 1;
ngx_rtmp_update_bandwidth(&ngx_rtmp_bw_in, n);
b->last += n;
Expand Down Expand Up @@ -771,18 +780,22 @@ ngx_rtmp_receive_message(ngx_rtmp_session_t *s,

#ifdef NGX_DEBUG
{
int nbufs;
uint32_t mlen;
ngx_int_t nbufs;
ngx_chain_t *ch;

for(nbufs = 1, ch = in;
ch->next;
ch = ch->next, ++nbufs);
mlen = 0;
nbufs = 0;
for (ch = in; ch; ch = ch->next) {
mlen += (ch->buf->last - ch->buf->pos);
++nbufs;
}

ngx_log_debug7(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
ngx_log_debug8(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"RTMP recv %s (%d) csid=%D timestamp=%D "
"mlen=%D msid=%D nbufs=%d",
"mlen=%D msid=%D nbufs=%d real_mlen=%D",
ngx_rtmp_message_type(h->type), (int)h->type,
h->csid, h->timestamp, h->mlen, h->msid, nbufs);
h->csid, h->timestamp, h->mlen, h->msid, nbufs, mlen);
}
#endif

Expand Down Expand Up @@ -902,6 +915,7 @@ ngx_rtmp_set_chunk_size(ngx_rtmp_session_t *s, ngx_uint_t size)

bi->pos += (ngx_cpymem(bo->last, bi->pos,
bo->end - bo->last) - bo->last);
bo->last = bo->end;
lo->next = ngx_rtmp_alloc_in_buf(s);
lo = lo->next;
if (lo == NULL) {
Expand Down
9 changes: 9 additions & 0 deletions nginx-rtmp-module/ngx_rtmp_handshake.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,15 @@ ngx_rtmp_handshake_recv(ngx_event_t *rev)
return;
}

if (s->dump_fd != NGX_INVALID_FILE) {
if (ngx_write_fd(s->dump_fd, b->last, n) == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, s->connection->log, ngx_errno,
"failed to write to rtmp dump file");
ngx_close_file(s->dump_fd);
s->dump_fd = NGX_INVALID_FILE;
}
}

b->last += n;
}

Expand Down
46 changes: 45 additions & 1 deletion nginx-rtmp-module/ngx_rtmp_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include "ngx_rtmp_proxy_protocol.h"


#define NGX_RTMP_ISO8601_DATE_LEN (sizeof("yyyy-mm-dd") - 1)


static void ngx_rtmp_close_connection(ngx_connection_t *c);
static u_char * ngx_rtmp_log_error(ngx_log_t *log, u_char *buf, size_t len);

Expand Down Expand Up @@ -141,6 +144,42 @@ ngx_rtmp_init_connection(ngx_connection_t *c)
}
}

static ngx_fd_t
ngx_rtmp_open_dump_file(ngx_rtmp_session_t *s)
{
ngx_fd_t fd;
ngx_str_t name;
ngx_rtmp_core_srv_conf_t *cscf;

cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);

if (cscf->dump_folder.len == 0) {
return NGX_INVALID_FILE;
}

name.len = cscf->dump_folder.len + sizeof("/ngx_rtmp_dump___.dat") +
NGX_RTMP_ISO8601_DATE_LEN + NGX_INT64_LEN + NGX_ATOMIC_T_LEN;
name.data = ngx_alloc(name.len, s->connection->log);
if (name.data == NULL) {
return NGX_INVALID_FILE;
}

ngx_sprintf(name.data, "%V/ngx_rtmp_dump_%*s_%P_%uA.dat%Z",
&cscf->dump_folder, NGX_RTMP_ISO8601_DATE_LEN,
ngx_cached_http_log_iso8601.data, ngx_pid, s->connection->number);

fd = ngx_open_file((char*)name.data, NGX_FILE_WRONLY, NGX_FILE_TRUNCATE,
NGX_FILE_DEFAULT_ACCESS);
if (fd == NGX_INVALID_FILE) {
ngx_log_error(NGX_LOG_ERR, s->connection->log, ngx_errno,
"failed to open rtmp dump file");
ngx_free(name.data);
return NGX_INVALID_FILE;
}

ngx_free(name.data);
return fd;
}

ngx_rtmp_session_t *
ngx_rtmp_init_session(ngx_connection_t *c, ngx_rtmp_addr_conf_t *addr_conf)
Expand All @@ -151,7 +190,7 @@ ngx_rtmp_init_session(ngx_connection_t *c, ngx_rtmp_addr_conf_t *addr_conf)

s = ngx_pcalloc(c->pool, sizeof(ngx_rtmp_session_t) +
sizeof(ngx_chain_t *) * ((ngx_rtmp_core_srv_conf_t *)
addr_conf->ctx-> srv_conf[ngx_rtmp_core_module
addr_conf->ctx->srv_conf[ngx_rtmp_core_module
.ctx_index])->out_queue);
if (s == NULL) {
ngx_rtmp_close_connection(c);
Expand Down Expand Up @@ -220,6 +259,7 @@ ngx_rtmp_init_session(ngx_connection_t *c, ngx_rtmp_addr_conf_t *addr_conf)
s->buflen = cscf->buflen;
ngx_rtmp_set_chunk_size(s, NGX_RTMP_DEFAULT_CHUNK_SIZE);

s->dump_fd = ngx_rtmp_open_dump_file(s);

if (ngx_rtmp_fire_event(s, NGX_RTMP_CONNECT, NULL, NULL) != NGX_OK) {
ngx_rtmp_finalize_session(s);
Expand Down Expand Up @@ -315,6 +355,10 @@ ngx_rtmp_close_session_handler(ngx_event_t *e)
s->out_pos %= s->out_queue;
}

if (s->dump_fd != NGX_INVALID_FILE) {
ngx_close_file(s->dump_fd);
}

ngx_rtmp_close_connection(c);
}

Expand Down
2 changes: 1 addition & 1 deletion nginx-rtmp-module/ngx_rtmp_live_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static ngx_command_t ngx_rtmp_live_commands[] = {

{ ngx_string("stream_buckets"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
ngx_conf_set_num_slot,
NGX_RTMP_APP_CONF_OFFSET,
offsetof(ngx_rtmp_live_app_conf_t, nbuckets),
NULL },
Expand Down
2 changes: 1 addition & 1 deletion nginx-rtmp-module/ngx_rtmp_send.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ ngx_rtmp_create_abort(ngx_rtmp_session_t *s, uint32_t csid)
"create: abort csid=%uD", csid);

{
NGX_RTMP_USER_START(s, NGX_RTMP_MSG_CHUNK_SIZE);
NGX_RTMP_USER_START(s, NGX_RTMP_MSG_ABORT);

NGX_RTMP_USER_OUT4(csid);

Expand Down
2 changes: 1 addition & 1 deletion nginx-rtmp-module/unused/ngx_rtmp_exec_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ ngx_rtmp_exec_child_dead(ngx_event_t *ev)
}

ngx_log_debug1(NGX_LOG_DEBUG_RTMP, e->log, 0,
"exec: shedule respawn %Mmsec", e->respawn_timeout);
"exec: schedule respawn %Mmsec", e->respawn_timeout);

e->respawn_evt.data = e;
e->respawn_evt.log = e->log;
Expand Down
4 changes: 2 additions & 2 deletions nginx-rtmp-module/unused/ngx_rtmp_limit_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ ngx_rtmp_limit_connect(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
rc = n > (ngx_uint_t) lmcf->max_conn ? NGX_ERROR : NGX_OK;

ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"limit: inc conection counter: %uD", n);
"limit: inc connection counter: %uD", n);

if (rc != NGX_OK) {
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
Expand Down Expand Up @@ -141,7 +141,7 @@ ngx_rtmp_limit_disconnect(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,

(void) n;
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"limit: dec conection counter: %uD", n);
"limit: dec connection counter: %uD", n);

return NGX_OK;
}
Expand Down

0 comments on commit b0aec7a

Please sign in to comment.