Skip to content

Commit 83a611d

Browse files
committed
[dev] optimize ngx_rtmp_gop_cache_send and ngx_rtmp_live_av functions.
1 parent b9621aa commit 83a611d

4 files changed

+123
-121
lines changed

ngx_http_flv_live_module.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,19 @@ static void ngx_http_flv_live_free_message(ngx_rtmp_session_t *s,
3131
ngx_chain_t *in);
3232
static void ngx_http_flv_live_close_http_request(ngx_rtmp_session_t *s);
3333

34-
extern ngx_rtmp_process_handler_t ngx_rtmp_live_process_handler;
35-
static ngx_rtmp_process_handler_t ngx_http_flv_live_process_handler = {
34+
extern ngx_rtmp_live_process_handler_t ngx_rtmp_live_process_handler;
35+
static ngx_rtmp_live_process_handler_t ngx_http_flv_live_process_handler = {
36+
NULL,
37+
NULL,
38+
NULL,
39+
NULL,
3640
ngx_http_flv_live_send_message,
3741
ngx_http_flv_live_meta_message,
3842
ngx_http_flv_live_append_message,
3943
ngx_http_flv_live_free_message
4044
};
4145

42-
ngx_rtmp_process_handler_t *ngx_rtmp_process_handlers[] = {
46+
ngx_rtmp_live_process_handler_t *ngx_rtmp_live_process_handlers[] = {
4347
&ngx_rtmp_live_process_handler,
4448
&ngx_http_flv_live_process_handler
4549
};

ngx_http_flv_live_module.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ typedef struct ngx_http_flv_live_conf_s {
5050

5151

5252
typedef struct {
53+
ngx_chain_t *meta;
54+
ngx_chain_t *apkt;
55+
ngx_chain_t *acopkt;
56+
ngx_chain_t *rpkt;
57+
5358
ngx_int_t (*send_message_pt)(ngx_rtmp_session_t *s,
5459
ngx_chain_t *out, unsigned int priority);
5560
ngx_chain_t *(*meta_message_pt)(ngx_rtmp_session_t *s,
@@ -59,7 +64,7 @@ typedef struct {
5964
ngx_chain_t *in);
6065
void (*free_message_pt)(ngx_rtmp_session_t *s,
6166
ngx_chain_t *in);
62-
} ngx_rtmp_process_handler_t;
67+
} ngx_rtmp_live_process_handler_t;
6368

6469

6570
ngx_int_t ngx_http_flv_live_send_header(ngx_rtmp_session_t *s);

ngx_rtmp_gop_cache_module.c

+37-33
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ static char *ngx_rtmp_gop_cache_merge_app_conf(ngx_conf_t *cf,
4343
void *parent, void *child);
4444

4545

46-
extern ngx_rtmp_process_handler_t *ngx_rtmp_process_handlers[2];
47-
extern ngx_module_t ngx_http_flv_live_module;
46+
extern ngx_rtmp_live_process_handler_t *ngx_rtmp_live_process_handlers
47+
[NGX_RTMP_PROTOCOL_HTTP + 1];
48+
extern ngx_module_t ngx_http_flv_live_module;
4849

4950

5051
static ngx_command_t ngx_rtmp_gop_cache_commands[] = {
@@ -579,21 +580,21 @@ ngx_rtmp_gop_cache_frame(ngx_rtmp_session_t *s, ngx_uint_t prio,
579580
void
580581
ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s)
581582
{
582-
ngx_rtmp_session_t *rs;
583-
ngx_chain_t *pkt, *apkt, *meta, *header;
584-
ngx_rtmp_live_ctx_t *ctx, *pub_ctx;
585-
ngx_http_flv_live_ctx_t *hflctx;
586-
ngx_rtmp_gop_cache_ctx_t *gctx;
587-
ngx_rtmp_live_app_conf_t *lacf;
588-
ngx_rtmp_gop_cache_t *cache;
589-
ngx_rtmp_gop_frame_t *gop_frame;
590-
ngx_rtmp_header_t ch, lh;
591-
ngx_uint_t meta_version;
592-
uint32_t delta;
593-
ngx_int_t csidx;
594-
ngx_rtmp_live_chunk_stream_t *cs;
595-
ngx_rtmp_process_handler_t *handler;
596-
ngx_http_request_t *r;
583+
ngx_rtmp_session_t *rs;
584+
ngx_chain_t *pkt, *apkt, *meta, *header;
585+
ngx_rtmp_live_ctx_t *ctx, *pub_ctx;
586+
ngx_http_flv_live_ctx_t *hflctx;
587+
ngx_rtmp_gop_cache_ctx_t *gctx;
588+
ngx_rtmp_live_app_conf_t *lacf;
589+
ngx_rtmp_gop_cache_t *cache;
590+
ngx_rtmp_gop_frame_t *gop_frame;
591+
ngx_rtmp_header_t ch, lh;
592+
ngx_uint_t meta_version;
593+
uint32_t delta;
594+
ngx_int_t csidx;
595+
ngx_rtmp_live_chunk_stream_t *cs;
596+
ngx_rtmp_live_process_handler_t *handler;
597+
ngx_http_request_t *r;
597598

598599
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
599600
if (lacf == NULL) {
@@ -616,7 +617,7 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s)
616617
pub_ctx = ctx->stream->pub_ctx;
617618
rs = pub_ctx->session;
618619
s->publisher = rs;
619-
handler = ngx_rtmp_process_handlers[ctx->protocol];
620+
handler = ngx_rtmp_live_process_handlers[ctx->protocol];
620621

621622
gctx = ngx_rtmp_get_module_ctx(rs, ngx_rtmp_gop_cache_module);
622623
if (gctx == NULL) {
@@ -627,7 +628,7 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s)
627628
if (ctx->protocol == NGX_RTMP_PROTOCOL_HTTP) {
628629
r = s->data;
629630
if (r == NULL || (r->connection && r->connection->destroyed)) {
630-
return;
631+
goto clear;
631632
}
632633

633634
hflctx = ngx_http_get_module_ctx(r, ngx_http_flv_live_module);
@@ -653,9 +654,6 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s)
653654
if (handler->send_message_pt(s, meta, 0) == NGX_OK) {
654655
ctx->meta_version = meta_version;
655656
}
656-
657-
handler->free_message_pt(s, meta);
658-
meta = NULL;
659657
}
660658

661659
for (gop_frame = cache->frame_head;
@@ -693,11 +691,6 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s)
693691
cs->active = 1;
694692
s->current_time = cs->timestamp;
695693
}
696-
697-
if (apkt) {
698-
handler->free_message_pt(s, apkt);
699-
apkt = NULL;
700-
}
701694
}
702695

703696
pkt = handler->append_message_pt(s, &ch, &lh, gop_frame->frame);
@@ -706,12 +699,7 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s)
706699

707700
cs->dropped += delta;
708701

709-
return;
710-
}
711-
712-
if (pkt) {
713-
handler->free_message_pt(s, pkt);
714-
pkt = NULL;
702+
goto clear;
715703
}
716704

717705
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
@@ -726,6 +714,22 @@ ngx_rtmp_gop_cache_send(ngx_rtmp_session_t *s)
726714
s->current_time = cs->timestamp;
727715
}
728716
}
717+
718+
return;
719+
720+
clear:
721+
722+
if (meta) {
723+
handler->free_message_pt(s, meta);
724+
}
725+
726+
if (pkt) {
727+
handler->free_message_pt(s, pkt);
728+
}
729+
730+
if (apkt) {
731+
handler->free_message_pt(s, apkt);
732+
}
729733
}
730734

731735

0 commit comments

Comments
 (0)