using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading;
using MediaBrowser.Model.Dto;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Controller.MediaEncoding;
///
/// Class TranscodingJob.
///
public sealed class TranscodingJob : IDisposable
{
private readonly ILogger _logger;
private readonly Lock _processLock = new();
private readonly Lock _timerLock = new();
private readonly ConcurrentDictionary _segmentEndPositionTicks = new();
private int _activeRequestCount;
private Timer? _killTimer;
///
/// Initializes a new instance of the class.
///
/// Instance of the interface.
public TranscodingJob(ILogger logger)
{
_logger = logger;
}
///
/// Gets or sets the play session identifier.
///
public string? PlaySessionId { get; set; }
///
/// Gets or sets the live stream identifier.
///
public string? LiveStreamId { get; set; }
///
/// Gets or sets a value indicating whether is live output.
///
public bool IsLiveOutput { get; set; }
///
/// Gets or sets the path.
///
public MediaSourceInfo? MediaSource { get; set; }
///
/// Gets or sets path.
///
public string? Path { get; set; }
///
/// Gets or sets the type.
///
public TranscodingJobType Type { get; set; }
///
/// Gets or sets the process.
///
public Process? Process { get; set; }
///
/// Gets or sets the active request count.
///
public int ActiveRequestCount
{
get => Volatile.Read(ref _activeRequestCount);
set => Volatile.Write(ref _activeRequestCount, value);
}
///
/// Gets or sets device id.
///
public string? DeviceId { get; set; }
///
/// Gets or sets cancellation token source.
///
public CancellationTokenSource? CancellationTokenSource { get; set; }
///
/// Gets or sets a value indicating whether has exited.
///
public bool HasExited { get; set; }
///
/// Gets or sets exit code.
///
public int ExitCode { get; set; }
///
/// Gets or sets a value indicating whether is user paused.
///
public bool IsUserPaused { get; set; }
///
/// Gets or sets id.
///
public string? Id { get; set; }
///
/// Gets or sets framerate.
///
public float? Framerate { get; set; }
///
/// Gets or sets completion percentage.
///
public double? CompletionPercentage { get; set; }
///
/// Gets or sets bytes downloaded.
///
public long BytesDownloaded { get; set; }
///
/// Gets or sets bytes transcoded.
///
public long? BytesTranscoded { get; set; }
///
/// Gets or sets bit rate.
///
public int? BitRate { get; set; }
///
/// Gets or sets transcoding position ticks.
///
public long? TranscodingPositionTicks { get; set; }
///
/// Gets or sets download position ticks.
///
public long? DownloadPositionTicks { get; set; }
///
/// Gets or sets transcoding throttler.
///
public TranscodingThrottler? TranscodingThrottler { get; set; }
///
/// Gets or sets transcoding segment cleaner.
///
public TranscodingSegmentCleaner? TranscodingSegmentCleaner { get; set; }
///
/// Gets or sets last ping date.
///
public DateTime LastPingDate { get; set; }
///
/// Gets or sets ping timeout.
///
public int PingTimeout { get; set; }
///
/// Records the end position of a served segment.
///
/// The segment index.
/// The segment end position ticks.
public void ReportSegmentDownloaded(int segmentIndex, long segmentEndPositionTicks)
{
if (segmentIndex < 0)
{
return;
}
_segmentEndPositionTicks[segmentIndex] = segmentEndPositionTicks;
}
///
/// Gets the highest index among served segments whose end position is at or before the given position.
///
/// The position ticks.
/// The segment index, or null if there is none.
public int? GetHighestServedSegmentIndexEndingAtOrBefore(long positionTicks)
{
int? result = null;
foreach (var (index, endPositionTicks) in _segmentEndPositionTicks)
{
if (endPositionTicks <= positionTicks && (result is null || index > result))
{
result = index;
}
}
return result;
}
///
/// Increments the active request count.
///
/// The incremented count.
public int IncrementActiveRequestCount()
=> Interlocked.Increment(ref _activeRequestCount);
///
/// Decrements the active request count.
///
/// The decremented count.
public int DecrementActiveRequestCount()
=> Interlocked.Decrement(ref _activeRequestCount);
///
/// Stop kill timer.
///
public void StopKillTimer()
{
lock (_timerLock)
{
_killTimer?.Change(Timeout.Infinite, Timeout.Infinite);
}
}
///
/// Dispose kill timer.
///
public void DisposeKillTimer()
{
lock (_timerLock)
{
if (_killTimer is not null)
{
_killTimer.Dispose();
_killTimer = null;
}
}
}
///
/// Start kill timer.
///
/// Callback action.
public void StartKillTimer(Action