From 2ad3f04a5ebbe4bc8598eaf1f4e6cb5aa6e0553d Mon Sep 17 00:00:00 2001
From: rlaphoenix <rlaphoenix@pm.me>
Date: Wed, 22 Feb 2023 04:30:24 +0000
Subject: [PATCH] Use the session DRM as the initial DRM for HLS

The variant-playlist may have DRM information that won't be in the invariant (stream) playlist. We need to begin with this DRM data if available.
---
 devine/core/manifests/hls.py | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/devine/core/manifests/hls.py b/devine/core/manifests/hls.py
index 20a1938..7baca7e 100644
--- a/devine/core/manifests/hls.py
+++ b/devine/core/manifests/hls.py
@@ -299,11 +299,22 @@ class HLS:
                 if callable(track.OnDecrypted):
                     track.OnDecrypted(track)
 
-        init_data = Queue(maxsize=1)
         segment_key = Queue(maxsize=1)
-        # otherwise will be stuck waiting on the first pool, forever
+        init_data = Queue(maxsize=1)
+
+        if track.drm:
+            session_drm = track.drm[0]  # just use the first supported DRM system for now
+            if isinstance(session_drm, Widevine):
+                # license and grab content keys
+                if not license_widevine:
+                    raise ValueError("license_widevine func must be supplied to use Widevine DRM")
+                license_widevine(session_drm)
+        else:
+            session_drm = None
+
+        # have data to begin with, or it will be stuck waiting on the first pool forever
+        segment_key.put((session_drm, None))
         init_data.put(None)
-        segment_key.put((None, None))
 
         with tqdm(total=len(master.segments), unit="segments") as pbar:
             with ThreadPoolExecutor(max_workers=16) as pool: