From 18deff0b836b869073336fef7eeae7e721c9a1bf Mon Sep 17 00:00:00 2001 From: Jason Dove <1695733+jasongdove@users.noreply.github.com> Date: Mon, 29 Jan 2024 11:31:16 -0600 Subject: [PATCH] add session api endpoints (#1578) --- CHANGELOG.md | 5 ++++ .../Streaming/HlsSessionWorker.cs | 29 ++++++++++++++++--- ErsatzTV.Core/FFmpeg/HlsSessionModel.cs | 7 +++++ .../Interfaces/FFmpeg/IHlsSessionWorker.cs | 2 ++ ErsatzTV/Controllers/Api/SessionController.cs | 27 +++++++++++++++++ 5 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 ErsatzTV.Core/FFmpeg/HlsSessionModel.cs create mode 100644 ErsatzTV/Controllers/Api/SessionController.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index dfb740f2c..1fbc53619 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - `Playout Templates` - templates to schedule using the specified criteria. Only one template will be selected each day - Much more to come on this feature as development continues - Show chapter markers in movie and episode media info +- Add two new API endpoints for interacting with transcoding sessions (MPEG-TS and HLS Segmenter): + - GET `/api/sessions` + - Show brief info about all active sessions + - DELETE `/api/session/{channel-number}` + - Stop the session for the given channel number ### Fixed - Fix error loading path replacements when using MySql diff --git a/ErsatzTV.Application/Streaming/HlsSessionWorker.cs b/ErsatzTV.Application/Streaming/HlsSessionWorker.cs index 64f121f86..bca8d9b42 100644 --- a/ErsatzTV.Application/Streaming/HlsSessionWorker.cs +++ b/ErsatzTV.Application/Streaming/HlsSessionWorker.cs @@ -39,6 +39,7 @@ public class HlsSessionWorker : IHlsSessionWorker private HlsSessionState _state; private Timer _timer; private DateTimeOffset _transcodedUntil; + private CancellationTokenSource _cancellationTokenSource; public HlsSessionWorker( IServiceScopeFactory serviceScopeFactory, @@ -61,6 +62,21 @@ public class HlsSessionWorker : IHlsSessionWorker public DateTimeOffset PlaylistStart { get; private set; } + public async Task Cancel(CancellationToken cancellationToken) + { + _logger.LogInformation("API termination request for HLS session for channel {Channel}", _channelNumber); + + await Slim.WaitAsync(cancellationToken); + try + { + await _cancellationTokenSource.CancelAsync(); + } + finally + { + Slim.Release(); + } + } + public void Touch() { lock (_sync) @@ -120,6 +136,8 @@ public class HlsSessionWorker : IHlsSessionWorker } public void PlayoutUpdated() => _state = HlsSessionState.PlayoutUpdated; + + public HlsSessionModel GetModel() => new(_channelNumber, _state.ToString(), _transcodedUntil, _lastAccess); void IDisposable.Dispose() { @@ -129,14 +147,14 @@ public class HlsSessionWorker : IHlsSessionWorker public async Task Run(string channelNumber, TimeSpan idleTimeout, CancellationToken incomingCancellationToken) { - var cts = CancellationTokenSource.CreateLinkedTokenSource(incomingCancellationToken); + _cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(incomingCancellationToken); [SuppressMessage("Usage", "VSTHRD100:Avoid async void methods")] async void Cancel(object o, ElapsedEventArgs e) { try { - await cts.CancelAsync(); + await _cancellationTokenSource.CancelAsync(); } catch (Exception) { @@ -154,7 +172,7 @@ public class HlsSessionWorker : IHlsSessionWorker _timer.Elapsed += Cancel; } - CancellationToken cancellationToken = cts.Token; + CancellationToken cancellationToken = _cancellationTokenSource.Token; _logger.LogInformation("Starting HLS session for channel {Channel}", channelNumber); @@ -495,7 +513,10 @@ public class HlsSessionWorker : IHlsSessionWorker // delete old segments var allSegments = Directory.GetFiles( Path.Combine(FileSystemLayout.TranscodeFolder, _channelNumber), - "live*.ts") + "live*.ts").Append( + Directory.GetFiles( + Path.Combine(FileSystemLayout.TranscodeFolder, _channelNumber), + "live*.mp4")) .Map( file => { diff --git a/ErsatzTV.Core/FFmpeg/HlsSessionModel.cs b/ErsatzTV.Core/FFmpeg/HlsSessionModel.cs new file mode 100644 index 000000000..081fc00a2 --- /dev/null +++ b/ErsatzTV.Core/FFmpeg/HlsSessionModel.cs @@ -0,0 +1,7 @@ +namespace ErsatzTV.Core.FFmpeg; + +public record HlsSessionModel( + string ChannelNumber, + string State, + DateTimeOffset TranscodedUntil, + DateTimeOffset LastAccess); diff --git a/ErsatzTV.Core/Interfaces/FFmpeg/IHlsSessionWorker.cs b/ErsatzTV.Core/Interfaces/FFmpeg/IHlsSessionWorker.cs index a8f98ec08..54331c850 100644 --- a/ErsatzTV.Core/Interfaces/FFmpeg/IHlsSessionWorker.cs +++ b/ErsatzTV.Core/Interfaces/FFmpeg/IHlsSessionWorker.cs @@ -4,7 +4,9 @@ namespace ErsatzTV.Core.Interfaces.FFmpeg; public interface IHlsSessionWorker : IDisposable { + Task Cancel(CancellationToken cancellationToken); void Touch(); Task> TrimPlaylist(DateTimeOffset filterBefore, CancellationToken cancellationToken); void PlayoutUpdated(); + HlsSessionModel GetModel(); } diff --git a/ErsatzTV/Controllers/Api/SessionController.cs b/ErsatzTV/Controllers/Api/SessionController.cs new file mode 100644 index 000000000..0c91b36ee --- /dev/null +++ b/ErsatzTV/Controllers/Api/SessionController.cs @@ -0,0 +1,27 @@ +using ErsatzTV.Core.FFmpeg; +using ErsatzTV.Core.Interfaces.FFmpeg; +using Microsoft.AspNetCore.Mvc; + +namespace ErsatzTV.Controllers.Api; + +[ApiController] +public class SessionController(IFFmpegSegmenterService ffmpegSegmenterService) +{ + [HttpGet("api/sessions")] + public List GetSessions() + { + return ffmpegSegmenterService.SessionWorkers.Values.Map(w => w.GetModel()).ToList(); + } + + [HttpDelete("api/session/{channelNumber}")] + public async Task StopSession(string channelNumber, CancellationToken cancellationToken) + { + if (ffmpegSegmenterService.SessionWorkers.TryGetValue(channelNumber, out IHlsSessionWorker worker)) + { + await worker.Cancel(cancellationToken); + return new NoContentResult(); + } + + return new NotFoundResult(); + } +}