nginx-mod-rtmp/ngx_rtmp_handler.c

1306 lines
33 KiB
C
Raw Normal View History

2012-03-08 16:21:22 +01:00
/*
* Copyright (c) 2012 Roman Arutyunyan
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_event.h>
#include <strings.h>
#include "ngx_rtmp.h"
2012-03-29 14:10:11 +02:00
#include "ngx_rtmp_amf.h"
2012-03-08 16:21:22 +01:00
static void ngx_rtmp_init_session(ngx_connection_t *c);
2012-04-07 23:44:57 +02:00
static void ngx_rtmp_close_connection(ngx_connection_t *c);
2012-03-08 16:21:22 +01:00
static void ngx_rtmp_handshake_recv(ngx_event_t *rev);
static void ngx_rtmp_handshake_send(ngx_event_t *rev);
static void ngx_rtmp_recv(ngx_event_t *rev);
static void ngx_rtmp_send(ngx_event_t *rev);
2012-03-13 06:41:51 +01:00
static ngx_int_t ngx_rtmp_receive_message(ngx_rtmp_session_t *s,
2012-04-05 19:28:41 +02:00
ngx_rtmp_header_t *h, ngx_chain_t *in);
static ngx_int_t ngx_rtmp_finalize_set_chunk_size(ngx_rtmp_session_t *s);
2012-03-08 16:21:22 +01:00
2012-03-15 07:23:49 +01:00
#ifdef NGX_DEBUG
char*
ngx_rtmp_message_type(uint8_t type) {
static char* types[] = {
"?",
"chunk_size",
"abort",
"ack",
"user",
"ack_size",
"bandwidth",
"edge",
"audio",
"video",
"?",
"?",
"?",
"?",
"?",
"amf3_meta",
"amf3_shared",
"amd3_cmd",
2012-04-18 14:37:18 +02:00
"amf_meta",
"amf_shared",
"amf_cmd",
"?",
"aggregate"
};
return type < sizeof(types) / sizeof(types[0])
? types[type]
: "?";
}
char*
ngx_rtmp_user_message_type(uint16_t evt) {
static char* evts[] = {
"stream_begin",
"stream_eof",
"stream dry",
"set_buflen",
"recorded",
"ping_request",
"ping_response",
};
return evt < sizeof(evts) / sizeof(evts[0])
? evts[evt]
: "?";
}
#endif
2012-03-08 16:21:22 +01:00
void
ngx_rtmp_init_connection(ngx_connection_t *c)
{
ngx_uint_t i;
ngx_rtmp_port_t *port;
struct sockaddr *sa;
struct sockaddr_in *sin;
ngx_rtmp_log_ctx_t *ctx;
ngx_rtmp_in_addr_t *addr;
ngx_rtmp_session_t *s;
ngx_rtmp_addr_conf_t *addr_conf;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
ngx_rtmp_in6_addr_t *addr6;
#endif
/* find the server configuration for the address:port */
/* AF_INET only */
port = c->listening->servers;
if (port->naddrs > 1) {
/*
* There are several addresses on this port and one of them
* is the "*:port" wildcard so getsockname() is needed to determine
* the server address.
*
* AcceptEx() already gave this address.
*/
if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
ngx_rtmp_close_connection(c);
return;
}
sa = c->local_sockaddr;
switch (sa->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) sa;
addr6 = port->addrs;
/* the last address is "*" */
for (i = 0; i < port->naddrs - 1; i++) {
if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
break;
}
}
addr_conf = &addr6[i].conf;
break;
#endif
default: /* AF_INET */
sin = (struct sockaddr_in *) sa;
addr = port->addrs;
/* the last address is "*" */
for (i = 0; i < port->naddrs - 1; i++) {
if (addr[i].addr == sin->sin_addr.s_addr) {
break;
}
}
addr_conf = &addr[i].conf;
break;
}
} else {
switch (c->local_sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
addr6 = port->addrs;
addr_conf = &addr6[0].conf;
break;
#endif
default: /* AF_INET */
addr = port->addrs;
addr_conf = &addr[0].conf;
break;
}
}
s = ngx_pcalloc(c->pool, sizeof(ngx_rtmp_session_t));
if (s == NULL) {
ngx_rtmp_close_connection(c);
return;
}
s->main_conf = addr_conf->ctx->main_conf;
s->srv_conf = addr_conf->ctx->srv_conf;
s->addr_text = &addr_conf->addr_text;
c->data = s;
s->connection = c;
2012-03-08 18:45:10 +01:00
ngx_log_error(NGX_LOG_INFO, c->log, 0, "*%ui client connected",
c->number, &c->addr_text);
2012-03-08 16:21:22 +01:00
ctx = ngx_palloc(c->pool, sizeof(ngx_rtmp_log_ctx_t));
if (ctx == NULL) {
ngx_rtmp_close_connection(c);
return;
}
ctx->client = &c->addr_text;
ctx->session = s;
c->log->connection = c->number;
c->log->handler = ngx_rtmp_log_error;
c->log->data = ctx;
c->log->action = "sending client greeting line";
c->log_error = NGX_ERROR_INFO;
ngx_rtmp_init_session(c);
}
static void
ngx_rtmp_init_session(ngx_connection_t *c)
{
2012-03-21 16:08:59 +01:00
ngx_rtmp_session_t *s;
ngx_rtmp_core_main_conf_t *cmcf;
ngx_rtmp_core_srv_conf_t *cscf;
ngx_buf_t *b;
size_t n, size;
ngx_rtmp_handler_pt *h;
ngx_array_t *ch;
2012-03-08 16:21:22 +01:00
s = c->data;
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_rtmp_max_module);
if (s->ctx == NULL) {
2012-03-13 14:51:41 +01:00
ngx_rtmp_close_connection(c);
2012-03-08 16:21:22 +01:00
return;
}
2012-04-18 14:37:18 +02:00
s->in_streams = ngx_pcalloc(c->pool, sizeof(ngx_rtmp_stream_t)
2012-03-13 06:41:51 +01:00
* cscf->max_streams);
if (s->in_streams == NULL) {
2012-03-13 14:51:41 +01:00
ngx_rtmp_close_connection(c);
2012-03-11 17:17:17 +01:00
return;
}
2012-03-08 16:21:22 +01:00
size = NGX_RTMP_HANDSHAKE_SIZE + 1;
2012-03-15 07:23:49 +01:00
2012-04-19 10:31:36 +02:00
s->timeout = cscf->timeout;
2012-04-05 19:28:41 +02:00
ngx_rtmp_set_chunk_size(s, NGX_RTMP_DEFAULT_CHUNK_SIZE);
2012-04-07 23:44:57 +02:00
2012-04-18 14:37:18 +02:00
/* start handshake */
b = &s->hs_in_buf;
b->start = b->pos = b->last = ngx_pcalloc(s->in_pool, size);
b->end = b->start + size;
b->temporary = 1;
b = &s->hs_out_buf;
2012-03-11 17:17:17 +01:00
b->start = b->pos = b->last = ngx_pcalloc(s->in_pool, size);
2012-03-08 16:21:22 +01:00
b->end = b->start + size;
b->temporary = 1;
c->write->handler = ngx_rtmp_handshake_send;
c->read->handler = ngx_rtmp_handshake_recv;
2012-03-21 16:08:59 +01:00
/* call connect callbacks */
cmcf = ngx_rtmp_get_module_main_conf(s, ngx_rtmp_core_module);
ch = &cmcf->events[NGX_RTMP_CONNECT];
h = ch->elts;
for(n = 0; n < ch->nelts; ++n, ++h) {
if (*h) {
if ((*h)(s, NULL, NULL) != NGX_OK) {
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
2012-03-21 16:08:59 +01:00
return;
}
}
}
2012-03-08 16:21:22 +01:00
ngx_rtmp_handshake_recv(c->read);
}
2012-03-15 07:23:49 +01:00
uint32_t
ngx_rtmp_get_timestamp()
{
ngx_time_t *tod;
tod = ngx_timeofday();
/* FIXME: divisor */
return (uint32_t)(tod->sec * 1000 + tod->msec) /*& 0x00ffffff*/;
2012-03-15 07:23:49 +01:00
}
2012-03-08 16:21:22 +01:00
void
ngx_rtmp_handshake_recv(ngx_event_t *rev)
{
2012-03-15 07:23:49 +01:00
ssize_t n;
ngx_connection_t *c;
ngx_rtmp_session_t *s;
ngx_buf_t *b;
u_char *p;
2012-03-08 16:21:22 +01:00
c = rev->data;
s = c->data;
2012-03-13 14:51:41 +01:00
if (c->destroyed) {
return;
}
2012-03-08 16:21:22 +01:00
if (rev->timedout) {
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
c->timedout = 1;
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
2012-03-08 16:21:22 +01:00
return;
}
if (rev->timer_set) {
ngx_del_timer(rev);
}
b = (s->hs_stage == NGX_RTMP_HS_READ_DATA)
? &s->hs_in_buf
: &s->hs_out_buf;
2012-03-08 16:21:22 +01:00
2012-03-15 07:23:49 +01:00
while (b->last != b->end) {
2012-03-08 16:21:22 +01:00
n = c->recv(c, b->last, b->end - b->last);
if (n == NGX_ERROR || n == 0) {
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
2012-03-08 18:45:10 +01:00
return;
2012-03-08 16:21:22 +01:00
}
if (n == NGX_AGAIN) {
2012-04-19 10:31:36 +02:00
ngx_add_timer(rev, s->timeout);
2012-03-08 16:21:22 +01:00
if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
2012-03-08 16:21:22 +01:00
}
return;
}
2012-03-15 07:23:49 +01:00
b->last += n;
2012-03-08 16:21:22 +01:00
}
2012-04-20 16:53:58 +02:00
if (rev->active) {
ngx_del_event(c->read, NGX_READ_EVENT, 0);
}
2012-03-08 16:21:22 +01:00
2012-03-15 07:23:49 +01:00
++s->hs_stage;
if (s->hs_stage == NGX_RTMP_HS_WRITE_DATA) {
if (*b->pos != NGX_RTMP_VERSION) {
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ERROR,
"invalid handshake signature");
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
return;
}
/* version is never needed anymore */
++b->pos;
/* store current time as our epoch */
2012-03-15 07:23:49 +01:00
s->epoch = ngx_rtmp_get_timestamp();
/* read client epoch */
2012-03-15 07:23:49 +01:00
p = (u_char*)&s->peer_epoch;
*p++ = b->pos[3];
*p++ = b->pos[2];
*p++ = b->pos[1];
*p++ = b->pos[0];
/* prepare output signature:
* set version, set epoch, fill zeroes */
2012-03-15 07:23:49 +01:00
p = (u_char*)&s->epoch;
b = &s->hs_out_buf;
b->pos[0] = NGX_RTMP_VERSION;
2012-03-15 07:23:49 +01:00
b->pos[4] = *p++;
b->pos[3] = *p++;
b->pos[2] = *p++;
b->pos[1] = *p++;
b->pos[5] = b->pos[6] = b->pos[7] = b->pos[8] = 0;
for(b->last = b->pos + 9, n = 1;
b->last < b->end;
++b->last, ++n)
{
*b->last = (u_char)(n & 0xff);
}
/* reply timestamp is the same as out epoch */
/*ngx_memcpy(s->hs_in_buf.pos + 4, b->pos + 1, 4);*/
2012-03-08 18:45:10 +01:00
ngx_rtmp_handshake_send(c->write);
2012-03-08 16:21:22 +01:00
return;
}
/* handshake done */
2012-03-11 17:17:17 +01:00
ngx_reset_pool(s->in_pool);
2012-03-08 16:21:22 +01:00
c->read->handler = ngx_rtmp_recv;
c->write->handler = ngx_rtmp_send;
2012-03-11 17:17:17 +01:00
2012-03-15 07:23:49 +01:00
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, c->log, 0,
"RTMP handshake done; epoch=%uD peer_epoch=%uD",
2012-03-15 07:23:49 +01:00
s->epoch, s->peer_epoch);
2012-03-08 16:21:22 +01:00
ngx_rtmp_recv(rev);
}
void
ngx_rtmp_handshake_send(ngx_event_t *wev)
{
2012-03-15 07:23:49 +01:00
ngx_int_t n;
ngx_connection_t *c;
ngx_rtmp_session_t *s;
ngx_buf_t *b;
2012-03-08 16:21:22 +01:00
c = wev->data;
s = c->data;
2012-03-13 14:51:41 +01:00
if (c->destroyed) {
return;
}
2012-03-08 16:21:22 +01:00
if (wev->timedout) {
2012-03-15 07:23:49 +01:00
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
"client timed out");
2012-03-08 16:21:22 +01:00
c->timedout = 1;
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
2012-03-08 16:21:22 +01:00
return;
}
if (wev->timer_set) {
ngx_del_timer(wev);
}
restart:
2012-03-15 07:23:49 +01:00
b = (s->hs_stage == NGX_RTMP_HS_WRITE_DATA)
? &s->hs_out_buf
: &s->hs_in_buf;
2012-03-15 07:23:49 +01:00
while(b->pos != b->last) {
2012-03-15 07:23:49 +01:00
n = c->send(c, b->pos, b->last - b->pos);
2012-03-08 16:21:22 +01:00
if (n == NGX_ERROR) {
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
2012-03-08 16:21:22 +01:00
return;
}
if (n == NGX_AGAIN || n == 0) {
2012-04-19 10:31:36 +02:00
ngx_add_timer(c->write, s->timeout);
2012-03-08 16:21:22 +01:00
if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
2012-03-08 16:21:22 +01:00
return;
}
}
b->pos += n;
2012-03-08 16:21:22 +01:00
}
2012-03-15 07:23:49 +01:00
++s->hs_stage;
if (s->hs_stage == NGX_RTMP_HS_WRITE_ECHO) {
2012-03-08 16:21:22 +01:00
goto restart;
}
2012-04-20 16:53:58 +02:00
if (wev->active) {
ngx_del_event(wev, NGX_WRITE_EVENT, 0);
}
b = &s->hs_out_buf;
2012-03-15 07:23:49 +01:00
b->pos = b->last = b->start + 1;
2012-03-08 16:21:22 +01:00
ngx_rtmp_handshake_recv(c->read);
}
2012-04-05 19:28:41 +02:00
static ngx_chain_t *
ngx_rtmp_alloc_in_buf(ngx_rtmp_session_t *s)
{
ngx_chain_t *cl;
ngx_buf_t *b;
size_t size;
if ((cl = ngx_alloc_chain_link(s->in_pool)) == NULL
|| (cl->buf = ngx_calloc_buf(s->in_pool)) == NULL)
{
return NULL;
}
cl->next = NULL;
b = cl->buf;
size = s->in_chunk_size + NGX_RTMP_MAX_CHUNK_HEADER;
b->start = b->last = b->pos = ngx_palloc(s->in_pool, size);
if (b->start == NULL) {
return NULL;
}
b->end = b->start + size;
return cl;
}
2012-03-08 16:21:22 +01:00
void
ngx_rtmp_recv(ngx_event_t *rev)
{
ngx_int_t n;
ngx_connection_t *c;
ngx_rtmp_session_t *s;
ngx_rtmp_core_srv_conf_t *cscf;
2012-03-13 06:41:51 +01:00
ngx_rtmp_header_t *h;
ngx_rtmp_stream_t *st, *st0;
2012-03-11 17:17:17 +01:00
ngx_chain_t *in, *head;
ngx_buf_t *b;
u_char *p, *pp, *old_pos;
size_t size, fsize, old_size;
uint8_t fmt;
uint32_t csid, timestamp;
2012-03-08 16:21:22 +01:00
c = rev->data;
s = c->data;
2012-03-11 17:17:17 +01:00
b = NULL;
old_pos = NULL;
old_size = 0;
2012-03-08 16:21:22 +01:00
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
2012-03-13 14:51:41 +01:00
if (c->destroyed) {
return;
}
for( ;; ) {
2012-03-11 17:17:17 +01:00
2012-03-13 06:41:51 +01:00
st = &s->in_streams[s->in_csid];
2012-03-11 17:17:17 +01:00
/* allocate new buffer */
2012-03-11 17:17:17 +01:00
if (st->in == NULL) {
2012-04-05 19:28:41 +02:00
st->in = ngx_rtmp_alloc_in_buf(s);
if (st->in == NULL) {
2012-03-11 17:17:17 +01:00
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ERROR,
2012-04-05 19:28:41 +02:00
"in buf alloc failed");
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
2012-03-11 17:17:17 +01:00
return;
}
2012-03-08 16:21:22 +01:00
}
2012-03-11 17:17:17 +01:00
h = &st->hdr;
in = st->in;
b = in->buf;
2012-03-08 16:21:22 +01:00
if (old_size) {
2012-03-08 16:21:22 +01:00
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, c->log, 0,
"reusing formerly read data: %d", old_size);
2012-03-15 07:23:49 +01:00
b->pos = b->start;
b->last = ngx_movemem(b->pos, old_pos, old_size);
2012-03-11 17:17:17 +01:00
2012-04-05 19:28:41 +02:00
if (s->in_chunk_size_changing) {
ngx_rtmp_finalize_set_chunk_size(s);
}
} else {
2012-03-08 16:21:22 +01:00
if (old_pos) {
b->pos = b->last = b->start;
}
2012-03-08 16:21:22 +01:00
n = c->recv(c, b->last, b->end - b->last);
2012-03-08 16:21:22 +01:00
if (n == NGX_ERROR || n == 0) {
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
return;
2012-03-08 16:21:22 +01:00
}
if (n == NGX_AGAIN) {
if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
}
return;
}
b->last += n;
s->in_bytes += n;
if (s->in_bytes - s->in_last_ack >= cscf->ack_window) {
s->in_last_ack = s->in_bytes;
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, c->log, 0,
"sending RTMP ACK(%D)", s->in_bytes);
if (ngx_rtmp_send_ack(s, s->in_bytes)) {
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
return;
}
}
2012-03-08 16:21:22 +01:00
}
old_pos = NULL;
old_size = 0;
/* parse headers */
if (b->pos == b->start) {
p = b->pos;
/* chunk basic header */
2012-03-11 17:17:17 +01:00
fmt = (*p >> 6) & 0x03;
csid = *p++ & 0x3f;
2012-03-11 17:17:17 +01:00
if (csid == 0) {
if (b->last - p < 1)
continue;
2012-03-11 17:17:17 +01:00
csid = 64;
csid += *(uint8_t*)p++;
2012-03-11 17:17:17 +01:00
} else if (csid == 1) {
if (b->last - p < 2)
continue;
2012-03-11 17:17:17 +01:00
csid = 64;
csid += *(uint8_t*)p++;
csid += (uint32_t)256 * (*(uint8_t*)p++);
}
2012-03-08 16:21:22 +01:00
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, c->log, 0,
"RTMP bheader fmt=%d csid=%D",
2012-03-11 17:17:17 +01:00
(int)fmt, csid);
2012-03-30 18:18:20 +02:00
if (csid >= (uint32_t)cscf->max_streams) {
2012-03-11 17:17:17 +01:00
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ERROR,
"RTMP in chunk stream too big: %D >= %D",
2012-03-11 17:17:17 +01:00
csid, cscf->max_streams);
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
2012-03-11 17:17:17 +01:00
return;
}
/* link orphan */
2012-03-13 06:41:51 +01:00
if (s->in_csid == 0) {
2012-03-11 17:17:17 +01:00
/* unlink from stream #0 */
st->in = st->in->next;
/* link to new stream */
2012-03-13 06:41:51 +01:00
s->in_csid = csid;
st = &s->in_streams[csid];
2012-03-11 17:17:17 +01:00
if (st->in == NULL) {
in->next = in;
} else {
in->next = st->in->next;
st->in->next = in;
}
st->in = in;
h = &st->hdr;
h->csid = csid;
}
/* get previous header to inherit data from */
timestamp = h->timestamp;
2012-03-11 17:17:17 +01:00
if (fmt <= 2 ) {
if (b->last - p < 3)
continue;
/* timestamp:
* big-endian 3b -> little-endian 4b */
pp = (u_char*)&timestamp;
pp[2] = *p++;
pp[1] = *p++;
pp[0] = *p++;
pp[3] = 0;
2012-03-13 06:41:51 +01:00
if (fmt <= 1) {
if (b->last - p < 4)
continue;
/* size:
* big-endian 3b -> little-endian 4b
* type:
* 1b -> 1b*/
pp = (u_char*)&h->mlen;
pp[2] = *p++;
pp[1] = *p++;
pp[0] = *p++;
pp[3] = 0;
h->type = *(uint8_t*)p++;
2012-03-11 17:17:17 +01:00
if (fmt == 0) {
if (b->last - p < 4)
continue;
/* stream:
* little-endian 4b -> little-endian 4b */
pp = (u_char*)&h->msid;
pp[0] = *p++;
pp[1] = *p++;
pp[2] = *p++;
pp[3] = *p++;
}
}
}
2012-03-08 16:21:22 +01:00
/* extended header */
if (timestamp >= 0x00ffffff) {
/* Messages with type=3 should
* never have ext timestamp field
* according to standard.
* However that's not always the case
* in real life */
if (fmt <= 2 || cscf->publish_time_fix) {
if (b->last - p < 4)
continue;
pp = (u_char*)&timestamp;
pp[3] = *p++;
pp[2] = *p++;
pp[1] = *p++;
pp[0] = *p++;
}
}
if (fmt == 1 || fmt == 2) {
h->timestamp += timestamp;
} else {
h->timestamp = timestamp;
}
2012-03-08 16:21:22 +01:00
2012-04-23 17:13:35 +02:00
ngx_log_debug8(NGX_LOG_DEBUG_RTMP, c->log, 0,
"RTMP mheader fmt=%d %s (%d) "
"time=%uD/%uD mlen=%D len=%D msid=%D",
(int)fmt, ngx_rtmp_message_type(h->type), (int)h->type,
timestamp, h->timestamp, h->mlen, st->len, h->msid);
2012-03-08 16:21:22 +01:00
/* header done */
b->pos = p;
2012-03-21 16:32:18 +01:00
if (h->mlen > cscf->max_message) {
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ERROR,
"too big message: %uz", cscf->max_message);
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
2012-03-21 16:32:18 +01:00
return;
}
}
2012-03-08 16:21:22 +01:00
2012-03-11 17:17:17 +01:00
size = b->last - b->pos;
fsize = h->mlen - st->len;
2012-03-11 17:17:17 +01:00
if (size < ngx_min(fsize, s->in_chunk_size))
2012-03-08 16:21:22 +01:00
continue;
2012-03-11 17:17:17 +01:00
/* buffer is ready */
if (fsize > s->in_chunk_size) {
2012-03-11 17:17:17 +01:00
/* collect fragmented chunks */
st->len += s->in_chunk_size;
2012-03-15 12:49:05 +01:00
b->last = b->pos + s->in_chunk_size;
old_pos = b->last;
old_size = size - s->in_chunk_size;
2012-03-11 17:17:17 +01:00
} else {
/* handle! */
head = st->in->next;
st->in->next = NULL;
b->last = b->pos + fsize;
old_pos = b->last;
old_size = size - fsize;
st->len = 0;
if (ngx_rtmp_receive_message(s, h, head) != NGX_OK) {
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
2012-03-08 16:21:22 +01:00
return;
}
2012-04-05 19:28:41 +02:00
if (s->in_chunk_size_changing) {
/* copy old data to a new buffer */
if (!old_size) {
ngx_rtmp_finalize_set_chunk_size(s);
}
} else {
/* add used bufs to stream #0 */
st0 = &s->in_streams[0];
st->in->next = st0->in;
st0->in = head;
st->in = NULL;
}
2012-03-08 16:21:22 +01:00
}
2012-03-13 06:41:51 +01:00
s->in_csid = 0;
2012-03-08 16:21:22 +01:00
}
}
static void
2012-03-08 16:21:22 +01:00
ngx_rtmp_send(ngx_event_t *wev)
{
2012-03-13 14:51:41 +01:00
ngx_connection_t *c;
ngx_rtmp_session_t *s;
2012-04-18 14:37:18 +02:00
ngx_int_t n;
2012-04-19 10:31:36 +02:00
ngx_rtmp_core_srv_conf_t *cscf;
2012-03-08 16:21:22 +01:00
c = wev->data;
s = c->data;
2012-03-13 14:51:41 +01:00
if (c->destroyed) {
return;
}
2012-03-08 16:21:22 +01:00
if (wev->timedout) {
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
"client timed out");
2012-03-08 16:21:22 +01:00
c->timedout = 1;
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
2012-03-08 16:21:22 +01:00
return;
}
if (wev->timer_set) {
ngx_del_timer(wev);
}
2012-04-19 07:53:18 +02:00
if (s->out_chain == NULL && s->out_pos != s->out_last) {
s->out_chain = s->out[s->out_pos];
s->out_bpos = s->out_chain->buf->pos;
}
2012-04-18 14:37:18 +02:00
while (s->out_chain) {
n = c->send(c, s->out_bpos, s->out_chain->buf->last - s->out_bpos);
2012-04-18 14:37:18 +02:00
if (n == NGX_AGAIN || n == 0) {
2012-04-19 10:31:36 +02:00
ngx_add_timer(c->write, s->timeout);
2012-03-08 16:21:22 +01:00
if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
2012-03-08 16:21:22 +01:00
}
2012-04-20 07:01:04 +02:00
return;
}
if (n < 0) {
ngx_rtmp_finalize_session(s);
return;
2012-03-08 16:21:22 +01:00
}
2012-04-18 14:37:18 +02:00
s->out_bpos += n;
if (s->out_bpos == s->out_chain->buf->last) {
s->out_chain = s->out_chain->next;
if (s->out_chain == NULL) {
2012-04-19 10:31:36 +02:00
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
2012-04-19 07:53:18 +02:00
ngx_rtmp_free_shared_chain(cscf, s->out[s->out_pos]);
2012-04-18 14:37:18 +02:00
++s->out_pos;
2012-04-19 07:53:18 +02:00
s->out_pos %= NGX_RTMP_OUT_QUEUE;
2012-04-18 14:37:18 +02:00
if (s->out_pos == s->out_last) {
break;
}
2012-04-19 07:53:18 +02:00
s->out_chain = s->out[s->out_pos];
}
2012-04-18 14:37:18 +02:00
s->out_bpos = s->out_chain->buf->pos;
}
2012-03-08 16:21:22 +01:00
}
2012-04-18 14:37:18 +02:00
if (wev->active) {
ngx_del_event(wev, NGX_WRITE_EVENT, 0);
}
2012-03-08 16:21:22 +01:00
}
void
2012-03-13 14:51:41 +01:00
ngx_rtmp_prepare_message(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_rtmp_header_t *lh, ngx_chain_t *out)
2012-03-08 16:21:22 +01:00
{
ngx_chain_t *l;
u_char *p, *pp;
ngx_int_t hsize, thsize, nbufs;
uint32_t mlen, timestamp, ext_timestamp;
static uint8_t hdrsize[] = { 12, 8, 4, 1 };
u_char th[7];
ngx_rtmp_core_srv_conf_t *cscf;
uint8_t fmt;
ngx_connection_t *c;
c = s->connection;
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
2012-03-30 18:18:20 +02:00
if (h->csid >= (uint32_t)cscf->max_streams) {
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ERROR,
"RTMP out chunk stream too big: %D >= %D",
h->csid, cscf->max_streams);
2012-04-07 23:44:57 +02:00
ngx_rtmp_finalize_session(s);
return;
}
2012-03-08 16:21:22 +01:00
/* detect packet size */
mlen = 0;
nbufs = 0;
for(l = out; l; l = l->next) {
mlen += (l->buf->last - l->buf->pos);
++nbufs;
}
2012-03-13 14:51:41 +01:00
fmt = 0;
if (lh && lh->csid && h->msid == lh->msid) {
++fmt;
2012-03-18 14:09:19 +01:00
if (h->type == lh->type && mlen == lh->mlen) {
++fmt;
if (h->timestamp == lh->timestamp) {
++fmt;
}
}
timestamp = h->timestamp - lh->timestamp;
} else {
timestamp = h->timestamp;
}
if (lh) {
*lh = *h;
lh->mlen = mlen;
}
2012-03-13 14:51:41 +01:00
hsize = hdrsize[fmt];
2012-03-08 16:21:22 +01:00
ngx_log_debug8(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"RTMP prep %s (%d) fmt=%d csid=%uD timestamp=%uD "
"mlen=%uD msid=%uD nbufs=%d",
ngx_rtmp_message_type(h->type), (int)h->type, (int)fmt,
2012-04-20 16:03:00 +02:00
h->csid, timestamp, mlen, h->msid, nbufs);
ext_timestamp = 0;
if (timestamp >= 0x00ffffff) {
ext_timestamp = timestamp;
timestamp = 0x00ffffff;
hsize += 4;
}
2012-03-08 16:21:22 +01:00
if (h->csid >= 64) {
++hsize;
if (h->csid >= 320) {
++hsize;
}
}
2012-03-08 16:21:22 +01:00
/* fill initial header */
out->buf->pos -= hsize;
p = out->buf->pos;
/* basic header */
*p = (fmt << 6);
if (h->csid >= 2 && h->csid <= 63) {
*p++ |= (((uint8_t)h->csid) & 0x3f);
} else if (h->csid >= 64 && h->csid < 320) {
++p;
*p++ = (uint8_t)(h->csid - 64);
} else {
*p++ |= 1;
*p++ = (uint8_t)(h->csid - 64);
*p++ = (uint8_t)((h->csid - 64) >> 8);
}
/* create fmt3 header for successive fragments */
2012-03-13 06:41:51 +01:00
thsize = p - out->buf->pos;
ngx_memcpy(th, out->buf->pos, thsize);
th[0] |= 0xc0;
2012-03-08 16:21:22 +01:00
/* message header */
if (fmt <= 2) {
pp = (u_char*)&timestamp;
*p++ = pp[2];
*p++ = pp[1];
*p++ = pp[0];
if (fmt <= 1) {
pp = (u_char*)&mlen;
*p++ = pp[2];
*p++ = pp[1];
*p++ = pp[0];
*p++ = h->type;
if (fmt == 0) {
pp = (u_char*)&h->msid;
*p++ = pp[0];
*p++ = pp[1];
*p++ = pp[2];
*p++ = pp[3];
}
}
}
2012-03-08 16:21:22 +01:00
/* extended header */
if (ext_timestamp) {
pp = (u_char*)&ext_timestamp;
*p++ = pp[3];
*p++ = pp[2];
*p++ = pp[1];
*p++ = pp[0];
/* This CONTRADICTS the standard
* but that's the way flash client
* wants data to be encoded;
* ffmpeg complains */
if (cscf->play_time_fix) {
ngx_memcpy(&th[thsize], p - 4, 4);
thsize += 4;
}
}
/* append headers to successive fragments */
for(out = out->next; out; out = out->next) {
2012-03-13 14:51:41 +01:00
out->buf->pos -= thsize;
ngx_memcpy(out->buf->pos, th, thsize);
}
}
2012-03-13 14:51:41 +01:00
ngx_int_t
2012-03-18 14:09:19 +01:00
ngx_rtmp_send_message(ngx_rtmp_session_t *s, ngx_chain_t *out,
ngx_uint_t priority)
{
2012-04-19 07:53:18 +02:00
ngx_int_t nmsg;
2012-03-18 14:09:19 +01:00
2012-04-19 07:53:18 +02:00
nmsg = (s->out_last - s->out_pos) % NGX_RTMP_OUT_QUEUE + 1;
2012-04-18 14:37:18 +02:00
/* drop packet?
* Note we always leave 1 slot free */
if (nmsg + priority * 8 >= NGX_RTMP_OUT_QUEUE) {
2012-04-19 07:53:18 +02:00
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
2012-04-18 14:37:18 +02:00
"RTMP drop message bufs=%ui, priority=%ui",
nmsg, priority);
2012-03-18 14:09:19 +01:00
return NGX_AGAIN;
}
2012-04-19 07:53:18 +02:00
s->out[s->out_last++] = out;
s->out_last %= NGX_RTMP_OUT_QUEUE;
2012-04-19 07:53:18 +02:00
ngx_rtmp_acquire_shared_chain(out);
2012-04-20 16:03:00 +02:00
ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"RTMP send nmsg=%ui, priority=%ui #%ui",
nmsg, priority, s->out_last);
2012-04-20 07:01:04 +02:00
2012-04-20 16:03:00 +02:00
if (priority && s->out_buffer
&& nmsg < NGX_RTMP_OUT_QUEUE_LOWAT)
{
2012-04-20 07:01:04 +02:00
return NGX_OK;
}
2012-04-19 07:53:18 +02:00
if (!s->connection->write->active) {
ngx_rtmp_send(s->connection->write);
/*return ngx_add_event(s->connection->write, NGX_WRITE_EVENT, NGX_CLEAR_EVENT);*/
}
2012-03-13 14:51:41 +01:00
2012-04-19 07:53:18 +02:00
return NGX_OK;
2012-03-08 16:21:22 +01:00
}
static ngx_int_t
ngx_rtmp_receive_message(ngx_rtmp_session_t *s,
ngx_rtmp_header_t *h, ngx_chain_t *in)
2012-03-08 16:21:22 +01:00
{
ngx_rtmp_core_main_conf_t *cmcf;
ngx_array_t *evhs;
size_t n;
2012-03-21 16:08:59 +01:00
ngx_rtmp_handler_pt *evh;
2012-03-13 06:41:51 +01:00
ngx_connection_t *c;
2012-03-13 06:41:51 +01:00
c = s->connection;
cmcf = ngx_rtmp_get_module_main_conf(s, ngx_rtmp_core_module);
2012-03-08 16:21:22 +01:00
#ifdef NGX_DEBUG
{
int nbufs;
2012-03-08 18:45:10 +01:00
ngx_chain_t *ch;
2012-03-08 16:21:22 +01:00
2012-03-13 06:41:51 +01:00
for(nbufs = 1, ch = in;
ch->next;
ch = ch->next, ++nbufs);
ngx_log_debug7(NGX_LOG_DEBUG_RTMP, c->log, 0,
"RTMP recv %s (%d) csid=%D timestamp=%D "
"mlen=%D msid=%D nbufs=%d",
ngx_rtmp_message_type(h->type), (int)h->type,
h->csid, h->timestamp, h->mlen, h->msid, nbufs);
2012-03-08 16:21:22 +01:00
}
#endif
if (h->type >= NGX_RTMP_MSG_MAX) {
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, c->log, 0,
"unexpected RTMP message type: %d", (int)h->type);
return NGX_OK;
}
2012-03-08 16:21:22 +01:00
evhs = &cmcf->events[h->type];
2012-03-13 06:41:51 +01:00
evh = evhs->elts;
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, c->log, 0,
"nhandlers: %d", evhs->nelts);
2012-03-13 06:41:51 +01:00
for(n = 0; n < evhs->nelts; ++n, ++evh) {
if (!evh) {
continue;
}
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, c->log, 0,
"calling handler %d", n);
2012-03-21 16:08:59 +01:00
switch ((*evh)(s, h, in)) {
case NGX_ERROR:
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, c->log, 0,
"handler %d failed", n);
return NGX_ERROR;
case NGX_DONE:
return NGX_OK;
}
2012-03-08 16:21:22 +01:00
}
return NGX_OK;
}
2012-04-05 19:28:41 +02:00
ngx_int_t
ngx_rtmp_set_chunk_size(ngx_rtmp_session_t *s, ngx_uint_t size)
{
ngx_rtmp_core_srv_conf_t *cscf;
ngx_chain_t *li, *fli, *lo, *flo;
ngx_buf_t *bi, *bo;
ngx_int_t n;
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
"setting chunk_size=%ui", size);
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
s->in_old_pool = s->in_pool;
s->in_chunk_size = size;
s->in_pool = ngx_create_pool(4096, s->connection->log);
/* copy existing chunk data */
if (s->in_old_pool) {
s->in_chunk_size_changing = 1;
s->in_streams[0].in = NULL;
for(n = 1; n < cscf->max_streams; ++n) {
/* stream buffer is circular
* for all streams except for the current one
* (which caused this chunk size change);
* we can simply ignore it */
li = s->in_streams[n].in;
if (li == NULL || li->next == NULL) {
continue;
}
/* move from last to the first */
li = li->next;
fli = li;
lo = ngx_rtmp_alloc_in_buf(s);
if (lo == NULL) {
return NGX_ERROR;
}
flo = lo;
for ( ;; ) {
bi = li->buf;
bo = lo->buf;
if (bo->end - bo->last >= bi->last - bi->pos) {
bo->last = ngx_cpymem(bo->last, bi->pos,
bi->last - bi->pos);
li = li->next;
if (li == fli) {
lo->next = flo;
s->in_streams[n].in = lo;
break;
}
} else {
bo->last = ngx_cpymem(bo->last, bi->pos,
bo->end - bo->last);
bi->pos += (bo->end - bo->last);
}
if (bo->last == bo->end) {
lo->next = ngx_rtmp_alloc_in_buf(s);
if (lo->next == NULL) {
return NGX_ERROR;
}
lo = lo->next;
}
}
}
}
return NGX_OK;
}
static ngx_int_t
ngx_rtmp_finalize_set_chunk_size(ngx_rtmp_session_t *s)
{
if (s->in_chunk_size_changing && s->in_old_pool) {
ngx_destroy_pool(s->in_old_pool);
s->in_old_pool = NULL;
s->in_chunk_size_changing = 0;
}
return NGX_OK;
}
2012-04-07 23:44:57 +02:00
static void
2012-03-08 16:21:22 +01:00
ngx_rtmp_close_connection(ngx_connection_t *c)
{
2012-03-13 14:51:41 +01:00
ngx_pool_t *pool;
2012-04-07 23:44:57 +02:00
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, "close connection");
pool = c->pool;
ngx_close_connection(c);
ngx_destroy_pool(pool);
}
static void
ngx_rtmp_close_session_handler(ngx_event_t *e)
{
ngx_rtmp_session_t *s;
ngx_connection_t *c;
2012-03-13 14:51:41 +01:00
ngx_rtmp_core_main_conf_t *cmcf;
ngx_rtmp_core_srv_conf_t *cscf;
2012-03-21 16:08:59 +01:00
ngx_rtmp_handler_pt *h;
ngx_array_t *dh;
2012-03-13 14:51:41 +01:00
size_t n;
2012-04-07 23:44:57 +02:00
s = e->data;
c = s->connection;
2012-04-04 15:00:05 +02:00
2012-03-13 14:51:41 +01:00
cmcf = ngx_rtmp_get_module_main_conf(s, ngx_rtmp_core_module);
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
2012-03-13 14:51:41 +01:00
2012-04-07 23:44:57 +02:00
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, "close session");
2012-03-08 16:21:22 +01:00
2012-03-13 14:51:41 +01:00
if (s) {
2012-03-21 16:08:59 +01:00
dh = &cmcf->events[NGX_RTMP_DISCONNECT];
h = dh->elts;
for(n = 0; n < dh->nelts; ++n, ++h) {
2012-03-13 14:51:41 +01:00
if (*h) {
2012-03-21 16:08:59 +01:00
(*h)(s, NULL, NULL);
2012-03-13 14:51:41 +01:00
}
}
2012-04-05 19:28:41 +02:00
if (s->in_old_pool) {
ngx_destroy_pool(s->in_old_pool);
}
2012-03-13 14:51:41 +01:00
if (s->in_pool) {
ngx_destroy_pool(s->in_pool);
}
}
2012-03-08 16:21:22 +01:00
2012-04-18 14:37:18 +02:00
while (s->out_pos != s->out_last) {
2012-04-19 07:53:18 +02:00
ngx_rtmp_free_shared_chain(cscf, s->out[s->out_pos++]);
s->out_pos %= NGX_RTMP_OUT_QUEUE;
}
2012-04-07 23:44:57 +02:00
ngx_rtmp_close_connection(c);
}
void
ngx_rtmp_finalize_session(ngx_rtmp_session_t *s)
{
ngx_event_t *e;
ngx_connection_t *c;
/* deferred session finalize;
* schedule handler here */
c = s->connection;
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, "finalize session");
c->destroyed = 1;
e = &s->close;
e->data = s;
e->handler = ngx_rtmp_close_session_handler;
e->log = c->log;
ngx_post_event(e, &ngx_posted_events);
2012-03-08 16:21:22 +01:00
}
u_char *
ngx_rtmp_log_error(ngx_log_t *log, u_char *buf, size_t len)
{
u_char *p;
ngx_rtmp_session_t *s;
ngx_rtmp_log_ctx_t *ctx;
if (log->action) {
p = ngx_snprintf(buf, len, " while %s", log->action);
len -= p - buf;
buf = p;
}
ctx = log->data;
p = ngx_snprintf(buf, len, ", client: %V", ctx->client);
len -= p - buf;
buf = p;
s = ctx->session;
if (s == NULL) {
return p;
}
p = ngx_snprintf(buf, len, ", server: %V", s->addr_text);
2012-03-08 16:21:22 +01:00
len -= p - buf;
buf = p;
return p;
}