ProgressiveStreamWriter.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Model.Logging;
  3. using ServiceStack.Service;
  4. using ServiceStack.ServiceHost;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Threading.Tasks;
  9. namespace MediaBrowser.Api.Playback.Progressive
  10. {
  11. public class ProgressiveStreamWriter : IStreamWriter, IHasOptions
  12. {
  13. private string Path { get; set; }
  14. private ILogger Logger { get; set; }
  15. private readonly IFileSystem _fileSystem;
  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. public ProgressiveStreamWriter(string path, ILogger logger, IFileSystem fileSystem)
  34. {
  35. Path = path;
  36. Logger = logger;
  37. _fileSystem = fileSystem;
  38. }
  39. /// <summary>
  40. /// Writes to.
  41. /// </summary>
  42. /// <param name="responseStream">The response stream.</param>
  43. public void WriteTo(Stream responseStream)
  44. {
  45. var task = WriteToAsync(responseStream);
  46. Task.WaitAll(task);
  47. }
  48. /// <summary>
  49. /// Writes to async.
  50. /// </summary>
  51. /// <param name="responseStream">The response stream.</param>
  52. /// <returns>Task.</returns>
  53. public async Task WriteToAsync(Stream responseStream)
  54. {
  55. try
  56. {
  57. await StreamFile(Path, responseStream).ConfigureAwait(false);
  58. }
  59. catch (Exception ex)
  60. {
  61. Logger.ErrorException("Error streaming media", ex);
  62. throw;
  63. }
  64. finally
  65. {
  66. ApiEntryPoint.Instance.OnTranscodeEndRequest(Path, TranscodingJobType.Progressive);
  67. }
  68. }
  69. /// <summary>
  70. /// Streams the file.
  71. /// </summary>
  72. /// <param name="path">The path.</param>
  73. /// <param name="outputStream">The output stream.</param>
  74. /// <returns>Task{System.Boolean}.</returns>
  75. private async Task StreamFile(string path, Stream outputStream)
  76. {
  77. var eofCount = 0;
  78. long position = 0;
  79. using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  80. {
  81. while (eofCount < 15)
  82. {
  83. await fs.CopyToAsync(outputStream).ConfigureAwait(false);
  84. var fsPosition = fs.Position;
  85. var bytesRead = fsPosition - position;
  86. //Logger.Debug("Streamed {0} bytes from file {1}", bytesRead, path);
  87. if (bytesRead == 0)
  88. {
  89. eofCount++;
  90. await Task.Delay(100).ConfigureAwait(false);
  91. }
  92. else
  93. {
  94. eofCount = 0;
  95. }
  96. position = fsPosition;
  97. }
  98. }
  99. }
  100. }
  101. }