Skip to content

Commit 1385440

Browse files
committed
Merge branch 'master' into control-redirect
2 parents b8b055d + 5e8fe20 commit 1385440

File tree

2 files changed

+60
-52
lines changed

2 files changed

+60
-52
lines changed

hls/ngx_rtmp_hls_module.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,22 +1348,24 @@ ngx_rtmp_hls_update_fragment(ngx_rtmp_session_t *s, uint64_t ts,
13481348
ngx_msec_t ts_frag_len;
13491349
ngx_int_t same_frag;
13501350
ngx_buf_t *b;
1351+
int64_t d;
13511352

13521353
hacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_hls_module);
1353-
13541354
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_hls_module);
1355-
13561355
f = NULL;
13571356

13581357
if (ctx->opened) {
1359-
13601358
f = ngx_rtmp_hls_get_frag(s, ctx->nfrags);
1361-
f->duration = (ts - ctx->frag_ts) / 90000.;
1359+
d = (int64_t) (ts - ctx->frag_ts);
13621360

1363-
if (f->duration * 1000 > hacf->max_fraglen) {
1361+
if (d > (int64_t) hacf->max_fraglen * 90 || d < -90000) {
13641362
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
1365-
"hls: max fragment length reached");
1363+
"hls: max fragment length reached: %.3f sec, ",
1364+
f->duration);
13661365
boundary = 1;
1366+
1367+
} else {
1368+
f->duration = (ts - ctx->frag_ts) / 90000.;
13671369
}
13681370
}
13691371

ngx_rtmp_mp4_module.c

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ static ngx_int_t ngx_rtmp_mp4_send(ngx_rtmp_session_t *s, ngx_file_t *f,
2424
static ngx_int_t ngx_rtmp_mp4_reset(ngx_rtmp_session_t *s);
2525

2626

27+
#define NGX_RTMP_MP4_MAX_FRAMES 8
28+
29+
2730
#pragma pack(push,4)
2831

2932

@@ -2092,15 +2095,16 @@ ngx_rtmp_mp4_send(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_uint_t *ts)
20922095
ngx_rtmp_header_t h, lh;
20932096
ngx_rtmp_core_srv_conf_t *cscf;
20942097
ngx_chain_t *out, in;
2095-
ngx_rtmp_mp4_track_t *t;
2096-
ngx_rtmp_mp4_cursor_t *cr;
2097-
uint32_t buflen, end_timestamp, sched,
2098-
timestamp, last_timestamp, rdelay;
2098+
ngx_rtmp_mp4_track_t *t, *cur_t;
2099+
ngx_rtmp_mp4_cursor_t *cr, *cur_cr;
2100+
uint32_t buflen, end_timestamp,
2101+
timestamp, last_timestamp, rdelay,
2102+
cur_timestamp;
20992103
ssize_t ret;
21002104
u_char fhdr[5];
21012105
size_t fhdr_size;
21022106
ngx_int_t rc;
2103-
ngx_uint_t n, active;
2107+
ngx_uint_t n, counter;
21042108

21052109
cscf = ngx_rtmp_get_module_srv_conf(s, ngx_rtmp_core_module);
21062110

@@ -2123,31 +2127,57 @@ ngx_rtmp_mp4_send(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_uint_t *ts)
21232127
buflen = (s->buflen ? s->buflen + NGX_RTMP_MP4_BUFLEN_ADDON:
21242128
NGX_RTMP_MP4_DEFAULT_BUFLEN);
21252129

2126-
t = ctx->tracks;
2127-
2128-
sched = 0;
2129-
active = 0;
2130+
counter = 0;
21302131
last_timestamp = 0;
2131-
21322132
end_timestamp = ctx->start_timestamp +
21332133
(ngx_current_msec - ctx->epoch) + buflen;
21342134

2135-
for (n = 0; n < ctx->ntracks; ++n, ++t) {
2136-
cr = &t->cursor;
2135+
for ( ;; ) {
2136+
counter++;
2137+
if (counter > NGX_RTMP_MP4_MAX_FRAMES) {
2138+
return NGX_OK;
2139+
}
21372140

2138-
if (!cr->valid) {
2139-
continue;
2141+
timestamp = 0;
2142+
t = NULL;
2143+
2144+
for (n = 0; n < ctx->ntracks; n++) {
2145+
cur_t = &ctx->tracks[n];
2146+
cur_cr = &cur_t->cursor;
2147+
2148+
if (!cur_cr->valid) {
2149+
continue;
2150+
}
2151+
2152+
cur_timestamp = ngx_rtmp_mp4_to_rtmp_timestamp(cur_t,
2153+
cur_cr->timestamp);
2154+
2155+
if (t == NULL || cur_timestamp < timestamp) {
2156+
timestamp = cur_timestamp;
2157+
t = cur_t;
2158+
}
21402159
}
21412160

2142-
timestamp = ngx_rtmp_mp4_to_rtmp_timestamp(t, cr->timestamp);
2161+
if (t == NULL) {
2162+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
2163+
"mp4: no track");
2164+
return NGX_DONE;
2165+
}
21432166

21442167
if (timestamp > end_timestamp) {
21452168
ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
2146-
"mp4: track#%ui ahead %uD > %uD",
2147-
t->id, timestamp, end_timestamp);
2148-
goto next;
2169+
"mp4: track#%ui ahead %uD > %uD",
2170+
t->id, timestamp, end_timestamp);
2171+
2172+
if (ts) {
2173+
*ts = last_timestamp;
2174+
}
2175+
2176+
return (uint32_t) (timestamp - end_timestamp);
21492177
}
21502178

2179+
cr = &t->cursor;
2180+
21512181
last_timestamp = ngx_rtmp_mp4_to_rtmp_timestamp(t, cr->last_timestamp);
21522182

21532183
ngx_memzero(&h, sizeof(h));
@@ -2245,7 +2275,7 @@ ngx_rtmp_mp4_send(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_uint_t *ts)
22452275
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
22462276
"mp4: track#%ui too big frame: %D>%uz",
22472277
t->id, cr->size, sizeof(ngx_rtmp_mp4_buffer));
2248-
continue;
2278+
goto next;
22492279
}
22502280

22512281
ret = ngx_read_file(f, ngx_rtmp_mp4_buffer + fhdr_size,
@@ -2254,7 +2284,7 @@ ngx_rtmp_mp4_send(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_uint_t *ts)
22542284
if (ret != (ssize_t) cr->size) {
22552285
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
22562286
"mp4: track#%ui could not read frame", t->id);
2257-
continue;
2287+
goto next;
22582288
}
22592289

22602290
in.buf = &in_buf;
@@ -2273,35 +2303,11 @@ ngx_rtmp_mp4_send(ngx_rtmp_session_t *s, ngx_file_t *f, ngx_uint_t *ts)
22732303

22742304
s->current_time = timestamp;
22752305

2276-
if (ngx_rtmp_mp4_next(s, t) != NGX_OK) {
2277-
continue;
2278-
}
2279-
22802306
next:
2281-
active = 1;
2282-
2283-
if (timestamp > end_timestamp &&
2284-
(sched == 0 || timestamp < end_timestamp + sched))
2285-
{
2286-
sched = (uint32_t) (timestamp - end_timestamp);
2307+
if (ngx_rtmp_mp4_next(s, t) != NGX_OK) {
2308+
return NGX_DONE;
22872309
}
22882310
}
2289-
2290-
if (sched) {
2291-
return sched;
2292-
}
2293-
2294-
if (active) {
2295-
return NGX_OK;
2296-
}
2297-
2298-
if (ts) {
2299-
*ts = last_timestamp;
2300-
}
2301-
2302-
/*ngx_rtmp_mp4_reset(s);*/
2303-
2304-
return NGX_DONE;
23052311
}
23062312

23072313

0 commit comments

Comments
 (0)