forked from kenzuya/unshackle
refactor(manifests): pick the licensable DRM and KID in one licence step
This commit is contained in:
@@ -38,7 +38,6 @@ from unshackle.core.manifests.segment_pipeline import (
|
||||
complete_track,
|
||||
fetch_segments,
|
||||
license_drm,
|
||||
licensing,
|
||||
)
|
||||
from unshackle.core.manifests.trim import trim_overlaps
|
||||
from unshackle.core.session import RnetSession
|
||||
@@ -641,15 +640,8 @@ class DASH:
|
||||
pssh_init = init_data
|
||||
track.drm_from_pssh(cdm, lambda system: system.from_init_data(pssh_init), log)
|
||||
|
||||
if track.drm:
|
||||
track_kid = track_kid or track.get_key_id(url=segments[0][0], session=session)
|
||||
drm = track.get_drm_for_cdm(cdm)
|
||||
if isinstance(drm, (Widevine, PlayReady, ClearKeyCENC)):
|
||||
# license and grab content keys (ClearKeyCENC uses no CDM)
|
||||
with licensing(ctx):
|
||||
license_drm(drm, ctx, track_kid=track_kid)
|
||||
else:
|
||||
drm = None
|
||||
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))
|
||||
|
||||
if DOWNLOAD_LICENCE_ONLY.is_set():
|
||||
progress(downloaded="[yellow]SKIPPED")
|
||||
|
||||
@@ -32,7 +32,7 @@ from unshackle.core import binaries
|
||||
from unshackle.core.cdm.detect import cdm_type_stub, is_playready_cdm, is_widevine_cdm
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.constants import DOWNLOAD_ALL_DRM, DOWNLOAD_LICENCE_ONLY, AnyTrack
|
||||
from unshackle.core.drm import DRM_T, ClearKey, ClearKeyCENC, MonaLisa, PlayReady, Widevine
|
||||
from unshackle.core.drm import DRM_T, ClearKey, ClearKeyCENC, PlayReady, Widevine
|
||||
from unshackle.core.drm.segment_decrypt import SegmentDecrypter, can_use
|
||||
from unshackle.core.drm.verify import decrypt_track
|
||||
from unshackle.core.events import events
|
||||
@@ -709,7 +709,6 @@ class HLS:
|
||||
)
|
||||
media_drm = HLS.get_drm(media_playlist_key, session, **({"kid": init_kid} if init_kid else {}))
|
||||
if isinstance(media_drm, (Widevine, PlayReady)):
|
||||
track_kid = init_kid or media_drm.kid
|
||||
# Preserve pre-existing keys (e.g. from server_cdm)
|
||||
if track.drm:
|
||||
for existing_drm in track.drm:
|
||||
@@ -718,8 +717,7 @@ class HLS:
|
||||
track.drm = [media_drm]
|
||||
if DOWNLOAD_ALL_DRM.is_set():
|
||||
track.drm += HLS.alternate_drm(media_keys, media_drm, session, init_kid)
|
||||
with licensing(ctx):
|
||||
license_drm(media_drm, ctx, track_kid=track_kid)
|
||||
license_drm(track, media_drm, ctx, init_kid)
|
||||
initial_drm_licensed = True
|
||||
initial_drm_key = media_playlist_key
|
||||
session_drm = media_drm
|
||||
@@ -730,14 +728,8 @@ class HLS:
|
||||
initial_drm_key = media_playlist_key
|
||||
initial_drm_licensed = True
|
||||
|
||||
if not initial_drm_licensed and session_drm and isinstance(session_drm, (Widevine, PlayReady)):
|
||||
with licensing(ctx):
|
||||
track_kid = HLS.get_track_kid_from_init(master, track, session) or session_drm.kid
|
||||
license_drm(session_drm, ctx, track_kid=track_kid)
|
||||
|
||||
if not initial_drm_licensed and session_drm and isinstance(session_drm, MonaLisa):
|
||||
with licensing(ctx):
|
||||
license_drm(session_drm, ctx)
|
||||
if not initial_drm_licensed:
|
||||
license_drm(track, session_drm, ctx, probe=lambda: HLS.get_track_kid_from_init(master, track, session))
|
||||
|
||||
if DOWNLOAD_LICENCE_ONLY.is_set():
|
||||
# a playlist can change its key part-way, and only the download loop licenses the later keys
|
||||
@@ -750,8 +742,7 @@ class HLS:
|
||||
continue
|
||||
licensed_keys.append(key)
|
||||
later_drm = HLS.get_drm_or_probe(key, master, track, session, segment.init_section)
|
||||
if isinstance(later_drm, (Widevine, PlayReady)) and license_widevine:
|
||||
license_widevine(later_drm, track_kid=later_drm.kid)
|
||||
license_drm(track, later_drm, ctx)
|
||||
progress(downloaded="[yellow]SKIPPED")
|
||||
return
|
||||
|
||||
@@ -1080,8 +1071,7 @@ class HLS:
|
||||
with licensing(ctx):
|
||||
init_kid = track.get_key_id(map_data[1]) if map_data and key.method != "AES-128" else None
|
||||
drm = HLS.get_drm(key, session, **({"kid": init_kid} if init_kid else {}))
|
||||
if isinstance(drm, (Widevine, PlayReady)):
|
||||
license_drm(drm, ctx, track_kid=init_kid or drm.kid)
|
||||
license_drm(track, drm, ctx, init_kid)
|
||||
if (
|
||||
encryption_data
|
||||
and isinstance(drm, (Widevine, PlayReady))
|
||||
|
||||
@@ -35,7 +35,6 @@ from unshackle.core.manifests.segment_pipeline import (
|
||||
complete_track,
|
||||
fetch_segments,
|
||||
license_drm,
|
||||
licensing,
|
||||
)
|
||||
from unshackle.core.session import RnetSession
|
||||
from unshackle.core.tracks import Audio, DownloadContext, Subtitle, Track, Tracks, Video, resume
|
||||
@@ -419,14 +418,8 @@ class ISM:
|
||||
segments = segments[kept]
|
||||
track.data["ism"]["fragment_times"] = segment_times[kept]
|
||||
|
||||
session_drm = None
|
||||
if track.drm:
|
||||
# Mirror HLS.download_track: pick the DRM matching the provided CDM
|
||||
# (or the first available) and license it if supported.
|
||||
session_drm = track.get_drm_for_cdm(ctx.cdm)
|
||||
if isinstance(session_drm, (Widevine, PlayReady)):
|
||||
with licensing(ctx):
|
||||
license_drm(session_drm, ctx)
|
||||
session_drm = track.get_drm_for_cdm(ctx.cdm)
|
||||
license_drm(track, session_drm, ctx)
|
||||
|
||||
if DOWNLOAD_LICENCE_ONLY.is_set():
|
||||
progress(downloaded="[yellow]SKIPPED")
|
||||
|
||||
@@ -12,14 +12,16 @@ from concurrent.futures import Future
|
||||
from contextlib import ExitStack, closing, contextmanager
|
||||
from pathlib import Path
|
||||
from typing import Any, BinaryIO, Callable, Iterator, Optional
|
||||
from uuid import UUID
|
||||
|
||||
from unshackle.core.constants import DOWNLOAD_CANCELLED, AnyTrack
|
||||
from unshackle.core.drm import DRM_T
|
||||
from unshackle.core.drm import DRM_T, ClearKeyCENC, MonaLisa, PlayReady, Widevine
|
||||
from unshackle.core.drm.segment_decrypt import SegmentDecrypter
|
||||
from unshackle.core.drm.verify import decrypt_track
|
||||
from unshackle.core.events import events
|
||||
from unshackle.core.remote_protocol import APIErrorCode, coded
|
||||
from unshackle.core.session import AnySession
|
||||
from unshackle.core.tracks import DownloadContext, resume
|
||||
from unshackle.core.tracks import DownloadContext, Track, resume
|
||||
from unshackle.core.tracks.track import assert_fragments_decrypted
|
||||
from unshackle.core.utilities import log_event, try_ensure_utf8
|
||||
|
||||
@@ -92,13 +94,46 @@ def licensing(ctx: DownloadContext) -> Iterator[None]:
|
||||
raise
|
||||
|
||||
|
||||
def license_drm(drm: DRM_T, ctx: DownloadContext, **kwargs: Any) -> None:
|
||||
"""Call ctx.license_widevine for one DRM, with the LICENSING and LICENSED progress around it."""
|
||||
if not ctx.license_widevine:
|
||||
raise ValueError("license_widevine func must be supplied to use DRM")
|
||||
ctx.progress(downloaded="LICENSING")
|
||||
ctx.license_widevine(drm, **kwargs)
|
||||
ctx.progress(downloaded="[yellow]LICENSED")
|
||||
LICENSABLE: dict[Track.Descriptor, tuple[type, ...]] = {
|
||||
Track.Descriptor.URL: (Widevine, PlayReady, ClearKeyCENC),
|
||||
Track.Descriptor.DASH: (Widevine, PlayReady, ClearKeyCENC),
|
||||
Track.Descriptor.HLS: (Widevine, PlayReady, MonaLisa),
|
||||
Track.Descriptor.ISM: (Widevine, PlayReady),
|
||||
}
|
||||
"""The DRM systems the licence step licenses, by the kind of manifest the track comes from."""
|
||||
|
||||
|
||||
def license_drm(
|
||||
track: AnyTrack,
|
||||
drm: Optional[DRM_T],
|
||||
ctx: DownloadContext,
|
||||
kid: Optional[UUID] = None,
|
||||
probe: Optional[Callable[[], Optional[UUID]]] = None,
|
||||
) -> None:
|
||||
"""The licence step: get the content keys of `drm` for `track` through ctx.license_widevine, with progress.
|
||||
|
||||
A DRM system that LICENSABLE does not list for the track gets no licence. The track KID is `kid`, else
|
||||
what `probe` reads from the init. An HLS or ISM DRM comes from the track's own key tag or protection
|
||||
header, so its first KID stands in when the init names none. A MonaLisa DRM gets no KID: its ticket gives
|
||||
the track's own key. A failed `probe` fails only this track; a failed licence cancels the run.
|
||||
ctx.license_widevine licenses every DRM system; dl and services pass it by keyword.
|
||||
"""
|
||||
if not isinstance(drm, LICENSABLE[track.descriptor]):
|
||||
return
|
||||
track_kid = None
|
||||
if not isinstance(drm, MonaLisa):
|
||||
track_kid = kid or (probe() if probe else None)
|
||||
if not track_kid and track.descriptor in (Track.Descriptor.HLS, Track.Descriptor.ISM):
|
||||
track_kid = getattr(drm, "kid", None)
|
||||
with licensing(ctx):
|
||||
if not ctx.license_widevine:
|
||||
raise coded(
|
||||
ValueError(f"No licence callback for the {type(drm).__name__} DRM of track {track.id}"),
|
||||
APIErrorCode.DRM_ERROR,
|
||||
)
|
||||
ctx.progress(downloaded="LICENSING")
|
||||
ctx.license_widevine(drm, track_kid=track_kid)
|
||||
ctx.progress(downloaded="[yellow]LICENSED")
|
||||
|
||||
|
||||
def fetch_segments(
|
||||
|
||||
@@ -27,7 +27,7 @@ from unshackle.core.cdm.detect import is_playready_cdm, is_widevine_cdm
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.constants import DOWNLOAD_ALL_DRM, DOWNLOAD_CANCELLED, DOWNLOAD_LICENCE_ONLY, DownloadCancelled
|
||||
from unshackle.core.downloaders.requests import requests
|
||||
from unshackle.core.drm import DRM_T, BlowfishStripe, ClearKeyCENC, PlayReady, Widevine
|
||||
from unshackle.core.drm import DRM_T, BlowfishStripe, PlayReady, Widevine
|
||||
from unshackle.core.drm.verify import (
|
||||
children,
|
||||
decrypt_track,
|
||||
@@ -723,6 +723,7 @@ class Track:
|
||||
track's init data and stores it on the track, so a service need not declare `drm` itself.
|
||||
"""
|
||||
from unshackle.core.manifests import DASH, HLS, ISM
|
||||
from unshackle.core.manifests.segment_pipeline import license_drm
|
||||
from unshackle.core.tracks.audio import Audio
|
||||
from unshackle.core.tracks.subtitle import Subtitle
|
||||
from unshackle.core.tracks.video import Video
|
||||
@@ -803,19 +804,19 @@ class Track:
|
||||
self.Descriptor.DASH: DASH,
|
||||
self.Descriptor.ISM: ISM,
|
||||
}
|
||||
ctx = DownloadContext(
|
||||
save_path=save_path,
|
||||
save_dir=save_dir,
|
||||
progress=progress,
|
||||
session=dl_session,
|
||||
proxy=proxy,
|
||||
max_workers=max_workers,
|
||||
adaptive_workers=adaptive_workers,
|
||||
download_processes=download_processes,
|
||||
license_widevine=prepare_drm,
|
||||
cdm=cdm,
|
||||
)
|
||||
if self.descriptor in manifest_parsers:
|
||||
ctx = DownloadContext(
|
||||
save_path=save_path,
|
||||
save_dir=save_dir,
|
||||
progress=progress,
|
||||
session=dl_session,
|
||||
proxy=proxy,
|
||||
max_workers=max_workers,
|
||||
adaptive_workers=adaptive_workers,
|
||||
download_processes=download_processes,
|
||||
license_widevine=prepare_drm,
|
||||
cdm=cdm,
|
||||
)
|
||||
manifest_parsers[self.descriptor].download_track(track=self, ctx=ctx)
|
||||
elif self.descriptor == self.Descriptor.URL:
|
||||
try:
|
||||
@@ -831,13 +832,7 @@ class Track:
|
||||
if self.drm:
|
||||
drm = self.get_drm_for_cdm(cdm)
|
||||
track_kid = None if isinstance(drm, BlowfishStripe) else self.get_key_id(session=session)
|
||||
if isinstance(drm, (Widevine, PlayReady, ClearKeyCENC)):
|
||||
if not prepare_drm:
|
||||
system = type(drm).__name__.removesuffix("CENC")
|
||||
raise ValueError(f"prepare_drm func must be supplied to use {system} DRM")
|
||||
progress(downloaded="LICENSING")
|
||||
prepare_drm(drm, track_kid=track_kid)
|
||||
progress(downloaded="[yellow]LICENSED")
|
||||
license_drm(self, drm, ctx, track_kid)
|
||||
else:
|
||||
drm = None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user