2
0

AudioHelper.cs 10 KB

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