2012-03-12 18:58:00 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Roman Arutyunyan
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-05-07 13:41:03 +02:00
|
|
|
#include "ngx_rtmp_live_module.h"
|
2012-03-23 18:29:22 +01:00
|
|
|
#include "ngx_rtmp_cmd_module.h"
|
2012-05-12 15:47:39 +02:00
|
|
|
#include "ngx_rtmp_codecs.h"
|
2012-03-12 18:58:00 +01:00
|
|
|
|
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
static ngx_rtmp_publish_pt next_publish;
|
|
|
|
static ngx_rtmp_play_pt next_play;
|
|
|
|
static ngx_rtmp_delete_stream_pt next_delete_stream;
|
2012-03-27 18:26:43 +02:00
|
|
|
|
|
|
|
|
2012-03-23 18:29:22 +01:00
|
|
|
/* Chunk stream ids for output */
|
2012-04-20 16:03:00 +02:00
|
|
|
#define NGX_RTMP_LIVE_CSID_AUDIO 6
|
|
|
|
#define NGX_RTMP_LIVE_CSID_VIDEO 7
|
|
|
|
#define NGX_RTMP_LIVE_MSID 1
|
2012-03-18 14:09:19 +01:00
|
|
|
|
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
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,
|
2012-03-12 18:58:00 +01:00
|
|
|
void *parent, void *child);
|
|
|
|
|
2012-03-21 16:08:59 +01:00
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
#define NGX_RTMP_LIVE_TIME_ABSOLUTE 0x01
|
|
|
|
#define NGX_RTMP_LIVE_TIME_RELATIVE 0x02
|
|
|
|
|
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
static ngx_command_t ngx_rtmp_live_commands[] = {
|
2012-03-21 16:08:59 +01:00
|
|
|
|
2012-03-23 13:03:32 +01:00
|
|
|
{ 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,
|
2012-03-23 13:39:28 +01:00
|
|
|
offsetof(ngx_rtmp_live_app_conf_t, live),
|
2012-03-23 13:03:32 +01:00
|
|
|
NULL },
|
|
|
|
|
|
|
|
{ ngx_string("stream_buckets"),
|
|
|
|
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
|
2012-03-12 18:58:00 +01:00
|
|
|
ngx_conf_set_str_slot,
|
2012-03-23 13:03:32 +01:00
|
|
|
NGX_RTMP_APP_CONF_OFFSET,
|
2012-03-23 13:39:28 +01:00
|
|
|
offsetof(ngx_rtmp_live_app_conf_t, nbuckets),
|
2012-03-23 13:03:32 +01:00
|
|
|
NULL },
|
|
|
|
|
2012-04-20 07:01:04 +02:00
|
|
|
{ ngx_string("buffer"),
|
2012-04-05 22:35:28 +02:00
|
|
|
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
|
2012-04-20 07:01:04 +02:00
|
|
|
ngx_conf_set_msec_slot,
|
2012-03-23 13:03:32 +01:00
|
|
|
NGX_RTMP_APP_CONF_OFFSET,
|
2012-04-20 07:01:04 +02:00
|
|
|
offsetof(ngx_rtmp_live_app_conf_t, buflen),
|
2012-03-12 18:58:00 +01:00
|
|
|
NULL },
|
|
|
|
|
|
|
|
ngx_null_command
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
static ngx_rtmp_module_t ngx_rtmp_live_module_ctx = {
|
2012-03-12 18:58:00 +01:00
|
|
|
NULL, /* preconfiguration */
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_live_postconfiguration, /* postconfiguration */
|
2012-03-12 18:58:00 +01:00
|
|
|
NULL, /* create main configuration */
|
|
|
|
NULL, /* init main configuration */
|
2012-03-23 13:03:32 +01:00
|
|
|
NULL, /* create server configuration */
|
|
|
|
NULL, /* merge server configuration */
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_live_create_app_conf, /* create app configuration */
|
|
|
|
ngx_rtmp_live_merge_app_conf /* merge app configuration */
|
2012-03-12 18:58:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_module_t ngx_rtmp_live_module = {
|
2012-03-12 18:58:00 +01:00
|
|
|
NGX_MODULE_V1,
|
2012-03-23 13:39:28 +01:00
|
|
|
&ngx_rtmp_live_module_ctx, /* module context */
|
|
|
|
ngx_rtmp_live_commands, /* module directives */
|
2012-03-12 18:58:00 +01:00
|
|
|
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 *
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_live_create_app_conf(ngx_conf_t *cf)
|
2012-03-12 18:58:00 +01:00
|
|
|
{
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_live_app_conf_t *lacf;
|
2012-03-12 18:58:00 +01:00
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
lacf = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_live_app_conf_t));
|
|
|
|
if (lacf == NULL) {
|
2012-03-12 18:58:00 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
lacf->live = NGX_CONF_UNSET;
|
|
|
|
lacf->nbuckets = NGX_CONF_UNSET;
|
2012-04-20 07:01:04 +02:00
|
|
|
lacf->buflen = NGX_CONF_UNSET;
|
2012-03-12 18:58:00 +01:00
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
return lacf;
|
2012-03-12 18:58:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_live_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
|
2012-03-12 18:58:00 +01:00
|
|
|
{
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_live_app_conf_t *prev = parent;
|
|
|
|
ngx_rtmp_live_app_conf_t *conf = child;
|
2012-03-12 18:58:00 +01:00
|
|
|
|
2012-03-23 13:03:32 +01:00
|
|
|
ngx_conf_merge_value(conf->live, prev->live, 0);
|
|
|
|
ngx_conf_merge_value(conf->nbuckets, prev->nbuckets, 1024);
|
2012-04-20 07:01:04 +02:00
|
|
|
ngx_conf_merge_msec_value(conf->buflen, prev->buflen, 0);
|
2012-03-12 18:58:00 +01:00
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
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);
|
2012-03-12 18:58:00 +01:00
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
static ngx_rtmp_live_stream_t **
|
|
|
|
ngx_rtmp_live_get_stream(ngx_rtmp_session_t *s, u_char *name, int create)
|
2012-03-12 18:58:00 +01:00
|
|
|
{
|
2012-04-20 16:03:00 +02:00
|
|
|
ngx_rtmp_live_app_conf_t *lacf;
|
|
|
|
ngx_rtmp_live_stream_t **stream;
|
|
|
|
size_t len;
|
2012-03-12 18:58:00 +01:00
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
|
|
|
|
if (lacf == NULL) {
|
2012-03-23 13:03:32 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-03-14 07:06:47 +01:00
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
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);
|
2012-03-14 07:06:47 +01:00
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
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));
|
|
|
|
|
|
|
|
return stream;
|
2012-03-14 07:06:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-12 18:58:00 +01:00
|
|
|
static void
|
2012-03-27 18:26:43 +02:00
|
|
|
ngx_rtmp_live_join(ngx_rtmp_session_t *s, u_char *name,
|
2012-03-12 18:58:00 +01:00
|
|
|
ngx_uint_t flags)
|
|
|
|
{
|
2012-04-20 16:03:00 +02:00
|
|
|
ngx_rtmp_live_ctx_t *ctx;
|
|
|
|
ngx_rtmp_live_stream_t **stream;
|
|
|
|
ngx_rtmp_live_app_conf_t *lacf;
|
2012-03-12 18:58:00 +01:00
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
|
|
|
|
if (lacf == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2012-03-12 18:58:00 +01:00
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module);
|
2012-03-12 18:58:00 +01:00
|
|
|
if (ctx == NULL) {
|
2012-04-20 16:03:00 +02:00
|
|
|
ctx = ngx_pcalloc(s->connection->pool, sizeof(ngx_rtmp_live_ctx_t));
|
2012-03-12 18:58:00 +01:00
|
|
|
ctx->session = s;
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_live_module);
|
2012-03-12 18:58:00 +01:00
|
|
|
}
|
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
if (ctx->stream) {
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log,
|
|
|
|
0, "live: already joined");
|
2012-03-12 18:58:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
|
|
|
"live: join '%s'", name);
|
2012-03-27 18:26:43 +02:00
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
stream = ngx_rtmp_live_get_stream(s, name, 1);
|
|
|
|
if (stream == NULL) {
|
2012-03-23 13:03:32 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-04-28 18:45:27 +02:00
|
|
|
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;
|
|
|
|
}
|
2012-04-20 16:03:00 +02:00
|
|
|
ctx->stream = *stream;
|
2012-03-12 18:58:00 +01:00
|
|
|
ctx->flags = flags;
|
2012-04-20 16:03:00 +02:00
|
|
|
ctx->next = (*stream)->ctx;
|
|
|
|
(*stream)->ctx = ctx;
|
|
|
|
|
|
|
|
if (lacf->buflen) {
|
|
|
|
s->out_buffer = 1;
|
|
|
|
}
|
2012-03-12 18:58:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-13 06:41:51 +01:00
|
|
|
static ngx_int_t
|
2012-03-27 18:26:43 +02:00
|
|
|
ngx_rtmp_live_delete_stream(ngx_rtmp_session_t *s, ngx_rtmp_delete_stream_t *v)
|
2012-03-12 18:58:00 +01:00
|
|
|
{
|
2012-04-20 16:03:00 +02:00
|
|
|
ngx_rtmp_live_ctx_t *ctx, **cctx;
|
|
|
|
ngx_rtmp_live_stream_t **stream;
|
|
|
|
ngx_rtmp_live_app_conf_t *lacf;
|
2012-03-12 18:58:00 +01:00
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
|
|
|
|
if (lacf == NULL) {
|
|
|
|
goto next;
|
|
|
|
}
|
2012-03-12 18:58:00 +01:00
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module);
|
2012-03-21 16:08:59 +01:00
|
|
|
if (ctx == NULL) {
|
2012-03-27 18:26:43 +02:00
|
|
|
goto next;
|
2012-03-12 18:58:00 +01:00
|
|
|
}
|
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
if (ctx->stream == NULL) {
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
|
|
|
"live: not joined ");
|
|
|
|
goto next;
|
2012-03-23 13:03:32 +01:00
|
|
|
}
|
2012-03-12 18:58:00 +01:00
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
|
|
|
"live: leave '%s'", ctx->stream->name);
|
|
|
|
|
2012-04-28 18:45:27 +02:00
|
|
|
if (ctx->stream->flags & NGX_RTMP_LIVE_PUBLISHING
|
|
|
|
&& ctx->flags & NGX_RTMP_LIVE_PUBLISHING)
|
|
|
|
{
|
|
|
|
ctx->stream->flags &= ~NGX_RTMP_LIVE_PUBLISHING;
|
|
|
|
}
|
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
for (cctx = &ctx->stream->ctx; *cctx; cctx = &(*cctx)->next) {
|
|
|
|
if (*cctx == ctx) {
|
|
|
|
*cctx = ctx->next;
|
2012-03-13 06:41:51 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
if (ctx->stream->ctx) {
|
|
|
|
ctx->stream = NULL;
|
|
|
|
goto next;
|
|
|
|
}
|
2012-04-07 21:38:28 +02:00
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
|
|
|
"live: delete empty stream '%s'", ctx->stream->name);
|
2012-04-07 21:38:28 +02:00
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
stream = ngx_rtmp_live_get_stream(s, ctx->stream->name, 0);
|
|
|
|
if (stream == NULL) {
|
2012-04-07 21:38:28 +02:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2012-04-20 16:03:00 +02:00
|
|
|
*stream = (*stream)->next;
|
2012-04-07 21:38:28 +02:00
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
ctx->stream->next = lacf->free_streams;
|
|
|
|
lacf->free_streams = ctx->stream;
|
|
|
|
ctx->stream = NULL;
|
2012-04-07 21:38:28 +02:00
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
next:
|
|
|
|
return next_delete_stream(s, v);
|
2012-04-07 21:38:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-13 06:41:51 +01:00
|
|
|
static ngx_int_t
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_live_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
|
2012-03-13 06:41:51 +01:00
|
|
|
ngx_chain_t *in)
|
|
|
|
{
|
|
|
|
ngx_connection_t *c;
|
2012-04-20 07:01:04 +02:00
|
|
|
ngx_rtmp_live_ctx_t *ctx, *pctx;
|
2012-04-20 16:03:00 +02:00
|
|
|
ngx_chain_t *out, *out_abs;
|
2012-03-17 07:12:19 +01:00
|
|
|
ngx_rtmp_core_srv_conf_t *cscf;
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_live_app_conf_t *lacf;
|
2012-03-18 14:09:19 +01:00
|
|
|
ngx_rtmp_session_t *ss;
|
2012-04-20 16:03:00 +02:00
|
|
|
ngx_rtmp_header_t ch, lh;
|
|
|
|
ngx_uint_t prio, peer_prio;
|
2012-05-07 13:41:03 +02:00
|
|
|
ngx_uint_t peers, dropped_peers;
|
2012-03-13 06:41:51 +01:00
|
|
|
|
|
|
|
c = s->connection;
|
2012-03-23 13:39:28 +01:00
|
|
|
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
|
|
|
|
if (lacf == NULL) {
|
2012-04-20 16:03:00 +02:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0,
|
2012-03-23 13:03:32 +01:00
|
|
|
"live: NULL application");
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module);
|
2012-04-20 16:03:00 +02:00
|
|
|
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))
|
|
|
|
{
|
2012-04-07 21:38:28 +02:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
if ((ctx->flags & NGX_RTMP_LIVE_PUBLISHING) == 0) {
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0,
|
|
|
|
"live: received audio/video from non-publisher");
|
|
|
|
return NGX_OK;
|
2012-04-05 22:35:28 +02:00
|
|
|
}
|
2012-03-20 00:52:39 +01:00
|
|
|
|
2012-04-26 14:47:15 +02:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, c->log, 0,
|
|
|
|
"live: av: %s timestamp=%uD",
|
2012-04-23 17:13:35 +02:00
|
|
|
h->type == NGX_RTMP_MSG_VIDEO ? "video" : "audio",
|
2012-04-26 14:47:15 +02:00
|
|
|
h->timestamp);
|
2012-04-20 16:03:00 +02:00
|
|
|
|
|
|
|
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
|
2012-03-20 00:52:39 +01:00
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
/* 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;
|
2012-03-18 14:09:19 +01:00
|
|
|
if (h->type == NGX_RTMP_MSG_VIDEO) {
|
2012-04-20 17:55:37 +02:00
|
|
|
prio = ngx_rtmp_get_video_frame_type(in);
|
2012-04-20 16:03:00 +02:00
|
|
|
ch.csid = NGX_RTMP_LIVE_CSID_VIDEO;
|
|
|
|
lh.timestamp = ctx->last_video;
|
2012-04-23 17:13:35 +02:00
|
|
|
ctx->last_video = ch.timestamp;
|
2012-03-20 00:52:39 +01:00
|
|
|
} else {
|
2012-04-20 16:03:00 +02:00
|
|
|
/* 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;
|
2012-04-23 17:13:35 +02:00
|
|
|
ctx->last_audio = ch.timestamp;
|
2012-03-13 06:41:51 +01:00
|
|
|
}
|
2012-04-20 16:03:00 +02:00
|
|
|
lh.csid = ch.csid;
|
2012-03-13 06:41:51 +01:00
|
|
|
|
2012-03-17 07:12:19 +01:00
|
|
|
out = ngx_rtmp_append_shared_bufs(cscf, NULL, in);
|
2012-04-26 14:47:15 +02:00
|
|
|
ngx_rtmp_prepare_message(s, &ch, &lh, out);
|
2012-03-13 06:41:51 +01:00
|
|
|
|
2012-05-07 13:41:03 +02:00
|
|
|
peers = 0;
|
|
|
|
dropped_peers = 0;
|
|
|
|
|
2012-04-20 07:01:04 +02:00
|
|
|
/* broadcast to all subscribers */
|
2012-04-20 16:03:00 +02:00
|
|
|
for (pctx = ctx->stream->ctx; pctx; pctx = pctx->next) {
|
|
|
|
if (pctx == ctx) {
|
2012-03-21 16:08:59 +01:00
|
|
|
continue;
|
|
|
|
}
|
2012-05-07 13:41:03 +02:00
|
|
|
++peers;
|
2012-04-07 21:38:28 +02:00
|
|
|
ss = pctx->session;
|
2012-03-16 19:09:04 +01:00
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
/* send absolute frame */
|
2012-04-26 14:47:15 +02:00
|
|
|
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);
|
2012-04-20 16:03:00 +02:00
|
|
|
pctx->msg_mask |= (1 << h->type);
|
|
|
|
ngx_rtmp_send_message(ss, out_abs, prio);
|
2012-04-26 14:47:15 +02:00
|
|
|
ngx_rtmp_free_shared_chain(cscf, out_abs);
|
2012-04-07 21:38:28 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-04-20 16:03:00 +02:00
|
|
|
/* push buffered data */
|
|
|
|
peer_prio = prio;
|
2012-05-07 13:41:03 +02:00
|
|
|
/*
|
2012-04-20 07:01:04 +02:00
|
|
|
if (lacf->buflen && h->timestamp >= pctx->next_push) {
|
2012-04-20 16:03:00 +02:00
|
|
|
peer_prio = 0;
|
2012-04-20 07:01:04 +02:00
|
|
|
pctx->next_push = h->timestamp + lacf->buflen;
|
2012-05-07 13:41:03 +02:00
|
|
|
}*/
|
|
|
|
if (ngx_rtmp_send_message(ss, out, peer_prio) != NGX_OK) {
|
|
|
|
++pctx->dropped;
|
|
|
|
++dropped_peers;
|
2012-03-21 16:08:59 +01:00
|
|
|
}
|
2012-03-13 06:41:51 +01:00
|
|
|
}
|
2012-04-18 14:37:18 +02:00
|
|
|
ngx_rtmp_free_shared_chain(cscf, out);
|
2012-03-14 07:06:47 +01:00
|
|
|
|
2012-05-07 13:41:03 +02:00
|
|
|
ngx_rtmp_update_bandwidth(&ctx->stream->bw_in, h->mlen);
|
|
|
|
ngx_rtmp_update_bandwidth(&ctx->stream->bw_out,
|
|
|
|
h->mlen * (peers - dropped_peers));
|
|
|
|
|
2012-03-13 06:41:51 +01:00
|
|
|
return NGX_OK;
|
2012-03-12 18:58:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-14 07:06:47 +01:00
|
|
|
static ngx_int_t
|
2012-03-27 18:26:43 +02:00
|
|
|
ngx_rtmp_live_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
|
2012-03-14 07:06:47 +01:00
|
|
|
{
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_live_app_conf_t *lacf;
|
2012-03-14 07:06:47 +01:00
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
|
2012-03-23 13:03:32 +01:00
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
if (lacf == NULL || !lacf->live) {
|
2012-03-27 18:26:43 +02:00
|
|
|
goto next;
|
2012-03-23 13:03:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
2012-03-27 18:26:43 +02:00
|
|
|
"live: publish: name='%s' type='%s'",
|
|
|
|
v->name, v->type);
|
2012-03-23 13:03:32 +01:00
|
|
|
|
|
|
|
/* join stream as publisher */
|
2012-03-27 18:26:43 +02:00
|
|
|
ngx_rtmp_live_join(s, v->name, NGX_RTMP_LIVE_PUBLISHING);
|
2012-03-12 18:58:00 +01:00
|
|
|
|
2012-03-27 18:26:43 +02:00
|
|
|
next:
|
|
|
|
return next_publish(s, v);
|
2012-03-14 07:06:47 +01:00
|
|
|
}
|
2012-03-12 18:58:00 +01:00
|
|
|
|
2012-03-13 06:41:51 +01:00
|
|
|
|
2012-03-14 17:38:15 +01:00
|
|
|
static ngx_int_t
|
2012-03-27 18:26:43 +02:00
|
|
|
ngx_rtmp_live_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
|
2012-03-14 17:38:15 +01:00
|
|
|
{
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_live_app_conf_t *lacf;
|
2012-03-19 16:55:46 +01:00
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
|
2012-03-23 13:03:32 +01:00
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
if (lacf == NULL || !lacf->live) {
|
2012-03-27 18:26:43 +02:00
|
|
|
goto next;
|
2012-03-23 13:03:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug4(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
2012-03-27 18:26:43 +02:00
|
|
|
"live: play: name='%s' start=%uD duration=%uD reset=%d",
|
|
|
|
v->name, (uint32_t)v->start,
|
|
|
|
(uint32_t)v->duration, (uint32_t)v->reset);
|
2012-03-16 19:09:04 +01:00
|
|
|
|
2012-03-23 13:03:32 +01:00
|
|
|
/* join stream as player */
|
2012-04-20 16:03:00 +02:00
|
|
|
ngx_rtmp_live_join(s, v->name, 0);
|
2012-03-16 19:09:04 +01:00
|
|
|
|
2012-03-27 18:26:43 +02:00
|
|
|
next:
|
|
|
|
return next_play(s, v);
|
2012-03-21 22:00:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-12 15:47:39 +02:00
|
|
|
static ngx_int_t
|
2012-05-21 17:35:11 +02:00
|
|
|
ngx_rtmp_live_ext_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
|
|
|
|
ngx_chain_t *in, ngx_uint_t skip)
|
2012-05-12 15:47:39 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
double audio_data_rate;
|
|
|
|
double audio_codec_id;
|
|
|
|
} v;
|
|
|
|
|
|
|
|
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_NUMBER,
|
|
|
|
ngx_string("videocodecid"),
|
|
|
|
&v.video_codec_id, 0 },
|
|
|
|
|
|
|
|
{ NGX_RTMP_AMF_NUMBER,
|
|
|
|
ngx_string("audiodatarate"),
|
|
|
|
&v.audio_data_rate, 0 },
|
|
|
|
|
|
|
|
{ NGX_RTMP_AMF_NUMBER,
|
|
|
|
ngx_string("audiocodecid"),
|
|
|
|
&v.audio_codec_id, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
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) },
|
|
|
|
};
|
|
|
|
|
2012-05-21 17:35:11 +02:00
|
|
|
|
2012-05-12 15:47:39 +02:00
|
|
|
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));
|
2012-05-12 17:03:41 +02:00
|
|
|
|
|
|
|
/* use -1 as a sign of unchanged data;
|
|
|
|
* 0 is a valid value for uncompressed audio */
|
|
|
|
v.audio_codec_id = -1;
|
|
|
|
|
2012-05-21 17:35:11 +02:00
|
|
|
if (ngx_rtmp_receive_amf(s, in, in_elts + skip,
|
|
|
|
sizeof(in_elts) / sizeof(in_elts[0]) - skip))
|
2012-05-12 15:47:39 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
meta->audio_data_rate = v.audio_data_rate;
|
2012-05-12 17:03:41 +02:00
|
|
|
meta->audio_codec_id = (v.audio_codec_id == -1
|
|
|
|
? 0 : v.audio_codec_id == 0
|
|
|
|
? NGX_RTMP_AUDIO_UNCOMPRESSED : v.audio_codec_id);
|
2012-05-12 15:47:39 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-21 17:35:11 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-14 07:06:47 +01:00
|
|
|
static ngx_int_t
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_live_postconfiguration(ngx_conf_t *cf)
|
2012-03-14 07:06:47 +01:00
|
|
|
{
|
|
|
|
ngx_rtmp_core_main_conf_t *cmcf;
|
2012-03-21 16:08:59 +01:00
|
|
|
ngx_rtmp_handler_pt *h;
|
2012-05-12 15:47:39 +02:00
|
|
|
ngx_rtmp_amf_handler_t *ch;
|
2012-03-14 07:06:47 +01:00
|
|
|
|
|
|
|
cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module);
|
|
|
|
|
2012-05-21 17:35:11 +02:00
|
|
|
/* register metadata handler */
|
2012-05-12 15:47:39 +02:00
|
|
|
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;
|
|
|
|
|
2012-05-21 17:35:11 +02:00
|
|
|
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;
|
|
|
|
|
2012-05-12 15:47:39 +02:00
|
|
|
/* register raw event handlers */
|
2012-03-21 16:08:59 +01:00
|
|
|
h = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_AUDIO]);
|
2012-03-23 13:39:28 +01:00
|
|
|
*h = ngx_rtmp_live_av;
|
2012-03-13 06:41:51 +01:00
|
|
|
|
2012-03-21 16:08:59 +01:00
|
|
|
h = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_VIDEO]);
|
2012-03-23 13:39:28 +01:00
|
|
|
*h = ngx_rtmp_live_av;
|
2012-03-13 06:41:51 +01:00
|
|
|
|
2012-03-27 18:26:43 +02:00
|
|
|
/* chain handlers */
|
|
|
|
next_publish = ngx_rtmp_publish;
|
|
|
|
ngx_rtmp_publish = ngx_rtmp_live_publish;
|
2012-03-14 07:06:47 +01:00
|
|
|
|
2012-03-27 18:26:43 +02:00
|
|
|
next_play = ngx_rtmp_play;
|
|
|
|
ngx_rtmp_play = ngx_rtmp_live_play;
|
2012-03-23 18:29:22 +01:00
|
|
|
|
2012-03-27 18:26:43 +02:00
|
|
|
next_delete_stream = ngx_rtmp_delete_stream;
|
|
|
|
ngx_rtmp_delete_stream = ngx_rtmp_live_delete_stream;
|
2012-03-12 18:58:00 +01:00
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|