Files
unshackle/unshackle/core/cdm/detect.py
T

94 lines
3.3 KiB
Python

"""What a built CDM object is: its DRM system and whether its device runs on another machine.
unshackle's own CDM classes and stand-ins carry ``is_playready``; a pywidevine or pyplayready CDM
is known by its class; a plugin CDM carries the plugin id as ``drm_system``. For a device name in config,
see ``loader.find_device``.
"""
from __future__ import annotations
from collections.abc import Mapping
from types import SimpleNamespace
from typing import Any
from unshackle.core import plugins
def cdm_type_stub(
cdm_type: str, security_level: int | None = None, facts: Mapping[str, Any] | None = None
) -> SimpleNamespace:
"""A CDM stand-in that only carries its DRM system, for when another machine holds the device.
``security_level`` lets a service pick a robustness tier; the attribute is absent when unknown.
A plugin id gives a stand-in of that plugin's system, with the ``facts`` that the plugin names in ``device_facts``.
"""
stub = SimpleNamespace(is_playready=cdm_type == "playready")
if plugin := plugins.get(cdm_type):
stub.drm_system = cdm_type
if facts is not None:
for name in plugin.device_facts:
setattr(stub, name, facts.get(name))
if security_level is not None:
stub.security_level = security_level
return stub
def plugin_device(cdm_for: Any, plugin_id: str) -> Any:
"""The device that ``cdm_for`` gives for the DRM plugin ``plugin_id``; None when it gives none of that plugin."""
cdm = cdm_for(plugin_id) if cdm_for else None
return cdm if cdm_system(cdm) == plugin_id else None
def cdm_system(cdm: Any) -> str | None:
"""``playready``, ``widevine`` or a plugin id for a CDM object; None for no CDM or one of no known system."""
if cdm is None:
return None
plugin_system = getattr(cdm, "drm_system", None)
if isinstance(plugin_system, str):
return plugin_system
playready = getattr(cdm, "is_playready", None)
if isinstance(playready, bool):
return "playready" if playready else "widevine"
from pyplayready.cdm import Cdm as PlayReadyCdm
from pywidevine.cdm import Cdm as WidevineCdm
if isinstance(cdm, PlayReadyCdm):
return "playready"
if isinstance(cdm, WidevineCdm):
return "widevine"
return None
def cdm_label(cdm: Any) -> str:
"""The display name of a CDM's DRM system: ``PlayReady``, the plugin's label, else ``Widevine``."""
system = cdm_system(cdm)
plugin = plugins.get(system)
return plugin.label if plugin else "PlayReady" if system == "playready" else "Widevine"
def is_playready_cdm(cdm: Any) -> bool:
"""True for a PlayReady CDM."""
return cdm_system(cdm) == "playready"
def is_widevine_cdm(cdm: Any) -> bool:
"""True for a Widevine CDM."""
return cdm_system(cdm) == "widevine"
def is_remote_cdm(cdm: Any) -> bool:
"""True for a CDM whose device runs on another machine."""
if getattr(cdm, "is_remote_cdm", False):
return True
from pyplayready.remote.remotecdm import RemoteCdm as PlayReadyRemoteCdm
from pywidevine.remotecdm import RemoteCdm as WidevineRemoteCdm
return isinstance(cdm, (PlayReadyRemoteCdm, WidevineRemoteCdm))
def is_remote_playready_cdm(cdm: Any) -> bool:
"""True for a PlayReady CDM whose device runs on another machine."""
return is_playready_cdm(cdm) and is_remote_cdm(cdm)