ProgressiveStreamWriter.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Model.Logging;
  4. using ServiceStack.Web;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Api.Playback.Progressive
  9. {
  10. public class ProgressiveStreamWriter : IStreamWriter, IHasOptions
  11. {
  12. private string Path { get; set; }
  13. private ILogger Logger { get; set; }
  14. private readonly IFileSystem _fileSystem;
  15. private readonly TranscodingJob _job;
  16. /// <summary>
  17. /// The _options
  18. /// </summary>
  19. private readonly IDictionary<string, string> _options = new Dictionary<string, string>();
  20. /// <summary>
  21. /// Gets the options.
  22. /// </summary>
  23. /// <value>The options.</value>
  24. public IDictionary<string, string> Options
  25. {
  26. get { return _options; }
  27. }
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="ProgressiveStreamWriter" /> class.
  30. /// </summary>
  31. /// <param name="path">The path.</param>
  32. /// <param name="logger">The logger.</param>
  33. /// <param name="fileSystem">The file system.</param>
  34. public ProgressiveStreamWriter(string path, ILogger logger, IFileSystem fileSystem, TranscodingJob job)
  35. {
  36. Path = path;
  37. Logger = logger;
  38. _fileSystem = fileSystem;
  39. _job = job;
  40. }
  41. /// <summary>
  42. /// Writes to.
  43. /// </summary>
  44. /// <param name="responseStream">The response stream.</param>
  45. public void WriteTo(Stream responseStream)
  46. {
  47. var task = WriteToAsync(responseStream);
  48. Task.WaitAll(task);
  49. }
  50. /// <summary>
  51. /// Writes to async.
  52. /// </summary>
  53. /// <param name="responseStream">The response stream.</param>
  54. /// <returns>Task.</returns>
  55. public async Task WriteToAsync(Stream responseStream)
  56. {
  57. try
  58. {
  59. await new ProgressiveFileCopier(_fileSystem, _job)
  60. .StreamFile(Path, responseStream).ConfigureAwait(false);
  61. }
  62. catch (Exception ex)
  63. {
  64. Logger.ErrorException("Error streaming media. The client has most likely disconnected or transcoding has failed.", ex);
  65. throw;
  66. }
  67. finally
  68. {
  69. ApiEntryPoint.Instance.OnTranscodeEndRequest(Path, TranscodingJobType.Progressive);
  70. }
  71. }
  72. }
  73. public class ProgressiveFileCopier
  74. {
  75. private readonly IFileSystem _fileSystem;
  76. private readonly TranscodingJob _job;
  77. public ProgressiveFileCopier(IFileSystem fileSystem, TranscodingJob job)
  78. {
  79. _fileSystem = fileSystem;
  80. _job = job;
  81. }
  82. public async Task StreamFile(string path, Stream outputStream)
  83. {
  84. var eofCount = 0;
  85. long position = 0;
  86. using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  87. {
  88. while (eofCount < 15)
  89. {
  90. await fs.CopyToAsync(outputStream).ConfigureAwait(false);
  91. var fsPosition = fs.Position;
  92. var bytesRead = fsPosition - position;
  93. //Logger.Debug("Streamed {0} bytes from file {1}", bytesRead, path);
  94. if (bytesRead == 0)
  95. {
  96. if (_job == null || _job.HasExited)
  97. {
  98. eofCount++;
  99. }
  100. await Task.Delay(100).ConfigureAwait(false);
  101. }
  102. else
  103. {
  104. eofCount = 0;
  105. }
  106. position = fsPosition;
  107. }
  108. }
  109. }
  110. }
  111. }