From ef424df677eb545ba5277649ee99fbea7e38bb55 Mon Sep 17 00:00:00 2001 From: Roman Arutyunyan Date: Thu, 2 Jan 2014 10:19:05 +0400 Subject: [PATCH] added control redirect --- ngx_rtmp_control_module.c | 55 ++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/ngx_rtmp_control_module.c b/ngx_rtmp_control_module.c index 2e870e7..09242d3 100644 --- a/ngx_rtmp_control_module.c +++ b/ngx_rtmp_control_module.c @@ -196,9 +196,12 @@ ngx_rtmp_control_drop_handler(ngx_http_request_t *r, ngx_rtmp_session_t *s) static const char * ngx_rtmp_control_redirect_handler(ngx_http_request_t *r, ngx_rtmp_session_t *s) { - u_char *n; - ngx_str_t name; - ngx_rtmp_control_ctx_t *ctx; + ngx_str_t name; + ngx_rtmp_play_t vplay; + ngx_rtmp_publish_t vpublish; + ngx_rtmp_live_ctx_t *lctx; + ngx_rtmp_control_ctx_t *ctx; + ngx_rtmp_close_stream_t vc; ngx_str_null(&name); ngx_http_arg(r, (u_char *) "newname", sizeof("newname") - 1, &name); @@ -207,21 +210,47 @@ ngx_rtmp_control_redirect_handler(ngx_http_request_t *r, ngx_rtmp_session_t *s) return "newname not specified"; } - n = ngx_palloc(r->pool, name.len + 1); - if (n == NULL) { - return "allocation failed"; + if (name.len >= NGX_RTMP_MAX_NAME) { + name.len = NGX_RTMP_MAX_NAME - 1; } - ngx_memcpy(n, name.data, name.len); - - n[name.len] = 0; - - ngx_rtmp_live_redirect(s, n); - ctx = ngx_http_get_module_ctx(r, ngx_rtmp_control_module); - ctx->count++; + ngx_memzero(&vc, sizeof(ngx_rtmp_close_stream_t)); + + /* close_stream should be synchronous */ + ngx_rtmp_close_stream(s, &vc); + + lctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module); + + if (lctx && lctx->publishing) { + /* publish */ + + ngx_memzero(&vpublish, sizeof(ngx_rtmp_publish_t)); + + ngx_memcpy(vpublish.name, name.data, name.len); + + ngx_rtmp_cmd_fill_args(vpublish.name, vpublish.args); + + if (ngx_rtmp_publish(s, &vpublish) != NGX_OK) { + return "publish failed"; + } + + } else { + /* play */ + + ngx_memzero(&vplay, sizeof(ngx_rtmp_play_t)); + + ngx_memcpy(vplay.name, name.data, name.len); + + ngx_rtmp_cmd_fill_args(vplay.name, vplay.args); + + if (ngx_rtmp_play(s, &vplay) != NGX_OK) { + return "play failed"; + } + } + return NGX_CONF_OK; }