ProgressiveStreamWriter.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 StreamState State { get; set; }
  15. private ILogger Logger { get; set; }
  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="state">The state.</param>
  33. /// <param name="logger">The logger.</param>
  34. public ProgressiveStreamWriter(string path, StreamState state, ILogger logger)
  35. {
  36. Path = path;
  37. State = state;
  38. Logger = logger;
  39. }
  40. /// <summary>
  41. /// Writes to.
  42. /// </summary>
  43. /// <param name="responseStream">The response stream.</param>
  44. public void WriteTo(Stream responseStream)
  45. {
  46. var task = WriteToAsync(responseStream);
  47. Task.WaitAll(task);
  48. }
  49. /// <summary>
  50. /// Writes to async.
  51. /// </summary>
  52. /// <param name="responseStream">The response stream.</param>
  53. /// <returns>Task.</returns>
  54. public async Task WriteToAsync(Stream responseStream)
  55. {
  56. try
  57. {
  58. await StreamFile(Path, responseStream).ConfigureAwait(false);
  59. }
  60. catch (Exception ex)
  61. {
  62. Logger.ErrorException("Error streaming media", ex);
  63. throw;
  64. }
  65. finally
  66. {
  67. ApiEntryPoint.Instance.OnTranscodeEndRequest(Path, TranscodingJobType.Progressive);
  68. }
  69. }
  70. /// <summary>
  71. /// Streams the file.
  72. /// </summary>
  73. /// <param name="path">The path.</param>
  74. /// <param name="outputStream">The output stream.</param>
  75. /// <returns>Task{System.Boolean}.</returns>
  76. private async Task StreamFile(string path, Stream outputStream)
  77. {
  78. var eofCount = 0;
  79. long position = 0;
  80. using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
  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.Info("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. }