@@ -17,21 +17,29 @@ static ngx_rtmp_delete_stream_pt next_delete_stream;
1717
1818
1919static char * ngx_rtmp_notify_on_event (ngx_conf_t * cf , ngx_command_t * cmd ,
20- void * conf );
20+ void * conf );
2121static ngx_int_t ngx_rtmp_notify_postconfiguration (ngx_conf_t * cf );
2222static void * ngx_rtmp_notify_create_app_conf (ngx_conf_t * cf );
2323static char * ngx_rtmp_notify_merge_app_conf (ngx_conf_t * cf ,
24- void * parent , void * child );
24+ void * parent , void * child );
25+ static ngx_int_t ngx_rtmp_notify_done (ngx_rtmp_session_t * s , char * cbname ,
26+ ngx_url_t * url );
2527
2628
29+ #define NGX_RTMP_NOTIFY_PUBLISHING 0x01
30+ #define NGX_RTMP_NOTIFY_PLAYING 0x02
31+
2732typedef struct {
2833 ngx_url_t * publish_url ;
2934 ngx_url_t * play_url ;
35+ ngx_url_t * publish_done_url ;
36+ ngx_url_t * play_done_url ;
3037 ngx_url_t * done_url ;
3138} ngx_rtmp_notify_app_conf_t ;
3239
3340
3441typedef struct {
42+ ngx_uint_t flags ;
3543 u_char name [NGX_RTMP_MAX_NAME ];
3644 u_char args [NGX_RTMP_MAX_ARGS ];
3745} ngx_rtmp_notify_ctx_t ;
@@ -53,6 +61,20 @@ static ngx_command_t ngx_rtmp_notify_commands[] = {
5361 0 ,
5462 NULL },
5563
64+ { ngx_string ("on_publish_done" ),
65+ NGX_RTMP_MAIN_CONF |NGX_RTMP_SRV_CONF |NGX_RTMP_APP_CONF |NGX_CONF_TAKE1 ,
66+ ngx_rtmp_notify_on_event ,
67+ NGX_RTMP_APP_CONF_OFFSET ,
68+ 0 ,
69+ NULL },
70+
71+ { ngx_string ("on_play_done" ),
72+ NGX_RTMP_MAIN_CONF |NGX_RTMP_SRV_CONF |NGX_RTMP_APP_CONF |NGX_CONF_TAKE1 ,
73+ ngx_rtmp_notify_on_event ,
74+ NGX_RTMP_APP_CONF_OFFSET ,
75+ 0 ,
76+ NULL },
77+
5678 { ngx_string ("on_done" ),
5779 NGX_RTMP_MAIN_CONF |NGX_RTMP_SRV_CONF |NGX_RTMP_APP_CONF |NGX_CONF_TAKE1 ,
5880 ngx_rtmp_notify_on_event ,
@@ -283,10 +305,12 @@ static ngx_chain_t *
283305ngx_rtmp_notify_done_create (ngx_rtmp_session_t * s , void * arg ,
284306 ngx_pool_t * pool )
285307{
308+ u_char * cbname = arg ;
309+
286310 ngx_rtmp_notify_app_conf_t * nacf ;
287311 ngx_chain_t * hl , * cl , * pl ;
288312 ngx_buf_t * b ;
289- size_t name_len , args_len ;
313+ size_t cbname_len , name_len , args_len ;
290314 ngx_str_t * addr_text ;
291315 ngx_rtmp_notify_ctx_t * ctx ;
292316
@@ -306,12 +330,13 @@ ngx_rtmp_notify_done_create(ngx_rtmp_session_t *s, void *arg,
306330 return NULL ;
307331 }
308332
333+ cbname_len = ngx_strlen (cbname );
309334 name_len = ctx ? ngx_strlen (ctx -> name ) : 0 ;
310335 args_len = ctx ? ngx_strlen (ctx -> args ) : 0 ;
311336 addr_text = & s -> connection -> addr_text ;
312337
313338 b = ngx_create_temp_buf (pool ,
314- sizeof ("&call=done " ) +
339+ sizeof ("&call=" ) + cbname_len +
315340 sizeof ("&addr=" ) + addr_text -> len +
316341 sizeof ("&name=" ) + name_len * 3
317342 + 1 + args_len );
@@ -417,22 +442,37 @@ ngx_rtmp_notify_play_handle(ngx_rtmp_session_t *s,
417442
418443
419444static void
420- ngx_rtmp_notify_save_name_args (ngx_rtmp_session_t * s ,
421- u_char name [NGX_RTMP_MAX_NAME ], u_char args [NGX_RTMP_MAX_ARGS ])
445+ ngx_rtmp_notify_init (ngx_rtmp_session_t * s ,
446+ u_char name [NGX_RTMP_MAX_NAME ], u_char args [NGX_RTMP_MAX_ARGS ],
447+ ngx_uint_t flags )
422448{
423449 ngx_rtmp_notify_ctx_t * ctx ;
450+ ngx_rtmp_notify_app_conf_t * nacf ;
451+
452+ nacf = ngx_rtmp_get_module_app_conf (s , ngx_rtmp_notify_module );
453+
454+ if (nacf -> done_url == NULL &&
455+ nacf -> play_done_url == NULL &&
456+ nacf -> publish_done_url == NULL )
457+ {
458+ return ;
459+ }
424460
425461 ctx = ngx_rtmp_get_module_ctx (s , ngx_rtmp_notify_module );
462+
426463 if (ctx == NULL ) {
427464 ctx = ngx_pcalloc (s -> connection -> pool , sizeof (ngx_rtmp_notify_ctx_t ));
428465 if (ctx == NULL ) {
429466 return ;
430467 }
468+
431469 ngx_rtmp_set_ctx (s , ctx , ngx_rtmp_notify_module );
432470 }
433471
434472 ngx_memcpy (ctx -> name , name , NGX_RTMP_MAX_NAME );
435473 ngx_memcpy (ctx -> args , args , NGX_RTMP_MAX_ARGS );
474+
475+ ctx -> flags |= flags ;
436476}
437477
438478
@@ -447,19 +487,22 @@ ngx_rtmp_notify_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
447487 }
448488
449489 nacf = ngx_rtmp_get_module_app_conf (s , ngx_rtmp_notify_module );
490+
450491 if (nacf == NULL ) {
451492 goto next ;
452493 }
453494
454- if (nacf -> done_url ) {
455- ngx_rtmp_notify_save_name_args (s , v -> name , v -> args );
456- }
495+ ngx_rtmp_notify_init (s , v -> name , v -> args , NGX_RTMP_NOTIFY_PUBLISHING );
457496
458497 if (nacf -> publish_url == NULL ) {
459498 goto next ;
460499 }
461500
501+ ngx_log_debug1 (NGX_LOG_DEBUG_RTMP , s -> connection -> log , 0 ,
502+ "notify: publish '%V'" , & nacf -> publish_url -> url );
503+
462504 ngx_memzero (& ci , sizeof (ci ));
505+
463506 ci .url = nacf -> publish_url ;
464507 ci .create = ngx_rtmp_notify_publish_create ;
465508 ci .handle = ngx_rtmp_notify_publish_handle ;
@@ -480,19 +523,22 @@ ngx_rtmp_notify_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
480523 ngx_rtmp_netcall_init_t ci ;
481524
482525 nacf = ngx_rtmp_get_module_app_conf (s , ngx_rtmp_notify_module );
526+
483527 if (nacf == NULL ) {
484528 goto next ;
485529 }
486530
487- if (nacf -> done_url ) {
488- ngx_rtmp_notify_save_name_args (s , v -> name , v -> args );
489- }
531+ ngx_rtmp_notify_init (s , v -> name , v -> args , NGX_RTMP_NOTIFY_PLAYING );
490532
491533 if (nacf -> play_url == NULL ) {
492534 goto next ;
493535 }
494536
537+ ngx_log_debug1 (NGX_LOG_DEBUG_RTMP , s -> connection -> log , 0 ,
538+ "notify: play '%V'" , & nacf -> play_url -> url );
539+
495540 ngx_memzero (& ci , sizeof (ci ));
541+
496542 ci .url = nacf -> play_url ;
497543 ci .create = ngx_rtmp_notify_play_create ;
498544 ci .handle = ngx_rtmp_notify_play_handle ;
@@ -510,32 +556,65 @@ static ngx_int_t
510556ngx_rtmp_notify_delete_stream (ngx_rtmp_session_t * s , ngx_rtmp_delete_stream_t
511557 * v )
512558{
559+ ngx_rtmp_notify_ctx_t * ctx ;
513560 ngx_rtmp_notify_app_conf_t * nacf ;
514- ngx_rtmp_netcall_init_t ci ;
561+
562+ ctx = ngx_rtmp_get_module_ctx (s , ngx_rtmp_notify_module );
563+
564+ if (ctx == NULL ) {
565+ goto next ;
566+ }
515567
516568 nacf = ngx_rtmp_get_module_app_conf (s , ngx_rtmp_notify_module );
517- if (nacf == NULL || nacf -> done_url == NULL ) {
569+
570+ if (nacf == NULL ) {
518571 goto next ;
519572 }
520573
521- ngx_memzero (& ci , sizeof (ci ));
522- ci .url = nacf -> done_url ;
523- ci .create = ngx_rtmp_notify_done_create ;
574+ if (nacf -> publish_done_url && (ctx -> flags & NGX_RTMP_NOTIFY_PUBLISHING )) {
575+ ngx_rtmp_notify_done (s , "publish_done" , nacf -> publish_done_url );
576+ }
577+
578+ if (nacf -> play_done_url && (ctx -> flags & NGX_RTMP_NOTIFY_PLAYING )) {
579+ ngx_rtmp_notify_done (s , "play_done" , nacf -> play_done_url );
580+ }
581+
582+ if (nacf -> done_url && ctx -> flags ) {
583+ ngx_rtmp_notify_done (s , "done" , nacf -> done_url );
584+ }
524585
525- ngx_rtmp_netcall_create ( s , & ci ) ;
586+ ctx -> flags = 0 ;
526587
527588next :
528589 return next_delete_stream (s , v );
529590}
530591
531592
593+ static ngx_int_t
594+ ngx_rtmp_notify_done (ngx_rtmp_session_t * s , char * cbname , ngx_url_t * url )
595+ {
596+ ngx_rtmp_netcall_init_t ci ;
597+
598+ ngx_log_debug2 (NGX_LOG_DEBUG_RTMP , s -> connection -> log , 0 ,
599+ "notify: %s '%V'" , cbname , & url -> url );
600+
601+ ngx_memzero (& ci , sizeof (ci ));
602+
603+ ci .url = url ;
604+ ci .arg = cbname ;
605+ ci .create = ngx_rtmp_notify_done_create ;
606+
607+ return ngx_rtmp_netcall_create (s , & ci );
608+ }
609+
610+
532611static char *
533612ngx_rtmp_notify_on_event (ngx_conf_t * cf , ngx_command_t * cmd , void * conf )
534613{
535614 ngx_rtmp_notify_app_conf_t * nacf ;
536615 ngx_str_t * url , * name ;
537616 ngx_url_t * u ;
538- size_t add ;
617+ size_t add , len ;
539618 ngx_str_t * value ;
540619
541620 value = cf -> args -> elts ;
@@ -568,13 +647,26 @@ ngx_rtmp_notify_on_event(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
568647
569648 nacf = ngx_rtmp_conf_get_module_app_conf (cf , ngx_rtmp_notify_module );
570649
650+ len = name -> len ;
651+
571652 switch (name -> data [4 ]) {
572- case 'l' : /* on_pLay */
573- nacf -> play_url = u ;
653+
654+ case 'l' : /* on_pLay... */
655+ if (len == sizeof ("on_play" ) - 1 ) {
656+ nacf -> play_url = u ;
657+ } else {
658+ nacf -> play_done_url = u ;
659+ }
574660 break ;
575- case 'u' : /* on_pUblish */
576- nacf -> publish_url = u ;
661+
662+ case 'u' : /* on_pUblish... */
663+ if (len == sizeof ("on_publish" ) - 1 ) {
664+ nacf -> publish_url = u ;
665+ } else {
666+ nacf -> publish_done_url = u ;
667+ }
577668 break ;
669+
578670 case 'o' : /* on_dOne */
579671 nacf -> done_url = u ;
580672 }
0 commit comments