Skip to content

Commit 997f24c

Browse files
committed
implemented key id recovery from iv
1 parent 8099f44 commit 997f24c

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

hls/ngx_rtmp_hls_module.c

+44-11
Original file line numberDiff line numberDiff line change
@@ -971,11 +971,11 @@ ngx_rtmp_hls_restore_stream(ngx_rtmp_session_t *s)
971971
ngx_file_t file;
972972
ssize_t ret;
973973
off_t offset;
974-
u_char *p, *last, *end, *next, *pa;
974+
u_char *p, *last, *end, *next, *pa, *pp, c;
975975
ngx_rtmp_hls_frag_t *f;
976976
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;
979979
static u_char buffer[4096];
980980

981981
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)
998998
duration = 0;
999999
discont = 0;
10001000
key_id = 0;
1001-
key = 0;
10021001

10031002
for ( ;; ) {
10041003

@@ -1046,8 +1045,47 @@ ngx_rtmp_hls_restore_stream(ngx_rtmp_session_t *s)
10461045
#define NGX_RTMP_XKEY_LEN (sizeof(NGX_RTMP_XKEY) - 1)
10471046

10481047
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+
}
10511089
}
10521090

10531091

@@ -1101,11 +1139,6 @@ ngx_rtmp_hls_restore_stream(ngx_rtmp_session_t *s)
11011139
mag *= 10;
11021140
}
11031141

1104-
if (key) {
1105-
key_id = f->id;
1106-
key = 0;
1107-
}
1108-
11091142
f->key_id = key_id;
11101143

11111144
ngx_rtmp_hls_next_frag(s);

0 commit comments

Comments
 (0)