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