forked from kenzuya/unshackle
refactor(manifests): pick the licensable DRM and KID in one licence step
This commit is contained in:
@@ -41,8 +41,8 @@ def playready_object(kid: UUID) -> str:
|
||||
return base64.b64encode(pro).decode()
|
||||
|
||||
|
||||
def widevine() -> Widevine:
|
||||
return Widevine(pssh=PSSH.new(system_id=PSSH.SystemId.Widevine, key_ids=[KID]))
|
||||
def widevine(kid: UUID = KID) -> Widevine:
|
||||
return Widevine(pssh=PSSH.new(system_id=PSSH.SystemId.Widevine, key_ids=[kid]))
|
||||
|
||||
|
||||
def playready(kid: UUID = KID) -> PlayReady:
|
||||
|
||||
@@ -10,9 +10,10 @@ from pathlib import Path
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
from pywidevine.pssh import PSSH
|
||||
|
||||
from unshackle.core.constants import DOWNLOAD_LICENCE_ONLY
|
||||
from unshackle.core.constants import DOWNLOAD_CANCELLED, DOWNLOAD_LICENCE_ONLY
|
||||
from unshackle.core.manifests.hls import HLS
|
||||
from unshackle.core.remote_service import segment_uri_filter
|
||||
from unshackle.core.tracks import Video
|
||||
@@ -76,6 +77,44 @@ def test_licence_only_licenses_a_key_that_starts_part_way(tmp_path: Path, monkey
|
||||
assert track.data["hls"]["unwanted_segments"] == []
|
||||
|
||||
|
||||
def test_a_refused_later_key_fails_through_the_licence_step(tmp_path: Path, monkeypatch: Any) -> None:
|
||||
"""The rotation loop licenses each later key through the licence step, so a refusal shows FAILED and cancels."""
|
||||
playlist = tmp_path / "media.m3u8"
|
||||
playlist.write_text(PLAYLIST, encoding="utf-8")
|
||||
track = Video(
|
||||
url="https://example.test/media.m3u8",
|
||||
language="en",
|
||||
descriptor=Track.Descriptor.HLS,
|
||||
codec=Video.Codec.HEVC,
|
||||
)
|
||||
track.from_file = playlist
|
||||
monkeypatch.setattr(HLS, "get_track_kid_from_init", staticmethod(lambda *args, **kwargs: None))
|
||||
|
||||
def licence(drm: Any, track_kid: Any = None) -> None:
|
||||
if track_kid == LATER_KID:
|
||||
raise PermissionError("EXAMPLE licence refused")
|
||||
|
||||
statuses: list[str] = []
|
||||
ctx = DownloadContext(
|
||||
save_path=tmp_path / "video.mp4",
|
||||
save_dir=tmp_path / "video_segments",
|
||||
progress=lambda **kw: statuses.append(kw["downloaded"]) if "downloaded" in kw else None,
|
||||
license_widevine=licence,
|
||||
cdm=None,
|
||||
)
|
||||
|
||||
DOWNLOAD_LICENCE_ONLY.set()
|
||||
try:
|
||||
with pytest.raises(PermissionError, match="EXAMPLE licence refused"):
|
||||
HLS.download_track(track=track, ctx=ctx)
|
||||
assert DOWNLOAD_CANCELLED.is_set()
|
||||
finally:
|
||||
DOWNLOAD_LICENCE_ONLY.clear()
|
||||
DOWNLOAD_CANCELLED.clear()
|
||||
|
||||
assert statuses == ["LICENSING", "[yellow]LICENSED", "LICENSING", "[red]FAILED"]
|
||||
|
||||
|
||||
def test_licence_only_skips_the_key_of_a_filtered_segment(tmp_path: Path, monkeypatch: Any) -> None:
|
||||
"""The filtered segment URIs go to the track data, and an import filter built from them drops the same segment."""
|
||||
playlist = tmp_path / "media.m3u8"
|
||||
|
||||
@@ -18,20 +18,23 @@ from pathlib import Path
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
import requests
|
||||
from Cryptodome.Cipher import AES
|
||||
from Cryptodome.Util.Padding import pad
|
||||
from pywidevine.pssh import PSSH
|
||||
|
||||
from tests.core.test_hls_kid_from_init import KEY_LINE, no_kid_pssh
|
||||
from tests.core.test_ism_init import piff_fragment
|
||||
from unshackle.core import constants
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.constants import DOWNLOAD_CANCELLED
|
||||
from unshackle.core.drm import MonaLisa, Widevine
|
||||
from unshackle.core.drm import ClearKeyCENC, MonaLisa, Widevine
|
||||
from unshackle.core.manifests import DASH, HLS, ISM
|
||||
from unshackle.core.manifests import dash as dash_module
|
||||
from unshackle.core.manifests import hls as hls_module
|
||||
from unshackle.core.manifests import segment_pipeline
|
||||
from unshackle.core.manifests.ism_init import PIFF_SENC_UUID
|
||||
from unshackle.core.tracks import Video
|
||||
from unshackle.core.tracks import track as track_module
|
||||
from unshackle.core.tracks.track import DownloadContext
|
||||
|
||||
@@ -401,3 +404,43 @@ def test_hls_rekey_takes_the_kid_from_the_init_segment_of_the_new_key(server, tm
|
||||
HLS.download_track(track, ctx)
|
||||
|
||||
assert licensed == [KID, later_kid]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("system", ["Widevine", "ClearKeyCENC"])
|
||||
@pytest.mark.parametrize("kind", ["dash", "hls", "ism", "url"])
|
||||
def test_each_manifest_kind_licenses_through_the_licence_step(server, tmp_path, monkeypatch, kind, system):
|
||||
"""A licence-only run of every download path: the step picks the systems and the KID for each kind.
|
||||
|
||||
The fake inits name no KID, so DASH and URL pass none, and HLS and ISM take the KID their DRM names.
|
||||
"""
|
||||
drm = widevine() if system == "Widevine" else ClearKeyCENC(kids=[KID])
|
||||
licensed: list[tuple[object, object]] = []
|
||||
|
||||
def licence(drm, track_kid=None):
|
||||
licensed.append((drm, track_kid))
|
||||
|
||||
if kind == "url":
|
||||
track = Video(url=f"{base(server)}init.mp4", language="en", descriptor=Video.Descriptor.URL, drm=[drm])
|
||||
else:
|
||||
track = {"dash": dash_track, "hls": lambda srv: hls_track(srv, False), "ism": ism_track}[kind](server)
|
||||
track.drm = [drm]
|
||||
ctx = make_ctx(tmp_path, f"{kind}.mp4")
|
||||
ctx.license_widevine = licence
|
||||
monkeypatch.setattr(constants.DOWNLOAD_LICENCE_ONLY, "is_set", lambda: True)
|
||||
|
||||
if kind == "url":
|
||||
track.download(requests.Session(), licence, progress=ctx.progress)
|
||||
else:
|
||||
{"dash": DASH, "hls": HLS, "ism": ISM}[kind].download_track(track, ctx)
|
||||
|
||||
expected = {
|
||||
("dash", "Widevine"): [(drm, None)],
|
||||
("dash", "ClearKeyCENC"): [(drm, None)],
|
||||
("hls", "Widevine"): [(drm, KID)],
|
||||
("hls", "ClearKeyCENC"): [],
|
||||
("ism", "Widevine"): [(drm, KID)],
|
||||
("ism", "ClearKeyCENC"): [],
|
||||
("url", "Widevine"): [(drm, None)],
|
||||
("url", "ClearKeyCENC"): [(drm, None)],
|
||||
}
|
||||
assert licensed == expected[(kind, system)]
|
||||
|
||||
@@ -11,8 +11,9 @@ from uuid import UUID
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.core.test_all_drm import playready, widevine
|
||||
from unshackle.core.constants import DOWNLOAD_CANCELLED
|
||||
from unshackle.core.drm import verify
|
||||
from unshackle.core.drm import ClearKey, ClearKeyCENC, MonaLisa, verify
|
||||
from unshackle.core.events import events
|
||||
from unshackle.core.manifests.segment_pipeline import (
|
||||
RollingMerge,
|
||||
@@ -20,9 +21,9 @@ from unshackle.core.manifests.segment_pipeline import (
|
||||
complete_track,
|
||||
fetch_segments,
|
||||
license_drm,
|
||||
licensing,
|
||||
)
|
||||
from unshackle.core.tracks.track import DownloadContext
|
||||
from unshackle.core.remote_protocol import APIErrorCode
|
||||
from unshackle.core.tracks.track import DownloadContext, Track
|
||||
|
||||
INIT = b"init" * 8
|
||||
DECRYPTED_INIT = b"clear-init" * 8
|
||||
@@ -197,17 +198,91 @@ def test_a_short_rolling_merge_raises_and_removes_the_output(tmp_path):
|
||||
check_merged(merger, 0, SimpleNamespace(id="EXAMPLE"), ctx, "EXAMPLE") # type: ignore[arg-type]
|
||||
|
||||
|
||||
def test_license_drm_wraps_the_callback_in_licensing_progress(tmp_path):
|
||||
received: list[tuple[Any, dict[str, Any]]] = []
|
||||
ctx = make_ctx(tmp_path, license_widevine=lambda drm, **kw: received.append((drm, kw)))
|
||||
drm = object()
|
||||
WV, PR, CKC, ML, AES = "Widevine", "PlayReady", "ClearKeyCENC", "MonaLisa", "ClearKey"
|
||||
PSSH_KID = UUID(hex="22222222222222222222222222222222")
|
||||
PROBED_KID = UUID(hex="33333333333333333333333333333333")
|
||||
URL, DASH_, HLS_, ISM_ = (Track.Descriptor.URL, Track.Descriptor.DASH, Track.Descriptor.HLS, Track.Descriptor.ISM)
|
||||
|
||||
with licensing(ctx):
|
||||
license_drm(drm, ctx, track_kid=KID) # type: ignore[arg-type]
|
||||
|
||||
assert received == [(drm, {"track_kid": KID})]
|
||||
assert ctx.statuses == ["LICENSING", "[yellow]LICENSED"]
|
||||
assert not DOWNLOAD_CANCELLED.is_set()
|
||||
def make_drm(system: str, monkeypatch: pytest.MonkeyPatch) -> Any:
|
||||
"""One DRM of each system; every one that names a KID names PSSH_KID."""
|
||||
if system == WV:
|
||||
return widevine(PSSH_KID)
|
||||
if system == PR:
|
||||
return playready(PSSH_KID)
|
||||
if system == CKC:
|
||||
return ClearKeyCENC(kids=[PSSH_KID])
|
||||
if system == ML:
|
||||
monkeypatch.setattr(MonaLisa, "extract_keys", lambda self: None)
|
||||
return MonaLisa("EXAMPLE", Path("unused.mld"))
|
||||
return ClearKey(key=bytes(16))
|
||||
|
||||
|
||||
def licence_calls(tmp_path: Path) -> tuple[DownloadContext, list[tuple[Any, Any]]]:
|
||||
calls: list[tuple[Any, Any]] = []
|
||||
return make_ctx(tmp_path, license_widevine=lambda drm, track_kid: calls.append((drm, track_kid))), calls
|
||||
|
||||
|
||||
LICENSED = {URL: {WV, PR, CKC}, DASH_: {WV, PR, CKC}, HLS_: {WV, PR, ML}, ISM_: {WV, PR}}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("descriptor", list(LICENSED), ids=lambda d: d.name)
|
||||
@pytest.mark.parametrize("system", [WV, PR, CKC, ML, AES])
|
||||
def test_the_licence_step_licenses_the_systems_each_manifest_kind_licensed(tmp_path, monkeypatch, descriptor, system):
|
||||
ctx, calls = licence_calls(tmp_path)
|
||||
drm = make_drm(system, monkeypatch)
|
||||
|
||||
license_drm(SimpleNamespace(descriptor=descriptor, id="EXAMPLE"), drm, ctx) # type: ignore[arg-type]
|
||||
|
||||
licensed = system in LICENSED[descriptor]
|
||||
assert [c[0] for c in calls] == ([drm] if licensed else [])
|
||||
assert ctx.statuses == (["LICENSING", "[yellow]LICENSED"] if licensed else []) # type: ignore[attr-defined]
|
||||
|
||||
|
||||
def test_the_licence_step_skips_a_track_with_no_drm(tmp_path):
|
||||
ctx, calls = licence_calls(tmp_path)
|
||||
license_drm(SimpleNamespace(descriptor=DASH_, id="EXAMPLE"), None, ctx) # type: ignore[arg-type]
|
||||
assert calls == [] and ctx.statuses == [] # type: ignore[attr-defined]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"descriptor, system, kid, probed, expected",
|
||||
[
|
||||
(DASH_, WV, KID, None, KID),
|
||||
(DASH_, WV, None, PROBED_KID, PROBED_KID),
|
||||
(DASH_, WV, None, None, None),
|
||||
(URL, CKC, None, "no probe", None),
|
||||
(HLS_, WV, KID, "no probe", KID),
|
||||
(HLS_, PR, None, "no probe", PSSH_KID),
|
||||
(HLS_, WV, None, None, PSSH_KID),
|
||||
(HLS_, ML, KID, PROBED_KID, None),
|
||||
(ISM_, PR, None, "no probe", PSSH_KID),
|
||||
],
|
||||
ids=[
|
||||
"dash-init-kid",
|
||||
"dash-probed",
|
||||
"dash-never-the-pssh-kid",
|
||||
"url-never-the-drm-kid",
|
||||
"hls-init-kid",
|
||||
"hls-drm-kid",
|
||||
"hls-probe-misses",
|
||||
"monalisa-no-kid",
|
||||
"ism-drm-kid",
|
||||
],
|
||||
)
|
||||
def test_the_licence_step_finds_the_track_kid(tmp_path, monkeypatch, descriptor, system, kid, probed, expected):
|
||||
ctx, calls = licence_calls(tmp_path)
|
||||
probes: list[bool] = []
|
||||
|
||||
def probe() -> Any:
|
||||
probes.append(True)
|
||||
return probed
|
||||
|
||||
track = SimpleNamespace(descriptor=descriptor, id="EXAMPLE")
|
||||
license_drm(track, make_drm(system, monkeypatch), ctx, kid, None if probed == "no probe" else probe) # type: ignore[arg-type]
|
||||
|
||||
assert [c[1] for c in calls] == [expected]
|
||||
assert not (kid and probes), "a KID the caller holds needs no probe"
|
||||
|
||||
|
||||
def test_a_licence_failure_cancels_the_other_tracks_and_shows_failed(tmp_path):
|
||||
@@ -216,18 +291,29 @@ def test_a_licence_failure_cancels_the_other_tracks_and_shows_failed(tmp_path):
|
||||
|
||||
ctx = make_ctx(tmp_path, license_widevine=refuse)
|
||||
with pytest.raises(PermissionError, match="EXAMPLE licence refused"):
|
||||
with licensing(ctx):
|
||||
license_drm(object(), ctx) # type: ignore[arg-type]
|
||||
license_drm(SimpleNamespace(descriptor=DASH_, id="EXAMPLE"), widevine(KID), ctx) # type: ignore[arg-type]
|
||||
|
||||
assert ctx.statuses == ["LICENSING", "[red]FAILED"]
|
||||
assert ctx.statuses == ["LICENSING", "[red]FAILED"] # type: ignore[attr-defined]
|
||||
assert DOWNLOAD_CANCELLED.is_set()
|
||||
|
||||
|
||||
def test_license_drm_without_a_callback_raises(tmp_path):
|
||||
def test_a_failed_kid_probe_fails_only_this_track(tmp_path):
|
||||
def probe() -> Any:
|
||||
raise ConnectionError("EXAMPLE init unreachable")
|
||||
|
||||
ctx, calls = licence_calls(tmp_path)
|
||||
with pytest.raises(ConnectionError):
|
||||
license_drm(SimpleNamespace(descriptor=DASH_, id="EXAMPLE"), widevine(KID), ctx, probe=probe) # type: ignore[arg-type]
|
||||
assert calls == [] and ctx.statuses == [] # type: ignore[attr-defined]
|
||||
assert not DOWNLOAD_CANCELLED.is_set()
|
||||
|
||||
|
||||
def test_the_licence_step_without_a_callback_raises_a_drm_error(tmp_path):
|
||||
ctx = make_ctx(tmp_path)
|
||||
with pytest.raises(ValueError, match="license_widevine func must be supplied"):
|
||||
license_drm(object(), ctx) # type: ignore[arg-type]
|
||||
assert ctx.statuses == []
|
||||
with pytest.raises(ValueError, match="No licence callback for the Widevine DRM of track EXAMPLE") as raised:
|
||||
license_drm(SimpleNamespace(descriptor=DASH_, id="EXAMPLE"), widevine(KID), ctx) # type: ignore[arg-type]
|
||||
assert raised.value.error_code == APIErrorCode.DRM_ERROR # type: ignore[attr-defined]
|
||||
assert ctx.statuses == ["[red]FAILED"] # type: ignore[attr-defined]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
Reference in New Issue
Block a user