ProgressiveStreamWriter.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Model.Logging;
  3. using ServiceStack.Web;
  4. using System;
  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. WriteToInternal(responseStream);
  48. }
  49. /// <summary>
  50. /// Writes to async.
  51. /// </summary>
  52. /// <param name="responseStream">The response stream.</param>
  53. /// <returns>Task.</returns>
  54. private void WriteToInternal(Stream responseStream)
  55. {
  56. try
  57. {
  58. new ProgressiveFileCopier(_fileSystem, _job)
  59. .StreamFile(Path, responseStream);
  60. }
  61. catch (Exception ex)
  62. {
  63. Logger.ErrorException("Error streaming media. The client has most likely disconnected or transcoding has failed.", ex);
  64. throw;
  65. }
  66. finally
  67. {
  68. if (_job != null)
  69. {
  70. ApiEntryPoint.Instance.OnTranscodeEndRequest(_job);
  71. }
  72. }
  73. }
  74. }
  75. public class ProgressiveFileCopier
  76. {
  77. private readonly IFileSystem _fileSystem;
  78. private readonly TranscodingJob _job;
  79. private long _bytesWritten = 0;
  80. public ProgressiveFileCopier(IFileSystem fileSystem, TranscodingJob job)
  81. {
  82. _fileSystem = fileSystem;
  83. _job = job;
  84. }
  85. public void StreamFile(string path, Stream outputStream)
  86. {
  87. var eofCount = 0;
  88. long position = 0;
  89. using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, false))
  90. {
  91. while (eofCount < 15)
  92. {
  93. CopyToInternal(fs, outputStream, 81920);
  94. var fsPosition = fs.Position;
  95. var bytesRead = fsPosition - position;
  96. //Logger.Debug("Streamed {0} bytes from file {1}", bytesRead, path);
  97. if (bytesRead == 0)
  98. {
  99. if (_job == null || _job.HasExited)
  100. {
  101. eofCount++;
  102. }
  103. var task = Task.Delay(100);
  104. Task.WaitAll(task);
  105. }
  106. else
  107. {
  108. eofCount = 0;
  109. }
  110. position = fsPosition;
  111. }
  112. }
  113. }
  114. private void CopyToInternal(Stream source, Stream destination, int bufferSize)
  115. {
  116. byte[] array = new byte[bufferSize];
  117. int count;
  118. while ((count = source.Read(array, 0, array.Length)) != 0)
  119. {
  120. destination.Write(array, 0, count);
  121. _bytesWritten += count;
  122. if (_job != null)
  123. {
  124. _job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten);
  125. }
  126. }
  127. }
  128. }
  129. }