nginx-mod-rtmp/ngx_rtmp_live_module.c

715 lines
20 KiB
C
Raw Normal View History

/*
* Copyright (c) 2012 Roman Arutyunyan
*/
2012-05-07 13:41:03 +02:00
#include "ngx_rtmp_live_module.h"
#include "ngx_rtmp_cmd_module.h"
#include "ngx_rtmp_codec_module.h"
2012-04-20 16:03:00 +02:00
static ngx_rtmp_publish_pt next_publish;
static ngx_rtmp_play_pt next_play;
2012-06-25 14:22:42 +02:00
static ngx_rtmp_close_stream_pt next_close_stream;
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);
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
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,
offsetof(ngx_rtmp_live_app_conf_t, live),
2012-03-23 13:03:32 +01:00
NULL },
{ ngx_string("meta"),
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, meta),
NULL },
2012-03-23 13:03:32 +01:00
{ ngx_string("stream_buckets"),
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
2012-03-23 13:03:32 +01:00
NGX_RTMP_APP_CONF_OFFSET,
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"),
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),
NULL },
2012-08-30 16:40:12 +02:00
{ ngx_string("sync"),
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, sync),
NULL },
{ ngx_string("atc"),
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, atc),
NULL },
2012-10-09 22:27:44 +02:00
{ ngx_string("interleave"),
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, interleave),
NULL },
2012-10-27 18:47:35 +02:00
{ ngx_string("wait_key"),
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, wait_key),
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 */
2012-03-23 13:03:32 +01:00
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->meta = NGX_CONF_UNSET;
lacf->nbuckets = NGX_CONF_UNSET;
2012-04-20 07:01:04 +02:00
lacf->buflen = NGX_CONF_UNSET;
2012-08-30 16:40:12 +02:00
lacf->sync = NGX_CONF_UNSET;
lacf->atc = NGX_CONF_UNSET;
2012-10-09 22:27:44 +02:00
lacf->interleave = NGX_CONF_UNSET;
2012-10-27 18:47:35 +02:00
lacf->wait_key = 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;
2012-03-23 13:03:32 +01:00
ngx_conf_merge_value(conf->live, prev->live, 0);
ngx_conf_merge_value(conf->meta, prev->meta, 1);
2012-03-23 13:03:32 +01:00
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-08-30 16:40:12 +02:00
ngx_conf_merge_msec_value(conf->sync, prev->sync, 0);
ngx_conf_merge_value(conf->atc, prev->atc, 0);
2012-10-09 22:27:44 +02:00
ngx_conf_merge_value(conf->interleave, prev->interleave, 0);
2012-10-27 18:47:35 +02:00
ngx_conf_merge_value(conf->wait_key, prev->wait_key, 0);
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);
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-04-20 16:03:00 +02:00
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) {
2012-03-23 13:03:32 +01:00
return NULL;
}
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-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));
(*stream)->epoch = ngx_current_msec;
2012-04-20 16:03:00 +02:00
return stream;
}
static void
ngx_rtmp_live_join(ngx_rtmp_session_t *s, u_char *name,
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-04-20 16:03:00 +02:00
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);
2012-10-25 16:17:08 +02:00
if (ctx && ctx->stream) {
2012-04-20 16:03:00 +02:00
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log,
0, "live: already joined");
return;
}
2012-10-25 16:17:08 +02:00
if (ctx == NULL) {
ctx = ngx_palloc(s->connection->pool, sizeof(ngx_rtmp_live_ctx_t));
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_live_module);
}
ngx_memzero(ctx, sizeof(*ctx));
ctx->session = s;
2012-04-20 16:03:00 +02:00
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"live: join '%s'", name);
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;
}
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;
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-10-27 15:39:04 +02:00
ctx->cs[0].csid = NGX_RTMP_MSG_AUDIO;
ctx->cs[1].csid = NGX_RTMP_MSG_VIDEO;
}
2012-03-13 06:41:51 +01:00
static ngx_int_t
2012-06-25 14:22:42 +02:00
ngx_rtmp_live_close_stream(ngx_rtmp_session_t *s, ngx_rtmp_close_stream_t *v)
{
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-04-20 16:03:00 +02:00
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);
2012-03-21 16:08:59 +01:00
if (ctx == NULL) {
goto next;
}
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-04-20 16:03:00 +02:00
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;
}
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-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-20 16:03:00 +02:00
stream = ngx_rtmp_live_get_stream(s, ctx->stream->name, 0);
if (stream == NULL) {
return NGX_ERROR;
}
2012-04-20 16:03:00 +02:00
*stream = (*stream)->next;
2012-04-20 16:03:00 +02:00
ctx->stream->next = lacf->free_streams;
lacf->free_streams = ctx->stream;
ctx->stream = NULL;
2012-04-20 16:03:00 +02:00
next:
2012-06-25 14:22:42 +02:00
return next_close_stream(s, v);
}
2012-10-27 15:39:04 +02:00
2012-03-13 06:41:51 +01:00
static ngx_int_t
ngx_rtmp_live_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
2012-10-27 18:47:35 +02:00
ngx_chain_t *in)
2012-03-13 06:41:51 +01:00
{
2012-04-20 07:01:04 +02:00
ngx_rtmp_live_ctx_t *ctx, *pctx;
ngx_rtmp_codec_ctx_t *codec_ctx;
2012-10-27 20:43:23 +02:00
ngx_chain_t *header, *meta,
*apkt, *rpkt,
*aheader, *rheader;
ngx_rtmp_core_srv_conf_t *cscf;
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;
2012-10-27 20:43:23 +02:00
ngx_int_t rc, sync;
2012-10-27 15:39:04 +02:00
ngx_uint_t prio;
ngx_uint_t peers;
2012-07-02 11:42:56 +02:00
ngx_uint_t header_version, meta_version;
2012-10-27 15:39:04 +02:00
ngx_uint_t csidx, hvidx;
uint32_t timestamp, delta;
ngx_rtmp_live_chunk_stream_t *cs;
#ifdef NGX_DEBUG
const char *type_s;
type_s = (h->type == NGX_RTMP_MSG_VIDEO ? "video" : "audio");
#endif
2012-03-13 06:41:51 +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 NGX_ERROR;
}
2012-10-27 18:47:35 +02:00
if (!lacf->live || in == NULL || in->buf == NULL) {
return NGX_OK;
}
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module);
2012-10-27 18:47:35 +02:00
if (ctx == NULL || ctx->stream == NULL) {
return NGX_OK;
}
2012-04-20 16:03:00 +02:00
if ((ctx->flags & NGX_RTMP_LIVE_PUBLISHING) == 0) {
2012-10-27 15:39:04 +02:00
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"live: %s from non-publisher", type_s);
2012-04-20 16:03:00 +02:00
return NGX_OK;
}
ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
2012-10-27 15:39:04 +02:00
"live: %s packet timestamp=%uD timeshift=%uD",
type_s, h->timestamp, h->timeshift);
2012-04-20 16:03:00 +02:00
2012-10-27 20:43:23 +02:00
sync = 0;
2012-10-27 18:47:35 +02:00
peers = 0;
2012-10-27 20:43:23 +02:00
aheader = NULL;
rheader = NULL;
apkt = NULL;
2012-10-28 12:10:12 +01:00
header = NULL;
2012-10-27 18:47:35 +02:00
header_version = 0;
meta = NULL;
meta_version = 0;
2012-10-27 20:43:23 +02:00
prio = (h->type == NGX_RTMP_MSG_VIDEO ?
ngx_rtmp_get_video_frame_type(in) : 0);
2012-04-20 16:03:00 +02:00
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
2012-10-27 15:39:04 +02:00
csidx = !(lacf->interleave || h->type == NGX_RTMP_MSG_VIDEO);
2012-10-27 18:47:35 +02:00
2012-10-27 15:39:04 +02:00
hvidx = (h->type == NGX_RTMP_MSG_VIDEO);
2012-10-27 18:47:35 +02:00
cs = &ctx->cs[csidx];
ngx_memzero(&ch, sizeof(ch));
2012-09-30 16:17:30 +02:00
timestamp = h->timestamp;
if (lacf->atc == 0) {
timestamp += h->timeshift;
}
2012-09-30 16:17:30 +02:00
ch.timestamp = timestamp;
2012-10-20 15:40:21 +02:00
ch.msid = NGX_RTMP_MSID;
2012-10-27 15:39:04 +02:00
ch.csid = cs->csid;
2012-04-20 16:03:00 +02:00
ch.type = h->type;
2012-10-09 22:27:44 +02:00
2012-10-27 15:39:04 +02:00
lh = ch;
2012-10-09 22:27:44 +02:00
2012-10-27 15:39:04 +02:00
if (cs->active) {
lh.timestamp = cs->timestamp;
2012-10-27 20:43:23 +02:00
} else {
sync = 1;
2012-03-13 06:41:51 +01:00
}
2012-10-09 22:27:44 +02:00
2012-10-27 15:39:04 +02:00
cs->active = 1;
cs->timestamp = ch.timestamp;
2012-10-25 16:17:08 +02:00
2012-10-27 15:39:04 +02:00
delta = ch.timestamp - lh.timestamp;
2012-03-13 06:41:51 +01:00
2012-10-27 20:43:23 +02:00
rpkt = ngx_rtmp_append_shared_bufs(cscf, NULL, in);
2012-03-13 06:41:51 +01:00
2012-10-27 20:43:23 +02:00
ngx_rtmp_prepare_message(s, &ch, &lh, rpkt);
2012-10-27 15:39:04 +02:00
codec_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module);
if (codec_ctx) {
2012-10-27 18:47:35 +02:00
if (h->type == NGX_RTMP_MSG_AUDIO) {
2012-10-27 18:47:35 +02:00
if (codec_ctx->aac_header) {
2012-10-27 15:39:04 +02:00
header = codec_ctx->aac_header;
header_version = codec_ctx->aac_version;
}
2012-10-27 18:47:35 +02:00
} else {
2012-10-27 18:47:35 +02:00
if (codec_ctx->avc_header) {
2012-10-27 15:39:04 +02:00
header = codec_ctx->avc_header;
header_version = codec_ctx->avc_version;
}
}
2012-10-27 18:47:35 +02:00
if (lacf->meta && codec_ctx->meta) {
2012-07-02 11:42:56 +02:00
meta = codec_ctx->meta;
meta_version = codec_ctx->meta_version;
}
}
2012-04-20 07:01:04 +02:00
/* broadcast to all subscribers */
2012-10-27 18:47:35 +02:00
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-10-27 15:39:04 +02:00
ss = pctx->session;
2012-10-27 20:43:23 +02:00
cs = &pctx->cs[csidx];
2012-10-27 20:43:23 +02:00
/* send metadata */
2012-10-27 18:47:35 +02:00
2012-10-27 15:39:04 +02:00
if (meta && meta_version != pctx->meta_version) {
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0,
2012-10-27 20:43:23 +02:00
"live: meta");
2012-10-27 15:39:04 +02:00
if (ngx_rtmp_send_message(ss, meta, 0) == NGX_OK) {
pctx->meta_version = meta_version;
}
}
2012-10-27 20:43:23 +02:00
/* sync stream */
2012-10-28 12:10:12 +01:00
if (cs->active && (sync || (lacf->sync && cs->dropped > lacf->sync))) {
2012-10-27 20:43:23 +02:00
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0,
2012-10-28 12:10:12 +01:00
"live: sync %s dropped=%uD", type_s, cs->dropped);
2012-10-27 20:43:23 +02:00
cs->active = 0;
2012-10-28 12:10:12 +01:00
cs->dropped = 0;
2012-10-27 20:43:23 +02:00
}
/* absolute packet */
2012-10-27 18:47:35 +02:00
2012-10-27 15:39:04 +02:00
if (!cs->active) {
2012-08-30 16:40:12 +02:00
2012-10-28 12:10:12 +01:00
if (lacf->wait_key && prio != NGX_RTMP_VIDEO_KEY_FRAME &&
(lacf->interleave || h->type == NGX_RTMP_MSG_VIDEO))
2012-10-27 18:47:35 +02:00
{
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0,
2012-10-27 20:43:23 +02:00
"live: skip non-key");
2012-08-30 16:40:12 +02:00
continue;
2012-10-28 12:10:12 +01:00
}
ch.timestamp = timestamp;
if (lacf->atc == 0) {
ch.timestamp -= (uint32_t) ss->epoch;
}
lh.timestamp = ch.timestamp - delta;
/*
if (ngx_rtmp_send_user_stream_eof(ss, NGX_RTMP_MSID) != NGX_OK) {
continue;
2012-08-30 16:40:12 +02:00
}
2012-10-27 15:39:04 +02:00
2012-10-28 12:10:12 +01:00
if (ngx_rtmp_send_user_stream_begin(ss, NGX_RTMP_MSID) != NGX_OK) {
continue;
}*/
2012-10-27 15:39:04 +02:00
if (header) {
2012-10-27 18:47:35 +02:00
/* send absolute codec header */
2012-10-27 20:43:23 +02:00
if (lacf->atc == 0 && (int32_t) lh.timestamp < 0) {
2012-10-27 18:47:35 +02:00
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0,
"live: abs %s header from the past", type_s);
continue;
}
2012-10-27 15:39:04 +02:00
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0,
"live: abs %s header timestamp=%uD",
type_s, lh.timestamp);
2012-10-27 20:43:23 +02:00
if (aheader == NULL) {
aheader = ngx_rtmp_append_shared_bufs(cscf, NULL, header);
ngx_rtmp_prepare_message(s, &lh, NULL, aheader);
}
2012-10-27 15:39:04 +02:00
2012-10-27 20:43:23 +02:00
rc = ngx_rtmp_send_message(ss, aheader, 0);
2012-08-30 16:40:12 +02:00
2012-10-27 20:43:23 +02:00
if (!lacf->atc) {
ngx_rtmp_free_shared_chain(cscf, aheader);
aheader = NULL;
}
2012-10-27 18:47:35 +02:00
if (rc != NGX_OK) {
continue;
}
cs->timestamp = lh.timestamp;
cs->active = 1;
pctx->header_versions[hvidx] = header_version;
2012-10-27 15:39:04 +02:00
} else {
2012-10-27 20:43:23 +02:00
2012-10-27 18:47:35 +02:00
/* send absolute packet */
2012-10-27 20:43:23 +02:00
if (lacf->atc == 0 && (int32_t) ch.timestamp < 0) {
2012-10-27 18:47:35 +02:00
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0,
"live: abs %s packet from the past", type_s);
continue;
}
2012-10-27 15:39:04 +02:00
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0,
2012-10-27 20:43:23 +02:00
"live: abs %s packet timestamp=%uD",
2012-10-27 15:39:04 +02:00
type_s, ch.timestamp);
2012-10-27 20:43:23 +02:00
if (apkt == NULL) {
apkt = ngx_rtmp_append_shared_bufs(cscf, NULL, in);
ngx_rtmp_prepare_message(s, &ch, NULL, apkt);
}
2012-10-27 18:47:35 +02:00
2012-10-27 20:43:23 +02:00
rc = ngx_rtmp_send_message(ss, apkt, prio);
2012-10-27 20:43:23 +02:00
if (!lacf->atc) {
ngx_rtmp_free_shared_chain(cscf, apkt);
apkt = NULL;
}
2012-10-27 18:47:35 +02:00
if (rc != NGX_OK) {
continue;
}
cs->timestamp = ch.timestamp;
cs->active = 1;
++peers;
2012-10-27 15:39:04 +02:00
continue;
2012-07-02 11:42:56 +02:00
}
}
2012-10-27 15:39:04 +02:00
/* send codec header if newer header has arrived */
2012-10-27 18:47:35 +02:00
2012-10-27 15:39:04 +02:00
if (header && pctx->header_versions[hvidx] != header_version) {
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0,
"live: new %s header", type_s);
2012-10-27 20:43:23 +02:00
if (rheader == NULL) {
rheader = ngx_rtmp_append_shared_bufs(cscf, NULL, header);
ngx_rtmp_prepare_message(s, &ch, &ch, rheader);
2012-10-27 15:39:04 +02:00
}
2012-10-27 20:43:23 +02:00
if (ngx_rtmp_send_message(ss, rheader, 0) == NGX_OK) {
2012-10-27 15:39:04 +02:00
pctx->header_versions[hvidx] = header_version;
}
}
2012-10-27 20:43:23 +02:00
/* send relative packet */
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0,
"live: rel %s packet delta=%uD",
type_s, delta);
2012-10-27 18:47:35 +02:00
2012-10-28 12:10:12 +01:00
if (ngx_rtmp_send_message(ss, rpkt, prio) != NGX_OK) {
++pctx->ndropped;
cs->dropped += delta;
continue;
2012-08-30 16:40:12 +02:00
}
2012-10-28 12:10:12 +01:00
cs->timestamp += delta;
++peers;
2012-03-13 06:41:51 +01:00
}
2012-10-27 15:39:04 +02:00
2012-10-27 20:43:23 +02:00
if (rpkt) {
ngx_rtmp_free_shared_chain(cscf, rpkt);
}
if (apkt) {
ngx_rtmp_free_shared_chain(cscf, apkt);
}
if (rheader) {
ngx_rtmp_free_shared_chain(cscf, rheader);
}
2012-10-27 20:43:23 +02:00
if (aheader) {
ngx_rtmp_free_shared_chain(cscf, aheader);
}
2012-05-07 13:41:03 +02:00
ngx_rtmp_update_bandwidth(&ctx->stream->bw_in, h->mlen);
2012-10-27 15:39:04 +02:00
ngx_rtmp_update_bandwidth(&ctx->stream->bw_out, h->mlen * peers);
2012-05-07 13:41:03 +02:00
2012-03-13 06:41:51 +01:00
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);
2012-03-23 13:03:32 +01:00
if (lacf == NULL || !lacf->live) {
goto next;
2012-03-23 13:03:32 +01:00
}
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"live: publish: name='%s' type='%s'",
v->name, v->type);
2012-03-23 13:03:32 +01:00
/* join stream as publisher */
ngx_rtmp_live_join(s, v->name, NGX_RTMP_LIVE_PUBLISHING);
next:
return next_publish(s, v);
}
2012-03-13 06:41:51 +01:00
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);
2012-03-23 13:03:32 +01:00
if (lacf == NULL || !lacf->live) {
goto next;
2012-03-23 13:03:32 +01:00
}
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);
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);
next:
return next_play(s, v);
}
static ngx_int_t
ngx_rtmp_live_postconfiguration(ngx_conf_t *cf)
{
ngx_rtmp_core_main_conf_t *cmcf;
2012-03-21 16:08:59 +01:00
ngx_rtmp_handler_pt *h;
cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module);
/* register raw event handlers */
2012-03-21 16:08:59 +01:00
h = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_AUDIO]);
*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]);
*h = ngx_rtmp_live_av;
2012-03-13 06:41:51 +01:00
/* 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;
2012-06-25 14:22:42 +02:00
next_close_stream = ngx_rtmp_close_stream;
ngx_rtmp_close_stream = ngx_rtmp_live_close_stream;
return NGX_OK;
}