ProgressiveStreamWriter.cs 3.5 KB

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