ProgressiveStreamWriter.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using MediaBrowser.Model.Logging;
  2. using System;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Model.IO;
  7. using MediaBrowser.Controller.Net;
  8. using System.Collections.Generic;
  9. using MediaBrowser.Common.IO;
  10. using MediaBrowser.Controller.IO;
  11. using MediaBrowser.Controller.Library;
  12. using MediaBrowser.Model.Services;
  13. namespace MediaBrowser.Api.Playback.Progressive
  14. {
  15. public class ProgressiveFileCopier : IAsyncStreamWriter, IHasHeaders
  16. {
  17. private readonly IFileSystem _fileSystem;
  18. private readonly TranscodingJob _job;
  19. private readonly ILogger _logger;
  20. private readonly string _path;
  21. private readonly CancellationToken _cancellationToken;
  22. private readonly Dictionary<string, string> _outputHeaders;
  23. // 256k
  24. private const int BufferSize = 81920;
  25. private long _bytesWritten = 0;
  26. public long StartPosition { get; set; }
  27. public bool AllowEndOfFile = true;
  28. private readonly IDirectStreamProvider _directStreamProvider;
  29. public ProgressiveFileCopier(IFileSystem fileSystem, string path, Dictionary<string, string> outputHeaders, TranscodingJob job, ILogger logger, CancellationToken cancellationToken)
  30. {
  31. _fileSystem = fileSystem;
  32. _path = path;
  33. _outputHeaders = outputHeaders;
  34. _job = job;
  35. _logger = logger;
  36. _cancellationToken = cancellationToken;
  37. }
  38. public ProgressiveFileCopier(IDirectStreamProvider directStreamProvider, Dictionary<string, string> outputHeaders, TranscodingJob job, ILogger logger, CancellationToken cancellationToken)
  39. {
  40. _directStreamProvider = directStreamProvider;
  41. _outputHeaders = outputHeaders;
  42. _job = job;
  43. _logger = logger;
  44. _cancellationToken = cancellationToken;
  45. }
  46. public IDictionary<string, string> Headers
  47. {
  48. get
  49. {
  50. return _outputHeaders;
  51. }
  52. }
  53. private Stream GetInputStream()
  54. {
  55. return _fileSystem.GetFileStream(_path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite, true);
  56. }
  57. public async Task WriteToAsync(Stream outputStream, CancellationToken cancellationToken)
  58. {
  59. try
  60. {
  61. if (_directStreamProvider != null)
  62. {
  63. await _directStreamProvider.CopyToAsync(outputStream, _cancellationToken).ConfigureAwait(false);
  64. return;
  65. }
  66. var eofCount = 0;
  67. using (var inputStream = GetInputStream())
  68. {
  69. if (StartPosition > 0)
  70. {
  71. inputStream.Position = StartPosition;
  72. }
  73. while (eofCount < 15 || !AllowEndOfFile)
  74. {
  75. var bytesRead = await CopyToAsyncInternal(inputStream, outputStream, BufferSize, _cancellationToken).ConfigureAwait(false);
  76. //var position = fs.Position;
  77. //_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
  78. if (bytesRead == 0)
  79. {
  80. if (_job == null || _job.HasExited)
  81. {
  82. eofCount++;
  83. }
  84. await Task.Delay(100, _cancellationToken).ConfigureAwait(false);
  85. }
  86. else
  87. {
  88. eofCount = 0;
  89. }
  90. }
  91. }
  92. }
  93. finally
  94. {
  95. if (_job != null)
  96. {
  97. ApiEntryPoint.Instance.OnTranscodeEndRequest(_job);
  98. }
  99. }
  100. }
  101. private async Task<int> CopyToAsyncInternal(Stream source, Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
  102. {
  103. byte[] buffer = new byte[bufferSize];
  104. int bytesRead;
  105. int totalBytesRead = 0;
  106. while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
  107. {
  108. await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
  109. _bytesWritten += bytesRead;
  110. totalBytesRead += bytesRead;
  111. if (_job != null)
  112. {
  113. _job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten);
  114. }
  115. }
  116. return totalBytesRead;
  117. }
  118. }
  119. }