ProgressiveStreamWriter.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. /// <param name="fileSystem">The file system.</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
  60. {
  61. Logger.Error("Error streaming media. The client has most likely disconnected or transcoding has failed.");
  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. }