mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-03 06:25:10 +00:00
fix(hls): bridge a stitched one-key FairPlay playlist with an all-zero tenc KID
This commit is contained in:
@@ -40,6 +40,7 @@ from unshackle.core.drm.cenc import (
|
||||
drm_stub,
|
||||
hls_key_drm,
|
||||
init_pssh_kid,
|
||||
is_clear_init,
|
||||
key_data,
|
||||
key_system,
|
||||
)
|
||||
@@ -1449,26 +1450,40 @@ class HLS:
|
||||
if not map_section:
|
||||
return None
|
||||
|
||||
map_uri = urljoin(map_section.base_uri or master.base_uri or "", map_section.uri)
|
||||
init_data = HLS.probe_init_section(master, session, map_section)
|
||||
if init_data is None:
|
||||
return None
|
||||
try:
|
||||
if map_section.byterange:
|
||||
byte_range = HLS.calculate_byte_range(map_section.byterange, 0)
|
||||
kid = track.get_key_id(init_data)
|
||||
if pssh and (kid is None or kid.int == 0):
|
||||
return init_pssh_kid(init_data)
|
||||
return kid
|
||||
except Exception as e:
|
||||
logging.getLogger("HLS").debug(f"Init map key-id probe failed for {map_section.uri}: {e!r}")
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def probe_init_section(
|
||||
master: M3U8, session: Session | RnetSession, init_section: m3u8.model.InitializationSection
|
||||
) -> bytes | None:
|
||||
"""The bytes of an init segment (EXT-X-MAP), or None when the request for it fails."""
|
||||
try:
|
||||
map_uri = urljoin(init_section.base_uri or master.base_uri or "", init_section.uri)
|
||||
if init_section.byterange:
|
||||
byte_range = HLS.calculate_byte_range(init_section.byterange, 0)
|
||||
headers = {"Range": f"bytes={byte_range}"}
|
||||
else:
|
||||
headers = {}
|
||||
map_res = unrecorded(session, "get", map_uri, headers=headers)
|
||||
if map_res.ok:
|
||||
kid = track.get_key_id(map_res.content)
|
||||
if pssh and (kid is None or kid.int == 0):
|
||||
return init_pssh_kid(map_res.content)
|
||||
return kid
|
||||
return bytes(map_res.content)
|
||||
logging.getLogger("HLS").warning(
|
||||
f"Could not read the Key ID from the init segment {safe_display_url(map_uri)}: "
|
||||
f"HTTP {map_res.status_code}"
|
||||
)
|
||||
# best-effort key-id probe over network + init parse; caller falls back without it
|
||||
# best-effort request; caller falls back without it
|
||||
except Exception as e:
|
||||
logging.getLogger("HLS").debug(f"Init map key-id probe failed for {map_uri}: {e!r}")
|
||||
logging.getLogger("HLS").debug(f"Init segment request failed for {init_section.uri}: {e!r}")
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
@@ -1510,19 +1525,59 @@ class HLS:
|
||||
|
||||
None when `only_unsupported_keys` is false of `manifest`, or when the init segment changes with no
|
||||
EXT-X-DISCONTINUITY: the merge splits there only on a changed EXT-X-KEY, and the tags go.
|
||||
The pssh boxes count only for one init segment in a one-key playlist: an all-zero tenc takes any content key.
|
||||
The pssh boxes count only in a one-key playlist, and with several init segments only as `get_stitched_kid` reads
|
||||
them: an all-zero tenc takes any content key.
|
||||
"""
|
||||
keyed = [span for span in key_spans(manifest, segments) if span.init_section and span.keys]
|
||||
if not keyed or not HLS.only_unsupported_keys(manifest):
|
||||
return None
|
||||
init_section = keyed[0].init_section
|
||||
one_key = len({key.uri for key in manifest.keys if key}) == 1
|
||||
pssh = one_key and all(span.init_section == init_section for span in keyed)
|
||||
if any(b.init_section != a.init_section and not b.discontinuity for a, b in pairwise(segments)):
|
||||
return None
|
||||
kid = HLS.get_track_kid_from_init(manifest, track, session, init_section, pssh=pssh)
|
||||
init_sections: list[m3u8.model.InitializationSection] = []
|
||||
for span in keyed:
|
||||
if span.init_section not in init_sections:
|
||||
init_sections.append(span.init_section)
|
||||
if one_key and len(init_sections) > 1:
|
||||
kid = HLS.get_stitched_kid(manifest, init_sections, track, session)
|
||||
else:
|
||||
kid = HLS.get_track_kid_from_init(manifest, track, session, init_sections[0], pssh=one_key)
|
||||
return None if kid in PLACEHOLDER_KIDS else kid
|
||||
|
||||
@staticmethod
|
||||
def get_stitched_kid(
|
||||
manifest: M3U8,
|
||||
init_sections: list[m3u8.model.InitializationSection],
|
||||
track: Track,
|
||||
session: Session | RnetSession,
|
||||
) -> UUID | None:
|
||||
"""The tenc Key ID of the first init segment, or when that is all-zero, the one Key ID that all of them give.
|
||||
|
||||
Each gives its tenc Key ID, or else that of its pssh boxes. One that is clear does not count, and one that
|
||||
unshackle cannot read makes it None.
|
||||
"""
|
||||
found: UUID | None = None
|
||||
for init_section in init_sections:
|
||||
init_data = HLS.probe_init_section(manifest, session, init_section)
|
||||
if not init_data:
|
||||
return None
|
||||
try:
|
||||
if is_clear_init(init_data):
|
||||
continue
|
||||
kid = track.get_key_id(init_data)
|
||||
if not (kid and kid.int):
|
||||
kid = init_pssh_kid(init_data)
|
||||
elif init_section is init_sections[0]:
|
||||
return kid
|
||||
except Exception as e:
|
||||
logging.getLogger("HLS").debug(f"Init map key-id probe failed for {init_section.uri}: {e!r}")
|
||||
return None
|
||||
if kid is None or found not in (None, kid):
|
||||
logging.getLogger("HLS").debug(f"{track.id}: init segment {init_section.uri} gives key ID {kid}")
|
||||
return None
|
||||
found = kid
|
||||
return found
|
||||
|
||||
@staticmethod
|
||||
def only_unsupported_keys(manifest: M3U8) -> bool:
|
||||
"""Whether each EXT-X-KEY of `manifest`, at least one, is sample encryption of a DRM system unshackle cannot license.
|
||||
|
||||
Reference in New Issue
Block a user