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