Compare commits
8
Commits
a0b38c48e2
...
devel
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
79c66d2c11 | ||
|
|
fc4ed4fc47 | ||
|
|
21176d7f64 | ||
|
|
9243d2f670 | ||
|
|
6f71dbf68c | ||
|
|
fb6cb0baa2 | ||
|
|
db35c84cdb | ||
|
|
4fbb3e813e |
@@ -0,0 +1,106 @@
|
|||||||
|
name: CI
|
||||||
|
|
||||||
|
# Spustí se při pushi do devel/release a jde i ručně spustit z GUI.
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [devel, release]
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: amd64
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- uses: actions/setup-python@v5
|
||||||
|
with:
|
||||||
|
python-version: "3.12"
|
||||||
|
|
||||||
|
- name: Instalace Poetry
|
||||||
|
run: pip install poetry==2.4.1
|
||||||
|
|
||||||
|
- name: Instalace závislostí (vč. dev skupiny)
|
||||||
|
run: poetry install --no-interaction
|
||||||
|
|
||||||
|
- name: Ruff
|
||||||
|
run: poetry run ruff check .
|
||||||
|
|
||||||
|
- name: Mypy
|
||||||
|
run: poetry run mypy src
|
||||||
|
|
||||||
|
- name: Testy
|
||||||
|
run: poetry run pytest -q
|
||||||
|
|
||||||
|
# Každá architektura se staví NATIVNĚ na vlastním runneru (label = architektura):
|
||||||
|
# amd64 -> srv2-amd64-runner (SRV2), arm64 -> srv1-arm64-runner (SRV1, RPi 4).
|
||||||
|
# Žádná emulace (QEMU). Každý běh pushne image s příponou architektury.
|
||||||
|
build:
|
||||||
|
needs: test
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
arch: [amd64, arm64]
|
||||||
|
runs-on: ${{ matrix.arch }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
# Docker jméno musí být malými písmeny (Honza/Restreamer -> honza/restreamer)
|
||||||
|
- name: Název image
|
||||||
|
run: |
|
||||||
|
echo "IMAGE=gitea.doubynet.eu/$(echo '${{ github.repository }}' \
|
||||||
|
| tr '[:upper:]' '[:lower:]')" >> "$GITHUB_ENV"
|
||||||
|
|
||||||
|
# release -> latest (produkce), devel -> devel (staging)
|
||||||
|
- name: Značka podle větve
|
||||||
|
run: |
|
||||||
|
if [ "${{ github.ref_name }}" = "release" ]; then
|
||||||
|
echo "MOVING_TAG=latest" >> "$GITHUB_ENV"
|
||||||
|
else
|
||||||
|
echo "MOVING_TAG=devel" >> "$GITHUB_ENV"
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Přihlášení do Gitea registry
|
||||||
|
run: |
|
||||||
|
echo '${{ secrets.REGISTRY_TOKEN }}' \
|
||||||
|
| docker login gitea.doubynet.eu -u '${{ github.actor }}' --password-stdin
|
||||||
|
|
||||||
|
# --provenance=false: bez atestace je per-arch tag obyčejný obrázek. S ní by to byl
|
||||||
|
# index (obrázek + atestace) a `docker manifest create` index do indexu nespojí.
|
||||||
|
- name: Build image (${{ matrix.arch }})
|
||||||
|
run: |
|
||||||
|
docker build --provenance=false \
|
||||||
|
-t "$IMAGE:$MOVING_TAG-${{ matrix.arch }}" \
|
||||||
|
-t "$IMAGE:${{ github.sha }}-${{ matrix.arch }}" .
|
||||||
|
|
||||||
|
- name: Push image (${{ matrix.arch }})
|
||||||
|
run: |
|
||||||
|
docker push "$IMAGE:$MOVING_TAG-${{ matrix.arch }}"
|
||||||
|
docker push "$IMAGE:${{ github.sha }}-${{ matrix.arch }}"
|
||||||
|
|
||||||
|
# Spojí obě architektury pod jedno jméno tagu. `docker pull ...:devel` pak na
|
||||||
|
# amd64 i arm64 stroji sám vybere správnou variantu.
|
||||||
|
manifest:
|
||||||
|
needs: build
|
||||||
|
runs-on: amd64
|
||||||
|
steps:
|
||||||
|
- name: Název image a značka
|
||||||
|
run: |
|
||||||
|
echo "IMAGE=gitea.doubynet.eu/$(echo '${{ github.repository }}' \
|
||||||
|
| tr '[:upper:]' '[:lower:]')" >> "$GITHUB_ENV"
|
||||||
|
if [ "${{ github.ref_name }}" = "release" ]; then
|
||||||
|
echo "MOVING_TAG=latest" >> "$GITHUB_ENV"
|
||||||
|
else
|
||||||
|
echo "MOVING_TAG=devel" >> "$GITHUB_ENV"
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Přihlášení do Gitea registry
|
||||||
|
run: |
|
||||||
|
echo '${{ secrets.REGISTRY_TOKEN }}' \
|
||||||
|
| docker login gitea.doubynet.eu -u '${{ github.actor }}' --password-stdin
|
||||||
|
|
||||||
|
- name: Multi-arch manifest (moving tag + commit SHA)
|
||||||
|
run: |
|
||||||
|
for TAG in "$MOVING_TAG" "${{ github.sha }}"; do
|
||||||
|
docker manifest create --amend "$IMAGE:$TAG" \
|
||||||
|
"$IMAGE:$TAG-amd64" "$IMAGE:$TAG-arm64"
|
||||||
|
docker manifest push "$IMAGE:$TAG"
|
||||||
|
done
|
||||||
@@ -1,24 +1,21 @@
|
|||||||
# AI Agents - Project Rules
|
# AI Agents - Project Rules
|
||||||
|
|
||||||
**Document Version:** v8 (independent, incremented on structural changes)
|
**Document Version:** v9 (independent, incremented on structural changes)
|
||||||
|
|
||||||
Language-agnostic rules for AI assistants (Claude Code, Cursor, Copilot, etc.).
|
Language-agnostic rules for AI assistants (Claude Code, Cursor, Copilot, etc.).
|
||||||
|
|
||||||
Everything language-specific — package manager, formatter, linter, test framework,
|
Everything specific — package manager, formatter, linter, test framework, logging library,
|
||||||
logging library, line length, project layout — lives in the matching design document:
|
line length, project layout, interface conventions — lives in the design documents.
|
||||||
|
|
||||||
| Project type | Design document |
|
**Find them, do not look them up in a list:** every file matching `DESIGN_DOCUMENT*.md` in the
|
||||||
|--------------------|------------------------------------|
|
project root applies to this project. The suffix says what it covers — the language
|
||||||
| Python application | `DESIGN_DOCUMENT_PYTHON.md` |
|
(`DESIGN_DOCUMENT_PYTHON.md`), whether it is an application or a library
|
||||||
| Python library | `DESIGN_DOCUMENT_PYTHON_MODULE.md` |
|
(`..._PYTHON_MODULE.md`), or a part of the system that has conventions of its own
|
||||||
| Rust application | `DESIGN_DOCUMENT_RUST.md` |
|
(`..._WEBAPP.md`). A project carries as many as it needs and nothing collides, so no list here
|
||||||
| Rust library | `DESIGN_DOCUMENT_RUST_LIB.md` |
|
needs maintaining and none can go stale.
|
||||||
| Godot | `DESIGN_DOCUMENT_GODOT.md` |
|
|
||||||
|
|
||||||
The language is part of the file name, so a project that mixes languages carries one design document
|
Where this file and a design document disagree, **the design document wins**. Where two design
|
||||||
per language and nothing collides.
|
documents disagree, the more specific one wins for what it covers.
|
||||||
|
|
||||||
Where this file and a design document disagree, **the design document wins**.
|
|
||||||
|
|
||||||
## First-time setup
|
## First-time setup
|
||||||
|
|
||||||
|
|||||||
@@ -12,5 +12,22 @@ All notable changes to this project are documented in this file.
|
|||||||
and per-channel MPEG-TS streams for Jellyfin Live TV.
|
and per-channel MPEG-TS streams for Jellyfin Live TV.
|
||||||
- Docker Compose deployment (`Dockerfile`, `docker-compose.yml`) and configuration templates
|
- Docker Compose deployment (`Dockerfile`, `docker-compose.yml`) and configuration templates
|
||||||
(`channels.example.yaml`, `.env.example`).
|
(`channels.example.yaml`, `.env.example`).
|
||||||
|
- Gitea Actions CI (`.gitea/workflows/ci.yml`): ruff, mypy and pytest, then native `amd64` and `arm64` image builds on
|
||||||
|
separate runners (no emulation), merged into one multi-arch tag (`:devel` from `devel`, `:latest` from `release`,
|
||||||
|
plus a commit-SHA tag). Per-architecture tags carry an `-amd64` / `-arm64` suffix.
|
||||||
- Unit test suite covering config parsing, URL expiry parsing, ffmpeg argument building, playlist generation, and
|
- Unit test suite covering config parsing, URL expiry parsing, ffmpeg argument building, playlist generation, and
|
||||||
broadcast fan-out.
|
broadcast fan-out.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- Stopping ffmpeg (on every proactive URL refresh) no longer stalls. The worker stops reading ffmpeg's stdout when it
|
||||||
|
breaks out to refresh, and asyncio's `Process.wait()` does not return until every pipe reaches EOF, so `stop()` always
|
||||||
|
ran into the terminate timeout ("did not exit in time, killing it") and could leave a channel worker stuck for good,
|
||||||
|
silently ending that channel's stream. `stop()` now drains the leftover stdout while waiting for ffmpeg to exit.
|
||||||
|
- `/stream/{channel_id}` now forces the connection closed after each response (`Connection: close`) instead of
|
||||||
|
allowing keep-alive. Jellyfin's M3U tuner client (`SharedHttpStream`) was reusing a pooled keep-alive connection
|
||||||
|
still mid-stream from an earlier request and reading raw MPEG-TS bytes as if they were a fresh HTTP response,
|
||||||
|
failing with "Received an invalid status line".
|
||||||
|
- The yt-dlp resolver now correctly classifies which of the two `requested_formats` entries is audio when a live
|
||||||
|
stream is split into separate video-only and audio-only renditions; it previously relied on `acodec` being set,
|
||||||
|
which yt-dlp does not consistently do for audio-only formats, causing every resolve to fail.
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ Initial implementation in place:
|
|||||||
aiohttp server (`/playlist.m3u`, `/stream/{channel_id}`, `/healthz`), config loading.
|
aiohttp server (`/playlist.m3u`, `/stream/{channel_id}`, `/healthz`), config loading.
|
||||||
- `serve.py` — entry point.
|
- `serve.py` — entry point.
|
||||||
- `Dockerfile` / `docker-compose.yml` — containerized deployment.
|
- `Dockerfile` / `docker-compose.yml` — containerized deployment.
|
||||||
|
- `.gitea/workflows/ci.yml` — CI: tests, then native `amd64` + `arm64` builds (one runner per architecture) merged
|
||||||
|
into a single multi-arch image in the Gitea registry.
|
||||||
- `channels.example.yaml`, `.env.example` — configuration templates.
|
- `channels.example.yaml`, `.env.example` — configuration templates.
|
||||||
- Unit tests for the pure/testable pieces (config parsing, URL expiry parsing, ffmpeg argument building, playlist
|
- Unit tests for the pure/testable pieces (config parsing, URL expiry parsing, ffmpeg argument building, playlist
|
||||||
generation, broadcast fan-out).
|
generation, broadcast fan-out).
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "restreamer"
|
name = "restreamer"
|
||||||
version = "0.1.0"
|
version = "1.0.0"
|
||||||
description = "Restreams YouTube livestreams as continuous local streams consumable by Jellyfin Live TV"
|
description = "Restreams YouTube livestreams as continuous local streams consumable by Jellyfin Live TV"
|
||||||
authors = [
|
authors = [
|
||||||
{name = "Jan Doubravsky",email = "jan.doubravsky@gmail.com"}
|
{name = "Jan Doubravsky",email = "jan.doubravsky@gmail.com"}
|
||||||
|
|||||||
@@ -74,6 +74,10 @@ class FfmpegStreamProcess:
|
|||||||
if self._process is None or self._process.returncode is not None:
|
if self._process is None or self._process.returncode is not None:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# wait() only returns once every pipe hits EOF, so unread stdout left over
|
||||||
|
# from the consumer breaking out of iter_chunks() must be drained, or it hangs forever.
|
||||||
|
stdout_drain_task = asyncio.create_task(self._drain_stdout())
|
||||||
|
|
||||||
self._process.terminate()
|
self._process.terminate()
|
||||||
try:
|
try:
|
||||||
await asyncio.wait_for(self._process.wait(), timeout=FFMPEG_TERMINATE_TIMEOUT_SECONDS)
|
await asyncio.wait_for(self._process.wait(), timeout=FFMPEG_TERMINATE_TIMEOUT_SECONDS)
|
||||||
@@ -82,9 +86,16 @@ class FfmpegStreamProcess:
|
|||||||
self._process.kill()
|
self._process.kill()
|
||||||
await self._process.wait()
|
await self._process.wait()
|
||||||
|
|
||||||
|
await stdout_drain_task
|
||||||
if self._stderr_task is not None:
|
if self._stderr_task is not None:
|
||||||
await self._stderr_task
|
await self._stderr_task
|
||||||
|
|
||||||
|
async def _drain_stdout(self) -> None:
|
||||||
|
if self._process is None or self._process.stdout is None:
|
||||||
|
return
|
||||||
|
while await self._process.stdout.read(FFMPEG_READ_CHUNK_SIZE):
|
||||||
|
pass
|
||||||
|
|
||||||
async def _drain_stderr(self) -> None:
|
async def _drain_stderr(self) -> None:
|
||||||
if self._process is None or self._process.stderr is None:
|
if self._process is None or self._process.stderr is None:
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -42,6 +42,10 @@ async def _handle_stream(request: web.Request) -> web.StreamResponse:
|
|||||||
raise web.HTTPNotFound(text=f"Unknown channel '{channel_id}'")
|
raise web.HTTPNotFound(text=f"Unknown channel '{channel_id}'")
|
||||||
|
|
||||||
response = web.StreamResponse(status=200, headers={"Content-Type": MPEGTS_CONTENT_TYPE})
|
response = web.StreamResponse(status=200, headers={"Content-Type": MPEGTS_CONTENT_TYPE})
|
||||||
|
# A live stream is never meant to be pooled and reused for a later request; some HTTP
|
||||||
|
# clients (e.g. Jellyfin's SharedHttpStream) get fed stale bytes from a dangling
|
||||||
|
# keep-alive connection if we let one linger here. Force a fresh connection every time.
|
||||||
|
response.force_close()
|
||||||
await response.prepare(request)
|
await response.prepare(request)
|
||||||
|
|
||||||
queue = broadcaster.subscribe()
|
queue = broadcaster.subscribe()
|
||||||
@@ -51,7 +55,7 @@ async def _handle_stream(request: web.Request) -> web.StreamResponse:
|
|||||||
if chunk is None:
|
if chunk is None:
|
||||||
break
|
break
|
||||||
await response.write(chunk)
|
await response.write(chunk)
|
||||||
except ConnectionResetError:
|
except (ConnectionResetError, ConnectionAbortedError):
|
||||||
logger.debug("Client disconnected from channel '{}'", channel_id)
|
logger.debug("Client disconnected from channel '{}'", channel_id)
|
||||||
finally:
|
finally:
|
||||||
broadcaster.unsubscribe(queue)
|
broadcaster.unsubscribe(queue)
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
from restreamer.ffmpeg_runner import build_ffmpeg_args
|
import asyncio
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from restreamer import ffmpeg_runner
|
||||||
|
from restreamer.constants import FFMPEG_TERMINATE_TIMEOUT_SECONDS
|
||||||
|
from restreamer.ffmpeg_runner import FfmpegStreamProcess, build_ffmpeg_args
|
||||||
from restreamer.resolver import ResolvedStream
|
from restreamer.resolver import ResolvedStream
|
||||||
|
|
||||||
|
|
||||||
@@ -42,3 +48,15 @@ def test_build_ffmpeg_args_maps_separate_video_and_audio_inputs() -> None:
|
|||||||
assert "-map" in args
|
assert "-map" in args
|
||||||
map_values = [args[i + 1] for i, a in enumerate(args) if a == "-map"]
|
map_values = [args[i + 1] for i, a in enumerate(args) if a == "-map"]
|
||||||
assert map_values == ["0:v:0", "1:a:0"]
|
assert map_values == ["0:v:0", "1:a:0"]
|
||||||
|
|
||||||
|
|
||||||
|
async def test_stop_returns_when_consumer_left_stdout_unread(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||||
|
monkeypatch.setattr(ffmpeg_runner, "build_ffmpeg_args", lambda stream: ["yes"])
|
||||||
|
process = FfmpegStreamProcess(ResolvedStream(video_url="https://example.com/stream.m3u8"), "test")
|
||||||
|
await process.start()
|
||||||
|
async for _chunk in process.iter_chunks():
|
||||||
|
break
|
||||||
|
|
||||||
|
await asyncio.wait_for(process.stop(), timeout=FFMPEG_TERMINATE_TIMEOUT_SECONDS / 2)
|
||||||
|
|
||||||
|
assert await process.wait() != 0
|
||||||
|
|||||||
Reference in New Issue
Block a user