TranscodingSegmentCleaner.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 idxMaxToDelete = (downloadPositionSeconds - segmentKeepSeconds) / _segmentLength;
  96. if (idxMaxToDelete > 0)
  97. {
  98. await DeleteSegmentFiles(_job, 0, idxMaxToDelete, 1500).ConfigureAwait(false);
  99. }
  100. }
  101. }
  102. }
  103. private async Task DeleteSegmentFiles(TranscodingJob job, long idxMin, long idxMax, int delayMs)
  104. {
  105. var path = job.Path ?? throw new ArgumentException("Path can't be null.");
  106. _logger.LogDebug("Deleting segment file(s) index {Min} to {Max} from {Path}", idxMin, idxMax, path);
  107. await Task.Delay(delayMs).ConfigureAwait(false);
  108. try
  109. {
  110. if (job.Type == TranscodingJobType.Hls)
  111. {
  112. DeleteHlsSegmentFiles(path, idxMin, idxMax);
  113. }
  114. }
  115. catch (Exception ex)
  116. {
  117. _logger.LogDebug(ex, "Error deleting segment file(s) {Path}", path);
  118. }
  119. }
  120. private void DeleteHlsSegmentFiles(string outputFilePath, long idxMin, long idxMax)
  121. {
  122. var directory = Path.GetDirectoryName(outputFilePath)
  123. ?? throw new ArgumentException("Path can't be a root directory.", nameof(outputFilePath));
  124. var name = Path.GetFileNameWithoutExtension(outputFilePath);
  125. var filesToDelete = _fileSystem.GetFilePaths(directory)
  126. .Where(f => long.TryParse(Path.GetFileNameWithoutExtension(f).Replace(name, string.Empty, StringComparison.Ordinal), out var idx)
  127. && (idx >= idxMin && idx <= idxMax));
  128. List<Exception>? exs = null;
  129. foreach (var file in filesToDelete)
  130. {
  131. try
  132. {
  133. _logger.LogDebug("Deleting HLS segment file {0}", file);
  134. _fileSystem.DeleteFile(file);
  135. }
  136. catch (IOException ex)
  137. {
  138. (exs ??= new List<Exception>()).Add(ex);
  139. _logger.LogDebug(ex, "Error deleting HLS segment file {Path}", file);
  140. }
  141. }
  142. if (exs is not null)
  143. {
  144. throw new AggregateException("Error deleting HLS segment files", exs);
  145. }
  146. }
  147. private void DisposeTimer()
  148. {
  149. if (_timer is not null)
  150. {
  151. _timer.Dispose();
  152. _timer = null;
  153. }
  154. }
  155. }