Add initial Restreamer implementation for YouTube-to-Jellyfin live restreaming
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import asyncio
|
||||
|
||||
from restreamer.broadcaster import ChannelBroadcaster
|
||||
|
||||
|
||||
async def test_publish_fans_out_to_all_subscribers() -> None:
|
||||
broadcaster = ChannelBroadcaster("test")
|
||||
first = broadcaster.subscribe()
|
||||
second = broadcaster.subscribe()
|
||||
|
||||
broadcaster.publish(b"chunk")
|
||||
|
||||
assert await first.get() == b"chunk"
|
||||
assert await second.get() == b"chunk"
|
||||
|
||||
|
||||
async def test_unsubscribe_stops_receiving() -> None:
|
||||
broadcaster = ChannelBroadcaster("test")
|
||||
queue = broadcaster.subscribe()
|
||||
broadcaster.unsubscribe(queue)
|
||||
|
||||
broadcaster.publish(b"chunk")
|
||||
|
||||
assert queue.empty()
|
||||
|
||||
|
||||
async def test_full_queue_drops_slow_subscriber() -> None:
|
||||
broadcaster = ChannelBroadcaster("test")
|
||||
queue = broadcaster.subscribe()
|
||||
|
||||
for _ in range(queue.maxsize + 1):
|
||||
broadcaster.publish(b"x")
|
||||
await asyncio.sleep(0)
|
||||
|
||||
assert broadcaster.subscriber_count == 0
|
||||
@@ -0,0 +1,51 @@
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from restreamer.config import load_channels
|
||||
|
||||
|
||||
def test_load_channels_parses_valid_file(tmp_path: Path) -> None:
|
||||
channels_file = tmp_path / "channels.yaml"
|
||||
channels_file.write_text(
|
||||
"""
|
||||
channels:
|
||||
- id: news
|
||||
name: "News Live"
|
||||
url: "https://www.youtube.com/watch?v=abc123"
|
||||
- id: music
|
||||
name: "Music Live"
|
||||
url: "https://www.youtube.com/watch?v=def456"
|
||||
refresh_interval_seconds: 3600
|
||||
""",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
channels = load_channels(channels_file)
|
||||
|
||||
assert [c.id for c in channels] == ["news", "music"]
|
||||
assert channels[1].refresh_interval_seconds == 3600
|
||||
|
||||
|
||||
def test_load_channels_missing_file_raises(tmp_path: Path) -> None:
|
||||
with pytest.raises(FileNotFoundError):
|
||||
load_channels(tmp_path / "does_not_exist.yaml")
|
||||
|
||||
|
||||
def test_load_channels_rejects_duplicate_ids(tmp_path: Path) -> None:
|
||||
channels_file = tmp_path / "channels.yaml"
|
||||
channels_file.write_text(
|
||||
"""
|
||||
channels:
|
||||
- id: news
|
||||
name: "News Live"
|
||||
url: "https://www.youtube.com/watch?v=abc123"
|
||||
- id: news
|
||||
name: "News Live Again"
|
||||
url: "https://www.youtube.com/watch?v=def456"
|
||||
""",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="Duplicate channel id"):
|
||||
load_channels(channels_file)
|
||||
@@ -0,0 +1,44 @@
|
||||
from restreamer.ffmpeg_runner import build_ffmpeg_args
|
||||
from restreamer.resolver import ResolvedStream
|
||||
|
||||
|
||||
def test_build_ffmpeg_args_single_input_without_headers() -> None:
|
||||
stream = ResolvedStream(video_url="https://example.com/stream.m3u8")
|
||||
|
||||
args = build_ffmpeg_args(stream)
|
||||
|
||||
assert args[0] == "ffmpeg"
|
||||
assert "-headers" not in args
|
||||
assert args.count("-i") == 1
|
||||
assert stream.video_url in args
|
||||
assert args[-1] == "pipe:1"
|
||||
|
||||
|
||||
def test_build_ffmpeg_args_includes_headers_when_present() -> None:
|
||||
stream = ResolvedStream(
|
||||
video_url="https://example.com/stream.m3u8",
|
||||
http_headers={"User-Agent": "test-agent", "Cookie": "a=b"},
|
||||
)
|
||||
|
||||
args = build_ffmpeg_args(stream)
|
||||
|
||||
assert "-headers" in args
|
||||
header_value = args[args.index("-headers") + 1]
|
||||
assert "User-Agent: test-agent\r\n" in header_value
|
||||
assert "Cookie: a=b\r\n" in header_value
|
||||
|
||||
|
||||
def test_build_ffmpeg_args_maps_separate_video_and_audio_inputs() -> None:
|
||||
stream = ResolvedStream(
|
||||
video_url="https://example.com/video.m3u8",
|
||||
audio_url="https://example.com/audio.m3u8",
|
||||
)
|
||||
|
||||
args = build_ffmpeg_args(stream)
|
||||
|
||||
assert args.count("-i") == 2
|
||||
assert stream.video_url in args
|
||||
assert stream.audio_url in args
|
||||
assert "-map" in args
|
||||
map_values = [args[i + 1] for i, a in enumerate(args) if a == "-map"]
|
||||
assert map_values == ["0:v:0", "1:a:0"]
|
||||
@@ -0,0 +1,19 @@
|
||||
from pydantic import HttpUrl
|
||||
|
||||
from restreamer.config import ChannelConfig
|
||||
from restreamer.http_server import build_playlist
|
||||
|
||||
|
||||
def test_build_playlist_lists_all_channels() -> None:
|
||||
channels = [
|
||||
ChannelConfig(id="news", name="News Live", url=HttpUrl("https://www.youtube.com/watch?v=abc123")),
|
||||
ChannelConfig(id="music", name="Music Live", url=HttpUrl("https://www.youtube.com/watch?v=def456")),
|
||||
]
|
||||
|
||||
playlist = build_playlist(channels, "http://localhost:8000/")
|
||||
|
||||
assert playlist.startswith("#EXTM3U\n")
|
||||
assert 'tvg-id="news"' in playlist
|
||||
assert "http://localhost:8000/stream/news" in playlist
|
||||
assert "http://localhost:8000/stream/music" in playlist
|
||||
assert "http://localhost:8000//stream" not in playlist
|
||||
@@ -0,0 +1,23 @@
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from restreamer.resolver import parse_expiry_from_url
|
||||
|
||||
|
||||
def test_parse_expiry_from_url_reads_expire_param() -> None:
|
||||
url = "https://r.googlevideo.com/videoplayback?id=abc&expire=1700000000&itag=96"
|
||||
|
||||
result = parse_expiry_from_url(url)
|
||||
|
||||
assert result == datetime.fromtimestamp(1700000000, tz=UTC)
|
||||
|
||||
|
||||
def test_parse_expiry_from_url_returns_none_without_param() -> None:
|
||||
url = "https://example.com/stream.m3u8?token=abc"
|
||||
|
||||
assert parse_expiry_from_url(url) is None
|
||||
|
||||
|
||||
def test_parse_expiry_from_url_ignores_non_numeric_value() -> None:
|
||||
url = "https://example.com/stream.m3u8?expire=not-a-number"
|
||||
|
||||
assert parse_expiry_from_url(url) is None
|
||||
Reference in New Issue
Block a user