@@ -971,11 +971,11 @@ ngx_rtmp_hls_restore_stream(ngx_rtmp_session_t *s)
971
971
ngx_file_t file ;
972
972
ssize_t ret ;
973
973
off_t offset ;
974
- u_char * p , * last , * end , * next , * pa ;
974
+ u_char * p , * last , * end , * next , * pa , * pp , c ;
975
975
ngx_rtmp_hls_frag_t * f ;
976
976
double duration ;
977
- ngx_int_t discont , key ;
978
- uint64_t mag , key_id ;
977
+ ngx_int_t discont ;
978
+ uint64_t mag , key_id , base ;
979
979
static u_char buffer [4096 ];
980
980
981
981
ctx = ngx_rtmp_get_module_ctx (s , ngx_rtmp_hls_module );
@@ -998,7 +998,6 @@ ngx_rtmp_hls_restore_stream(ngx_rtmp_session_t *s)
998
998
duration = 0 ;
999
999
discont = 0 ;
1000
1000
key_id = 0 ;
1001
- key = 0 ;
1002
1001
1003
1002
for ( ;; ) {
1004
1003
@@ -1046,8 +1045,47 @@ ngx_rtmp_hls_restore_stream(ngx_rtmp_session_t *s)
1046
1045
#define NGX_RTMP_XKEY_LEN (sizeof(NGX_RTMP_XKEY) - 1)
1047
1046
1048
1047
if (ngx_memcmp (p , NGX_RTMP_XKEY , NGX_RTMP_XKEY_LEN ) == 0 ) {
1049
- key = 1 ;
1050
- /* TODO: parse key_id since it may differ from id */
1048
+
1049
+ /* recover key id from initialization vector */
1050
+
1051
+ key_id = 0 ;
1052
+ base = 1 ;
1053
+ pp = last - 1 ;
1054
+
1055
+ for ( ;; ) {
1056
+ if (pp < p ) {
1057
+ ngx_log_error (NGX_LOG_ERR , s -> connection -> log , 0 ,
1058
+ "hls: failed to read key id" );
1059
+ break ;
1060
+ }
1061
+
1062
+ c = * pp ;
1063
+ if (c == 'x' ) {
1064
+ break ;
1065
+ }
1066
+
1067
+ if (c >= '0' && c <= '9' ) {
1068
+ c -= '0' ;
1069
+ goto next ;
1070
+ }
1071
+
1072
+ c |= 0x20 ;
1073
+
1074
+ if (c >= 'a' && c <= 'f' ) {
1075
+ c -= 'a' - 10 ;
1076
+ goto next ;
1077
+ }
1078
+
1079
+ ngx_log_error (NGX_LOG_ERR , s -> connection -> log , 0 ,
1080
+ "hls: bad character in key id" );
1081
+ break ;
1082
+
1083
+ next :
1084
+
1085
+ key_id += base * c ;
1086
+ base *= 0x10 ;
1087
+ pp -- ;
1088
+ }
1051
1089
}
1052
1090
1053
1091
@@ -1101,11 +1139,6 @@ ngx_rtmp_hls_restore_stream(ngx_rtmp_session_t *s)
1101
1139
mag *= 10 ;
1102
1140
}
1103
1141
1104
- if (key ) {
1105
- key_id = f -> id ;
1106
- key = 0 ;
1107
- }
1108
-
1109
1142
f -> key_id = key_id ;
1110
1143
1111
1144
ngx_rtmp_hls_next_frag (s );
0 commit comments