1 Star 0 Fork 0

hankersyan/nginx-rtmp-module

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ngx_rtmp_play_module.c 32.13 KB
一键复制 编辑 原始数据 按行查看 历史
Roman Arutyunyan 提交于 2014-09-17 07:10 . fixed compilation
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
/*
* Copyright (C) Roman Arutyunyan
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <nginx.h>
#include "ngx_rtmp_play_module.h"
#include "ngx_rtmp_cmd_module.h"
#include "ngx_rtmp_netcall_module.h"
#include "ngx_rtmp_streams.h"
static ngx_rtmp_play_pt next_play;
static ngx_rtmp_close_stream_pt next_close_stream;
static ngx_rtmp_seek_pt next_seek;
static ngx_rtmp_pause_pt next_pause;
static char *ngx_rtmp_play_url(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static void *ngx_rtmp_play_create_main_conf(ngx_conf_t *cf);
static ngx_int_t ngx_rtmp_play_postconfiguration(ngx_conf_t *cf);
static void * ngx_rtmp_play_create_app_conf(ngx_conf_t *cf);
static char * ngx_rtmp_play_merge_app_conf(ngx_conf_t *cf,
void *parent, void *child);
static ngx_int_t ngx_rtmp_play_do_init(ngx_rtmp_session_t *s);
static ngx_int_t ngx_rtmp_play_do_done(ngx_rtmp_session_t *s);
static ngx_int_t ngx_rtmp_play_do_start(ngx_rtmp_session_t *s);
static ngx_int_t ngx_rtmp_play_do_stop(ngx_rtmp_session_t *s);
static ngx_int_t ngx_rtmp_play_do_seek(ngx_rtmp_session_t *s,
ngx_uint_t timestamp);
static ngx_int_t ngx_rtmp_play_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v);
static ngx_int_t ngx_rtmp_play_seek(ngx_rtmp_session_t *s, ngx_rtmp_seek_t *v);
static ngx_int_t ngx_rtmp_play_pause(ngx_rtmp_session_t *s,
ngx_rtmp_pause_t *v);
static void ngx_rtmp_play_send(ngx_event_t *e);
static ngx_int_t ngx_rtmp_play_open(ngx_rtmp_session_t *s, double start);
static ngx_int_t ngx_rtmp_play_remote_handle(ngx_rtmp_session_t *s,
void *arg, ngx_chain_t *in);
static ngx_chain_t * ngx_rtmp_play_remote_create(ngx_rtmp_session_t *s,
void *arg, ngx_pool_t *pool);
static ngx_int_t ngx_rtmp_play_open_remote(ngx_rtmp_session_t *s,
ngx_rtmp_play_t *v);
static ngx_int_t ngx_rtmp_play_next_entry(ngx_rtmp_session_t *s,
ngx_rtmp_play_t *v);
static ngx_rtmp_play_entry_t * ngx_rtmp_play_get_current_entry(
ngx_rtmp_session_t *s);
static void ngx_rtmp_play_cleanup_local_file(ngx_rtmp_session_t *s);
static void ngx_rtmp_play_copy_local_file(ngx_rtmp_session_t *s, u_char *name);
static u_char * ngx_rtmp_play_get_local_file_path(ngx_rtmp_session_t *s);
static ngx_command_t ngx_rtmp_play_commands[] = {
{ ngx_string("play"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_1MORE,
ngx_rtmp_play_url,
NGX_RTMP_APP_CONF_OFFSET,
0,
NULL },
{ ngx_string("play_temp_path"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_RTMP_APP_CONF_OFFSET,
offsetof(ngx_rtmp_play_app_conf_t, temp_path),
NULL },
{ ngx_string("play_local_path"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_RTMP_APP_CONF_OFFSET,
offsetof(ngx_rtmp_play_app_conf_t, local_path),
NULL },
ngx_null_command
};
static ngx_rtmp_module_t ngx_rtmp_play_module_ctx = {
NULL, /* preconfiguration */
ngx_rtmp_play_postconfiguration, /* postconfiguration */
ngx_rtmp_play_create_main_conf, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_rtmp_play_create_app_conf, /* create app configuration */
ngx_rtmp_play_merge_app_conf /* merge app configuration */
};
ngx_module_t ngx_rtmp_play_module = {
NGX_MODULE_V1,
&ngx_rtmp_play_module_ctx, /* module context */
ngx_rtmp_play_commands, /* 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_PLAY_TMP_FILE "nginx-rtmp-vod."
static void *
ngx_rtmp_play_create_main_conf(ngx_conf_t *cf)
{
ngx_rtmp_play_main_conf_t *pmcf;
pmcf = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_play_main_conf_t));
if (pmcf == NULL) {
return NULL;
}
if (ngx_array_init(&pmcf->fmts, cf->pool, 1,
sizeof(ngx_rtmp_play_fmt_t *))
!= NGX_OK)
{
return NULL;
}
return pmcf;
}
static void *
ngx_rtmp_play_create_app_conf(ngx_conf_t *cf)
{
ngx_rtmp_play_app_conf_t *pacf;
pacf = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_play_app_conf_t));
if (pacf == NULL) {
return NULL;
}
pacf->nbuckets = 1024;
return pacf;
}
static char *
ngx_rtmp_play_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_rtmp_play_app_conf_t *prev = parent;
ngx_rtmp_play_app_conf_t *conf = child;
ngx_rtmp_play_entry_t **ppe;
ngx_conf_merge_str_value(conf->temp_path, prev->temp_path, "/tmp");
ngx_conf_merge_str_value(conf->local_path, prev->local_path, "");
if (prev->entries.nelts == 0) {
goto done;
}
if (conf->entries.nelts == 0) {
conf->entries = prev->entries;
goto done;
}
ppe = ngx_array_push_n(&conf->entries, prev->entries.nelts);
if (ppe == NULL) {
return NGX_CONF_ERROR;
}
ngx_memcpy(ppe, prev->entries.elts, prev->entries.nelts * sizeof(void *));
done:
if (conf->entries.nelts == 0) {
return NGX_CONF_OK;
}
conf->ctx = ngx_pcalloc(cf->pool, sizeof(void *) * conf->nbuckets);
if (conf->ctx == NULL) {
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
static ngx_int_t
ngx_rtmp_play_join(ngx_rtmp_session_t *s)
{
ngx_rtmp_play_ctx_t *ctx, **pctx;
ngx_rtmp_play_app_conf_t *pacf;
ngx_uint_t h;
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: join");
pacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_play_module);
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
if (ctx == NULL || ctx->joined) {
return NGX_ERROR;
}
h = ngx_hash_key(ctx->name, ngx_strlen(ctx->name));
pctx = &pacf->ctx[h % pacf->nbuckets];
while (*pctx) {
if (!ngx_strncmp((*pctx)->name, ctx->name, NGX_RTMP_MAX_NAME)) {
break;
}
pctx = &(*pctx)->next;
}
ctx->next = *pctx;
*pctx = ctx;
ctx->joined = 1;
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_play_leave(ngx_rtmp_session_t *s)
{
ngx_rtmp_play_ctx_t *ctx, **pctx;
ngx_rtmp_play_app_conf_t *pacf;
ngx_uint_t h;
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: leave");
pacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_play_module);
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
if (ctx == NULL || !ctx->joined) {
return NGX_ERROR;
}
h = ngx_hash_key(ctx->name, ngx_strlen(ctx->name));
pctx = &pacf->ctx[h % pacf->nbuckets];
while (*pctx && *pctx != ctx) {
pctx = &(*pctx)->next;
}
if (*pctx == NULL) {
return NGX_ERROR;
}
*pctx = (*pctx)->next;
ctx->joined = 0;
return NGX_OK;
}
static void
ngx_rtmp_play_send(ngx_event_t *e)
{
ngx_rtmp_session_t *s = e->data;
ngx_rtmp_play_ctx_t *ctx;
ngx_int_t rc;
ngx_uint_t ts;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
if (ctx == NULL || ctx->fmt == NULL || ctx->fmt->send == NULL) {
return;
}
ts = 0;
rc = ctx->fmt->send(s, &ctx->file, &ts);
if (rc > 0) {
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: send schedule %i", rc);
ngx_add_timer(e, rc);
return;
}
if (rc == NGX_AGAIN) {
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: send buffer full");
ngx_post_event(e, &s->posted_dry_events);
return;
}
if (rc == NGX_OK) {
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: send restart");
ngx_post_event(e, &ngx_posted_events);
return;
}
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: send done");
ngx_rtmp_send_stream_eof(s, NGX_RTMP_MSID);
ngx_rtmp_send_play_status(s, "NetStream.Play.Complete", "status", ts, 0);
ngx_rtmp_send_status(s, "NetStream.Play.Stop", "status", "Stopped");
}
static ngx_int_t
ngx_rtmp_play_do_init(ngx_rtmp_session_t *s)
{
ngx_rtmp_play_ctx_t *ctx;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
if (ctx == NULL) {
return NGX_ERROR;
}
if (ctx->fmt && ctx->fmt->init &&
ctx->fmt->init(s, &ctx->file, ctx->aindex, ctx->vindex) != NGX_OK)
{
return NGX_ERROR;
}
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_play_do_done(ngx_rtmp_session_t *s)
{
ngx_rtmp_play_ctx_t *ctx;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
if (ctx == NULL) {
return NGX_ERROR;
}
if (ctx->fmt && ctx->fmt->done &&
ctx->fmt->done(s, &ctx->file) != NGX_OK)
{
return NGX_ERROR;
}
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_play_do_start(ngx_rtmp_session_t *s)
{
ngx_rtmp_play_ctx_t *ctx;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
if (ctx == NULL) {
return NGX_ERROR;
}
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: start");
if (ctx->fmt && ctx->fmt->start &&
ctx->fmt->start(s, &ctx->file) != NGX_OK)
{
return NGX_ERROR;
}
ngx_post_event((&ctx->send_evt), &ngx_posted_events);
ctx->playing = 1;
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_play_do_seek(ngx_rtmp_session_t *s, ngx_uint_t timestamp)
{
ngx_rtmp_play_ctx_t *ctx;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
if (ctx == NULL) {
return NGX_ERROR;
}
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: seek timestamp=%ui", timestamp);
if (ctx->fmt && ctx->fmt->seek &&
ctx->fmt->seek(s, &ctx->file, timestamp) != NGX_OK)
{
return NGX_ERROR;
}
if (ctx->playing) {
ngx_post_event((&ctx->send_evt), &ngx_posted_events);
}
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_play_do_stop(ngx_rtmp_session_t *s)
{
ngx_rtmp_play_ctx_t *ctx;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
if (ctx == NULL) {
return NGX_ERROR;
}
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: stop");
if (ctx->send_evt.timer_set) {
ngx_del_timer(&ctx->send_evt);
}
#if (nginx_version >= 1007005)
if (ctx->send_evt.posted)
#else
if (ctx->send_evt.prev)
#endif
{
ngx_delete_posted_event((&ctx->send_evt));
}
if (ctx->fmt && ctx->fmt->stop &&
ctx->fmt->stop(s, &ctx->file) != NGX_OK)
{
return NGX_ERROR;
}
ctx->playing = 0;
return NGX_OK;
}
/* This function returns pointer to a static buffer */
static u_char *
ngx_rtmp_play_get_local_file_path(ngx_rtmp_session_t *s)
{
ngx_rtmp_play_app_conf_t *pacf;
ngx_rtmp_play_ctx_t *ctx;
u_char *p;
static u_char path[NGX_MAX_PATH + 1];
pacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_play_module);
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
p = ngx_snprintf(path, NGX_MAX_PATH, "%V/" NGX_RTMP_PLAY_TMP_FILE "%ui",
&pacf->temp_path, ctx->file_id);
*p = 0;
return path;
}
static void
ngx_rtmp_play_copy_local_file(ngx_rtmp_session_t *s, u_char *name)
{
ngx_rtmp_play_app_conf_t *pacf;
ngx_rtmp_play_ctx_t *ctx;
u_char *path, *p;
static u_char dpath[NGX_MAX_PATH + 1];
pacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_play_module);
if (pacf == NULL) {
return;
}
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
if (ctx == NULL || ctx->file_id == 0) {
return;
}
path = ngx_rtmp_play_get_local_file_path(s);
p = ngx_snprintf(dpath, NGX_MAX_PATH, "%V/%s%V", &pacf->local_path,
name + ctx->pfx_size, &ctx->sfx);
*p = 0;
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: copy local file '%s' to '%s'", path, dpath);
if (ngx_rename_file(path, dpath) == 0) {
ctx->file_id = 0;
return;
}
ngx_log_error(NGX_LOG_ERR, s->connection->log, ngx_errno,
"play: error copying local file '%s' to '%s'",
path, dpath);
ngx_rtmp_play_cleanup_local_file(s);
}
static void
ngx_rtmp_play_cleanup_local_file(ngx_rtmp_session_t *s)
{
ngx_rtmp_play_ctx_t *ctx;
u_char *path;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
if (ctx == NULL || ctx->file_id == 0) {
return;
}
path = ngx_rtmp_play_get_local_file_path(s);
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: deleting local file '%s'", path);
ctx->file_id = 0;
ngx_delete_file(path);
}
static ngx_int_t
ngx_rtmp_play_close_stream(ngx_rtmp_session_t *s, ngx_rtmp_close_stream_t *v)
{
ngx_rtmp_play_ctx_t *ctx;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
if (ctx == NULL) {
goto next;
}
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: close_stream");
ngx_rtmp_play_do_stop(s);
ngx_rtmp_play_do_done(s);
if (ctx->file.fd != NGX_INVALID_FILE) {
ngx_close_file(ctx->file.fd);
ctx->file.fd = NGX_INVALID_FILE;
ngx_rtmp_send_stream_eof(s, NGX_RTMP_MSID);
ngx_rtmp_send_status(s, "NetStream.Play.Stop", "status",
"Stop video on demand");
}
if (ctx->file_id) {
ngx_rtmp_play_cleanup_local_file(s);
}
ngx_rtmp_play_leave(s);
next:
return next_close_stream(s, v);
}
static ngx_int_t
ngx_rtmp_play_seek(ngx_rtmp_session_t *s, ngx_rtmp_seek_t *v)
{
ngx_rtmp_play_ctx_t *ctx;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
if (ctx == NULL || ctx->file.fd == NGX_INVALID_FILE) {
goto next;
}
if (!ctx->opened) {
ctx->post_seek = (ngx_uint_t) v->offset;
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: post seek=%ui", ctx->post_seek);
goto next;
}
if (ngx_rtmp_send_stream_eof(s, NGX_RTMP_MSID) != NGX_OK) {
return NGX_ERROR;
}
ngx_rtmp_play_do_seek(s, (ngx_uint_t) v->offset);
if (ngx_rtmp_send_status(s, "NetStream.Seek.Notify", "status", "Seeking")
!= NGX_OK)
{
return NGX_ERROR;
}
if (ngx_rtmp_send_stream_begin(s, NGX_RTMP_MSID) != NGX_OK) {
return NGX_ERROR;
}
next:
return next_seek(s, v);
}
static ngx_int_t
ngx_rtmp_play_pause(ngx_rtmp_session_t *s, ngx_rtmp_pause_t *v)
{
ngx_rtmp_play_ctx_t *ctx;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
if (ctx == NULL || ctx->file.fd == NGX_INVALID_FILE) {
goto next;
}
if (!ctx->opened) {
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: pause ignored");
goto next;
}
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: pause=%i timestamp=%f",
(ngx_int_t) v->pause, v->position);
if (v->pause) {
if (ngx_rtmp_send_status(s, "NetStream.Pause.Notify", "status",
"Paused video on demand")
!= NGX_OK)
{
return NGX_ERROR;
}
ngx_rtmp_play_do_stop(s);
} else {
if (ngx_rtmp_send_status(s, "NetStream.Unpause.Notify", "status",
"Unpaused video on demand")
!= NGX_OK)
{
return NGX_ERROR;
}
ngx_rtmp_play_do_start(s); /*TODO: v->position? */
}
next:
return next_pause(s, v);
}
static ngx_int_t
ngx_rtmp_play_parse_index(char type, u_char *args)
{
u_char *p, c;
static u_char name[] = "xindex=";
name[0] = (u_char) type;
for ( ;; ) {
p = (u_char *) ngx_strstr(args, name);
if (p == NULL) {
return 0;
}
if (p != args) {
c = *(p - 1);
if (c != '?' && c != '&') {
args = p + 1;
continue;
}
}
return atoi((char *) p + (sizeof(name) - 1));
}
}
static ngx_int_t
ngx_rtmp_play_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
{
ngx_rtmp_play_main_conf_t *pmcf;
ngx_rtmp_play_app_conf_t *pacf;
ngx_rtmp_play_ctx_t *ctx;
u_char *p;
ngx_rtmp_play_fmt_t *fmt, **pfmt;
ngx_str_t *pfx, *sfx;
ngx_str_t name;
ngx_uint_t n;
pmcf = ngx_rtmp_get_module_main_conf(s, ngx_rtmp_play_module);
pacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_play_module);
if (pacf == NULL || pacf->entries.nelts == 0) {
goto next;
}
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
"play: play name='%s' timestamp=%i",
v->name, (ngx_int_t) v->start);
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
if (ctx && ctx->file.fd != NGX_INVALID_FILE) {
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
"play: already playing");
goto next;
}
/* check for double-dot in v->name;
* we should not move out of play directory */
for (p = v->name; *p; ++p) {
if (ngx_path_separator(p[0]) &&
p[1] == '.' && p[2] == '.' &&
ngx_path_separator(p[3]))
{
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
"play: bad name '%s'", v->name);
return NGX_ERROR;
}
}
if (ctx == NULL) {
ctx = ngx_palloc(s->connection->pool, sizeof(ngx_rtmp_play_ctx_t));
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_play_module);
}
ngx_memzero(ctx, sizeof(*ctx));
ctx->session = s;
ctx->aindex = ngx_rtmp_play_parse_index('a', v->args);
ctx->vindex = ngx_rtmp_play_parse_index('v', v->args);
ctx->file.log = s->connection->log;
ngx_memcpy(ctx->name, v->name, NGX_RTMP_MAX_NAME);
name.len = ngx_strlen(v->name);
name.data = v->name;
pfmt = pmcf->fmts.elts;
for (n = 0; n < pmcf->fmts.nelts; ++n, ++pfmt) {
fmt = *pfmt;
pfx = &fmt->pfx;
sfx = &fmt->sfx;
if (pfx->len == 0 && ctx->fmt == NULL) {
ctx->fmt = fmt;
}
if (pfx->len && name.len >= pfx->len &&
ngx_strncasecmp(pfx->data, name.data, pfx->len) == 0)
{
ctx->pfx_size = pfx->len;
ctx->fmt = fmt;
break;
}
if (name.len >= sfx->len &&
ngx_strncasecmp(sfx->data, name.data + name.len - sfx->len,
sfx->len) == 0)
{
ctx->fmt = fmt;
}
}
if (ctx->fmt == NULL) {
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
"play: fmt not found");
goto next;
}
ctx->file.fd = NGX_INVALID_FILE;
ctx->nentry = NGX_CONF_UNSET_UINT;
ctx->post_seek = NGX_CONF_UNSET_UINT;
sfx = &ctx->fmt->sfx;
if (name.len < sfx->len ||
ngx_strncasecmp(sfx->data, name.data + name.len - sfx->len,
sfx->len))
{
ctx->sfx = *sfx;
}
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: fmt=%V", &ctx->fmt->name);
return ngx_rtmp_play_next_entry(s, v);
next:
return next_play(s, v);
}
static ngx_int_t
ngx_rtmp_play_next_entry(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
{
ngx_rtmp_play_app_conf_t *pacf;
ngx_rtmp_play_ctx_t *ctx;
ngx_rtmp_play_entry_t *pe;
u_char *p;
static u_char path[NGX_MAX_PATH + 1];
pacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_play_module);
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
for ( ;; ) {
if (ctx->file.fd != NGX_INVALID_FILE) {
ngx_close_file(ctx->file.fd);
ctx->file.fd = NGX_INVALID_FILE;
}
if (ctx->file_id) {
ngx_rtmp_play_cleanup_local_file(s);
}
ctx->nentry = (ctx->nentry == NGX_CONF_UNSET_UINT ?
0 : ctx->nentry + 1);
if (ctx->nentry >= pacf->entries.nelts) {
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: all entries failed");
ngx_rtmp_send_status(s, "NetStream.Play.StreamNotFound", "error",
"Video on demand stream not found");
break;
}
pe = ngx_rtmp_play_get_current_entry(s);
ngx_log_debug4(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: trying %s entry %ui/%uz '%V'",
pe->url ? "remote" : "local",
ctx->nentry + 1, pacf->entries.nelts,
pe->url ? &pe->url->url : pe->root);
/* open remote */
if (pe->url) {
return ngx_rtmp_play_open_remote(s, v);
}
/* open local */
p = ngx_snprintf(path, NGX_MAX_PATH, "%V/%s%V",
pe->root, v->name + ctx->pfx_size, &ctx->sfx);
*p = 0;
ctx->file.fd = ngx_open_file(path, NGX_FILE_RDONLY, NGX_FILE_OPEN,
NGX_FILE_DEFAULT_ACCESS);
if (ctx->file.fd == NGX_INVALID_FILE) {
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, ngx_errno,
"play: error opening file '%s'", path);
continue;
}
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: open local file '%s'", path);
if (ngx_rtmp_play_open(s, v->start) != NGX_OK) {
return NGX_ERROR;
}
break;
}
return next_play(s, v);
}
static ngx_int_t
ngx_rtmp_play_open(ngx_rtmp_session_t *s, double start)
{
ngx_rtmp_play_ctx_t *ctx;
ngx_event_t *e;
ngx_uint_t timestamp;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
if (ctx->file.fd == NGX_INVALID_FILE) {
return NGX_ERROR;
}
if (ngx_rtmp_send_stream_begin(s, NGX_RTMP_MSID) != NGX_OK) {
return NGX_ERROR;
}
if (ngx_rtmp_send_status(s, "NetStream.Play.Start", "status",
"Start video on demand")
!= NGX_OK)
{
return NGX_ERROR;
}
if (ngx_rtmp_play_join(s) != NGX_OK) {
return NGX_ERROR;
}
e = &ctx->send_evt;
e->data = s;
e->handler = ngx_rtmp_play_send;
e->log = s->connection->log;
ngx_rtmp_send_recorded(s, 1);
if (ngx_rtmp_send_sample_access(s) != NGX_OK) {
return NGX_ERROR;
}
if (ngx_rtmp_play_do_init(s) != NGX_OK) {
return NGX_ERROR;
}
timestamp = ctx->post_seek != NGX_CONF_UNSET_UINT ? ctx->post_seek :
(start < 0 ? 0 : (ngx_uint_t) start);
if (ngx_rtmp_play_do_seek(s, timestamp) != NGX_OK) {
return NGX_ERROR;
}
if (ngx_rtmp_play_do_start(s) != NGX_OK) {
return NGX_ERROR;
}
ctx->opened = 1;
return NGX_OK;
}
static ngx_chain_t *
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_ctx_t *ctx;
ngx_rtmp_play_entry_t *pe;
ngx_str_t *addr_text, uri;
u_char *p, *name;
size_t args_len, name_len, len;
static ngx_str_t text_plain = ngx_string("text/plain");
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
pe = ngx_rtmp_play_get_current_entry(s);
name = v->name + ctx->pfx_size;
name_len = ngx_strlen(name);
args_len = ngx_strlen(v->args);
addr_text = &s->connection->addr_text;
len = pe->url->uri.len + 1 +
name_len + ctx->sfx.len +
sizeof("?addr=") + addr_text->len * 3 +
1 + args_len;
uri.data = ngx_palloc(pool, len);
if (uri.data == NULL) {
return NULL;
}
p = uri.data;
p = ngx_cpymem(p, pe->url->uri.data, pe->url->uri.len);
if (p == uri.data || p[-1] != '/') {
*p++ = '/';
}
p = ngx_cpymem(p, name, name_len);
p = ngx_cpymem(p, ctx->sfx.data, ctx->sfx.len);
p = ngx_cpymem(p, (u_char*)"?addr=", sizeof("&addr=") -1);
p = (u_char*)ngx_escape_uri(p, addr_text->data, addr_text->len,
NGX_ESCAPE_ARGS);
if (args_len) {
*p++ = '&';
p = (u_char *) ngx_cpymem(p, v->args, args_len);
}
uri.len = p - uri.data;
return ngx_rtmp_netcall_http_format_request(NGX_RTMP_NETCALL_HTTP_GET,
&pe->url->host, &uri,
NULL, NULL, pool, &text_plain);
}
static ngx_int_t
ngx_rtmp_play_remote_handle(ngx_rtmp_session_t *s, void *arg, ngx_chain_t *in)
{
ngx_rtmp_play_t *v = arg;
ngx_rtmp_play_ctx_t *ctx;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
if (ctx->nbody == 0) {
return ngx_rtmp_play_next_entry(s, v);
}
if (ctx->file_id) {
ngx_rtmp_play_copy_local_file(s, v->name);
}
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: open remote file");
if (ngx_rtmp_play_open(s, v->start) != NGX_OK) {
return NGX_ERROR;
}
return next_play(s, (ngx_rtmp_play_t *)arg);
}
static ngx_int_t
ngx_rtmp_play_remote_sink(ngx_rtmp_session_t *s, ngx_chain_t *in)
{
ngx_rtmp_play_ctx_t *ctx;
ngx_buf_t *b;
ngx_int_t rc;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
/* skip HTTP header */
while (in && ctx->ncrs != 2) {
b = in->buf;
for (; b->pos != b->last && ctx->ncrs != 2; ++b->pos) {
switch (*b->pos) {
case '\n':
++ctx->ncrs;
case '\r':
break;
default:
ctx->ncrs = 0;
}
/* 10th header byte is HTTP response header */
if (++ctx->nheader == 10 && *b->pos != (u_char) '2') {
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
"play: remote HTTP response code: %cxx",
*b->pos);
return NGX_ERROR;
}
}
if (b->pos == b->last) {
in = in->next;
}
}
/* write to temp file */
for (; in; in = in->next) {
b = in->buf;
if (b->pos == b->last) {
continue;
}
rc = ngx_write_fd(ctx->file.fd, b->pos, b->last - b->pos);
if (rc == NGX_ERROR) {
ngx_log_error(NGX_LOG_INFO, s->connection->log, ngx_errno,
"play: error writing to temp file");
return NGX_ERROR;
}
ctx->nbody += rc;
}
return NGX_OK;
}
static ngx_rtmp_play_entry_t *
ngx_rtmp_play_get_current_entry(ngx_rtmp_session_t *s)
{
ngx_rtmp_play_app_conf_t *pacf;
ngx_rtmp_play_ctx_t *ctx;
ngx_rtmp_play_entry_t **ppe;
pacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_play_module);
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
ppe = pacf->entries.elts;
return ppe[ctx->nentry];
}
static ngx_int_t
ngx_rtmp_play_open_remote(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
{
ngx_rtmp_play_app_conf_t *pacf;
ngx_rtmp_play_ctx_t *ctx;
ngx_rtmp_play_entry_t *pe;
ngx_rtmp_netcall_init_t ci;
u_char *path;
ngx_err_t err;
static ngx_uint_t file_id;
pacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_play_module);
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_play_module);
ctx->ncrs = 0;
ctx->nheader = 0;
ctx->nbody = 0;
for ( ;; ) {
ctx->file_id = ++file_id;
/* no zero after overflow */
if (ctx->file_id == 0) {
continue;
}
path = ngx_rtmp_play_get_local_file_path(s);
ctx->file.fd = ngx_open_tempfile(path, pacf->local_path.len, 0);
if (pacf->local_path.len == 0) {
ctx->file_id = 0;
}
if (ctx->file.fd != NGX_INVALID_FILE) {
break;
}
err = ngx_errno;
if (err != NGX_EEXIST) {
ctx->file_id = 0;
ngx_log_error(NGX_LOG_INFO, s->connection->log, err,
"play: failed to create temp file");
return NGX_ERROR;
}
}
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"play: temp file '%s' file_id=%ui",
path, ctx->file_id);
pe = ngx_rtmp_play_get_current_entry(s);
ngx_memzero(&ci, sizeof(ci));
ci.url = pe->url;
ci.create = ngx_rtmp_play_remote_create;
ci.sink = ngx_rtmp_play_remote_sink;
ci.handle = ngx_rtmp_play_remote_handle;
ci.arg = v;
ci.argsize = sizeof(*v);
return ngx_rtmp_netcall_create(s, &ci);
}
static char *
ngx_rtmp_play_url(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_rtmp_play_app_conf_t *pacf = conf;
ngx_rtmp_play_entry_t *pe, **ppe;
ngx_str_t url;
ngx_url_t *u;
size_t add, n;
ngx_str_t *value;
if (pacf->entries.nalloc == 0 &&
ngx_array_init(&pacf->entries, cf->pool, 1, sizeof(void *)) != NGX_OK)
{
return NGX_CONF_ERROR;
}
value = cf->args->elts;
for (n = 1; n < cf->args->nelts; ++n) {
ppe = ngx_array_push(&pacf->entries);
if (ppe == NULL) {
return NGX_CONF_ERROR;
}
pe = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_play_entry_t));
if (pe == NULL) {
return NGX_CONF_ERROR;
}
*ppe = pe;
if (ngx_strncasecmp(value[n].data, (u_char *) "http://", 7)) {
/* local file */
pe->root = ngx_palloc(cf->pool, sizeof(ngx_str_t));
if (pe->root == NULL) {
return NGX_CONF_ERROR;
}
*pe->root = value[n];
continue;
}
/* http case */
url = value[n];
add = sizeof("http://") - 1;
url.data += add;
url.len -= add;
u = ngx_pcalloc(cf->pool, sizeof(ngx_url_t));
if (u == NULL) {
return NGX_CONF_ERROR;
}
u->url.len = url.len;
u->url.data = url.data;
u->default_port = 80;
u->uri_part = 1;
if (ngx_parse_url(cf->pool, u) != NGX_OK) {
if (u->err) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"%s in url \"%V\"", u->err, &u->url);
}
return NGX_CONF_ERROR;
}
pe->url = u;
}
return NGX_CONF_OK;
}
static ngx_int_t
ngx_rtmp_play_postconfiguration(ngx_conf_t *cf)
{
next_play = ngx_rtmp_play;
ngx_rtmp_play = ngx_rtmp_play_play;
next_close_stream = ngx_rtmp_close_stream;
ngx_rtmp_close_stream = ngx_rtmp_play_close_stream;
next_seek = ngx_rtmp_seek;
ngx_rtmp_seek = ngx_rtmp_play_seek;
next_pause = ngx_rtmp_pause;
ngx_rtmp_pause = ngx_rtmp_play_pause;
return NGX_OK;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hankersyan/nginx-rtmp-module.git
git@gitee.com:hankersyan/nginx-rtmp-module.git
hankersyan
nginx-rtmp-module
nginx-rtmp-module
master

搜索帮助