forked from kenzuya/unshackle
fix(dash): license a track whose only key system is the W3C common PSSH as ClearKey
This commit is contained in:
@@ -10,7 +10,7 @@ from typing import TYPE_CHECKING, Any, Self
|
||||
from uuid import UUID
|
||||
|
||||
import m3u8
|
||||
from construct import Container
|
||||
from construct import ConstructError, Container
|
||||
from pymp4.parser import Box
|
||||
from pyplayready.system.pssh import PSSH as PR_PSSH
|
||||
from pywidevine.cdm import Cdm as WidevineCdm
|
||||
@@ -34,6 +34,7 @@ if TYPE_CHECKING:
|
||||
|
||||
WIDEVINE_ID: UUID = WV_PSSH.SystemId.Widevine
|
||||
PLAYREADY_ID: UUID = PR_PSSH.SYSTEM_ID
|
||||
COMMON_ID = UUID("1077efec-c0b2-4d02-ace3-3c1e52e2fb4b")
|
||||
KEYFORMATS = {
|
||||
WidevineCdm.urn: WIDEVINE_ID,
|
||||
f"urn:uuid:{PLAYREADY_ID}": PLAYREADY_ID,
|
||||
@@ -46,6 +47,17 @@ PLACEHOLDER_KIDS = {
|
||||
}
|
||||
|
||||
|
||||
def common_pssh_kids(pssh_b64: str | None) -> list[UUID]:
|
||||
"""The real KIDs of a base64 v1 PSSH box of the W3C common system ID, or an empty list for any other data."""
|
||||
try:
|
||||
box = Box.parse(base64.b64decode(pssh_b64 or ""))
|
||||
except (ValueError, ConstructError):
|
||||
return []
|
||||
if box.type != b"pssh" or box.system_ID != COMMON_ID or box.version != 1:
|
||||
return []
|
||||
return [kid for kid in box.key_IDs if kid not in PLACEHOLDER_KIDS]
|
||||
|
||||
|
||||
def key_system(key: Any) -> UUID | None:
|
||||
"""The system ID that an EXT-X-KEY's KEYFORMAT names, or None when it names neither Widevine nor PlayReady."""
|
||||
return KEYFORMATS.get((getattr(key, "keyformat", None) or "").lower())
|
||||
|
||||
@@ -31,6 +31,7 @@ from unshackle.core.config import config
|
||||
from unshackle.core.constants import DOWNLOAD_ALL_DRM, DOWNLOAD_LICENCE_ONLY, AnyTrack
|
||||
from unshackle.core.downloaders.requests import parse_content_range, range_answers, request_range
|
||||
from unshackle.core.drm import DRM_T, ClearKeyCENC, PlayReady, Widevine
|
||||
from unshackle.core.drm.cenc import COMMON_ID, PLACEHOLDER_KIDS, common_pssh_kids
|
||||
from unshackle.core.drm.segment_decrypt import SEGMENT_DRM_T, SegmentDecrypter, can_use
|
||||
from unshackle.core.drm.verify import walk
|
||||
from unshackle.core.manifests.segment_pipeline import (
|
||||
@@ -496,9 +497,8 @@ class DASH:
|
||||
# Preserve existing DRM if it was set by the service, especially when service set Widevine
|
||||
# but manifest only contains PlayReady protection (common scenario for some services)
|
||||
existing_drm = track.drm
|
||||
manifest_drm = DASH.get_drm(
|
||||
representation.findall("ContentProtection") + adaptation_set.findall("ContentProtection")
|
||||
)
|
||||
protections = representation.findall("ContentProtection") + adaptation_set.findall("ContentProtection")
|
||||
manifest_drm = DASH.get_drm(protections)
|
||||
|
||||
# Only override existing DRM if:
|
||||
# 1. No existing DRM was set, OR
|
||||
@@ -642,6 +642,8 @@ class DASH:
|
||||
elif not track.drm and init_data and isinstance(track, (Video, Audio)):
|
||||
pssh_init = init_data
|
||||
track.drm_from_pssh(cdm, lambda system: system.from_init_data(pssh_init), log)
|
||||
if not track.drm and (clearkey := DASH.get_common_clearkey(protections)):
|
||||
track.drm = [clearkey]
|
||||
|
||||
drm = track.get_drm_for_cdm(cdm)
|
||||
license_drm(track, drm, ctx, track_kid, probe=lambda: track.get_key_id(url=segments[0][0], session=session))
|
||||
@@ -1507,11 +1509,6 @@ class DASH:
|
||||
@staticmethod
|
||||
def get_drm(protections: list[Element]) -> list[DRM_T]:
|
||||
drm: list[DRM_T] = []
|
||||
PLACEHOLDER_KIDS = {
|
||||
UUID("00000000-0000-0000-0000-000000000000"), # All zeros (key rotation default)
|
||||
UUID("00010203-0405-0607-0809-0a0b0c0d0e0f"), # Sequential 0x00-0x0f
|
||||
UUID("00010203-0405-0607-0809-101112131415"), # Shaka Packager test pattern
|
||||
}
|
||||
|
||||
for protection in protections:
|
||||
urn = (protection.get("schemeIdUri") or "").lower()
|
||||
@@ -1570,53 +1567,72 @@ class DASH:
|
||||
elif urn == ClearKeyCENC.urn:
|
||||
# W3C EME ClearKey (org.w3.clearkey): match the scheme UUID alone,
|
||||
# value="ClearKey1.0" is spec'd (DASH-IF CCP) but not required in the wild
|
||||
kid_attr = protection.get("default_KID") or protection.get("{urn:mpeg:cenc:2013}default_KID")
|
||||
kid = None
|
||||
if kid_attr:
|
||||
try:
|
||||
kid = UUID(kid_attr)
|
||||
except ValueError:
|
||||
try:
|
||||
kid = UUID(bytes=base64.b64decode(kid_attr))
|
||||
except ValueError:
|
||||
kid = None
|
||||
|
||||
if not kid:
|
||||
# DASH-IF puts default_KID on the sibling mp4protection element
|
||||
kid = next(
|
||||
(
|
||||
UUID(p.get("default_KID") or p.get("{urn:mpeg:cenc:2013}default_KID"))
|
||||
for p in protections
|
||||
if p.get("default_KID") or p.get("{urn:mpeg:cenc:2013}default_KID")
|
||||
),
|
||||
None,
|
||||
)
|
||||
|
||||
if not kid or kid in PLACEHOLDER_KIDS:
|
||||
continue
|
||||
|
||||
# license URL appears under several namespaces/casings in the wild
|
||||
laurl = next(
|
||||
(
|
||||
text.strip()
|
||||
for name in (
|
||||
"{https://dashif.org/CPS}Laurl",
|
||||
"{https://dashif.org/CPS}laurl",
|
||||
"{http://dashif.org/guidelines/clearKey}Laurl",
|
||||
"{http://dashif.org/guidelines/clearKey}laurl",
|
||||
"Laurl",
|
||||
"laurl",
|
||||
)
|
||||
for text in [protection.findtext(name)]
|
||||
if text and text.strip()
|
||||
),
|
||||
None,
|
||||
)
|
||||
|
||||
drm.append(ClearKeyCENC(kids=[kid], laurl=laurl))
|
||||
clearkey = DASH.get_clearkey(protection, protections)
|
||||
if clearkey:
|
||||
drm.append(clearkey)
|
||||
|
||||
return drm
|
||||
|
||||
@staticmethod
|
||||
def default_kid(protection: Element) -> UUID | None:
|
||||
"""The default_KID of a ContentProtection, given as a UUID or base64, or None when it has none or it is no KID."""
|
||||
kid_attr = protection.get("default_KID") or protection.get("{urn:mpeg:cenc:2013}default_KID")
|
||||
if not kid_attr:
|
||||
return None
|
||||
try:
|
||||
return UUID(kid_attr)
|
||||
except ValueError:
|
||||
try:
|
||||
return UUID(bytes=base64.b64decode(kid_attr))
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def get_common_clearkey(protections: list[Element]) -> ClearKeyCENC | None:
|
||||
"""The ClearKeyCENC of the first W3C common PSSH ContentProtection that names a real KID, or None.
|
||||
|
||||
A common PSSH names KIDs for any DRM system, so the caller asks for it only after the manifest and the init
|
||||
segment gave no other DRM system.
|
||||
"""
|
||||
common = (p for p in protections if (p.get("schemeIdUri") or "").lower() == f"urn:uuid:{COMMON_ID}")
|
||||
return next(filter(None, (DASH.get_clearkey(p, protections) for p in common)), None)
|
||||
|
||||
@staticmethod
|
||||
def get_clearkey(protection: Element, protections: list[Element]) -> ClearKeyCENC | None:
|
||||
"""The ClearKeyCENC of a ClearKey or W3C common PSSH ContentProtection, or None when it names no real KID.
|
||||
|
||||
The KID is the default_KID of the element, else of a sibling (DASH-IF puts it on mp4protection), else each KID
|
||||
of its common PSSH box.
|
||||
"""
|
||||
kid = next(filter(None, map(DASH.default_kid, [protection, *protections])), None)
|
||||
kids = (
|
||||
[kid]
|
||||
if kid and kid not in PLACEHOLDER_KIDS
|
||||
else common_pssh_kids(protection.findtext("{urn:mpeg:cenc:2013}pssh") or protection.findtext("pssh"))
|
||||
)
|
||||
if not kids:
|
||||
return None
|
||||
|
||||
# license URL appears under several namespaces/casings in the wild
|
||||
laurl = next(
|
||||
(
|
||||
text.strip()
|
||||
for name in (
|
||||
"{https://dashif.org/CPS}Laurl",
|
||||
"{https://dashif.org/CPS}laurl",
|
||||
"{http://dashif.org/guidelines/clearKey}Laurl",
|
||||
"{http://dashif.org/guidelines/clearKey}laurl",
|
||||
"Laurl",
|
||||
"laurl",
|
||||
)
|
||||
for text in [protection.findtext(name)]
|
||||
if text and text.strip()
|
||||
),
|
||||
None,
|
||||
)
|
||||
|
||||
return ClearKeyCENC(kids=kids, laurl=laurl)
|
||||
|
||||
@staticmethod
|
||||
def pt_to_sec(d: str | float) -> float:
|
||||
if isinstance(d, float):
|
||||
|
||||
Reference in New Issue
Block a user