TranscodingThrottler.cs 4.6 KB

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