1111#include "ngx_rtmp_notify_module.h"
1212
1313
14- static ngx_rtmp_connect_pt next_connect ;
1514static ngx_rtmp_play_pt next_play ;
1615static 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
185184static 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 );
188185static ngx_int_t ngx_http_flv_live_play (ngx_rtmp_session_t * s ,
189186 ngx_rtmp_play_t * v );
190187static 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 );
194192static void ngx_http_flv_live_read_handler (ngx_event_t * rev );
195193static 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-
11751122ngx_int_t
11761123ngx_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+
13371339void
13381340ngx_http_flv_live_read_handler (ngx_event_t * rev )
13391341{
0 commit comments