Skip to content

Commit dfb02f4

Browse files
committed
[dev] added continuity counter in TS headers.
1 parent b7c5df1 commit dfb02f4

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

hls/ngx_rtmp_hls_module.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,7 @@ ngx_rtmp_hls_open_fragment(ngx_rtmp_session_t *s, uint64_t ts,
849849
uint64_t id;
850850
ngx_fd_t fd;
851851
ngx_uint_t g;
852+
ngx_uint_t counter;
852853
ngx_rtmp_hls_ctx_t *ctx;
853854
ngx_rtmp_codec_ctx_t *codec_ctx;
854855
ngx_rtmp_hls_frag_t *f;
@@ -930,12 +931,14 @@ ngx_rtmp_hls_open_fragment(ngx_rtmp_session_t *s, uint64_t ts,
930931
}
931932
}
932933

933-
ngx_log_debug6(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
934+
counter = (ngx_uint_t) (ctx->nfrags + ctx->frag);
935+
936+
ngx_log_debug7(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
934937
"hls: open fragment file='%s', keyfile='%s', "
935-
"frag=%uL, n=%ui, time=%uL, discont=%i",
938+
"frag=%uL, n=%ui, time=%uL, discont=%i, cc=%u",
936939
ctx->stream.data,
937940
ctx->keyfile.data ? ctx->keyfile.data : (u_char *) "",
938-
ctx->frag, ctx->nfrags, ts, discont);
941+
ctx->frag, ctx->nfrags, ts, discont, counter);
939942

940943
if (hacf->keys &&
941944
ngx_rtmp_mpegts_init_encryption(&ctx->file, ctx->key, 16, ctx->key_id)
@@ -948,7 +951,7 @@ ngx_rtmp_hls_open_fragment(ngx_rtmp_session_t *s, uint64_t ts,
948951

949952
codec_ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module);
950953
if (ngx_rtmp_mpegts_open_file(&ctx->file, ctx->stream.data, codec_ctx,
951-
s->connection->log)
954+
counter, s->connection->log)
952955
!= NGX_OK)
953956
{
954957
return NGX_ERROR;

hls/ngx_rtmp_mpegts.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ ngx_rtmp_mpegts_write_file(ngx_rtmp_mpegts_file_t *file, u_char *in,
173173

174174
static ngx_int_t
175175
ngx_rtmp_mpegts_write_header(ngx_rtmp_mpegts_file_t *file,
176-
ngx_rtmp_codec_ctx_t *codec_ctx)
176+
ngx_rtmp_codec_ctx_t *codec_ctx, ngx_uint_t counter)
177177
{
178178
ngx_int_t stream_bytes;
179179
ngx_rtmp_mpegts_crc_t crc;
@@ -186,6 +186,12 @@ ngx_rtmp_mpegts_write_header(ngx_rtmp_mpegts_file_t *file,
186186
stream_bytes = 0;
187187
ngx_memcpy(buf, ngx_rtmp_mpegts_header, sizeof(ngx_rtmp_mpegts_header));
188188

189+
/* 4 bits, periodical */
190+
counter %= 0x10;
191+
/* fill headers */
192+
buf[3] = (buf[3] & 0xf0) + (u_char) counter;
193+
buf[191] = (buf[191] & 0xf0) + (u_char) counter;
194+
189195
if (codec_ctx->video_codec_id) {
190196
/* video info */
191197
ngx_memcpy(buf + NGX_RTMP_MPEGTS_PMT_LOOP_OFFSET + stream_bytes,
@@ -409,7 +415,7 @@ ngx_rtmp_mpegts_init_encryption(ngx_rtmp_mpegts_file_t *file,
409415

410416
ngx_int_t
411417
ngx_rtmp_mpegts_open_file(ngx_rtmp_mpegts_file_t *file, u_char *path,
412-
ngx_rtmp_codec_ctx_t *codec_ctx, ngx_log_t *log)
418+
ngx_rtmp_codec_ctx_t *codec_ctx, ngx_uint_t counter, ngx_log_t *log)
413419
{
414420
file->log = log;
415421

@@ -424,7 +430,7 @@ ngx_rtmp_mpegts_open_file(ngx_rtmp_mpegts_file_t *file, u_char *path,
424430

425431
file->size = 0;
426432

427-
if (ngx_rtmp_mpegts_write_header(file, codec_ctx) != NGX_OK) {
433+
if (ngx_rtmp_mpegts_write_header(file, codec_ctx, counter) != NGX_OK) {
428434
ngx_log_error(NGX_LOG_ERR, log, ngx_errno,
429435
"hls: error writing fragment header");
430436
ngx_close_file(file->fd);

hls/ngx_rtmp_mpegts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ typedef struct {
3939
ngx_int_t ngx_rtmp_mpegts_init_encryption(ngx_rtmp_mpegts_file_t *file,
4040
u_char *key, size_t key_len, uint64_t iv);
4141
ngx_int_t ngx_rtmp_mpegts_open_file(ngx_rtmp_mpegts_file_t *file, u_char *path,
42-
ngx_rtmp_codec_ctx_t *codec_ctx, ngx_log_t *log);
42+
ngx_rtmp_codec_ctx_t *codec_ctx, ngx_uint_t counter, ngx_log_t *log);
4343
ngx_int_t ngx_rtmp_mpegts_close_file(ngx_rtmp_mpegts_file_t *file);
4444
ngx_int_t ngx_rtmp_mpegts_write_frame(ngx_rtmp_mpegts_file_t *file,
4545
ngx_rtmp_mpegts_frame_t *f, ngx_buf_t *b);

0 commit comments

Comments
 (0)