Files
ersatztv/ErsatzTV.Infrastructure/Streaming/ExternalJsonPlayoutItemProvider.cs
T
timothyandClaude Opus 4.8 4a9502cbb5
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 10s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 1m26s
Build ErsatzTV Image / CI image pin matches docker/ci (pull_request) Successful in 6s
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 6s
Build ErsatzTV Image / decisions.md append-only (pull_request) Successful in 5s
Build ErsatzTV Image / Functional E2E (curl contracts) (pull_request) Successful in 14m54s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 18m23s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 18m47s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
fix(480): probe external-JSON remote-stream URLs before handing them to ffmpeg
External-JSON playout channels build their own /media/plex/{server}/{plexFile}
URL in ExternalJsonPlayoutItemProvider.StreamRemotely and the handler assigns it
without routing through ValidatePlayoutItemPath, so the #473 class survived here:
a media item gone from the server 404s under ffmpeg (exit 8) and the same dead
item is re-selected for its whole slot.

Route StreamRemotely through the same IRemoteStreamProber seam #473/PR #479 added
for the generated-playout path. Probe runs before the Plex metadata round-trip
(the URL needs only server.Id + plexFile), so a gone item skips it. An unavailable
stream returns PlayoutItemNotAvailableFromMediaServer, which the handler already
maps to a real-error card. The fail-open policy (redirected-404 only) lives inside
IRemoteStreamProber, so this second call site duplicates only the decision to probe.

Tests: ExternalJsonPlayoutItemProviderTests pins both directions; proven non-vacuous
by neutralizing the probe. docs/decisions.md gets a #480 entry closing the #473
scope gap (append-only: the old #473 entry is cross-referenced, not edited).

fixes #480

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-20 00:17:15 +02:00

322 lines
12 KiB
C#

using System.Globalization;
using System.IO.Abstractions;
using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Domain.Filler;
using ErsatzTV.Core.Errors;
using ErsatzTV.Core.Interfaces.Metadata;
using ErsatzTV.Core.Interfaces.Plex;
using ErsatzTV.Core.Interfaces.Streaming;
using ErsatzTV.Core.Plex;
using ErsatzTV.Core.Streaming;
using ErsatzTV.Infrastructure.Data;
using ErsatzTV.Infrastructure.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace ErsatzTV.Infrastructure.Streaming;
public class ExternalJsonPlayoutItemProvider : IExternalJsonPlayoutItemProvider
{
private readonly IDbContextFactory<TvContext> _dbContextFactory;
private readonly IFileSystem _fileSystem;
private readonly ILocalStatisticsProvider _localStatisticsProvider;
private readonly ILogger<ExternalJsonPlayoutItemProvider> _logger;
private readonly IPlexPathReplacementService _plexPathReplacementService;
private readonly IPlexSecretStore _plexSecretStore;
private readonly IPlexServerApiClient _plexServerApiClient;
private readonly IRemoteStreamProber _remoteStreamProber;
public ExternalJsonPlayoutItemProvider(
IDbContextFactory<TvContext> dbContextFactory,
IFileSystem fileSystem,
IPlexPathReplacementService plexPathReplacementService,
IPlexServerApiClient plexServerApiClient,
IPlexSecretStore plexSecretStore,
ILocalStatisticsProvider localStatisticsProvider,
IRemoteStreamProber remoteStreamProber,
ILogger<ExternalJsonPlayoutItemProvider> logger)
{
_dbContextFactory = dbContextFactory;
_fileSystem = fileSystem;
_plexPathReplacementService = plexPathReplacementService;
_plexServerApiClient = plexServerApiClient;
_plexSecretStore = plexSecretStore;
_localStatisticsProvider = localStatisticsProvider;
_remoteStreamProber = remoteStreamProber;
_logger = logger;
}
public async Task<Either<BaseError, PlayoutItemWithPath>> CheckForExternalJson(
Channel channel,
DateTimeOffset now,
string ffprobePath,
CancellationToken cancellationToken)
{
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
Option<Playout> maybePlayout = await dbContext.Playouts
.AsNoTracking()
.SelectOneAsync(p => p.ChannelId, p => p.ChannelId == channel.Id, cancellationToken);
foreach (Playout playout in maybePlayout)
{
// playout must be external json
if (playout.ScheduleKind == PlayoutScheduleKind.ExternalJson)
{
// json file must exist
if (_fileSystem.File.Exists(playout.ScheduleFile))
{
return await GetExternalJsonPlayoutItem(dbContext, playout, now, ffprobePath, cancellationToken);
}
_logger.LogWarning(
"Unable to locate json schedule file {File} for channel {Number} - {Name}",
playout.ScheduleFile,
channel.Number,
channel.Name);
}
}
return new UnableToLocatePlayoutItem();
}
private async Task<Either<BaseError, PlayoutItemWithPath>> GetExternalJsonPlayoutItem(
TvContext dbContext,
Playout playout,
DateTimeOffset now,
string ffprobePath,
CancellationToken cancellationToken)
{
Option<ExternalJsonChannel> maybeChannel = JsonConvert.DeserializeObject<ExternalJsonChannel>(
await _fileSystem.File.ReadAllTextAsync(playout.ScheduleFile, cancellationToken));
// must deserialize channel from json
foreach (ExternalJsonChannel channel in maybeChannel)
{
// TODO: null start time should log and throw
DateTimeOffset startTime = DateTimeOffset.Parse(
channel.StartTime ?? string.Empty,
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal).ToLocalTime();
//_logger.LogDebug("external json start time: {StartTime}", startTime);
foreach (ExternalJsonProgram program in channel.Programs)
{
int milliseconds = program.Duration;
DateTimeOffset nextStart = startTime + TimeSpan.FromMilliseconds(milliseconds);
if (nextStart > now)
{
//_logger.LogDebug("should play program {@Program}", program);
return await BuildPlayoutItem(dbContext, startTime, program, ffprobePath, cancellationToken);
}
startTime = nextStart;
}
}
return new UnableToLocatePlayoutItem();
}
private async Task<Either<BaseError, PlayoutItemWithPath>> BuildPlayoutItem(
TvContext dbContext,
DateTimeOffset startTime,
ExternalJsonProgram program,
string ffprobePath,
CancellationToken cancellationToken)
{
// find any library path from the appropriate plex server
List<LibraryPath> maybeLibraryPath = await dbContext.LibraryPaths
.AsNoTracking()
.Filter(lp => ((PlexMediaSource)((PlexLibrary)lp.Library).MediaSource).ServerName == program.ServerKey)
.OrderBy(lp => lp.Id)
.Take(1)
.ToListAsync(cancellationToken);
foreach (LibraryPath libraryPath in maybeLibraryPath.HeadOrNone())
{
string localPath = await _plexPathReplacementService.GetReplacementPlexPath(
libraryPath.Id,
program.File,
cancellationToken);
if (_fileSystem.File.Exists(localPath))
{
return await StreamLocally(startTime, program, ffprobePath, localPath);
}
return await StreamRemotely(dbContext, startTime, program, cancellationToken);
}
return new UnableToLocatePlayoutItem();
}
private async Task<Either<BaseError, PlayoutItemWithPath>> StreamLocally(
DateTimeOffset startTime,
ExternalJsonProgram program,
string ffprobePath,
string localPath)
{
// ffprobe on demand
Either<BaseError, MediaVersion> maybeMediaVersion =
await _localStatisticsProvider.GetStatistics(ffprobePath, localPath);
foreach (MediaVersion mediaVersion in maybeMediaVersion.RightToSeq())
{
// build playout item
var episode = new Episode
{
MediaVersions = [mediaVersion],
EpisodeMetadata =
[
new EpisodeMetadata
{
EpisodeNumber = program.Episode,
Title = program.Title
}
],
Season = new Season
{
SeasonNumber = program.Season,
Show = new Show
{
ShowMetadata =
[
new ShowMetadata
{
Title = program.ShowTitle
}
]
}
}
};
return new PlayoutItemWithPath(GetPlayoutItem(startTime, episode, program), localPath);
}
return new UnableToLocatePlayoutItem();
}
private async Task<Either<BaseError, PlayoutItemWithPath>> StreamRemotely(
TvContext dbContext,
DateTimeOffset startTime,
ExternalJsonProgram program,
CancellationToken cancellationToken)
{
Option<PlexMediaSource> maybeServer = await dbContext.PlexMediaSources
.Include(pms => pms.Connections)
.SelectOneAsync(pms => pms.ServerName, pms => pms.ServerName == program.ServerKey, cancellationToken);
foreach (PlexMediaSource server in maybeServer)
{
Option<PlexConnection> maybeConnection = server.Connections.SingleOrDefault(c => c.IsActive);
foreach (PlexConnection connection in maybeConnection)
{
Option<PlexServerAuthToken> maybeToken =
await _plexSecretStore.GetServerAuthToken(server.ClientIdentifier);
foreach (PlexServerAuthToken token in maybeToken)
{
var plexUrl =
$"http://localhost:{Settings.StreamingPort}/media/plex/{server.Id}/{program.PlexFile}";
// #480: probe the remote-stream URL before handing it to ffmpeg, exactly as the
// generated-playout path does in
// GetPlayoutItemProcessByChannelNumberHandler.ValidatePlayoutItemPath (#473). Without
// this, an item that is gone from the media server 404s under ffmpeg (exit 8) and the
// same dead item is re-selected for its whole slot. The fail-open contract (only a
// *redirected* 404 fails closed) lives inside IRemoteStreamProber, so this call site
// only owns the decision to probe, not the policy. Probing first also skips the Plex
// metadata round-trip when the item is already gone.
if (!await _remoteStreamProber.IsAvailable(plexUrl, cancellationToken))
{
return new PlayoutItemNotAvailableFromMediaServer(plexUrl);
}
MediaItem mediaItem = program.Type switch
{
"episode" => await GetPlexEpisode(server, connection, token, program),
_ => await GetPlexMovie(server, connection, token, program)
};
return new PlayoutItemWithPath(GetPlayoutItem(startTime, mediaItem, program), plexUrl);
}
}
}
// TODO: log errors?
return new UnableToLocatePlayoutItem();
}
private async Task<MediaItem> GetPlexEpisode(
PlexMediaSource plexMediaSource,
PlexConnection connection,
PlexServerAuthToken token,
ExternalJsonProgram program)
{
Either<BaseError, Tuple<EpisodeMetadata, MediaVersion>> maybeStatistics =
await _plexServerApiClient.GetEpisodeMetadataAndStatistics(
plexMediaSource.Id,
program.RatingKey,
connection,
token);
foreach (Tuple<EpisodeMetadata, MediaVersion> result in maybeStatistics.RightToSeq())
{
return new PlexEpisode
{
EpisodeMetadata = [result.Item1],
MediaVersions = [result.Item2]
};
}
throw new NotSupportedException();
}
private async Task<MediaItem> GetPlexMovie(
PlexMediaSource plexMediaSource,
PlexConnection connection,
PlexServerAuthToken token,
ExternalJsonProgram program)
{
Either<BaseError, Tuple<MovieMetadata, MediaVersion>> maybeStatistics =
await _plexServerApiClient.GetMovieMetadataAndStatistics(
plexMediaSource.Id,
program.RatingKey,
connection,
token);
foreach (Tuple<MovieMetadata, MediaVersion> result in maybeStatistics.RightToSeq())
{
return new PlexMovie
{
MovieMetadata = [result.Item1],
MediaVersions = [result.Item2]
};
}
throw new NotSupportedException();
}
private static PlayoutItem GetPlayoutItem(
DateTimeOffset startTime,
MediaItem mediaItem,
ExternalJsonProgram program) =>
new()
{
Start = startTime.UtcDateTime,
Finish = startTime.AddMilliseconds(program.Duration).UtcDateTime,
FillerKind = FillerKind.None,
ChapterTitle = null,
GuideFinish = null,
GuideGroup = 0,
CustomTitle = null,
InPoint = TimeSpan.Zero,
OutPoint = TimeSpan.FromMilliseconds(program.Duration),
MediaItem = mediaItem
};
}