ProgressiveStreamWriter.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. using MediaBrowser.Controller.Net;
  8. using System.Collections.Generic;
  9. using ServiceStack.Web;
  10. namespace MediaBrowser.Api.Playback.Progressive
  11. {
  12. public class ProgressiveFileCopier : IAsyncStreamSource, IHasOptions
  13. {
  14. private readonly IFileSystem _fileSystem;
  15. private readonly TranscodingJob _job;
  16. private readonly ILogger _logger;
  17. private readonly string _path;
  18. private readonly CancellationToken _cancellationToken;
  19. private readonly Dictionary<string, string> _outputHeaders;
  20. // 256k
  21. private const int BufferSize = 81920;
  22. private long _bytesWritten = 0;
  23. public long StartPosition { get; set; }
  24. public bool AllowEndOfFile = true;
  25. public ProgressiveFileCopier(IFileSystem fileSystem, string path, Dictionary<string, string> outputHeaders, TranscodingJob job, ILogger logger, CancellationToken cancellationToken)
  26. {
  27. _fileSystem = fileSystem;
  28. _path = path;
  29. _outputHeaders = outputHeaders;
  30. _job = job;
  31. _logger = logger;
  32. _cancellationToken = cancellationToken;
  33. }
  34. public IDictionary<string, string> Options
  35. {
  36. get
  37. {
  38. return _outputHeaders;
  39. }
  40. }
  41. public async Task WriteToAsync(Stream outputStream)
  42. {
  43. try
  44. {
  45. var eofCount = 0;
  46. using (var fs = _fileSystem.GetFileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  47. {
  48. if (StartPosition > 0)
  49. {
  50. fs.Position = StartPosition;
  51. }
  52. while (eofCount < 15 || !AllowEndOfFile)
  53. {
  54. var bytesRead = await CopyToAsyncInternal(fs, outputStream, BufferSize, _cancellationToken).ConfigureAwait(false);
  55. //var position = fs.Position;
  56. //_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
  57. if (bytesRead == 0)
  58. {
  59. if (_job == null || _job.HasExited)
  60. {
  61. eofCount++;
  62. }
  63. await Task.Delay(100, _cancellationToken).ConfigureAwait(false);
  64. }
  65. else
  66. {
  67. eofCount = 0;
  68. }
  69. }
  70. }
  71. }
  72. finally
  73. {
  74. if (_job != null)
  75. {
  76. ApiEntryPoint.Instance.OnTranscodeEndRequest(_job);
  77. }
  78. }
  79. }
  80. private async Task<int> CopyToAsyncInternal(Stream source, Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
  81. {
  82. byte[] buffer = new byte[bufferSize];
  83. int bytesRead;
  84. int totalBytesRead = 0;
  85. while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
  86. {
  87. await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
  88. _bytesWritten += bytesRead;
  89. totalBytesRead += bytesRead;
  90. if (_job != null)
  91. {
  92. _job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten);
  93. }
  94. }
  95. return totalBytesRead;
  96. }
  97. }
  98. }