TranscodingJobDto.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System;
  2. using System.Diagnostics;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Threading;
  5. using MediaBrowser.Controller.MediaEncoding;
  6. using MediaBrowser.Model.Dto;
  7. using Microsoft.Extensions.Logging;
  8. namespace Jellyfin.Api.Models.PlaybackDtos;
  9. /// <summary>
  10. /// Class TranscodingJob.
  11. /// </summary>
  12. public class TranscodingJobDto : IDisposable
  13. {
  14. /// <summary>
  15. /// The process lock.
  16. /// </summary>
  17. [SuppressMessage("Microsoft.Performance", "CA1051:NoVisibleInstanceFields", MessageId = "ProcessLock", Justification = "Imported from ServiceStack")]
  18. [SuppressMessage("Microsoft.Performance", "SA1401:PrivateField", MessageId = "ProcessLock", Justification = "Imported from ServiceStack")]
  19. public readonly object ProcessLock = new object();
  20. /// <summary>
  21. /// Timer lock.
  22. /// </summary>
  23. private readonly object _timerLock = new object();
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="TranscodingJobDto"/> class.
  26. /// </summary>
  27. /// <param name="logger">Instance of the <see cref="ILogger{TranscodingJobDto}"/> interface.</param>
  28. public TranscodingJobDto(ILogger<TranscodingJobDto> logger)
  29. {
  30. Logger = logger;
  31. }
  32. /// <summary>
  33. /// Gets or sets the play session identifier.
  34. /// </summary>
  35. /// <value>The play session identifier.</value>
  36. public string? PlaySessionId { get; set; }
  37. /// <summary>
  38. /// Gets or sets the live stream identifier.
  39. /// </summary>
  40. /// <value>The live stream identifier.</value>
  41. public string? LiveStreamId { get; set; }
  42. /// <summary>
  43. /// Gets or sets a value indicating whether is live output.
  44. /// </summary>
  45. public bool IsLiveOutput { get; set; }
  46. /// <summary>
  47. /// Gets or sets the path.
  48. /// </summary>
  49. /// <value>The path.</value>
  50. public MediaSourceInfo? MediaSource { get; set; }
  51. /// <summary>
  52. /// Gets or sets path.
  53. /// </summary>
  54. public string? Path { get; set; }
  55. /// <summary>
  56. /// Gets or sets the type.
  57. /// </summary>
  58. /// <value>The type.</value>
  59. public TranscodingJobType Type { get; set; }
  60. /// <summary>
  61. /// Gets or sets the process.
  62. /// </summary>
  63. /// <value>The process.</value>
  64. public Process? Process { get; set; }
  65. /// <summary>
  66. /// Gets logger.
  67. /// </summary>
  68. public ILogger<TranscodingJobDto> Logger { get; private set; }
  69. /// <summary>
  70. /// Gets or sets the active request count.
  71. /// </summary>
  72. /// <value>The active request count.</value>
  73. public int ActiveRequestCount { get; set; }
  74. /// <summary>
  75. /// Gets or sets the kill timer.
  76. /// </summary>
  77. /// <value>The kill timer.</value>
  78. private Timer? KillTimer { get; set; }
  79. /// <summary>
  80. /// Gets or sets device id.
  81. /// </summary>
  82. public string? DeviceId { get; set; }
  83. /// <summary>
  84. /// Gets or sets cancellation token source.
  85. /// </summary>
  86. public CancellationTokenSource? CancellationTokenSource { get; set; }
  87. /// <summary>
  88. /// Gets or sets a value indicating whether has exited.
  89. /// </summary>
  90. public bool HasExited { get; set; }
  91. /// <summary>
  92. /// Gets or sets exit code.
  93. /// </summary>
  94. public int ExitCode { get; set; }
  95. /// <summary>
  96. /// Gets or sets a value indicating whether is user paused.
  97. /// </summary>
  98. public bool IsUserPaused { get; set; }
  99. /// <summary>
  100. /// Gets or sets id.
  101. /// </summary>
  102. public string? Id { get; set; }
  103. /// <summary>
  104. /// Gets or sets framerate.
  105. /// </summary>
  106. public float? Framerate { get; set; }
  107. /// <summary>
  108. /// Gets or sets completion percentage.
  109. /// </summary>
  110. public double? CompletionPercentage { get; set; }
  111. /// <summary>
  112. /// Gets or sets bytes downloaded.
  113. /// </summary>
  114. public long BytesDownloaded { get; set; }
  115. /// <summary>
  116. /// Gets or sets bytes transcoded.
  117. /// </summary>
  118. public long? BytesTranscoded { get; set; }
  119. /// <summary>
  120. /// Gets or sets bit rate.
  121. /// </summary>
  122. public int? BitRate { get; set; }
  123. /// <summary>
  124. /// Gets or sets transcoding position ticks.
  125. /// </summary>
  126. public long? TranscodingPositionTicks { get; set; }
  127. /// <summary>
  128. /// Gets or sets download position ticks.
  129. /// </summary>
  130. public long? DownloadPositionTicks { get; set; }
  131. /// <summary>
  132. /// Gets or sets transcoding throttler.
  133. /// </summary>
  134. public TranscodingThrottler? TranscodingThrottler { get; set; }
  135. /// <summary>
  136. /// Gets or sets last ping date.
  137. /// </summary>
  138. public DateTime LastPingDate { get; set; }
  139. /// <summary>
  140. /// Gets or sets ping timeout.
  141. /// </summary>
  142. public int PingTimeout { get; set; }
  143. /// <summary>
  144. /// Stop kill timer.
  145. /// </summary>
  146. public void StopKillTimer()
  147. {
  148. lock (_timerLock)
  149. {
  150. KillTimer?.Change(Timeout.Infinite, Timeout.Infinite);
  151. }
  152. }
  153. /// <summary>
  154. /// Dispose kill timer.
  155. /// </summary>
  156. public void DisposeKillTimer()
  157. {
  158. lock (_timerLock)
  159. {
  160. if (KillTimer is not null)
  161. {
  162. KillTimer.Dispose();
  163. KillTimer = null;
  164. }
  165. }
  166. }
  167. /// <summary>
  168. /// Start kill timer.
  169. /// </summary>
  170. /// <param name="callback">Callback action.</param>
  171. public void StartKillTimer(Action<object?> callback)
  172. {
  173. StartKillTimer(callback, PingTimeout);
  174. }
  175. /// <summary>
  176. /// Start kill timer.
  177. /// </summary>
  178. /// <param name="callback">Callback action.</param>
  179. /// <param name="intervalMs">Callback interval.</param>
  180. public void StartKillTimer(Action<object?> callback, int intervalMs)
  181. {
  182. if (HasExited)
  183. {
  184. return;
  185. }
  186. lock (_timerLock)
  187. {
  188. if (KillTimer is null)
  189. {
  190. Logger.LogDebug("Starting kill timer at {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
  191. KillTimer = new Timer(new TimerCallback(callback), this, intervalMs, Timeout.Infinite);
  192. }
  193. else
  194. {
  195. Logger.LogDebug("Changing kill timer to {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
  196. KillTimer.Change(intervalMs, Timeout.Infinite);
  197. }
  198. }
  199. }
  200. /// <summary>
  201. /// Change kill timer if started.
  202. /// </summary>
  203. public void ChangeKillTimerIfStarted()
  204. {
  205. if (HasExited)
  206. {
  207. return;
  208. }
  209. lock (_timerLock)
  210. {
  211. if (KillTimer is not null)
  212. {
  213. var intervalMs = PingTimeout;
  214. Logger.LogDebug("Changing kill timer to {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
  215. KillTimer.Change(intervalMs, Timeout.Infinite);
  216. }
  217. }
  218. }
  219. /// <inheritdoc />
  220. public void Dispose()
  221. {
  222. Dispose(true);
  223. GC.SuppressFinalize(this);
  224. }
  225. /// <summary>
  226. /// Dispose all resources.
  227. /// </summary>
  228. /// <param name="disposing">Whether to dispose all resources.</param>
  229. protected virtual void Dispose(bool disposing)
  230. {
  231. if (disposing)
  232. {
  233. Process?.Dispose();
  234. Process = null;
  235. KillTimer?.Dispose();
  236. KillTimer = null;
  237. CancellationTokenSource?.Dispose();
  238. CancellationTokenSource = null;
  239. TranscodingThrottler?.Dispose();
  240. TranscodingThrottler = null;
  241. }
  242. }
  243. }