Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_mp4_module.c @ 5155:2bdb94e65a4c stable-1.2
Merge of r5098: mp4: fixed handling of too small mdat atoms.
Mp4: fixed handling of too small mdat atoms (ticket #266).
Patch by Gernot Vormayr (with minor changes).
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Fri, 29 Mar 2013 17:20:59 +0000 |
parents | 1c7616100797 |
children |
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 | |
589 if (ngx_http_set_content_type(r) != NGX_OK) { | |
590 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
591 } | |
592 | |
593 if (mp4 == NULL) { | |
594 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); | |
595 if (b == NULL) { | |
596 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
597 } | |
598 | |
599 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)); | |
600 if (b->file == NULL) { | |
601 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
602 } | |
603 } | |
604 | |
605 rc = ngx_http_send_header(r); | |
606 | |
607 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { | |
608 return rc; | |
609 } | |
610 | |
611 if (mp4) { | |
612 return ngx_http_output_filter(r, mp4->out); | |
613 } | |
614 | |
615 b->file_pos = 0; | |
616 b->file_last = of.size; | |
617 | |
618 b->in_file = b->file_last ? 1 : 0; | |
4659
f12d474f0d5e
Merge of r4612: proper subrequest handling in various modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4585
diff
changeset
|
619 b->last_buf = (r == r->main) ? 1 : 0; |
4085 | 620 b->last_in_chain = 1; |
621 | |
622 b->file->fd = of.fd; | |
623 b->file->name = path; | |
624 b->file->log = log; | |
625 b->file->directio = of.is_directio; | |
626 | |
627 out.buf = b; | |
628 out.next = NULL; | |
629 | |
630 return ngx_http_output_filter(r, &out); | |
631 } | |
632 | |
633 | |
634 static ngx_int_t | |
635 ngx_http_mp4_process(ngx_http_mp4_file_t *mp4) | |
636 { | |
637 off_t start_offset, adjustment; | |
638 ngx_int_t rc; | |
639 ngx_uint_t i, j; | |
640 ngx_chain_t **prev; | |
641 ngx_http_mp4_trak_t *trak; | |
642 ngx_http_mp4_conf_t *conf; | |
643 | |
644 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
645 "mp4 start:%ui", mp4->start); | |
646 | |
647 conf = ngx_http_get_module_loc_conf(mp4->request, ngx_http_mp4_module); | |
648 | |
649 mp4->buffer_size = conf->buffer_size; | |
650 | |
651 rc = ngx_http_mp4_read_atom(mp4, ngx_http_mp4_atoms, mp4->end); | |
652 if (rc != NGX_OK) { | |
653 return rc; | |
654 } | |
655 | |
656 if (mp4->trak.nelts == 0) { | |
657 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
658 "no mp4 trak atoms were found in \"%s\"", | |
659 mp4->file.name.data); | |
660 return NGX_ERROR; | |
661 } | |
662 | |
663 if (mp4->mdat_atom.buf == NULL) { | |
664 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
665 "no mp4 mdat atom was found in \"%s\"", | |
666 mp4->file.name.data); | |
667 return NGX_ERROR; | |
668 } | |
669 | |
670 prev = &mp4->out; | |
671 | |
672 if (mp4->ftyp_atom.buf) { | |
673 *prev = &mp4->ftyp_atom; | |
674 prev = &mp4->ftyp_atom.next; | |
675 } | |
676 | |
677 *prev = &mp4->moov_atom; | |
678 prev = &mp4->moov_atom.next; | |
679 | |
680 if (mp4->mvhd_atom.buf) { | |
681 mp4->moov_size += mp4->mvhd_atom_buf.last - mp4->mvhd_atom_buf.pos; | |
682 *prev = &mp4->mvhd_atom; | |
683 prev = &mp4->mvhd_atom.next; | |
684 } | |
685 | |
686 start_offset = mp4->end; | |
687 trak = mp4->trak.elts; | |
688 | |
689 for (i = 0; i < mp4->trak.nelts; i++) { | |
690 | |
691 if (ngx_http_mp4_update_stts_atom(mp4, &trak[i]) != NGX_OK) { | |
692 return NGX_ERROR; | |
693 } | |
694 | |
695 if (ngx_http_mp4_update_stss_atom(mp4, &trak[i]) != NGX_OK) { | |
696 return NGX_ERROR; | |
697 } | |
698 | |
699 ngx_http_mp4_update_ctts_atom(mp4, &trak[i]); | |
700 | |
701 if (ngx_http_mp4_update_stsc_atom(mp4, &trak[i]) != NGX_OK) { | |
702 return NGX_ERROR; | |
703 } | |
704 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
705 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
|
706 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
707 } |
4085 | 708 |
4112 | 709 if (trak[i].out[NGX_HTTP_MP4_CO64_DATA].buf) { |
710 if (ngx_http_mp4_update_co64_atom(mp4, &trak[i]) != NGX_OK) { | |
711 return NGX_ERROR; | |
712 } | |
713 | |
714 } else { | |
715 if (ngx_http_mp4_update_stco_atom(mp4, &trak[i]) != NGX_OK) { | |
716 return NGX_ERROR; | |
717 } | |
4085 | 718 } |
719 | |
720 ngx_http_mp4_update_stbl_atom(mp4, &trak[i]); | |
721 ngx_http_mp4_update_minf_atom(mp4, &trak[i]); | |
722 trak[i].size += trak[i].mdhd_size; | |
723 trak[i].size += trak[i].hdlr_size; | |
724 ngx_http_mp4_update_mdia_atom(mp4, &trak[i]); | |
725 trak[i].size += trak[i].tkhd_size; | |
726 ngx_http_mp4_update_trak_atom(mp4, &trak[i]); | |
727 | |
728 mp4->moov_size += trak[i].size; | |
729 | |
730 if (start_offset > trak[i].start_offset) { | |
731 start_offset = trak[i].start_offset; | |
732 } | |
733 | |
734 *prev = &trak[i].out[NGX_HTTP_MP4_TRAK_ATOM]; | |
735 prev = &trak[i].out[NGX_HTTP_MP4_TRAK_ATOM].next; | |
736 | |
737 for (j = 0; j < NGX_HTTP_MP4_LAST_ATOM + 1; j++) { | |
738 if (trak[i].out[j].buf) { | |
739 *prev = &trak[i].out[j]; | |
740 prev = &trak[i].out[j].next; | |
741 } | |
742 } | |
743 } | |
744 | |
745 mp4->moov_size += 8; | |
746 | |
747 ngx_mp4_set_32value(mp4->moov_atom_header, mp4->moov_size); | |
748 ngx_mp4_set_atom_name(mp4->moov_atom_header, 'm', 'o', 'o', 'v'); | |
749 mp4->content_length += mp4->moov_size; | |
750 | |
751 *prev = &mp4->mdat_atom; | |
752 | |
5155
2bdb94e65a4c
Merge of r5098: mp4: fixed handling of too small mdat atoms.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4727
diff
changeset
|
753 if (start_offset > mp4->mdat_data.buf->file_last) { |
2bdb94e65a4c
Merge of r5098: mp4: fixed handling of too small mdat atoms.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4727
diff
changeset
|
754 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
2bdb94e65a4c
Merge of r5098: mp4: fixed handling of too small mdat atoms.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4727
diff
changeset
|
755 "start time is out mp4 mdat atom in \"%s\"", |
2bdb94e65a4c
Merge of r5098: mp4: fixed handling of too small mdat atoms.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4727
diff
changeset
|
756 mp4->file.name.data); |
2bdb94e65a4c
Merge of r5098: mp4: fixed handling of too small mdat atoms.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4727
diff
changeset
|
757 return NGX_ERROR; |
2bdb94e65a4c
Merge of r5098: mp4: fixed handling of too small mdat atoms.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4727
diff
changeset
|
758 } |
2bdb94e65a4c
Merge of r5098: mp4: fixed handling of too small mdat atoms.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4727
diff
changeset
|
759 |
4085 | 760 adjustment = mp4->ftyp_size + mp4->moov_size |
761 + ngx_http_mp4_update_mdat_atom(mp4, start_offset) | |
762 - start_offset; | |
763 | |
4088
8fe1da7b8386
bugfix of r4086: nginx could not be built without debug log.
Igor Sysoev <igor@sysoev.ru>
parents:
4087
diff
changeset
|
764 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, |
4579 | 765 "mp4 adjustment:%O", adjustment); |
4085 | 766 |
767 for (i = 0; i < mp4->trak.nelts; i++) { | |
4112 | 768 if (trak[i].out[NGX_HTTP_MP4_CO64_DATA].buf) { |
769 ngx_http_mp4_adjust_co64_atom(mp4, &trak[i], adjustment); | |
770 } else { | |
771 ngx_http_mp4_adjust_stco_atom(mp4, &trak[i], (int32_t) adjustment); | |
772 } | |
4085 | 773 } |
774 | |
775 return NGX_OK; | |
776 } | |
777 | |
778 | |
779 typedef struct { | |
780 u_char size[4]; | |
781 u_char name[4]; | |
782 } ngx_mp4_atom_header_t; | |
783 | |
784 typedef struct { | |
785 u_char size[4]; | |
786 u_char name[4]; | |
787 u_char size64[8]; | |
788 } ngx_mp4_atom_header64_t; | |
789 | |
790 | |
791 static ngx_int_t | |
792 ngx_http_mp4_read_atom(ngx_http_mp4_file_t *mp4, | |
793 ngx_http_mp4_atom_handler_t *atom, uint64_t atom_data_size) | |
794 { | |
795 off_t end; | |
796 size_t atom_header_size; | |
797 u_char *atom_header, *atom_name; | |
798 uint64_t atom_size; | |
799 ngx_int_t rc; | |
800 ngx_uint_t n; | |
801 | |
802 end = mp4->offset + atom_data_size; | |
803 | |
804 while (mp4->offset < end) { | |
805 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
806 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
|
807 return NGX_ERROR; |
4085 | 808 } |
809 | |
810 atom_header = mp4->buffer_pos; | |
811 atom_size = ngx_mp4_get_32value(atom_header); | |
812 atom_header_size = sizeof(ngx_mp4_atom_header_t); | |
813 | |
814 if (atom_size == 0) { | |
815 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
816 "mp4 atom end"); | |
817 return NGX_OK; | |
818 } | |
819 | |
820 if (atom_size < sizeof(ngx_mp4_atom_header_t)) { | |
821 | |
822 if (atom_size == 1) { | |
823 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
824 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
|
825 != NGX_OK) |
4085 | 826 { |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
827 return NGX_ERROR; |
4085 | 828 } |
829 | |
830 /* 64-bit atom size */ | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
831 atom_header = mp4->buffer_pos; |
4085 | 832 atom_size = ngx_mp4_get_64value(atom_header + 8); |
833 atom_header_size = sizeof(ngx_mp4_atom_header64_t); | |
834 | |
835 } else { | |
836 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
837 "\"%s\" mp4 atom is too small:%uL", | |
838 mp4->file.name.data, atom_size); | |
839 return NGX_ERROR; | |
840 } | |
841 } | |
842 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
843 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
|
844 return NGX_ERROR; |
4085 | 845 } |
846 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
847 atom_header = mp4->buffer_pos; |
4085 | 848 atom_name = atom_header + sizeof(uint32_t); |
849 | |
4088
8fe1da7b8386
bugfix of r4086: nginx could not be built without debug log.
Igor Sysoev <igor@sysoev.ru>
parents:
4087
diff
changeset
|
850 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, |
4085 | 851 "mp4 atom: %*s @%O:%uL", |
852 4, atom_name, mp4->offset, atom_size); | |
853 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
854 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
|
855 || mp4->offset + (off_t) atom_size > end) |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
856 { |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
857 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
858 "\"%s\" mp4 atom too large:%uL", |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
859 mp4->file.name.data, atom_size); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
860 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
861 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
862 |
4085 | 863 for (n = 0; atom[n].name; n++) { |
864 | |
865 if (ngx_strncmp(atom_name, atom[n].name, 4) == 0) { | |
866 | |
867 ngx_mp4_atom_next(mp4, atom_header_size); | |
868 | |
869 rc = atom[n].handler(mp4, atom_size - atom_header_size); | |
870 if (rc != NGX_OK) { | |
871 return rc; | |
872 } | |
873 | |
874 goto next; | |
875 } | |
876 } | |
877 | |
878 ngx_mp4_atom_next(mp4, atom_size); | |
879 | |
880 next: | |
881 continue; | |
882 } | |
883 | |
884 return NGX_OK; | |
885 } | |
886 | |
887 | |
888 static ngx_int_t | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
889 ngx_http_mp4_read(ngx_http_mp4_file_t *mp4, size_t size) |
4085 | 890 { |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
891 ssize_t n; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
892 |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
893 if (mp4->buffer_pos + size <= mp4->buffer_end) { |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
894 return NGX_OK; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
895 } |
4085 | 896 |
897 if (mp4->offset + (off_t) mp4->buffer_size > mp4->end) { | |
898 mp4->buffer_size = (size_t) (mp4->end - mp4->offset); | |
899 } | |
900 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
901 if (mp4->buffer_size < size) { |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
902 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
903 "\"%s\" mp4 file truncated", mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
904 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
905 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
906 |
4085 | 907 if (mp4->buffer == NULL) { |
908 mp4->buffer = ngx_palloc(mp4->request->pool, mp4->buffer_size); | |
909 if (mp4->buffer == NULL) { | |
910 return NGX_ERROR; | |
911 } | |
912 | |
913 mp4->buffer_start = mp4->buffer; | |
914 } | |
915 | |
916 n = ngx_read_file(&mp4->file, mp4->buffer_start, mp4->buffer_size, | |
917 mp4->offset); | |
918 | |
919 if (n == NGX_ERROR) { | |
920 return NGX_ERROR; | |
921 } | |
922 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
923 if ((size_t) n != mp4->buffer_size) { |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
924 ngx_log_error(NGX_LOG_CRIT, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
925 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
|
926 n, mp4->buffer_size, mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
927 return NGX_ERROR; |
4085 | 928 } |
929 | |
930 mp4->buffer_pos = mp4->buffer_start; | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
931 mp4->buffer_end = mp4->buffer_start + mp4->buffer_size; |
4085 | 932 |
933 return NGX_OK; | |
934 } | |
935 | |
936 | |
937 static ngx_int_t | |
938 ngx_http_mp4_read_ftyp_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
939 { | |
940 u_char *ftyp_atom; | |
941 size_t atom_size; | |
942 ngx_buf_t *atom; | |
943 | |
944 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 ftyp atom"); | |
945 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
946 if (atom_data_size > 1024 |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
947 || 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
|
948 { |
4085 | 949 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
950 "\"%s\" mp4 ftyp atom is too large:%uL", | |
951 mp4->file.name.data, atom_data_size); | |
952 return NGX_ERROR; | |
953 } | |
954 | |
955 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; | |
956 | |
957 ftyp_atom = ngx_palloc(mp4->request->pool, atom_size); | |
958 if (ftyp_atom == NULL) { | |
959 return NGX_ERROR; | |
960 } | |
961 | |
962 ngx_mp4_set_32value(ftyp_atom, atom_size); | |
963 ngx_mp4_set_atom_name(ftyp_atom, 'f', 't', 'y', 'p'); | |
964 | |
965 /* | |
966 * only moov atom content is guaranteed to be in mp4->buffer | |
967 * during sending response, so ftyp atom content should be copied | |
968 */ | |
969 ngx_memcpy(ftyp_atom + sizeof(ngx_mp4_atom_header_t), | |
970 ngx_mp4_atom_data(mp4), (size_t) atom_data_size); | |
971 | |
972 atom = &mp4->ftyp_atom_buf; | |
973 atom->temporary = 1; | |
974 atom->pos = ftyp_atom; | |
975 atom->last = ftyp_atom + atom_size; | |
976 | |
977 mp4->ftyp_atom.buf = atom; | |
978 mp4->ftyp_size = atom_size; | |
979 mp4->content_length = atom_size; | |
980 | |
981 ngx_mp4_atom_next(mp4, atom_data_size); | |
982 | |
983 return NGX_OK; | |
984 } | |
985 | |
986 | |
987 /* | |
988 * Small excess buffer to process atoms after moov atom, mp4->buffer_start | |
989 * will be set to this buffer part after moov atom processing. | |
990 */ | |
991 #define NGX_HTTP_MP4_MOOV_BUFFER_EXCESS (4 * 1024) | |
992 | |
993 static ngx_int_t | |
994 ngx_http_mp4_read_moov_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
995 { | |
996 ngx_int_t rc; | |
997 ngx_uint_t no_mdat; | |
998 ngx_buf_t *atom; | |
999 ngx_http_mp4_conf_t *conf; | |
1000 | |
1001 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 moov atom"); | |
1002 | |
1003 no_mdat = (mp4->mdat_atom.buf == NULL); | |
1004 | |
1005 if (no_mdat && mp4->start == 0) { | |
1006 /* | |
1007 * send original file if moov atom resides before | |
1008 * mdat atom and client requests integral file | |
1009 */ | |
1010 return NGX_DECLINED; | |
1011 } | |
1012 | |
1013 conf = ngx_http_get_module_loc_conf(mp4->request, ngx_http_mp4_module); | |
1014 | |
1015 if (atom_data_size > mp4->buffer_size) { | |
1016 | |
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 if (atom_data_size > conf->max_buffer_size) { |
4085 | 1018 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
1019 "\"%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
|
1020 "you may want to increase mp4_max_buffer_size", |
4085 | 1021 mp4->file.name.data, atom_data_size); |
1022 return NGX_ERROR; | |
1023 } | |
1024 | |
1025 ngx_pfree(mp4->request->pool, mp4->buffer); | |
1026 mp4->buffer = NULL; | |
1027 mp4->buffer_pos = NULL; | |
1028 mp4->buffer_end = NULL; | |
1029 | |
1030 mp4->buffer_size = (size_t) atom_data_size | |
1031 + NGX_HTTP_MP4_MOOV_BUFFER_EXCESS * no_mdat; | |
1032 } | |
1033 | |
4727
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
1034 if (ngx_http_mp4_read(mp4, (size_t) atom_data_size) != NGX_OK) { |
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
1035 return NGX_ERROR; |
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
1036 } |
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
1037 |
4085 | 1038 mp4->trak.elts = &mp4->traks; |
1039 mp4->trak.size = sizeof(ngx_http_mp4_trak_t); | |
1040 mp4->trak.nalloc = 2; | |
1041 mp4->trak.pool = mp4->request->pool; | |
1042 | |
1043 atom = &mp4->moov_atom_buf; | |
1044 atom->temporary = 1; | |
1045 atom->pos = mp4->moov_atom_header; | |
1046 atom->last = mp4->moov_atom_header + 8; | |
1047 | |
1048 mp4->moov_atom.buf = &mp4->moov_atom_buf; | |
1049 | |
1050 rc = ngx_http_mp4_read_atom(mp4, ngx_http_mp4_moov_atoms, atom_data_size); | |
1051 | |
1052 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 moov atom done"); | |
1053 | |
1054 if (no_mdat) { | |
1055 mp4->buffer_start = mp4->buffer_pos; | |
1056 mp4->buffer_size = NGX_HTTP_MP4_MOOV_BUFFER_EXCESS; | |
1057 | |
4727
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
1058 if (mp4->buffer_start + mp4->buffer_size > mp4->buffer_end) { |
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
1059 mp4->buffer = NULL; |
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
1060 mp4->buffer_pos = NULL; |
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
1061 mp4->buffer_end = NULL; |
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
1062 } |
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
1063 |
4085 | 1064 } else { |
1065 /* skip atoms after moov atom */ | |
1066 mp4->offset = mp4->end; | |
1067 } | |
1068 | |
1069 return rc; | |
1070 } | |
1071 | |
1072 | |
1073 static ngx_int_t | |
1074 ngx_http_mp4_read_mdat_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1075 { | |
1076 ngx_buf_t *data; | |
1077 | |
1078 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 mdat atom"); | |
1079 | |
1080 data = &mp4->mdat_data_buf; | |
1081 data->file = &mp4->file; | |
1082 data->in_file = 1; | |
1083 data->last_buf = 1; | |
1084 data->last_in_chain = 1; | |
1085 data->file_last = mp4->offset + atom_data_size; | |
1086 | |
1087 mp4->mdat_atom.buf = &mp4->mdat_atom_buf; | |
1088 mp4->mdat_atom.next = &mp4->mdat_data; | |
1089 mp4->mdat_data.buf = data; | |
1090 | |
1091 if (mp4->trak.nelts) { | |
1092 /* skip atoms after mdat atom */ | |
1093 mp4->offset = mp4->end; | |
1094 | |
1095 } else { | |
1096 ngx_mp4_atom_next(mp4, atom_data_size); | |
1097 } | |
1098 | |
1099 return NGX_OK; | |
1100 } | |
1101 | |
1102 | |
1103 static size_t | |
1104 ngx_http_mp4_update_mdat_atom(ngx_http_mp4_file_t *mp4, off_t start_offset) | |
1105 { | |
1106 off_t atom_data_size; | |
1107 u_char *atom_header; | |
1108 uint32_t atom_header_size; | |
1109 uint64_t atom_size; | |
1110 ngx_buf_t *atom; | |
1111 | |
1112 atom_data_size = mp4->mdat_data.buf->file_last - start_offset; | |
1113 mp4->mdat_data.buf->file_pos = start_offset; | |
1114 | |
4088
8fe1da7b8386
bugfix of r4086: nginx could not be built without debug log.
Igor Sysoev <igor@sysoev.ru>
parents:
4087
diff
changeset
|
1115 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, |
4085 | 1116 "mdat new offset @%O:%O", start_offset, atom_data_size); |
1117 | |
1118 atom_header = mp4->mdat_atom_header; | |
1119 | |
4155
d9636bf3f159
Fix of building on platforms with 32-bit off_t. (closed #23)
Igor Sysoev <igor@sysoev.ru>
parents:
4112
diff
changeset
|
1120 if ((uint64_t) atom_data_size > 0xffffffff) { |
4085 | 1121 atom_size = 1; |
1122 atom_header_size = sizeof(ngx_mp4_atom_header64_t); | |
1123 ngx_mp4_set_64value(atom_header + sizeof(ngx_mp4_atom_header_t), | |
1124 sizeof(ngx_mp4_atom_header64_t) + atom_data_size); | |
1125 } else { | |
1126 atom_size = sizeof(ngx_mp4_atom_header_t) + atom_data_size; | |
1127 atom_header_size = sizeof(ngx_mp4_atom_header_t); | |
1128 } | |
1129 | |
4282
71cdac0b9ea6
Fix of "Content-Length" header of MP4 response if start argument was used.
Igor Sysoev <igor@sysoev.ru>
parents:
4189
diff
changeset
|
1130 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
|
1131 |
4085 | 1132 ngx_mp4_set_32value(atom_header, atom_size); |
1133 ngx_mp4_set_atom_name(atom_header, 'm', 'd', 'a', 't'); | |
1134 | |
1135 atom = &mp4->mdat_atom_buf; | |
1136 atom->temporary = 1; | |
1137 atom->pos = atom_header; | |
1138 atom->last = atom_header + atom_header_size; | |
1139 | |
1140 return atom_header_size; | |
1141 } | |
1142 | |
1143 | |
1144 typedef struct { | |
1145 u_char size[4]; | |
1146 u_char name[4]; | |
1147 u_char version[1]; | |
1148 u_char flags[3]; | |
1149 u_char creation_time[4]; | |
1150 u_char modification_time[4]; | |
1151 u_char timescale[4]; | |
1152 u_char duration[4]; | |
1153 u_char rate[4]; | |
1154 u_char volume[2]; | |
1155 u_char reserved[10]; | |
1156 u_char matrix[36]; | |
1157 u_char preview_time[4]; | |
1158 u_char preview_duration[4]; | |
1159 u_char poster_time[4]; | |
1160 u_char selection_time[4]; | |
1161 u_char selection_duration[4]; | |
1162 u_char current_time[4]; | |
1163 u_char next_track_id[4]; | |
1164 } ngx_mp4_mvhd_atom_t; | |
1165 | |
1166 typedef struct { | |
1167 u_char size[4]; | |
1168 u_char name[4]; | |
1169 u_char version[1]; | |
1170 u_char flags[3]; | |
1171 u_char creation_time[8]; | |
1172 u_char modification_time[8]; | |
1173 u_char timescale[4]; | |
1174 u_char duration[8]; | |
1175 u_char rate[4]; | |
1176 u_char volume[2]; | |
1177 u_char reserved[10]; | |
1178 u_char matrix[36]; | |
1179 u_char preview_time[4]; | |
1180 u_char preview_duration[4]; | |
1181 u_char poster_time[4]; | |
1182 u_char selection_time[4]; | |
1183 u_char selection_duration[4]; | |
1184 u_char current_time[4]; | |
1185 u_char next_track_id[4]; | |
1186 } ngx_mp4_mvhd64_atom_t; | |
1187 | |
1188 | |
1189 static ngx_int_t | |
1190 ngx_http_mp4_read_mvhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1191 { | |
1192 u_char *atom_header; | |
1193 size_t atom_size; | |
1194 uint32_t timescale; | |
1195 uint64_t duration; | |
1196 ngx_buf_t *atom; | |
1197 ngx_mp4_mvhd_atom_t *mvhd_atom; | |
1198 ngx_mp4_mvhd64_atom_t *mvhd64_atom; | |
1199 | |
1200 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 mvhd atom"); | |
1201 | |
1202 atom_header = ngx_mp4_atom_header(mp4); | |
1203 mvhd_atom = (ngx_mp4_mvhd_atom_t *) atom_header; | |
1204 mvhd64_atom = (ngx_mp4_mvhd64_atom_t *) atom_header; | |
1205 ngx_mp4_set_atom_name(atom_header, 'm', 'v', 'h', 'd'); | |
1206 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1207 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
|
1208 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1209 "\"%s\" mp4 mvhd atom too small", mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1210 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1211 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1212 |
4085 | 1213 if (mvhd_atom->version[0] == 0) { |
1214 /* version 0: 32-bit duration */ | |
1215 timescale = ngx_mp4_get_32value(mvhd_atom->timescale); | |
1216 duration = ngx_mp4_get_32value(mvhd_atom->duration); | |
1217 | |
1218 } else { | |
1219 /* version 1: 64-bit duration */ | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1220 |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1221 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
|
1222 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1223 "\"%s\" mp4 mvhd atom too small", |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1224 mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1225 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1226 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1227 |
4085 | 1228 timescale = ngx_mp4_get_32value(mvhd64_atom->timescale); |
1229 duration = ngx_mp4_get_64value(mvhd64_atom->duration); | |
1230 } | |
1231 | |
1232 mp4->timescale = timescale; | |
1233 | |
1234 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1235 "mvhd timescale:%uD, duration:%uL, time:%.3fs", | |
1236 timescale, duration, (double) duration / timescale); | |
1237 | |
1238 duration -= (uint64_t) mp4->start * timescale / 1000; | |
1239 | |
1240 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1241 "mvhd new duration:%uL, time:%.3fs", | |
1242 duration, (double) duration / timescale); | |
1243 | |
1244 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; | |
1245 ngx_mp4_set_32value(mvhd_atom->size, atom_size); | |
1246 | |
1247 if (mvhd_atom->version[0] == 0) { | |
1248 ngx_mp4_set_32value(mvhd_atom->duration, duration); | |
1249 | |
1250 } else { | |
1251 ngx_mp4_set_64value(mvhd64_atom->duration, duration); | |
1252 } | |
1253 | |
1254 atom = &mp4->mvhd_atom_buf; | |
1255 atom->temporary = 1; | |
1256 atom->pos = atom_header; | |
1257 atom->last = atom_header + atom_size; | |
1258 | |
1259 mp4->mvhd_atom.buf = atom; | |
1260 | |
1261 ngx_mp4_atom_next(mp4, atom_data_size); | |
1262 | |
1263 return NGX_OK; | |
1264 } | |
1265 | |
1266 | |
1267 static ngx_int_t | |
1268 ngx_http_mp4_read_trak_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1269 { | |
4099
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1270 u_char *atom_header, *atom_end; |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1271 off_t atom_file_end; |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1272 ngx_int_t rc; |
4085 | 1273 ngx_buf_t *atom; |
1274 ngx_http_mp4_trak_t *trak; | |
1275 | |
1276 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 trak atom"); | |
1277 | |
1278 trak = ngx_array_push(&mp4->trak); | |
1279 if (trak == NULL) { | |
1280 return NGX_ERROR; | |
1281 } | |
1282 | |
1283 ngx_memzero(trak, sizeof(ngx_http_mp4_trak_t)); | |
1284 | |
1285 atom_header = ngx_mp4_atom_header(mp4); | |
1286 ngx_mp4_set_atom_name(atom_header, 't', 'r', 'a', 'k'); | |
1287 | |
1288 atom = &trak->trak_atom_buf; | |
1289 atom->temporary = 1; | |
1290 atom->pos = atom_header; | |
1291 atom->last = atom_header + sizeof(ngx_mp4_atom_header_t); | |
1292 | |
1293 trak->out[NGX_HTTP_MP4_TRAK_ATOM].buf = atom; | |
1294 | |
4099
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1295 atom_end = mp4->buffer_pos + atom_data_size; |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1296 atom_file_end = mp4->offset + atom_data_size; |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1297 |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1298 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
|
1299 |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1300 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
|
1301 "mp4 trak atom: %i", rc); |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1302 |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1303 if (rc == NGX_DECLINED) { |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1304 /* skip this trak */ |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1305 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
|
1306 mp4->trak.nelts--; |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1307 mp4->buffer_pos = atom_end; |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1308 mp4->offset = atom_file_end; |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1309 return NGX_OK; |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1310 } |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1311 |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1312 return rc; |
4085 | 1313 } |
1314 | |
1315 | |
1316 static void | |
1317 ngx_http_mp4_update_trak_atom(ngx_http_mp4_file_t *mp4, | |
1318 ngx_http_mp4_trak_t *trak) | |
1319 { | |
1320 ngx_buf_t *atom; | |
1321 | |
1322 trak->size += sizeof(ngx_mp4_atom_header_t); | |
1323 atom = &trak->trak_atom_buf; | |
1324 ngx_mp4_set_32value(atom->pos, trak->size); | |
1325 } | |
1326 | |
1327 | |
1328 static ngx_int_t | |
1329 ngx_http_mp4_read_cmov_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1330 { | |
1331 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
1332 "\"%s\" mp4 compressed moov atom (cmov) is not supported", | |
1333 mp4->file.name.data); | |
1334 | |
1335 return NGX_ERROR; | |
1336 } | |
1337 | |
1338 | |
1339 typedef struct { | |
1340 u_char size[4]; | |
1341 u_char name[4]; | |
1342 u_char version[1]; | |
1343 u_char flags[3]; | |
1344 u_char creation_time[4]; | |
1345 u_char modification_time[4]; | |
1346 u_char track_id[4]; | |
1347 u_char reserved1[4]; | |
1348 u_char duration[4]; | |
1349 u_char reserved2[8]; | |
1350 u_char layer[2]; | |
1351 u_char group[2]; | |
1352 u_char volume[2]; | |
1353 u_char reverved3[2]; | |
1354 u_char matrix[36]; | |
1355 u_char width[4]; | |
1356 u_char heigth[4]; | |
1357 } ngx_mp4_tkhd_atom_t; | |
1358 | |
1359 typedef struct { | |
1360 u_char size[4]; | |
1361 u_char name[4]; | |
1362 u_char version[1]; | |
1363 u_char flags[3]; | |
1364 u_char creation_time[8]; | |
1365 u_char modification_time[8]; | |
1366 u_char track_id[4]; | |
1367 u_char reserved1[4]; | |
1368 u_char duration[8]; | |
1369 u_char reserved2[8]; | |
1370 u_char layer[2]; | |
1371 u_char group[2]; | |
1372 u_char volume[2]; | |
1373 u_char reverved3[2]; | |
1374 u_char matrix[36]; | |
1375 u_char width[4]; | |
1376 u_char heigth[4]; | |
1377 } ngx_mp4_tkhd64_atom_t; | |
1378 | |
1379 | |
1380 static ngx_int_t | |
1381 ngx_http_mp4_read_tkhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1382 { | |
1383 u_char *atom_header; | |
1384 size_t atom_size; | |
1385 uint64_t duration; | |
1386 ngx_buf_t *atom; | |
1387 ngx_http_mp4_trak_t *trak; | |
1388 ngx_mp4_tkhd_atom_t *tkhd_atom; | |
1389 ngx_mp4_tkhd64_atom_t *tkhd64_atom; | |
1390 | |
1391 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 tkhd atom"); | |
1392 | |
1393 atom_header = ngx_mp4_atom_header(mp4); | |
1394 tkhd_atom = (ngx_mp4_tkhd_atom_t *) atom_header; | |
1395 tkhd64_atom = (ngx_mp4_tkhd64_atom_t *) atom_header; | |
1396 ngx_mp4_set_atom_name(tkhd_atom, 't', 'k', 'h', 'd'); | |
1397 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1398 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
|
1399 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1400 "\"%s\" mp4 tkhd atom too small", mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1401 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1402 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1403 |
4085 | 1404 if (tkhd_atom->version[0] == 0) { |
1405 /* version 0: 32-bit duration */ | |
1406 duration = ngx_mp4_get_32value(tkhd_atom->duration); | |
1407 | |
1408 } else { | |
1409 /* version 1: 64-bit duration */ | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1410 |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1411 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
|
1412 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1413 "\"%s\" mp4 tkhd atom too small", |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1414 mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1415 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1416 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1417 |
4085 | 1418 duration = ngx_mp4_get_64value(tkhd64_atom->duration); |
1419 } | |
1420 | |
1421 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1422 "tkhd duration:%uL, time:%.3fs", | |
1423 duration, (double) duration / mp4->timescale); | |
1424 | |
1425 duration -= (uint64_t) mp4->start * mp4->timescale / 1000; | |
1426 | |
1427 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1428 "tkhd new duration:%uL, time:%.3fs", | |
1429 duration, (double) duration / mp4->timescale); | |
1430 | |
1431 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; | |
1432 | |
1433 trak = ngx_mp4_last_trak(mp4); | |
1434 trak->tkhd_size = atom_size; | |
1435 | |
1436 ngx_mp4_set_32value(tkhd_atom->size, atom_size); | |
1437 | |
1438 if (tkhd_atom->version[0] == 0) { | |
1439 ngx_mp4_set_32value(tkhd_atom->duration, duration); | |
1440 | |
1441 } else { | |
1442 ngx_mp4_set_64value(tkhd64_atom->duration, duration); | |
1443 } | |
1444 | |
1445 atom = &trak->tkhd_atom_buf; | |
1446 atom->temporary = 1; | |
1447 atom->pos = atom_header; | |
1448 atom->last = atom_header + atom_size; | |
1449 | |
1450 trak->out[NGX_HTTP_MP4_TKHD_ATOM].buf = atom; | |
1451 | |
1452 ngx_mp4_atom_next(mp4, atom_data_size); | |
1453 | |
1454 return NGX_OK; | |
1455 } | |
1456 | |
1457 | |
1458 static ngx_int_t | |
1459 ngx_http_mp4_read_mdia_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1460 { | |
1461 u_char *atom_header; | |
1462 ngx_buf_t *atom; | |
1463 ngx_http_mp4_trak_t *trak; | |
1464 | |
1465 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "process mdia atom"); | |
1466 | |
1467 atom_header = ngx_mp4_atom_header(mp4); | |
1468 ngx_mp4_set_atom_name(atom_header, 'm', 'd', 'i', 'a'); | |
1469 | |
1470 trak = ngx_mp4_last_trak(mp4); | |
1471 | |
1472 atom = &trak->mdia_atom_buf; | |
1473 atom->temporary = 1; | |
1474 atom->pos = atom_header; | |
1475 atom->last = atom_header + sizeof(ngx_mp4_atom_header_t); | |
1476 | |
1477 trak->out[NGX_HTTP_MP4_MDIA_ATOM].buf = atom; | |
1478 | |
1479 return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_mdia_atoms, atom_data_size); | |
1480 } | |
1481 | |
1482 | |
1483 static void | |
1484 ngx_http_mp4_update_mdia_atom(ngx_http_mp4_file_t *mp4, | |
1485 ngx_http_mp4_trak_t *trak) | |
1486 { | |
1487 ngx_buf_t *atom; | |
1488 | |
1489 trak->size += sizeof(ngx_mp4_atom_header_t); | |
1490 atom = &trak->mdia_atom_buf; | |
1491 ngx_mp4_set_32value(atom->pos, trak->size); | |
1492 } | |
1493 | |
1494 | |
1495 typedef struct { | |
1496 u_char size[4]; | |
1497 u_char name[4]; | |
1498 u_char version[1]; | |
1499 u_char flags[3]; | |
1500 u_char creation_time[4]; | |
1501 u_char modification_time[4]; | |
1502 u_char timescale[4]; | |
1503 u_char duration[4]; | |
1504 u_char language[2]; | |
1505 u_char quality[2]; | |
1506 } ngx_mp4_mdhd_atom_t; | |
1507 | |
1508 typedef struct { | |
1509 u_char size[4]; | |
1510 u_char name[4]; | |
1511 u_char version[1]; | |
1512 u_char flags[3]; | |
1513 u_char creation_time[8]; | |
1514 u_char modification_time[8]; | |
1515 u_char timescale[4]; | |
1516 u_char duration[8]; | |
1517 u_char language[2]; | |
1518 u_char quality[2]; | |
1519 } ngx_mp4_mdhd64_atom_t; | |
1520 | |
1521 | |
1522 static ngx_int_t | |
1523 ngx_http_mp4_read_mdhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1524 { | |
1525 u_char *atom_header; | |
1526 size_t atom_size; | |
1527 uint32_t timescale; | |
1528 uint64_t duration; | |
1529 ngx_buf_t *atom; | |
1530 ngx_http_mp4_trak_t *trak; | |
1531 ngx_mp4_mdhd_atom_t *mdhd_atom; | |
1532 ngx_mp4_mdhd64_atom_t *mdhd64_atom; | |
1533 | |
1534 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 mdhd atom"); | |
1535 | |
1536 atom_header = ngx_mp4_atom_header(mp4); | |
1537 mdhd_atom = (ngx_mp4_mdhd_atom_t *) atom_header; | |
1538 mdhd64_atom = (ngx_mp4_mdhd64_atom_t *) atom_header; | |
1539 ngx_mp4_set_atom_name(mdhd_atom, 'm', 'd', 'h', 'd'); | |
1540 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1541 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
|
1542 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1543 "\"%s\" mp4 mdhd atom too small", mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1544 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1545 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1546 |
4085 | 1547 if (mdhd_atom->version[0] == 0) { |
1548 /* version 0: everything is 32-bit */ | |
1549 timescale = ngx_mp4_get_32value(mdhd_atom->timescale); | |
1550 duration = ngx_mp4_get_32value(mdhd_atom->duration); | |
1551 | |
1552 } else { | |
1553 /* version 1: 64-bit duration and 32-bit timescale */ | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1554 |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1555 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
|
1556 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1557 "\"%s\" mp4 mdhd atom too small", |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1558 mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1559 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1560 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1561 |
4085 | 1562 timescale = ngx_mp4_get_32value(mdhd64_atom->timescale); |
1563 duration = ngx_mp4_get_64value(mdhd64_atom->duration); | |
1564 } | |
1565 | |
1566 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1567 "mdhd timescale:%uD, duration:%uL, time:%.3fs", | |
1568 timescale, duration, (double) duration / timescale); | |
1569 | |
1570 duration -= (uint64_t) mp4->start * timescale / 1000; | |
1571 | |
1572 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1573 "mdhd new duration:%uL, time:%.3fs", | |
1574 duration, (double) duration / timescale); | |
1575 | |
1576 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; | |
1577 | |
1578 trak = ngx_mp4_last_trak(mp4); | |
1579 trak->mdhd_size = atom_size; | |
1580 trak->timescale = timescale; | |
1581 | |
1582 ngx_mp4_set_32value(mdhd_atom->size, atom_size); | |
1583 | |
1584 if (mdhd_atom->version[0] == 0) { | |
1585 ngx_mp4_set_32value(mdhd_atom->duration, duration); | |
1586 | |
1587 } else { | |
1588 ngx_mp4_set_64value(mdhd64_atom->duration, duration); | |
1589 } | |
1590 | |
1591 atom = &trak->mdhd_atom_buf; | |
1592 atom->temporary = 1; | |
1593 atom->pos = atom_header; | |
1594 atom->last = atom_header + atom_size; | |
1595 | |
1596 trak->out[NGX_HTTP_MP4_MDHD_ATOM].buf = atom; | |
1597 | |
1598 ngx_mp4_atom_next(mp4, atom_data_size); | |
1599 | |
1600 return NGX_OK; | |
1601 } | |
1602 | |
1603 | |
1604 static ngx_int_t | |
1605 ngx_http_mp4_read_hdlr_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1606 { | |
1607 u_char *atom_header; | |
1608 size_t atom_size; | |
1609 ngx_buf_t *atom; | |
1610 ngx_http_mp4_trak_t *trak; | |
1611 | |
1612 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 hdlr atom"); | |
1613 | |
1614 atom_header = ngx_mp4_atom_header(mp4); | |
1615 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; | |
1616 ngx_mp4_set_32value(atom_header, atom_size); | |
1617 ngx_mp4_set_atom_name(atom_header, 'h', 'd', 'l', 'r'); | |
1618 | |
1619 trak = ngx_mp4_last_trak(mp4); | |
1620 | |
1621 atom = &trak->hdlr_atom_buf; | |
1622 atom->temporary = 1; | |
1623 atom->pos = atom_header; | |
1624 atom->last = atom_header + atom_size; | |
1625 | |
1626 trak->hdlr_size = atom_size; | |
1627 trak->out[NGX_HTTP_MP4_HDLR_ATOM].buf = atom; | |
1628 | |
1629 ngx_mp4_atom_next(mp4, atom_data_size); | |
1630 | |
1631 return NGX_OK; | |
1632 } | |
1633 | |
1634 | |
1635 static ngx_int_t | |
1636 ngx_http_mp4_read_minf_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1637 { | |
1638 u_char *atom_header; | |
1639 ngx_buf_t *atom; | |
1640 ngx_http_mp4_trak_t *trak; | |
1641 | |
1642 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "process minf atom"); | |
1643 | |
1644 atom_header = ngx_mp4_atom_header(mp4); | |
1645 ngx_mp4_set_atom_name(atom_header, 'm', 'i', 'n', 'f'); | |
1646 | |
1647 trak = ngx_mp4_last_trak(mp4); | |
1648 | |
1649 atom = &trak->minf_atom_buf; | |
1650 atom->temporary = 1; | |
1651 atom->pos = atom_header; | |
1652 atom->last = atom_header + sizeof(ngx_mp4_atom_header_t); | |
1653 | |
1654 trak->out[NGX_HTTP_MP4_MINF_ATOM].buf = atom; | |
1655 | |
1656 return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_minf_atoms, atom_data_size); | |
1657 } | |
1658 | |
1659 | |
1660 static void | |
1661 ngx_http_mp4_update_minf_atom(ngx_http_mp4_file_t *mp4, | |
1662 ngx_http_mp4_trak_t *trak) | |
1663 { | |
1664 ngx_buf_t *atom; | |
1665 | |
1666 trak->size += sizeof(ngx_mp4_atom_header_t) | |
1667 + trak->vmhd_size | |
1668 + trak->smhd_size | |
1669 + trak->dinf_size; | |
1670 atom = &trak->minf_atom_buf; | |
1671 ngx_mp4_set_32value(atom->pos, trak->size); | |
1672 } | |
1673 | |
1674 | |
1675 static ngx_int_t | |
1676 ngx_http_mp4_read_vmhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1677 { | |
1678 u_char *atom_header; | |
1679 size_t atom_size; | |
1680 ngx_buf_t *atom; | |
1681 ngx_http_mp4_trak_t *trak; | |
1682 | |
1683 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 vmhd atom"); | |
1684 | |
1685 atom_header = ngx_mp4_atom_header(mp4); | |
1686 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; | |
1687 ngx_mp4_set_32value(atom_header, atom_size); | |
1688 ngx_mp4_set_atom_name(atom_header, 'v', 'm', 'h', 'd'); | |
1689 | |
1690 trak = ngx_mp4_last_trak(mp4); | |
1691 | |
1692 atom = &trak->vmhd_atom_buf; | |
1693 atom->temporary = 1; | |
1694 atom->pos = atom_header; | |
1695 atom->last = atom_header + atom_size; | |
1696 | |
1697 trak->vmhd_size += atom_size; | |
1698 trak->out[NGX_HTTP_MP4_VMHD_ATOM].buf = atom; | |
1699 | |
1700 ngx_mp4_atom_next(mp4, atom_data_size); | |
1701 | |
1702 return NGX_OK; | |
1703 } | |
1704 | |
1705 | |
1706 static ngx_int_t | |
1707 ngx_http_mp4_read_smhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1708 { | |
1709 u_char *atom_header; | |
1710 size_t atom_size; | |
1711 ngx_buf_t *atom; | |
1712 ngx_http_mp4_trak_t *trak; | |
1713 | |
1714 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 smhd atom"); | |
1715 | |
1716 atom_header = ngx_mp4_atom_header(mp4); | |
1717 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; | |
1718 ngx_mp4_set_32value(atom_header, atom_size); | |
1719 ngx_mp4_set_atom_name(atom_header, 's', 'm', 'h', 'd'); | |
1720 | |
1721 trak = ngx_mp4_last_trak(mp4); | |
1722 | |
1723 atom = &trak->smhd_atom_buf; | |
1724 atom->temporary = 1; | |
1725 atom->pos = atom_header; | |
1726 atom->last = atom_header + atom_size; | |
1727 | |
1728 trak->vmhd_size += atom_size; | |
1729 trak->out[NGX_HTTP_MP4_SMHD_ATOM].buf = atom; | |
1730 | |
1731 ngx_mp4_atom_next(mp4, atom_data_size); | |
1732 | |
1733 return NGX_OK; | |
1734 } | |
1735 | |
1736 | |
1737 static ngx_int_t | |
1738 ngx_http_mp4_read_dinf_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1739 { | |
1740 u_char *atom_header; | |
1741 size_t atom_size; | |
1742 ngx_buf_t *atom; | |
1743 ngx_http_mp4_trak_t *trak; | |
1744 | |
1745 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 dinf atom"); | |
1746 | |
1747 atom_header = ngx_mp4_atom_header(mp4); | |
1748 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; | |
1749 ngx_mp4_set_32value(atom_header, atom_size); | |
1750 ngx_mp4_set_atom_name(atom_header, 'd', 'i', 'n', 'f'); | |
1751 | |
1752 trak = ngx_mp4_last_trak(mp4); | |
1753 | |
1754 atom = &trak->dinf_atom_buf; | |
1755 atom->temporary = 1; | |
1756 atom->pos = atom_header; | |
1757 atom->last = atom_header + atom_size; | |
1758 | |
1759 trak->dinf_size += atom_size; | |
1760 trak->out[NGX_HTTP_MP4_DINF_ATOM].buf = atom; | |
1761 | |
1762 ngx_mp4_atom_next(mp4, atom_data_size); | |
1763 | |
1764 return NGX_OK; | |
1765 } | |
1766 | |
1767 | |
1768 static ngx_int_t | |
1769 ngx_http_mp4_read_stbl_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1770 { | |
1771 u_char *atom_header; | |
1772 ngx_buf_t *atom; | |
1773 ngx_http_mp4_trak_t *trak; | |
1774 | |
1775 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "process stbl atom"); | |
1776 | |
1777 atom_header = ngx_mp4_atom_header(mp4); | |
1778 ngx_mp4_set_atom_name(atom_header, 's', 't', 'b', 'l'); | |
1779 | |
1780 trak = ngx_mp4_last_trak(mp4); | |
1781 | |
1782 atom = &trak->stbl_atom_buf; | |
1783 atom->temporary = 1; | |
1784 atom->pos = atom_header; | |
1785 atom->last = atom_header + sizeof(ngx_mp4_atom_header_t); | |
1786 | |
1787 trak->out[NGX_HTTP_MP4_STBL_ATOM].buf = atom; | |
1788 | |
1789 return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_stbl_atoms, atom_data_size); | |
1790 } | |
1791 | |
1792 | |
1793 static void | |
1794 ngx_http_mp4_update_stbl_atom(ngx_http_mp4_file_t *mp4, | |
1795 ngx_http_mp4_trak_t *trak) | |
1796 { | |
1797 ngx_buf_t *atom; | |
1798 | |
1799 trak->size += sizeof(ngx_mp4_atom_header_t); | |
1800 atom = &trak->stbl_atom_buf; | |
1801 ngx_mp4_set_32value(atom->pos, trak->size); | |
1802 } | |
1803 | |
1804 | |
1805 typedef struct { | |
1806 u_char size[4]; | |
1807 u_char name[4]; | |
1808 u_char version[1]; | |
1809 u_char flags[3]; | |
1810 u_char entries[4]; | |
4099
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1811 |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1812 u_char media_size[4]; |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1813 u_char media_name[4]; |
4085 | 1814 } ngx_mp4_stsd_atom_t; |
1815 | |
1816 | |
1817 static ngx_int_t | |
1818 ngx_http_mp4_read_stsd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1819 { | |
1820 u_char *atom_header, *atom_table; | |
1821 size_t atom_size; | |
1822 ngx_buf_t *atom; | |
1823 ngx_mp4_stsd_atom_t *stsd_atom; | |
1824 ngx_http_mp4_trak_t *trak; | |
1825 | |
1826 /* sample description atom */ | |
1827 | |
1828 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stsd atom"); | |
1829 | |
1830 atom_header = ngx_mp4_atom_header(mp4); | |
1831 stsd_atom = (ngx_mp4_stsd_atom_t *) atom_header; | |
1832 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; | |
1833 atom_table = atom_header + atom_size; | |
1834 ngx_mp4_set_32value(stsd_atom->size, atom_size); | |
1835 ngx_mp4_set_atom_name(stsd_atom, 's', 't', 's', 'd'); | |
1836 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1837 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
|
1838 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
|
1839 "\"%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
|
1840 return NGX_ERROR; |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1841 } |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1842 |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1843 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
|
1844 "stsd entries:%uD, media:%*s", |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1845 ngx_mp4_get_32value(stsd_atom->entries), |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1846 4, stsd_atom->media_name); |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1847 |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1848 /* 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
|
1849 |
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1850 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
|
1851 && ngx_strncmp(stsd_atom->media_name, "mp4a", 4) != 0) |
4085 | 1852 { |
4099
9ee6944590c0
Skipping traks with unsupported media formats.
Igor Sysoev <igor@sysoev.ru>
parents:
4098
diff
changeset
|
1853 return NGX_DECLINED; |
4085 | 1854 } |
1855 | |
1856 trak = ngx_mp4_last_trak(mp4); | |
1857 | |
1858 atom = &trak->stsd_atom_buf; | |
1859 atom->temporary = 1; | |
1860 atom->pos = atom_header; | |
1861 atom->last = atom_table; | |
1862 | |
1863 trak->out[NGX_HTTP_MP4_STSD_ATOM].buf = atom; | |
1864 trak->size += atom_size; | |
1865 | |
1866 ngx_mp4_atom_next(mp4, atom_data_size); | |
1867 | |
1868 return NGX_OK; | |
1869 } | |
1870 | |
1871 | |
1872 typedef struct { | |
1873 u_char size[4]; | |
1874 u_char name[4]; | |
1875 u_char version[1]; | |
1876 u_char flags[3]; | |
1877 u_char entries[4]; | |
1878 } ngx_mp4_stts_atom_t; | |
1879 | |
1880 typedef struct { | |
1881 u_char count[4]; | |
1882 u_char duration[4]; | |
1883 } ngx_mp4_stts_entry_t; | |
1884 | |
1885 | |
1886 static ngx_int_t | |
1887 ngx_http_mp4_read_stts_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
1888 { | |
1889 u_char *atom_header, *atom_table, *atom_end; | |
1890 uint32_t entries; | |
1891 ngx_buf_t *atom, *data; | |
1892 ngx_mp4_stts_atom_t *stts_atom; | |
1893 ngx_http_mp4_trak_t *trak; | |
1894 | |
1895 /* time-to-sample atom */ | |
1896 | |
1897 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stts atom"); | |
1898 | |
1899 atom_header = ngx_mp4_atom_header(mp4); | |
1900 stts_atom = (ngx_mp4_stts_atom_t *) atom_header; | |
1901 ngx_mp4_set_atom_name(stts_atom, 's', 't', 't', 's'); | |
1902 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1903 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
|
1904 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1905 "\"%s\" mp4 stts atom too small", mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1906 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1907 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1908 |
4085 | 1909 entries = ngx_mp4_get_32value(stts_atom->entries); |
1910 | |
1911 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
4096 | 1912 "mp4 time-to-sample entries:%uD", entries); |
4085 | 1913 |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1914 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
|
1915 + entries * sizeof(ngx_mp4_stts_entry_t) > atom_data_size) |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1916 { |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1917 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1918 "\"%s\" mp4 stts atom too small", mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1919 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1920 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
1921 |
4085 | 1922 atom_table = atom_header + sizeof(ngx_mp4_stts_atom_t); |
1923 atom_end = atom_table + entries * sizeof(ngx_mp4_stts_entry_t); | |
1924 | |
1925 trak = ngx_mp4_last_trak(mp4); | |
1926 trak->time_to_sample_entries = entries; | |
1927 | |
1928 atom = &trak->stts_atom_buf; | |
1929 atom->temporary = 1; | |
1930 atom->pos = atom_header; | |
1931 atom->last = atom_table; | |
1932 | |
1933 data = &trak->stts_data_buf; | |
1934 data->temporary = 1; | |
1935 data->pos = atom_table; | |
1936 data->last = atom_end; | |
1937 | |
1938 trak->out[NGX_HTTP_MP4_STTS_ATOM].buf = atom; | |
1939 trak->out[NGX_HTTP_MP4_STTS_DATA].buf = data; | |
1940 | |
1941 ngx_mp4_atom_next(mp4, atom_data_size); | |
1942 | |
1943 return NGX_OK; | |
1944 } | |
1945 | |
1946 | |
1947 static ngx_int_t | |
1948 ngx_http_mp4_update_stts_atom(ngx_http_mp4_file_t *mp4, | |
1949 ngx_http_mp4_trak_t *trak) | |
1950 { | |
1951 size_t atom_size; | |
1952 uint32_t entries, count, duration; | |
1953 uint64_t start_time; | |
1954 ngx_buf_t *atom, *data; | |
1955 ngx_uint_t start_sample; | |
1956 ngx_mp4_stts_atom_t *stts_atom; | |
1957 ngx_mp4_stts_entry_t *entry, *end; | |
1958 | |
1959 /* | |
1960 * mdia.minf.stbl.stts updating requires trak->timescale | |
1961 * from mdia.mdhd atom which may reside after mdia.minf | |
1962 */ | |
1963 | |
1964 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1965 "mp4 stts atom update"); | |
1966 | |
1967 data = trak->out[NGX_HTTP_MP4_STTS_DATA].buf; | |
1968 | |
1969 if (data == NULL) { | |
1970 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
1971 "no mp4 stts atoms were found in \"%s\"", | |
1972 mp4->file.name.data); | |
1973 return NGX_ERROR; | |
1974 } | |
1975 | |
1976 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
|
1977 start_time = (uint64_t) mp4->start * trak->timescale / 1000; |
4085 | 1978 |
1979 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
4096 | 1980 "time-to-sample start_time:%uL", start_time); |
4085 | 1981 |
1982 start_sample = 0; | |
1983 entry = (ngx_mp4_stts_entry_t *) data->pos; | |
1984 end = (ngx_mp4_stts_entry_t *) data->last; | |
1985 | |
1986 while (entry < end) { | |
1987 count = ngx_mp4_get_32value(entry->count); | |
1988 duration = ngx_mp4_get_32value(entry->duration); | |
1989 | |
1990 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1991 "count:%uD, duration:%uD", count, duration); | |
1992 | |
4306 | 1993 if (start_time < (uint64_t) count * duration) { |
4085 | 1994 start_sample += (ngx_uint_t) (start_time / duration); |
4578 | 1995 count -= (uint32_t) (start_time / duration); |
4085 | 1996 ngx_mp4_set_32value(entry->count, count); |
1997 goto found; | |
1998 } | |
1999 | |
2000 start_sample += count; | |
2001 start_time -= count * duration; | |
2002 entries--; | |
2003 entry++; | |
2004 } | |
2005 | |
2006 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
4094 | 2007 "start time is out mp4 stts samples in \"%s\"", |
4085 | 2008 mp4->file.name.data); |
2009 | |
2010 return NGX_ERROR; | |
2011 | |
2012 found: | |
2013 | |
2014 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2015 "start_sample:%ui, new count:%uD", start_sample, count); | |
2016 | |
2017 trak->start_sample = start_sample; | |
2018 | |
2019 data->pos = (u_char *) entry; | |
2020 atom_size = sizeof(ngx_mp4_stts_atom_t) + (data->last - data->pos); | |
2021 trak->size += atom_size; | |
2022 | |
2023 atom = trak->out[NGX_HTTP_MP4_STTS_ATOM].buf; | |
2024 stts_atom = (ngx_mp4_stts_atom_t *) atom->pos; | |
2025 ngx_mp4_set_32value(stts_atom->size, atom_size); | |
2026 ngx_mp4_set_32value(stts_atom->entries, entries); | |
2027 | |
2028 return NGX_OK; | |
2029 } | |
2030 | |
2031 | |
2032 typedef struct { | |
2033 u_char size[4]; | |
2034 u_char name[4]; | |
2035 u_char version[1]; | |
2036 u_char flags[3]; | |
2037 u_char entries[4]; | |
2038 } ngx_http_mp4_stss_atom_t; | |
2039 | |
2040 | |
2041 static ngx_int_t | |
2042 ngx_http_mp4_read_stss_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
2043 { | |
2044 u_char *atom_header, *atom_table, *atom_end; | |
2045 uint32_t entries; | |
2046 ngx_buf_t *atom, *data; | |
2047 ngx_http_mp4_trak_t *trak; | |
2048 ngx_http_mp4_stss_atom_t *stss_atom; | |
2049 | |
2050 /* sync samples atom */ | |
2051 | |
2052 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stss atom"); | |
2053 | |
2054 atom_header = ngx_mp4_atom_header(mp4); | |
2055 stss_atom = (ngx_http_mp4_stss_atom_t *) atom_header; | |
2056 ngx_mp4_set_atom_name(stss_atom, 's', 't', 's', 's'); | |
2057 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2058 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
|
2059 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2060 "\"%s\" mp4 stss atom too small", mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2061 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2062 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2063 |
4085 | 2064 entries = ngx_mp4_get_32value(stss_atom->entries); |
2065 | |
2066 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
4096 | 2067 "sync sample entries:%uD", entries); |
4085 | 2068 |
2069 trak = ngx_mp4_last_trak(mp4); | |
2070 trak->sync_samples_entries = entries; | |
2071 | |
2072 atom_table = atom_header + sizeof(ngx_http_mp4_stss_atom_t); | |
2073 | |
2074 atom = &trak->stss_atom_buf; | |
2075 atom->temporary = 1; | |
2076 atom->pos = atom_header; | |
2077 atom->last = atom_table; | |
2078 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2079 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
|
2080 + entries * sizeof(uint32_t) > atom_data_size) |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2081 { |
4085 | 2082 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
|
2083 "\"%s\" mp4 stss atom too small", mp4->file.name.data); |
4085 | 2084 return NGX_ERROR; |
2085 } | |
2086 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2087 atom_end = atom_table + entries * sizeof(uint32_t); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2088 |
4085 | 2089 data = &trak->stss_data_buf; |
2090 data->temporary = 1; | |
2091 data->pos = atom_table; | |
2092 data->last = atom_end; | |
2093 | |
2094 trak->out[NGX_HTTP_MP4_STSS_ATOM].buf = atom; | |
2095 trak->out[NGX_HTTP_MP4_STSS_DATA].buf = data; | |
2096 | |
2097 ngx_mp4_atom_next(mp4, atom_data_size); | |
2098 | |
2099 return NGX_OK; | |
2100 } | |
2101 | |
2102 | |
2103 static ngx_int_t | |
2104 ngx_http_mp4_update_stss_atom(ngx_http_mp4_file_t *mp4, | |
2105 ngx_http_mp4_trak_t *trak) | |
2106 { | |
2107 size_t atom_size; | |
2108 uint32_t entries, sample, start_sample, *entry, *end; | |
2109 ngx_buf_t *atom, *data; | |
2110 ngx_http_mp4_stss_atom_t *stss_atom; | |
2111 | |
2112 /* | |
2113 * mdia.minf.stbl.stss updating requires trak->start_sample | |
2114 * from mdia.minf.stbl.stts which depends on value from mdia.mdhd | |
2115 * atom which may reside after mdia.minf | |
2116 */ | |
2117 | |
2118 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2119 "mp4 stss atom update"); | |
2120 | |
2121 data = trak->out[NGX_HTTP_MP4_STSS_DATA].buf; | |
2122 | |
2123 if (data == NULL) { | |
2124 return NGX_OK; | |
2125 } | |
2126 | |
2127 /* sync samples starts from 1 */ | |
2128 start_sample = trak->start_sample + 1; | |
2129 entries = trak->sync_samples_entries; | |
2130 | |
2131 entry = (uint32_t *) data->pos; | |
2132 end = (uint32_t *) data->last; | |
2133 | |
2134 while (entry < end) { | |
2135 sample = ngx_mp4_get_32value(entry); | |
2136 | |
2137 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2138 "start:%uD, sync:%uD", start_sample, sample); | |
2139 | |
2140 if (sample >= start_sample) { | |
2141 goto found; | |
2142 } | |
2143 | |
2144 entries--; | |
2145 entry++; | |
2146 } | |
2147 | |
2148 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
2149 "start sample is out of mp4 stss atom in \"%s\"", | |
2150 mp4->file.name.data); | |
2151 | |
2152 return NGX_ERROR; | |
2153 | |
2154 found: | |
2155 | |
2156 data->pos = (u_char *) entry; | |
2157 | |
2158 start_sample = trak->start_sample; | |
2159 | |
2160 while (entry < end) { | |
2161 sample = ngx_mp4_get_32value(entry); | |
2162 sample -= start_sample; | |
2163 ngx_mp4_set_32value(entry, sample); | |
2164 entry++; | |
2165 } | |
2166 | |
2167 atom_size = sizeof(ngx_http_mp4_stss_atom_t) + (data->last - data->pos); | |
2168 trak->size += atom_size; | |
2169 | |
2170 atom = trak->out[NGX_HTTP_MP4_STSS_ATOM].buf; | |
2171 stss_atom = (ngx_http_mp4_stss_atom_t *) atom->pos; | |
2172 | |
2173 ngx_mp4_set_32value(stss_atom->size, atom_size); | |
2174 ngx_mp4_set_32value(stss_atom->entries, entries); | |
2175 | |
2176 return NGX_OK; | |
2177 } | |
2178 | |
2179 | |
2180 typedef struct { | |
2181 u_char size[4]; | |
2182 u_char name[4]; | |
2183 u_char version[1]; | |
2184 u_char flags[3]; | |
2185 u_char entries[4]; | |
2186 } ngx_mp4_ctts_atom_t; | |
2187 | |
2188 typedef struct { | |
2189 u_char count[4]; | |
2190 u_char offset[4]; | |
2191 } ngx_mp4_ctts_entry_t; | |
2192 | |
2193 | |
2194 static ngx_int_t | |
2195 ngx_http_mp4_read_ctts_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
2196 { | |
2197 u_char *atom_header, *atom_table, *atom_end; | |
2198 uint32_t entries; | |
2199 ngx_buf_t *atom, *data; | |
2200 ngx_mp4_ctts_atom_t *ctts_atom; | |
2201 ngx_http_mp4_trak_t *trak; | |
2202 | |
2203 /* composition offsets atom */ | |
2204 | |
2205 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 ctts atom"); | |
2206 | |
2207 atom_header = ngx_mp4_atom_header(mp4); | |
2208 ctts_atom = (ngx_mp4_ctts_atom_t *) atom_header; | |
2209 ngx_mp4_set_atom_name(ctts_atom, 'c', 't', 't', 's'); | |
2210 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2211 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
|
2212 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2213 "\"%s\" mp4 ctts atom too small", mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2214 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2215 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2216 |
4085 | 2217 entries = ngx_mp4_get_32value(ctts_atom->entries); |
2218 | |
2219 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
4096 | 2220 "composition offset entries:%uD", entries); |
4085 | 2221 |
2222 trak = ngx_mp4_last_trak(mp4); | |
2223 trak->composition_offset_entries = entries; | |
2224 | |
2225 atom_table = atom_header + sizeof(ngx_mp4_ctts_atom_t); | |
2226 | |
2227 atom = &trak->ctts_atom_buf; | |
2228 atom->temporary = 1; | |
2229 atom->pos = atom_header; | |
2230 atom->last = atom_table; | |
2231 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2232 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
|
2233 + entries * sizeof(ngx_mp4_ctts_entry_t) > atom_data_size) |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2234 { |
4085 | 2235 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
|
2236 "\"%s\" mp4 ctts atom too small", mp4->file.name.data); |
4085 | 2237 return NGX_ERROR; |
2238 } | |
2239 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2240 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
|
2241 |
4085 | 2242 data = &trak->ctts_data_buf; |
2243 data->temporary = 1; | |
2244 data->pos = atom_table; | |
2245 data->last = atom_end; | |
2246 | |
2247 trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf = atom; | |
2248 trak->out[NGX_HTTP_MP4_CTTS_DATA].buf = data; | |
2249 | |
2250 ngx_mp4_atom_next(mp4, atom_data_size); | |
2251 | |
2252 return NGX_OK; | |
2253 } | |
2254 | |
2255 | |
2256 static void | |
2257 ngx_http_mp4_update_ctts_atom(ngx_http_mp4_file_t *mp4, | |
2258 ngx_http_mp4_trak_t *trak) | |
2259 { | |
2260 size_t atom_size; | |
2261 uint32_t entries, count, start_sample; | |
2262 ngx_buf_t *atom, *data; | |
2263 ngx_mp4_ctts_atom_t *ctts_atom; | |
2264 ngx_mp4_ctts_entry_t *entry, *end; | |
2265 | |
2266 /* | |
2267 * mdia.minf.stbl.ctts updating requires trak->start_sample | |
2268 * from mdia.minf.stbl.stts which depends on value from mdia.mdhd | |
2269 * atom which may reside after mdia.minf | |
2270 */ | |
2271 | |
2272 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2273 "mp4 ctts atom update"); | |
2274 | |
2275 data = trak->out[NGX_HTTP_MP4_CTTS_DATA].buf; | |
2276 | |
2277 if (data == NULL) { | |
2278 return; | |
2279 } | |
2280 | |
2281 /* sync samples starts from 1 */ | |
2282 start_sample = trak->start_sample + 1; | |
2283 entries = trak->composition_offset_entries; | |
2284 entry = (ngx_mp4_ctts_entry_t *) data->pos; | |
2285 end = (ngx_mp4_ctts_entry_t *) data->last; | |
2286 | |
2287 while (entry < end) { | |
2288 count = ngx_mp4_get_32value(entry->count); | |
2289 | |
4088
8fe1da7b8386
bugfix of r4086: nginx could not be built without debug log.
Igor Sysoev <igor@sysoev.ru>
parents:
4087
diff
changeset
|
2290 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, |
4085 | 2291 "start:%uD, count:%uD, offset:%uD", |
2292 start_sample, count, ngx_mp4_get_32value(entry->offset)); | |
2293 | |
2294 if (start_sample <= count) { | |
2295 count -= (start_sample - 1); | |
2296 ngx_mp4_set_32value(entry->count, count); | |
2297 goto found; | |
2298 } | |
2299 | |
2300 start_sample -= count; | |
2301 entries--; | |
2302 entry++; | |
2303 } | |
2304 | |
2305 trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf = NULL; | |
2306 trak->out[NGX_HTTP_MP4_CTTS_DATA].buf = NULL; | |
2307 | |
2308 return; | |
2309 | |
2310 found: | |
2311 | |
2312 data->pos = (u_char *) entry; | |
2313 atom_size = sizeof(ngx_mp4_ctts_atom_t) + (data->last - data->pos); | |
2314 trak->size += atom_size; | |
2315 | |
2316 atom = trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf; | |
2317 ctts_atom = (ngx_mp4_ctts_atom_t *) atom->pos; | |
2318 | |
2319 ngx_mp4_set_32value(ctts_atom->size, atom_size); | |
2320 ngx_mp4_set_32value(ctts_atom->entries, entries); | |
2321 | |
2322 return; | |
2323 } | |
2324 | |
2325 | |
2326 typedef struct { | |
2327 u_char size[4]; | |
2328 u_char name[4]; | |
2329 u_char version[1]; | |
2330 u_char flags[3]; | |
2331 u_char entries[4]; | |
2332 } ngx_mp4_stsc_atom_t; | |
2333 | |
2334 | |
2335 static ngx_int_t | |
2336 ngx_http_mp4_read_stsc_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
2337 { | |
2338 u_char *atom_header, *atom_table, *atom_end; | |
2339 uint32_t entries; | |
2340 ngx_buf_t *atom, *data; | |
2341 ngx_mp4_stsc_atom_t *stsc_atom; | |
2342 ngx_http_mp4_trak_t *trak; | |
2343 | |
2344 /* sample-to-chunk atom */ | |
2345 | |
2346 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stsc atom"); | |
2347 | |
2348 atom_header = ngx_mp4_atom_header(mp4); | |
2349 stsc_atom = (ngx_mp4_stsc_atom_t *) atom_header; | |
2350 ngx_mp4_set_atom_name(stsc_atom, 's', 't', 's', 'c'); | |
2351 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2352 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
|
2353 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2354 "\"%s\" mp4 stsc atom too small", mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2355 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2356 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2357 |
4085 | 2358 entries = ngx_mp4_get_32value(stsc_atom->entries); |
2359 | |
2360 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
4096 | 2361 "sample-to-chunk entries:%uD", entries); |
4085 | 2362 |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2363 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
|
2364 + entries * sizeof(ngx_mp4_stsc_entry_t) > atom_data_size) |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2365 { |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2366 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2367 "\"%s\" mp4 stsc atom too small", mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2368 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2369 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2370 |
4085 | 2371 atom_table = atom_header + sizeof(ngx_mp4_stsc_atom_t); |
2372 atom_end = atom_table + entries * sizeof(ngx_mp4_stsc_entry_t); | |
2373 | |
2374 trak = ngx_mp4_last_trak(mp4); | |
2375 trak->sample_to_chunk_entries = entries; | |
2376 | |
2377 atom = &trak->stsc_atom_buf; | |
2378 atom->temporary = 1; | |
2379 atom->pos = atom_header; | |
2380 atom->last = atom_table; | |
2381 | |
2382 data = &trak->stsc_data_buf; | |
2383 data->temporary = 1; | |
2384 data->pos = atom_table; | |
2385 data->last = atom_end; | |
2386 | |
2387 trak->out[NGX_HTTP_MP4_STSC_ATOM].buf = atom; | |
2388 trak->out[NGX_HTTP_MP4_STSC_DATA].buf = data; | |
2389 | |
2390 ngx_mp4_atom_next(mp4, atom_data_size); | |
2391 | |
2392 return NGX_OK; | |
2393 } | |
2394 | |
2395 | |
2396 static ngx_int_t | |
2397 ngx_http_mp4_update_stsc_atom(ngx_http_mp4_file_t *mp4, | |
2398 ngx_http_mp4_trak_t *trak) | |
2399 { | |
2400 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
|
2401 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
|
2402 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
|
2403 ngx_buf_t *atom, *data, *buf; |
4085 | 2404 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
|
2405 ngx_mp4_stsc_entry_t *entry, *first, *end; |
4085 | 2406 |
2407 /* | |
2408 * mdia.minf.stbl.stsc updating requires trak->start_sample | |
2409 * from mdia.minf.stbl.stts which depends on value from mdia.mdhd | |
2410 * atom which may reside after mdia.minf | |
2411 */ | |
2412 | |
2413 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2414 "mp4 stsc atom update"); | |
2415 | |
2416 data = trak->out[NGX_HTTP_MP4_STSC_DATA].buf; | |
2417 | |
2418 if (data == NULL) { | |
2419 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
2420 "no mp4 stsc atoms were found in \"%s\"", | |
2421 mp4->file.name.data); | |
2422 return NGX_ERROR; | |
2423 } | |
2424 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2425 if (trak->sample_to_chunk_entries == 0) { |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2426 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2427 "zero number of entries in stsc atom in \"%s\"", |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2428 mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2429 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2430 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2431 |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2432 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
|
2433 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
|
2434 |
4085 | 2435 entry = (ngx_mp4_stsc_entry_t *) data->pos; |
2436 end = (ngx_mp4_stsc_entry_t *) data->last; | |
2437 | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2438 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
|
2439 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
|
2440 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
|
2441 entry++; |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2442 |
4085 | 2443 while (entry < end) { |
2444 | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2445 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
|
2446 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2447 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
|
2448 "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
|
2449 "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
|
2450 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
|
2451 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2452 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
|
2453 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2454 if (start_sample <= n) { |
4085 | 2455 goto found; |
2456 } | |
2457 | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2458 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
|
2459 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2460 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
|
2461 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
|
2462 id = ngx_mp4_get_32value(entry->id); |
4085 | 2463 entries--; |
2464 entry++; | |
2465 } | |
2466 | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2467 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
|
2468 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2469 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
|
2470 "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
|
2471 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
|
2472 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2473 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
|
2474 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2475 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
|
2476 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
|
2477 "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
|
2478 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
|
2479 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
|
2480 } |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2481 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2482 found: |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2483 |
4085 | 2484 entries++; |
2485 entry--; | |
2486 | |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2487 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
|
2488 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
|
2489 "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
|
2490 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
|
2491 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
|
2492 } |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2493 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2494 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
|
2495 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2496 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
|
2497 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
|
2498 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2499 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
|
2500 "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
|
2501 trak->start_chunk, trak->chunk_samples); |
4085 | 2502 |
2503 data->pos = (u_char *) entry; | |
2504 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
|
2505 |
4382
b4d54fa76853
Fixed mp4 if first entry in stsc was skipped (ticket #72).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4306
diff
changeset
|
2506 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
|
2507 |
4727
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
2508 if (trak->chunk_samples && next_chunk - trak->start_chunk == 2) { |
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
2509 |
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
2510 /* last chunk in the entry */ |
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
2511 |
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
2512 ngx_mp4_set_32value(entry->samples, samples - trak->chunk_samples); |
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
2513 |
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
2514 } 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
|
2515 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2516 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
|
2517 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
|
2518 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
|
2519 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
|
2520 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2521 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
|
2522 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
|
2523 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
|
2524 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
|
2525 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2526 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
|
2527 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2528 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
|
2529 |
4727
1c7616100797
Merge of r4688, r4689, r4706:
Maxim Dounin <mdounin@mdounin.ru>
parents:
4659
diff
changeset
|
2530 entries++; |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2531 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
|
2532 } |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2533 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2534 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
|
2535 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
|
2536 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
|
2537 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
|
2538 } |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2539 |
4085 | 2540 trak->size += atom_size; |
2541 | |
2542 atom = trak->out[NGX_HTTP_MP4_STSC_ATOM].buf; | |
2543 stsc_atom = (ngx_mp4_stsc_atom_t *) atom->pos; | |
2544 | |
2545 ngx_mp4_set_32value(stsc_atom->size, atom_size); | |
2546 ngx_mp4_set_32value(stsc_atom->entries, entries); | |
2547 | |
2548 return NGX_OK; | |
2549 } | |
2550 | |
2551 | |
2552 typedef struct { | |
2553 u_char size[4]; | |
2554 u_char name[4]; | |
2555 u_char version[1]; | |
2556 u_char flags[3]; | |
2557 u_char uniform_size[4]; | |
2558 u_char entries[4]; | |
2559 } ngx_mp4_stsz_atom_t; | |
2560 | |
2561 | |
2562 static ngx_int_t | |
2563 ngx_http_mp4_read_stsz_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
2564 { | |
2565 u_char *atom_header, *atom_table, *atom_end; | |
2566 size_t atom_size; | |
2567 uint32_t entries, size; | |
2568 ngx_buf_t *atom, *data; | |
2569 ngx_mp4_stsz_atom_t *stsz_atom; | |
2570 ngx_http_mp4_trak_t *trak; | |
2571 | |
2572 /* sample sizes atom */ | |
2573 | |
2574 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stsz atom"); | |
2575 | |
2576 atom_header = ngx_mp4_atom_header(mp4); | |
2577 stsz_atom = (ngx_mp4_stsz_atom_t *) atom_header; | |
2578 ngx_mp4_set_atom_name(stsz_atom, 's', 't', 's', 'z'); | |
2579 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2580 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
|
2581 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2582 "\"%s\" mp4 stsz atom too small", mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2583 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2584 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2585 |
4085 | 2586 size = ngx_mp4_get_32value(stsz_atom->uniform_size); |
2587 entries = ngx_mp4_get_32value(stsz_atom->entries); | |
2588 | |
2589 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2590 "sample uniform size:%uD, entries:%uD", size, entries); | |
2591 | |
2592 trak = ngx_mp4_last_trak(mp4); | |
2593 trak->sample_sizes_entries = entries; | |
2594 | |
2595 atom_table = atom_header + sizeof(ngx_mp4_stsz_atom_t); | |
2596 | |
2597 atom = &trak->stsz_atom_buf; | |
2598 atom->temporary = 1; | |
2599 atom->pos = atom_header; | |
2600 atom->last = atom_table; | |
2601 | |
2602 trak->out[NGX_HTTP_MP4_STSZ_ATOM].buf = atom; | |
2603 | |
2604 if (size == 0) { | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2605 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
|
2606 + entries * sizeof(uint32_t) > atom_data_size) |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2607 { |
4085 | 2608 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
|
2609 "\"%s\" mp4 stsz atom too small", |
4085 | 2610 mp4->file.name.data); |
2611 return NGX_ERROR; | |
2612 } | |
2613 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2614 atom_end = atom_table + entries * sizeof(uint32_t); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2615 |
4085 | 2616 data = &trak->stsz_data_buf; |
2617 data->temporary = 1; | |
2618 data->pos = atom_table; | |
2619 data->last = atom_end; | |
2620 | |
2621 trak->out[NGX_HTTP_MP4_STSZ_DATA].buf = data; | |
2622 | |
2623 } else { | |
2624 /* 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
|
2625 /* TODO : chunk samples */ |
4085 | 2626 atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size; |
2627 ngx_mp4_set_32value(atom_header, atom_size); | |
2628 trak->size += atom_size; | |
2629 } | |
2630 | |
2631 ngx_mp4_atom_next(mp4, atom_data_size); | |
2632 | |
2633 return NGX_OK; | |
2634 } | |
2635 | |
2636 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2637 static ngx_int_t |
4085 | 2638 ngx_http_mp4_update_stsz_atom(ngx_http_mp4_file_t *mp4, |
2639 ngx_http_mp4_trak_t *trak) | |
2640 { | |
2641 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
|
2642 uint32_t *pos, *end; |
4085 | 2643 ngx_buf_t *atom, *data; |
2644 ngx_mp4_stsz_atom_t *stsz_atom; | |
2645 | |
2646 /* | |
2647 * mdia.minf.stbl.stsz updating requires trak->start_sample | |
2648 * from mdia.minf.stbl.stts which depends on value from mdia.mdhd | |
2649 * atom which may reside after mdia.minf | |
2650 */ | |
2651 | |
2652 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2653 "mp4 stsz atom update"); | |
2654 | |
2655 data = trak->out[NGX_HTTP_MP4_STSZ_DATA].buf; | |
2656 | |
2657 if (data) { | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2658 if (trak->start_sample > trak->sample_sizes_entries) { |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2659 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2660 "start time is out mp4 stsz samples in \"%s\"", |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2661 mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2662 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2663 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2664 |
4085 | 2665 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
|
2666 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
|
2667 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2668 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
|
2669 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
|
2670 } |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2671 |
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2672 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, |
4112 | 2673 "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
|
2674 |
4085 | 2675 atom_size = sizeof(ngx_mp4_stsz_atom_t) + (data->last - data->pos); |
2676 trak->size += atom_size; | |
2677 | |
2678 atom = trak->out[NGX_HTTP_MP4_STSZ_ATOM].buf; | |
2679 stsz_atom = (ngx_mp4_stsz_atom_t *) atom->pos; | |
2680 | |
2681 ngx_mp4_set_32value(stsz_atom->size, atom_size); | |
2682 ngx_mp4_set_32value(stsz_atom->entries, | |
2683 trak->sample_sizes_entries - trak->start_sample); | |
2684 } | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2685 |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2686 return NGX_OK; |
4085 | 2687 } |
2688 | |
2689 | |
2690 typedef struct { | |
2691 u_char size[4]; | |
2692 u_char name[4]; | |
2693 u_char version[1]; | |
2694 u_char flags[3]; | |
2695 u_char entries[4]; | |
2696 } ngx_mp4_stco_atom_t; | |
2697 | |
2698 | |
2699 static ngx_int_t | |
2700 ngx_http_mp4_read_stco_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
2701 { | |
2702 u_char *atom_header, *atom_table, *atom_end; | |
2703 uint32_t entries; | |
2704 ngx_buf_t *atom, *data; | |
2705 ngx_mp4_stco_atom_t *stco_atom; | |
2706 ngx_http_mp4_trak_t *trak; | |
2707 | |
2708 /* chunk offsets atom */ | |
2709 | |
2710 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stco atom"); | |
2711 | |
2712 atom_header = ngx_mp4_atom_header(mp4); | |
2713 stco_atom = (ngx_mp4_stco_atom_t *) atom_header; | |
2714 ngx_mp4_set_atom_name(stco_atom, 's', 't', 'c', 'o'); | |
2715 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2716 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
|
2717 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2718 "\"%s\" mp4 stco atom too small", mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2719 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2720 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2721 |
4085 | 2722 entries = ngx_mp4_get_32value(stco_atom->entries); |
2723 | |
2724 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "chunks:%uD", entries); | |
2725 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2726 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
|
2727 + entries * sizeof(uint32_t) > atom_data_size) |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2728 { |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2729 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2730 "\"%s\" mp4 stco atom too small", mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2731 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2732 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2733 |
4085 | 2734 atom_table = atom_header + sizeof(ngx_mp4_stco_atom_t); |
2735 atom_end = atom_table + entries * sizeof(uint32_t); | |
2736 | |
2737 trak = ngx_mp4_last_trak(mp4); | |
2738 trak->chunks = entries; | |
2739 | |
4107 | 2740 atom = &trak->stco_atom_buf; |
4085 | 2741 atom->temporary = 1; |
2742 atom->pos = atom_header; | |
2743 atom->last = atom_table; | |
2744 | |
4107 | 2745 data = &trak->stco_data_buf; |
4085 | 2746 data->temporary = 1; |
2747 data->pos = atom_table; | |
2748 data->last = atom_end; | |
2749 | |
2750 trak->out[NGX_HTTP_MP4_STCO_ATOM].buf = atom; | |
2751 trak->out[NGX_HTTP_MP4_STCO_DATA].buf = data; | |
2752 | |
2753 ngx_mp4_atom_next(mp4, atom_data_size); | |
2754 | |
2755 return NGX_OK; | |
2756 } | |
2757 | |
2758 | |
2759 static ngx_int_t | |
2760 ngx_http_mp4_update_stco_atom(ngx_http_mp4_file_t *mp4, | |
2761 ngx_http_mp4_trak_t *trak) | |
2762 { | |
2763 size_t atom_size; | |
2764 ngx_buf_t *atom, *data; | |
2765 ngx_mp4_stco_atom_t *stco_atom; | |
2766 | |
2767 /* | |
2768 * mdia.minf.stbl.stco updating requires trak->start_chunk | |
2769 * from mdia.minf.stbl.stsc which depends on value from mdia.mdhd | |
2770 * atom which may reside after mdia.minf | |
2771 */ | |
2772 | |
2773 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2774 "mp4 stco atom update"); | |
2775 | |
2776 data = trak->out[NGX_HTTP_MP4_STCO_DATA].buf; | |
2777 | |
2778 if (data == NULL) { | |
2779 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
2780 "no mp4 stco atoms were found in \"%s\"", | |
2781 mp4->file.name.data); | |
2782 return NGX_ERROR; | |
2783 } | |
2784 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2785 if (trak->start_chunk > trak->chunks) { |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2786 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2787 "start time is out mp4 stco chunks in \"%s\"", |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2788 mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2789 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2790 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2791 |
4098
a3e07fab98a3
Fix of case when start sample does not reside on chunk boundary.
Igor Sysoev <igor@sysoev.ru>
parents:
4096
diff
changeset
|
2792 data->pos += trak->start_chunk * sizeof(uint32_t); |
4085 | 2793 atom_size = sizeof(ngx_mp4_stco_atom_t) + (data->last - data->pos); |
2794 trak->size += atom_size; | |
2795 | |
2796 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
|
2797 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
|
2798 ngx_mp4_set_32value(data->pos, trak->start_offset); |
4085 | 2799 |
2800 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2801 "start chunk offset:%uD", trak->start_offset); | |
2802 | |
2803 atom = trak->out[NGX_HTTP_MP4_STCO_ATOM].buf; | |
2804 stco_atom = (ngx_mp4_stco_atom_t *) atom->pos; | |
2805 | |
2806 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
|
2807 ngx_mp4_set_32value(stco_atom->entries, trak->chunks - trak->start_chunk); |
4085 | 2808 |
2809 return NGX_OK; | |
2810 } | |
2811 | |
2812 | |
2813 static void | |
2814 ngx_http_mp4_adjust_stco_atom(ngx_http_mp4_file_t *mp4, | |
2815 ngx_http_mp4_trak_t *trak, int32_t adjustment) | |
2816 { | |
2817 uint32_t offset, *entry, *end; | |
2818 ngx_buf_t *data; | |
2819 | |
2820 /* | |
2821 * moov.trak.mdia.minf.stbl.stco adjustment requires | |
2822 * minimal start offset of all traks and new moov atom size | |
2823 */ | |
2824 | |
2825 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2826 "mp4 stco atom adjustment"); | |
2827 | |
2828 data = trak->out[NGX_HTTP_MP4_STCO_DATA].buf; | |
2829 entry = (uint32_t *) data->pos; | |
2830 end = (uint32_t *) data->last; | |
2831 | |
2832 while (entry < end) { | |
2833 offset = ngx_mp4_get_32value(entry); | |
2834 offset += adjustment; | |
2835 ngx_mp4_set_32value(entry, offset); | |
2836 entry++; | |
2837 } | |
2838 } | |
2839 | |
2840 | |
4112 | 2841 typedef struct { |
2842 u_char size[4]; | |
2843 u_char name[4]; | |
2844 u_char version[1]; | |
2845 u_char flags[3]; | |
2846 u_char entries[4]; | |
2847 } ngx_mp4_co64_atom_t; | |
2848 | |
2849 | |
2850 static ngx_int_t | |
2851 ngx_http_mp4_read_co64_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size) | |
2852 { | |
2853 u_char *atom_header, *atom_table, *atom_end; | |
2854 uint32_t entries; | |
2855 ngx_buf_t *atom, *data; | |
2856 ngx_mp4_co64_atom_t *co64_atom; | |
2857 ngx_http_mp4_trak_t *trak; | |
2858 | |
2859 /* chunk offsets atom */ | |
2860 | |
2861 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 co64 atom"); | |
2862 | |
2863 atom_header = ngx_mp4_atom_header(mp4); | |
2864 co64_atom = (ngx_mp4_co64_atom_t *) atom_header; | |
2865 ngx_mp4_set_atom_name(co64_atom, 'c', 'o', '6', '4'); | |
2866 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2867 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
|
2868 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2869 "\"%s\" mp4 co64 atom too small", mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2870 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2871 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2872 |
4112 | 2873 entries = ngx_mp4_get_32value(co64_atom->entries); |
2874 | |
2875 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "chunks:%uD", entries); | |
2876 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2877 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
|
2878 + entries * sizeof(uint64_t) > atom_data_size) |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2879 { |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2880 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2881 "\"%s\" mp4 co64 atom too small", mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2882 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2883 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2884 |
4112 | 2885 atom_table = atom_header + sizeof(ngx_mp4_co64_atom_t); |
2886 atom_end = atom_table + entries * sizeof(uint64_t); | |
2887 | |
2888 trak = ngx_mp4_last_trak(mp4); | |
2889 trak->chunks = entries; | |
2890 | |
2891 atom = &trak->co64_atom_buf; | |
2892 atom->temporary = 1; | |
2893 atom->pos = atom_header; | |
2894 atom->last = atom_table; | |
2895 | |
2896 data = &trak->co64_data_buf; | |
2897 data->temporary = 1; | |
2898 data->pos = atom_table; | |
2899 data->last = atom_end; | |
2900 | |
2901 trak->out[NGX_HTTP_MP4_CO64_ATOM].buf = atom; | |
2902 trak->out[NGX_HTTP_MP4_CO64_DATA].buf = data; | |
2903 | |
2904 ngx_mp4_atom_next(mp4, atom_data_size); | |
2905 | |
2906 return NGX_OK; | |
2907 } | |
2908 | |
2909 | |
2910 static ngx_int_t | |
2911 ngx_http_mp4_update_co64_atom(ngx_http_mp4_file_t *mp4, | |
2912 ngx_http_mp4_trak_t *trak) | |
2913 { | |
2914 size_t atom_size; | |
2915 ngx_buf_t *atom, *data; | |
2916 ngx_mp4_co64_atom_t *co64_atom; | |
2917 | |
2918 /* | |
2919 * mdia.minf.stbl.co64 updating requires trak->start_chunk | |
2920 * from mdia.minf.stbl.stsc which depends on value from mdia.mdhd | |
2921 * atom which may reside after mdia.minf | |
2922 */ | |
2923 | |
2924 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2925 "mp4 co64 atom update"); | |
2926 | |
2927 data = trak->out[NGX_HTTP_MP4_CO64_DATA].buf; | |
2928 | |
2929 if (data == NULL) { | |
2930 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, | |
2931 "no mp4 co64 atoms were found in \"%s\"", | |
2932 mp4->file.name.data); | |
2933 return NGX_ERROR; | |
2934 } | |
2935 | |
4585
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2936 if (trak->start_chunk > trak->chunks) { |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2937 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0, |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2938 "start time is out mp4 co64 chunks in \"%s\"", |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2939 mp4->file.name.data); |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2940 return NGX_ERROR; |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2941 } |
3f874d645f45
Mp4: sanity checks cleanup.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4579
diff
changeset
|
2942 |
4112 | 2943 data->pos += trak->start_chunk * sizeof(uint64_t); |
2944 atom_size = sizeof(ngx_mp4_co64_atom_t) + (data->last - data->pos); | |
2945 trak->size += atom_size; | |
2946 | |
2947 trak->start_offset = ngx_mp4_get_64value(data->pos); | |
2948 trak->start_offset += trak->chunk_samples_size; | |
2949 ngx_mp4_set_64value(data->pos, trak->start_offset); | |
2950 | |
2951 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2952 "start chunk offset:%uL", trak->start_offset); | |
2953 | |
2954 atom = trak->out[NGX_HTTP_MP4_CO64_ATOM].buf; | |
2955 co64_atom = (ngx_mp4_co64_atom_t *) atom->pos; | |
2956 | |
2957 ngx_mp4_set_32value(co64_atom->size, atom_size); | |
2958 ngx_mp4_set_32value(co64_atom->entries, trak->chunks - trak->start_chunk); | |
2959 | |
2960 return NGX_OK; | |
2961 } | |
2962 | |
2963 | |
2964 static void | |
2965 ngx_http_mp4_adjust_co64_atom(ngx_http_mp4_file_t *mp4, | |
2966 ngx_http_mp4_trak_t *trak, off_t adjustment) | |
2967 { | |
2968 uint64_t offset, *entry, *end; | |
2969 ngx_buf_t *data; | |
2970 | |
2971 /* | |
2972 * moov.trak.mdia.minf.stbl.co64 adjustment requires | |
2973 * minimal start offset of all traks and new moov atom size | |
2974 */ | |
2975 | |
2976 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
2977 "mp4 co64 atom adjustment"); | |
2978 | |
2979 data = trak->out[NGX_HTTP_MP4_CO64_DATA].buf; | |
2980 entry = (uint64_t *) data->pos; | |
2981 end = (uint64_t *) data->last; | |
2982 | |
2983 while (entry < end) { | |
2984 offset = ngx_mp4_get_64value(entry); | |
2985 offset += adjustment; | |
2986 ngx_mp4_set_64value(entry, offset); | |
2987 entry++; | |
2988 } | |
2989 } | |
2990 | |
2991 | |
4085 | 2992 static char * |
2993 ngx_http_mp4(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
2994 { | |
2995 ngx_http_core_loc_conf_t *clcf; | |
2996 | |
2997 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | |
2998 clcf->handler = ngx_http_mp4_handler; | |
2999 | |
3000 return NGX_CONF_OK; | |
3001 } | |
3002 | |
3003 | |
3004 static void * | |
3005 ngx_http_mp4_create_conf(ngx_conf_t *cf) | |
3006 { | |
3007 ngx_http_mp4_conf_t *conf; | |
3008 | |
3009 conf = ngx_palloc(cf->pool, sizeof(ngx_http_mp4_conf_t)); | |
3010 if (conf == NULL) { | |
3011 return NULL; | |
3012 } | |
3013 | |
3014 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
|
3015 conf->max_buffer_size = NGX_CONF_UNSET_SIZE; |
4085 | 3016 |
3017 return conf; | |
3018 } | |
3019 | |
3020 | |
3021 static char * | |
3022 ngx_http_mp4_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
3023 { | |
3024 ngx_http_mp4_conf_t *prev = parent; | |
3025 ngx_http_mp4_conf_t *conf = child; | |
3026 | |
3027 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
|
3028 ngx_conf_merge_size_value(conf->max_buffer_size, prev->max_buffer_size, |
4085 | 3029 10 * 1024 * 1024); |
3030 | |
3031 return NGX_CONF_OK; | |
3032 } |