ProgressiveStreamWriter.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using MediaBrowser.Model.Logging;
  2. using System;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using CommonIO;
  7. namespace MediaBrowser.Api.Playback.Progressive
  8. {
  9. public class ProgressiveFileCopier
  10. {
  11. private readonly IFileSystem _fileSystem;
  12. private readonly TranscodingJob _job;
  13. private readonly ILogger _logger;
  14. // 256k
  15. private const int BufferSize = 81920;
  16. private long _bytesWritten = 0;
  17. public ProgressiveFileCopier(IFileSystem fileSystem, TranscodingJob job, ILogger logger)
  18. {
  19. _fileSystem = fileSystem;
  20. _job = job;
  21. _logger = logger;
  22. }
  23. public async Task StreamFile(string path, Stream outputStream, CancellationToken cancellationToken)
  24. {
  25. try
  26. {
  27. var eofCount = 0;
  28. using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  29. {
  30. while (eofCount < 15)
  31. {
  32. var bytesRead = await CopyToAsyncInternal(fs, outputStream, BufferSize, cancellationToken).ConfigureAwait(false);
  33. //var position = fs.Position;
  34. //_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
  35. if (bytesRead == 0)
  36. {
  37. if (_job == null || _job.HasExited)
  38. {
  39. eofCount++;
  40. }
  41. await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  42. }
  43. else
  44. {
  45. eofCount = 0;
  46. }
  47. }
  48. }
  49. }
  50. finally
  51. {
  52. if (_job != null)
  53. {
  54. ApiEntryPoint.Instance.OnTranscodeEndRequest(_job);
  55. }
  56. }
  57. }
  58. private async Task<int> CopyToAsyncInternal(Stream source, Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
  59. {
  60. byte[] buffer = new byte[bufferSize];
  61. int bytesRead;
  62. int totalBytesRead = 0;
  63. while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
  64. {
  65. await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
  66. _bytesWritten += bytesRead;
  67. totalBytesRead += bytesRead;
  68. if (_job != null)
  69. {
  70. _job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten);
  71. }
  72. }
  73. return totalBytesRead;
  74. }
  75. }
  76. }