Skip to content

Commit ef424df

Browse files
committed
added control redirect
1 parent 5b68307 commit ef424df

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

ngx_rtmp_control_module.c

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,12 @@ ngx_rtmp_control_drop_handler(ngx_http_request_t *r, ngx_rtmp_session_t *s)
196196
static const char *
197197
ngx_rtmp_control_redirect_handler(ngx_http_request_t *r, ngx_rtmp_session_t *s)
198198
{
199-
u_char *n;
200-
ngx_str_t name;
201-
ngx_rtmp_control_ctx_t *ctx;
199+
ngx_str_t name;
200+
ngx_rtmp_play_t vplay;
201+
ngx_rtmp_publish_t vpublish;
202+
ngx_rtmp_live_ctx_t *lctx;
203+
ngx_rtmp_control_ctx_t *ctx;
204+
ngx_rtmp_close_stream_t vc;
202205

203206
ngx_str_null(&name);
204207
ngx_http_arg(r, (u_char *) "newname", sizeof("newname") - 1, &name);
@@ -207,20 +210,46 @@ ngx_rtmp_control_redirect_handler(ngx_http_request_t *r, ngx_rtmp_session_t *s)
207210
return "newname not specified";
208211
}
209212

210-
n = ngx_palloc(r->pool, name.len + 1);
211-
if (n == NULL) {
212-
return "allocation failed";
213+
if (name.len >= NGX_RTMP_MAX_NAME) {
214+
name.len = NGX_RTMP_MAX_NAME - 1;
213215
}
214216

215-
ngx_memcpy(n, name.data, name.len);
217+
ctx = ngx_http_get_module_ctx(r, ngx_rtmp_control_module);
218+
ctx->count++;
216219

217-
n[name.len] = 0;
220+
ngx_memzero(&vc, sizeof(ngx_rtmp_close_stream_t));
218221

219-
ngx_rtmp_live_redirect(s, n);
222+
/* close_stream should be synchronous */
223+
ngx_rtmp_close_stream(s, &vc);
220224

221-
ctx = ngx_http_get_module_ctx(r, ngx_rtmp_control_module);
225+
lctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_live_module);
222226

223-
ctx->count++;
227+
if (lctx && lctx->publishing) {
228+
/* publish */
229+
230+
ngx_memzero(&vpublish, sizeof(ngx_rtmp_publish_t));
231+
232+
ngx_memcpy(vpublish.name, name.data, name.len);
233+
234+
ngx_rtmp_cmd_fill_args(vpublish.name, vpublish.args);
235+
236+
if (ngx_rtmp_publish(s, &vpublish) != NGX_OK) {
237+
return "publish failed";
238+
}
239+
240+
} else {
241+
/* play */
242+
243+
ngx_memzero(&vplay, sizeof(ngx_rtmp_play_t));
244+
245+
ngx_memcpy(vplay.name, name.data, name.len);
246+
247+
ngx_rtmp_cmd_fill_args(vplay.name, vplay.args);
248+
249+
if (ngx_rtmp_play(s, &vplay) != NGX_OK) {
250+
return "play failed";
251+
}
252+
}
224253

225254
return NGX_CONF_OK;
226255
}

0 commit comments

Comments
 (0)