ProgressiveStreamWriter.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. /// <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)
  33. {
  34. Path = path;
  35. Logger = logger;
  36. }
  37. /// <summary>
  38. /// Writes to.
  39. /// </summary>
  40. /// <param name="responseStream">The response stream.</param>
  41. public void WriteTo(Stream responseStream)
  42. {
  43. var task = WriteToAsync(responseStream);
  44. Task.WaitAll(task);
  45. }
  46. /// <summary>
  47. /// Writes to async.
  48. /// </summary>
  49. /// <param name="responseStream">The response stream.</param>
  50. /// <returns>Task.</returns>
  51. public async Task WriteToAsync(Stream responseStream)
  52. {
  53. try
  54. {
  55. await StreamFile(Path, responseStream).ConfigureAwait(false);
  56. }
  57. catch (Exception ex)
  58. {
  59. Logger.ErrorException("Error streaming media", ex);
  60. throw;
  61. }
  62. finally
  63. {
  64. ApiEntryPoint.Instance.OnTranscodeEndRequest(Path, TranscodingJobType.Progressive);
  65. }
  66. }
  67. /// <summary>
  68. /// Streams the file.
  69. /// </summary>
  70. /// <param name="path">The path.</param>
  71. /// <param name="outputStream">The output stream.</param>
  72. /// <returns>Task{System.Boolean}.</returns>
  73. private async Task StreamFile(string path, Stream outputStream)
  74. {
  75. var eofCount = 0;
  76. long position = 0;
  77. using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
  78. {
  79. while (eofCount < 15)
  80. {
  81. await fs.CopyToAsync(outputStream).ConfigureAwait(false);
  82. var fsPosition = fs.Position;
  83. var bytesRead = fsPosition - position;
  84. //Logger.Debug("Streamed {0} bytes from file {1}", bytesRead, path);
  85. if (bytesRead == 0)
  86. {
  87. eofCount++;
  88. await Task.Delay(100).ConfigureAwait(false);
  89. }
  90. else
  91. {
  92. eofCount = 0;
  93. }
  94. position = fsPosition;
  95. }
  96. }
  97. }
  98. }
  99. }