feat(tracks): resume segment decryption and rolling merge through the segment store

This commit is contained in:
imSp4rky
2026-11-28 22:09:44 -07:00
parent f4d178c26b
commit 82991dd823
26 changed files with 1125 additions and 186 deletions
+65 -4
View File
@@ -37,6 +37,7 @@ SEGMENTS = [
]
RESUMABLE = DownloadMode()
KID = UUID(int=1)
KEY_ARGS = ["--key", f"{KID.hex}:{'00' * 16}"]
def make_track(descriptor: Video.Descriptor = Video.Descriptor.DASH) -> Video:
@@ -115,7 +116,7 @@ def test_a_mode_that_cannot_resume_withdraws_the_sidecar(tmp_path, keep):
store.open(MPD, SEGMENTS, mode=RESUMABLE)
(store.save_dir / "0.mp4").write_bytes(b"decrypted in place")
assert store.open(MPD, SEGMENTS, mode=DownloadMode(segment_decrypt=True)) is False
assert store.open(MPD, SEGMENTS, mode=DownloadMode(rolling_merge=True, stateful_merge=True)) is False
assert not store.sidecar.exists()
assert store.open(MPD, SEGMENTS, mode=RESUMABLE) is False
@@ -124,11 +125,14 @@ def test_a_mode_that_cannot_resume_withdraws_the_sidecar(tmp_path, keep):
"descriptor, mode, resumable",
[
(Video.Descriptor.DASH, RESUMABLE, True),
(Video.Descriptor.DASH, DownloadMode(segment_decrypt=True), False),
(Video.Descriptor.DASH, DownloadMode(rolling_merge=True), False),
(Video.Descriptor.DASH, DownloadMode(segment_keys=KEY_ARGS), True),
(Video.Descriptor.DASH, DownloadMode(rolling_merge=True), True),
(Video.Descriptor.DASH, DownloadMode(rolling_merge=True, stateful_merge=True), False),
(Video.Descriptor.DASH, DownloadMode(stateful_merge=True), True),
(Video.Descriptor.DASH, DownloadMode(drm=ClearKeyCENC(kids=[KID])), True),
(Video.Descriptor.ISM, RESUMABLE, True),
(Video.Descriptor.ISM, DownloadMode(rolling_merge=True), False),
(Video.Descriptor.ISM, DownloadMode(rolling_merge=True), True),
(Video.Descriptor.HLS, DownloadMode(segment_keys=KEY_ARGS, rolling_merge=True, drm=widevine()), True),
(Video.Descriptor.HLS, RESUMABLE, True),
(Video.Descriptor.HLS, DownloadMode(drm=widevine()), True),
(Video.Descriptor.HLS, DownloadMode(aes128=True), False),
@@ -139,9 +143,12 @@ def test_a_mode_that_cannot_resume_withdraws_the_sidecar(tmp_path, keep):
"dash",
"dash-segment-decrypt",
"dash-rolling",
"dash-rolling-stateful",
"dash-stateful-post-merge",
"dash-clearkey-cenc",
"ism",
"ism-rolling",
"hls-segment-decrypt-rolling",
"hls",
"hls-widevine",
"hls-aes128",
@@ -215,6 +222,60 @@ def test_sidecar_is_never_inside_save_dir(tmp_path):
assert store.sidecar.parent == store.save_dir.parent
def test_open_keeps_decrypted_segments_only_for_the_same_keys(tmp_path, keep):
keys = DownloadMode(segment_keys=KEY_ARGS)
store = make_store(tmp_path)
store.open(MPD, SEGMENTS, mode=keys)
(store.save_dir / "0.clear").write_bytes(b"decrypted")
(store.save_dir / "0.mp4").write_bytes(b"left by a stopped decrypt")
(store.save_dir / "1.mp4").write_bytes(b"downloaded")
again = make_store(tmp_path)
assert again.open(MPD, SEGMENTS, mode=keys) is True
assert again.clear == {0: store.save_dir / "0.clear"} and again.skip == {0}
again.merging()
assert sorted(p.name for p in store.save_dir.iterdir()) == ["0.clear", "1.mp4"]
(store.save_dir / "0.mp4").write_bytes(b"left by a stopped decrypt")
other = make_store(tmp_path)
assert other.open(MPD, SEGMENTS, mode=DownloadMode(segment_keys=["--key", "other"])) is True
assert other.clear == {} and other.skip == frozenset()
assert sorted(p.name for p in store.save_dir.iterdir()) == ["0.mp4", "1.mp4"]
@pytest.mark.parametrize(
"mode, output, cursor",
[
(DownloadMode(rolling_merge=True), b"x" * 50, 2),
(DownloadMode(rolling_merge=True), b"x" * 49, 0),
(DownloadMode(), b"x" * 50, 0),
(DownloadMode(rolling_merge=True, segment_keys=KEY_ARGS), b"x" * 50, 0),
],
ids=["kept", "short-output", "no-rolling-merge", "other-keys"],
)
def test_a_checkpoint_counts_only_for_its_merge(tmp_path, keep, mode, output, cursor):
store = make_store(tmp_path)
store.open(MPD, SEGMENTS, mode=DownloadMode(rolling_merge=True))
for i in range(3):
(store.save_dir / f"{i}.mp4").write_bytes(b"segment")
(tmp_path / "Video_x.mp4").write_bytes(output)
assert store.checkpoint(2, 50)
again = make_store(tmp_path)
assert again.open(MPD, SEGMENTS, mode=mode) is True
assert (again.cursor, again.skip) == (cursor, frozenset(range(cursor)))
assert json.loads(again.sidecar.read_text()).get("merge") == ([2, 50] if cursor else None)
assert sorted(p.name for p in store.save_dir.iterdir()) == [f"{i}.mp4" for i in range(cursor, 3)]
def test_a_store_that_keeps_nothing_records_no_checkpoint(tmp_path, monkeypatch):
monkeypatch.setattr(config, "continue_downloads", False)
store = make_store(tmp_path)
store.open(MPD, SEGMENTS, mode=DownloadMode(rolling_merge=True))
assert store.checkpoint(1, 10) is False
assert not store.sidecar.exists()
@pytest.fixture
def one_attempt(monkeypatch, keep):
monkeypatch.setattr(config, "merge_segments", False)