nginx rtmp

本文章介绍nginx rtmp module

常量

ngx_log_pid : worker 的process id

nginx 启动

ngx_rtmp.c 定义 ngx_rtmp_module, ngx_module_t 结构体

ngx_rtmp_commands 处理nginx.conf里的配置项,调用ngx_rtmp_block。

ngx_rtmp_block
  ngx_rtmp_optimize_servers
    ngx_rtmp_init_listening
      ngx_rtmp_add_listening
        ngx_create_listening
        ls->handler = ngx_rtmp_init_connection
  ngx_rtmp_init_event_handlers --> ngx_rtmp_amf_message_handler

ngx_rtmp_notify_postconfiguration =

handshake

c0+c1   –> client一起发送

s0+s1+s2 <— server分次多次回调发送

c2 —>

ngx_rtmp_init_connection
  ngx_rtmp_init_session
    ngx_rtmp_set_chunk_size   
  ngx_rtmp_handshake
    ngx_rtmp_handshake_recv
      ngx_rtmp_handshake_parse_challenge
      case NGX_RTMP_HANDSHAKE_SERVER_SEND_CHALLENGE
      ngx_rtmp_handshake_send
        case NGX_RTMP_HANDSHAKE_SERVER_SEND_RESPONSE:
        ngx_rtmp_handshake_send
          case NGX_RTMP_HANDSHAKE_SERVER_RECV_RESPONSE:
            ngx_rtmp_handshake_recv
           .....
           case NGX_RTMP_HANDSHAKE_SERVER_DONE:
            ngx_rtmp_handshake_done
              ngx_rtmp_cycle !!!!!!!!!!!!
                ngx_rtmp_reset_ping
                  ngx_add_timer
                  
           

connect

ngx_rtmp_handshake_done调用

ngx_rtmp_cycle
  ngx_rtmp_recv
    ngx_rtmp_receive_message
      ((*evh)(s, h, in))
      ngx_rtmp_amf_message_handler
        ngx_rtmp_amf_read
          ngx_rtmp_amf_get
        switch ((*ph)(s, h, in))  --- ngx_rtmp_cmd_map
        ngx_rtmp_cmd_connect_init
          ngx_rtmp_connect =  ngx_rtmp_notify_connect
            ngx_rtmp_netcall_create -- ngx_rtmp_notify_connect_create, ngx_rtmp_notify_connect_create
  ngx_rtmp_relay_postconfiguration
  ngx_rtmp_relay_on_result
    ngx_rtmp_relay_connect_local
    ngx_rtmp_cmd_start_connect

record

ngx_rtmp_record_postconfiguration
|       h = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_AUDIO]);
|       *h = ngx_rtmp_record_av;
|
|       h = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_VIDEO]);
|       *h = ngx_rtmp_record_av;
ngx_rtmp_record_av
ngx_rtmp_record_node_av





## to solve.....

static ngx_rtmp_amf_handler_t ngx_rtmp_cmd_map[] = { { ngx_string(“connect”), ngx_rtmp_cmd_connect_init }, { ngx_string(“createStream”), ngx_rtmp_cmd_create_stream_init }, { ngx_string(“closeStream”), ngx_rtmp_cmd_close_stream_init }, { ngx_string(“deleteStream”), ngx_rtmp_cmd_delete_stream_init }, { ngx_string(“publish”), ngx_rtmp_cmd_publish_init }, { ngx_string(“play”), ngx_rtmp_cmd_play_init }, { ngx_string(“play2”), ngx_rtmp_cmd_play2_init }, { ngx_string(“seek”), ngx_rtmp_cmd_seek_init }, { ngx_string(“pause”), ngx_rtmp_cmd_pause_init }, { ngx_string(“pauseraw”), ngx_rtmp_cmd_pause_init }, };¬








ngx_rtmp_cmd_module.c

ngx_module_t ngx_rtmp_cmd_module = { NGX_MODULE_V1, &ngx_rtmp_cmd_module_ctx, /* module context / NULL, / module directives /¬ NGX_RTMP_MODULE, / module type /¬ NULL, / init master /¬ NULL, / init module /¬ NULL, / init process /¬ NULL, / init thread /¬ NULL, / exit thread /¬ NULL, / exit process /¬ NULL, / exit master */¬ NGX_MODULE_V1_PADDING¬ };¬



ngx_rtmp.h 定义    

662 extern ngx_module_t ngx_rtmp_module;¬ 664 #define NGX_RTMP_MODULE 0x504D5452 /* “RTMP” */¬

70 static ngx_rtmp_module_t  ngx_rtmp_cmd_module_ctx = {¬

| 71 NULL, /* preconfiguration /¬ | 72 ngx_rtmp_cmd_postconfiguration, / postconfiguration /¬ | 73 NULL, / create main configuration /¬ | 74 NULL, / init main configuration /¬ | 75 NULL, / create server configuration /¬ | 76 NULL, / merge server configuration /¬ | 77 NULL, / create app configuration /¬ | 78 NULL / merge app configuration */¬ | 79 };¬



## ngx_rtmp_cmd_postconfiguration

会将上面的connect-> connect_init数据写入到cmcf->amf





## ngx_rtmp_cmd_connect_init



# 设定worker socket path


`ngx_rtmp_listener_init_init_conf`函数修改响应内容。配置有bug,没生效


# hls


ngx_rtmp_hls_postconfiguration


## connect



#0 ngx_rtmp_netcall_create (s=0x156c7b0, ci=0x7fff4dd09eb0) at ./src/rtmp/ngx_rtmp_netcall_module.c:178 #1 0x0000000000523ffe in ngx_rtmp_notify_play (s=0x156c7b0, v=0x9c2b80 ) at ./src/rtmp/ngx_rtmp_notify_module.c:2058 #2 0x000000000052d523 in ngx_rtmp_log_play (s=0x156c7b0, v=0x9c2b80 ) at ./src/rtmp/ngx_rtmp_log_module.c:3502 #3 0x00000000004ed1f2 in ngx_rtmp_cmd_start_play (s=0x156c7b0, v=0x9c2b80 ) at ./src/rtmp/ngx_rtmp_cmd_module.c:749 #4 0x000000000053b882 in ngx_rtmp_http_hls_play_local (r=0x1545630) at ./src/rtmp/hls/ngx_rtmp_hls_module.c:3635 #5 0x0000000000539e0a in ngx_rtmp_hls_connect_done (s=0x156c7b0, h=0x0, in=0x0) at ./src/rtmp/hls/ngx_rtmp_hls_module.c:2766 #6 0x00000000004dab06 in ngx_rtmp_fire_event (s=0x156c7b0, evt=26, h=0x0, in=0x0) at ./src/rtmp/ngx_rtmp.c:908 #7 0x00000000004ec4b3 in ngx_rtmp_cmd_connect (s=0x156c7b0, v=0x151a1b8) at ./src/rtmp/ngx_rtmp_cmd_module.c:407 #8 0x000000000050179b in ngx_rtmp_live_connect (s=0x156c7b0, v=0x151a1b8) at ./src/rtmp/ngx_rtmp_live_module.c:2962 #9 0x0000000000521bac in ngx_rtmp_notify_connect_handle (s=0x156c7b0, arg=0x151a1b8, in=0x151aad8) at ./src/rtmp/ngx_rtmp_notify_module.c:1202 #10 0x000000000050cad0 in ngx_rtmp_netcall_close (cc=0x7f989edb2750) at ./src/rtmp/ngx_rtmp_netcall_module.c:333 #11 0x000000000050cdf3 in ngx_rtmp_netcall_recv (rev=0x7f989ecc1310) at ./src/rtmp/ngx_rtmp_netcall_module.c:436 #12 0x000000000044ffb6 in ngx_epoll_process_events (cycle=0x15160b0, timer=2994, flags=1) at src/event/modules/ngx_epoll_module.c:822 #13 0x0000000000440231 in ngx_process_events_and_timers (cycle=0x15160b0) at src/event/ngx_event.c:242 #14 0x000000000044db3b in ngx_worker_process_cycle (cycle=0x15160b0, data=0x0) at src/os/unix/ngx_process_cycle.c:753 #15 0x000000000044a437 in ngx_spawn_process (cycle=0x15160b0, proc=0x44da46 , data=0x0, name=0x552e2b “worker process”, respawn=-3) at src/os/unix/ngx_process.c:198 #16 0x000000000044c977 in ngx_start_worker_processes (cycle=0x15160b0, n=1, type=-3) at src/os/unix/ngx_process_cycle.c:358 #17 0x000000000044bf94 in ngx_master_process_cycle (cycle=0x15160b0) at src/os/unix/ngx_process_cycle.c:130 #18 0x0000000000412ab9 in main (argc=3, argv=0x7fff4dd0b308) at src/core/nginx.c:367



ngx_rtmp_notify_connect_handle next_connect = ngx_rtmp_live_connect next_connect = ngx_rtmp_cmd_connect —- 重要函数



# hls video

#0 ngx_rtmp_hls_video (s=0x1571810, h=0x1545808, in=0x156c800) at ./src/rtmp/hls/ngx_rtmp_hls_module.c:2476 #1 0x00000000004e25d3 in ngx_rtmp_receive_message (s=0x1571810, h=0x1545808, in=0x156c800) at ./src/rtmp/ngx_rtmp_handler.c:926 #2 0x00000000004e11e8 in ngx_rtmp_recv (rev=0x7f989ecc1970) at ./src/rtmp/ngx_rtmp_handler.c:494 #3 0x000000000044ffb6 in ngx_epoll_process_events (cycle=0x15160b0, timer=999, flags=1) at src/event/modules/ngx_epoll_module.c:822 #4 0x0000000000440231 in ngx_process_events_and_timers (cycle=0x15160b0) at src/event/ngx_event.c:242 #5 0x000000000044db3b in ngx_worker_process_cycle (cycle=0x15160b0, data=0x0) at src/os/unix/ngx_process_cycle.c:753 #6 0x000000000044a437 in ngx_spawn_process (cycle=0x15160b0, proc=0x44da46 , data=0x0, name=0x552e2b “worker process”, respawn=-3) at src/os/unix/ngx_process.c:198 #7 0x000000000044c977 in ngx_start_worker_processes (cycle=0x15160b0, n=1, type=-3) at src/os/unix/ngx_process_cycle.c:358 #8 0x000000000044bf94 in ngx_master_process_cycle (cycle=0x15160b0) at src/os/unix/ngx_process_cycle.c:130 #9 0x0000000000412ab9 in main (argc=3, argv=0x7fff4dd0b308) at src/core/nginx.c:367



## codec

ngx_rtmp_codec_av





# hls play


ngx_rtmp_hls_play s->hls_stime_ms = ngx_current_msec ngx_rtmp_hls_open_file

ngx_rtmp_hls_request_close_handler ngx_rtmp_hls_session_close_handler s->hls_etime_ms = ngx_current_msec