using System;
using System.IO;
namespace MediaBrowser.Controller.MediaEncoding;
///
/// Represents a durable record of an HLS transcoding session for HA pod recovery.
///
public sealed class TranscodeSession
{
///
/// Gets or sets the unique play session identifier.
///
public string PlaySessionId { get; set; } = string.Empty;
///
/// Gets or sets the name of the pod that currently owns this session's lease.
///
public string OwnerPod { get; set; } = string.Empty;
///
/// Gets or sets the UTC time at which the owning pod's lease expires.
///
public DateTime LeaseExpiresUtc { get; set; }
///
/// Gets or sets the absolute path to the HLS manifest (.m3u8) file on shared storage.
///
public string ManifestPath { get; set; } = string.Empty;
///
/// Gets or sets the path prefix for transcoded segment files on shared storage.
///
public string SegmentPathPrefix { get; set; } = string.Empty;
///
/// Gets or sets the media source identifier associated with this session.
///
public string MediaSourceId { get; set; } = string.Empty;
///
/// Gets or sets the zero-based index of the last segment that was fully written to durable storage.
///
public int LastCompletedSegmentIndex { get; set; }
///
/// Gets or sets the last durable playback offset in ticks, used to resume playback after failover.
///
public long LastDurablePlaybackOffset { get; set; }
///
/// Creates a session record for an HLS output, deriving and
/// from the playlist path so that cleanup can recognise
/// every file the session owns.
///
/// The play session identifier.
/// The media source identifier.
/// The name of the pod that owns the session.
/// The absolute path of the HLS playlist (.m3u8) file.
/// The initial lease duration.
/// The new .
public static TranscodeSession CreateForPlaylist(
string playSessionId,
string mediaSourceId,
string ownerPod,
string playlistPath,
TimeSpan leaseDuration)
=> new TranscodeSession
{
PlaySessionId = playSessionId,
OwnerPod = ownerPod,
LeaseExpiresUtc = DateTime.UtcNow.Add(leaseDuration),
ManifestPath = playlistPath,
SegmentPathPrefix = GetSegmentPathPrefix(playlistPath),
MediaSourceId = mediaSourceId,
};
///
/// Gets the prefix every segment file of the HLS output at starts with.
/// Segments are written as <playlist path without extension><index><segment extension>.
///
/// The absolute path of the HLS playlist (.m3u8) file.
/// The segment path prefix.
public static string GetSegmentPathPrefix(string playlistPath)
=> Path.ChangeExtension(playlistPath, null) ?? playlistPath;
}