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