jellyfin/Jellyfin.Api/Helpers/FileStreamResponseHelpers.cs

121 lines
5.3 KiB
C#
Raw Normal View History

2020-09-05 21:03:21 +02:00
using System;
2020-07-12 11:14:38 +02:00
using System.IO;
using System.Net.Http;
2021-02-14 15:11:46 +01:00
using System.Net.Mime;
2020-07-12 11:14:38 +02:00
using System.Threading;
using System.Threading.Tasks;
2020-07-27 21:42:40 +02:00
using Jellyfin.Api.Models.PlaybackDtos;
2020-07-12 11:14:38 +02:00
using Jellyfin.Api.Models.StreamingDtos;
using MediaBrowser.Controller.MediaEncoding;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
namespace Jellyfin.Api.Helpers
{
/// <summary>
/// The stream response helpers.
/// </summary>
public static class FileStreamResponseHelpers
{
/// <summary>
/// Returns a static file from a remote source.
/// </summary>
/// <param name="state">The current <see cref="StreamState"/>.</param>
2020-07-23 12:46:54 +02:00
/// <param name="httpClient">The <see cref="HttpClient"/> making the remote request.</param>
2020-08-10 15:53:32 +02:00
/// <param name="httpContext">The current http context.</param>
2020-11-17 19:43:00 +01:00
/// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
2020-07-12 11:14:38 +02:00
/// <returns>A <see cref="Task{ActionResult}"/> containing the API response.</returns>
public static async Task<ActionResult> GetStaticRemoteStreamResult(
StreamState state,
2020-08-10 15:53:32 +02:00
HttpClient httpClient,
2020-11-17 19:43:00 +01:00
HttpContext httpContext,
CancellationToken cancellationToken = default)
2020-07-12 11:14:38 +02:00
{
if (state.RemoteHttpHeaders.TryGetValue(HeaderNames.UserAgent, out var useragent))
{
httpClient.DefaultRequestHeaders.Add(HeaderNames.UserAgent, useragent);
}
2020-08-17 23:03:45 +02:00
// Can't dispose the response as it's required up the call chain.
2021-02-15 14:19:08 +01:00
var response = await httpClient.GetAsync(new Uri(state.MediaPath), cancellationToken).ConfigureAwait(false);
2021-02-14 15:11:46 +01:00
var contentType = response.Content.Headers.ContentType?.ToString() ?? MediaTypeNames.Text.Plain;
2020-07-12 11:14:38 +02:00
2020-08-10 15:53:32 +02:00
httpContext.Response.Headers[HeaderNames.AcceptRanges] = "none";
2020-07-12 11:14:38 +02:00
2020-11-17 19:43:00 +01:00
return new FileStreamResult(await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false), contentType);
2020-07-12 11:14:38 +02:00
}
/// <summary>
/// Returns a static file from the server.
/// </summary>
/// <param name="path">The path to the file.</param>
/// <param name="contentType">The content type of the file.</param>
/// <returns>An <see cref="ActionResult"/> the file.</returns>
public static ActionResult GetStaticFileResult(
string path,
2022-01-09 02:53:53 +01:00
string contentType)
2020-07-12 11:14:38 +02:00
{
2020-09-05 10:38:16 +02:00
return new PhysicalFileResult(path, contentType) { EnableRangeProcessing = true };
2020-07-12 11:14:38 +02:00
}
/// <summary>
/// Returns a transcoded file from the server.
/// </summary>
/// <param name="state">The current <see cref="StreamState"/>.</param>
/// <param name="isHeadRequest">Whether the current request is a HTTP HEAD request so only the headers get returned.</param>
2020-08-10 15:53:32 +02:00
/// <param name="httpContext">The current http context.</param>
2020-07-12 11:14:38 +02:00
/// <param name="transcodingJobHelper">The <see cref="TranscodingJobHelper"/> singleton.</param>
/// <param name="ffmpegCommandLineArguments">The command line arguments to start ffmpeg.</param>
/// <param name="transcodingJobType">The <see cref="TranscodingJobType"/>.</param>
/// <param name="cancellationTokenSource">The <see cref="CancellationTokenSource"/>.</param>
/// <returns>A <see cref="Task{ActionResult}"/> containing the transcoded file.</returns>
public static async Task<ActionResult> GetTranscodedFile(
StreamState state,
bool isHeadRequest,
2020-08-10 15:53:32 +02:00
HttpContext httpContext,
2020-07-12 11:14:38 +02:00
TranscodingJobHelper transcodingJobHelper,
string ffmpegCommandLineArguments,
TranscodingJobType transcodingJobType,
CancellationTokenSource cancellationTokenSource)
{
// Use the command line args with a dummy playlist path
var outputPath = state.OutputFilePath;
2020-08-10 15:53:32 +02:00
httpContext.Response.Headers[HeaderNames.AcceptRanges] = "none";
2020-07-12 11:14:38 +02:00
var contentType = state.GetMimeType(outputPath);
// Headers only
if (isHeadRequest)
{
httpContext.Response.Headers[HeaderNames.ContentType] = contentType;
return new OkResult();
2020-07-12 11:14:38 +02:00
}
var transcodingLock = transcodingJobHelper.GetTranscodingLock(outputPath);
await transcodingLock.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);
try
{
2020-07-27 21:42:40 +02:00
TranscodingJobDto? job;
2020-07-12 11:14:38 +02:00
if (!File.Exists(outputPath))
{
2020-08-10 15:53:32 +02:00
job = await transcodingJobHelper.StartFfMpeg(state, outputPath, ffmpegCommandLineArguments, httpContext.Request, transcodingJobType, cancellationTokenSource).ConfigureAwait(false);
2020-07-12 11:14:38 +02:00
}
else
{
2020-07-27 21:42:40 +02:00
job = transcodingJobHelper.OnTranscodeBeginRequest(outputPath, TranscodingJobType.Progressive);
2020-07-12 11:14:38 +02:00
state.Dispose();
}
2020-09-25 23:59:17 +02:00
var stream = new ProgressiveFileStream(outputPath, job, transcodingJobHelper);
return new FileStreamResult(stream, contentType);
2020-07-12 11:14:38 +02:00
}
finally
{
transcodingLock.Release();
}
}
}
}