TranscodingJobDto.cs 8.4 KB

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