TranscodingSegmentCleaner.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Common.Configuration;
  8. using MediaBrowser.Model.Configuration;
  9. using MediaBrowser.Model.IO;
  10. using Microsoft.Extensions.Logging;
  11. namespace MediaBrowser.Controller.MediaEncoding;
  12. /// <summary>
  13. /// Transcoding segment cleaner.
  14. /// </summary>
  15. public class TranscodingSegmentCleaner : IDisposable
  16. {
  17. private readonly TranscodingJob _job;
  18. private readonly ILogger<TranscodingSegmentCleaner> _logger;
  19. private readonly IConfigurationManager _config;
  20. private readonly IFileSystem _fileSystem;
  21. private readonly IMediaEncoder _mediaEncoder;
  22. private Timer? _timer;
  23. private int _segmentLength;
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="TranscodingSegmentCleaner"/> class.
  26. /// </summary>
  27. /// <param name="job">Transcoding job dto.</param>
  28. /// <param name="logger">Instance of the <see cref="ILogger{TranscodingSegmentCleaner}"/> interface.</param>
  29. /// <param name="config">Instance of the <see cref="IConfigurationManager"/> interface.</param>
  30. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  31. /// <param name="mediaEncoder">Instance of the <see cref="IMediaEncoder"/> interface.</param>
  32. /// <param name="segmentLength">The segment length of this transcoding job.</param>
  33. public TranscodingSegmentCleaner(TranscodingJob job, ILogger<TranscodingSegmentCleaner> logger, IConfigurationManager config, IFileSystem fileSystem, IMediaEncoder mediaEncoder, int segmentLength)
  34. {
  35. _job = job;
  36. _logger = logger;
  37. _config = config;
  38. _fileSystem = fileSystem;
  39. _mediaEncoder = mediaEncoder;
  40. _segmentLength = segmentLength;
  41. }
  42. /// <summary>
  43. /// Start timer.
  44. /// </summary>
  45. public void Start()
  46. {
  47. _timer = new Timer(TimerCallback, null, 20000, 20000);
  48. }
  49. /// <summary>
  50. /// Stop cleaner.
  51. /// </summary>
  52. public void Stop()
  53. {
  54. DisposeTimer();
  55. }
  56. /// <summary>
  57. /// Dispose cleaner.
  58. /// </summary>
  59. public void Dispose()
  60. {
  61. Dispose(true);
  62. GC.SuppressFinalize(this);
  63. }
  64. /// <summary>
  65. /// Dispose cleaner.
  66. /// </summary>
  67. /// <param name="disposing">Disposing.</param>
  68. protected virtual void Dispose(bool disposing)
  69. {
  70. if (disposing)
  71. {
  72. DisposeTimer();
  73. }
  74. }
  75. private EncodingOptions GetOptions()
  76. {
  77. return _config.GetEncodingOptions();
  78. }
  79. private async void TimerCallback(object? state)
  80. {
  81. if (_job.HasExited)
  82. {
  83. DisposeTimer();
  84. return;
  85. }
  86. var options = GetOptions();
  87. var enableSegmentDeletion = options.EnableSegmentDeletion;
  88. var segmentKeepSeconds = Math.Max(options.SegmentKeepSeconds, 20);
  89. if (enableSegmentDeletion)
  90. {
  91. var downloadPositionTicks = _job.DownloadPositionTicks ?? 0;
  92. var downloadPositionSeconds = Convert.ToInt64(TimeSpan.FromTicks(downloadPositionTicks).TotalSeconds);
  93. if (downloadPositionSeconds > 0 && segmentKeepSeconds > 0 && downloadPositionSeconds > segmentKeepSeconds)
  94. {
  95. var idxMaxToRemove = (downloadPositionSeconds - segmentKeepSeconds) / _segmentLength;
  96. if (idxMaxToRemove > 0)
  97. {
  98. await DeleteSegmentFiles(_job, 0, idxMaxToRemove, 0, 1500).ConfigureAwait(false);
  99. }
  100. }
  101. }
  102. }
  103. private async Task DeleteSegmentFiles(TranscodingJob job, long idxMin, long idxMax, int retryCount, int delayMs)
  104. {
  105. if (retryCount >= 10)
  106. {
  107. return;
  108. }
  109. var path = job.Path ?? throw new ArgumentException("Path can't be null.");
  110. _logger.LogDebug("Deleting segment file(s) index {Min} to {Max} from {Path}", idxMin, idxMax, path);
  111. await Task.Delay(delayMs).ConfigureAwait(false);
  112. try
  113. {
  114. if (job.Type == TranscodingJobType.Hls)
  115. {
  116. DeleteHlsSegmentFiles(path, idxMin, idxMax);
  117. }
  118. }
  119. catch (IOException ex)
  120. {
  121. _logger.LogError(ex, "Error deleting segment file(s) {Path}", path);
  122. await DeleteSegmentFiles(job, idxMin, idxMax, retryCount + 1, 500).ConfigureAwait(false);
  123. }
  124. catch (Exception ex)
  125. {
  126. _logger.LogError(ex, "Error deleting segment file(s) {Path}", path);
  127. }
  128. }
  129. private void DeleteHlsSegmentFiles(string outputFilePath, long idxMin, long idxMax)
  130. {
  131. var directory = Path.GetDirectoryName(outputFilePath)
  132. ?? throw new ArgumentException("Path can't be a root directory.", nameof(outputFilePath));
  133. var name = Path.GetFileNameWithoutExtension(outputFilePath);
  134. var filesToDelete = _fileSystem.GetFilePaths(directory)
  135. .Where(f => long.TryParse(Path.GetFileNameWithoutExtension(f).Replace(name, string.Empty, StringComparison.Ordinal), out var idx) && idx >= idxMin && idx <= idxMax);
  136. List<Exception>? exs = null;
  137. foreach (var file in filesToDelete)
  138. {
  139. try
  140. {
  141. _logger.LogDebug("Deleting HLS segment file {0}", file);
  142. _fileSystem.DeleteFile(file);
  143. }
  144. catch (IOException ex)
  145. {
  146. (exs ??= new List<Exception>(4)).Add(ex);
  147. _logger.LogError(ex, "Error deleting HLS segment file {Path}", file);
  148. }
  149. }
  150. if (exs is not null)
  151. {
  152. throw new AggregateException("Error deleting HLS segment files", exs);
  153. }
  154. }
  155. private void DisposeTimer()
  156. {
  157. if (_timer is not null)
  158. {
  159. _timer.Dispose();
  160. _timer = null;
  161. }
  162. }
  163. }