2021-04-17 10:37:55 +00:00
|
|
|
|
using System.Net.Http;
|
2020-08-09 23:20:14 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Jellyfin.Api.Models.StreamingDtos;
|
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2020-11-13 18:14:44 +00:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2020-08-31 17:05:21 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2020-08-09 23:20:14 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Devices;
|
|
|
|
|
using MediaBrowser.Controller.Dlna;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
|
|
|
|
using MediaBrowser.Controller.Net;
|
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
|
|
|
|
using MediaBrowser.Model.Net;
|
2020-08-10 13:53:32 +00:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2020-08-09 23:20:14 +00:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
|
|
|
|
namespace Jellyfin.Api.Helpers
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Audio helper.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class AudioHelper
|
|
|
|
|
{
|
|
|
|
|
private readonly IDlnaManager _dlnaManager;
|
|
|
|
|
private readonly IAuthorizationContext _authContext;
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly IMediaSourceManager _mediaSourceManager;
|
|
|
|
|
private readonly IServerConfigurationManager _serverConfigurationManager;
|
|
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
|
|
|
|
private readonly IDeviceManager _deviceManager;
|
|
|
|
|
private readonly TranscodingJobHelper _transcodingJobHelper;
|
|
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2020-08-10 13:53:32 +00:00
|
|
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
2021-04-20 21:03:36 +00:00
|
|
|
|
private readonly EncodingHelper _encodingHelper;
|
2020-08-09 23:20:14 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="AudioHelper"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dlnaManager">Instance of the <see cref="IDlnaManager"/> interface.</param>
|
|
|
|
|
/// <param name="authContext">Instance of the <see cref="IAuthorizationContext"/> interface.</param>
|
|
|
|
|
/// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
|
|
|
|
|
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
|
|
|
|
|
/// <param name="mediaSourceManager">Instance of the <see cref="IMediaSourceManager"/> interface.</param>
|
|
|
|
|
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
|
|
|
|
|
/// <param name="mediaEncoder">Instance of the <see cref="IMediaEncoder"/> interface.</param>
|
|
|
|
|
/// <param name="deviceManager">Instance of the <see cref="IDeviceManager"/> interface.</param>
|
|
|
|
|
/// <param name="transcodingJobHelper">Instance of <see cref="TranscodingJobHelper"/>.</param>
|
|
|
|
|
/// <param name="httpClientFactory">Instance of the <see cref="IHttpClientFactory"/> interface.</param>
|
2020-08-10 13:53:32 +00:00
|
|
|
|
/// <param name="httpContextAccessor">Instance of the <see cref="IHttpContextAccessor"/> interface.</param>
|
2021-04-20 21:03:36 +00:00
|
|
|
|
/// <param name="encodingHelper">Instance of <see cref="EncodingHelper"/>.</param>
|
2020-08-09 23:20:14 +00:00
|
|
|
|
public AudioHelper(
|
|
|
|
|
IDlnaManager dlnaManager,
|
|
|
|
|
IAuthorizationContext authContext,
|
|
|
|
|
IUserManager userManager,
|
|
|
|
|
ILibraryManager libraryManager,
|
|
|
|
|
IMediaSourceManager mediaSourceManager,
|
|
|
|
|
IServerConfigurationManager serverConfigurationManager,
|
|
|
|
|
IMediaEncoder mediaEncoder,
|
|
|
|
|
IDeviceManager deviceManager,
|
|
|
|
|
TranscodingJobHelper transcodingJobHelper,
|
2020-08-10 13:53:32 +00:00
|
|
|
|
IHttpClientFactory httpClientFactory,
|
2021-04-20 21:03:36 +00:00
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
|
|
|
|
EncodingHelper encodingHelper)
|
2020-08-09 23:20:14 +00:00
|
|
|
|
{
|
|
|
|
|
_dlnaManager = dlnaManager;
|
|
|
|
|
_authContext = authContext;
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_mediaSourceManager = mediaSourceManager;
|
|
|
|
|
_serverConfigurationManager = serverConfigurationManager;
|
|
|
|
|
_mediaEncoder = mediaEncoder;
|
|
|
|
|
_deviceManager = deviceManager;
|
|
|
|
|
_transcodingJobHelper = transcodingJobHelper;
|
|
|
|
|
_httpClientFactory = httpClientFactory;
|
2020-08-10 13:53:32 +00:00
|
|
|
|
_httpContextAccessor = httpContextAccessor;
|
2021-04-20 21:03:36 +00:00
|
|
|
|
_encodingHelper = encodingHelper;
|
2020-08-09 23:20:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get audio stream.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="transcodingJobType">Transcoding job type.</param>
|
|
|
|
|
/// <param name="streamingRequest">Streaming controller.Request dto.</param>
|
|
|
|
|
/// <returns>A <see cref="Task"/> containing the resulting <see cref="ActionResult"/>.</returns>
|
|
|
|
|
public async Task<ActionResult> GetAudioStream(
|
|
|
|
|
TranscodingJobType transcodingJobType,
|
|
|
|
|
StreamingRequestDto streamingRequest)
|
|
|
|
|
{
|
2020-11-13 16:04:31 +00:00
|
|
|
|
if (_httpContextAccessor.HttpContext == null)
|
|
|
|
|
{
|
2020-11-13 18:14:44 +00:00
|
|
|
|
throw new ResourceNotFoundException(nameof(_httpContextAccessor.HttpContext));
|
2020-11-13 16:04:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-10 13:53:32 +00:00
|
|
|
|
bool isHeadRequest = _httpContextAccessor.HttpContext.Request.Method == System.Net.WebRequestMethods.Http.Head;
|
2020-08-09 23:20:14 +00:00
|
|
|
|
var cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
|
|
|
|
|
using var state = await StreamingHelpers.GetStreamingState(
|
|
|
|
|
streamingRequest,
|
2020-08-10 13:53:32 +00:00
|
|
|
|
_httpContextAccessor.HttpContext.Request,
|
2020-08-09 23:20:14 +00:00
|
|
|
|
_authContext,
|
|
|
|
|
_mediaSourceManager,
|
|
|
|
|
_userManager,
|
|
|
|
|
_libraryManager,
|
|
|
|
|
_serverConfigurationManager,
|
|
|
|
|
_mediaEncoder,
|
2021-04-20 21:03:36 +00:00
|
|
|
|
_encodingHelper,
|
2020-08-09 23:20:14 +00:00
|
|
|
|
_dlnaManager,
|
|
|
|
|
_deviceManager,
|
|
|
|
|
_transcodingJobHelper,
|
|
|
|
|
transcodingJobType,
|
|
|
|
|
cancellationTokenSource.Token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (streamingRequest.Static && state.DirectStreamProvider != null)
|
|
|
|
|
{
|
2020-08-10 13:53:32 +00:00
|
|
|
|
StreamingHelpers.AddDlnaHeaders(state, _httpContextAccessor.HttpContext.Response.Headers, true, streamingRequest.StartTimeTicks, _httpContextAccessor.HttpContext.Request, _dlnaManager);
|
2020-08-09 23:20:14 +00:00
|
|
|
|
|
|
|
|
|
await new ProgressiveFileCopier(state.DirectStreamProvider, null, _transcodingJobHelper, CancellationToken.None)
|
|
|
|
|
{
|
|
|
|
|
AllowEndOfFile = false
|
2020-08-10 13:53:32 +00:00
|
|
|
|
}.WriteToAsync(_httpContextAccessor.HttpContext.Response.Body, CancellationToken.None)
|
2020-08-09 23:20:14 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
// TODO (moved from MediaBrowser.Api): Don't hardcode contentType
|
2020-08-10 13:53:32 +00:00
|
|
|
|
return new FileStreamResult(_httpContextAccessor.HttpContext.Response.Body, MimeTypes.GetMimeType("file.ts")!);
|
2020-08-09 23:20:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Static remote stream
|
|
|
|
|
if (streamingRequest.Static && state.InputProtocol == MediaProtocol.Http)
|
|
|
|
|
{
|
2020-08-10 13:53:32 +00:00
|
|
|
|
StreamingHelpers.AddDlnaHeaders(state, _httpContextAccessor.HttpContext.Response.Headers, true, streamingRequest.StartTimeTicks, _httpContextAccessor.HttpContext.Request, _dlnaManager);
|
2020-08-09 23:20:14 +00:00
|
|
|
|
|
2020-08-31 17:05:21 +00:00
|
|
|
|
var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);
|
2020-08-10 13:53:32 +00:00
|
|
|
|
return await FileStreamResponseHelpers.GetStaticRemoteStreamResult(state, isHeadRequest, httpClient, _httpContextAccessor.HttpContext).ConfigureAwait(false);
|
2020-08-09 23:20:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (streamingRequest.Static && state.InputProtocol != MediaProtocol.File)
|
|
|
|
|
{
|
2020-08-10 13:53:32 +00:00
|
|
|
|
return new BadRequestObjectResult($"Input protocol {state.InputProtocol} cannot be streamed statically");
|
2020-08-09 23:20:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var outputPath = state.OutputFilePath;
|
|
|
|
|
var outputPathExists = System.IO.File.Exists(outputPath);
|
|
|
|
|
|
|
|
|
|
var transcodingJob = _transcodingJobHelper.GetTranscodingJob(outputPath, TranscodingJobType.Progressive);
|
|
|
|
|
var isTranscodeCached = outputPathExists && transcodingJob != null;
|
|
|
|
|
|
2020-08-10 13:53:32 +00:00
|
|
|
|
StreamingHelpers.AddDlnaHeaders(state, _httpContextAccessor.HttpContext.Response.Headers, streamingRequest.Static || isTranscodeCached, streamingRequest.StartTimeTicks, _httpContextAccessor.HttpContext.Request, _dlnaManager);
|
2020-08-09 23:20:14 +00:00
|
|
|
|
|
|
|
|
|
// Static stream
|
|
|
|
|
if (streamingRequest.Static)
|
|
|
|
|
{
|
|
|
|
|
var contentType = state.GetMimeType("." + state.OutputContainer, false) ?? state.GetMimeType(state.MediaPath);
|
|
|
|
|
|
|
|
|
|
if (state.MediaSource.IsInfiniteStream)
|
|
|
|
|
{
|
|
|
|
|
await new ProgressiveFileCopier(state.MediaPath, null, _transcodingJobHelper, CancellationToken.None)
|
|
|
|
|
{
|
|
|
|
|
AllowEndOfFile = false
|
2020-08-10 13:53:32 +00:00
|
|
|
|
}.WriteToAsync(_httpContextAccessor.HttpContext.Response.Body, CancellationToken.None)
|
2020-08-09 23:20:14 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2020-08-10 13:53:32 +00:00
|
|
|
|
return new FileStreamResult(_httpContextAccessor.HttpContext.Response.Body, contentType);
|
2020-08-09 23:20:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FileStreamResponseHelpers.GetStaticFileResult(
|
|
|
|
|
state.MediaPath,
|
|
|
|
|
contentType,
|
|
|
|
|
isHeadRequest,
|
2020-08-10 13:53:32 +00:00
|
|
|
|
_httpContextAccessor.HttpContext);
|
2020-08-09 23:20:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Need to start ffmpeg (because media can't be returned directly)
|
|
|
|
|
var encodingOptions = _serverConfigurationManager.GetEncodingOptions();
|
2021-04-20 21:03:36 +00:00
|
|
|
|
var ffmpegCommandLineArguments = _encodingHelper.GetProgressiveAudioFullCommandLine(state, encodingOptions, outputPath);
|
2020-08-09 23:20:14 +00:00
|
|
|
|
return await FileStreamResponseHelpers.GetTranscodedFile(
|
|
|
|
|
state,
|
|
|
|
|
isHeadRequest,
|
2020-08-10 13:53:32 +00:00
|
|
|
|
_httpContextAccessor.HttpContext,
|
2020-08-09 23:20:14 +00:00
|
|
|
|
_transcodingJobHelper,
|
|
|
|
|
ffmpegCommandLineArguments,
|
|
|
|
|
transcodingJobType,
|
|
|
|
|
cancellationTokenSource).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|