annotate src/http/modules/ngx_http_mp4_module.c @ 4087:3aa3b7bb9f0d

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