BaseProgressiveStreamingHandler.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.Net.Handlers;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Model.Entities;
  6. using System;
  7. using System.IO;
  8. using System.Net;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Api.Streaming
  11. {
  12. /// <summary>
  13. /// Class BaseMediaHandler
  14. /// </summary>
  15. /// <typeparam name="TBaseItemType">The type of the T base item type.</typeparam>
  16. public abstract class BaseProgressiveStreamingHandler<TBaseItemType> : BaseStreamingHandler<TBaseItemType>
  17. where TBaseItemType : BaseItem, IHasMediaStreams, new()
  18. {
  19. /// <summary>
  20. /// Gets the type of the transcoding job.
  21. /// </summary>
  22. /// <value>The type of the transcoding job.</value>
  23. protected override TranscodingJobType TranscodingJobType
  24. {
  25. get { return TranscodingJobType.Progressive; }
  26. }
  27. /// <summary>
  28. /// Processes the request.
  29. /// </summary>
  30. /// <param name="ctx">The CTX.</param>
  31. /// <returns>Task.</returns>
  32. public override Task ProcessRequest(HttpListenerContext ctx)
  33. {
  34. HttpListenerContext = ctx;
  35. if (!RequiresConversion())
  36. {
  37. return new StaticFileHandler(Kernel)
  38. {
  39. Path = LibraryItem.Path
  40. }.ProcessRequest(ctx);
  41. }
  42. var outputPath = OutputFilePath;
  43. if (File.Exists(outputPath) && !Plugin.Instance.HasActiveTranscodingJob(outputPath, TranscodingJobType.Progressive))
  44. {
  45. return new StaticFileHandler(Kernel)
  46. {
  47. Path = outputPath
  48. }.ProcessRequest(ctx);
  49. }
  50. return base.ProcessRequest(ctx);
  51. }
  52. /// <summary>
  53. /// Requireses the conversion.
  54. /// </summary>
  55. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  56. protected bool RequiresConversion()
  57. {
  58. return !GetBoolQueryStringParam("static");
  59. }
  60. /// <summary>
  61. /// Writes the response to output stream.
  62. /// </summary>
  63. /// <param name="stream">The stream.</param>
  64. /// <param name="responseInfo">The response info.</param>
  65. /// <param name="contentLength">Length of the content.</param>
  66. /// <returns>Task.</returns>
  67. protected override async Task WriteResponseToOutputStream(Stream stream, ResponseInfo responseInfo, long? contentLength)
  68. {
  69. // Use the command line args with a dummy playlist path
  70. var outputPath = OutputFilePath;
  71. if (!File.Exists(outputPath))
  72. {
  73. await StartFFMpeg(outputPath).ConfigureAwait(false);
  74. }
  75. else
  76. {
  77. Plugin.Instance.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
  78. }
  79. try
  80. {
  81. await StreamFile(outputPath, stream).ConfigureAwait(false);
  82. }
  83. catch (Exception ex)
  84. {
  85. //Logger.ErrorException("Error streaming media", ex);
  86. }
  87. finally
  88. {
  89. Plugin.Instance.OnTranscodeEndRequest(outputPath, TranscodingJobType.Progressive);
  90. }
  91. }
  92. /// <summary>
  93. /// Streams the file.
  94. /// </summary>
  95. /// <param name="path">The path.</param>
  96. /// <param name="outputStream">The output stream.</param>
  97. /// <returns>Task{System.Boolean}.</returns>
  98. private async Task StreamFile(string path, Stream outputStream)
  99. {
  100. var eofCount = 0;
  101. long position = 0;
  102. using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
  103. {
  104. while (eofCount < 15)
  105. {
  106. await fs.CopyToAsync(outputStream).ConfigureAwait(false);
  107. var fsPosition = fs.Position;
  108. var bytesRead = fsPosition - position;
  109. //Logger.LogInfo("Streamed {0} bytes from file {1}", bytesRead, path);
  110. if (bytesRead == 0)
  111. {
  112. eofCount++;
  113. await Task.Delay(100).ConfigureAwait(false);
  114. }
  115. else
  116. {
  117. eofCount = 0;
  118. }
  119. position = fsPosition;
  120. }
  121. }
  122. }
  123. /// <summary>
  124. /// Deletes the partial stream files.
  125. /// </summary>
  126. /// <param name="outputFilePath">The output file path.</param>
  127. protected override void DeletePartialStreamFiles(string outputFilePath)
  128. {
  129. File.Delete(outputFilePath);
  130. }
  131. }
  132. }