add session api endpoints (#1578)

This commit is contained in:
Jason Dove
2024-01-29 11:31:16 -06:00
committed by GitHub
parent 16007a888e
commit 18deff0b83
5 changed files with 66 additions and 4 deletions
+5
View File
@@ -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
@@ -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 =>
{
+7
View File
@@ -0,0 +1,7 @@
namespace ErsatzTV.Core.FFmpeg;
public record HlsSessionModel(
string ChannelNumber,
string State,
DateTimeOffset TranscodedUntil,
DateTimeOffset LastAccess);
@@ -4,7 +4,9 @@ namespace ErsatzTV.Core.Interfaces.FFmpeg;
public interface IHlsSessionWorker : IDisposable
{
Task Cancel(CancellationToken cancellationToken);
void Touch();
Task<Option<TrimPlaylistResult>> TrimPlaylist(DateTimeOffset filterBefore, CancellationToken cancellationToken);
void PlayoutUpdated();
HlsSessionModel GetModel();
}
@@ -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<HlsSessionModel> GetSessions()
{
return ffmpegSegmenterService.SessionWorkers.Values.Map(w => w.GetModel()).ToList();
}
[HttpDelete("api/session/{channelNumber}")]
public async Task<IActionResult> StopSession(string channelNumber, CancellationToken cancellationToken)
{
if (ffmpegSegmenterService.SessionWorkers.TryGetValue(channelNumber, out IHlsSessionWorker worker))
{
await worker.Cancel(cancellationToken);
return new NoContentResult();
}
return new NotFoundResult();
}
}