Skip to content

Commit c09ffa9

Browse files
committed
[fix] fix the #115 bug.
1 parent 3e5ae82 commit c09ffa9

File tree

1 file changed

+64
-12
lines changed

1 file changed

+64
-12
lines changed

ngx_http_flv_live_module.c

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ static void ngx_http_flv_live_free_request(ngx_rtmp_session_t *s);
203203

204204
static void ngx_http_flv_live_read_handler(ngx_event_t *rev);
205205
static void ngx_http_flv_live_write_handler(ngx_event_t *wev);
206+
static void ngx_http_flv_live_correct_timestamp(ngx_rtmp_session_t *s,
207+
ngx_flag_t correct);
206208

207209
static ngx_int_t ngx_http_flv_live_preprocess(ngx_http_request_t *r,
208210
ngx_rtmp_connection_t *rconn);
@@ -1517,11 +1519,15 @@ ngx_http_flv_live_write_handler(ngx_event_t *wev)
15171519

15181520
if (s->out_chain == NULL && s->out_pos != s->out_last) {
15191521
s->out_chain = s->out[s->out_pos];
1522+
ngx_http_flv_live_correct_timestamp(s, 1);
15201523
s->out_bpos = s->out_chain->buf->pos;
1524+
} else if (s->out_chain) {
1525+
ngx_http_flv_live_correct_timestamp(s, 1);
15211526
}
15221527

15231528
while (s->out_chain) {
15241529
n = c->send(c, s->out_bpos, s->out_chain->buf->last - s->out_bpos);
1530+
ngx_http_flv_live_correct_timestamp(s, 0);
15251531

15261532
if (n == NGX_AGAIN || n == 0) {
15271533
ngx_add_timer(c->write, s->timeout);
@@ -1553,6 +1559,7 @@ ngx_http_flv_live_write_handler(ngx_event_t *wev)
15531559
break;
15541560
}
15551561
s->out_chain = s->out[s->out_pos];
1562+
ngx_http_flv_live_correct_timestamp(s, 1);
15561563
}
15571564
s->out_bpos = s->out_chain->buf->pos;
15581565
}
@@ -1566,6 +1573,57 @@ ngx_http_flv_live_write_handler(ngx_event_t *wev)
15661573
}
15671574

15681575

1576+
static void
1577+
ngx_http_flv_live_correct_timestamp(ngx_rtmp_session_t *s, ngx_flag_t correct)
1578+
{
1579+
uint8_t type;
1580+
uint32_t timestamp;
1581+
u_char *p, *pt;
1582+
ngx_buf_t *b;
1583+
1584+
if (s->out_chain == NULL) {
1585+
return;
1586+
}
1587+
1588+
b = s->out_chain->buf;
1589+
1590+
if (b->start + NGX_RTMP_MAX_CHUNK_HEADER != b->pos) {
1591+
type = b->pos[0] & 0x1f;
1592+
if (type != NGX_RTMP_MSG_VIDEO && type != NGX_RTMP_MSG_AUDIO) {
1593+
return;
1594+
}
1595+
1596+
timestamp = 0;
1597+
pt = (u_char *) &timestamp;
1598+
1599+
p = b->pos + 4;
1600+
pt[2] = *p++;
1601+
pt[1] = *p++;
1602+
pt[0] = *p++;
1603+
pt[3] = *p++;
1604+
1605+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, s->connection->log, 0,
1606+
"flv live: offset_timestamp=%uD", s->offset_timestamp);
1607+
1608+
if (correct) {
1609+
timestamp -= s->offset_timestamp;
1610+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, s->connection->log, 0,
1611+
"flv live: correct timestamp=%uD", timestamp);
1612+
} else {
1613+
timestamp += s->offset_timestamp;
1614+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, s->connection->log, 0,
1615+
"flv live: recover timestamp=%uD", timestamp);
1616+
}
1617+
1618+
p = b->pos + 4;
1619+
p[2] = *pt++;
1620+
p[1] = *pt++;
1621+
p[0] = *pt++;
1622+
p[3] = *pt++;
1623+
}
1624+
}
1625+
1626+
15691627
static ngx_int_t
15701628
ngx_http_flv_live_preprocess(ngx_http_request_t *r,
15711629
ngx_rtmp_connection_t *rconn)
@@ -2091,7 +2149,6 @@ static ngx_chain_t *
20912149
ngx_http_flv_live_append_message(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
20922150
ngx_rtmp_header_t *lh, ngx_chain_t *in)
20932151
{
2094-
ngx_rtmp_header_t header;
20952152
ngx_rtmp_core_srv_conf_t *cscf;
20962153
ngx_http_request_t *r;
20972154

@@ -2105,25 +2162,20 @@ ngx_http_flv_live_append_message(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
21052162
return NULL;
21062163
}
21072164

2108-
header = *h;
2109-
2110-
if (header.type == NGX_RTMP_MSG_VIDEO ||
2111-
header.type == NGX_RTMP_MSG_AUDIO)
2112-
{
2165+
if (h->type == NGX_RTMP_MSG_VIDEO || h->type == NGX_RTMP_MSG_AUDIO) {
21132166
if (!s->offset_timestamp_set) {
21142167
s->offset_timestamp_set = 1;
2115-
s->offset_timestamp = header.timestamp;
2116-
} else if (header.timestamp == 0) {
2168+
s->offset_timestamp = h->timestamp;
2169+
} else if (h->timestamp == 0) {
21172170
s->offset_timestamp = 0;
21182171
}
21192172

2120-
header.timestamp -= s->offset_timestamp;
2121-
ngx_log_error(NGX_LOG_DEBUG_HTTP, s->connection->log, 0,
2173+
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, s->connection->log, 0,
21222174
"flv live: timestamp=%uD, offset_timestamp=%uD",
2123-
header.timestamp, s->offset_timestamp);
2175+
h->timestamp, s->offset_timestamp);
21242176
}
21252177

2126-
return ngx_http_flv_live_append_shared_bufs(cscf, &header, in, r->chunked);
2178+
return ngx_http_flv_live_append_shared_bufs(cscf, h, in, r->chunked);
21272179
}
21282180

21292181

0 commit comments

Comments
 (0)