2
0

TranscodingJobDto.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 a value indicating whether is user paused.
  94. /// </summary>
  95. public bool IsUserPaused { get; set; }
  96. /// <summary>
  97. /// Gets or sets id.
  98. /// </summary>
  99. public string? Id { get; set; }
  100. /// <summary>
  101. /// Gets or sets framerate.
  102. /// </summary>
  103. public float? Framerate { get; set; }
  104. /// <summary>
  105. /// Gets or sets completion percentage.
  106. /// </summary>
  107. public double? CompletionPercentage { get; set; }
  108. /// <summary>
  109. /// Gets or sets bytes downloaded.
  110. /// </summary>
  111. public long? BytesDownloaded { get; set; }
  112. /// <summary>
  113. /// Gets or sets bytes transcoded.
  114. /// </summary>
  115. public long? BytesTranscoded { get; set; }
  116. /// <summary>
  117. /// Gets or sets bit rate.
  118. /// </summary>
  119. public int? BitRate { get; set; }
  120. /// <summary>
  121. /// Gets or sets transcoding position ticks.
  122. /// </summary>
  123. public long? TranscodingPositionTicks { get; set; }
  124. /// <summary>
  125. /// Gets or sets download position ticks.
  126. /// </summary>
  127. public long? DownloadPositionTicks { get; set; }
  128. /// <summary>
  129. /// Gets or sets transcoding throttler.
  130. /// </summary>
  131. public TranscodingThrottler? TranscodingThrottler { get; set; }
  132. /// <summary>
  133. /// Gets or sets last ping date.
  134. /// </summary>
  135. public DateTime LastPingDate { get; set; }
  136. /// <summary>
  137. /// Gets or sets ping timeout.
  138. /// </summary>
  139. public int PingTimeout { get; set; }
  140. /// <summary>
  141. /// Stop kill timer.
  142. /// </summary>
  143. public void StopKillTimer()
  144. {
  145. lock (_timerLock)
  146. {
  147. KillTimer?.Change(Timeout.Infinite, Timeout.Infinite);
  148. }
  149. }
  150. /// <summary>
  151. /// Dispose kill timer.
  152. /// </summary>
  153. public void DisposeKillTimer()
  154. {
  155. lock (_timerLock)
  156. {
  157. if (KillTimer != null)
  158. {
  159. KillTimer.Dispose();
  160. KillTimer = null;
  161. }
  162. }
  163. }
  164. /// <summary>
  165. /// Start kill timer.
  166. /// </summary>
  167. /// <param name="callback">Callback action.</param>
  168. public void StartKillTimer(Action<object?> callback)
  169. {
  170. StartKillTimer(callback, PingTimeout);
  171. }
  172. /// <summary>
  173. /// Start kill timer.
  174. /// </summary>
  175. /// <param name="callback">Callback action.</param>
  176. /// <param name="intervalMs">Callback interval.</param>
  177. public void StartKillTimer(Action<object?> callback, int intervalMs)
  178. {
  179. if (HasExited)
  180. {
  181. return;
  182. }
  183. lock (_timerLock)
  184. {
  185. if (KillTimer == null)
  186. {
  187. Logger.LogDebug("Starting kill timer at {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
  188. KillTimer = new Timer(new TimerCallback(callback), this, intervalMs, Timeout.Infinite);
  189. }
  190. else
  191. {
  192. Logger.LogDebug("Changing kill timer to {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
  193. KillTimer.Change(intervalMs, Timeout.Infinite);
  194. }
  195. }
  196. }
  197. /// <summary>
  198. /// Change kill timer if started.
  199. /// </summary>
  200. public void ChangeKillTimerIfStarted()
  201. {
  202. if (HasExited)
  203. {
  204. return;
  205. }
  206. lock (_timerLock)
  207. {
  208. if (KillTimer != null)
  209. {
  210. var intervalMs = PingTimeout;
  211. Logger.LogDebug("Changing kill timer to {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
  212. KillTimer.Change(intervalMs, Timeout.Infinite);
  213. }
  214. }
  215. }
  216. /// <inheritdoc />
  217. public void Dispose()
  218. {
  219. Dispose(true);
  220. GC.SuppressFinalize(this);
  221. }
  222. /// <summary>
  223. /// Dispose all resources.
  224. /// </summary>
  225. /// <param name="disposing">Whether to dispose all resources.</param>
  226. protected virtual void Dispose(bool disposing)
  227. {
  228. if (disposing)
  229. {
  230. Process?.Dispose();
  231. Process = null;
  232. KillTimer?.Dispose();
  233. KillTimer = null;
  234. CancellationTokenSource?.Dispose();
  235. CancellationTokenSource = null;
  236. TranscodingThrottler?.Dispose();
  237. TranscodingThrottler = null;
  238. }
  239. }
  240. }
  241. }