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