forked from kaltura/nginx-vod-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_http_vod_mss.c
353 lines (313 loc) · 10.1 KB
/
ngx_http_vod_mss.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include <ngx_http.h>
#include "ngx_http_vod_submodule.h"
#include "ngx_http_vod_utils.h"
#include "vod/mss/mss_packager.h"
#include "vod/mss/mss_playready.h"
#include "vod/udrm.h"
// constants
#define SUPPORTED_CODECS (VOD_CODEC_FLAG(AVC) | VOD_CODEC_FLAG(AAC) | VOD_CODEC_FLAG(MP3))
// typedefs
typedef struct {
uint64_t bitrate;
uint64_t time;
ngx_str_t media_type;
} fragment_params_t;
// constants
static const ngx_http_vod_match_definition_t fragment_match_definition[] = {
{ MATCH_FIXED_STRING, 0, 0, ngx_string("QualityLevels(") },
{ MATCH_NUMBER, offsetof(fragment_params_t, bitrate), 0, ngx_null_string },
{ MATCH_FIXED_STRING, 0, 0, ngx_string(")/Fragments(") },
{ MATCH_DELIM_STRING, offsetof(fragment_params_t, media_type), '=',ngx_null_string },
{ MATCH_NUMBER, offsetof(fragment_params_t, time), 0, ngx_null_string },
{ MATCH_FIXED_STRING, 0, 0, ngx_string(")") },
{ MATCH_END, 0, 0, ngx_null_string },
};
// content types
static u_char mp4_audio_content_type[] = "audio/mp4";
static u_char mp4_video_content_type[] = "video/mp4";
static u_char manifest_content_type[] = "text/xml"; // TODO: consider moving to application/vnd.ms-sstr+xml
// extensions
static const u_char m4s_file_ext[] = ".m4s";
static ngx_int_t
ngx_http_vod_mss_handle_manifest(
ngx_http_vod_submodule_context_t* submodule_context,
ngx_str_t* response,
ngx_str_t* content_type)
{
vod_status_t rc;
if (submodule_context->conf->drm_enabled)
{
rc = mss_playready_build_manifest(
&submodule_context->request_context,
&submodule_context->conf->mss.manifest_conf,
&submodule_context->media_set,
response);
}
else
{
rc = mss_packager_build_manifest(
&submodule_context->request_context,
&submodule_context->conf->mss.manifest_conf,
&submodule_context->media_set,
0,
NULL,
NULL,
response);
}
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_mss_handle_manifest: mss_packager_build_manifest failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
content_type->data = manifest_content_type;
content_type->len = sizeof(manifest_content_type) - 1;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_mss_init_frame_processor(
ngx_http_vod_submodule_context_t* submodule_context,
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)
{
ngx_http_vod_loc_conf_t* conf = submodule_context->conf;
fragment_writer_state_t* state;
segment_writer_t drm_writer;
vod_status_t rc;
bool_t reuse_buffers = FALSE;
bool_t size_only = ngx_http_vod_submodule_size_only(submodule_context);
if (conf->drm_enabled)
{
rc = mss_playready_get_fragment_writer(
&drm_writer,
&submodule_context->request_context,
&submodule_context->media_set,
submodule_context->request_params.segment_index,
conf->min_single_nalu_per_frame_segment > 0 && submodule_context->request_params.segment_index >= conf->min_single_nalu_per_frame_segment - 1,
segment_writer,
submodule_context->media_set.sequences[0].encryption_key, // iv
size_only,
output_buffer,
response_size);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_mss_init_frame_processor: mss_playready_get_fragment_writer failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
if (drm_writer.write_tail != NULL)
{
segment_writer = &drm_writer;
reuse_buffers = TRUE; // mp4_encrypt allocates new buffers
}
}
else
{
rc = mss_packager_build_fragment_header(
&submodule_context->request_context,
&submodule_context->media_set,
submodule_context->request_params.segment_index,
0,
NULL,
NULL,
size_only,
output_buffer,
response_size);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_mss_init_frame_processor: mss_packager_build_fragment_header failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
}
if (!size_only || *response_size == 0)
{
rc = mp4_builder_frame_writer_init(
&submodule_context->request_context,
submodule_context->media_set.sequences,
segment_writer->write_tail,
segment_writer->context,
reuse_buffers,
&state);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_mss_init_frame_processor: mp4_builder_frame_writer_init failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
*frame_processor = (ngx_http_vod_frame_processor_t)mp4_builder_frame_writer_process;
*frame_processor_state = state;
}
// set the 'Content-type' header
if (submodule_context->media_set.track_count[MEDIA_TYPE_VIDEO])
{
content_type->len = sizeof(mp4_video_content_type) - 1;
content_type->data = mp4_video_content_type;
}
else
{
content_type->len = sizeof(mp4_audio_content_type) - 1;
content_type->data = mp4_audio_content_type;
}
return NGX_OK;
}
static const ngx_http_vod_request_t mss_manifest_request = {
REQUEST_FLAG_TIME_DEPENDENT_ON_LIVE,
PARSE_FLAG_TOTAL_SIZE_ESTIMATE | PARSE_FLAG_PARSED_EXTRA_DATA,
REQUEST_CLASS_MANIFEST,
SUPPORTED_CODECS,
MSS_TIMESCALE,
ngx_http_vod_mss_handle_manifest,
NULL,
};
static const ngx_http_vod_request_t mss_fragment_request = {
REQUEST_FLAG_SINGLE_TRACK,
PARSE_FLAG_FRAMES_ALL,
REQUEST_CLASS_SEGMENT,
SUPPORTED_CODECS,
MSS_TIMESCALE,
NULL,
ngx_http_vod_mss_init_frame_processor,
};
static const ngx_http_vod_request_t mss_playready_fragment_request = {
REQUEST_FLAG_SINGLE_TRACK,
PARSE_FLAG_FRAMES_ALL | PARSE_FLAG_PARSED_EXTRA_DATA,
REQUEST_CLASS_SEGMENT,
SUPPORTED_CODECS,
MSS_TIMESCALE,
NULL,
ngx_http_vod_mss_init_frame_processor,
};
void
ngx_http_vod_mss_create_loc_conf(
ngx_conf_t *cf,
ngx_http_vod_mss_loc_conf_t *conf)
{
conf->manifest_conf.duplicate_bitrate_threshold = NGX_CONF_UNSET_UINT;
}
static char *
ngx_http_vod_mss_merge_loc_conf(
ngx_conf_t *cf,
ngx_http_vod_loc_conf_t *base,
ngx_http_vod_mss_loc_conf_t *conf,
ngx_http_vod_mss_loc_conf_t *prev)
{
ngx_conf_merge_str_value(conf->manifest_file_name_prefix, prev->manifest_file_name_prefix, "manifest");
ngx_conf_merge_uint_value(conf->manifest_conf.duplicate_bitrate_threshold, prev->manifest_conf.duplicate_bitrate_threshold, 4096);
return NGX_CONF_OK;
}
int
ngx_http_vod_mss_get_file_path_components(ngx_str_t* uri)
{
u_char* end_pos = uri->data + uri->len;
if (end_pos[-1] == ')')
{
// parse "QualityLevels(2364883)/Fragments(video=0)" as the file part of the path
return 2;
}
else
{
return 1;
}
}
ngx_int_t
ngx_http_vod_mss_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)
{
fragment_params_t fragment_params;
ngx_int_t rc;
// fragment
if (end_pos[-1] == ')')
{
if (!ngx_http_vod_parse_string(fragment_match_definition, start_pos, end_pos, &fragment_params))
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_mss_parse_uri_file_name: ngx_http_vod_parse_string failed");
return NGX_HTTP_BAD_REQUEST;
}
if (fragment_params.media_type.len != sizeof(MSS_STREAM_TYPE_VIDEO) - 1)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_mss_parse_uri_file_name: invalid media type length %uz", fragment_params.media_type.len);
return NGX_HTTP_BAD_REQUEST;
}
request_params->sequences_mask = (1 << mss_sequence_index(fragment_params.bitrate));
if (ngx_memcmp(fragment_params.media_type.data, MSS_STREAM_TYPE_VIDEO, sizeof(MSS_STREAM_TYPE_VIDEO) - 1) == 0)
{
request_params->tracks_mask[MEDIA_TYPE_VIDEO] = (1 << mss_track_index(fragment_params.bitrate));
}
else if (ngx_memcmp(fragment_params.media_type.data, MSS_STREAM_TYPE_AUDIO, sizeof(MSS_STREAM_TYPE_AUDIO) - 1) == 0)
{
request_params->tracks_mask[MEDIA_TYPE_AUDIO] = (1 << mss_track_index(fragment_params.bitrate));
}
else
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_mss_parse_uri_file_name: invalid media type %V", &fragment_params.media_type);
return NGX_HTTP_BAD_REQUEST;
}
request_params->segment_time = fragment_params.time / 10000;
// Note: assuming no discontinuity, if this changes the segment index will be recalculated
request_params->segment_index = segmenter_get_segment_index_no_discontinuity(
&conf->segmenter,
request_params->segment_time + SEGMENT_FROM_TIMESTAMP_MARGIN);
*request = conf->drm_enabled ? &mss_playready_fragment_request : &mss_fragment_request;
return NGX_OK;
}
// manifest
else if (ngx_http_vod_starts_with(start_pos, end_pos, &conf->mss.manifest_file_name_prefix))
{
*request = &mss_manifest_request;
start_pos += conf->mss.manifest_file_name_prefix.len;
rc = ngx_http_vod_parse_uri_file_name(r, start_pos, end_pos, PARSE_FILE_NAME_MULTI_STREAMS_PER_TYPE, request_params);
if (rc != NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_mss_parse_uri_file_name: ngx_http_vod_parse_uri_file_name failed %i", rc);
return rc;
}
return NGX_OK;
}
// fragment with non-standard format (used with redirect)
else if (ngx_http_vod_match_prefix_postfix(start_pos, end_pos, &conf->hls.m3u8_config.segment_file_name_prefix, m4s_file_ext))
{
start_pos += conf->hls.m3u8_config.segment_file_name_prefix.len;
end_pos -= (sizeof(m4s_file_ext) - 1);
*request = conf->drm_enabled ? &mss_playready_fragment_request : &mss_fragment_request;
return ngx_http_vod_parse_uri_file_name(r, start_pos, end_pos, PARSE_FILE_NAME_EXPECT_SEGMENT_INDEX, request_params);
}
else
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_mss_parse_uri_file_name: unidentified request");
return NGX_HTTP_BAD_REQUEST;
}
}
ngx_int_t
ngx_http_vod_mss_parse_drm_info(
ngx_http_vod_submodule_context_t* submodule_context,
ngx_str_t* drm_info,
void** output)
{
vod_status_t rc;
rc = udrm_parse_response(
&submodule_context->request_context,
drm_info,
TRUE,
output);
if (rc != VOD_OK)
{
return NGX_ERROR;
}
return NGX_OK;
}
DEFINE_SUBMODULE(mss);