Update the continuity_counter for packets pat and pmt

This commit is contained in:
Sergey Dryabzhinsky 2016-02-27 01:13:22 +03:00
parent bf332f3794
commit 96b69327fa
3 changed files with 28 additions and 11 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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);