TranscodingJobDto.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Controller.MediaEncoding;
  9. using MediaBrowser.Model.Dto;
  10. using Microsoft.Extensions.Logging;
  11. namespace Jellyfin.Api.Models.PlaybackDtos
  12. {
  13. /// <summary>
  14. /// Class TranscodingJob.
  15. /// </summary>
  16. public class TranscodingJobDto
  17. {
  18. /// <summary>
  19. /// The process lock.
  20. /// </summary>
  21. [SuppressMessage("Microsoft.Performance", "CA1051:NoVisibleInstanceFields", MessageId = "ProcessLock", Justification = "Imported from ServiceStack")]
  22. [SuppressMessage("Microsoft.Performance", "SA1401:PrivateField", MessageId = "ProcessLock", Justification = "Imported from ServiceStack")]
  23. public readonly object ProcessLock = new object();
  24. /// <summary>
  25. /// Timer lock.
  26. /// </summary>
  27. private readonly object _timerLock = new object();
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="TranscodingJobDto"/> class.
  30. /// </summary>
  31. /// <param name="logger">Instance of the <see cref="ILogger{TranscodingJobDto}"/> interface.</param>
  32. public TranscodingJobDto(ILogger<TranscodingJobDto> logger)
  33. {
  34. Logger = logger;
  35. }
  36. /// <summary>
  37. /// Gets or sets the play session identifier.
  38. /// </summary>
  39. /// <value>The play session identifier.</value>
  40. public string? PlaySessionId { get; set; }
  41. /// <summary>
  42. /// Gets or sets the live stream identifier.
  43. /// </summary>
  44. /// <value>The live stream identifier.</value>
  45. public string? LiveStreamId { get; set; }
  46. /// <summary>
  47. /// Gets or sets a value indicating whether is live output.
  48. /// </summary>
  49. public bool IsLiveOutput { get; set; }
  50. /// <summary>
  51. /// Gets or sets the path.
  52. /// </summary>
  53. /// <value>The path.</value>
  54. public MediaSourceInfo? MediaSource { get; set; }
  55. /// <summary>
  56. /// Gets or sets path.
  57. /// </summary>
  58. public string? Path { get; set; }
  59. /// <summary>
  60. /// Gets or sets the type.
  61. /// </summary>
  62. /// <value>The type.</value>
  63. public TranscodingJobType Type { get; set; }
  64. /// <summary>
  65. /// Gets or sets the process.
  66. /// </summary>
  67. /// <value>The process.</value>
  68. public Process? Process { get; set; }
  69. /// <summary>
  70. /// Gets logger.
  71. /// </summary>
  72. public ILogger<TranscodingJobDto> Logger { get; private set; }
  73. /// <summary>
  74. /// Gets or sets the active request count.
  75. /// </summary>
  76. /// <value>The active request count.</value>
  77. public int ActiveRequestCount { get; set; }
  78. /// <summary>
  79. /// Gets or sets the kill timer.
  80. /// </summary>
  81. /// <value>The kill timer.</value>
  82. private Timer? KillTimer { get; set; }
  83. /// <summary>
  84. /// Gets or sets device id.
  85. /// </summary>
  86. public string? DeviceId { get; set; }
  87. /// <summary>
  88. /// Gets or sets cancellation token source.
  89. /// </summary>
  90. public CancellationTokenSource? CancellationTokenSource { get; set; }
  91. /// <summary>
  92. /// Gets or sets a value indicating whether has exited.
  93. /// </summary>
  94. public bool HasExited { 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 != 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 == 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 != 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. }
  220. }