2
0

ProgressiveStreamWriter.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. using MediaBrowser.Controller.Library;
  11. namespace MediaBrowser.Api.Playback.Progressive
  12. {
  13. public class ProgressiveFileCopier : IAsyncStreamSource, IHasOptions
  14. {
  15. private readonly IFileSystem _fileSystem;
  16. private readonly TranscodingJob _job;
  17. private readonly ILogger _logger;
  18. private readonly string _path;
  19. private readonly CancellationToken _cancellationToken;
  20. private readonly Dictionary<string, string> _outputHeaders;
  21. // 256k
  22. private const int BufferSize = 81920;
  23. private long _bytesWritten = 0;
  24. public long StartPosition { get; set; }
  25. public bool AllowEndOfFile = true;
  26. private 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> Options
  45. {
  46. get
  47. {
  48. return _outputHeaders;
  49. }
  50. }
  51. private Stream GetInputStream()
  52. {
  53. return _fileSystem.GetFileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true);
  54. }
  55. public async Task WriteToAsync(Stream outputStream)
  56. {
  57. try
  58. {
  59. if (_directStreamProvider != null)
  60. {
  61. await _directStreamProvider.CopyToAsync(outputStream, _cancellationToken).ConfigureAwait(false);
  62. return;
  63. }
  64. var eofCount = 0;
  65. using (var inputStream = GetInputStream())
  66. {
  67. if (StartPosition > 0)
  68. {
  69. inputStream.Position = StartPosition;
  70. }
  71. while (eofCount < 15 || !AllowEndOfFile)
  72. {
  73. var bytesRead = await CopyToAsyncInternal(inputStream, outputStream, BufferSize, _cancellationToken).ConfigureAwait(false);
  74. //var position = fs.Position;
  75. //_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
  76. if (bytesRead == 0)
  77. {
  78. if (_job == null || _job.HasExited)
  79. {
  80. eofCount++;
  81. }
  82. await Task.Delay(100, _cancellationToken).ConfigureAwait(false);
  83. }
  84. else
  85. {
  86. eofCount = 0;
  87. }
  88. }
  89. }
  90. }
  91. finally
  92. {
  93. if (_job != null)
  94. {
  95. ApiEntryPoint.Instance.OnTranscodeEndRequest(_job);
  96. }
  97. }
  98. }
  99. private async Task<int> CopyToAsyncInternal(Stream source, Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
  100. {
  101. byte[] buffer = new byte[bufferSize];
  102. int bytesRead;
  103. int totalBytesRead = 0;
  104. while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
  105. {
  106. await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
  107. _bytesWritten += bytesRead;
  108. totalBytesRead += bytesRead;
  109. if (_job != null)
  110. {
  111. _job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten);
  112. }
  113. }
  114. return totalBytesRead;
  115. }
  116. }
  117. }