implemented broadcast

This commit is contained in:
Roman Arutyunyan
2012-03-13 09:41:51 +04:00
parent fca2c2c8f5
commit 4602695d5c
10 changed files with 277 additions and 187 deletions

View File

@ -60,10 +60,8 @@ ngx_module_t ngx_rtmp_broadcast_module = {
};
#define NGX_RTMP_PUBLISHER 0x01
#define NGX_RTMP_SUBSCRIBER 0x02
#define NGX_RTMP_SESSION_HASH_SIZE 16384
#define NGX_RTMP_BROADCAST_PUBLISHER 0x01
#define NGX_RTMP_BROADCAST_SUBSCRIBER 0x02
typedef struct ngx_rtmp_broadcast_ctx_s {
@ -107,11 +105,13 @@ ngx_rtmp_broadcast_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
static ngx_rtmp_broadcast_ctx_t **
ngx_rtmp_broadcast_get_head(ngx_rtmp_broadcast_ctx_t *ctx)
ngx_rtmp_broadcast_get_head(ngx_rtmp_session_t *s)
{
ngx_rtmp_broadcast_srv_conf_t *bscf;
ngx_rtmp_broadcast_ctx_t *ctx;
bscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_broadcast_module);
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_broadcast_module);
return &bscf->contexts[
ngx_hash_key(ctx->stream.data, ctx->stream.len)
@ -143,15 +143,15 @@ ngx_rtmp_broadcast_join(ngx_rtmp_session_t *s, ngx_str_t *stream,
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, c->log, 0,
"join broadcast stream '%V'", &stream);
s->stream = *stream;
hctx = ngx_rtmp_broadcast_get_head(ctx);
ctx->stream = *stream;
hctx = ngx_rtmp_broadcast_get_head(s);
ctx->next = *hctx;
ctx->flags = flags;
*hctx = ctx;
}
static void
static ngx_int_t
ngx_rtmp_broadcast_leave(ngx_rtmp_session_t *s)
{
ngx_connection_t *c;
@ -161,34 +161,107 @@ ngx_rtmp_broadcast_leave(ngx_rtmp_session_t *s)
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_broadcast_module);
if (ctx == NULL || !ctx->stream.len) {
return;
return NGX_OK;
}
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, c->log, 0,
"leave broadcast stream '%V'", &s->stream);
"leave broadcast stream '%V'", &ctx->stream);
hctx = ngx_rtmp_broadcast_get_head(ctx);
hctx = ngx_rtmp_broadcast_get_head(s);
ngx_str_null(&ctx->stream);
for(; *hctx; hctx = &(*hctx)->next) {
if (*hctx == ctx) {
*hctx = (*hctx)->next;
return;
break;
}
}
return NGX_OK;
}
static ngx_rtmp_broadcast_connect(ngx_rtmp_session_t *s, double in_trans,
static ngx_int_t
ngx_rtmp_broadcast_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in)
{
ngx_connection_t *c;
ngx_rtmp_broadcast_ctx_t *ctx, *cctx;
ngx_chain_t *out, *l;
u_char *p;
c = s->connection;
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_broadcast_module);
if (ctx == NULL || !(ctx->flags & NGX_RTMP_BROADCAST_PUBLISHER)) {
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0,
"received audio/video from non-publisher");
return NGX_ERROR;
}
if (in == NULL || in->buf == NULL) {
return NGX_OK;
}
/* copy data to output stream */
out = NULL;
p = in->buf->pos;
for(;;) {
l = ngx_rtmp_alloc_shared_buf(s);
if (l == NULL || l->buf == NULL) {
return NGX_ERROR;
}
if (out == NULL) {
out = l;
}
while (l->buf->end - l->buf->last > in->buf->last - p) {
l->buf->last = ngx_cpymem(l->buf->last, p,
in->buf->last - p);
in = in->next;
p = in->buf->pos;
}
l->buf->last = ngx_cpymem(l->buf->last, p,
l->buf->end - l->buf->last);
p += (l->buf->end - l->buf->last);
}
ngx_rtmp_prepare_message(h, out, 0/*fmt*/);
/* broadcast to all subscribers */
for(cctx = *ngx_rtmp_broadcast_get_head(s);
cctx; cctx = cctx->next)
{
if (cctx != ctx
&& cctx->flags & NGX_RTMP_BROADCAST_SUBSCRIBER
&& cctx->stream.len == ctx->stream.len
&& !ngx_strncmp(cctx->stream.data, ctx->stream.data,
ctx->stream.len))
{
ngx_rtmp_send_message(s, out);
}
}
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_broadcast_connect(ngx_rtmp_session_t *s, double in_trans,
ngx_chain_t *in)
{
static double trans;
static u_char app[1024];
static u_char url[1024];
static ngx_str_t app_str;
static ngx_rtmp_amf0_elt_t in_cmd[] = {
{ NGX_RTMP_AMF0_STRING, "app", app, sizeof(app) },
{ NGX_RTMP_AMF0_STRING, "pageUrl", url, sizeof(utl) },
{ NGX_RTMP_AMF0_STRING, "pageUrl", url, sizeof(url) },
};
static ngx_rtmp_amf0_elt_t out_inf[] = {
@ -216,15 +289,19 @@ static ngx_rtmp_broadcast_connect(ngx_rtmp_session_t *s, double in_trans,
}
trans = in_trans;
ngx_str_set(&inf[0], "NetConnection.Connect.Success");
ngx_str_set(&inf[1], "status");
ngx_str_set(&inf[2], "Connection succeeded.");
ngx_str_set(&out_inf[0], "NetConnection.Connect.Success");
ngx_str_set(&out_inf[1], "status");
ngx_str_set(&out_inf[2], "Connection succeeded.");
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, c->log, 0,
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"connect() called; app='%s' url='%s'",
app, url);
/*ngx_rtmp_broadcast_join(s, app);*/
if (0) {
app_str.data = app;
app_str.len = ngx_strlen(app);
ngx_rtmp_broadcast_join(s, &app_str, 0);
}
return ngx_rtmp_send_ack_size(s, 65536)
|| ngx_rtmp_send_bandwidth(s, 65536, NGX_RTMP_LIMIT_SOFT)
@ -242,8 +319,10 @@ ngx_rtmp_broadcast_postconfiguration(ngx_conf_t *cf)
ngx_rtmp_core_main_conf_t *cmcf;
ngx_hash_key_t *h;
ngx_rtmp_disconnect_handler_pt *dh;
ngx_rtmp_event_handler_pt *avh;
cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module);
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_rtmp_module);
/* add connect() handler */
h = ngx_array_push(&cmcf->calls);
@ -255,6 +334,15 @@ ngx_rtmp_broadcast_postconfiguration(ngx_conf_t *cf)
ngx_str_set(&h->key, "connect");
h->value = ngx_rtmp_broadcast_connect;
/* register audio/video broadcast handler */
avh = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_AUDIO]);
*avh = ngx_rtmp_broadcast_av;
avh = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_VIDEO]);
*avh = ngx_rtmp_broadcast_av;
/* add disconnect handler */
dh = ngx_array_push(&cmcf->disconnect);