ProgressiveStreamWriter.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Model.IO;
  8. using MediaBrowser.Model.Services;
  9. using MediaBrowser.Model.System;
  10. using Microsoft.Extensions.Logging;
  11. using OperatingSystem = MediaBrowser.Common.System.OperatingSystem;
  12. namespace MediaBrowser.Api.Playback.Progressive
  13. {
  14. public class ProgressiveFileCopier : IAsyncStreamWriter, IHasHeaders
  15. {
  16. private readonly IFileSystem _fileSystem;
  17. private readonly TranscodingJob _job;
  18. private readonly ILogger _logger;
  19. private readonly string _path;
  20. private readonly CancellationToken _cancellationToken;
  21. private readonly Dictionary<string, string> _outputHeaders;
  22. const int StreamCopyToBufferSize = 81920;
  23. private long _bytesWritten = 0;
  24. public long StartPosition { get; set; }
  25. public bool AllowEndOfFile = true;
  26. private readonly IDirectStreamProvider _directStreamProvider;
  27. public ProgressiveFileCopier(IFileSystem fileSystem, string path, Dictionary<string, string> outputHeaders, TranscodingJob job, ILogger logger, CancellationToken cancellationToken)
  28. {
  29. _fileSystem = fileSystem;
  30. _path = path;
  31. _outputHeaders = outputHeaders;
  32. _job = job;
  33. _logger = logger;
  34. _cancellationToken = cancellationToken;
  35. }
  36. public ProgressiveFileCopier(IDirectStreamProvider directStreamProvider, Dictionary<string, string> outputHeaders, TranscodingJob job, ILogger logger, CancellationToken cancellationToken)
  37. {
  38. _directStreamProvider = directStreamProvider;
  39. _outputHeaders = outputHeaders;
  40. _job = job;
  41. _logger = logger;
  42. _cancellationToken = cancellationToken;
  43. }
  44. public IDictionary<string, string> Headers => _outputHeaders;
  45. private Stream GetInputStream(bool allowAsyncFileRead)
  46. {
  47. var fileOpenOptions = FileOpenOptions.SequentialScan;
  48. if (allowAsyncFileRead)
  49. {
  50. fileOpenOptions |= FileOpenOptions.Asynchronous;
  51. }
  52. return _fileSystem.GetFileStream(_path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite, fileOpenOptions);
  53. }
  54. public async Task WriteToAsync(Stream outputStream, CancellationToken cancellationToken)
  55. {
  56. cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _cancellationToken).Token;
  57. try
  58. {
  59. if (_directStreamProvider != null)
  60. {
  61. await _directStreamProvider.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false);
  62. return;
  63. }
  64. var eofCount = 0;
  65. // use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
  66. var allowAsyncFileRead = OperatingSystem.Id != OperatingSystemId.Windows;
  67. using (var inputStream = GetInputStream(allowAsyncFileRead))
  68. {
  69. if (StartPosition > 0)
  70. {
  71. inputStream.Position = StartPosition;
  72. }
  73. while (eofCount < 20 || !AllowEndOfFile)
  74. {
  75. int bytesRead;
  76. if (allowAsyncFileRead)
  77. {
  78. bytesRead = await CopyToInternalAsync(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
  79. }
  80. else
  81. {
  82. bytesRead = await CopyToInternalAsyncWithSyncRead(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
  83. }
  84. //var position = fs.Position;
  85. //_logger.LogDebug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
  86. if (bytesRead == 0)
  87. {
  88. if (_job == null || _job.HasExited)
  89. {
  90. eofCount++;
  91. }
  92. await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  93. }
  94. else
  95. {
  96. eofCount = 0;
  97. }
  98. }
  99. }
  100. }
  101. finally
  102. {
  103. if (_job != null)
  104. {
  105. ApiEntryPoint.Instance.OnTranscodeEndRequest(_job);
  106. }
  107. }
  108. }
  109. private async Task<int> CopyToInternalAsyncWithSyncRead(Stream source, Stream destination, CancellationToken cancellationToken)
  110. {
  111. var array = new byte[StreamCopyToBufferSize];
  112. int bytesRead;
  113. int totalBytesRead = 0;
  114. while ((bytesRead = source.Read(array, 0, array.Length)) != 0)
  115. {
  116. var bytesToWrite = bytesRead;
  117. if (bytesToWrite > 0)
  118. {
  119. await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
  120. _bytesWritten += bytesRead;
  121. totalBytesRead += bytesRead;
  122. if (_job != null)
  123. {
  124. _job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten);
  125. }
  126. }
  127. }
  128. return totalBytesRead;
  129. }
  130. private async Task<int> CopyToInternalAsync(Stream source, Stream destination, CancellationToken cancellationToken)
  131. {
  132. var array = new byte[StreamCopyToBufferSize];
  133. int bytesRead;
  134. int totalBytesRead = 0;
  135. while ((bytesRead = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false)) != 0)
  136. {
  137. var bytesToWrite = bytesRead;
  138. if (bytesToWrite > 0)
  139. {
  140. await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
  141. _bytesWritten += bytesRead;
  142. totalBytesRead += bytesRead;
  143. if (_job != null)
  144. {
  145. _job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten);
  146. }
  147. }
  148. }
  149. return totalBytesRead;
  150. }
  151. }
  152. }