TranscodingThrottler.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Model.Configuration;
  3. using MediaBrowser.Model.Logging;
  4. using System;
  5. using System.IO;
  6. using System.Threading;
  7. namespace MediaBrowser.Api.Playback
  8. {
  9. public class TranscodingThrottler : IDisposable
  10. {
  11. private readonly TranscodingJob _job;
  12. private readonly ILogger _logger;
  13. private Timer _timer;
  14. private bool _isPaused;
  15. private readonly IConfigurationManager _config;
  16. public TranscodingThrottler(TranscodingJob job, ILogger logger, IConfigurationManager config)
  17. {
  18. _job = job;
  19. _logger = logger;
  20. _config = config;
  21. }
  22. private EncodingOptions GetOptions()
  23. {
  24. return _config.GetConfiguration<EncodingOptions>("encoding");
  25. }
  26. public void Start()
  27. {
  28. _timer = new Timer(TimerCallback, null, 5000, 5000);
  29. }
  30. private void TimerCallback(object state)
  31. {
  32. if (_job.HasExited)
  33. {
  34. DisposeTimer();
  35. return;
  36. }
  37. var options = GetOptions();
  38. if (options.EnableThrottling && IsThrottleAllowed(_job, options.ThrottleThresholdSeconds))
  39. {
  40. PauseTranscoding();
  41. }
  42. else
  43. {
  44. UnpauseTranscoding();
  45. }
  46. }
  47. private void PauseTranscoding()
  48. {
  49. if (!_isPaused)
  50. {
  51. _logger.Debug("Sending pause command to ffmpeg");
  52. try
  53. {
  54. _job.Process.StandardInput.Write("c");
  55. _isPaused = true;
  56. }
  57. catch (Exception ex)
  58. {
  59. _logger.ErrorException("Error pausing transcoding", ex);
  60. }
  61. }
  62. }
  63. private void UnpauseTranscoding()
  64. {
  65. if (_isPaused)
  66. {
  67. _logger.Debug("Sending unpause command to ffmpeg");
  68. try
  69. {
  70. _job.Process.StandardInput.WriteLine();
  71. _isPaused = false;
  72. }
  73. catch (Exception ex)
  74. {
  75. _logger.ErrorException("Error unpausing transcoding", ex);
  76. }
  77. }
  78. }
  79. private bool IsThrottleAllowed(TranscodingJob job, int thresholdSeconds)
  80. {
  81. var bytesDownloaded = job.BytesDownloaded ?? 0;
  82. var transcodingPositionTicks = job.TranscodingPositionTicks ?? 0;
  83. var downloadPositionTicks = job.DownloadPositionTicks ?? 0;
  84. var path = job.Path;
  85. var gapLengthInTicks = TimeSpan.FromSeconds(thresholdSeconds).Ticks;
  86. if (downloadPositionTicks > 0 && transcodingPositionTicks > 0)
  87. {
  88. // HLS - time-based consideration
  89. var targetGap = gapLengthInTicks;
  90. var gap = transcodingPositionTicks - downloadPositionTicks;
  91. if (gap < targetGap)
  92. {
  93. //_logger.Debug("Not throttling transcoder gap {0} target gap {1}", gap, targetGap);
  94. return false;
  95. }
  96. //_logger.Debug("Throttling transcoder gap {0} target gap {1}", gap, targetGap);
  97. return true;
  98. }
  99. if (bytesDownloaded > 0 && transcodingPositionTicks > 0)
  100. {
  101. // Progressive Streaming - byte-based consideration
  102. try
  103. {
  104. var bytesTranscoded = job.BytesTranscoded ?? new FileInfo(path).Length;
  105. // Estimate the bytes the transcoder should be ahead
  106. double gapFactor = gapLengthInTicks;
  107. gapFactor /= transcodingPositionTicks;
  108. var targetGap = bytesTranscoded * gapFactor;
  109. var gap = bytesTranscoded - bytesDownloaded;
  110. if (gap < targetGap)
  111. {
  112. //_logger.Debug("Not throttling transcoder gap {0} target gap {1} bytes downloaded {2}", gap, targetGap, bytesDownloaded);
  113. return false;
  114. }
  115. //_logger.Debug("Throttling transcoder gap {0} target gap {1} bytes downloaded {2}", gap, targetGap, bytesDownloaded);
  116. return true;
  117. }
  118. catch
  119. {
  120. //_logger.Error("Error getting output size");
  121. return false;
  122. }
  123. }
  124. //_logger.Debug("No throttle data for " + path);
  125. return false;
  126. }
  127. public void Stop()
  128. {
  129. DisposeTimer();
  130. UnpauseTranscoding();
  131. }
  132. public void Dispose()
  133. {
  134. DisposeTimer();
  135. }
  136. private void DisposeTimer()
  137. {
  138. if (_timer != null)
  139. {
  140. _timer.Dispose();
  141. _timer = null;
  142. }
  143. }
  144. }
  145. }