feat(hls): carry a held media playlist over --remote and export, inflate gzip segments, add own_stream_only mux opt-in

This commit is contained in:
imSp4rky
2026-12-02 11:12:02 -07:00
parent ccdd14c845
commit 9842a61d3f
27 changed files with 404 additions and 62 deletions
+109
View File
@@ -0,0 +1,109 @@
"""An HLS track whose media playlist is held as text, or read from a file, over a remote round trip and an export.
The server side serializes the tracks reply with its manifest snapshot, the client side rebuilds the tracks from
the JSON and downloads them from a loopback server.
"""
from __future__ import annotations
import json
import logging
from pathlib import Path
import pytest
import requests
from tests.core.test_rolling_merge_hls_ism import INIT, PARTS, base, make_ctx, no_cancel, server # noqa: F401
from tests.core.test_wire import json_copy, movie
from unshackle.core.import_service import resolve_import_manifest_data
from unshackle.core.manifests import HLS
from unshackle.core.manifests.snapshot import capture
from unshackle.core.tracks import Tracks, Video
from unshackle.core.wire import apply_tracks_reply, serialize_tracks_reply, track_from_dict
def media_playlist(prefix: str = "") -> str:
lines = ["#EXTM3U", "#EXT-X-VERSION:6", "#EXT-X-TARGETDURATION:4", f'#EXT-X-MAP:URI="{prefix}init.mp4"']
for i in range(len(PARTS)):
lines += ["#EXTINF:4.0,", f"{prefix}part{i}.m4s"]
return "\n".join([*lines, "#EXT-X-ENDLIST"])
def video(url: str, **kwargs) -> Video:
return Video(
url,
"en",
descriptor=Video.Descriptor.HLS,
codec=Video.Codec.AVC,
width=1920,
height=1080,
bitrate=1_000_000,
id_="v1",
**kwargs,
)
def held(srv, text: str | None = None) -> Video:
return video(base(srv), data={"hls": {"playlist_text": text or media_playlist()}})
def from_file(tmp_path: Path, text: str) -> Video:
path = tmp_path / "server" / "Video_v1.m3u8"
path.parent.mkdir()
path.write_text(text, encoding="utf-8")
return video(path.as_uri(), from_file=path)
def round_trip(track: Video) -> Tracks:
"""The tracks a remote client rebuilds from the tracks reply of a server that holds ``track``."""
tracks = Tracks(track)
reply = json_copy({**serialize_tracks_reply(movie(), tracks, [], None, lambda data: data), **capture(tracks)})
assert "file:" not in json.dumps(reply)
return apply_tracks_reply(reply, movie(), requests.Session())
def downloaded(track: Video, tmp_path: Path) -> bytes:
ctx = make_ctx(tmp_path, "out.mp4")
HLS.download_track(track, ctx)
return ctx.save_path.read_bytes()
def test_a_held_playlist_downloads_on_the_client(server, tmp_path): # noqa: F811
client = round_trip(held(server))
assert downloaded(client.videos[0], tmp_path) == INIT + b"".join(PARTS)
def test_a_from_file_playlist_downloads_on_the_client(server, tmp_path): # noqa: F811
client = round_trip(from_file(tmp_path, media_playlist(base(server))))
assert downloaded(client.videos[0], tmp_path) == INIT + b"".join(PARTS)
def test_an_export_import_keeps_the_held_playlist(server, tmp_path): # noqa: F811
track = held(server)
imported = Tracks(track_from_dict(json_copy(track.to_dict())))
saved = json_copy(capture(Tracks(track)))
resolve_import_manifest_data(imported, "HLS", session=requests.Session(), language="en", title=movie(), saved=saved)
assert downloaded(imported.videos[0], tmp_path) == INIT + b"".join(PARTS)
def test_the_client_takes_no_playlist_from_the_service_data(server): # noqa: F811
tracks = Tracks(held(server))
reply = json_copy({**serialize_tracks_reply(movie(), tracks, [], None, lambda data: data), **capture(tracks)})
reply["hls_playlists"] = []
reply["video"][0]["data"] = {"hls": {"playlist_text": media_playlist("file:///etc/")}}
assert apply_tracks_reply(reply, movie(), requests.Session()).videos[0].held_playlist() is None
@pytest.mark.parametrize(
"make",
[
pytest.param(lambda srv, tmp: from_file(tmp, media_playlist()), id="relative-to-a-server-file"),
pytest.param(lambda srv, tmp: held(srv, media_playlist("file:///etc/")), id="file-segment"),
pytest.param(lambda srv, tmp: held(srv, "#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1\nm.m3u8\n"), id="variant"),
],
)
def test_the_client_refuses_a_playlist_it_must_not_download(make, server, tmp_path, caplog): # noqa: F811
with caplog.at_level(logging.WARNING):
client = round_trip(make(server, tmp_path))
assert not client.videos
assert [r.getMessage() for r in caplog.records if r.name == "snapshot"][0].startswith("Refusing track v1: ")