From 96b69327fab912506b75945876b51b11cb45cd87 Mon Sep 17 00:00:00 2001 From: Sergey Dryabzhinsky Date: Sat, 27 Feb 2016 01:13:22 +0300 Subject: [PATCH] Update the continuity_counter for packets pat and pmt --- hls/ngx_rtmp_hls_module.c | 13 ++++++++----- hls/ngx_rtmp_mpegts.c | 24 +++++++++++++++++++----- hls/ngx_rtmp_mpegts.h | 2 +- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/hls/ngx_rtmp_hls_module.c b/hls/ngx_rtmp_hls_module.c index a91ec62..1e8f493 100644 --- a/hls/ngx_rtmp_hls_module.c +++ b/hls/ngx_rtmp_hls_module.c @@ -944,7 +944,7 @@ ngx_rtmp_hls_open_fragment(ngx_rtmp_session_t *s, uint64_t ts, uint64_t id; ngx_fd_t fd; ngx_str_t *datetime; - ngx_uint_t g; + ngx_uint_t g, mpegts_cc; ngx_rtmp_hls_ctx_t *ctx; ngx_rtmp_codec_ctx_t *codec_ctx; ngx_rtmp_hls_frag_t *f; @@ -1027,12 +1027,15 @@ ngx_rtmp_hls_open_fragment(ngx_rtmp_session_t *s, uint64_t ts, } } - ngx_log_debug6(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, + // This is continuity counter for TS header + mpegts_cc = (ctx->nfrags + ctx->frag); + + ngx_log_debug7(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, "hls: open fragment file='%s', keyfile='%s', " - "frag=%uL, n=%ui, time=%uL, discont=%i", + "frag=%uL, n=%ui, time=%uL, discont=%i, tscc=%ui", ctx->stream.data, ctx->keyfile.data ? ctx->keyfile.data : (u_char *) "", - ctx->frag, ctx->nfrags, ts, discont); + ctx->frag, ctx->nfrags, ts, discont, mpegts_cc); if (hacf->keys && ngx_rtmp_mpegts_init_encryption(&ctx->file, ctx->key, 16, ctx->key_id) @@ -1046,7 +1049,7 @@ ngx_rtmp_hls_open_fragment(ngx_rtmp_session_t *s, uint64_t ts, codec_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module); if (ngx_rtmp_mpegts_open_file(&ctx->file, ctx->stream.data, - s->connection->log, &codec_ctx->audio_codec_id) + s->connection->log, &codec_ctx->audio_codec_id, mpegts_cc) != NGX_OK) { return NGX_ERROR; diff --git a/hls/ngx_rtmp_mpegts.c b/hls/ngx_rtmp_mpegts.c index c05a81c..71bb766 100644 --- a/hls/ngx_rtmp_mpegts.c +++ b/hls/ngx_rtmp_mpegts.c @@ -12,8 +12,13 @@ static u_char ngx_rtmp_mpegts_header[] = { + /* https://en.wikipedia.org/wiki/MPEG_transport_stream#Packet */ + /* TS */ - 0x47, 0x40, 0x00, 0x10, 0x00, + 0x47, // Sync byte + 0x40, 0x00, // TEI(1) + PUS(1) + TP(1) + PID(13) + 0x10, // SC(2) + AFF(1) + PF(1) + CC(4) + 0x00, /* PSI */ 0x00, 0xb0, 0x0d, 0x00, 0x01, 0xc1, 0x00, 0x00, /* PAT */ @@ -40,7 +45,10 @@ static u_char ngx_rtmp_mpegts_header[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* TS */ - 0x47, 0x4f, 0xff, 0x10, 0x00, + 0x47, + 0x4f, 0xff, + 0x10, + 0x00, /* PSI */ 0x02, 0xb0, 0x17, 0x00, 0x01, 0xc1, 0x00, 0x00, /* PMT */ @@ -165,7 +173,7 @@ ngx_rtmp_mpegts_write_file(ngx_rtmp_mpegts_file_t *file, u_char *in, static ngx_int_t -ngx_rtmp_mpegts_write_header(ngx_rtmp_mpegts_file_t *file, ngx_uint_t *audio_codec_id) +ngx_rtmp_mpegts_write_header(ngx_rtmp_mpegts_file_t *file, ngx_uint_t *audio_codec_id, ngx_uint_t mpegts_cc) { if (*audio_codec_id == NGX_RTMP_AUDIO_AAC) { @@ -178,6 +186,12 @@ ngx_rtmp_mpegts_write_header(ngx_rtmp_mpegts_file_t *file, ngx_uint_t *audio_cod sizeof(ngx_rtmp_mpegts_header_mp3)); } + // Truncate counter to 4 bits here + mpegts_cc %= 0x0f; + // And fill headers + ngx_rtmp_mpegts_header[3] = (ngx_rtmp_mpegts_header[3] & 0xf0) + (u_char)mpegts_cc; + ngx_rtmp_mpegts_header[191] = (ngx_rtmp_mpegts_header[191] & 0xf0) + (u_char)mpegts_cc; + return ngx_rtmp_mpegts_write_file(file, ngx_rtmp_mpegts_header, sizeof(ngx_rtmp_mpegts_header)); } @@ -369,7 +383,7 @@ ngx_rtmp_mpegts_init_encryption(ngx_rtmp_mpegts_file_t *file, ngx_int_t ngx_rtmp_mpegts_open_file(ngx_rtmp_mpegts_file_t *file, u_char *path, - ngx_log_t *log, ngx_uint_t *audio_codec_id) + ngx_log_t *log, ngx_uint_t *audio_codec_id, ngx_uint_t mpegts_cc) { file->log = log; @@ -384,7 +398,7 @@ ngx_rtmp_mpegts_open_file(ngx_rtmp_mpegts_file_t *file, u_char *path, file->size = 0; - if (ngx_rtmp_mpegts_write_header(file, audio_codec_id) != NGX_OK) { + if (ngx_rtmp_mpegts_write_header(file, audio_codec_id, mpegts_cc) != NGX_OK) { ngx_log_error(NGX_LOG_ERR, log, ngx_errno, "hls: error writing fragment header"); ngx_close_file(file->fd); diff --git a/hls/ngx_rtmp_mpegts.h b/hls/ngx_rtmp_mpegts.h index 1acde62..8c8cb1b 100644 --- a/hls/ngx_rtmp_mpegts.h +++ b/hls/ngx_rtmp_mpegts.h @@ -37,7 +37,7 @@ typedef struct { ngx_int_t ngx_rtmp_mpegts_init_encryption(ngx_rtmp_mpegts_file_t *file, u_char *key, size_t key_len, uint64_t iv); ngx_int_t ngx_rtmp_mpegts_open_file(ngx_rtmp_mpegts_file_t *file, u_char *path, - ngx_log_t *log, ngx_uint_t *audio_codec_id); + ngx_log_t *log, ngx_uint_t *audio_codec_id, ngx_uint_t mpegts_cc); ngx_int_t ngx_rtmp_mpegts_close_file(ngx_rtmp_mpegts_file_t *file); ngx_int_t ngx_rtmp_mpegts_write_frame(ngx_rtmp_mpegts_file_t *file, ngx_rtmp_mpegts_frame_t *f, ngx_buf_t *b);