TranscodingJob.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. using Jellyfin.Api.Models.PlaybackDtos;
  5. using MediaBrowser.Controller.MediaEncoding;
  6. using MediaBrowser.Model.Dto;
  7. using Microsoft.Extensions.Logging;
  8. namespace Jellyfin.Api.Models.TranscodingDtos
  9. {
  10. /// <summary>
  11. /// The transcoding job.
  12. /// </summary>
  13. public class TranscodingJob
  14. {
  15. private readonly ILogger _logger;
  16. private readonly object _timerLock = new object();
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="TranscodingJob"/> class.
  19. /// </summary>
  20. /// <param name="logger">Instance of the <see cref="ILogger{TranscodingJob}"/> interface.</param>
  21. public TranscodingJob(ILogger<TranscodingJob> logger)
  22. {
  23. _logger = logger;
  24. }
  25. /// <summary>
  26. /// Gets or sets the play session identifier.
  27. /// </summary>
  28. /// <value>The play session identifier.</value>
  29. public string? PlaySessionId { get; set; }
  30. /// <summary>
  31. /// Gets or sets the live stream identifier.
  32. /// </summary>
  33. /// <value>The live stream identifier.</value>
  34. public string? LiveStreamId { get; set; }
  35. /// <summary>
  36. /// Gets or sets a value indicating whether the transcoding job is a live output.
  37. /// </summary>
  38. public bool IsLiveOutput { get; set; }
  39. /// <summary>
  40. /// Gets or sets the path.
  41. /// </summary>
  42. /// <value>The path.</value>
  43. public MediaSourceInfo? MediaSource { get; set; }
  44. /// <summary>
  45. /// Gets or sets the transcoding path.
  46. /// </summary>
  47. public string? Path { get; set; }
  48. /// <summary>
  49. /// Gets or sets the type.
  50. /// </summary>
  51. /// <value>The type.</value>
  52. public TranscodingJobType Type { get; set; }
  53. /// <summary>
  54. /// Gets or sets the process.
  55. /// </summary>
  56. /// <value>The process.</value>
  57. public Process? Process { get; set; }
  58. /// <summary>
  59. /// Gets or sets the active request count.
  60. /// </summary>
  61. /// <value>The active request count.</value>
  62. public int ActiveRequestCount { get; set; }
  63. /// <summary>
  64. /// Gets or sets the kill timer.
  65. /// </summary>
  66. /// <value>The kill timer.</value>
  67. private Timer? KillTimer { get; set; }
  68. /// <summary>
  69. /// Gets or sets the device id.
  70. /// </summary>
  71. public string? DeviceId { get; set; }
  72. /// <summary>
  73. /// Gets or sets the cancellation token source.
  74. /// </summary>
  75. public CancellationTokenSource? CancellationTokenSource { get; set; }
  76. /// <summary>
  77. /// Gets or sets a value indicating whether the transcoding job has exited.
  78. /// </summary>
  79. public bool HasExited { get; set; }
  80. /// <summary>
  81. /// Gets or sets a value indicating whether the user has paused the video.
  82. /// </summary>
  83. public bool IsUserPaused { get; set; }
  84. /// <summary>
  85. /// Gets or sets the id.
  86. /// </summary>
  87. public string? Id { get; set; }
  88. /// <summary>
  89. /// Gets or sets the framerate.
  90. /// </summary>
  91. public float? Framerate { get; set; }
  92. /// <summary>
  93. /// Gets or sets the completion percentage.
  94. /// </summary>
  95. public double? CompletionPercentage { get; set; }
  96. /// <summary>
  97. /// Gets or sets the bytes downloaded.
  98. /// </summary>
  99. public long? BytesDownloaded { get; set; }
  100. /// <summary>
  101. /// Gets or sets the bytes transcoded.
  102. /// </summary>
  103. public long? BytesTranscoded { get; set; }
  104. /// <summary>
  105. /// Gets or sets the bitrate.
  106. /// </summary>
  107. public int? BitRate { get; set; }
  108. /// <summary>
  109. /// Gets or sets the transcoding position ticks.
  110. /// </summary>
  111. public long? TranscodingPositionTicks { get; set; }
  112. /// <summary>
  113. /// Gets or sets the download position ticks.
  114. /// </summary>
  115. public long? DownloadPositionTicks { get; set; }
  116. /// <summary>
  117. /// Gets or sets the transcodign throttler.
  118. /// </summary>
  119. public TranscodingThrottler? TranscodingThrottler { get; set; }
  120. /// <summary>
  121. /// Gets or sets the last ping datetime.
  122. /// </summary>
  123. public DateTime LastPingDate { get; set; }
  124. /// <summary>
  125. /// Gets or sets the ping timeout.
  126. /// </summary>
  127. public int PingTimeout { get; set; }
  128. /// <summary>
  129. /// Stops the kill timer.
  130. /// </summary>
  131. public void StopKillTimer()
  132. {
  133. lock (_timerLock)
  134. {
  135. KillTimer?.Change(Timeout.Infinite, Timeout.Infinite);
  136. }
  137. }
  138. /// <summary>
  139. /// Disposes the kill timer.
  140. /// </summary>
  141. public void DisposeKillTimer()
  142. {
  143. lock (_timerLock)
  144. {
  145. if (KillTimer != null)
  146. {
  147. KillTimer.Dispose();
  148. KillTimer = null;
  149. }
  150. }
  151. }
  152. /// <summary>
  153. /// Starts the kill timer.
  154. /// </summary>
  155. /// <param name="callback">The amount of ms the timer should wait before the transcoding job gets killed.</param>
  156. public void StartKillTimer(Action<object> callback)
  157. {
  158. StartKillTimer(callback, PingTimeout);
  159. }
  160. /// <summary>
  161. /// Starts the kill timer.
  162. /// </summary>
  163. /// <param name="callback">The <see cref="Action"/> to run when the kill timer has finished.</param>
  164. /// <param name="intervalMs">The amount of ms the timer should wait before the transcoding job gets killed.</param>
  165. public void StartKillTimer(Action<object> callback, int intervalMs)
  166. {
  167. if (HasExited)
  168. {
  169. return;
  170. }
  171. lock (_timerLock)
  172. {
  173. if (KillTimer == null)
  174. {
  175. _logger.LogDebug($"Starting kill timer at {intervalMs}ms. JobId {Id} PlaySessionId {PlaySessionId}");
  176. KillTimer = new Timer(new TimerCallback(callback), this, intervalMs, Timeout.Infinite);
  177. }
  178. else
  179. {
  180. _logger.LogDebug($"Changing kill timer to {intervalMs}ms. JobId {Id} PlaySessionId {PlaySessionId}");
  181. KillTimer.Change(intervalMs, Timeout.Infinite);
  182. }
  183. }
  184. }
  185. /// <summary>
  186. /// Changes the kill timer if it has started.
  187. /// </summary>
  188. public void ChangeKillTimerIfStarted()
  189. {
  190. if (HasExited)
  191. {
  192. return;
  193. }
  194. lock (_timerLock)
  195. {
  196. if (KillTimer != null)
  197. {
  198. var intervalMs = PingTimeout;
  199. _logger.LogDebug($"Changing kill timer to {intervalMs}ms. JobId {Id} PlaySessionId {PlaySessionId}");
  200. KillTimer.Change(intervalMs, Timeout.Infinite);
  201. }
  202. }
  203. }
  204. }
  205. }