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