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