Skip to content

Commit b4ca0be

Browse files
committed
implemented absolute timestamp feature (atc on)
1 parent ac13bbf commit b4ca0be

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

ngx_rtmp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ typedef struct {
151151
typedef struct {
152152
uint32_t csid; /* chunk stream id */
153153
uint32_t timestamp; /* timestamp (delta) */
154+
uint32_t timeshift; /* clock - timestamp */
154155
uint32_t mlen; /* message length */
155156
uint8_t type; /* message type id */
156157
uint32_t msid; /* message stream id */
@@ -201,6 +202,7 @@ typedef struct {
201202
/* connection timestamps */
202203
ngx_msec_t epoch;
203204
ngx_msec_t peer_epoch;
205+
ngx_msec_t base_time;
204206

205207
/* ping */
206208
ngx_event_t ping_evt;

ngx_rtmp_handler.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ ngx_rtmp_recv(ngx_event_t *rev)
396396
st->dtime = timestamp;
397397
} else {
398398
h->timestamp = timestamp;
399+
h->timeshift = (uint32_t) ngx_current_msec - timestamp;
399400
st->dtime = 0;
400401
}
401402
}

ngx_rtmp_live_module.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ static ngx_command_t ngx_rtmp_live_commands[] = {
6060
offsetof(ngx_rtmp_live_app_conf_t, sync),
6161
NULL },
6262

63+
{ ngx_string("atc"),
64+
NGX_RTMP_MAIN_CONF|NGX_RTMP_SRV_CONF|NGX_RTMP_APP_CONF|NGX_CONF_TAKE1,
65+
ngx_conf_set_flag_slot,
66+
NGX_RTMP_APP_CONF_OFFSET,
67+
offsetof(ngx_rtmp_live_app_conf_t, atc),
68+
NULL },
69+
6370
ngx_null_command
6471
};
6572

@@ -107,6 +114,7 @@ ngx_rtmp_live_create_app_conf(ngx_conf_t *cf)
107114
lacf->nbuckets = NGX_CONF_UNSET;
108115
lacf->buflen = NGX_CONF_UNSET;
109116
lacf->sync = NGX_CONF_UNSET;
117+
lacf->atc = NGX_CONF_UNSET;
110118

111119
return lacf;
112120
}
@@ -123,6 +131,7 @@ ngx_rtmp_live_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
123131
ngx_conf_merge_value(conf->nbuckets, prev->nbuckets, 1024);
124132
ngx_conf_merge_msec_value(conf->buflen, prev->buflen, 0);
125133
ngx_conf_merge_msec_value(conf->sync, prev->sync, 0);
134+
ngx_conf_merge_value(conf->atc, prev->atc, 0);
126135

127136
conf->pool = ngx_create_pool(4096, &cf->cycle->new_log);
128137
if (conf->pool == NULL) {
@@ -337,10 +346,10 @@ ngx_rtmp_live_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
337346
return NGX_OK;
338347
}
339348

340-
ngx_log_debug2(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
341-
"live: av: %s timestamp=%uD",
349+
ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
350+
"live: av: %s timestamp=%uD timeshift=%uD",
342351
h->type == NGX_RTMP_MSG_VIDEO ? "video" : "audio",
343-
h->timestamp);
352+
h->timestamp, h->timeshift);
344353

345354
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
346355

@@ -410,17 +419,21 @@ ngx_rtmp_live_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
410419
++peers;
411420
ss = pctx->session;
412421
last = (uint32_t *)((u_char *)pctx + last_offset);
413-
ch.timestamp = s->epoch + h->timestamp - ss->epoch;
422+
423+
ch.timestamp = lacf->atc ? h->timestamp :
424+
h->timeshift + h->timestamp - (uint32_t)ss->epoch;
414425

415426
/* send absolute frame */
416427
if ((pctx->msg_mask & (1 << h->type)) == 0) {
417428

418429
/* packet from the past for the peer */
419-
if (s->epoch + h->timestamp < ss->epoch) {
430+
if (lacf->atc == 0 &&
431+
h->timeshift + h->timestamp < (uint32_t)ss->epoch)
432+
{
420433
ngx_log_debug3(NGX_LOG_DEBUG_RTMP, ss->connection->log, 0,
421434
"live: av: %s packet from the past %uD < %uD",
422435
h->type == NGX_RTMP_MSG_VIDEO ? "video" : "audio",
423-
(uint32_t)(s->epoch + h->timestamp), (uint32_t)ss->epoch);
436+
h->timeshift + h->timestamp, (uint32_t)ss->epoch);
424437
continue;
425438
}
426439

ngx_rtmp_live_module.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef struct {
5555
ngx_flag_t live;
5656
ngx_flag_t meta;
5757
ngx_msec_t sync;
58+
ngx_flag_t atc;
5859
ngx_msec_t buflen;
5960
ngx_pool_t *pool;
6061
ngx_rtmp_live_stream_t *free_streams;

0 commit comments

Comments
 (0)