summaryrefslogtreecommitdiff
path: root/src/http/modules/ngx_http_mp4_module.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/http/modules/ngx_http_mp4_module.c')
-rw-r--r--src/http/modules/ngx_http_mp4_module.c754
1 files changed, 596 insertions, 158 deletions
diff --git a/src/http/modules/ngx_http_mp4_module.c b/src/http/modules/ngx_http_mp4_module.c
index 426a0b97f..8f439ba92 100644
--- a/src/http/modules/ngx_http_mp4_module.c
+++ b/src/http/modules/ngx_http_mp4_module.c
@@ -27,14 +27,15 @@
#define NGX_HTTP_MP4_CTTS_ATOM 15
#define NGX_HTTP_MP4_CTTS_DATA 16
#define NGX_HTTP_MP4_STSC_ATOM 17
-#define NGX_HTTP_MP4_STSC_CHUNK 18
+#define NGX_HTTP_MP4_STSC_START 18
#define NGX_HTTP_MP4_STSC_DATA 19
-#define NGX_HTTP_MP4_STSZ_ATOM 20
-#define NGX_HTTP_MP4_STSZ_DATA 21
-#define NGX_HTTP_MP4_STCO_ATOM 22
-#define NGX_HTTP_MP4_STCO_DATA 23
-#define NGX_HTTP_MP4_CO64_ATOM 24
-#define NGX_HTTP_MP4_CO64_DATA 25
+#define NGX_HTTP_MP4_STSC_END 20
+#define NGX_HTTP_MP4_STSZ_ATOM 21
+#define NGX_HTTP_MP4_STSZ_DATA 22
+#define NGX_HTTP_MP4_STCO_ATOM 23
+#define NGX_HTTP_MP4_STCO_DATA 24
+#define NGX_HTTP_MP4_CO64_ATOM 25
+#define NGX_HTTP_MP4_CO64_DATA 26
#define NGX_HTTP_MP4_LAST_ATOM NGX_HTTP_MP4_CO64_DATA
@@ -62,10 +63,15 @@ typedef struct {
uint32_t chunks;
ngx_uint_t start_sample;
+ ngx_uint_t end_sample;
ngx_uint_t start_chunk;
- ngx_uint_t chunk_samples;
- uint64_t chunk_samples_size;
+ ngx_uint_t end_chunk;
+ ngx_uint_t start_chunk_samples;
+ ngx_uint_t end_chunk_samples;
+ uint64_t start_chunk_samples_size;
+ uint64_t end_chunk_samples_size;
off_t start_offset;
+ off_t end_offset;
size_t tkhd_size;
size_t mdhd_size;
@@ -95,7 +101,8 @@ typedef struct {
ngx_buf_t ctts_atom_buf;
ngx_buf_t ctts_data_buf;
ngx_buf_t stsc_atom_buf;
- ngx_buf_t stsc_chunk_buf;
+ ngx_buf_t stsc_start_chunk_buf;
+ ngx_buf_t stsc_end_chunk_buf;
ngx_buf_t stsc_data_buf;
ngx_buf_t stsz_atom_buf;
ngx_buf_t stsz_data_buf;
@@ -104,7 +111,8 @@ typedef struct {
ngx_buf_t co64_atom_buf;
ngx_buf_t co64_data_buf;
- ngx_mp4_stsc_entry_t stsc_chunk_entry;
+ ngx_mp4_stsc_entry_t stsc_start_chunk_entry;
+ ngx_mp4_stsc_entry_t stsc_end_chunk_entry;
} ngx_http_mp4_trak_t;
@@ -121,6 +129,7 @@ typedef struct {
off_t end;
off_t content_length;
ngx_uint_t start;
+ ngx_uint_t length;
uint32_t timescale;
ngx_http_request_t *request;
ngx_array_t trak;
@@ -219,7 +228,7 @@ static ngx_int_t ngx_http_mp4_read_moov_atom(ngx_http_mp4_file_t *mp4,
static ngx_int_t ngx_http_mp4_read_mdat_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static size_t ngx_http_mp4_update_mdat_atom(ngx_http_mp4_file_t *mp4,
- off_t start_offset);
+ off_t start_offset, off_t end_offset);
static ngx_int_t ngx_http_mp4_read_mvhd_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_read_trak_atom(ngx_http_mp4_file_t *mp4,
@@ -258,18 +267,26 @@ static ngx_int_t ngx_http_mp4_read_stts_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_update_stts_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak);
+static ngx_int_t ngx_http_mp4_crop_stts_data(ngx_http_mp4_file_t *mp4,
+ ngx_http_mp4_trak_t *trak, ngx_uint_t start);
static ngx_int_t ngx_http_mp4_read_stss_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_update_stss_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak);
+static void ngx_http_mp4_crop_stss_data(ngx_http_mp4_file_t *mp4,
+ ngx_http_mp4_trak_t *trak, ngx_uint_t start);
static ngx_int_t ngx_http_mp4_read_ctts_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static void ngx_http_mp4_update_ctts_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak);
+static void ngx_http_mp4_crop_ctts_data(ngx_http_mp4_file_t *mp4,
+ ngx_http_mp4_trak_t *trak, ngx_uint_t start);
static ngx_int_t ngx_http_mp4_read_stsc_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_update_stsc_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak);
+static ngx_int_t ngx_http_mp4_crop_stsc_data(ngx_http_mp4_file_t *mp4,
+ ngx_http_mp4_trak_t *trak, ngx_uint_t start);
static ngx_int_t ngx_http_mp4_read_stsz_atom(ngx_http_mp4_file_t *mp4,
uint64_t atom_data_size);
static ngx_int_t ngx_http_mp4_update_stsz_atom(ngx_http_mp4_file_t *mp4,
@@ -403,8 +420,8 @@ ngx_http_mp4_handler(ngx_http_request_t *r)
{
u_char *last;
size_t root;
- ngx_int_t rc, start;
- ngx_uint_t level;
+ ngx_int_t rc, start, end;
+ ngx_uint_t level, length;
ngx_str_t path, value;
ngx_log_t *log;
ngx_buf_t *b;
@@ -509,6 +526,7 @@ ngx_http_mp4_handler(ngx_http_request_t *r)
r->allow_ranges = 1;
start = -1;
+ length = 0;
r->headers_out.content_length_n = of.size;
mp4 = NULL;
b = NULL;
@@ -530,10 +548,30 @@ ngx_http_mp4_handler(ngx_http_request_t *r)
start = -1;
}
}
+
+ if (ngx_http_arg(r, (u_char *) "end", 3, &value) == NGX_OK) {
+
+ ngx_set_errno(0);
+ end = (int) (strtod((char *) value.data, NULL) * 1000);
+
+ if (ngx_errno != 0) {
+ end = -1;
+ }
+
+ if (end > 0) {
+ if (start < 0) {
+ start = 0;
+ }
+
+ if (end > start) {
+ length = end - start;
+ }
+ }
+ }
}
if (start >= 0) {
- r->allow_ranges = 0;
+ r->single_range = 1;
mp4 = ngx_pcalloc(r->pool, sizeof(ngx_http_mp4_file_t));
if (mp4 == NULL) {
@@ -545,6 +583,7 @@ ngx_http_mp4_handler(ngx_http_request_t *r)
mp4->file.log = r->connection->log;
mp4->end = of.size;
mp4->start = (ngx_uint_t) start;
+ mp4->length = length;
mp4->request = r;
switch (ngx_http_mp4_process(mp4)) {
@@ -650,15 +689,15 @@ ngx_http_mp4_handler(ngx_http_request_t *r)
static ngx_int_t
ngx_http_mp4_process(ngx_http_mp4_file_t *mp4)
{
- off_t start_offset, adjustment;
+ off_t start_offset, end_offset, adjustment;
ngx_int_t rc;
ngx_uint_t i, j;
ngx_chain_t **prev;
ngx_http_mp4_trak_t *trak;
ngx_http_mp4_conf_t *conf;
- ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
- "mp4 start:%ui", mp4->start);
+ ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "mp4 start:%ui, length:%ui", mp4->start, mp4->length);
conf = ngx_http_get_module_loc_conf(mp4->request, ngx_http_mp4_module);
@@ -700,6 +739,7 @@ ngx_http_mp4_process(ngx_http_mp4_file_t *mp4)
}
start_offset = mp4->end;
+ end_offset = 0;
trak = mp4->trak.elts;
for (i = 0; i < mp4->trak.nelts; i++) {
@@ -747,6 +787,10 @@ ngx_http_mp4_process(ngx_http_mp4_file_t *mp4)
start_offset = trak[i].start_offset;
}
+ if (end_offset < trak[i].end_offset) {
+ end_offset = trak[i].end_offset;
+ }
+
*prev = &trak[i].out[NGX_HTTP_MP4_TRAK_ATOM];
prev = &trak[i].out[NGX_HTTP_MP4_TRAK_ATOM].next;
@@ -758,6 +802,10 @@ ngx_http_mp4_process(ngx_http_mp4_file_t *mp4)
}
}
+ if (end_offset < start_offset) {
+ end_offset = start_offset;
+ }
+
mp4->moov_size += 8;
ngx_mp4_set_32value(mp4->moov_atom_header, mp4->moov_size);
@@ -774,7 +822,7 @@ ngx_http_mp4_process(ngx_http_mp4_file_t *mp4)
}
adjustment = mp4->ftyp_size + mp4->moov_size
- + ngx_http_mp4_update_mdat_atom(mp4, start_offset)
+ + ngx_http_mp4_update_mdat_atom(mp4, start_offset, end_offset)
- start_offset;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
@@ -1018,7 +1066,7 @@ ngx_http_mp4_read_moov_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
no_mdat = (mp4->mdat_atom.buf == NULL);
- if (no_mdat && mp4->start == 0) {
+ if (no_mdat && mp4->start == 0 && mp4->length == 0) {
/*
* send original file if moov atom resides before
* mdat atom and client requests integral file
@@ -1117,7 +1165,8 @@ ngx_http_mp4_read_mdat_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
static size_t
-ngx_http_mp4_update_mdat_atom(ngx_http_mp4_file_t *mp4, off_t start_offset)
+ngx_http_mp4_update_mdat_atom(ngx_http_mp4_file_t *mp4, off_t start_offset,
+ off_t end_offset)
{
off_t atom_data_size;
u_char *atom_header;
@@ -1125,8 +1174,9 @@ ngx_http_mp4_update_mdat_atom(ngx_http_mp4_file_t *mp4, off_t start_offset)
uint64_t atom_size;
ngx_buf_t *atom;
- atom_data_size = mp4->mdat_data.buf->file_last - start_offset;
+ atom_data_size = end_offset - start_offset;
mp4->mdat_data.buf->file_pos = start_offset;
+ mp4->mdat_data.buf->file_last = end_offset;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
"mdat new offset @%O:%O", start_offset, atom_data_size);
@@ -1208,7 +1258,7 @@ ngx_http_mp4_read_mvhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
u_char *atom_header;
size_t atom_size;
uint32_t timescale;
- uint64_t duration, start_time;
+ uint64_t duration, start_time, length_time;
ngx_buf_t *atom;
ngx_mp4_mvhd_atom_t *mvhd_atom;
ngx_mp4_mvhd64_atom_t *mvhd64_atom;
@@ -1262,6 +1312,14 @@ ngx_http_mp4_read_mvhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
duration -= start_time;
+ if (mp4->length) {
+ length_time = (uint64_t) mp4->length * timescale / 1000;
+
+ if (duration > length_time) {
+ duration = length_time;
+ }
+ }
+
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
"mvhd new duration:%uL, time:%.3fs",
duration, (double) duration / timescale);
@@ -1407,7 +1465,7 @@ ngx_http_mp4_read_tkhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
{
u_char *atom_header;
size_t atom_size;
- uint64_t duration, start_time;
+ uint64_t duration, start_time, length_time;
ngx_buf_t *atom;
ngx_http_mp4_trak_t *trak;
ngx_mp4_tkhd_atom_t *tkhd_atom;
@@ -1449,7 +1507,7 @@ ngx_http_mp4_read_tkhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
start_time = (uint64_t) mp4->start * mp4->timescale / 1000;
- if (duration < start_time) {
+ if (duration <= start_time) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
"tkhd duration is less than start time");
return NGX_DECLINED;
@@ -1457,6 +1515,14 @@ ngx_http_mp4_read_tkhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
duration -= start_time;
+ if (mp4->length) {
+ length_time = (uint64_t) mp4->length * mp4->timescale / 1000;
+
+ if (duration > length_time) {
+ duration = length_time;
+ }
+ }
+
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
"tkhd new duration:%uL, time:%.3fs",
duration, (double) duration / mp4->timescale);
@@ -1558,7 +1624,7 @@ ngx_http_mp4_read_mdhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
u_char *atom_header;
size_t atom_size;
uint32_t timescale;
- uint64_t duration, start_time;
+ uint64_t duration, start_time, length_time;
ngx_buf_t *atom;
ngx_http_mp4_trak_t *trak;
ngx_mp4_mdhd_atom_t *mdhd_atom;
@@ -1602,7 +1668,7 @@ ngx_http_mp4_read_mdhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
start_time = (uint64_t) mp4->start * timescale / 1000;
- if (duration < start_time) {
+ if (duration <= start_time) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
"mdhd duration is less than start time");
return NGX_DECLINED;
@@ -1610,6 +1676,14 @@ ngx_http_mp4_read_mdhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
duration -= start_time;
+ if (mp4->length) {
+ length_time = (uint64_t) mp4->length * timescale / 1000;
+
+ if (duration > length_time) {
+ duration = length_time;
+ }
+ }
+
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
"mdhd new duration:%uL, time:%.3fs",
duration, (double) duration / timescale);
@@ -1981,13 +2055,9 @@ static ngx_int_t
ngx_http_mp4_update_stts_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak)
{
- size_t atom_size;
- uint32_t entries, count, duration;
- uint64_t start_time;
- ngx_buf_t *atom, *data;
- ngx_uint_t start_sample;
- ngx_mp4_stts_atom_t *stts_atom;
- ngx_mp4_stts_entry_t *entry, *end;
+ size_t atom_size;
+ ngx_buf_t *atom, *data;
+ ngx_mp4_stts_atom_t *stts_atom;
/*
* mdia.minf.stbl.stts updating requires trak->timescale
@@ -2006,12 +2076,60 @@ ngx_http_mp4_update_stts_atom(ngx_http_mp4_file_t *mp4,
return NGX_ERROR;
}
- entries = trak->time_to_sample_entries;
- start_time = (uint64_t) mp4->start * trak->timescale / 1000;
+ if (ngx_http_mp4_crop_stts_data(mp4, trak, 1) != NGX_OK) {
+ return NGX_ERROR;
+ }
+
+ if (ngx_http_mp4_crop_stts_data(mp4, trak, 0) != NGX_OK) {
+ return NGX_ERROR;
+ }
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
- "time-to-sample start_time:%uL", start_time);
+ "time-to-sample entries:%uD", trak->time_to_sample_entries);
+
+ atom_size = sizeof(ngx_mp4_stts_atom_t) + (data->last - data->pos);
+ trak->size += atom_size;
+
+ atom = trak->out[NGX_HTTP_MP4_STTS_ATOM].buf;
+ stts_atom = (ngx_mp4_stts_atom_t *) atom->pos;
+ ngx_mp4_set_32value(stts_atom->size, atom_size);
+ ngx_mp4_set_32value(stts_atom->entries, trak->time_to_sample_entries);
+
+ return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_mp4_crop_stts_data(ngx_http_mp4_file_t *mp4,
+ ngx_http_mp4_trak_t *trak, ngx_uint_t start)
+{
+ uint32_t count, duration, rest;
+ uint64_t start_time;
+ ngx_buf_t *data;
+ ngx_uint_t start_sample, entries, start_sec;
+ ngx_mp4_stts_entry_t *entry, *end;
+
+ if (start) {
+ start_sec = mp4->start;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "mp4 stts crop start_time:%ui", start_sec);
+
+ } else if (mp4->length) {
+ start_sec = mp4->length;
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "mp4 stts crop end_time:%ui", start_sec);
+
+ } else {
+ return NGX_OK;
+ }
+
+ data = trak->out[NGX_HTTP_MP4_STTS_DATA].buf;
+
+ start_time = (uint64_t) start_sec * trak->timescale / 1000;
+
+ entries = trak->time_to_sample_entries;
start_sample = 0;
entry = (ngx_mp4_stts_entry_t *) data->pos;
end = (ngx_mp4_stts_entry_t *) data->last;
@@ -2020,13 +2138,13 @@ ngx_http_mp4_update_stts_atom(ngx_http_mp4_file_t *mp4,
count = ngx_mp4_get_32value(entry->count);
duration = ngx_mp4_get_32value(entry->duration);
- ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
- "count:%uD, duration:%uD", count, duration);
+ ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "time:%uL, count:%uD, duration:%uD",
+ start_time, count, duration);
if (start_time < (uint64_t) count * duration) {
start_sample += (ngx_uint_t) (start_time / duration);
- count -= (uint32_t) (start_time / duration);
- ngx_mp4_set_32value(entry->count, count);
+ rest = (uint32_t) (start_time / duration);
goto found;
}
@@ -2036,27 +2154,44 @@ ngx_http_mp4_update_stts_atom(ngx_http_mp4_file_t *mp4,
entry++;
}
- ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
- "start time is out mp4 stts samples in \"%s\"",
- mp4->file.name.data);
+ if (start) {
+ ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
+ "start time is out mp4 stts samples in \"%s\"",
+ mp4->file.name.data);
- return NGX_ERROR;
+ return NGX_ERROR;
+
+ } else {
+ trak->end_sample = trak->start_sample + start_sample;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "end_sample:%ui", trak->end_sample);
+
+ return NGX_OK;
+ }
found:
- ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
- "start_sample:%ui, new count:%uD", start_sample, count);
+ if (start) {
+ ngx_mp4_set_32value(entry->count, count - rest);
+ data->pos = (u_char *) entry;
+ trak->time_to_sample_entries = entries;
+ trak->start_sample = start_sample;
- trak->start_sample = start_sample;
+ ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "start_sample:%ui, new count:%uD",
+ trak->start_sample, count - rest);
- data->pos = (u_char *) entry;
- atom_size = sizeof(ngx_mp4_stts_atom_t) + (data->last - data->pos);
- trak->size += atom_size;
+ } else {
+ ngx_mp4_set_32value(entry->count, rest);
+ data->last = (u_char *) (entry + 1);
+ trak->time_to_sample_entries -= entries - 1;
+ trak->end_sample = trak->start_sample + start_sample;
- atom = trak->out[NGX_HTTP_MP4_STTS_ATOM].buf;
- stts_atom = (ngx_mp4_stts_atom_t *) atom->pos;
- ngx_mp4_set_32value(stts_atom->size, atom_size);
- ngx_mp4_set_32value(stts_atom->entries, entries);
+ ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "end_sample:%ui, new count:%uD",
+ trak->end_sample, rest);
+ }
return NGX_OK;
}
@@ -2138,7 +2273,7 @@ ngx_http_mp4_update_stss_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak)
{
size_t atom_size;
- uint32_t entries, sample, start_sample, *entry, *end;
+ uint32_t sample, start_sample, *entry, *end;
ngx_buf_t *atom, *data;
ngx_http_mp4_stss_atom_t *stss_atom;
@@ -2157,18 +2292,79 @@ ngx_http_mp4_update_stss_atom(ngx_http_mp4_file_t *mp4,
return NGX_OK;
}
+ ngx_http_mp4_crop_stss_data(mp4, trak, 1);
+ ngx_http_mp4_crop_stss_data(mp4, trak, 0);
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "sync sample entries:%uD", trak->sync_samples_entries);
+
+ if (trak->sync_samples_entries) {
+ entry = (uint32_t *) data->pos;
+ end = (uint32_t *) data->last;
+
+ start_sample = trak->start_sample;
+
+ while (entry < end) {
+ sample = ngx_mp4_get_32value(entry);
+ sample -= start_sample;
+ ngx_mp4_set_32value(entry, sample);
+ entry++;
+ }
+
+ } else {
+ trak->out[NGX_HTTP_MP4_STSS_DATA].buf = NULL;
+ }
+
+ atom_size = sizeof(ngx_http_mp4_stss_atom_t) + (data->last - data->pos);
+ trak->size += atom_size;
+
+ atom = trak->out[NGX_HTTP_MP4_STSS_ATOM].buf;
+ stss_atom = (ngx_http_mp4_stss_atom_t *) atom->pos;
+
+ ngx_mp4_set_32value(stss_atom->size, atom_size);
+ ngx_mp4_set_32value(stss_atom->entries, trak->sync_samples_entries);
+
+ return NGX_OK;
+}
+
+
+static void
+ngx_http_mp4_crop_stss_data(ngx_http_mp4_file_t *mp4,
+ ngx_http_mp4_trak_t *trak, ngx_uint_t start)
+{
+ uint32_t sample, start_sample, *entry, *end;
+ ngx_buf_t *data;
+ ngx_uint_t entries;
+
/* sync samples starts from 1 */
- start_sample = trak->start_sample + 1;
- entries = trak->sync_samples_entries;
+ if (start) {
+ start_sample = trak->start_sample + 1;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "mp4 stss crop start_sample:%uD", start_sample);
+
+ } else if (mp4->length) {
+ start_sample = trak->end_sample + 1;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "mp4 stss crop end_sample:%uD", start_sample);
+
+ } else {
+ return;
+ }
+
+ data = trak->out[NGX_HTTP_MP4_STSS_DATA].buf;
+
+ entries = trak->sync_samples_entries;
entry = (uint32_t *) data->pos;
end = (uint32_t *) data->last;
while (entry < end) {
sample = ngx_mp4_get_32value(entry);
- ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
- "start:%uD, sync:%uD", start_sample, sample);
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "sync:%uD", sample);
if (sample >= start_sample) {
goto found;
@@ -2179,31 +2375,18 @@ ngx_http_mp4_update_stss_atom(ngx_http_mp4_file_t *mp4,
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
- "start sample is out of mp4 stss atom");
+ "sample is out of mp4 stss atom");
found:
- data->pos = (u_char *) entry;
-
- start_sample = trak->start_sample;
+ if (start) {
+ data->pos = (u_char *) entry;
+ trak->sync_samples_entries = entries;
- while (entry < end) {
- sample = ngx_mp4_get_32value(entry);
- sample -= start_sample;
- ngx_mp4_set_32value(entry, sample);
- entry++;
+ } else {
+ data->last = (u_char *) entry;
+ trak->sync_samples_entries -= entries;
}
-
- atom_size = sizeof(ngx_http_mp4_stss_atom_t) + (data->last - data->pos);
- trak->size += atom_size;
-
- atom = trak->out[NGX_HTTP_MP4_STSS_ATOM].buf;
- stss_atom = (ngx_http_mp4_stss_atom_t *) atom->pos;
-
- ngx_mp4_set_32value(stss_atom->size, atom_size);
- ngx_mp4_set_32value(stss_atom->entries, entries);
-
- return NGX_OK;
}
@@ -2287,11 +2470,9 @@ static void
ngx_http_mp4_update_ctts_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak)
{
- size_t atom_size;
- uint32_t entries, count, start_sample;
- ngx_buf_t *atom, *data;
- ngx_mp4_ctts_atom_t *ctts_atom;
- ngx_mp4_ctts_entry_t *entry, *end;
+ size_t atom_size;
+ ngx_buf_t *atom, *data;
+ ngx_mp4_ctts_atom_t *ctts_atom;
/*
* mdia.minf.stbl.ctts updating requires trak->start_sample
@@ -2308,8 +2489,61 @@ ngx_http_mp4_update_ctts_atom(ngx_http_mp4_file_t *mp4,
return;
}
+ ngx_http_mp4_crop_ctts_data(mp4, trak, 1);
+ ngx_http_mp4_crop_ctts_data(mp4, trak, 0);
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "composition offset entries:%uD",
+ trak->composition_offset_entries);
+
+ if (trak->composition_offset_entries == 0) {
+ trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf = NULL;
+ trak->out[NGX_HTTP_MP4_CTTS_DATA].buf = NULL;
+ return;
+ }
+
+ atom_size = sizeof(ngx_mp4_ctts_atom_t) + (data->last - data->pos);
+ trak->size += atom_size;
+
+ atom = trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf;
+ ctts_atom = (ngx_mp4_ctts_atom_t *) atom->pos;
+
+ ngx_mp4_set_32value(ctts_atom->size, atom_size);
+ ngx_mp4_set_32value(ctts_atom->entries, trak->composition_offset_entries);
+
+ return;
+}
+
+
+static void
+ngx_http_mp4_crop_ctts_data(ngx_http_mp4_file_t *mp4,
+ ngx_http_mp4_trak_t *trak, ngx_uint_t start)
+{
+ uint32_t count, start_sample, rest;
+ ngx_buf_t *data;
+ ngx_uint_t entries;
+ ngx_mp4_ctts_entry_t *entry, *end;
+
/* sync samples starts from 1 */
- start_sample = trak->start_sample + 1;
+
+ if (start) {
+ start_sample = trak->start_sample + 1;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "mp4 ctts crop start_sample:%uD", start_sample);
+
+ } else if (mp4->length) {
+ start_sample = trak->end_sample - trak->start_sample + 1;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "mp4 ctts crop end_sample:%uD", start_sample);
+
+ } else {
+ return;
+ }
+
+ data = trak->out[NGX_HTTP_MP4_CTTS_DATA].buf;
+
entries = trak->composition_offset_entries;
entry = (ngx_mp4_ctts_entry_t *) data->pos;
end = (ngx_mp4_ctts_entry_t *) data->last;
@@ -2318,12 +2552,11 @@ ngx_http_mp4_update_ctts_atom(ngx_http_mp4_file_t *mp4,
count = ngx_mp4_get_32value(entry->count);
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
- "start:%uD, count:%uD, offset:%uD",
+ "sample:%uD, count:%uD, offset:%uD",
start_sample, count, ngx_mp4_get_32value(entry->offset));
if (start_sample <= count) {
- count -= (start_sample - 1);
- ngx_mp4_set_32value(entry->count, count);
+ rest = start_sample - 1;
goto found;
}
@@ -2332,24 +2565,25 @@ ngx_http_mp4_update_ctts_atom(ngx_http_mp4_file_t *mp4,
entry++;
}
- trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf = NULL;
- trak->out[NGX_HTTP_MP4_CTTS_DATA].buf = NULL;
+ if (start) {
+ data->pos = (u_char *) end;
+ trak->composition_offset_entries = 0;
+ }
return;
found:
- data->pos = (u_char *) entry;
- atom_size = sizeof(ngx_mp4_ctts_atom_t) + (data->last - data->pos);
- trak->size += atom_size;
+ if (start) {
+ ngx_mp4_set_32value(entry->count, count - rest);
+ data->pos = (u_char *) entry;
+ trak->composition_offset_entries = entries;
- atom = trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf;
- ctts_atom = (ngx_mp4_ctts_atom_t *) atom->pos;
-
- ngx_mp4_set_32value(ctts_atom->size, atom_size);
- ngx_mp4_set_32value(ctts_atom->entries, entries);
-
- return;
+ } else {
+ ngx_mp4_set_32value(entry->count, rest);
+ data->last = (u_char *) (entry + 1);
+ trak->composition_offset_entries -= entries - 1;
+ }
}
@@ -2428,11 +2662,10 @@ ngx_http_mp4_update_stsc_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak)
{
size_t atom_size;
- uint32_t start_sample, entries, chunk, samples, id,
- next_chunk, n;
- ngx_buf_t *atom, *data, *buf;
+ uint32_t chunk;
+ ngx_buf_t *atom, *data;
ngx_mp4_stsc_atom_t *stsc_atom;
- ngx_mp4_stsc_entry_t *entry, *first, *end;
+ ngx_mp4_stsc_entry_t *entry, *end;
/*
* mdia.minf.stbl.stsc updating requires trak->start_sample
@@ -2459,15 +2692,97 @@ ngx_http_mp4_update_stsc_atom(ngx_http_mp4_file_t *mp4,
return NGX_ERROR;
}
- start_sample = (uint32_t) trak->start_sample;
+ if (ngx_http_mp4_crop_stsc_data(mp4, trak, 1) != NGX_OK) {
+ return NGX_ERROR;
+ }
+
+ if (ngx_http_mp4_crop_stsc_data(mp4, trak, 0) != NGX_OK) {
+ return NGX_ERROR;
+ }
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "sample-to-chunk entries:%uD",
+ trak->sample_to_chunk_entries);
+
+ entry = (ngx_mp4_stsc_entry_t *) data->pos;
+ end = (ngx_mp4_stsc_entry_t *) data->last;
+
+ while (entry < end) {
+ chunk = ngx_mp4_get_32value(entry->chunk);
+ chunk -= trak->start_chunk;
+ ngx_mp4_set_32value(entry->chunk, chunk);
+ entry++;
+ }
+
+ atom_size = sizeof(ngx_mp4_stsc_atom_t)
+ + trak->sample_to_chunk_entries * sizeof(ngx_mp4_stsc_entry_t);
+
+ trak->size += atom_size;
+
+ atom = trak->out[NGX_HTTP_MP4_STSC_ATOM].buf;
+ stsc_atom = (ngx_mp4_stsc_atom_t *) atom->pos;
+
+ ngx_mp4_set_32value(stsc_atom->size, atom_size);
+ ngx_mp4_set_32value(stsc_atom->entries, trak->sample_to_chunk_entries);
+
+ return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_mp4_crop_stsc_data(ngx_http_mp4_file_t *mp4,
+ ngx_http_mp4_trak_t *trak, ngx_uint_t start)
+{
+ uint32_t start_sample, chunk, samples, id, next_chunk, n,
+ prev_samples;
+ ngx_buf_t *data, *buf;
+ ngx_uint_t entries, target_chunk, chunk_samples;
+ ngx_mp4_stsc_entry_t *entry, *end, *first;
+
entries = trak->sample_to_chunk_entries - 1;
+ if (start) {
+ start_sample = (uint32_t) trak->start_sample;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "mp4 stsc crop start_sample:%uD", start_sample);
+
+ } else if (mp4->length) {
+ start_sample = (uint32_t) (trak->end_sample - trak->start_sample);
+ samples = 0;
+
+ data = trak->out[NGX_HTTP_MP4_STSC_START].buf;
+
+ if (data) {
+ entry = (ngx_mp4_stsc_entry_t *) data->pos;
+ samples = ngx_mp4_get_32value(entry->samples);
+ entries--;
+
+ if (samples > start_sample) {
+ samples = start_sample;
+ ngx_mp4_set_32value(entry->samples, samples);
+ }
+
+ start_sample -= samples;
+ }
+
+ ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "mp4 stsc crop end_sample:%uD, ext_samples:%uD",
+ start_sample, samples);
+
+ } else {
+ return NGX_OK;
+ }
+
+ data = trak->out[NGX_HTTP_MP4_STSC_DATA].buf;
+
entry = (ngx_mp4_stsc_entry_t *) data->pos;
end = (ngx_mp4_stsc_entry_t *) data->last;
chunk = ngx_mp4_get_32value(entry->chunk);
samples = ngx_mp4_get_32value(entry->samples);
id = ngx_mp4_get_32value(entry->id);
+ prev_samples = 0;
entry++;
while (entry < end) {
@@ -2475,7 +2790,7 @@ ngx_http_mp4_update_stsc_atom(ngx_http_mp4_file_t *mp4,
next_chunk = ngx_mp4_get_32value(entry->chunk);
ngx_log_debug5(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
- "start_sample:%uD, chunk:%uD, chunks:%uD, "
+ "sample:%uD, chunk:%uD, chunks:%uD, "
"samples:%uD, id:%uD",
start_sample, chunk, next_chunk - chunk, samples, id);
@@ -2487,6 +2802,7 @@ ngx_http_mp4_update_stsc_atom(ngx_http_mp4_file_t *mp4,
start_sample -= n;
+ prev_samples = samples;
chunk = next_chunk;
samples = ngx_mp4_get_32value(entry->samples);
id = ngx_mp4_get_32value(entry->id);
@@ -2497,15 +2813,15 @@ ngx_http_mp4_update_stsc_atom(ngx_http_mp4_file_t *mp4,
next_chunk = trak->chunks + 1;
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
- "start_sample:%uD, chunk:%uD, chunks:%uD, samples:%uD",
+ "sample:%uD, chunk:%uD, chunks:%uD, samples:%uD",
start_sample, chunk, next_chunk - chunk, samples);
n = (next_chunk - chunk) * samples;
if (start_sample > n) {
ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
- "start time is out mp4 stsc chunks in \"%s\"",
- mp4->file.name.data);
+ "%s time is out mp4 stsc chunks in \"%s\"",
+ start ? "start" : "end", mp4->file.name.data);
return NGX_ERROR;
}
@@ -2521,59 +2837,91 @@ found:
return NGX_ERROR;
}
- trak->start_chunk = chunk - 1;
+ target_chunk = chunk - 1;
+ target_chunk += start_sample / samples;
+ chunk_samples = start_sample % samples;
- trak->start_chunk += start_sample / samples;
- trak->chunk_samples = start_sample % samples;
+ if (start) {
+ data->pos = (u_char *) entry;
- ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
- "start chunk:%ui, samples:%uD",
- trak->start_chunk, trak->chunk_samples);
+ trak->sample_to_chunk_entries = entries;
+ trak->start_chunk = target_chunk;
+ trak->start_chunk_samples = chunk_samples;
+
+ ngx_mp4_set_32value(entry->chunk, trak->start_chunk + 1);
+
+ samples -= chunk_samples;
+
+ ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "start_chunk:%ui, start_chunk_samples:%ui",
+ trak->start_chunk, trak->start_chunk_samples);
+
+ } else {
+ if (start_sample) {
+ data->last = (u_char *) (entry + 1);
+ trak->sample_to_chunk_entries -= entries - 1;
+ trak->end_chunk_samples = samples;
- data->pos = (u_char *) entry;
- atom_size = sizeof(ngx_mp4_stsc_atom_t) + (data->last - data->pos);
+ } else {
+ data->last = (u_char *) entry;
+ trak->sample_to_chunk_entries -= entries;
+ trak->end_chunk_samples = prev_samples;
+ }
- ngx_mp4_set_32value(entry->chunk, 1);
+ if (chunk_samples) {
+ trak->end_chunk = target_chunk + 1;
+ trak->end_chunk_samples = chunk_samples;
- if (trak->chunk_samples && next_chunk - trak->start_chunk == 2) {
+ } else {
+ trak->end_chunk = target_chunk;
+ }
- /* last chunk in the entry */
+ samples = chunk_samples;
+ next_chunk = chunk + 1;
- ngx_mp4_set_32value(entry->samples, samples - trak->chunk_samples);
+ ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "end_chunk:%ui, end_chunk_samples:%ui",
+ trak->end_chunk, trak->end_chunk_samples);
+ }
- } else if (trak->chunk_samples) {
+ if (chunk_samples && next_chunk - target_chunk == 2) {
- first = &trak->stsc_chunk_entry;
+ ngx_mp4_set_32value(entry->samples, samples);
+
+ } else if (chunk_samples && start) {
+
+ first = &trak->stsc_start_chunk_entry;
ngx_mp4_set_32value(first->chunk, 1);
- ngx_mp4_set_32value(first->samples, samples - trak->chunk_samples);
+ ngx_mp4_set_32value(first->samples, samples);
ngx_mp4_set_32value(first->id, id);
- buf = &trak->stsc_chunk_buf;
+ buf = &trak->stsc_start_chunk_buf;
buf->temporary = 1;
buf->pos = (u_char *) first;
buf->last = (u_char *) first + sizeof(ngx_mp4_stsc_entry_t);
- trak->out[NGX_HTTP_MP4_STSC_CHUNK].buf = buf;
+ trak->out[NGX_HTTP_MP4_STSC_START].buf = buf;
- ngx_mp4_set_32value(entry->chunk, 2);
+ ngx_mp4_set_32value(entry->chunk, trak->start_chunk + 2);
- entries++;
- atom_size += sizeof(ngx_mp4_stsc_entry_t);
- }
+ trak->sample_to_chunk_entries++;
- while (++entry < end) {
- chunk = ngx_mp4_get_32value(entry->chunk);
- chunk -= trak->start_chunk;
- ngx_mp4_set_32value(entry->chunk, chunk);
- }
+ } else if (chunk_samples) {
- trak->size += atom_size;
+ first = &trak->stsc_end_chunk_entry;
+ ngx_mp4_set_32value(first->chunk, trak->end_chunk - trak->start_chunk);
+ ngx_mp4_set_32value(first->samples, samples);
+ ngx_mp4_set_32value(first->id, id);
- atom = trak->out[NGX_HTTP_MP4_STSC_ATOM].buf;
- stsc_atom = (ngx_mp4_stsc_atom_t *) atom->pos;
+ buf = &trak->stsc_end_chunk_buf;
+ buf->temporary = 1;
+ buf->pos = (u_char *) first;
+ buf->last = (u_char *) first + sizeof(ngx_mp4_stsc_entry_t);
- ngx_mp4_set_32value(stsc_atom->size, atom_size);
- ngx_mp4_set_32value(stsc_atom->entries, entries);
+ trak->out[NGX_HTTP_MP4_STSC_END].buf = buf;
+
+ trak->sample_to_chunk_entries++;
+ }
return NGX_OK;
}
@@ -2669,7 +3017,7 @@ ngx_http_mp4_update_stsz_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak)
{
size_t atom_size;
- uint32_t *pos, *end;
+ uint32_t *pos, *end, entries;
ngx_buf_t *atom, *data;
ngx_mp4_stsz_atom_t *stsz_atom;
@@ -2685,22 +3033,47 @@ ngx_http_mp4_update_stsz_atom(ngx_http_mp4_file_t *mp4,
data = trak->out[NGX_HTTP_MP4_STSZ_DATA].buf;
if (data) {
- if (trak->start_sample > trak->sample_sizes_entries) {
+ entries = trak->sample_sizes_entries;
+
+ if (trak->start_sample > entries) {
ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
"start time is out mp4 stsz samples in \"%s\"",
mp4->file.name.data);
return NGX_ERROR;
}
+ entries -= trak->start_sample;
data->pos += trak->start_sample * sizeof(uint32_t);
end = (uint32_t *) data->pos;
- for (pos = end - trak->chunk_samples; pos < end; pos++) {
- trak->chunk_samples_size += ngx_mp4_get_32value(pos);
+ for (pos = end - trak->start_chunk_samples; pos < end; pos++) {
+ trak->start_chunk_samples_size += ngx_mp4_get_32value(pos);
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
- "chunk samples sizes:%uL", trak->chunk_samples_size);
+ "chunk samples sizes:%uL",
+ trak->start_chunk_samples_size);
+
+ if (mp4->length) {
+ if (trak->end_sample - trak->start_sample > entries) {
+ ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
+ "end time is out mp4 stsz samples in \"%s\"",
+ mp4->file.name.data);
+ return NGX_ERROR;
+ }
+
+ entries = trak->end_sample - trak->start_sample;
+ data->last = data->pos + entries * sizeof(uint32_t);
+ end = (uint32_t *) data->last;
+
+ for (pos = end - trak->end_chunk_samples; pos < end; pos++) {
+ trak->end_chunk_samples_size += ngx_mp4_get_32value(pos);
+ }
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "mp4 stsz end_chunk_samples_size:%uL",
+ trak->end_chunk_samples_size);
+ }
atom_size = sizeof(ngx_mp4_stsz_atom_t) + (data->last - data->pos);
trak->size += atom_size;
@@ -2709,8 +3082,7 @@ ngx_http_mp4_update_stsz_atom(ngx_http_mp4_file_t *mp4,
stsz_atom = (ngx_mp4_stsz_atom_t *) atom->pos;
ngx_mp4_set_32value(stsz_atom->size, atom_size);
- ngx_mp4_set_32value(stsz_atom->entries,
- trak->sample_sizes_entries - trak->start_sample);
+ ngx_mp4_set_32value(stsz_atom->entries, entries);
}
return NGX_OK;
@@ -2791,6 +3163,7 @@ ngx_http_mp4_update_stco_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak)
{
size_t atom_size;
+ uint32_t entries;
ngx_buf_t *atom, *data;
ngx_mp4_stco_atom_t *stco_atom;
@@ -2820,21 +3193,53 @@ ngx_http_mp4_update_stco_atom(ngx_http_mp4_file_t *mp4,
}
data->pos += trak->start_chunk * sizeof(uint32_t);
- atom_size = sizeof(ngx_mp4_stco_atom_t) + (data->last - data->pos);
- trak->size += atom_size;
trak->start_offset = ngx_mp4_get_32value(data->pos);
- trak->start_offset += trak->chunk_samples_size;
+ trak->start_offset += trak->start_chunk_samples_size;
ngx_mp4_set_32value(data->pos, trak->start_offset);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
- "start chunk offset:%uD", trak->start_offset);
+ "start chunk offset:%O", trak->start_offset);
+
+ if (mp4->length) {
+
+ if (trak->end_chunk > trak->chunks) {
+ ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
+ "end time is out mp4 stco chunks in \"%s\"",
+ mp4->file.name.data);
+ return NGX_ERROR;
+ }
+
+ entries = trak->end_chunk - trak->start_chunk;
+ data->last = data->pos + entries * sizeof(uint32_t);
+
+ if (entries) {
+ trak->end_offset =
+ ngx_mp4_get_32value(data->last - sizeof(uint32_t));
+ trak->end_offset += trak->end_chunk_samples_size;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "end chunk offset:%O", trak->end_offset);
+ }
+
+ } else {
+ entries = trak->chunks - trak->start_chunk;
+ trak->end_offset = mp4->mdat_data.buf->file_last;
+ }
+
+ if (entries == 0) {
+ trak->start_offset = mp4->end;
+ trak->end_offset = 0;
+ }
+
+ atom_size = sizeof(ngx_mp4_stco_atom_t) + (data->last - data->pos);
+ trak->size += atom_size;
atom = trak->out[NGX_HTTP_MP4_STCO_ATOM].buf;
stco_atom = (ngx_mp4_stco_atom_t *) atom->pos;
ngx_mp4_set_32value(stco_atom->size, atom_size);
- ngx_mp4_set_32value(stco_atom->entries, trak->chunks - trak->start_chunk);
+ ngx_mp4_set_32value(stco_atom->entries, entries);
return NGX_OK;
}
@@ -2942,6 +3347,7 @@ ngx_http_mp4_update_co64_atom(ngx_http_mp4_file_t *mp4,
ngx_http_mp4_trak_t *trak)
{
size_t atom_size;
+ uint64_t entries;
ngx_buf_t *atom, *data;
ngx_mp4_co64_atom_t *co64_atom;
@@ -2971,21 +3377,53 @@ ngx_http_mp4_update_co64_atom(ngx_http_mp4_file_t *mp4,
}
data->pos += trak->start_chunk * sizeof(uint64_t);
- atom_size = sizeof(ngx_mp4_co64_atom_t) + (data->last - data->pos);
- trak->size += atom_size;
trak->start_offset = ngx_mp4_get_64value(data->pos);
- trak->start_offset += trak->chunk_samples_size;
+ trak->start_offset += trak->start_chunk_samples_size;
ngx_mp4_set_64value(data->pos, trak->start_offset);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
- "start chunk offset:%uL", trak->start_offset);
+ "start chunk offset:%O", trak->start_offset);
+
+ if (mp4->length) {
+
+ if (trak->end_chunk > trak->chunks) {
+ ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
+ "end time is out mp4 co64 chunks in \"%s\"",
+ mp4->file.name.data);
+ return NGX_ERROR;
+ }
+
+ entries = trak->end_chunk - trak->start_chunk;
+ data->last = data->pos + entries * sizeof(uint64_t);
+
+ if (entries) {
+ trak->end_offset =
+ ngx_mp4_get_64value(data->last - sizeof(uint64_t));
+ trak->end_offset += trak->end_chunk_samples_size;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
+ "end chunk offset:%O", trak->end_offset);
+ }
+
+ } else {
+ entries = trak->chunks - trak->start_chunk;
+ trak->end_offset = mp4->mdat_data.buf->file_last;
+ }
+
+ if (entries == 0) {
+ trak->start_offset = mp4->end;
+ trak->end_offset = 0;
+ }
+
+ atom_size = sizeof(ngx_mp4_co64_atom_t) + (data->last - data->pos);
+ trak->size += atom_size;
atom = trak->out[NGX_HTTP_MP4_CO64_ATOM].buf;
co64_atom = (ngx_mp4_co64_atom_t *) atom->pos;
ngx_mp4_set_32value(co64_atom->size, atom_size);
- ngx_mp4_set_32value(co64_atom->entries, trak->chunks - trak->start_chunk);
+ ngx_mp4_set_32value(co64_atom->entries, entries);
return NGX_OK;
}