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