Skip to content

Commit eff973c

Browse files
committed
fixes in hls encryption
1 parent 53064a4 commit eff973c

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

hls/ngx_rtmp_hls_module.c

+22-18
Original file line numberDiff line numberDiff line change
@@ -518,15 +518,15 @@ ngx_rtmp_hls_write_playlist(ngx_rtmp_session_t *s)
518518
p = buffer;
519519
end = p + sizeof(buffer);
520520

521-
p = ngx_slprintf(buffer, end,
521+
p = ngx_slprintf(p, end,
522522
"#EXTM3U\n"
523523
"#EXT-X-VERSION:3\n"
524524
"#EXT-X-MEDIA-SEQUENCE:%uL\n"
525525
"#EXT-X-TARGETDURATION:%ui\n",
526526
ctx->frag, max_frag);
527527

528528
if (hacf->type == NGX_RTMP_HLS_TYPE_EVENT) {
529-
ngx_slprintf(p, end, "#EXT-X-PLAYLIST-TYPE: EVENT\n");
529+
p = ngx_slprintf(p, end, "#EXT-X-PLAYLIST-TYPE: EVENT\n");
530530
}
531531

532532
n = ngx_write_fd(fd, buffer, p - buffer);
@@ -565,7 +565,7 @@ ngx_rtmp_hls_write_playlist(ngx_rtmp_session_t *s)
565565
p = ngx_slprintf(p, end, "#EXT-X-KEY:METHOD=AES-128,"
566566
"URI=\"%V%V%s%uL.key\",IV=0x%032XL",
567567
&hacf->keys_url, &key_name_part,
568-
key_sep, f->key_id);
568+
key_sep, f->key_id, f->key_id);
569569
}
570570

571571
p = ngx_slprintf(p, end,
@@ -914,18 +914,19 @@ ngx_rtmp_hls_open_fragment(ngx_rtmp_session_t *s, uint64_t ts,
914914
ctx->keyfile.data ? ctx->keyfile.data : (u_char *) "",
915915
ctx->frag, ctx->nfrags, ts, discont);
916916

917-
if (ngx_rtmp_mpegts_open_file(&ctx->file, ctx->stream.data,
918-
s->connection->log)
917+
if (hacf->keys &&
918+
ngx_rtmp_mpegts_init_encryption(&ctx->file, ctx->key, 16, ctx->key_id)
919919
!= NGX_OK)
920920
{
921+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
922+
"hls: failed to initialize hls encryption");
921923
return NGX_ERROR;
922924
}
923925

924-
if (hacf->keys &&
925-
ngx_rtmp_mpegts_init_encryption(&ctx->file, ctx->key, 16, id) != NGX_OK)
926+
if (ngx_rtmp_mpegts_open_file(&ctx->file, ctx->stream.data,
927+
s->connection->log)
928+
!= NGX_OK)
926929
{
927-
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
928-
"hls: failed to initialize hls encryption");
929930
return NGX_ERROR;
930931
}
931932

@@ -938,6 +939,7 @@ ngx_rtmp_hls_open_fragment(ngx_rtmp_session_t *s, uint64_t ts,
938939
f->active = 1;
939940
f->discont = discont;
940941
f->id = id;
942+
f->key_id = ctx->key_id;
941943

942944
ctx->frag_ts = ts;
943945

@@ -2096,8 +2098,8 @@ ngx_rtmp_hls_cleanup_dir(ngx_str_t *ppath, ngx_msec_t playlen)
20962098
{
20972099
max_age = playlen / 1000;
20982100

2099-
} else if (name.len >= 4 && name.data[name.len - 3] == '.' &&
2100-
name.data[name.len - 2] == 'k' &&
2101+
} else if (name.len >= 4 && name.data[name.len - 4] == '.' &&
2102+
name.data[name.len - 3] == 'k' &&
21012103
name.data[name.len - 2] == 'e' &&
21022104
name.data[name.len - 1] == 'y')
21032105
{
@@ -2296,15 +2298,13 @@ ngx_rtmp_hls_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
22962298
}
22972299
}
22982300

2299-
if (conf->keys_path.len == 0) {
2300-
conf->keys_path = conf->path;
2301-
}
2301+
ngx_conf_merge_str_value(conf->path, prev->path, "");
23022302

2303-
if (conf->keys && conf->keys_path.len && conf->cleanup &&
2304-
ngx_strcmp(conf->keys_path.data, conf->path.data) == 0 &&
2303+
if (conf->keys && conf->cleanup && conf->keys_path.len &&
2304+
ngx_strcmp(conf->keys_path.data, conf->path.data) != 0 &&
23052305
conf->type != NGX_RTMP_HLS_TYPE_EVENT)
23062306
{
2307-
if (conf->keys_path.data[conf->path.len - 1] == '/') {
2307+
if (conf->keys_path.data[conf->keys_path.len - 1] == '/') {
23082308
conf->keys_path.len--;
23092309
}
23102310

@@ -2332,7 +2332,11 @@ ngx_rtmp_hls_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
23322332
}
23332333
}
23342334

2335-
ngx_conf_merge_str_value(conf->path, prev->path, "");
2335+
ngx_conf_merge_str_value(conf->keys_path, prev->keys_path, "");
2336+
2337+
if (conf->keys_path.len == 0) {
2338+
conf->keys_path = conf->path;
2339+
}
23362340

23372341
return NGX_CONF_OK;
23382342
}

0 commit comments

Comments
 (0)