From 1bbfd15907bc27f228379c38fe7a1a9ffe5f4b99 Mon Sep 17 00:00:00 2001 From: Leonid Pershin Date: Fri, 24 Jul 2026 16:07:37 +0300 Subject: [PATCH] Enhance configuration and routing for streaming features: update .env.example and appsettings.json to include new scheduler and streaming options, add streaming endpoint mapping in Program.cs, and integrate streaming services in DependencyInjection. Introduce segment path resolution in MediaPathResolver for asset management. --- .env.example | 8 + .../Endpoints/StreamingEndpoints.cs | 164 ++++++++++++++++++ backend/src/TeleWave.Api/Program.cs | 1 + backend/src/TeleWave.Api/appsettings.json | 6 + .../GetLivePlaylist/GetLivePlaylistQuery.cs | 7 + .../GetLivePlaylistQueryHandler.cs | 91 ++++++++++ .../GetPublicEpg/GetPublicEpgQuery.cs | 8 + .../GetPublicEpg/GetPublicEpgQueryHandler.cs | 54 ++++++ .../ListPublicChannelsQuery.cs | 5 + .../ListPublicChannelsQueryHandler.cs | 21 +++ .../Streaming/StreamingDtos.cs | 11 ++ .../Streaming/StreamingOptions.cs | 12 ++ .../Broadcast/Live/LiveWindowCalculator.cs | 81 +++++++++ .../Broadcast/Live/LiveWindowModels.cs | 30 ++++ .../DependencyInjection.cs | 4 + .../Media/MediaPathResolver.cs | 7 + .../Streaming/StreamTokenService.cs | 76 ++++++++ .../Broadcast/LiveWindowCalculatorTests.cs | 100 +++++++++++ 18 files changed, 686 insertions(+) create mode 100644 backend/src/TeleWave.Api/Endpoints/StreamingEndpoints.cs create mode 100644 backend/src/TeleWave.Application/Streaming/GetLivePlaylist/GetLivePlaylistQuery.cs create mode 100644 backend/src/TeleWave.Application/Streaming/GetLivePlaylist/GetLivePlaylistQueryHandler.cs create mode 100644 backend/src/TeleWave.Application/Streaming/GetPublicEpg/GetPublicEpgQuery.cs create mode 100644 backend/src/TeleWave.Application/Streaming/GetPublicEpg/GetPublicEpgQueryHandler.cs create mode 100644 backend/src/TeleWave.Application/Streaming/ListPublicChannels/ListPublicChannelsQuery.cs create mode 100644 backend/src/TeleWave.Application/Streaming/ListPublicChannels/ListPublicChannelsQueryHandler.cs create mode 100644 backend/src/TeleWave.Application/Streaming/StreamingDtos.cs create mode 100644 backend/src/TeleWave.Application/Streaming/StreamingOptions.cs create mode 100644 backend/src/TeleWave.Domain/Broadcast/Live/LiveWindowCalculator.cs create mode 100644 backend/src/TeleWave.Domain/Broadcast/Live/LiveWindowModels.cs create mode 100644 backend/src/TeleWave.Infrastructure/Streaming/StreamTokenService.cs create mode 100644 backend/tests/TeleWave.Domain.Tests/Broadcast/LiveWindowCalculatorTests.cs diff --git a/.env.example b/.env.example index 38bbe0b..267a27f 100644 --- a/.env.example +++ b/.env.example @@ -27,10 +27,18 @@ AdminSeed__Password=change-me-strong-admin-password # и volumes в docker-compose.yml). Не меняй RootPath без синхронного изменения тома. Storage__RootPath=/media Storage__SegmentSeconds=2 +# Сколько сегментов держать в скользящем окне live-плейлиста. +Storage__LiveWindowSegments=10 Storage__KeepOriginals=false # Порог свободного места, ниже которого загрузка отклоняется (10 ГБ). Storage__MinFreeSpaceBytes=10737418240 +# ── Планировщик эфира ────────────────────────────────────────────────────── +# На сколько дней вперёд держать расписание; сколько часов прошлого хранить; период тика. +Scheduler__HorizonDays=3 +Scheduler__RetentionHours=24 +Scheduler__TickMinutes=30 + # ── Обработка медиа (ffmpeg) ─────────────────────────────────────────────── Media__FfmpegPath=/usr/bin/ffmpeg Media__FfprobePath=/usr/bin/ffprobe diff --git a/backend/src/TeleWave.Api/Endpoints/StreamingEndpoints.cs b/backend/src/TeleWave.Api/Endpoints/StreamingEndpoints.cs new file mode 100644 index 0000000..c9dcb01 --- /dev/null +++ b/backend/src/TeleWave.Api/Endpoints/StreamingEndpoints.cs @@ -0,0 +1,164 @@ +using System.Globalization; +using System.Text; +using System.Text.RegularExpressions; +using LiteCqrs; +using TeleWave.Api.Common; +using TeleWave.Application.Common.Interfaces; +using TeleWave.Application.Streaming; +using TeleWave.Application.Streaming.GetLivePlaylist; +using TeleWave.Application.Streaming.GetPublicEpg; +using TeleWave.Application.Streaming.ListPublicChannels; +using TeleWave.Infrastructure.Media; +using TeleWave.Infrastructure.Streaming; + +namespace TeleWave.Api.Endpoints; + +public static class StreamingEndpoints +{ + private const string StreamCookieName = "tw_stream"; + private static readonly Regex SegmentFileName = new(@"^seg\d{1,6}\.ts$", RegexOptions.Compiled); + + public static IEndpointRouteBuilder MapStreamingEndpoints(this IEndpointRouteBuilder app) + { + // Публичный API канала (Bearer): список, EPG, выдача stream-cookie. + var channels = app.MapGroup("/api/channels").WithTags("Channels").RequireAuthorization(); + channels.MapGet("", ListChannels).Produces>(); + channels.MapPost("/{slug}/watch", Watch).Produces(StatusCodes.Status204NoContent); + channels.MapGet("/{slug}/epg", Epg); + + // Раздача эфира (cookie tw_stream): плейлист и сегменты — их грузит