ProgressiveStreamWriter.cs 3.9 KB

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