FileStreamResponseHelpers.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.IO;
  3. using System.Net.Http;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Jellyfin.Api.Models.PlaybackDtos;
  7. using Jellyfin.Api.Models.StreamingDtos;
  8. using MediaBrowser.Controller.MediaEncoding;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.Net.Http.Headers;
  12. namespace Jellyfin.Api.Helpers
  13. {
  14. /// <summary>
  15. /// The stream response helpers.
  16. /// </summary>
  17. public static class FileStreamResponseHelpers
  18. {
  19. /// <summary>
  20. /// Returns a static file from a remote source.
  21. /// </summary>
  22. /// <param name="state">The current <see cref="StreamState"/>.</param>
  23. /// <param name="isHeadRequest">Whether the current request is a HTTP HEAD request so only the headers get returned.</param>
  24. /// <param name="controller">The <see cref="ControllerBase"/> managing the response.</param>
  25. /// <param name="httpClient">The <see cref="HttpClient"/> making the remote request.</param>
  26. /// <returns>A <see cref="Task{ActionResult}"/> containing the API response.</returns>
  27. public static async Task<ActionResult> GetStaticRemoteStreamResult(
  28. StreamState state,
  29. bool isHeadRequest,
  30. ControllerBase controller,
  31. HttpClient httpClient)
  32. {
  33. if (state.RemoteHttpHeaders.TryGetValue(HeaderNames.UserAgent, out var useragent))
  34. {
  35. httpClient.DefaultRequestHeaders.Add(HeaderNames.UserAgent, useragent);
  36. }
  37. using var response = await httpClient.GetAsync(state.MediaPath).ConfigureAwait(false);
  38. var contentType = response.Content.Headers.ContentType.ToString();
  39. controller.Response.Headers[HeaderNames.AcceptRanges] = "none";
  40. if (isHeadRequest)
  41. {
  42. return controller.File(Array.Empty<byte>(), contentType);
  43. }
  44. return controller.File(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), contentType);
  45. }
  46. /// <summary>
  47. /// Returns a static file from the server.
  48. /// </summary>
  49. /// <param name="path">The path to the file.</param>
  50. /// <param name="contentType">The content type of the file.</param>
  51. /// <param name="isHeadRequest">Whether the current request is a HTTP HEAD request so only the headers get returned.</param>
  52. /// <param name="controller">The <see cref="ControllerBase"/> managing the response.</param>
  53. /// <returns>An <see cref="ActionResult"/> the file.</returns>
  54. public static ActionResult GetStaticFileResult(
  55. string path,
  56. string contentType,
  57. bool isHeadRequest,
  58. ControllerBase controller)
  59. {
  60. controller.Response.ContentType = contentType;
  61. // if the request is a head request, return a NoContent result with the same headers as it would with a GET request
  62. if (isHeadRequest)
  63. {
  64. return controller.NoContent();
  65. }
  66. return controller.PhysicalFile(path, contentType);
  67. }
  68. /// <summary>
  69. /// Returns a transcoded file from the server.
  70. /// </summary>
  71. /// <param name="state">The current <see cref="StreamState"/>.</param>
  72. /// <param name="isHeadRequest">Whether the current request is a HTTP HEAD request so only the headers get returned.</param>
  73. /// <param name="controller">The <see cref="ControllerBase"/> managing the response.</param>
  74. /// <param name="transcodingJobHelper">The <see cref="TranscodingJobHelper"/> singleton.</param>
  75. /// <param name="ffmpegCommandLineArguments">The command line arguments to start ffmpeg.</param>
  76. /// <param name="request">The <see cref="HttpRequest"/> starting the transcoding.</param>
  77. /// <param name="transcodingJobType">The <see cref="TranscodingJobType"/>.</param>
  78. /// <param name="cancellationTokenSource">The <see cref="CancellationTokenSource"/>.</param>
  79. /// <returns>A <see cref="Task{ActionResult}"/> containing the transcoded file.</returns>
  80. public static async Task<ActionResult> GetTranscodedFile(
  81. StreamState state,
  82. bool isHeadRequest,
  83. ControllerBase controller,
  84. TranscodingJobHelper transcodingJobHelper,
  85. string ffmpegCommandLineArguments,
  86. HttpRequest request,
  87. TranscodingJobType transcodingJobType,
  88. CancellationTokenSource cancellationTokenSource)
  89. {
  90. // Use the command line args with a dummy playlist path
  91. var outputPath = state.OutputFilePath;
  92. controller.Response.Headers[HeaderNames.AcceptRanges] = "none";
  93. var contentType = state.GetMimeType(outputPath);
  94. // Headers only
  95. if (isHeadRequest)
  96. {
  97. return controller.File(Array.Empty<byte>(), contentType);
  98. }
  99. var transcodingLock = transcodingJobHelper.GetTranscodingLock(outputPath);
  100. await transcodingLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
  101. try
  102. {
  103. TranscodingJobDto? job;
  104. if (!File.Exists(outputPath))
  105. {
  106. job = await transcodingJobHelper.StartFfMpeg(state, outputPath, ffmpegCommandLineArguments, request, transcodingJobType, cancellationTokenSource).ConfigureAwait(false);
  107. }
  108. else
  109. {
  110. job = transcodingJobHelper.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
  111. state.Dispose();
  112. }
  113. var memoryStream = new MemoryStream();
  114. await new ProgressiveFileCopier(outputPath, job, transcodingJobHelper, CancellationToken.None).WriteToAsync(memoryStream, CancellationToken.None).ConfigureAwait(false);
  115. memoryStream.Position = 0;
  116. return controller.File(memoryStream, contentType);
  117. }
  118. finally
  119. {
  120. transcodingLock.Release();
  121. }
  122. }
  123. }
  124. }