using System; using System.Diagnostics; using System.Threading; using MediaBrowser.Api.Playback; using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Model.Dto; using Microsoft.Extensions.Logging; namespace MediaBrowser.Api { /// /// Class TranscodingJob. /// public class TranscodingJob { /// /// 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; } public bool IsLiveOutput { get; set; } /// /// Gets or sets the path. /// /// The path. public MediaSourceInfo MediaSource { get; set; } 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; } public ILogger Logger { get; private 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; } public string DeviceId { get; set; } public CancellationTokenSource CancellationTokenSource { get; set; } public object ProcessLock = new object(); public bool HasExited { get; set; } public bool IsUserPaused { get; set; } public string Id { get; set; } public float? Framerate { get; set; } public double? CompletionPercentage { get; set; } public long? BytesDownloaded { get; set; } public long? BytesTranscoded { get; set; } public int? BitRate { get; set; } public long? TranscodingPositionTicks { get; set; } public long? DownloadPositionTicks { get; set; } public TranscodingThrottler TranscodingThrottler { get; set; } private readonly object _timerLock = new object(); public DateTime LastPingDate { get; set; } public int PingTimeout { get; set; } public TranscodingJob(ILogger logger) { Logger = logger; } public void StopKillTimer() { lock (_timerLock) { if (KillTimer != null) { KillTimer.Change(Timeout.Infinite, Timeout.Infinite); } } } public void DisposeKillTimer() { lock (_timerLock) { if (KillTimer != null) { KillTimer.Dispose(); KillTimer = null; } } } public void StartKillTimer(Action callback) { StartKillTimer(callback, PingTimeout); } public void StartKillTimer(Action callback, int intervalMs) { if (HasExited) { return; } lock (_timerLock) { if (KillTimer == null) { Logger.LogDebug("Starting kill timer at {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId); KillTimer = new Timer(new TimerCallback(callback), this, intervalMs, Timeout.Infinite); } else { Logger.LogDebug("Changing kill timer to {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId); KillTimer.Change(intervalMs, Timeout.Infinite); } } } public void ChangeKillTimerIfStarted() { if (HasExited) { return; } lock (_timerLock) { if (KillTimer != null) { var intervalMs = PingTimeout; Logger.LogDebug("Changing kill timer to {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId); KillTimer.Change(intervalMs, Timeout.Infinite); } } } } }