4085
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6 #include <ngx_config.h>
|
|
7 #include <ngx_core.h>
|
|
8 #include <ngx_http.h>
|
|
9
|
|
10
|
|
11 #define NGX_HTTP_MP4_TRAK_ATOM 0
|
|
12 #define NGX_HTTP_MP4_TKHD_ATOM 1
|
|
13 #define NGX_HTTP_MP4_MDIA_ATOM 2
|
|
14 #define NGX_HTTP_MP4_MDHD_ATOM 3
|
|
15 #define NGX_HTTP_MP4_HDLR_ATOM 4
|
|
16 #define NGX_HTTP_MP4_MINF_ATOM 5
|
|
17 #define NGX_HTTP_MP4_VMHD_ATOM 6
|
|
18 #define NGX_HTTP_MP4_SMHD_ATOM 7
|
|
19 #define NGX_HTTP_MP4_DINF_ATOM 8
|
|
20 #define NGX_HTTP_MP4_STBL_ATOM 9
|
|
21 #define NGX_HTTP_MP4_STSD_ATOM 10
|
|
22 #define NGX_HTTP_MP4_STTS_ATOM 11
|
|
23 #define NGX_HTTP_MP4_STTS_DATA 12
|
|
24 #define NGX_HTTP_MP4_STSS_ATOM 13
|
|
25 #define NGX_HTTP_MP4_STSS_DATA 14
|
|
26 #define NGX_HTTP_MP4_CTTS_ATOM 15
|
|
27 #define NGX_HTTP_MP4_CTTS_DATA 16
|
|
28 #define NGX_HTTP_MP4_STSC_ATOM 17
|
|
29 #define NGX_HTTP_MP4_STSC_DATA 18
|
|
30 #define NGX_HTTP_MP4_STSZ_ATOM 19
|
|
31 #define NGX_HTTP_MP4_STSZ_DATA 20
|
|
32 #define NGX_HTTP_MP4_STCO_ATOM 21
|
|
33 #define NGX_HTTP_MP4_STCO_DATA 22
|
|
34
|
|
35 #define NGX_HTTP_MP4_LAST_ATOM NGX_HTTP_MP4_STCO_DATA
|
|
36
|
|
37
|
|
38 typedef struct {
|
|
39 size_t buffer_size;
|
|
40 size_t max_moov_size;
|
|
41 } ngx_http_mp4_conf_t;
|
|
42
|
|
43
|
|
44 typedef struct {
|
|
45 uint32_t timescale;
|
|
46 uint32_t time_to_sample_entries;
|
|
47 uint32_t sample_to_chunk_entries;
|
|
48 uint32_t sync_samples_entries;
|
|
49 uint32_t composition_offset_entries;
|
|
50 uint32_t sample_sizes_entries;
|
|
51 uint32_t chunks;
|
|
52
|
|
53 ngx_uint_t start_sample;
|
|
54 ngx_uint_t start_chunk;
|
|
55 off_t start_offset;
|
|
56
|
|
57 size_t tkhd_size;
|
|
58 size_t mdhd_size;
|
|
59 size_t hdlr_size;
|
|
60 size_t vmhd_size;
|
|
61 size_t smhd_size;
|
|
62 size_t dinf_size;
|
|
63 size_t size;
|
|
64
|
|
65 ngx_chain_t out[NGX_HTTP_MP4_LAST_ATOM + 1];
|
|
66
|
|
67 ngx_buf_t trak_atom_buf;
|
|
68 ngx_buf_t tkhd_atom_buf;
|
|
69 ngx_buf_t mdia_atom_buf;
|
|
70 ngx_buf_t mdhd_atom_buf;
|
|
71 ngx_buf_t hdlr_atom_buf;
|
|
72 ngx_buf_t minf_atom_buf;
|
|
73 ngx_buf_t vmhd_atom_buf;
|
|
74 ngx_buf_t smhd_atom_buf;
|
|
75 ngx_buf_t dinf_atom_buf;
|
|
76 ngx_buf_t stbl_atom_buf;
|
|
77 ngx_buf_t stsd_atom_buf;
|
|
78 ngx_buf_t stts_atom_buf;
|
|
79 ngx_buf_t stts_data_buf;
|
|
80 ngx_buf_t stss_atom_buf;
|
|
81 ngx_buf_t stss_data_buf;
|
|
82 ngx_buf_t ctts_atom_buf;
|
|
83 ngx_buf_t ctts_data_buf;
|
|
84 ngx_buf_t stsc_atom_buf;
|
|
85 ngx_buf_t stsc_data_buf;
|
|
86 ngx_buf_t stsz_atom_buf;
|
|
87 ngx_buf_t stsz_data_buf;
|
|
88 ngx_buf_t tsco_atom_buf;
|
|
89 ngx_buf_t tsco_data_buf;
|
|
90 } ngx_http_mp4_trak_t;
|
|
91
|
|
92
|
|
93 typedef struct {
|
|
94 ngx_file_t file;
|
|
95
|
|
96 u_char *buffer;
|
|
97 u_char *buffer_start;
|
|
98 u_char *buffer_pos;
|
|
99 u_char *buffer_end;
|
|
100 size_t buffer_size;
|
|
101
|
|
102 off_t offset;
|
|
103 off_t end;
|
|
104 off_t content_length;
|
|
105 ngx_uint_t start;
|
|
106 uint32_t timescale;
|
|
107 ngx_http_request_t *request;
|
|
108 ngx_array_t trak;
|
|
109 ngx_http_mp4_trak_t traks[2];
|
|
110
|
|
111 size_t ftyp_size;
|
|
112 size_t moov_size;
|
|
113
|
|
114 ngx_chain_t *out;
|
|
115 ngx_chain_t ftyp_atom;
|
|
116 ngx_chain_t moov_atom;
|
|
117 ngx_chain_t mvhd_atom;
|
|
118 ngx_chain_t mdat_atom;
|
|
119 ngx_chain_t mdat_data;
|
|
120
|
|
121 ngx_buf_t ftyp_atom_buf;
|
|
122 ngx_buf_t moov_atom_buf;
|
|
123 ngx_buf_t mvhd_atom_buf;
|
|
124 ngx_buf_t mdat_atom_buf;
|
|
125 ngx_buf_t mdat_data_buf;
|
|
126
|
|
127 u_char moov_atom_header[8];
|
|
128 u_char mdat_atom_header[16];
|
|
129 } ngx_http_mp4_file_t;
|
|
130
|
|
131
|
|
132 typedef struct {
|
|
133 char *name;
|
|
134 ngx_int_t (*handler)(ngx_http_mp4_file_t *mp4,
|
|
135 uint64_t atom_data_size);
|
|
136 } ngx_http_mp4_atom_handler_t;
|
|
137
|
|
138
|
|
139 #define ngx_mp4_atom_header(mp4) (mp4->buffer_pos - 8)
|
|
140 #define ngx_mp4_atom_data(mp4) mp4->buffer_pos
|
|
141 #define ngx_mp4_atom_next(mp4, n) mp4->buffer_pos += n; mp4->offset += n
|
|
142
|
|
143
|
|
144 #define ngx_mp4_set_atom_name(p, n1, n2, n3, n4) \
|
|
145 ((u_char *) (p))[4] = n1; \
|
|
146 ((u_char *) (p))[5] = n2; \
|
|
147 ((u_char *) (p))[6] = n3; \
|
|
148 ((u_char *) (p))[7] = n4
|
|
149
|
|
150 #define ngx_mp4_get_32value(p) \
|
|
151 ( (((u_char *) (p))[0] << 24) \
|
|
152 + (((u_char *) (p))[1] << 16) \
|
|
153 + (((u_char *) (p))[2] << 8) \
|
|
154 + (((u_char *) (p))[3]) )
|
|
155
|
|
156 #define ngx_mp4_set_32value(p, n) \
|
|
157 ((u_char *) (p))[0] = (u_char) ((n) >> 24); \
|
|
158 ((u_char *) (p))[1] = (u_char) ((n) >> 16); \
|
|
159 ((u_char *) (p))[2] = (u_char) ((n) >> 8); \
|
|
160 ((u_char *) (p))[3] = (u_char) (n)
|
|
161
|
|
162 #define ngx_mp4_get_64value(p) \
|
|
163 ( ((uint64_t) ((u_char *) (p))[0] << 56) \
|
|
164 + ((uint64_t) ((u_char *) (p))[1] << 48) \
|
|
165 + ((uint64_t) ((u_char *) (p))[2] << 40) \
|
|
166 + ((uint64_t) ((u_char *) (p))[3] << 32) \
|
|
167 + ((uint64_t) ((u_char *) (p))[4] << 24) \
|
|
168 + ( ((u_char *) (p))[5] << 16) \
|
|
169 + ( ((u_char *) (p))[6] << 8) \
|
|
170 + ( ((u_char *) (p))[7]) )
|
|
171
|
|
172 #define ngx_mp4_set_64value(p, n) \
|
|
173 ((u_char *) (p))[0] = (u_char) ((n) >> 56); \
|
|
174 ((u_char *) (p))[1] = (u_char) ((n) >> 48); \
|
|
175 ((u_char *) (p))[2] = (u_char) ((n) >> 40); \
|
|
176 ((u_char *) (p))[3] = (u_char) ((n) >> 32); \
|
|
177 ((u_char *) (p))[4] = (u_char) ((n) >> 24); \
|
|
178 ((u_char *) (p))[5] = (u_char) ((n) >> 16); \
|
|
179 ((u_char *) (p))[6] = (u_char) ((n) >> 8); \
|
|
180 ((u_char *) (p))[7] = (u_char) (n)
|
|
181
|
|
182 #define ngx_mp4_last_trak(mp4) \
|
|
183 &((ngx_http_mp4_trak_t *) mp4->trak.elts)[mp4->trak.nelts - 1]
|
|
184
|
|
185
|
|
186 static ngx_int_t ngx_http_mp4_process(ngx_http_mp4_file_t *mp4);
|
|
187 static ngx_int_t ngx_http_mp4_read_atom(ngx_http_mp4_file_t *mp4,
|
|
188 ngx_http_mp4_atom_handler_t *atom, uint64_t atom_data_size);
|
|
189 static ngx_int_t ngx_http_mp4_read(ngx_http_mp4_file_t *mp4);
|
|
190 static ngx_int_t ngx_http_mp4_read_ftyp_atom(ngx_http_mp4_file_t *mp4,
|
|
191 uint64_t atom_data_size);
|
|
192 static ngx_int_t ngx_http_mp4_read_moov_atom(ngx_http_mp4_file_t *mp4,
|
|
193 uint64_t atom_data_size);
|
|
194 static ngx_int_t ngx_http_mp4_read_mdat_atom(ngx_http_mp4_file_t *mp4,
|
|
195 uint64_t atom_data_size);
|
|
196 static size_t ngx_http_mp4_update_mdat_atom(ngx_http_mp4_file_t *mp4,
|
|
197 off_t start_offset);
|
|
198 static ngx_int_t ngx_http_mp4_read_mvhd_atom(ngx_http_mp4_file_t *mp4,
|
|
199 uint64_t atom_data_size);
|
|
200 static ngx_int_t ngx_http_mp4_read_trak_atom(ngx_http_mp4_file_t *mp4,
|
|
201 uint64_t atom_data_size);
|
|
202 static void ngx_http_mp4_update_trak_atom(ngx_http_mp4_file_t *mp4,
|
|
203 ngx_http_mp4_trak_t *trak);
|
|
204 static ngx_int_t ngx_http_mp4_read_cmov_atom(ngx_http_mp4_file_t *mp4,
|
|
205 uint64_t atom_data_size);
|
|
206 static ngx_int_t ngx_http_mp4_read_tkhd_atom(ngx_http_mp4_file_t *mp4,
|
|
207 uint64_t atom_data_size);
|
|
208 static ngx_int_t ngx_http_mp4_read_mdia_atom(ngx_http_mp4_file_t *mp4,
|
|
209 uint64_t atom_data_size);
|
|
210 static void ngx_http_mp4_update_mdia_atom(ngx_http_mp4_file_t *mp4,
|
|
211 ngx_http_mp4_trak_t *trak);
|
|
212 static ngx_int_t ngx_http_mp4_read_mdhd_atom(ngx_http_mp4_file_t *mp4,
|
|
213 uint64_t atom_data_size);
|
|
214 static ngx_int_t ngx_http_mp4_read_hdlr_atom(ngx_http_mp4_file_t *mp4,
|
|
215 uint64_t atom_data_size);
|
|
216 static ngx_int_t ngx_http_mp4_read_minf_atom(ngx_http_mp4_file_t *mp4,
|
|
217 uint64_t atom_data_size);
|
|
218 static void ngx_http_mp4_update_minf_atom(ngx_http_mp4_file_t *mp4,
|
|
219 ngx_http_mp4_trak_t *trak);
|
|
220 static ngx_int_t ngx_http_mp4_read_dinf_atom(ngx_http_mp4_file_t *mp4,
|
|
221 uint64_t atom_data_size);
|
|
222 static ngx_int_t ngx_http_mp4_read_vmhd_atom(ngx_http_mp4_file_t *mp4,
|
|
223 uint64_t atom_data_size);
|
|
224 static ngx_int_t ngx_http_mp4_read_smhd_atom(ngx_http_mp4_file_t *mp4,
|
|
225 uint64_t atom_data_size);
|
|
226 static ngx_int_t ngx_http_mp4_read_stbl_atom(ngx_http_mp4_file_t *mp4,
|
|
227 uint64_t atom_data_size);
|
|
228 static void ngx_http_mp4_update_stbl_atom(ngx_http_mp4_file_t *mp4,
|
|
229 ngx_http_mp4_trak_t *trak);
|
|
230 static ngx_int_t ngx_http_mp4_read_stsd_atom(ngx_http_mp4_file_t *mp4,
|
|
231 uint64_t atom_data_size);
|
|
232 static ngx_int_t ngx_http_mp4_read_stts_atom(ngx_http_mp4_file_t *mp4,
|
|
233 uint64_t atom_data_size);
|
|
234 static ngx_int_t ngx_http_mp4_update_stts_atom(ngx_http_mp4_file_t *mp4,
|
|
235 ngx_http_mp4_trak_t *trak);
|
|
236 static ngx_int_t ngx_http_mp4_read_stss_atom(ngx_http_mp4_file_t *mp4,
|
|
237 uint64_t atom_data_size);
|
|
238 static ngx_int_t ngx_http_mp4_update_stss_atom(ngx_http_mp4_file_t *mp4,
|
|
239 ngx_http_mp4_trak_t *trak);
|
|
240 static ngx_int_t ngx_http_mp4_read_ctts_atom(ngx_http_mp4_file_t *mp4,
|
|
241 uint64_t atom_data_size);
|
|
242 static void ngx_http_mp4_update_ctts_atom(ngx_http_mp4_file_t *mp4,
|
|
243 ngx_http_mp4_trak_t *trak);
|
|
244 static ngx_int_t ngx_http_mp4_read_stsc_atom(ngx_http_mp4_file_t *mp4,
|
|
245 uint64_t atom_data_size);
|
|
246 static ngx_int_t ngx_http_mp4_update_stsc_atom(ngx_http_mp4_file_t *mp4,
|
|
247 ngx_http_mp4_trak_t *trak);
|
|
248 static ngx_int_t ngx_http_mp4_read_stsz_atom(ngx_http_mp4_file_t *mp4,
|
|
249 uint64_t atom_data_size);
|
|
250 static void ngx_http_mp4_update_stsz_atom(ngx_http_mp4_file_t *mp4,
|
|
251 ngx_http_mp4_trak_t *trak);
|
|
252 static ngx_int_t ngx_http_mp4_read_stco_atom(ngx_http_mp4_file_t *mp4,
|
|
253 uint64_t atom_data_size);
|
|
254 static ngx_int_t ngx_http_mp4_update_stco_atom(ngx_http_mp4_file_t *mp4,
|
|
255 ngx_http_mp4_trak_t *trak);
|
|
256 static void ngx_http_mp4_adjust_stco_atom(ngx_http_mp4_file_t *mp4,
|
|
257 ngx_http_mp4_trak_t *trak, int32_t adjustment);
|
|
258 static char *ngx_http_mp4(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
|
259 static void *ngx_http_mp4_create_conf(ngx_conf_t *cf);
|
|
260 static char *ngx_http_mp4_merge_conf(ngx_conf_t *cf, void *parent, void *child);
|
|
261
|
|
262 static ngx_command_t ngx_http_mp4_commands[] = {
|
|
263
|
|
264 { ngx_string("mp4"),
|
|
265 NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
|
|
266 ngx_http_mp4,
|
|
267 0,
|
|
268 0,
|
|
269 NULL },
|
|
270
|
|
271 { ngx_string("mp4_buffer_size"),
|
|
272 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
273 ngx_conf_set_size_slot,
|
|
274 NGX_HTTP_LOC_CONF_OFFSET,
|
|
275 offsetof(ngx_http_mp4_conf_t, buffer_size),
|
|
276 NULL },
|
|
277
|
|
278 { ngx_string("mp4_max_moov_size"),
|
|
279 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
280 ngx_conf_set_size_slot,
|
|
281 NGX_HTTP_LOC_CONF_OFFSET,
|
|
282 offsetof(ngx_http_mp4_conf_t, max_moov_size),
|
|
283 NULL },
|
|
284
|
|
285 ngx_null_command
|
|
286 };
|
|
287
|
|
288
|
|
289 static ngx_http_module_t ngx_http_mp4_module_ctx = {
|
|
290 NULL, /* preconfiguration */
|
|
291 NULL, /* postconfiguration */
|
|
292
|
|
293 NULL, /* create main configuration */
|
|
294 NULL, /* init main configuration */
|
|
295
|
|
296 NULL, /* create server configuration */
|
|
297 NULL, /* merge server configuration */
|
|
298
|
|
299 ngx_http_mp4_create_conf, /* create location configuration */
|
|
300 ngx_http_mp4_merge_conf /* merge location configuration */
|
|
301 };
|
|
302
|
|
303
|
|
304 ngx_module_t ngx_http_mp4_module = {
|
|
305 NGX_MODULE_V1,
|
|
306 &ngx_http_mp4_module_ctx, /* module context */
|
|
307 ngx_http_mp4_commands, /* module directives */
|
|
308 NGX_HTTP_MODULE, /* module type */
|
|
309 NULL, /* init master */
|
|
310 NULL, /* init module */
|
|
311 NULL, /* init process */
|
|
312 NULL, /* init thread */
|
|
313 NULL, /* exit thread */
|
|
314 NULL, /* exit process */
|
|
315 NULL, /* exit master */
|
|
316 NGX_MODULE_V1_PADDING
|
|
317 };
|
|
318
|
|
319
|
|
320 static ngx_http_mp4_atom_handler_t ngx_http_mp4_atoms[] = {
|
|
321 { "ftyp", ngx_http_mp4_read_ftyp_atom },
|
|
322 { "moov", ngx_http_mp4_read_moov_atom },
|
|
323 { "mdat", ngx_http_mp4_read_mdat_atom },
|
|
324 { NULL, NULL }
|
|
325 };
|
|
326
|
|
327 static ngx_http_mp4_atom_handler_t ngx_http_mp4_moov_atoms[] = {
|
|
328 { "mvhd", ngx_http_mp4_read_mvhd_atom },
|
|
329 { "trak", ngx_http_mp4_read_trak_atom },
|
|
330 { "cmov", ngx_http_mp4_read_cmov_atom },
|
|
331 { NULL, NULL }
|
|
332 };
|
|
333
|
|
334 static ngx_http_mp4_atom_handler_t ngx_http_mp4_trak_atoms[] = {
|
|
335 { "tkhd", ngx_http_mp4_read_tkhd_atom },
|
|
336 { "mdia", ngx_http_mp4_read_mdia_atom },
|
|
337 { NULL, NULL }
|
|
338 };
|
|
339
|
|
340 static ngx_http_mp4_atom_handler_t ngx_http_mp4_mdia_atoms[] = {
|
|
341 { "mdhd", ngx_http_mp4_read_mdhd_atom },
|
|
342 { "hdlr", ngx_http_mp4_read_hdlr_atom },
|
|
343 { "minf", ngx_http_mp4_read_minf_atom },
|
|
344 { NULL, NULL }
|
|
345 };
|
|
346
|
|
347 static ngx_http_mp4_atom_handler_t ngx_http_mp4_minf_atoms[] = {
|
|
348 { "vmhd", ngx_http_mp4_read_vmhd_atom },
|
|
349 { "smhd", ngx_http_mp4_read_smhd_atom },
|
|
350 { "dinf", ngx_http_mp4_read_dinf_atom },
|
|
351 { "stbl", ngx_http_mp4_read_stbl_atom },
|
|
352 { NULL, NULL }
|
|
353 };
|
|
354
|
|
355 static ngx_http_mp4_atom_handler_t ngx_http_mp4_stbl_atoms[] = {
|
|
356 { "stsd", ngx_http_mp4_read_stsd_atom },
|
|
357 { "stts", ngx_http_mp4_read_stts_atom },
|
|
358 { "stss", ngx_http_mp4_read_stss_atom },
|
|
359 { "ctts", ngx_http_mp4_read_ctts_atom },
|
|
360 { "stsc", ngx_http_mp4_read_stsc_atom },
|
|
361 { "stsz", ngx_http_mp4_read_stsz_atom },
|
|
362 { "stco", ngx_http_mp4_read_stco_atom },
|
|
363 { NULL, NULL }
|
|
364 };
|
|
365
|
|
366
|
|
367 static ngx_int_t
|
|
368 ngx_http_mp4_handler(ngx_http_request_t *r)
|
|
369 {
|
|
370 u_char *last;
|
|
371 size_t root;
|
|
372 ngx_int_t rc, start;
|
|
373 ngx_uint_t level;
|
|
374 ngx_str_t path, value;
|
|
375 ngx_log_t *log;
|
|
376 ngx_buf_t *b;
|
|
377 ngx_chain_t out;
|
|
378 ngx_http_mp4_file_t *mp4;
|
|
379 ngx_open_file_info_t of;
|
|
380 ngx_http_core_loc_conf_t *clcf;
|
|
381
|
|
382 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
|
|
383 return NGX_HTTP_NOT_ALLOWED;
|
|
384 }
|
|
385
|
|
386 if (r->uri.data[r->uri.len - 1] == '/') {
|
|
387 return NGX_DECLINED;
|
|
388 }
|
|
389
|
|
390 rc = ngx_http_discard_request_body(r);
|
|
391
|
|
392 if (rc != NGX_OK) {
|
|
393 return rc;
|
|
394 }
|
|
395
|
|
396 last = ngx_http_map_uri_to_path(r, &path, &root, 0);
|
|
397 if (last == NULL) {
|
|
398 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
399 }
|
|
400
|
|
401 log = r->connection->log;
|
|
402
|
|
403 path.len = last - path.data;
|
|
404
|
|
405 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
|
|
406 "http mp4 filename: \"%V\"", &path);
|
|
407
|
|
408 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
409
|
|
410 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
|
|
411
|
|
412 of.read_ahead = clcf->read_ahead;
|
|
413 of.valid = clcf->open_file_cache_valid;
|
|
414 of.min_uses = clcf->open_file_cache_min_uses;
|
|
415 of.errors = clcf->open_file_cache_errors;
|
|
416 of.events = clcf->open_file_cache_events;
|
|
417
|
|
418 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
|
|
419 != NGX_OK)
|
|
420 {
|
|
421 switch (of.err) {
|
|
422
|
|
423 case 0:
|
|
424 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
425
|
|
426 case NGX_ENOENT:
|
|
427 case NGX_ENOTDIR:
|
|
428 case NGX_ENAMETOOLONG:
|
|
429
|
|
430 level = NGX_LOG_ERR;
|
|
431 rc = NGX_HTTP_NOT_FOUND;
|
|
432 break;
|
|
433
|
|
434 case NGX_EACCES:
|
|
435
|
|
436 level = NGX_LOG_ERR;
|
|
437 rc = NGX_HTTP_FORBIDDEN;
|
|
438 break;
|
|
439
|
|
440 default:
|
|
441
|
|
442 level = NGX_LOG_CRIT;
|
|
443 rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
444 break;
|
|
445 }
|
|
446
|
|
447 if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) {
|
|
448 ngx_log_error(level, log, of.err,
|
|
449 "%s \"%s\" failed", of.failed, path.data);
|
|
450 }
|
|
451
|
|
452 return rc;
|
|
453 }
|
|
454
|
|
455 if (!of.is_file) {
|
|
456
|
|
457 if (ngx_close_file(of.fd) == NGX_FILE_ERROR) {
|
|
458 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
459 ngx_close_file_n " \"%s\" failed", path.data);
|
|
460 }
|
|
461
|
|
462 return NGX_DECLINED;
|
|
463 }
|
|
464
|
|
465 r->root_tested = !r->error_page;
|
|
466 r->allow_ranges = 1;
|
|
467
|
|
468 start = -1;
|
|
469 r->headers_out.content_length_n = of.size;
|
|
470 mp4 = NULL;
|
|
471 b = NULL;
|
|
472
|
|
473 if (r->args.len) {
|
|
474
|
|
475 if (ngx_http_arg(r, (u_char *) "start", 5, &value) == NGX_OK) {
|
|
476
|
|
477 start = ngx_atofp(value.data, value.len, 3);
|
|
478
|
|
479 if (start != NGX_ERROR) {
|
|
480 r->allow_ranges = 0;
|
|
481
|
|
482 mp4 = ngx_pcalloc(r->pool, sizeof(ngx_http_mp4_file_t));
|
|
483 if (mp4 == NULL) {
|
|
484 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
485 }
|
|
486
|
|
487 mp4->file.fd = of.fd;
|
|
488 mp4->file.name = path;
|
|
489 mp4->file.log = r->connection->log;;
|
|
490 mp4->end = of.size;
|
|
491 mp4->start = (ngx_uint_t) start;
|
|
492 mp4->request = r;
|
|
493
|
|
494 switch (ngx_http_mp4_process(mp4)) {
|
|
495
|
|
496 case NGX_DECLINED:
|
|
497 if (mp4->buffer) {
|
|
498 ngx_pfree(r->pool, mp4->buffer);
|
|
499 }
|
|
500
|
|
501 ngx_pfree(r->pool, mp4);
|
|
502 mp4 = NULL;
|
|
503
|
|
504 break;
|
|
505
|
|
506 case NGX_OK:
|
|
507 r->headers_out.content_length_n = mp4->content_length;
|
|
508 break;
|
|
509
|
|
510 default: /* NGX_ERROR */
|
|
511 if (mp4->buffer) {
|
|
512 ngx_pfree(r->pool, mp4->buffer);
|
|
513 }
|
|
514
|
|
515 ngx_pfree(r->pool, mp4);
|
|
516
|
|
517 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
518 }
|
|
519 }
|
|
520 }
|
|
521 }
|
|
522
|
|
523 log->action = "sending mp4 to client";
|
|
524
|
|
525 if (clcf->directio <= of.size) {
|
|
526
|
|
527 /*
|
|
528 * DIRECTIO is set on transfer only
|
|
529 * to allow kernel to cache "moov" atom
|
|
530 */
|
|
531
|
|
532 if (ngx_directio_on(of.fd) == NGX_FILE_ERROR) {
|
|
533 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
534 ngx_directio_on_n " \"%s\" failed", path.data);
|
|
535 }
|
|
536
|
|
537 if (mp4) {
|
|
538 mp4->file.directio = 1;
|
|
539 }
|
|
540 }
|
|
541
|
|
542 r->headers_out.status = NGX_HTTP_OK;
|
|
543 r->headers_out.last_modified_time = of.mtime;
|
|
544
|
|
545 if (ngx_http_set_content_type(r) != NGX_OK) {
|
|
546 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
547 }
|
|
548
|
|
549 if (mp4 == NULL) {
|
|
550 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
|
|
551 if (b == NULL) {
|
|
552 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
553 }
|
|
554
|
|
555 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
|
|
556 if (b->file == NULL) {
|
|
557 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
558 }
|
|
559 }
|
|
560
|
|
561 rc = ngx_http_send_header(r);
|
|
562
|
|
563 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
|
|
564 return rc;
|
|
565 }
|
|
566
|
|
567 if (mp4) {
|
|
568 return ngx_http_output_filter(r, mp4->out);
|
|
569 }
|
|
570
|
|
571 b->file_pos = 0;
|
|
572 b->file_last = of.size;
|
|
573
|
|
574 b->in_file = b->file_last ? 1 : 0;
|
|
575 b->last_buf = 1;
|
|
576 b->last_in_chain = 1;
|
|
577
|
|
578 b->file->fd = of.fd;
|
|
579 b->file->name = path;
|
|
580 b->file->log = log;
|
|
581 b->file->directio = of.is_directio;
|
|
582
|
|
583 out.buf = b;
|
|
584 out.next = NULL;
|
|
585
|
|
586 return ngx_http_output_filter(r, &out);
|
|
587 }
|
|
588
|
|
589
|
|
590 static ngx_int_t
|
|
591 ngx_http_mp4_process(ngx_http_mp4_file_t *mp4)
|
|
592 {
|
|
593 off_t start_offset, adjustment;
|
|
594 ngx_int_t rc;
|
|
595 ngx_uint_t i, j;
|
|
596 ngx_chain_t **prev;
|
|
597 ngx_http_mp4_trak_t *trak;
|
|
598 ngx_http_mp4_conf_t *conf;
|
|
599
|
|
600 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
601 "mp4 start:%ui", mp4->start);
|
|
602
|
|
603 conf = ngx_http_get_module_loc_conf(mp4->request, ngx_http_mp4_module);
|
|
604
|
|
605 mp4->buffer_size = conf->buffer_size;
|
|
606
|
|
607 rc = ngx_http_mp4_read_atom(mp4, ngx_http_mp4_atoms, mp4->end);
|
|
608 if (rc != NGX_OK) {
|
|
609 return rc;
|
|
610 }
|
|
611
|
|
612 if (mp4->trak.nelts == 0) {
|
|
613 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
614 "no mp4 trak atoms were found in \"%s\"",
|
|
615 mp4->file.name.data);
|
|
616 return NGX_ERROR;
|
|
617 }
|
|
618
|
|
619 if (mp4->mdat_atom.buf == NULL) {
|
|
620 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
621 "no mp4 mdat atom was found in \"%s\"",
|
|
622 mp4->file.name.data);
|
|
623 return NGX_ERROR;
|
|
624 }
|
|
625
|
|
626 prev = &mp4->out;
|
|
627
|
|
628 if (mp4->ftyp_atom.buf) {
|
|
629 *prev = &mp4->ftyp_atom;
|
|
630 prev = &mp4->ftyp_atom.next;
|
|
631 }
|
|
632
|
|
633 *prev = &mp4->moov_atom;
|
|
634 prev = &mp4->moov_atom.next;
|
|
635
|
|
636 if (mp4->mvhd_atom.buf) {
|
|
637 mp4->moov_size += mp4->mvhd_atom_buf.last - mp4->mvhd_atom_buf.pos;
|
|
638 *prev = &mp4->mvhd_atom;
|
|
639 prev = &mp4->mvhd_atom.next;
|
|
640 }
|
|
641
|
|
642 start_offset = mp4->end;
|
|
643 trak = mp4->trak.elts;
|
|
644
|
|
645 for (i = 0; i < mp4->trak.nelts; i++) {
|
|
646
|
|
647 if (ngx_http_mp4_update_stts_atom(mp4, &trak[i]) != NGX_OK) {
|
|
648 return NGX_ERROR;
|
|
649 }
|
|
650
|
|
651 if (ngx_http_mp4_update_stss_atom(mp4, &trak[i]) != NGX_OK) {
|
|
652 return NGX_ERROR;
|
|
653 }
|
|
654
|
|
655 ngx_http_mp4_update_ctts_atom(mp4, &trak[i]);
|
|
656
|
|
657 if (ngx_http_mp4_update_stsc_atom(mp4, &trak[i]) != NGX_OK) {
|
|
658 return NGX_ERROR;
|
|
659 }
|
|
660
|
|
661 ngx_http_mp4_update_stsz_atom(mp4, &trak[i]);
|
|
662
|
|
663 if (ngx_http_mp4_update_stco_atom(mp4, &trak[i]) != NGX_OK) {
|
|
664 return NGX_ERROR;
|
|
665 }
|
|
666
|
|
667 ngx_http_mp4_update_stbl_atom(mp4, &trak[i]);
|
|
668 ngx_http_mp4_update_minf_atom(mp4, &trak[i]);
|
|
669 trak[i].size += trak[i].mdhd_size;
|
|
670 trak[i].size += trak[i].hdlr_size;
|
|
671 ngx_http_mp4_update_mdia_atom(mp4, &trak[i]);
|
|
672 trak[i].size += trak[i].tkhd_size;
|
|
673 ngx_http_mp4_update_trak_atom(mp4, &trak[i]);
|
|
674
|
|
675 mp4->moov_size += trak[i].size;
|
|
676
|
|
677 if (start_offset > trak[i].start_offset) {
|
|
678 start_offset = trak[i].start_offset;
|
|
679 }
|
|
680
|
|
681 *prev = &trak[i].out[NGX_HTTP_MP4_TRAK_ATOM];
|
|
682 prev = &trak[i].out[NGX_HTTP_MP4_TRAK_ATOM].next;
|
|
683
|
|
684 for (j = 0; j < NGX_HTTP_MP4_LAST_ATOM + 1; j++) {
|
|
685 if (trak[i].out[j].buf) {
|
|
686 *prev = &trak[i].out[j];
|
|
687 prev = &trak[i].out[j].next;
|
|
688 }
|
|
689 }
|
|
690 }
|
|
691
|
|
692 mp4->moov_size += 8;
|
|
693
|
|
694 ngx_mp4_set_32value(mp4->moov_atom_header, mp4->moov_size);
|
|
695 ngx_mp4_set_atom_name(mp4->moov_atom_header, 'm', 'o', 'o', 'v');
|
|
696 mp4->content_length += mp4->moov_size;
|
|
697
|
|
698 *prev = &mp4->mdat_atom;
|
|
699
|
|
700 adjustment = mp4->ftyp_size + mp4->moov_size
|
|
701 + ngx_http_mp4_update_mdat_atom(mp4, start_offset)
|
|
702 - start_offset;
|
|
703
|
|
704 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
705 "mp4 adjustment:%D", adjustment);
|
|
706
|
|
707 for (i = 0; i < mp4->trak.nelts; i++) {
|
|
708 ngx_http_mp4_adjust_stco_atom(mp4, &trak[i], (int32_t) adjustment);
|
|
709 }
|
|
710
|
|
711 return NGX_OK;
|
|
712 }
|
|
713
|
|
714
|
|
715 typedef struct {
|
|
716 u_char size[4];
|
|
717 u_char name[4];
|
|
718 } ngx_mp4_atom_header_t;
|
|
719
|
|
720 typedef struct {
|
|
721 u_char size[4];
|
|
722 u_char name[4];
|
|
723 u_char size64[8];
|
|
724 } ngx_mp4_atom_header64_t;
|
|
725
|
|
726
|
|
727 static ngx_int_t
|
|
728 ngx_http_mp4_read_atom(ngx_http_mp4_file_t *mp4,
|
|
729 ngx_http_mp4_atom_handler_t *atom, uint64_t atom_data_size)
|
|
730 {
|
|
731 off_t end;
|
|
732 size_t atom_header_size;
|
|
733 u_char *atom_header, *atom_name;
|
|
734 uint64_t atom_size;
|
|
735 ngx_int_t rc;
|
|
736 ngx_uint_t n;
|
|
737
|
|
738 end = mp4->offset + atom_data_size;
|
|
739
|
|
740 while (mp4->offset < end) {
|
|
741
|
|
742 if (mp4->buffer_pos + sizeof(uint32_t) > mp4->buffer_end) {
|
|
743 if (ngx_http_mp4_read(mp4) != NGX_OK) {
|
|
744 return NGX_ERROR;
|
|
745 }
|
|
746 }
|
|
747
|
|
748 atom_header = mp4->buffer_pos;
|
|
749 atom_size = ngx_mp4_get_32value(atom_header);
|
|
750 atom_header_size = sizeof(ngx_mp4_atom_header_t);
|
|
751
|
|
752 if (atom_size == 0) {
|
|
753 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
754 "mp4 atom end");
|
|
755 return NGX_OK;
|
|
756 }
|
|
757
|
|
758 if (atom_size < sizeof(ngx_mp4_atom_header_t)) {
|
|
759
|
|
760 if (atom_size == 1) {
|
|
761
|
|
762 if (mp4->buffer_pos + sizeof(ngx_mp4_atom_header64_t)
|
|
763 > mp4->buffer_end)
|
|
764 {
|
|
765 if (ngx_http_mp4_read(mp4) != NGX_OK) {
|
|
766 return NGX_ERROR;
|
|
767 }
|
|
768
|
|
769 atom_header = mp4->buffer_pos;
|
|
770 }
|
|
771
|
|
772 /* 64-bit atom size */
|
|
773 atom_size = ngx_mp4_get_64value(atom_header + 8);
|
|
774 atom_header_size = sizeof(ngx_mp4_atom_header64_t);
|
|
775
|
|
776 } else {
|
|
777 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
778 "\"%s\" mp4 atom is too small:%uL",
|
|
779 mp4->file.name.data, atom_size);
|
|
780 return NGX_ERROR;
|
|
781 }
|
|
782 }
|
|
783
|
|
784 if (mp4->buffer_pos + sizeof(ngx_mp4_atom_header_t) > mp4->buffer_end) {
|
|
785 if (ngx_http_mp4_read(mp4) != NGX_OK) {
|
|
786 return NGX_ERROR;
|
|
787 }
|
|
788
|
|
789 atom_header = mp4->buffer_pos;
|
|
790 }
|
|
791
|
|
792 atom_name = atom_header + sizeof(uint32_t);
|
|
793
|
|
794 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
795 "mp4 atom: %*s @%O:%uL",
|
|
796 4, atom_name, mp4->offset, atom_size);
|
|
797
|
|
798 for (n = 0; atom[n].name; n++) {
|
|
799
|
|
800 if (ngx_strncmp(atom_name, atom[n].name, 4) == 0) {
|
|
801
|
|
802 ngx_mp4_atom_next(mp4, atom_header_size);
|
|
803
|
|
804 rc = atom[n].handler(mp4, atom_size - atom_header_size);
|
|
805 if (rc != NGX_OK) {
|
|
806 return rc;
|
|
807 }
|
|
808
|
|
809 goto next;
|
|
810 }
|
|
811 }
|
|
812
|
|
813 ngx_mp4_atom_next(mp4, atom_size);
|
|
814
|
|
815 next:
|
|
816 continue;
|
|
817 }
|
|
818
|
|
819 return NGX_OK;
|
|
820 }
|
|
821
|
|
822
|
|
823 static ngx_int_t
|
|
824 ngx_http_mp4_read(ngx_http_mp4_file_t *mp4)
|
|
825 {
|
|
826 ngx_int_t n;
|
|
827
|
|
828 if (mp4->offset + (off_t) mp4->buffer_size > mp4->end) {
|
|
829 mp4->buffer_size = (size_t) (mp4->end - mp4->offset);
|
|
830 }
|
|
831
|
|
832 if (mp4->buffer == NULL) {
|
|
833 mp4->buffer = ngx_palloc(mp4->request->pool, mp4->buffer_size);
|
|
834 if (mp4->buffer == NULL) {
|
|
835 return NGX_ERROR;
|
|
836 }
|
|
837
|
|
838 mp4->buffer_start = mp4->buffer;
|
|
839 mp4->buffer_end = mp4->buffer + mp4->buffer_size;
|
|
840 }
|
|
841
|
|
842 n = ngx_read_file(&mp4->file, mp4->buffer_start, mp4->buffer_size,
|
|
843 mp4->offset);
|
|
844
|
|
845 if (n == NGX_ERROR) {
|
|
846 return NGX_ERROR;
|
|
847 }
|
|
848
|
|
849 if (n == 0) {
|
|
850 return NGX_OK;
|
|
851 }
|
|
852
|
|
853 mp4->buffer_pos = mp4->buffer_start;
|
|
854
|
|
855 return NGX_OK;
|
|
856 }
|
|
857
|
|
858
|
|
859 static ngx_int_t
|
|
860 ngx_http_mp4_read_ftyp_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
861 {
|
|
862 u_char *ftyp_atom;
|
|
863 size_t atom_size;
|
|
864 ngx_buf_t *atom;
|
|
865
|
|
866 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 ftyp atom");
|
|
867
|
|
868 if (atom_data_size > 1024) {
|
|
869 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
870 "\"%s\" mp4 ftyp atom is too large:%uL",
|
|
871 mp4->file.name.data, atom_data_size);
|
|
872 return NGX_ERROR;
|
|
873 }
|
|
874
|
|
875 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
|
|
876
|
|
877 ftyp_atom = ngx_palloc(mp4->request->pool, atom_size);
|
|
878 if (ftyp_atom == NULL) {
|
|
879 return NGX_ERROR;
|
|
880 }
|
|
881
|
|
882 ngx_mp4_set_32value(ftyp_atom, atom_size);
|
|
883 ngx_mp4_set_atom_name(ftyp_atom, 'f', 't', 'y', 'p');
|
|
884
|
|
885 /*
|
|
886 * only moov atom content is guaranteed to be in mp4->buffer
|
|
887 * during sending response, so ftyp atom content should be copied
|
|
888 */
|
|
889 ngx_memcpy(ftyp_atom + sizeof(ngx_mp4_atom_header_t),
|
|
890 ngx_mp4_atom_data(mp4), (size_t) atom_data_size);
|
|
891
|
|
892 atom = &mp4->ftyp_atom_buf;
|
|
893 atom->temporary = 1;
|
|
894 atom->pos = ftyp_atom;
|
|
895 atom->last = ftyp_atom + atom_size;
|
|
896
|
|
897 mp4->ftyp_atom.buf = atom;
|
|
898 mp4->ftyp_size = atom_size;
|
|
899 mp4->content_length = atom_size;
|
|
900
|
|
901 ngx_mp4_atom_next(mp4, atom_data_size);
|
|
902
|
|
903 return NGX_OK;
|
|
904 }
|
|
905
|
|
906
|
|
907 /*
|
|
908 * Small excess buffer to process atoms after moov atom, mp4->buffer_start
|
|
909 * will be set to this buffer part after moov atom processing.
|
|
910 */
|
|
911 #define NGX_HTTP_MP4_MOOV_BUFFER_EXCESS (4 * 1024)
|
|
912
|
|
913 static ngx_int_t
|
|
914 ngx_http_mp4_read_moov_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
915 {
|
|
916 ngx_int_t rc;
|
|
917 ngx_uint_t no_mdat;
|
|
918 ngx_buf_t *atom;
|
|
919 ngx_http_mp4_conf_t *conf;
|
|
920
|
|
921 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 moov atom");
|
|
922
|
|
923 no_mdat = (mp4->mdat_atom.buf == NULL);
|
|
924
|
|
925 if (no_mdat && mp4->start == 0) {
|
|
926 /*
|
|
927 * send original file if moov atom resides before
|
|
928 * mdat atom and client requests integral file
|
|
929 */
|
|
930 return NGX_DECLINED;
|
|
931 }
|
|
932
|
|
933 conf = ngx_http_get_module_loc_conf(mp4->request, ngx_http_mp4_module);
|
|
934
|
|
935 if (atom_data_size > mp4->buffer_size) {
|
|
936
|
|
937 if (atom_data_size > conf->max_moov_size) {
|
|
938 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
939 "\"%s\" mp4 moov atom is too large:%uL, "
|
|
940 "you may want to increase mp4_max_moov_size",
|
|
941 mp4->file.name.data, atom_data_size);
|
|
942 return NGX_ERROR;
|
|
943 }
|
|
944
|
|
945 ngx_pfree(mp4->request->pool, mp4->buffer);
|
|
946 mp4->buffer = NULL;
|
|
947 mp4->buffer_pos = NULL;
|
|
948 mp4->buffer_end = NULL;
|
|
949
|
|
950 mp4->buffer_size = (size_t) atom_data_size
|
|
951 + NGX_HTTP_MP4_MOOV_BUFFER_EXCESS * no_mdat;
|
|
952 }
|
|
953
|
|
954 mp4->trak.elts = &mp4->traks;
|
|
955 mp4->trak.size = sizeof(ngx_http_mp4_trak_t);
|
|
956 mp4->trak.nalloc = 2;
|
|
957 mp4->trak.pool = mp4->request->pool;
|
|
958
|
|
959 atom = &mp4->moov_atom_buf;
|
|
960 atom->temporary = 1;
|
|
961 atom->pos = mp4->moov_atom_header;
|
|
962 atom->last = mp4->moov_atom_header + 8;
|
|
963
|
|
964 mp4->moov_atom.buf = &mp4->moov_atom_buf;
|
|
965
|
|
966 rc = ngx_http_mp4_read_atom(mp4, ngx_http_mp4_moov_atoms, atom_data_size);
|
|
967
|
|
968 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 moov atom done");
|
|
969
|
|
970 if (no_mdat) {
|
|
971 mp4->buffer_start = mp4->buffer_pos;
|
|
972 mp4->buffer_size = NGX_HTTP_MP4_MOOV_BUFFER_EXCESS;
|
|
973
|
|
974 } else {
|
|
975 /* skip atoms after moov atom */
|
|
976 mp4->offset = mp4->end;
|
|
977 }
|
|
978
|
|
979 return rc;
|
|
980 }
|
|
981
|
|
982
|
|
983 static ngx_int_t
|
|
984 ngx_http_mp4_read_mdat_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
985 {
|
|
986 ngx_buf_t *data;
|
|
987
|
|
988 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 mdat atom");
|
|
989
|
|
990 data = &mp4->mdat_data_buf;
|
|
991 data->file = &mp4->file;
|
|
992 data->in_file = 1;
|
|
993 data->last_buf = 1;
|
|
994 data->last_in_chain = 1;
|
|
995 data->file_last = mp4->offset + atom_data_size;
|
|
996
|
|
997 mp4->mdat_atom.buf = &mp4->mdat_atom_buf;
|
|
998 mp4->mdat_atom.next = &mp4->mdat_data;
|
|
999 mp4->mdat_data.buf = data;
|
|
1000
|
|
1001 if (mp4->trak.nelts) {
|
|
1002 /* skip atoms after mdat atom */
|
|
1003 mp4->offset = mp4->end;
|
|
1004
|
|
1005 } else {
|
|
1006 ngx_mp4_atom_next(mp4, atom_data_size);
|
|
1007 }
|
|
1008
|
|
1009 return NGX_OK;
|
|
1010 }
|
|
1011
|
|
1012
|
|
1013 static size_t
|
|
1014 ngx_http_mp4_update_mdat_atom(ngx_http_mp4_file_t *mp4, off_t start_offset)
|
|
1015 {
|
|
1016 off_t atom_data_size;
|
|
1017 u_char *atom_header;
|
|
1018 uint32_t atom_header_size;
|
|
1019 uint64_t atom_size;
|
|
1020 ngx_buf_t *atom;
|
|
1021
|
|
1022 atom_data_size = mp4->mdat_data.buf->file_last - start_offset;
|
|
1023 mp4->mdat_data.buf->file_pos = start_offset;
|
|
1024 mp4->content_length += atom_data_size;
|
|
1025
|
|
1026 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
1027 "mdat new offset @%O:%O", start_offset, atom_data_size);
|
|
1028
|
|
1029 atom_header = mp4->mdat_atom_header;
|
|
1030
|
|
1031 if (atom_data_size > 0xffffffff) {
|
|
1032 atom_size = 1;
|
|
1033 atom_header_size = sizeof(ngx_mp4_atom_header64_t);
|
|
1034 ngx_mp4_set_64value(atom_header + sizeof(ngx_mp4_atom_header_t),
|
|
1035 sizeof(ngx_mp4_atom_header64_t) + atom_data_size);
|
|
1036 } else {
|
|
1037 atom_size = sizeof(ngx_mp4_atom_header_t) + atom_data_size;
|
|
1038 atom_header_size = sizeof(ngx_mp4_atom_header_t);
|
|
1039 }
|
|
1040
|
|
1041 ngx_mp4_set_32value(atom_header, atom_size);
|
|
1042 ngx_mp4_set_atom_name(atom_header, 'm', 'd', 'a', 't');
|
|
1043
|
|
1044 atom = &mp4->mdat_atom_buf;
|
|
1045 atom->temporary = 1;
|
|
1046 atom->pos = atom_header;
|
|
1047 atom->last = atom_header + atom_header_size;
|
|
1048
|
|
1049 return atom_header_size;
|
|
1050 }
|
|
1051
|
|
1052
|
|
1053 typedef struct {
|
|
1054 u_char size[4];
|
|
1055 u_char name[4];
|
|
1056 u_char version[1];
|
|
1057 u_char flags[3];
|
|
1058 u_char creation_time[4];
|
|
1059 u_char modification_time[4];
|
|
1060 u_char timescale[4];
|
|
1061 u_char duration[4];
|
|
1062 u_char rate[4];
|
|
1063 u_char volume[2];
|
|
1064 u_char reserved[10];
|
|
1065 u_char matrix[36];
|
|
1066 u_char preview_time[4];
|
|
1067 u_char preview_duration[4];
|
|
1068 u_char poster_time[4];
|
|
1069 u_char selection_time[4];
|
|
1070 u_char selection_duration[4];
|
|
1071 u_char current_time[4];
|
|
1072 u_char next_track_id[4];
|
|
1073 } ngx_mp4_mvhd_atom_t;
|
|
1074
|
|
1075 typedef struct {
|
|
1076 u_char size[4];
|
|
1077 u_char name[4];
|
|
1078 u_char version[1];
|
|
1079 u_char flags[3];
|
|
1080 u_char creation_time[8];
|
|
1081 u_char modification_time[8];
|
|
1082 u_char timescale[4];
|
|
1083 u_char duration[8];
|
|
1084 u_char rate[4];
|
|
1085 u_char volume[2];
|
|
1086 u_char reserved[10];
|
|
1087 u_char matrix[36];
|
|
1088 u_char preview_time[4];
|
|
1089 u_char preview_duration[4];
|
|
1090 u_char poster_time[4];
|
|
1091 u_char selection_time[4];
|
|
1092 u_char selection_duration[4];
|
|
1093 u_char current_time[4];
|
|
1094 u_char next_track_id[4];
|
|
1095 } ngx_mp4_mvhd64_atom_t;
|
|
1096
|
|
1097
|
|
1098 static ngx_int_t
|
|
1099 ngx_http_mp4_read_mvhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
1100 {
|
|
1101 u_char *atom_header;
|
|
1102 size_t atom_size;
|
|
1103 uint32_t timescale;
|
|
1104 uint64_t duration;
|
|
1105 ngx_buf_t *atom;
|
|
1106 ngx_mp4_mvhd_atom_t *mvhd_atom;
|
|
1107 ngx_mp4_mvhd64_atom_t *mvhd64_atom;
|
|
1108
|
|
1109 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 mvhd atom");
|
|
1110
|
|
1111 atom_header = ngx_mp4_atom_header(mp4);
|
|
1112 mvhd_atom = (ngx_mp4_mvhd_atom_t *) atom_header;
|
|
1113 mvhd64_atom = (ngx_mp4_mvhd64_atom_t *) atom_header;
|
|
1114 ngx_mp4_set_atom_name(atom_header, 'm', 'v', 'h', 'd');
|
|
1115
|
|
1116 if (mvhd_atom->version[0] == 0) {
|
|
1117 /* version 0: 32-bit duration */
|
|
1118 timescale = ngx_mp4_get_32value(mvhd_atom->timescale);
|
|
1119 duration = ngx_mp4_get_32value(mvhd_atom->duration);
|
|
1120
|
|
1121 } else {
|
|
1122 /* version 1: 64-bit duration */
|
|
1123 timescale = ngx_mp4_get_32value(mvhd64_atom->timescale);
|
|
1124 duration = ngx_mp4_get_64value(mvhd64_atom->duration);
|
|
1125 }
|
|
1126
|
|
1127 mp4->timescale = timescale;
|
|
1128
|
|
1129 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
1130 "mvhd timescale:%uD, duration:%uL, time:%.3fs",
|
|
1131 timescale, duration, (double) duration / timescale);
|
|
1132
|
|
1133 duration -= (uint64_t) mp4->start * timescale / 1000;
|
|
1134
|
|
1135 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
1136 "mvhd new duration:%uL, time:%.3fs",
|
|
1137 duration, (double) duration / timescale);
|
|
1138
|
|
1139 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
|
|
1140 ngx_mp4_set_32value(mvhd_atom->size, atom_size);
|
|
1141
|
|
1142 if (mvhd_atom->version[0] == 0) {
|
|
1143 ngx_mp4_set_32value(mvhd_atom->duration, duration);
|
|
1144
|
|
1145 } else {
|
|
1146 ngx_mp4_set_64value(mvhd64_atom->duration, duration);
|
|
1147 }
|
|
1148
|
|
1149 atom = &mp4->mvhd_atom_buf;
|
|
1150 atom->temporary = 1;
|
|
1151 atom->pos = atom_header;
|
|
1152 atom->last = atom_header + atom_size;
|
|
1153
|
|
1154 mp4->mvhd_atom.buf = atom;
|
|
1155
|
|
1156 ngx_mp4_atom_next(mp4, atom_data_size);
|
|
1157
|
|
1158 return NGX_OK;
|
|
1159 }
|
|
1160
|
|
1161
|
|
1162 static ngx_int_t
|
|
1163 ngx_http_mp4_read_trak_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
1164 {
|
|
1165 u_char *atom_header;
|
|
1166 ngx_buf_t *atom;
|
|
1167 ngx_http_mp4_trak_t *trak;
|
|
1168
|
|
1169 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 trak atom");
|
|
1170
|
|
1171 trak = ngx_array_push(&mp4->trak);
|
|
1172 if (trak == NULL) {
|
|
1173 return NGX_ERROR;
|
|
1174 }
|
|
1175
|
|
1176 ngx_memzero(trak, sizeof(ngx_http_mp4_trak_t));
|
|
1177
|
|
1178 atom_header = ngx_mp4_atom_header(mp4);
|
|
1179 ngx_mp4_set_atom_name(atom_header, 't', 'r', 'a', 'k');
|
|
1180
|
|
1181 atom = &trak->trak_atom_buf;
|
|
1182 atom->temporary = 1;
|
|
1183 atom->pos = atom_header;
|
|
1184 atom->last = atom_header + sizeof(ngx_mp4_atom_header_t);
|
|
1185
|
|
1186 trak->out[NGX_HTTP_MP4_TRAK_ATOM].buf = atom;
|
|
1187
|
|
1188 return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_trak_atoms, atom_data_size);
|
|
1189 }
|
|
1190
|
|
1191
|
|
1192 static void
|
|
1193 ngx_http_mp4_update_trak_atom(ngx_http_mp4_file_t *mp4,
|
|
1194 ngx_http_mp4_trak_t *trak)
|
|
1195 {
|
|
1196 ngx_buf_t *atom;
|
|
1197
|
|
1198 trak->size += sizeof(ngx_mp4_atom_header_t);
|
|
1199 atom = &trak->trak_atom_buf;
|
|
1200 ngx_mp4_set_32value(atom->pos, trak->size);
|
|
1201 }
|
|
1202
|
|
1203
|
|
1204 static ngx_int_t
|
|
1205 ngx_http_mp4_read_cmov_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
1206 {
|
|
1207 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
1208 "\"%s\" mp4 compressed moov atom (cmov) is not supported",
|
|
1209 mp4->file.name.data);
|
|
1210
|
|
1211 return NGX_ERROR;
|
|
1212 }
|
|
1213
|
|
1214
|
|
1215 typedef struct {
|
|
1216 u_char size[4];
|
|
1217 u_char name[4];
|
|
1218 u_char version[1];
|
|
1219 u_char flags[3];
|
|
1220 u_char creation_time[4];
|
|
1221 u_char modification_time[4];
|
|
1222 u_char track_id[4];
|
|
1223 u_char reserved1[4];
|
|
1224 u_char duration[4];
|
|
1225 u_char reserved2[8];
|
|
1226 u_char layer[2];
|
|
1227 u_char group[2];
|
|
1228 u_char volume[2];
|
|
1229 u_char reverved3[2];
|
|
1230 u_char matrix[36];
|
|
1231 u_char width[4];
|
|
1232 u_char heigth[4];
|
|
1233 } ngx_mp4_tkhd_atom_t;
|
|
1234
|
|
1235 typedef struct {
|
|
1236 u_char size[4];
|
|
1237 u_char name[4];
|
|
1238 u_char version[1];
|
|
1239 u_char flags[3];
|
|
1240 u_char creation_time[8];
|
|
1241 u_char modification_time[8];
|
|
1242 u_char track_id[4];
|
|
1243 u_char reserved1[4];
|
|
1244 u_char duration[8];
|
|
1245 u_char reserved2[8];
|
|
1246 u_char layer[2];
|
|
1247 u_char group[2];
|
|
1248 u_char volume[2];
|
|
1249 u_char reverved3[2];
|
|
1250 u_char matrix[36];
|
|
1251 u_char width[4];
|
|
1252 u_char heigth[4];
|
|
1253 } ngx_mp4_tkhd64_atom_t;
|
|
1254
|
|
1255
|
|
1256 static ngx_int_t
|
|
1257 ngx_http_mp4_read_tkhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
1258 {
|
|
1259 u_char *atom_header;
|
|
1260 size_t atom_size;
|
|
1261 uint64_t duration;
|
|
1262 ngx_buf_t *atom;
|
|
1263 ngx_http_mp4_trak_t *trak;
|
|
1264 ngx_mp4_tkhd_atom_t *tkhd_atom;
|
|
1265 ngx_mp4_tkhd64_atom_t *tkhd64_atom;
|
|
1266
|
|
1267 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 tkhd atom");
|
|
1268
|
|
1269 atom_header = ngx_mp4_atom_header(mp4);
|
|
1270 tkhd_atom = (ngx_mp4_tkhd_atom_t *) atom_header;
|
|
1271 tkhd64_atom = (ngx_mp4_tkhd64_atom_t *) atom_header;
|
|
1272 ngx_mp4_set_atom_name(tkhd_atom, 't', 'k', 'h', 'd');
|
|
1273
|
|
1274 if (tkhd_atom->version[0] == 0) {
|
|
1275 /* version 0: 32-bit duration */
|
|
1276 duration = ngx_mp4_get_32value(tkhd_atom->duration);
|
|
1277
|
|
1278 } else {
|
|
1279 /* version 1: 64-bit duration */
|
|
1280 duration = ngx_mp4_get_64value(tkhd64_atom->duration);
|
|
1281 }
|
|
1282
|
|
1283 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
1284 "tkhd duration:%uL, time:%.3fs",
|
|
1285 duration, (double) duration / mp4->timescale);
|
|
1286
|
|
1287 duration -= (uint64_t) mp4->start * mp4->timescale / 1000;
|
|
1288
|
|
1289 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
1290 "tkhd new duration:%uL, time:%.3fs",
|
|
1291 duration, (double) duration / mp4->timescale);
|
|
1292
|
|
1293 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
|
|
1294
|
|
1295 trak = ngx_mp4_last_trak(mp4);
|
|
1296 trak->tkhd_size = atom_size;
|
|
1297
|
|
1298 ngx_mp4_set_32value(tkhd_atom->size, atom_size);
|
|
1299
|
|
1300 if (tkhd_atom->version[0] == 0) {
|
|
1301 ngx_mp4_set_32value(tkhd_atom->duration, duration);
|
|
1302
|
|
1303 } else {
|
|
1304 ngx_mp4_set_64value(tkhd64_atom->duration, duration);
|
|
1305 }
|
|
1306
|
|
1307 atom = &trak->tkhd_atom_buf;
|
|
1308 atom->temporary = 1;
|
|
1309 atom->pos = atom_header;
|
|
1310 atom->last = atom_header + atom_size;
|
|
1311
|
|
1312 trak->out[NGX_HTTP_MP4_TKHD_ATOM].buf = atom;
|
|
1313
|
|
1314 ngx_mp4_atom_next(mp4, atom_data_size);
|
|
1315
|
|
1316 return NGX_OK;
|
|
1317 }
|
|
1318
|
|
1319
|
|
1320 static ngx_int_t
|
|
1321 ngx_http_mp4_read_mdia_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
1322 {
|
|
1323 u_char *atom_header;
|
|
1324 ngx_buf_t *atom;
|
|
1325 ngx_http_mp4_trak_t *trak;
|
|
1326
|
|
1327 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "process mdia atom");
|
|
1328
|
|
1329 atom_header = ngx_mp4_atom_header(mp4);
|
|
1330 ngx_mp4_set_atom_name(atom_header, 'm', 'd', 'i', 'a');
|
|
1331
|
|
1332 trak = ngx_mp4_last_trak(mp4);
|
|
1333
|
|
1334 atom = &trak->mdia_atom_buf;
|
|
1335 atom->temporary = 1;
|
|
1336 atom->pos = atom_header;
|
|
1337 atom->last = atom_header + sizeof(ngx_mp4_atom_header_t);
|
|
1338
|
|
1339 trak->out[NGX_HTTP_MP4_MDIA_ATOM].buf = atom;
|
|
1340
|
|
1341 return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_mdia_atoms, atom_data_size);
|
|
1342 }
|
|
1343
|
|
1344
|
|
1345 static void
|
|
1346 ngx_http_mp4_update_mdia_atom(ngx_http_mp4_file_t *mp4,
|
|
1347 ngx_http_mp4_trak_t *trak)
|
|
1348 {
|
|
1349 ngx_buf_t *atom;
|
|
1350
|
|
1351 trak->size += sizeof(ngx_mp4_atom_header_t);
|
|
1352 atom = &trak->mdia_atom_buf;
|
|
1353 ngx_mp4_set_32value(atom->pos, trak->size);
|
|
1354 }
|
|
1355
|
|
1356
|
|
1357 typedef struct {
|
|
1358 u_char size[4];
|
|
1359 u_char name[4];
|
|
1360 u_char version[1];
|
|
1361 u_char flags[3];
|
|
1362 u_char creation_time[4];
|
|
1363 u_char modification_time[4];
|
|
1364 u_char timescale[4];
|
|
1365 u_char duration[4];
|
|
1366 u_char language[2];
|
|
1367 u_char quality[2];
|
|
1368 } ngx_mp4_mdhd_atom_t;
|
|
1369
|
|
1370 typedef struct {
|
|
1371 u_char size[4];
|
|
1372 u_char name[4];
|
|
1373 u_char version[1];
|
|
1374 u_char flags[3];
|
|
1375 u_char creation_time[8];
|
|
1376 u_char modification_time[8];
|
|
1377 u_char timescale[4];
|
|
1378 u_char duration[8];
|
|
1379 u_char language[2];
|
|
1380 u_char quality[2];
|
|
1381 } ngx_mp4_mdhd64_atom_t;
|
|
1382
|
|
1383
|
|
1384 static ngx_int_t
|
|
1385 ngx_http_mp4_read_mdhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
1386 {
|
|
1387 u_char *atom_header;
|
|
1388 size_t atom_size;
|
|
1389 uint32_t timescale;
|
|
1390 uint64_t duration;
|
|
1391 ngx_buf_t *atom;
|
|
1392 ngx_http_mp4_trak_t *trak;
|
|
1393 ngx_mp4_mdhd_atom_t *mdhd_atom;
|
|
1394 ngx_mp4_mdhd64_atom_t *mdhd64_atom;
|
|
1395
|
|
1396 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 mdhd atom");
|
|
1397
|
|
1398 atom_header = ngx_mp4_atom_header(mp4);
|
|
1399 mdhd_atom = (ngx_mp4_mdhd_atom_t *) atom_header;
|
|
1400 mdhd64_atom = (ngx_mp4_mdhd64_atom_t *) atom_header;
|
|
1401 ngx_mp4_set_atom_name(mdhd_atom, 'm', 'd', 'h', 'd');
|
|
1402
|
|
1403 if (mdhd_atom->version[0] == 0) {
|
|
1404 /* version 0: everything is 32-bit */
|
|
1405 timescale = ngx_mp4_get_32value(mdhd_atom->timescale);
|
|
1406 duration = ngx_mp4_get_32value(mdhd_atom->duration);
|
|
1407
|
|
1408 } else {
|
|
1409 /* version 1: 64-bit duration and 32-bit timescale */
|
|
1410 timescale = ngx_mp4_get_32value(mdhd64_atom->timescale);
|
|
1411 duration = ngx_mp4_get_64value(mdhd64_atom->duration);
|
|
1412 }
|
|
1413
|
|
1414 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
1415 "mdhd timescale:%uD, duration:%uL, time:%.3fs",
|
|
1416 timescale, duration, (double) duration / timescale);
|
|
1417
|
|
1418 duration -= (uint64_t) mp4->start * timescale / 1000;
|
|
1419
|
|
1420 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
1421 "mdhd new duration:%uL, time:%.3fs",
|
|
1422 duration, (double) duration / timescale);
|
|
1423
|
|
1424 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
|
|
1425
|
|
1426 trak = ngx_mp4_last_trak(mp4);
|
|
1427 trak->mdhd_size = atom_size;
|
|
1428 trak->timescale = timescale;
|
|
1429
|
|
1430 ngx_mp4_set_32value(mdhd_atom->size, atom_size);
|
|
1431
|
|
1432 if (mdhd_atom->version[0] == 0) {
|
|
1433 ngx_mp4_set_32value(mdhd_atom->duration, duration);
|
|
1434
|
|
1435 } else {
|
|
1436 ngx_mp4_set_64value(mdhd64_atom->duration, duration);
|
|
1437 }
|
|
1438
|
|
1439 atom = &trak->mdhd_atom_buf;
|
|
1440 atom->temporary = 1;
|
|
1441 atom->pos = atom_header;
|
|
1442 atom->last = atom_header + atom_size;
|
|
1443
|
|
1444 trak->out[NGX_HTTP_MP4_MDHD_ATOM].buf = atom;
|
|
1445
|
|
1446 ngx_mp4_atom_next(mp4, atom_data_size);
|
|
1447
|
|
1448 return NGX_OK;
|
|
1449 }
|
|
1450
|
|
1451
|
|
1452 static ngx_int_t
|
|
1453 ngx_http_mp4_read_hdlr_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
1454 {
|
|
1455 u_char *atom_header;
|
|
1456 size_t atom_size;
|
|
1457 ngx_buf_t *atom;
|
|
1458 ngx_http_mp4_trak_t *trak;
|
|
1459
|
|
1460 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 hdlr atom");
|
|
1461
|
|
1462 atom_header = ngx_mp4_atom_header(mp4);
|
|
1463 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
|
|
1464 ngx_mp4_set_32value(atom_header, atom_size);
|
|
1465 ngx_mp4_set_atom_name(atom_header, 'h', 'd', 'l', 'r');
|
|
1466
|
|
1467 trak = ngx_mp4_last_trak(mp4);
|
|
1468
|
|
1469 atom = &trak->hdlr_atom_buf;
|
|
1470 atom->temporary = 1;
|
|
1471 atom->pos = atom_header;
|
|
1472 atom->last = atom_header + atom_size;
|
|
1473
|
|
1474 trak->hdlr_size = atom_size;
|
|
1475 trak->out[NGX_HTTP_MP4_HDLR_ATOM].buf = atom;
|
|
1476
|
|
1477 ngx_mp4_atom_next(mp4, atom_data_size);
|
|
1478
|
|
1479 return NGX_OK;
|
|
1480 }
|
|
1481
|
|
1482
|
|
1483 static ngx_int_t
|
|
1484 ngx_http_mp4_read_minf_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
1485 {
|
|
1486 u_char *atom_header;
|
|
1487 ngx_buf_t *atom;
|
|
1488 ngx_http_mp4_trak_t *trak;
|
|
1489
|
|
1490 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "process minf atom");
|
|
1491
|
|
1492 atom_header = ngx_mp4_atom_header(mp4);
|
|
1493 ngx_mp4_set_atom_name(atom_header, 'm', 'i', 'n', 'f');
|
|
1494
|
|
1495 trak = ngx_mp4_last_trak(mp4);
|
|
1496
|
|
1497 atom = &trak->minf_atom_buf;
|
|
1498 atom->temporary = 1;
|
|
1499 atom->pos = atom_header;
|
|
1500 atom->last = atom_header + sizeof(ngx_mp4_atom_header_t);
|
|
1501
|
|
1502 trak->out[NGX_HTTP_MP4_MINF_ATOM].buf = atom;
|
|
1503
|
|
1504 return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_minf_atoms, atom_data_size);
|
|
1505 }
|
|
1506
|
|
1507
|
|
1508 static void
|
|
1509 ngx_http_mp4_update_minf_atom(ngx_http_mp4_file_t *mp4,
|
|
1510 ngx_http_mp4_trak_t *trak)
|
|
1511 {
|
|
1512 ngx_buf_t *atom;
|
|
1513
|
|
1514 trak->size += sizeof(ngx_mp4_atom_header_t)
|
|
1515 + trak->vmhd_size
|
|
1516 + trak->smhd_size
|
|
1517 + trak->dinf_size;
|
|
1518 atom = &trak->minf_atom_buf;
|
|
1519 ngx_mp4_set_32value(atom->pos, trak->size);
|
|
1520 }
|
|
1521
|
|
1522
|
|
1523 static ngx_int_t
|
|
1524 ngx_http_mp4_read_vmhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
1525 {
|
|
1526 u_char *atom_header;
|
|
1527 size_t atom_size;
|
|
1528 ngx_buf_t *atom;
|
|
1529 ngx_http_mp4_trak_t *trak;
|
|
1530
|
|
1531 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 vmhd atom");
|
|
1532
|
|
1533 atom_header = ngx_mp4_atom_header(mp4);
|
|
1534 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
|
|
1535 ngx_mp4_set_32value(atom_header, atom_size);
|
|
1536 ngx_mp4_set_atom_name(atom_header, 'v', 'm', 'h', 'd');
|
|
1537
|
|
1538 trak = ngx_mp4_last_trak(mp4);
|
|
1539
|
|
1540 atom = &trak->vmhd_atom_buf;
|
|
1541 atom->temporary = 1;
|
|
1542 atom->pos = atom_header;
|
|
1543 atom->last = atom_header + atom_size;
|
|
1544
|
|
1545 trak->vmhd_size += atom_size;
|
|
1546 trak->out[NGX_HTTP_MP4_VMHD_ATOM].buf = atom;
|
|
1547
|
|
1548 ngx_mp4_atom_next(mp4, atom_data_size);
|
|
1549
|
|
1550 return NGX_OK;
|
|
1551 }
|
|
1552
|
|
1553
|
|
1554 static ngx_int_t
|
|
1555 ngx_http_mp4_read_smhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
1556 {
|
|
1557 u_char *atom_header;
|
|
1558 size_t atom_size;
|
|
1559 ngx_buf_t *atom;
|
|
1560 ngx_http_mp4_trak_t *trak;
|
|
1561
|
|
1562 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 smhd atom");
|
|
1563
|
|
1564 atom_header = ngx_mp4_atom_header(mp4);
|
|
1565 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
|
|
1566 ngx_mp4_set_32value(atom_header, atom_size);
|
|
1567 ngx_mp4_set_atom_name(atom_header, 's', 'm', 'h', 'd');
|
|
1568
|
|
1569 trak = ngx_mp4_last_trak(mp4);
|
|
1570
|
|
1571 atom = &trak->smhd_atom_buf;
|
|
1572 atom->temporary = 1;
|
|
1573 atom->pos = atom_header;
|
|
1574 atom->last = atom_header + atom_size;
|
|
1575
|
|
1576 trak->vmhd_size += atom_size;
|
|
1577 trak->out[NGX_HTTP_MP4_SMHD_ATOM].buf = atom;
|
|
1578
|
|
1579 ngx_mp4_atom_next(mp4, atom_data_size);
|
|
1580
|
|
1581 return NGX_OK;
|
|
1582 }
|
|
1583
|
|
1584
|
|
1585 static ngx_int_t
|
|
1586 ngx_http_mp4_read_dinf_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
1587 {
|
|
1588 u_char *atom_header;
|
|
1589 size_t atom_size;
|
|
1590 ngx_buf_t *atom;
|
|
1591 ngx_http_mp4_trak_t *trak;
|
|
1592
|
|
1593 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 dinf atom");
|
|
1594
|
|
1595 atom_header = ngx_mp4_atom_header(mp4);
|
|
1596 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
|
|
1597 ngx_mp4_set_32value(atom_header, atom_size);
|
|
1598 ngx_mp4_set_atom_name(atom_header, 'd', 'i', 'n', 'f');
|
|
1599
|
|
1600 trak = ngx_mp4_last_trak(mp4);
|
|
1601
|
|
1602 atom = &trak->dinf_atom_buf;
|
|
1603 atom->temporary = 1;
|
|
1604 atom->pos = atom_header;
|
|
1605 atom->last = atom_header + atom_size;
|
|
1606
|
|
1607 trak->dinf_size += atom_size;
|
|
1608 trak->out[NGX_HTTP_MP4_DINF_ATOM].buf = atom;
|
|
1609
|
|
1610 ngx_mp4_atom_next(mp4, atom_data_size);
|
|
1611
|
|
1612 return NGX_OK;
|
|
1613 }
|
|
1614
|
|
1615
|
|
1616 static ngx_int_t
|
|
1617 ngx_http_mp4_read_stbl_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
1618 {
|
|
1619 u_char *atom_header;
|
|
1620 ngx_buf_t *atom;
|
|
1621 ngx_http_mp4_trak_t *trak;
|
|
1622
|
|
1623 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "process stbl atom");
|
|
1624
|
|
1625 atom_header = ngx_mp4_atom_header(mp4);
|
|
1626 ngx_mp4_set_atom_name(atom_header, 's', 't', 'b', 'l');
|
|
1627
|
|
1628 trak = ngx_mp4_last_trak(mp4);
|
|
1629
|
|
1630 atom = &trak->stbl_atom_buf;
|
|
1631 atom->temporary = 1;
|
|
1632 atom->pos = atom_header;
|
|
1633 atom->last = atom_header + sizeof(ngx_mp4_atom_header_t);
|
|
1634
|
|
1635 trak->out[NGX_HTTP_MP4_STBL_ATOM].buf = atom;
|
|
1636
|
|
1637 return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_stbl_atoms, atom_data_size);
|
|
1638 }
|
|
1639
|
|
1640
|
|
1641 static void
|
|
1642 ngx_http_mp4_update_stbl_atom(ngx_http_mp4_file_t *mp4,
|
|
1643 ngx_http_mp4_trak_t *trak)
|
|
1644 {
|
|
1645 ngx_buf_t *atom;
|
|
1646
|
|
1647 trak->size += sizeof(ngx_mp4_atom_header_t);
|
|
1648 atom = &trak->stbl_atom_buf;
|
|
1649 ngx_mp4_set_32value(atom->pos, trak->size);
|
|
1650 }
|
|
1651
|
|
1652
|
|
1653 typedef struct {
|
|
1654 u_char size[4];
|
|
1655 u_char name[4];
|
|
1656 u_char version[1];
|
|
1657 u_char flags[3];
|
|
1658 u_char entries[4];
|
|
1659 } ngx_mp4_stsd_atom_t;
|
|
1660
|
|
1661
|
|
1662 static ngx_int_t
|
|
1663 ngx_http_mp4_read_stsd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
1664 {
|
|
1665 u_char *atom_header, *atom_table;
|
|
1666 size_t atom_size;
|
|
1667 ngx_buf_t *atom;
|
|
1668 ngx_mp4_stsd_atom_t *stsd_atom;
|
|
1669 ngx_http_mp4_trak_t *trak;
|
|
1670
|
|
1671 /* sample description atom */
|
|
1672
|
|
1673 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stsd atom");
|
|
1674
|
|
1675 atom_header = ngx_mp4_atom_header(mp4);
|
|
1676 stsd_atom = (ngx_mp4_stsd_atom_t *) atom_header;
|
|
1677 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
|
|
1678 atom_table = atom_header + atom_size;
|
|
1679 ngx_mp4_set_32value(stsd_atom->size, atom_size);
|
|
1680 ngx_mp4_set_atom_name(stsd_atom, 's', 't', 's', 'd');
|
|
1681
|
|
1682 #if (NGX_DEBUG)
|
|
1683 {
|
|
1684 uint32_t entries;
|
|
1685
|
|
1686 entries = ngx_mp4_get_32value(stsd_atom->entries);
|
|
1687
|
|
1688 if (entries) {
|
|
1689 /* codecs of interest: avc1 (H.264), mp4a (MPEG-4 audio) */
|
|
1690
|
|
1691 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
1692 "stsd entries:%ui, codec:%*s",
|
|
1693 entries, 4, atom_table);
|
|
1694 }
|
|
1695 }
|
|
1696 #endif
|
|
1697
|
|
1698 trak = ngx_mp4_last_trak(mp4);
|
|
1699
|
|
1700 atom = &trak->stsd_atom_buf;
|
|
1701 atom->temporary = 1;
|
|
1702 atom->pos = atom_header;
|
|
1703 atom->last = atom_table;
|
|
1704
|
|
1705 trak->out[NGX_HTTP_MP4_STSD_ATOM].buf = atom;
|
|
1706 trak->size += atom_size;
|
|
1707
|
|
1708 ngx_mp4_atom_next(mp4, atom_data_size);
|
|
1709
|
|
1710 return NGX_OK;
|
|
1711 }
|
|
1712
|
|
1713
|
|
1714 typedef struct {
|
|
1715 u_char size[4];
|
|
1716 u_char name[4];
|
|
1717 u_char version[1];
|
|
1718 u_char flags[3];
|
|
1719 u_char entries[4];
|
|
1720 } ngx_mp4_stts_atom_t;
|
|
1721
|
|
1722 typedef struct {
|
|
1723 u_char count[4];
|
|
1724 u_char duration[4];
|
|
1725 } ngx_mp4_stts_entry_t;
|
|
1726
|
|
1727
|
|
1728 static ngx_int_t
|
|
1729 ngx_http_mp4_read_stts_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
1730 {
|
|
1731 u_char *atom_header, *atom_table, *atom_end;
|
|
1732 uint32_t entries;
|
|
1733 ngx_buf_t *atom, *data;
|
|
1734 ngx_mp4_stts_atom_t *stts_atom;
|
|
1735 ngx_http_mp4_trak_t *trak;
|
|
1736
|
|
1737 /* time-to-sample atom */
|
|
1738
|
|
1739 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stts atom");
|
|
1740
|
|
1741 atom_header = ngx_mp4_atom_header(mp4);
|
|
1742 stts_atom = (ngx_mp4_stts_atom_t *) atom_header;
|
|
1743 ngx_mp4_set_atom_name(stts_atom, 's', 't', 't', 's');
|
|
1744
|
|
1745 entries = ngx_mp4_get_32value(stts_atom->entries);
|
|
1746
|
|
1747 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
1748 "mp4 time-to-sample entries:%ui", entries);
|
|
1749
|
|
1750 atom_table = atom_header + sizeof(ngx_mp4_stts_atom_t);
|
|
1751 atom_end = atom_table + entries * sizeof(ngx_mp4_stts_entry_t);
|
|
1752
|
|
1753 if ((uint64_t) (atom_end - stts_atom->version) > atom_data_size) {
|
|
1754 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
1755 "\"%s\" mp4 stts atom too large",
|
|
1756 mp4->file.name.data);
|
|
1757 return NGX_ERROR;
|
|
1758 }
|
|
1759
|
|
1760 trak = ngx_mp4_last_trak(mp4);
|
|
1761 trak->time_to_sample_entries = entries;
|
|
1762
|
|
1763 atom = &trak->stts_atom_buf;
|
|
1764 atom->temporary = 1;
|
|
1765 atom->pos = atom_header;
|
|
1766 atom->last = atom_table;
|
|
1767
|
|
1768 data = &trak->stts_data_buf;
|
|
1769 data->temporary = 1;
|
|
1770 data->pos = atom_table;
|
|
1771 data->last = atom_end;
|
|
1772
|
|
1773 trak->out[NGX_HTTP_MP4_STTS_ATOM].buf = atom;
|
|
1774 trak->out[NGX_HTTP_MP4_STTS_DATA].buf = data;
|
|
1775
|
|
1776 ngx_mp4_atom_next(mp4, atom_data_size);
|
|
1777
|
|
1778 return NGX_OK;
|
|
1779 }
|
|
1780
|
|
1781
|
|
1782 static ngx_int_t
|
|
1783 ngx_http_mp4_update_stts_atom(ngx_http_mp4_file_t *mp4,
|
|
1784 ngx_http_mp4_trak_t *trak)
|
|
1785 {
|
|
1786 size_t atom_size;
|
|
1787 uint32_t entries, count, duration;
|
|
1788 uint64_t start_time;
|
|
1789 ngx_buf_t *atom, *data;
|
|
1790 ngx_uint_t start_sample;
|
|
1791 ngx_mp4_stts_atom_t *stts_atom;
|
|
1792 ngx_mp4_stts_entry_t *entry, *end;
|
|
1793
|
|
1794 /*
|
|
1795 * mdia.minf.stbl.stts updating requires trak->timescale
|
|
1796 * from mdia.mdhd atom which may reside after mdia.minf
|
|
1797 */
|
|
1798
|
|
1799 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
1800 "mp4 stts atom update");
|
|
1801
|
|
1802 data = trak->out[NGX_HTTP_MP4_STTS_DATA].buf;
|
|
1803
|
|
1804 if (data == NULL) {
|
|
1805 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
1806 "no mp4 stts atoms were found in \"%s\"",
|
|
1807 mp4->file.name.data);
|
|
1808 return NGX_ERROR;
|
|
1809 }
|
|
1810
|
|
1811 entries = trak->time_to_sample_entries;
|
|
1812 start_time = mp4->start * trak->timescale / 1000;
|
|
1813
|
|
1814 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
1815 "time-to-sample start_time:%ui", start_time);
|
|
1816
|
|
1817 start_sample = 0;
|
|
1818 entry = (ngx_mp4_stts_entry_t *) data->pos;
|
|
1819 end = (ngx_mp4_stts_entry_t *) data->last;
|
|
1820
|
|
1821 while (entry < end) {
|
|
1822 count = ngx_mp4_get_32value(entry->count);
|
|
1823 duration = ngx_mp4_get_32value(entry->duration);
|
|
1824
|
|
1825 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
1826 "count:%uD, duration:%uD", count, duration);
|
|
1827
|
|
1828 if (start_time < count * duration) {
|
|
1829 start_sample += (ngx_uint_t) (start_time / duration);
|
|
1830 count -= start_sample;
|
|
1831 ngx_mp4_set_32value(entry->count, count);
|
|
1832 goto found;
|
|
1833 }
|
|
1834
|
|
1835 start_sample += count;
|
|
1836 start_time -= count * duration;
|
|
1837 entries--;
|
|
1838 entry++;
|
|
1839 }
|
|
1840
|
|
1841 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
1842 "start time is out mp4 stts samples in \"%\"",
|
|
1843 mp4->file.name.data);
|
|
1844
|
|
1845 return NGX_ERROR;
|
|
1846
|
|
1847 found:
|
|
1848
|
|
1849 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
1850 "start_sample:%ui, new count:%uD", start_sample, count);
|
|
1851
|
|
1852 trak->start_sample = start_sample;
|
|
1853
|
|
1854 data->pos = (u_char *) entry;
|
|
1855 atom_size = sizeof(ngx_mp4_stts_atom_t) + (data->last - data->pos);
|
|
1856 trak->size += atom_size;
|
|
1857
|
|
1858 atom = trak->out[NGX_HTTP_MP4_STTS_ATOM].buf;
|
|
1859 stts_atom = (ngx_mp4_stts_atom_t *) atom->pos;
|
|
1860 ngx_mp4_set_32value(stts_atom->size, atom_size);
|
|
1861 ngx_mp4_set_32value(stts_atom->entries, entries);
|
|
1862
|
|
1863 return NGX_OK;
|
|
1864 }
|
|
1865
|
|
1866
|
|
1867 typedef struct {
|
|
1868 u_char size[4];
|
|
1869 u_char name[4];
|
|
1870 u_char version[1];
|
|
1871 u_char flags[3];
|
|
1872 u_char entries[4];
|
|
1873 } ngx_http_mp4_stss_atom_t;
|
|
1874
|
|
1875
|
|
1876 static ngx_int_t
|
|
1877 ngx_http_mp4_read_stss_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
1878 {
|
|
1879 u_char *atom_header, *atom_table, *atom_end;
|
|
1880 uint32_t entries;
|
|
1881 ngx_buf_t *atom, *data;
|
|
1882 ngx_http_mp4_trak_t *trak;
|
|
1883 ngx_http_mp4_stss_atom_t *stss_atom;
|
|
1884
|
|
1885 /* sync samples atom */
|
|
1886
|
|
1887 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stss atom");
|
|
1888
|
|
1889 atom_header = ngx_mp4_atom_header(mp4);
|
|
1890 stss_atom = (ngx_http_mp4_stss_atom_t *) atom_header;
|
|
1891 ngx_mp4_set_atom_name(stss_atom, 's', 't', 's', 's');
|
|
1892
|
|
1893 entries = ngx_mp4_get_32value(stss_atom->entries);
|
|
1894
|
|
1895 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
1896 "sync sample entries:%ui", entries);
|
|
1897
|
|
1898 trak = ngx_mp4_last_trak(mp4);
|
|
1899 trak->sync_samples_entries = entries;
|
|
1900
|
|
1901 atom_table = atom_header + sizeof(ngx_http_mp4_stss_atom_t);
|
|
1902
|
|
1903 atom = &trak->stss_atom_buf;
|
|
1904 atom->temporary = 1;
|
|
1905 atom->pos = atom_header;
|
|
1906 atom->last = atom_table;
|
|
1907
|
|
1908 atom_end = atom_table + entries * sizeof(uint32_t);
|
|
1909
|
|
1910 if ((uint64_t) (atom_end - stss_atom->version) > atom_data_size) {
|
|
1911 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
1912 "\"%s\" mp4 stss atom too large", mp4->file.name.data);
|
|
1913 return NGX_ERROR;
|
|
1914 }
|
|
1915
|
|
1916 data = &trak->stss_data_buf;
|
|
1917 data->temporary = 1;
|
|
1918 data->pos = atom_table;
|
|
1919 data->last = atom_end;
|
|
1920
|
|
1921 trak->out[NGX_HTTP_MP4_STSS_ATOM].buf = atom;
|
|
1922 trak->out[NGX_HTTP_MP4_STSS_DATA].buf = data;
|
|
1923
|
|
1924 ngx_mp4_atom_next(mp4, atom_data_size);
|
|
1925
|
|
1926 return NGX_OK;
|
|
1927 }
|
|
1928
|
|
1929
|
|
1930 static ngx_int_t
|
|
1931 ngx_http_mp4_update_stss_atom(ngx_http_mp4_file_t *mp4,
|
|
1932 ngx_http_mp4_trak_t *trak)
|
|
1933 {
|
|
1934 size_t atom_size;
|
|
1935 uint32_t entries, sample, start_sample, *entry, *end;
|
|
1936 ngx_buf_t *atom, *data;
|
|
1937 ngx_http_mp4_stss_atom_t *stss_atom;
|
|
1938
|
|
1939 /*
|
|
1940 * mdia.minf.stbl.stss updating requires trak->start_sample
|
|
1941 * from mdia.minf.stbl.stts which depends on value from mdia.mdhd
|
|
1942 * atom which may reside after mdia.minf
|
|
1943 */
|
|
1944
|
|
1945 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
1946 "mp4 stss atom update");
|
|
1947
|
|
1948 data = trak->out[NGX_HTTP_MP4_STSS_DATA].buf;
|
|
1949
|
|
1950 if (data == NULL) {
|
|
1951 return NGX_OK;
|
|
1952 }
|
|
1953
|
|
1954 /* sync samples starts from 1 */
|
|
1955 start_sample = trak->start_sample + 1;
|
|
1956 entries = trak->sync_samples_entries;
|
|
1957
|
|
1958 entry = (uint32_t *) data->pos;
|
|
1959 end = (uint32_t *) data->last;
|
|
1960
|
|
1961 while (entry < end) {
|
|
1962 sample = ngx_mp4_get_32value(entry);
|
|
1963
|
|
1964 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
1965 "start:%uD, sync:%uD", start_sample, sample);
|
|
1966
|
|
1967 if (sample >= start_sample) {
|
|
1968 goto found;
|
|
1969 }
|
|
1970
|
|
1971 entries--;
|
|
1972 entry++;
|
|
1973 }
|
|
1974
|
|
1975 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
1976 "start sample is out of mp4 stss atom in \"%s\"",
|
|
1977 mp4->file.name.data);
|
|
1978
|
|
1979 return NGX_ERROR;
|
|
1980
|
|
1981 found:
|
|
1982
|
|
1983 data->pos = (u_char *) entry;
|
|
1984
|
|
1985 start_sample = trak->start_sample;
|
|
1986
|
|
1987 while (entry < end) {
|
|
1988 sample = ngx_mp4_get_32value(entry);
|
|
1989 sample -= start_sample;
|
|
1990 ngx_mp4_set_32value(entry, sample);
|
|
1991 entry++;
|
|
1992 }
|
|
1993
|
|
1994 atom_size = sizeof(ngx_http_mp4_stss_atom_t) + (data->last - data->pos);
|
|
1995 trak->size += atom_size;
|
|
1996
|
|
1997 atom = trak->out[NGX_HTTP_MP4_STSS_ATOM].buf;
|
|
1998 stss_atom = (ngx_http_mp4_stss_atom_t *) atom->pos;
|
|
1999
|
|
2000 ngx_mp4_set_32value(stss_atom->size, atom_size);
|
|
2001 ngx_mp4_set_32value(stss_atom->entries, entries);
|
|
2002
|
|
2003 return NGX_OK;
|
|
2004 }
|
|
2005
|
|
2006
|
|
2007 typedef struct {
|
|
2008 u_char size[4];
|
|
2009 u_char name[4];
|
|
2010 u_char version[1];
|
|
2011 u_char flags[3];
|
|
2012 u_char entries[4];
|
|
2013 } ngx_mp4_ctts_atom_t;
|
|
2014
|
|
2015 typedef struct {
|
|
2016 u_char count[4];
|
|
2017 u_char offset[4];
|
|
2018 } ngx_mp4_ctts_entry_t;
|
|
2019
|
|
2020
|
|
2021 static ngx_int_t
|
|
2022 ngx_http_mp4_read_ctts_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
2023 {
|
|
2024 u_char *atom_header, *atom_table, *atom_end;
|
|
2025 uint32_t entries;
|
|
2026 ngx_buf_t *atom, *data;
|
|
2027 ngx_mp4_ctts_atom_t *ctts_atom;
|
|
2028 ngx_http_mp4_trak_t *trak;
|
|
2029
|
|
2030 /* composition offsets atom */
|
|
2031
|
|
2032 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 ctts atom");
|
|
2033
|
|
2034 atom_header = ngx_mp4_atom_header(mp4);
|
|
2035 ctts_atom = (ngx_mp4_ctts_atom_t *) atom_header;
|
|
2036 ngx_mp4_set_atom_name(ctts_atom, 'c', 't', 't', 's');
|
|
2037
|
|
2038 entries = ngx_mp4_get_32value(ctts_atom->entries);
|
|
2039
|
|
2040 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
2041 "composition offset entries:%ui", entries);
|
|
2042
|
|
2043 trak = ngx_mp4_last_trak(mp4);
|
|
2044 trak->composition_offset_entries = entries;
|
|
2045
|
|
2046 atom_table = atom_header + sizeof(ngx_mp4_ctts_atom_t);
|
|
2047
|
|
2048 atom = &trak->ctts_atom_buf;
|
|
2049 atom->temporary = 1;
|
|
2050 atom->pos = atom_header;
|
|
2051 atom->last = atom_table;
|
|
2052
|
|
2053 atom_end = atom_table + entries * sizeof(ngx_mp4_ctts_entry_t);
|
|
2054
|
|
2055 if ((uint64_t) (atom_end - ctts_atom->version) > atom_data_size) {
|
|
2056 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
2057 "\"%s\" mp4 ctts atom too large", mp4->file.name.data);
|
|
2058 return NGX_ERROR;
|
|
2059 }
|
|
2060
|
|
2061 data = &trak->ctts_data_buf;
|
|
2062 data->temporary = 1;
|
|
2063 data->pos = atom_table;
|
|
2064 data->last = atom_end;
|
|
2065
|
|
2066 trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf = atom;
|
|
2067 trak->out[NGX_HTTP_MP4_CTTS_DATA].buf = data;
|
|
2068
|
|
2069 ngx_mp4_atom_next(mp4, atom_data_size);
|
|
2070
|
|
2071 return NGX_OK;
|
|
2072 }
|
|
2073
|
|
2074
|
|
2075 static void
|
|
2076 ngx_http_mp4_update_ctts_atom(ngx_http_mp4_file_t *mp4,
|
|
2077 ngx_http_mp4_trak_t *trak)
|
|
2078 {
|
|
2079 size_t atom_size;
|
|
2080 uint32_t entries, count, start_sample;
|
|
2081 ngx_buf_t *atom, *data;
|
|
2082 ngx_mp4_ctts_atom_t *ctts_atom;
|
|
2083 ngx_mp4_ctts_entry_t *entry, *end;
|
|
2084
|
|
2085 /*
|
|
2086 * mdia.minf.stbl.ctts updating requires trak->start_sample
|
|
2087 * from mdia.minf.stbl.stts which depends on value from mdia.mdhd
|
|
2088 * atom which may reside after mdia.minf
|
|
2089 */
|
|
2090
|
|
2091 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
2092 "mp4 ctts atom update");
|
|
2093
|
|
2094 data = trak->out[NGX_HTTP_MP4_CTTS_DATA].buf;
|
|
2095
|
|
2096 if (data == NULL) {
|
|
2097 return;
|
|
2098 }
|
|
2099
|
|
2100 /* sync samples starts from 1 */
|
|
2101 start_sample = trak->start_sample + 1;
|
|
2102 entries = trak->composition_offset_entries;
|
|
2103 entry = (ngx_mp4_ctts_entry_t *) data->pos;
|
|
2104 end = (ngx_mp4_ctts_entry_t *) data->last;
|
|
2105
|
|
2106 while (entry < end) {
|
|
2107 count = ngx_mp4_get_32value(entry->count);
|
|
2108
|
|
2109 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
2110 "start:%uD, count:%uD, offset:%uD",
|
|
2111 start_sample, count, ngx_mp4_get_32value(entry->offset));
|
|
2112
|
|
2113 if (start_sample <= count) {
|
|
2114 count -= (start_sample - 1);
|
|
2115 ngx_mp4_set_32value(entry->count, count);
|
|
2116 goto found;
|
|
2117 }
|
|
2118
|
|
2119 start_sample -= count;
|
|
2120 entries--;
|
|
2121 entry++;
|
|
2122 }
|
|
2123
|
|
2124 trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf = NULL;
|
|
2125 trak->out[NGX_HTTP_MP4_CTTS_DATA].buf = NULL;
|
|
2126
|
|
2127 return;
|
|
2128
|
|
2129 found:
|
|
2130
|
|
2131 data->pos = (u_char *) entry;
|
|
2132 atom_size = sizeof(ngx_mp4_ctts_atom_t) + (data->last - data->pos);
|
|
2133 trak->size += atom_size;
|
|
2134
|
|
2135 atom = trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf;
|
|
2136 ctts_atom = (ngx_mp4_ctts_atom_t *) atom->pos;
|
|
2137
|
|
2138 ngx_mp4_set_32value(ctts_atom->size, atom_size);
|
|
2139 ngx_mp4_set_32value(ctts_atom->entries, entries);
|
|
2140
|
|
2141 return;
|
|
2142 }
|
|
2143
|
|
2144
|
|
2145 typedef struct {
|
|
2146 u_char size[4];
|
|
2147 u_char name[4];
|
|
2148 u_char version[1];
|
|
2149 u_char flags[3];
|
|
2150 u_char entries[4];
|
|
2151 } ngx_mp4_stsc_atom_t;
|
|
2152
|
|
2153 typedef struct {
|
|
2154 u_char chunk[4];
|
|
2155 u_char samples[4];
|
|
2156 u_char id[4];
|
|
2157 } ngx_mp4_stsc_entry_t;
|
|
2158
|
|
2159
|
|
2160 static ngx_int_t
|
|
2161 ngx_http_mp4_read_stsc_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
2162 {
|
|
2163 u_char *atom_header, *atom_table, *atom_end;
|
|
2164 uint32_t entries;
|
|
2165 ngx_buf_t *atom, *data;
|
|
2166 ngx_mp4_stsc_atom_t *stsc_atom;
|
|
2167 ngx_http_mp4_trak_t *trak;
|
|
2168
|
|
2169 /* sample-to-chunk atom */
|
|
2170
|
|
2171 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stsc atom");
|
|
2172
|
|
2173 atom_header = ngx_mp4_atom_header(mp4);
|
|
2174 stsc_atom = (ngx_mp4_stsc_atom_t *) atom_header;
|
|
2175 ngx_mp4_set_atom_name(stsc_atom, 's', 't', 's', 'c');
|
|
2176
|
|
2177 entries = ngx_mp4_get_32value(stsc_atom->entries);
|
|
2178
|
|
2179 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
2180 "sample-to-chunk entries:%ui", entries);
|
|
2181
|
|
2182 atom_table = atom_header + sizeof(ngx_mp4_stsc_atom_t);
|
|
2183 atom_end = atom_table + entries * sizeof(ngx_mp4_stsc_entry_t);
|
|
2184
|
|
2185 if ((uint64_t) (atom_end - stsc_atom->version) > atom_data_size) {
|
|
2186 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
2187 "\"%s\" mp4 stsc atom too large",
|
|
2188 mp4->file.name.data);
|
|
2189 return NGX_ERROR;
|
|
2190 }
|
|
2191
|
|
2192 trak = ngx_mp4_last_trak(mp4);
|
|
2193 trak->sample_to_chunk_entries = entries;
|
|
2194
|
|
2195 atom = &trak->stsc_atom_buf;
|
|
2196 atom->temporary = 1;
|
|
2197 atom->pos = atom_header;
|
|
2198 atom->last = atom_table;
|
|
2199
|
|
2200 data = &trak->stsc_data_buf;
|
|
2201 data->temporary = 1;
|
|
2202 data->pos = atom_table;
|
|
2203 data->last = atom_end;
|
|
2204
|
|
2205 trak->out[NGX_HTTP_MP4_STSC_ATOM].buf = atom;
|
|
2206 trak->out[NGX_HTTP_MP4_STSC_DATA].buf = data;
|
|
2207
|
|
2208 ngx_mp4_atom_next(mp4, atom_data_size);
|
|
2209
|
|
2210 return NGX_OK;
|
|
2211 }
|
|
2212
|
|
2213
|
|
2214 static ngx_int_t
|
|
2215 ngx_http_mp4_update_stsc_atom(ngx_http_mp4_file_t *mp4,
|
|
2216 ngx_http_mp4_trak_t *trak)
|
|
2217 {
|
|
2218 size_t atom_size;
|
|
2219 uint32_t entries, chunk, samples, prev_chunk, prev_samples;
|
|
2220 ngx_buf_t *atom, *data;
|
|
2221 ngx_mp4_stsc_atom_t *stsc_atom;
|
|
2222 ngx_mp4_stsc_entry_t *entry, *end;
|
|
2223
|
|
2224 /*
|
|
2225 * mdia.minf.stbl.stsc updating requires trak->start_sample
|
|
2226 * from mdia.minf.stbl.stts which depends on value from mdia.mdhd
|
|
2227 * atom which may reside after mdia.minf
|
|
2228 */
|
|
2229
|
|
2230 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
2231 "mp4 stsc atom update");
|
|
2232
|
|
2233 data = trak->out[NGX_HTTP_MP4_STSC_DATA].buf;
|
|
2234
|
|
2235 if (data == NULL) {
|
|
2236 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
2237 "no mp4 stsc atoms were found in \"%s\"",
|
|
2238 mp4->file.name.data);
|
|
2239 return NGX_ERROR;
|
|
2240 }
|
|
2241
|
|
2242 chunk = 0;
|
|
2243 samples = 1;
|
|
2244 prev_chunk = 0;
|
|
2245 prev_samples = 0;
|
|
2246
|
|
2247 entries = trak->sample_to_chunk_entries;
|
|
2248 entry = (ngx_mp4_stsc_entry_t *) data->pos;
|
|
2249 end = (ngx_mp4_stsc_entry_t *) data->last;
|
|
2250
|
|
2251 while (entry < end) {
|
|
2252
|
|
2253 chunk = ngx_mp4_get_32value(entry->chunk);
|
|
2254 samples = ngx_mp4_get_32value(entry->samples);
|
|
2255
|
|
2256 trak->start_sample -= (chunk - prev_chunk) * prev_samples;
|
|
2257
|
|
2258 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
2259 "chunk:%ui, samples:%ui, id:%ui",
|
|
2260 chunk, samples, ngx_mp4_get_32value(entry->id));
|
|
2261
|
|
2262 if (trak->start_sample < samples) {
|
|
2263 goto found;
|
|
2264 }
|
|
2265
|
|
2266 prev_chunk = chunk;
|
|
2267 prev_samples = samples;
|
|
2268 entries--;
|
|
2269 entry++;
|
|
2270 }
|
|
2271
|
|
2272 entries++;
|
|
2273 entry--;
|
|
2274
|
|
2275 found:
|
|
2276
|
|
2277 data->pos = (u_char *) entry;
|
|
2278
|
|
2279 chunk = trak->start_sample / samples;
|
|
2280 trak->start_chunk = chunk;
|
|
2281 atom_size = sizeof(ngx_mp4_stsc_atom_t) + (data->last - data->pos);
|
|
2282 trak->size += atom_size;
|
|
2283
|
|
2284 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
2285 "start chunk:%ui", chunk);
|
|
2286
|
|
2287 atom = trak->out[NGX_HTTP_MP4_STSC_ATOM].buf;
|
|
2288 stsc_atom = (ngx_mp4_stsc_atom_t *) atom->pos;
|
|
2289
|
|
2290 ngx_mp4_set_32value(stsc_atom->size, atom_size);
|
|
2291 ngx_mp4_set_32value(stsc_atom->entries, entries);
|
|
2292
|
|
2293 return NGX_OK;
|
|
2294 }
|
|
2295
|
|
2296
|
|
2297 typedef struct {
|
|
2298 u_char size[4];
|
|
2299 u_char name[4];
|
|
2300 u_char version[1];
|
|
2301 u_char flags[3];
|
|
2302 u_char uniform_size[4];
|
|
2303 u_char entries[4];
|
|
2304 } ngx_mp4_stsz_atom_t;
|
|
2305
|
|
2306
|
|
2307 static ngx_int_t
|
|
2308 ngx_http_mp4_read_stsz_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
2309 {
|
|
2310 u_char *atom_header, *atom_table, *atom_end;
|
|
2311 size_t atom_size;
|
|
2312 uint32_t entries, size;
|
|
2313 ngx_buf_t *atom, *data;
|
|
2314 ngx_mp4_stsz_atom_t *stsz_atom;
|
|
2315 ngx_http_mp4_trak_t *trak;
|
|
2316
|
|
2317 /* sample sizes atom */
|
|
2318
|
|
2319 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stsz atom");
|
|
2320
|
|
2321 atom_header = ngx_mp4_atom_header(mp4);
|
|
2322 stsz_atom = (ngx_mp4_stsz_atom_t *) atom_header;
|
|
2323 ngx_mp4_set_atom_name(stsz_atom, 's', 't', 's', 'z');
|
|
2324
|
|
2325 size = ngx_mp4_get_32value(stsz_atom->uniform_size);
|
|
2326 entries = ngx_mp4_get_32value(stsz_atom->entries);
|
|
2327
|
|
2328 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
2329 "sample uniform size:%uD, entries:%uD", size, entries);
|
|
2330
|
|
2331 trak = ngx_mp4_last_trak(mp4);
|
|
2332 trak->sample_sizes_entries = entries;
|
|
2333
|
|
2334 atom_table = atom_header + sizeof(ngx_mp4_stsz_atom_t);
|
|
2335
|
|
2336 atom = &trak->stsz_atom_buf;
|
|
2337 atom->temporary = 1;
|
|
2338 atom->pos = atom_header;
|
|
2339 atom->last = atom_table;
|
|
2340
|
|
2341 trak->out[NGX_HTTP_MP4_STSZ_ATOM].buf = atom;
|
|
2342
|
|
2343 if (size == 0) {
|
|
2344 atom_end = atom_table + entries * sizeof(uint32_t);
|
|
2345
|
|
2346 if ((uint64_t) (atom_end - stsz_atom->version) > atom_data_size) {
|
|
2347 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
2348 "\"%s\" mp4 stsz atom too large",
|
|
2349 mp4->file.name.data);
|
|
2350 return NGX_ERROR;
|
|
2351 }
|
|
2352
|
|
2353 data = &trak->stsz_data_buf;
|
|
2354 data->temporary = 1;
|
|
2355 data->pos = atom_table;
|
|
2356 data->last = atom_end;
|
|
2357
|
|
2358 trak->out[NGX_HTTP_MP4_STSZ_DATA].buf = data;
|
|
2359
|
|
2360 } else {
|
|
2361 /* if size != 0 then all samples are the same size */
|
|
2362 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
|
|
2363 ngx_mp4_set_32value(atom_header, atom_size);
|
|
2364 trak->size += atom_size;
|
|
2365 }
|
|
2366
|
|
2367 ngx_mp4_atom_next(mp4, atom_data_size);
|
|
2368
|
|
2369 return NGX_OK;
|
|
2370 }
|
|
2371
|
|
2372
|
|
2373 static void
|
|
2374 ngx_http_mp4_update_stsz_atom(ngx_http_mp4_file_t *mp4,
|
|
2375 ngx_http_mp4_trak_t *trak)
|
|
2376 {
|
|
2377 size_t atom_size;
|
|
2378 ngx_buf_t *atom, *data;
|
|
2379 ngx_mp4_stsz_atom_t *stsz_atom;
|
|
2380
|
|
2381 /*
|
|
2382 * mdia.minf.stbl.stsz updating requires trak->start_sample
|
|
2383 * from mdia.minf.stbl.stts which depends on value from mdia.mdhd
|
|
2384 * atom which may reside after mdia.minf
|
|
2385 */
|
|
2386
|
|
2387 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
2388 "mp4 stsz atom update");
|
|
2389
|
|
2390 data = trak->out[NGX_HTTP_MP4_STSZ_DATA].buf;
|
|
2391
|
|
2392 if (data) {
|
|
2393 data->pos += trak->start_sample * sizeof(uint32_t);
|
|
2394 atom_size = sizeof(ngx_mp4_stsz_atom_t) + (data->last - data->pos);
|
|
2395 trak->size += atom_size;
|
|
2396
|
|
2397 atom = trak->out[NGX_HTTP_MP4_STSZ_ATOM].buf;
|
|
2398 stsz_atom = (ngx_mp4_stsz_atom_t *) atom->pos;
|
|
2399
|
|
2400 ngx_mp4_set_32value(stsz_atom->size, atom_size);
|
|
2401 ngx_mp4_set_32value(stsz_atom->entries,
|
|
2402 trak->sample_sizes_entries - trak->start_sample);
|
|
2403 }
|
|
2404 }
|
|
2405
|
|
2406
|
|
2407 typedef struct {
|
|
2408 u_char size[4];
|
|
2409 u_char name[4];
|
|
2410 u_char version[1];
|
|
2411 u_char flags[3];
|
|
2412 u_char entries[4];
|
|
2413 } ngx_mp4_stco_atom_t;
|
|
2414
|
|
2415
|
|
2416 static ngx_int_t
|
|
2417 ngx_http_mp4_read_stco_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
|
|
2418 {
|
|
2419 u_char *atom_header, *atom_table, *atom_end;
|
|
2420 uint32_t entries;
|
|
2421 ngx_buf_t *atom, *data;
|
|
2422 ngx_mp4_stco_atom_t *stco_atom;
|
|
2423 ngx_http_mp4_trak_t *trak;
|
|
2424
|
|
2425 /* chunk offsets atom */
|
|
2426
|
|
2427 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stco atom");
|
|
2428
|
|
2429 atom_header = ngx_mp4_atom_header(mp4);
|
|
2430 stco_atom = (ngx_mp4_stco_atom_t *) atom_header;
|
|
2431 ngx_mp4_set_atom_name(stco_atom, 's', 't', 'c', 'o');
|
|
2432
|
|
2433 entries = ngx_mp4_get_32value(stco_atom->entries);
|
|
2434
|
|
2435 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "chunks:%uD", entries);
|
|
2436
|
|
2437 atom_table = atom_header + sizeof(ngx_mp4_stco_atom_t);
|
|
2438 atom_end = atom_table + entries * sizeof(uint32_t);
|
|
2439
|
|
2440 if ((uint64_t) (atom_end - stco_atom->version) > atom_data_size) {
|
|
2441 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
2442 "\"%s\" mp4 stco atom too large", mp4->file.name.data);
|
|
2443 return NGX_ERROR;
|
|
2444 }
|
|
2445
|
|
2446 trak = ngx_mp4_last_trak(mp4);
|
|
2447 trak->chunks = entries;
|
|
2448
|
|
2449 atom = &trak->tsco_atom_buf;
|
|
2450 atom->temporary = 1;
|
|
2451 atom->pos = atom_header;
|
|
2452 atom->last = atom_table;
|
|
2453
|
|
2454 data = &trak->tsco_data_buf;
|
|
2455 data->temporary = 1;
|
|
2456 data->pos = atom_table;
|
|
2457 data->last = atom_end;
|
|
2458
|
|
2459 trak->out[NGX_HTTP_MP4_STCO_ATOM].buf = atom;
|
|
2460 trak->out[NGX_HTTP_MP4_STCO_DATA].buf = data;
|
|
2461
|
|
2462 ngx_mp4_atom_next(mp4, atom_data_size);
|
|
2463
|
|
2464 return NGX_OK;
|
|
2465 }
|
|
2466
|
|
2467
|
|
2468 static ngx_int_t
|
|
2469 ngx_http_mp4_update_stco_atom(ngx_http_mp4_file_t *mp4,
|
|
2470 ngx_http_mp4_trak_t *trak)
|
|
2471 {
|
|
2472 size_t atom_size;
|
|
2473 uint32_t start_chunk;
|
|
2474 ngx_buf_t *atom, *data;
|
|
2475 ngx_mp4_stco_atom_t *stco_atom;
|
|
2476
|
|
2477 /*
|
|
2478 * mdia.minf.stbl.stco updating requires trak->start_chunk
|
|
2479 * from mdia.minf.stbl.stsc which depends on value from mdia.mdhd
|
|
2480 * atom which may reside after mdia.minf
|
|
2481 */
|
|
2482
|
|
2483 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
2484 "mp4 stco atom update");
|
|
2485
|
|
2486 if (trak->start_chunk > trak->chunks) {
|
|
2487 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
2488 "start time is out mp4 stsc chunks in \"%\"",
|
|
2489 mp4->file.name.data);
|
|
2490 return NGX_ERROR;
|
|
2491 }
|
|
2492
|
|
2493 data = trak->out[NGX_HTTP_MP4_STCO_DATA].buf;
|
|
2494
|
|
2495 if (data == NULL) {
|
|
2496 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
|
|
2497 "no mp4 stco atoms were found in \"%s\"",
|
|
2498 mp4->file.name.data);
|
|
2499 return NGX_ERROR;
|
|
2500 }
|
|
2501
|
|
2502 start_chunk = trak->start_chunk;
|
|
2503
|
|
2504 data->pos += start_chunk * sizeof(uint32_t);
|
|
2505 atom_size = sizeof(ngx_mp4_stco_atom_t) + (data->last - data->pos);
|
|
2506 trak->size += atom_size;
|
|
2507
|
|
2508 trak->start_offset = ngx_mp4_get_32value(data->pos);
|
|
2509
|
|
2510 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
2511 "start chunk offset:%uD", trak->start_offset);
|
|
2512
|
|
2513 atom = trak->out[NGX_HTTP_MP4_STCO_ATOM].buf;
|
|
2514 stco_atom = (ngx_mp4_stco_atom_t *) atom->pos;
|
|
2515
|
|
2516 ngx_mp4_set_32value(stco_atom->size, atom_size);
|
|
2517 ngx_mp4_set_32value(stco_atom->entries, trak->chunks - start_chunk);
|
|
2518
|
|
2519 return NGX_OK;
|
|
2520 }
|
|
2521
|
|
2522
|
|
2523 static void
|
|
2524 ngx_http_mp4_adjust_stco_atom(ngx_http_mp4_file_t *mp4,
|
|
2525 ngx_http_mp4_trak_t *trak, int32_t adjustment)
|
|
2526 {
|
|
2527 uint32_t offset, *entry, *end;
|
|
2528 ngx_buf_t *data;
|
|
2529
|
|
2530 /*
|
|
2531 * moov.trak.mdia.minf.stbl.stco adjustment requires
|
|
2532 * minimal start offset of all traks and new moov atom size
|
|
2533 */
|
|
2534
|
|
2535 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
|
|
2536 "mp4 stco atom adjustment");
|
|
2537
|
|
2538 data = trak->out[NGX_HTTP_MP4_STCO_DATA].buf;
|
|
2539 entry = (uint32_t *) data->pos;
|
|
2540 end = (uint32_t *) data->last;
|
|
2541
|
|
2542 while (entry < end) {
|
|
2543 offset = ngx_mp4_get_32value(entry);
|
|
2544 offset += adjustment;
|
|
2545 ngx_mp4_set_32value(entry, offset);
|
|
2546 entry++;
|
|
2547 }
|
|
2548 }
|
|
2549
|
|
2550
|
|
2551 static char *
|
|
2552 ngx_http_mp4(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
2553 {
|
|
2554 ngx_http_core_loc_conf_t *clcf;
|
|
2555
|
|
2556 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
2557 clcf->handler = ngx_http_mp4_handler;
|
|
2558
|
|
2559 return NGX_CONF_OK;
|
|
2560 }
|
|
2561
|
|
2562
|
|
2563 static void *
|
|
2564 ngx_http_mp4_create_conf(ngx_conf_t *cf)
|
|
2565 {
|
|
2566 ngx_http_mp4_conf_t *conf;
|
|
2567
|
|
2568 conf = ngx_palloc(cf->pool, sizeof(ngx_http_mp4_conf_t));
|
|
2569 if (conf == NULL) {
|
|
2570 return NULL;
|
|
2571 }
|
|
2572
|
|
2573 conf->buffer_size = NGX_CONF_UNSET_SIZE;
|
|
2574 conf->max_moov_size = NGX_CONF_UNSET_SIZE;
|
|
2575
|
|
2576 return conf;
|
|
2577 }
|
|
2578
|
|
2579
|
|
2580 static char *
|
|
2581 ngx_http_mp4_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
2582 {
|
|
2583 ngx_http_mp4_conf_t *prev = parent;
|
|
2584 ngx_http_mp4_conf_t *conf = child;
|
|
2585
|
|
2586 ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size, 512 * 1024);
|
|
2587 ngx_conf_merge_size_value(conf->max_moov_size, prev->max_moov_size,
|
|
2588 10 * 1024 * 1024);
|
|
2589
|
|
2590 return NGX_CONF_OK;
|
|
2591 }
|