Skip to content

Commit 25a349b

Browse files
committed
fix bug: Video frames are sent repeatedly
1 parent bd64e5a commit 25a349b

File tree

6 files changed

+178
-108
lines changed

6 files changed

+178
-108
lines changed

mpegts/ngx_hls_http_module.c

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ ngx_hls_http_send_header(ngx_http_request_t *r, ngx_uint_t status, ngx_keyval_t
259259
r->headers_out.status = status;
260260
r->keepalive = 0; /* set Connection to closed */
261261

262+
//set eTag
263+
if (ngx_http_set_etag(r) != NGX_OK) {
264+
return NGX_HTTP_INTERNAL_SERVER_ERROR;
265+
}
266+
262267
while (h && h->key.len) {
263268
rc = ngx_http_set_header_out(r, &h->key, &h->value);
264269
if (rc != NGX_OK) {
@@ -332,7 +337,7 @@ ngx_hls_http_redirect_handler(ngx_http_request_t *r,
332337
r->headers_out.content_length_n = 0;
333338
r->header_only = 1;
334339

335-
rc = ngx_hls_http_send_header(r, NGX_HTTP_MOVED_TEMPORARILY, ngx_m3u8_headers);
340+
rc = ngx_hls_http_send_header(r, NGX_HTTP_MOVED_TEMPORARILY, NULL);
336341
if (rc != NGX_OK) {
337342
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
338343
"hls-http: redirect_handler| "
@@ -397,23 +402,22 @@ ngx_hls_http_cleanup(void *data)
397402
{
398403
ngx_http_request_t *r;
399404
ngx_hls_http_ctx_t *ctx;
400-
// ngx_chain_t *cl;
405+
ngx_chain_t *cl;
401406

402407
r = data;
403408
ctx = ngx_http_get_module_ctx(r, ngx_hls_http_module);
404409

405410
if (!ctx) {
406411
return;
407412
}
408-
/*
413+
409414
cl = ctx->out_chain;
410415
while (cl) {
411416
ctx->out_chain = cl->next;
412417
ngx_put_chainbuf(cl);
413418
cl = ctx->out_chain;
414419
}
415420
ctx->out_chain = NULL;
416-
*/
417421

418422
if (ctx->session) {
419423
ctx->session->request = NULL;
@@ -619,8 +623,6 @@ ngx_hls_http_m3u8_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf
619623
return NGX_HTTP_INTERNAL_SERVER_ERROR;
620624
}
621625

622-
buf = ngx_create_temp_buf(r->connection->pool, 1024*512);
623-
624626
s = ngx_hls_live_fetch_session(&ctx->serverid, &ctx->stream, &ctx->sid);
625627
if (s == NULL) {
626628
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
@@ -634,8 +636,6 @@ ngx_hls_http_m3u8_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf
634636

635637
return NGX_HTTP_INTERNAL_SERVER_ERROR;
636638
}
637-
s->log->connection = r->connection->number;
638-
s->number = r->connection->number;
639639
s->sockaddr = ngx_pcalloc(s->pool, sizeof(struct sockaddr));
640640
ngx_memcpy(s->sockaddr, r->connection->sockaddr, sizeof(struct sockaddr));
641641
}
@@ -644,27 +644,33 @@ ngx_hls_http_m3u8_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf
644644

645645
ctx->session = s;
646646

647-
out = ngx_pcalloc(s->pool, sizeof(ngx_chain_t));
648-
out->buf = buf;
647+
if (!ctx->m3u8) {
648+
ctx->m3u8 = ngx_pcalloc(s->pool, sizeof(ngx_chain_t));
649+
ctx->m3u8->buf = ngx_create_temp_buf(r->connection->pool, 1024*512);
650+
}
649651

652+
out = ctx->m3u8;
653+
buf = out->buf;
650654
buf->last = buf->pos = buf->start;
651655
buf->memory = 1;
652656
buf->flush = 1;
653-
buf->last_in_chain = 1;
654-
buf->last_buf = 1;
657+
// buf->last_in_chain = 1;
658+
// buf->last_buf = 1;
655659

656-
rc = ngx_hls_live_write_playlist(s, buf);
660+
rc = ngx_hls_live_write_playlist(s, buf, &r->headers_out.last_modified_time);
657661
if (rc != NGX_OK) {
658662
goto again;
659663
}
660664

661665
r->headers_out.content_length_n = buf->last - buf->pos;
662666

663-
rc = ngx_hls_http_send_header(r, NGX_HTTP_OK, ngx_m3u8_headers);
664-
if (rc != NGX_OK) {
665-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
666-
"hls-http: m3u8_handler| send http header failed");
667-
return rc;
667+
if (!r->header_sent) {
668+
rc = ngx_hls_http_send_header(r, NGX_HTTP_OK, ngx_m3u8_headers);
669+
if (rc != NGX_OK) {
670+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
671+
"hls-http: m3u8_handler| send http header failed");
672+
return rc;
673+
}
668674
}
669675

670676
rc = ngx_http_output_filter(r, out);
@@ -769,7 +775,7 @@ ngx_hls_http_write_handler(ngx_http_request_t *r)
769775
}
770776

771777
if (ctx->out_chain == NULL) {
772-
ctx->out_chain = ngx_hls_live_prepare_out_chain(s, ctx->frag, 1);
778+
ctx->out_chain = ngx_hls_live_prepare_out_chain(s, ctx->frag, 4);
773779
}
774780

775781
rc = NGX_OK;
@@ -818,7 +824,7 @@ ngx_hls_http_write_handler(ngx_http_request_t *r)
818824
break;
819825
}
820826

821-
ctx->out_chain = ngx_hls_live_prepare_out_chain(s, ctx->frag, 1);
827+
ctx->out_chain = ngx_hls_live_prepare_out_chain(s, ctx->frag, 4);
822828
}
823829

824830
if (wev->active) {
@@ -871,6 +877,7 @@ ngx_hls_http_ts_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf)
871877
ctx->frag = frag;
872878

873879
r->headers_out.content_length_n = frag->length;
880+
r->headers_out.last_modified_time = frag->last_modified_time;
874881
rc = ngx_hls_http_send_header(r, NGX_HTTP_OK, ngx_ts_headers);
875882
if (rc != NGX_OK) {
876883
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
@@ -880,7 +887,7 @@ ngx_hls_http_ts_handler(ngx_http_request_t *r, ngx_rtmp_addr_conf_t *addr_conf)
880887

881888
ngx_rtmp_shared_acquire_frag(frag);
882889

883-
if (1) {
890+
if (0) {
884891
r->count++;
885892
r->write_event_handler = ngx_hls_http_write_handler;
886893

@@ -904,6 +911,7 @@ ngx_hls_http_handler(ngx_http_request_t *r)
904911
ngx_int_t rc;
905912
ngx_http_cleanup_t *cln;
906913
ngx_str_t sstr;
914+
ngx_hls_http_ctx_t *ctx;
907915

908916
hlcf = ngx_http_get_module_loc_conf(r, ngx_hls_http_module);
909917

@@ -919,14 +927,16 @@ ngx_hls_http_handler(ngx_http_request_t *r)
919927
return NGX_DECLINED;
920928
}
921929

922-
cln = ngx_http_cleanup_add(r, 0);
923-
if (cln == NULL) {
924-
925-
return NGX_HTTP_INTERNAL_SERVER_ERROR;
930+
ctx = ngx_http_get_module_ctx(r, ngx_hls_http_module);
931+
if (!ctx) {
932+
cln = ngx_http_cleanup_add(r, 0);
933+
if (cln == NULL) {
934+
return NGX_HTTP_INTERNAL_SERVER_ERROR;
935+
}
936+
cln->handler = ngx_hls_http_cleanup;
937+
cln->data = r;
938+
r->read_event_handler = ngx_http_test_reading;
926939
}
927-
cln->handler = ngx_hls_http_cleanup;
928-
cln->data = r;
929-
r->read_event_handler = ngx_http_test_reading;
930940

931941
if(!ngx_strncmp(r->uri.data + r->uri.len - 5, ".m3u8", 5)) {
932942

@@ -963,22 +973,19 @@ ngx_hls_http_m3u8(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in)
963973
ctx = ngx_http_get_module_ctx(r, ngx_hls_http_module);
964974

965975
if (!ctx->m3u8) {
966-
ctx->m3u8 = ngx_get_chainbuf(1024*512, 1);
976+
ctx->m3u8 = ngx_pcalloc(s->pool, sizeof(ngx_chain_t));
977+
ctx->m3u8->buf = ngx_create_temp_buf(r->connection->pool, 1024*512);
967978
}
968979

969980
out = ctx->m3u8;
970-
971-
out->next = NULL;
972-
973981
buf = out->buf;
974-
975982
buf->last = buf->pos = buf->start;
976983
buf->memory = 1;
977984
buf->flush = 1;
978-
buf->last_in_chain = 1;
979-
buf->last_buf = 1;
985+
// buf->last_in_chain = 1;
986+
// buf->last_buf = 1;
980987

981-
ngx_hls_live_write_playlist(s, buf);
988+
ngx_hls_live_write_playlist(s, buf, &r->headers_out.last_modified_time);
982989

983990
r->headers_out.content_length_n = buf->last - buf->pos;
984991

@@ -996,7 +1003,7 @@ ngx_hls_http_m3u8(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h, ngx_chain_t *in)
9961003
return rc;
9971004
}
9981005

999-
return rc;
1006+
return NGX_OK;
10001007
}
10011008

10021009
static ngx_int_t

0 commit comments

Comments
 (0)