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