AudioHelper.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System.Net.Http;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Jellyfin.Api.Models.StreamingDtos;
  5. using MediaBrowser.Common.Configuration;
  6. using MediaBrowser.Common.Extensions;
  7. using MediaBrowser.Common.Net;
  8. using MediaBrowser.Controller.Configuration;
  9. using MediaBrowser.Controller.Devices;
  10. using MediaBrowser.Controller.Dlna;
  11. using MediaBrowser.Controller.Library;
  12. using MediaBrowser.Controller.MediaEncoding;
  13. using MediaBrowser.Controller.Net;
  14. using MediaBrowser.Model.IO;
  15. using MediaBrowser.Model.MediaInfo;
  16. using MediaBrowser.Model.Net;
  17. using Microsoft.AspNetCore.Http;
  18. using Microsoft.AspNetCore.Mvc;
  19. using Microsoft.Extensions.Configuration;
  20. namespace Jellyfin.Api.Helpers
  21. {
  22. /// <summary>
  23. /// Audio helper.
  24. /// </summary>
  25. public class AudioHelper
  26. {
  27. private readonly IDlnaManager _dlnaManager;
  28. private readonly IAuthorizationContext _authContext;
  29. private readonly IUserManager _userManager;
  30. private readonly ILibraryManager _libraryManager;
  31. private readonly IMediaSourceManager _mediaSourceManager;
  32. private readonly IServerConfigurationManager _serverConfigurationManager;
  33. private readonly IMediaEncoder _mediaEncoder;
  34. private readonly IDeviceManager _deviceManager;
  35. private readonly TranscodingJobHelper _transcodingJobHelper;
  36. private readonly IHttpClientFactory _httpClientFactory;
  37. private readonly IHttpContextAccessor _httpContextAccessor;
  38. private readonly EncodingHelper _encodingHelper;
  39. /// <summary>
  40. /// Initializes a new instance of the <see cref="AudioHelper"/> class.
  41. /// </summary>
  42. /// <param name="dlnaManager">Instance of the <see cref="IDlnaManager"/> interface.</param>
  43. /// <param name="authContext">Instance of the <see cref="IAuthorizationContext"/> interface.</param>
  44. /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
  45. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  46. /// <param name="mediaSourceManager">Instance of the <see cref="IMediaSourceManager"/> interface.</param>
  47. /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
  48. /// <param name="mediaEncoder">Instance of the <see cref="IMediaEncoder"/> interface.</param>
  49. /// <param name="deviceManager">Instance of the <see cref="IDeviceManager"/> interface.</param>
  50. /// <param name="transcodingJobHelper">Instance of <see cref="TranscodingJobHelper"/>.</param>
  51. /// <param name="httpClientFactory">Instance of the <see cref="IHttpClientFactory"/> interface.</param>
  52. /// <param name="httpContextAccessor">Instance of the <see cref="IHttpContextAccessor"/> interface.</param>
  53. /// <param name="encodingHelper">Instance of <see cref="EncodingHelper"/>.</param>
  54. public AudioHelper(
  55. IDlnaManager dlnaManager,
  56. IAuthorizationContext authContext,
  57. IUserManager userManager,
  58. ILibraryManager libraryManager,
  59. IMediaSourceManager mediaSourceManager,
  60. IServerConfigurationManager serverConfigurationManager,
  61. IMediaEncoder mediaEncoder,
  62. IDeviceManager deviceManager,
  63. TranscodingJobHelper transcodingJobHelper,
  64. IHttpClientFactory httpClientFactory,
  65. IHttpContextAccessor httpContextAccessor,
  66. EncodingHelper encodingHelper)
  67. {
  68. _dlnaManager = dlnaManager;
  69. _authContext = authContext;
  70. _userManager = userManager;
  71. _libraryManager = libraryManager;
  72. _mediaSourceManager = mediaSourceManager;
  73. _serverConfigurationManager = serverConfigurationManager;
  74. _mediaEncoder = mediaEncoder;
  75. _deviceManager = deviceManager;
  76. _transcodingJobHelper = transcodingJobHelper;
  77. _httpClientFactory = httpClientFactory;
  78. _httpContextAccessor = httpContextAccessor;
  79. _encodingHelper = encodingHelper;
  80. }
  81. /// <summary>
  82. /// Get audio stream.
  83. /// </summary>
  84. /// <param name="transcodingJobType">Transcoding job type.</param>
  85. /// <param name="streamingRequest">Streaming controller.Request dto.</param>
  86. /// <returns>A <see cref="Task"/> containing the resulting <see cref="ActionResult"/>.</returns>
  87. public async Task<ActionResult> GetAudioStream(
  88. TranscodingJobType transcodingJobType,
  89. StreamingRequestDto streamingRequest)
  90. {
  91. if (_httpContextAccessor.HttpContext == null)
  92. {
  93. throw new ResourceNotFoundException(nameof(_httpContextAccessor.HttpContext));
  94. }
  95. bool isHeadRequest = _httpContextAccessor.HttpContext.Request.Method == System.Net.WebRequestMethods.Http.Head;
  96. // CTS lifecycle is managed internally.
  97. var cancellationTokenSource = new CancellationTokenSource();
  98. using var state = await StreamingHelpers.GetStreamingState(
  99. streamingRequest,
  100. _httpContextAccessor.HttpContext.Request,
  101. _authContext,
  102. _mediaSourceManager,
  103. _userManager,
  104. _libraryManager,
  105. _serverConfigurationManager,
  106. _mediaEncoder,
  107. _encodingHelper,
  108. _dlnaManager,
  109. _deviceManager,
  110. _transcodingJobHelper,
  111. transcodingJobType,
  112. cancellationTokenSource.Token)
  113. .ConfigureAwait(false);
  114. if (streamingRequest.Static && state.DirectStreamProvider != null)
  115. {
  116. StreamingHelpers.AddDlnaHeaders(state, _httpContextAccessor.HttpContext.Response.Headers, true, streamingRequest.StartTimeTicks, _httpContextAccessor.HttpContext.Request, _dlnaManager);
  117. await new ProgressiveFileCopier(state.DirectStreamProvider, null, _transcodingJobHelper, CancellationToken.None)
  118. {
  119. AllowEndOfFile = false
  120. }.WriteToAsync(_httpContextAccessor.HttpContext.Response.Body, CancellationToken.None)
  121. .ConfigureAwait(false);
  122. // TODO (moved from MediaBrowser.Api): Don't hardcode contentType
  123. return new FileStreamResult(_httpContextAccessor.HttpContext.Response.Body, MimeTypes.GetMimeType("file.ts")!);
  124. }
  125. // Static remote stream
  126. if (streamingRequest.Static && state.InputProtocol == MediaProtocol.Http)
  127. {
  128. StreamingHelpers.AddDlnaHeaders(state, _httpContextAccessor.HttpContext.Response.Headers, true, streamingRequest.StartTimeTicks, _httpContextAccessor.HttpContext.Request, _dlnaManager);
  129. var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);
  130. return await FileStreamResponseHelpers.GetStaticRemoteStreamResult(state, isHeadRequest, httpClient, _httpContextAccessor.HttpContext).ConfigureAwait(false);
  131. }
  132. if (streamingRequest.Static && state.InputProtocol != MediaProtocol.File)
  133. {
  134. return new BadRequestObjectResult($"Input protocol {state.InputProtocol} cannot be streamed statically");
  135. }
  136. var outputPath = state.OutputFilePath;
  137. var outputPathExists = System.IO.File.Exists(outputPath);
  138. var transcodingJob = _transcodingJobHelper.GetTranscodingJob(outputPath, TranscodingJobType.Progressive);
  139. var isTranscodeCached = outputPathExists && transcodingJob != null;
  140. StreamingHelpers.AddDlnaHeaders(state, _httpContextAccessor.HttpContext.Response.Headers, streamingRequest.Static || isTranscodeCached, streamingRequest.StartTimeTicks, _httpContextAccessor.HttpContext.Request, _dlnaManager);
  141. // Static stream
  142. if (streamingRequest.Static)
  143. {
  144. var contentType = state.GetMimeType("." + state.OutputContainer, false) ?? state.GetMimeType(state.MediaPath);
  145. if (state.MediaSource.IsInfiniteStream)
  146. {
  147. await new ProgressiveFileCopier(state.MediaPath, null, _transcodingJobHelper, CancellationToken.None)
  148. {
  149. AllowEndOfFile = false
  150. }.WriteToAsync(_httpContextAccessor.HttpContext.Response.Body, CancellationToken.None)
  151. .ConfigureAwait(false);
  152. return new FileStreamResult(_httpContextAccessor.HttpContext.Response.Body, contentType);
  153. }
  154. return FileStreamResponseHelpers.GetStaticFileResult(
  155. state.MediaPath,
  156. contentType,
  157. isHeadRequest,
  158. _httpContextAccessor.HttpContext);
  159. }
  160. // Need to start ffmpeg (because media can't be returned directly)
  161. var encodingOptions = _serverConfigurationManager.GetEncodingOptions();
  162. var ffmpegCommandLineArguments = _encodingHelper.GetProgressiveAudioFullCommandLine(state, encodingOptions, outputPath);
  163. return await FileStreamResponseHelpers.GetTranscodedFile(
  164. state,
  165. isHeadRequest,
  166. _httpContextAccessor.HttpContext,
  167. _transcodingJobHelper,
  168. ffmpegCommandLineArguments,
  169. transcodingJobType,
  170. cancellationTokenSource).ConfigureAwait(false);
  171. }
  172. }
  173. }