ProgressiveStreamWriter.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /// <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. 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 StreamFile(Path, responseStream).ConfigureAwait(false);
  57. }
  58. catch (Exception ex)
  59. {
  60. Logger.ErrorException("Error streaming media", ex);
  61. throw;
  62. }
  63. finally
  64. {
  65. ApiEntryPoint.Instance.OnTranscodeEndRequest(Path, TranscodingJobType.Progressive);
  66. }
  67. }
  68. /// <summary>
  69. /// Streams the file.
  70. /// </summary>
  71. /// <param name="path">The path.</param>
  72. /// <param name="outputStream">The output stream.</param>
  73. /// <returns>Task{System.Boolean}.</returns>
  74. private async Task StreamFile(string path, Stream outputStream)
  75. {
  76. var eofCount = 0;
  77. long position = 0;
  78. using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  79. {
  80. while (eofCount < 15)
  81. {
  82. await fs.CopyToAsync(outputStream).ConfigureAwait(false);
  83. var fsPosition = fs.Position;
  84. var bytesRead = fsPosition - position;
  85. //Logger.Debug("Streamed {0} bytes from file {1}", bytesRead, path);
  86. if (bytesRead == 0)
  87. {
  88. eofCount++;
  89. await Task.Delay(100).ConfigureAwait(false);
  90. }
  91. else
  92. {
  93. eofCount = 0;
  94. }
  95. position = fsPosition;
  96. }
  97. }
  98. }
  99. }
  100. }