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