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