Skip to content

Commit 0fb3ce4

Browse files
committed
implemented idle_streams feature
1 parent 8f9965b commit 0fb3ce4

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

ngx_rtmp_live_module.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ static ngx_command_t ngx_rtmp_live_commands[] = {
9494
offsetof(ngx_rtmp_live_app_conf_t, play_restart),
9595
NULL },
9696

97+
{ ngx_string("idle_streams"),
98+
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
99+
ngx_conf_set_flag_slot,
100+
NGX_RTMP_APP_CONF_OFFSET,
101+
offsetof(ngx_rtmp_live_app_conf_t, idle_streams),
102+
NULL },
103+
97104
{ ngx_string("drop_idle_publisher"),
98105
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
99106
ngx_rtmp_live_set_msec_slot,
@@ -153,6 +160,7 @@ ngx_rtmp_live_create_app_conf(ngx_conf_t *cf)
153160
lacf->wait_video = NGX_CONF_UNSET;
154161
lacf->publish_notify = NGX_CONF_UNSET;
155162
lacf->play_restart = NGX_CONF_UNSET;
163+
lacf->idle_streams = NGX_CONF_UNSET;
156164

157165
return lacf;
158166
}
@@ -174,6 +182,7 @@ ngx_rtmp_live_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
174182
ngx_conf_merge_value(conf->wait_video, prev->wait_video, 0);
175183
ngx_conf_merge_value(conf->publish_notify, prev->publish_notify, 0);
176184
ngx_conf_merge_value(conf->play_restart, prev->play_restart, 0);
185+
ngx_conf_merge_value(conf->idle_streams, prev->idle_streams, 1);
177186

178187
conf->pool = ngx_create_pool(4096, &cf->cycle->new_log);
179188
if (conf->pool == NULL) {
@@ -505,8 +514,19 @@ ngx_rtmp_live_join(ngx_rtmp_session_t *s, u_char *name, unsigned publisher)
505514
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
506515
"live: join '%s'", name);
507516

508-
stream = ngx_rtmp_live_get_stream(s, name, 1);
509-
if (stream == NULL) {
517+
stream = ngx_rtmp_live_get_stream(s, name, publisher || lacf->idle_streams);
518+
519+
if (stream == NULL ||
520+
!(publisher || (*stream)->publishing || lacf->idle_streams))
521+
{
522+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
523+
"live: stream not found");
524+
525+
ngx_rtmp_send_status(s, "NetStream.Play.StreamNotFound", "error",
526+
"No such stream");
527+
528+
ngx_rtmp_finalize_session(s);
529+
510530
return;
511531
}
512532

@@ -546,7 +566,8 @@ ngx_rtmp_live_join(ngx_rtmp_session_t *s, u_char *name, unsigned publisher)
546566
static ngx_int_t
547567
ngx_rtmp_live_close_stream(ngx_rtmp_session_t *s, ngx_rtmp_close_stream_t *v)
548568
{
549-
ngx_rtmp_live_ctx_t *ctx, **cctx;
569+
ngx_rtmp_session_t *ss;
570+
ngx_rtmp_live_ctx_t *ctx, **cctx, *pctx;
550571
ngx_rtmp_live_stream_t **stream;
551572
ngx_rtmp_live_app_conf_t *lacf;
552573

@@ -589,6 +610,17 @@ ngx_rtmp_live_close_stream(ngx_rtmp_session_t *s, ngx_rtmp_close_stream_t *v)
589610
"status", "Stop publishing");
590611
}
591612

613+
if (!lacf->idle_streams) {
614+
for (pctx = ctx->stream->ctx; pctx; pctx = pctx->next) {
615+
if (pctx->publishing == 0) {
616+
ss = pctx->session;
617+
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0,
618+
"live: no publisher");
619+
ngx_rtmp_finalize_session(ss);
620+
}
621+
}
622+
}
623+
592624
if (ctx->stream->ctx) {
593625
ctx->stream = NULL;
594626
goto next;

ngx_rtmp_live_module.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ typedef struct {
6868
ngx_flag_t wait_video;
6969
ngx_flag_t publish_notify;
7070
ngx_flag_t play_restart;
71+
ngx_flag_t idle_streams;
7172
ngx_msec_t buflen;
7273
ngx_pool_t *pool;
7374
ngx_rtmp_live_stream_t *free_streams;

0 commit comments

Comments
 (0)