AudioHelper.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System.IO;
  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.MediaInfo;
  16. using MediaBrowser.Model.Net;
  17. using Microsoft.AspNetCore.Http;
  18. using Microsoft.AspNetCore.Mvc;
  19. namespace Jellyfin.Api.Helpers
  20. {
  21. /// <summary>
  22. /// Audio helper.
  23. /// </summary>
  24. public class AudioHelper
  25. {
  26. private readonly IDlnaManager _dlnaManager;
  27. private readonly IAuthorizationContext _authContext;
  28. private readonly IUserManager _userManager;
  29. private readonly ILibraryManager _libraryManager;
  30. private readonly IMediaSourceManager _mediaSourceManager;
  31. private readonly IServerConfigurationManager _serverConfigurationManager;
  32. private readonly IMediaEncoder _mediaEncoder;
  33. private readonly IDeviceManager _deviceManager;
  34. private readonly TranscodingJobHelper _transcodingJobHelper;
  35. private readonly IHttpClientFactory _httpClientFactory;
  36. private readonly IHttpContextAccessor _httpContextAccessor;
  37. private readonly EncodingHelper _encodingHelper;
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="AudioHelper"/> class.
  40. /// </summary>
  41. /// <param name="dlnaManager">Instance of the <see cref="IDlnaManager"/> interface.</param>
  42. /// <param name="authContext">Instance of the <see cref="IAuthorizationContext"/> interface.</param>
  43. /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
  44. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  45. /// <param name="mediaSourceManager">Instance of the <see cref="IMediaSourceManager"/> interface.</param>
  46. /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
  47. /// <param name="mediaEncoder">Instance of the <see cref="IMediaEncoder"/> interface.</param>
  48. /// <param name="deviceManager">Instance of the <see cref="IDeviceManager"/> interface.</param>
  49. /// <param name="transcodingJobHelper">Instance of <see cref="TranscodingJobHelper"/>.</param>
  50. /// <param name="httpClientFactory">Instance of the <see cref="IHttpClientFactory"/> interface.</param>
  51. /// <param name="httpContextAccessor">Instance of the <see cref="IHttpContextAccessor"/> interface.</param>
  52. /// <param name="encodingHelper">Instance of <see cref="EncodingHelper"/>.</param>
  53. public AudioHelper(
  54. IDlnaManager dlnaManager,
  55. IAuthorizationContext authContext,
  56. IUserManager userManager,
  57. ILibraryManager libraryManager,
  58. IMediaSourceManager mediaSourceManager,
  59. IServerConfigurationManager serverConfigurationManager,
  60. IMediaEncoder mediaEncoder,
  61. IDeviceManager deviceManager,
  62. TranscodingJobHelper transcodingJobHelper,
  63. IHttpClientFactory httpClientFactory,
  64. IHttpContextAccessor httpContextAccessor,
  65. EncodingHelper encodingHelper)
  66. {
  67. _dlnaManager = dlnaManager;
  68. _authContext = authContext;
  69. _userManager = userManager;
  70. _libraryManager = libraryManager;
  71. _mediaSourceManager = mediaSourceManager;
  72. _serverConfigurationManager = serverConfigurationManager;
  73. _mediaEncoder = mediaEncoder;
  74. _deviceManager = deviceManager;
  75. _transcodingJobHelper = transcodingJobHelper;
  76. _httpClientFactory = httpClientFactory;
  77. _httpContextAccessor = httpContextAccessor;
  78. _encodingHelper = encodingHelper;
  79. }
  80. /// <summary>
  81. /// Get audio stream.
  82. /// </summary>
  83. /// <param name="transcodingJobType">Transcoding job type.</param>
  84. /// <param name="streamingRequest">Streaming controller.Request dto.</param>
  85. /// <returns>A <see cref="Task"/> containing the resulting <see cref="ActionResult"/>.</returns>
  86. public async Task<ActionResult> GetAudioStream(
  87. TranscodingJobType transcodingJobType,
  88. StreamingRequestDto streamingRequest)
  89. {
  90. if (_httpContextAccessor.HttpContext == null)
  91. {
  92. throw new ResourceNotFoundException(nameof(_httpContextAccessor.HttpContext));
  93. }
  94. bool isHeadRequest = _httpContextAccessor.HttpContext.Request.Method == System.Net.WebRequestMethods.Http.Head;
  95. // CTS lifecycle is managed internally.
  96. var cancellationTokenSource = new CancellationTokenSource();
  97. using var state = await StreamingHelpers.GetStreamingState(
  98. streamingRequest,
  99. _httpContextAccessor.HttpContext.Request,
  100. _authContext,
  101. _mediaSourceManager,
  102. _userManager,
  103. _libraryManager,
  104. _serverConfigurationManager,
  105. _mediaEncoder,
  106. _encodingHelper,
  107. _dlnaManager,
  108. _deviceManager,
  109. _transcodingJobHelper,
  110. transcodingJobType,
  111. cancellationTokenSource.Token)
  112. .ConfigureAwait(false);
  113. if (streamingRequest.Static && state.DirectStreamProvider != null)
  114. {
  115. StreamingHelpers.AddDlnaHeaders(state, _httpContextAccessor.HttpContext.Response.Headers, true, streamingRequest.StartTimeTicks, _httpContextAccessor.HttpContext.Request, _dlnaManager);
  116. var liveStreamInfo = _mediaSourceManager.GetLiveStreamInfo(streamingRequest.LiveStreamId);
  117. if (liveStreamInfo == null)
  118. {
  119. throw new FileNotFoundException();
  120. }
  121. var liveStream = new ProgressiveFileStream(liveStreamInfo.GetStream());
  122. // TODO (moved from MediaBrowser.Api): Don't hardcode contentType
  123. return new FileStreamResult(liveStream, 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, 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 = 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. var stream = new ProgressiveFileStream(state.MediaPath, null, _transcodingJobHelper);
  148. return new FileStreamResult(stream, contentType);
  149. }
  150. return FileStreamResponseHelpers.GetStaticFileResult(
  151. state.MediaPath,
  152. contentType);
  153. }
  154. // Need to start ffmpeg (because media can't be returned directly)
  155. var encodingOptions = _serverConfigurationManager.GetEncodingOptions();
  156. var ffmpegCommandLineArguments = _encodingHelper.GetProgressiveAudioFullCommandLine(state, encodingOptions, outputPath);
  157. return await FileStreamResponseHelpers.GetTranscodedFile(
  158. state,
  159. isHeadRequest,
  160. _httpContextAccessor.HttpContext,
  161. _transcodingJobHelper,
  162. ffmpegCommandLineArguments,
  163. transcodingJobType,
  164. cancellationTokenSource).ConfigureAwait(false);
  165. }
  166. }
  167. }