ProgressiveStreamWriter.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 bool AllowEndOfFile = true;
  24. public ProgressiveFileCopier(IFileSystem fileSystem, string path, Dictionary<string, string> outputHeaders, TranscodingJob job, ILogger logger, CancellationToken cancellationToken)
  25. {
  26. _fileSystem = fileSystem;
  27. _path = path;
  28. _outputHeaders = outputHeaders;
  29. _job = job;
  30. _logger = logger;
  31. _cancellationToken = cancellationToken;
  32. }
  33. public IDictionary<string, string> Options
  34. {
  35. get
  36. {
  37. return _outputHeaders;
  38. }
  39. }
  40. public async Task WriteToAsync(Stream outputStream)
  41. {
  42. try
  43. {
  44. var eofCount = 0;
  45. using (var fs = _fileSystem.GetFileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  46. {
  47. while (eofCount < 15 || !AllowEndOfFile)
  48. {
  49. var bytesRead = await CopyToAsyncInternal(fs, outputStream, BufferSize, _cancellationToken).ConfigureAwait(false);
  50. //var position = fs.Position;
  51. //_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
  52. if (bytesRead == 0)
  53. {
  54. if (_job == null || _job.HasExited)
  55. {
  56. eofCount++;
  57. }
  58. await Task.Delay(100, _cancellationToken).ConfigureAwait(false);
  59. }
  60. else
  61. {
  62. eofCount = 0;
  63. }
  64. }
  65. }
  66. }
  67. finally
  68. {
  69. if (_job != null)
  70. {
  71. ApiEntryPoint.Instance.OnTranscodeEndRequest(_job);
  72. }
  73. }
  74. }
  75. private async Task<int> CopyToAsyncInternal(Stream source, Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
  76. {
  77. byte[] buffer = new byte[bufferSize];
  78. int bytesRead;
  79. int totalBytesRead = 0;
  80. while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
  81. {
  82. await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
  83. _bytesWritten += bytesRead;
  84. totalBytesRead += bytesRead;
  85. if (_job != null)
  86. {
  87. _job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten);
  88. }
  89. }
  90. return totalBytesRead;
  91. }
  92. }
  93. }