2012-03-23 15:57:25 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Roman Arutyunyan
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include "ngx_rtmp.h"
|
2012-03-23 18:29:22 +01:00
|
|
|
#include "ngx_rtmp_cmd_module.h"
|
2012-03-29 12:07:57 +02:00
|
|
|
#include "ngx_rtmp_netcall_module.h"
|
2012-06-09 17:22:18 +02:00
|
|
|
#include "ngx_rtmp_codec_module.h"
|
2012-08-20 18:44:14 +02:00
|
|
|
#include "ngx_rtmp_record_module.h"
|
2012-03-23 15:57:25 +01:00
|
|
|
|
|
|
|
|
2012-03-27 18:26:43 +02:00
|
|
|
static ngx_rtmp_publish_pt next_publish;
|
|
|
|
static ngx_rtmp_delete_stream_pt next_delete_stream;
|
|
|
|
|
|
|
|
|
2012-09-05 14:49:39 +02:00
|
|
|
static char *ngx_rtmp_record_recorder(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
|
|
void *conf);
|
2012-03-29 12:07:57 +02:00
|
|
|
static char * ngx_rtmp_notify_on_record_done(ngx_conf_t *cf,
|
2012-09-04 15:47:32 +02:00
|
|
|
ngx_command_t *cmd, void *conf);
|
2012-03-23 15:57:25 +01:00
|
|
|
static ngx_int_t ngx_rtmp_record_postconfiguration(ngx_conf_t *cf);
|
|
|
|
static void * ngx_rtmp_record_create_app_conf(ngx_conf_t *cf);
|
|
|
|
static char * ngx_rtmp_record_merge_app_conf(ngx_conf_t *cf,
|
2012-09-04 15:47:32 +02:00
|
|
|
void *parent, void *child);
|
2012-06-09 17:22:18 +02:00
|
|
|
static ngx_int_t ngx_rtmp_record_write_frame(ngx_rtmp_session_t *s,
|
2012-09-04 15:47:32 +02:00
|
|
|
ngx_rtmp_record_node_ctx_t *rctx,
|
|
|
|
ngx_rtmp_header_t *h, ngx_chain_t *in);
|
|
|
|
static ngx_int_t ngx_rtmp_record_av(ngx_rtmp_session_t *s,
|
|
|
|
ngx_rtmp_header_t *h, ngx_chain_t *in);
|
|
|
|
static ngx_int_t ngx_rtmp_record_node_av(ngx_rtmp_session_t *s,
|
|
|
|
ngx_rtmp_record_node_ctx_t *rctx, ngx_rtmp_header_t *h, ngx_chain_t *in);
|
2012-09-05 18:20:45 +02:00
|
|
|
static ngx_int_t ngx_rtmp_record_node_open(ngx_rtmp_session_t *s,
|
|
|
|
ngx_rtmp_record_node_ctx_t *rctx);
|
|
|
|
static ngx_int_t ngx_rtmp_record_node_close(ngx_rtmp_session_t *s,
|
|
|
|
ngx_rtmp_record_node_ctx_t *rctx);
|
|
|
|
static u_char * ngx_rtmp_record_make_path(ngx_rtmp_session_t *s,
|
|
|
|
ngx_rtmp_record_node_ctx_t *rctx);
|
|
|
|
static ngx_int_t ngx_rtmp_record_init(ngx_rtmp_session_t *s);
|
2012-03-23 15:57:25 +01:00
|
|
|
|
|
|
|
|
2012-04-11 15:07:06 +02:00
|
|
|
static ngx_conf_bitmask_t ngx_rtmp_record_mask[] = {
|
|
|
|
{ ngx_string("off"), NGX_RTMP_RECORD_OFF },
|
2012-04-12 06:12:15 +02:00
|
|
|
{ ngx_string("all"), NGX_RTMP_RECORD_AUDIO |
|
|
|
|
NGX_RTMP_RECORD_VIDEO },
|
2012-04-11 15:07:06 +02:00
|
|
|
{ ngx_string("audio"), NGX_RTMP_RECORD_AUDIO },
|
|
|
|
{ ngx_string("video"), NGX_RTMP_RECORD_VIDEO },
|
|
|
|
{ ngx_string("keyframes"), NGX_RTMP_RECORD_KEYFRAMES },
|
2012-09-05 12:33:21 +02:00
|
|
|
{ ngx_string("manual"), NGX_RTMP_RECORD_MANUAL },
|
2012-04-11 15:07:06 +02:00
|
|
|
{ ngx_null_string, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-03-23 15:57:25 +01:00
|
|
|
static ngx_command_t ngx_rtmp_record_commands[] = {
|
|
|
|
|
|
|
|
{ ngx_string("record"),
|
2012-09-05 14:49:39 +02:00
|
|
|
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|
|
|
|
|
NGX_RTMP_REC_CONF|NGX_CONF_1MORE,
|
2012-04-11 15:07:06 +02:00
|
|
|
ngx_conf_set_bitmask_slot,
|
|
|
|
NGX_RTMP_APP_CONF_OFFSET,
|
2012-09-04 18:36:25 +02:00
|
|
|
offsetof(ngx_rtmp_record_app_conf_t, def.flags),
|
2012-04-11 15:07:06 +02:00
|
|
|
ngx_rtmp_record_mask },
|
|
|
|
|
|
|
|
{ ngx_string("record_path"),
|
2012-09-05 14:49:39 +02:00
|
|
|
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|
|
|
|
|
NGX_RTMP_REC_CONF|NGX_CONF_TAKE1,
|
2012-03-23 15:57:25 +01:00
|
|
|
ngx_conf_set_str_slot,
|
|
|
|
NGX_RTMP_APP_CONF_OFFSET,
|
2012-09-04 18:36:25 +02:00
|
|
|
offsetof(ngx_rtmp_record_app_conf_t, def.path),
|
2012-03-23 15:57:25 +01:00
|
|
|
NULL },
|
|
|
|
|
2012-04-11 16:30:29 +02:00
|
|
|
{ ngx_string("record_suffix"),
|
2012-09-05 14:49:39 +02:00
|
|
|
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|
|
|
|
|
NGX_RTMP_REC_CONF|NGX_CONF_TAKE1,
|
2012-04-11 16:30:29 +02:00
|
|
|
ngx_conf_set_str_slot,
|
|
|
|
NGX_RTMP_APP_CONF_OFFSET,
|
2012-09-04 18:36:25 +02:00
|
|
|
offsetof(ngx_rtmp_record_app_conf_t, def.suffix),
|
2012-04-11 16:30:29 +02:00
|
|
|
NULL },
|
|
|
|
|
2012-06-20 16:37:52 +02:00
|
|
|
{ ngx_string("record_unique"),
|
2012-09-05 14:49:39 +02:00
|
|
|
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|
|
|
|
|
NGX_RTMP_REC_CONF|NGX_CONF_TAKE1,
|
2012-06-20 16:37:52 +02:00
|
|
|
ngx_conf_set_flag_slot,
|
|
|
|
NGX_RTMP_APP_CONF_OFFSET,
|
2012-09-04 18:36:25 +02:00
|
|
|
offsetof(ngx_rtmp_record_app_conf_t, def.unique),
|
2012-06-20 16:37:52 +02:00
|
|
|
NULL },
|
|
|
|
|
2012-04-11 15:07:06 +02:00
|
|
|
{ ngx_string("record_max_size"),
|
2012-09-05 14:49:39 +02:00
|
|
|
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|
|
|
|
|
NGX_RTMP_REC_CONF|NGX_CONF_TAKE1,
|
2012-03-23 15:57:25 +01:00
|
|
|
ngx_conf_set_size_slot,
|
|
|
|
NGX_RTMP_APP_CONF_OFFSET,
|
2012-09-04 18:36:25 +02:00
|
|
|
offsetof(ngx_rtmp_record_app_conf_t, def.max_size),
|
2012-03-23 15:57:25 +01:00
|
|
|
NULL },
|
|
|
|
|
2012-04-11 15:07:06 +02:00
|
|
|
{ ngx_string("record_max_frames"),
|
2012-09-05 14:49:39 +02:00
|
|
|
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|
|
|
|
|
NGX_RTMP_REC_CONF|NGX_CONF_TAKE1,
|
2012-04-11 15:07:06 +02:00
|
|
|
ngx_conf_set_size_slot,
|
|
|
|
NGX_RTMP_APP_CONF_OFFSET,
|
2012-09-04 18:36:25 +02:00
|
|
|
offsetof(ngx_rtmp_record_app_conf_t, def.max_frames),
|
2012-04-11 15:07:06 +02:00
|
|
|
NULL },
|
|
|
|
|
|
|
|
{ ngx_string("record_interval"),
|
2012-09-05 14:49:39 +02:00
|
|
|
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|
|
|
|
|
NGX_RTMP_REC_CONF|NGX_CONF_TAKE1,
|
2012-04-11 15:07:06 +02:00
|
|
|
ngx_conf_set_msec_slot,
|
|
|
|
NGX_RTMP_APP_CONF_OFFSET,
|
2012-09-04 18:36:25 +02:00
|
|
|
offsetof(ngx_rtmp_record_app_conf_t, def.interval),
|
2012-04-11 15:07:06 +02:00
|
|
|
NULL },
|
|
|
|
|
2012-03-29 12:07:57 +02:00
|
|
|
{ ngx_string("on_record_done"),
|
2012-09-05 14:49:39 +02:00
|
|
|
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|
|
|
|
|
NGX_RTMP_REC_CONF|NGX_CONF_TAKE1,
|
2012-03-29 12:07:57 +02:00
|
|
|
ngx_rtmp_notify_on_record_done,
|
|
|
|
NGX_RTMP_APP_CONF_OFFSET,
|
|
|
|
0,
|
|
|
|
NULL },
|
|
|
|
|
2012-09-05 14:49:39 +02:00
|
|
|
{ ngx_string("recorder"),
|
|
|
|
NGX_RTMP_APP_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1,
|
|
|
|
ngx_rtmp_record_recorder,
|
|
|
|
NGX_RTMP_APP_CONF_OFFSET,
|
|
|
|
0,
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
|
2012-03-23 15:57:25 +01:00
|
|
|
ngx_null_command
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_rtmp_module_t ngx_rtmp_record_module_ctx = {
|
|
|
|
NULL, /* preconfiguration */
|
|
|
|
ngx_rtmp_record_postconfiguration, /* postconfiguration */
|
|
|
|
NULL, /* create main configuration */
|
|
|
|
NULL, /* init main configuration */
|
|
|
|
NULL, /* create server configuration */
|
|
|
|
NULL, /* merge server configuration */
|
|
|
|
ngx_rtmp_record_create_app_conf, /* create app configuration */
|
|
|
|
ngx_rtmp_record_merge_app_conf /* merge app configuration */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ngx_module_t ngx_rtmp_record_module = {
|
|
|
|
NGX_MODULE_V1,
|
|
|
|
&ngx_rtmp_record_module_ctx, /* module context */
|
|
|
|
ngx_rtmp_record_commands, /* module directives */
|
|
|
|
NGX_RTMP_MODULE, /* module type */
|
2012-03-23 18:29:22 +01:00
|
|
|
NULL, /* init master */
|
2012-03-23 15:57:25 +01:00
|
|
|
NULL, /* init module */
|
|
|
|
NULL, /* init process */
|
|
|
|
NULL, /* init thread */
|
|
|
|
NULL, /* exit thread */
|
|
|
|
NULL, /* exit process */
|
|
|
|
NULL, /* exit master */
|
|
|
|
NGX_MODULE_V1_PADDING
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void *
|
|
|
|
ngx_rtmp_record_create_app_conf(ngx_conf_t *cf)
|
|
|
|
{
|
|
|
|
ngx_rtmp_record_app_conf_t *racf;
|
|
|
|
|
|
|
|
racf = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_record_app_conf_t));
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-03-23 15:57:25 +01:00
|
|
|
if (racf == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-04 18:36:25 +02:00
|
|
|
racf->def.max_size = NGX_CONF_UNSET;
|
|
|
|
racf->def.max_frames = NGX_CONF_UNSET;
|
|
|
|
racf->def.interval = NGX_CONF_UNSET;
|
|
|
|
racf->def.unique = NGX_CONF_UNSET;
|
2012-09-05 15:07:33 +02:00
|
|
|
racf->def.url = NGX_CONF_UNSET_PTR;
|
2012-09-04 18:36:25 +02:00
|
|
|
|
|
|
|
ngx_str_set(&racf->def.id, "default");
|
|
|
|
|
|
|
|
if (ngx_array_init(&racf->nodes, cf->pool, 1,
|
|
|
|
sizeof(ngx_rtmp_record_node_t *))
|
|
|
|
!= NGX_OK)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-03-23 15:57:25 +01:00
|
|
|
|
|
|
|
return racf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_rtmp_record_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
|
|
{
|
|
|
|
ngx_rtmp_record_app_conf_t *prev = parent;
|
|
|
|
ngx_rtmp_record_app_conf_t *conf = child;
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_rtmp_record_node_t **node;
|
|
|
|
|
|
|
|
ngx_conf_merge_str_value(conf->def.path, prev->def.path, "");
|
|
|
|
ngx_conf_merge_str_value(conf->def.suffix, prev->def.suffix, ".flv");
|
|
|
|
ngx_conf_merge_size_value(conf->def.max_size, prev->def.max_size, 0);
|
|
|
|
ngx_conf_merge_size_value(conf->def.max_frames, prev->def.max_frames, 0);
|
|
|
|
ngx_conf_merge_value(conf->def.unique, prev->def.unique, 0);
|
|
|
|
ngx_conf_merge_msec_value(conf->def.interval, prev->def.interval,
|
2012-09-04 15:47:32 +02:00
|
|
|
(ngx_msec_t) NGX_CONF_UNSET);
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_conf_merge_bitmask_value(conf->def.flags, prev->def.flags, 0);
|
2012-09-05 15:07:33 +02:00
|
|
|
ngx_conf_merge_ptr_value(conf->def.url, prev->def.url, NULL);
|
2012-09-04 18:36:25 +02:00
|
|
|
|
|
|
|
if (conf->def.flags) {
|
|
|
|
node = ngx_array_push(&conf->nodes);
|
|
|
|
if (node == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
*node = &conf->def;
|
|
|
|
}
|
2012-03-23 15:57:25 +01:00
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_rtmp_record_write_header(ngx_file_t *file)
|
|
|
|
{
|
|
|
|
static u_char flv_header[] = {
|
|
|
|
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,
|
2012-03-23 18:29:22 +01:00
|
|
|
0x00 /* PreviousTagSize0 (not actually a header) */
|
2012-03-23 15:57:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return ngx_write_file(file, flv_header, sizeof(flv_header), 0) == NGX_ERROR
|
2012-09-04 15:47:32 +02:00
|
|
|
? NGX_ERROR
|
|
|
|
: NGX_OK;
|
2012-03-23 15:57:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-05 18:20:45 +02:00
|
|
|
static ngx_rtmp_record_node_ctx_t *
|
|
|
|
ngx_rtmp_record_get_node_ctx(ngx_rtmp_session_t *s, ngx_uint_t n)
|
|
|
|
{
|
|
|
|
ngx_rtmp_record_ctx_t *ctx;
|
|
|
|
ngx_rtmp_record_node_ctx_t *nodes;
|
|
|
|
|
|
|
|
if (ngx_rtmp_record_init(s) != NGX_OK) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_record_module);
|
|
|
|
|
|
|
|
if (n >= ctx->nodes.nelts) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes = ctx->nodes.elts;
|
|
|
|
|
|
|
|
return &nodes[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t
|
|
|
|
ngx_rtmp_record_open(ngx_rtmp_session_t *s, ngx_uint_t n, u_char **path)
|
|
|
|
{
|
|
|
|
ngx_rtmp_record_node_ctx_t *rctx;
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
|
|
|
"record: #%ui manual open", n);
|
|
|
|
|
|
|
|
rctx = ngx_rtmp_record_get_node_ctx(s, n);
|
|
|
|
|
|
|
|
if (rctx == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_rtmp_record_node_open(s, rctx) != NGX_OK) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
*path = ngx_rtmp_record_make_path(s, rctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t
|
|
|
|
ngx_rtmp_record_close(ngx_rtmp_session_t *s, ngx_uint_t n, u_char **path)
|
|
|
|
{
|
|
|
|
ngx_rtmp_record_node_ctx_t *rctx;
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
|
|
|
"record: #%ui manual close", n);
|
|
|
|
|
|
|
|
rctx = ngx_rtmp_record_get_node_ctx(s, n);
|
|
|
|
|
|
|
|
if (rctx == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_rtmp_record_node_close(s, rctx) != NGX_OK) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
*path = ngx_rtmp_record_make_path(s, rctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-20 16:37:52 +02:00
|
|
|
/* This funcion returns pointer to a static buffer */
|
2012-09-05 18:20:45 +02:00
|
|
|
static u_char *
|
2012-09-04 15:47:32 +02:00
|
|
|
ngx_rtmp_record_make_path(ngx_rtmp_session_t *s,
|
|
|
|
ngx_rtmp_record_node_ctx_t *rctx)
|
2012-06-20 16:37:52 +02:00
|
|
|
{
|
|
|
|
ngx_rtmp_record_ctx_t *ctx;
|
|
|
|
u_char *p, *l;
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_rtmp_record_node_t *rc;
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-06-20 16:37:52 +02:00
|
|
|
static u_char buf[NGX_TIME_T_LEN + 1];
|
|
|
|
static u_char path[NGX_MAX_PATH + 1];
|
|
|
|
|
|
|
|
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_record_module);
|
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
rc = rctx->conf;
|
|
|
|
|
2012-06-20 16:37:52 +02:00
|
|
|
/* create file path */
|
|
|
|
p = path;
|
|
|
|
l = path + sizeof(path) - 1;
|
2012-09-04 15:47:32 +02:00
|
|
|
|
|
|
|
p = ngx_cpymem(p, rc->path.data,
|
|
|
|
ngx_min(rc->path.len, (size_t)(l - p - 1)));
|
2012-06-20 16:37:52 +02:00
|
|
|
*p++ = '/';
|
|
|
|
p = (u_char *)ngx_escape_uri(p, ctx->name, ngx_min(ngx_strlen(ctx->name),
|
|
|
|
(size_t)(l - p)), NGX_ESCAPE_URI_COMPONENT);
|
2012-09-04 15:47:32 +02:00
|
|
|
|
|
|
|
/* append timestamp */
|
|
|
|
if (rc->unique) {
|
2012-06-20 16:37:52 +02:00
|
|
|
p = ngx_cpymem(p, buf, ngx_min(ngx_sprintf(buf, "-%T",
|
2012-09-04 15:47:32 +02:00
|
|
|
rctx->timestamp) - buf, l - p));
|
2012-06-20 16:37:52 +02:00
|
|
|
}
|
2012-09-04 15:47:32 +02:00
|
|
|
|
|
|
|
p = ngx_cpymem(p, rc->suffix.data,
|
|
|
|
ngx_min(rc->suffix.len, (size_t)(l - p)));
|
2012-06-20 16:37:52 +02:00
|
|
|
*p = 0;
|
|
|
|
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
|
|
|
"record: %V path: '%s'", &rc->id, path);
|
|
|
|
|
2012-06-20 16:37:52 +02:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-05 18:20:45 +02:00
|
|
|
static ngx_int_t
|
|
|
|
ngx_rtmp_record_node_open(ngx_rtmp_session_t *s,
|
|
|
|
ngx_rtmp_record_node_ctx_t *rctx)
|
2012-04-11 15:07:06 +02:00
|
|
|
{
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_rtmp_record_node_t *rc;
|
|
|
|
ngx_err_t err;
|
|
|
|
u_char *path;
|
|
|
|
|
|
|
|
rc = rctx->conf;
|
2012-04-11 15:07:06 +02:00
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
if (rctx->file.fd != NGX_INVALID_FILE) {
|
|
|
|
return NGX_OK;
|
2012-04-11 15:07:06 +02:00
|
|
|
}
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
|
|
|
"record: %V opening", &rc->id);
|
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
rctx->timestamp = ngx_cached_time->sec;
|
|
|
|
|
|
|
|
path = ngx_rtmp_record_make_path(s, rctx);
|
|
|
|
|
|
|
|
rctx->nframes = 0;
|
|
|
|
|
|
|
|
ngx_memzero(&rctx->file, sizeof(rctx->file));
|
|
|
|
|
|
|
|
rctx->last = *ngx_cached_time;
|
|
|
|
rctx->file.offset = 0;
|
|
|
|
rctx->file.log = s->connection->log;
|
|
|
|
rctx->file.fd = ngx_open_file(path, NGX_FILE_WRONLY, NGX_FILE_TRUNCATE,
|
|
|
|
NGX_FILE_DEFAULT_ACCESS);
|
|
|
|
|
|
|
|
if (rctx->file.fd == NGX_INVALID_FILE) {
|
2012-04-11 15:07:06 +02:00
|
|
|
err = ngx_errno;
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-04-11 15:07:06 +02:00
|
|
|
if (err != NGX_ENOENT) {
|
|
|
|
ngx_log_error(NGX_LOG_CRIT, s->connection->log, err,
|
2012-09-04 18:36:25 +02:00
|
|
|
"record: %V failed to open file '%s'",
|
|
|
|
&rc->id, path);
|
2012-04-11 15:07:06 +02:00
|
|
|
}
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-06-09 17:22:18 +02:00
|
|
|
return NGX_OK;
|
2012-04-11 15:07:06 +02:00
|
|
|
}
|
|
|
|
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
|
|
|
"record: %V opened '%s'", &rc->id, path);
|
2012-04-11 15:07:06 +02:00
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-23 15:57:25 +01:00
|
|
|
static ngx_int_t
|
2012-09-05 18:20:45 +02:00
|
|
|
ngx_rtmp_record_init(ngx_rtmp_session_t *s)
|
2012-09-04 15:47:32 +02:00
|
|
|
{
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_rtmp_record_app_conf_t *racf;
|
2012-09-04 15:47:32 +02:00
|
|
|
ngx_rtmp_record_ctx_t *ctx;
|
|
|
|
ngx_rtmp_record_node_ctx_t *rctx;
|
2012-09-05 18:20:45 +02:00
|
|
|
ngx_rtmp_record_node_t **node;
|
|
|
|
ngx_uint_t n;
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-09-05 18:20:45 +02:00
|
|
|
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_record_module);
|
|
|
|
|
|
|
|
if (ctx) {
|
|
|
|
return NGX_OK;
|
2012-09-04 15:47:32 +02:00
|
|
|
}
|
|
|
|
|
2012-09-04 18:36:25 +02:00
|
|
|
racf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_record_module);
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-09-04 18:36:25 +02:00
|
|
|
if (racf == NULL || racf->nodes.nelts == 0) {
|
2012-09-05 18:20:45 +02:00
|
|
|
return NGX_OK;
|
2012-09-04 18:36:25 +02:00
|
|
|
}
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-09-05 18:20:45 +02:00
|
|
|
ctx = ngx_pcalloc(s->connection->pool, sizeof(ngx_rtmp_record_ctx_t));
|
2012-09-04 15:47:32 +02:00
|
|
|
|
|
|
|
if (ctx == NULL) {
|
2012-09-05 18:20:45 +02:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-09-05 18:20:45 +02:00
|
|
|
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_record_module);
|
2012-03-23 15:57:25 +01:00
|
|
|
|
2012-09-05 18:20:45 +02:00
|
|
|
if (ngx_array_init(&ctx->nodes, s->connection->pool, racf->nodes.nelts,
|
|
|
|
sizeof(ngx_rtmp_record_node_ctx_t))
|
2012-09-04 18:36:25 +02:00
|
|
|
!= NGX_OK)
|
2012-09-05 18:20:45 +02:00
|
|
|
{
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-09-05 18:20:45 +02:00
|
|
|
node = racf->nodes.elts;
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-09-05 18:20:45 +02:00
|
|
|
rctx = ngx_array_push_n(&ctx->nodes, racf->nodes.nelts);
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-09-05 18:20:45 +02:00
|
|
|
if (rctx == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2012-03-23 15:57:25 +01:00
|
|
|
|
2012-09-05 18:20:45 +02:00
|
|
|
for (n = 0; n < racf->nodes.nelts; ++n, ++node, ++rctx) {
|
|
|
|
ngx_memzero(rctx, sizeof(*rctx));
|
2012-03-23 15:57:25 +01:00
|
|
|
|
2012-09-05 18:20:45 +02:00
|
|
|
rctx->conf = *node;
|
|
|
|
rctx->file.fd = NGX_INVALID_FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_rtmp_record_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
|
|
|
|
{
|
|
|
|
ngx_rtmp_record_app_conf_t *racf;
|
|
|
|
ngx_rtmp_record_ctx_t *ctx;
|
|
|
|
u_char *p;
|
|
|
|
ngx_uint_t n;
|
|
|
|
ngx_rtmp_record_node_ctx_t *rctx;
|
|
|
|
|
|
|
|
if (s->auto_pushed) {
|
|
|
|
goto next;
|
2012-09-04 18:36:25 +02:00
|
|
|
}
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-09-05 18:20:45 +02:00
|
|
|
racf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_record_module);
|
|
|
|
|
|
|
|
if (racf == NULL || racf->nodes.nelts == 0) {
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_rtmp_record_init(s) != NGX_OK) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
|
|
|
"record: publish %ui nodes",
|
|
|
|
racf->nodes.nelts);
|
|
|
|
|
|
|
|
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_record_module);
|
|
|
|
|
2012-06-20 16:37:52 +02:00
|
|
|
ngx_memcpy(ctx->name, v->name, sizeof(ctx->name));
|
2012-06-29 14:00:00 +02:00
|
|
|
ngx_memcpy(ctx->args, v->args, sizeof(ctx->args));
|
2012-03-23 15:57:25 +01:00
|
|
|
|
2012-07-23 15:19:25 +02:00
|
|
|
/* terminate name on /../ */
|
|
|
|
for (p = ctx->name; *p; ++p) {
|
|
|
|
if (ngx_path_separator(p[0]) &&
|
|
|
|
p[1] == '.' && p[2] == '.' &&
|
|
|
|
ngx_path_separator(p[3]))
|
|
|
|
{
|
|
|
|
*p = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-04 18:36:25 +02:00
|
|
|
rctx = ctx->nodes.elts;
|
|
|
|
|
|
|
|
for (n = 0; n < ctx->nodes.nelts; ++n, ++rctx) {
|
2012-09-05 12:33:21 +02:00
|
|
|
if (rctx->conf->flags & (NGX_RTMP_RECORD_OFF|NGX_RTMP_RECORD_MANUAL)) {
|
2012-09-04 15:47:32 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-09-05 18:20:45 +02:00
|
|
|
ngx_rtmp_record_node_open(s, rctx);
|
2012-03-27 18:26:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
next:
|
|
|
|
return next_publish(s, v);
|
2012-03-23 15:57:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-29 12:07:57 +02:00
|
|
|
static ngx_chain_t *
|
|
|
|
ngx_rtmp_record_notify_create(ngx_rtmp_session_t *s, void *arg,
|
2012-09-04 15:47:32 +02:00
|
|
|
ngx_pool_t *pool)
|
2012-03-29 12:07:57 +02:00
|
|
|
{
|
2012-09-04 15:47:32 +02:00
|
|
|
ngx_rtmp_record_node_ctx_t *rctx = arg;
|
2012-09-05 15:07:33 +02:00
|
|
|
ngx_rtmp_record_node_t *rc;
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-04-11 15:07:06 +02:00
|
|
|
ngx_rtmp_record_ctx_t *ctx;
|
2012-03-29 12:07:57 +02:00
|
|
|
ngx_chain_t *hl, *cl, *pl;
|
|
|
|
ngx_buf_t *b;
|
2012-07-12 14:53:16 +02:00
|
|
|
ngx_str_t *addr_text;
|
2012-06-29 14:00:00 +02:00
|
|
|
size_t path_len, name_len, args_len;
|
2012-06-20 16:37:52 +02:00
|
|
|
u_char *path;
|
2012-03-29 12:07:57 +02:00
|
|
|
|
2012-04-11 15:07:06 +02:00
|
|
|
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_record_module);
|
2012-03-29 12:07:57 +02:00
|
|
|
|
2012-09-05 15:07:33 +02:00
|
|
|
rc = rctx->conf;
|
|
|
|
|
2012-03-29 12:07:57 +02:00
|
|
|
/* common variables */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
path = ngx_rtmp_record_make_path(s, rctx);
|
2012-06-29 14:00:00 +02:00
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
path_len = ngx_strlen(path);
|
|
|
|
name_len = ngx_strlen(ctx->name);
|
|
|
|
args_len = ngx_strlen(ctx->args);
|
2012-07-12 14:53:16 +02:00
|
|
|
addr_text = &s->connection->addr_text;
|
2012-04-11 15:07:06 +02:00
|
|
|
|
2012-03-29 12:07:57 +02:00
|
|
|
b = ngx_create_temp_buf(pool,
|
2012-09-04 15:47:32 +02:00
|
|
|
sizeof("&call=record_done") +
|
2012-09-05 15:07:33 +02:00
|
|
|
sizeof("&recorder=") + rc->id.len +
|
2012-09-04 15:47:32 +02:00
|
|
|
sizeof("&addr=") + addr_text->len +
|
|
|
|
sizeof("&name=") + name_len * 3 +
|
|
|
|
sizeof("&path=") + path_len * 3 +
|
|
|
|
+ 1 + args_len);
|
2012-03-29 12:07:57 +02:00
|
|
|
if (b == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pl->buf = b;
|
|
|
|
|
|
|
|
b->last = ngx_cpymem(b->last, (u_char*)"&call=record_done",
|
2012-09-04 15:47:32 +02:00
|
|
|
sizeof("&call=record_done") - 1);
|
2012-03-29 12:07:57 +02:00
|
|
|
|
2012-09-05 15:07:33 +02:00
|
|
|
b->last = ngx_cpymem(b->last, (u_char *)"&recorder=",
|
|
|
|
sizeof("&recorder=") - 1);
|
|
|
|
b->last = (u_char*)ngx_escape_uri(b->last, rc->id.data, rc->id.len, 0);
|
|
|
|
|
2012-07-12 14:53:16 +02:00
|
|
|
b->last = ngx_cpymem(b->last, (u_char*)"&addr=", sizeof("&addr=") -1);
|
|
|
|
b->last = (u_char*)ngx_escape_uri(b->last, addr_text->data,
|
2012-09-04 15:47:32 +02:00
|
|
|
addr_text->len, 0);
|
2012-07-12 14:53:16 +02:00
|
|
|
|
2012-06-29 14:00:00 +02:00
|
|
|
b->last = ngx_cpymem(b->last, (u_char*)"&name=", sizeof("&name=") - 1);
|
|
|
|
b->last = (u_char*)ngx_escape_uri(b->last, ctx->name, name_len, 0);
|
|
|
|
|
2012-03-29 12:07:57 +02:00
|
|
|
b->last = ngx_cpymem(b->last, (u_char*)"&path=", sizeof("&path=") - 1);
|
2012-06-29 14:00:00 +02:00
|
|
|
b->last = (u_char*)ngx_escape_uri(b->last, path, path_len, 0);
|
|
|
|
|
|
|
|
if (args_len) {
|
|
|
|
*b->last++ = '&';
|
|
|
|
b->last = (u_char *)ngx_cpymem(b->last, ctx->args, args_len);
|
|
|
|
}
|
2012-03-29 12:07:57 +02:00
|
|
|
|
|
|
|
/* HTTP header */
|
2012-09-04 15:47:32 +02:00
|
|
|
hl = ngx_rtmp_netcall_http_format_header(rctx->conf->url, pool,
|
2012-03-30 17:07:14 +02:00
|
|
|
cl->buf->last - cl->buf->pos + (pl->buf->last - pl->buf->pos),
|
|
|
|
&ngx_rtmp_netcall_content_type_urlencoded);
|
2012-03-29 12:07:57 +02:00
|
|
|
|
|
|
|
if (hl == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
hl->next = cl;
|
|
|
|
cl->next = pl;
|
2012-03-29 14:10:11 +02:00
|
|
|
pl->next = NULL;
|
2012-03-29 12:07:57 +02:00
|
|
|
|
|
|
|
return hl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
2012-09-04 15:47:32 +02:00
|
|
|
ngx_rtmp_record_notify(ngx_rtmp_session_t *s, ngx_rtmp_record_node_ctx_t *rctx)
|
2012-03-29 12:07:57 +02:00
|
|
|
{
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_rtmp_netcall_init_t ci;
|
|
|
|
ngx_rtmp_record_node_t *rc;
|
2012-03-29 12:07:57 +02:00
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
rc = rctx->conf;
|
|
|
|
|
|
|
|
if (rc->url == NULL) {
|
2012-03-29 12:07:57 +02:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_memzero(&ci, sizeof(ci));
|
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
ci.url = rc->url;
|
2012-03-29 12:07:57 +02:00
|
|
|
ci.create = ngx_rtmp_record_notify_create;
|
2012-09-04 15:47:32 +02:00
|
|
|
ci.arg = rctx;
|
2012-03-29 12:07:57 +02:00
|
|
|
|
|
|
|
return ngx_rtmp_netcall_create(s, &ci);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-05 18:20:45 +02:00
|
|
|
static ngx_int_t
|
|
|
|
ngx_rtmp_record_node_close(ngx_rtmp_session_t *s,
|
|
|
|
ngx_rtmp_record_node_ctx_t *rctx)
|
2012-03-23 15:57:25 +01:00
|
|
|
{
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_rtmp_record_node_t *rc;
|
|
|
|
ngx_err_t err;
|
|
|
|
|
|
|
|
rc = rctx->conf;
|
2012-03-23 15:57:25 +01:00
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
if (rctx->file.fd == NGX_INVALID_FILE) {
|
2012-03-23 15:57:25 +01:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
if (ngx_close_file(rctx->file.fd) == NGX_FILE_ERROR) {
|
2012-03-23 15:57:25 +01:00
|
|
|
err = ngx_errno;
|
|
|
|
ngx_log_error(NGX_LOG_CRIT, s->connection->log, err,
|
2012-09-04 18:36:25 +02:00
|
|
|
"record: %V error closing file", &rc->id);
|
2012-03-23 15:57:25 +01:00
|
|
|
}
|
2012-09-04 15:47:32 +02:00
|
|
|
|
|
|
|
rctx->file.fd = NGX_INVALID_FILE;
|
2012-03-23 15:57:25 +01:00
|
|
|
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
|
|
|
"record: %V closed", &rc->id);
|
2012-03-23 15:57:25 +01:00
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
return ngx_rtmp_record_notify(s, rctx);
|
2012-03-23 15:57:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-27 18:26:43 +02:00
|
|
|
static ngx_int_t
|
|
|
|
ngx_rtmp_record_delete_stream(ngx_rtmp_session_t *s,
|
2012-09-05 12:33:21 +02:00
|
|
|
ngx_rtmp_delete_stream_t *v)
|
2012-03-27 18:26:43 +02:00
|
|
|
{
|
2012-09-04 15:47:32 +02:00
|
|
|
ngx_rtmp_record_ctx_t *ctx;
|
|
|
|
ngx_rtmp_record_node_ctx_t *rctx;
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_uint_t n;
|
2012-09-04 15:47:32 +02:00
|
|
|
|
|
|
|
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_record_module);
|
|
|
|
|
|
|
|
if (ctx == NULL) {
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
|
|
|
"record: delete_stream %ui nodes",
|
|
|
|
ctx->nodes.nelts);
|
|
|
|
|
|
|
|
rctx = ctx->nodes.elts;
|
|
|
|
|
|
|
|
for (n = 0; n < ctx->nodes.nelts; ++n, ++rctx) {
|
2012-09-05 18:20:45 +02:00
|
|
|
ngx_rtmp_record_node_close(s, rctx);
|
2012-09-04 15:47:32 +02:00
|
|
|
}
|
2012-03-27 18:26:43 +02:00
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
next:
|
2012-03-27 18:26:43 +02:00
|
|
|
return next_delete_stream(s, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-23 15:57:25 +01:00
|
|
|
static ngx_int_t
|
2012-09-04 15:47:32 +02:00
|
|
|
ngx_rtmp_record_write_frame(ngx_rtmp_session_t *s,
|
|
|
|
ngx_rtmp_record_node_ctx_t *rctx,
|
|
|
|
ngx_rtmp_header_t *h, ngx_chain_t *in)
|
2012-03-23 15:57:25 +01:00
|
|
|
{
|
2012-09-04 18:36:25 +02:00
|
|
|
u_char hdr[11], *p, *ph;
|
|
|
|
uint32_t timestamp, tag_size;
|
|
|
|
ngx_rtmp_record_node_t *rc;
|
2012-03-23 15:57:25 +01:00
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
rc = rctx->conf;
|
2012-03-23 15:57:25 +01:00
|
|
|
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
|
|
|
"record: %V frame: mlen=%uD", &rc->id, h->mlen);
|
2012-03-23 15:57:25 +01:00
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
timestamp = h->timestamp - rctx->epoch;
|
2012-03-23 15:57:25 +01:00
|
|
|
|
|
|
|
/* write tag header */
|
|
|
|
ph = hdr;
|
|
|
|
|
|
|
|
*ph++ = (u_char)h->type;
|
|
|
|
|
|
|
|
p = (u_char*)&h->mlen;
|
|
|
|
*ph++ = p[2];
|
|
|
|
*ph++ = p[1];
|
|
|
|
*ph++ = p[0];
|
|
|
|
|
|
|
|
p = (u_char*)×tamp;
|
|
|
|
*ph++ = p[2];
|
|
|
|
*ph++ = p[1];
|
|
|
|
*ph++ = p[0];
|
|
|
|
*ph++ = p[3];
|
|
|
|
|
|
|
|
*ph++ = 0;
|
|
|
|
*ph++ = 0;
|
|
|
|
*ph++ = 0;
|
|
|
|
|
|
|
|
tag_size = (ph - hdr) + h->mlen;
|
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
if (ngx_write_file(&rctx->file, hdr, ph - hdr, rctx->file.offset)
|
|
|
|
== NGX_ERROR)
|
2012-03-23 15:57:25 +01:00
|
|
|
{
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-03-23 18:29:22 +01:00
|
|
|
/* write tag body
|
|
|
|
* FIXME: NGINX
|
|
|
|
* ngx_write_chain seems to fit best
|
|
|
|
* but it suffers from uncontrollable
|
|
|
|
* allocations.
|
2012-03-23 15:57:25 +01:00
|
|
|
* we're left with plain writing */
|
|
|
|
for(; in; in = in->next) {
|
|
|
|
if (in->buf->pos == in->buf->last) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-09-04 15:47:32 +02:00
|
|
|
|
|
|
|
if (ngx_write_file(&rctx->file, in->buf->pos, in->buf->last
|
|
|
|
- in->buf->pos, rctx->file.offset)
|
|
|
|
== NGX_ERROR)
|
2012-03-23 15:57:25 +01:00
|
|
|
{
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write tag size */
|
|
|
|
ph = hdr;
|
|
|
|
p = (u_char*)&tag_size;
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-03-23 15:57:25 +01:00
|
|
|
*ph++ = p[3];
|
|
|
|
*ph++ = p[2];
|
|
|
|
*ph++ = p[1];
|
|
|
|
*ph++ = p[0];
|
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
if (ngx_write_file(&rctx->file, hdr, ph - hdr,
|
|
|
|
rctx->file.offset)
|
|
|
|
== NGX_ERROR)
|
2012-03-23 15:57:25 +01:00
|
|
|
{
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
++rctx->nframes;
|
2012-04-11 15:07:06 +02:00
|
|
|
|
2012-03-23 15:57:25 +01:00
|
|
|
/* watch max size */
|
2012-09-04 18:36:25 +02:00
|
|
|
if ((rc->max_size && rctx->file.offset >= (ngx_int_t) rc->max_size) ||
|
2012-09-04 15:47:32 +02:00
|
|
|
(rc->max_frames && rctx->nframes >= rc->max_frames))
|
2012-03-23 15:57:25 +01:00
|
|
|
{
|
2012-09-05 18:20:45 +02:00
|
|
|
ngx_rtmp_record_node_close(s, rctx);
|
2012-03-23 15:57:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-09 17:22:18 +02:00
|
|
|
static size_t
|
|
|
|
ngx_rtmp_record_get_chain_mlen(ngx_chain_t *in)
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
for (ret = 0; in; in = in->next) {
|
|
|
|
ret += (in->buf->last - in->buf->pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_rtmp_record_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
|
2012-09-04 15:47:32 +02:00
|
|
|
ngx_chain_t *in)
|
2012-06-09 17:22:18 +02:00
|
|
|
{
|
|
|
|
ngx_rtmp_record_ctx_t *ctx;
|
2012-09-04 15:47:32 +02:00
|
|
|
ngx_rtmp_record_node_ctx_t *rctx;
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_uint_t n;
|
2012-06-09 17:22:18 +02:00
|
|
|
|
|
|
|
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_record_module);
|
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
if (ctx == NULL) {
|
2012-09-03 17:54:05 +02:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-04 18:36:25 +02:00
|
|
|
rctx = ctx->nodes.elts;
|
|
|
|
|
|
|
|
for (n = 0; n < ctx->nodes.nelts; ++n, ++rctx) {
|
2012-09-04 15:47:32 +02:00
|
|
|
ngx_rtmp_record_node_av(s, rctx, h, in);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_rtmp_record_node_av(ngx_rtmp_session_t *s, ngx_rtmp_record_node_ctx_t *rctx,
|
|
|
|
ngx_rtmp_header_t *h, ngx_chain_t *in)
|
|
|
|
{
|
|
|
|
ngx_time_t next;
|
|
|
|
ngx_rtmp_header_t ch;
|
|
|
|
ngx_rtmp_codec_ctx_t *codec_ctx;
|
|
|
|
ngx_int_t keyframe;
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_rtmp_record_node_t *rc;
|
2012-09-04 15:47:32 +02:00
|
|
|
|
|
|
|
rc = rctx->conf;
|
|
|
|
|
|
|
|
if (rc->flags & NGX_RTMP_RECORD_OFF) {
|
2012-09-05 18:20:45 +02:00
|
|
|
ngx_rtmp_record_node_close(s, rctx);
|
2012-06-09 17:22:18 +02:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-05 00:33:49 +02:00
|
|
|
keyframe = (ngx_rtmp_get_video_frame_type(in) == NGX_RTMP_VIDEO_KEY_FRAME);
|
|
|
|
|
2012-09-05 12:33:21 +02:00
|
|
|
if (keyframe && (rc->flags & NGX_RTMP_RECORD_MANUAL) == 0) {
|
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
if (rc->interval != (ngx_msec_t) NGX_CONF_UNSET) {
|
|
|
|
|
|
|
|
next = rctx->last;
|
|
|
|
next.msec += rc->interval;
|
|
|
|
next.sec += (next.msec / 1000);
|
2012-09-03 17:54:05 +02:00
|
|
|
next.msec %= 1000;
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-09-05 12:33:21 +02:00
|
|
|
if (ngx_cached_time->sec > next.sec ||
|
2012-09-04 15:47:32 +02:00
|
|
|
(ngx_cached_time->sec == next.sec &&
|
|
|
|
ngx_cached_time->msec > next.msec))
|
2012-09-03 17:54:05 +02:00
|
|
|
{
|
2012-09-05 18:20:45 +02:00
|
|
|
ngx_rtmp_record_node_close(s, rctx);
|
|
|
|
ngx_rtmp_record_node_open(s, rctx);
|
2012-06-09 17:22:18 +02:00
|
|
|
}
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-09-05 12:33:21 +02:00
|
|
|
} else {
|
2012-09-05 18:20:45 +02:00
|
|
|
ngx_rtmp_record_node_open(s, rctx);
|
2012-06-09 17:22:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
if (rctx->file.fd == NGX_INVALID_FILE) {
|
2012-06-09 17:22:18 +02:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (h->type == NGX_RTMP_MSG_AUDIO &&
|
2012-09-04 15:47:32 +02:00
|
|
|
(rc->flags & NGX_RTMP_RECORD_AUDIO) == 0)
|
2012-06-09 17:22:18 +02:00
|
|
|
{
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (h->type == NGX_RTMP_MSG_VIDEO &&
|
2012-09-04 15:47:32 +02:00
|
|
|
(rc->flags & NGX_RTMP_RECORD_VIDEO) == 0 &&
|
|
|
|
((rc->flags & NGX_RTMP_RECORD_KEYFRAMES) == 0 || !keyframe))
|
2012-06-09 17:22:18 +02:00
|
|
|
{
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
if (rctx->file.offset == 0) {
|
|
|
|
rctx->epoch = h->timestamp;
|
2012-06-09 17:22:18 +02:00
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
if (ngx_rtmp_record_write_header(&rctx->file) != NGX_OK) {
|
2012-09-05 18:20:45 +02:00
|
|
|
ngx_rtmp_record_node_close(s, rctx);
|
2012-06-09 19:29:19 +02:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-09 17:22:18 +02:00
|
|
|
codec_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module);
|
|
|
|
if (codec_ctx) {
|
|
|
|
ch = *h;
|
|
|
|
|
2012-07-02 16:04:50 +02:00
|
|
|
#if 0
|
|
|
|
/* metadata */
|
|
|
|
if (codec_ctx->meta) {
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
|
|
|
"record: writing metadata");
|
|
|
|
ch.type = NGX_RTMP_MSG_AMF_META;
|
|
|
|
ch.mlen = ngx_rtmp_record_get_chain_mlen(codec_ctx->meta);
|
|
|
|
if (ngx_rtmp_record_write_frame(s, &ch, codec_ctx->meta)
|
|
|
|
!= NGX_OK)
|
|
|
|
{
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* AAC header */
|
2012-09-04 15:47:32 +02:00
|
|
|
if (codec_ctx->aac_header && (rc->flags & NGX_RTMP_RECORD_AUDIO))
|
2012-06-09 19:29:19 +02:00
|
|
|
{
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
|
|
|
"record: %V writing AAC header", &rc->id);
|
|
|
|
|
2012-06-09 17:22:18 +02:00
|
|
|
ch.type = NGX_RTMP_MSG_AUDIO;
|
|
|
|
ch.mlen = ngx_rtmp_record_get_chain_mlen(codec_ctx->aac_header);
|
2012-09-04 15:47:32 +02:00
|
|
|
|
|
|
|
if (ngx_rtmp_record_write_frame(s, rctx, &ch, codec_ctx->aac_header)
|
|
|
|
!= NGX_OK)
|
2012-06-09 17:22:18 +02:00
|
|
|
{
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-02 16:04:50 +02:00
|
|
|
/* AVC header */
|
2012-09-04 15:47:32 +02:00
|
|
|
if (codec_ctx->avc_header &&
|
|
|
|
(rc->flags & (NGX_RTMP_RECORD_VIDEO|NGX_RTMP_RECORD_KEYFRAMES)))
|
2012-06-09 19:29:19 +02:00
|
|
|
{
|
2012-09-04 18:36:25 +02:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
|
|
|
"record: %V writing AVC header", &rc->id);
|
2012-09-04 15:47:32 +02:00
|
|
|
|
2012-06-09 17:22:18 +02:00
|
|
|
ch.type = NGX_RTMP_MSG_VIDEO;
|
|
|
|
ch.mlen = ngx_rtmp_record_get_chain_mlen(codec_ctx->avc_header);
|
2012-09-04 15:47:32 +02:00
|
|
|
|
|
|
|
if (ngx_rtmp_record_write_frame(s, rctx, &ch, codec_ctx->avc_header)
|
|
|
|
!= NGX_OK)
|
2012-06-09 17:22:18 +02:00
|
|
|
{
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-04 15:47:32 +02:00
|
|
|
return ngx_rtmp_record_write_frame(s, rctx, h, in);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-29 12:07:57 +02:00
|
|
|
static char *
|
|
|
|
ngx_rtmp_notify_on_record_done(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
|
|
{
|
|
|
|
ngx_rtmp_record_app_conf_t *racf;
|
|
|
|
ngx_str_t *url;
|
|
|
|
ngx_url_t *u;
|
|
|
|
size_t add;
|
|
|
|
ngx_str_t *value;
|
|
|
|
|
|
|
|
value = cf->args->elts;
|
|
|
|
url = &value[1];
|
|
|
|
|
|
|
|
add = 0;
|
|
|
|
|
|
|
|
u = ngx_pcalloc(cf->pool, sizeof(ngx_url_t));
|
|
|
|
if (u == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_strncasecmp(url->data, (u_char *) "http://", 7) == 0) {
|
|
|
|
add = 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
u->url.len = url->len - add;
|
|
|
|
u->url.data = url->data + add;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
racf = ngx_rtmp_conf_get_module_app_conf(cf, ngx_rtmp_record_module);
|
|
|
|
|
2012-09-04 18:36:25 +02:00
|
|
|
racf->def.url = u;
|
2012-03-29 12:07:57 +02:00
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-05 14:49:39 +02:00
|
|
|
static char *
|
|
|
|
ngx_rtmp_record_recorder(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
|
|
{
|
|
|
|
char *rv, *rvm;
|
|
|
|
ngx_int_t i;
|
|
|
|
ngx_str_t *value;
|
|
|
|
ngx_conf_t save;
|
|
|
|
ngx_rtmp_module_t *module;
|
|
|
|
ngx_rtmp_record_app_conf_t *racf, *rracf;
|
|
|
|
ngx_rtmp_record_node_t **prc, *rc;
|
|
|
|
ngx_rtmp_conf_ctx_t *ctx, *pctx;
|
|
|
|
|
|
|
|
racf = ngx_rtmp_conf_get_module_app_conf(cf, ngx_rtmp_record_module);
|
|
|
|
|
|
|
|
ctx = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_conf_ctx_t));
|
|
|
|
if (ctx == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
pctx = cf->ctx;
|
|
|
|
|
|
|
|
ctx->main_conf = pctx->main_conf;
|
|
|
|
ctx->srv_conf = pctx->srv_conf;
|
|
|
|
|
|
|
|
ctx->app_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_rtmp_max_module);
|
|
|
|
if (ctx->app_conf == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; ngx_modules[i]; i++) {
|
|
|
|
if (ngx_modules[i]->type != NGX_RTMP_MODULE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
module = ngx_modules[i]->ctx;
|
|
|
|
|
|
|
|
if (module->create_app_conf) {
|
|
|
|
ctx->app_conf[ngx_modules[i]->ctx_index] =
|
|
|
|
module->create_app_conf(cf);
|
|
|
|
if (ctx->app_conf[ngx_modules[i]->ctx_index] == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prc = ngx_array_push(&racf->nodes);
|
|
|
|
if (prc == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
rracf = ctx->app_conf[ngx_rtmp_record_module.ctx_index];
|
|
|
|
|
|
|
|
rc = &rracf->def;
|
|
|
|
|
|
|
|
value = cf->args->elts;
|
|
|
|
rc->id = value[1];
|
|
|
|
|
|
|
|
*prc = rc;
|
|
|
|
|
|
|
|
save = *cf;
|
|
|
|
cf->ctx = ctx;
|
|
|
|
cf->cmd_type = NGX_RTMP_REC_CONF;
|
|
|
|
|
|
|
|
rv = ngx_conf_parse(cf, NULL);
|
|
|
|
|
|
|
|
rvm = ngx_rtmp_record_merge_app_conf(cf, racf, rracf);
|
|
|
|
if (rvm != NGX_CONF_OK) {
|
|
|
|
return rvm;
|
|
|
|
}
|
|
|
|
|
|
|
|
*cf= save;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-23 15:57:25 +01:00
|
|
|
static ngx_int_t
|
|
|
|
ngx_rtmp_record_postconfiguration(ngx_conf_t *cf)
|
|
|
|
{
|
|
|
|
ngx_rtmp_core_main_conf_t *cmcf;
|
|
|
|
ngx_rtmp_handler_pt *h;
|
|
|
|
|
|
|
|
cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module);
|
|
|
|
|
|
|
|
h = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_AUDIO]);
|
|
|
|
*h = ngx_rtmp_record_av;
|
|
|
|
|
|
|
|
h = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_VIDEO]);
|
|
|
|
*h = ngx_rtmp_record_av;
|
|
|
|
|
2012-03-27 18:26:43 +02:00
|
|
|
next_publish = ngx_rtmp_publish;
|
|
|
|
ngx_rtmp_publish = ngx_rtmp_record_publish;
|
2012-03-23 15:57:25 +01:00
|
|
|
|
2012-03-27 18:26:43 +02:00
|
|
|
next_delete_stream = ngx_rtmp_delete_stream;
|
|
|
|
ngx_rtmp_delete_stream = ngx_rtmp_record_delete_stream;
|
2012-03-23 15:57:25 +01:00
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|