feat(drm): license a plugin DRM at the licence step and over --remote with the client's plugin device

This commit is contained in:
imSp4rky
2026-12-03 15:51:42 -07:00
parent f1f91a7d5c
commit 0536056ed6
24 changed files with 803 additions and 136 deletions
+63 -23
View File
@@ -44,7 +44,7 @@ from unshackle.core.config import config
from unshackle.core.console import SyncLive, console
from unshackle.core.constants import AnyTrack, DownloadCancelled, RunCancel
from unshackle.core.credential import Credential
from unshackle.core.drm import CustomDRM, cbcs, real_kids
from unshackle.core.drm import CustomDRM, cbcs, custom_drm, real_kids
from unshackle.core.drm.cenc import PLACEHOLDER_KIDS
from unshackle.core.drm.discovery import track_pssh
from unshackle.core.launch import launch_keys
@@ -52,6 +52,7 @@ from unshackle.core.manifests.snapshot import match_track
from unshackle.core.proxies.resolve import is_loopback
from unshackle.core.remote_protocol import (
BAD_KEY_ROUTE,
DEVICE_FACT_TYPES,
HEALTH_ROUTE,
KEYS_ROUTE,
LICENSE_ROUTE,
@@ -69,6 +70,7 @@ from unshackle.core.remote_protocol import (
BadKeyReport,
ClientErrorKind,
ClientLogin,
ClientPlugin,
LicenseReply,
LicenseRequest,
LicensingMode,
@@ -123,6 +125,35 @@ SESSION_LOST = (
)
# what a recovering call can meet on its way, so these log only once the recovery fails
RECOVERABLE = (*SESSION_LOST, APIErrorCode.SERVER_DRAINING)
def session_plugins(cdm_for: Callable[..., Any] | None) -> dict[str, ClientPlugin]:
"""Each DRM plugin of this machine for a remote session create: its version, and the facts of its device.
A fact is the plain value of an attribute that the plugin names in ``device_facts``, never a path or a device file.
"""
from unshackle.core import plugins
from unshackle.core.cdm.detect import plugin_device
listed: dict[str, ClientPlugin] = {}
for plugin in plugins.plugins().values():
entry = ClientPlugin(version=plugin.version)
if plugin.device_facts:
try:
device = plugin_device(cdm_for, plugin.id)
except Exception as e:
log.warning(f"The {plugin.label} device did not load: {e}")
device = None
if device is not None:
entry["device"] = {
name: value
for name in plugin.device_facts
if isinstance(value := getattr(device, name, None), DEVICE_FACT_TYPES)
}
listed[plugin.id] = entry
return listed
RETRY_AFTER_MAX = 3600
RETRY_AFTER_LOCK = Lock()
RATE_LIMIT_TRIES = 3
@@ -1154,6 +1185,7 @@ class RemoteService(TitleSource):
**login,
**route,
**session_request(params, self._service_params),
"plugins": session_plugins(getattr(self.ctx.obj, "cdm_for", None) if cdm is not None else None),
}
if cdm is not None:
from unshackle.core.cdm.detect import cdm_system
@@ -1625,7 +1657,7 @@ class RemoteService(TitleSource):
media playlist names the DRM and the downloader honours ``drm_preference``. On the server's device
there is no such licence: the error names ``kid``, else each KID of ``drm`` with no content key.
"""
from unshackle.core.cdm.detect import cdm_label, cdm_system
from unshackle.core.cdm.detect import cdm_label, cdm_system, plugin_device
if refusal.get("reason") == "server_device":
vaults = (
@@ -1654,29 +1686,37 @@ class RemoteService(TitleSource):
why = "The server vault holds no content key for this track"
else:
why = "The server CDM refused this track"
cdm = self.local_cdm
if cdm is None:
raise coded(
click.ClickException(f"{why}, and {track} needs a local CDM to license it"), APIErrorCode.DRM_ERROR
)
drm_class = cdm_label(cdm)
height = getattr(track, "height", None)
systems = {d.__class__.__name__ for d in track.drm or ()}
if systems and drm_class not in systems:
other = "Widevine" if drm_class == "PlayReady" else "PlayReady"
if not (
other in systems
and self.ctx.obj.cdm_for
and cdm_system(self.ctx.obj.cdm_for(other.lower(), height)) == other.lower()
):
label = f"the {height}p track" if height else str(track)
hint = f"; pass -q {height} to start the session on your device" if cap and height else ""
have = " and ".join(sorted(systems))
raise click.ClickException(
f"{why}, and {label} has only {have} DRM, which your local {drm_class} CDM cannot license{hint}"
if (custom := custom_drm(track)) is not None:
drm_class = custom.label
if plugin_device(self.ctx.obj.cdm_for, custom.plugin_id) is None:
raise coded(
click.ClickException(f"{why}, and {track} needs a {drm_class} device to license it"),
APIErrorCode.DRM_ERROR,
)
drm_class = other
track.drm_preference = drm_class.lower()
else:
cdm = self.local_cdm
if cdm is None:
raise coded(
click.ClickException(f"{why}, and {track} needs a local CDM to license it"), APIErrorCode.DRM_ERROR
)
drm_class = cdm_label(cdm)
systems = {d.__class__.__name__ for d in track.drm or ()}
if systems and drm_class not in systems:
other = "Widevine" if drm_class == "PlayReady" else "PlayReady"
if not (
other in systems
and self.ctx.obj.cdm_for
and cdm_system(self.ctx.obj.cdm_for(other.lower(), height)) == other.lower()
):
label = f"the {height}p track" if height else str(track)
hint = f"; pass -q {height} to start the session on your device" if cap and height else ""
have = " and ".join(sorted(systems))
raise click.ClickException(
f"{why}, and {label} has only {have} DRM, which your local {drm_class} CDM cannot license{hint}"
)
drm_class = other
track.drm_preference = drm_class.lower()
self.client_licensed.add(str(track.id))
from unshackle.core.api.progress import track_log_label