jellyfin/MediaBrowser.Model/Dlna/StreamBuilder.cs

1922 lines
80 KiB
C#
Raw Normal View History

#nullable disable
2020-02-03 17:49:27 -07:00
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using MediaBrowser.Model.Dto;
2018-12-27 16:27:57 -07:00
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.MediaInfo;
using MediaBrowser.Model.Session;
using Microsoft.Extensions.Logging;
2018-12-27 16:27:57 -07:00
namespace MediaBrowser.Model.Dlna
{
public class StreamBuilder
{
private readonly ILogger _logger;
private readonly ITranscoderSupport _transcoderSupport;
public StreamBuilder(ITranscoderSupport transcoderSupport, ILogger logger)
{
_transcoderSupport = transcoderSupport;
_logger = logger;
}
public StreamBuilder(ILogger<StreamBuilder> logger)
2018-12-27 16:27:57 -07:00
: this(new FullTranscoderSupport(), logger)
{
}
public StreamInfo BuildAudioItem(AudioOptions options)
{
ValidateAudioInput(options);
var mediaSources = new List<MediaSourceInfo>();
foreach (MediaSourceInfo i in options.MediaSources)
2018-12-27 16:27:57 -07:00
{
if (string.IsNullOrEmpty(options.MediaSourceId) ||
2020-01-09 09:07:13 -07:00
string.Equals(i.Id, options.MediaSourceId, StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
mediaSources.Add(i);
}
}
var streams = new List<StreamInfo>();
foreach (MediaSourceInfo i in mediaSources)
2018-12-27 16:27:57 -07:00
{
StreamInfo streamInfo = BuildAudioItem(i, options);
2018-12-27 16:27:57 -07:00
if (streamInfo != null)
{
streams.Add(streamInfo);
}
}
foreach (StreamInfo stream in streams)
2018-12-27 16:27:57 -07:00
{
stream.DeviceId = options.DeviceId;
stream.DeviceProfileId = options.Profile.Id;
}
return GetOptimalStream(streams, options.GetMaxBitrate(true) ?? 0);
}
public StreamInfo BuildVideoItem(VideoOptions options)
{
ValidateInput(options);
var mediaSources = new List<MediaSourceInfo>();
foreach (MediaSourceInfo i in options.MediaSources)
2018-12-27 16:27:57 -07:00
{
if (string.IsNullOrEmpty(options.MediaSourceId) ||
2020-01-09 09:07:13 -07:00
string.Equals(i.Id, options.MediaSourceId, StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
mediaSources.Add(i);
}
}
var streams = new List<StreamInfo>();
foreach (MediaSourceInfo i in mediaSources)
2018-12-27 16:27:57 -07:00
{
2019-01-13 13:37:13 -07:00
var streamInfo = BuildVideoItem(i, options);
2018-12-27 16:27:57 -07:00
if (streamInfo != null)
{
streams.Add(streamInfo);
}
}
foreach (StreamInfo stream in streams)
2018-12-27 16:27:57 -07:00
{
stream.DeviceId = options.DeviceId;
stream.DeviceProfileId = options.Profile.Id;
}
return GetOptimalStream(streams, options.GetMaxBitrate(false) ?? 0);
}
2019-01-20 03:34:33 -07:00
private static StreamInfo GetOptimalStream(List<StreamInfo> streams, long maxBitrate)
=> SortMediaSources(streams, maxBitrate).FirstOrDefault();
2018-12-27 16:27:57 -07:00
2019-01-20 03:34:33 -07:00
private static IOrderedEnumerable<StreamInfo> SortMediaSources(List<StreamInfo> streams, long maxBitrate)
2018-12-27 16:27:57 -07:00
{
return streams.OrderBy(i =>
{
// Nothing beats direct playing a file
if (i.PlayMethod == PlayMethod.DirectPlay && i.MediaSource.Protocol == MediaProtocol.File)
{
return 0;
}
return 1;
}).ThenBy(i =>
{
switch (i.PlayMethod)
{
// Let's assume direct streaming a file is just as desirable as direct playing a remote url
case PlayMethod.DirectStream:
case PlayMethod.DirectPlay:
return 0;
default:
return 1;
}
}).ThenBy(i =>
{
switch (i.MediaSource.Protocol)
{
case MediaProtocol.File:
return 0;
default:
return 1;
}
}).ThenBy(i =>
{
if (maxBitrate > 0)
{
if (i.MediaSource.Bitrate.HasValue)
{
return Math.Abs(i.MediaSource.Bitrate.Value - maxBitrate);
}
}
return 0;
2019-01-20 03:34:33 -07:00
}).ThenBy(streams.IndexOf);
2018-12-27 16:27:57 -07:00
}
2019-01-20 03:34:33 -07:00
private static TranscodeReason? GetTranscodeReasonForFailedCondition(ProfileCondition condition)
2018-12-27 16:27:57 -07:00
{
switch (condition.Property)
{
2019-01-20 03:34:33 -07:00
case ProfileConditionValue.AudioBitrate:
2018-12-27 16:27:57 -07:00
return TranscodeReason.AudioBitrateNotSupported;
case ProfileConditionValue.AudioChannels:
return TranscodeReason.AudioChannelsNotSupported;
case ProfileConditionValue.AudioProfile:
return TranscodeReason.AudioProfileNotSupported;
case ProfileConditionValue.AudioSampleRate:
return TranscodeReason.AudioSampleRateNotSupported;
case ProfileConditionValue.Has64BitOffsets:
// TODO
return null;
case ProfileConditionValue.Height:
return TranscodeReason.VideoResolutionNotSupported;
case ProfileConditionValue.IsAnamorphic:
return TranscodeReason.AnamorphicVideoNotSupported;
case ProfileConditionValue.IsAvc:
// TODO
return null;
case ProfileConditionValue.IsInterlaced:
return TranscodeReason.InterlacedVideoNotSupported;
case ProfileConditionValue.IsSecondaryAudio:
return TranscodeReason.SecondaryAudioNotSupported;
case ProfileConditionValue.NumAudioStreams:
// TODO
return null;
case ProfileConditionValue.NumVideoStreams:
// TODO
return null;
case ProfileConditionValue.PacketLength:
// TODO
return null;
case ProfileConditionValue.RefFrames:
return TranscodeReason.RefFramesNotSupported;
case ProfileConditionValue.VideoBitDepth:
return TranscodeReason.VideoBitDepthNotSupported;
case ProfileConditionValue.AudioBitDepth:
return TranscodeReason.AudioBitDepthNotSupported;
case ProfileConditionValue.VideoBitrate:
return TranscodeReason.VideoBitrateNotSupported;
case ProfileConditionValue.VideoCodecTag:
return TranscodeReason.VideoCodecNotSupported;
case ProfileConditionValue.VideoFramerate:
return TranscodeReason.VideoFramerateNotSupported;
case ProfileConditionValue.VideoLevel:
return TranscodeReason.VideoLevelNotSupported;
case ProfileConditionValue.VideoProfile:
return TranscodeReason.VideoProfileNotSupported;
case ProfileConditionValue.VideoTimestamp:
// TODO
return null;
case ProfileConditionValue.Width:
return TranscodeReason.VideoResolutionNotSupported;
default:
return null;
}
}
public static string NormalizeMediaSourceFormatIntoSingleContainer(string inputContainer, DeviceProfile profile, DlnaProfileType type)
2018-12-27 16:27:57 -07:00
{
if (string.IsNullOrEmpty(inputContainer))
{
return null;
}
var formats = ContainerProfile.SplitValue(inputContainer);
if (formats.Length == 1)
{
return formats[0];
}
if (profile != null)
{
foreach (var format in formats)
{
foreach (var directPlayProfile in profile.DirectPlayProfiles)
{
2019-01-20 03:34:33 -07:00
if (directPlayProfile.Type == type
&& directPlayProfile.SupportsContainer(format))
2018-12-27 16:27:57 -07:00
{
2019-01-20 03:34:33 -07:00
return format;
2018-12-27 16:27:57 -07:00
}
}
}
}
return formats[0];
}
private StreamInfo BuildAudioItem(MediaSourceInfo item, AudioOptions options)
{
2019-01-20 03:34:33 -07:00
StreamInfo playlistItem = new StreamInfo
2018-12-27 16:27:57 -07:00
{
ItemId = options.ItemId,
MediaType = DlnaProfileType.Audio,
MediaSource = item,
RunTimeTicks = item.RunTimeTicks,
Context = options.Context,
DeviceProfile = options.Profile
};
if (options.ForceDirectPlay)
{
playlistItem.PlayMethod = PlayMethod.DirectPlay;
playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Audio);
2018-12-27 16:27:57 -07:00
return playlistItem;
}
if (options.ForceDirectStream)
{
playlistItem.PlayMethod = PlayMethod.DirectStream;
playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Audio);
2018-12-27 16:27:57 -07:00
return playlistItem;
}
2019-01-13 13:37:13 -07:00
var audioStream = item.GetDefaultAudioStream(null);
2018-12-27 16:27:57 -07:00
var directPlayInfo = GetAudioDirectPlayMethods(item, audioStream, options);
var directPlayMethods = directPlayInfo.Item1;
2019-01-20 03:34:33 -07:00
var transcodeReasons = directPlayInfo.Item2.ToList();
2018-12-27 16:27:57 -07:00
2019-01-09 15:33:17 -07:00
int? inputAudioChannels = audioStream?.Channels;
int? inputAudioBitrate = audioStream?.BitDepth;
int? inputAudioSampleRate = audioStream?.SampleRate;
2019-02-12 05:28:02 -07:00
int? inputAudioBitDepth = audioStream?.BitDepth;
2018-12-27 16:27:57 -07:00
2021-04-17 03:19:09 -07:00
if (directPlayMethods.Any())
2018-12-27 16:27:57 -07:00
{
2019-01-09 15:33:17 -07:00
string audioCodec = audioStream?.Codec;
2018-12-27 16:27:57 -07:00
// Make sure audio codec profiles are satisfied
var conditions = new List<ProfileCondition>();
2019-01-13 13:37:13 -07:00
foreach (var i in options.Profile.CodecProfiles)
2018-12-27 16:27:57 -07:00
{
if (i.Type == CodecType.Audio && i.ContainsAnyCodec(audioCodec, item.Container))
{
bool applyConditions = true;
foreach (ProfileCondition applyCondition in i.ApplyConditions)
2018-12-27 16:27:57 -07:00
{
2019-01-20 03:34:33 -07:00
if (!ConditionProcessor.IsAudioConditionSatisfied(applyCondition, inputAudioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth))
2018-12-27 16:27:57 -07:00
{
LogConditionFailure(options.Profile, "AudioCodecProfile", applyCondition, item);
applyConditions = false;
break;
}
}
if (applyConditions)
{
2019-01-09 15:33:17 -07:00
conditions.AddRange(i.Conditions);
2018-12-27 16:27:57 -07:00
}
}
}
bool all = true;
foreach (ProfileCondition c in conditions)
2018-12-27 16:27:57 -07:00
{
2019-01-20 03:34:33 -07:00
if (!ConditionProcessor.IsAudioConditionSatisfied(c, inputAudioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth))
2018-12-27 16:27:57 -07:00
{
LogConditionFailure(options.Profile, "AudioCodecProfile", c, item);
var transcodeReason = GetTranscodeReasonForFailedCondition(c);
if (transcodeReason.HasValue)
{
transcodeReasons.Add(transcodeReason.Value);
}
2018-12-27 16:27:57 -07:00
all = false;
break;
}
}
if (all)
{
if (directPlayMethods.Contains(PlayMethod.DirectStream))
{
playlistItem.PlayMethod = PlayMethod.DirectStream;
}
playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Audio);
2018-12-27 16:27:57 -07:00
return playlistItem;
}
}
TranscodingProfile transcodingProfile = null;
2019-01-13 13:37:13 -07:00
foreach (var i in options.Profile.TranscodingProfiles)
2018-12-27 16:27:57 -07:00
{
2019-01-20 03:34:33 -07:00
if (i.Type == playlistItem.MediaType
&& i.Context == options.Context
&& _transcoderSupport.CanEncodeToAudioCodec(i.AudioCodec ?? i.Container))
2018-12-27 16:27:57 -07:00
{
2019-01-20 03:34:33 -07:00
transcodingProfile = i;
break;
2018-12-27 16:27:57 -07:00
}
}
if (transcodingProfile != null)
{
if (!item.SupportsTranscoding)
{
return null;
}
SetStreamInfoOptionsFromTranscodingProfile(playlistItem, transcodingProfile);
var audioCodecProfiles = new List<CodecProfile>();
2019-01-13 13:37:13 -07:00
foreach (var i in options.Profile.CodecProfiles)
2018-12-27 16:27:57 -07:00
{
if (i.Type == CodecType.Audio && i.ContainsAnyCodec(transcodingProfile.AudioCodec, transcodingProfile.Container))
{
audioCodecProfiles.Add(i);
}
2020-06-20 01:35:29 -07:00
if (audioCodecProfiles.Count >= 1)
{
break;
}
2018-12-27 16:27:57 -07:00
}
var audioTranscodingConditions = new List<ProfileCondition>();
2019-01-13 13:37:13 -07:00
foreach (var i in audioCodecProfiles)
2018-12-27 16:27:57 -07:00
{
bool applyConditions = true;
2019-01-13 13:37:13 -07:00
foreach (var applyCondition in i.ApplyConditions)
2018-12-27 16:27:57 -07:00
{
2019-01-20 03:34:33 -07:00
if (!ConditionProcessor.IsAudioConditionSatisfied(applyCondition, inputAudioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth))
2018-12-27 16:27:57 -07:00
{
LogConditionFailure(options.Profile, "AudioCodecProfile", applyCondition, item);
applyConditions = false;
break;
}
}
if (applyConditions)
{
foreach (ProfileCondition c in i.Conditions)
2018-12-27 16:27:57 -07:00
{
audioTranscodingConditions.Add(c);
}
}
}
ApplyTranscodingConditions(playlistItem, audioTranscodingConditions, null, true, true);
// Honor requested max channels
playlistItem.GlobalMaxAudioChannels = options.MaxAudioChannels;
var configuredBitrate = options.GetMaxBitrate(true);
long transcodingBitrate = options.AudioTranscodingBitrate ??
(options.Context == EncodingContext.Streaming ? options.Profile.MusicStreamingTranscodingBitrate : null) ??
configuredBitrate ??
128000;
if (configuredBitrate.HasValue)
{
transcodingBitrate = Math.Min(configuredBitrate.Value, transcodingBitrate);
}
var longBitrate = Math.Min(transcodingBitrate, playlistItem.AudioBitrate ?? transcodingBitrate);
playlistItem.AudioBitrate = longBitrate > int.MaxValue ? int.MaxValue : Convert.ToInt32(longBitrate);
}
playlistItem.TranscodeReasons = transcodeReasons.ToArray();
return playlistItem;
}
2019-01-20 03:34:33 -07:00
private static long? GetBitrateForDirectPlayCheck(MediaSourceInfo item, AudioOptions options, bool isAudio)
2018-12-27 16:27:57 -07:00
{
if (item.Protocol == MediaProtocol.File)
{
return options.Profile.MaxStaticBitrate;
}
return options.GetMaxBitrate(isAudio);
}
2019-01-09 15:33:17 -07:00
private (IEnumerable<PlayMethod>, IEnumerable<TranscodeReason>) GetAudioDirectPlayMethods(MediaSourceInfo item, MediaStream audioStream, AudioOptions options)
2018-12-27 16:27:57 -07:00
{
2019-01-09 15:33:17 -07:00
DirectPlayProfile directPlayProfile = options.Profile.DirectPlayProfiles
.FirstOrDefault(x => x.Type == DlnaProfileType.Audio && IsAudioDirectPlaySupported(x, item, audioStream));
2018-12-27 16:27:57 -07:00
2019-01-09 15:33:17 -07:00
if (directPlayProfile == null)
2018-12-27 16:27:57 -07:00
{
2020-10-12 11:05:11 -07:00
_logger.LogInformation(
"Profile: {0}, No audio direct play profiles found for {1} with codec {2}",
2019-01-09 15:33:17 -07:00
options.Profile.Name ?? "Unknown Profile",
item.Path ?? "Unknown path",
audioStream.Codec ?? "Unknown codec");
2019-01-09 15:33:17 -07:00
return (Enumerable.Empty<PlayMethod>(), GetTranscodeReasonsFromDirectPlayProfile(item, null, audioStream, options.Profile.DirectPlayProfiles));
2018-12-27 16:27:57 -07:00
}
var playMethods = new List<PlayMethod>();
2019-01-09 15:33:17 -07:00
var transcodeReasons = new List<TranscodeReason>();
2018-12-27 16:27:57 -07:00
2019-01-09 15:33:17 -07:00
// While options takes the network and other factors into account. Only applies to direct stream
if (item.SupportsDirectStream)
2018-12-27 16:27:57 -07:00
{
2019-01-09 15:33:17 -07:00
if (IsAudioEligibleForDirectPlay(item, options.GetMaxBitrate(true) ?? 0, PlayMethod.DirectStream))
2018-12-27 16:27:57 -07:00
{
2019-01-09 15:33:17 -07:00
if (options.EnableDirectStream)
2018-12-27 16:27:57 -07:00
{
2019-01-09 15:33:17 -07:00
playMethods.Add(PlayMethod.DirectStream);
2018-12-27 16:27:57 -07:00
}
}
2019-01-09 15:33:17 -07:00
else
{
transcodeReasons.Add(TranscodeReason.ContainerBitrateExceedsLimit);
}
}
2018-12-27 16:27:57 -07:00
2019-01-09 15:33:17 -07:00
// The profile describes what the device supports
// If device requirements are satisfied then allow both direct stream and direct play
if (item.SupportsDirectPlay)
{
if (IsAudioEligibleForDirectPlay(item, GetBitrateForDirectPlayCheck(item, options, true) ?? 0, PlayMethod.DirectPlay))
2018-12-27 16:27:57 -07:00
{
2019-01-09 15:33:17 -07:00
if (options.EnableDirectPlay)
2018-12-27 16:27:57 -07:00
{
2019-01-09 15:33:17 -07:00
playMethods.Add(PlayMethod.DirectPlay);
2018-12-27 16:27:57 -07:00
}
}
2019-01-09 15:33:17 -07:00
else
{
transcodeReasons.Add(TranscodeReason.ContainerBitrateExceedsLimit);
}
2018-12-27 16:27:57 -07:00
}
if (playMethods.Count > 0)
{
transcodeReasons.Clear();
}
else
{
transcodeReasons = transcodeReasons.Distinct().ToList();
}
2019-01-09 15:33:17 -07:00
return (playMethods, transcodeReasons);
2018-12-27 16:27:57 -07:00
}
2019-01-20 03:34:33 -07:00
private static List<TranscodeReason> GetTranscodeReasonsFromDirectPlayProfile(MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream, IEnumerable<DirectPlayProfile> directPlayProfiles)
2018-12-27 16:27:57 -07:00
{
var mediaType = videoStream == null ? DlnaProfileType.Audio : DlnaProfileType.Video;
2021-03-27 01:48:59 -07:00
2018-12-27 16:27:57 -07:00
var containerSupported = false;
var audioSupported = false;
var videoSupported = false;
foreach (var profile in directPlayProfiles)
{
// Check container type
2021-03-27 01:48:59 -07:00
if (profile.Type == mediaType && profile.SupportsContainer(item.Container))
2018-12-27 16:27:57 -07:00
{
containerSupported = true;
2019-01-09 15:33:17 -07:00
videoSupported = videoStream != null && profile.SupportsVideoCodec(videoStream.Codec);
2018-12-27 16:27:57 -07:00
2019-01-09 15:33:17 -07:00
audioSupported = audioStream != null && profile.SupportsAudioCodec(audioStream.Codec);
2018-12-27 16:27:57 -07:00
if (videoSupported && audioSupported)
{
break;
}
}
}
2019-01-09 15:33:17 -07:00
var list = new List<TranscodeReason>();
2018-12-27 16:27:57 -07:00
if (!containerSupported)
{
list.Add(TranscodeReason.ContainerNotSupported);
}
if (videoStream != null && !videoSupported)
{
list.Add(TranscodeReason.VideoCodecNotSupported);
}
if (audioStream != null && !audioSupported)
{
list.Add(TranscodeReason.AudioCodecNotSupported);
}
return list;
}
2019-01-20 03:34:33 -07:00
private static int? GetDefaultSubtitleStreamIndex(MediaSourceInfo item, SubtitleProfile[] subtitleProfiles)
2018-12-27 16:27:57 -07:00
{
int highestScore = -1;
2019-01-13 13:37:13 -07:00
foreach (var stream in item.MediaStreams)
2018-12-27 16:27:57 -07:00
{
2019-01-20 03:34:33 -07:00
if (stream.Type == MediaStreamType.Subtitle
&& stream.Score.HasValue
&& stream.Score.Value > highestScore)
2018-12-27 16:27:57 -07:00
{
2019-01-20 03:34:33 -07:00
highestScore = stream.Score.Value;
2018-12-27 16:27:57 -07:00
}
}
var topStreams = new List<MediaStream>();
2019-01-13 13:37:13 -07:00
foreach (var stream in item.MediaStreams)
2018-12-27 16:27:57 -07:00
{
if (stream.Type == MediaStreamType.Subtitle && stream.Score.HasValue && stream.Score.Value == highestScore)
{
topStreams.Add(stream);
}
}
// If multiple streams have an equal score, try to pick the most efficient one
if (topStreams.Count > 1)
{
2019-01-13 13:37:13 -07:00
foreach (var stream in topStreams)
2018-12-27 16:27:57 -07:00
{
2019-01-13 13:37:13 -07:00
foreach (var profile in subtitleProfiles)
2018-12-27 16:27:57 -07:00
{
2020-01-09 09:07:13 -07:00
if (profile.Method == SubtitleDeliveryMethod.External && string.Equals(profile.Format, stream.Codec, StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
return stream.Index;
}
}
}
}
// If no optimization panned out, just use the original default
return item.DefaultSubtitleStreamIndex;
}
2019-01-20 03:34:33 -07:00
private static void SetStreamInfoOptionsFromTranscodingProfile(StreamInfo playlistItem, TranscodingProfile transcodingProfile)
2018-12-27 16:27:57 -07:00
{
if (string.IsNullOrEmpty(transcodingProfile.AudioCodec))
{
playlistItem.AudioCodecs = Array.Empty<string>();
}
else
{
playlistItem.AudioCodecs = transcodingProfile.AudioCodec.Split(',');
}
playlistItem.Container = transcodingProfile.Container;
playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
if (string.IsNullOrEmpty(transcodingProfile.VideoCodec))
{
playlistItem.VideoCodecs = Array.Empty<string>();
}
else
{
playlistItem.VideoCodecs = transcodingProfile.VideoCodec.Split(',');
}
playlistItem.CopyTimestamps = transcodingProfile.CopyTimestamps;
playlistItem.EnableSubtitlesInManifest = transcodingProfile.EnableSubtitlesInManifest;
playlistItem.EnableMpegtsM2TsMode = transcodingProfile.EnableMpegtsM2TsMode;
playlistItem.BreakOnNonKeyFrames = transcodingProfile.BreakOnNonKeyFrames;
if (transcodingProfile.MinSegments > 0)
{
playlistItem.MinSegments = transcodingProfile.MinSegments;
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
if (transcodingProfile.SegmentLength > 0)
{
playlistItem.SegmentLength = transcodingProfile.SegmentLength;
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
playlistItem.SubProtocol = transcodingProfile.Protocol;
2019-01-20 03:34:33 -07:00
if (!string.IsNullOrEmpty(transcodingProfile.MaxAudioChannels)
&& int.TryParse(transcodingProfile.MaxAudioChannels, NumberStyles.Any, CultureInfo.InvariantCulture, out int transcodingMaxAudioChannels))
2018-12-27 16:27:57 -07:00
{
2019-01-20 03:34:33 -07:00
playlistItem.TranscodingMaxAudioChannels = transcodingMaxAudioChannels;
2018-12-27 16:27:57 -07:00
}
}
private StreamInfo BuildVideoItem(MediaSourceInfo item, VideoOptions options)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item));
2018-12-27 16:27:57 -07:00
}
2019-01-20 03:34:33 -07:00
StreamInfo playlistItem = new StreamInfo
2018-12-27 16:27:57 -07:00
{
ItemId = options.ItemId,
MediaType = DlnaProfileType.Video,
MediaSource = item,
RunTimeTicks = item.RunTimeTicks,
Context = options.Context,
DeviceProfile = options.Profile
};
playlistItem.SubtitleStreamIndex = options.SubtitleStreamIndex ?? GetDefaultSubtitleStreamIndex(item, options.Profile.SubtitleProfiles);
2019-01-13 13:37:13 -07:00
var subtitleStream = playlistItem.SubtitleStreamIndex.HasValue ? item.GetMediaStream(MediaStreamType.Subtitle, playlistItem.SubtitleStreamIndex.Value) : null;
2018-12-27 16:27:57 -07:00
2019-01-13 13:37:13 -07:00
var audioStream = item.GetDefaultAudioStream(options.AudioStreamIndex ?? item.DefaultAudioStreamIndex);
2018-12-27 16:27:57 -07:00
if (audioStream != null)
{
playlistItem.AudioStreamIndex = audioStream.Index;
}
2019-01-13 13:37:13 -07:00
var videoStream = item.VideoStream;
2018-12-27 16:27:57 -07:00
// TODO: This doesn't account for situations where the device is able to handle the media's bitrate, but the connection isn't fast enough
2018-12-27 16:27:57 -07:00
var directPlayEligibilityResult = IsEligibleForDirectPlay(item, GetBitrateForDirectPlayCheck(item, options, true) ?? 0, subtitleStream, options, PlayMethod.DirectPlay);
var directStreamEligibilityResult = IsEligibleForDirectPlay(item, options.GetMaxBitrate(false) ?? 0, subtitleStream, options, PlayMethod.DirectStream);
bool isEligibleForDirectPlay = options.EnableDirectPlay && (options.ForceDirectPlay || directPlayEligibilityResult.Item1);
bool isEligibleForDirectStream = options.EnableDirectStream && (options.ForceDirectStream || directStreamEligibilityResult.Item1);
2020-10-12 11:05:11 -07:00
_logger.LogInformation(
"Profile: {0}, Path: {1}, isEligibleForDirectPlay: {2}, isEligibleForDirectStream: {3}",
2018-12-27 16:27:57 -07:00
options.Profile.Name ?? "Unknown Profile",
item.Path ?? "Unknown path",
isEligibleForDirectPlay,
isEligibleForDirectStream);
2019-01-20 03:34:33 -07:00
var transcodeReasons = new List<TranscodeReason>();
2018-12-27 16:27:57 -07:00
if (isEligibleForDirectPlay || isEligibleForDirectStream)
{
// See if it can be direct played
2021-08-28 15:32:50 -07:00
var directPlayInfo = GetVideoDirectPlayProfile(options, item, videoStream, audioStream, isEligibleForDirectStream);
2018-12-27 16:27:57 -07:00
var directPlay = directPlayInfo.Item1;
if (directPlay != null)
{
playlistItem.PlayMethod = directPlay.Value;
playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Video);
2018-12-27 16:27:57 -07:00
if (subtitleStream != null)
{
2019-01-13 13:37:13 -07:00
var subtitleProfile = GetSubtitleProfile(item, subtitleStream, options.Profile.SubtitleProfiles, directPlay.Value, _transcoderSupport, item.Container, null);
2018-12-27 16:27:57 -07:00
playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
playlistItem.SubtitleFormat = subtitleProfile.Format;
}
return playlistItem;
}
transcodeReasons.AddRange(directPlayInfo.Item2);
}
if (directPlayEligibilityResult.Item2.HasValue)
{
transcodeReasons.Add(directPlayEligibilityResult.Item2.Value);
}
if (directStreamEligibilityResult.Item2.HasValue)
{
transcodeReasons.Add(directStreamEligibilityResult.Item2.Value);
}
// Can't direct play, find the transcoding profile
TranscodingProfile transcodingProfile = null;
2019-01-13 13:37:13 -07:00
foreach (var i in options.Profile.TranscodingProfiles)
2018-12-27 16:27:57 -07:00
{
if (i.Type == playlistItem.MediaType && i.Context == options.Context)
{
transcodingProfile = i;
break;
}
}
if (transcodingProfile != null)
{
if (!item.SupportsTranscoding)
{
return null;
}
if (subtitleStream != null)
{
2019-01-13 13:37:13 -07:00
var subtitleProfile = GetSubtitleProfile(item, subtitleStream, options.Profile.SubtitleProfiles, PlayMethod.Transcode, _transcoderSupport, transcodingProfile.Container, transcodingProfile.Protocol);
2018-12-27 16:27:57 -07:00
playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
playlistItem.SubtitleFormat = subtitleProfile.Format;
playlistItem.SubtitleCodecs = new[] { subtitleProfile.Format };
}
playlistItem.PlayMethod = PlayMethod.Transcode;
SetStreamInfoOptionsFromTranscodingProfile(playlistItem, transcodingProfile);
var isFirstAppliedCodecProfile = true;
2019-01-13 13:37:13 -07:00
foreach (var i in options.Profile.CodecProfiles)
2018-12-27 16:27:57 -07:00
{
if (i.Type == CodecType.Video && i.ContainsAnyCodec(transcodingProfile.VideoCodec, transcodingProfile.Container))
{
bool applyConditions = true;
foreach (ProfileCondition applyCondition in i.ApplyConditions)
2018-12-27 16:27:57 -07:00
{
2019-01-20 04:03:05 -07:00
int? width = videoStream?.Width;
int? height = videoStream?.Height;
int? bitDepth = videoStream?.BitDepth;
int? videoBitrate = videoStream?.BitRate;
double? videoLevel = videoStream?.Level;
string videoProfile = videoStream?.Profile;
2018-12-27 16:27:57 -07:00
float videoFramerate = videoStream == null ? 0 : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate ?? 0;
2019-01-20 04:03:05 -07:00
bool? isAnamorphic = videoStream?.IsAnamorphic;
bool? isInterlaced = videoStream?.IsInterlaced;
string videoCodecTag = videoStream?.CodecTag;
bool? isAvc = videoStream?.IsAVC;
2018-12-27 16:27:57 -07:00
TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : item.Timestamp;
2019-01-20 04:03:05 -07:00
int? packetLength = videoStream?.PacketLength;
int? refFrames = videoStream?.RefFrames;
2018-12-27 16:27:57 -07:00
int? numAudioStreams = item.GetStreamCount(MediaStreamType.Audio);
int? numVideoStreams = item.GetStreamCount(MediaStreamType.Video);
2019-01-20 03:34:33 -07:00
if (!ConditionProcessor.IsVideoConditionSatisfied(applyCondition, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
2018-12-27 16:27:57 -07:00
{
2020-06-14 02:11:11 -07:00
// LogConditionFailure(options.Profile, "VideoCodecProfile.ApplyConditions", applyCondition, item);
2018-12-27 16:27:57 -07:00
applyConditions = false;
break;
}
}
if (applyConditions)
{
var transcodingVideoCodecs = ContainerProfile.SplitValue(transcodingProfile.VideoCodec);
foreach (var transcodingVideoCodec in transcodingVideoCodecs)
{
if (i.ContainsAnyCodec(transcodingVideoCodec, transcodingProfile.Container))
{
ApplyTranscodingConditions(playlistItem, i.Conditions, transcodingVideoCodec, true, isFirstAppliedCodecProfile);
isFirstAppliedCodecProfile = false;
}
}
}
}
}
// Honor requested max channels
playlistItem.GlobalMaxAudioChannels = options.MaxAudioChannels;
2021-08-28 15:32:50 -07:00
int audioBitrate = GetAudioBitrate(options.GetMaxBitrate(false) ?? 0, playlistItem.TargetAudioCodec, audioStream, playlistItem);
2018-12-27 16:27:57 -07:00
playlistItem.AudioBitrate = Math.Min(playlistItem.AudioBitrate ?? audioBitrate, audioBitrate);
isFirstAppliedCodecProfile = true;
2019-01-13 13:37:13 -07:00
foreach (var i in options.Profile.CodecProfiles)
2018-12-27 16:27:57 -07:00
{
if (i.Type == CodecType.VideoAudio && i.ContainsAnyCodec(transcodingProfile.AudioCodec, transcodingProfile.Container))
{
bool applyConditions = true;
foreach (ProfileCondition applyCondition in i.ApplyConditions)
2018-12-27 16:27:57 -07:00
{
bool? isSecondaryAudio = audioStream == null ? null : item.IsSecondaryAudio(audioStream);
int? inputAudioBitrate = audioStream == null ? null : audioStream.BitRate;
int? audioChannels = audioStream == null ? null : audioStream.Channels;
string audioProfile = audioStream == null ? null : audioStream.Profile;
int? inputAudioSampleRate = audioStream == null ? null : audioStream.SampleRate;
int? inputAudioBitDepth = audioStream == null ? null : audioStream.BitDepth;
2019-01-20 03:34:33 -07:00
if (!ConditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth, audioProfile, isSecondaryAudio))
2018-12-27 16:27:57 -07:00
{
2020-06-14 02:11:11 -07:00
// LogConditionFailure(options.Profile, "VideoCodecProfile.ApplyConditions", applyCondition, item);
2018-12-27 16:27:57 -07:00
applyConditions = false;
break;
}
}
if (applyConditions)
{
var transcodingAudioCodecs = ContainerProfile.SplitValue(transcodingProfile.AudioCodec);
foreach (var transcodingAudioCodec in transcodingAudioCodecs)
{
if (i.ContainsAnyCodec(transcodingAudioCodec, transcodingProfile.Container))
{
ApplyTranscodingConditions(playlistItem, i.Conditions, transcodingAudioCodec, true, isFirstAppliedCodecProfile);
isFirstAppliedCodecProfile = false;
}
}
}
}
}
var maxBitrateSetting = options.GetMaxBitrate(false);
// Honor max rate
if (maxBitrateSetting.HasValue)
{
var availableBitrateForVideo = maxBitrateSetting.Value;
if (playlistItem.AudioBitrate.HasValue)
{
availableBitrateForVideo -= playlistItem.AudioBitrate.Value;
}
// Make sure the video bitrate is lower than bitrate settings but at least 64k
long currentValue = playlistItem.VideoBitrate ?? availableBitrateForVideo;
var longBitrate = Math.Max(Math.Min(availableBitrateForVideo, currentValue), 64000);
playlistItem.VideoBitrate = longBitrate >= int.MaxValue ? int.MaxValue : Convert.ToInt32(longBitrate);
}
}
playlistItem.TranscodeReasons = transcodeReasons.ToArray();
return playlistItem;
}
private static int GetDefaultAudioBitrate(string audioCodec, int? audioChannels)
2018-12-27 16:27:57 -07:00
{
if (!string.IsNullOrEmpty(audioCodec))
2018-12-27 16:27:57 -07:00
{
// Default to a higher bitrate for stream copy
if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase)
|| string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase)
|| string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase)
|| string.Equals(audioCodec, "eac3", StringComparison.OrdinalIgnoreCase))
{
if ((audioChannels ?? 0) < 2)
{
return 128000;
}
return (audioChannels ?? 0) >= 6 ? 640000 : 384000;
}
if (string.Equals(audioCodec, "flac", StringComparison.OrdinalIgnoreCase)
|| string.Equals(audioCodec, "alac", StringComparison.OrdinalIgnoreCase))
{
if ((audioChannels ?? 0) < 2)
{
return 768000;
}
return (audioChannels ?? 0) >= 6 ? 3584000 : 1536000;
}
2018-12-27 16:27:57 -07:00
}
return 192000;
}
2021-08-28 15:32:50 -07:00
private static int GetAudioBitrate(long maxTotalBitrate, string[] targetAudioCodecs, MediaStream audioStream, StreamInfo item)
2018-12-27 16:27:57 -07:00
{
2019-01-20 04:03:05 -07:00
string targetAudioCodec = targetAudioCodecs.Length == 0 ? null : targetAudioCodecs[0];
2018-12-27 16:27:57 -07:00
2019-01-20 04:03:05 -07:00
int? targetAudioChannels = item.GetTargetAudioChannels(targetAudioCodec);
2018-12-27 16:27:57 -07:00
2019-01-20 04:03:05 -07:00
int defaultBitrate;
int encoderAudioBitrateLimit = int.MaxValue;
2018-12-27 16:27:57 -07:00
2019-01-20 04:03:05 -07:00
if (audioStream == null)
2018-12-27 16:27:57 -07:00
{
2019-01-20 04:03:05 -07:00
defaultBitrate = 192000;
2018-12-27 16:27:57 -07:00
}
2019-01-20 04:03:05 -07:00
else
2018-12-27 16:27:57 -07:00
{
if (targetAudioChannels.HasValue
&& audioStream.Channels.HasValue
&& audioStream.Channels.Value > targetAudioChannels.Value)
2019-01-20 04:03:05 -07:00
{
// Reduce the bitrate if we're downmixing.
defaultBitrate = GetDefaultAudioBitrate(targetAudioCodec, targetAudioChannels);
}
else if (targetAudioChannels.HasValue
&& audioStream.Channels.HasValue
&& audioStream.Channels.Value <= targetAudioChannels.Value
&& !string.IsNullOrEmpty(audioStream.Codec)
&& targetAudioCodecs != null
&& targetAudioCodecs.Length > 0
&& !Array.Exists(targetAudioCodecs, elem => string.Equals(audioStream.Codec, elem, StringComparison.OrdinalIgnoreCase)))
{
// Shift the bitrate if we're transcoding to a different audio codec.
defaultBitrate = GetDefaultAudioBitrate(targetAudioCodec, audioStream.Channels.Value);
2019-01-20 04:03:05 -07:00
}
else
{
defaultBitrate = audioStream.BitRate ?? GetDefaultAudioBitrate(targetAudioCodec, targetAudioChannels);
2019-01-20 04:03:05 -07:00
}
2019-01-07 16:27:46 -07:00
// Seeing webm encoding failures when source has 1 audio channel and 22k bitrate.
2018-12-27 16:27:57 -07:00
// Any attempts to transcode over 64k will fail
2019-01-20 04:03:05 -07:00
if (audioStream.Channels == 1
&& (audioStream.BitRate ?? 0) < 64000)
2018-12-27 16:27:57 -07:00
{
2019-01-20 04:03:05 -07:00
encoderAudioBitrateLimit = 64000;
2018-12-27 16:27:57 -07:00
}
}
if (maxTotalBitrate > 0)
{
defaultBitrate = Math.Min(GetMaxAudioBitrateForTotalBitrate(maxTotalBitrate), defaultBitrate);
}
return Math.Min(defaultBitrate, encoderAudioBitrateLimit);
}
2019-01-20 04:03:05 -07:00
private static int GetMaxAudioBitrateForTotalBitrate(long totalBitrate)
2018-12-27 16:27:57 -07:00
{
if (totalBitrate <= 640000)
{
return 128000;
}
2019-01-20 04:03:05 -07:00
else if (totalBitrate <= 2000000)
2018-12-27 16:27:57 -07:00
{
return 384000;
}
2019-01-20 04:03:05 -07:00
else if (totalBitrate <= 3000000)
2018-12-27 16:27:57 -07:00
{
return 448000;
}
else if (totalBitrate <= 4000000)
{
return 640000;
}
else if (totalBitrate <= 5000000)
{
return 768000;
}
else if (totalBitrate <= 10000000)
{
return 1536000;
}
else if (totalBitrate <= 15000000)
{
return 2304000;
}
else if (totalBitrate <= 20000000)
{
return 3584000;
}
2018-12-27 16:27:57 -07:00
return 7168000;
2018-12-27 16:27:57 -07:00
}
2019-01-20 04:03:05 -07:00
private (PlayMethod?, List<TranscodeReason>) GetVideoDirectPlayProfile(
VideoOptions options,
2018-12-27 16:27:57 -07:00
MediaSourceInfo mediaSource,
MediaStream videoStream,
MediaStream audioStream,
bool isEligibleForDirectStream)
{
if (options.ForceDirectPlay)
{
2019-01-20 04:03:05 -07:00
return (PlayMethod.DirectPlay, new List<TranscodeReason>());
2018-12-27 16:27:57 -07:00
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
if (options.ForceDirectStream)
{
2019-01-20 04:03:05 -07:00
return (PlayMethod.DirectStream, new List<TranscodeReason>());
2018-12-27 16:27:57 -07:00
}
2019-01-20 04:03:05 -07:00
DeviceProfile profile = options.Profile;
string container = mediaSource.Container;
2019-01-20 04:03:05 -07:00
2018-12-27 16:27:57 -07:00
// See if it can be direct played
DirectPlayProfile directPlay = null;
foreach (var p in profile.DirectPlayProfiles)
2018-12-27 16:27:57 -07:00
{
if (p.Type == DlnaProfileType.Video && IsVideoDirectPlaySupported(p, container, videoStream, audioStream))
2018-12-27 16:27:57 -07:00
{
directPlay = p;
2018-12-27 16:27:57 -07:00
break;
}
}
if (directPlay == null)
{
2020-10-12 11:05:11 -07:00
_logger.LogInformation(
"Container: {Container}, Video: {Video}, Audio: {Audio} cannot be direct played by profile: {Profile} for path: {Path}",
container,
videoStream?.Codec ?? "no video",
audioStream?.Codec ?? "no audio",
profile.Name ?? "unknown profile",
mediaSource.Path ?? "unknown path");
2018-12-27 16:27:57 -07:00
2019-01-20 04:03:05 -07:00
return (null, GetTranscodeReasonsFromDirectPlayProfile(mediaSource, videoStream, audioStream, profile.DirectPlayProfiles));
2018-12-27 16:27:57 -07:00
}
var conditions = new List<ProfileCondition>();
foreach (var p in profile.ContainerProfiles)
2018-12-27 16:27:57 -07:00
{
if (p.Type == DlnaProfileType.Video
&& p.ContainsContainer(container))
2018-12-27 16:27:57 -07:00
{
foreach (var c in p.Conditions)
2018-12-27 16:27:57 -07:00
{
conditions.Add(c);
}
}
}
2019-01-20 03:34:33 -07:00
int? width = videoStream?.Width;
int? height = videoStream?.Height;
int? bitDepth = videoStream?.BitDepth;
int? videoBitrate = videoStream?.BitRate;
double? videoLevel = videoStream?.Level;
string videoProfile = videoStream?.Profile;
2018-12-27 16:27:57 -07:00
float videoFramerate = videoStream == null ? 0 : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate ?? 0;
2019-01-20 03:34:33 -07:00
bool? isAnamorphic = videoStream?.IsAnamorphic;
bool? isInterlaced = videoStream?.IsInterlaced;
string videoCodecTag = videoStream?.CodecTag;
bool? isAvc = videoStream?.IsAVC;
2018-12-27 16:27:57 -07:00
2019-01-20 03:34:33 -07:00
int? audioBitrate = audioStream?.BitRate;
int? audioChannels = audioStream?.Channels;
string audioProfile = audioStream?.Profile;
int? audioSampleRate = audioStream?.SampleRate;
int? audioBitDepth = audioStream?.BitDepth;
2018-12-27 16:27:57 -07:00
TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : mediaSource.Timestamp;
2019-01-20 03:34:33 -07:00
int? packetLength = videoStream?.PacketLength;
int? refFrames = videoStream?.RefFrames;
2018-12-27 16:27:57 -07:00
int? numAudioStreams = mediaSource.GetStreamCount(MediaStreamType.Audio);
int? numVideoStreams = mediaSource.GetStreamCount(MediaStreamType.Video);
// Check container conditions
foreach (ProfileCondition i in conditions)
2018-12-27 16:27:57 -07:00
{
2019-01-20 03:34:33 -07:00
if (!ConditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
2018-12-27 16:27:57 -07:00
{
LogConditionFailure(profile, "VideoContainerProfile", i, mediaSource);
var transcodeReason = GetTranscodeReasonForFailedCondition(i);
var transcodeReasons = transcodeReason.HasValue
? new List<TranscodeReason> { transcodeReason.Value }
2019-01-20 04:03:05 -07:00
: new List<TranscodeReason>();
2018-12-27 16:27:57 -07:00
2019-01-20 04:03:05 -07:00
return (null, transcodeReasons);
2018-12-27 16:27:57 -07:00
}
}
2019-01-20 04:03:05 -07:00
string videoCodec = videoStream?.Codec;
2018-12-27 16:27:57 -07:00
conditions = new List<ProfileCondition>();
2019-01-13 13:37:13 -07:00
foreach (var i in profile.CodecProfiles)
2018-12-27 16:27:57 -07:00
{
if (i.Type == CodecType.Video && i.ContainsAnyCodec(videoCodec, container))
{
bool applyConditions = true;
foreach (ProfileCondition applyCondition in i.ApplyConditions)
2018-12-27 16:27:57 -07:00
{
2019-01-20 03:34:33 -07:00
if (!ConditionProcessor.IsVideoConditionSatisfied(applyCondition, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
2018-12-27 16:27:57 -07:00
{
2020-06-14 02:11:11 -07:00
// LogConditionFailure(profile, "VideoCodecProfile.ApplyConditions", applyCondition, mediaSource);
2018-12-27 16:27:57 -07:00
applyConditions = false;
break;
}
}
if (applyConditions)
{
foreach (ProfileCondition c in i.Conditions)
2018-12-27 16:27:57 -07:00
{
conditions.Add(c);
}
}
}
}
foreach (ProfileCondition i in conditions)
2018-12-27 16:27:57 -07:00
{
2019-01-20 03:34:33 -07:00
if (!ConditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
2018-12-27 16:27:57 -07:00
{
LogConditionFailure(profile, "VideoCodecProfile", i, mediaSource);
var transcodeReason = GetTranscodeReasonForFailedCondition(i);
var transcodeReasons = transcodeReason.HasValue
? new List<TranscodeReason> { transcodeReason.Value }
2019-01-20 03:34:33 -07:00
: new List<TranscodeReason>();
2018-12-27 16:27:57 -07:00
2019-01-20 04:03:05 -07:00
return (null, transcodeReasons);
2018-12-27 16:27:57 -07:00
}
}
if (audioStream != null)
{
string audioCodec = audioStream.Codec;
conditions = new List<ProfileCondition>();
2021-08-28 15:32:50 -07:00
bool? isSecondaryAudio = mediaSource.IsSecondaryAudio(audioStream);
2018-12-27 16:27:57 -07:00
2019-01-13 13:37:13 -07:00
foreach (var i in profile.CodecProfiles)
2018-12-27 16:27:57 -07:00
{
if (i.Type == CodecType.VideoAudio && i.ContainsAnyCodec(audioCodec, container))
{
bool applyConditions = true;
foreach (ProfileCondition applyCondition in i.ApplyConditions)
2018-12-27 16:27:57 -07:00
{
2019-01-20 03:34:33 -07:00
if (!ConditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, audioBitrate, audioSampleRate, audioBitDepth, audioProfile, isSecondaryAudio))
2018-12-27 16:27:57 -07:00
{
2020-06-14 02:11:11 -07:00
// LogConditionFailure(profile, "VideoAudioCodecProfile.ApplyConditions", applyCondition, mediaSource);
2018-12-27 16:27:57 -07:00
applyConditions = false;
break;
}
}
if (applyConditions)
{
foreach (ProfileCondition c in i.Conditions)
2018-12-27 16:27:57 -07:00
{
conditions.Add(c);
}
}
}
}
foreach (ProfileCondition i in conditions)
2018-12-27 16:27:57 -07:00
{
2019-01-20 03:34:33 -07:00
if (!ConditionProcessor.IsVideoAudioConditionSatisfied(i, audioChannels, audioBitrate, audioSampleRate, audioBitDepth, audioProfile, isSecondaryAudio))
2018-12-27 16:27:57 -07:00
{
LogConditionFailure(profile, "VideoAudioCodecProfile", i, mediaSource);
var transcodeReason = GetTranscodeReasonForFailedCondition(i);
var transcodeReasons = transcodeReason.HasValue
? new List<TranscodeReason> { transcodeReason.Value }
2019-01-20 03:34:33 -07:00
: new List<TranscodeReason>();
2018-12-27 16:27:57 -07:00
2019-01-20 04:03:05 -07:00
return (null, transcodeReasons);
2018-12-27 16:27:57 -07:00
}
}
}
if (isEligibleForDirectStream && mediaSource.SupportsDirectStream)
{
2019-01-20 04:03:05 -07:00
return (PlayMethod.DirectStream, new List<TranscodeReason>());
2018-12-27 16:27:57 -07:00
}
2019-01-20 04:03:05 -07:00
return (null, new List<TranscodeReason> { TranscodeReason.ContainerBitrateExceedsLimit });
2018-12-27 16:27:57 -07:00
}
private void LogConditionFailure(DeviceProfile profile, string type, ProfileCondition condition, MediaSourceInfo mediaSource)
{
2020-10-12 11:05:11 -07:00
_logger.LogInformation(
"Profile: {0}, DirectPlay=false. Reason={1}.{2} Condition: {3}. ConditionValue: {4}. IsRequired: {5}. Path: {6}",
2018-12-27 16:27:57 -07:00
type,
profile.Name ?? "Unknown Profile",
condition.Property,
condition.Condition,
condition.Value ?? string.Empty,
condition.IsRequired,
mediaSource.Path ?? "Unknown path");
}
2019-01-20 03:34:33 -07:00
private (bool directPlay, TranscodeReason? reason) IsEligibleForDirectPlay(
MediaSourceInfo item,
2018-12-27 16:27:57 -07:00
long maxBitrate,
MediaStream subtitleStream,
VideoOptions options,
PlayMethod playMethod)
{
if (subtitleStream != null)
{
2019-01-13 13:37:13 -07:00
var subtitleProfile = GetSubtitleProfile(item, subtitleStream, options.Profile.SubtitleProfiles, playMethod, _transcoderSupport, item.Container, null);
2018-12-27 16:27:57 -07:00
if (subtitleProfile.Method != SubtitleDeliveryMethod.External && subtitleProfile.Method != SubtitleDeliveryMethod.Embed)
{
_logger.LogInformation("Not eligible for {0} due to unsupported subtitles", playMethod);
2019-01-09 15:33:17 -07:00
return (false, TranscodeReason.SubtitleCodecNotSupported);
2018-12-27 16:27:57 -07:00
}
}
2019-01-09 15:33:17 -07:00
bool result = IsAudioEligibleForDirectPlay(item, maxBitrate, playMethod);
2018-12-27 16:27:57 -07:00
return (result, result ? null : TranscodeReason.ContainerBitrateExceedsLimit);
2018-12-27 16:27:57 -07:00
}
2019-01-20 04:03:05 -07:00
public static SubtitleProfile GetSubtitleProfile(
MediaSourceInfo mediaSource,
MediaStream subtitleStream,
SubtitleProfile[] subtitleProfiles,
PlayMethod playMethod,
ITranscoderSupport transcoderSupport,
string outputContainer,
string transcodingSubProtocol)
2018-12-27 16:27:57 -07:00
{
if (!subtitleStream.IsExternal && (playMethod != PlayMethod.Transcode || !string.Equals(transcodingSubProtocol, "hls", StringComparison.OrdinalIgnoreCase)))
{
// Look for supported embedded subs of the same format
2019-01-13 13:37:13 -07:00
foreach (var profile in subtitleProfiles)
2018-12-27 16:27:57 -07:00
{
if (!profile.SupportsLanguage(subtitleStream.Language))
{
continue;
}
if (profile.Method != SubtitleDeliveryMethod.Embed)
{
continue;
}
if (!ContainerProfile.ContainsContainer(profile.Container, outputContainer))
{
continue;
}
2021-08-28 15:32:50 -07:00
if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(outputContainer))
2018-12-27 16:27:57 -07:00
{
continue;
}
2020-01-09 09:07:13 -07:00
if (subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format) && string.Equals(profile.Format, subtitleStream.Codec, StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
return profile;
}
}
// Look for supported embedded subs of a convertible format
2019-01-13 13:37:13 -07:00
foreach (var profile in subtitleProfiles)
2018-12-27 16:27:57 -07:00
{
if (!profile.SupportsLanguage(subtitleStream.Language))
{
continue;
}
if (profile.Method != SubtitleDeliveryMethod.Embed)
{
continue;
}
if (!ContainerProfile.ContainsContainer(profile.Container, outputContainer))
{
continue;
}
2021-08-28 15:32:50 -07:00
if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(outputContainer))
2018-12-27 16:27:57 -07:00
{
continue;
}
if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsSubtitleConversionTo(profile.Format))
{
return profile;
}
}
}
// Look for an external or hls profile that matches the stream type (text/graphical) and doesn't require conversion
return GetExternalSubtitleProfile(mediaSource, subtitleStream, subtitleProfiles, playMethod, transcoderSupport, false) ??
GetExternalSubtitleProfile(mediaSource, subtitleStream, subtitleProfiles, playMethod, transcoderSupport, true) ??
new SubtitleProfile
{
Method = SubtitleDeliveryMethod.Encode,
Format = subtitleStream.Codec
};
}
2021-08-28 15:32:50 -07:00
private static bool IsSubtitleEmbedSupported(string transcodingContainer)
2018-12-27 16:27:57 -07:00
{
if (!string.IsNullOrEmpty(transcodingContainer))
{
2019-01-21 08:57:10 -07:00
string[] normalizedContainers = ContainerProfile.SplitValue(transcodingContainer);
2018-12-27 16:27:57 -07:00
2019-01-21 08:57:10 -07:00
if (ContainerProfile.ContainsContainer(normalizedContainers, "ts")
|| ContainerProfile.ContainsContainer(normalizedContainers, "mpegts")
|| ContainerProfile.ContainsContainer(normalizedContainers, "mp4"))
2018-12-27 16:27:57 -07:00
{
return false;
}
2019-01-21 08:57:10 -07:00
else if (ContainerProfile.ContainsContainer(normalizedContainers, "mkv")
|| ContainerProfile.ContainsContainer(normalizedContainers, "matroska"))
2018-12-27 16:27:57 -07:00
{
return true;
}
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
return false;
}
private static SubtitleProfile GetExternalSubtitleProfile(MediaSourceInfo mediaSource, MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod, ITranscoderSupport transcoderSupport, bool allowConversion)
{
2019-01-13 13:37:13 -07:00
foreach (var profile in subtitleProfiles)
2018-12-27 16:27:57 -07:00
{
if (profile.Method != SubtitleDeliveryMethod.External && profile.Method != SubtitleDeliveryMethod.Hls)
{
continue;
}
if (profile.Method == SubtitleDeliveryMethod.Hls && playMethod != PlayMethod.Transcode)
{
continue;
}
if (!profile.SupportsLanguage(subtitleStream.Language))
{
continue;
}
if (!subtitleStream.IsExternal && !transcoderSupport.CanExtractSubtitles(subtitleStream.Codec))
{
continue;
}
if ((profile.Method == SubtitleDeliveryMethod.External && subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format)) ||
(profile.Method == SubtitleDeliveryMethod.Hls && subtitleStream.IsTextSubtitleStream))
{
2020-01-09 09:07:13 -07:00
bool requiresConversion = !string.Equals(subtitleStream.Codec, profile.Format, StringComparison.OrdinalIgnoreCase);
2018-12-27 16:27:57 -07:00
if (!requiresConversion)
{
return profile;
}
if (!allowConversion)
{
continue;
}
// TODO: Build this into subtitleStream.SupportsExternalStream
if (mediaSource.IsInfiniteStream)
{
continue;
}
if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsExternalStream && subtitleStream.SupportsSubtitleConversionTo(profile.Format))
{
return profile;
}
}
}
return null;
}
private bool IsAudioEligibleForDirectPlay(MediaSourceInfo item, long maxBitrate, PlayMethod playMethod)
{
// Don't restrict by bitrate if coming from an external domain
if (item.IsRemote)
{
return true;
}
2019-01-20 04:03:05 -07:00
long requestedMaxBitrate = maxBitrate > 0 ? maxBitrate : 1000000;
2018-12-27 16:27:57 -07:00
// If we don't know the bitrate, then force a transcode if requested max bitrate is under 40 mbps
2019-01-20 04:03:05 -07:00
int itemBitrate = item.Bitrate ?? 40000000;
2018-12-27 16:27:57 -07:00
if (itemBitrate > requestedMaxBitrate)
{
2020-10-12 11:05:11 -07:00
_logger.LogInformation(
"Bitrate exceeds {PlayBackMethod} limit: media bitrate: {MediaBitrate}, max bitrate: {MaxBitrate}",
playMethod,
itemBitrate,
requestedMaxBitrate);
2018-12-27 16:27:57 -07:00
return false;
}
return true;
}
2019-01-20 04:03:05 -07:00
private static void ValidateInput(VideoOptions options)
2018-12-27 16:27:57 -07:00
{
ValidateAudioInput(options);
if (options.AudioStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
{
throw new ArgumentException("MediaSourceId is required when a specific audio stream is requested");
}
if (options.SubtitleStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
{
throw new ArgumentException("MediaSourceId is required when a specific subtitle stream is requested");
}
}
2019-01-20 04:03:05 -07:00
private static void ValidateAudioInput(AudioOptions options)
2018-12-27 16:27:57 -07:00
{
if (options.ItemId.Equals(Guid.Empty))
{
throw new ArgumentException("ItemId is required");
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
if (string.IsNullOrEmpty(options.DeviceId))
{
throw new ArgumentException("DeviceId is required");
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
if (options.Profile == null)
{
throw new ArgumentException("Profile is required");
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
if (options.MediaSources == null)
{
throw new ArgumentException("MediaSources is required");
}
}
private void ApplyTranscodingConditions(StreamInfo item, IEnumerable<ProfileCondition> conditions, string qualifier, bool enableQualifiedConditions, bool enableNonQualifiedConditions)
{
foreach (ProfileCondition condition in conditions)
2018-12-27 16:27:57 -07:00
{
string value = condition.Value;
if (string.IsNullOrEmpty(value))
{
continue;
}
// No way to express this
if (condition.Condition == ProfileConditionType.GreaterThanEqual)
{
continue;
}
switch (condition.Property)
{
case ProfileConditionValue.AudioBitrate:
{
if (!enableNonQualifiedConditions)
{
continue;
}
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var num))
2018-12-27 16:27:57 -07:00
{
if (condition.Condition == ProfileConditionType.Equals)
{
item.AudioBitrate = num;
}
else if (condition.Condition == ProfileConditionType.LessThanEqual)
{
item.AudioBitrate = Math.Min(num, item.AudioBitrate ?? num);
}
else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
{
item.AudioBitrate = Math.Max(num, item.AudioBitrate ?? num);
}
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
break;
}
2020-09-28 13:04:31 -07:00
case ProfileConditionValue.AudioSampleRate:
{
if (!enableNonQualifiedConditions)
{
continue;
}
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var num))
{
if (condition.Condition == ProfileConditionType.Equals)
{
item.AudioSampleRate = num;
}
else if (condition.Condition == ProfileConditionType.LessThanEqual)
{
item.AudioSampleRate = Math.Min(num, item.AudioSampleRate ?? num);
}
else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
{
item.AudioSampleRate = Math.Max(num, item.AudioSampleRate ?? num);
}
}
break;
}
2018-12-27 16:27:57 -07:00
case ProfileConditionValue.AudioChannels:
{
if (string.IsNullOrEmpty(qualifier))
{
if (!enableNonQualifiedConditions)
{
continue;
}
}
else
{
if (!enableQualifiedConditions)
{
continue;
}
}
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var num))
2018-12-27 16:27:57 -07:00
{
if (condition.Condition == ProfileConditionType.Equals)
{
item.SetOption(qualifier, "audiochannels", num.ToString(CultureInfo.InvariantCulture));
}
else if (condition.Condition == ProfileConditionType.LessThanEqual)
{
item.SetOption(qualifier, "audiochannels", Math.Min(num, item.GetTargetAudioChannels(qualifier) ?? num).ToString(CultureInfo.InvariantCulture));
}
else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
{
item.SetOption(qualifier, "audiochannels", Math.Max(num, item.GetTargetAudioChannels(qualifier) ?? num).ToString(CultureInfo.InvariantCulture));
}
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
break;
}
2020-09-28 13:04:31 -07:00
2018-12-27 16:27:57 -07:00
case ProfileConditionValue.IsAvc:
{
if (!enableNonQualifiedConditions)
{
continue;
}
if (bool.TryParse(value, out var isAvc))
2018-12-27 16:27:57 -07:00
{
if (isAvc && condition.Condition == ProfileConditionType.Equals)
{
item.RequireAvc = true;
}
else if (!isAvc && condition.Condition == ProfileConditionType.NotEquals)
{
item.RequireAvc = true;
}
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
break;
}
2020-09-28 13:04:31 -07:00
2018-12-27 16:27:57 -07:00
case ProfileConditionValue.IsAnamorphic:
{
if (!enableNonQualifiedConditions)
{
continue;
}
if (bool.TryParse(value, out var isAnamorphic))
2018-12-27 16:27:57 -07:00
{
if (isAnamorphic && condition.Condition == ProfileConditionType.Equals)
{
item.RequireNonAnamorphic = true;
}
else if (!isAnamorphic && condition.Condition == ProfileConditionType.NotEquals)
{
item.RequireNonAnamorphic = true;
}
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
break;
}
2020-09-28 13:04:31 -07:00
2018-12-27 16:27:57 -07:00
case ProfileConditionValue.IsInterlaced:
{
if (string.IsNullOrEmpty(qualifier))
{
if (!enableNonQualifiedConditions)
{
continue;
}
}
else
{
if (!enableQualifiedConditions)
{
continue;
}
}
if (bool.TryParse(value, out var isInterlaced))
2018-12-27 16:27:57 -07:00
{
if (!isInterlaced && condition.Condition == ProfileConditionType.Equals)
{
item.SetOption(qualifier, "deinterlace", "true");
}
else if (isInterlaced && condition.Condition == ProfileConditionType.NotEquals)
{
item.SetOption(qualifier, "deinterlace", "true");
}
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
break;
}
2020-09-28 13:04:31 -07:00
2018-12-27 16:27:57 -07:00
case ProfileConditionValue.AudioProfile:
case ProfileConditionValue.Has64BitOffsets:
case ProfileConditionValue.PacketLength:
case ProfileConditionValue.NumAudioStreams:
case ProfileConditionValue.NumVideoStreams:
case ProfileConditionValue.IsSecondaryAudio:
case ProfileConditionValue.VideoTimestamp:
{
// Not supported yet
break;
}
2020-09-28 13:04:31 -07:00
2018-12-27 16:27:57 -07:00
case ProfileConditionValue.RefFrames:
{
if (string.IsNullOrEmpty(qualifier))
{
if (!enableNonQualifiedConditions)
{
continue;
}
}
else
{
if (!enableQualifiedConditions)
{
continue;
}
}
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var num))
2018-12-27 16:27:57 -07:00
{
if (condition.Condition == ProfileConditionType.Equals)
{
item.SetOption(qualifier, "maxrefframes", num.ToString(CultureInfo.InvariantCulture));
}
else if (condition.Condition == ProfileConditionType.LessThanEqual)
{
item.SetOption(qualifier, "maxrefframes", Math.Min(num, item.GetTargetRefFrames(qualifier) ?? num).ToString(CultureInfo.InvariantCulture));
}
else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
{
item.SetOption(qualifier, "maxrefframes", Math.Max(num, item.GetTargetRefFrames(qualifier) ?? num).ToString(CultureInfo.InvariantCulture));
}
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
break;
}
2020-09-28 13:04:31 -07:00
2018-12-27 16:27:57 -07:00
case ProfileConditionValue.VideoBitDepth:
{
if (string.IsNullOrEmpty(qualifier))
{
if (!enableNonQualifiedConditions)
{
continue;
}
}
else
{
if (!enableQualifiedConditions)
{
continue;
}
}
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var num))
2018-12-27 16:27:57 -07:00
{
if (condition.Condition == ProfileConditionType.Equals)
{
item.SetOption(qualifier, "videobitdepth", num.ToString(CultureInfo.InvariantCulture));
}
else if (condition.Condition == ProfileConditionType.LessThanEqual)
{
item.SetOption(qualifier, "videobitdepth", Math.Min(num, item.GetTargetVideoBitDepth(qualifier) ?? num).ToString(CultureInfo.InvariantCulture));
}
else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
{
item.SetOption(qualifier, "videobitdepth", Math.Max(num, item.GetTargetVideoBitDepth(qualifier) ?? num).ToString(CultureInfo.InvariantCulture));
}
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
break;
}
2020-09-28 13:04:31 -07:00
2018-12-27 16:27:57 -07:00
case ProfileConditionValue.VideoProfile:
{
if (string.IsNullOrEmpty(qualifier))
{
continue;
}
2021-08-28 15:32:50 -07:00
// change from split by | to comma
// strip spaces to avoid having to encode
var values = value
.Split('|', StringSplitOptions.RemoveEmptyEntries);
2018-12-27 16:27:57 -07:00
2021-08-28 15:32:50 -07:00
if (condition.Condition == ProfileConditionType.Equals || condition.Condition == ProfileConditionType.EqualsAny)
{
item.SetOption(qualifier, "profile", string.Join(',', values));
2018-12-27 16:27:57 -07:00
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
break;
}
2020-09-28 13:04:31 -07:00
2018-12-27 16:27:57 -07:00
case ProfileConditionValue.Height:
{
if (!enableNonQualifiedConditions)
{
continue;
}
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var num))
2018-12-27 16:27:57 -07:00
{
if (condition.Condition == ProfileConditionType.Equals)
{
item.MaxHeight = num;
}
else if (condition.Condition == ProfileConditionType.LessThanEqual)
{
item.MaxHeight = Math.Min(num, item.MaxHeight ?? num);
}
else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
{
item.MaxHeight = Math.Max(num, item.MaxHeight ?? num);
}
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
break;
}
2020-09-28 13:04:31 -07:00
2018-12-27 16:27:57 -07:00
case ProfileConditionValue.VideoBitrate:
{
if (!enableNonQualifiedConditions)
{
continue;
}
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var num))
2018-12-27 16:27:57 -07:00
{
if (condition.Condition == ProfileConditionType.Equals)
{
item.VideoBitrate = num;
}
else if (condition.Condition == ProfileConditionType.LessThanEqual)
{
item.VideoBitrate = Math.Min(num, item.VideoBitrate ?? num);
}
else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
{
item.VideoBitrate = Math.Max(num, item.VideoBitrate ?? num);
}
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
break;
}
2020-09-28 13:04:31 -07:00
2018-12-27 16:27:57 -07:00
case ProfileConditionValue.VideoFramerate:
{
if (!enableNonQualifiedConditions)
{
continue;
}
if (float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var num))
2018-12-27 16:27:57 -07:00
{
if (condition.Condition == ProfileConditionType.Equals)
{
item.MaxFramerate = num;
}
else if (condition.Condition == ProfileConditionType.LessThanEqual)
{
item.MaxFramerate = Math.Min(num, item.MaxFramerate ?? num);
}
else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
{
item.MaxFramerate = Math.Max(num, item.MaxFramerate ?? num);
}
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
break;
}
2020-09-28 13:04:31 -07:00
2018-12-27 16:27:57 -07:00
case ProfileConditionValue.VideoLevel:
{
if (string.IsNullOrEmpty(qualifier))
{
continue;
}
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var num))
2018-12-27 16:27:57 -07:00
{
if (condition.Condition == ProfileConditionType.Equals)
{
item.SetOption(qualifier, "level", num.ToString(CultureInfo.InvariantCulture));
}
else if (condition.Condition == ProfileConditionType.LessThanEqual)
{
item.SetOption(qualifier, "level", Math.Min(num, item.GetTargetVideoLevel(qualifier) ?? num).ToString(CultureInfo.InvariantCulture));
}
else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
{
item.SetOption(qualifier, "level", Math.Max(num, item.GetTargetVideoLevel(qualifier) ?? num).ToString(CultureInfo.InvariantCulture));
}
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
break;
}
2020-09-28 13:04:31 -07:00
2018-12-27 16:27:57 -07:00
case ProfileConditionValue.Width:
{
if (!enableNonQualifiedConditions)
{
continue;
}
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var num))
2018-12-27 16:27:57 -07:00
{
if (condition.Condition == ProfileConditionType.Equals)
{
item.MaxWidth = num;
}
else if (condition.Condition == ProfileConditionType.LessThanEqual)
{
item.MaxWidth = Math.Min(num, item.MaxWidth ?? num);
}
else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
{
item.MaxWidth = Math.Max(num, item.MaxWidth ?? num);
}
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
break;
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
default:
break;
}
}
}
2019-01-20 04:03:05 -07:00
private static bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audioStream)
2018-12-27 16:27:57 -07:00
{
// Check container type
if (!profile.SupportsContainer(item.Container))
{
return false;
}
// Check audio codec
2019-01-20 04:03:05 -07:00
string audioCodec = audioStream?.Codec;
2018-12-27 16:27:57 -07:00
if (!profile.SupportsAudioCodec(audioCodec))
{
return false;
}
return true;
}
private bool IsVideoDirectPlaySupported(DirectPlayProfile profile, string container, MediaStream videoStream, MediaStream audioStream)
2018-12-27 16:27:57 -07:00
{
// Check container type
if (!profile.SupportsContainer(container))
2018-12-27 16:27:57 -07:00
{
return false;
}
// Check video codec
2019-01-20 04:03:05 -07:00
string videoCodec = videoStream?.Codec;
2018-12-27 16:27:57 -07:00
if (!profile.SupportsVideoCodec(videoCodec))
{
return false;
}
// Check audio codec
2019-01-20 04:03:05 -07:00
if (audioStream != null && !profile.SupportsAudioCodec(audioStream.Codec))
2018-12-27 16:27:57 -07:00
{
2019-01-20 04:03:05 -07:00
return false;
2018-12-27 16:27:57 -07:00
}
return true;
}
}
}