作者原創(chuàng),轉(zhuǎn)載請(qǐng)聯(lián)系作者
連接到來(lái)的處理步驟
通過(guò)上文知道翩蘸,RTMP是通過(guò)ngx_rtmp_block()方式注冊(cè)連接到來(lái)的回調(diào)ngx_rtmp_init_connection(). 當(dāng)連接來(lái)到后沮榜,主要有如下幾個(gè)步驟:
1懂算、初始化RTMP SESSION相叁,通過(guò)ngx_rtmp_init_session党涕,這是一具重要結(jié)構(gòu)放典,這個(gè)結(jié)構(gòu)是每個(gè)連接保持必要信息逝变,在后續(xù)的處理時(shí)作為入?yún)⑹褂谩?/p>
typedef struct {
uint32_t signature; /* "RTMP" */ /* <-- FIXME wtf */
ngx_event_t close;
void **ctx;
void **main_conf;
void **srv_conf;
void **app_conf;
ngx_str_t *addr_text;
int connected;
#if (nginx_version >= 1007005)
ngx_queue_t posted_dry_events;
#else
ngx_event_t *posted_dry_events;
#endif
/* client buffer time in msec */
uint32_t buflen;
uint32_t ack_size;
/* connection parameters */
ngx_str_t app;
ngx_str_t args;
ngx_str_t flashver;
ngx_str_t swf_url;
ngx_str_t tc_url;
uint32_t acodecs;
uint32_t vcodecs;
ngx_str_t page_url;
/* handshake data */
ngx_buf_t *hs_buf;
u_char *hs_digest;
unsigned hs_old:1;
ngx_uint_t hs_stage;
/* connection timestamps */
ngx_msec_t epoch;
ngx_msec_t peer_epoch;
ngx_msec_t base_time;
uint32_t current_time;
/* ping */
ngx_event_t ping_evt;
unsigned ping_active:1;
unsigned ping_reset:1;
/* auto-pushed? */
unsigned auto_pushed:1;
unsigned relay:1;
unsigned static_relay:1;
/* input stream 0 (reserved by RTMP spec)
* is used as free chain link */
ngx_rtmp_stream_t *in_streams;
uint32_t in_csid;
ngx_uint_t in_chunk_size;
ngx_pool_t *in_pool;
uint32_t in_bytes;
uint32_t in_last_ack;
ngx_pool_t *in_old_pool;
ngx_int_t in_chunk_size_changing;
ngx_connection_t *connection;
/* circular buffer of RTMP message pointers */
ngx_msec_t timeout;
uint32_t out_bytes;
size_t out_pos, out_last;
ngx_chain_t *out_chain;
u_char *out_bpos;
unsigned out_buffer:1;
size_t out_queue;
size_t out_cork;
ngx_chain_t *out[0];
} ngx_rtmp_session_t;
2、初始化后如果配置有NGX_RTMP_CONNECT事件奋构,則通過(guò)ngx_rtmp_fire_event觸發(fā)HTTP事件壳影,根據(jù)之前文章提到的,在ngx_rtmp_limit_module模塊中注冊(cè)弥臼,其回調(diào)是ngx_rtmp_limit_connect,此回調(diào)主要是判斷是否超出了連接限制
3宴咧、通過(guò)ngx_rtmp_handshake進(jìn)入握手階段,通過(guò)ngx_rtmp_handshake_done結(jié)束
4径缅、進(jìn)入RTMP消息連循環(huán)ngx_rtmp_cycle掺栅,就是不停接收RTMP消息,處理RTMP消息纳猪。
5氧卧、流結(jié)束或出錯(cuò)等,都通過(guò)ngx_rtmp_finalize_session結(jié)束處理