ProgressiveStreamWriter.cs 3.5 KB

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