vod-http is now working

This commit is contained in:
Roman Arutyunyan
2012-10-23 21:59:31 +04:00
parent eacfd8fac4
commit b8424c9b63
5 changed files with 85 additions and 67 deletions

View File

@ -491,15 +491,17 @@ ngx_rtmp_netcall_send(ngx_event_t *wev)
ngx_chain_t * ngx_chain_t *
ngx_rtmp_netcall_http_format_header(ngx_str_t *uri, ngx_str_t *host, ngx_rtmp_netcall_http_format_header(ngx_int_t method, ngx_str_t *uri,
ngx_pool_t *pool, size_t content_length, ngx_str_t *host, ngx_pool_t *pool,
ngx_str_t *content_type) size_t content_length,
ngx_str_t *content_type)
{ {
ngx_chain_t *cl; ngx_chain_t *cl;
ngx_buf_t *b; ngx_buf_t *b;
const char *method_s;
static char rq_tmpl[] = static char rq_tmpl[] =
"POST %V HTTP/1.0\r\n" "%s %V HTTP/1.0\r\n"
"Host: %V\r\n" "Host: %V\r\n"
"Content-Type: %V\r\n" "Content-Type: %V\r\n"
"Connection: Close\r\n" "Connection: Close\r\n"
@ -513,6 +515,7 @@ ngx_rtmp_netcall_http_format_header(ngx_str_t *uri, ngx_str_t *host,
} }
b = ngx_create_temp_buf(pool, sizeof(rq_tmpl) b = ngx_create_temp_buf(pool, sizeof(rq_tmpl)
+ sizeof("POST") - 1 /* longest method */
+ uri->len + uri->len
+ host->len + host->len
+ content_type->len + content_type->len
@ -523,9 +526,21 @@ ngx_rtmp_netcall_http_format_header(ngx_str_t *uri, ngx_str_t *host,
} }
cl->buf = b; cl->buf = b;
cl->next = NULL;
switch (method) {
case NGX_RTMP_NETCALL_HTTP_GET:
method_s = "GET";
break;
case NGX_RTMP_NETCALL_HTTP_POST:
method_s = "POST";
break;
default:
return NULL;
}
b->last = ngx_snprintf(b->last, b->end - b->last, rq_tmpl, b->last = ngx_snprintf(b->last, b->end - b->last, rq_tmpl,
uri, host, content_type, content_length); method_s, uri, host, content_type, content_length);
return cl; return cl;
} }

View File

@ -20,6 +20,9 @@ typedef ngx_int_t (*ngx_rtmp_netcall_sink_pt)(ngx_rtmp_session_t *s,
typedef ngx_int_t (*ngx_rtmp_netcall_handle_pt)(ngx_rtmp_session_t *s, typedef ngx_int_t (*ngx_rtmp_netcall_handle_pt)(ngx_rtmp_session_t *s,
void *arg, ngx_chain_t *in); void *arg, ngx_chain_t *in);
#define NGX_RTMP_NETCALL_HTTP_GET 1
#define NGX_RTMP_NETCALL_HTTP_POST 2
/* If handle is NULL then netcall is created detached /* If handle is NULL then netcall is created detached
* which means it's completely independent of RTMP * which means it's completely independent of RTMP
@ -48,9 +51,9 @@ ngx_int_t ngx_rtmp_netcall_create(ngx_rtmp_session_t *s,
/* HTTP handling */ /* HTTP handling */
ngx_chain_t * ngx_rtmp_netcall_http_format_session(ngx_rtmp_session_t *s, ngx_chain_t * ngx_rtmp_netcall_http_format_session(ngx_rtmp_session_t *s,
ngx_pool_t *pool); ngx_pool_t *pool);
ngx_chain_t * ngx_rtmp_netcall_http_format_header(ngx_str_t *uri, ngx_chain_t * ngx_rtmp_netcall_http_format_header(ngx_int_t method,
ngx_str_t *host, ngx_pool_t *pool, size_t content_length, ngx_str_t *uri, ngx_str_t *host, ngx_pool_t *pool,
ngx_str_t *content_type); size_t content_length, ngx_str_t *content_type);
ngx_chain_t * ngx_rtmp_netcall_http_skip_header(ngx_chain_t *in); ngx_chain_t * ngx_rtmp_netcall_http_skip_header(ngx_chain_t *in);

View File

@ -220,7 +220,7 @@ ngx_rtmp_notify_publish_create(ngx_rtmp_session_t *s, void *arg,
b = ngx_create_temp_buf(pool, b = ngx_create_temp_buf(pool,
sizeof("&call=publish") + sizeof("&call=publish") +
sizeof("&addr=") + addr_text->len + sizeof("&addr=") + addr_text->len *3 +
sizeof("&name=") + name_len * 3 + sizeof("&name=") + name_len * 3 +
sizeof("&type=") + type_len * 3 + sizeof("&type=") + type_len * 3 +
1 + args_len); 1 + args_len);
@ -250,7 +250,8 @@ ngx_rtmp_notify_publish_create(ngx_rtmp_session_t *s, void *arg,
/* HTTP header */ /* HTTP header */
url = nacf->url[NGX_RTMP_NOTIFY_PUBLISH]; url = nacf->url[NGX_RTMP_NOTIFY_PUBLISH];
hl = ngx_rtmp_netcall_http_format_header(&url->uri, &url->host, hl = ngx_rtmp_netcall_http_format_header(NGX_RTMP_NETCALL_HTTP_POST,
&url->uri, &url->host,
pool, cl->buf->last - cl->buf->pos + (pl->buf->last - pl->buf->pos), pool, cl->buf->last - cl->buf->pos + (pl->buf->last - pl->buf->pos),
&ngx_rtmp_notify_urlencoded); &ngx_rtmp_notify_urlencoded);
@ -301,7 +302,7 @@ ngx_rtmp_notify_play_create(ngx_rtmp_session_t *s, void *arg,
b = ngx_create_temp_buf(pool, b = ngx_create_temp_buf(pool,
sizeof("&call=play") + sizeof("&call=play") +
sizeof("&addr=") + addr_text->len + sizeof("&addr=") + addr_text->len * 3 +
sizeof("&name=") + name_len * 3 + sizeof("&name=") + name_len * 3 +
sizeof("&start=&duration=&reset=") + NGX_OFF_T_LEN * 3 sizeof("&start=&duration=&reset=") + NGX_OFF_T_LEN * 3
+ 1 + args_len); + 1 + args_len);
@ -332,7 +333,8 @@ ngx_rtmp_notify_play_create(ngx_rtmp_session_t *s, void *arg,
/* HTTP header */ /* HTTP header */
url = nacf->url[NGX_RTMP_NOTIFY_PLAY]; url = nacf->url[NGX_RTMP_NOTIFY_PLAY];
hl = ngx_rtmp_netcall_http_format_header(&url->uri, &url->host, hl = ngx_rtmp_netcall_http_format_header(NGX_RTMP_NETCALL_HTTP_POST,
&url->uri, &url->host,
pool, cl->buf->last - cl->buf->pos + (pl->buf->last - pl->buf->pos), pool, cl->buf->last - cl->buf->pos + (pl->buf->last - pl->buf->pos),
&ngx_rtmp_notify_urlencoded); &ngx_rtmp_notify_urlencoded);
@ -382,7 +384,7 @@ ngx_rtmp_notify_done_create(ngx_rtmp_session_t *s, void *arg,
b = ngx_create_temp_buf(pool, b = ngx_create_temp_buf(pool,
sizeof("&call=") + cbname_len + sizeof("&call=") + cbname_len +
sizeof("&addr=") + addr_text->len + sizeof("&addr=") + addr_text->len * 3 +
sizeof("&name=") + name_len * 3 sizeof("&name=") + name_len * 3
+ 1 + args_len); + 1 + args_len);
if (b == NULL) { if (b == NULL) {
@ -409,7 +411,8 @@ ngx_rtmp_notify_done_create(ngx_rtmp_session_t *s, void *arg,
} }
/* HTTP header */ /* HTTP header */
hl = ngx_rtmp_netcall_http_format_header(&ds->url->uri, &ds->url->host, hl = ngx_rtmp_netcall_http_format_header(NGX_RTMP_NETCALL_HTTP_POST,
&ds->url->uri, &ds->url->host,
pool, cl->buf->last - cl->buf->pos + (pl->buf->last - pl->buf->pos), pool, cl->buf->last - cl->buf->pos + (pl->buf->last - pl->buf->pos),
&ngx_rtmp_notify_urlencoded); &ngx_rtmp_notify_urlencoded);
@ -464,7 +467,7 @@ ngx_rtmp_notify_record_done_create(ngx_rtmp_session_t *s, void *arg,
b = ngx_create_temp_buf(pool, b = ngx_create_temp_buf(pool,
sizeof("&call=record_done") + sizeof("&call=record_done") +
sizeof("&recorder=") + v->recorder.len + sizeof("&recorder=") + v->recorder.len +
sizeof("&addr=") + addr_text->len + sizeof("&addr=") + addr_text->len *3 +
sizeof("&name=") + name_len * 3 + sizeof("&name=") + name_len * 3 +
sizeof("&path=") + v->path.len * 3 + sizeof("&path=") + v->path.len * 3 +
+ 1 + args_len); + 1 + args_len);
@ -499,7 +502,8 @@ ngx_rtmp_notify_record_done_create(ngx_rtmp_session_t *s, void *arg,
/* HTTP header */ /* HTTP header */
url = nacf->url[NGX_RTMP_NOTIFY_RECORD_DONE]; url = nacf->url[NGX_RTMP_NOTIFY_RECORD_DONE];
hl = ngx_rtmp_netcall_http_format_header(&url->uri, &url->host, hl = ngx_rtmp_netcall_http_format_header(NGX_RTMP_NETCALL_HTTP_POST,
&url->uri, &url->host,
pool, cl->buf->last - cl->buf->pos + (pl->buf->last - pl->buf->pos), pool, cl->buf->last - cl->buf->pos + (pl->buf->last - pl->buf->pos),
&ngx_rtmp_notify_urlencoded); &ngx_rtmp_notify_urlencoded);

View File

@ -381,7 +381,7 @@ ngx_rtmp_play_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
pacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_play_module); pacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_play_module);
if (pacf == NULL || pacf->root.len == 0) { if (pacf == NULL || (pacf->root.len == 0 && pacf->url == NULL)) {
goto next; goto next;
} }
@ -458,8 +458,11 @@ ngx_rtmp_play_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
goto next; goto next;
} }
ctx->file.fd = NGX_INVALID_FILE;
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0, ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
"play: %V", &ctx->fmt->name); "play %s: %V", pacf->url ? "remote" : "local",
&ctx->fmt->name);
sfx = &ctx->fmt->sfx; sfx = &ctx->fmt->sfx;
@ -471,17 +474,26 @@ ngx_rtmp_play_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
sfx = &nosfx; sfx = &nosfx;
} }
p = ngx_snprintf(path, sizeof(path), "%V/%V%V", &pacf->root, &name, sfx); /* remote? */
*p = 0;
ctx->file.fd = NGX_INVALID_FILE;
if (pacf->url) { if (pacf->url) {
ctx->name.data = ngx_palloc(s->connection->pool, name.len + sfx->len);
if (ctx->name.data == NULL) {
return NGX_ERROR;
}
p = ngx_sprintf(ctx->name.data, "%V%V", &name, sfx);
*p = 0;
ctx->name.len = p - ctx->name.data;
return ngx_rtmp_play_open_remote(s, v); return ngx_rtmp_play_open_remote(s, v);
} }
/* open local */ /* open local */
p = ngx_snprintf(path, sizeof(path), "%V/%V%V", &pacf->root, &name, sfx);
*p = 0;
ctx->file.fd = ngx_open_file(path, NGX_FILE_RDONLY, NGX_FILE_OPEN, ctx->file.fd = ngx_open_file(path, NGX_FILE_RDONLY, NGX_FILE_OPEN,
NGX_FILE_DEFAULT_ACCESS); NGX_FILE_DEFAULT_ACCESS);
@ -540,71 +552,50 @@ ngx_rtmp_play_remote_create(ngx_rtmp_session_t *s, void *arg, ngx_pool_t *pool)
ngx_rtmp_play_t *v = arg; ngx_rtmp_play_t *v = arg;
ngx_rtmp_play_app_conf_t *pacf; ngx_rtmp_play_app_conf_t *pacf;
ngx_chain_t *hl, *cl, *pl; ngx_rtmp_play_ctx_t *ctx;
ngx_buf_t *b; ngx_chain_t *hl;
ngx_str_t *addr_text, uri; ngx_str_t *addr_text, uri;
u_char *p; u_char *p;
size_t name_len, args_len; size_t args_len, len;
static ngx_str_t text_plain = ngx_string("text/plain"); static ngx_str_t text_plain = ngx_string("text/plain");
pacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_play_module); pacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_play_module);
/* common variables */ ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
cl = ngx_rtmp_netcall_http_format_session(s, pool);
if (cl == NULL) {
return NULL;
}
/* publish variables */
pl = ngx_alloc_chain_link(pool);
if (pl == NULL) {
return NULL;
}
name_len = ngx_strlen(v->name);
args_len = ngx_strlen(v->args); args_len = ngx_strlen(v->args);
addr_text = &s->connection->addr_text; addr_text = &s->connection->addr_text;
b = ngx_create_temp_buf(pool, len = pacf->url->uri.len + ctx->name.len +
sizeof("&addr=") + addr_text->len + sizeof("?addr=") + addr_text->len * 3 +
1 + args_len); 1 + args_len;
if (b == NULL) {
uri.data = ngx_palloc(pool, len);
if (uri.data == NULL) {
return NULL; return NULL;
} }
pl->buf = b; p= uri.data;
b->last = ngx_cpymem(b->last, (u_char*)"&addr=", sizeof("&addr=") -1);
b->last = (u_char*)ngx_escape_uri(b->last, addr_text->data,
addr_text->len, 0);
p = ngx_cpymem(p, pacf->url->uri.data, pacf->url->uri.len);
p = ngx_cpymem(p, ctx->name.data, ctx->name.len);
p = ngx_cpymem(p, (u_char*)"?addr=", sizeof("&addr=") -1);
p = (u_char*)ngx_escape_uri(p, addr_text->data, addr_text->len, 0);
if (args_len) { if (args_len) {
*b->last++ = '&'; *p++ = '&';
b->last = (u_char *)ngx_cpymem(b->last, v->args, args_len); p = (u_char *) ngx_cpymem(p, v->args, args_len);
} }
/* create uri */ uri.len = p - uri.data;
uri.len = pacf->url->uri.len + name_len;
uri.data = ngx_palloc(pool, uri.len);
p = ngx_cpymem(uri.data, pacf->url->uri.data, pacf->url->uri.len);
ngx_memcpy(p, v->name, name_len);
/* HTTP header */ /* HTTP header */
hl = ngx_rtmp_netcall_http_format_header(&uri, &pacf->url->host, hl = ngx_rtmp_netcall_http_format_header(NGX_RTMP_NETCALL_HTTP_GET,
pool, cl->buf->last - cl->buf->pos + (pl->buf->last - pl->buf->pos), &uri, &pacf->url->host, pool, 0, &text_plain);
&text_plain);
if (hl == NULL) { if (hl == NULL) {
return NULL; return NULL;
} }
hl->next = cl;
cl->next = pl;
pl->next = NULL;
return hl; return hl;
} }
@ -632,20 +623,23 @@ ngx_rtmp_play_remote_sink(ngx_rtmp_session_t *s, ngx_chain_t *in)
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module); ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
/* skip HTTP header */ /* skip HTTP header */
for (; in && ctx->ncrs != 2; in = in->next) { while (in && ctx->ncrs != 2) {
b = in->buf; b = in->buf;
for (; b->pos != b->last && ctx->ncrs != 2; ++b->pos) { for (; b->pos != b->last && ctx->ncrs != 2; ++b->pos) {
switch (*b->pos) { switch (*b->pos) {
case '\n': case '\n':
++ctx->ncrs; ++ctx->ncrs;
break;
case '\r': case '\r':
break; break;
default: default:
ctx->ncrs = 0; ctx->ncrs = 0;
} }
} }
if (b->pos == b->last) {
in = in->next;
}
} }
/* write to temp file */ /* write to temp file */
@ -721,7 +715,8 @@ ngx_rtmp_play_open_remote(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
static char * static char *
ngx_rtmp_play_url(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) ngx_rtmp_play_url(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{ {
ngx_rtmp_play_app_conf_t *pacf; ngx_rtmp_play_app_conf_t *pacf = conf;
ngx_str_t url; ngx_str_t url;
ngx_url_t *u; ngx_url_t *u;
size_t add; size_t add;

View File

@ -40,6 +40,7 @@ typedef struct {
ngx_rtmp_play_fmt_t *fmt; ngx_rtmp_play_fmt_t *fmt;
ngx_event_t send_evt; ngx_event_t send_evt;
ngx_uint_t ncrs; ngx_uint_t ncrs;
ngx_str_t name;
} ngx_rtmp_play_ctx_t; } ngx_rtmp_play_ctx_t;