Skip to content

Commit

Permalink
Merge pull request #80 from gnolizuh/release
Browse files Browse the repository at this point in the history
逻辑优化
  • Loading branch information
gnolizuh authored Jul 21, 2017
2 parents 27af4ab + eec5789 commit af85d04
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 143 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ A nginx.conf example:

application news {
live on;
http_flv on;
gop_cache on;
gop_cache_count 5; # cache 5 GOPs

Expand Down
1 change: 0 additions & 1 deletion README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ BLSS是一个NGINX第三方模块,它基于开源项目[nginx-rtmp-module](htt

application news {
live on;
http_flv on;
gop_cache on;
gop_cache_count 5; # cache 5 GOPs

Expand Down
166 changes: 52 additions & 114 deletions ngx_http_flv_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,28 @@ static char * ngx_http_flv_http_merge_conf(ngx_conf_t *cf, void *parent, void *c

/* rtmp handler registered */
static ngx_int_t ngx_http_flv_rtmp_init(ngx_conf_t *cf);
static void * ngx_http_flv_rtmp_create_app_conf(ngx_conf_t *cf);
static char * ngx_http_flv_rtmp_merge_app_conf(ngx_conf_t *cf, void *parent, void *child);

static ngx_int_t ngx_http_flv_send_message(ngx_rtmp_session_t *s, ngx_chain_t *out, ngx_uint_t priority);
static ngx_int_t ngx_http_flv_connect_local(ngx_http_request_t *r, ngx_str_t *app, ngx_str_t *name);
static void ngx_http_flv_http_send_header(ngx_rtmp_session_t *s, ngx_rtmp_session_t *ps);
static ngx_int_t ngx_http_flv_http_send_message(ngx_rtmp_session_t *s, ngx_chain_t *in, ngx_uint_t priority);
static ngx_chain_t * ngx_http_flv_http_append_shared_bufs(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_rtmp_header_t *lh, ngx_chain_t *in);
static void ngx_http_flv_http_free_shared_chain(ngx_rtmp_session_t *s, ngx_chain_t *in);


static u_char ngx_http_flv_header[] = {
"HTTP/1.1 200 OK\r\n"
"Cache-Control: no-cache\r\n"
"Content-Type: video/x-flv\r\n"
"Connection: close\r\n"
"Expires: -1\r\n"
"Pragma: no-cache\r\n"
"\r\n"
};


ngx_rtmp_send_handler_t ngx_http_flv_send_handler = {
ngx_http_flv_http_send_header,
ngx_http_flv_http_send_message,
ngx_http_flv_http_append_shared_bufs,
ngx_http_flv_http_free_shared_chain
Expand Down Expand Up @@ -89,19 +100,6 @@ ngx_module_t ngx_http_flv_httpmodule = {
};


static ngx_command_t ngx_http_flv_rtmpcommands[] = {

{ ngx_string("http_flv"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_SVI_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
NGX_RTMP_APP_CONF_OFFSET,
offsetof(ngx_http_flv_rtmp_app_conf_t, http_flv),
NULL },

ngx_null_command
};


static ngx_rtmp_module_t ngx_http_flv_rtmpmodule_ctx = {
NULL, /* preconfiguration */
ngx_http_flv_rtmp_init, /* postconfiguration */
Expand All @@ -111,15 +109,15 @@ static ngx_rtmp_module_t ngx_http_flv_rtmpmodule_ctx = {
NULL, /* merge server configuration */
NULL, /* create service configuration */
NULL, /* merge service configuration */
ngx_http_flv_rtmp_create_app_conf, /* create application configuration */
ngx_http_flv_rtmp_merge_app_conf, /* merge application configuration */
NULL, /* create application configuration */
NULL, /* merge application configuration */
};


ngx_module_t ngx_http_flv_rtmpmodule = {
NGX_MODULE_V1,
&ngx_http_flv_rtmpmodule_ctx, /* module context */
ngx_http_flv_rtmpcommands, /* module directives */
NULL, /* module directives */
NGX_RTMP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
Expand Down Expand Up @@ -456,34 +454,6 @@ ngx_http_flv_http_merge_conf(ngx_conf_t *cf, void *parent, void *child)
}


static void *
ngx_http_flv_rtmp_create_app_conf(ngx_conf_t *cf)
{
ngx_http_flv_rtmp_app_conf_t *hacf;

hacf = ngx_pcalloc(cf->pool, sizeof(ngx_http_flv_rtmp_app_conf_t));
if (hacf == NULL) {
return NULL;
}

hacf->http_flv = NGX_CONF_UNSET;

return hacf;
}


static char *
ngx_http_flv_rtmp_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_flv_rtmp_app_conf_t *prev = parent;
ngx_http_flv_rtmp_app_conf_t *conf = child;

ngx_conf_merge_value(conf->http_flv, prev->http_flv, 0);

return NGX_CONF_OK;
}


static ngx_int_t
ngx_http_flv_connect_end(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
Expand Down Expand Up @@ -562,6 +532,42 @@ ngx_http_flv_append_shared_bufs(ngx_rtmp_core_srv_conf_t *cscf, ngx_rtmp_header_
}


static void
ngx_http_flv_http_send_header(ngx_rtmp_session_t *s, ngx_rtmp_session_t *ps)
{
ngx_rtmp_core_srv_conf_t *cscf;
ngx_rtmp_codec_ctx_t *codec_ctx;
ngx_chain_t c1, c2, *pkt;
ngx_buf_t b1, b2;

u_char flv_header[] = "FLV\x1\0\0\0\0\x9\0\0\0\0";

cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);

codec_ctx = ngx_rtmp_get_module_ctx(ps, ngx_rtmp_codec_module);
if (codec_ctx != NULL) {
if (codec_ctx->video_header != NULL) flv_header[4] |= 0x01;
if (codec_ctx->aac_header != NULL) flv_header[4] |= 0x04;
}

c1.buf = &b1;
c2.buf = &b2;
c1.next = &c2;
c2.next = NULL;

b1.start = b1.pos = &ngx_http_flv_header[0];
b1.end = b1.last = b1.pos + sizeof(ngx_http_flv_header) - 1;

b2.start = b2.pos = &flv_header[0];
b2.end = b2.last = b2.pos + sizeof(flv_header) - 1;

pkt = ngx_rtmp_append_shared_bufs(cscf, NULL, &c1);

ngx_http_flv_send_message(s, pkt, 0);

ngx_rtmp_free_shared_chain(cscf, pkt);
}

static ngx_int_t
ngx_http_flv_http_send_message(ngx_rtmp_session_t *s, ngx_chain_t *in, ngx_uint_t priority)
{
Expand Down Expand Up @@ -597,62 +603,6 @@ ngx_http_flv_http_free_shared_chain(ngx_rtmp_session_t *s, ngx_chain_t *in)
}


static ngx_int_t
ngx_http_flv_send_header(ngx_rtmp_session_t *s)
{
static u_char httpheader[] = {
"HTTP/1.1 200 OK\r\n"
"Cache-Control: no-cache\r\n"
"Content-Type: video/x-flv\r\n"
"Connection: close\r\n"
"Expires: -1\r\n"
"Pragma: no-cache\r\n"
"\r\n"
};

static u_char flvheader[] = {
0x46, /* 'F' */
0x4c, /* 'L' */
0x56, /* 'V' */
0x01, /* version = 1 */
0x05, /* 00000 1 0 1 = has audio & video */
0x00,
0x00,
0x00,
0x09, /* header size */
0x00,
0x00,
0x00,
0x00 /* PreviousTagSize0 (not actually a header) */
};

ngx_rtmp_core_srv_conf_t *cscf;
ngx_chain_t c1, c2, *pkt;
ngx_buf_t b1, b2;

cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);

c1.buf = &b1;
c2.buf = &b2;
c1.next = &c2;
c2.next = NULL;

b1.start = b1.pos = &httpheader[0];
b1.end = b1.last = b1.pos + sizeof(httpheader) - 1;

b2.start = b2.pos = &flvheader[0];
b2.end = b2.last = b2.pos + sizeof(flvheader);

pkt = ngx_rtmp_append_shared_bufs(cscf, NULL, &c1);

ngx_http_flv_send_message(s, pkt, 0);

ngx_rtmp_free_shared_chain(cscf, pkt);

return NGX_OK;
}


static void
ngx_http_flv_start(ngx_rtmp_session_t *s)
{
Expand Down Expand Up @@ -819,18 +769,12 @@ ngx_http_flv_close_stream(ngx_rtmp_session_t *s, ngx_rtmp_close_stream_t *v)
static ngx_int_t
ngx_http_flv_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
{
ngx_http_flv_rtmp_app_conf_t *hacf;
ngx_http_flv_rtmp_ctx_t *ctx;

if (s->proto != NGX_PROTO_TYPE_HTTP_FLV_PULL) {
goto next;
}

hacf = ngx_rtmp_get_module_app_conf(s, ngx_http_flv_rtmpmodule);
if (hacf == NULL || !hacf->http_flv) {
goto next;
}

ctx = ngx_rtmp_get_module_ctx(s, ngx_http_flv_rtmpmodule);
if (ctx == NULL) {
goto next;
Expand All @@ -845,12 +789,6 @@ ngx_http_flv_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)

ngx_http_flv_join(s, v->name, 0);

if (!ctx->initialized) {
ngx_http_flv_send_header(s);

ctx->initialized = 1;
}

ngx_rtmp_playing++;

next:
Expand Down
9 changes: 1 addition & 8 deletions ngx_http_flv_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@
#include "ngx_rtmp.h"


typedef struct {
unsigned initialized:1;
} ngx_http_flv_rtmp_ctx_t;


typedef struct {
ngx_flag_t http_flv;
} ngx_http_flv_rtmp_app_conf_t;
typedef struct {} ngx_http_flv_rtmp_ctx_t;


typedef struct {
Expand Down
2 changes: 1 addition & 1 deletion ngx_rtmp_control_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ ngx_rtmp_control_walk_stream(ngx_http_request_t *r,
const char *s;
ngx_rtmp_live_ctx_t *lctx;

for (n = 0; n < 2; ++ n) {
for (n = 0; n < NGX_RTMP_LIVE_TYPE_SIZE; ++ n) {
for (lctx = ls->ctx[n]; lctx; lctx = lctx->next) {
s = ngx_rtmp_control_walk_session(r, lctx);
if (s != NGX_CONF_OK) {
Expand Down
28 changes: 18 additions & 10 deletions ngx_rtmp_gop_cache_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *ss)
{
ngx_rtmp_session_t *s;
ngx_chain_t *pkt, *apkt, *meta, *header;
ngx_rtmp_live_ctx_t *ctx, *pctx;
ngx_rtmp_live_ctx_t *pctx;
ngx_rtmp_gop_cache_ctx_t *gctx;
ngx_rtmp_live_app_conf_t *lacf;
ngx_rtmp_gop_cache_t *cache;
Expand All @@ -571,9 +571,9 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *ss)
return;
}

ctx = ngx_rtmp_get_module_ctx(ss, ngx_rtmp_live_module);
if (ctx == NULL || ctx->stream == NULL ||
ctx->stream->pctx == NULL || !ctx->stream->publishing) {
pctx = ngx_rtmp_get_module_ctx(ss, ngx_rtmp_live_module);
if (pctx == NULL || pctx->stream == NULL ||
pctx->stream->pctx == NULL || !pctx->stream->publishing) {
return;
}

Expand All @@ -583,15 +583,13 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *ss)
header = NULL;
meta_version = 0;

s = ctx->stream->pctx->session;
s = pctx->stream->pctx->session;

gctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_gop_cache_module);
if (gctx == NULL) {
return;
}

pctx = ctx->stream->pctx;

handler = ngx_rtmp_send_handlers[ss->proto == NGX_PROTO_TYPE_HTTP_FLV_PULL ? 1 : 0];

for (cache = gctx->head; cache; cache = cache->next) {
Expand All @@ -601,20 +599,30 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *ss)
meta_version = cache->meta_version;
}

if (!pctx->header) {
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0,
"gop cache send: header");

handler->send_header(ss, s);

pctx->header = 1;
}

/* send metadata */
if (meta && meta_version != ctx->meta_version) {

if (meta && meta_version != pctx->meta_version) {
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0,
"gop cache send: meta");

if (handler->send_message(ss, meta, 0) == NGX_OK) {
ctx->meta_version = meta_version;
pctx->meta_version = meta_version;
}
}

for (gop_frame = cache->head; gop_frame; gop_frame = gop_frame->next) {
csidx = !(lacf->interleave || gop_frame->h.type == NGX_RTMP_MSG_VIDEO);

cs = &ctx->cs[csidx];
cs = &pctx->cs[csidx];

lh = ch = gop_frame->h;

Expand Down
1 change: 1 addition & 0 deletions ngx_rtmp_gop_cache_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ typedef struct ngx_rtmp_gop_cache_s ngx_rtmp_gop_cache_t;


typedef struct {
void (*send_header)(ngx_rtmp_session_t *s, ngx_rtmp_session_t *ps);
ngx_int_t (*send_message)(ngx_rtmp_session_t *s, ngx_chain_t *in, ngx_uint_t priority);
ngx_chain_t *(*append_shared_bufs)(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_rtmp_header_t *lh, ngx_chain_t *in);
void (*free_shared_chain)(ngx_rtmp_session_t *s, ngx_chain_t *in);
Expand Down
Loading

0 comments on commit af85d04

Please sign in to comment.