Merge branch 'master' into hlslight

This commit is contained in:
Roman Arutyunyan 2012-06-14 20:49:08 +04:00
commit f679b888c8
20 changed files with 1442 additions and 1115 deletions

27
README
View file

@ -11,6 +11,9 @@ NGINX-based RTMP server
* H264 support
* Online transcoding with FFmpeg
(experimental; Linux only)
* HTTP callbacks on publish/play/record
* Advanced buffering techniques
@ -77,6 +80,30 @@ rtmp {
#allow play all;
}
# Transcoding (ffmpeg needed)
application big {
live on;
# On every pusblished stream run this command (ffmpeg)
# with substitutions: $app/${app}, $name/${name} for application & stream name.
#
# This ffmpeg call receives stream from this application &
# reduces the resolution down to 32x32. The stream is the published to
# 'small' application (see below) under the same name.
#
# ffmpeg can do anything with the stream like video/audio
# transcoding, resizing, altering container/codec params etc
#
# Multiple exec lines can be specified.
exec /usr/bin/ffmpeg -re -i rtmp://localhost:1935/$app/$name -vcodec flv -acodec copy -s 32x32 -f flv rtmp://localhost:1935/small/${name};
}
application small {
live on;
# Video with rediced resolution comes here from ffmpeg
}
application mypush {
live on;

5
config
View file

@ -10,6 +10,8 @@ CORE_MODULES="$CORE_MODULES
ngx_rtmp_netcall_module \
ngx_rtmp_notify_module \
ngx_rtmp_relay_module \
ngx_rtmp_exec_module \
ngx_rtmp_codec_module \
"
@ -37,7 +39,8 @@ NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
$ngx_addon_dir/ngx_rtmp_stat_module.c \
$ngx_addon_dir/ngx_rtmp_relay_module.c \
$ngx_addon_dir/ngx_rtmp_bandwidth.c \
$ngx_addon_dir/ngx_rtmp_codecs.c \
$ngx_addon_dir/ngx_rtmp_exec_module.c \
$ngx_addon_dir/ngx_rtmp_codec_module.c \
"
CFLAGS="$CFLAGS -I$ngx_addon_dir"

View file

@ -384,8 +384,12 @@ ngx_rtmp_init_event_handlers(ngx_conf_t *cf, ngx_rtmp_core_main_conf_t *cmcf)
};
static size_t amf_events[] = {
NGX_RTMP_MSG_AMF_CMD,
NGX_RTMP_MSG_AMF_META,
NGX_RTMP_MSG_AMF_CMD
NGX_RTMP_MSG_AMF_SHARED,
NGX_RTMP_MSG_AMF3_CMD,
NGX_RTMP_MSG_AMF3_META,
NGX_RTMP_MSG_AMF3_SHARED
};
/* init standard protocol events */
@ -400,10 +404,6 @@ ngx_rtmp_init_event_handlers(ngx_conf_t *cf, ngx_rtmp_core_main_conf_t *cmcf)
*eh = ngx_rtmp_amf_message_handler;
}
/* init amf shared object events */
eh = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_AMF_SHARED]);
*eh = ngx_rtmp_amf_shared_object_handler;
/* init user protocol events */
eh = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_USER]);
*eh = ngx_rtmp_user_message_handler;

View file

@ -61,6 +61,8 @@ static ngx_int_t
ngx_rtmp_cmd_connect_init(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
size_t len;
static ngx_rtmp_connect_t v;
static ngx_rtmp_amf_elt_t in_cmd[] = {
@ -92,6 +94,10 @@ ngx_rtmp_cmd_connect_init(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
{ NGX_RTMP_AMF_STRING,
ngx_string("pageUrl"),
v.page_url, sizeof(v.page_url) },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("objectEncoding"),
&v.object_encoding, 0},
};
static ngx_rtmp_amf_elt_t in_elts[] = {
@ -112,6 +118,11 @@ ngx_rtmp_cmd_connect_init(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
return NGX_ERROR;
}
len = ngx_strlen(v.app);
if (len && v.app[len - 1] == '/') {
v.app[len - 1] = 0;
}
return ngx_rtmp_connect
? ngx_rtmp_connect(s, &v)
: NGX_OK;
@ -188,11 +199,13 @@ ngx_rtmp_cmd_connect(ngx_rtmp_session_t *s, ngx_rtmp_connect_t *v)
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
ngx_log_debug7(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
ngx_log_debug8(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"connect: app='%s' flashver='%s' swf_url='%s' "
"tc_url='%s' page_url='%s' acodecs=%uD vcodecs=%uD",
"tc_url='%s' page_url='%s' acodecs=%uD vcodecs=%uD "
"object_encoding=%ui",
v->app, v->flashver, v->swf_url, v->tc_url, v->page_url,
(uint32_t)v->acodecs, (uint32_t)v->vcodecs);
(uint32_t)v->acodecs, (uint32_t)v->vcodecs,
(ngx_int_t)v->object_encoding);
trans = v->trans;
@ -240,6 +253,8 @@ ngx_rtmp_cmd_connect(ngx_rtmp_session_t *s, ngx_rtmp_connect_t *v)
return NGX_ERROR;
}
object_encoding = v->object_encoding;
/* send all replies */
return ngx_rtmp_send_ack_size(s, cscf->ack_window) != NGX_OK
|| ngx_rtmp_send_bandwidth(s, cscf->ack_window,
@ -363,6 +378,18 @@ ngx_rtmp_cmd_delete_stream(ngx_rtmp_session_t *s, ngx_rtmp_delete_stream_t *v)
}
static void
ngx_rtmp_cmd_cutoff_args(u_char *s)
{
u_char *p;
p = (u_char *)ngx_strchr(s, '?');
if (p) {
*p = 0;
}
}
static ngx_int_t
ngx_rtmp_cmd_publish_init(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
@ -398,6 +425,8 @@ ngx_rtmp_cmd_publish_init(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
return NGX_ERROR;
}
ngx_rtmp_cmd_cutoff_args(v.name);
return ngx_rtmp_publish
? ngx_rtmp_publish(s, &v)
: NGX_OK;
@ -605,6 +634,8 @@ ngx_rtmp_cmd_play_init(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
return NGX_ERROR;
}
ngx_rtmp_cmd_cutoff_args(v.name);
return ngx_rtmp_play
? ngx_rtmp_play(s, &v)
: NGX_OK;

View file

@ -13,19 +13,23 @@
#include "ngx_rtmp.h"
/* Basic RTMP call support */
#define NGX_RTMP_MAX_APP 128
#define NGX_RTMP_MAX_NAME 256
#define NGX_RTMP_MAX_URL 256
/* TODO: improve string sizes */
/* Basic RTMP call support */
typedef struct {
double trans;
u_char app[128];
u_char app[NGX_RTMP_MAX_APP];
u_char flashver[32];
u_char swf_url[256];
u_char tc_url[256];
u_char swf_url[NGX_RTMP_MAX_URL];
u_char tc_url[NGX_RTMP_MAX_URL];
double acodecs;
double vcodecs;
u_char page_url[256];
u_char page_url[NGX_RTMP_MAX_URL];
double object_encoding;
} ngx_rtmp_connect_t;
@ -41,14 +45,14 @@ typedef struct {
typedef struct {
u_char name[256];
u_char name[NGX_RTMP_MAX_NAME];
u_char type[16];
int silent;
} ngx_rtmp_publish_t;
typedef struct {
u_char name[256];
u_char name[NGX_RTMP_MAX_NAME];
} ngx_rtmp_fcpublish_t;
@ -58,7 +62,7 @@ typedef ngx_rtmp_fcpublish_t ngx_rtmp_fcunsubscribe_t;
typedef struct {
u_char name[256];
u_char name[NGX_RTMP_MAX_NAME];
double start;
double duration;
int reset;

422
ngx_rtmp_codec_module.c Normal file
View file

@ -0,0 +1,422 @@
/*
* Copyright (c) 2012 Roman Arutyunyan
*/
#include "ngx_rtmp_codec_module.h"
#include "ngx_rtmp_live_module.h"
#include "ngx_rtmp_cmd_module.h"
static ngx_int_t ngx_rtmp_codec_postconfiguration(ngx_conf_t *cf);
/* Global header version is used to identify
* incoming AAC/AVC header */
static ngx_uint_t header_version;
static ngx_rtmp_module_t ngx_rtmp_codec_module_ctx = {
NULL, /* preconfiguration */
ngx_rtmp_codec_postconfiguration, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create app configuration */
NULL /* merge app configuration */
};
ngx_module_t ngx_rtmp_codec_module = {
NGX_MODULE_V1,
&ngx_rtmp_codec_module_ctx, /* module context */
NULL, /* 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
};
static const char *
audio_codecs[] = {
"",
"ADPCM",
"MP3",
"LinearLE",
"Nellymoser16",
"Nellymoser8",
"Nellymoser",
"G711A",
"G711U",
"",
"AAC",
"Speex",
"",
"",
"MP3-8K",
"DeviceSpecific",
"Uncompressed"
};
static const char *
video_codecs[] = {
"",
"Jpeg",
"Sorenson-H263",
"ScreenVideo",
"On2-VP6",
"On2-VP6-Alpha",
"ScreenVideo2",
"H264",
};
u_char *
ngx_rtmp_get_audio_codec_name(ngx_uint_t id)
{
return (u_char *)(id < sizeof(audio_codecs) / sizeof(audio_codecs[0])
? audio_codecs[id]
: "");
}
u_char *
ngx_rtmp_get_video_codec_name(ngx_uint_t id)
{
return (u_char *)(id < sizeof(video_codecs) / sizeof(video_codecs[0])
? video_codecs[id]
: "");
}
static ngx_int_t
ngx_rtmp_codec_disconnect(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
ngx_rtmp_codec_ctx_t *ctx;
ngx_rtmp_core_srv_conf_t *cscf;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module);
if (ctx == NULL) {
return NGX_OK;
}
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
if (ctx->avc_header) {
ngx_rtmp_free_shared_chain(cscf, ctx->avc_header);
ctx->avc_header = NULL;
}
if (ctx->aac_header) {
ngx_rtmp_free_shared_chain(cscf, ctx->aac_header);
ctx->aac_header = NULL;
}
if (ctx->avc_pheader) {
ngx_rtmp_free_shared_chain(cscf, ctx->avc_pheader);
ctx->avc_pheader = NULL;
}
if (ctx->aac_pheader) {
ngx_rtmp_free_shared_chain(cscf, ctx->aac_pheader);
ctx->aac_pheader = NULL;
}
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_codec_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
ngx_rtmp_core_srv_conf_t *cscf;
ngx_rtmp_codec_ctx_t *ctx;
ngx_chain_t **header, **pheader;
uint8_t fmt;
ngx_rtmp_header_t ch, lh;
ngx_uint_t *version;
if (h->type != NGX_RTMP_MSG_AUDIO && h->type != NGX_RTMP_MSG_VIDEO) {
return NGX_OK;
}
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module);
if (ctx == NULL) {
ctx = ngx_pcalloc(s->connection->pool, sizeof(ngx_rtmp_codec_ctx_t));
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_codec_module);
}
/* save codec */
if (in->buf->last - in->buf->pos < 1) {
return NGX_OK;
}
fmt = in->buf->pos[0];
if (h->type == NGX_RTMP_MSG_AUDIO) {
ctx->audio_codec_id = (fmt & 0xf0) >> 4;
} else {
ctx->video_codec_id = (fmt & 0x0f);
}
/* save AVC/AAC header */
if (in->buf->last - in->buf->pos < 2) {
return NGX_OK;
}
/* no conf */
if (in->buf->pos[1]) {
return NGX_OK;
}
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
header = NULL;
if (h->type == NGX_RTMP_MSG_AUDIO) {
if (ctx->audio_codec_id == NGX_RTMP_AUDIO_AAC) {
header = &ctx->aac_header;
pheader = &ctx->aac_pheader;
version = &ctx->aac_version;
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"codec: AAC header arrived");
}
} else {
if (ctx->video_codec_id == NGX_RTMP_VIDEO_H264) {
header = &ctx->avc_header;
pheader = &ctx->avc_pheader;
version = &ctx->avc_version;
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"codec: AVC/H264 header arrived");
}
}
if (header == NULL) {
return NGX_OK;
}
if (*header) {
ngx_rtmp_free_shared_chain(cscf, *header);
}
if (*pheader) {
ngx_rtmp_free_shared_chain(cscf, *pheader);
}
/* equal headers; timeout diff is zero */
ngx_memzero(&ch, sizeof(ch));
ch.msid = NGX_RTMP_LIVE_MSID;
ch.type = h->type;
ch.csid = (h->type == NGX_RTMP_MSG_VIDEO
? NGX_RTMP_LIVE_CSID_VIDEO
: NGX_RTMP_LIVE_CSID_AUDIO);
lh = ch;
*header = ngx_rtmp_append_shared_bufs(cscf, NULL, in);
*pheader = ngx_rtmp_append_shared_bufs(cscf, NULL, in);
ngx_rtmp_prepare_message(s, &ch, &lh, *pheader);
/* don't want zero as version value */
do {
*version = ++header_version;
} while (*version == 0);
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_codec_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
ngx_rtmp_codec_ctx_t *ctx;
ngx_uint_t skip;
static struct {
double width;
double height;
double duration;
double frame_rate;
double video_data_rate;
double video_codec_id_n;
u_char video_codec_id_s[32];
double audio_data_rate;
double audio_codec_id_n;
u_char audio_codec_id_s[32];
u_char profile[32];
u_char level[32];
} v;
static ngx_rtmp_amf_elt_t in_video_codec_id[] = {
{ NGX_RTMP_AMF_NUMBER,
ngx_null_string,
&v.video_codec_id_n, 0 },
{ NGX_RTMP_AMF_STRING,
ngx_null_string,
&v.video_codec_id_s, sizeof(v.video_codec_id_s) },
};
static ngx_rtmp_amf_elt_t in_audio_codec_id[] = {
{ NGX_RTMP_AMF_NUMBER,
ngx_null_string,
&v.audio_codec_id_n, 0 },
{ NGX_RTMP_AMF_STRING,
ngx_null_string,
&v.audio_codec_id_s, sizeof(v.audio_codec_id_s) },
};
static ngx_rtmp_amf_elt_t in_inf[] = {
{ NGX_RTMP_AMF_NUMBER,
ngx_string("width"),
&v.width, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("height"),
&v.height, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("duration"),
&v.duration, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("framerate"),
&v.frame_rate, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("fps"),
&v.frame_rate, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("videodatarate"),
&v.video_data_rate, 0 },
{ NGX_RTMP_AMF_VARIANT,
ngx_string("videocodecid"),
in_video_codec_id, sizeof(in_video_codec_id) },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("audiodatarate"),
&v.audio_data_rate, 0 },
{ NGX_RTMP_AMF_VARIANT,
ngx_string("audiocodecid"),
in_audio_codec_id, sizeof(in_audio_codec_id) },
{ NGX_RTMP_AMF_STRING,
ngx_string("profile"),
&v.profile, sizeof(v.profile) },
{ NGX_RTMP_AMF_STRING,
ngx_string("level"),
&v.level, sizeof(v.level) },
};
static ngx_rtmp_amf_elt_t in_elts[] = {
{ NGX_RTMP_AMF_STRING,
ngx_null_string,
NULL, 0 },
{ NGX_RTMP_AMF_OBJECT,
ngx_null_string,
in_inf, sizeof(in_inf) },
};
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module);
if (ctx == NULL) {
ctx = ngx_pcalloc(s->connection->pool, sizeof(ngx_rtmp_codec_ctx_t));
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_codec_module);
}
ngx_memzero(&v, sizeof(v));
/* use -1 as a sign of unchanged data;
* 0 is a valid value for uncompressed audio */
v.audio_codec_id_n = -1;
/* FFmpeg sends a string in front of actal metadata; ignore it */
skip = !(in->buf->last > in->buf->pos
&& *in->buf->pos == NGX_RTMP_AMF_STRING);
if (ngx_rtmp_receive_amf(s, in, in_elts + skip,
sizeof(in_elts) / sizeof(in_elts[0]) - skip))
{
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
"codec: error parsing data frame");
return NGX_OK;
}
ctx->width = v.width;
ctx->height = v.height;
ctx->duration = v.duration;
ctx->frame_rate = v.frame_rate;
ctx->video_data_rate = v.video_data_rate;
ctx->video_codec_id = v.video_codec_id_n;
ctx->audio_data_rate = v.audio_data_rate;
ctx->audio_codec_id = (v.audio_codec_id_n == -1
? 0 : v.audio_codec_id_n == 0
? NGX_RTMP_AUDIO_UNCOMPRESSED : v.audio_codec_id_n);
ngx_memcpy(ctx->profile, v.profile, sizeof(v.profile));
ngx_memcpy(ctx->level, v.level, sizeof(v.level));
ngx_log_debug8(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"codec: data frame: "
"width=%ui height=%ui duration=%ui frame_rate=%ui "
"video=%s (%ui) audio=%s (%ui)",
ctx->width, ctx->height, ctx->duration, ctx->frame_rate,
ngx_rtmp_get_video_codec_name(ctx->video_codec_id),
ctx->video_codec_id,
ngx_rtmp_get_audio_codec_name(ctx->audio_codec_id),
ctx->audio_codec_id);
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_codec_postconfiguration(ngx_conf_t *cf)
{
ngx_rtmp_core_main_conf_t *cmcf;
ngx_rtmp_handler_pt *h;
ngx_rtmp_amf_handler_t *ch;
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_codec_av;
h = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_VIDEO]);
*h = ngx_rtmp_codec_av;
h = ngx_array_push(&cmcf->events[NGX_RTMP_DISCONNECT]);
*h = ngx_rtmp_codec_disconnect;
/* register metadata handler */
ch = ngx_array_push(&cmcf->amf);
if (ch == NULL) {
return NGX_ERROR;
}
ngx_str_set(&ch->name, "@setDataFrame");
ch->handler = ngx_rtmp_codec_meta_data;
ch = ngx_array_push(&cmcf->amf);
if (ch == NULL) {
return NGX_ERROR;
}
ngx_str_set(&ch->name, "onMetaData");
ch->handler = ngx_rtmp_codec_meta_data;
return NGX_OK;
}

76
ngx_rtmp_codec_module.h Normal file
View file

@ -0,0 +1,76 @@
/*
* Copyright (c) 2012 Roman Arutyunyan
*/
#ifndef _NGX_RTMP_CODEC_H_INCLUDED_
#define _NGX_RTMP_CODEC_H_INCLUDED_
#include "ngx_rtmp.h"
/* Audio codecs */
enum {
/* Uncompressed codec id is actually 0,
* but we use another value for consistency */
NGX_RTMP_AUDIO_UNCOMPRESSED = 16,
NGX_RTMP_AUDIO_ADPCM = 1,
NGX_RTMP_AUDIO_MP3 = 2,
NGX_RTMP_AUDIO_LINEAR_LE = 3,
NGX_RTMP_AUDIO_NELLY16 = 4,
NGX_RTMP_AUDIO_NELLY8 = 5,
NGX_RTMP_AUDIO_NELLY = 6,
NGX_RTMP_AUDIO_G711A = 7,
NGX_RTMP_AUDIO_G711U = 8,
NGX_RTMP_AUDIO_AAC = 10,
NGX_RTMP_AUDIO_SPEEX = 11,
NGX_RTMP_AUDIO_MP3_8 = 14,
NGX_RTMP_AUDIO_DEVSPEC = 15,
};
/* Video codecs */
enum {
NGX_RTMP_VIDEO_JPEG = 1,
NGX_RTMP_VIDEO_SORENSON_H263 = 2,
NGX_RTMP_VIDEO_SCREEN = 3,
NGX_RTMP_VIDEO_ON2_VP6 = 4,
NGX_RTMP_VIDEO_ON2_VP6_ALPHA = 5,
NGX_RTMP_VIDEO_SCREEN2 = 6,
NGX_RTMP_VIDEO_H264 = 7
};
u_char * ngx_rtmp_get_audio_codec_name(ngx_uint_t id);
u_char * ngx_rtmp_get_video_codec_name(ngx_uint_t id);
typedef struct {
ngx_uint_t width;
ngx_uint_t height;
ngx_uint_t duration;
ngx_uint_t frame_rate;
ngx_uint_t video_data_rate;
ngx_uint_t video_codec_id;
ngx_uint_t audio_data_rate;
ngx_uint_t audio_codec_id;
u_char profile[32];
u_char level[32];
ngx_uint_t avc_version;
ngx_uint_t aac_version;
ngx_chain_t *avc_header;
ngx_chain_t *aac_header;
/* prepared headers (for live streaming) */
ngx_chain_t *avc_pheader;
ngx_chain_t *aac_pheader;
} ngx_rtmp_codec_ctx_t;
extern ngx_module_t ngx_rtmp_codec_module;
#endif /* _NGX_RTMP_LIVE_H_INCLUDED_ */

View file

@ -1,57 +0,0 @@
/*
* Copyright (c) 2012 Roman Arutyunyan
*/
#include "ngx_rtmp_codecs.h"
const char * audio_codecs[] = {
"",
"ADPCM",
"MP3",
"LinearLE",
"Nellymoser16",
"Nellymoser8",
"Nellymoser",
"G711A",
"G711U",
"",
"AAC",
"Speex",
"",
"",
"MP3-8K",
"DeviceSpecific",
"Uncompressed"
};
const char * video_codecs[] = {
"",
"Jpeg",
"Sorenson-H263",
"ScreenVideo",
"On2-VP6",
"On2-VP6-Alpha",
"ScreenVideo2",
"H264",
};
u_char *
ngx_rtmp_get_audio_codec_name(ngx_uint_t id)
{
return (u_char *)(id < sizeof(audio_codecs) / sizeof(audio_codecs[0])
? audio_codecs[id]
: "");
}
u_char *
ngx_rtmp_get_video_codec_name(ngx_uint_t id)
{
return (u_char *)(id < sizeof(video_codecs) / sizeof(video_codecs[0])
? video_codecs[id]
: "");
}

View file

@ -1,42 +0,0 @@
/*
* Copyright (c) 2012 Roman Arutyunyan
*/
#ifndef _NGX_RTMP_CODECS_H_INCLUDED_
#define _NGX_RTMP_CODECS_H_INCLUDED_
#include <ngx_core.h>
/* Audio codecs */
enum {
/* Uncompressed codec id is actually 0,
* but we use another value for consistency */
NGX_RTMP_AUDIO_UNCOMPRESSED = 12,
NGX_RTMP_AUDIO_ADPCM = 1,
NGX_RTMP_AUDIO_MP3 = 2,
NGX_RTMP_AUDIO_NELLY8 = 5,
NGX_RTMP_AUDIO_NELLY = 6,
NGX_RTMP_AUDIO_HE_ACC = 10,
NGX_RTMP_AUDIO_SPEEX = 11
};
/* Video codecs */
enum {
NGX_RTMP_VIDEO_SORENSON_H263 = 2,
NGX_RTMP_VIDEO_SCREEN = 3,
NGX_RTMP_VIDEO_ON2_VP6 = 4,
NGX_RTMP_VIDEO_ON2_VP6_ALPHA = 5,
NGX_RTMP_VIDEO_H264 = 7
};
u_char * ngx_rtmp_get_audio_codec_name(ngx_uint_t id);
u_char * ngx_rtmp_get_video_codec_name(ngx_uint_t id);
#endif /* _NGX_RTMP_CODECS_H_INCLUDED_ */

View file

@ -250,7 +250,7 @@ ngx_rtmp_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_value(conf->publish_time_fix, prev->publish_time_fix, 1);
if (prev->pool == NULL) {
prev->pool = ngx_create_pool(8192, cf->log);
prev->pool = ngx_create_pool(4096, &cf->cycle->new_log);
if (prev->pool == NULL) {
return NGX_CONF_ERROR;
}

541
ngx_rtmp_exec_module.c Normal file
View file

@ -0,0 +1,541 @@
/*
* Copyright (c) 2012 Roman Arutyunyan
*/
#include "ngx_rtmp_cmd_module.h"
#include <malloc.h>
#ifdef NGX_LINUX
#include <unistd.h>
#endif
static ngx_rtmp_publish_pt next_publish;
static ngx_rtmp_delete_stream_pt next_delete_stream;
static ngx_int_t ngx_rtmp_exec_postconfiguration(ngx_conf_t *cf);
static void * ngx_rtmp_exec_create_app_conf(ngx_conf_t *cf);
static char * ngx_rtmp_exec_merge_app_conf(ngx_conf_t *cf,
void *parent, void *child);
static char * ngx_rtmp_exec_exec(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
#define NGX_RTMP_EXEC_RESPAWN 0x01
#define NGX_RTMP_EXEC_KILL 0x02
typedef struct {
ngx_str_t cmd;
ngx_array_t args; /* ngx_str_t */
} ngx_rtmp_exec_conf_t;
typedef struct {
ngx_array_t execs; /* ngx_rtmp_exec_conf_t */
ngx_msec_t respawn_timeout;
ngx_flag_t respawn;
} ngx_rtmp_exec_app_conf_t;
typedef struct {
ngx_rtmp_session_t *session;
size_t index;
unsigned active:1;
int pid;
int pipefd;
ngx_connection_t dummy_conn; /*needed by ngx_xxx_event*/
ngx_event_t read_evt, write_evt;
ngx_event_t respawn_evt;
} ngx_rtmp_exec_t;
typedef struct {
u_char name[NGX_RTMP_MAX_NAME];
ngx_rtmp_exec_t *execs;
} ngx_rtmp_exec_ctx_t;
static ngx_int_t ngx_rtmp_exec_kill(ngx_rtmp_session_t *s, ngx_rtmp_exec_t *e,
ngx_int_t term);
static ngx_int_t ngx_rtmp_exec_run(ngx_rtmp_session_t *s, size_t n);
static ngx_command_t ngx_rtmp_exec_commands[] = {
{ ngx_string("exec"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_1MORE,
ngx_rtmp_exec_exec,
NGX_RTMP_APP_CONF_OFFSET,
0,
NULL },
{ ngx_string("respawn"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
NGX_RTMP_APP_CONF_OFFSET,
offsetof(ngx_rtmp_exec_app_conf_t, respawn),
NULL },
{ ngx_string("respawn_timeout"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_RTMP_APP_CONF_OFFSET,
offsetof(ngx_rtmp_exec_app_conf_t, respawn_timeout),
NULL },
ngx_null_command
};
static ngx_rtmp_module_t ngx_rtmp_exec_module_ctx = {
NULL, /* preconfiguration */
ngx_rtmp_exec_postconfiguration, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_rtmp_exec_create_app_conf, /* create app configuration */
ngx_rtmp_exec_merge_app_conf /* merge app configuration */
};
ngx_module_t ngx_rtmp_exec_module = {
NGX_MODULE_V1,
&ngx_rtmp_exec_module_ctx, /* module context */
ngx_rtmp_exec_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
};
static void *
ngx_rtmp_exec_create_app_conf(ngx_conf_t *cf)
{
ngx_rtmp_exec_app_conf_t *eacf;
eacf = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_exec_app_conf_t));
if (eacf == NULL) {
return NULL;
}
eacf->respawn = NGX_CONF_UNSET;
eacf->respawn_timeout = NGX_CONF_UNSET;
if (ngx_array_init(&eacf->execs, cf->pool, 1,
sizeof(ngx_rtmp_exec_conf_t)) != NGX_OK)
{
return NULL;
}
return eacf;
}
static char *
ngx_rtmp_exec_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_rtmp_exec_app_conf_t *prev = parent;
ngx_rtmp_exec_app_conf_t *conf = child;
size_t n;
ngx_rtmp_exec_conf_t *ec, *pec;
ngx_conf_merge_value(conf->respawn, prev->respawn, 1);
ngx_conf_merge_msec_value(conf->respawn_timeout, prev->respawn_timeout,
5000);
if (prev->execs.nelts) {
ec = ngx_array_push_n(&conf->execs, prev->execs.nelts);
if (ec == NULL) {
return NGX_CONF_ERROR;
}
pec = prev->execs.elts;
for (n = 0; n < prev->execs.nelts; ++n, ++ec, ++pec) {
*ec = *pec;
}
}
return NGX_CONF_OK;
}
static void
ngx_rtmp_exec_respawn(ngx_event_t *ev)
{
ngx_rtmp_exec_t *e;
e = ev->data;
ngx_rtmp_exec_run(e->session, e->index);
}
static void
ngx_rtmp_exec_child_dead(ngx_event_t *ev)
{
ngx_connection_t *dummy_conn;
ngx_rtmp_exec_t *e;
ngx_rtmp_session_t *s;
ngx_rtmp_exec_app_conf_t *eacf;
dummy_conn = ev->data;
e = dummy_conn->data;
s = e->session;
eacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_exec_module);
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"exec: child %ui exited; %s",
(ngx_int_t)e->pid,
eacf->respawn ? "respawning" : "ignoring");
ngx_rtmp_exec_kill(s, e, 0);
if (!eacf->respawn) {
return;
}
if (eacf->respawn_timeout == 0) {
ngx_rtmp_exec_run(s, e->index);
return;
}
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"exec: shedule respawn %Mmsec", eacf->respawn_timeout);
e->respawn_evt.data = e;
e->respawn_evt.log = s->connection->log;
e->respawn_evt.handler = ngx_rtmp_exec_respawn;
ngx_add_timer(&e->respawn_evt, eacf->respawn_timeout);
}
static ngx_int_t
ngx_rtmp_exec_kill(ngx_rtmp_session_t *s, ngx_rtmp_exec_t *e, ngx_int_t term)
{
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"exec: terminating child %ui",
(ngx_int_t)e->pid);
if (e->respawn_evt.timer_set) {
ngx_del_timer(&e->respawn_evt);
}
ngx_del_event(&e->read_evt, NGX_READ_EVENT, 0);
e->active = 0;
close(e->pipefd);
if (!term) {
return NGX_OK;
}
if (kill(e->pid, SIGKILL) == -1) {
ngx_log_error(NGX_LOG_INFO, s->connection->log, ngx_errno,
"exec: kill failed pid=%i", (ngx_int_t)e->pid);
} else {
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"exec: killed pid=%i", (ngx_int_t)e->pid);
}
return NGX_OK;
}
static void
ngx_rtmp_exec_append(ngx_str_t *result, u_char *data, size_t len)
{
if (len == 0) {
len = ngx_strlen(data);
}
/* use malloc in child */
if (result->len == 0) {
result->data = malloc(len + 1);
result->len = len;
ngx_memcpy(result->data, data, len);
result->data[len] = 0;
return;
}
result->data = realloc(result->data, result->len + len + 1);
ngx_memcpy(result->data + result->len, data, len);
result->len += len;
result->data[result->len] = 0;
}
static char *
ngx_rtmp_exec_prepare_arg(ngx_rtmp_session_t *s, ngx_str_t *arg)
{
ngx_rtmp_core_app_conf_t *cacf;
ngx_rtmp_exec_ctx_t *ctx;
u_char *p, *pp;
ngx_str_t result;
cacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_core_module);
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_exec_module);
/* substitute $app/${app} & $name/${name} */
ngx_str_set(&result, "");
pp = arg->data;
for ( ;; ) {
p = (u_char *)ngx_strchr(pp, '$');
ngx_rtmp_exec_append(&result, pp, p ? p - pp : 0);
if (p == NULL) {
return (char *)result.data;
}
pp = p + 1;
if (p != arg->data && p[-1] == '\\') {
goto dollar;
}
if (!ngx_strncmp(p + 1, "app", sizeof("app") - 1)
|| !ngx_strncmp(p + 1, "{app}", sizeof("{app}") - 1))
{
ngx_rtmp_exec_append(&result, cacf->name.data, cacf->name.len);
pp += (p[1] == '{' ? sizeof("{app}") - 1 : sizeof("app") - 1);
continue;
}
if (!ngx_strncmp(p + 1, "name", sizeof("name") - 1)
|| !ngx_strncmp(p + 1, "{name}", sizeof("{name}") - 1))
{
ngx_rtmp_exec_append(&result, ctx->name, 0);
pp += (p[1] == '{' ? sizeof("{name}") - 1 : sizeof("name") - 1);
continue;
}
dollar:
ngx_rtmp_exec_append(&result, (u_char *)"$", 1);
}
}
static ngx_int_t
ngx_rtmp_exec_run(ngx_rtmp_session_t *s, size_t n)
{
#ifdef NGX_LINUX
ngx_rtmp_exec_app_conf_t *eacf;
ngx_rtmp_exec_ctx_t *ctx;
int pid;
int pipefd[2];
int ret;
ngx_rtmp_exec_conf_t *ec;
ngx_rtmp_exec_t *e;
ngx_str_t *arg;
char **args;
eacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_exec_module);
ec = (ngx_rtmp_exec_conf_t *)eacf->execs.elts + n;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_exec_module);
e = ctx->execs + n;
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"exec: starting child '%V'",
&ec->cmd);
if (pipe(pipefd) == -1) {
ngx_log_error(NGX_LOG_INFO, s->connection->log, ngx_errno,
"exec: pipe failed");
return NGX_ERROR;
}
/* make pipe write end survive through exec */
ret = fcntl(pipefd[1], F_GETFD);
if (ret != -1) {
ret &= ~FD_CLOEXEC;
ret = fcntl(pipefd[1], F_SETFD, ret);
}
if (ret == -1) {
close(pipefd[0]);
close(pipefd[1]);
ngx_log_error(NGX_LOG_INFO, s->connection->log, ngx_errno,
"exec: fcntl failed");
return NGX_ERROR;
}
pid = fork();
switch (pid) {
case -1:
close(pipefd[0]);
close(pipefd[1]);
ngx_log_error(NGX_LOG_INFO, s->connection->log, ngx_errno,
"exec: fork failed");
return NGX_ERROR;
case 0:
/* child */
args = malloc((ec->args.nelts + 2) * sizeof(char *));
if (args == NULL) {
exit(1);
}
arg = ec->args.elts;
args[0] = (char *)ec->cmd.data;
for (n = 0; n < ec->args.nelts; ++n, ++arg) {
args[n + 1] = ngx_rtmp_exec_prepare_arg(s, arg);
}
args[n + 1] = NULL;
if (execv((char *)ec->cmd.data, args) == -1) {
exit(1);
}
break;
default:
/* parent */
close(pipefd[1]);
e->session = s;
e->index = n;
e->active = 1;
e->pid = pid;
e->pipefd = pipefd[0];
e->dummy_conn.fd = e->pipefd;
e->dummy_conn.data = e;
e->dummy_conn.read = &e->read_evt;
e->dummy_conn.write = &e->write_evt;
e->read_evt.data = &e->dummy_conn;
e->write_evt.data = &e->dummy_conn;
e->read_evt.log = s->connection->log;
e->read_evt.handler = ngx_rtmp_exec_child_dead;
if (ngx_add_event(&e->read_evt, NGX_READ_EVENT, 0) != NGX_OK) {
ngx_log_error(NGX_LOG_INFO, s->connection->log, ngx_errno,
"exec: failed to add child control event");
}
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"exec: child '%V' started pid=%ui",
&ec->cmd, (ngx_uint_t)pid);
break;
}
#endif /* NGX_LINUX */
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_exec_delete_stream(ngx_rtmp_session_t *s, ngx_rtmp_delete_stream_t *v)
{
ngx_rtmp_exec_app_conf_t *eacf;
ngx_rtmp_exec_ctx_t *ctx;
ngx_rtmp_exec_t *e;
size_t n;
eacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_exec_module);
if (eacf == NULL) {
goto next;
}
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_exec_module);
if (ctx == NULL || ctx->execs == NULL) {
goto next;
}
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"exec: delete %uz command(s)", eacf->execs.nelts);
e = ctx->execs;
for (n = 0; n < eacf->execs.nelts; ++n, ++e) {
if (e->active) {
ngx_rtmp_exec_kill(s, e, 1);
}
}
next:
return next_delete_stream(s, v);
}
static ngx_int_t
ngx_rtmp_exec_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
{
ngx_rtmp_exec_app_conf_t *eacf;
ngx_rtmp_exec_conf_t *ec;
ngx_rtmp_exec_ctx_t *ctx;
size_t n;
eacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_exec_module);
if (eacf == NULL || eacf->execs.nelts == 0) {
goto next;
}
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_exec_module);
if (ctx == NULL) {
ctx = ngx_pcalloc(s->connection->pool, sizeof(ngx_rtmp_exec_ctx_t));
if (ctx == NULL) {
return NGX_ERROR;
}
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_exec_module);
ctx->execs = ngx_pcalloc(s->connection->pool, eacf->execs.nelts
* sizeof(ngx_rtmp_exec_t));
}
ngx_memcpy(ctx->name, v->name, NGX_RTMP_MAX_NAME);
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"exec: run %uz command(s)", eacf->execs.nelts);
ec = eacf->execs.elts;
for (n = 0; n < eacf->execs.nelts; ++n, ++ec) {
ngx_rtmp_exec_run(s, n);
}
next:
return next_publish(s, v);
}
static char *
ngx_rtmp_exec_exec(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *value;
ngx_rtmp_exec_app_conf_t *eacf;
size_t n, nargs;
ngx_str_t *s;
ngx_rtmp_exec_conf_t *ec;
eacf = ngx_rtmp_conf_get_module_app_conf(cf, ngx_rtmp_exec_module);
value = cf->args->elts;
ec = ngx_array_push(&eacf->execs);
if (ec == NULL) {
return NGX_CONF_ERROR;
}
ec->cmd = value[1];
if (cf->args->nelts == 2) {
return NGX_CONF_OK;
}
nargs = cf->args->nelts - 2;
if (ngx_array_init(&ec->args, cf->pool, nargs,
sizeof(ngx_str_t)) != NGX_OK)
{
return NGX_CONF_ERROR;
}
s = ngx_array_push_n(&ec->args, nargs);
for (n = 2; n < cf->args->nelts; ++n, ++s) {
*s = value[n];
}
return NGX_CONF_OK;
}
static ngx_int_t
ngx_rtmp_exec_postconfiguration(ngx_conf_t *cf)
{
next_publish = ngx_rtmp_publish;
ngx_rtmp_publish = ngx_rtmp_exec_publish;
next_delete_stream = ngx_rtmp_delete_stream;
ngx_rtmp_delete_stream = ngx_rtmp_exec_delete_stream;
return NGX_OK;
}

View file

@ -41,7 +41,7 @@ ngx_rtmp_message_type(uint8_t type)
"?",
"amf3_meta",
"amf3_shared",
"amd3_cmd",
"amf3_cmd",
"amf_meta",
"amf_shared",
"amf_cmd",

View file

@ -5,7 +5,7 @@
#include "ngx_rtmp_live_module.h"
#include "ngx_rtmp_cmd_module.h"
#include "ngx_rtmp_codecs.h"
#include "ngx_rtmp_codec_module.h"
static ngx_rtmp_publish_pt next_publish;
@ -13,12 +13,6 @@ static ngx_rtmp_play_pt next_play;
static ngx_rtmp_delete_stream_pt next_delete_stream;
/* Chunk stream ids for output */
#define NGX_RTMP_LIVE_CSID_AUDIO 6
#define NGX_RTMP_LIVE_CSID_VIDEO 7
#define NGX_RTMP_LIVE_MSID 1
static ngx_int_t ngx_rtmp_live_postconfiguration(ngx_conf_t *cf);
static void * ngx_rtmp_live_create_app_conf(ngx_conf_t *cf);
static char * ngx_rtmp_live_merge_app_conf(ngx_conf_t *cf,
@ -268,10 +262,6 @@ ngx_rtmp_live_delete_stream(ngx_rtmp_session_t *s, ngx_rtmp_delete_stream_t *v)
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"live: delete empty stream '%s'", ctx->stream->name);
if (ctx->stream->avc_header) {
ngx_rtmp_free_shared_chain(cscf, ctx->stream->avc_header);
}
stream = ngx_rtmp_live_get_stream(s, ctx->stream->name, 0);
if (stream == NULL) {
return NGX_ERROR;
@ -292,14 +282,16 @@ ngx_rtmp_live_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
ngx_rtmp_live_ctx_t *ctx, *pctx;
ngx_chain_t *out, *out_abs;
ngx_rtmp_codec_ctx_t *codec_ctx;
ngx_chain_t *out, *peer_out, *header_out, *pheader_out;
ngx_rtmp_core_srv_conf_t *cscf;
ngx_rtmp_live_app_conf_t *lacf;
ngx_rtmp_session_t *ss;
ngx_rtmp_header_t ch, lh;
ngx_uint_t prio, peer_prio;
ngx_uint_t peers, dropped_peers;
uint8_t flv_fmt;
size_t header_offset;
ngx_uint_t header_version;
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
if (lacf == NULL) {
@ -358,24 +350,25 @@ ngx_rtmp_live_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
peers = 0;
dropped_peers = 0;
/* parse meta */
if (in->buf->last - in->buf->pos >= 1) {
flv_fmt = *in->buf->pos;
codec_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module);
header_out = NULL;
pheader_out = NULL;
header_offset = 0;
header_version = 0;
if (codec_ctx) {
if (h->type == NGX_RTMP_MSG_AUDIO) {
ctx->stream->meta.audio_codec_id = (flv_fmt & 0xf0) >> 4;
if (codec_ctx->aac_pheader) {
header_out = codec_ctx->aac_header;
pheader_out = codec_ctx->aac_pheader;
header_offset = offsetof(ngx_rtmp_live_ctx_t, aac_version);
header_version = codec_ctx->aac_version;
}
} else {
ctx->stream->meta.video_codec_id = (flv_fmt & 0x0f);
if (ctx->stream->meta.video_codec_id == 7
&& in->buf->last - in->buf->pos >= 2
&& in->buf->pos[1] == 0)
{
/* AVC/H264 sequence header;
* save it for future */
if (ctx->stream->avc_header) {
ngx_rtmp_free_shared_chain(cscf, ctx->stream->avc_header);
}
ctx->stream->avc_header = out;
ngx_rtmp_acquire_shared_chain(out);
if (codec_ctx->avc_pheader) {
header_out = codec_ctx->avc_header;
pheader_out = codec_ctx->avc_pheader;
header_offset = offsetof(ngx_rtmp_live_ctx_t, avc_version);
header_version = codec_ctx->avc_version;
}
}
}
@ -395,27 +388,35 @@ ngx_rtmp_live_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
"live: av: abs %s timestamp=%uD",
h->type == NGX_RTMP_MSG_VIDEO ? "video" : "audio",
ch.timestamp);
out_abs = ngx_rtmp_append_shared_bufs(cscf, NULL, in);
ngx_rtmp_prepare_message(s, &ch, NULL, out_abs);
/* send codec header as abs frame if any */
peer_out = ngx_rtmp_append_shared_bufs(cscf, NULL,
header_out ? header_out : in);
ngx_rtmp_prepare_message(s, &ch, NULL, peer_out);
pctx->msg_mask |= (1 << h->type);
ngx_rtmp_send_message(ss, out_abs, prio);
ngx_rtmp_free_shared_chain(cscf, out_abs);
/* send AVC/H264 header */
if (ctx->stream->avc_header) {
/*TODO: make it absolute & send instead of ythe above frame */
ngx_rtmp_send_message(ss, ctx->stream->avc_header, prio);
if (ngx_rtmp_send_message(ss, peer_out, prio) == NGX_OK
&& header_out)
{
*(ngx_uint_t *)((u_char *)pctx + header_offset)
= header_version;
}
ngx_rtmp_free_shared_chain(cscf, peer_out);
continue;
}
/* send AVC/H264 header if newer header has arrived */
if (pheader_out && *(ngx_uint_t *)((u_char *)pctx + header_offset)
!= header_version)
{
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0,
"live: sending codec header");
if (ngx_rtmp_send_message(ss, pheader_out, prio) == NGX_OK) {
*(ngx_uint_t *)((u_char *)pctx + header_offset)
= header_version;
}
}
/* push buffered data */
peer_prio = prio;
/*
if (lacf->buflen && h->timestamp >= pctx->next_push) {
peer_prio = 0;
pctx->next_push = h->timestamp + lacf->buflen;
}*/
if (ngx_rtmp_send_message(ss, out, peer_prio) != NGX_OK) {
++pctx->dropped;
++dropped_peers;
@ -478,192 +479,14 @@ next:
}
static ngx_int_t
ngx_rtmp_live_ext_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in, ngx_uint_t skip)
{
ngx_rtmp_live_app_conf_t *lacf;
ngx_rtmp_live_ctx_t *ctx;
ngx_rtmp_live_meta_t *meta;
static struct {
double width;
double height;
double duration;
double frame_rate;
double video_data_rate;
double video_codec_id_n;
u_char video_codec_id_s[32];
double audio_data_rate;
double audio_codec_id_n;
u_char audio_codec_id_s[32];
} v;
static ngx_rtmp_amf_elt_t in_video_codec_id[] = {
{ NGX_RTMP_AMF_NUMBER,
ngx_null_string,
&v.video_codec_id_n, 0 },
{ NGX_RTMP_AMF_STRING,
ngx_null_string,
&v.video_codec_id_s, sizeof(v.video_codec_id_s) },
};
static ngx_rtmp_amf_elt_t in_audio_codec_id[] = {
{ NGX_RTMP_AMF_NUMBER,
ngx_null_string,
&v.audio_codec_id_n, 0 },
{ NGX_RTMP_AMF_STRING,
ngx_null_string,
&v.audio_codec_id_s, sizeof(v.audio_codec_id_s) },
};
static ngx_rtmp_amf_elt_t in_inf[] = {
{ NGX_RTMP_AMF_NUMBER,
ngx_string("width"),
&v.width, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("height"),
&v.height, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("duration"),
&v.duration, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("framerate"),
&v.frame_rate, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("videodatarate"),
&v.video_data_rate, 0 },
{ NGX_RTMP_AMF_VARIANT,
ngx_string("videocodecid"),
in_video_codec_id, sizeof(in_video_codec_id) },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("audiodatarate"),
&v.audio_data_rate, 0 },
{ NGX_RTMP_AMF_VARIANT,
ngx_string("audiocodecid"),
in_audio_codec_id, sizeof(in_audio_codec_id) },
};
static ngx_rtmp_amf_elt_t in_elts[] = {
{ NGX_RTMP_AMF_STRING,
ngx_null_string,
NULL, 0 },
{ NGX_RTMP_AMF_OBJECT,
ngx_null_string,
in_inf, sizeof(in_inf) },
};
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
if (lacf == NULL || !lacf->live) {
return NGX_OK;
}
ngx_memzero(&v, sizeof(v));
/* use -1 as a sign of unchanged data;
* 0 is a valid value for uncompressed audio */
v.audio_codec_id_n = -1;
if (ngx_rtmp_receive_amf(s, in, in_elts + skip,
sizeof(in_elts) / sizeof(in_elts[0]) - skip))
{
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
"live: error parsing data frame");
return NGX_OK;
}
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module);
if (ctx == NULL) {
return NGX_OK;
}
if ((ctx->flags & NGX_RTMP_LIVE_PUBLISHING) == 0) {
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
"live: received data stream from non-publisher");
return NGX_OK;
}
meta = &ctx->stream->meta;
meta->width = v.width;
meta->height = v.height;
meta->duration = v.duration;
meta->frame_rate = v.frame_rate;
meta->video_data_rate = v.video_data_rate;
meta->video_codec_id = v.video_codec_id_n;
meta->audio_data_rate = v.audio_data_rate;
meta->audio_codec_id = (v.audio_codec_id_n == -1
? 0 : v.audio_codec_id_n == 0
? NGX_RTMP_AUDIO_UNCOMPRESSED : v.audio_codec_id_n);
ngx_log_debug8(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"live: data frame: "
"width=%ui height=%ui duration=%ui frame_rate=%ui "
"video=%s (%ui) audio=%s (%ui)",
meta->width, meta->height, meta->duration, meta->frame_rate,
ngx_rtmp_get_video_codec_name(meta->video_codec_id),
meta->video_codec_id,
ngx_rtmp_get_audio_codec_name(meta->audio_codec_id),
meta->audio_codec_id);
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_live_data_frame(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
return ngx_rtmp_live_ext_meta_data(s, h, in, 0);
}
static ngx_int_t
ngx_rtmp_live_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
return ngx_rtmp_live_ext_meta_data(s, h, in, 1);
}
static ngx_int_t
ngx_rtmp_live_postconfiguration(ngx_conf_t *cf)
{
ngx_rtmp_core_main_conf_t *cmcf;
ngx_rtmp_handler_pt *h;
ngx_rtmp_amf_handler_t *ch;
cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module);
/* register metadata handler */
ch = ngx_array_push(&cmcf->amf);
if (ch == NULL) {
return NGX_ERROR;
}
ngx_str_set(&ch->name, "@setDataFrame");
ch->handler = ngx_rtmp_live_data_frame;
ch = ngx_array_push(&cmcf->amf);
if (ch == NULL) {
return NGX_ERROR;
}
ngx_str_set(&ch->name, "onMetaData");
ch->handler = ngx_rtmp_live_meta_data;
/* register raw event handlers */
h = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_AUDIO]);
*h = ngx_rtmp_live_av;

View file

@ -1,650 +0,0 @@
/*
* Copyright (c) 2012 Roman Arutyunyan
*/
#include "ngx_rtmp_live_module.h"
#include "ngx_rtmp_cmd_module.h"
#include "ngx_rtmp_codecs.h"
static ngx_rtmp_publish_pt next_publish;
static ngx_rtmp_play_pt next_play;
static ngx_rtmp_delete_stream_pt next_delete_stream;
/* Chunk stream ids for output */
#define NGX_RTMP_LIVE_CSID_AUDIO 6
#define NGX_RTMP_LIVE_CSID_VIDEO 7
#define NGX_RTMP_LIVE_MSID 1
static ngx_int_t ngx_rtmp_live_postconfiguration(ngx_conf_t *cf);
static void * ngx_rtmp_live_create_app_conf(ngx_conf_t *cf);
static char * ngx_rtmp_live_merge_app_conf(ngx_conf_t *cf,
void *parent, void *child);
#define NGX_RTMP_LIVE_TIME_ABSOLUTE 0x01
#define NGX_RTMP_LIVE_TIME_RELATIVE 0x02
static ngx_command_t ngx_rtmp_live_commands[] = {
{ ngx_string("live"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
NGX_RTMP_APP_CONF_OFFSET,
offsetof(ngx_rtmp_live_app_conf_t, live),
NULL },
{ ngx_string("stream_buckets"),
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_live_app_conf_t, nbuckets),
NULL },
{ ngx_string("buffer"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_RTMP_APP_CONF_OFFSET,
offsetof(ngx_rtmp_live_app_conf_t, buflen),
NULL },
ngx_null_command
};
static ngx_rtmp_module_t ngx_rtmp_live_module_ctx = {
NULL, /* preconfiguration */
ngx_rtmp_live_postconfiguration, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_rtmp_live_create_app_conf, /* create app configuration */
ngx_rtmp_live_merge_app_conf /* merge app configuration */
};
ngx_module_t ngx_rtmp_live_module = {
NGX_MODULE_V1,
&ngx_rtmp_live_module_ctx, /* module context */
ngx_rtmp_live_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
};
static void *
ngx_rtmp_live_create_app_conf(ngx_conf_t *cf)
{
ngx_rtmp_live_app_conf_t *lacf;
lacf = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_live_app_conf_t));
if (lacf == NULL) {
return NULL;
}
lacf->live = NGX_CONF_UNSET;
lacf->nbuckets = NGX_CONF_UNSET;
lacf->buflen = NGX_CONF_UNSET;
return lacf;
}
static char *
ngx_rtmp_live_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_rtmp_live_app_conf_t *prev = parent;
ngx_rtmp_live_app_conf_t *conf = child;
ngx_conf_merge_value(conf->live, prev->live, 0);
ngx_conf_merge_value(conf->nbuckets, prev->nbuckets, 1024);
ngx_conf_merge_msec_value(conf->buflen, prev->buflen, 0);
conf->pool = ngx_create_pool(4096, &cf->cycle->new_log);
if (conf->pool == NULL) {
return NGX_CONF_ERROR;
}
conf->streams = ngx_pcalloc(cf->pool,
sizeof(ngx_rtmp_live_stream_t *) * conf->nbuckets);
return NGX_CONF_OK;
}
static ngx_rtmp_live_stream_t **
ngx_rtmp_live_get_stream(ngx_rtmp_session_t *s, u_char *name, int create)
{
ngx_rtmp_live_app_conf_t *lacf;
ngx_rtmp_live_stream_t **stream;
size_t len;
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
if (lacf == NULL) {
return NULL;
}
len = ngx_strlen(name);
stream = &lacf->streams[ngx_hash_key(name, len) % lacf->nbuckets];
for (; *stream; stream = &(*stream)->next) {
if (ngx_strcmp(name, (*stream)->name) == 0) {
return stream;
}
}
if (!create) {
return NULL;
}
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"live: create stream '%s'", name);
if (lacf->free_streams) {
*stream = lacf->free_streams;
lacf->free_streams = lacf->free_streams->next;
} else {
*stream = ngx_palloc(lacf->pool, sizeof(ngx_rtmp_live_stream_t));
}
ngx_memzero(*stream, sizeof(ngx_rtmp_live_stream_t));
ngx_memcpy((*stream)->name, name,
ngx_min(sizeof((*stream)->name) - 1, len));
(*stream)->epoch = ngx_current_msec;
return stream;
}
static void
ngx_rtmp_live_join(ngx_rtmp_session_t *s, u_char *name,
ngx_uint_t flags)
{
ngx_rtmp_live_ctx_t *ctx;
ngx_rtmp_live_stream_t **stream;
ngx_rtmp_live_app_conf_t *lacf;
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
if (lacf == NULL) {
return;
}
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module);
if (ctx == NULL) {
ctx = ngx_pcalloc(s->connection->pool, sizeof(ngx_rtmp_live_ctx_t));
ctx->session = s;
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_live_module);
}
if (ctx->stream) {
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log,
0, "live: already joined");
return;
}
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"live: join '%s'", name);
stream = ngx_rtmp_live_get_stream(s, name, 1);
if (stream == NULL) {
return;
}
if (flags & NGX_RTMP_LIVE_PUBLISHING) {
if ((*stream)->flags & NGX_RTMP_LIVE_PUBLISHING) {
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
"live: already publishing");
return;
}
(*stream)->flags |= NGX_RTMP_LIVE_PUBLISHING;
}
ctx->stream = *stream;
ctx->flags = flags;
ctx->next = (*stream)->ctx;
(*stream)->ctx = ctx;
if (lacf->buflen) {
s->out_buffer = 1;
}
}
static ngx_int_t
ngx_rtmp_live_delete_stream(ngx_rtmp_session_t *s, ngx_rtmp_delete_stream_t *v)
{
ngx_rtmp_live_ctx_t *ctx, **cctx;
ngx_rtmp_live_stream_t **stream;
ngx_rtmp_live_app_conf_t *lacf;
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
if (lacf == NULL) {
goto next;
}
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module);
if (ctx == NULL) {
goto next;
}
if (ctx->stream == NULL) {
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"live: not joined ");
goto next;
}
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"live: leave '%s'", ctx->stream->name);
if (ctx->stream->flags & NGX_RTMP_LIVE_PUBLISHING
&& ctx->flags & NGX_RTMP_LIVE_PUBLISHING)
{
ctx->stream->flags &= ~NGX_RTMP_LIVE_PUBLISHING;
}
for (cctx = &ctx->stream->ctx; *cctx; cctx = &(*cctx)->next) {
if (*cctx == ctx) {
*cctx = ctx->next;
break;
}
}
if (ctx->stream->ctx) {
ctx->stream = NULL;
goto next;
}
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"live: delete empty stream '%s'", ctx->stream->name);
stream = ngx_rtmp_live_get_stream(s, ctx->stream->name, 0);
if (stream == NULL) {
return NGX_ERROR;
}
*stream = (*stream)->next;
ctx->stream->next = lacf->free_streams;
lacf->free_streams = ctx->stream;
ctx->stream = NULL;
next:
return next_delete_stream(s, v);
}
static ngx_int_t
ngx_rtmp_live_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
ngx_rtmp_live_ctx_t *ctx, *pctx;
ngx_chain_t *out, *out_abs;
ngx_rtmp_core_srv_conf_t *cscf;
ngx_rtmp_live_app_conf_t *lacf;
ngx_rtmp_session_t *ss;
ngx_rtmp_header_t ch, lh;
ngx_uint_t prio, peer_prio;
ngx_uint_t peers, dropped_peers;
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
if (lacf == NULL) {
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"live: NULL application");
return NGX_ERROR;
}
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module);
if (!lacf->live
|| in == NULL || in->buf == NULL
|| ctx == NULL || ctx->stream == NULL
|| (h->type != NGX_RTMP_MSG_VIDEO
&& h->type != NGX_RTMP_MSG_AUDIO))
{
return NGX_OK;
}
if ((ctx->flags & NGX_RTMP_LIVE_PUBLISHING) == 0) {
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"live: received audio/video from non-publisher");
return NGX_OK;
}
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"live: av: %s timestamp=%uD",
h->type == NGX_RTMP_MSG_VIDEO ? "video" : "audio",
h->timestamp);
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
/* prepare output header */
ngx_memzero(&ch, sizeof(ch));
ngx_memzero(&lh, sizeof(lh));
ch.timestamp = h->timestamp;
ch.msid = NGX_RTMP_LIVE_MSID;
ch.type = h->type;
lh.msid = ch.msid;
if (h->type == NGX_RTMP_MSG_VIDEO) {
prio = ngx_rtmp_get_video_frame_type(in);
ch.csid = NGX_RTMP_LIVE_CSID_VIDEO;
lh.timestamp = ctx->last_video;
ctx->last_video = ch.timestamp;
} else {
/* audio priority is the same as video key frame's */
prio = NGX_RTMP_VIDEO_KEY_FRAME;
ch.csid = NGX_RTMP_LIVE_CSID_AUDIO;
lh.timestamp = ctx->last_audio;
ctx->last_audio = ch.timestamp;
}
lh.csid = ch.csid;
out = ngx_rtmp_append_shared_bufs(cscf, NULL, in);
ngx_rtmp_prepare_message(s, &ch, &lh, out);
peers = 0;
dropped_peers = 0;
/* broadcast to all subscribers */
for (pctx = ctx->stream->ctx; pctx; pctx = pctx->next) {
if (pctx == ctx) {
continue;
}
++peers;
ss = pctx->session;
/* send absolute frame */
if ((pctx->msg_mask & (1 << h->type)) == 0) {
ch.timestamp = ngx_current_msec - ss->epoch;
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0,
"live: av: abs %s timestamp=%uD",
h->type == NGX_RTMP_MSG_VIDEO ? "video" : "audio",
ch.timestamp);
out_abs = ngx_rtmp_append_shared_bufs(cscf, NULL, in);
ngx_rtmp_prepare_message(s, &ch, NULL, out_abs);
pctx->msg_mask |= (1 << h->type);
ngx_rtmp_send_message(ss, out_abs, prio);
ngx_rtmp_free_shared_chain(cscf, out_abs);
continue;
}
/* push buffered data */
peer_prio = prio;
/*
if (lacf->buflen && h->timestamp >= pctx->next_push) {
peer_prio = 0;
pctx->next_push = h->timestamp + lacf->buflen;
}*/
if (ngx_rtmp_send_message(ss, out, peer_prio) != NGX_OK) {
++pctx->dropped;
++dropped_peers;
}
}
ngx_rtmp_free_shared_chain(cscf, out);
ngx_rtmp_update_bandwidth(&ctx->stream->bw_in, h->mlen);
ngx_rtmp_update_bandwidth(&ctx->stream->bw_out,
h->mlen * (peers - dropped_peers));
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_live_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
{
ngx_rtmp_live_app_conf_t *lacf;
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
if (lacf == NULL || !lacf->live) {
goto next;
}
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"live: publish: name='%s' type='%s'",
v->name, v->type);
/* join stream as publisher */
ngx_rtmp_live_join(s, v->name, NGX_RTMP_LIVE_PUBLISHING);
next:
return next_publish(s, v);
}
static ngx_int_t
ngx_rtmp_live_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
{
ngx_rtmp_live_app_conf_t *lacf;
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
if (lacf == NULL || !lacf->live) {
goto next;
}
ngx_log_debug4(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"live: play: name='%s' start=%uD duration=%uD reset=%d",
v->name, (uint32_t)v->start,
(uint32_t)v->duration, (uint32_t)v->reset);
/* join stream as player */
ngx_rtmp_live_join(s, v->name, 0);
next:
return next_play(s, v);
}
static ngx_int_t
ngx_rtmp_live_ext_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in, ngx_uint_t skip)
{
ngx_rtmp_live_app_conf_t *lacf;
ngx_rtmp_live_ctx_t *ctx;
ngx_rtmp_live_meta_t *meta;
static struct {
double width;
double height;
double duration;
double frame_rate;
double video_data_rate;
double video_codec_id_n;
u_char video_codec_id_s[32];
double audio_data_rate;
double audio_codec_id_n;
u_char audio_codec_id_s[32];
} v;
static ngx_rtmp_amf_elt_t in_video_codec_id[] = {
{ NGX_RTMP_AMF_NUMBER,
ngx_null_string,
&v.video_codec_id_n, 0 },
{ NGX_RTMP_AMF_STRING,
ngx_null_string,
&v.video_codec_id_s, sizeof(v.video_codec_id_s) },
};
static ngx_rtmp_amf_elt_t in_audio_codec_id[] = {
{ NGX_RTMP_AMF_NUMBER,
ngx_null_string,
&v.audio_codec_id_n, 0 },
{ NGX_RTMP_AMF_STRING,
ngx_null_string,
&v.audio_codec_id_s, sizeof(v.audio_codec_id_s) },
};
static ngx_rtmp_amf_elt_t in_inf[] = {
{ NGX_RTMP_AMF_NUMBER,
ngx_string("width"),
&v.width, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("height"),
&v.height, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("duration"),
&v.duration, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("framerate"),
&v.frame_rate, 0 },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("videodatarate"),
&v.video_data_rate, 0 },
{ NGX_RTMP_AMF_VARIANT,
ngx_string("videocodecid"),
in_video_codec_id, sizeof(in_video_codec_id) },
{ NGX_RTMP_AMF_NUMBER,
ngx_string("audiodatarate"),
&v.audio_data_rate, 0 },
{ NGX_RTMP_AMF_VARIANT,
ngx_string("audiocodecid"),
in_audio_codec_id, sizeof(in_audio_codec_id) },
};
static ngx_rtmp_amf_elt_t in_elts[] = {
{ NGX_RTMP_AMF_STRING,
ngx_null_string,
NULL, 0 },
{ NGX_RTMP_AMF_OBJECT,
ngx_null_string,
in_inf, sizeof(in_inf) },
};
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
if (lacf == NULL || !lacf->live) {
return NGX_OK;
}
ngx_memzero(&v, sizeof(v));
/* use -1 as a sign of unchanged data;
* 0 is a valid value for uncompressed audio */
v.audio_codec_id_n = -1;
if (ngx_rtmp_receive_amf(s, in, in_elts + skip,
sizeof(in_elts) / sizeof(in_elts[0]) - skip))
{
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
"live: error parsing data frame");
return NGX_OK;
}
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module);
if (ctx == NULL) {
return NGX_OK;
}
if ((ctx->flags & NGX_RTMP_LIVE_PUBLISHING) == 0) {
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
"live: received data stream from non-publisher");
return NGX_OK;
}
meta = &ctx->stream->meta;
meta->width = v.width;
meta->height = v.height;
meta->duration = v.duration;
meta->frame_rate = v.frame_rate;
meta->video_data_rate = v.video_data_rate;
meta->video_codec_id = v.video_codec_id_n;
meta->audio_data_rate = v.audio_data_rate;
meta->audio_codec_id = (v.audio_codec_id_n == -1
? 0 : v.audio_codec_id_n == 0
? NGX_RTMP_AUDIO_UNCOMPRESSED : v.audio_codec_id_n);
ngx_log_debug8(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"live: data frame: "
"width=%ui height=%ui duration=%ui frame_rate=%ui "
"video=%s (%ui) audio=%s (%ui)",
meta->width, meta->height, meta->duration, meta->frame_rate,
ngx_rtmp_get_video_codec_name(meta->video_codec_id),
meta->video_codec_id,
ngx_rtmp_get_audio_codec_name(meta->audio_codec_id),
meta->audio_codec_id);
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_live_data_frame(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
return ngx_rtmp_live_ext_meta_data(s, h, in, 0);
}
static ngx_int_t
ngx_rtmp_live_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
return ngx_rtmp_live_ext_meta_data(s, h, in, 1);
}
static ngx_int_t
ngx_rtmp_live_postconfiguration(ngx_conf_t *cf)
{
ngx_rtmp_core_main_conf_t *cmcf;
ngx_rtmp_handler_pt *h;
ngx_rtmp_amf_handler_t *ch;
cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module);
/* register metadata handler */
ch = ngx_array_push(&cmcf->amf);
if (ch == NULL) {
return NGX_ERROR;
}
ngx_str_set(&ch->name, "@setDataFrame");
ch->handler = ngx_rtmp_live_data_frame;
ch = ngx_array_push(&cmcf->amf);
if (ch == NULL) {
return NGX_ERROR;
}
ngx_str_set(&ch->name, "onMetaData");
ch->handler = ngx_rtmp_live_meta_data;
/* register raw event handlers */
h = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_AUDIO]);
*h = ngx_rtmp_live_av;
h = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_VIDEO]);
*h = ngx_rtmp_live_av;
/* chain handlers */
next_publish = ngx_rtmp_publish;
ngx_rtmp_publish = ngx_rtmp_live_publish;
next_play = ngx_rtmp_play;
ngx_rtmp_play = ngx_rtmp_live_play;
next_delete_stream = ngx_rtmp_delete_stream;
ngx_rtmp_delete_stream = ngx_rtmp_live_delete_stream;
return NGX_OK;
}

View file

@ -8,6 +8,7 @@
#include "ngx_rtmp.h"
#include "ngx_rtmp_cmd_module.h"
#include "ngx_rtmp_bandwidth.h"
@ -15,16 +16,10 @@
#define NGX_RTMP_LIVE_PUBLISHING 0x01
typedef struct {
ngx_uint_t width;
ngx_uint_t height;
ngx_uint_t duration;
ngx_uint_t frame_rate;
ngx_uint_t video_data_rate;
ngx_uint_t video_codec_id;
ngx_uint_t audio_data_rate;
ngx_uint_t audio_codec_id;
} ngx_rtmp_live_meta_t;
/* Chunk stream ids for output */
#define NGX_RTMP_LIVE_CSID_AUDIO 6
#define NGX_RTMP_LIVE_CSID_VIDEO 7
#define NGX_RTMP_LIVE_MSID 1
typedef struct ngx_rtmp_live_ctx_s ngx_rtmp_live_ctx_t;
@ -42,19 +37,19 @@ struct ngx_rtmp_live_ctx_s {
uint32_t next_push;
uint32_t last_audio;
uint32_t last_video;
ngx_uint_t aac_version;
ngx_uint_t avc_version;
};
struct ngx_rtmp_live_stream_s {
u_char name[256];
u_char name[NGX_RTMP_MAX_NAME];
ngx_rtmp_live_stream_t *next;
ngx_rtmp_live_ctx_t *ctx;
ngx_uint_t flags;
ngx_rtmp_bandwidth_t bw_in;
ngx_rtmp_bandwidth_t bw_out;
ngx_rtmp_live_meta_t meta;
ngx_msec_t epoch;
ngx_chain_t *avc_header;
};

View file

@ -164,9 +164,9 @@ ngx_rtmp_user_message_handler(ngx_rtmp_session_t *s,
}
static ngx_int_t
ngx_rtmp_amf_message_basic_handler(ngx_rtmp_session_t *s,
ngx_rtmp_header_t *h, ngx_chain_t *in, ngx_int_t name_typeless)
ngx_int_t
ngx_rtmp_amf_message_handler(ngx_rtmp_session_t *s,
ngx_rtmp_header_t *h, ngx_chain_t *in)
{
ngx_rtmp_amf_ctx_t act;
ngx_rtmp_core_main_conf_t *cmcf;
@ -185,12 +185,24 @@ ngx_rtmp_amf_message_basic_handler(ngx_rtmp_session_t *s,
/* AMF command names come with string type, but shared object names
* come without type */
if (name_typeless) {
if (h->type == NGX_RTMP_MSG_AMF_SHARED ||
h->type == NGX_RTMP_MSG_AMF3_SHARED)
{
elts[0].type |= NGX_RTMP_AMF_TYPELESS;
} else {
elts[0].type &= ~NGX_RTMP_AMF_TYPELESS;
}
if ((h->type == NGX_RTMP_MSG_AMF3_SHARED ||
h->type == NGX_RTMP_MSG_AMF3_META ||
h->type == NGX_RTMP_MSG_AMF3_CMD)
&& in->buf->last > in->buf->pos)
{
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"AMF3 prefix: %ui", (ngx_int_t)*in->buf->pos);
++in->buf->pos;
}
cmcf = ngx_rtmp_get_module_main_conf(s, ngx_rtmp_core_module);
/* read AMF func name & transaction id */
@ -233,22 +245,6 @@ ngx_rtmp_amf_message_basic_handler(ngx_rtmp_session_t *s,
}
ngx_int_t
ngx_rtmp_amf_message_handler(ngx_rtmp_session_t *s,
ngx_rtmp_header_t *h, ngx_chain_t *in)
{
return ngx_rtmp_amf_message_basic_handler(s, h, in, 0);
}
ngx_int_t
ngx_rtmp_amf_shared_object_handler(ngx_rtmp_session_t *s,
ngx_rtmp_header_t *h, ngx_chain_t *in)
{
return ngx_rtmp_amf_message_basic_handler(s, h, in, 1);
}
ngx_int_t
ngx_rtmp_receive_amf(ngx_rtmp_session_t *s, ngx_chain_t *in,
ngx_rtmp_amf_elt_t *elts, size_t nelts)

View file

@ -8,6 +8,7 @@
#include "ngx_rtmp.h"
#include "ngx_rtmp_cmd_module.h"
#include "ngx_rtmp_netcall_module.h"
#include "ngx_rtmp_codec_module.h"
static ngx_rtmp_publish_pt next_publish;
@ -23,6 +24,8 @@ 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,
void *parent, void *child);
static ngx_int_t ngx_rtmp_record_write_frame(ngx_rtmp_session_t *s,
ngx_rtmp_header_t *h, ngx_chain_t *in);
typedef struct {
@ -231,17 +234,12 @@ ngx_rtmp_record_open(ngx_rtmp_session_t *s)
"record: failed to open file" " \"%s\" failed",
ctx->path);
}
return NGX_ERROR;
return NGX_OK;
}
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"record: opened '%s'", ctx->path);
if (ngx_rtmp_record_write_header(&ctx->file) != NGX_OK) {
ngx_rtmp_record_close(s);
return NGX_ERROR;
}
return NGX_OK;
}
@ -418,66 +416,20 @@ ngx_rtmp_record_delete_stream(ngx_rtmp_session_t *s,
static ngx_int_t
ngx_rtmp_record_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_rtmp_record_write_frame(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
ngx_rtmp_record_ctx_t *ctx;
ngx_rtmp_record_app_conf_t *racf;
u_char hdr[11], *p, *ph;
uint32_t timestamp, tag_size;
ngx_time_t next;
ngx_rtmp_record_ctx_t *ctx;
ngx_rtmp_record_app_conf_t *racf;
racf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_record_module);
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_record_module);
if (racf == NULL || ctx == NULL || racf->flags & NGX_RTMP_RECORD_OFF) {
return NGX_OK;
}
if (ctx->file.fd == NGX_INVALID_FILE && racf->interval
&& (ctx->last.sec || ctx->last.msec))
{
next = ctx->last;
next.msec += racf->interval;
next.sec += (next.msec / 1000);
next.msec %= 1000;
if (ngx_cached_time->sec > next.sec
|| (ngx_cached_time->sec == next.sec
&& ngx_cached_time->msec > next.msec))
{
if (ngx_rtmp_record_open(s) != NGX_OK) {
ngx_log_error(NGX_LOG_CRIT, s->connection->log, 0,
"record: '%s' failed", ctx->path);
}
}
}
if (ctx->file.fd == NGX_INVALID_FILE) {
return NGX_OK;
}
/* filter frames */
if (h->type == NGX_RTMP_MSG_AUDIO &&
(racf->flags & NGX_RTMP_RECORD_AUDIO) == 0)
{
return NGX_OK;
}
if (h->type == NGX_RTMP_MSG_VIDEO &&
(racf->flags & NGX_RTMP_RECORD_VIDEO) == 0 &&
((racf->flags & NGX_RTMP_RECORD_KEYFRAMES) == 0
|| ngx_rtmp_get_video_frame_type(in) != NGX_RTMP_VIDEO_KEY_FRAME))
{
return NGX_OK;
}
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"record: av: mlen=%uD", h->mlen);
if (ctx->file.offset == 0) {
ctx->epoch = h->timestamp;
}
timestamp = h->timestamp - ctx->epoch;
/* write tag header */
@ -554,6 +506,118 @@ ngx_rtmp_record_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
}
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,
ngx_chain_t *in)
{
ngx_rtmp_record_ctx_t *ctx;
ngx_rtmp_record_app_conf_t *racf;
ngx_time_t next;
ngx_rtmp_header_t ch;
ngx_rtmp_codec_ctx_t *codec_ctx;
racf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_record_module);
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_record_module);
if (racf == NULL || ctx == NULL || racf->flags & NGX_RTMP_RECORD_OFF) {
return NGX_OK;
}
if (ctx->file.fd == NGX_INVALID_FILE && racf->interval
&& (ctx->last.sec || ctx->last.msec))
{
next = ctx->last;
next.msec += racf->interval;
next.sec += (next.msec / 1000);
next.msec %= 1000;
if (ngx_cached_time->sec > next.sec
|| (ngx_cached_time->sec == next.sec
&& ngx_cached_time->msec > next.msec))
{
if (ngx_rtmp_record_open(s) != NGX_OK) {
ngx_log_error(NGX_LOG_CRIT, s->connection->log, 0,
"record: '%s' failed", ctx->path);
}
}
}
if (ctx->file.fd == NGX_INVALID_FILE) {
return NGX_OK;
}
/* filter frames */
if (h->type == NGX_RTMP_MSG_AUDIO &&
(racf->flags & NGX_RTMP_RECORD_AUDIO) == 0)
{
return NGX_OK;
}
if (h->type == NGX_RTMP_MSG_VIDEO &&
(racf->flags & NGX_RTMP_RECORD_VIDEO) == 0 &&
((racf->flags & NGX_RTMP_RECORD_KEYFRAMES) == 0
|| ngx_rtmp_get_video_frame_type(in) != NGX_RTMP_VIDEO_KEY_FRAME))
{
return NGX_OK;
}
if (ctx->file.offset == 0) {
ctx->epoch = h->timestamp;
if (ngx_rtmp_record_write_header(&ctx->file) != NGX_OK) {
ngx_rtmp_record_close(s);
return NGX_OK;
}
codec_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module);
if (codec_ctx) {
ch = *h;
if (codec_ctx->aac_header && (racf->flags & NGX_RTMP_RECORD_AUDIO))
{
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"record: writing AAC header");
ch.type = NGX_RTMP_MSG_AUDIO;
ch.mlen = ngx_rtmp_record_get_chain_mlen(codec_ctx->aac_header);
if (ngx_rtmp_record_write_frame(s, &ch, codec_ctx->aac_header)
!= NGX_OK)
{
return NGX_OK;
}
}
if (codec_ctx->avc_header && (racf->flags
& (NGX_RTMP_RECORD_VIDEO|NGX_RTMP_RECORD_KEYFRAMES)))
{
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"record: writing AVC header");
ch.type = NGX_RTMP_MSG_VIDEO;
ch.mlen = ngx_rtmp_record_get_chain_mlen(codec_ctx->avc_header);
if (ngx_rtmp_record_write_frame(s, &ch, codec_ctx->avc_header)
!= NGX_OK)
{
return NGX_OK;
}
}
}
}
return ngx_rtmp_record_write_frame(s, h, in);
}
static char *
ngx_rtmp_notify_on_record_done(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{

View file

@ -9,6 +9,7 @@
static ngx_rtmp_publish_pt next_publish;
static ngx_rtmp_play_pt next_play;
static ngx_rtmp_delete_stream_pt next_delete_stream;
static ngx_int_t ngx_rtmp_relay_postconfiguration(ngx_conf_t *cf);
@ -316,6 +317,10 @@ ngx_rtmp_relay_create_local_ctx(ngx_rtmp_session_t *s, ngx_str_t *app,
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_relay_module);
}
ctx->session = s;
if (ctx->publish) {
return NULL;
}
ctx->name.len = name->len;
ctx->name.data = ngx_palloc(s->connection->pool, name->len);
@ -1003,8 +1008,7 @@ ngx_rtmp_relay_handshake_done(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
static ngx_int_t
ngx_rtmp_relay_disconnect(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
ngx_rtmp_relay_delete_stream(ngx_rtmp_session_t *s, ngx_rtmp_delete_stream_t *v)
{
ngx_rtmp_relay_app_conf_t *racf;
ngx_rtmp_relay_ctx_t *ctx, **cctx;
@ -1012,7 +1016,7 @@ ngx_rtmp_relay_disconnect(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_relay_module);
if (ctx == NULL || ctx->publish == NULL) {
return NGX_OK;
goto next;
}
/* play end disconnect? */
@ -1052,7 +1056,9 @@ ngx_rtmp_relay_disconnect(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_rtmp_finalize_session(ctx->publish->session);
}
return NGX_OK;
ctx->publish = NULL;
goto next;
}
/* publish end disconnect */
@ -1077,7 +1083,8 @@ ngx_rtmp_relay_disconnect(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
*cctx = ctx->next;
}
return NGX_OK;
next:
return next_delete_stream(s, v);
}
@ -1149,9 +1156,6 @@ ngx_rtmp_relay_postconfiguration(ngx_conf_t *cf)
h = ngx_array_push(&cmcf->events[NGX_RTMP_HANDSHAKE_DONE]);
*h = ngx_rtmp_relay_handshake_done;
h = ngx_array_push(&cmcf->events[NGX_RTMP_DISCONNECT]);
*h = ngx_rtmp_relay_disconnect;
next_publish = ngx_rtmp_publish;
ngx_rtmp_publish = ngx_rtmp_relay_publish;
@ -1159,6 +1163,9 @@ ngx_rtmp_relay_postconfiguration(ngx_conf_t *cf)
next_play = ngx_rtmp_play;
ngx_rtmp_play = ngx_rtmp_relay_play;
next_delete_stream = ngx_rtmp_delete_stream;
ngx_rtmp_delete_stream = ngx_rtmp_relay_delete_stream;
ch = ngx_array_push(&cmcf->amf);
ngx_str_set(&ch->name, "_result");

View file

@ -8,7 +8,7 @@
#include "ngx_rtmp.h"
#include "ngx_rtmp_live_module.h"
#include "ngx_rtmp_codecs.h"
#include "ngx_rtmp_codec_module.h"
static ngx_int_t ngx_rtmp_stat_postconfiguration(ngx_conf_t *cf);
@ -93,6 +93,7 @@ ngx_module_t ngx_rtmp_stat_module = {
};
#define NGX_RTMP_STAT_BUFSIZE 256
@ -199,12 +200,57 @@ ngx_rtmp_stat_bw(ngx_http_request_t *r, ngx_chain_t ***lll,
}
#ifdef NGX_RTMP_POOL_DEBUG
static void
ngx_rtmp_stat_get_pool_size(ngx_pool_t *pool, ngx_uint_t *nlarge,
size_t *size)
{
ngx_pool_large_t *l;
ngx_pool_t *p, *n;
*nlarge = 0;
for (l = pool->large; l; l = l->next) {
++*nlarge;
}
*size = 0;
for (p = pool, n = pool->d.next; /* void */; p = n, n = n->d.next) {
*size += (p->d.last - (u_char *)p);
if (n == NULL) {
break;
}
}
}
static void
ngx_rtmp_stat_dump_pool(ngx_http_request_t *r, ngx_chain_t ***lll,
ngx_pool_t *pool)
{
ngx_uint_t nlarge;
size_t size;
u_char buf[NGX_OFF_T_LEN + 1];
size = 0;
nlarge = 0;
ngx_rtmp_stat_get_pool_size(pool, &nlarge, &size);
NGX_RTMP_STAT_L("<pool><nlarge>");
NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf),
"%ui", nlarge) - buf);
NGX_RTMP_STAT_L("</nlarge><size>");
NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf),
"%uz", size) - buf);
NGX_RTMP_STAT_L("</size></pool>\r\n");
}
#endif
static void
ngx_rtmp_stat_live(ngx_http_request_t *r, ngx_chain_t ***lll,
ngx_rtmp_live_app_conf_t *lacf)
{
ngx_rtmp_live_stream_t *stream;
ngx_rtmp_live_meta_t *meta;
ngx_rtmp_codec_ctx_t *codec;
ngx_rtmp_live_ctx_t *ctx;
ngx_rtmp_session_t *s;
ngx_int_t n;
@ -212,7 +258,7 @@ ngx_rtmp_stat_live(ngx_http_request_t *r, ngx_chain_t ***lll,
ngx_int_t publishing;
u_char buf[NGX_OFF_T_LEN + 1];
ngx_rtmp_stat_loc_conf_t *slcf;
u_char *codec;
u_char *cname;
slcf = ngx_http_get_module_loc_conf(r, ngx_rtmp_stat_module);
@ -233,39 +279,19 @@ ngx_rtmp_stat_live(ngx_http_request_t *r, ngx_chain_t ***lll,
"%M", ngx_current_msec - stream->epoch) - buf);
NGX_RTMP_STAT_L("</time>");
meta = &stream->meta;
NGX_RTMP_STAT_L("<meta><width>");
NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf),
"%ui", meta->width) - buf);
NGX_RTMP_STAT_L("</width><height>");
NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf),
"%ui", meta->height) - buf);
NGX_RTMP_STAT_L("</height><framerate>");
NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf),
"%ui", meta->frame_rate) - buf);
NGX_RTMP_STAT_L("</framerate><video>");
codec = ngx_rtmp_get_video_codec_name(meta->video_codec_id);
if (*codec) {
NGX_RTMP_STAT_ECS(codec);
}
NGX_RTMP_STAT_L("</video><audio>");
codec = ngx_rtmp_get_audio_codec_name(meta->audio_codec_id);
if (*codec) {
NGX_RTMP_STAT_ECS(codec);
}
NGX_RTMP_STAT_L("</audio></meta>\r\n");
ngx_rtmp_stat_bw(r, lll, &stream->bw_in, &stream->bw_out);
nclients = 0;
codec = NULL;
for (ctx = stream->ctx; ctx; ctx = ctx->next, ++nclients) {
s = ctx->session;
/* TODO: add
* 1) session start time
* 2) drop stats */
if (slcf->stat & NGX_RTMP_STAT_CLIENTS) {
NGX_RTMP_STAT_L("<client>");
#ifdef NGX_RTMP_POOL_DEBUG
ngx_rtmp_stat_dump_pool(r, lll, s->connection->pool);
#endif
NGX_RTMP_STAT_L("<address>");
NGX_RTMP_STAT_S(&s->connection->addr_text);
NGX_RTMP_STAT_L("</address>");
@ -280,6 +306,12 @@ ngx_rtmp_stat_live(ngx_http_request_t *r, ngx_chain_t ***lll,
"%uz", ctx->dropped) - buf);
NGX_RTMP_STAT_L("</dropped>");
NGX_RTMP_STAT_L("<avsync>");
NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf),
"%L", (int64_t)ctx->last_audio
- ctx->last_video) - buf);
NGX_RTMP_STAT_L("</avsync>");
if (s->flashver.len) {
NGX_RTMP_STAT_L("<flashver>");
NGX_RTMP_STAT_ES(&s->flashver);
@ -300,10 +332,47 @@ ngx_rtmp_stat_live(ngx_http_request_t *r, ngx_chain_t ***lll,
}
if (ctx->flags & NGX_RTMP_LIVE_PUBLISHING) {
publishing = 1;
codec = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module);
}
}
total_nclients += nclients;
if (codec) {
NGX_RTMP_STAT_L("<meta><width>");
NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf),
"%ui", codec->width) - buf);
NGX_RTMP_STAT_L("</width><height>");
NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf),
"%ui", codec->height) - buf);
NGX_RTMP_STAT_L("</height><framerate>");
NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf),
"%ui", codec->frame_rate) - buf);
NGX_RTMP_STAT_L("</framerate>");
cname = ngx_rtmp_get_video_codec_name(codec->video_codec_id);
if (*cname) {
NGX_RTMP_STAT_L("<video>");
NGX_RTMP_STAT_ECS(cname);
NGX_RTMP_STAT_L("</video>");
}
cname = ngx_rtmp_get_audio_codec_name(codec->audio_codec_id);
if (*cname) {
NGX_RTMP_STAT_L("<audio>");
NGX_RTMP_STAT_ECS(cname);
NGX_RTMP_STAT_L("</audio>");
}
if (*codec->profile) {
NGX_RTMP_STAT_L("<profile>");
NGX_RTMP_STAT_ECS(codec->profile);
NGX_RTMP_STAT_L("</profile>");
}
if (*codec->level) {
NGX_RTMP_STAT_L("<level>");
NGX_RTMP_STAT_ECS(codec->level);
NGX_RTMP_STAT_L("</level>");
}
NGX_RTMP_STAT_L("</meta>\r\n");
}
NGX_RTMP_STAT_L("<nclients>");
NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf),
"%uz", nclients) - buf);
@ -357,6 +426,10 @@ ngx_rtmp_stat_server(ngx_http_request_t *r, ngx_chain_t ***lll,
NGX_RTMP_STAT_L("<server>\r\n");
#ifdef NGX_RTMP_POOL_DEBUG
ngx_rtmp_stat_dump_pool(r, lll, cscf->pool);
#endif
cacf = cscf->applications.elts;
for (n = 0; n < cscf->applications.nelts; ++n, ++cacf) {
ngx_rtmp_stat_application(r, lll, *cacf);

View file

@ -92,7 +92,11 @@
<td><xsl:value-of select="round(bwout div 1024)"/></td>
<td><xsl:value-of select="meta/width"/>x<xsl:value-of select="meta/height"/></td>
<td align="middle"><xsl:value-of select="meta/framerate"/></td>
<td><xsl:value-of select="meta/video"/></td>
<td>
<xsl:value-of select="meta/video"/>
<xsl:apply-templates select="meta/profile"/>
<xsl:apply-templates select="meta/level"/>
</td>
<td><xsl:value-of select="meta/audio"/></td>
<td> <xsl:apply-templates select="publishing"/> </td>
<td>
@ -113,6 +117,7 @@
<th>Flash version</th>
<th>Page URL</th>
<th>Dropped</th>
<th>A-V</th>
<th>Time</th>
</tr>
<xsl:apply-templates select="client"/>
@ -158,6 +163,7 @@
</a>
</td>
<td><xsl:value-of select="dropped"/></td>
<td><xsl:value-of select="avsync"/></td>
<td>
<xsl:call-template name="showtime">
<xsl:with-param name="time" select="time"/>
@ -170,4 +176,12 @@
publishing
</xsl:template>
<xsl:template match="profile">
/ <xsl:value-of select="."/>
</xsl:template>
<xsl:template match="level">
/ <xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>