Skip to content

Commit dca5847

Browse files
committed
[fix] fix #115 when 'chunked_transfer_encoding' was on.
1 parent 2c7e1d1 commit dca5847

File tree

3 files changed

+61
-34
lines changed

3 files changed

+61
-34
lines changed

README.CN.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,13 @@ nginx-http-flv-module包含了[nginx-rtmp-module](https://github.com/arut/nginx-
247247

248248
最好将配置项`worker_processes`设置为1,因为在多进程模式下,`ngx_rtmp_stat_module`可能不会从指定的worker进程获取统计数据,因为HTTP请求是被随机分配给worker进程的。`ngx_rtmp_control_module`也有同样的问题。这个问题可以通过这个补丁[per-worker-listener](https://github.com/arut/nginx-patches/blob/master/per-worker-listener)优化。
249249

250-
另外,`vhost`功能在多进程模式下还不能完全正确运行,等待修复。例如,下面的配置在多进程模式下是没有问题的:
250+
另外,`vhost`功能在多进程模式下还不能完全正确运行,等待修复。例如,不管向哪个域名推流,下面的配置在多进程模式下是没有问题的:
251251

252252
rtmp {
253253
...
254254
server {
255255
listen 1935;
256+
server_name 1st_domain_name;
256257

257258
application myapp {
258259
...
@@ -261,7 +262,7 @@ nginx-http-flv-module包含了[nginx-rtmp-module](https://github.com/arut/nginx-
261262

262263
server {
263264
listen 1935;
264-
server_name localhost;
265+
server_name 2nd_domain_name;
265266

266267
application myapp {
267268
...
@@ -275,6 +276,7 @@ nginx-http-flv-module包含了[nginx-rtmp-module](https://github.com/arut/nginx-
275276
...
276277
server {
277278
listen 1935;
279+
server_name 1st_domain_name;
278280

279281
application myapp {
280282
...
@@ -283,7 +285,7 @@ nginx-http-flv-module包含了[nginx-rtmp-module](https://github.com/arut/nginx-
283285

284286
server {
285287
listen 1945;
286-
server_name localhost;
288+
server_name 2nd_domain_name;
287289

288290
application myapp {
289291
...

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,13 @@ The directives `rtmp_auto_push`, `rtmp_auto_push_reconnect` and `rtmp_socket_dir
247247

248248
It's better to specify the directive `worker_processes` as 1, because `ngx_rtmp_stat_module` may not get statistics from a specified worker process in multi-processes mode, for HTTP requests are randomly distributed to worker processes. `ngx_rtmp_control_module` has the same problem. The problem can be optimized by this patch [per-worker-listener](https://github.com/arut/nginx-patches/blob/master/per-worker-listener).
249249

250-
In addtion, `vhost` feature is not perfect in multi-processes mode yet, waiting to be fixed. For example, the following configuration is OK in multi-processes mode:
250+
In addtion, `vhost` feature is not perfect in multi-processes mode yet, waiting to be fixed. For example, whichever domain name streams are pushed to, the following configuration is OK in multi-processes mode:
251251

252252
rtmp {
253253
...
254254
server {
255255
listen 1935;
256+
server_name 1st_domain_name;
256257

257258
application myapp {
258259
...
@@ -261,7 +262,7 @@ In addtion, `vhost` feature is not perfect in multi-processes mode yet, waiting
261262

262263
server {
263264
listen 1935;
264-
server_name localhost;
265+
server_name 2nd_domain_name;
265266

266267
application myapp {
267268
...
@@ -275,6 +276,7 @@ While the following configuration doesn't work for play requests distinated to t
275276
...
276277
server {
277278
listen 1935;
279+
server_name 1st_domain_name;
278280

279281
application myapp {
280282
...
@@ -283,7 +285,7 @@ While the following configuration doesn't work for play requests distinated to t
283285

284286
server {
285287
listen 1945;
286-
server_name localhost;
288+
server_name 2nd_domain_name;
287289

288290
application myapp {
289291
...

ngx_http_flv_live_module.c

+51-28
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+
207+
static ngx_int_t ngx_http_flv_live_send(ngx_rtmp_session_t *s);
206208
static void ngx_http_flv_live_correct_timestamp(ngx_rtmp_session_t *s,
207209
ngx_flag_t correct);
208210

@@ -1522,15 +1524,11 @@ ngx_http_flv_live_write_handler(ngx_event_t *wev)
15221524

15231525
if (s->out_chain == NULL && s->out_pos != s->out_last) {
15241526
s->out_chain = s->out[s->out_pos];
1525-
ngx_http_flv_live_correct_timestamp(s, 1);
15261527
s->out_bpos = s->out_chain->buf->pos;
1527-
} else if (s->out_chain) {
1528-
ngx_http_flv_live_correct_timestamp(s, 1);
15291528
}
15301529

15311530
while (s->out_chain) {
1532-
n = c->send(c, s->out_bpos, s->out_chain->buf->last - s->out_bpos);
1533-
ngx_http_flv_live_correct_timestamp(s, 0);
1531+
n = ngx_http_flv_live_send(s);
15341532

15351533
if (n == NGX_AGAIN || n == 0) {
15361534
ngx_add_timer(c->write, s->timeout);
@@ -1562,7 +1560,6 @@ ngx_http_flv_live_write_handler(ngx_event_t *wev)
15621560
break;
15631561
}
15641562
s->out_chain = s->out[s->out_pos];
1565-
ngx_http_flv_live_correct_timestamp(s, 1);
15661563
}
15671564
s->out_bpos = s->out_chain->buf->pos;
15681565
}
@@ -1576,41 +1573,67 @@ ngx_http_flv_live_write_handler(ngx_event_t *wev)
15761573
}
15771574

15781575

1576+
static ngx_int_t
1577+
ngx_http_flv_live_send(ngx_rtmp_session_t *s)
1578+
{
1579+
ngx_int_t n;
1580+
ngx_connection_t *c;
1581+
ngx_http_request_t *r;
1582+
1583+
c = s->connection;
1584+
r = s->data;
1585+
1586+
if (r->chunked) {
1587+
if (s->out_chain == s->out[s->out_pos]) {
1588+
n = c->send(c, s->out_bpos, s->out_chain->buf->last - s->out_bpos);
1589+
if (n == NGX_AGAIN || n == 0 || n < 0) {
1590+
return n;
1591+
}
1592+
1593+
if (n != s->out_chain->buf->last - s->out_bpos ||
1594+
s->out_chain->next == NULL)
1595+
{
1596+
return n;
1597+
}
1598+
1599+
s->out_chain = s->out_chain->next;
1600+
1601+
s->out_bytes += n;
1602+
s->out_bpos = s->out_chain->buf->pos;
1603+
s->ping_reset = 1;
1604+
ngx_rtmp_update_bandwidth(&ngx_rtmp_bw_out, n);
1605+
}
1606+
}
1607+
1608+
ngx_http_flv_live_correct_timestamp(s, 1);
1609+
n = c->send(c, s->out_bpos, s->out_chain->buf->last - s->out_bpos);
1610+
ngx_http_flv_live_correct_timestamp(s, 0);
1611+
1612+
return n;
1613+
}
1614+
1615+
15791616
static void
15801617
ngx_http_flv_live_correct_timestamp(ngx_rtmp_session_t *s, ngx_flag_t correct)
15811618
{
1582-
uint8_t type;
1583-
uint32_t timestamp;
1584-
u_char *p, *pt;
1585-
ngx_chain_t *cl;
1586-
ngx_buf_t *b;
1587-
ngx_http_request_t *r;
1619+
uint8_t type;
1620+
uint32_t timestamp;
1621+
u_char *p, *pt;
1622+
ngx_chain_t *cl;
1623+
ngx_buf_t *b;
15881624

15891625
cl = s->out_chain;
15901626
if (cl == NULL) {
15911627
return;
15921628
}
15931629

1594-
if (cl != s->out[s->out_pos]) {
1595-
return;
1596-
}
1597-
1598-
r = s->data;
1599-
if (r->chunked) {
1600-
cl = cl->next;
1601-
if (cl == NULL) {
1602-
return;
1603-
}
1604-
}
1605-
16061630
b = cl->buf;
16071631
if (b->start + NGX_RTMP_MAX_CHUNK_HEADER != b->pos) {
16081632
type = b->pos[0] & 0x1f;
16091633

1610-
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, s->connection->log, 0,
1611-
"flv live: type=%uD, chunked=%uD, "
1612-
"correct=%uD, offset_timestamp=%uD",
1613-
type, r->chunked, correct, s->offset_timestamp);
1634+
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, s->connection->log, 0,
1635+
"flv live: type=%uD, correct=%uD, offset_timestamp=%uD",
1636+
type, correct, s->offset_timestamp);
16141637

16151638
if (type != NGX_RTMP_MSG_VIDEO && type != NGX_RTMP_MSG_AUDIO) {
16161639
return;

0 commit comments

Comments
 (0)