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