FileStreamResponseHelpers.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.IO;
  3. using System.Net.Http;
  4. using System.Net.Mime;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Jellyfin.Api.Models.PlaybackDtos;
  8. using Jellyfin.Api.Models.StreamingDtos;
  9. using MediaBrowser.Controller.MediaEncoding;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.AspNetCore.Mvc;
  12. using Microsoft.Net.Http.Headers;
  13. namespace Jellyfin.Api.Helpers
  14. {
  15. /// <summary>
  16. /// The stream response helpers.
  17. /// </summary>
  18. public static class FileStreamResponseHelpers
  19. {
  20. /// <summary>
  21. /// Returns a static file from a remote source.
  22. /// </summary>
  23. /// <param name="state">The current <see cref="StreamState"/>.</param>
  24. /// <param name="httpClient">The <see cref="HttpClient"/> making the remote request.</param>
  25. /// <param name="httpContext">The current http context.</param>
  26. /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
  27. /// <returns>A <see cref="Task{ActionResult}"/> containing the API response.</returns>
  28. public static async Task<ActionResult> GetStaticRemoteStreamResult(
  29. StreamState state,
  30. HttpClient httpClient,
  31. HttpContext httpContext,
  32. CancellationToken cancellationToken = default)
  33. {
  34. if (state.RemoteHttpHeaders.TryGetValue(HeaderNames.UserAgent, out var useragent))
  35. {
  36. httpClient.DefaultRequestHeaders.Add(HeaderNames.UserAgent, useragent);
  37. }
  38. // Can't dispose the response as it's required up the call chain.
  39. var response = await httpClient.GetAsync(new Uri(state.MediaPath), cancellationToken).ConfigureAwait(false);
  40. var contentType = response.Content.Headers.ContentType?.ToString() ?? MediaTypeNames.Text.Plain;
  41. httpContext.Response.Headers[HeaderNames.AcceptRanges] = "none";
  42. return new FileStreamResult(await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false), contentType);
  43. }
  44. /// <summary>
  45. /// Returns a static file from the server.
  46. /// </summary>
  47. /// <param name="path">The path to the file.</param>
  48. /// <param name="contentType">The content type of the file.</param>
  49. /// <returns>An <see cref="ActionResult"/> the file.</returns>
  50. public static ActionResult GetStaticFileResult(
  51. string path,
  52. string contentType)
  53. {
  54. return new PhysicalFileResult(path, contentType) { EnableRangeProcessing = true };
  55. }
  56. /// <summary>
  57. /// Returns a transcoded file from the server.
  58. /// </summary>
  59. /// <param name="state">The current <see cref="StreamState"/>.</param>
  60. /// <param name="isHeadRequest">Whether the current request is a HTTP HEAD request so only the headers get returned.</param>
  61. /// <param name="httpContext">The current http context.</param>
  62. /// <param name="transcodingJobHelper">The <see cref="TranscodingJobHelper"/> singleton.</param>
  63. /// <param name="ffmpegCommandLineArguments">The command line arguments to start ffmpeg.</param>
  64. /// <param name="transcodingJobType">The <see cref="TranscodingJobType"/>.</param>
  65. /// <param name="cancellationTokenSource">The <see cref="CancellationTokenSource"/>.</param>
  66. /// <returns>A <see cref="Task{ActionResult}"/> containing the transcoded file.</returns>
  67. public static async Task<ActionResult> GetTranscodedFile(
  68. StreamState state,
  69. bool isHeadRequest,
  70. HttpContext httpContext,
  71. TranscodingJobHelper transcodingJobHelper,
  72. string ffmpegCommandLineArguments,
  73. TranscodingJobType transcodingJobType,
  74. CancellationTokenSource cancellationTokenSource)
  75. {
  76. // Use the command line args with a dummy playlist path
  77. var outputPath = state.OutputFilePath;
  78. httpContext.Response.Headers[HeaderNames.AcceptRanges] = "none";
  79. var contentType = state.GetMimeType(outputPath);
  80. // Headers only
  81. if (isHeadRequest)
  82. {
  83. httpContext.Response.Headers[HeaderNames.ContentType] = contentType;
  84. return new OkResult();
  85. }
  86. var transcodingLock = transcodingJobHelper.GetTranscodingLock(outputPath);
  87. await transcodingLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
  88. try
  89. {
  90. TranscodingJobDto? job;
  91. if (!File.Exists(outputPath))
  92. {
  93. job = await transcodingJobHelper.StartFfMpeg(state, outputPath, ffmpegCommandLineArguments, httpContext.Request, transcodingJobType, cancellationTokenSource).ConfigureAwait(false);
  94. }
  95. else
  96. {
  97. job = transcodingJobHelper.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
  98. state.Dispose();
  99. }
  100. var stream = new ProgressiveFileStream(outputPath, job, transcodingJobHelper);
  101. return new FileStreamResult(stream, contentType);
  102. }
  103. finally
  104. {
  105. transcodingLock.Release();
  106. }
  107. }
  108. }
  109. }