using System; using System.Diagnostics; using System.Threading; using Jellyfin.Api.Models.PlaybackDtos; using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Model.Dto; using Microsoft.Extensions.Logging; namespace Jellyfin.Api.Models.TranscodingDtos { /// /// The transcoding job. /// public class TranscodingJob { private readonly ILogger _logger; private readonly object _timerLock = new object(); /// /// Initializes a new instance of the class. /// /// Instance of the interface. public TranscodingJob(ILogger logger) { _logger = logger; } /// /// Gets or sets the play session identifier. /// /// The play session identifier. public string? PlaySessionId { get; set; } /// /// Gets or sets the live stream identifier. /// /// The live stream identifier. public string? LiveStreamId { get; set; } /// /// Gets or sets a value indicating whether the transcoding job is a live output. /// public bool IsLiveOutput { get; set; } /// /// Gets or sets the path. /// /// The path. public MediaSourceInfo? MediaSource { get; set; } /// /// Gets or sets the transcoding path. /// public string? Path { get; set; } /// /// Gets or sets the type. /// /// The type. public TranscodingJobType Type { get; set; } /// /// Gets or sets the process. /// /// The process. public Process? Process { get; set; } /// /// Gets or sets the active request count. /// /// The active request count. public int ActiveRequestCount { get; set; } /// /// Gets or sets the kill timer. /// /// The kill timer. private Timer? KillTimer { get; set; } /// /// Gets or sets the device id. /// public string? DeviceId { get; set; } /// /// Gets or sets the cancellation token source. /// public CancellationTokenSource? CancellationTokenSource { get; set; } /// /// Gets or sets a value indicating whether the transcoding job has exited. /// public bool HasExited { get; set; } /// /// Gets or sets a value indicating whether the user has paused the video. /// public bool IsUserPaused { get; set; } /// /// Gets or sets the id. /// public string? Id { get; set; } /// /// Gets or sets the framerate. /// public float? Framerate { get; set; } /// /// Gets or sets the completion percentage. /// public double? CompletionPercentage { get; set; } /// /// Gets or sets the bytes downloaded. /// public long? BytesDownloaded { get; set; } /// /// Gets or sets the bytes transcoded. /// public long? BytesTranscoded { get; set; } /// /// Gets or sets the bitrate. /// public int? BitRate { get; set; } /// /// Gets or sets the transcoding position ticks. /// public long? TranscodingPositionTicks { get; set; } /// /// Gets or sets the download position ticks. /// public long? DownloadPositionTicks { get; set; } /// /// Gets or sets the transcodign throttler. /// public TranscodingThrottler? TranscodingThrottler { get; set; } /// /// Gets or sets the last ping datetime. /// public DateTime LastPingDate { get; set; } /// /// Gets or sets the ping timeout. /// public int PingTimeout { get; set; } /// /// Stops the kill timer. /// public void StopKillTimer() { lock (_timerLock) { KillTimer?.Change(Timeout.Infinite, Timeout.Infinite); } } /// /// Disposes the kill timer. /// public void DisposeKillTimer() { lock (_timerLock) { if (KillTimer != null) { KillTimer.Dispose(); KillTimer = null; } } } /// /// Starts the kill timer. /// /// The amount of ms the timer should wait before the transcoding job gets killed. public void StartKillTimer(Action callback) { StartKillTimer(callback, PingTimeout); } /// /// Starts the kill timer. /// /// The to run when the kill timer has finished. /// The amount of ms the timer should wait before the transcoding job gets killed. public void StartKillTimer(Action callback, int intervalMs) { if (HasExited) { return; } lock (_timerLock) { if (KillTimer == null) { _logger.LogDebug($"Starting kill timer at {intervalMs}ms. JobId {Id} PlaySessionId {PlaySessionId}"); KillTimer = new Timer(new TimerCallback(callback), this, intervalMs, Timeout.Infinite); } else { _logger.LogDebug($"Changing kill timer to {intervalMs}ms. JobId {Id} PlaySessionId {PlaySessionId}"); KillTimer.Change(intervalMs, Timeout.Infinite); } } } /// /// Changes the kill timer if it has started. /// public void ChangeKillTimerIfStarted() { if (HasExited) { return; } lock (_timerLock) { if (KillTimer != null) { var intervalMs = PingTimeout; _logger.LogDebug($"Changing kill timer to {intervalMs}ms. JobId {Id} PlaySessionId {PlaySessionId}"); KillTimer.Change(intervalMs, Timeout.Infinite); } } } } }