TranscodingJob.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. using MediaBrowser.Api.Playback;
  5. using MediaBrowser.Controller.MediaEncoding;
  6. using MediaBrowser.Model.Dto;
  7. using Microsoft.Extensions.Logging;
  8. namespace MediaBrowser.Api
  9. {
  10. /// <summary>
  11. /// Class TranscodingJob.
  12. /// </summary>
  13. public class TranscodingJob
  14. {
  15. /// <summary>
  16. /// Gets or sets the play session identifier.
  17. /// </summary>
  18. /// <value>The play session identifier.</value>
  19. public string PlaySessionId { get; set; }
  20. /// <summary>
  21. /// Gets or sets the live stream identifier.
  22. /// </summary>
  23. /// <value>The live stream identifier.</value>
  24. public string LiveStreamId { get; set; }
  25. public bool IsLiveOutput { get; set; }
  26. /// <summary>
  27. /// Gets or sets the path.
  28. /// </summary>
  29. /// <value>The path.</value>
  30. public MediaSourceInfo MediaSource { get; set; }
  31. public string Path { get; set; }
  32. /// <summary>
  33. /// Gets or sets the type.
  34. /// </summary>
  35. /// <value>The type.</value>
  36. public TranscodingJobType Type { get; set; }
  37. /// <summary>
  38. /// Gets or sets the process.
  39. /// </summary>
  40. /// <value>The process.</value>
  41. public Process Process { get; set; }
  42. public ILogger Logger { get; private set; }
  43. /// <summary>
  44. /// Gets or sets the active request count.
  45. /// </summary>
  46. /// <value>The active request count.</value>
  47. public int ActiveRequestCount { get; set; }
  48. /// <summary>
  49. /// Gets or sets the kill timer.
  50. /// </summary>
  51. /// <value>The kill timer.</value>
  52. private Timer KillTimer { get; set; }
  53. public string DeviceId { get; set; }
  54. public CancellationTokenSource CancellationTokenSource { get; set; }
  55. public object ProcessLock = new object();
  56. public bool HasExited { get; set; }
  57. public bool IsUserPaused { get; set; }
  58. public string Id { get; set; }
  59. public float? Framerate { get; set; }
  60. public double? CompletionPercentage { get; set; }
  61. public long? BytesDownloaded { get; set; }
  62. public long? BytesTranscoded { get; set; }
  63. public int? BitRate { get; set; }
  64. public long? TranscodingPositionTicks { get; set; }
  65. public long? DownloadPositionTicks { get; set; }
  66. public TranscodingThrottler TranscodingThrottler { get; set; }
  67. private readonly object _timerLock = new object();
  68. public DateTime LastPingDate { get; set; }
  69. public int PingTimeout { get; set; }
  70. public TranscodingJob(ILogger logger)
  71. {
  72. Logger = logger;
  73. }
  74. public void StopKillTimer()
  75. {
  76. lock (_timerLock)
  77. {
  78. if (KillTimer != null)
  79. {
  80. KillTimer.Change(Timeout.Infinite, Timeout.Infinite);
  81. }
  82. }
  83. }
  84. public void DisposeKillTimer()
  85. {
  86. lock (_timerLock)
  87. {
  88. if (KillTimer != null)
  89. {
  90. KillTimer.Dispose();
  91. KillTimer = null;
  92. }
  93. }
  94. }
  95. public void StartKillTimer(Action<object> callback)
  96. {
  97. StartKillTimer(callback, PingTimeout);
  98. }
  99. public void StartKillTimer(Action<object> callback, int intervalMs)
  100. {
  101. if (HasExited)
  102. {
  103. return;
  104. }
  105. lock (_timerLock)
  106. {
  107. if (KillTimer == null)
  108. {
  109. Logger.LogDebug("Starting kill timer at {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
  110. KillTimer = new Timer(new TimerCallback(callback), this, intervalMs, Timeout.Infinite);
  111. }
  112. else
  113. {
  114. Logger.LogDebug("Changing kill timer to {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
  115. KillTimer.Change(intervalMs, Timeout.Infinite);
  116. }
  117. }
  118. }
  119. public void ChangeKillTimerIfStarted()
  120. {
  121. if (HasExited)
  122. {
  123. return;
  124. }
  125. lock (_timerLock)
  126. {
  127. if (KillTimer != null)
  128. {
  129. var intervalMs = PingTimeout;
  130. Logger.LogDebug("Changing kill timer to {0}ms. JobId {1} PlaySessionId {2}", intervalMs, Id, PlaySessionId);
  131. KillTimer.Change(intervalMs, Timeout.Infinite);
  132. }
  133. }
  134. }
  135. }
  136. }