Skip to content

Commit

Permalink
implemented applications
Browse files Browse the repository at this point in the history
  • Loading branch information
arut committed Mar 23, 2012
1 parent e563c31 commit 31d18ed
Show file tree
Hide file tree
Showing 12 changed files with 746 additions and 312 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@

- fix broken data_frame(?)

- fix '..greeing line..' in log
13 changes: 8 additions & 5 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ CORE_MODULES="$CORE_MODULES
ngx_rtmp_module \
ngx_rtmp_core_module \
ngx_rtmp_access_module \
ngx_rtmp_broadcast_module"
ngx_rtmp_broadcast_module \
"

NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
$ngx_addon_dir/ngx_rtmp.c \
$ngx_addon_dir/ngx_rtmp_shared.c \
$ngx_addon_dir/ngx_rtmp_handler.c \
$ngx_addon_dir/ngx_rtmp_core_module.c \
$ngx_addon_dir/ngx_rtmp_app.c \
$ngx_addon_dir/ngx_rtmp_amf0.c \
$ngx_addon_dir/ngx_rtmp_send.c \
$ngx_addon_dir/ngx_rtmp_shared.c \
$ngx_addon_dir/ngx_rtmp_handler.c \
$ngx_addon_dir/ngx_rtmp_receive.c \
$ngx_addon_dir/ngx_rtmp_core_module.c \
$ngx_addon_dir/ngx_rtmp_access_module.c \
$ngx_addon_dir/ngx_rtmp_broadcast_module.c"
$ngx_addon_dir/ngx_rtmp_broadcast_module.c \
"
102 changes: 99 additions & 3 deletions ngx_rtmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ static ngx_int_t ngx_rtmp_init_events(ngx_conf_t *cf,
ngx_rtmp_core_main_conf_t *cmcf);
static ngx_int_t ngx_rtmp_init_event_handlers(ngx_conf_t *cf,
ngx_rtmp_core_main_conf_t *cmcf);
static char * ngx_rtmp_merge_applications(ngx_conf_t *cf,
ngx_array_t *applications, void **app_conf, ngx_rtmp_module_t *module,
ngx_uint_t ctx_index);


ngx_uint_t ngx_rtmp_max_module;
Expand Down Expand Up @@ -75,7 +78,7 @@ ngx_rtmp_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
ngx_rtmp_listen_t *listen;
ngx_rtmp_module_t *module;
ngx_rtmp_conf_ctx_t *ctx;
ngx_rtmp_core_srv_conf_t **cscfp;
ngx_rtmp_core_srv_conf_t *cscf, **cscfp;
ngx_rtmp_core_main_conf_t *cmcf;

ctx = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_conf_ctx_t));
Expand Down Expand Up @@ -118,7 +121,18 @@ ngx_rtmp_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)


/*
* create the main_conf's, the null srv_conf's, and the null loc_conf's
* the rtmp null app_conf context, it is used to merge
* the server{}s' app_conf's
*/

ctx->app_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_rtmp_max_module);
if (ctx->app_conf == NULL) {
return NGX_CONF_ERROR;
}


/*
* create the main_conf's, the null srv_conf's, and the null app_conf's
* of the all rtmp modules
*/

Expand All @@ -143,6 +157,13 @@ ngx_rtmp_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
return NGX_CONF_ERROR;
}
}

if (module->create_app_conf) {
ctx->app_conf[mi] = module->create_app_conf(cf);
if (ctx->app_conf[mi] == NULL) {
return NGX_CONF_ERROR;
}
}
}

pcf = *cf;
Expand Down Expand Up @@ -214,9 +235,38 @@ ngx_rtmp_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
return rv;
}
}

if (module->merge_app_conf) {

/* merge the server{}'s app_conf */

/*ctx->app_conf = cscfp[s]->ctx->loc_conf;*/

rv = module->merge_app_conf(cf,
ctx->app_conf[mi],
cscfp[s]->ctx->app_conf[mi]);
if (rv != NGX_CONF_OK) {
*cf = pcf;
return rv;
}

/* merge the applications{}' app_conf's */

cscf = cscfp[s]->ctx->srv_conf[ngx_rtmp_core_module.ctx_index];

rv = ngx_rtmp_merge_applications(cf, &cscf->applications,
cscfp[s]->ctx->app_conf,
module, mi);
if (rv != NGX_CONF_OK) {
*cf = pcf;
return rv;
}
}

}
}


if (ngx_rtmp_init_events(cf, cmcf) != NGX_OK) {
return NGX_CONF_ERROR;
}
Expand Down Expand Up @@ -259,10 +309,45 @@ ngx_rtmp_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
}


static char *
ngx_rtmp_merge_applications(ngx_conf_t *cf, ngx_array_t *applications,
void **app_conf, ngx_rtmp_module_t *module, ngx_uint_t ctx_index)
{
char *rv;
ngx_rtmp_conf_ctx_t *ctx, saved;
ngx_rtmp_core_app_conf_t **cacfp;
ngx_uint_t n;

if (applications == NULL) {
return NGX_CONF_OK;
}

ctx = (ngx_rtmp_conf_ctx_t *) cf->ctx;
saved = *ctx;

cacfp = applications->elts;
for (n = 0; n < applications->nelts; ++n, ++cacfp) {

ctx->app_conf = (*cacfp)->app_conf;

rv = module->merge_app_conf(cf, app_conf[ctx_index],
(*cacfp)->app_conf[ctx_index]);
if (rv != NGX_CONF_OK) {
return rv;
}
}

*ctx = saved;

return NGX_CONF_OK;
}


static ngx_int_t
ngx_rtmp_init_events(ngx_conf_t *cf, ngx_rtmp_core_main_conf_t *cmcf)
{
size_t n;
size_t n;
ngx_rtmp_amf0_handler_t *h;

for(n = 0; n < NGX_RTMP_MAX_EVENT; ++n) {
if (ngx_array_init(&cmcf->events[n], cf->pool, 1,
Expand All @@ -278,6 +363,11 @@ ngx_rtmp_init_events(ngx_conf_t *cf, ngx_rtmp_core_main_conf_t *cmcf)
return NGX_ERROR;
}

/* this handler of connect should always be the first */
h = ngx_array_push(&cmcf->amf0);
ngx_str_set(&h->name, "connect");
h->handler = ngx_rtmp_connect;

return NGX_OK;
}

Expand Down Expand Up @@ -321,6 +411,11 @@ ngx_rtmp_init_event_handlers(ngx_conf_t *cf, ngx_rtmp_core_main_conf_t *cmcf)
eh = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_USER]);
*eh = ngx_rtmp_user_message_handler;

/* init several AMF0 callbacks */
h = ngx_array_push(&cmcf->amf0);
ngx_str_set(&h->name, "createStream");
h->handler = ngx_rtmp_create_stream;

/* init amf0 callbacks */
ngx_array_init(&cmcf->amf0_arrays, cf->pool, 1, sizeof(ngx_hash_key_t));

Expand Down Expand Up @@ -674,6 +769,7 @@ ngx_rtmp_cmp_conf_addrs(const void *one, const void *two)
return 0;
}


void *
ngx_rtmp_rmemcpy(void *dst, void* src, size_t n)
{
Expand Down
41 changes: 38 additions & 3 deletions ngx_rtmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
typedef struct {
void **main_conf;
void **srv_conf;
void **app_conf;
} ngx_rtmp_conf_ctx_t;


Expand Down Expand Up @@ -178,8 +179,19 @@ typedef struct {
void **ctx;
void **main_conf;
void **srv_conf;
void **app_conf;

ngx_str_t *addr_text;
int connected;

/* connection parameters */
ngx_str_t app;
ngx_str_t flashver;
ngx_str_t swf_url;
ngx_str_t tc_url;
uint32_t acodecs;
uint32_t vcodecs;
ngx_str_t page_url;

/* TODO: allocate this bufs from shared pool */
ngx_buf_t hs_in_buf;
Expand Down Expand Up @@ -232,6 +244,8 @@ typedef struct {


typedef struct ngx_rtmp_core_srv_conf_s {
ngx_array_t applications; /* ngx_rtmp_core_app_conf_t */

ngx_msec_t timeout;
ngx_flag_t so_keepalive;
ngx_int_t max_streams;
Expand All @@ -244,14 +258,19 @@ typedef struct ngx_rtmp_core_srv_conf_s {
ngx_chain_t *free_chains;
size_t max_buf;
size_t max_message;
ngx_flag_t wait_key_frame;
ngx_flag_t play_time_fix;
ngx_flag_t publish_time_fix;

ngx_rtmp_conf_ctx_t *ctx;
} ngx_rtmp_core_srv_conf_t;


typedef struct {
ngx_str_t name;
void **app_conf;
} ngx_rtmp_core_app_conf_t;


typedef struct {
ngx_str_t *client;
ngx_rtmp_session_t *session;
Expand All @@ -266,18 +285,24 @@ typedef struct {
char *(*init_main_conf)(ngx_conf_t *cf, void *conf);

void *(*create_srv_conf)(ngx_conf_t *cf);
char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev,
void *conf);
char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev,
void *conf);

void *(*create_app_conf)(ngx_conf_t *cf);
char *(*merge_app_conf)(ngx_conf_t *cf, void *prev,
void *conf);
} ngx_rtmp_module_t;

#define NGX_RTMP_MODULE 0x504D5452 /* "RTMP" */

#define NGX_RTMP_MAIN_CONF 0x02000000
#define NGX_RTMP_SRV_CONF 0x04000000
#define NGX_RTMP_APP_CONF 0x08000000


#define NGX_RTMP_MAIN_CONF_OFFSET offsetof(ngx_rtmp_conf_ctx_t, main_conf)
#define NGX_RTMP_SRV_CONF_OFFSET offsetof(ngx_rtmp_conf_ctx_t, srv_conf)
#define NGX_RTMP_APP_CONF_OFFSET offsetof(ngx_rtmp_conf_ctx_t, app_conf)


#define ngx_rtmp_get_module_ctx(s, module) (s)->ctx[module.ctx_index]
Expand All @@ -288,6 +313,8 @@ typedef struct {
#define ngx_rtmp_get_module_main_conf(s, module) \
(s)->main_conf[module.ctx_index]
#define ngx_rtmp_get_module_srv_conf(s, module) (s)->srv_conf[module.ctx_index]
#define ngx_rtmp_get_module_app_conf(s, module) ((s)->app_conf ? \
(s)->app_conf[module.ctx_index] : NULL)

#define ngx_rtmp_conf_get_module_main_conf(cf, module) \
((ngx_rtmp_conf_ctx_t *) cf->ctx)->main_conf[module.ctx_index]
Expand Down Expand Up @@ -321,6 +348,14 @@ ngx_int_t ngx_rtmp_user_message_handler(ngx_rtmp_session_t *s,
ngx_int_t ngx_rtmp_amf0_message_handler(ngx_rtmp_session_t *s,
ngx_rtmp_header_t *h, ngx_chain_t *in);

/* Standard AMF0 handlers */
ngx_int_t ngx_rtmp_connect(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in);
ngx_int_t ngx_rtmp_create_stream(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in);
ngx_int_t ngx_rtmp_amf0_default(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *in);


/* Shared output buffers */
ngx_chain_t * ngx_rtmp_alloc_shared_buf(ngx_rtmp_core_srv_conf_t *cscf);
Expand Down
Loading

0 comments on commit 31d18ed

Please sign in to comment.