Skip to content

Commit 213f15f

Browse files
committed
[fix] fix on_connect command bug.
1 parent 0f631b6 commit 213f15f

File tree

7 files changed

+107
-113
lines changed

7 files changed

+107
-113
lines changed

ngx_http_flv_live_module.c

Lines changed: 87 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "ngx_rtmp_notify_module.h"
1212

1313

14-
static ngx_rtmp_connect_pt next_connect;
1514
static ngx_rtmp_play_pt next_play;
1615
static ngx_rtmp_close_stream_pt next_close_stream;
1716

@@ -183,14 +182,13 @@ static ngx_int_t ngx_http_flv_live_request(ngx_rtmp_session_t *s,
183182
ngx_rtmp_header_t *h, ngx_chain_t *in);
184183

185184
static void ngx_http_flv_live_stop(ngx_rtmp_session_t *s);
186-
static ngx_int_t ngx_http_flv_live_connect(ngx_rtmp_session_t *s,
187-
ngx_rtmp_connect_t *v);
188185
static ngx_int_t ngx_http_flv_live_play(ngx_rtmp_session_t *s,
189186
ngx_rtmp_play_t *v);
190187
static ngx_int_t ngx_http_flv_live_close_stream(ngx_rtmp_session_t *s,
191188
ngx_rtmp_close_stream_t *v);
192189

193190

191+
static void ngx_http_flv_live_play_handler(ngx_event_t *ev);
194192
static void ngx_http_flv_live_read_handler(ngx_event_t *rev);
195193
static void ngx_http_flv_live_write_handler(ngx_event_t *wev);
196194

@@ -309,9 +307,6 @@ ngx_http_flv_live_init_handlers(ngx_cycle_t *cycle)
309307
h = ngx_array_push(&cmcf->events[NGX_HTTP_FLV_LIVE_REQUEST]);
310308
*h = ngx_http_flv_live_request;
311309

312-
next_connect = ngx_rtmp_connect;
313-
ngx_rtmp_connect = ngx_http_flv_live_connect;
314-
315310
next_play = ngx_rtmp_play;
316311
ngx_rtmp_play = ngx_http_flv_live_play;
317312

@@ -920,6 +915,12 @@ ngx_http_flv_live_request(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
920915

921916
ngx_http_request_t *r;
922917
ngx_http_flv_live_ctx_t *ctx;
918+
ngx_rtmp_notify_srv_conf_t *nscf;
919+
920+
nscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_notify_module);
921+
if (nscf && nscf->url[NGX_RTMP_NOTIFY_CONNECT]) {
922+
s->wait_notify_connect = 1;
923+
}
923924

924925
r = s->data;
925926
ctx = ngx_http_get_module_ctx(r, ngx_http_flv_live_module);
@@ -943,6 +944,21 @@ ngx_http_flv_live_request(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
943944
(ngx_int_t) v.duration, (ngx_int_t) v.reset,
944945
(ngx_int_t) v.silent);
945946

947+
if (s->wait_notify_connect) {
948+
ngx_memzero(&ctx->play, sizeof(ngx_event_t));
949+
ctx->play.handler = ngx_http_flv_live_play_handler;
950+
ctx->play.log = s->connection->log;
951+
ctx->play.data = s->connection;
952+
953+
ngx_http_flv_live_play_handler(&ctx->play);
954+
955+
if (r->main->blocked == 0) {
956+
r->blocked++;
957+
}
958+
959+
return NGX_OK;
960+
}
961+
946962
return ngx_rtmp_play(s, &v);
947963
}
948964

@@ -1068,7 +1084,7 @@ ngx_http_flv_live_join(ngx_rtmp_session_t *s, u_char *name,
10681084
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
10691085
"flv live: no racf or relay pulls, check on_play");
10701086

1071-
if (!s->wait_notification) {
1087+
if (!s->wait_notify_play) {
10721088
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
10731089
"flv live: no relay pulls and no on_play, quit");
10741090

@@ -1103,75 +1119,6 @@ ngx_http_flv_live_join(ngx_rtmp_session_t *s, u_char *name,
11031119
}
11041120

11051121

1106-
ngx_int_t
1107-
ngx_http_flv_live_connect(ngx_rtmp_session_t *s, ngx_rtmp_connect_t *v)
1108-
{
1109-
ngx_rtmp_core_srv_conf_t *cscf;
1110-
ngx_rtmp_core_app_conf_t **cacfp;
1111-
ngx_uint_t n;
1112-
u_char *p;
1113-
1114-
#define NGX_RTMP_SET_STRPAR(name) \
1115-
do { \
1116-
if (s->name.len != ngx_strlen(v->name) \
1117-
|| ngx_strncasecmp(s->name.data, v->name, ngx_strlen(v->name))) \
1118-
{ \
1119-
s->name.len = ngx_strlen(v->name); \
1120-
s->name.data = ngx_palloc(s->connection->pool, \
1121-
ngx_strlen(v->name)); \
1122-
ngx_memcpy(s->name.data, v->name, ngx_strlen(v->name)); \
1123-
} \
1124-
} while (0)
1125-
1126-
NGX_RTMP_SET_STRPAR(app);
1127-
NGX_RTMP_SET_STRPAR(args);
1128-
NGX_RTMP_SET_STRPAR(flashver);
1129-
NGX_RTMP_SET_STRPAR(swf_url);
1130-
NGX_RTMP_SET_STRPAR(tc_url);
1131-
NGX_RTMP_SET_STRPAR(page_url);
1132-
1133-
#undef NGX_RTMP_SET_STRPAR
1134-
1135-
if (s->auto_pushed) {
1136-
s->host_start = v->server_name;
1137-
s->host_end = v->server_name + ngx_strlen(v->server_name);
1138-
}
1139-
1140-
if (ngx_rtmp_process_virtual_host(s) != NGX_OK) {
1141-
return NGX_ERROR;
1142-
}
1143-
1144-
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
1145-
1146-
p = ngx_strlchr(s->app.data, s->app.data + s->app.len, '?');
1147-
if (p) {
1148-
s->app.len = (p - s->app.data);
1149-
}
1150-
1151-
/* find application & set app_conf */
1152-
cacfp = cscf->applications.elts;
1153-
for(n = 0; n < cscf->applications.nelts; ++n, ++cacfp) {
1154-
if ((*cacfp)->name.len == s->app.len &&
1155-
ngx_strncmp((*cacfp)->name.data, s->app.data, s->app.len) == 0)
1156-
{
1157-
/* found app! */
1158-
s->app_conf = (*cacfp)->app_conf;
1159-
s->valid_application = 1;
1160-
s->app_found = 1;
1161-
break;
1162-
}
1163-
}
1164-
1165-
if (s->app_conf == NULL) {
1166-
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
1167-
"connect: application not found: '%V'", &s->app);
1168-
return NGX_ERROR;
1169-
}
1170-
1171-
return next_connect(s, v);
1172-
}
1173-
1174-
11751122
ngx_int_t
11761123
ngx_http_flv_live_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
11771124
{
@@ -1184,23 +1131,26 @@ ngx_http_flv_live_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
11841131
goto next;
11851132
}
11861133

1134+
nacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_notify_module);
1135+
if (nacf && nacf->url[NGX_RTMP_NOTIFY_PLAY]) {
1136+
s->wait_notify_play = 1;
1137+
}
1138+
11871139
r = s->data;
1188-
if (r == NULL) {
1140+
if (r == NULL || s->wait_notify_play) {
11891141
goto next;
11901142
}
11911143

1192-
r->main->blocked++;
1193-
1194-
nacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_notify_module);
1195-
if (nacf && nacf->url[NGX_RTMP_NOTIFY_PLAY]) {
1196-
s->wait_notification = 1;
1197-
goto next;
1144+
if (r->main->blocked == 0) {
1145+
r->main->blocked++;
11981146
}
11991147

12001148
/* join stream as a subscriber */
12011149

12021150
if (ngx_http_flv_live_join(s, v->name, 0) == NGX_ERROR) {
1203-
r->main->blocked--;
1151+
if (r->main->blocked) {
1152+
r->main->blocked--;
1153+
}
12041154

12051155
return NGX_ERROR;
12061156
}
@@ -1228,7 +1178,9 @@ ngx_http_flv_live_close_http_request(ngx_rtmp_session_t *s)
12281178

12291179
r = s->data;
12301180
if (r && r->connection && !r->connection->destroyed) {
1231-
r->main->blocked--;
1181+
if (r->main->blocked) {
1182+
r->main->blocked--;
1183+
}
12321184

12331185
if (r->chunked) {
12341186
ngx_http_flv_live_send_tail(s);
@@ -1334,6 +1286,56 @@ ngx_http_flv_live_close_stream(ngx_rtmp_session_t *s,
13341286
}
13351287

13361288

1289+
void
1290+
ngx_http_flv_live_play_handler(ngx_event_t *ev)
1291+
{
1292+
static ngx_rtmp_play_t v;
1293+
1294+
ngx_connection_t *c;
1295+
ngx_http_request_t *r;
1296+
ngx_rtmp_session_t *s;
1297+
ngx_http_flv_live_ctx_t *ctx;
1298+
1299+
c = ev->data;
1300+
r = c->data;
1301+
1302+
ctx = ngx_http_get_module_ctx(r, ngx_http_flv_live_module);
1303+
s = ctx->s;
1304+
1305+
if (c->destroyed) {
1306+
return;
1307+
}
1308+
1309+
if (!s->wait_notify_connect) {
1310+
if (ev->timer_set) {
1311+
ngx_del_timer(ev);
1312+
}
1313+
1314+
ngx_memzero(&v, sizeof(ngx_rtmp_play_t));
1315+
1316+
ngx_memcpy(v.name, ctx->stream.data, ngx_min(ctx->stream.len,
1317+
sizeof(v.name) - 1));
1318+
ngx_memcpy(v.args, s->args.data, ngx_min(s->args.len,
1319+
sizeof(v.args) - 1));
1320+
1321+
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
1322+
"flv live: name='%s' args='%s' start='%i' duration='%i' "
1323+
"reset='%i' silent='%i'",
1324+
v.name, v.args, (ngx_int_t) v.start,
1325+
(ngx_int_t) v.duration, (ngx_int_t) v.reset,
1326+
(ngx_int_t) v.silent);
1327+
1328+
if (r->main->blocked) {
1329+
r->blocked--;
1330+
}
1331+
1332+
ngx_rtmp_play(s, &v);
1333+
} else {
1334+
ngx_add_timer(ev, 20);
1335+
}
1336+
}
1337+
1338+
13371339
void
13381340
ngx_http_flv_live_read_handler(ngx_event_t *rev)
13391341
{

ngx_http_flv_live_module.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ typedef struct ngx_http_flv_live_ctx_s {
3636
ngx_str_t app;
3737
ngx_str_t port;
3838
ngx_str_t stream;
39+
40+
ngx_event_t play;
3941
} ngx_http_flv_live_ctx_t;
4042

4143

ngx_rtmp.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,10 +1111,6 @@ ngx_rtmp_process_virtual_host(ngx_rtmp_session_t *s)
11111111
ngx_str_t host;
11121112
ngx_str_t hschema, rschema, *schema;
11131113

1114-
if (s->app_found) {
1115-
return NGX_OK;
1116-
}
1117-
11181114
if (s->auto_pushed) {
11191115
goto next;
11201116
}

ngx_rtmp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,9 @@ struct ngx_rtmp_session_s {
378378
ngx_pool_t *out_pool;
379379
ngx_pool_t *out_temp_pool;
380380

381-
unsigned app_found:1;
382381
unsigned server_changed:1;
383-
unsigned wait_notification:1;
382+
unsigned wait_notify_connect:1;
383+
unsigned wait_notify_play:1;
384384

385385
/* input stream 0 (reserved by RTMP spec)
386386
* is used as free chain link */

ngx_rtmp_cmd_module.c

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -276,16 +276,9 @@ ngx_rtmp_cmd_connect(ngx_rtmp_session_t *s, ngx_rtmp_connect_t *v)
276276

277277

278278
#define NGX_RTMP_SET_STRPAR(name) \
279-
do { \
280-
if (s->name.len != ngx_strlen(v->name) \
281-
|| ngx_strncasecmp(s->name.data, v->name, ngx_strlen(v->name))) \
282-
{ \
283-
s->name.len = ngx_strlen(v->name); \
284-
s->name.data = ngx_palloc(s->connection->pool, \
285-
ngx_strlen(v->name)); \
286-
ngx_memcpy(s->name.data, v->name, ngx_strlen(v->name)); \
287-
} \
288-
} while (0)
279+
s->name.len = ngx_strlen(v->name); \
280+
s->name.data = ngx_palloc(s->connection->pool, s->name.len); \
281+
ngx_memcpy(s->name.data, v->name, s->name.len)
289282

290283
NGX_RTMP_SET_STRPAR(app);
291284
NGX_RTMP_SET_STRPAR(args);
@@ -307,15 +300,14 @@ ngx_rtmp_cmd_connect(ngx_rtmp_session_t *s, ngx_rtmp_connect_t *v)
307300

308301
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
309302

310-
if (s->app_found) {
311-
goto next;
312-
}
313-
314303
p = ngx_strlchr(s->app.data, s->app.data + s->app.len, '?');
315304
if (p) {
316305
s->app.len = (p - s->app.data);
317306
}
318307

308+
s->acodecs = (uint32_t) v->acodecs;
309+
s->vcodecs = (uint32_t) v->vcodecs;
310+
319311
/* find application & set app_conf */
320312
cacfp = cscf->applications.elts;
321313
for(n = 0; n < cscf->applications.nelts; ++n, ++cacfp) {
@@ -325,7 +317,6 @@ ngx_rtmp_cmd_connect(ngx_rtmp_session_t *s, ngx_rtmp_connect_t *v)
325317
/* found app! */
326318
s->app_conf = (*cacfp)->app_conf;
327319
s->valid_application = 1;
328-
s->app_found = 1;
329320
break;
330321
}
331322
}
@@ -336,12 +327,12 @@ ngx_rtmp_cmd_connect(ngx_rtmp_session_t *s, ngx_rtmp_connect_t *v)
336327
return NGX_ERROR;
337328
}
338329

339-
next:
340-
s->acodecs = (uint32_t) v->acodecs;
341-
s->vcodecs = (uint32_t) v->vcodecs;
342-
343330
object_encoding = v->object_encoding;
344331

332+
if (s->wait_notify_connect) {
333+
s->wait_notify_connect = 0;
334+
}
335+
345336
return ngx_rtmp_send_ack_size(s, cscf->ack_window) != NGX_OK ||
346337
ngx_rtmp_send_bandwidth(s, cscf->ack_window,
347338
NGX_RTMP_LIMIT_DYNAMIC) != NGX_OK ||

ngx_rtmp_live_module.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,11 +1493,14 @@ ngx_rtmp_live_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
14931493
r = s->data;
14941494
if (r) {
14951495
if (ngx_http_flv_live_join(s, v->name, 0) == NGX_ERROR) {
1496-
r->main->blocked--;
1496+
if (r->main->blocked) {
1497+
r->main->blocked--;
1498+
}
1499+
14971500
return NGX_ERROR;
14981501
}
14991502

1500-
s->wait_notification = 0;
1503+
s->wait_notify_play = 0;
15011504

15021505
goto next;
15031506
}

ngx_rtmp_notify_module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ ngx_rtmp_notify_play_handle(ngx_rtmp_session_t *s,
11081108
u->url.len = rc - 7;
11091109
u->default_port = 1935;
11101110
u->uri_part = 1;
1111-
u->no_resolve = (s->wait_notification ? 0 : 1); /* want ip here */
1111+
u->no_resolve = (s->wait_notify_play ? 0 : 1); /* want ip here */
11121112

11131113
if (ngx_parse_url(s->connection->pool, u) != NGX_OK) {
11141114
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,

0 commit comments

Comments
 (0)