Fix recordings dying mid-stream; record to growing .ts with live buffer playback
A recording of a live event crashed partway through: when its short Akamai token expired, the proxy's stale-alias check swapped the recording's mapping to the most recently registered deferred stream, of any content. Segment requests were then built against the wrong CDN path, returned 403, and ffmpeg exited, splitting the recording into several files. Proxy: - Never swap URN-backed livestreams; they refresh themselves from the API. Only consider swap candidates with the same URN. Recording lifecycle: - Complete a recording once its stream has been gone from the API for 5 minutes (broadcasts can end before ValidTo). - Fail a recording still waiting for its stream after ValidTo (or 12h after ValidFrom) instead of retrying forever. - Wait for a DVR window whose start= is still in the future instead of starting ffmpeg against a 400/404. - Keep the original start time across restarts; don't mark a recording failed on shutdown cancellation. Growing .ts recordings: - ffmpeg writes MPEG-TS (-c copy -copyts) to stdout and the plugin appends it to one file per recording, so restarts continue the same file and timeline. - The Recordings folder lists any recording with a file on disk, not only completed ones. - In-progress recordings are exposed as infinite streams reading through a new endpoint that follows the growing file (same pattern as Jellyfin's DVR), so they can be watched from the start like a livestream buffer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -132,10 +132,13 @@ public class StreamProxyService : IStreamProxyService
|
||||
!string.IsNullOrEmpty(streamInfo.AuthenticatedUrl));
|
||||
|
||||
// Check for stale alias: only look for fresher stream if current token is EXPIRED or EXPIRING SOON
|
||||
// Don't replace a valid token (>5s left) with a new deferred registration
|
||||
if (!streamInfo.NeedsAuthentication && tokenTimeLeft < 5)
|
||||
// Don't replace a valid token (>5s left) with a new deferred registration.
|
||||
// Livestreams with a URN refresh themselves from the API, so never swap them: an unrelated
|
||||
// registration (e.g. from a library refresh) would point the stream at the wrong CDN path.
|
||||
var selfRefreshing = streamInfo.IsLiveStream && !string.IsNullOrEmpty(streamInfo.Urn);
|
||||
if (!streamInfo.NeedsAuthentication && tokenTimeLeft < 5 && !selfRefreshing)
|
||||
{
|
||||
var freshStream = FindFreshestStream();
|
||||
var freshStream = FindFreshestStream(streamInfo.Urn);
|
||||
if (freshStream != null && freshStream.Value.Value.NeedsAuthentication)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
@@ -595,8 +598,9 @@ public class StreamProxyService : IStreamProxyService
|
||||
/// <summary>
|
||||
/// Finds the freshest (most recently registered) stream that needs authentication or has a valid token.
|
||||
/// </summary>
|
||||
/// <param name="urn">When set, only streams for this URN are considered.</param>
|
||||
/// <returns>The freshest stream entry, or null if none found.</returns>
|
||||
private KeyValuePair<string, StreamInfo>? FindFreshestStream()
|
||||
private KeyValuePair<string, StreamInfo>? FindFreshestStream(string? urn)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
@@ -604,6 +608,11 @@ public class StreamProxyService : IStreamProxyService
|
||||
// or have tokens that aren't expired yet
|
||||
var candidates = _streamMappings.Where(kvp =>
|
||||
{
|
||||
if (!string.IsNullOrEmpty(urn) && !string.Equals(kvp.Value.Urn, urn, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return false; // Different content
|
||||
}
|
||||
|
||||
if (kvp.Value.NeedsAuthentication)
|
||||
{
|
||||
return true; // Fresh deferred registration
|
||||
|
||||
Reference in New Issue
Block a user