forked from kaltura/nginx-vod-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_http_vod_dash.c
442 lines (395 loc) · 13.1 KB
/
ngx_http_vod_dash.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include <ngx_http.h>
#include "ngx_http_vod_submodule.h"
#include "ngx_http_vod_utils.h"
#include "ngx_simple_json_parser.h"
#include "vod/dash/dash_packager.h"
#include "vod/dash/edash_packager.h"
// content types
static u_char mpd_content_type[] = "application/dash+xml";
static u_char mp4_audio_content_type[] = "audio/mp4";
static u_char mp4_video_content_type[] = "video/mp4";
// file extensions
static const u_char manifest_file_ext[] = ".mpd";
static const u_char init_segment_file_ext[] = ".mp4";
static const u_char fragment_file_ext[] = ".m4s";
// drm info json keys
ngx_str_t drm_info_key = ngx_string("key");
ngx_str_t drm_info_key_id = ngx_string("key_id");
ngx_str_t drm_info_pssh = ngx_string("pssh");
ngx_str_t drm_info_system_id = ngx_string("uuid");
ngx_str_t drm_info_data = ngx_string("data");
static ngx_int_t
ngx_http_vod_dash_handle_manifest(
ngx_http_vod_submodule_context_t* submodule_context,
ngx_str_t* response,
ngx_str_t* content_type)
{
ngx_str_t base_url = ngx_null_string;
vod_status_t rc;
if (submodule_context->conf->dash.absolute_manifest_urls)
{
ngx_http_vod_get_base_url(submodule_context->r, submodule_context->conf, &submodule_context->r->uri, &base_url);
}
if (submodule_context->conf->drm_enabled)
{
rc = edash_packager_build_mpd(
&submodule_context->request_context,
&submodule_context->conf->dash.mpd_config,
&base_url,
&submodule_context->conf->segmenter,
&submodule_context->mpeg_metadata,
response);
}
else
{
rc = dash_packager_build_mpd(
&submodule_context->request_context,
&submodule_context->conf->dash.mpd_config,
&base_url,
&submodule_context->conf->segmenter,
&submodule_context->mpeg_metadata,
0,
NULL,
NULL,
response);
}
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_dash_handle_manifest: (e)dash_packager_build_mpd failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
content_type->data = mpd_content_type;
content_type->len = sizeof(mpd_content_type) - 1;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_dash_handle_init_segment(
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 = edash_packager_build_init_mp4(
&submodule_context->request_context,
&submodule_context->mpeg_metadata,
submodule_context->conf->drm_clear_lead_segment_count > 0,
ngx_http_vod_submodule_size_only(submodule_context),
response);
}
else
{
rc = dash_packager_build_init_mp4(
&submodule_context->request_context,
&submodule_context->mpeg_metadata,
ngx_http_vod_submodule_size_only(submodule_context),
NULL,
NULL,
response);
}
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_dash_handle_init_segment: (e)dash_packager_build_init_mp4 failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
if (submodule_context->mpeg_metadata.stream_count[MEDIA_TYPE_VIDEO])
{
content_type->data = mp4_video_content_type;
content_type->len = sizeof(mp4_video_content_type) - 1;
}
else
{
content_type->data = mp4_audio_content_type;
content_type->len = sizeof(mp4_audio_content_type) - 1;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_dash_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)
{
fragment_writer_state_t* state;
segment_writer_t edash_writer;
vod_status_t rc;
bool_t size_only = ngx_http_vod_submodule_size_only(submodule_context);
if (submodule_context->conf->drm_enabled &&
submodule_context->request_params.segment_index >= submodule_context->conf->drm_clear_lead_segment_count)
{
rc = edash_packager_get_fragment_writer(
&edash_writer,
&submodule_context->request_context,
submodule_context->mpeg_metadata.first_stream,
submodule_context->request_params.segment_index,
segment_writer,
submodule_context->cur_suburi->file_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_dash_init_frame_processor: edash_packager_get_fragment_writer failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
segment_writer = &edash_writer;
}
else
{
// build the fragment header
rc = dash_packager_build_fragment_header(
&submodule_context->request_context,
submodule_context->mpeg_metadata.first_stream,
submodule_context->request_params.segment_index,
submodule_context->conf->drm_enabled ? 2 : 0,
0,
NULL,
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_dash_init_frame_processor: dash_packager_build_fragment_header failed %i", rc);
return ngx_http_vod_status_to_ngx_error(rc);
}
}
// initialize the frame processor
if (!size_only || *response_size == 0)
{
rc = mp4_builder_frame_writer_init(
&submodule_context->request_context,
submodule_context->mpeg_metadata.first_stream,
read_cache_state,
segment_writer->write_tail,
segment_writer->context,
&state);
if (rc != VOD_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_dash_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->mpeg_metadata.stream_count[MEDIA_TYPE_VIDEO])
{
content_type->len = sizeof(mp4_video_content_type) - 1;
content_type->data = (u_char *)mp4_video_content_type;
}
else
{
content_type->len = sizeof(mp4_audio_content_type) - 1;
content_type->data = (u_char *)mp4_audio_content_type;
}
return NGX_OK;
}
static const ngx_http_vod_request_t dash_manifest_request = {
0,
PARSE_FLAG_DURATION_LIMITS_AND_TOTAL_SIZE | PARSE_FLAG_CODEC_NAME,
dash_packager_compare_streams,
offsetof(ngx_http_vod_loc_conf_t, duplicate_bitrate_threshold),
REQUEST_CLASS_MANIFEST,
ngx_http_vod_dash_handle_manifest,
NULL,
};
static const ngx_http_vod_request_t dash_init_request = {
REQUEST_FLAG_SINGLE_STREAM,
PARSE_BASIC_METADATA_ONLY | PARSE_FLAG_SAVE_RAW_ATOMS,
NULL,
0,
REQUEST_CLASS_OTHER,
ngx_http_vod_dash_handle_init_segment,
NULL,
};
static const ngx_http_vod_request_t dash_fragment_request = {
REQUEST_FLAG_SINGLE_STREAM,
PARSE_FLAG_FRAMES_ALL,
NULL,
0,
REQUEST_CLASS_SEGMENT,
NULL,
ngx_http_vod_dash_init_frame_processor,
};
static const ngx_http_vod_request_t edash_fragment_request = {
REQUEST_FLAG_SINGLE_STREAM,
PARSE_FLAG_FRAMES_ALL | PARSE_FLAG_PARSED_EXTRA_DATA,
NULL,
0,
REQUEST_CLASS_SEGMENT,
NULL,
ngx_http_vod_dash_init_frame_processor,
};
static void
ngx_http_vod_dash_create_loc_conf(
ngx_conf_t *cf,
ngx_http_vod_dash_loc_conf_t *conf)
{
conf->absolute_manifest_urls = NGX_CONF_UNSET;
conf->mpd_config.segment_timeline = NGX_CONF_UNSET;
}
static char *
ngx_http_vod_dash_merge_loc_conf(
ngx_conf_t *cf,
ngx_http_vod_loc_conf_t *base,
ngx_http_vod_dash_loc_conf_t *conf,
ngx_http_vod_dash_loc_conf_t *prev)
{
ngx_conf_merge_value(conf->absolute_manifest_urls, prev->absolute_manifest_urls, 1);
ngx_conf_merge_str_value(conf->manifest_file_name_prefix, prev->manifest_file_name_prefix, "manifest");
ngx_conf_merge_str_value(conf->mpd_config.init_file_name_prefix, prev->mpd_config.init_file_name_prefix, "init");
ngx_conf_merge_str_value(conf->mpd_config.fragment_file_name_prefix, prev->mpd_config.fragment_file_name_prefix, "fragment");
ngx_conf_merge_value(conf->mpd_config.segment_timeline, prev->mpd_config.segment_timeline, 1);
return NGX_CONF_OK;
}
static int
ngx_http_vod_dash_get_file_path_components(ngx_str_t* uri)
{
return 1;
}
static ngx_int_t
ngx_http_vod_dash_parse_uri_file_name(
ngx_http_request_t *r,
ngx_http_vod_loc_conf_t *conf,
u_char* start_pos,
u_char* end_pos,
ngx_http_vod_request_params_t* request_params)
{
ngx_int_t rc;
bool_t expect_segment_index;
// fragment
if (ngx_http_vod_match_prefix_postfix(start_pos, end_pos, &conf->dash.mpd_config.fragment_file_name_prefix, fragment_file_ext))
{
start_pos += conf->dash.mpd_config.fragment_file_name_prefix.len;
end_pos -= (sizeof(fragment_file_ext) - 1);
request_params->request = conf->drm_enabled ? &edash_fragment_request : &dash_fragment_request;
expect_segment_index = TRUE;
}
// init segment
else if (ngx_http_vod_match_prefix_postfix(start_pos, end_pos, &conf->dash.mpd_config.init_file_name_prefix, init_segment_file_ext))
{
start_pos += conf->dash.mpd_config.init_file_name_prefix.len;
end_pos -= (sizeof(init_segment_file_ext) - 1);
request_params->request = &dash_init_request;
expect_segment_index = FALSE;
}
// manifest
else if (ngx_http_vod_match_prefix_postfix(start_pos, end_pos, &conf->dash.manifest_file_name_prefix, manifest_file_ext))
{
start_pos += conf->dash.manifest_file_name_prefix.len;
end_pos -= (sizeof(manifest_file_ext) - 1);
request_params->request = &dash_manifest_request;
expect_segment_index = FALSE;
}
else
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_dash_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, expect_segment_index, request_params);
if (rc != NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_dash_parse_uri_file_name: ngx_http_vod_parse_uri_file_name failed %i", rc);
return rc;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_dash_parse_drm_info(
ngx_http_vod_submodule_context_t* submodule_context,
ngx_str_t* drm_info,
void** output)
{
edash_drm_info_t* result;
ngx_json_value_t* cur_input_pssh;
edash_pssh_info_t* cur_output_pssh;
ngx_json_value_t parsed_info;
ngx_array_t *pssh_array;
ngx_int_t rc;
ngx_uint_t i;
result = ngx_palloc(submodule_context->r->pool, sizeof(*result));
if (result == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_dash_parse_drm_info: ngx_palloc failed (1)");
return NGX_ERROR;
}
// note: drm_info is guaranteed to be null terminated
rc = ngx_json_parse(submodule_context->r->pool, drm_info->data, &parsed_info);
if (rc != NGX_JSON_OK)
{
ngx_log_error(NGX_LOG_ERR, submodule_context->request_context.log, 0,
"ngx_http_vod_dash_parse_drm_info: ngx_json_parse failed %i", rc);
return NGX_ERROR;
}
rc = ngx_json_get_element_fixed_binary_string(&parsed_info, &drm_info_key, result->key, sizeof(result->key));
if (rc != NGX_JSON_OK)
{
ngx_log_error(NGX_LOG_ERR, submodule_context->request_context.log, 0,
"ngx_http_vod_dash_parse_drm_info: ngx_json_get_element_fixed_binary_string(key) failed %i", rc);
return NGX_ERROR;
}
rc = ngx_json_get_element_fixed_binary_string(&parsed_info, &drm_info_key_id, result->key_id, sizeof(result->key_id));
if (rc != NGX_JSON_OK)
{
ngx_log_error(NGX_LOG_ERR, submodule_context->request_context.log, 0,
"ngx_http_vod_dash_parse_drm_info: ngx_json_get_element_fixed_binary_string(key_id) failed %i", rc);
return NGX_ERROR;
}
rc = ngx_json_get_element_array(&parsed_info, &drm_info_pssh, &pssh_array);
if (rc != NGX_JSON_OK)
{
ngx_log_error(NGX_LOG_ERR, submodule_context->request_context.log, 0,
"ngx_http_vod_dash_parse_drm_info: ngx_json_get_element_array(pssh) failed %i", rc);
return NGX_ERROR;
}
result->pssh_array.count = pssh_array->nelts;
result->pssh_array.first = ngx_palloc(
submodule_context->r->pool,
sizeof(*result->pssh_array.first) * result->pssh_array.count);
if (result->pssh_array.first == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, submodule_context->request_context.log, 0,
"ngx_http_vod_dash_parse_drm_info: ngx_palloc failed (2)");
return NGX_ERROR;
}
result->pssh_array.last = result->pssh_array.first + result->pssh_array.count;
for (i = 0; i < pssh_array->nelts; i++)
{
cur_input_pssh = (ngx_json_value_t*)pssh_array->elts + i;
cur_output_pssh = result->pssh_array.first + i;
rc = ngx_json_get_element_guid_string(cur_input_pssh, &drm_info_system_id, cur_output_pssh->system_id);
if (rc != NGX_JSON_OK)
{
ngx_log_error(NGX_LOG_ERR, submodule_context->request_context.log, 0,
"ngx_http_vod_dash_parse_drm_info: ngx_json_get_element_guid_string(uuid) failed %i", rc);
return NGX_ERROR;
}
rc = ngx_json_get_element_binary_string(submodule_context->r->pool, cur_input_pssh, &drm_info_data, &cur_output_pssh->data);
if (rc != NGX_JSON_OK)
{
ngx_log_error(NGX_LOG_ERR, submodule_context->request_context.log, 0,
"ngx_http_vod_dash_parse_drm_info: ngx_json_get_element_binary_string(data) failed %i", rc);
return NGX_ERROR;
}
}
*output = result;
return NGX_OK;
}
DEFINE_SUBMODULE(dash);