2012-03-12 18:58:00 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Roman Arutyunyan
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include "ngx_rtmp.h"
|
2012-03-23 18:29:22 +01:00
|
|
|
#include "ngx_rtmp_cmd_module.h"
|
2012-03-12 18:58:00 +01:00
|
|
|
|
|
|
|
|
2012-03-27 18:26:43 +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-23 18:29:22 +01:00
|
|
|
/* Chunk stream ids for output */
|
2012-03-23 13:39:28 +01:00
|
|
|
#define NGX_RTMP_LIVE_CSID_AUDIO 6
|
|
|
|
#define NGX_RTMP_LIVE_CSID_VIDEO 7
|
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-03-23 13:39:28 +01:00
|
|
|
typedef struct ngx_rtmp_live_ctx_s ngx_rtmp_live_ctx_t;
|
2012-03-23 13:03:32 +01:00
|
|
|
|
|
|
|
|
2012-03-12 18:58:00 +01:00
|
|
|
typedef struct {
|
2012-03-23 13:03:32 +01:00
|
|
|
ngx_int_t nbuckets;
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_live_ctx_t **contexts;
|
2012-03-23 13:03:32 +01:00
|
|
|
ngx_flag_t live;
|
|
|
|
ngx_flag_t wait_key_frame;
|
2012-03-23 13:39:28 +01:00
|
|
|
} ngx_rtmp_live_app_conf_t;
|
2012-03-12 18:58:00 +01:00
|
|
|
|
|
|
|
|
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 },
|
|
|
|
|
|
|
|
{ ngx_string("wait_key_frame"),
|
|
|
|
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_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, wait_key_frame),
|
2012-03-12 18:58:00 +01:00
|
|
|
NULL },
|
|
|
|
|
2012-03-23 13:03:32 +01:00
|
|
|
|
2012-03-12 18:58:00 +01:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-03-21 16:08:59 +01:00
|
|
|
/* session flags */
|
2012-03-23 13:39:28 +01:00
|
|
|
#define NGX_RTMP_LIVE_PUBLISHING 0x01
|
|
|
|
#define NGX_RTMP_LIVE_PLAYING 0x02
|
|
|
|
#define NGX_RTMP_LIVE_KEYFRAME 0x04
|
2012-03-12 18:58:00 +01:00
|
|
|
|
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
struct ngx_rtmp_live_ctx_s {
|
2012-03-12 18:58:00 +01:00
|
|
|
ngx_str_t stream;
|
|
|
|
ngx_rtmp_session_t *session;
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_live_ctx_t *next;
|
2012-03-18 14:09:19 +01:00
|
|
|
ngx_uint_t flags;
|
2012-03-12 18:58:00 +01:00
|
|
|
uint32_t csid;
|
2012-03-16 19:09:04 +01:00
|
|
|
ngx_chain_t *data_frame;
|
2012-03-23 13:03:32 +01:00
|
|
|
};
|
2012-03-12 18:58:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
lacf->wait_key_frame = 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);
|
|
|
|
ngx_conf_merge_value(conf->wait_key_frame, prev->wait_key_frame, 1);
|
2012-03-12 18:58:00 +01:00
|
|
|
|
|
|
|
conf->contexts = ngx_pcalloc(cf->pool,
|
2012-03-23 13:39:28 +01:00
|
|
|
sizeof(ngx_rtmp_live_ctx_t *) * conf->nbuckets);
|
2012-03-12 18:58:00 +01:00
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
static ngx_rtmp_live_ctx_t **
|
|
|
|
ngx_rtmp_live_get_head(ngx_rtmp_session_t *s)
|
2012-03-12 18:58:00 +01:00
|
|
|
{
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_live_app_conf_t *lacf;
|
|
|
|
ngx_rtmp_live_ctx_t *ctx;
|
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);
|
2012-03-12 18:58:00 +01:00
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
if (lacf == NULL) {
|
2012-03-23 13:03:32 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-03-14 07:06:47 +01:00
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module);
|
2012-03-14 07:06:47 +01:00
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
return &lacf->contexts[
|
2012-03-23 13:03:32 +01:00
|
|
|
ngx_hash_key(ctx->stream.data, ctx->stream.len)
|
2012-03-23 13:39:28 +01:00
|
|
|
% lacf->nbuckets];
|
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)
|
|
|
|
{
|
|
|
|
ngx_connection_t *c;
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_live_ctx_t *ctx, **hctx;
|
2012-03-12 18:58:00 +01:00
|
|
|
|
|
|
|
c = s->connection;
|
|
|
|
|
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-03-23 13:39:28 +01:00
|
|
|
ctx = ngx_pcalloc(c->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
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->stream.len) {
|
2012-03-23 13:03:32 +01:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, "live: already joined");
|
2012-03-12 18:58:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, c->log, 0,
|
2012-03-27 18:26:43 +02:00
|
|
|
"live: join '%s'", name);
|
|
|
|
|
|
|
|
ctx->stream.len = ngx_strlen(name);
|
|
|
|
ctx->stream.data = ngx_palloc(s->connection->pool,
|
|
|
|
ctx->stream.len);
|
|
|
|
ngx_memcpy(ctx->stream.data, name, ctx->stream.len);
|
2012-03-12 18:58:00 +01:00
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
hctx = ngx_rtmp_live_get_head(s);
|
2012-03-23 13:03:32 +01:00
|
|
|
if (hctx == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2012-03-12 18:58:00 +01:00
|
|
|
ctx->next = *hctx;
|
|
|
|
ctx->flags = flags;
|
|
|
|
*hctx = ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
ngx_connection_t *c;
|
2012-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_live_ctx_t *ctx, **hctx;
|
2012-03-12 18:58:00 +01:00
|
|
|
|
|
|
|
c = s->connection;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, c->log, 0,
|
2012-03-23 13:03:32 +01:00
|
|
|
"live: leave '%V'", &ctx->stream);
|
2012-03-12 18:58:00 +01:00
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
hctx = ngx_rtmp_live_get_head(s);
|
2012-03-23 13:03:32 +01:00
|
|
|
if (hctx == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2012-03-12 18:58:00 +01:00
|
|
|
ngx_str_null(&ctx->stream);
|
|
|
|
|
|
|
|
for(; *hctx; hctx = &(*hctx)->next) {
|
|
|
|
if (*hctx == ctx) {
|
|
|
|
*hctx = (*hctx)->next;
|
2012-03-13 06:41:51 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-27 18:26:43 +02:00
|
|
|
next:
|
|
|
|
return next_delete_stream(s, v);
|
2012-03-13 06:41:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-15 17:43:03 +01:00
|
|
|
#define NGX_RTMP_VIDEO_KEY_FRAME 1
|
|
|
|
#define NGX_RTMP_VIDEO_INTER_FRAME 2
|
|
|
|
#define NGX_RTMP_VIDEO_DISPOSABLE_FRAME 3
|
2012-03-18 14:09:19 +01:00
|
|
|
#define NGX_RTMP_AUDIO_FRAME NGX_RTMP_VIDEO_KEY_FRAME
|
2012-03-15 17:43:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_rtmp_get_video_frame_type(ngx_chain_t *in)
|
|
|
|
{
|
|
|
|
return (in->buf->pos[0] & 0xf0) >> 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-03-23 13:39:28 +01:00
|
|
|
ngx_rtmp_live_ctx_t *ctx, *cctx, *cnext;
|
2012-03-16 19:09:04 +01:00
|
|
|
ngx_chain_t *out;
|
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-03-20 00:52:39 +01:00
|
|
|
ngx_rtmp_header_t sh;
|
2012-03-18 14:09:19 +01:00
|
|
|
ngx_uint_t priority;
|
|
|
|
int keyframe;
|
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);
|
2012-03-23 13:03:32 +01:00
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
if (lacf == NULL) {
|
2012-03-23 13:03:32 +01:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0,
|
|
|
|
"live: NULL application");
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
if (!lacf->live) {
|
2012-03-23 13:03:32 +01:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module);
|
2012-03-23 13:03:32 +01:00
|
|
|
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
|
2012-03-13 06:41:51 +01:00
|
|
|
|
2012-03-20 00:52:39 +01:00
|
|
|
memset(&sh, 0, sizeof(sh));
|
2012-03-23 13:39:28 +01:00
|
|
|
sh.timestamp = (h->timestamp + s->epoch);
|
2012-03-23 13:03:32 +01:00
|
|
|
sh.msid = 1;
|
2012-03-20 00:52:39 +01:00
|
|
|
sh.type = h->type;
|
|
|
|
|
|
|
|
ngx_log_debug4(NGX_LOG_DEBUG_RTMP, c->log, 0,
|
2012-03-23 13:03:32 +01:00
|
|
|
"live: av: peer_epoch=%uD; my_epoch=%uD timestamp=%uD; r=%uD",
|
2012-03-20 00:52:39 +01:00
|
|
|
s->peer_epoch, s->epoch, h->timestamp, sh.timestamp);
|
|
|
|
|
2012-03-18 14:09:19 +01:00
|
|
|
keyframe = 0;
|
|
|
|
if (h->type == NGX_RTMP_MSG_VIDEO) {
|
2012-03-23 13:39:28 +01:00
|
|
|
sh.csid = NGX_RTMP_LIVE_CSID_VIDEO;
|
2012-03-18 14:09:19 +01:00
|
|
|
priority = ngx_rtmp_get_video_frame_type(in);
|
|
|
|
if (priority == NGX_RTMP_VIDEO_KEY_FRAME) {
|
|
|
|
keyframe = 1;
|
|
|
|
}
|
|
|
|
|
2012-03-20 00:52:39 +01:00
|
|
|
} else if (h->type == NGX_RTMP_MSG_AUDIO) {
|
2012-03-23 13:39:28 +01:00
|
|
|
sh.csid = NGX_RTMP_LIVE_CSID_AUDIO;
|
2012-03-18 14:09:19 +01:00
|
|
|
priority = NGX_RTMP_AUDIO_FRAME;
|
2012-03-20 00:52:39 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
return NGX_OK;
|
2012-03-18 14:09:19 +01:00
|
|
|
}
|
|
|
|
|
2012-03-14 17:38:15 +01:00
|
|
|
if (ctx == NULL
|
2012-03-23 13:39:28 +01:00
|
|
|
|| !(ctx->flags & NGX_RTMP_LIVE_PUBLISHING))
|
2012-03-14 17:38:15 +01:00
|
|
|
{
|
2012-03-13 06:41:51 +01:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0,
|
2012-03-23 13:03:32 +01:00
|
|
|
"live: received audio/video from non-publisher");
|
2012-04-04 15:01:35 +02:00
|
|
|
return NGX_OK;
|
2012-03-13 06:41:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (in == NULL || in->buf == NULL) {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-17 07:12:19 +01:00
|
|
|
out = ngx_rtmp_append_shared_bufs(cscf, NULL, in);
|
2012-03-13 06:41:51 +01:00
|
|
|
|
2012-03-18 14:09:19 +01:00
|
|
|
ngx_rtmp_prepare_message(s, &sh, NULL, out);
|
2012-03-13 06:41:51 +01:00
|
|
|
|
2012-03-23 13:39:28 +01:00
|
|
|
/* live to all subscribers */
|
|
|
|
for (cctx = *ngx_rtmp_live_get_head(s); cctx; cctx = cnext) {
|
2012-03-20 00:52:39 +01:00
|
|
|
/* session can die further in loop
|
|
|
|
* so save next ptr while it's not too late */
|
|
|
|
cnext = cctx->next;
|
|
|
|
|
2012-03-21 16:08:59 +01:00
|
|
|
if (cctx == ctx
|
2012-03-23 13:39:28 +01:00
|
|
|
|| !(cctx->flags & NGX_RTMP_LIVE_PLAYING)
|
2012-03-21 16:08:59 +01:00
|
|
|
|| cctx->stream.len != ctx->stream.len
|
|
|
|
|| ngx_strncmp(cctx->stream.data, ctx->stream.data,
|
2012-03-13 06:41:51 +01:00
|
|
|
ctx->stream.len))
|
|
|
|
{
|
2012-03-21 16:08:59 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ss = cctx->session;
|
2012-03-16 19:09:04 +01:00
|
|
|
|
2012-03-21 16:08:59 +01:00
|
|
|
/* waiting for a keyframe? */
|
2012-03-23 13:39:28 +01:00
|
|
|
if (lacf->wait_key_frame
|
2012-03-21 16:08:59 +01:00
|
|
|
&& sh.type == NGX_RTMP_MSG_VIDEO
|
2012-03-23 13:39:28 +01:00
|
|
|
&& !(cctx->flags & NGX_RTMP_LIVE_KEYFRAME)
|
2012-03-21 16:08:59 +01:00
|
|
|
&& !keyframe)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_rtmp_send_message(ss, out, priority) == NGX_OK
|
|
|
|
&& keyframe
|
2012-03-23 13:39:28 +01:00
|
|
|
&& !(cctx->flags & NGX_RTMP_LIVE_KEYFRAME))
|
2012-03-21 16:08:59 +01:00
|
|
|
{
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
|
2012-03-23 13:03:32 +01:00
|
|
|
"live: keyframe sent");
|
2012-03-23 13:39:28 +01:00
|
|
|
cctx->flags |= NGX_RTMP_LIVE_KEYFRAME;
|
2012-03-21 16:08:59 +01:00
|
|
|
}
|
2012-03-13 06:41:51 +01:00
|
|
|
}
|
|
|
|
|
2012-03-17 07:12:19 +01:00
|
|
|
ngx_rtmp_free_shared_bufs(cscf, out);
|
2012-03-14 07:06:47 +01:00
|
|
|
|
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-03-27 18:26:43 +02:00
|
|
|
ngx_rtmp_live_join(s, v->name, NGX_RTMP_LIVE_PLAYING);
|
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-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-03-14 07:06:47 +01:00
|
|
|
|
2012-03-23 18:29:22 +01:00
|
|
|
/* register raw event handlers */
|
2012-03-14 07:06:47 +01:00
|
|
|
cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module);
|
|
|
|
|
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;
|
|
|
|
}
|