fix(hls): keep the EXT-X-KEY of another KEYFORMAT in force when a playlist renews one key tag

This commit is contained in:
imSp4rky
2026-12-01 18:58:20 -07:00
parent 3963e80ec3
commit 7a5bac7878
5 changed files with 94 additions and 13 deletions
+2 -3
View File
@@ -48,7 +48,7 @@ from unshackle.core.drm.segment_decrypt import SegmentDecrypter, can_use
from unshackle.core.drm.verify import decrypt_track
from unshackle.core.events import events
from unshackle.core.manifests.dash import PeriodRebase, mdhd_timescale, missing_language, rebase_webvtt_files
from unshackle.core.manifests.hls_keys import KeySpan, key_spans
from unshackle.core.manifests.hls_keys import KeySpan, key_spans, keyformat
from unshackle.core.manifests.segment_pipeline import (
append_segment,
check_decrypted,
@@ -1627,8 +1627,7 @@ class HLS:
spans = key_spans(manifest, manifest.segments)
clear_inits = [span.init_section for span in spans if any(key.method == "NONE" for key in span.keys if key)]
return all(
span.init_section not in clear_inits
and any((key.keyformat or "identity").lower() != "identity" for key in span.keys if key)
span.init_section not in clear_inits and any(keyformat(key) != "identity" for key in span.keys if key)
for span in spans
if HLS.encrypts(span.keys)
)
+25 -6
View File
@@ -20,20 +20,39 @@ class KeySpan(NamedTuple):
init_section: m3u8.model.InitializationSection | None
def keyformat(key: m3u8.model.Key) -> str:
"""The KEYFORMAT of an EXT-X-KEY in lower case, identity when it has none."""
return (key.keyformat or "identity").lower()
def by_keyformat(keys: list[m3u8.model.Key]) -> dict[str, list[m3u8.model.Key]]:
"""`keys` grouped by `keyformat`, in the order of the first tag of each group."""
groups: dict[str, list[m3u8.model.Key]] = {}
for key in keys:
groups.setdefault(keyformat(key), []).append(key)
return groups
def key_spans(manifest: M3U8, wanted_segments: list[Any]) -> list[KeySpan]:
"""The spans of `wanted_segments`, which are segments of `manifest` in playlist order.
The EXT-X-KEY tags before a segment govern it and each later segment up to the next such tags, whatever their
KEYFORMAT, also when the segment is not wanted. python-m3u8 puts them on that segment only, and on no segment when
EXT-X-DISCONTINUITY-SEQUENCE follows. RFC 8216 4.3.3.3 allows that tag only before the first segment, so a lost
EXT-X-KEY heads `manifest.keys`.
An EXT-X-KEY tag governs its segment and each later segment up to the next tag with the same `keyformat` and
takes its place, also when the segment is not wanted (RFC 8216 4.3.2.4). A new tag ends each tag in force of
another METHOD, METHOD=NONE included, because the tags of one segment give one content key. python-m3u8 puts the
tags on that segment only, and on no segment when EXT-X-DISCONTINUITY-SEQUENCE follows. RFC 8216 4.3.3.3 allows
that tag only before the first segment, so a lost EXT-X-KEY heads `manifest.keys`, before the tags of the first
segment.
"""
wanted = {id(segment) for segment in wanted_segments}
spans: list[KeySpan] = []
keys: list[Any] = list(takewhile(bool, manifest.keys or []))
first = manifest.segments[0].keys if manifest.segments else []
keys: list[Any] = list(takewhile(lambda key: key and key not in first, manifest.keys or []))
start = 0
for segment in manifest.segments:
keys = segment.keys or keys
if segment.keys:
methods = {key.method for key in segment.keys}
kept = by_keyformat([key for key in keys if key.method in methods])
keys = [key for group in (kept | by_keyformat(segment.keys)).values() for key in group]
if id(segment) not in wanted:
continue
if not spans or keys != spans[-1].keys or segment.init_section != spans[-1].init_section: