TranscodingThrottler.cs 5.3 KB

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