Skip to content

Commit

Permalink
Merge pull request #81 from gnolizuh/release
Browse files Browse the repository at this point in the history
IP streaming url format supported
  • Loading branch information
gnolizuh authored Jul 24, 2017
2 parents af85d04 + 5116409 commit fa27097
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 100 deletions.
9 changes: 9 additions & 0 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ BLSS是一个NGINX第三方模块,它基于开源项目[nginx-rtmp-module](htt

ffmpeg -re -i movie.flv -vcodec copy -acodec copy -f flv rtmp://test.pub.rtmp.cctv/news/test

以IP形式进行推流:

ffmpeg -re -i movie.flv -vcodec copy -acodec copy -f flv rtmp://192.168.1.100/test.pub.rtmp.cctv/news/test

### 播放

客户端需要绑定HOST:
Expand All @@ -133,3 +137,8 @@ BLSS是一个NGINX第三方模块,它基于开源项目[nginx-rtmp-module](htt

rtmp://test.sub.rtmp.cctv/news/test
http://test.sub.httpflv.cctv/news/test.flv

以IP形式进行播放

rtmp://192.168.1.100/test.sub.rtmp.cctv/news/test
http://192.168.1.100/test.sub.httpflv.cctv/news/test.flv
1 change: 0 additions & 1 deletion config
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ RTMP_DEPS=" \
RTMP_CORE_SRCS=" \
$ngx_addon_dir/ngx_rtmp.c \
$ngx_addon_dir/ngx_rtmp_init.c \
$ngx_addon_dir/ngx_rtmp_parse.c \
$ngx_addon_dir/ngx_rtmp_variables.c \
$ngx_addon_dir/ngx_rtmp_handshake.c \
$ngx_addon_dir/ngx_rtmp_handler.c \
Expand Down
100 changes: 47 additions & 53 deletions ngx_http_flv_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static char * ngx_http_flv_http_merge_conf(ngx_conf_t *cf, void *parent, void *c
static ngx_int_t ngx_http_flv_rtmp_init(ngx_conf_t *cf);

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 ngx_int_t ngx_http_flv_connect_local(ngx_rtmp_session_t *s);
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);
Expand Down Expand Up @@ -178,33 +178,6 @@ ngx_http_flv_send_message(ngx_rtmp_session_t *s, ngx_chain_t *out,
}


static ngx_int_t
ngx_http_flv_get_info(ngx_str_t *uri, ngx_str_t *app, ngx_str_t *name)
{
size_t len;

if (uri == NULL || uri->len == 0) {

return NGX_ERROR;
}

len = 0;
for(; uri->data[len] == '/' || uri->len == len; ++ len); // skip first '/'

app->data = &uri->data[len]; // we got app

for(; uri->data[len] != '/' || uri->len == len; ++ len); // reach next '/'

app->len = &uri->data[len ++] - app->data;

name->data = &uri->data[len];
name->len = &uri->data[uri->len] - name->data
- ngx_strlen(".flv"); // we got name

return NGX_OK;
}


static ngx_int_t
ngx_http_flv_play_local(ngx_rtmp_session_t *s)
{
Expand Down Expand Up @@ -255,34 +228,26 @@ ngx_http_flv_close_session_handler(ngx_rtmp_session_t *s)


static ngx_int_t
ngx_http_flv_connect_local(ngx_http_request_t *r, ngx_str_t *app, ngx_str_t *name)
ngx_http_flv_connect_local(ngx_rtmp_session_t *s)
{
static ngx_rtmp_connect_t v;

ngx_rtmp_session_t *s;
ngx_http_flv_rtmp_ctx_t *rtmpctx;
ngx_http_flv_http_ctx_t *httpctx;

httpctx = ngx_http_get_module_ctx(r, ngx_http_flv_httpmodule);

s = httpctx->rs;

ngx_memzero(&v, sizeof(ngx_rtmp_connect_t));

ngx_memcpy(v.app, app->data, ngx_min(app->len, sizeof(v.app) - 1));
ngx_memcpy(v.args, r->args.data, ngx_min(r->args.len, sizeof(v.args) - 1));
ngx_memcpy(v.app, s->app.data, ngx_min(s->app.len, sizeof(v.app) - 1));
ngx_memcpy(v.flashver, "HTTP FLV flashver", ngx_strlen("HTTP FLV flashver"));
ngx_memcpy(v.swf_url, "HTTP FLV swf_url", ngx_strlen("HTTP FLV swf_url"));
ngx_memcpy(v.page_url, "HTTP FLV page_url", ngx_strlen("HTTP FLV page_url"));

*ngx_snprintf(v.tc_url, NGX_RTMP_MAX_URL, "http://%V/%V", &s->host, app) = 0;
*ngx_snprintf(v.tc_url, NGX_RTMP_MAX_URL, "http://%V/%V", &s->host, &s->app) = 0;

#define NGX_RTMP_SET_STRPAR(name) \
s->name.len = ngx_strlen(v.name); \
s->name.data = ngx_palloc(s->connection->pool, s->name.len); \
ngx_memcpy(s->name.data, v.name, s->name.len)

NGX_RTMP_SET_STRPAR(app);
NGX_RTMP_SET_STRPAR(args);
NGX_RTMP_SET_STRPAR(flashver);
NGX_RTMP_SET_STRPAR(swf_url);
Expand All @@ -291,9 +256,6 @@ ngx_http_flv_connect_local(ngx_http_request_t *r, ngx_str_t *app, ngx_str_t *nam

#undef NGX_RTMP_SET_STRPAR

s->name.len = name->len;
s->name.data = ngx_pstrdup(s->pool, name);

rtmpctx = ngx_rtmp_get_module_ctx(s, ngx_http_flv_rtmpmodule);
if (rtmpctx == NULL) {
rtmpctx = ngx_pcalloc(s->pool, sizeof(ngx_http_flv_rtmp_ctx_t));
Expand Down Expand Up @@ -328,9 +290,11 @@ ngx_http_flv_http_handler(ngx_http_request_t *r)
{
ngx_http_flv_httploc_conf_t *hlcf;
ngx_http_cleanup_t *cln;
ngx_http_flv_http_ctx_t *httpctx;
ngx_rtmp_session_t *s;
ngx_int_t rc = 0;
ngx_str_t app, name;
ngx_int_t nslash;
u_char *p;
size_t i;

hlcf = ngx_http_get_module_loc_conf(r, ngx_http_flv_httpmodule);
Expand All @@ -350,18 +314,14 @@ ngx_http_flv_http_handler(ngx_http_request_t *r)

nslash = 0;
for (i = 0; i < r->uri.len; ++ i) {

if (r->uri.data[i] == '/') {

++ nslash;
} else if (r->uri.data[i] == '?') {

break;
}
}

if (nslash != 2) {

if (nslash > 3 || nslash < 2) {
return NGX_DECLINED;
}

Expand All @@ -380,18 +340,52 @@ ngx_http_flv_http_handler(ngx_http_request_t *r)
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"http_flv handle uri: '%V' args: '%V'", &r->uri, &r->args);

if (ngx_http_flv_get_info(&r->uri, &app, &name) != NGX_OK) {
// init session
ngx_http_flv_init_connection(r);

httpctx = ngx_http_get_module_ctx(r, ngx_http_flv_httpmodule);
if (httpctx == NULL) {
return NGX_DECLINED;
}

ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"http_flv handle app: '%V' name: '%V'", &app, &name);
// get rtmp session
s = httpctx->rs;

ngx_http_flv_init_connection(r);
s->proto = NGX_PROTO_TYPE_HTTP_FLV_PULL;
s->host_mask = NGX_RTMP_HOSTNAME_HTTP_FLV|NGX_RTMP_HOSTNAME_SUB;

p = ngx_strrlchr(r->uri.data + r->uri.len, r->uri.data + 1, '/');
if (!p) {
return NGX_DECLINED;
}

// get app
s->app.data = r->uri.data + 1;
s->app.len = p - s->app.data;

// get name
s->name.data = p + 1;
s->name.len = r->uri.data + r->uri.len - s->name.data - 4;

// get host
s->host = r->headers_in.host->value;
p = ngx_strlchr(s->host.data, s->host.data + s->host.len, ':');
if (p) {
s->host.len = p - s->host.data;
}

// get args
s->args.len = r->args.len;
s->args.data = ngx_palloc(s->connection->pool, s->args.len);
ngx_memcpy(s->args.data, r->args.data, s->args.len);

ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"http_flv handle app: '%V' name: '%V' args: '%V'",
&s->app, &s->name, &s->args);

if (ngx_http_flv_connect_local(r, &app, &name) != NGX_OK) {
ngx_rtmp_format_app(s);

if (ngx_http_flv_connect_local(s) != NGX_OK) {
return NGX_DECLINED;
}

Expand Down
30 changes: 30 additions & 0 deletions ngx_rtmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,36 @@ ngx_rtmp_is_codec_header(ngx_chain_t *in)
}


static ngx_inline u_char *
ngx_strrlchr(u_char *p, u_char *first, u_char c)
{
while (--p > first) {

if (*p == c) {
return p;
}
}

return NULL;
}


static ngx_inline void
ngx_rtmp_format_app(ngx_rtmp_session_t *s)
{
u_char *p;

p = ngx_strlchr(s->app.data, s->app.data + s->app.len, '/');
if (p) {
s->host.data = s->app.data;
s->host.len = p - s->host.data;

s->app.data = p + 1;
s->app.len = s->app.len - s->host.len - 1;
}
}


extern ngx_rtmp_bandwidth_t ngx_rtmp_bw_out;
extern ngx_rtmp_bandwidth_t ngx_rtmp_bw_in;

Expand Down
20 changes: 19 additions & 1 deletion ngx_rtmp_cmd_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ ngx_rtmp_cmd_connect_init(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,

ngx_rtmp_cmd_fill_args(v.app, v.args);

/* set host mask */
s->host_mask |= NGX_RTMP_HOSTNAME_RTMP;

ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
"connect: app='%s' args='%s' flashver='%s' swf_url='%s' "
"tc_url='%s' page_url='%s' acodecs=%uD vcodecs=%uD "
Expand Down Expand Up @@ -331,13 +334,28 @@ ngx_rtmp_cmd_connect(ngx_rtmp_session_t *s, ngx_rtmp_connect_t *v)

#undef NGX_RTMP_SET_STRPAR

ngx_rtmp_parse_tcurl(s->tc_url, &s->host, &s->host_mask);
// make host
s->host.data = s->tc_url.data + 7;
s->host.len = s->tc_url.len - 7;

p = ngx_strlchr(s->host.data, s->host.data + s->host.len, ':');
if (!p) {
p = ngx_strlchr(s->host.data, s->host.data + s->host.len, '/');
if (!p) {
p = s->host.data + s->host.len;
}
}

s->host.len = p - s->host.data;

// make app
p = ngx_strlchr(s->app.data, s->app.data + s->app.len, '?');
if (p) {
s->app.len = (p - s->app.data);
}

ngx_rtmp_format_app(s);

s->acodecs = (uint32_t) v->acodecs;
s->vcodecs = (uint32_t) v->vcodecs;

Expand Down
8 changes: 0 additions & 8 deletions ngx_rtmp_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ ngx_http_flv_init_connection(ngx_http_request_t *r)
struct sockaddr_in *sin;
ngx_rtmp_in_addr_t *addr;
ngx_int_t unix_socket;
u_char *p;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
ngx_rtmp_in6_addr_t *addr6;
Expand Down Expand Up @@ -138,12 +137,6 @@ ngx_http_flv_init_connection(ngx_http_request_t *r)
return;
}

s->host = r->headers_in.host->value;
p = ngx_strlchr(s->host.data, s->host.data + s->host.len, ':');
if (p != NULL) {
s->host.len = p - s->host.data;
}

r->read_event_handler = ngx_http_test_reading;
r->blocked = 1;

Expand Down Expand Up @@ -221,7 +214,6 @@ ngx_http_flv_init_session(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf
ngx_queue_init(&s->posted_dry_events);
#endif

s->proto = NGX_PROTO_TYPE_HTTP_FLV_PULL;
s->epoch = ngx_current_msec;
s->timeout = cscf->timeout;
s->buflen = cscf->buflen;
Expand Down
37 changes: 0 additions & 37 deletions ngx_rtmp_parse.c

This file was deleted.

0 comments on commit fa27097

Please sign in to comment.