forked from kaltura/nginx-vod-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_vod_hds.c
206 lines (179 loc) · 5.86 KB
/
ngx_http_vod_hds.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <ngx_http.h>
#include "ngx_http_vod_submodule.h"
#include "ngx_http_vod_utils.h"
#include "vod/hds/hds_manifest.h"
#include "vod/hds/hds_fragment.h"
// content types
static u_char f4m_content_type[] = "video/f4m";
static u_char f4f_content_type[] = "video/f4f";
// file extensions
static const u_char manifest_file_ext[] = ".f4m";
static ngx_int_t
ngx_http_vod_hds_handle_manifest(
ngx_http_vod_submodule_context_t* submodule_context,
ngx_str_t* response,
ngx_str_t* content_type)
{
vod_status_t rc;
rc = hds_packager_build_manifest(
&submodule_context->request_context,
&submodule_context->conf->hds.manifest_config,
&submodule_context->r->uri,
&submodule_context->conf->segmenter,
&submodule_context->media_set,
response);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hds_handle_manifest: hds_packager_build_manifest failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
content_type->data = f4m_content_type;
content_type->len = sizeof(f4m_content_type) - 1;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_hds_init_frame_processor(
ngx_http_vod_submodule_context_t* submodule_context,
read_cache_state_t* read_cache_state,
segment_writer_t* segment_writer,
ngx_http_vod_frame_processor_t* frame_processor,
void** frame_processor_state,
ngx_str_t* output_buffer,
size_t* response_size,
ngx_str_t* content_type)
{
hds_muxer_state_t* state;
vod_status_t rc;
rc = hds_muxer_init_fragment(
&submodule_context->request_context,
&submodule_context->conf->hds.fragment_config,
submodule_context->request_params.segment_index,
submodule_context->media_set.sequences,
read_cache_state,
segment_writer->write_tail,
segment_writer->context,
ngx_http_vod_submodule_size_only(submodule_context),
output_buffer,
response_size,
&state);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_hds_init_frame_processor: hds_muxer_init_fragment failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
*frame_processor = (ngx_http_vod_frame_processor_t)hds_muxer_process_frames;
*frame_processor_state = state;
// set the 'Content-type' header
content_type->len = sizeof(f4f_content_type) - 1;
content_type->data = (u_char *)f4f_content_type;
return NGX_OK;
}
static const ngx_http_vod_request_t hds_manifest_request = {
0,
PARSE_FLAG_DURATION_LIMITS_AND_TOTAL_SIZE,
REQUEST_CLASS_MANIFEST,
ngx_http_vod_hds_handle_manifest,
NULL,
};
static const ngx_http_vod_request_t hds_fragment_request = {
REQUEST_FLAG_SINGLE_TRACK_PER_MEDIA_TYPE,
PARSE_FLAG_FRAMES_ALL | PARSE_FLAG_EXTRA_DATA,
REQUEST_CLASS_SEGMENT,
NULL,
ngx_http_vod_hds_init_frame_processor,
};
static void
ngx_http_vod_hds_create_loc_conf(
ngx_conf_t *cf,
ngx_http_vod_hds_loc_conf_t *conf)
{
conf->fragment_config.generate_moof_atom = NGX_CONF_UNSET;
}
static char *
ngx_http_vod_hds_merge_loc_conf(
ngx_conf_t *cf,
ngx_http_vod_loc_conf_t *base,
ngx_http_vod_hds_loc_conf_t *conf,
ngx_http_vod_hds_loc_conf_t *prev)
{
ngx_conf_merge_str_value(conf->manifest_config.fragment_file_name_prefix, prev->manifest_config.fragment_file_name_prefix, "frag");
ngx_conf_merge_str_value(conf->manifest_file_name_prefix, prev->manifest_file_name_prefix, "manifest");
ngx_conf_merge_value(conf->fragment_config.generate_moof_atom, prev->fragment_config.generate_moof_atom, 1);
return NGX_CONF_OK;
}
static int
ngx_http_vod_hds_get_file_path_components(ngx_str_t* uri)
{
return 1;
}
static ngx_int_t
ngx_http_vod_hds_parse_uri_file_name(
ngx_http_request_t *r,
ngx_http_vod_loc_conf_t *conf,
u_char* start_pos,
u_char* end_pos,
request_params_t* request_params,
const ngx_http_vod_request_t** request)
{
ngx_int_t rc;
// fragment request
if (ngx_http_vod_starts_with(start_pos, end_pos, &conf->hds.manifest_config.fragment_file_name_prefix))
{
// sample fragment file name: frag-f3-v1-a1-Seg1-Frag1
start_pos += conf->hds.manifest_config.fragment_file_name_prefix.len;
// parse the fragment index
end_pos = ngx_http_vod_extract_uint32_token_reverse(start_pos, end_pos, &request_params->segment_index);
if (request_params->segment_index == 0)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_hds_parse_uri_file_name: failed to parse fragment index");
return NGX_HTTP_BAD_REQUEST;
}
request_params->segment_index--; // convert to 0-based
// extract the '-Seg1-Frag' part
end_pos -= sizeof("-Seg1-Frag") - 1;
if (end_pos < start_pos ||
ngx_memcmp(end_pos, "-Seg1-Frag", sizeof("-Seg1-Frag") - 1) != 0)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_hds_parse_uri_file_name: invalid segment / fragment requested");
return NGX_HTTP_BAD_REQUEST;
}
*request = &hds_fragment_request;
}
// manifest request
else if (ngx_http_vod_match_prefix_postfix(start_pos, end_pos, &conf->hds.manifest_file_name_prefix, manifest_file_ext))
{
start_pos += conf->hds.manifest_file_name_prefix.len;
end_pos -= (sizeof(manifest_file_ext) - 1);
*request = &hds_manifest_request;
}
else
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_hds_parse_uri_file_name: unidentified request");
return NGX_HTTP_BAD_REQUEST;
}
// parse the required tracks string
rc = ngx_http_vod_parse_uri_file_name(r, start_pos, end_pos, FALSE, request_params);
if (rc != NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_hds_parse_uri_file_name: ngx_http_vod_parse_uri_file_name failed %i", rc);
return rc;
}
return NGX_OK;
}
ngx_int_t
ngx_http_vod_hds_parse_drm_info(
ngx_http_vod_submodule_context_t* submodule_context,
ngx_str_t* drm_info,
void** output)
{
ngx_log_error(NGX_LOG_ERR, submodule_context->request_context.log, 0,
"ngx_http_vod_hds_parse_drm_info: drm support for hds not implemented");
return VOD_UNEXPECTED;
}
DEFINE_SUBMODULE(hds);