Skip to content

Commit 231b215

Browse files
committed
oclp publish and oclp play need to wait respone to decide continue or disconnect
1 parent 3da5ac4 commit 231b215

File tree

5 files changed

+145
-54
lines changed

5 files changed

+145
-54
lines changed

http/ngx_http_flv_live_module.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,6 @@ ngx_http_flv_live_write_handler(ngx_http_request_t *r)
276276
ngx_del_timer(wev);
277277
}
278278

279-
rc = ngx_http_flv_live_send_header(r);
280-
if (rc == NGX_ERROR || rc > NGX_OK) {
281-
ngx_http_finalize_request(r, rc);
282-
return;
283-
}
284-
285279
ctx = ngx_http_get_module_ctx(r, ngx_http_flv_live_module);
286280
s = ctx->session;
287281

@@ -290,6 +284,14 @@ ngx_http_flv_live_write_handler(ngx_http_request_t *r)
290284
return;
291285
}
292286

287+
if (s->out_chain) {
288+
rc = ngx_http_flv_live_send_header(r);
289+
if (rc == NGX_ERROR || rc > NGX_OK) {
290+
ngx_http_finalize_request(r, rc);
291+
return;
292+
}
293+
}
294+
293295
while (s->out_chain) {
294296
present = r->connection->sent;
295297

ngx_rtmp_cmd_module.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,6 @@ ngx_rtmp_publish_filter(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
223223

224224
s->published = 1;
225225

226-
ngx_rtmp_oclp_pnotify_start(s, 1);
227-
228226
if (!s->relay) { /* relay pull */
229227
ngx_rtmp_cmd_stream_init(s, v->name, v->args, 1);
230228
}
@@ -244,8 +242,6 @@ ngx_rtmp_play_filter(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
244242

245243
s->played = 1;
246244

247-
ngx_rtmp_oclp_pnotify_start(s, 0);
248-
249245
if (!s->relay) { /* relay push */
250246
ngx_rtmp_cmd_stream_init(s, v->name, v->args, 0);
251247
}
@@ -277,8 +273,6 @@ ngx_rtmp_close_stream_filter(ngx_rtmp_session_t *s,
277273
ngx_live_delete_stream(&s->serverid, &s->stream);
278274
}
279275

280-
ngx_rtmp_oclp_pnotify_done(s);
281-
282276
return ngx_rtmp_close_stream(s, v);
283277
}
284278

ngx_rtmp_init.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ ngx_rtmp_close_session_handler(ngx_event_t *e)
220220

221221
s = e->data;
222222
c = s->connection;
223+
if (c) {
224+
c->destroyed = 1;
225+
}
223226

224227
ngx_log_error(NGX_LOG_INFO, s->log, 0, "async close session");
225228

@@ -336,9 +339,6 @@ ngx_rtmp_finalize_session(ngx_rtmp_session_t *s)
336339
return;
337340
}
338341

339-
if (c) {
340-
c->destroyed = 1;
341-
}
342342
e = &s->close;
343343
e->data = s;
344344
e->handler = ngx_rtmp_close_session_handler;

ngx_rtmp_oclp_module.c

Lines changed: 134 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@
88
#include <ngx_config.h>
99
#include <ngx_core.h>
1010
#include "ngx_rtmp.h"
11+
#include "ngx_rtmp_cmd_module.h"
1112
#include "ngx_live_relay.h"
1213
#include "ngx_dynamic_resolver.h"
1314
#include "ngx_toolkit_misc.h"
1415
#include "ngx_netcall.h"
1516

1617

18+
static ngx_rtmp_publish_pt next_publish;
19+
static ngx_rtmp_play_pt next_play;
20+
static ngx_rtmp_close_stream_pt next_close_stream;
21+
22+
1723
static ngx_live_push_pt next_push;
1824
static ngx_live_pull_pt next_pull;
1925
static ngx_live_push_close_pt next_push_close;
@@ -137,6 +143,9 @@ typedef struct {
137143
ngx_rtmp_oclp_event_t *event;
138144
ngx_uint_t type;
139145
ngx_live_relay_t *relay;
146+
147+
ngx_rtmp_publish_t publish_v;
148+
ngx_rtmp_play_t play_v;
140149
} ngx_rtmp_oclp_ctx_t;
141150

142151

@@ -815,85 +824,97 @@ static void
815824
ngx_rtmp_oclp_pnotify_start_handle(ngx_netcall_ctx_t *nctx, ngx_int_t code)
816825
{
817826
ngx_rtmp_session_t *s;
827+
ngx_rtmp_oclp_ctx_t *octx;
818828

819829
s = nctx->data;
820830

821-
if (code != NGX_HTTP_OK) {
831+
octx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_oclp_module);
832+
833+
if (code < NGX_HTTP_OK || code > NGX_HTTP_SPECIAL_RESPONSE) {
822834
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
823835
"oclp %s start notify error: %i",
824836
ngx_rtmp_oclp_app_type[nctx->type], code);
825837

826838
if (code != -1) {
827-
if (nctx->type == NGX_RTMP_OCLP_PUBLISH) {
828-
ngx_rtmp_send_status(s, "NetStream.Publish.Forbidden", "status",
829-
"Publish stream Forbidden");
830-
} else {
831-
s->status = 403;
832-
ngx_rtmp_send_status(s, "NetStream.Play.Forbidden", "status",
833-
"Play stream Forbidden");
834-
}
835-
ngx_rtmp_finalize_session(s);
839+
goto error;
836840
}
837841

838-
return;
842+
goto next;
843+
}
844+
845+
next:
846+
if (octx->type == NGX_RTMP_OCLP_PUBLISH) {
847+
if (next_publish(s, &octx->publish_v) != NGX_OK) {
848+
goto error;
849+
}
850+
} else {
851+
if (next_play(s, &octx->play_v) != NGX_OK) {
852+
goto error;
853+
}
839854
}
840855

841856
ngx_rtmp_oclp_common_update_create(s, nctx);
857+
858+
return;
859+
860+
error:
861+
if (octx->type == NGX_RTMP_OCLP_PUBLISH) {
862+
ngx_rtmp_send_status(s, "NetStream.Publish.Forbidden", "status",
863+
"Publish stream Forbidden");
864+
} else {
865+
s->status = 403;
866+
ngx_rtmp_send_status(s, "NetStream.Play.Forbidden", "status",
867+
"Play stream Forbidden");
868+
}
869+
ngx_rtmp_finalize_session(s);
842870
}
843871

844-
void
845-
ngx_rtmp_oclp_pnotify_start(ngx_rtmp_session_t *s, unsigned publishing)
872+
static ngx_int_t
873+
ngx_rtmp_oclp_pnotify_start(ngx_rtmp_session_t *s, ngx_uint_t type)
846874
{
847875
ngx_rtmp_oclp_app_conf_t *oacf;
848876
ngx_rtmp_oclp_event_t *event;
849877
ngx_rtmp_oclp_ctx_t *ctx;
850878
ngx_netcall_ctx_t *nctx;
851-
ngx_uint_t type;
852-
853-
if (s->relay || s->interprocess) {
854-
return;
855-
}
856-
857-
type = publishing? NGX_RTMP_OCLP_PUBLISH: NGX_RTMP_OCLP_PLAY;
858879

859880
oacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_oclp_module);
860881

861882
if (oacf->events[type].nelts == 0) {
862-
return;
883+
return NGX_DECLINED;
863884
}
864885

865886
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_oclp_module);
866887
if (ctx == NULL) {
867888
ctx = ngx_pcalloc(s->pool, sizeof(ngx_rtmp_oclp_ctx_t));
868889
if (ctx == NULL) {
869-
return;
890+
ngx_log_error(NGX_LOG_ERR, s->log, 0, "palloc oclp ctx failed");
891+
return NGX_ERROR;
870892
}
871893
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_oclp_module);
872894
}
873895

874896
event = oacf->events[type].elts;
875897

876-
if (oacf->events[type].nelts &&
877-
(event->stage & NGX_RTMP_OCLP_START) == NGX_RTMP_OCLP_START)
878-
{
879-
nctx = ngx_netcall_create_ctx(type, &event->groupid,
880-
event->stage, event->timeout, event->update, 0);
898+
nctx = ngx_netcall_create_ctx(type, &event->groupid,
899+
event->stage, event->timeout, event->update, 0);
881900

882-
ngx_rtmp_oclp_common_url(&nctx->url, s, event, nctx,
883-
NGX_RTMP_OCLP_START);
884-
nctx->handler = ngx_rtmp_oclp_pnotify_start_handle;
885-
nctx->data = s;
901+
ngx_rtmp_oclp_common_url(&nctx->url, s, event, nctx,
902+
NGX_RTMP_OCLP_START);
903+
nctx->handler = ngx_rtmp_oclp_pnotify_start_handle;
904+
nctx->data = s;
886905

887-
ctx->nctx = nctx;
906+
ctx->nctx = nctx;
907+
ctx->type = type;
888908

889-
ngx_log_error(NGX_LOG_INFO, s->log, 0, "oclp %s start create %V",
890-
ngx_rtmp_oclp_app_type[nctx->type], &nctx->url);
909+
ngx_log_error(NGX_LOG_INFO, s->log, 0, "oclp %s start create %V",
910+
ngx_rtmp_oclp_app_type[nctx->type], &nctx->url);
891911

892-
ngx_netcall_create(nctx, s->log);
893-
}
912+
ngx_netcall_create(nctx, s->log);
913+
914+
return NGX_OK;
894915
}
895916

896-
void
917+
static void
897918
ngx_rtmp_oclp_pnotify_done(ngx_rtmp_session_t *s)
898919
{
899920
ngx_rtmp_oclp_ctx_t *ctx;
@@ -1459,6 +1480,73 @@ ngx_rtmp_oclp_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in)
14591480
return NGX_OK;
14601481
}
14611482

1483+
static ngx_int_t
1484+
ngx_rtmp_oclp_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
1485+
{
1486+
ngx_rtmp_oclp_ctx_t *octx;
1487+
1488+
if (s->relay || s->interprocess) {
1489+
goto next;
1490+
}
1491+
1492+
switch (ngx_rtmp_oclp_pnotify_start(s, NGX_RTMP_OCLP_PUBLISH)) {
1493+
case NGX_OK:
1494+
break;
1495+
case NGX_DECLINED:
1496+
goto next;
1497+
default:
1498+
return NGX_ERROR;
1499+
}
1500+
1501+
octx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_oclp_module);
1502+
ngx_memcpy(&octx->publish_v, v, sizeof(ngx_rtmp_publish_t));
1503+
1504+
return NGX_OK;
1505+
1506+
next:
1507+
return next_publish(s, v);
1508+
}
1509+
1510+
static ngx_int_t
1511+
ngx_rtmp_oclp_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
1512+
{
1513+
ngx_rtmp_oclp_ctx_t *octx;
1514+
1515+
if (s->relay || s->interprocess) {
1516+
goto next;
1517+
}
1518+
1519+
switch (ngx_rtmp_oclp_pnotify_start(s, NGX_RTMP_OCLP_PLAY)) {
1520+
case NGX_OK:
1521+
break;
1522+
case NGX_DECLINED:
1523+
goto next;
1524+
default:
1525+
return NGX_ERROR;
1526+
}
1527+
1528+
octx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_oclp_module);
1529+
ngx_memcpy(&octx->play_v, v, sizeof(ngx_rtmp_play_t));
1530+
1531+
return NGX_OK;
1532+
1533+
next:
1534+
return next_play(s, v);
1535+
}
1536+
1537+
static ngx_int_t
1538+
ngx_rtmp_oclp_close_stream(ngx_rtmp_session_t *s, ngx_rtmp_close_stream_t *v)
1539+
{
1540+
if (s->relay || s->interprocess) {
1541+
goto next;
1542+
}
1543+
1544+
ngx_rtmp_oclp_pnotify_done(s);
1545+
1546+
next:
1547+
return next_close_stream(s, v);
1548+
}
1549+
14621550
static ngx_int_t
14631551
ngx_rtmp_oclp_postconfiguration(ngx_conf_t *cf)
14641552
{
@@ -1485,5 +1573,14 @@ ngx_rtmp_oclp_postconfiguration(ngx_conf_t *cf)
14851573
next_pull_close = ngx_live_pull_close;
14861574
ngx_live_pull_close = ngx_rtmp_oclp_pull_close;
14871575

1576+
next_publish = ngx_rtmp_publish;
1577+
ngx_rtmp_publish = ngx_rtmp_oclp_publish;
1578+
1579+
next_play = ngx_rtmp_play;
1580+
ngx_rtmp_play = ngx_rtmp_oclp_play;
1581+
1582+
next_close_stream = ngx_rtmp_close_stream;
1583+
ngx_rtmp_close_stream = ngx_rtmp_oclp_close_stream;
1584+
14881585
return NGX_OK;
14891586
}

ngx_rtmp_oclp_module.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include "ngx_netcall.h"
1313

1414

15-
void ngx_rtmp_oclp_pnotify_done(ngx_rtmp_session_t *s);
16-
void ngx_rtmp_oclp_pnotify_start(ngx_rtmp_session_t *s, unsigned publishing);
1715
void ngx_rtmp_oclp_stream_start(ngx_rtmp_session_t *s);
1816
void ngx_rtmp_oclp_stream_done(ngx_rtmp_session_t *s);
1917

0 commit comments

Comments
 (0)