You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Model/Dlna/StreamBuilder.cs

2080 lines
93 KiB

#nullable disable
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using MediaBrowser.Model.Dto;
6 years ago
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.MediaInfo;
using MediaBrowser.Model.Session;
using Microsoft.Extensions.Logging;
6 years ago
namespace MediaBrowser.Model.Dlna
{
public class StreamBuilder
{
// Aliases
internal const TranscodeReason ContainerReasons = TranscodeReason.ContainerNotSupported | TranscodeReason.ContainerBitrateExceedsLimit;
internal const TranscodeReason AudioReasons = TranscodeReason.AudioCodecNotSupported | TranscodeReason.AudioBitrateNotSupported | TranscodeReason.AudioChannelsNotSupported | TranscodeReason.AudioProfileNotSupported | TranscodeReason.AudioSampleRateNotSupported | TranscodeReason.SecondaryAudioNotSupported | TranscodeReason.AudioBitDepthNotSupported | TranscodeReason.AudioIsExternal;
internal const TranscodeReason VideoReasons = TranscodeReason.VideoCodecNotSupported | TranscodeReason.VideoResolutionNotSupported | TranscodeReason.AnamorphicVideoNotSupported | TranscodeReason.InterlacedVideoNotSupported | TranscodeReason.VideoBitDepthNotSupported | TranscodeReason.VideoBitrateNotSupported | TranscodeReason.VideoFramerateNotSupported | TranscodeReason.VideoLevelNotSupported | TranscodeReason.RefFramesNotSupported;
internal const TranscodeReason DirectStreamReasons = AudioReasons | TranscodeReason.ContainerNotSupported;
6 years ago
private readonly ILogger _logger;
private readonly ITranscoderSupport _transcoderSupport;
private static readonly string[] _supportedHlsVideoCodecs = new string[] { "h264", "hevc" };
private static readonly string[] _supportedHlsAudioCodecsTs = new string[] { "aac", "ac3", "eac3", "mp3" };
private static readonly string[] _supportedHlsAudioCodecsMp4 = new string[] { "aac", "ac3", "eac3", "mp3", "alac", "flac", "opus", "dca", "truehd" };
6 years ago
public StreamBuilder(ITranscoderSupport transcoderSupport, ILogger logger)
{
_transcoderSupport = transcoderSupport;
_logger = logger;
}
public StreamBuilder(ILogger<StreamBuilder> logger)
6 years ago
: this(new FullTranscoderSupport(), logger)
{
}
public StreamInfo BuildAudioItem(AudioOptions options)
{
ValidateAudioInput(options);
var mediaSources = new List<MediaSourceInfo>();
foreach (MediaSourceInfo i in options.MediaSources)
6 years ago
{
if (string.IsNullOrEmpty(options.MediaSourceId) ||
string.Equals(i.Id, options.MediaSourceId, StringComparison.OrdinalIgnoreCase))
6 years ago
{
mediaSources.Add(i);
}
}
var streams = new List<StreamInfo>();
foreach (MediaSourceInfo i in mediaSources)
6 years ago
{
StreamInfo streamInfo = BuildAudioItem(i, options);
6 years ago
if (streamInfo != null)
{
streams.Add(streamInfo);
}
}
foreach (StreamInfo stream in streams)
6 years ago
{
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)
6 years ago
{
if (string.IsNullOrEmpty(options.MediaSourceId) ||
string.Equals(i.Id, options.MediaSourceId, StringComparison.OrdinalIgnoreCase))
6 years ago
{
mediaSources.Add(i);
}
}
var streams = new List<StreamInfo>();
foreach (MediaSourceInfo i in mediaSources)
6 years ago
{
var streamInfo = BuildVideoItem(i, options);
6 years ago
if (streamInfo != null)
{
streams.Add(streamInfo);
}
}
foreach (StreamInfo stream in streams)
6 years ago
{
stream.DeviceId = options.DeviceId;
stream.DeviceProfileId = options.Profile.Id;
}
return GetOptimalStream(streams, options.GetMaxBitrate(false) ?? 0);
}
private static StreamInfo GetOptimalStream(List<StreamInfo> streams, long maxBitrate)
=> SortMediaSources(streams, maxBitrate).FirstOrDefault();
6 years ago
private static IOrderedEnumerable<StreamInfo> SortMediaSources(List<StreamInfo> streams, long maxBitrate)
6 years ago
{
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;
}).ThenBy(streams.IndexOf);
6 years ago
}
private static TranscodeReason GetTranscodeReasonForFailedCondition(ProfileCondition condition)
6 years ago
{
switch (condition.Property)
{
case ProfileConditionValue.AudioBitrate:
6 years ago
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 0;
6 years ago
case ProfileConditionValue.Height:
return TranscodeReason.VideoResolutionNotSupported;
case ProfileConditionValue.IsAnamorphic:
return TranscodeReason.AnamorphicVideoNotSupported;
case ProfileConditionValue.IsAvc:
// TODO
return 0;
6 years ago
case ProfileConditionValue.IsInterlaced:
return TranscodeReason.InterlacedVideoNotSupported;
case ProfileConditionValue.IsSecondaryAudio:
return TranscodeReason.SecondaryAudioNotSupported;
case ProfileConditionValue.NumAudioStreams:
// TODO
return 0;
6 years ago
case ProfileConditionValue.NumVideoStreams:
// TODO
return 0;
6 years ago
case ProfileConditionValue.PacketLength:
// TODO
return 0;
6 years ago
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.VideoRangeType:
return TranscodeReason.VideoRangeTypeNotSupported;
6 years ago
case ProfileConditionValue.VideoTimestamp:
// TODO
return 0;
6 years ago
case ProfileConditionValue.Width:
return TranscodeReason.VideoResolutionNotSupported;
default:
return 0;
6 years ago
}
}
public static string NormalizeMediaSourceFormatIntoSingleContainer(string inputContainer, DeviceProfile profile, DlnaProfileType type, DirectPlayProfile playProfile = null)
6 years ago
{
if (string.IsNullOrEmpty(inputContainer))
{
return null;
}
var formats = ContainerProfile.SplitValue(inputContainer);
if (profile != null)
{
var playProfiles = playProfile == null ? profile.DirectPlayProfiles : new[] { playProfile };
6 years ago
foreach (var format in formats)
{
foreach (var directPlayProfile in playProfiles)
6 years ago
{
if (directPlayProfile.Type == type
&& directPlayProfile.SupportsContainer(format))
6 years ago
{
return format;
6 years ago
}
}
}
}
return formats[0];
}
private StreamInfo BuildAudioItem(MediaSourceInfo item, AudioOptions options)
{
StreamInfo playlistItem = new StreamInfo
6 years ago
{
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);
6 years ago
return playlistItem;
}
if (options.ForceDirectStream)
{
playlistItem.PlayMethod = PlayMethod.DirectStream;
playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Audio);
6 years ago
return playlistItem;
}
var audioStream = item.GetDefaultAudioStream(null);
6 years ago
var directPlayInfo = GetAudioDirectPlayProfile(item, audioStream, options);
6 years ago
var directPlayMethod = directPlayInfo.PlayMethod;
var transcodeReasons = directPlayInfo.TranscodeReasons;
6 years ago
int? inputAudioChannels = audioStream?.Channels;
int? inputAudioBitrate = audioStream?.BitDepth;
int? inputAudioSampleRate = audioStream?.SampleRate;
int? inputAudioBitDepth = audioStream?.BitDepth;
6 years ago
if (directPlayMethod.HasValue)
6 years ago
{
var profile = options.Profile;
var audioFailureConditions = GetProfileConditionsForAudio(profile.CodecProfiles, item.Container, audioStream?.Codec, inputAudioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth, true);
var audioFailureReasons = AggregateFailureConditions(item, profile, "AudioCodecProfile", audioFailureConditions);
transcodeReasons |= audioFailureReasons;
6 years ago
if (audioFailureReasons == 0)
6 years ago
{
playlistItem.PlayMethod = directPlayMethod.Value;
playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Audio, directPlayInfo.Profile);
6 years ago
return playlistItem;
}
}
TranscodingProfile transcodingProfile = null;
foreach (var i in options.Profile.TranscodingProfiles)
6 years ago
{
if (i.Type == playlistItem.MediaType
&& i.Context == options.Context
&& _transcoderSupport.CanEncodeToAudioCodec(i.AudioCodec ?? i.Container))
6 years ago
{
transcodingProfile = i;
break;
6 years ago
}
}
if (transcodingProfile != null)
{
if (!item.SupportsTranscoding)
{
return null;
}
SetStreamInfoOptionsFromTranscodingProfile(item, playlistItem, transcodingProfile);
6 years ago
var audioTranscodingConditions = GetProfileConditionsForAudio(options.Profile.CodecProfiles, transcodingProfile.Container, transcodingProfile.AudioCodec, inputAudioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth, false).ToArray();
6 years ago
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;
6 years ago
return playlistItem;
}
private (DirectPlayProfile Profile, PlayMethod? PlayMethod, TranscodeReason TranscodeReasons) GetAudioDirectPlayProfile(MediaSourceInfo item, MediaStream audioStream, AudioOptions options)
6 years ago
{
var directPlayProfile = options.Profile.DirectPlayProfiles
.FirstOrDefault(x => x.Type == DlnaProfileType.Audio && IsAudioDirectPlaySupported(x, item, audioStream));
6 years ago
if (directPlayProfile == null)
6 years ago
{
_logger.LogDebug(
"Profile: {0}, No audio direct play profiles found for {1} with codec {2}",
options.Profile.Name ?? "Unknown Profile",
item.Path ?? "Unknown path",
audioStream.Codec ?? "Unknown codec");
return (null, null, GetTranscodeReasonsFromDirectPlayProfile(item, null, audioStream, options.Profile.DirectPlayProfiles));
6 years ago
}
var playMethods = new List<PlayMethod>();
TranscodeReason transcodeReasons = 0;
6 years ago
// The profile describes what the device supports
// If device requirements are satisfied then allow both direct stream and direct play
if (item.SupportsDirectPlay)
6 years ago
{
if (IsItemBitrateEligibleForDirectPlayback(item, options.GetMaxBitrate(true) ?? 0, PlayMethod.DirectPlay))
6 years ago
{
if (options.EnableDirectPlay)
6 years ago
{
return (directPlayProfile, PlayMethod.DirectPlay, 0);
6 years ago
}
}
else
{
transcodeReasons |= TranscodeReason.ContainerBitrateExceedsLimit;
}
}
6 years ago
// While options takes the network and other factors into account. Only applies to direct stream
if (item.SupportsDirectStream)
{
if (IsItemBitrateEligibleForDirectPlayback(item, options.GetMaxBitrate(true) ?? 0, PlayMethod.DirectStream))
6 years ago
{
if (options.EnableDirectStream)
6 years ago
{
return (directPlayProfile, PlayMethod.DirectStream, transcodeReasons);
6 years ago
}
}
else
{
transcodeReasons |= TranscodeReason.ContainerBitrateExceedsLimit;
}
6 years ago
}
return (directPlayProfile, null, transcodeReasons);
6 years ago
}
private static TranscodeReason GetTranscodeReasonsFromDirectPlayProfile(MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream, IEnumerable<DirectPlayProfile> directPlayProfiles)
6 years ago
{
var mediaType = videoStream == null ? DlnaProfileType.Audio : DlnaProfileType.Video;
6 years ago
var containerSupported = false;
var audioSupported = false;
var videoSupported = false;
TranscodeReason reasons = 0;
6 years ago
foreach (var profile in directPlayProfiles)
{
// Check container type
if (profile.Type == mediaType && profile.SupportsContainer(item.Container))
6 years ago
{
containerSupported = true;
videoSupported = videoStream == null || profile.SupportsVideoCodec(videoStream.Codec);
6 years ago
audioSupported = audioStream == null || profile.SupportsAudioCodec(audioStream.Codec);
6 years ago
if (videoSupported && audioSupported)
{
break;
}
}
}
if (!containerSupported)
{
reasons |= TranscodeReason.ContainerNotSupported;
6 years ago
}
if (!videoSupported)
6 years ago
{
reasons |= TranscodeReason.VideoCodecNotSupported;
6 years ago
}
if (!audioSupported)
6 years ago
{
reasons |= TranscodeReason.AudioCodecNotSupported;
6 years ago
}
return reasons;
6 years ago
}
private static int? GetDefaultSubtitleStreamIndex(MediaSourceInfo item, SubtitleProfile[] subtitleProfiles)
6 years ago
{
int highestScore = -1;
foreach (var stream in item.MediaStreams)
6 years ago
{
if (stream.Type == MediaStreamType.Subtitle
&& stream.Score.HasValue
&& stream.Score.Value > highestScore)
6 years ago
{
highestScore = stream.Score.Value;
6 years ago
}
}
var topStreams = new List<MediaStream>();
foreach (var stream in item.MediaStreams)
6 years ago
{
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)
{
foreach (var stream in topStreams)
6 years ago
{
foreach (var profile in subtitleProfiles)
6 years ago
{
if (profile.Method == SubtitleDeliveryMethod.External && string.Equals(profile.Format, stream.Codec, StringComparison.OrdinalIgnoreCase))
6 years ago
{
return stream.Index;
}
}
}
}
// If no optimization panned out, just use the original default
return item.DefaultSubtitleStreamIndex;
}
private static void SetStreamInfoOptionsFromTranscodingProfile(MediaSourceInfo item, StreamInfo playlistItem, TranscodingProfile transcodingProfile)
6 years ago
{
var container = transcodingProfile.Container;
var protocol = transcodingProfile.Protocol;
6 years ago
item.TranscodingContainer = container;
item.TranscodingSubProtocol = protocol;
6 years ago
if (playlistItem.PlayMethod == PlayMethod.Transcode)
6 years ago
{
playlistItem.Container = container;
playlistItem.SubProtocol = protocol;
6 years ago
}
playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
if (!string.IsNullOrEmpty(transcodingProfile.MaxAudioChannels)
&& int.TryParse(transcodingProfile.MaxAudioChannels, NumberStyles.Any, CultureInfo.InvariantCulture, out int transcodingMaxAudioChannels))
6 years ago
{
playlistItem.TranscodingMaxAudioChannels = transcodingMaxAudioChannels;
6 years ago
}
playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
6 years ago
playlistItem.CopyTimestamps = transcodingProfile.CopyTimestamps;
playlistItem.EnableSubtitlesInManifest = transcodingProfile.EnableSubtitlesInManifest;
playlistItem.EnableMpegtsM2TsMode = transcodingProfile.EnableMpegtsM2TsMode;
playlistItem.BreakOnNonKeyFrames = transcodingProfile.BreakOnNonKeyFrames;
if (transcodingProfile.MinSegments > 0)
{
playlistItem.MinSegments = transcodingProfile.MinSegments;
}
6 years ago
if (transcodingProfile.SegmentLength > 0)
{
playlistItem.SegmentLength = transcodingProfile.SegmentLength;
}
}
private static void SetStreamInfoOptionsFromDirectPlayProfile(VideoOptions options, MediaSourceInfo item, StreamInfo playlistItem, DirectPlayProfile directPlayProfile)
{
var container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Video, directPlayProfile);
var protocol = "http";
6 years ago
item.TranscodingContainer = container;
item.TranscodingSubProtocol = protocol;
playlistItem.Container = container;
playlistItem.SubProtocol = protocol;
playlistItem.VideoCodecs = new[] { item.VideoStream.Codec };
playlistItem.AudioCodecs = ContainerProfile.SplitValue(directPlayProfile.AudioCodec);
6 years ago
}
private StreamInfo BuildVideoItem(MediaSourceInfo item, VideoOptions options)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item));
6 years ago
}
StreamInfo playlistItem = new StreamInfo
6 years ago
{
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);
var subtitleStream = playlistItem.SubtitleStreamIndex.HasValue ? item.GetMediaStream(MediaStreamType.Subtitle, playlistItem.SubtitleStreamIndex.Value) : null;
6 years ago
var audioStream = item.GetDefaultAudioStream(options.AudioStreamIndex ?? item.DefaultAudioStreamIndex);
6 years ago
if (audioStream != null)
{
playlistItem.AudioStreamIndex = audioStream.Index;
}
// Collect candidate audio streams
ICollection<MediaStream> candidateAudioStreams = audioStream == null ? Array.Empty<MediaStream>() : new[] { audioStream };
if (!options.AudioStreamIndex.HasValue || options.AudioStreamIndex < 0)
{
if (audioStream?.IsDefault == true)
{
candidateAudioStreams = item.MediaStreams.Where(stream => stream.Type == MediaStreamType.Audio && stream.IsDefault).ToArray();
}
else
{
candidateAudioStreams = item.MediaStreams.Where(stream => stream.Type == MediaStreamType.Audio && stream.Language == audioStream?.Language).ToArray();
}
}
var videoStream = item.VideoStream;
6 years ago
var directPlayBitrateEligibility = IsBitrateEligibleForDirectPlayback(item, options.GetMaxBitrate(false) ?? 0, options, PlayMethod.DirectPlay);
var directStreamBitrateEligibility = IsBitrateEligibleForDirectPlayback(item, options.GetMaxBitrate(false) ?? 0, options, PlayMethod.DirectStream);
bool isEligibleForDirectPlay = options.EnableDirectPlay && (options.ForceDirectPlay || directPlayBitrateEligibility == 0);
bool isEligibleForDirectStream = options.EnableDirectStream && (options.ForceDirectStream || directStreamBitrateEligibility == 0);
var transcodeReasons = directPlayBitrateEligibility | directStreamBitrateEligibility;
6 years ago
_logger.LogDebug(
"Profile: {0}, Path: {1}, isEligibleForDirectPlay: {2}, isEligibleForDirectStream: {3}",
6 years ago
options.Profile.Name ?? "Unknown Profile",
item.Path ?? "Unknown path",
isEligibleForDirectPlay,
isEligibleForDirectStream);
DirectPlayProfile directPlayProfile = null;
6 years ago
if (isEligibleForDirectPlay || isEligibleForDirectStream)
{
// See if it can be direct played
var directPlayInfo = GetVideoDirectPlayProfile(options, item, videoStream, audioStream, candidateAudioStreams, subtitleStream, isEligibleForDirectPlay, isEligibleForDirectStream);
var directPlay = directPlayInfo.PlayMethod;
transcodeReasons |= directPlayInfo.TranscodeReasons;
6 years ago
if (directPlay.HasValue)
6 years ago
{
directPlayProfile = directPlayInfo.Profile;
6 years ago
playlistItem.PlayMethod = directPlay.Value;
playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Video, directPlayProfile);
playlistItem.VideoCodecs = new[] { videoStream.Codec };
if (directPlay == PlayMethod.DirectPlay)
{
playlistItem.SubProtocol = "http";
var audioStreamIndex = directPlayInfo.AudioStreamIndex ?? audioStream?.Index;
if (audioStreamIndex.HasValue)
{
playlistItem.AudioStreamIndex = audioStreamIndex;
playlistItem.AudioCodecs = new[] { item.GetMediaStream(MediaStreamType.Audio, audioStreamIndex.Value)?.Codec };
}
}
else if (directPlay == PlayMethod.DirectStream)
{
playlistItem.AudioStreamIndex = audioStream?.Index;
if (audioStream != null)
{
playlistItem.AudioCodecs = ContainerProfile.SplitValue(directPlayProfile.AudioCodec);
}
SetStreamInfoOptionsFromDirectPlayProfile(options, item, playlistItem, directPlayProfile);
BuildStreamVideoItem(playlistItem, options, item, videoStream, audioStream, candidateAudioStreams, directPlayProfile.Container, directPlayProfile.VideoCodec, directPlayProfile.AudioCodec);
}
6 years ago
if (subtitleStream != null)
{
var subtitleProfile = GetSubtitleProfile(item, subtitleStream, options.Profile.SubtitleProfiles, directPlay.Value, _transcoderSupport, directPlayProfile.Container, null);
6 years ago
playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
playlistItem.SubtitleFormat = subtitleProfile.Format;
}
}
_logger.LogDebug(
"DirectPlay Result for Profile: {0}, Path: {1}, PlayMethod: {2}, AudioStreamIndex: {3}, SubtitleStreamIndex: {4}, Reasons: {5}",
options.Profile.Name ?? "Anonymous Profile",
item.Path ?? "Unknown path",
directPlayInfo.PlayMethod,
directPlayInfo.AudioStreamIndex ?? audioStream?.Index,
playlistItem.SubtitleStreamIndex,
directPlayInfo.TranscodeReasons);
6 years ago
}
playlistItem.TranscodeReasons = transcodeReasons;
if (playlistItem.PlayMethod != PlayMethod.DirectStream && playlistItem.PlayMethod != PlayMethod.DirectPlay)
6 years ago
{
// Can't direct play, find the transcoding profile
// If we do this for direct-stream we will overwrite the info
var transcodingProfile = GetVideoTranscodeProfile(item, options, videoStream, audioStream, candidateAudioStreams, subtitleStream, playlistItem);
if (transcodingProfile != null)
{
SetStreamInfoOptionsFromTranscodingProfile(item, playlistItem, transcodingProfile);
BuildStreamVideoItem(playlistItem, options, item, videoStream, audioStream, candidateAudioStreams, transcodingProfile.Container, transcodingProfile.VideoCodec, transcodingProfile.AudioCodec);
playlistItem.PlayMethod = PlayMethod.Transcode;
if (subtitleStream != null)
{
var subtitleProfile = GetSubtitleProfile(item, subtitleStream, options.Profile.SubtitleProfiles, PlayMethod.Transcode, _transcoderSupport, transcodingProfile.Container, transcodingProfile.Protocol);
playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
playlistItem.SubtitleFormat = subtitleProfile.Format;
playlistItem.SubtitleCodecs = new[] { subtitleProfile.Format };
}
if ((playlistItem.TranscodeReasons & (VideoReasons | TranscodeReason.ContainerBitrateExceedsLimit)) != 0)
{
ApplyTranscodingConditions(playlistItem, transcodingProfile.Conditions, null, true, true);
}
}
6 years ago
}
_logger.LogInformation(
"StreamBuilder.BuildVideoItem( Profile={0}, Path={1}, AudioStreamIndex={2}, SubtitleStreamIndex={3} ) => ( PlayMethod={4}, TranscodeReason={5} ) {6}",
options.Profile.Name ?? "Anonymous Profile",
item.Path ?? "Unknown path",
options.AudioStreamIndex,
options.SubtitleStreamIndex,
playlistItem.PlayMethod,
playlistItem.TranscodeReasons,
playlistItem.ToUrl("media:", "<token>"));
item.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Video, directPlayProfile);
return playlistItem;
}
private TranscodingProfile GetVideoTranscodeProfile(MediaSourceInfo item, VideoOptions options, MediaStream videoStream, MediaStream audioStream, IEnumerable<MediaStream> candidateAudioStreams, MediaStream subtitleStream, StreamInfo playlistItem)
{
if (!(item.SupportsTranscoding || item.SupportsDirectStream))
6 years ago
{
return null;
6 years ago
}
var transcodingProfiles = options.Profile.TranscodingProfiles
.Where(i => i.Type == playlistItem.MediaType && i.Context == options.Context);
if (options.AllowVideoStreamCopy)
6 years ago
{
// prefer direct copy profile
float videoFramerate = videoStream?.AverageFrameRate ?? videoStream?.RealFrameRate ?? 0;
TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : item.Timestamp;
int? numAudioStreams = item.GetStreamCount(MediaStreamType.Audio);
int? numVideoStreams = item.GetStreamCount(MediaStreamType.Video);
transcodingProfiles = transcodingProfiles.ToLookup(transcodingProfile =>
6 years ago
{
var videoCodecs = ContainerProfile.SplitValue(transcodingProfile.VideoCodec);
if (ContainerProfile.ContainsContainer(videoCodecs, item.VideoStream?.Codec))
{
var videoCodec = videoStream?.Codec;
var container = transcodingProfile.Container;
var appliedVideoConditions = options.Profile.CodecProfiles
.Where(i => i.Type == CodecType.Video &&
i.ContainsAnyCodec(videoCodec, container) &&
i.ApplyConditions.All(applyCondition => ConditionProcessor.IsVideoConditionSatisfied(applyCondition, videoStream?.Width, videoStream?.Height, videoStream?.BitDepth, videoStream?.BitRate, videoStream?.Profile, videoStream?.VideoRangeType, videoStream?.Level, videoFramerate, videoStream?.PacketLength, timestamp, videoStream?.IsAnamorphic, videoStream?.IsInterlaced, videoStream?.RefFrames, numVideoStreams, numAudioStreams, videoStream?.CodecTag, videoStream?.IsAVC)))
.Select(i =>
i.Conditions.All(condition => ConditionProcessor.IsVideoConditionSatisfied(condition, videoStream?.Width, videoStream?.Height, videoStream?.BitDepth, videoStream?.BitRate, videoStream?.Profile, videoStream?.VideoRangeType, videoStream?.Level, videoFramerate, videoStream?.PacketLength, timestamp, videoStream?.IsAnamorphic, videoStream?.IsInterlaced, videoStream?.RefFrames, numVideoStreams, numAudioStreams, videoStream?.CodecTag, videoStream?.IsAVC)));
// An empty appliedVideoConditions means that the codec has no conditions for the current video stream
var conditionsSatisfied = appliedVideoConditions.All(satisfied => satisfied);
return conditionsSatisfied ? 1 : 2;
}
return 3;
})
.OrderBy(lookup => lookup.Key)
.SelectMany(lookup => lookup);
6 years ago
}
return transcodingProfiles.FirstOrDefault();
}
private void BuildStreamVideoItem(StreamInfo playlistItem, VideoOptions options, MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream, IEnumerable<MediaStream> candidateAudioStreams, string container, string videoCodec, string audioCodec)
{
// Prefer matching video codecs
var videoCodecs = ContainerProfile.SplitValue(videoCodec);
// Enforce HLS video codec restrictions
if (string.Equals(playlistItem.SubProtocol, "hls", StringComparison.OrdinalIgnoreCase))
{
videoCodecs = videoCodecs.Where(codec => _supportedHlsVideoCodecs.Contains(codec)).ToArray();
}
var directVideoCodec = ContainerProfile.ContainsContainer(videoCodecs, videoStream?.Codec) ? videoStream?.Codec : null;
if (directVideoCodec != null)
{
// merge directVideoCodec to videoCodecs
Array.Resize(ref videoCodecs, videoCodecs.Length + 1);
videoCodecs[^1] = directVideoCodec;
}
playlistItem.VideoCodecs = videoCodecs;
// Copy video codec options as a starting point, this applies to transcode and direct-stream
playlistItem.MaxFramerate = videoStream?.AverageFrameRate;
var qualifier = videoStream?.Codec;
if (videoStream?.Level != null)
6 years ago
{
playlistItem.SetOption(qualifier, "level", videoStream.Level.Value.ToString(CultureInfo.InvariantCulture));
}
if (videoStream?.BitDepth != null)
{
playlistItem.SetOption(qualifier, "videobitdepth", videoStream.BitDepth.Value.ToString(CultureInfo.InvariantCulture));
}
if (!string.IsNullOrEmpty(videoStream?.Profile))
{
playlistItem.SetOption(qualifier, "profile", videoStream.Profile.ToLowerInvariant());
}
if (videoStream != null && videoStream.Level != 0)
{
playlistItem.SetOption(qualifier, "level", videoStream.Level.ToString());
}
// Prefer matching audio codecs, could do better here
var audioCodecs = ContainerProfile.SplitValue(audioCodec);
// Enforce HLS audio codec restrictions
if (string.Equals(playlistItem.SubProtocol, "hls", StringComparison.OrdinalIgnoreCase))
{
if (string.Equals(playlistItem.Container, "mp4", StringComparison.OrdinalIgnoreCase))
{
audioCodecs = audioCodecs.Where(codec => _supportedHlsAudioCodecsMp4.Contains(codec)).ToArray();
}
else
{
audioCodecs = audioCodecs.Where(codec => _supportedHlsAudioCodecsTs.Contains(codec)).ToArray();
}
}
var directAudioStream = candidateAudioStreams.FirstOrDefault(stream => ContainerProfile.ContainsContainer(audioCodecs, stream.Codec));
playlistItem.AudioCodecs = audioCodecs;
if (directAudioStream != null)
{
audioStream = directAudioStream;
playlistItem.AudioStreamIndex = audioStream.Index;
playlistItem.AudioCodecs = new[] { audioStream.Codec };
// Copy matching audio codec options
playlistItem.AudioSampleRate = audioStream.SampleRate;
playlistItem.SetOption(qualifier, "audiochannels", audioStream.Channels.ToString());
if (!string.IsNullOrEmpty(audioStream.Profile))
6 years ago
{
playlistItem.SetOption(audioStream.Codec, "profile", audioStream.Profile.ToLowerInvariant());
6 years ago
}
if (audioStream.Level != 0)
6 years ago
{
playlistItem.SetOption(audioStream.Codec, "level", audioStream.Level.ToString());
6 years ago
}
}
6 years ago
int? width = videoStream?.Width;
int? height = videoStream?.Height;
int? bitDepth = videoStream?.BitDepth;
int? videoBitrate = videoStream?.BitRate;
double? videoLevel = videoStream?.Level;
string videoProfile = videoStream?.Profile;
string videoRangeType = videoStream?.VideoRangeType;
float videoFramerate = videoStream == null ? 0 : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate ?? 0;
bool? isAnamorphic = videoStream?.IsAnamorphic;
bool? isInterlaced = videoStream?.IsInterlaced;
string videoCodecTag = videoStream?.CodecTag;
bool? isAvc = videoStream?.IsAVC;
6 years ago
TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : item.Timestamp;
int? packetLength = videoStream?.PacketLength;
int? refFrames = videoStream?.RefFrames;
6 years ago
int? numAudioStreams = item.GetStreamCount(MediaStreamType.Audio);
int? numVideoStreams = item.GetStreamCount(MediaStreamType.Video);
6 years ago
var appliedVideoConditions = options.Profile.CodecProfiles
.Where(i => i.Type == CodecType.Video &&
i.ContainsAnyCodec(videoStream?.Codec, container) &&
i.ApplyConditions.All(applyCondition => ConditionProcessor.IsVideoConditionSatisfied(applyCondition, width, height, bitDepth, videoBitrate, videoProfile, videoRangeType, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc)));
var isFirstAppliedCodecProfile = true;
foreach (var i in appliedVideoConditions)
{
var transcodingVideoCodecs = ContainerProfile.SplitValue(videoCodec);
foreach (var transcodingVideoCodec in transcodingVideoCodecs)
6 years ago
{
if (i.ContainsAnyCodec(transcodingVideoCodec, container))
6 years ago
{
ApplyTranscodingConditions(playlistItem, i.Conditions, transcodingVideoCodec, true, isFirstAppliedCodecProfile);
isFirstAppliedCodecProfile = false;
continue;
6 years ago
}
}
}
6 years ago
// Honor requested max channels
playlistItem.GlobalMaxAudioChannels = options.MaxAudioChannels;
6 years ago
int audioBitrate = GetAudioBitrate(options.GetMaxBitrate(false) ?? 0, playlistItem.TargetAudioCodec, audioStream, playlistItem);
playlistItem.AudioBitrate = Math.Min(playlistItem.AudioBitrate ?? audioBitrate, audioBitrate);
6 years ago
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;
var appliedAudioConditions = options.Profile.CodecProfiles
.Where(i => i.Type == CodecType.VideoAudio &&
i.ContainsAnyCodec(audioStream?.Codec, container) &&
i.ApplyConditions.All(applyCondition => ConditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth, audioProfile, isSecondaryAudio)));
isFirstAppliedCodecProfile = true;
foreach (var i in appliedAudioConditions)
{
var transcodingAudioCodecs = ContainerProfile.SplitValue(audioCodec);
foreach (var transcodingAudioCodec in transcodingAudioCodecs)
6 years ago
{
if (i.ContainsAnyCodec(transcodingAudioCodec, container))
6 years ago
{
ApplyTranscodingConditions(playlistItem, i.Conditions, transcodingAudioCodec, true, isFirstAppliedCodecProfile);
isFirstAppliedCodecProfile = false;
break;
6 years ago
}
}
}
6 years ago
var maxBitrateSetting = options.GetMaxBitrate(false);
// Honor max rate
if (maxBitrateSetting.HasValue)
{
var availableBitrateForVideo = maxBitrateSetting.Value;
6 years ago
if (playlistItem.AudioBitrate.HasValue)
{
availableBitrateForVideo -= playlistItem.AudioBitrate.Value;
6 years ago
}
// Make sure the video bitrate is lower than bitrate settings but at least 64k
// Don't use Math.Clamp as availableBitrateForVideo can be lower then 64k.
var currentValue = playlistItem.VideoBitrate ?? availableBitrateForVideo;
playlistItem.VideoBitrate = Math.Max(Math.Min(availableBitrateForVideo, currentValue), 64_000);
}
6 years ago
_logger.LogDebug(
"Transcode Result for Profile: {Profile}, Path: {Path}, PlayMethod: {PlayMethod}, AudioStreamIndex: {AudioStreamIndex}, SubtitleStreamIndex: {SubtitleStreamIndex}, Reasons: {TranscodeReason}",
options.Profile?.Name ?? "Anonymous Profile",
item.Path ?? "Unknown path",
playlistItem?.PlayMethod,
audioStream?.Index,
playlistItem?.SubtitleStreamIndex,
playlistItem?.TranscodeReasons);
6 years ago
}
private static int GetDefaultAudioBitrate(string audioCodec, int? audioChannels)
6 years ago
{
if (!string.IsNullOrEmpty(audioCodec))
6 years ago
{
// 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;
}
6 years ago
}
return 192000;
}
private static int GetAudioBitrate(long maxTotalBitrate, string[] targetAudioCodecs, MediaStream audioStream, StreamInfo item)
6 years ago
{
string targetAudioCodec = targetAudioCodecs.Length == 0 ? null : targetAudioCodecs[0];
6 years ago
int? targetAudioChannels = item.GetTargetAudioChannels(targetAudioCodec);
6 years ago
int defaultBitrate;
int encoderAudioBitrateLimit = int.MaxValue;
6 years ago
if (audioStream == null)
6 years ago
{
defaultBitrate = 192000;
6 years ago
}
else
6 years ago
{
if (targetAudioChannels.HasValue
&& audioStream.Channels.HasValue
&& audioStream.Channels.Value > targetAudioChannels.Value)
{
// 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);
}
else
{
defaultBitrate = audioStream.BitRate ?? GetDefaultAudioBitrate(targetAudioCodec, targetAudioChannels);
}
// Seeing webm encoding failures when source has 1 audio channel and 22k bitrate.
6 years ago
// Any attempts to transcode over 64k will fail
if (audioStream.Channels == 1
&& (audioStream.BitRate ?? 0) < 64000)
6 years ago
{
encoderAudioBitrateLimit = 64000;
6 years ago
}
}
if (maxTotalBitrate > 0)
{
defaultBitrate = Math.Min(GetMaxAudioBitrateForTotalBitrate(maxTotalBitrate), defaultBitrate);
}
return Math.Min(defaultBitrate, encoderAudioBitrateLimit);
}
private static int GetMaxAudioBitrateForTotalBitrate(long totalBitrate)
6 years ago
{
if (totalBitrate <= 640000)
{
return 128000;
}
else if (totalBitrate <= 2000000)
6 years ago
{
return 384000;
}
else if (totalBitrate <= 3000000)
6 years ago
{
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;
}
6 years ago
return 7168000;
6 years ago
}
private (DirectPlayProfile Profile, PlayMethod? PlayMethod, int? AudioStreamIndex, TranscodeReason TranscodeReasons) GetVideoDirectPlayProfile(
VideoOptions options,
6 years ago
MediaSourceInfo mediaSource,
MediaStream videoStream,
MediaStream audioStream,
ICollection<MediaStream> candidateAudioStreams,
MediaStream subtitleStream,
bool isEligibleForDirectPlay,
6 years ago
bool isEligibleForDirectStream)
{
if (options.ForceDirectPlay)
{
return (null, PlayMethod.DirectPlay, audioStream?.Index, 0);
6 years ago
}
6 years ago
if (options.ForceDirectStream)
{
return (null, PlayMethod.DirectStream, audioStream?.Index, 0);
6 years ago
}
DeviceProfile profile = options.Profile;
string container = mediaSource.Container;
// Video
int? width = videoStream?.Width;
int? height = videoStream?.Height;
int? bitDepth = videoStream?.BitDepth;
int? videoBitrate = videoStream?.BitRate;
double? videoLevel = videoStream?.Level;
string videoProfile = videoStream?.Profile;
string videoRangeType = videoStream?.VideoRangeType;
6 years ago
float videoFramerate = videoStream == null ? 0 : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate ?? 0;
bool? isAnamorphic = videoStream?.IsAnamorphic;
bool? isInterlaced = videoStream?.IsInterlaced;
string videoCodecTag = videoStream?.CodecTag;
bool? isAvc = videoStream?.IsAVC;
6 years ago
TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : mediaSource.Timestamp;
int? packetLength = videoStream?.PacketLength;
int? refFrames = videoStream?.RefFrames;
6 years ago
int? numAudioStreams = mediaSource.GetStreamCount(MediaStreamType.Audio);
int? numVideoStreams = mediaSource.GetStreamCount(MediaStreamType.Video);
var checkVideoConditions = (ProfileCondition[] conditions) =>
conditions.Where(applyCondition => !ConditionProcessor.IsVideoConditionSatisfied(applyCondition, width, height, bitDepth, videoBitrate, videoProfile, videoRangeType, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc));
6 years ago
// Check container conditions
var containerProfileReasons = AggregateFailureConditions(
mediaSource,
profile,
"VideoCodecProfile",
profile.ContainerProfiles
.Where(containerProfile => containerProfile.Type == DlnaProfileType.Video && containerProfile.ContainsContainer(container))
.SelectMany(containerProfile => checkVideoConditions(containerProfile.Conditions)));
// Check video conditions
var videoCodecProfileReasons = AggregateFailureConditions(
mediaSource,
profile,
"VideoCodecProfile",
profile.CodecProfiles
.Where(codecProfile => codecProfile.Type == CodecType.Video &&
codecProfile.ContainsAnyCodec(videoStream?.Codec, container) &&
!checkVideoConditions(codecProfile.ApplyConditions).Any())
.SelectMany(codecProfile => checkVideoConditions(codecProfile.Conditions)));
6 years ago
// Check audiocandidates profile conditions
var audioStreamMatches = candidateAudioStreams.ToDictionary(s => s, audioStream => CheckVideoAudioStreamDirectPlay(options, mediaSource, container, audioStream));
6 years ago
TranscodeReason subtitleProfileReasons = 0;
if (subtitleStream != null)
6 years ago
{
var subtitleProfile = GetSubtitleProfile(mediaSource, subtitleStream, options.Profile.SubtitleProfiles, PlayMethod.DirectPlay, _transcoderSupport, container, null);
6 years ago
if (subtitleProfile.Method != SubtitleDeliveryMethod.Drop
&& subtitleProfile.Method != SubtitleDeliveryMethod.External
&& subtitleProfile.Method != SubtitleDeliveryMethod.Embed)
{
_logger.LogDebug("Not eligible for {0} due to unsupported subtitles", PlayMethod.DirectPlay);
subtitleProfileReasons |= TranscodeReason.SubtitleCodecNotSupported;
6 years ago
}
}
var rankings = new[] { VideoReasons, AudioReasons, ContainerReasons };
var rank = (ref TranscodeReason a) =>
6 years ago
{
var index = 1;
foreach (var flag in rankings)
6 years ago
{
var reason = a & flag;
if (reason != 0)
6 years ago
{
return index;
6 years ago
}
index++;
6 years ago
}
return index;
};
var containerSupported = false;
// Check DirectPlay profiles to see if it can be direct played
var analyzedProfiles = profile.DirectPlayProfiles
.Where(directPlayProfile => directPlayProfile.Type == DlnaProfileType.Video)
.Select((directPlayProfile, order) =>
6 years ago
{
TranscodeReason directPlayProfileReasons = 0;
TranscodeReason audioCodecProfileReasons = 0;
// Check container type
if (!directPlayProfile.SupportsContainer(container))
6 years ago
{
directPlayProfileReasons |= TranscodeReason.ContainerNotSupported;
}
else
{
containerSupported = true;
}
6 years ago
// Check video codec
string videoCodec = videoStream?.Codec;
if (!directPlayProfile.SupportsVideoCodec(videoCodec))
{
directPlayProfileReasons |= TranscodeReason.VideoCodecNotSupported;
6 years ago
}
// Check audio codec
MediaStream selectedAudioStream = null;
if (candidateAudioStreams.Any())
{
selectedAudioStream = candidateAudioStreams.FirstOrDefault(audioStream => directPlayProfile.SupportsAudioCodec(audioStream.Codec));
if (selectedAudioStream == null)
{
directPlayProfileReasons |= TranscodeReason.AudioCodecNotSupported;
}
else
{
audioCodecProfileReasons = audioStreamMatches.GetValueOrDefault(selectedAudioStream);
}
}
var failureReasons = directPlayProfileReasons | containerProfileReasons | subtitleProfileReasons;
if ((failureReasons & TranscodeReason.VideoCodecNotSupported) == 0)
{
failureReasons |= videoCodecProfileReasons;
}
if ((failureReasons & TranscodeReason.AudioCodecNotSupported) == 0)
{
failureReasons |= audioCodecProfileReasons;
}
var directStreamFailureReasons = failureReasons & (~DirectStreamReasons);
PlayMethod? playMethod = null;
if (failureReasons == 0 && isEligibleForDirectPlay && mediaSource.SupportsDirectPlay)
{
playMethod = PlayMethod.DirectPlay;
}
else if (directStreamFailureReasons == 0 && isEligibleForDirectStream && mediaSource.SupportsDirectStream)
{
playMethod = PlayMethod.DirectStream;
}
var ranked = rank(ref failureReasons);
return (Result: (Profile: directPlayProfile, PlayMethod: playMethod, AudioStreamIndex: selectedAudioStream?.Index, TranscodeReason: failureReasons), Order: order, Rank: ranked);
})
.OrderByDescending(analysis => analysis.Result.PlayMethod)
.ThenByDescending(analysis => analysis.Rank)
.ThenBy(analysis => analysis.Order)
.ToArray()
.ToLookup(analysis => analysis.Result.PlayMethod != null);
var profileMatch = analyzedProfiles[true]
.Select(analysis => analysis.Result)
.FirstOrDefault();
if (profileMatch.Profile != null)
{
return profileMatch;
6 years ago
}
var failureReasons = analyzedProfiles[false]
.Select(analysis => analysis.Result)
.Where(result => !containerSupported || (result.TranscodeReason & TranscodeReason.ContainerNotSupported) == 0)
.FirstOrDefault().TranscodeReason;
if (failureReasons == 0)
6 years ago
{
failureReasons = TranscodeReason.DirectPlayError;
6 years ago
}
return (Profile: null, PlayMethod: null, AudioStreamIndex: null, TranscodeReasons: failureReasons);
}
private TranscodeReason CheckVideoAudioStreamDirectPlay(VideoOptions options, MediaSourceInfo mediaSource, string container, MediaStream audioStream)
{
var profile = options.Profile;
var audioFailureConditions = GetProfileConditionsForVideoAudio(profile.CodecProfiles, container, audioStream.Codec, audioStream.Channels, audioStream.BitRate, audioStream.SampleRate, audioStream.BitDepth, audioStream.Profile, mediaSource.IsSecondaryAudio(audioStream));
var audioStreamFailureReasons = AggregateFailureConditions(mediaSource, profile, "VideoAudioCodecProfile", audioFailureConditions);
if (audioStream?.IsExternal == true)
6 years ago
{
audioStreamFailureReasons |= TranscodeReason.AudioIsExternal;
6 years ago
}
return audioStreamFailureReasons;
}
private TranscodeReason AggregateFailureConditions(MediaSourceInfo mediaSource, DeviceProfile profile, string type, IEnumerable<ProfileCondition> conditions)
{
return conditions.Aggregate<ProfileCondition, TranscodeReason>(0, (reasons, i) =>
{
LogConditionFailure(profile, type, i, mediaSource);
var transcodeReasons = GetTranscodeReasonForFailedCondition(i);
return reasons | transcodeReasons;
});
6 years ago
}
private void LogConditionFailure(DeviceProfile profile, string type, ProfileCondition condition, MediaSourceInfo mediaSource)
{
_logger.LogDebug(
"Profile: {0}, DirectPlay=false. Reason={1}.{2} Condition: {3}. ConditionValue: {4}. IsRequired: {5}. Path: {6}",
6 years ago
type,
profile.Name ?? "Unknown Profile",
condition.Property,
condition.Condition,
condition.Value ?? string.Empty,
condition.IsRequired,
mediaSource.Path ?? "Unknown path");
}
private TranscodeReason IsBitrateEligibleForDirectPlayback(
MediaSourceInfo item,
6 years ago
long maxBitrate,
VideoOptions options,
PlayMethod playMethod)
{
bool result = IsItemBitrateEligibleForDirectPlayback(item, maxBitrate, playMethod);
if (!result)
{
return TranscodeReason.ContainerBitrateExceedsLimit;
}
else
{
return 0;
}
6 years ago
}
public static SubtitleProfile GetSubtitleProfile(
MediaSourceInfo mediaSource,
MediaStream subtitleStream,
SubtitleProfile[] subtitleProfiles,
PlayMethod playMethod,
ITranscoderSupport transcoderSupport,
string outputContainer,
string transcodingSubProtocol)
6 years ago
{
if (!subtitleStream.IsExternal && (playMethod != PlayMethod.Transcode || !string.Equals(transcodingSubProtocol, "hls", StringComparison.OrdinalIgnoreCase)))
{
// Look for supported embedded subs of the same format
foreach (var profile in subtitleProfiles)
6 years ago
{
if (!profile.SupportsLanguage(subtitleStream.Language))
{
continue;
}
if (profile.Method != SubtitleDeliveryMethod.Embed)
{
continue;
}
if (!ContainerProfile.ContainsContainer(profile.Container, outputContainer))
{
continue;
}
if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(outputContainer))
6 years ago
{
continue;
}
if (subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format) && string.Equals(profile.Format, subtitleStream.Codec, StringComparison.OrdinalIgnoreCase))
6 years ago
{
return profile;
}
}
// Look for supported embedded subs of a convertible format
foreach (var profile in subtitleProfiles)
6 years ago
{
if (!profile.SupportsLanguage(subtitleStream.Language))
{
continue;
}
if (profile.Method != SubtitleDeliveryMethod.Embed)
{
continue;
}
if (!ContainerProfile.ContainsContainer(profile.Container, outputContainer))
{
continue;
}
if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(outputContainer))
6 years ago
{
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
};
}
private static bool IsSubtitleEmbedSupported(string transcodingContainer)
6 years ago
{
if (!string.IsNullOrEmpty(transcodingContainer))
{
6 years ago
string[] normalizedContainers = ContainerProfile.SplitValue(transcodingContainer);
6 years ago
6 years ago
if (ContainerProfile.ContainsContainer(normalizedContainers, "ts")
|| ContainerProfile.ContainsContainer(normalizedContainers, "mpegts")
|| ContainerProfile.ContainsContainer(normalizedContainers, "mp4"))
6 years ago
{
return false;
}
6 years ago
else if (ContainerProfile.ContainsContainer(normalizedContainers, "mkv")
|| ContainerProfile.ContainsContainer(normalizedContainers, "matroska"))
6 years ago
{
return true;
}
}
6 years ago
return false;
}
private static SubtitleProfile GetExternalSubtitleProfile(MediaSourceInfo mediaSource, MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod, ITranscoderSupport transcoderSupport, bool allowConversion)
{
foreach (var profile in subtitleProfiles)
6 years ago
{
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))
{
bool requiresConversion = !string.Equals(subtitleStream.Codec, profile.Format, StringComparison.OrdinalIgnoreCase);
6 years ago
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 IsItemBitrateEligibleForDirectPlayback(MediaSourceInfo item, long maxBitrate, PlayMethod playMethod)
6 years ago
{
// Don't restrict by bitrate if coming from an external domain
if (item.IsRemote)
{
return true;
}
long requestedMaxBitrate = maxBitrate > 0 ? maxBitrate : 1000000;
6 years ago
// If we don't know the bitrate, then force a transcode if requested max bitrate is under 40 mbps
int itemBitrate = item.Bitrate ?? 40000000;
6 years ago
if (itemBitrate > requestedMaxBitrate)
{
_logger.LogDebug(
"Bitrate exceeds {PlayBackMethod} limit: media bitrate: {MediaBitrate}, max bitrate: {MaxBitrate}",
playMethod,
itemBitrate,
requestedMaxBitrate);
6 years ago
return false;
}
return true;
}
private static void ValidateInput(VideoOptions options)
6 years ago
{
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");
}
}
private static void ValidateAudioInput(AudioOptions options)
6 years ago
{
if (options.ItemId.Equals(default))
6 years ago
{
throw new ArgumentException("ItemId is required");
}
6 years ago
if (string.IsNullOrEmpty(options.DeviceId))
{
throw new ArgumentException("DeviceId is required");
}
6 years ago
if (options.Profile == null)
{
throw new ArgumentException("Profile is required");
}
6 years ago
if (options.MediaSources == null)
{
throw new ArgumentException("MediaSources is required");
}
}
private static IEnumerable<ProfileCondition> GetProfileConditionsForVideoAudio(
IEnumerable<CodecProfile> codecProfiles,
string container,
string codec,
int? audioChannels,
int? audioBitrate,
int? audioSampleRate,
int? audioBitDepth,
string audioProfile,
bool? isSecondaryAudio)
{
return codecProfiles
.Where(profile => profile.Type == CodecType.VideoAudio &&
profile.ContainsAnyCodec(codec, container) &&
profile.ApplyConditions.All(applyCondition => ConditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, audioBitrate, audioSampleRate, audioBitDepth, audioProfile, isSecondaryAudio)))
.SelectMany(profile => profile.Conditions)
.Where(condition => !ConditionProcessor.IsVideoAudioConditionSatisfied(condition, audioChannels, audioBitrate, audioSampleRate, audioBitDepth, audioProfile, isSecondaryAudio));
}
private static IEnumerable<ProfileCondition> GetProfileConditionsForAudio(
IEnumerable<CodecProfile> codecProfiles,
string container,
string codec,
int? audioChannels,
int? audioBitrate,
int? audioSampleRate,
int? audioBitDepth,
bool checkConditions)
{
var conditions = codecProfiles
.Where(profile => profile.Type == CodecType.Audio &&
profile.ContainsAnyCodec(codec, container) &&
profile.ApplyConditions.All(applyCondition => ConditionProcessor.IsAudioConditionSatisfied(applyCondition, audioChannels, audioBitrate, audioSampleRate, audioBitDepth)))
.SelectMany(profile => profile.Conditions);
if (!checkConditions)
{
return conditions;
}
return conditions.Where(condition => !ConditionProcessor.IsAudioConditionSatisfied(condition, audioChannels, audioBitrate, audioSampleRate, audioBitDepth));
}
6 years ago
private void ApplyTranscodingConditions(StreamInfo item, IEnumerable<ProfileCondition> conditions, string qualifier, bool enableQualifiedConditions, bool enableNonQualifiedConditions)
{
foreach (ProfileCondition condition in conditions)
6 years ago
{
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))
6 years ago
{
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);
}
}
6 years ago
break;
}
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;
}
6 years ago
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))
6 years ago
{
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));
}
}
6 years ago
break;
}
6 years ago
case ProfileConditionValue.IsAvc:
{
if (!enableNonQualifiedConditions)
{
continue;
}
if (bool.TryParse(value, out var isAvc))
6 years ago
{
if (isAvc && condition.Condition == ProfileConditionType.Equals)
{
item.RequireAvc = true;
}
else if (!isAvc && condition.Condition == ProfileConditionType.NotEquals)
{
item.RequireAvc = true;
}
}
6 years ago
break;
}
6 years ago
case ProfileConditionValue.IsAnamorphic:
{
if (!enableNonQualifiedConditions)
{
continue;
}
if (bool.TryParse(value, out var isAnamorphic))
6 years ago
{
if (isAnamorphic && condition.Condition == ProfileConditionType.Equals)
{
item.RequireNonAnamorphic = true;
}
else if (!isAnamorphic && condition.Condition == ProfileConditionType.NotEquals)
{
item.RequireNonAnamorphic = true;
}
}
6 years ago
break;
}
6 years ago
case ProfileConditionValue.IsInterlaced:
{
if (string.IsNullOrEmpty(qualifier))
{
if (!enableNonQualifiedConditions)
{
continue;
}
}
else
{
if (!enableQualifiedConditions)
{
continue;
}
}
if (bool.TryParse(value, out var isInterlaced))
6 years ago
{
if (!isInterlaced && condition.Condition == ProfileConditionType.Equals)
{
item.SetOption(qualifier, "deinterlace", "true");
}
else if (isInterlaced && condition.Condition == ProfileConditionType.NotEquals)
{
item.SetOption(qualifier, "deinterlace", "true");
}
}
6 years ago
break;
}
6 years ago
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;
}
6 years ago
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))
6 years ago
{
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));
}
}
6 years ago
break;
}
6 years ago
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))
6 years ago
{
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));
}
}
6 years ago
break;
}
6 years ago
case ProfileConditionValue.VideoProfile:
{
if (string.IsNullOrEmpty(qualifier))
{
continue;
}
// change from split by | to comma
// strip spaces to avoid having to encode
var values = value
.Split('|', StringSplitOptions.RemoveEmptyEntries);
6 years ago
if (condition.Condition == ProfileConditionType.Equals)
{
item.SetOption(qualifier, "profile", string.Join(',', values));
6 years ago
}
else if (condition.Condition == ProfileConditionType.EqualsAny)
{
var currentValue = item.GetOption(qualifier, "profile");
if (!string.IsNullOrEmpty(currentValue) && values.Any(value => value == currentValue))
{
item.SetOption(qualifier, "profile", currentValue);
}
else
{
item.SetOption(qualifier, "profile", string.Join(',', values));
}
}
6 years ago
break;
}
case ProfileConditionValue.VideoRangeType:
{
if (string.IsNullOrEmpty(qualifier))
{
continue;
}
// change from split by | to comma
// strip spaces to avoid having to encode
var values = value
.Split('|', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (condition.Condition == ProfileConditionType.Equals)
{
item.SetOption(qualifier, "rangetype", string.Join(',', values));
}
else if (condition.Condition == ProfileConditionType.EqualsAny)
{
var currentValue = item.GetOption(qualifier, "rangetype");
if (!string.IsNullOrEmpty(currentValue) && values.Any(v => string.Equals(v, currentValue, StringComparison.OrdinalIgnoreCase)))
{
item.SetOption(qualifier, "rangetype", currentValue);
}
else
{
item.SetOption(qualifier, "rangetype", string.Join(',', values));
}
}
break;
}
6 years ago
case ProfileConditionValue.Height:
{
if (!enableNonQualifiedConditions)
{
continue;
}
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var num))
6 years ago
{
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);
}
}
6 years ago
break;
}
6 years ago
case ProfileConditionValue.VideoBitrate:
{
if (!enableNonQualifiedConditions)
{
continue;
}
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var num))
6 years ago
{
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);
}
}
6 years ago
break;
}
6 years ago
case ProfileConditionValue.VideoFramerate:
{
if (!enableNonQualifiedConditions)
{
continue;
}
if (float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var num))
6 years ago
{
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);
}
}
6 years ago
break;
}
6 years ago
case ProfileConditionValue.VideoLevel:
{
if (string.IsNullOrEmpty(qualifier))
{
continue;
}
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var num))
6 years ago
{
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));
}
}
6 years ago
break;
}
6 years ago
case ProfileConditionValue.Width:
{
if (!enableNonQualifiedConditions)
{
continue;
}
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var num))
6 years ago
{
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);
}
}
6 years ago
break;
}
6 years ago
default:
break;
}
}
}
private static bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audioStream)
6 years ago
{
// Check container type
if (!profile.SupportsContainer(item.Container))
{
return false;
}
// Check audio codec
string audioCodec = audioStream?.Codec;
6 years ago
if (!profile.SupportsAudioCodec(audioCodec))
{
return false;
}
return true;
}
}
}