jellyfin-server/Jellyfin.Api/Helpers/StreamingHelpers.cs

783 lines
31 KiB
C#
Raw Normal View History

2020-09-05 21:09:21 +00:00
using System;
2020-07-11 09:14:23 +00:00
using System.Collections.Generic;
using System.Globalization;
2020-07-12 09:14:38 +00:00
using System.IO;
2020-07-11 09:14:23 +00:00
using System.Linq;
2020-07-12 09:14:38 +00:00
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Api.Extensions;
2020-07-12 09:14:38 +00:00
using Jellyfin.Api.Models.StreamingDtos;
2021-09-19 18:53:31 +00:00
using Jellyfin.Extensions;
2020-07-12 09:14:38 +00:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Devices;
2020-07-11 09:14:23 +00:00
using MediaBrowser.Controller.Dlna;
2020-07-12 09:14:38 +00:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
2020-07-11 09:14:23 +00:00
using MediaBrowser.Model.Dlna;
2020-07-12 09:14:38 +00:00
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
2020-07-11 09:14:23 +00:00
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
2020-07-12 09:14:38 +00:00
using Microsoft.Net.Http.Headers;
2020-07-11 09:14:23 +00:00
2023-01-31 11:18:10 +00:00
namespace Jellyfin.Api.Helpers;
/// <summary>
/// The streaming helpers.
/// </summary>
public static class StreamingHelpers
2020-07-11 09:14:23 +00:00
{
/// <summary>
2023-01-31 11:18:10 +00:00
/// Gets the current streaming state.
2020-07-11 09:14:23 +00:00
/// </summary>
2023-01-31 11:18:10 +00:00
/// <param name="streamingRequest">The <see cref="StreamingRequestDto"/>.</param>
/// <param name="httpContext">The <see cref="HttpContext"/>.</param>
/// <param name="mediaSourceManager">Instance of the <see cref="IMediaSourceManager"/> 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="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
/// <param name="mediaEncoder">Instance of the <see cref="IMediaEncoder"/> interface.</param>
/// <param name="encodingHelper">Instance of <see cref="EncodingHelper"/>.</param>
/// <param name="dlnaManager">Instance of the <see cref="IDlnaManager"/> interface.</param>
/// <param name="deviceManager">Instance of the <see cref="IDeviceManager"/> interface.</param>
/// <param name="transcodingJobHelper">Initialized <see cref="TranscodingJobHelper"/>.</param>
/// <param name="transcodingJobType">The <see cref="TranscodingJobType"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>A <see cref="Task"/> containing the current <see cref="StreamState"/>.</returns>
public static async Task<StreamState> GetStreamingState(
StreamingRequestDto streamingRequest,
HttpContext httpContext,
IMediaSourceManager mediaSourceManager,
IUserManager userManager,
ILibraryManager libraryManager,
IServerConfigurationManager serverConfigurationManager,
IMediaEncoder mediaEncoder,
EncodingHelper encodingHelper,
IDlnaManager dlnaManager,
IDeviceManager deviceManager,
TranscodingJobHelper transcodingJobHelper,
TranscodingJobType transcodingJobType,
CancellationToken cancellationToken)
2020-07-11 09:14:23 +00:00
{
2023-01-31 11:18:10 +00:00
var httpRequest = httpContext.Request;
// Parse the DLNA time seek header
if (!streamingRequest.StartTimeTicks.HasValue)
{
var timeSeek = httpRequest.Headers["TimeSeekRange.dlna.org"];
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
streamingRequest.StartTimeTicks = ParseTimeSeekHeader(timeSeek.ToString());
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (!string.IsNullOrWhiteSpace(streamingRequest.Params))
{
ParseParams(streamingRequest);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
streamingRequest.StreamOptions = ParseStreamOptions(httpRequest.Query);
if (httpRequest.Path.Value is null)
{
throw new ResourceNotFoundException(nameof(httpRequest.Path));
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
var url = httpRequest.Path.Value.AsSpan().RightPart('.').ToString();
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (string.IsNullOrEmpty(streamingRequest.AudioCodec))
{
streamingRequest.AudioCodec = encodingHelper.InferAudioCodec(url);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
var enableDlnaHeaders = !string.IsNullOrWhiteSpace(streamingRequest.Params) ||
streamingRequest.StreamOptions.ContainsKey("dlnaheaders") ||
string.Equals(httpRequest.Headers["GetContentFeatures.DLNA.ORG"], "1", StringComparison.OrdinalIgnoreCase);
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
var state = new StreamState(mediaSourceManager, transcodingJobType, transcodingJobHelper)
{
Request = streamingRequest,
RequestedUrl = url,
UserAgent = httpRequest.Headers[HeaderNames.UserAgent],
EnableDlnaHeaders = enableDlnaHeaders
};
var userId = httpContext.User.GetUserId();
if (!userId.Equals(default))
{
state.User = userManager.GetUserById(userId);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (state.IsVideoRequest && !string.IsNullOrWhiteSpace(state.Request.VideoCodec))
{
state.SupportedVideoCodecs = state.Request.VideoCodec.Split(',', StringSplitOptions.RemoveEmptyEntries);
state.Request.VideoCodec = state.SupportedVideoCodecs.FirstOrDefault();
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (!string.IsNullOrWhiteSpace(streamingRequest.AudioCodec))
{
state.SupportedAudioCodecs = streamingRequest.AudioCodec.Split(',', StringSplitOptions.RemoveEmptyEntries);
state.Request.AudioCodec = state.SupportedAudioCodecs.FirstOrDefault(mediaEncoder.CanEncodeToAudioCodec)
?? state.SupportedAudioCodecs.FirstOrDefault();
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (!string.IsNullOrWhiteSpace(streamingRequest.SubtitleCodec))
{
state.SupportedSubtitleCodecs = streamingRequest.SubtitleCodec.Split(',', StringSplitOptions.RemoveEmptyEntries);
state.Request.SubtitleCodec = state.SupportedSubtitleCodecs.FirstOrDefault(mediaEncoder.CanEncodeToSubtitleCodec)
?? state.SupportedSubtitleCodecs.FirstOrDefault();
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
var item = libraryManager.GetItemById(streamingRequest.Id);
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
state.IsInputVideo = string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
MediaSourceInfo? mediaSource = null;
if (string.IsNullOrWhiteSpace(streamingRequest.LiveStreamId))
{
var currentJob = !string.IsNullOrWhiteSpace(streamingRequest.PlaySessionId)
? transcodingJobHelper.GetTranscodingJob(streamingRequest.PlaySessionId)
: null;
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (currentJob is not null)
{
mediaSource = currentJob.MediaSource;
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (mediaSource is null)
{
var mediaSources = await mediaSourceManager.GetPlaybackMediaSources(libraryManager.GetItemById(streamingRequest.Id), null, false, false, cancellationToken).ConfigureAwait(false);
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
mediaSource = string.IsNullOrEmpty(streamingRequest.MediaSourceId)
? mediaSources[0]
: mediaSources.Find(i => string.Equals(i.Id, streamingRequest.MediaSourceId, StringComparison.Ordinal));
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (mediaSource is null && Guid.Parse(streamingRequest.MediaSourceId).Equals(streamingRequest.Id))
{
mediaSource = mediaSources[0];
2020-07-12 09:14:38 +00:00
}
}
2023-01-31 11:18:10 +00:00
}
else
{
var liveStreamInfo = await mediaSourceManager.GetLiveStreamWithDirectStreamProvider(streamingRequest.LiveStreamId, cancellationToken).ConfigureAwait(false);
mediaSource = liveStreamInfo.Item1;
state.DirectStreamProvider = liveStreamInfo.Item2;
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
var encodingOptions = serverConfigurationManager.GetEncodingOptions();
2020-11-12 03:02:56 +00:00
2023-01-31 11:18:10 +00:00
encodingHelper.AttachMediaSourceInfo(state, encodingOptions, mediaSource, url);
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
string? containerInternal = Path.GetExtension(state.RequestedUrl);
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (!string.IsNullOrEmpty(streamingRequest.Container))
{
containerInternal = streamingRequest.Container;
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (string.IsNullOrEmpty(containerInternal))
{
containerInternal = streamingRequest.Static ?
StreamBuilder.NormalizeMediaSourceFormatIntoSingleContainer(state.InputContainer, null, DlnaProfileType.Audio)
: GetOutputFileExtension(state, mediaSource);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
state.OutputContainer = (containerInternal ?? string.Empty).TrimStart('.');
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
state.OutputAudioBitrate = encodingHelper.GetAudioBitrateParam(streamingRequest.AudioBitRate, streamingRequest.AudioCodec, state.AudioStream);
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
state.OutputAudioCodec = streamingRequest.AudioCodec;
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
state.OutputAudioChannels = encodingHelper.GetNumAudioChannelsParam(state, state.AudioStream, state.OutputAudioCodec);
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (state.VideoRequest is not null)
{
state.OutputVideoCodec = state.Request.VideoCodec;
state.OutputVideoBitrate = encodingHelper.GetVideoBitrateParamValue(state.VideoRequest, state.VideoStream, state.OutputVideoCodec);
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
encodingHelper.TryStreamCopy(state);
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (!EncodingHelper.IsCopyCodec(state.OutputVideoCodec) && state.OutputVideoBitrate.HasValue)
{
var isVideoResolutionNotRequested = !state.VideoRequest.Width.HasValue
&& !state.VideoRequest.Height.HasValue
&& !state.VideoRequest.MaxWidth.HasValue
&& !state.VideoRequest.MaxHeight.HasValue;
if (isVideoResolutionNotRequested
&& state.VideoStream is not null
&& state.VideoRequest.VideoBitRate.HasValue
&& state.VideoStream.BitRate.HasValue
&& state.VideoRequest.VideoBitRate.Value >= state.VideoStream.BitRate.Value)
2020-07-12 09:14:38 +00:00
{
2023-01-31 11:18:10 +00:00
// Don't downscale the resolution if the width/height/MaxWidth/MaxHeight is not requested,
// and the requested video bitrate is higher than source video bitrate.
if (state.VideoStream.Width.HasValue || state.VideoStream.Height.HasValue)
{
2023-01-31 11:18:10 +00:00
state.VideoRequest.MaxWidth = state.VideoStream?.Width;
state.VideoRequest.MaxHeight = state.VideoStream?.Height;
}
2020-07-12 09:14:38 +00:00
}
2023-01-31 11:18:10 +00:00
else
{
var resolution = ResolutionNormalizer.Normalize(
state.VideoStream?.BitRate,
state.OutputVideoBitrate.Value,
state.VideoRequest.MaxWidth,
state.VideoRequest.MaxHeight);
state.VideoRequest.MaxWidth = resolution.MaxWidth;
state.VideoRequest.MaxHeight = resolution.MaxHeight;
}
2020-07-12 09:14:38 +00:00
}
2023-01-31 11:18:10 +00:00
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
ApplyDeviceProfileSettings(state, dlnaManager, deviceManager, httpRequest, streamingRequest.DeviceProfileId, streamingRequest.Static);
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
var ext = string.IsNullOrWhiteSpace(state.OutputContainer)
? GetOutputFileExtension(state, mediaSource)
: ("." + state.OutputContainer);
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
state.OutputFilePath = GetOutputFilePath(state, ext!, serverConfigurationManager, streamingRequest.DeviceId, streamingRequest.PlaySessionId);
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
return state;
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Adds the dlna headers.
/// </summary>
/// <param name="state">The state.</param>
/// <param name="responseHeaders">The response headers.</param>
/// <param name="isStaticallyStreamed">if set to <c>true</c> [is statically streamed].</param>
/// <param name="startTimeTicks">The start time in ticks.</param>
/// <param name="request">The <see cref="HttpRequest"/>.</param>
/// <param name="dlnaManager">Instance of the <see cref="IDlnaManager"/> interface.</param>
public static void AddDlnaHeaders(
StreamState state,
IHeaderDictionary responseHeaders,
bool isStaticallyStreamed,
long? startTimeTicks,
HttpRequest request,
IDlnaManager dlnaManager)
{
if (!state.EnableDlnaHeaders)
2020-07-11 09:14:23 +00:00
{
2023-01-31 11:18:10 +00:00
return;
}
2020-07-11 09:14:23 +00:00
2023-01-31 11:18:10 +00:00
var profile = state.DeviceProfile;
2020-07-11 09:14:23 +00:00
2023-01-31 11:18:10 +00:00
StringValues transferMode = request.Headers["transferMode.dlna.org"];
responseHeaders.Add("transferMode.dlna.org", string.IsNullOrEmpty(transferMode) ? "Streaming" : transferMode.ToString());
responseHeaders.Add("realTimeInfo.dlna.org", "DLNA.ORG_TLAG=*");
2020-07-11 09:14:23 +00:00
2023-01-31 11:18:10 +00:00
if (state.RunTimeTicks.HasValue)
{
if (string.Equals(request.Headers["getMediaInfo.sec"], "1", StringComparison.OrdinalIgnoreCase))
2020-07-11 09:14:23 +00:00
{
2023-01-31 11:18:10 +00:00
var ms = TimeSpan.FromTicks(state.RunTimeTicks.Value).TotalMilliseconds;
responseHeaders.Add("MediaInfo.sec", string.Format(
CultureInfo.InvariantCulture,
"SEC_Duration={0};",
Convert.ToInt32(ms)));
}
2020-07-11 09:14:23 +00:00
2023-01-31 11:18:10 +00:00
if (!isStaticallyStreamed && profile is not null)
{
AddTimeSeekResponseHeaders(state, responseHeaders, startTimeTicks);
2020-07-11 09:14:23 +00:00
}
2023-01-31 11:18:10 +00:00
}
2020-07-11 09:14:23 +00:00
2023-01-31 11:18:10 +00:00
profile ??= dlnaManager.GetDefaultProfile();
2020-07-11 09:14:23 +00:00
2023-01-31 11:18:10 +00:00
var audioCodec = state.ActualOutputAudioCodec;
2020-07-11 09:14:23 +00:00
2023-01-31 11:18:10 +00:00
if (!state.IsVideoRequest)
{
responseHeaders.Add("contentFeatures.dlna.org", ContentFeatureBuilder.BuildAudioHeader(
profile,
state.OutputContainer,
audioCodec,
state.OutputAudioBitrate,
state.OutputAudioSampleRate,
state.OutputAudioChannels,
state.OutputAudioBitDepth,
isStaticallyStreamed,
state.RunTimeTicks,
state.TranscodeSeekInfo));
}
else
{
var videoCodec = state.ActualOutputVideoCodec;
2020-07-11 09:14:23 +00:00
2023-01-31 11:18:10 +00:00
responseHeaders.Add(
"contentFeatures.dlna.org",
ContentFeatureBuilder.BuildVideoHeader(profile, state.OutputContainer, videoCodec, audioCodec, state.OutputWidth, state.OutputHeight, state.TargetVideoBitDepth, state.OutputVideoBitrate, state.TargetTimestamp, isStaticallyStreamed, state.RunTimeTicks, state.TargetVideoProfile, state.TargetVideoRangeType, state.TargetVideoLevel, state.TargetFramerate, state.TargetPacketLength, state.TranscodeSeekInfo, state.IsTargetAnamorphic, state.IsTargetInterlaced, state.TargetRefFrames, state.TargetVideoStreamCount, state.TargetAudioStreamCount, state.TargetVideoCodecTag, state.IsTargetAVC).FirstOrDefault() ?? string.Empty);
2020-07-11 09:14:23 +00:00
}
2023-01-31 11:18:10 +00:00
}
2020-07-11 09:14:23 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Parses the time seek header.
/// </summary>
/// <param name="value">The time seek header string.</param>
/// <returns>A nullable <see cref="long"/> representing the seek time in ticks.</returns>
private static long? ParseTimeSeekHeader(ReadOnlySpan<char> value)
{
if (value.IsEmpty)
2020-07-11 09:14:23 +00:00
{
2023-01-31 11:18:10 +00:00
return null;
}
2020-07-11 09:14:23 +00:00
2023-01-31 11:18:10 +00:00
const string npt = "npt=";
if (!value.StartsWith(npt, StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException("Invalid timeseek header");
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
var index = value.IndexOf('-');
value = index == -1
? value.Slice(npt.Length)
: value.Slice(npt.Length, index - npt.Length);
if (!value.Contains(':'))
2023-01-31 11:18:10 +00:00
{
// Parses npt times in the format of '417.33'
if (double.TryParse(value, CultureInfo.InvariantCulture, out var seconds))
2020-07-11 09:14:23 +00:00
{
2023-01-31 11:18:10 +00:00
return TimeSpan.FromSeconds(seconds).Ticks;
2020-07-11 09:14:23 +00:00
}
2023-01-31 11:18:10 +00:00
throw new ArgumentException("Invalid timeseek header");
2020-07-11 09:14:23 +00:00
}
2023-01-31 11:18:10 +00:00
try
{
// Parses npt times in the format of '10:19:25.7'
return TimeSpan.Parse(value, CultureInfo.InvariantCulture).Ticks;
}
catch
{
throw new ArgumentException("Invalid timeseek header");
}
}
/// <summary>
/// Parses query parameters as StreamOptions.
/// </summary>
/// <param name="queryString">The query string.</param>
/// <returns>A <see cref="Dictionary{String,String}"/> containing the stream options.</returns>
private static Dictionary<string, string?> ParseStreamOptions(IQueryCollection queryString)
{
Dictionary<string, string?> streamOptions = new Dictionary<string, string?>();
foreach (var param in queryString)
2020-07-11 09:14:23 +00:00
{
2023-01-31 11:18:10 +00:00
if (char.IsLower(param.Key[0]))
2020-07-12 09:14:38 +00:00
{
2023-01-31 11:18:10 +00:00
// This was probably not parsed initially and should be a StreamOptions
// or the generated URL should correctly serialize it
// TODO: This should be incorporated either in the lower framework for parsing requests
streamOptions[param.Key] = param.Value;
2020-07-12 09:14:38 +00:00
}
}
2023-01-31 11:18:10 +00:00
return streamOptions;
}
2020-07-11 09:14:23 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Adds the dlna time seek headers to the response.
/// </summary>
/// <param name="state">The current <see cref="StreamState"/>.</param>
/// <param name="responseHeaders">The <see cref="IHeaderDictionary"/> of the response.</param>
/// <param name="startTimeTicks">The start time in ticks.</param>
private static void AddTimeSeekResponseHeaders(StreamState state, IHeaderDictionary responseHeaders, long? startTimeTicks)
{
var runtimeSeconds = TimeSpan.FromTicks(state.RunTimeTicks!.Value).TotalSeconds.ToString(CultureInfo.InvariantCulture);
var startSeconds = TimeSpan.FromTicks(startTimeTicks ?? 0).TotalSeconds.ToString(CultureInfo.InvariantCulture);
responseHeaders.Add("TimeSeekRange.dlna.org", string.Format(
CultureInfo.InvariantCulture,
"npt={0}-{1}/{1}",
startSeconds,
runtimeSeconds));
responseHeaders.Add("X-AvailableSeekRange", string.Format(
CultureInfo.InvariantCulture,
"1 npt={0}-{1}",
startSeconds,
runtimeSeconds));
}
/// <summary>
/// Gets the output file extension.
/// </summary>
/// <param name="state">The state.</param>
/// <param name="mediaSource">The mediaSource.</param>
/// <returns>System.String.</returns>
private static string? GetOutputFileExtension(StreamState state, MediaSourceInfo? mediaSource)
{
var ext = Path.GetExtension(state.RequestedUrl);
if (!string.IsNullOrEmpty(ext))
{
return ext;
2020-07-11 09:14:23 +00:00
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
// Try to infer based on the desired video codec
if (state.IsVideoRequest)
2020-07-12 09:14:38 +00:00
{
2023-01-31 11:18:10 +00:00
var videoCodec = state.Request.VideoCodec;
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase) ||
string.Equals(videoCodec, "hevc", StringComparison.OrdinalIgnoreCase))
2020-07-12 09:14:38 +00:00
{
2023-01-31 11:18:10 +00:00
return ".ts";
2020-07-12 09:14:38 +00:00
}
2023-01-31 11:18:10 +00:00
if (string.Equals(videoCodec, "theora", StringComparison.OrdinalIgnoreCase))
2020-07-12 09:14:38 +00:00
{
2023-01-31 11:18:10 +00:00
return ".ogv";
2020-07-12 09:14:38 +00:00
}
2023-01-31 11:18:10 +00:00
if (string.Equals(videoCodec, "vp8", StringComparison.OrdinalIgnoreCase)
|| string.Equals(videoCodec, "vp9", StringComparison.OrdinalIgnoreCase)
|| string.Equals(videoCodec, "vpx", StringComparison.OrdinalIgnoreCase))
2020-07-12 09:14:38 +00:00
{
2023-01-31 11:18:10 +00:00
return ".webm";
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (string.Equals(videoCodec, "wmv", StringComparison.OrdinalIgnoreCase))
{
return ".asf";
}
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
// Try to infer based on the desired audio codec
if (!state.IsVideoRequest)
{
var audioCodec = state.Request.AudioCodec;
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (string.Equals("aac", audioCodec, StringComparison.OrdinalIgnoreCase))
{
return ".aac";
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (string.Equals("mp3", audioCodec, StringComparison.OrdinalIgnoreCase))
{
return ".mp3";
2020-07-12 09:14:38 +00:00
}
2023-01-31 11:18:10 +00:00
if (string.Equals("vorbis", audioCodec, StringComparison.OrdinalIgnoreCase))
{
2023-01-31 11:18:10 +00:00
return ".ogg";
}
2023-01-31 11:18:10 +00:00
if (string.Equals("wma", audioCodec, StringComparison.OrdinalIgnoreCase))
{
return ".wma";
}
2020-07-12 09:14:38 +00:00
}
2023-01-31 11:18:10 +00:00
// Fallback to the container of mediaSource
if (!string.IsNullOrEmpty(mediaSource?.Container))
2020-07-12 09:14:38 +00:00
{
2023-01-31 11:18:10 +00:00
var idx = mediaSource.Container.IndexOf(',', StringComparison.OrdinalIgnoreCase);
return '.' + (idx == -1 ? mediaSource.Container : mediaSource.Container[..idx]).Trim();
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
return null;
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Gets the output file path for transcoding.
/// </summary>
/// <param name="state">The current <see cref="StreamState"/>.</param>
/// <param name="outputFileExtension">The file extension of the output file.</param>
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
/// <param name="deviceId">The device id.</param>
/// <param name="playSessionId">The play session id.</param>
/// <returns>The complete file path, including the folder, for the transcoding file.</returns>
private static string GetOutputFilePath(StreamState state, string outputFileExtension, IServerConfigurationManager serverConfigurationManager, string? deviceId, string? playSessionId)
{
var data = $"{state.MediaPath}-{state.UserAgent}-{deviceId!}-{playSessionId!}";
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
var filename = data.GetMD5().ToString("N", CultureInfo.InvariantCulture);
var ext = outputFileExtension?.ToLowerInvariant();
var folder = serverConfigurationManager.GetTranscodePath();
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
return Path.Combine(folder, filename + ext);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
private static void ApplyDeviceProfileSettings(StreamState state, IDlnaManager dlnaManager, IDeviceManager deviceManager, HttpRequest request, string? deviceProfileId, bool? @static)
{
if (!string.IsNullOrWhiteSpace(deviceProfileId))
{
state.DeviceProfile = dlnaManager.GetProfile(deviceProfileId);
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (state.DeviceProfile is null)
2020-07-12 09:14:38 +00:00
{
2023-01-31 11:18:10 +00:00
var caps = deviceManager.GetCapabilities(deviceProfileId);
state.DeviceProfile = caps is null ? dlnaManager.GetProfile(request.Headers) : caps.DeviceProfile;
2020-07-12 09:14:38 +00:00
}
2023-01-31 11:18:10 +00:00
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
var profile = state.DeviceProfile;
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (profile is null)
{
// Don't use settings from the default profile.
// Only use a specific profile if it was requested.
return;
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
var audioCodec = state.ActualOutputAudioCodec;
var videoCodec = state.ActualOutputVideoCodec;
var mediaProfile = !state.IsVideoRequest
? profile.GetAudioMediaProfile(state.OutputContainer, audioCodec, state.OutputAudioChannels, state.OutputAudioBitrate, state.OutputAudioSampleRate, state.OutputAudioBitDepth)
: profile.GetVideoMediaProfile(
state.OutputContainer,
audioCodec,
videoCodec,
state.OutputWidth,
state.OutputHeight,
state.TargetVideoBitDepth,
state.OutputVideoBitrate,
state.TargetVideoProfile,
state.TargetVideoRangeType,
state.TargetVideoLevel,
state.TargetFramerate,
state.TargetPacketLength,
state.TargetTimestamp,
state.IsTargetAnamorphic,
state.IsTargetInterlaced,
state.TargetRefFrames,
state.TargetVideoStreamCount,
state.TargetAudioStreamCount,
state.TargetVideoCodecTag,
state.IsTargetAVC);
if (mediaProfile is not null)
{
state.MimeType = mediaProfile.MimeType;
}
if (!(@static.HasValue && @static.Value))
{
var transcodingProfile = !state.IsVideoRequest ? profile.GetAudioTranscodingProfile(state.OutputContainer, audioCodec) : profile.GetVideoTranscodingProfile(state.OutputContainer, audioCodec, videoCodec);
if (transcodingProfile is not null)
2020-07-12 09:14:38 +00:00
{
2023-01-31 11:18:10 +00:00
state.EstimateContentLength = transcodingProfile.EstimateContentLength;
// state.EnableMpegtsM2TsMode = transcodingProfile.EnableMpegtsM2TsMode;
state.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (state.VideoRequest is not null)
2020-07-12 09:14:38 +00:00
{
2023-01-31 11:18:10 +00:00
state.VideoRequest.CopyTimestamps = transcodingProfile.CopyTimestamps;
state.VideoRequest.EnableSubtitlesInManifest = transcodingProfile.EnableSubtitlesInManifest;
2020-07-12 09:14:38 +00:00
}
}
}
2023-01-31 11:18:10 +00:00
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Parses the parameters.
/// </summary>
/// <param name="request">The request.</param>
private static void ParseParams(StreamingRequestDto request)
{
if (string.IsNullOrEmpty(request.Params))
2020-07-12 09:14:38 +00:00
{
2023-01-31 11:18:10 +00:00
return;
}
2020-07-22 14:57:06 +00:00
2023-01-31 11:18:10 +00:00
var vals = request.Params.Split(';');
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
var videoRequest = request as VideoRequestDto;
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
for (var i = 0; i < vals.Length; i++)
{
var val = vals[i];
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
if (string.IsNullOrWhiteSpace(val))
{
continue;
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
switch (i)
{
case 0:
request.DeviceProfileId = val;
break;
case 1:
request.DeviceId = val;
break;
case 2:
request.MediaSourceId = val;
break;
case 3:
request.Static = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
break;
case 4:
if (videoRequest is not null)
{
videoRequest.VideoCodec = val;
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
break;
case 5:
request.AudioCodec = val;
break;
case 6:
if (videoRequest is not null)
{
videoRequest.AudioStreamIndex = int.Parse(val, CultureInfo.InvariantCulture);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
break;
case 7:
if (videoRequest is not null)
{
videoRequest.SubtitleStreamIndex = int.Parse(val, CultureInfo.InvariantCulture);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
break;
case 8:
if (videoRequest is not null)
{
videoRequest.VideoBitRate = int.Parse(val, CultureInfo.InvariantCulture);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
break;
case 9:
request.AudioBitRate = int.Parse(val, CultureInfo.InvariantCulture);
break;
case 10:
request.MaxAudioChannels = int.Parse(val, CultureInfo.InvariantCulture);
break;
case 11:
if (videoRequest is not null)
{
videoRequest.MaxFramerate = float.Parse(val, CultureInfo.InvariantCulture);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
break;
case 12:
if (videoRequest is not null)
{
videoRequest.MaxWidth = int.Parse(val, CultureInfo.InvariantCulture);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
break;
case 13:
if (videoRequest is not null)
{
videoRequest.MaxHeight = int.Parse(val, CultureInfo.InvariantCulture);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
break;
case 14:
request.StartTimeTicks = long.Parse(val, CultureInfo.InvariantCulture);
break;
case 15:
if (videoRequest is not null)
{
videoRequest.Level = val;
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
break;
case 16:
if (videoRequest is not null)
{
videoRequest.MaxRefFrames = int.Parse(val, CultureInfo.InvariantCulture);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
break;
case 17:
if (videoRequest is not null)
{
videoRequest.MaxVideoBitDepth = int.Parse(val, CultureInfo.InvariantCulture);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
break;
case 18:
if (videoRequest is not null)
{
videoRequest.Profile = val;
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
break;
case 19:
// cabac no longer used
break;
case 20:
request.PlaySessionId = val;
break;
case 21:
// api_key
break;
case 22:
request.LiveStreamId = val;
break;
case 23:
// Duplicating ItemId because of MediaMonkey
break;
case 24:
if (videoRequest is not null)
{
videoRequest.CopyTimestamps = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
break;
case 25:
if (!string.IsNullOrWhiteSpace(val) && videoRequest is not null)
{
if (Enum.TryParse(val, out SubtitleDeliveryMethod method))
2020-07-12 09:14:38 +00:00
{
2023-01-31 11:18:10 +00:00
videoRequest.SubtitleMethod = method;
2020-07-12 09:14:38 +00:00
}
2023-01-31 11:18:10 +00:00
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
break;
case 26:
request.TranscodingMaxAudioChannels = int.Parse(val, CultureInfo.InvariantCulture);
break;
case 27:
if (videoRequest is not null)
{
videoRequest.EnableSubtitlesInManifest = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
break;
case 28:
request.Tag = val;
break;
case 29:
if (videoRequest is not null)
{
videoRequest.RequireAvc = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
break;
case 30:
request.SubtitleCodec = val;
break;
case 31:
if (videoRequest is not null)
{
videoRequest.RequireNonAnamorphic = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
break;
case 32:
if (videoRequest is not null)
{
videoRequest.DeInterlace = string.Equals("true", val, StringComparison.OrdinalIgnoreCase);
}
2020-07-12 09:14:38 +00:00
2023-01-31 11:18:10 +00:00
break;
case 33:
request.TranscodeReasons = val;
break;
2020-07-12 09:14:38 +00:00
}
}
2020-07-11 09:14:23 +00:00
}
}