mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-02 05:55:08 +00:00
feat(drm): bridge a FairPlay-only HLS track only for a service with get_fairplay_bridge_license
This commit is contained in:
@@ -451,11 +451,48 @@ needs:
|
||||
| `get_widevine_license(title, track, challenge)` | license bytes | POST the Widevine challenge to the license server, return the response. |
|
||||
| `get_playready_license(title, track, challenge)` | license | Same for PlayReady; defaults to delegating to Widevine. |
|
||||
| `get_clearkey_license(title, track, challenge)` | dict / str / bytes / `None` | For `ClearKeyCENC`; return `None` to let the framework POST to the manifest `Laurl`. |
|
||||
| `get_fairplay_bridge_license(title, track, challenge, drm_system)` | license | Optional, not on the base class. Licenses a FairPlay-only HLS track; see [The FairPlay bridge hook](#the-fairplay-bridge-hook). |
|
||||
|
||||
You do not open CDM sessions or operate shaka-packager yourself. The `Widevine`/`PlayReady`
|
||||
classes drive the CDM handshake and call the decryption backend. Your hook is only the
|
||||
"send this challenge, give me back the license" step.
|
||||
|
||||
### The FairPlay bridge hook
|
||||
|
||||
unshackle has no FairPlay CDM and never sends a FairPlay challenge. For a FairPlay-only track it can send a PlayReady or Widevine challenge for the KIDs of the init segments to the licence server of the platform, which works only when that server issues the same content key to PlayReady or Widevine. The bridge runs only for a service that defines
|
||||
`get_fairplay_bridge_license`. The base class does not define it.
|
||||
|
||||
```python
|
||||
def get_fairplay_bridge_license(
|
||||
self, *, challenge: bytes, title: Title_T, track: AnyTrack, drm_system: str
|
||||
) -> bytes | str:
|
||||
"""The licence server of this platform issues the content keys of its FairPlay-only tracks to PlayReady."""
|
||||
licence = self.get_playready_license if drm_system == "playready" else self.get_widevine_license
|
||||
return licence(challenge=challenge, title=title, track=track)
|
||||
```
|
||||
|
||||
- `drm_system` is `"playready"` or `"widevine"`. The hook must accept it.
|
||||
- The hook can also declare the arguments of the licence callback of that system: `pssh` for Widevine,
|
||||
`pssh_b64` for PlayReady, and `session_id`. It gets only the arguments that it declares.
|
||||
- The key IDs are on the DRM object of the track (`track.drm[0].kids`) when the hook runs. A service that
|
||||
needs its own request shape builds the request in the hook.
|
||||
- The challenge of every other track goes to `get_playready_license` or `get_widevine_license`, as before.
|
||||
|
||||
The DRM system of the challenge is the `drm_preference` of the track, else the system of the configured
|
||||
CDM. For a licence server that issues the content key to one system only, set `drm_preference` on the
|
||||
FairPlay-only tracks in `get_tracks()`:
|
||||
|
||||
```python
|
||||
for track in tracks.videos + tracks.audio:
|
||||
if not track.drm:
|
||||
track.drm_preference = "playready"
|
||||
```
|
||||
|
||||
Without the hook, the download of a FairPlay-only track stops with
|
||||
`None of the key systems are supported`, and no challenge goes to the licence server. A licence
|
||||
server that does not issue the content key refuses the challenge, and can flag the device. Test the route with a licence-only run
|
||||
(`--cdm-only --skip-dl`) before a service relies on it.
|
||||
|
||||
### Decrypting a cbcs file yourself
|
||||
|
||||
Some tracks cannot use the framework's decryption: a single cbcs file whose sample entries share
|
||||
@@ -479,26 +516,28 @@ that needs a process on the server also works for a remote client.
|
||||
|
||||
A media playlist can name only a key system that unshackle does not read, such as an `skd://` key
|
||||
over cbcs media, while the licence server of the platform also issues the same content key over
|
||||
PlayReady or Widevine. `HLS.download_track` licenses such a track with no service code when all of
|
||||
these conditions are true:
|
||||
PlayReady or Widevine. `HLS.download_track` licenses such a track when all of these conditions are true:
|
||||
|
||||
- The service has the [FairPlay bridge hook](#the-fairplay-bridge-hook).
|
||||
- The track has no DRM object.
|
||||
- Every `EXT-X-KEY` of its media playlist is `SAMPLE-AES` or `SAMPLE-AES-CTR` for a DRM system
|
||||
unshackle cannot license, or `METHOD=NONE`. A segment under `METHOD=NONE` must have an init segment
|
||||
that no encrypted segment uses: a decrypter that reads the encrypted init segment decrypts the clear
|
||||
samples too. A tag with `KEYFORMAT="identity"`, or with no `KEYFORMAT`, names no DRM system: each
|
||||
encrypted segment also needs a tag of a DRM system.
|
||||
- The configured CDM is a PlayReady or Widevine CDM.
|
||||
- The `drm_preference` of the track, or else the configured CDM, is PlayReady or Widevine.
|
||||
- The init segment (`EXT-X-MAP`) of the track names a key ID in its `tenc` box. When the `tenc` key ID is all
|
||||
zeros, the playlist must have one key URI, and each init segment under the key tag must give the same
|
||||
key ID: its `tenc` key ID, or else the one key ID that its `pssh` boxes name. A clear init segment,
|
||||
such as the one of a clear lead or tail, does not count. MPEG-TS media has no init segment.
|
||||
- The init segment does not change without an `EXT-X-DISCONTINUITY`.
|
||||
|
||||
The download reads the key ID from the init segment, builds a DRM object for the DRM system of the
|
||||
CDM with `drm_stub`, and sends the challenge to the licence callback of the service. Over `--remote`
|
||||
the server reads the same key ID at the licence step, so it accepts the DRM object of the client. A
|
||||
licence that gives no content key stops the track before it downloads a segment.
|
||||
The download reads the key ID from the init segment, builds a DRM object for that DRM system with
|
||||
`drm_stub`, and sends the challenge to the hook. Over `--remote` the tracks reply tells the client that
|
||||
the service on the server has the hook. The server reads the same key ID at the licence step, accepts
|
||||
the DRM object of the client and sends the challenge to the hook of its service. A server that cannot
|
||||
read the key ID refuses the challenge. A licence that gives no content key stops the track before it
|
||||
downloads a segment.
|
||||
|
||||
unshackle reads one key ID from each init segment. So it licenses a playlist that changes its content key
|
||||
correctly only when each content key has its own init segment with a real `tenc` key ID, after an
|
||||
|
||||
@@ -78,7 +78,14 @@ need a `.prd` device. You can hold both, and unshackle automatically selects the
|
||||
appropriate one for each track.
|
||||
|
||||
!!! note "HLS tracks with only a FairPlay key tag"
|
||||
unshackle has no FairPlay CDM. When each `EXT-X-KEY` of an fMP4 playlist is sample encryption
|
||||
unshackle has no FairPlay CDM and never sends a FairPlay challenge.
|
||||
For a FairPlay-only track it can send a PlayReady or Widevine challenge for the KIDs of the init
|
||||
segments to the licence server of the platform, which works only when that server issues the same
|
||||
content key to PlayReady or Widevine.
|
||||
Only a service that defines this route does it. For another service, the download of a FairPlay-only
|
||||
track stops with `None of the key systems are supported`.
|
||||
|
||||
When each `EXT-X-KEY` of an fMP4 playlist is sample encryption
|
||||
for a DRM system that unshackle cannot license, such as FairPlay, unshackle reads the key ID from
|
||||
the init segment and asks for the licence with your PlayReady or Widevine device. unshackle reads one
|
||||
key ID from each init segment. So it licenses a playlist that changes its content key correctly only
|
||||
@@ -579,8 +586,8 @@ unshackle dl --skip-dl --all-drm --export EXAMPLE 81234567
|
||||
system does not license, so the title fails unless you use `--best-available`.
|
||||
- An HLS playlist that changes its `EXT-X-KEY` entries part-way: only the first set
|
||||
licenses with the second system.
|
||||
- An HLS playlist with only FairPlay key tags: the DRM system of the configured CDM
|
||||
licenses it.
|
||||
- An HLS playlist with only FairPlay key tags, for a service that defines the FairPlay
|
||||
route: the DRM system of the track's `drm_preference`, else of the configured CDM, licenses it.
|
||||
- A remote session that the server CDM licenses, and `unshackle import`. unshackle
|
||||
shows a warning and ignores the flag.
|
||||
- A remote CDM that stores content keys can answer without a challenge to the service.
|
||||
|
||||
@@ -97,6 +97,7 @@ def stand_in_service() -> SimpleNamespace:
|
||||
get_playready_license=lambda **_: None,
|
||||
get_widevine_service_certificate=lambda **_: None,
|
||||
get_clearkey_license=lambda **_: None,
|
||||
fairplay_bridge=lambda: None,
|
||||
session=None,
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
"""The FairPlay bridge runs only for a service that defines ``get_fairplay_bridge_license``."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import json
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.core.key_resolver import resolver
|
||||
from tests.core.test_hls_service_drm_keys import no_ffprobe # noqa: F401
|
||||
from tests.core.test_hls_service_drm_keys import (
|
||||
KID,
|
||||
SKD_KEY,
|
||||
InitServer,
|
||||
download,
|
||||
hls_video,
|
||||
init_kid_probe,
|
||||
playlist,
|
||||
)
|
||||
from tests.lang_selection import test_kind_only_selection as harness
|
||||
from unshackle.core.api.handlers import handle_proxy_license
|
||||
from unshackle.core.cdm.detect import cdm_type_stub
|
||||
from unshackle.core.drm.cenc import drm_stub
|
||||
from unshackle.core.manifests.hls import HLS
|
||||
from unshackle.core.title_source import TitleSource
|
||||
from unshackle.core.tracks.track import Track
|
||||
|
||||
|
||||
class Example(TitleSource):
|
||||
"""EXAMPLE: licence callbacks that note each call, and no FairPlay bridge hook."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.calls: list[tuple[str, dict[str, Any]]] = []
|
||||
|
||||
def authenticate(self, cookies: Any = None, credential: Any = None) -> None:
|
||||
pass
|
||||
|
||||
def get_titles(self) -> Any:
|
||||
raise NotImplementedError
|
||||
|
||||
def get_tracks(self, title: Any) -> Any:
|
||||
raise NotImplementedError
|
||||
|
||||
def get_chapters(self, title: Any) -> Any:
|
||||
raise NotImplementedError
|
||||
|
||||
def get_playready_license(self, *, challenge: Any, title: Any, track: Any, pssh_b64: str | None = None) -> str:
|
||||
self.calls.append(("playready", {"challenge": challenge, "pssh_b64": pssh_b64}))
|
||||
return "<License/>"
|
||||
|
||||
def get_widevine_license(self, *, challenge: Any, title: Any, track: Any, pssh: Any = None) -> bytes:
|
||||
self.calls.append(("widevine", {"challenge": challenge, "pssh": pssh}))
|
||||
return b"LIC"
|
||||
|
||||
|
||||
class Bridged(Example):
|
||||
"""EXAMPLE with the hook: its licence server issues the content keys of its FairPlay-only tracks."""
|
||||
|
||||
def get_fairplay_bridge_license(
|
||||
self, *, challenge: Any, title: Any, track: Any, drm_system: str, pssh_b64: str | None = None, pssh: Any = None
|
||||
) -> str | bytes:
|
||||
self.calls.append(("bridge", {"challenge": challenge, "drm_system": drm_system, "pssh_b64": pssh_b64}))
|
||||
return "<License/>" if drm_system == "playready" else b"LIC"
|
||||
|
||||
|
||||
def test_a_fairplay_only_track_is_refused_without_the_hook(tmp_path: Path, monkeypatch: Any) -> None:
|
||||
track = hls_video(tmp_path, playlist(SKD_KEY))
|
||||
probe = init_kid_probe(monkeypatch, KID)
|
||||
session = InitServer()
|
||||
|
||||
with pytest.raises(NotImplementedError, match="None of the key systems are supported"):
|
||||
download(track, cdm_type_stub("playready"), session=session, fairplay_bridge=False)
|
||||
|
||||
assert not track.drm
|
||||
assert not probe.called
|
||||
assert not session.requests
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"service, drm, route",
|
||||
[
|
||||
pytest.param(Bridged, lambda: HLS.bridge_stub("playready", [KID]), "bridge", id="bridged-stub"),
|
||||
pytest.param(Bridged, lambda: drm_stub("playready", [KID.hex]), "playready", id="the-stub-of-the-service"),
|
||||
pytest.param(Example, lambda: drm_stub("playready", [KID.hex]), "playready", id="no-hook"),
|
||||
],
|
||||
)
|
||||
def test_a_bridged_challenge_goes_to_the_hook_and_every_other_to_the_system_callback(
|
||||
service: type[Example], drm: Any, route: str
|
||||
) -> None:
|
||||
keys = resolver()
|
||||
keys.service = service()
|
||||
track = SimpleNamespace(drm=[drm()])
|
||||
|
||||
assert keys._service_licence("playready", challenge="C", title=None, track=track, pssh_b64="P", session_id=b"s")
|
||||
|
||||
expected = {"challenge": "C", "pssh_b64": "P", **({"drm_system": "playready"} if route == "bridge" else {})}
|
||||
assert keys.service.calls == [(route, expected)]
|
||||
|
||||
|
||||
def test_drm_preference_picks_the_system_of_the_stub(tmp_path: Path, monkeypatch: Any) -> None:
|
||||
track = hls_video(tmp_path, playlist(SKD_KEY))
|
||||
track.drm_preference = "pr"
|
||||
init_kid_probe(monkeypatch, KID)
|
||||
licensed: list[str] = []
|
||||
|
||||
download(track, cdm_type_stub("widevine"), lambda drm, track_kid=None: licensed.append(type(drm).__name__), True)
|
||||
|
||||
assert licensed == ["PlayReady"]
|
||||
assert track.drm is not None and track.drm[0].bridged
|
||||
|
||||
|
||||
def test_the_drm_of_the_service_needs_no_hook(tmp_path: Path, monkeypatch: Any) -> None:
|
||||
"""A service that puts its own stub on a FairPlay-only track keeps its route, with no hook."""
|
||||
track = hls_video(tmp_path, playlist(SKD_KEY))
|
||||
track.drm = [drm_stub("playready", [KID.hex])]
|
||||
init_kid_probe(monkeypatch, KID)
|
||||
licensed: list[tuple[str, UUID | None]] = []
|
||||
|
||||
download(
|
||||
track,
|
||||
cdm_type_stub("widevine"),
|
||||
lambda drm, track_kid=None: licensed.append((type(drm).__name__, track_kid)),
|
||||
licence_only=True,
|
||||
fairplay_bridge=False,
|
||||
)
|
||||
|
||||
assert licensed == [("PlayReady", KID)]
|
||||
assert track.drm is not None and not track.drm[0].bridged
|
||||
|
||||
|
||||
@pytest.mark.parametrize("system", ["playready", "widevine"])
|
||||
def test_the_relay_route_sends_a_bridged_challenge_to_the_hook(system: str) -> None:
|
||||
service = Bridged()
|
||||
stub = HLS.bridge_stub(system, [KID])
|
||||
pssh_b64 = stub.pssh_b64 if system == "playready" else stub.pssh.dumps()
|
||||
|
||||
reply = handle_proxy_license(
|
||||
service, None, SimpleNamespace(drm=[stub]), base64.b64encode(b"<C/>").decode(), system, pssh_b64=pssh_b64
|
||||
)
|
||||
|
||||
assert json.loads(reply.body)["license"]
|
||||
assert [(route, call["drm_system"]) for route, call in service.calls] == [("bridge", system)]
|
||||
assert service.calls[0][1]["pssh_b64"] == (pssh_b64 if system == "playready" else None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("hook", [None, print], ids=["no-hook", "hook"])
|
||||
def test_dl_tells_each_track_download_whether_the_service_has_the_hook(
|
||||
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, hook: Any
|
||||
) -> None:
|
||||
handed: list[bool] = []
|
||||
monkeypatch.setattr(harness.FakeService, "fairplay_bridge", lambda self: hook)
|
||||
monkeypatch.setattr(Track, "download", lambda self, *a, **k: handed.append(k["fairplay_bridge"]))
|
||||
|
||||
def download_all(tracks: Any, workers: int, run_one: Any, **_: Any) -> None:
|
||||
for i, track in enumerate(tracks):
|
||||
run_one(track, i)
|
||||
|
||||
harness.run(monkeypatch, caplog, {1: {}}, to_end=True, no_mux=True, download=download_all)
|
||||
|
||||
assert handed and set(handed) == {hook is not None}
|
||||
@@ -197,6 +197,7 @@ def download(
|
||||
license_widevine=licence or (lambda drm, track_kid=None: pytest.fail("a licence was requested")),
|
||||
cdm=cdm,
|
||||
max_workers=2,
|
||||
fairplay_bridge=True,
|
||||
)
|
||||
HLS.download_track(track, ctx)
|
||||
return ctx.save_path.read_bytes() if ctx.save_path.exists() else b""
|
||||
|
||||
@@ -73,7 +73,14 @@ def hls_video(tmp_path: Path, text: str) -> Video:
|
||||
return track
|
||||
|
||||
|
||||
def download(track: Video, cdm: Any, licence: Any = None, licence_only: bool = False, session: Any = None) -> None:
|
||||
def download(
|
||||
track: Video,
|
||||
cdm: Any,
|
||||
licence: Any = None,
|
||||
licence_only: bool = False,
|
||||
session: Any = None,
|
||||
fairplay_bridge: bool = True,
|
||||
) -> None:
|
||||
ctx = DownloadContext(
|
||||
save_path=Path(track.from_file).with_name("video.mp4"),
|
||||
save_dir=Path(track.from_file).with_name("video_segments"),
|
||||
@@ -81,6 +88,7 @@ def download(track: Video, cdm: Any, licence: Any = None, licence_only: bool = F
|
||||
license_widevine=licence or (lambda drm, track_kid=None: pytest.fail("a licence was requested")),
|
||||
cdm=cdm,
|
||||
session=session,
|
||||
fairplay_bridge=fairplay_bridge,
|
||||
)
|
||||
if licence_only:
|
||||
DOWNLOAD_LICENCE_ONLY.set()
|
||||
|
||||
@@ -23,7 +23,7 @@ def session_entry(**fields: Any) -> SessionEntry:
|
||||
**{
|
||||
"session_id": "sess",
|
||||
"service_tag": "EXAMPLE",
|
||||
"service_instance": SimpleNamespace(),
|
||||
"service_instance": SimpleNamespace(fairplay_bridge=lambda: None),
|
||||
"licensing": LicensingMode(Licensor.SERVER_CDM),
|
||||
**fields,
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ def test_a_playlist_unshackle_cannot_license_gets_both_stubs_of_its_key_id():
|
||||
"""The client sends the stub that it built, and the server must know the key id to accept it."""
|
||||
track = skd_track()
|
||||
|
||||
ensure_track_drm(track, skd_session())
|
||||
ensure_track_drm(track, skd_session(), bridge=True)
|
||||
|
||||
assert [type(drm).__name__ for drm in track.drm] == ["Widevine", "PlayReady"]
|
||||
assert all(drm.kids == [KID] for drm in track.drm)
|
||||
@@ -120,7 +120,7 @@ def test_the_stubs_take_the_key_id_of_the_init_pssh_when_its_tenc_names_none():
|
||||
track = skd_track()
|
||||
track.get_key_id = lambda init: None # type: ignore[method-assign]
|
||||
|
||||
ensure_track_drm(track, skd_session(init(widevine_box(KID), encv(UUID(int=0)))))
|
||||
ensure_track_drm(track, skd_session(init(widevine_box(KID), encv(UUID(int=0)))), bridge=True)
|
||||
|
||||
assert [drm.kids for drm in track.drm] == [[KID], [KID]]
|
||||
|
||||
@@ -130,3 +130,11 @@ def test_only_the_server_builds_the_stubs_before_the_download():
|
||||
|
||||
assert track.load_drm_from_playlist(skd_session()) is False
|
||||
assert not track.drm
|
||||
|
||||
|
||||
def test_a_service_without_the_bridge_hook_gets_no_stubs():
|
||||
track = skd_track()
|
||||
|
||||
ensure_track_drm(track, skd_session())
|
||||
|
||||
assert not track.drm
|
||||
|
||||
@@ -77,8 +77,8 @@ async def _run_batch(monkeypatch, tracks, cdm_type, calls):
|
||||
return {"00" * 16: "key"}
|
||||
|
||||
monkeypatch.setattr(handlers, "get_validated_session", fake_get_session)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "find_title_for_track", lambda tid, session: SimpleNamespace())
|
||||
monkeypatch.setattr(server_keys, "detect_cdm_type_for_service", lambda tag, cfg: cdm_type)
|
||||
monkeypatch.setattr(server_keys, "resolve_device_name", lambda *a, **k: "dev")
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
"""The FairPlay bridge over ``--remote``: the real client against the real server routes, over loopback HTTP."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from click import ClickException
|
||||
from langcodes import Language
|
||||
|
||||
from tests.core.key_resolver import resolver
|
||||
from tests.core.test_hls_service_drm_keys import KID, OTHER_KID, ROTATION, InitServer, download, encrypted_init
|
||||
from tests.remote.sessions import session_entry
|
||||
from tests.remote.unit.test_relay_per_header_pssh import license_all, pr_kids
|
||||
from tests.remote.unit.test_remote_loopback import StubService, make_client, server # noqa: F401
|
||||
from unshackle.core.api import handlers, server_keys
|
||||
from unshackle.core.api.key_policy import KeyPolicy
|
||||
from unshackle.core.cdm.detect import cdm_type_stub
|
||||
from unshackle.core.drm import PlayReady, cenc
|
||||
from unshackle.core.tracks import Tracks, Video
|
||||
from unshackle.core.tracks.track import Track
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
URL = "https://cdn.example/media.m3u8"
|
||||
|
||||
|
||||
class Origin(InitServer):
|
||||
"""The CDN of EXAMPLE: the FairPlay-only rotation playlist and its init segments."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__(init=encrypted_init(KID), rot=encrypted_init(OTHER_KID))
|
||||
|
||||
def get(self, url: str, **kwargs: Any) -> SimpleNamespace: # type: ignore[override]
|
||||
if url.endswith(".m3u8"):
|
||||
return SimpleNamespace(ok=True, status_code=200, text=ROTATION)
|
||||
return super().get(url, **kwargs)
|
||||
|
||||
|
||||
def serve(origin: type[Origin]) -> Any:
|
||||
"""A ``get_tracks`` that gives one FairPlay-only HLS video from ``origin``."""
|
||||
|
||||
def get_tracks(self: StubService, title: Any) -> Tracks:
|
||||
self.session = origin()
|
||||
video = Video(
|
||||
url=URL, language=Language.get("en"), descriptor=Track.Descriptor.HLS, codec=Video.Codec.HEVC, id_="vid"
|
||||
)
|
||||
return Tracks([video])
|
||||
|
||||
return get_tracks
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def no_ffprobe(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(cenc, "ffprobe", lambda data: {})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def served(monkeypatch: pytest.MonkeyPatch) -> list[dict[str, Any]]:
|
||||
"""The server's EXAMPLE serves one FairPlay-only HLS video; each call of its hook lands in the list."""
|
||||
calls: list[dict[str, Any]] = []
|
||||
|
||||
monkeypatch.setattr(StubService, "get_tracks", serve(Origin))
|
||||
return calls
|
||||
|
||||
|
||||
def bridge(remote: Any, tmp_path: Path) -> Video:
|
||||
"""Download the first video of the first title in a licence-only run, as dl does, and return it."""
|
||||
title = remote.get_titles()[0]
|
||||
track = remote.get_tracks(title).videos[0]
|
||||
track.from_file = tmp_path / "media.m3u8"
|
||||
track.from_file.write_text(ROTATION, encoding="utf-8")
|
||||
bridged = remote.fairplay_bridge() is not None
|
||||
download(track, cdm_type_stub("playready"), lambda drm, track_kid=None: None, True, Origin(), bridged)
|
||||
keys = resolver()
|
||||
keys.service = remote
|
||||
assert track.drm is not None
|
||||
license_all(track.drm, lambda **kw: keys._service_licence("playready", title=title, track=track, **kw))
|
||||
return track
|
||||
|
||||
|
||||
def test_a_served_hook_licenses_each_key_id_of_a_rotation_through_the_relay(
|
||||
server: SimpleNamespace, # noqa: F811
|
||||
served: list[dict[str, Any]],
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
def hook(self: StubService, *, challenge: str, title: Any, track: Any, drm_system: str, pssh_b64: str) -> str:
|
||||
served.append({"drm_system": drm_system, "kids": pr_kids(pssh_b64)})
|
||||
return "<License/>"
|
||||
|
||||
monkeypatch.setattr(StubService, "get_fairplay_bridge_license", hook, raising=False)
|
||||
remote = make_client(server.url)
|
||||
try:
|
||||
remote.authenticate()
|
||||
track = bridge(remote, tmp_path)
|
||||
finally:
|
||||
remote.close()
|
||||
|
||||
assert remote.fairplay_bridge() == remote.relay_fairplay_bridge
|
||||
assert served == [{"drm_system": "playready", "kids": [KID]}, {"drm_system": "playready", "kids": [OTHER_KID]}]
|
||||
assert isinstance(track.drm[0], PlayReady) and track.drm[0].content_keys.keys() == {KID, OTHER_KID}
|
||||
|
||||
|
||||
def test_a_served_service_without_the_hook_gets_no_bridge(
|
||||
server: SimpleNamespace, # noqa: F811
|
||||
served: list[dict[str, Any]],
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
remote = make_client(server.url)
|
||||
try:
|
||||
remote.authenticate()
|
||||
with pytest.raises(NotImplementedError, match="None of the key systems are supported"):
|
||||
bridge(remote, tmp_path)
|
||||
assert remote.fairplay_bridge() is None
|
||||
finally:
|
||||
remote.close()
|
||||
|
||||
assert not served
|
||||
|
||||
|
||||
def test_the_server_refuses_a_bridged_challenge_it_cannot_read_the_key_ids_of(
|
||||
server: SimpleNamespace, # noqa: F811
|
||||
served: list[dict[str, Any]],
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
"""The client's stub must not reach get_playready_license when the server's own stub build failed."""
|
||||
|
||||
class Unreachable(Origin):
|
||||
def get(self, url: str, **kwargs: Any) -> SimpleNamespace: # type: ignore[override]
|
||||
if url.endswith(".m3u8"):
|
||||
return super().get(url, **kwargs)
|
||||
raise ConnectionError("EXAMPLE init segment unreachable")
|
||||
|
||||
def licence(self: StubService, **kwargs: Any) -> str:
|
||||
served.append(kwargs)
|
||||
return "<License/>"
|
||||
|
||||
monkeypatch.setattr(StubService, "get_tracks", serve(Unreachable))
|
||||
monkeypatch.setattr(StubService, "get_fairplay_bridge_license", licence, raising=False)
|
||||
monkeypatch.setattr(StubService, "get_playready_license", licence, raising=False)
|
||||
remote = make_client(server.url)
|
||||
try:
|
||||
remote.authenticate()
|
||||
with pytest.raises(ClickException, match="DRM_ERROR.*read no key ID of track .* for the FairPlay bridge"):
|
||||
bridge(remote, tmp_path)
|
||||
finally:
|
||||
remote.close()
|
||||
|
||||
assert not served
|
||||
|
||||
|
||||
@pytest.mark.parametrize("hook", [None, print], ids=["no-hook", "hook"])
|
||||
def test_the_server_builds_stubs_only_for_a_service_with_the_hook(monkeypatch: pytest.MonkeyPatch, hook: Any) -> None:
|
||||
"""The batch keys route and the licence route ask the service before ``ensure_track_drm`` may bridge."""
|
||||
track = SimpleNamespace(id="vid", drm=None, data={})
|
||||
session = session_entry(service_instance=SimpleNamespace(fairplay_bridge=lambda: hook), tracks={"vid": track})
|
||||
asked: list[bool] = []
|
||||
|
||||
def ensure(track: Any, session: Any = None, init_data: Any = None, bridge: bool = False) -> None:
|
||||
asked.append(bridge)
|
||||
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", ensure)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", ensure)
|
||||
monkeypatch.setattr(server_keys, "fetch_init_segment", lambda *a: None)
|
||||
|
||||
server_keys.resolve_batch(session, ["vid"], "playready", KeyPolicy())
|
||||
with contextlib.suppress(Exception):
|
||||
handlers.license_response(
|
||||
{"track_id": "vid", "challenge": "Qw==", "drm_type": "playready", "pssh": "cHNzaA=="}, session, "s", None
|
||||
)
|
||||
|
||||
assert asked == [hook is not None] * 2
|
||||
@@ -24,6 +24,7 @@ from unshackle.core.api import handlers, server_keys
|
||||
from unshackle.core.api.key_policy import KeyPolicy
|
||||
from unshackle.core.drm import PlayReady, Widevine
|
||||
from unshackle.core.service import Service
|
||||
from unshackle.core.title_source import TitleSource
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
@@ -39,6 +40,9 @@ class _Recorder:
|
||||
def __init__(self) -> None:
|
||||
self.calls: list[dict[str, Any]] = []
|
||||
|
||||
def fairplay_bridge(self) -> None:
|
||||
return None
|
||||
|
||||
def get_widevine_service_certificate(self, *, challenge: Any, title: Any, track: Any) -> None:
|
||||
return None
|
||||
|
||||
@@ -234,6 +238,31 @@ def test_server_cdm_playready_classic_signature_gets_three_arguments(playready_e
|
||||
assert set(service.calls[0]) == {"challenge", "title", "track"}
|
||||
|
||||
|
||||
class BridgeService(ClassicService):
|
||||
"""EXAMPLE with the FairPlay bridge hook."""
|
||||
|
||||
fairplay_bridge = TitleSource.fairplay_bridge
|
||||
|
||||
def get_fairplay_bridge_license(self, *, challenge: Any, title: Any, track: Any, drm_system: str) -> str:
|
||||
self.calls.append({"hook": drm_system})
|
||||
return "<License>ok</License>"
|
||||
|
||||
|
||||
def test_server_cdm_sends_the_challenge_of_a_bridged_track_to_the_hook(playready_env: None) -> None:
|
||||
service = BridgeService()
|
||||
keys = server_keys.handle_single_server_cdm(
|
||||
service=service,
|
||||
title=SimpleNamespace(),
|
||||
track=SimpleNamespace(drm=[SimpleNamespace(bridged=True)]),
|
||||
pssh_b64=PSSH_B64,
|
||||
drm_type="playready",
|
||||
policy=KeyPolicy(),
|
||||
)
|
||||
|
||||
assert keys == {KID.hex: KEY.hex()}
|
||||
assert service.calls == [{"hook": "playready"}]
|
||||
|
||||
|
||||
def test_server_cdm_playready_offers_pssh_b64_and_session_id_not_pssh(playready_env: None) -> None:
|
||||
service = VarKwService()
|
||||
_server_cdm(service, "playready")
|
||||
|
||||
@@ -54,6 +54,9 @@ class PsshService:
|
||||
def __init__(self) -> None:
|
||||
self.calls: list[dict[str, Any]] = []
|
||||
|
||||
def fairplay_bridge(self) -> None:
|
||||
return None
|
||||
|
||||
def get_playready_license(self, *, challenge: str, title: Any, track: Any, pssh_b64: str | None = None) -> str:
|
||||
self.calls.append({"challenge": challenge, "pssh_b64": pssh_b64})
|
||||
return "<License>x</License>"
|
||||
|
||||
@@ -36,6 +36,7 @@ from unshackle.core.remote_protocol import CHANNEL_ROUTE, LOGS_ROUTE, PROMPT_ROU
|
||||
from unshackle.core.remote_service import RemoteError, RemoteService
|
||||
from unshackle.core.service import Service
|
||||
from unshackle.core.session import RnetSession, record_requests
|
||||
from unshackle.core.title_source import TitleSource
|
||||
from unshackle.core.titles.movie import Movie, Movies
|
||||
from unshackle.core.tracks import Audio, Chapters, Tracks
|
||||
from unshackle.core.tracks.track import Track
|
||||
@@ -102,6 +103,8 @@ class StubService:
|
||||
def decrypts_samples(self, track: Any) -> bool:
|
||||
return False
|
||||
|
||||
fairplay_bridge = TitleSource.fairplay_bridge
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def server(request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch, tmp_path: Any) -> Iterator[SimpleNamespace]:
|
||||
|
||||
@@ -54,8 +54,8 @@ def env(monkeypatch):
|
||||
monkeypatch.setattr(drm_mod, "PlayReady", _FakePlayReady)
|
||||
monkeypatch.setitem(cdm_mod.__dict__, "load_cdm", lambda *a, **k: object())
|
||||
monkeypatch.setattr(detect_mod, "is_playready_cdm", lambda cdm: True)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "resolve_device_name", lambda *a, **k: "dev")
|
||||
monkeypatch.setattr(server_keys, "check_vaults", lambda kids, name, **_: None)
|
||||
monkeypatch.setattr(server_keys, "cache_to_vaults", lambda keys, name, **_: None)
|
||||
|
||||
@@ -84,6 +84,9 @@ class Service:
|
||||
def __init__(self):
|
||||
self.pssh: list[str] = []
|
||||
|
||||
def fairplay_bridge(self) -> None:
|
||||
return None
|
||||
|
||||
def get_playready_license(self, *, challenge, title, track, pssh_b64=None):
|
||||
self.pssh.append(pssh_b64)
|
||||
return "<License>stand-in</License>"
|
||||
@@ -94,8 +97,8 @@ def licence(monkeypatch):
|
||||
cdm, service = FakeCdm(), Service()
|
||||
monkeypatch.setitem(cdm_mod.__dict__, "load_cdm", lambda *a, **k: cdm)
|
||||
monkeypatch.setattr(detect_mod, "is_playready_cdm", lambda c: c is cdm)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "resolve_device_name", lambda *a, **k: "dev")
|
||||
monkeypatch.setattr(server_keys, "check_vaults", lambda kids, name, **_: None)
|
||||
monkeypatch.setattr(server_keys, "cache_to_vaults", lambda keys, name, **_: None)
|
||||
|
||||
@@ -146,8 +146,8 @@ async def test_batch_reports_vault_keys_from_the_init_segment_pssh(monkeypatch):
|
||||
return {KID.hex: "c"}
|
||||
|
||||
monkeypatch.setattr(handlers, "get_validated_session", validated)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "find_title_for_track", lambda tid, session: SimpleNamespace())
|
||||
monkeypatch.setattr(server_keys, "detect_cdm_type_for_service", lambda tag, cfg: "widevine")
|
||||
monkeypatch.setattr(server_keys, "resolve_device_name", lambda *a, **k: "dev")
|
||||
@@ -194,8 +194,8 @@ async def test_batch_asks_again_when_a_cached_vault_answer_lacks_the_track_kid(m
|
||||
return {KID.hex: "v", CDM_KID.hex: "c"}
|
||||
|
||||
monkeypatch.setattr(handlers, "get_validated_session", validated)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "find_title_for_track", lambda tid, session: SimpleNamespace())
|
||||
monkeypatch.setattr(server_keys, "detect_cdm_type_for_service", lambda tag, cfg: "widevine")
|
||||
monkeypatch.setattr(server_keys, "resolve_device_name", lambda *a, **k: "dev")
|
||||
@@ -431,8 +431,8 @@ async def batch_answer(monkeypatch, track, request=None, licensing=None):
|
||||
return session
|
||||
|
||||
monkeypatch.setattr(handlers, "get_validated_session", validated)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "find_title_for_track", lambda tid, session: SimpleNamespace())
|
||||
monkeypatch.setattr(server_keys, "detect_cdm_type_for_service", lambda tag, cfg: "widevine")
|
||||
monkeypatch.setattr(server_keys, "resolve_device_name", lambda *a, **k: "dev")
|
||||
|
||||
@@ -81,8 +81,8 @@ PSSH = base64.b64encode(b"A").decode()
|
||||
def server_env(monkeypatch):
|
||||
monkeypatch.setitem(cdm_mod.__dict__, "load_cdm", lambda *a, **k: object())
|
||||
monkeypatch.setattr(detect_mod, "is_playready_cdm", lambda cdm: True)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "resolve_device_name", lambda *a, **k: "dev")
|
||||
monkeypatch.setattr(server_keys, "check_vaults", lambda kids, name, **_: None)
|
||||
monkeypatch.setattr(server_keys, "cache_to_vaults", lambda keys, name, **_: None)
|
||||
|
||||
@@ -129,8 +129,8 @@ def widevine_env(monkeypatch):
|
||||
|
||||
monkeypatch.setattr(wv_pssh_mod, "PSSH", lambda b64: b64)
|
||||
monkeypatch.setattr(drm_mod, "Widevine", _FakeWidevine)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "resolve_device_name", lambda *a, **k: "dev")
|
||||
|
||||
def no_device(*a, **k):
|
||||
@@ -292,8 +292,8 @@ def playready_env(monkeypatch):
|
||||
return cls()
|
||||
|
||||
monkeypatch.setattr(drm_mod, "PlayReady", _FakePlayReady)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "resolve_device_name", lambda *a, **k: "dev")
|
||||
|
||||
def no_device(*a, **k):
|
||||
@@ -318,8 +318,8 @@ CAP_DETAILS = {"reason": "server_cdm_max_height", "max_height": 1080}
|
||||
|
||||
|
||||
def test_single_licence_refuses_a_capped_track_with_details(monkeypatch):
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "detect_cdm_type_for_service", lambda tag, cfg: "widevine")
|
||||
monkeypatch.setattr(server_keys, "resolve_device_name", lambda *a, **k: "dev")
|
||||
monkeypatch.setattr(server_keys, "check_vaults", lambda kids, name, **_: None)
|
||||
@@ -366,6 +366,9 @@ def test_the_tracks_response_echoes_the_session_choice(monkeypatch):
|
||||
class StubService:
|
||||
session = SimpleNamespace(headers={}, cookies=[])
|
||||
|
||||
def fairplay_bridge(self):
|
||||
return None
|
||||
|
||||
def get_tracks(self, title):
|
||||
return Tracks()
|
||||
|
||||
|
||||
@@ -51,8 +51,8 @@ def playready_env(monkeypatch):
|
||||
monkeypatch.setattr(drm_mod, "PlayReady", _FakePlayReady)
|
||||
monkeypatch.setitem(cdm_mod.__dict__, "load_cdm", lambda *a, **k: object())
|
||||
monkeypatch.setattr(detect_mod, "is_playready_cdm", lambda cdm: True)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "resolve_device_name", lambda *a, **k: "dev")
|
||||
monkeypatch.setattr(handlers.config, "serve", {"users": {}}, raising=False)
|
||||
|
||||
@@ -96,8 +96,8 @@ def test_widevine_still_uses_vault_shortcut(monkeypatch):
|
||||
|
||||
monkeypatch.setattr(wv_pssh_mod, "PSSH", _FakeWvPSSH)
|
||||
monkeypatch.setattr(drm_mod, "Widevine", _FakeWidevine)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "resolve_device_name", lambda *a, **k: "dev")
|
||||
monkeypatch.setattr(
|
||||
server_keys, "check_vaults", lambda kids, name, **_: ({kid.hex: "vaultkey"}, {kid.hex: "sqlite"})
|
||||
@@ -144,8 +144,8 @@ async def test_batch_shares_full_bundle_per_pssh(monkeypatch):
|
||||
return dict(bundle_a) if pssh_str == pssh_a else dict(bundle_b)
|
||||
|
||||
monkeypatch.setattr(handlers, "get_validated_session", fake_get_session)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "find_title_for_track", lambda tid, session: SimpleNamespace())
|
||||
monkeypatch.setattr(server_keys, "detect_cdm_type_for_service", lambda tag, cfg: "playready")
|
||||
monkeypatch.setattr(server_keys, "resolve_device_name", lambda *a, **k: "dev")
|
||||
|
||||
@@ -80,6 +80,7 @@ def _session(track):
|
||||
get_widevine_service_certificate=lambda **k: None,
|
||||
get_widevine_license=lambda **k: b"",
|
||||
get_playready_license=lambda **k: b"",
|
||||
fairplay_bridge=lambda: None,
|
||||
)
|
||||
return session_entry(service_instance=service, tracks={"vid": track})
|
||||
|
||||
@@ -93,8 +94,8 @@ def env(monkeypatch):
|
||||
monkeypatch.setitem(cdm_mod.__dict__, "load_cdm", lambda name, **k: SimpleNamespace(device=name))
|
||||
monkeypatch.setattr(detect_mod, "is_widevine_cdm", lambda cdm: cdm.device.startswith("wv"))
|
||||
monkeypatch.setattr(detect_mod, "is_playready_cdm", lambda cdm: cdm.device.startswith("pr"))
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "ensure_track_drm", lambda track, session=None, init_data=None, **_: None)
|
||||
monkeypatch.setattr(server_keys, "check_vaults", lambda kids, name, **_: None)
|
||||
monkeypatch.setattr(server_keys, "cache_to_vaults", lambda keys, name, **_: None)
|
||||
monkeypatch.setattr(server_keys, "find_title_for_track", lambda tid, session: SimpleNamespace())
|
||||
@@ -266,6 +267,9 @@ def test_the_tracks_response_names_the_system_of_the_loaded_device(monkeypatch,
|
||||
session = SimpleNamespace(headers={}, cookies=[])
|
||||
ctx = SimpleNamespace(obj=SimpleNamespace(cdm=SimpleNamespace(is_playready=system == "playready")))
|
||||
|
||||
def fairplay_bridge(self):
|
||||
return None
|
||||
|
||||
def get_tracks(self, title):
|
||||
return Tracks()
|
||||
|
||||
@@ -325,6 +329,7 @@ def _reply_then_licence(monkeypatch, track, drm_type, loaded=None):
|
||||
"get_tracks": lambda self, title: Tracks([track]),
|
||||
"get_chapters": lambda self, title: [],
|
||||
"decrypts_samples": lambda self, track: False,
|
||||
"fairplay_bridge": lambda self: None,
|
||||
"get_widevine_service_certificate": lambda self, **k: None,
|
||||
"get_widevine_license": lambda self, **k: b"",
|
||||
"get_playready_license": lambda self, **k: b"",
|
||||
|
||||
@@ -37,6 +37,9 @@ class Service:
|
||||
def __init__(self):
|
||||
self.licensed = []
|
||||
|
||||
def fairplay_bridge(self):
|
||||
return None
|
||||
|
||||
def get_widevine_service_certificate(self, **_):
|
||||
return None
|
||||
|
||||
|
||||
@@ -198,7 +198,7 @@ def server(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> SimpleNamespace:
|
||||
async def validated(session_id: str, request: Any) -> Any:
|
||||
return entry
|
||||
|
||||
def ensure(track: Any, session: Any = None, init_data: Any = None) -> None:
|
||||
def ensure(track: Any, session: Any = None, init_data: Any = None, bridge: bool = False) -> None:
|
||||
ensured.append(track.id)
|
||||
track.drm = [SimpleNamespace(kids=[LATE])]
|
||||
|
||||
|
||||
@@ -123,6 +123,9 @@ def test_tracks_handler_marks_the_title_live_before_it_fetches(monkeypatch: pyte
|
||||
class StubService:
|
||||
session = SimpleNamespace(headers={}, cookies=[])
|
||||
|
||||
def fairplay_bridge(self) -> None:
|
||||
return None
|
||||
|
||||
def get_tracks(self, title: Any) -> Tracks:
|
||||
seen_during_fetch.append(published[-1][1]["title"] if published else None)
|
||||
return Tracks()
|
||||
|
||||
@@ -111,6 +111,9 @@ class StubService:
|
||||
def decrypts_samples(self, track):
|
||||
return False
|
||||
|
||||
def fairplay_bridge(self):
|
||||
return None
|
||||
|
||||
|
||||
def stub_video():
|
||||
from unshackle.core.tracks import Video
|
||||
|
||||
@@ -2339,6 +2339,7 @@ class dl:
|
||||
progress=progress_callables[i],
|
||||
on_segments=disk_check.add_segments if disk_check else None,
|
||||
sample_decrypter=service.sample_decrypter(track),
|
||||
fairplay_bridge=service.fairplay_bridge() is not None,
|
||||
)
|
||||
# DRM-free and HLS-ClearKey tracks never reach prepare_drm, so export here.
|
||||
# drm=None on purpose: licensed tracks already recorded their DRM/keys
|
||||
|
||||
@@ -86,6 +86,7 @@ from unshackle.core.remote_protocol import (
|
||||
from unshackle.core.run_plan import SESSION_DL_KEYS, PlanError, served_plan
|
||||
from unshackle.core.services import Services
|
||||
from unshackle.core.session import RnetSession, utf8_text
|
||||
from unshackle.core.title_source import service_licence
|
||||
from unshackle.core.titles import Episode, Song
|
||||
from unshackle.core.tunnel import (
|
||||
HEARTBEAT,
|
||||
@@ -3187,6 +3188,7 @@ async def session_tracks_handler(
|
||||
sample_decrypt_tracks=[
|
||||
str(t.id) for t in [*tracks.videos, *tracks.audio] if service_instance.decrypts_samples(t)
|
||||
],
|
||||
fairplay_bridge=service_instance.fairplay_bridge() is not None,
|
||||
)
|
||||
|
||||
reply = await asyncio.to_thread(
|
||||
@@ -3600,16 +3602,13 @@ def handle_proxy_license(
|
||||
if drm_type == "widevine":
|
||||
from pywidevine.pssh import PSSH
|
||||
|
||||
licence, challenge = service.get_widevine_license, challenge_bytes
|
||||
challenge = challenge_bytes
|
||||
relayed = {"pssh": PSSH(pssh_b64)} if pssh_b64 else {}
|
||||
else:
|
||||
licence, challenge = service.get_playready_license, challenge_bytes.decode("utf-8", errors="replace")
|
||||
challenge = challenge_bytes.decode("utf-8", errors="replace")
|
||||
relayed = {"pssh_b64": pssh_b64} if pssh_b64 else {}
|
||||
license_blob = call_licence(
|
||||
lambda **kw: licence(**declared_kwargs(licence, {"title": title, "track": track, **kw})),
|
||||
challenge,
|
||||
**relayed,
|
||||
)
|
||||
licence = partial(service_licence, service, drm_type, title=title, track=track)
|
||||
license_blob = call_licence(licence, challenge, **relayed)
|
||||
except APIError:
|
||||
raise
|
||||
except (Exception, SystemExit) as exc:
|
||||
@@ -3811,7 +3810,7 @@ def license_response(data: LicenseRequest, session: Any, session_id: str, reques
|
||||
|
||||
pssh_b64 = data.get("pssh")
|
||||
if pssh_b64 or mode == "server_cdm":
|
||||
ensure_track_drm(track, getattr(service, "session", None))
|
||||
ensure_track_drm(track, getattr(service, "session", None), bridge=service.fairplay_bridge() is not None)
|
||||
|
||||
if mode == "server_cdm":
|
||||
# The server's config.cdm mapping decides the DRM system, as in the batch path.
|
||||
@@ -3853,6 +3852,11 @@ def license_response(data: LicenseRequest, session: Any, session_id: str, reques
|
||||
server_keys.note_served_keys(session, keys, key_sources)
|
||||
return web.json_response(LicenseReply(keys=keys, vault_keys=list(key_sources), drm_type=server_drm_type))
|
||||
|
||||
if pssh_b64 and not track.drm and service.fairplay_bridge():
|
||||
raise APIError(
|
||||
APIErrorCode.DRM_ERROR,
|
||||
f"The server read no key ID of track {track_log_label(track)} for the FairPlay bridge",
|
||||
)
|
||||
if pssh_b64:
|
||||
try:
|
||||
server_keys.require_track_pssh(track, pssh_b64, drm_type)
|
||||
@@ -3951,7 +3955,7 @@ async def session_keys_handler(data: TrackKeys, session_id: str, request: web.Re
|
||||
def named_kids() -> set[str]:
|
||||
track = session.tracks.get(track_id) if isinstance(track_id, str) else None
|
||||
if track is not None:
|
||||
ensure_track_drm(track, getattr(service, "session", None))
|
||||
ensure_track_drm(track, getattr(service, "session", None), bridge=service.fairplay_bridge() is not None)
|
||||
return {kid.hex for t in list(session.tracks.values()) for drm in t.drm or [] for kid in real_kids(drm)}
|
||||
|
||||
def store(named: set[str]) -> tuple[Any, dict[Any, str]]:
|
||||
|
||||
@@ -438,6 +438,7 @@ def service_details(tag: str) -> dict[str, Any]:
|
||||
service_data["has_drm"] = (
|
||||
getattr(service_module, "get_widevine_license", None) is not _BaseService.get_widevine_license
|
||||
or getattr(service_module, "get_playready_license", None) is not _BaseService.get_playready_license
|
||||
or hasattr(service_module, "get_fairplay_bridge_license")
|
||||
)
|
||||
|
||||
# Prefer the service's explicit AUTH_METHODS; otherwise infer from authenticate().
|
||||
|
||||
@@ -6,6 +6,7 @@ import logging
|
||||
import uuid
|
||||
from collections.abc import Callable, Iterable, Sequence
|
||||
from dataclasses import dataclass, field
|
||||
from functools import partial
|
||||
from typing import TYPE_CHECKING, Any, NamedTuple
|
||||
|
||||
from unshackle.core.api import licence_cap
|
||||
@@ -19,7 +20,7 @@ from unshackle.core.cdm.loader import device_system
|
||||
from unshackle.core.drm import WHOLE_FILE_DRM, is_null_key, whole_file_cipher
|
||||
from unshackle.core.drm.discovery import drm_from_init_segment, ensure_track_drm, fetch_init_segment, track_pssh
|
||||
from unshackle.core.remote_protocol import APIErrorCode
|
||||
from unshackle.core.utilities import declared_kwargs
|
||||
from unshackle.core.title_source import service_licence
|
||||
from unshackle.core.utils.redact import redact_all
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -399,6 +400,7 @@ def handle_single_server_cdm(
|
||||
return vault_keys
|
||||
raise refusal or APIError(APIErrorCode.NO_CONTENT, "The server vault holds no content key for this track")
|
||||
|
||||
licence = partial(service_licence, service, drm_type, title=title, track=track)
|
||||
if drm_type == "playready":
|
||||
from unshackle.core.drm import PlayReady
|
||||
|
||||
@@ -424,9 +426,7 @@ def handle_single_server_cdm(
|
||||
track,
|
||||
cdm=cdm,
|
||||
certificate=lambda challenge, **_: None,
|
||||
licence=lambda **kw: service.get_playready_license(
|
||||
**declared_kwargs(service.get_playready_license, {**kw, "title": title, "track": track})
|
||||
),
|
||||
licence=licence,
|
||||
)
|
||||
elif drm_type == "widevine":
|
||||
from pywidevine.pssh import PSSH as WvPSSH
|
||||
@@ -455,9 +455,7 @@ def handle_single_server_cdm(
|
||||
certificate=lambda challenge, **_: service.get_widevine_service_certificate(
|
||||
challenge=challenge, title=title, track=track
|
||||
),
|
||||
licence=lambda **kw: service.get_widevine_license(
|
||||
**declared_kwargs(service.get_widevine_license, {**kw, "title": title, "track": track})
|
||||
),
|
||||
licence=licence,
|
||||
)
|
||||
else:
|
||||
raise APIError(
|
||||
@@ -641,7 +639,7 @@ def resolve_batch(session: Any, track_ids: Iterable[str], drm_type: str, policy:
|
||||
|
||||
svc_session = getattr(service, "session", None)
|
||||
init_data = fetch_init_segment(track, svc_session)
|
||||
ensure_track_drm(track, svc_session, init_data)
|
||||
ensure_track_drm(track, svc_session, init_data, bridge=service.fairplay_bridge() is not None)
|
||||
cipher = whole_file_cipher(track)
|
||||
if cipher is not None:
|
||||
if not session.licensing.vault_only:
|
||||
|
||||
@@ -210,6 +210,8 @@ class CencDrm:
|
||||
Exceptions: Any
|
||||
content_keys: dict[UUID, str]
|
||||
_kid: UUID | None
|
||||
bridged = False
|
||||
"""A stub ``HLS.bridge_stub`` built for a FairPlay-only track: its challenge goes to the FairPlay bridge hook."""
|
||||
|
||||
@classmethod
|
||||
def from_pssh_box(cls, box: Container, kid: UUID | None) -> Self:
|
||||
|
||||
@@ -87,10 +87,11 @@ def drm_from_init_segment(track: Any, session: Any = None, init_data: bytes | No
|
||||
return drms
|
||||
|
||||
|
||||
def ensure_track_drm(track: Any, session: Any = None, init_data: bytes | None = None) -> None:
|
||||
def ensure_track_drm(track: Any, session: Any = None, init_data: bytes | None = None, bridge: bool = False) -> None:
|
||||
"""Fill ``track.drm`` from the manifest when the track has none; a failure leaves it empty and never raises.
|
||||
|
||||
Reads DASH ContentProtection and then the init segment, the HLS playlist keys, or the ISM ProtectionHeader.
|
||||
With ``bridge`` (the service has a FairPlay bridge hook) a FairPlay-only HLS track gets the bridged stubs.
|
||||
"""
|
||||
if track.drm:
|
||||
return
|
||||
@@ -109,7 +110,7 @@ def ensure_track_drm(track: Any, session: Any = None, init_data: bytes | None =
|
||||
|
||||
if (track.data.get("hls") or getattr(track, "descriptor", None) is Track.Descriptor.HLS) and track.url:
|
||||
try:
|
||||
if track.load_drm_from_playlist(session, bridge=True):
|
||||
if track.load_drm_from_playlist(session, bridge=bridge):
|
||||
return
|
||||
except Exception as e:
|
||||
log.debug(f"HLS DRM prefetch failed for {track.id}: {e!r}")
|
||||
|
||||
@@ -37,10 +37,10 @@ from unshackle.core.drm import (
|
||||
real_kids,
|
||||
verify,
|
||||
)
|
||||
from unshackle.core.title_source import TitleSource
|
||||
from unshackle.core.title_source import TitleSource, service_licence
|
||||
from unshackle.core.titles import Title_T
|
||||
from unshackle.core.tracks import Video
|
||||
from unshackle.core.utilities import declared_kwargs, log_event
|
||||
from unshackle.core.utilities import log_event
|
||||
from unshackle.core.utils.redact import redact_secrets
|
||||
from unshackle.core.utils.subprocess import ffmpeg_decodes
|
||||
from unshackle.core.vaults import KeySource, Vaults
|
||||
@@ -708,18 +708,14 @@ class ContentKeyResolver:
|
||||
tree.add(label)
|
||||
|
||||
def _service_licence(self, drm_system: str | None = None, **kwargs: Any) -> Any:
|
||||
"""Call the service's licence function for the track's DRM system.
|
||||
|
||||
Arguments go through declared_kwargs, so a service receives only the ones its own
|
||||
licence function declares.
|
||||
"""Call the service's licence function, or its FairPlay bridge hook, for the track's DRM system (``service_licence``).
|
||||
|
||||
_prepare_drm passes drm_system so the choice follows the track, not the loaded CDM:
|
||||
a track downloading at the same time can swap self.cdms.cdm.
|
||||
"""
|
||||
if drm_system is None:
|
||||
drm_system = cdm_system(self.cdms.cdm) or "widevine"
|
||||
fn = self.service.get_playready_license if drm_system == "playready" else self.service.get_widevine_license
|
||||
return fn(**declared_kwargs(fn, kwargs))
|
||||
return service_licence(self.service, drm_system, **kwargs)
|
||||
|
||||
def _licence_one_drm(self, prepare: Callable, drm: Widevine | PlayReady, track_kid: UUID | None) -> None:
|
||||
"""Send one challenge for the KIDs of this DRM, also when the DRM or a vault has the content keys.
|
||||
|
||||
@@ -28,6 +28,7 @@ from pywidevine.pssh import PSSH as WV_PSSH
|
||||
from requests import Session
|
||||
|
||||
from unshackle.core import binaries
|
||||
from unshackle.core.cdm.choice import drm_system
|
||||
from unshackle.core.cdm.detect import cdm_system, 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
|
||||
@@ -713,7 +714,11 @@ class HLS:
|
||||
session_drm = track.get_drm_for_cdm(cdm) if track.drm else None
|
||||
bridge_drm = None
|
||||
bridge_kids: list[UUID] = []
|
||||
if session_drm is None and (system := cdm_system(cdm)):
|
||||
if (
|
||||
session_drm is None
|
||||
and ctx.fairplay_bridge
|
||||
and (system := drm_system(track.drm_preference) or cdm_system(cdm))
|
||||
):
|
||||
bridge_kids = HLS.get_bridge_kids(master, wanted_segments, track, session)
|
||||
bridge_drm = HLS.bridge_stub(system, bridge_kids) if bridge_kids else None
|
||||
if bridge_drm:
|
||||
@@ -1602,14 +1607,16 @@ class HLS:
|
||||
|
||||
@staticmethod
|
||||
def bridge_stub(system: str, kids: list[UUID]) -> Widevine | PlayReady:
|
||||
"""The `drm_stub` of `system` for `kids`: one Widevine header that names each, or a PlayReady header for each.
|
||||
"""The `drm_stub` of `system` for `kids`, marked bridged: one Widevine header for all, or a PlayReady header each.
|
||||
|
||||
A PlayReady stub header holds one KID, so one that named several would license only the first content key.
|
||||
"""
|
||||
if system != "playready":
|
||||
return drm_stub(system, [kid.hex for kid in kids])
|
||||
drm, *others = (cast(PlayReady, drm_stub(system, [kid.hex])) for kid in kids)
|
||||
drm.absorb(*others)
|
||||
drm = drm_stub(system, [kid.hex for kid in kids])
|
||||
else:
|
||||
drm, *others = (cast(PlayReady, drm_stub(system, [kid.hex])) for kid in kids)
|
||||
drm.absorb(*others)
|
||||
drm.bridged = True
|
||||
return drm
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -278,6 +278,7 @@ class TracksReply(TypedDict, total=False):
|
||||
server_cdm: bool | None
|
||||
server_cdm_type: str
|
||||
sample_decrypt_tracks: list[str]
|
||||
fairplay_bridge: bool
|
||||
|
||||
|
||||
class SegmentsRequest(TypedDict):
|
||||
|
||||
@@ -835,6 +835,8 @@ class RemoteService(TitleSource):
|
||||
"""The server's answer to who licenses this remote session; an older server gives it in the tracks reply."""
|
||||
_server_cdm_request: bool | None = None
|
||||
"""This client's own server CDM choice; None leaves it to the server."""
|
||||
_fairplay_bridge = False
|
||||
"""The last tracks reply said the service on the server has the FairPlay bridge hook."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -1451,6 +1453,7 @@ class RemoteService(TitleSource):
|
||||
self.report_local_fallback()
|
||||
self.server_cdm_type = session_fields.get("server_cdm_type", "widevine")
|
||||
self._sample_decrypt_tracks.update(session_fields.get("sample_decrypt_tracks") or [])
|
||||
self._fairplay_bridge = bool(session_fields.get("fairplay_bridge"))
|
||||
|
||||
for track in [*tracks.videos, *tracks.audio, *tracks.subtitles]:
|
||||
if track.descriptor == Track.Descriptor.HLS:
|
||||
@@ -1743,6 +1746,22 @@ class RemoteService(TitleSource):
|
||||
) -> bytes | str | None:
|
||||
return self.proxy_license(challenge, track, "playready", pssh_b64)
|
||||
|
||||
def fairplay_bridge(self) -> Callable[..., Any] | None:
|
||||
"""The relay of the FairPlay bridge, when the tracks reply says the service on the server has the hook."""
|
||||
return self.relay_fairplay_bridge if self._fairplay_bridge else None
|
||||
|
||||
def relay_fairplay_bridge(
|
||||
self,
|
||||
*,
|
||||
challenge: bytes,
|
||||
track: AnyTrack,
|
||||
drm_system: str,
|
||||
pssh: PSSH | None = None,
|
||||
pssh_b64: str | None = None,
|
||||
) -> bytes | str | None:
|
||||
"""Relay a bridged challenge with the PSSH of its header; the server sends it to the hook of its service."""
|
||||
return self.proxy_license(challenge, track, drm_system, pssh.dumps() if pssh else pssh_b64)
|
||||
|
||||
def get_widevine_service_certificate(
|
||||
self,
|
||||
*,
|
||||
|
||||
@@ -223,6 +223,14 @@ class TitleSource(metaclass=ABCMeta):
|
||||
"""
|
||||
return None
|
||||
|
||||
def fairplay_bridge(self) -> Callable[..., Any] | None:
|
||||
"""The service's ``get_fairplay_bridge_license``, or None: only a service with it gets FairPlay-only tracks bridged.
|
||||
|
||||
The hook gets a PlayReady or Widevine challenge for the key IDs of the init segments, with ``challenge``,
|
||||
``title``, ``track``, ``drm_system`` and the extras of that system's licence callback.
|
||||
"""
|
||||
return getattr(self, "get_fairplay_bridge_license", None)
|
||||
|
||||
@abstractmethod
|
||||
def get_titles(self) -> Titles_T:
|
||||
"""The titles of the title ID."""
|
||||
@@ -312,4 +320,16 @@ class TitleSource(metaclass=ABCMeta):
|
||||
"""
|
||||
|
||||
|
||||
__all__ = ("CacheIdentity", "KeyArrival", "ServerKeySource", "TitleSource")
|
||||
def service_licence(source: TitleSource, drm_system: str, **kwargs: Any) -> Any:
|
||||
"""Call the licence callback of ``source`` for a ``drm_system`` challenge, with the ``kwargs`` it declares.
|
||||
|
||||
A track with a bridged stub goes to ``source.fairplay_bridge()`` with ``drm_system``; any other track goes to
|
||||
``get_playready_license`` or ``get_widevine_license``.
|
||||
"""
|
||||
bridged = any(getattr(drm, "bridged", False) for drm in getattr(kwargs.get("track"), "drm", None) or [])
|
||||
hook = source.fairplay_bridge() if bridged else None
|
||||
fn = hook or (source.get_playready_license if drm_system == "playready" else source.get_widevine_license)
|
||||
return fn(**declared_kwargs(fn, {**kwargs, "drm_system": drm_system} if hook else kwargs))
|
||||
|
||||
|
||||
__all__ = ("CacheIdentity", "KeyArrival", "ServerKeySource", "TitleSource", "service_licence")
|
||||
|
||||
@@ -247,6 +247,7 @@ class Subtitle(Track):
|
||||
download_processes: int = 1,
|
||||
on_segments: Callable[[Track, list[Any]], None] | None = None,
|
||||
sample_decrypter: Callable[[int], bytes | SampleDecrypter] | None = None,
|
||||
fairplay_bridge: bool = False,
|
||||
) -> None:
|
||||
super().download(
|
||||
session,
|
||||
@@ -260,6 +261,7 @@ class Subtitle(Track):
|
||||
download_processes=download_processes,
|
||||
on_segments=on_segments,
|
||||
sample_decrypter=sample_decrypter,
|
||||
fairplay_bridge=fairplay_bridge,
|
||||
)
|
||||
if not self.path:
|
||||
return
|
||||
|
||||
@@ -403,6 +403,8 @@ class DownloadContext:
|
||||
"""Called by the HLS step with a track and its wanted media segments, before they download."""
|
||||
sample_decrypter: Callable[[int], bytes | SampleDecrypter] | None = None
|
||||
"""The service's key or `cbcs.SampleDecrypter` for a 1-based `stsd` entry, for a track it decrypts itself."""
|
||||
fairplay_bridge: bool = False
|
||||
"""The service has a FairPlay bridge hook, so the HLS step may license a FairPlay-only track as PlayReady or Widevine."""
|
||||
|
||||
def ensure_session(self) -> AnySession:
|
||||
"""Return the HTTP session, or a new ``Session`` if none was set."""
|
||||
@@ -732,12 +734,13 @@ class Track:
|
||||
download_processes: int = 1,
|
||||
on_segments: Callable[["Track", list[Any]], None] | None = None,
|
||||
sample_decrypter: Callable[[int], bytes | SampleDecrypter] | None = None,
|
||||
fairplay_bridge: bool = False,
|
||||
) -> None:
|
||||
"""Download and optionally Decrypt this Track, then emit TRACK_DOWNLOADED.
|
||||
|
||||
For a URL-descriptor Video or Audio track with no `drm` set, unshackle probes the DRM from the
|
||||
track's init data and stores it on the track, so a service need not declare `drm` itself.
|
||||
`sample_decrypter` decrypts a track the service decrypts itself (see DownloadContext).
|
||||
`sample_decrypter` and `fairplay_bridge` go on the DownloadContext.
|
||||
"""
|
||||
from unshackle.core.manifests import DASH, HLS, ISM, url
|
||||
from unshackle.core.manifests.segment_pipeline import licensing
|
||||
@@ -814,6 +817,7 @@ class Track:
|
||||
cdm=cdm,
|
||||
on_segments=on_segments,
|
||||
sample_decrypter=sample_decrypter,
|
||||
fairplay_bridge=fairplay_bridge,
|
||||
)
|
||||
if self.descriptor in manifest_parsers:
|
||||
manifest_parsers[self.descriptor].download_track(track=self, ctx=ctx)
|
||||
|
||||
@@ -65,6 +65,7 @@ class EXAMPLE(Service):
|
||||
get_widevine_* service certificate + license (per-segment PSSH through `track`)
|
||||
get_playready_license PlayReady challenge POST
|
||||
get_clearkey_license DASH org.w3.clearkey JWK Set POST (Laurl fallback)
|
||||
get_fairplay_bridge_license commented out: FairPlay-only HLS tracks, opt-in only
|
||||
"""
|
||||
|
||||
# ALIASES: extra CLI tags that resolve to this service (e.g. `dl EX ...`).
|
||||
@@ -523,6 +524,18 @@ class EXAMPLE(Service):
|
||||
response.raise_for_status()
|
||||
return response.content
|
||||
|
||||
# unshackle has no FairPlay CDM and never sends a FairPlay challenge. For a FairPlay-only track it can send a
|
||||
# PlayReady or Widevine challenge for the KIDs of the init segments to the licence server of the platform, which
|
||||
# works only when that server issues the same content key to PlayReady or Widevine. Defining this method opts the
|
||||
# service in; a server that does not issue the content key refuses the challenge and can flag the device, so
|
||||
# enable it only after a licence-only run (`--cdm-only --skip-dl`) proves the route.
|
||||
#
|
||||
# def get_fairplay_bridge_license(
|
||||
# self, *, challenge: bytes, title: Title_T, track: AnyTrack, drm_system: str
|
||||
# ) -> bytes | str | None:
|
||||
# licence = self.get_playready_license if drm_system == "playready" else self.get_widevine_license
|
||||
# return licence(challenge=challenge, title=title, track=track)
|
||||
|
||||
def get_clearkey_license(self, *, challenge: bytes, title: Title_T, track: AnyTrack) -> bytes | str | dict | None:
|
||||
# DASH org.w3.clearkey: `challenge` is the W3C JSON license request; return the
|
||||
# JWK Set response. Omit this method entirely when the manifest carries a Laurl:
|
||||
|
||||
Reference in New Issue
Block a user