Mercurial > hg > nginx
annotate src/http/modules/ngx_http_mp4_module.c @ 4098:a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 14 Sep 2011 05:16:20 +0000 |
parents | f9a1646b9611 |
children | 9ee6944590c0 |
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 | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
11 #define NGX_HTTP_MP4_TRAK_ATOM 0 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
12 #define NGX_HTTP_MP4_TKHD_ATOM 1 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
13 #define NGX_HTTP_MP4_MDIA_ATOM 2 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
14 #define NGX_HTTP_MP4_MDHD_ATOM 3 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
15 #define NGX_HTTP_MP4_HDLR_ATOM 4 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
16 #define NGX_HTTP_MP4_MINF_ATOM 5 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
17 #define NGX_HTTP_MP4_VMHD_ATOM 6 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
18 #define NGX_HTTP_MP4_SMHD_ATOM 7 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
19 #define NGX_HTTP_MP4_DINF_ATOM 8 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
20 #define NGX_HTTP_MP4_STBL_ATOM 9 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
21 #define NGX_HTTP_MP4_STSD_ATOM 10 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
22 #define NGX_HTTP_MP4_STTS_ATOM 11 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
23 #define NGX_HTTP_MP4_STTS_DATA 12 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
24 #define NGX_HTTP_MP4_STSS_ATOM 13 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
25 #define NGX_HTTP_MP4_STSS_DATA 14 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
26 #define NGX_HTTP_MP4_CTTS_ATOM 15 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
27 #define NGX_HTTP_MP4_CTTS_DATA 16 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
28 #define NGX_HTTP_MP4_STSC_ATOM 17 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
29 #define NGX_HTTP_MP4_STSC_CHUNK 18 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
30 #define NGX_HTTP_MP4_STSC_DATA 19 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
31 #define NGX_HTTP_MP4_STSZ_ATOM 20 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
32 #define NGX_HTTP_MP4_STSZ_DATA 21 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
33 #define NGX_HTTP_MP4_STCO_ATOM 22 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
34 #define NGX_HTTP_MP4_STCO_DATA 23 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
35 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
36 #define NGX_HTTP_MP4_LAST_ATOM NGX_HTTP_MP4_STCO_DATA |
4085 | 37 |
38 | |
39 typedef struct { | |
40 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
|
41 size_t max_buffer_size; |
4085 | 42 } ngx_http_mp4_conf_t; |
43 | |
44 | |
45 typedef struct { | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
46 u_char chunk[4]; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
47 u_char samples[4]; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
48 u_char id[4]; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
49 } ngx_mp4_stsc_entry_t; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
50 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
51 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
52 typedef struct { |
4085 | 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; | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
63 ngx_uint_t chunk_samples; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
64 ngx_uint_t chunk_samples_size; |
4085 | 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; | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
95 ngx_buf_t stsc_chunk_buf; |
4085 | 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; | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
101 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
102 ngx_mp4_stsc_entry_t stsc_chunk_entry; |
4085 | 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 | |
4089
e27670e1ab70
mp4_max_moov_size directive has been renamed to mp4_max_buffer_size.
Igor Sysoev <igor@sysoev.ru>
parents:
4088
diff
changeset
|
291 { ngx_string("mp4_max_buffer_size"), |
4085 | 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, | |
4089
e27670e1ab70
mp4_max_moov_size directive has been renamed to mp4_max_buffer_size.
Igor Sysoev <igor@sysoev.ru>
parents:
4088
diff
changeset
|
295 offsetof(ngx_http_mp4_conf_t, max_buffer_size), |
4085 | 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; | |
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
|
426 of.directio = NGX_MAX_OFF_T_VALUE; |
4085 | 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 | |
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
|
551 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
|
552 |
4085 | 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 | |
4088
8fe1da7b8386
bugfix of r4086: nginx could not be built without debug log.
Igor Sysoev <igor@sysoev.ru>
parents:
4087
diff
changeset
|
720 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, |
4085 | 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 | |
4088
8fe1da7b8386
bugfix of r4086: nginx could not be built without debug log.
Igor Sysoev <igor@sysoev.ru>
parents:
4087
diff
changeset
|
810 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, |
4085 | 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 | |
4089
e27670e1ab70
mp4_max_moov_size directive has been renamed to mp4_max_buffer_size.
Igor Sysoev <igor@sysoev.ru>
parents:
4088
diff
changeset
|
953 if (atom_data_size > conf->max_buffer_size) { |
4085 | 954 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
955 "\"%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
|
956 "you may want to increase mp4_max_buffer_size", |
4085 | 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 | |
4088
8fe1da7b8386
bugfix of r4086: nginx could not be built without debug log.
Igor Sysoev <igor@sysoev.ru>
parents:
4087
diff
changeset
|
1042 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, |
4085 | 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; | |
1182 ngx_buf_t *atom; | |
1183 ngx_http_mp4_trak_t *trak; | |
1184 | |
1185 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 trak atom"); | |
1186 | |
1187 trak = ngx_array_push(&mp4->trak); | |
1188 if (trak == NULL) { | |
1189 return NGX_ERROR; | |
1190 } | |
1191 | |
1192 ngx_memzero(trak, sizeof(ngx_http_mp4_trak_t)); | |
1193 | |
1194 atom_header = ngx_mp4_atom_header(mp4); | |
1195 ngx_mp4_set_atom_name(atom_header, 't', 'r', 'a', 'k'); | |
1196 | |
1197 atom = &trak->trak_atom_buf; | |
1198 atom->temporary = 1; | |
1199 atom->pos = atom_header; | |
1200 atom->last = atom_header + sizeof(ngx_mp4_atom_header_t); | |
1201 | |
1202 trak->out[NGX_HTTP_MP4_TRAK_ATOM].buf = atom; | |
1203 | |
1204 return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_trak_atoms, atom_data_size); | |
1205 } | |
1206 | |
1207 | |
1208 static void | |
1209 ngx_http_mp4_update_trak_atom(ngx_http_mp4_file_t *mp4, | |
1210 ngx_http_mp4_trak_t *trak) | |
1211 { | |
1212 ngx_buf_t *atom; | |
1213 | |
1214 trak->size += sizeof(ngx_mp4_atom_header_t); | |
1215 atom = &trak->trak_atom_buf; | |
1216 ngx_mp4_set_32value(atom->pos, trak->size); | |
1217 } | |
1218 | |
1219 | |
1220 static ngx_int_t | |
1221 ngx_http_mp4_read_cmov_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1222 { | |
1223 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
1224 "\"%s\" mp4 compressed moov atom (cmov) is not supported", | |
1225 mp4->file.name.data); | |
1226 | |
1227 return NGX_ERROR; | |
1228 } | |
1229 | |
1230 | |
1231 typedef struct { | |
1232 u_char size[4]; | |
1233 u_char name[4]; | |
1234 u_char version[1]; | |
1235 u_char flags[3]; | |
1236 u_char creation_time[4]; | |
1237 u_char modification_time[4]; | |
1238 u_char track_id[4]; | |
1239 u_char reserved1[4]; | |
1240 u_char duration[4]; | |
1241 u_char reserved2[8]; | |
1242 u_char layer[2]; | |
1243 u_char group[2]; | |
1244 u_char volume[2]; | |
1245 u_char reverved3[2]; | |
1246 u_char matrix[36]; | |
1247 u_char width[4]; | |
1248 u_char heigth[4]; | |
1249 } ngx_mp4_tkhd_atom_t; | |
1250 | |
1251 typedef struct { | |
1252 u_char size[4]; | |
1253 u_char name[4]; | |
1254 u_char version[1]; | |
1255 u_char flags[3]; | |
1256 u_char creation_time[8]; | |
1257 u_char modification_time[8]; | |
1258 u_char track_id[4]; | |
1259 u_char reserved1[4]; | |
1260 u_char duration[8]; | |
1261 u_char reserved2[8]; | |
1262 u_char layer[2]; | |
1263 u_char group[2]; | |
1264 u_char volume[2]; | |
1265 u_char reverved3[2]; | |
1266 u_char matrix[36]; | |
1267 u_char width[4]; | |
1268 u_char heigth[4]; | |
1269 } ngx_mp4_tkhd64_atom_t; | |
1270 | |
1271 | |
1272 static ngx_int_t | |
1273 ngx_http_mp4_read_tkhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1274 { | |
1275 u_char *atom_header; | |
1276 size_t atom_size; | |
1277 uint64_t duration; | |
1278 ngx_buf_t *atom; | |
1279 ngx_http_mp4_trak_t *trak; | |
1280 ngx_mp4_tkhd_atom_t *tkhd_atom; | |
1281 ngx_mp4_tkhd64_atom_t *tkhd64_atom; | |
1282 | |
1283 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 tkhd atom"); | |
1284 | |
1285 atom_header = ngx_mp4_atom_header(mp4); | |
1286 tkhd_atom = (ngx_mp4_tkhd_atom_t *) atom_header; | |
1287 tkhd64_atom = (ngx_mp4_tkhd64_atom_t *) atom_header; | |
1288 ngx_mp4_set_atom_name(tkhd_atom, 't', 'k', 'h', 'd'); | |
1289 | |
1290 if (tkhd_atom->version[0] == 0) { | |
1291 /* version 0: 32-bit duration */ | |
1292 duration = ngx_mp4_get_32value(tkhd_atom->duration); | |
1293 | |
1294 } else { | |
1295 /* version 1: 64-bit duration */ | |
1296 duration = ngx_mp4_get_64value(tkhd64_atom->duration); | |
1297 } | |
1298 | |
1299 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1300 "tkhd duration:%uL, time:%.3fs", | |
1301 duration, (double) duration / mp4->timescale); | |
1302 | |
1303 duration -= (uint64_t) mp4->start * mp4->timescale / 1000; | |
1304 | |
1305 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1306 "tkhd new duration:%uL, time:%.3fs", | |
1307 duration, (double) duration / mp4->timescale); | |
1308 | |
1309 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; | |
1310 | |
1311 trak = ngx_mp4_last_trak(mp4); | |
1312 trak->tkhd_size = atom_size; | |
1313 | |
1314 ngx_mp4_set_32value(tkhd_atom->size, atom_size); | |
1315 | |
1316 if (tkhd_atom->version[0] == 0) { | |
1317 ngx_mp4_set_32value(tkhd_atom->duration, duration); | |
1318 | |
1319 } else { | |
1320 ngx_mp4_set_64value(tkhd64_atom->duration, duration); | |
1321 } | |
1322 | |
1323 atom = &trak->tkhd_atom_buf; | |
1324 atom->temporary = 1; | |
1325 atom->pos = atom_header; | |
1326 atom->last = atom_header + atom_size; | |
1327 | |
1328 trak->out[NGX_HTTP_MP4_TKHD_ATOM].buf = atom; | |
1329 | |
1330 ngx_mp4_atom_next(mp4, atom_data_size); | |
1331 | |
1332 return NGX_OK; | |
1333 } | |
1334 | |
1335 | |
1336 static ngx_int_t | |
1337 ngx_http_mp4_read_mdia_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1338 { | |
1339 u_char *atom_header; | |
1340 ngx_buf_t *atom; | |
1341 ngx_http_mp4_trak_t *trak; | |
1342 | |
1343 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "process mdia atom"); | |
1344 | |
1345 atom_header = ngx_mp4_atom_header(mp4); | |
1346 ngx_mp4_set_atom_name(atom_header, 'm', 'd', 'i', 'a'); | |
1347 | |
1348 trak = ngx_mp4_last_trak(mp4); | |
1349 | |
1350 atom = &trak->mdia_atom_buf; | |
1351 atom->temporary = 1; | |
1352 atom->pos = atom_header; | |
1353 atom->last = atom_header + sizeof(ngx_mp4_atom_header_t); | |
1354 | |
1355 trak->out[NGX_HTTP_MP4_MDIA_ATOM].buf = atom; | |
1356 | |
1357 return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_mdia_atoms, atom_data_size); | |
1358 } | |
1359 | |
1360 | |
1361 static void | |
1362 ngx_http_mp4_update_mdia_atom(ngx_http_mp4_file_t *mp4, | |
1363 ngx_http_mp4_trak_t *trak) | |
1364 { | |
1365 ngx_buf_t *atom; | |
1366 | |
1367 trak->size += sizeof(ngx_mp4_atom_header_t); | |
1368 atom = &trak->mdia_atom_buf; | |
1369 ngx_mp4_set_32value(atom->pos, trak->size); | |
1370 } | |
1371 | |
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[4]; | |
1379 u_char modification_time[4]; | |
1380 u_char timescale[4]; | |
1381 u_char duration[4]; | |
1382 u_char language[2]; | |
1383 u_char quality[2]; | |
1384 } ngx_mp4_mdhd_atom_t; | |
1385 | |
1386 typedef struct { | |
1387 u_char size[4]; | |
1388 u_char name[4]; | |
1389 u_char version[1]; | |
1390 u_char flags[3]; | |
1391 u_char creation_time[8]; | |
1392 u_char modification_time[8]; | |
1393 u_char timescale[4]; | |
1394 u_char duration[8]; | |
1395 u_char language[2]; | |
1396 u_char quality[2]; | |
1397 } ngx_mp4_mdhd64_atom_t; | |
1398 | |
1399 | |
1400 static ngx_int_t | |
1401 ngx_http_mp4_read_mdhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1402 { | |
1403 u_char *atom_header; | |
1404 size_t atom_size; | |
1405 uint32_t timescale; | |
1406 uint64_t duration; | |
1407 ngx_buf_t *atom; | |
1408 ngx_http_mp4_trak_t *trak; | |
1409 ngx_mp4_mdhd_atom_t *mdhd_atom; | |
1410 ngx_mp4_mdhd64_atom_t *mdhd64_atom; | |
1411 | |
1412 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 mdhd atom"); | |
1413 | |
1414 atom_header = ngx_mp4_atom_header(mp4); | |
1415 mdhd_atom = (ngx_mp4_mdhd_atom_t *) atom_header; | |
1416 mdhd64_atom = (ngx_mp4_mdhd64_atom_t *) atom_header; | |
1417 ngx_mp4_set_atom_name(mdhd_atom, 'm', 'd', 'h', 'd'); | |
1418 | |
1419 if (mdhd_atom->version[0] == 0) { | |
1420 /* version 0: everything is 32-bit */ | |
1421 timescale = ngx_mp4_get_32value(mdhd_atom->timescale); | |
1422 duration = ngx_mp4_get_32value(mdhd_atom->duration); | |
1423 | |
1424 } else { | |
1425 /* version 1: 64-bit duration and 32-bit timescale */ | |
1426 timescale = ngx_mp4_get_32value(mdhd64_atom->timescale); | |
1427 duration = ngx_mp4_get_64value(mdhd64_atom->duration); | |
1428 } | |
1429 | |
1430 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1431 "mdhd timescale:%uD, duration:%uL, time:%.3fs", | |
1432 timescale, duration, (double) duration / timescale); | |
1433 | |
1434 duration -= (uint64_t) mp4->start * timescale / 1000; | |
1435 | |
1436 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1437 "mdhd new duration:%uL, time:%.3fs", | |
1438 duration, (double) duration / timescale); | |
1439 | |
1440 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; | |
1441 | |
1442 trak = ngx_mp4_last_trak(mp4); | |
1443 trak->mdhd_size = atom_size; | |
1444 trak->timescale = timescale; | |
1445 | |
1446 ngx_mp4_set_32value(mdhd_atom->size, atom_size); | |
1447 | |
1448 if (mdhd_atom->version[0] == 0) { | |
1449 ngx_mp4_set_32value(mdhd_atom->duration, duration); | |
1450 | |
1451 } else { | |
1452 ngx_mp4_set_64value(mdhd64_atom->duration, duration); | |
1453 } | |
1454 | |
1455 atom = &trak->mdhd_atom_buf; | |
1456 atom->temporary = 1; | |
1457 atom->pos = atom_header; | |
1458 atom->last = atom_header + atom_size; | |
1459 | |
1460 trak->out[NGX_HTTP_MP4_MDHD_ATOM].buf = atom; | |
1461 | |
1462 ngx_mp4_atom_next(mp4, atom_data_size); | |
1463 | |
1464 return NGX_OK; | |
1465 } | |
1466 | |
1467 | |
1468 static ngx_int_t | |
1469 ngx_http_mp4_read_hdlr_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1470 { | |
1471 u_char *atom_header; | |
1472 size_t atom_size; | |
1473 ngx_buf_t *atom; | |
1474 ngx_http_mp4_trak_t *trak; | |
1475 | |
1476 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 hdlr atom"); | |
1477 | |
1478 atom_header = ngx_mp4_atom_header(mp4); | |
1479 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; | |
1480 ngx_mp4_set_32value(atom_header, atom_size); | |
1481 ngx_mp4_set_atom_name(atom_header, 'h', 'd', 'l', 'r'); | |
1482 | |
1483 trak = ngx_mp4_last_trak(mp4); | |
1484 | |
1485 atom = &trak->hdlr_atom_buf; | |
1486 atom->temporary = 1; | |
1487 atom->pos = atom_header; | |
1488 atom->last = atom_header + atom_size; | |
1489 | |
1490 trak->hdlr_size = atom_size; | |
1491 trak->out[NGX_HTTP_MP4_HDLR_ATOM].buf = atom; | |
1492 | |
1493 ngx_mp4_atom_next(mp4, atom_data_size); | |
1494 | |
1495 return NGX_OK; | |
1496 } | |
1497 | |
1498 | |
1499 static ngx_int_t | |
1500 ngx_http_mp4_read_minf_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1501 { | |
1502 u_char *atom_header; | |
1503 ngx_buf_t *atom; | |
1504 ngx_http_mp4_trak_t *trak; | |
1505 | |
1506 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "process minf atom"); | |
1507 | |
1508 atom_header = ngx_mp4_atom_header(mp4); | |
1509 ngx_mp4_set_atom_name(atom_header, 'm', 'i', 'n', 'f'); | |
1510 | |
1511 trak = ngx_mp4_last_trak(mp4); | |
1512 | |
1513 atom = &trak->minf_atom_buf; | |
1514 atom->temporary = 1; | |
1515 atom->pos = atom_header; | |
1516 atom->last = atom_header + sizeof(ngx_mp4_atom_header_t); | |
1517 | |
1518 trak->out[NGX_HTTP_MP4_MINF_ATOM].buf = atom; | |
1519 | |
1520 return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_minf_atoms, atom_data_size); | |
1521 } | |
1522 | |
1523 | |
1524 static void | |
1525 ngx_http_mp4_update_minf_atom(ngx_http_mp4_file_t *mp4, | |
1526 ngx_http_mp4_trak_t *trak) | |
1527 { | |
1528 ngx_buf_t *atom; | |
1529 | |
1530 trak->size += sizeof(ngx_mp4_atom_header_t) | |
1531 + trak->vmhd_size | |
1532 + trak->smhd_size | |
1533 + trak->dinf_size; | |
1534 atom = &trak->minf_atom_buf; | |
1535 ngx_mp4_set_32value(atom->pos, trak->size); | |
1536 } | |
1537 | |
1538 | |
1539 static ngx_int_t | |
1540 ngx_http_mp4_read_vmhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1541 { | |
1542 u_char *atom_header; | |
1543 size_t atom_size; | |
1544 ngx_buf_t *atom; | |
1545 ngx_http_mp4_trak_t *trak; | |
1546 | |
1547 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 vmhd atom"); | |
1548 | |
1549 atom_header = ngx_mp4_atom_header(mp4); | |
1550 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; | |
1551 ngx_mp4_set_32value(atom_header, atom_size); | |
1552 ngx_mp4_set_atom_name(atom_header, 'v', 'm', 'h', 'd'); | |
1553 | |
1554 trak = ngx_mp4_last_trak(mp4); | |
1555 | |
1556 atom = &trak->vmhd_atom_buf; | |
1557 atom->temporary = 1; | |
1558 atom->pos = atom_header; | |
1559 atom->last = atom_header + atom_size; | |
1560 | |
1561 trak->vmhd_size += atom_size; | |
1562 trak->out[NGX_HTTP_MP4_VMHD_ATOM].buf = atom; | |
1563 | |
1564 ngx_mp4_atom_next(mp4, atom_data_size); | |
1565 | |
1566 return NGX_OK; | |
1567 } | |
1568 | |
1569 | |
1570 static ngx_int_t | |
1571 ngx_http_mp4_read_smhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1572 { | |
1573 u_char *atom_header; | |
1574 size_t atom_size; | |
1575 ngx_buf_t *atom; | |
1576 ngx_http_mp4_trak_t *trak; | |
1577 | |
1578 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 smhd atom"); | |
1579 | |
1580 atom_header = ngx_mp4_atom_header(mp4); | |
1581 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; | |
1582 ngx_mp4_set_32value(atom_header, atom_size); | |
1583 ngx_mp4_set_atom_name(atom_header, 's', 'm', 'h', 'd'); | |
1584 | |
1585 trak = ngx_mp4_last_trak(mp4); | |
1586 | |
1587 atom = &trak->smhd_atom_buf; | |
1588 atom->temporary = 1; | |
1589 atom->pos = atom_header; | |
1590 atom->last = atom_header + atom_size; | |
1591 | |
1592 trak->vmhd_size += atom_size; | |
1593 trak->out[NGX_HTTP_MP4_SMHD_ATOM].buf = atom; | |
1594 | |
1595 ngx_mp4_atom_next(mp4, atom_data_size); | |
1596 | |
1597 return NGX_OK; | |
1598 } | |
1599 | |
1600 | |
1601 static ngx_int_t | |
1602 ngx_http_mp4_read_dinf_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1603 { | |
1604 u_char *atom_header; | |
1605 size_t atom_size; | |
1606 ngx_buf_t *atom; | |
1607 ngx_http_mp4_trak_t *trak; | |
1608 | |
1609 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 dinf atom"); | |
1610 | |
1611 atom_header = ngx_mp4_atom_header(mp4); | |
1612 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; | |
1613 ngx_mp4_set_32value(atom_header, atom_size); | |
1614 ngx_mp4_set_atom_name(atom_header, 'd', 'i', 'n', 'f'); | |
1615 | |
1616 trak = ngx_mp4_last_trak(mp4); | |
1617 | |
1618 atom = &trak->dinf_atom_buf; | |
1619 atom->temporary = 1; | |
1620 atom->pos = atom_header; | |
1621 atom->last = atom_header + atom_size; | |
1622 | |
1623 trak->dinf_size += atom_size; | |
1624 trak->out[NGX_HTTP_MP4_DINF_ATOM].buf = atom; | |
1625 | |
1626 ngx_mp4_atom_next(mp4, atom_data_size); | |
1627 | |
1628 return NGX_OK; | |
1629 } | |
1630 | |
1631 | |
1632 static ngx_int_t | |
1633 ngx_http_mp4_read_stbl_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1634 { | |
1635 u_char *atom_header; | |
1636 ngx_buf_t *atom; | |
1637 ngx_http_mp4_trak_t *trak; | |
1638 | |
1639 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "process stbl atom"); | |
1640 | |
1641 atom_header = ngx_mp4_atom_header(mp4); | |
1642 ngx_mp4_set_atom_name(atom_header, 's', 't', 'b', 'l'); | |
1643 | |
1644 trak = ngx_mp4_last_trak(mp4); | |
1645 | |
1646 atom = &trak->stbl_atom_buf; | |
1647 atom->temporary = 1; | |
1648 atom->pos = atom_header; | |
1649 atom->last = atom_header + sizeof(ngx_mp4_atom_header_t); | |
1650 | |
1651 trak->out[NGX_HTTP_MP4_STBL_ATOM].buf = atom; | |
1652 | |
1653 return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_stbl_atoms, atom_data_size); | |
1654 } | |
1655 | |
1656 | |
1657 static void | |
1658 ngx_http_mp4_update_stbl_atom(ngx_http_mp4_file_t *mp4, | |
1659 ngx_http_mp4_trak_t *trak) | |
1660 { | |
1661 ngx_buf_t *atom; | |
1662 | |
1663 trak->size += sizeof(ngx_mp4_atom_header_t); | |
1664 atom = &trak->stbl_atom_buf; | |
1665 ngx_mp4_set_32value(atom->pos, trak->size); | |
1666 } | |
1667 | |
1668 | |
1669 typedef struct { | |
1670 u_char size[4]; | |
1671 u_char name[4]; | |
1672 u_char version[1]; | |
1673 u_char flags[3]; | |
1674 u_char entries[4]; | |
1675 } ngx_mp4_stsd_atom_t; | |
1676 | |
1677 | |
1678 static ngx_int_t | |
1679 ngx_http_mp4_read_stsd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1680 { | |
1681 u_char *atom_header, *atom_table; | |
1682 size_t atom_size; | |
1683 ngx_buf_t *atom; | |
1684 ngx_mp4_stsd_atom_t *stsd_atom; | |
1685 ngx_http_mp4_trak_t *trak; | |
1686 | |
1687 /* sample description atom */ | |
1688 | |
1689 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stsd atom"); | |
1690 | |
1691 atom_header = ngx_mp4_atom_header(mp4); | |
1692 stsd_atom = (ngx_mp4_stsd_atom_t *) atom_header; | |
1693 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; | |
1694 atom_table = atom_header + atom_size; | |
1695 ngx_mp4_set_32value(stsd_atom->size, atom_size); | |
1696 ngx_mp4_set_atom_name(stsd_atom, 's', 't', 's', 'd'); | |
1697 | |
1698 #if (NGX_DEBUG) | |
1699 { | |
1700 uint32_t entries; | |
1701 | |
1702 entries = ngx_mp4_get_32value(stsd_atom->entries); | |
1703 | |
1704 if (entries) { | |
1705 /* codecs of interest: avc1 (H.264), mp4a (MPEG-4 audio) */ | |
1706 | |
1707 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
4096 | 1708 "stsd entries:%uD, codec:%*s", |
4095 | 1709 entries, 4, |
1710 atom_header + sizeof(ngx_mp4_stsd_atom_t) + 4); | |
1711 | |
4085 | 1712 } |
1713 } | |
1714 #endif | |
1715 | |
1716 trak = ngx_mp4_last_trak(mp4); | |
1717 | |
1718 atom = &trak->stsd_atom_buf; | |
1719 atom->temporary = 1; | |
1720 atom->pos = atom_header; | |
1721 atom->last = atom_table; | |
1722 | |
1723 trak->out[NGX_HTTP_MP4_STSD_ATOM].buf = atom; | |
1724 trak->size += atom_size; | |
1725 | |
1726 ngx_mp4_atom_next(mp4, atom_data_size); | |
1727 | |
1728 return NGX_OK; | |
1729 } | |
1730 | |
1731 | |
1732 typedef struct { | |
1733 u_char size[4]; | |
1734 u_char name[4]; | |
1735 u_char version[1]; | |
1736 u_char flags[3]; | |
1737 u_char entries[4]; | |
1738 } ngx_mp4_stts_atom_t; | |
1739 | |
1740 typedef struct { | |
1741 u_char count[4]; | |
1742 u_char duration[4]; | |
1743 } ngx_mp4_stts_entry_t; | |
1744 | |
1745 | |
1746 static ngx_int_t | |
1747 ngx_http_mp4_read_stts_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1748 { | |
1749 u_char *atom_header, *atom_table, *atom_end; | |
1750 uint32_t entries; | |
1751 ngx_buf_t *atom, *data; | |
1752 ngx_mp4_stts_atom_t *stts_atom; | |
1753 ngx_http_mp4_trak_t *trak; | |
1754 | |
1755 /* time-to-sample atom */ | |
1756 | |
1757 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stts atom"); | |
1758 | |
1759 atom_header = ngx_mp4_atom_header(mp4); | |
1760 stts_atom = (ngx_mp4_stts_atom_t *) atom_header; | |
1761 ngx_mp4_set_atom_name(stts_atom, 's', 't', 't', 's'); | |
1762 | |
1763 entries = ngx_mp4_get_32value(stts_atom->entries); | |
1764 | |
1765 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
4096 | 1766 "mp4 time-to-sample entries:%uD", entries); |
4085 | 1767 |
1768 atom_table = atom_header + sizeof(ngx_mp4_stts_atom_t); | |
1769 atom_end = atom_table + entries * sizeof(ngx_mp4_stts_entry_t); | |
1770 | |
1771 if ((uint64_t) (atom_end - stts_atom->version) > atom_data_size) { | |
1772 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
1773 "\"%s\" mp4 stts atom too large", | |
1774 mp4->file.name.data); | |
1775 return NGX_ERROR; | |
1776 } | |
1777 | |
1778 trak = ngx_mp4_last_trak(mp4); | |
1779 trak->time_to_sample_entries = entries; | |
1780 | |
1781 atom = &trak->stts_atom_buf; | |
1782 atom->temporary = 1; | |
1783 atom->pos = atom_header; | |
1784 atom->last = atom_table; | |
1785 | |
1786 data = &trak->stts_data_buf; | |
1787 data->temporary = 1; | |
1788 data->pos = atom_table; | |
1789 data->last = atom_end; | |
1790 | |
1791 trak->out[NGX_HTTP_MP4_STTS_ATOM].buf = atom; | |
1792 trak->out[NGX_HTTP_MP4_STTS_DATA].buf = data; | |
1793 | |
1794 ngx_mp4_atom_next(mp4, atom_data_size); | |
1795 | |
1796 return NGX_OK; | |
1797 } | |
1798 | |
1799 | |
1800 static ngx_int_t | |
1801 ngx_http_mp4_update_stts_atom(ngx_http_mp4_file_t *mp4, | |
1802 ngx_http_mp4_trak_t *trak) | |
1803 { | |
1804 size_t atom_size; | |
1805 uint32_t entries, count, duration; | |
1806 uint64_t start_time; | |
1807 ngx_buf_t *atom, *data; | |
1808 ngx_uint_t start_sample; | |
1809 ngx_mp4_stts_atom_t *stts_atom; | |
1810 ngx_mp4_stts_entry_t *entry, *end; | |
1811 | |
1812 /* | |
1813 * mdia.minf.stbl.stts updating requires trak->timescale | |
1814 * from mdia.mdhd atom which may reside after mdia.minf | |
1815 */ | |
1816 | |
1817 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1818 "mp4 stts atom update"); | |
1819 | |
1820 data = trak->out[NGX_HTTP_MP4_STTS_DATA].buf; | |
1821 | |
1822 if (data == NULL) { | |
1823 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
1824 "no mp4 stts atoms were found in \"%s\"", | |
1825 mp4->file.name.data); | |
1826 return NGX_ERROR; | |
1827 } | |
1828 | |
1829 entries = trak->time_to_sample_entries; | |
1830 start_time = mp4->start * trak->timescale / 1000; | |
1831 | |
1832 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
4096 | 1833 "time-to-sample start_time:%uL", start_time); |
4085 | 1834 |
1835 start_sample = 0; | |
1836 entry = (ngx_mp4_stts_entry_t *) data->pos; | |
1837 end = (ngx_mp4_stts_entry_t *) data->last; | |
1838 | |
1839 while (entry < end) { | |
1840 count = ngx_mp4_get_32value(entry->count); | |
1841 duration = ngx_mp4_get_32value(entry->duration); | |
1842 | |
1843 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1844 "count:%uD, duration:%uD", count, duration); | |
1845 | |
1846 if (start_time < count * duration) { | |
1847 start_sample += (ngx_uint_t) (start_time / duration); | |
1848 count -= start_sample; | |
1849 ngx_mp4_set_32value(entry->count, count); | |
1850 goto found; | |
1851 } | |
1852 | |
1853 start_sample += count; | |
1854 start_time -= count * duration; | |
1855 entries--; | |
1856 entry++; | |
1857 } | |
1858 | |
1859 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
4094 | 1860 "start time is out mp4 stts samples in \"%s\"", |
4085 | 1861 mp4->file.name.data); |
1862 | |
1863 return NGX_ERROR; | |
1864 | |
1865 found: | |
1866 | |
1867 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1868 "start_sample:%ui, new count:%uD", start_sample, count); | |
1869 | |
1870 trak->start_sample = start_sample; | |
1871 | |
1872 data->pos = (u_char *) entry; | |
1873 atom_size = sizeof(ngx_mp4_stts_atom_t) + (data->last - data->pos); | |
1874 trak->size += atom_size; | |
1875 | |
1876 atom = trak->out[NGX_HTTP_MP4_STTS_ATOM].buf; | |
1877 stts_atom = (ngx_mp4_stts_atom_t *) atom->pos; | |
1878 ngx_mp4_set_32value(stts_atom->size, atom_size); | |
1879 ngx_mp4_set_32value(stts_atom->entries, entries); | |
1880 | |
1881 return NGX_OK; | |
1882 } | |
1883 | |
1884 | |
1885 typedef struct { | |
1886 u_char size[4]; | |
1887 u_char name[4]; | |
1888 u_char version[1]; | |
1889 u_char flags[3]; | |
1890 u_char entries[4]; | |
1891 } ngx_http_mp4_stss_atom_t; | |
1892 | |
1893 | |
1894 static ngx_int_t | |
1895 ngx_http_mp4_read_stss_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1896 { | |
1897 u_char *atom_header, *atom_table, *atom_end; | |
1898 uint32_t entries; | |
1899 ngx_buf_t *atom, *data; | |
1900 ngx_http_mp4_trak_t *trak; | |
1901 ngx_http_mp4_stss_atom_t *stss_atom; | |
1902 | |
1903 /* sync samples atom */ | |
1904 | |
1905 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stss atom"); | |
1906 | |
1907 atom_header = ngx_mp4_atom_header(mp4); | |
1908 stss_atom = (ngx_http_mp4_stss_atom_t *) atom_header; | |
1909 ngx_mp4_set_atom_name(stss_atom, 's', 't', 's', 's'); | |
1910 | |
1911 entries = ngx_mp4_get_32value(stss_atom->entries); | |
1912 | |
1913 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
4096 | 1914 "sync sample entries:%uD", entries); |
4085 | 1915 |
1916 trak = ngx_mp4_last_trak(mp4); | |
1917 trak->sync_samples_entries = entries; | |
1918 | |
1919 atom_table = atom_header + sizeof(ngx_http_mp4_stss_atom_t); | |
1920 | |
1921 atom = &trak->stss_atom_buf; | |
1922 atom->temporary = 1; | |
1923 atom->pos = atom_header; | |
1924 atom->last = atom_table; | |
1925 | |
1926 atom_end = atom_table + entries * sizeof(uint32_t); | |
1927 | |
1928 if ((uint64_t) (atom_end - stss_atom->version) > atom_data_size) { | |
1929 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
1930 "\"%s\" mp4 stss atom too large", mp4->file.name.data); | |
1931 return NGX_ERROR; | |
1932 } | |
1933 | |
1934 data = &trak->stss_data_buf; | |
1935 data->temporary = 1; | |
1936 data->pos = atom_table; | |
1937 data->last = atom_end; | |
1938 | |
1939 trak->out[NGX_HTTP_MP4_STSS_ATOM].buf = atom; | |
1940 trak->out[NGX_HTTP_MP4_STSS_DATA].buf = data; | |
1941 | |
1942 ngx_mp4_atom_next(mp4, atom_data_size); | |
1943 | |
1944 return NGX_OK; | |
1945 } | |
1946 | |
1947 | |
1948 static ngx_int_t | |
1949 ngx_http_mp4_update_stss_atom(ngx_http_mp4_file_t *mp4, | |
1950 ngx_http_mp4_trak_t *trak) | |
1951 { | |
1952 size_t atom_size; | |
1953 uint32_t entries, sample, start_sample, *entry, *end; | |
1954 ngx_buf_t *atom, *data; | |
1955 ngx_http_mp4_stss_atom_t *stss_atom; | |
1956 | |
1957 /* | |
1958 * mdia.minf.stbl.stss updating requires trak->start_sample | |
1959 * from mdia.minf.stbl.stts which depends on value from mdia.mdhd | |
1960 * atom which may reside after mdia.minf | |
1961 */ | |
1962 | |
1963 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1964 "mp4 stss atom update"); | |
1965 | |
1966 data = trak->out[NGX_HTTP_MP4_STSS_DATA].buf; | |
1967 | |
1968 if (data == NULL) { | |
1969 return NGX_OK; | |
1970 } | |
1971 | |
1972 /* sync samples starts from 1 */ | |
1973 start_sample = trak->start_sample + 1; | |
1974 entries = trak->sync_samples_entries; | |
1975 | |
1976 entry = (uint32_t *) data->pos; | |
1977 end = (uint32_t *) data->last; | |
1978 | |
1979 while (entry < end) { | |
1980 sample = ngx_mp4_get_32value(entry); | |
1981 | |
1982 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1983 "start:%uD, sync:%uD", start_sample, sample); | |
1984 | |
1985 if (sample >= start_sample) { | |
1986 goto found; | |
1987 } | |
1988 | |
1989 entries--; | |
1990 entry++; | |
1991 } | |
1992 | |
1993 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
1994 "start sample is out of mp4 stss atom in \"%s\"", | |
1995 mp4->file.name.data); | |
1996 | |
1997 return NGX_ERROR; | |
1998 | |
1999 found: | |
2000 | |
2001 data->pos = (u_char *) entry; | |
2002 | |
2003 start_sample = trak->start_sample; | |
2004 | |
2005 while (entry < end) { | |
2006 sample = ngx_mp4_get_32value(entry); | |
2007 sample -= start_sample; | |
2008 ngx_mp4_set_32value(entry, sample); | |
2009 entry++; | |
2010 } | |
2011 | |
2012 atom_size = sizeof(ngx_http_mp4_stss_atom_t) + (data->last - data->pos); | |
2013 trak->size += atom_size; | |
2014 | |
2015 atom = trak->out[NGX_HTTP_MP4_STSS_ATOM].buf; | |
2016 stss_atom = (ngx_http_mp4_stss_atom_t *) atom->pos; | |
2017 | |
2018 ngx_mp4_set_32value(stss_atom->size, atom_size); | |
2019 ngx_mp4_set_32value(stss_atom->entries, entries); | |
2020 | |
2021 return NGX_OK; | |
2022 } | |
2023 | |
2024 | |
2025 typedef struct { | |
2026 u_char size[4]; | |
2027 u_char name[4]; | |
2028 u_char version[1]; | |
2029 u_char flags[3]; | |
2030 u_char entries[4]; | |
2031 } ngx_mp4_ctts_atom_t; | |
2032 | |
2033 typedef struct { | |
2034 u_char count[4]; | |
2035 u_char offset[4]; | |
2036 } ngx_mp4_ctts_entry_t; | |
2037 | |
2038 | |
2039 static ngx_int_t | |
2040 ngx_http_mp4_read_ctts_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
2041 { | |
2042 u_char *atom_header, *atom_table, *atom_end; | |
2043 uint32_t entries; | |
2044 ngx_buf_t *atom, *data; | |
2045 ngx_mp4_ctts_atom_t *ctts_atom; | |
2046 ngx_http_mp4_trak_t *trak; | |
2047 | |
2048 /* composition offsets atom */ | |
2049 | |
2050 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 ctts atom"); | |
2051 | |
2052 atom_header = ngx_mp4_atom_header(mp4); | |
2053 ctts_atom = (ngx_mp4_ctts_atom_t *) atom_header; | |
2054 ngx_mp4_set_atom_name(ctts_atom, 'c', 't', 't', 's'); | |
2055 | |
2056 entries = ngx_mp4_get_32value(ctts_atom->entries); | |
2057 | |
2058 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
4096 | 2059 "composition offset entries:%uD", entries); |
4085 | 2060 |
2061 trak = ngx_mp4_last_trak(mp4); | |
2062 trak->composition_offset_entries = entries; | |
2063 | |
2064 atom_table = atom_header + sizeof(ngx_mp4_ctts_atom_t); | |
2065 | |
2066 atom = &trak->ctts_atom_buf; | |
2067 atom->temporary = 1; | |
2068 atom->pos = atom_header; | |
2069 atom->last = atom_table; | |
2070 | |
2071 atom_end = atom_table + entries * sizeof(ngx_mp4_ctts_entry_t); | |
2072 | |
2073 if ((uint64_t) (atom_end - ctts_atom->version) > atom_data_size) { | |
2074 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
2075 "\"%s\" mp4 ctts atom too large", mp4->file.name.data); | |
2076 return NGX_ERROR; | |
2077 } | |
2078 | |
2079 data = &trak->ctts_data_buf; | |
2080 data->temporary = 1; | |
2081 data->pos = atom_table; | |
2082 data->last = atom_end; | |
2083 | |
2084 trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf = atom; | |
2085 trak->out[NGX_HTTP_MP4_CTTS_DATA].buf = data; | |
2086 | |
2087 ngx_mp4_atom_next(mp4, atom_data_size); | |
2088 | |
2089 return NGX_OK; | |
2090 } | |
2091 | |
2092 | |
2093 static void | |
2094 ngx_http_mp4_update_ctts_atom(ngx_http_mp4_file_t *mp4, | |
2095 ngx_http_mp4_trak_t *trak) | |
2096 { | |
2097 size_t atom_size; | |
2098 uint32_t entries, count, start_sample; | |
2099 ngx_buf_t *atom, *data; | |
2100 ngx_mp4_ctts_atom_t *ctts_atom; | |
2101 ngx_mp4_ctts_entry_t *entry, *end; | |
2102 | |
2103 /* | |
2104 * mdia.minf.stbl.ctts updating requires trak->start_sample | |
2105 * from mdia.minf.stbl.stts which depends on value from mdia.mdhd | |
2106 * atom which may reside after mdia.minf | |
2107 */ | |
2108 | |
2109 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2110 "mp4 ctts atom update"); | |
2111 | |
2112 data = trak->out[NGX_HTTP_MP4_CTTS_DATA].buf; | |
2113 | |
2114 if (data == NULL) { | |
2115 return; | |
2116 } | |
2117 | |
2118 /* sync samples starts from 1 */ | |
2119 start_sample = trak->start_sample + 1; | |
2120 entries = trak->composition_offset_entries; | |
2121 entry = (ngx_mp4_ctts_entry_t *) data->pos; | |
2122 end = (ngx_mp4_ctts_entry_t *) data->last; | |
2123 | |
2124 while (entry < end) { | |
2125 count = ngx_mp4_get_32value(entry->count); | |
2126 | |
4088
8fe1da7b8386
bugfix of r4086: nginx could not be built without debug log.
Igor Sysoev <igor@sysoev.ru>
parents:
4087
diff
changeset
|
2127 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, |
4085 | 2128 "start:%uD, count:%uD, offset:%uD", |
2129 start_sample, count, ngx_mp4_get_32value(entry->offset)); | |
2130 | |
2131 if (start_sample <= count) { | |
2132 count -= (start_sample - 1); | |
2133 ngx_mp4_set_32value(entry->count, count); | |
2134 goto found; | |
2135 } | |
2136 | |
2137 start_sample -= count; | |
2138 entries--; | |
2139 entry++; | |
2140 } | |
2141 | |
2142 trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf = NULL; | |
2143 trak->out[NGX_HTTP_MP4_CTTS_DATA].buf = NULL; | |
2144 | |
2145 return; | |
2146 | |
2147 found: | |
2148 | |
2149 data->pos = (u_char *) entry; | |
2150 atom_size = sizeof(ngx_mp4_ctts_atom_t) + (data->last - data->pos); | |
2151 trak->size += atom_size; | |
2152 | |
2153 atom = trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf; | |
2154 ctts_atom = (ngx_mp4_ctts_atom_t *) atom->pos; | |
2155 | |
2156 ngx_mp4_set_32value(ctts_atom->size, atom_size); | |
2157 ngx_mp4_set_32value(ctts_atom->entries, entries); | |
2158 | |
2159 return; | |
2160 } | |
2161 | |
2162 | |
2163 typedef struct { | |
2164 u_char size[4]; | |
2165 u_char name[4]; | |
2166 u_char version[1]; | |
2167 u_char flags[3]; | |
2168 u_char entries[4]; | |
2169 } ngx_mp4_stsc_atom_t; | |
2170 | |
2171 | |
2172 static ngx_int_t | |
2173 ngx_http_mp4_read_stsc_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
2174 { | |
2175 u_char *atom_header, *atom_table, *atom_end; | |
2176 uint32_t entries; | |
2177 ngx_buf_t *atom, *data; | |
2178 ngx_mp4_stsc_atom_t *stsc_atom; | |
2179 ngx_http_mp4_trak_t *trak; | |
2180 | |
2181 /* sample-to-chunk atom */ | |
2182 | |
2183 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stsc atom"); | |
2184 | |
2185 atom_header = ngx_mp4_atom_header(mp4); | |
2186 stsc_atom = (ngx_mp4_stsc_atom_t *) atom_header; | |
2187 ngx_mp4_set_atom_name(stsc_atom, 's', 't', 's', 'c'); | |
2188 | |
2189 entries = ngx_mp4_get_32value(stsc_atom->entries); | |
2190 | |
2191 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
4096 | 2192 "sample-to-chunk entries:%uD", entries); |
4085 | 2193 |
2194 atom_table = atom_header + sizeof(ngx_mp4_stsc_atom_t); | |
2195 atom_end = atom_table + entries * sizeof(ngx_mp4_stsc_entry_t); | |
2196 | |
2197 if ((uint64_t) (atom_end - stsc_atom->version) > atom_data_size) { | |
2198 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
2199 "\"%s\" mp4 stsc atom too large", | |
2200 mp4->file.name.data); | |
2201 return NGX_ERROR; | |
2202 } | |
2203 | |
2204 trak = ngx_mp4_last_trak(mp4); | |
2205 trak->sample_to_chunk_entries = entries; | |
2206 | |
2207 atom = &trak->stsc_atom_buf; | |
2208 atom->temporary = 1; | |
2209 atom->pos = atom_header; | |
2210 atom->last = atom_table; | |
2211 | |
2212 data = &trak->stsc_data_buf; | |
2213 data->temporary = 1; | |
2214 data->pos = atom_table; | |
2215 data->last = atom_end; | |
2216 | |
2217 trak->out[NGX_HTTP_MP4_STSC_ATOM].buf = atom; | |
2218 trak->out[NGX_HTTP_MP4_STSC_DATA].buf = data; | |
2219 | |
2220 ngx_mp4_atom_next(mp4, atom_data_size); | |
2221 | |
2222 return NGX_OK; | |
2223 } | |
2224 | |
2225 | |
2226 static ngx_int_t | |
2227 ngx_http_mp4_update_stsc_atom(ngx_http_mp4_file_t *mp4, | |
2228 ngx_http_mp4_trak_t *trak) | |
2229 { | |
2230 size_t atom_size; | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2231 uint32_t start_sample, entries, chunk, samples, id, |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2232 next_chunk, n; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2233 ngx_buf_t *atom, *data, *buf; |
4085 | 2234 ngx_mp4_stsc_atom_t *stsc_atom; |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2235 ngx_mp4_stsc_entry_t *entry, *first, *end; |
4085 | 2236 |
2237 /* | |
2238 * mdia.minf.stbl.stsc updating requires trak->start_sample | |
2239 * from mdia.minf.stbl.stts which depends on value from mdia.mdhd | |
2240 * atom which may reside after mdia.minf | |
2241 */ | |
2242 | |
2243 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2244 "mp4 stsc atom update"); | |
2245 | |
2246 data = trak->out[NGX_HTTP_MP4_STSC_DATA].buf; | |
2247 | |
2248 if (data == NULL) { | |
2249 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
2250 "no mp4 stsc atoms were found in \"%s\"", | |
2251 mp4->file.name.data); | |
2252 return NGX_ERROR; | |
2253 } | |
2254 | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2255 start_sample = (uint32_t) trak->start_sample; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2256 entries = trak->sample_to_chunk_entries - 1; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2257 |
4085 | 2258 entry = (ngx_mp4_stsc_entry_t *) data->pos; |
2259 end = (ngx_mp4_stsc_entry_t *) data->last; | |
2260 | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2261 chunk = ngx_mp4_get_32value(entry->chunk); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2262 samples = ngx_mp4_get_32value(entry->samples); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2263 id = ngx_mp4_get_32value(entry->id); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2264 entry++; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2265 |
4085 | 2266 while (entry < end) { |
2267 | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2268 next_chunk = ngx_mp4_get_32value(entry->chunk); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2269 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2270 ngx_log_debug5(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2271 "start_sample:%uD, chunk:%uD, chunks:%uD, " |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2272 "samples:%uD, id:%uD", |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2273 start_sample, chunk, next_chunk - chunk, samples, id); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2274 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2275 n = (next_chunk - chunk) * samples; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2276 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2277 if (start_sample <= n) { |
4085 | 2278 goto found; |
2279 } | |
2280 | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2281 start_sample -= n; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2282 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2283 chunk = next_chunk; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2284 samples = ngx_mp4_get_32value(entry->samples); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2285 id = ngx_mp4_get_32value(entry->id); |
4085 | 2286 entries--; |
2287 entry++; | |
2288 } | |
2289 | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2290 next_chunk = trak->chunks; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2291 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2292 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2293 "start_sample:%uD, chunk:%uD, chunks:%uD, samples:%uD", |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2294 start_sample, chunk, next_chunk - chunk, samples); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2295 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2296 n = (next_chunk - chunk) * samples; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2297 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2298 if (start_sample > n) { |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2299 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2300 "start time is out mp4 stsc chunks in \"%s\"", |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2301 mp4->file.name.data); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2302 return NGX_ERROR; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2303 } |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2304 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2305 found: |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2306 |
4085 | 2307 entries++; |
2308 entry--; | |
2309 | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2310 if (samples == 0) { |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2311 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2312 "zero number of samples in \"%s\"", |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2313 mp4->file.name.data); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2314 return NGX_ERROR; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2315 } |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2316 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2317 trak->start_chunk = chunk - 1; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2318 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2319 trak->start_chunk += start_sample / samples; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2320 trak->chunk_samples = start_sample % samples; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2321 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2322 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2323 "start chunk:%ui, samples:%uD", |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2324 trak->start_chunk, trak->chunk_samples); |
4085 | 2325 |
2326 data->pos = (u_char *) entry; | |
2327 atom_size = sizeof(ngx_mp4_stsc_atom_t) + (data->last - data->pos); | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2328 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2329 if (trak->chunk_samples) { |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2330 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2331 first = &trak->stsc_chunk_entry; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2332 ngx_mp4_set_32value(first->chunk, 1); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2333 ngx_mp4_set_32value(first->samples, samples - trak->chunk_samples); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2334 ngx_mp4_set_32value(first->id, id); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2335 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2336 buf = &trak->stsc_chunk_buf; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2337 buf->temporary = 1; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2338 buf->pos = (u_char *) first; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2339 buf->last = (u_char *) first + sizeof(ngx_mp4_stsc_entry_t); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2340 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2341 trak->out[NGX_HTTP_MP4_STSC_CHUNK].buf = buf; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2342 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2343 ngx_mp4_set_32value(entry->chunk, 2); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2344 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2345 atom_size += sizeof(ngx_mp4_stsc_entry_t); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2346 } |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2347 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2348 while (++entry < end) { |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2349 chunk = ngx_mp4_get_32value(entry->chunk); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2350 chunk -= trak->start_chunk; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2351 ngx_mp4_set_32value(entry->chunk, chunk); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2352 } |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2353 |
4085 | 2354 trak->size += atom_size; |
2355 | |
2356 atom = trak->out[NGX_HTTP_MP4_STSC_ATOM].buf; | |
2357 stsc_atom = (ngx_mp4_stsc_atom_t *) atom->pos; | |
2358 | |
2359 ngx_mp4_set_32value(stsc_atom->size, atom_size); | |
2360 ngx_mp4_set_32value(stsc_atom->entries, entries); | |
2361 | |
2362 return NGX_OK; | |
2363 } | |
2364 | |
2365 | |
2366 typedef struct { | |
2367 u_char size[4]; | |
2368 u_char name[4]; | |
2369 u_char version[1]; | |
2370 u_char flags[3]; | |
2371 u_char uniform_size[4]; | |
2372 u_char entries[4]; | |
2373 } ngx_mp4_stsz_atom_t; | |
2374 | |
2375 | |
2376 static ngx_int_t | |
2377 ngx_http_mp4_read_stsz_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
2378 { | |
2379 u_char *atom_header, *atom_table, *atom_end; | |
2380 size_t atom_size; | |
2381 uint32_t entries, size; | |
2382 ngx_buf_t *atom, *data; | |
2383 ngx_mp4_stsz_atom_t *stsz_atom; | |
2384 ngx_http_mp4_trak_t *trak; | |
2385 | |
2386 /* sample sizes atom */ | |
2387 | |
2388 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stsz atom"); | |
2389 | |
2390 atom_header = ngx_mp4_atom_header(mp4); | |
2391 stsz_atom = (ngx_mp4_stsz_atom_t *) atom_header; | |
2392 ngx_mp4_set_atom_name(stsz_atom, 's', 't', 's', 'z'); | |
2393 | |
2394 size = ngx_mp4_get_32value(stsz_atom->uniform_size); | |
2395 entries = ngx_mp4_get_32value(stsz_atom->entries); | |
2396 | |
2397 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2398 "sample uniform size:%uD, entries:%uD", size, entries); | |
2399 | |
2400 trak = ngx_mp4_last_trak(mp4); | |
2401 trak->sample_sizes_entries = entries; | |
2402 | |
2403 atom_table = atom_header + sizeof(ngx_mp4_stsz_atom_t); | |
2404 | |
2405 atom = &trak->stsz_atom_buf; | |
2406 atom->temporary = 1; | |
2407 atom->pos = atom_header; | |
2408 atom->last = atom_table; | |
2409 | |
2410 trak->out[NGX_HTTP_MP4_STSZ_ATOM].buf = atom; | |
2411 | |
2412 if (size == 0) { | |
2413 atom_end = atom_table + entries * sizeof(uint32_t); | |
2414 | |
2415 if ((uint64_t) (atom_end - stsz_atom->version) > atom_data_size) { | |
2416 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
2417 "\"%s\" mp4 stsz atom too large", | |
2418 mp4->file.name.data); | |
2419 return NGX_ERROR; | |
2420 } | |
2421 | |
2422 data = &trak->stsz_data_buf; | |
2423 data->temporary = 1; | |
2424 data->pos = atom_table; | |
2425 data->last = atom_end; | |
2426 | |
2427 trak->out[NGX_HTTP_MP4_STSZ_DATA].buf = data; | |
2428 | |
2429 } else { | |
2430 /* if size != 0 then all samples are the same size */ | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2431 /* TODO : chunk samples */ |
4085 | 2432 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; |
2433 ngx_mp4_set_32value(atom_header, atom_size); | |
2434 trak->size += atom_size; | |
2435 } | |
2436 | |
2437 ngx_mp4_atom_next(mp4, atom_data_size); | |
2438 | |
2439 return NGX_OK; | |
2440 } | |
2441 | |
2442 | |
2443 static void | |
2444 ngx_http_mp4_update_stsz_atom(ngx_http_mp4_file_t *mp4, | |
2445 ngx_http_mp4_trak_t *trak) | |
2446 { | |
2447 size_t atom_size; | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2448 uint32_t *pos, *end; |
4085 | 2449 ngx_buf_t *atom, *data; |
2450 ngx_mp4_stsz_atom_t *stsz_atom; | |
2451 | |
2452 /* | |
2453 * mdia.minf.stbl.stsz updating requires trak->start_sample | |
2454 * from mdia.minf.stbl.stts which depends on value from mdia.mdhd | |
2455 * atom which may reside after mdia.minf | |
2456 */ | |
2457 | |
2458 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2459 "mp4 stsz atom update"); | |
2460 | |
2461 data = trak->out[NGX_HTTP_MP4_STSZ_DATA].buf; | |
2462 | |
2463 if (data) { | |
2464 data->pos += trak->start_sample * sizeof(uint32_t); | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2465 end = (uint32_t *) data->pos; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2466 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2467 for (pos = end - trak->chunk_samples; pos < end; pos++) { |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2468 trak->chunk_samples_size += ngx_mp4_get_32value(pos); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2469 } |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2470 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2471 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2472 "chunk samples sizes:%uD", trak->chunk_samples_size); |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2473 |
4085 | 2474 atom_size = sizeof(ngx_mp4_stsz_atom_t) + (data->last - data->pos); |
2475 trak->size += atom_size; | |
2476 | |
2477 atom = trak->out[NGX_HTTP_MP4_STSZ_ATOM].buf; | |
2478 stsz_atom = (ngx_mp4_stsz_atom_t *) atom->pos; | |
2479 | |
2480 ngx_mp4_set_32value(stsz_atom->size, atom_size); | |
2481 ngx_mp4_set_32value(stsz_atom->entries, | |
2482 trak->sample_sizes_entries - trak->start_sample); | |
2483 } | |
2484 } | |
2485 | |
2486 | |
2487 typedef struct { | |
2488 u_char size[4]; | |
2489 u_char name[4]; | |
2490 u_char version[1]; | |
2491 u_char flags[3]; | |
2492 u_char entries[4]; | |
2493 } ngx_mp4_stco_atom_t; | |
2494 | |
2495 | |
2496 static ngx_int_t | |
2497 ngx_http_mp4_read_stco_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
2498 { | |
2499 u_char *atom_header, *atom_table, *atom_end; | |
2500 uint32_t entries; | |
2501 ngx_buf_t *atom, *data; | |
2502 ngx_mp4_stco_atom_t *stco_atom; | |
2503 ngx_http_mp4_trak_t *trak; | |
2504 | |
2505 /* chunk offsets atom */ | |
2506 | |
2507 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stco atom"); | |
2508 | |
2509 atom_header = ngx_mp4_atom_header(mp4); | |
2510 stco_atom = (ngx_mp4_stco_atom_t *) atom_header; | |
2511 ngx_mp4_set_atom_name(stco_atom, 's', 't', 'c', 'o'); | |
2512 | |
2513 entries = ngx_mp4_get_32value(stco_atom->entries); | |
2514 | |
2515 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "chunks:%uD", entries); | |
2516 | |
2517 atom_table = atom_header + sizeof(ngx_mp4_stco_atom_t); | |
2518 atom_end = atom_table + entries * sizeof(uint32_t); | |
2519 | |
2520 if ((uint64_t) (atom_end - stco_atom->version) > atom_data_size) { | |
2521 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
2522 "\"%s\" mp4 stco atom too large", mp4->file.name.data); | |
2523 return NGX_ERROR; | |
2524 } | |
2525 | |
2526 trak = ngx_mp4_last_trak(mp4); | |
2527 trak->chunks = entries; | |
2528 | |
2529 atom = &trak->tsco_atom_buf; | |
2530 atom->temporary = 1; | |
2531 atom->pos = atom_header; | |
2532 atom->last = atom_table; | |
2533 | |
2534 data = &trak->tsco_data_buf; | |
2535 data->temporary = 1; | |
2536 data->pos = atom_table; | |
2537 data->last = atom_end; | |
2538 | |
2539 trak->out[NGX_HTTP_MP4_STCO_ATOM].buf = atom; | |
2540 trak->out[NGX_HTTP_MP4_STCO_DATA].buf = data; | |
2541 | |
2542 ngx_mp4_atom_next(mp4, atom_data_size); | |
2543 | |
2544 return NGX_OK; | |
2545 } | |
2546 | |
2547 | |
2548 static ngx_int_t | |
2549 ngx_http_mp4_update_stco_atom(ngx_http_mp4_file_t *mp4, | |
2550 ngx_http_mp4_trak_t *trak) | |
2551 { | |
2552 size_t atom_size; | |
2553 ngx_buf_t *atom, *data; | |
2554 ngx_mp4_stco_atom_t *stco_atom; | |
2555 | |
2556 /* | |
2557 * mdia.minf.stbl.stco updating requires trak->start_chunk | |
2558 * from mdia.minf.stbl.stsc which depends on value from mdia.mdhd | |
2559 * atom which may reside after mdia.minf | |
2560 */ | |
2561 | |
2562 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2563 "mp4 stco atom update"); | |
2564 | |
2565 data = trak->out[NGX_HTTP_MP4_STCO_DATA].buf; | |
2566 | |
2567 if (data == NULL) { | |
2568 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
2569 "no mp4 stco atoms were found in \"%s\"", | |
2570 mp4->file.name.data); | |
2571 return NGX_ERROR; | |
2572 } | |
2573 | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2574 data->pos += trak->start_chunk * sizeof(uint32_t); |
4085 | 2575 atom_size = sizeof(ngx_mp4_stco_atom_t) + (data->last - data->pos); |
2576 trak->size += atom_size; | |
2577 | |
2578 trak->start_offset = ngx_mp4_get_32value(data->pos); | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2579 trak->start_offset += trak->chunk_samples_size; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2580 ngx_mp4_set_32value(data->pos, trak->start_offset); |
4085 | 2581 |
2582 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2583 "start chunk offset:%uD", trak->start_offset); | |
2584 | |
2585 atom = trak->out[NGX_HTTP_MP4_STCO_ATOM].buf; | |
2586 stco_atom = (ngx_mp4_stco_atom_t *) atom->pos; | |
2587 | |
2588 ngx_mp4_set_32value(stco_atom->size, atom_size); | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2589 ngx_mp4_set_32value(stco_atom->entries, trak->chunks - trak->start_chunk); |
4085 | 2590 |
2591 return NGX_OK; | |
2592 } | |
2593 | |
2594 | |
2595 static void | |
2596 ngx_http_mp4_adjust_stco_atom(ngx_http_mp4_file_t *mp4, | |
2597 ngx_http_mp4_trak_t *trak, int32_t adjustment) | |
2598 { | |
2599 uint32_t offset, *entry, *end; | |
2600 ngx_buf_t *data; | |
2601 | |
2602 /* | |
2603 * moov.trak.mdia.minf.stbl.stco adjustment requires | |
2604 * minimal start offset of all traks and new moov atom size | |
2605 */ | |
2606 | |
2607 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2608 "mp4 stco atom adjustment"); | |
2609 | |
2610 data = trak->out[NGX_HTTP_MP4_STCO_DATA].buf; | |
2611 entry = (uint32_t *) data->pos; | |
2612 end = (uint32_t *) data->last; | |
2613 | |
2614 while (entry < end) { | |
2615 offset = ngx_mp4_get_32value(entry); | |
2616 offset += adjustment; | |
2617 ngx_mp4_set_32value(entry, offset); | |
2618 entry++; | |
2619 } | |
2620 } | |
2621 | |
2622 | |
2623 static char * | |
2624 ngx_http_mp4(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
2625 { | |
2626 ngx_http_core_loc_conf_t *clcf; | |
2627 | |
2628 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | |
2629 clcf->handler = ngx_http_mp4_handler; | |
2630 | |
2631 return NGX_CONF_OK; | |
2632 } | |
2633 | |
2634 | |
2635 static void * | |
2636 ngx_http_mp4_create_conf(ngx_conf_t *cf) | |
2637 { | |
2638 ngx_http_mp4_conf_t *conf; | |
2639 | |
2640 conf = ngx_palloc(cf->pool, sizeof(ngx_http_mp4_conf_t)); | |
2641 if (conf == NULL) { | |
2642 return NULL; | |
2643 } | |
2644 | |
2645 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
|
2646 conf->max_buffer_size = NGX_CONF_UNSET_SIZE; |
4085 | 2647 |
2648 return conf; | |
2649 } | |
2650 | |
2651 | |
2652 static char * | |
2653 ngx_http_mp4_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
2654 { | |
2655 ngx_http_mp4_conf_t *prev = parent; | |
2656 ngx_http_mp4_conf_t *conf = child; | |
2657 | |
2658 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
|
2659 ngx_conf_merge_size_value(conf->max_buffer_size, prev->max_buffer_size, |
4085 | 2660 10 * 1024 * 1024); |
2661 | |
2662 return NGX_CONF_OK; | |
2663 } |