ProgressiveStreamWriter.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using MediaBrowser.Model.Logging;
  2. using ServiceStack.Web;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using CommonIO;
  9. namespace MediaBrowser.Api.Playback.Progressive
  10. {
  11. public class ProgressiveFileCopier
  12. {
  13. private readonly IFileSystem _fileSystem;
  14. private readonly TranscodingJob _job;
  15. private readonly ILogger _logger;
  16. // 256k
  17. private const int BufferSize = 262144;
  18. private long _bytesWritten = 0;
  19. public ProgressiveFileCopier(IFileSystem fileSystem, TranscodingJob job, ILogger logger)
  20. {
  21. _fileSystem = fileSystem;
  22. _job = job;
  23. _logger = logger;
  24. }
  25. public async Task StreamFile(string path, Stream outputStream, CancellationToken cancellationToken)
  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. private async Task<int> CopyToAsyncInternal(Stream source, Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
  51. {
  52. byte[] buffer = new byte[bufferSize];
  53. int bytesRead;
  54. int totalBytesRead = 0;
  55. while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
  56. {
  57. await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
  58. _bytesWritten += bytesRead;
  59. totalBytesRead += bytesRead;
  60. if (_job != null)
  61. {
  62. _job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten);
  63. }
  64. }
  65. return totalBytesRead;
  66. }
  67. }
  68. }