@@ -491,6 +491,7 @@ ngx_rtmp_hls_write_playlist(ngx_rtmp_session_t *s)
491
491
ngx_rtmp_hls_frag_t * f ;
492
492
ngx_uint_t i , max_frag ;
493
493
ngx_str_t name_part , key_name_part ;
494
+ uint64_t prev_key_id ;
494
495
const char * sep , * key_sep ;
495
496
496
497
@@ -552,6 +553,8 @@ ngx_rtmp_hls_write_playlist(ngx_rtmp_session_t *s)
552
553
key_name_part = ctx -> name ;
553
554
}
554
555
556
+ prev_key_id = 0 ;
557
+
555
558
for (i = 0 ; i < ctx -> nfrags ; i ++ ) {
556
559
f = ngx_rtmp_hls_get_frag (s , i );
557
560
@@ -562,13 +565,15 @@ ngx_rtmp_hls_write_playlist(ngx_rtmp_session_t *s)
562
565
p = ngx_slprintf (p , end , "#EXT-X-DISCONTINUITY\n" );
563
566
}
564
567
565
- if (hacf -> keys && (i == 0 || f -> id != f -> key_id )) {
568
+ if (hacf -> keys && (i == 0 || f -> key_id != prev_key_id )) {
566
569
p = ngx_slprintf (p , end , "#EXT-X-KEY:METHOD=AES-128,"
567
- "URI=\"%V%V%s%uL.key\",IV=0x%032XL" ,
570
+ "URI=\"%V%V%s%uL.key\",IV=0x%032XL\n " ,
568
571
& hacf -> keys_url , & key_name_part ,
569
572
key_sep , f -> key_id , f -> key_id );
570
573
}
571
574
575
+ prev_key_id = f -> key_id ;
576
+
572
577
p = ngx_slprintf (p , end ,
573
578
"#EXTINF:%.3f,\n"
574
579
"%V%V%s%uL.ts\n" ,
@@ -884,10 +889,10 @@ ngx_rtmp_hls_open_fragment(ngx_rtmp_session_t *s, uint64_t ts,
884
889
return NGX_ERROR ;
885
890
}
886
891
887
- ngx_sprintf (ctx -> keyfile .data + ctx -> keyfile .len , "%uL.ts %Z" , id );
892
+ ngx_sprintf (ctx -> keyfile .data + ctx -> keyfile .len , "%uL.key %Z" , id );
888
893
889
- fd = ngx_open_file (ctx -> keyfile .data , NGX_FILE_RDONLY ,
890
- NGX_FILE_OPEN , 0 );
894
+ fd = ngx_open_file (ctx -> keyfile .data , NGX_FILE_WRONLY ,
895
+ NGX_FILE_TRUNCATE , NGX_FILE_DEFAULT_ACCESS );
891
896
892
897
if (fd == NGX_INVALID_FILE ) {
893
898
ngx_log_error (NGX_LOG_ERR , s -> connection -> log , ngx_errno ,
@@ -969,8 +974,8 @@ ngx_rtmp_hls_restore_stream(ngx_rtmp_session_t *s)
969
974
u_char * p , * last , * end , * next , * pa ;
970
975
ngx_rtmp_hls_frag_t * f ;
971
976
double duration ;
972
- ngx_int_t discont ;
973
- uint64_t mag ;
977
+ ngx_int_t discont , key ;
978
+ uint64_t mag , key_id ;
974
979
static u_char buffer [4096 ];
975
980
976
981
ctx = ngx_rtmp_get_module_ctx (s , ngx_rtmp_hls_module );
@@ -992,6 +997,8 @@ ngx_rtmp_hls_restore_stream(ngx_rtmp_session_t *s)
992
997
f = NULL ;
993
998
duration = 0 ;
994
999
discont = 0 ;
1000
+ key_id = 0 ;
1001
+ key = 0 ;
995
1002
996
1003
for ( ;; ) {
997
1004
@@ -1035,6 +1042,14 @@ ngx_rtmp_hls_restore_stream(ngx_rtmp_session_t *s)
1035
1042
}
1036
1043
1037
1044
1045
+ #define NGX_RTMP_XKEY "#EXT-X-KEY:"
1046
+ #define NGX_RTMP_XKEY_LEN (sizeof(NGX_RTMP_XKEY) - 1)
1047
+
1048
+ if (ngx_memcmp (p , NGX_RTMP_XKEY , NGX_RTMP_XKEY_LEN ) == 0 ) {
1049
+ key = 1 ;
1050
+ }
1051
+
1052
+
1038
1053
#define NGX_RTMP_EXTINF "#EXTINF:"
1039
1054
#define NGX_RTMP_EXTINF_LEN (sizeof(NGX_RTMP_EXTINF) - 1)
1040
1055
@@ -1085,6 +1100,13 @@ ngx_rtmp_hls_restore_stream(ngx_rtmp_session_t *s)
1085
1100
mag *= 10 ;
1086
1101
}
1087
1102
1103
+ if (key ) {
1104
+ key_id = f -> id ;
1105
+ key = 0 ;
1106
+ }
1107
+
1108
+ f -> key_id = key_id ;
1109
+
1088
1110
ngx_rtmp_hls_next_frag (s );
1089
1111
1090
1112
ngx_log_debug6 (NGX_LOG_DEBUG_RTMP , s -> connection -> log , 0 ,
0 commit comments