TranscodingThrottler.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. public void Start()
  13. {
  14. _timer = new Timer(TimerCallback, null, 1000, 1000);
  15. }
  16. private void TimerCallback(object state)
  17. {
  18. if (IsThrottleAllowed(_job))
  19. {
  20. PauseTranscoding();
  21. }
  22. else
  23. {
  24. UnpauseTranscoding();
  25. }
  26. }
  27. private void PauseTranscoding()
  28. {
  29. _logger.Debug("Sending pause command to ffmpeg");
  30. _job.Process.StandardInput.WriteLine("p");
  31. }
  32. private void UnpauseTranscoding()
  33. {
  34. _logger.Debug("Sending unpause command to ffmpeg");
  35. _job.Process.StandardInput.WriteLine("u");
  36. }
  37. private readonly long _gapLengthInTicks = TimeSpan.FromMinutes(2).Ticks;
  38. public TranscodingThrottler(TranscodingJob job, ILogger logger)
  39. {
  40. _job = job;
  41. _logger = logger;
  42. }
  43. private bool IsThrottleAllowed(TranscodingJob job)
  44. {
  45. //var job = string.IsNullOrEmpty(request.TranscodingJobId) ?
  46. //null :
  47. //ApiEntryPoint.Instance.GetTranscodingJob(request.TranscodingJobId);
  48. //var limits = new List<long>();
  49. //if (state.InputBitrate.HasValue)
  50. //{
  51. // // Bytes per second
  52. // limits.Add((state.InputBitrate.Value / 8));
  53. //}
  54. //if (state.InputFileSize.HasValue && state.RunTimeTicks.HasValue)
  55. //{
  56. // var totalSeconds = TimeSpan.FromTicks(state.RunTimeTicks.Value).TotalSeconds;
  57. // if (totalSeconds > 1)
  58. // {
  59. // var timeBasedLimit = state.InputFileSize.Value / totalSeconds;
  60. // limits.Add(Convert.ToInt64(timeBasedLimit));
  61. // }
  62. //}
  63. //// Take the greater of the above to methods, just to be safe
  64. //var throttleLimit = limits.Count > 0 ? limits.First() : 0;
  65. //// Pad to play it safe
  66. //var bytesPerSecond = Convert.ToInt64(1.05 * throttleLimit);
  67. //// Don't even start evaluating this until at least two minutes have content have been consumed
  68. //var targetGap = throttleLimit * 120;
  69. var bytesDownloaded = job.BytesDownloaded ?? 0;
  70. var transcodingPositionTicks = job.TranscodingPositionTicks ?? 0;
  71. var downloadPositionTicks = job.DownloadPositionTicks ?? 0;
  72. var path = job.Path;
  73. if (downloadPositionTicks > 0 && transcodingPositionTicks > 0)
  74. {
  75. // HLS - time-based consideration
  76. var targetGap = _gapLengthInTicks;
  77. var gap = transcodingPositionTicks - downloadPositionTicks;
  78. if (gap < targetGap)
  79. {
  80. //Logger.Debug("Not throttling transcoder gap {0} target gap {1}", gap, targetGap);
  81. return false;
  82. }
  83. //Logger.Debug("Throttling transcoder gap {0} target gap {1}", gap, targetGap);
  84. return true;
  85. }
  86. if (bytesDownloaded > 0 && transcodingPositionTicks > 0)
  87. {
  88. // Progressive Streaming - byte-based consideration
  89. try
  90. {
  91. var bytesTranscoded = job.BytesTranscoded ?? new FileInfo(path).Length;
  92. // Estimate the bytes the transcoder should be ahead
  93. double gapFactor = _gapLengthInTicks;
  94. gapFactor /= transcodingPositionTicks;
  95. var targetGap = bytesTranscoded * gapFactor;
  96. var gap = bytesTranscoded - bytesDownloaded;
  97. if (gap < targetGap)
  98. {
  99. //Logger.Debug("Not throttling transcoder gap {0} target gap {1} bytes downloaded {2}", gap, targetGap, bytesDownloaded);
  100. return false;
  101. }
  102. //Logger.Debug("Throttling transcoder gap {0} target gap {1} bytes downloaded {2}", gap, targetGap, bytesDownloaded);
  103. return true;
  104. }
  105. catch
  106. {
  107. //Logger.Error("Error getting output size");
  108. }
  109. }
  110. else
  111. {
  112. //Logger.Debug("No throttle data for " + path);
  113. }
  114. return false;
  115. }
  116. public void Dispose()
  117. {
  118. DisposeTimer();
  119. }
  120. private void DisposeTimer()
  121. {
  122. if (_timer != null)
  123. {
  124. _timer.Dispose();
  125. _timer = null;
  126. }
  127. }
  128. }
  129. }