jellyfin-server/MediaBrowser.Model/Dlna/StreamBuilder.cs

1791 lines
78 KiB
C#
Raw Normal View History

2014-04-02 21:55:19 +00:00
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Extensions;
2015-04-04 19:35:29 +00:00
using MediaBrowser.Model.Logging;
2014-04-30 15:07:02 +00:00
using MediaBrowser.Model.MediaInfo;
using MediaBrowser.Model.Session;
2014-04-02 21:55:19 +00:00
using System;
using System.Collections.Generic;
2016-10-22 02:08:34 +00:00
using System.Globalization;
2016-12-13 17:04:37 +00:00
using System.Linq;
2014-04-01 22:23:07 +00:00
namespace MediaBrowser.Model.Dlna
{
public class StreamBuilder
{
2015-04-04 19:35:29 +00:00
private readonly ILogger _logger;
private readonly ITranscoderSupport _transcoderSupport;
public StreamBuilder(ITranscoderSupport transcoderSupport, ILogger logger)
{
_transcoderSupport = transcoderSupport;
2015-04-04 19:35:29 +00:00
_logger = logger;
}
2015-04-04 19:35:29 +00:00
public StreamBuilder(ILogger logger)
: this(new FullTranscoderSupport(), logger)
{
}
2014-04-01 22:23:07 +00:00
public StreamInfo BuildAudioItem(AudioOptions options)
{
ValidateAudioInput(options);
2014-04-03 22:50:04 +00:00
2017-08-19 19:43:35 +00:00
var mediaSources = new List<MediaSourceInfo>();
2015-03-16 01:48:25 +00:00
foreach (MediaSourceInfo i in options.MediaSources)
2014-04-01 22:23:07 +00:00
{
2015-03-16 04:39:55 +00:00
if (string.IsNullOrEmpty(options.MediaSourceId) ||
2015-03-16 01:48:25 +00:00
StringHelper.EqualsIgnoreCase(i.Id, options.MediaSourceId))
2014-05-08 21:23:24 +00:00
{
2015-03-16 01:48:25 +00:00
mediaSources.Add(i);
2014-05-08 21:23:24 +00:00
}
2014-04-01 22:23:07 +00:00
}
2017-08-19 19:43:35 +00:00
var streams = new List<StreamInfo>();
2014-05-08 21:23:24 +00:00
foreach (MediaSourceInfo i in mediaSources)
2015-03-02 18:48:21 +00:00
{
StreamInfo streamInfo = BuildAudioItem(i, options);
if (streamInfo != null)
{
streams.Add(streamInfo);
}
}
2014-04-01 22:23:07 +00:00
2014-05-08 20:09:53 +00:00
foreach (StreamInfo stream in streams)
2014-04-01 22:23:07 +00:00
{
stream.DeviceId = options.DeviceId;
stream.DeviceProfileId = options.Profile.Id;
}
2016-08-23 05:08:07 +00:00
return GetOptimalStream(streams, options.GetMaxBitrate(true));
2014-04-01 22:23:07 +00:00
}
public StreamInfo BuildVideoItem(VideoOptions options)
{
ValidateInput(options);
2017-08-19 19:43:35 +00:00
var mediaSources = new List<MediaSourceInfo>();
2015-03-16 01:48:25 +00:00
foreach (MediaSourceInfo i in options.MediaSources)
2014-04-01 22:23:07 +00:00
{
2015-03-16 04:39:55 +00:00
if (string.IsNullOrEmpty(options.MediaSourceId) ||
2015-03-16 01:48:25 +00:00
StringHelper.EqualsIgnoreCase(i.Id, options.MediaSourceId))
2014-05-08 21:23:24 +00:00
{
2015-03-16 01:48:25 +00:00
mediaSources.Add(i);
2014-05-08 21:23:24 +00:00
}
2014-04-01 22:23:07 +00:00
}
2017-08-19 19:43:35 +00:00
var streams = new List<StreamInfo>();
2014-05-08 21:23:24 +00:00
foreach (MediaSourceInfo i in mediaSources)
2015-03-02 18:48:21 +00:00
{
StreamInfo streamInfo = BuildVideoItem(i, options);
if (streamInfo != null)
{
streams.Add(streamInfo);
2015-03-02 18:48:21 +00:00
}
}
2014-04-01 22:23:07 +00:00
2014-05-08 20:09:53 +00:00
foreach (StreamInfo stream in streams)
2014-04-01 22:23:07 +00:00
{
stream.DeviceId = options.DeviceId;
stream.DeviceProfileId = options.Profile.Id;
}
2016-08-23 05:08:07 +00:00
return GetOptimalStream(streams, options.GetMaxBitrate(false));
2014-04-01 22:23:07 +00:00
}
2017-01-03 05:15:59 +00:00
private StreamInfo GetOptimalStream(List<StreamInfo> streams, long? maxBitrate)
2014-04-01 22:23:07 +00:00
{
2017-08-19 19:43:35 +00:00
var sorted = StreamInfoSorter.SortMediaSources(streams, maxBitrate);
2014-05-08 21:23:24 +00:00
2017-08-19 19:43:35 +00:00
foreach (StreamInfo stream in sorted)
2014-05-08 21:23:24 +00:00
{
return stream;
}
2015-03-02 18:48:21 +00:00
return null;
2014-04-01 22:23:07 +00:00
}
2017-06-24 18:33:19 +00:00
private TranscodeReason? GetTranscodeReasonForFailedCondition(ProfileCondition condition)
{
switch (condition.Property)
{
case ProfileConditionValue.AudioBitrate:
if (condition.Condition == ProfileConditionType.LessThanEqual)
{
return TranscodeReason.AudioBitrateNotSupported;
}
return TranscodeReason.AudioBitrateNotSupported;
case ProfileConditionValue.AudioChannels:
if (condition.Condition == ProfileConditionType.LessThanEqual)
{
return TranscodeReason.AudioChannelsNotSupported;
}
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;
2017-06-26 15:10:52 +00:00
case ProfileConditionValue.AudioBitDepth:
return TranscodeReason.AudioBitDepthNotSupported;
2017-06-24 18:33:19 +00:00
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;
}
}
2017-08-04 20:29:34 +00:00
public static string NormalizeMediaSourceFormatIntoSingleContainer(string inputContainer, DeviceProfile profile, DlnaProfileType type)
{
if (string.IsNullOrWhiteSpace(inputContainer))
{
return null;
}
var formats = ContainerProfile.SplitValue(inputContainer);
2017-08-19 19:43:35 +00:00
if (formats.Length == 1)
2017-08-04 20:29:34 +00:00
{
return formats[0];
}
if (profile != null)
{
foreach (var format in formats)
{
foreach (var directPlayProfile in profile.DirectPlayProfiles)
{
if (directPlayProfile.Type == type)
{
if (directPlayProfile.SupportsContainer(format))
{
return format;
}
}
}
}
}
return formats[0];
}
2014-04-06 17:53:23 +00:00
private StreamInfo BuildAudioItem(MediaSourceInfo item, AudioOptions options)
2014-04-01 22:23:07 +00:00
{
2017-08-19 19:43:35 +00:00
var transcodeReasons = new List<TranscodeReason>();
2017-06-24 18:33:19 +00:00
2014-05-08 20:09:53 +00:00
StreamInfo playlistItem = new StreamInfo
2014-04-01 22:23:07 +00:00
{
2014-04-06 17:53:23 +00:00
ItemId = options.ItemId,
2014-04-01 22:23:07 +00:00
MediaType = DlnaProfileType.Audio,
2014-04-18 17:16:25 +00:00
MediaSource = item,
2015-01-02 05:36:27 +00:00
RunTimeTicks = item.RunTimeTicks,
2015-02-05 05:29:37 +00:00
Context = options.Context,
DeviceProfile = options.Profile
2014-04-01 22:23:07 +00:00
};
if (options.ForceDirectPlay)
{
playlistItem.PlayMethod = PlayMethod.DirectPlay;
2017-08-04 20:29:34 +00:00
playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Audio);
return playlistItem;
}
if (options.ForceDirectStream)
{
playlistItem.PlayMethod = PlayMethod.DirectStream;
2017-08-04 20:29:34 +00:00
playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Audio);
return playlistItem;
}
2015-03-23 17:19:21 +00:00
MediaStream audioStream = item.GetDefaultAudioStream(null);
2017-06-24 18:33:19 +00:00
var directPlayInfo = GetAudioDirectPlayMethods(item, audioStream, options);
2017-08-19 19:43:35 +00:00
var directPlayMethods = directPlayInfo.Item1;
2017-06-24 18:33:19 +00:00
transcodeReasons.AddRange(directPlayInfo.Item2);
2016-07-09 17:39:04 +00:00
ConditionProcessor conditionProcessor = new ConditionProcessor();
int? inputAudioChannels = audioStream == null ? null : audioStream.Channels;
int? inputAudioBitrate = audioStream == null ? null : audioStream.BitDepth;
2017-05-13 19:31:25 +00:00
int? inputAudioSampleRate = audioStream == null ? null : audioStream.SampleRate;
2017-06-26 15:10:52 +00:00
int? inputAudioBitDepth = audioStream == null ? null : audioStream.BitDepth;
2016-07-09 17:39:04 +00:00
2015-03-13 01:55:22 +00:00
if (directPlayMethods.Count > 0)
2014-04-01 22:23:07 +00:00
{
2015-03-13 01:55:22 +00:00
string audioCodec = audioStream == null ? null : audioStream.Codec;
2014-04-01 22:23:07 +00:00
2015-03-13 01:55:22 +00:00
// Make sure audio codec profiles are satisfied
if (!string.IsNullOrEmpty(audioCodec))
2014-04-01 22:23:07 +00:00
{
2017-08-19 19:43:35 +00:00
var conditions = new List<ProfileCondition>();
2015-03-13 01:55:22 +00:00
foreach (CodecProfile i in options.Profile.CodecProfiles)
2014-04-06 17:53:23 +00:00
{
2017-09-25 05:06:15 +00:00
if (i.Type == CodecType.Audio && i.ContainsAnyCodec(audioCodec, item.Container))
2014-05-09 19:43:06 +00:00
{
2016-07-09 17:39:04 +00:00
bool applyConditions = true;
foreach (ProfileCondition applyCondition in i.ApplyConditions)
2014-07-01 04:06:28 +00:00
{
2017-06-26 15:10:52 +00:00
if (!conditionProcessor.IsAudioConditionSatisfied(applyCondition, inputAudioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth))
2016-07-09 17:39:04 +00:00
{
LogConditionFailure(options.Profile, "AudioCodecProfile", applyCondition, item);
applyConditions = false;
break;
}
}
if (applyConditions)
{
foreach (ProfileCondition c in i.Conditions)
{
conditions.Add(c);
}
2014-07-01 04:06:28 +00:00
}
2014-05-09 19:43:06 +00:00
}
2015-03-13 01:55:22 +00:00
}
2014-04-24 05:08:10 +00:00
2015-03-13 01:55:22 +00:00
bool all = true;
foreach (ProfileCondition c in conditions)
{
2017-06-26 15:10:52 +00:00
if (!conditionProcessor.IsAudioConditionSatisfied(c, inputAudioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth))
2014-05-09 19:43:06 +00:00
{
2015-07-10 18:30:42 +00:00
LogConditionFailure(options.Profile, "AudioCodecProfile", c, item);
2017-06-24 18:33:19 +00:00
var transcodeReason = GetTranscodeReasonForFailedCondition(c);
if (transcodeReason.HasValue)
{
transcodeReasons.Add(transcodeReason.Value);
}
2015-03-13 01:55:22 +00:00
all = false;
break;
2014-05-09 19:43:06 +00:00
}
2015-03-13 01:55:22 +00:00
}
2014-05-09 19:43:06 +00:00
2015-03-13 01:55:22 +00:00
if (all)
{
if (directPlayMethods.Contains(PlayMethod.DirectStream))
2015-03-13 01:55:22 +00:00
{
playlistItem.PlayMethod = PlayMethod.DirectStream;
}
2017-08-04 20:29:34 +00:00
playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Audio);
2014-04-24 05:08:10 +00:00
2015-03-13 01:55:22 +00:00
return playlistItem;
2014-04-06 17:53:23 +00:00
}
2014-04-01 22:23:07 +00:00
}
}
2014-05-09 19:43:06 +00:00
TranscodingProfile transcodingProfile = null;
foreach (TranscodingProfile i in options.Profile.TranscodingProfiles)
{
2014-07-17 03:17:14 +00:00
if (i.Type == playlistItem.MediaType && i.Context == options.Context)
2014-05-09 19:43:06 +00:00
{
if (_transcoderSupport.CanEncodeToAudioCodec(i.AudioCodec ?? i.Container))
{
transcodingProfile = i;
break;
}
2014-05-09 19:43:06 +00:00
}
}
2014-04-01 22:23:07 +00:00
if (transcodingProfile != null)
{
2015-03-02 18:48:21 +00:00
if (!item.SupportsTranscoding)
{
return null;
}
playlistItem.PlayMethod = PlayMethod.Transcode;
2014-04-18 05:03:01 +00:00
playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
2014-04-18 17:16:25 +00:00
playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
2014-04-01 22:23:07 +00:00
playlistItem.Container = transcodingProfile.Container;
2016-06-26 16:21:10 +00:00
if (string.IsNullOrEmpty(transcodingProfile.AudioCodec))
{
playlistItem.AudioCodecs = new string[] { };
}
else
{
playlistItem.AudioCodecs = transcodingProfile.AudioCodec.Split(',');
}
2017-03-15 19:57:18 +00:00
playlistItem.SubProtocol = transcodingProfile.Protocol;
2014-04-01 22:23:07 +00:00
2017-08-19 19:43:35 +00:00
var audioCodecProfiles = new List<CodecProfile>();
2014-05-09 23:08:08 +00:00
foreach (CodecProfile i in options.Profile.CodecProfiles)
{
2017-09-25 05:06:15 +00:00
if (i.Type == CodecType.Audio && i.ContainsAnyCodec(transcodingProfile.AudioCodec, transcodingProfile.Container))
2014-05-09 23:08:08 +00:00
{
audioCodecProfiles.Add(i);
}
if (audioCodecProfiles.Count >= 1) break;
}
2017-08-19 19:43:35 +00:00
var audioTranscodingConditions = new List<ProfileCondition>();
2014-05-09 23:08:08 +00:00
foreach (CodecProfile i in audioCodecProfiles)
2014-07-01 04:06:28 +00:00
{
2016-07-09 17:39:04 +00:00
bool applyConditions = true;
foreach (ProfileCondition applyCondition in i.ApplyConditions)
2014-07-01 04:06:28 +00:00
{
2017-06-26 15:10:52 +00:00
if (!conditionProcessor.IsAudioConditionSatisfied(applyCondition, inputAudioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth))
2016-07-09 17:39:04 +00:00
{
LogConditionFailure(options.Profile, "AudioCodecProfile", applyCondition, item);
applyConditions = false;
break;
}
}
if (applyConditions)
{
foreach (ProfileCondition c in i.Conditions)
{
audioTranscodingConditions.Add(c);
}
2014-07-01 04:06:28 +00:00
}
}
2014-04-01 22:23:07 +00:00
ApplyTranscodingConditions(playlistItem, audioTranscodingConditions, null, false);
2014-04-06 17:53:23 +00:00
// Honor requested max channels
if (options.MaxAudioChannels.HasValue)
{
2014-05-08 20:09:53 +00:00
int currentValue = playlistItem.MaxAudioChannels ?? options.MaxAudioChannels.Value;
2014-04-06 17:53:23 +00:00
playlistItem.MaxAudioChannels = Math.Min(options.MaxAudioChannels.Value, currentValue);
}
2017-01-03 05:15:59 +00:00
long transcodingBitrate = options.AudioTranscodingBitrate ??
2016-08-23 05:08:07 +00:00
options.Profile.MusicStreamingTranscodingBitrate ??
2014-08-14 13:24:30 +00:00
128000;
2017-01-03 05:15:59 +00:00
var configuredBitrate = options.GetMaxBitrate(true);
2016-08-23 05:08:07 +00:00
if (configuredBitrate.HasValue)
{
transcodingBitrate = Math.Min(configuredBitrate.Value, transcodingBitrate);
}
2017-01-03 05:15:59 +00:00
var longBitrate = Math.Min(transcodingBitrate, playlistItem.AudioBitrate ?? transcodingBitrate);
playlistItem.AudioBitrate = longBitrate > int.MaxValue ? int.MaxValue : Convert.ToInt32(longBitrate);
2014-04-01 22:23:07 +00:00
}
2017-06-24 18:33:19 +00:00
playlistItem.TranscodeReasons = transcodeReasons;
2014-04-01 22:23:07 +00:00
return playlistItem;
}
2017-01-03 05:15:59 +00:00
private long? GetBitrateForDirectPlayCheck(MediaSourceInfo item, AudioOptions options, bool isAudio)
2015-03-28 20:22:27 +00:00
{
if (item.Protocol == MediaProtocol.File)
{
return options.Profile.MaxStaticBitrate;
}
2016-08-23 05:08:07 +00:00
return options.GetMaxBitrate(isAudio);
2015-03-28 20:22:27 +00:00
}
2017-06-24 18:33:19 +00:00
private Tuple<List<PlayMethod>, List<TranscodeReason>> GetAudioDirectPlayMethods(MediaSourceInfo item, MediaStream audioStream, AudioOptions options)
2015-03-13 01:55:22 +00:00
{
2017-08-19 19:43:35 +00:00
var transcodeReasons = new List<TranscodeReason>();
2017-06-24 18:33:19 +00:00
2015-03-13 01:55:22 +00:00
DirectPlayProfile directPlayProfile = null;
foreach (DirectPlayProfile i in options.Profile.DirectPlayProfiles)
{
if (i.Type == DlnaProfileType.Audio && IsAudioDirectPlaySupported(i, item, audioStream))
{
directPlayProfile = i;
break;
}
}
2017-08-19 19:43:35 +00:00
var playMethods = new List<PlayMethod>();
2015-03-13 01:55:22 +00:00
if (directPlayProfile != null)
{
// While options takes the network and other factors into account. Only applies to direct stream
2017-06-24 18:33:19 +00:00
if (item.SupportsDirectStream)
2015-03-13 01:55:22 +00:00
{
2017-06-24 18:33:19 +00:00
if (IsAudioEligibleForDirectPlay(item, options.GetMaxBitrate(true), PlayMethod.DirectStream))
{
if (options.EnableDirectStream)
{
playMethods.Add(PlayMethod.DirectStream);
}
}
else
{
transcodeReasons.Add(TranscodeReason.ContainerBitrateExceedsLimit);
}
2015-03-13 01:55:22 +00:00
}
2015-07-10 18:30:42 +00:00
2015-03-13 01:55:22 +00:00
// The profile describes what the device supports
// If device requirements are satisfied then allow both direct stream and direct play
2017-06-24 18:33:19 +00:00
if (item.SupportsDirectPlay)
2015-03-13 01:55:22 +00:00
{
2017-06-24 18:33:19 +00:00
if (IsAudioEligibleForDirectPlay(item, GetBitrateForDirectPlayCheck(item, options, true), PlayMethod.DirectPlay))
{
if (options.EnableDirectPlay)
{
playMethods.Add(PlayMethod.DirectPlay);
}
}
else
{
transcodeReasons.Add(TranscodeReason.ContainerBitrateExceedsLimit);
}
2015-03-13 01:55:22 +00:00
}
}
2015-07-10 18:30:42 +00:00
else
{
2017-06-24 18:33:19 +00:00
transcodeReasons.InsertRange(0, GetTranscodeReasonsFromDirectPlayProfile(item, null, audioStream, options.Profile.DirectPlayProfiles));
2015-10-05 03:24:24 +00:00
_logger.Info("Profile: {0}, No direct play profiles found for Path: {1}",
2015-07-10 18:30:42 +00:00
options.Profile.Name ?? "Unknown Profile",
2015-09-20 17:28:32 +00:00
item.Path ?? "Unknown path");
2015-07-10 18:30:42 +00:00
}
2015-03-13 01:55:22 +00:00
2017-06-24 18:33:19 +00:00
if (playMethods.Count > 0)
{
transcodeReasons.Clear();
}
else
{
transcodeReasons = transcodeReasons.Distinct().ToList();
}
return new Tuple<List<PlayMethod>, List<TranscodeReason>>(playMethods, transcodeReasons);
}
private List<TranscodeReason> GetTranscodeReasonsFromDirectPlayProfile(MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream, IEnumerable<DirectPlayProfile> directPlayProfiles)
{
var list = new List<TranscodeReason>();
var containerSupported = false;
var audioSupported = false;
var videoSupported = false;
foreach (var profile in directPlayProfiles)
{
// Check container type
if (profile.SupportsContainer(item.Container))
2017-06-24 18:33:19 +00:00
{
containerSupported = true;
if (videoStream != null)
2017-06-24 18:33:19 +00:00
{
// Check video codec
2017-08-19 19:43:35 +00:00
var videoCodecs = profile.GetVideoCodecs();
if (videoCodecs.Length > 0)
2017-06-24 18:33:19 +00:00
{
string videoCodec = videoStream.Codec;
if (!string.IsNullOrEmpty(videoCodec) && ListHelper.ContainsIgnoreCase(videoCodecs, videoCodec))
2017-06-24 18:33:19 +00:00
{
videoSupported = true;
2017-06-24 18:33:19 +00:00
}
}
else
{
videoSupported = true;
}
}
2017-06-24 18:33:19 +00:00
if (audioStream != null)
{
// Check audio codec
2017-08-19 19:43:35 +00:00
var audioCodecs = profile.GetAudioCodecs();
if (audioCodecs.Length > 0)
{
string audioCodec = audioStream.Codec;
if (!string.IsNullOrEmpty(audioCodec) && ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
2017-06-24 18:33:19 +00:00
{
audioSupported = true;
2017-06-24 18:33:19 +00:00
}
}
else
{
audioSupported = true;
}
2017-06-24 18:33:19 +00:00
}
}
}
if (!containerSupported)
{
list.Add(TranscodeReason.ContainerNotSupported);
}
if (videoStream != null && !videoSupported)
{
list.Add(TranscodeReason.VideoCodecNotSupported);
}
if (audioStream != null && !audioSupported)
{
2017-06-25 17:50:17 +00:00
list.Add(TranscodeReason.AudioCodecNotSupported);
2017-06-24 18:33:19 +00:00
}
return list;
2015-03-13 01:55:22 +00:00
}
2015-03-31 16:24:16 +00:00
private int? GetDefaultSubtitleStreamIndex(MediaSourceInfo item, SubtitleProfile[] subtitleProfiles)
{
int highestScore = -1;
foreach (MediaStream stream in item.MediaStreams)
{
if (stream.Type == MediaStreamType.Subtitle && stream.Score.HasValue)
{
if (stream.Score.Value > highestScore)
{
highestScore = stream.Score.Value;
}
2015-09-20 17:28:32 +00:00
}
2015-03-31 16:24:16 +00:00
}
2017-08-19 19:43:35 +00:00
var topStreams = new List<MediaStream>();
2015-03-31 16:24:16 +00:00
foreach (MediaStream stream in item.MediaStreams)
{
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 (MediaStream stream in topStreams)
{
foreach (SubtitleProfile profile in subtitleProfiles)
{
if (profile.Method == SubtitleDeliveryMethod.External && StringHelper.EqualsIgnoreCase(profile.Format, stream.Codec))
{
return stream.Index;
}
}
}
}
// If no optimization panned out, just use the original default
return item.DefaultSubtitleStreamIndex;
}
2014-04-01 22:23:07 +00:00
private StreamInfo BuildVideoItem(MediaSourceInfo item, VideoOptions options)
{
2017-07-01 18:00:43 +00:00
if (item == null)
{
throw new ArgumentNullException("item");
}
2017-08-19 19:43:35 +00:00
var transcodeReasons = new List<TranscodeReason>();
2017-06-24 18:33:19 +00:00
2014-05-08 20:09:53 +00:00
StreamInfo playlistItem = new StreamInfo
2014-04-01 22:23:07 +00:00
{
ItemId = options.ItemId,
MediaType = DlnaProfileType.Video,
2014-04-18 17:16:25 +00:00
MediaSource = item,
2015-01-02 05:36:27 +00:00
RunTimeTicks = item.RunTimeTicks,
2015-02-05 05:29:37 +00:00
Context = options.Context,
DeviceProfile = options.Profile
2014-04-01 22:23:07 +00:00
};
2015-03-31 16:24:16 +00:00
playlistItem.SubtitleStreamIndex = options.SubtitleStreamIndex ?? GetDefaultSubtitleStreamIndex(item, options.Profile.SubtitleProfiles);
MediaStream subtitleStream = playlistItem.SubtitleStreamIndex.HasValue ? item.GetMediaStream(MediaStreamType.Subtitle, playlistItem.SubtitleStreamIndex.Value) : null;
2015-03-23 17:19:21 +00:00
MediaStream audioStream = item.GetDefaultAudioStream(options.AudioStreamIndex ?? item.DefaultAudioStreamIndex);
2015-03-31 19:33:38 +00:00
int? audioStreamIndex = null;
if (audioStream != null)
{
audioStreamIndex = audioStream.Index;
}
2015-03-23 17:19:21 +00:00
2014-05-09 23:08:08 +00:00
MediaStream videoStream = item.VideoStream;
2014-04-01 22:23:07 +00:00
2015-03-23 20:06:06 +00:00
// TODO: This doesn't accout for situation of device being able to handle media bitrate, but wifi connection not fast enough
2017-07-03 17:16:01 +00:00
var directPlayEligibilityResult = IsEligibleForDirectPlay(item, GetBitrateForDirectPlayCheck(item, options, true), subtitleStream, options, PlayMethod.DirectPlay);
var directStreamEligibilityResult = IsEligibleForDirectPlay(item, options.GetMaxBitrate(false), subtitleStream, options, PlayMethod.DirectStream);
bool isEligibleForDirectPlay = options.EnableDirectPlay && (options.ForceDirectPlay || directPlayEligibilityResult.Item1);
bool isEligibleForDirectStream = options.EnableDirectStream && (options.ForceDirectStream || directStreamEligibilityResult.Item1);
2015-10-05 03:24:24 +00:00
_logger.Info("Profile: {0}, Path: {1}, isEligibleForDirectPlay: {2}, isEligibleForDirectStream: {3}",
2015-04-04 19:35:29 +00:00
options.Profile.Name ?? "Unknown Profile",
item.Path ?? "Unknown path",
isEligibleForDirectPlay,
isEligibleForDirectStream);
2015-03-23 20:06:06 +00:00
if (isEligibleForDirectPlay || isEligibleForDirectStream)
2014-04-01 22:23:07 +00:00
{
// See if it can be direct played
2017-06-24 18:33:19 +00:00
var directPlayInfo = GetVideoDirectPlayProfile(options, item, videoStream, audioStream, isEligibleForDirectPlay, isEligibleForDirectStream);
var directPlay = directPlayInfo.Item1;
2014-04-01 22:23:07 +00:00
if (directPlay != null)
{
2014-10-12 01:46:02 +00:00
playlistItem.PlayMethod = directPlay.Value;
2017-08-04 20:29:34 +00:00
playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileType.Video);
2014-04-01 22:23:07 +00:00
if (subtitleStream != null)
{
2017-10-13 18:32:58 +00:00
SubtitleProfile subtitleProfile = GetSubtitleProfile(item, subtitleStream, options.Profile.SubtitleProfiles, directPlay.Value, _transcoderSupport, null, null);
2015-01-15 05:54:42 +00:00
playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
playlistItem.SubtitleFormat = subtitleProfile.Format;
}
2014-04-24 05:08:10 +00:00
return playlistItem;
2014-04-01 22:23:07 +00:00
}
2017-06-24 18:33:19 +00:00
transcodeReasons.AddRange(directPlayInfo.Item2);
2014-04-01 22:23:07 +00:00
}
2017-07-03 17:16:01 +00:00
if (directPlayEligibilityResult.Item2.HasValue)
{
transcodeReasons.Add(directPlayEligibilityResult.Item2.Value);
}
if (directStreamEligibilityResult.Item2.HasValue)
{
transcodeReasons.Add(directStreamEligibilityResult.Item2.Value);
}
2014-04-01 22:23:07 +00:00
// Can't direct play, find the transcoding profile
2014-05-08 21:23:24 +00:00
TranscodingProfile transcodingProfile = null;
foreach (TranscodingProfile i in options.Profile.TranscodingProfiles)
{
2014-07-17 03:17:14 +00:00
if (i.Type == playlistItem.MediaType && i.Context == options.Context)
2014-05-08 21:23:24 +00:00
{
transcodingProfile = i;
break;
}
}
2014-04-01 22:23:07 +00:00
if (transcodingProfile != null)
{
2015-03-02 18:48:21 +00:00
if (!item.SupportsTranscoding)
{
return null;
}
if (subtitleStream != null)
{
2017-10-13 18:32:58 +00:00
SubtitleProfile subtitleProfile = GetSubtitleProfile(item, subtitleStream, options.Profile.SubtitleProfiles, PlayMethod.Transcode, _transcoderSupport, transcodingProfile.Protocol, transcodingProfile.Container);
2015-01-15 05:54:42 +00:00
playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
playlistItem.SubtitleFormat = subtitleProfile.Format;
2017-01-29 20:00:29 +00:00
playlistItem.SubtitleCodecs = new[] { subtitleProfile.Format };
}
playlistItem.PlayMethod = PlayMethod.Transcode;
2014-04-01 22:23:07 +00:00
playlistItem.Container = transcodingProfile.Container;
2014-04-18 17:16:25 +00:00
playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
2014-04-18 05:03:01 +00:00
playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
2016-03-07 04:56:45 +00:00
2016-06-26 16:21:10 +00:00
playlistItem.AudioCodecs = transcodingProfile.AudioCodec.Split(',');
2016-03-07 04:56:45 +00:00
2017-03-15 19:57:18 +00:00
playlistItem.VideoCodecs = transcodingProfile.VideoCodec.Split(',');
playlistItem.CopyTimestamps = transcodingProfile.CopyTimestamps;
2016-07-13 19:16:51 +00:00
playlistItem.EnableSubtitlesInManifest = transcodingProfile.EnableSubtitlesInManifest;
2016-05-14 05:40:01 +00:00
2017-03-31 19:05:19 +00:00
playlistItem.BreakOnNonKeyFrames = transcodingProfile.BreakOnNonKeyFrames;
if (transcodingProfile.MinSegments > 0)
{
playlistItem.MinSegments = transcodingProfile.MinSegments;
}
if (transcodingProfile.SegmentLength > 0)
{
playlistItem.SegmentLength = transcodingProfile.SegmentLength;
}
2016-05-14 05:40:01 +00:00
if (!string.IsNullOrEmpty(transcodingProfile.MaxAudioChannels))
{
int transcodingMaxAudioChannels;
2016-10-22 02:08:34 +00:00
if (int.TryParse(transcodingProfile.MaxAudioChannels, NumberStyles.Any, CultureInfo.InvariantCulture, out transcodingMaxAudioChannels))
2016-05-14 05:40:01 +00:00
{
playlistItem.TranscodingMaxAudioChannels = transcodingMaxAudioChannels;
}
}
playlistItem.SubProtocol = transcodingProfile.Protocol;
playlistItem.AudioStreamIndex = audioStreamIndex;
2016-07-09 17:39:04 +00:00
ConditionProcessor conditionProcessor = new ConditionProcessor();
2014-04-01 22:23:07 +00:00
var isFirstAppliedCodecProfile = true;
2014-05-09 23:08:08 +00:00
foreach (CodecProfile i in options.Profile.CodecProfiles)
{
2017-09-25 05:06:15 +00:00
if (i.Type == CodecType.Video && i.ContainsAnyCodec(transcodingProfile.VideoCodec, transcodingProfile.Container))
2014-05-09 23:08:08 +00:00
{
2016-07-09 17:39:04 +00:00
bool applyConditions = true;
foreach (ProfileCondition applyCondition in i.ApplyConditions)
2014-07-01 04:06:28 +00:00
{
2017-09-25 19:15:01 +00:00
int? width = videoStream == null ? null : videoStream.Width;
int? height = videoStream == null ? null : videoStream.Height;
int? bitDepth = videoStream == null ? null : videoStream.BitDepth;
int? videoBitrate = videoStream == null ? null : videoStream.BitRate;
double? videoLevel = videoStream == null ? null : videoStream.Level;
string videoProfile = videoStream == null ? null : videoStream.Profile;
float? videoFramerate = videoStream == null ? null : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate;
bool? isAnamorphic = videoStream == null ? null : videoStream.IsAnamorphic;
bool? isInterlaced = videoStream == null ? (bool?)null : videoStream.IsInterlaced;
string videoCodecTag = videoStream == null ? null : videoStream.CodecTag;
bool? isAvc = videoStream == null ? null : videoStream.IsAVC;
2016-07-09 17:39:04 +00:00
2017-09-25 19:15:01 +00:00
TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : item.Timestamp;
int? packetLength = videoStream == null ? null : videoStream.PacketLength;
int? refFrames = videoStream == null ? null : videoStream.RefFrames;
int? numAudioStreams = item.GetStreamCount(MediaStreamType.Audio);
int? numVideoStreams = item.GetStreamCount(MediaStreamType.Video);
if (!conditionProcessor.IsVideoConditionSatisfied(applyCondition, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
2016-07-09 17:39:04 +00:00
{
2017-09-25 19:15:01 +00:00
LogConditionFailure(options.Profile, "VideoCodecProfile", applyCondition, item);
2016-07-09 17:39:04 +00:00
applyConditions = false;
break;
}
}
if (applyConditions)
{
var transcodingVideoCodecs = ContainerProfile.SplitValue(transcodingProfile.VideoCodec);
foreach (var transcodingVideoCodec in transcodingVideoCodecs)
2016-07-09 17:39:04 +00:00
{
2017-09-25 05:06:15 +00:00
if (i.ContainsAnyCodec(transcodingVideoCodec, transcodingProfile.Container))
2017-09-22 20:38:59 +00:00
{
ApplyTranscodingConditions(playlistItem, i.Conditions, transcodingVideoCodec, !isFirstAppliedCodecProfile);
isFirstAppliedCodecProfile = false;
}
2016-07-09 17:39:04 +00:00
}
2014-07-01 04:06:28 +00:00
}
2014-05-09 23:08:08 +00:00
}
}
2014-04-01 22:23:07 +00:00
2017-08-19 19:43:35 +00:00
var audioTranscodingConditions = new List<ProfileCondition>();
2014-05-09 23:08:08 +00:00
foreach (CodecProfile i in options.Profile.CodecProfiles)
{
2017-09-25 05:06:15 +00:00
if (i.Type == CodecType.VideoAudio && i.ContainsAnyCodec(playlistItem.TargetAudioCodec, transcodingProfile.Container))
2014-05-09 23:08:08 +00:00
{
2016-07-09 17:39:04 +00:00
bool applyConditions = true;
foreach (ProfileCondition applyCondition in i.ApplyConditions)
2014-07-01 04:06:28 +00:00
{
2017-09-25 19:15:01 +00: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;
2016-07-09 17:39:04 +00:00
2017-09-25 19:15:01 +00:00
if (!conditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth, audioProfile, isSecondaryAudio))
2016-07-09 17:39:04 +00:00
{
LogConditionFailure(options.Profile, "VideoCodecProfile", applyCondition, item);
applyConditions = false;
break;
}
}
if (applyConditions)
{
foreach (ProfileCondition c in i.Conditions)
{
audioTranscodingConditions.Add(c);
}
break;
2014-07-01 04:06:28 +00:00
}
2014-05-09 23:08:08 +00:00
}
}
2014-04-06 17:53:23 +00:00
// Honor requested max channels
if (options.MaxAudioChannels.HasValue)
{
2014-05-08 20:09:53 +00:00
int currentValue = playlistItem.MaxAudioChannels ?? options.MaxAudioChannels.Value;
2014-04-06 17:53:23 +00:00
playlistItem.MaxAudioChannels = Math.Min(options.MaxAudioChannels.Value, currentValue);
}
2016-08-23 05:08:07 +00:00
int audioBitrate = GetAudioBitrate(playlistItem.SubProtocol, options.GetMaxBitrate(false), playlistItem.TargetAudioChannels, playlistItem.TargetAudioCodec, audioStream);
2015-04-20 18:04:02 +00:00
playlistItem.AudioBitrate = Math.Min(playlistItem.AudioBitrate ?? audioBitrate, audioBitrate);
2014-04-06 17:53:23 +00:00
2017-01-03 05:15:59 +00:00
var maxBitrateSetting = options.GetMaxBitrate(false);
2014-04-06 17:53:23 +00:00
// Honor max rate
if (maxBitrateSetting.HasValue)
2014-04-06 17:53:23 +00:00
{
2017-01-03 05:15:59 +00:00
var videoBitrate = maxBitrateSetting.Value;
2014-04-06 17:53:23 +00:00
if (playlistItem.AudioBitrate.HasValue)
{
videoBitrate -= playlistItem.AudioBitrate.Value;
}
2015-04-20 18:04:02 +00:00
// Make sure the video bitrate is lower than bitrate settings but at least 64k
2017-01-03 05:15:59 +00:00
long currentValue = playlistItem.VideoBitrate ?? videoBitrate;
var longBitrate = Math.Max(Math.Min(videoBitrate, currentValue), 64000);
playlistItem.VideoBitrate = longBitrate > int.MaxValue ? int.MaxValue : Convert.ToInt32(longBitrate);
2014-04-06 17:53:23 +00:00
}
2017-09-07 18:17:18 +00:00
// Do this after initial values are set to account for greater than/less than conditions
ApplyTranscodingConditions(playlistItem, audioTranscodingConditions, null, false);
}
2014-06-23 16:05:19 +00:00
2017-06-24 18:33:19 +00:00
playlistItem.TranscodeReasons = transcodeReasons;
return playlistItem;
}
private int GetDefaultAudioBitrateIfUnknown(MediaStream audioStream)
{
if ((audioStream.Channels ?? 0) >= 6)
{
return 384000;
}
return 192000;
}
2017-09-25 05:06:15 +00:00
private int GetAudioBitrate(string subProtocol, long? maxTotalBitrate, int? targetAudioChannels, string[] targetAudioCodecs, MediaStream audioStream)
{
2017-09-25 05:06:15 +00:00
var targetAudioCodec = targetAudioCodecs.Length == 0 ? null : targetAudioCodecs[0];
int defaultBitrate = audioStream == null ? 192000 : audioStream.BitRate ?? GetDefaultAudioBitrateIfUnknown(audioStream);
2016-08-24 21:06:04 +00:00
// Reduce the bitrate if we're downmixing
if (targetAudioChannels.HasValue && audioStream != null && audioStream.Channels.HasValue && targetAudioChannels.Value < audioStream.Channels.Value)
2016-04-12 17:37:58 +00:00
{
2016-08-24 21:06:04 +00:00
defaultBitrate = StringHelper.EqualsIgnoreCase(targetAudioCodec, "ac3") ? 192000 : 128000;
2016-07-30 04:21:26 +00:00
}
2015-04-25 03:30:44 +00:00
2016-10-01 03:33:26 +00:00
if (StringHelper.EqualsIgnoreCase(subProtocol, "hls"))
{
2016-10-01 03:33:26 +00:00
defaultBitrate = Math.Min(384000, defaultBitrate);
}
else
{
defaultBitrate = Math.Min(448000, defaultBitrate);
2015-04-25 03:30:44 +00:00
}
int encoderAudioBitrateLimit = int.MaxValue;
if (audioStream != null)
{
// Seeing webm encoding failures when source has 1 audio channel and 22k bitrate.
// Any attempts to transcode over 64k will fail
if (audioStream.Channels.HasValue &&
audioStream.Channels.Value == 1)
{
if ((audioStream.BitRate ?? 0) < 64000)
{
encoderAudioBitrateLimit = 64000;
}
2014-06-23 16:05:19 +00:00
}
2014-04-01 22:23:07 +00:00
}
2016-10-01 03:33:26 +00:00
if (maxTotalBitrate.HasValue)
{
if (maxTotalBitrate.Value < 640000)
{
defaultBitrate = Math.Min(128000, defaultBitrate);
}
}
2015-04-25 03:30:44 +00:00
return Math.Min(defaultBitrate, encoderAudioBitrateLimit);
2014-04-01 22:23:07 +00:00
}
2017-06-24 18:33:19 +00:00
private Tuple<PlayMethod?, List<TranscodeReason>> GetVideoDirectPlayProfile(VideoOptions options,
2014-04-24 05:08:10 +00:00
MediaSourceInfo mediaSource,
MediaStream videoStream,
2015-03-23 20:06:06 +00:00
MediaStream audioStream,
bool isEligibleForDirectPlay,
2016-12-23 17:57:47 +00:00
bool isEligibleForDirectStream)
2014-04-24 05:08:10 +00:00
{
DeviceProfile profile = options.Profile;
if (options.ForceDirectPlay)
{
2017-06-24 18:33:19 +00:00
return new Tuple<PlayMethod?, List<TranscodeReason>>(PlayMethod.DirectPlay, new List<TranscodeReason>());
}
if (options.ForceDirectStream)
{
2017-06-24 18:33:19 +00:00
return new Tuple<PlayMethod?, List<TranscodeReason>>(PlayMethod.DirectStream, new List<TranscodeReason>());
}
2014-04-24 05:08:10 +00:00
// See if it can be direct played
2014-05-08 21:23:24 +00:00
DirectPlayProfile directPlay = null;
foreach (DirectPlayProfile i in profile.DirectPlayProfiles)
{
if (i.Type == DlnaProfileType.Video && IsVideoDirectPlaySupported(i, mediaSource, videoStream, audioStream))
{
directPlay = i;
break;
}
}
2014-04-24 05:08:10 +00:00
if (directPlay == null)
{
2015-10-05 03:24:24 +00:00
_logger.Info("Profile: {0}, No direct play profiles found for Path: {1}",
2015-04-04 19:35:29 +00:00
profile.Name ?? "Unknown Profile",
2015-09-20 17:28:32 +00:00
mediaSource.Path ?? "Unknown path");
2017-06-24 18:33:19 +00:00
return new Tuple<PlayMethod?, List<TranscodeReason>>(null, GetTranscodeReasonsFromDirectPlayProfile(mediaSource, videoStream, audioStream, profile.DirectPlayProfiles));
2014-04-24 05:08:10 +00:00
}
2014-05-08 20:09:53 +00:00
string container = mediaSource.Container;
2014-04-24 05:08:10 +00:00
2017-08-19 19:43:35 +00:00
var conditions = new List<ProfileCondition>();
2014-05-09 19:43:06 +00:00
foreach (ContainerProfile i in profile.ContainerProfiles)
{
if (i.Type == DlnaProfileType.Video &&
2016-12-13 17:04:37 +00:00
i.ContainsContainer(container))
2014-05-09 19:43:06 +00:00
{
2014-09-07 15:52:25 +00:00
foreach (ProfileCondition c in i.Conditions)
2014-07-01 04:06:28 +00:00
{
conditions.Add(c);
}
2014-05-09 19:43:06 +00:00
}
}
2014-04-24 05:08:10 +00:00
2014-05-08 20:09:53 +00:00
ConditionProcessor conditionProcessor = new ConditionProcessor();
2014-04-24 05:08:10 +00:00
2014-05-08 20:09:53 +00:00
int? width = videoStream == null ? null : videoStream.Width;
int? height = videoStream == null ? null : videoStream.Height;
int? bitDepth = videoStream == null ? null : videoStream.BitDepth;
int? videoBitrate = videoStream == null ? null : videoStream.BitRate;
double? videoLevel = videoStream == null ? null : videoStream.Level;
string videoProfile = videoStream == null ? null : videoStream.Profile;
float? videoFramerate = videoStream == null ? null : videoStream.AverageFrameRate ?? videoStream.AverageFrameRate;
2014-06-22 16:25:47 +00:00
bool? isAnamorphic = videoStream == null ? null : videoStream.IsAnamorphic;
2017-05-29 12:35:59 +00:00
bool? isInterlaced = videoStream == null ? (bool?)null : videoStream.IsInterlaced;
2015-10-19 16:05:03 +00:00
string videoCodecTag = videoStream == null ? null : videoStream.CodecTag;
2016-10-03 06:28:45 +00:00
bool? isAvc = videoStream == null ? null : videoStream.IsAVC;
2014-04-24 05:08:10 +00:00
2014-05-08 20:09:53 +00:00
int? audioBitrate = audioStream == null ? null : audioStream.BitRate;
int? audioChannels = audioStream == null ? null : audioStream.Channels;
string audioProfile = audioStream == null ? null : audioStream.Profile;
2017-05-13 19:31:25 +00:00
int? audioSampleRate = audioStream == null ? null : audioStream.SampleRate;
2017-06-26 15:10:52 +00:00
int? audioBitDepth = audioStream == null ? null : audioStream.BitDepth;
2014-04-24 05:08:10 +00:00
2014-05-08 20:09:53 +00:00
TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : mediaSource.Timestamp;
int? packetLength = videoStream == null ? null : videoStream.PacketLength;
2014-09-09 01:15:31 +00:00
int? refFrames = videoStream == null ? null : videoStream.RefFrames;
2014-04-24 05:08:10 +00:00
int? numAudioStreams = mediaSource.GetStreamCount(MediaStreamType.Audio);
int? numVideoStreams = mediaSource.GetStreamCount(MediaStreamType.Video);
2014-04-24 05:08:10 +00:00
// Check container conditions
2014-05-09 19:43:06 +00:00
foreach (ProfileCondition i in conditions)
2014-04-24 05:08:10 +00:00
{
2017-05-29 12:35:59 +00:00
if (!conditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
2014-05-09 19:43:06 +00:00
{
2015-04-14 19:11:29 +00:00
LogConditionFailure(profile, "VideoContainerProfile", i, mediaSource);
2015-04-04 19:35:29 +00:00
2017-07-01 18:00:43 +00:00
var transcodeReason = GetTranscodeReasonForFailedCondition(i);
var transcodeReasons = transcodeReason.HasValue
? new List<TranscodeReason> { transcodeReason.Value }
: new List<TranscodeReason> { };
return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
2014-05-09 19:43:06 +00:00
}
2014-04-24 05:08:10 +00:00
}
2014-05-08 20:09:53 +00:00
string videoCodec = videoStream == null ? null : videoStream.Codec;
2014-04-24 05:08:10 +00:00
2014-05-09 19:43:06 +00:00
conditions = new List<ProfileCondition>();
foreach (CodecProfile i in profile.CodecProfiles)
{
2017-09-25 05:06:15 +00:00
if (i.Type == CodecType.Video && i.ContainsAnyCodec(videoCodec, container))
2014-07-01 04:06:28 +00:00
{
2016-07-09 17:39:04 +00:00
bool applyConditions = true;
foreach (ProfileCondition applyCondition in i.ApplyConditions)
2014-07-01 04:06:28 +00:00
{
2017-05-29 12:35:59 +00:00
if (!conditionProcessor.IsVideoConditionSatisfied(applyCondition, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
2016-07-09 17:39:04 +00:00
{
LogConditionFailure(profile, "VideoCodecProfile", applyCondition, mediaSource);
applyConditions = false;
break;
}
}
if (applyConditions)
{
foreach (ProfileCondition c in i.Conditions)
{
conditions.Add(c);
}
2014-07-01 04:06:28 +00:00
}
}
2014-05-09 19:43:06 +00:00
}
2014-04-24 05:08:10 +00:00
2014-05-09 19:43:06 +00:00
foreach (ProfileCondition i in conditions)
2014-04-24 05:08:10 +00:00
{
2017-05-29 12:35:59 +00:00
if (!conditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
2014-05-09 19:43:06 +00:00
{
2015-04-14 19:11:29 +00:00
LogConditionFailure(profile, "VideoCodecProfile", i, mediaSource);
2015-04-04 19:35:29 +00:00
2017-06-24 18:33:19 +00:00
var transcodeReason = GetTranscodeReasonForFailedCondition(i);
var transcodeReasons = transcodeReason.HasValue
? new List<TranscodeReason> { transcodeReason.Value }
: new List<TranscodeReason> { };
return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
2014-05-09 19:43:06 +00:00
}
2014-04-24 05:08:10 +00:00
}
if (audioStream != null)
{
2014-05-08 20:09:53 +00:00
string audioCodec = audioStream.Codec;
2014-04-24 05:08:10 +00:00
2014-05-09 19:43:06 +00:00
conditions = new List<ProfileCondition>();
2016-07-09 17:39:04 +00:00
bool? isSecondaryAudio = audioStream == null ? null : mediaSource.IsSecondaryAudio(audioStream);
2014-05-09 19:43:06 +00:00
foreach (CodecProfile i in profile.CodecProfiles)
{
2017-09-25 05:06:15 +00:00
if (i.Type == CodecType.VideoAudio && i.ContainsAnyCodec(audioCodec, container))
2014-07-01 04:06:28 +00:00
{
2016-07-09 17:39:04 +00:00
bool applyConditions = true;
foreach (ProfileCondition applyCondition in i.ApplyConditions)
2014-07-01 04:06:28 +00:00
{
2017-06-26 15:10:52 +00:00
if (!conditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, audioBitrate, audioSampleRate, audioBitDepth, audioProfile, isSecondaryAudio))
2016-07-09 17:39:04 +00:00
{
2017-07-30 18:02:25 +00:00
LogConditionFailure(profile, "VideoAudioCodecProfile.ApplyConditions", applyCondition, mediaSource);
2016-07-09 17:39:04 +00:00
applyConditions = false;
break;
}
}
if (applyConditions)
{
foreach (ProfileCondition c in i.Conditions)
{
conditions.Add(c);
}
2014-07-01 04:06:28 +00:00
}
}
2014-05-09 19:43:06 +00:00
}
2014-04-24 05:08:10 +00:00
2014-05-09 19:43:06 +00:00
foreach (ProfileCondition i in conditions)
2014-04-24 05:08:10 +00:00
{
2017-06-26 15:10:52 +00:00
if (!conditionProcessor.IsVideoAudioConditionSatisfied(i, audioChannels, audioBitrate, audioSampleRate, audioBitDepth, audioProfile, isSecondaryAudio))
2014-05-09 19:43:06 +00:00
{
2015-04-14 19:11:29 +00:00
LogConditionFailure(profile, "VideoAudioCodecProfile", i, mediaSource);
2015-09-20 17:28:32 +00:00
2017-06-24 18:33:19 +00:00
var transcodeReason = GetTranscodeReasonForFailedCondition(i);
var transcodeReasons = transcodeReason.HasValue
? new List<TranscodeReason> { transcodeReason.Value }
: new List<TranscodeReason> { };
return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
2014-05-09 19:43:06 +00:00
}
2014-04-24 05:08:10 +00:00
}
}
2015-04-17 17:46:41 +00:00
if (isEligibleForDirectStream && mediaSource.SupportsDirectStream)
2015-03-17 03:51:35 +00:00
{
2017-06-24 18:33:19 +00:00
return new Tuple<PlayMethod?, List<TranscodeReason>>(PlayMethod.DirectStream, new List<TranscodeReason>());
2015-03-17 03:51:35 +00:00
}
2015-03-23 20:06:06 +00:00
2017-06-24 18:33:19 +00:00
return new Tuple<PlayMethod?, List<TranscodeReason>>(null, new List<TranscodeReason> { TranscodeReason.ContainerBitrateExceedsLimit });
2014-04-24 05:08:10 +00:00
}
2015-04-14 19:11:29 +00:00
private void LogConditionFailure(DeviceProfile profile, string type, ProfileCondition condition, MediaSourceInfo mediaSource)
{
2015-10-05 03:24:24 +00:00
_logger.Info("Profile: {0}, DirectPlay=false. Reason={1}.{2} Condition: {3}. ConditionValue: {4}. IsRequired: {5}. Path: {6}",
2015-04-14 19:11:29 +00:00
type,
profile.Name ?? "Unknown Profile",
condition.Property,
condition.Condition,
condition.Value ?? string.Empty,
condition.IsRequired,
mediaSource.Path ?? "Unknown path");
}
2017-07-03 17:16:01 +00:00
private Tuple<bool, TranscodeReason?> IsEligibleForDirectPlay(MediaSourceInfo item,
2017-01-03 05:15:59 +00:00
long? maxBitrate,
MediaStream subtitleStream,
2015-07-13 21:26:11 +00:00
VideoOptions options,
PlayMethod playMethod)
2014-04-01 22:23:07 +00:00
{
if (subtitleStream != null)
2014-04-01 22:23:07 +00:00
{
2017-10-13 18:32:58 +00:00
SubtitleProfile subtitleProfile = GetSubtitleProfile(item, subtitleStream, options.Profile.SubtitleProfiles, playMethod, _transcoderSupport, null, null);
2015-01-15 05:54:42 +00:00
if (subtitleProfile.Method != SubtitleDeliveryMethod.External && subtitleProfile.Method != SubtitleDeliveryMethod.Embed)
{
2015-10-05 03:24:24 +00:00
_logger.Info("Not eligible for {0} due to unsupported subtitles", playMethod);
2017-07-03 17:16:01 +00:00
return new Tuple<bool, TranscodeReason?>(false, TranscodeReason.SubtitleCodecNotSupported);
}
2014-04-01 22:23:07 +00:00
}
2017-07-03 17:16:01 +00:00
var result = IsAudioEligibleForDirectPlay(item, maxBitrate, playMethod);
if (result)
{
return new Tuple<bool, TranscodeReason?>(result, null);
}
return new Tuple<bool, TranscodeReason?>(result, TranscodeReason.ContainerBitrateExceedsLimit);
2014-04-06 17:53:23 +00:00
}
2017-10-13 18:32:58 +00:00
public static SubtitleProfile GetSubtitleProfile(MediaSourceInfo mediaSource, MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod, ITranscoderSupport transcoderSupport, string transcodingSubProtocol, string transcodingContainer)
{
2017-01-29 20:00:29 +00:00
if (!subtitleStream.IsExternal && (playMethod != PlayMethod.Transcode || !string.Equals(transcodingSubProtocol, "hls", StringComparison.OrdinalIgnoreCase)))
2015-07-14 16:39:34 +00:00
{
2017-01-29 20:00:29 +00:00
// Look for supported embedded subs of the same format
2015-07-14 16:39:34 +00:00
foreach (SubtitleProfile profile in subtitleProfiles)
{
if (!profile.SupportsLanguage(subtitleStream.Language))
{
continue;
}
2015-07-31 02:34:15 +00:00
if (profile.Method != SubtitleDeliveryMethod.Embed)
{
continue;
}
2017-01-29 20:00:29 +00:00
if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(subtitleStream, profile, transcodingSubProtocol, transcodingContainer))
{
continue;
}
2015-07-31 02:34:15 +00:00
if (subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format) && StringHelper.EqualsIgnoreCase(profile.Format, subtitleStream.Codec))
2015-07-14 16:39:34 +00:00
{
return profile;
}
}
2017-01-29 20:00:29 +00:00
// Look for supported embedded subs of a convertible format
foreach (SubtitleProfile profile in subtitleProfiles)
{
if (!profile.SupportsLanguage(subtitleStream.Language))
{
continue;
}
if (profile.Method != SubtitleDeliveryMethod.Embed)
{
continue;
}
if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(subtitleStream, profile, transcodingSubProtocol, transcodingContainer))
{
continue;
}
if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsSubtitleConversionTo(profile.Format))
{
return profile;
}
}
2015-07-14 16:39:34 +00:00
}
2016-04-02 23:39:08 +00:00
// Look for an external or hls profile that matches the stream type (text/graphical) and doesn't require conversion
2017-10-13 18:32:58 +00:00
return GetExternalSubtitleProfile(mediaSource, subtitleStream, subtitleProfiles, playMethod, transcoderSupport, false) ??
GetExternalSubtitleProfile(mediaSource, subtitleStream, subtitleProfiles, playMethod, transcoderSupport, true) ??
new SubtitleProfile
2017-09-25 05:06:15 +00:00
{
Method = SubtitleDeliveryMethod.Encode,
Format = subtitleStream.Codec
};
2016-04-02 23:39:08 +00:00
}
2017-01-29 20:00:29 +00:00
private static bool IsSubtitleEmbedSupported(MediaStream subtitleStream, SubtitleProfile subtitleProfile, string transcodingSubProtocol, string transcodingContainer)
{
2017-08-04 14:49:46 +00:00
if (!string.IsNullOrWhiteSpace(transcodingContainer))
2017-01-29 20:00:29 +00:00
{
2017-08-04 14:49:46 +00:00
var normalizedContainers = ContainerProfile.SplitValue(transcodingContainer);
if (ContainerProfile.ContainsContainer(normalizedContainers, "ts"))
{
return false;
}
if (ContainerProfile.ContainsContainer(normalizedContainers, "mpegts"))
{
return false;
}
if (ContainerProfile.ContainsContainer(normalizedContainers, "mp4"))
{
return false;
}
if (ContainerProfile.ContainsContainer(normalizedContainers, "mkv") ||
ContainerProfile.ContainsContainer(normalizedContainers, "matroska"))
{
return true;
}
2017-01-29 20:00:29 +00:00
}
return false;
}
2017-10-13 18:32:58 +00:00
private static SubtitleProfile GetExternalSubtitleProfile(MediaSourceInfo mediaSource, MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod, ITranscoderSupport transcoderSupport, bool allowConversion)
2016-04-02 23:39:08 +00:00
{
2015-03-23 17:19:21 +00:00
foreach (SubtitleProfile profile in subtitleProfiles)
2015-09-20 17:28:32 +00:00
{
2016-03-06 21:31:56 +00:00
if (profile.Method != SubtitleDeliveryMethod.External && profile.Method != SubtitleDeliveryMethod.Hls)
2015-09-20 17:28:32 +00:00
{
continue;
}
2015-03-31 16:24:16 +00:00
2016-03-07 18:50:58 +00:00
if (profile.Method == SubtitleDeliveryMethod.Hls && playMethod != PlayMethod.Transcode)
{
continue;
}
2015-03-28 20:22:27 +00:00
if (!profile.SupportsLanguage(subtitleStream.Language))
{
continue;
}
if (!subtitleStream.IsExternal && !transcoderSupport.CanExtractSubtitles(subtitleStream.Codec))
{
continue;
}
2016-03-06 21:31:56 +00:00
if ((profile.Method == SubtitleDeliveryMethod.External && subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format)) ||
(profile.Method == SubtitleDeliveryMethod.Hls && subtitleStream.IsTextSubtitleStream))
{
2015-09-20 17:28:32 +00:00
bool requiresConversion = !StringHelper.EqualsIgnoreCase(subtitleStream.Codec, profile.Format);
2015-09-13 21:32:02 +00:00
2016-06-25 05:16:54 +00:00
if (!requiresConversion)
2015-03-31 16:24:16 +00:00
{
2016-06-25 05:16:54 +00:00
return profile;
2016-04-02 23:39:08 +00:00
}
2016-06-25 05:16:54 +00:00
if (!allowConversion)
2016-04-02 23:39:08 +00:00
{
2016-06-25 05:16:54 +00:00
continue;
2016-04-02 23:39:08 +00:00
}
2017-10-13 18:32:58 +00:00
// TODO: Build this into subtitleStream.SupportsExternalStream
if (mediaSource.IsInfiniteStream)
{
continue;
}
2016-06-25 05:16:54 +00:00
if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsExternalStream && subtitleStream.SupportsSubtitleConversionTo(profile.Format))
2016-04-02 23:39:08 +00:00
{
return profile;
}
}
2015-02-02 18:14:02 +00:00
}
2016-04-02 23:39:08 +00:00
return null;
}
2017-03-24 15:03:49 +00:00
private bool IsAudioEligibleForDirectPlay(MediaSourceInfo item, long? maxBitrate, PlayMethod playMethod)
2014-04-06 17:53:23 +00:00
{
2017-03-18 23:37:29 +00:00
// Don't restrict by bitrate if coming from an external domain
if (item.IsRemote)
{
2017-06-24 18:33:19 +00:00
return true;
2017-03-18 23:37:29 +00:00
}
2017-11-19 04:59:34 +00:00
var requestedMaxBitrate = maxBitrate ?? 1000000;
2015-07-10 18:30:42 +00:00
// If we don't know the bitrate, then force a transcode if requested max bitrate is under 40 mbps
var itemBitrate = item.Bitrate ??
40000000;
2016-04-13 20:49:16 +00:00
2017-11-19 04:59:34 +00:00
if (itemBitrate > requestedMaxBitrate)
2016-04-13 20:49:16 +00:00
{
2017-11-19 04:59:34 +00:00
_logger.Info("Bitrate exceeds " + playMethod + " limit: media bitrate: {0}, max bitrate: {1}", item.Bitrate.Value.ToString(CultureInfo.InvariantCulture), requestedMaxBitrate.ToString(CultureInfo.InvariantCulture));
2016-04-13 20:49:16 +00:00
return false;
}
return true;
2014-04-01 22:23:07 +00:00
}
private void ValidateInput(VideoOptions options)
{
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 void ValidateAudioInput(AudioOptions options)
{
if (string.IsNullOrEmpty(options.ItemId))
{
throw new ArgumentException("ItemId is required");
}
if (string.IsNullOrEmpty(options.DeviceId))
{
throw new ArgumentException("DeviceId is required");
}
if (options.Profile == null)
{
throw new ArgumentException("Profile is required");
}
if (options.MediaSources == null)
{
throw new ArgumentException("MediaSources is required");
}
}
private void ApplyTranscodingConditions(StreamInfo item, IEnumerable<ProfileCondition> conditions, string qualifier, bool qualifiedOnly)
2014-04-01 22:23:07 +00:00
{
2014-05-09 19:43:06 +00:00
foreach (ProfileCondition condition in conditions)
2014-04-01 22:23:07 +00:00
{
2014-05-08 20:09:53 +00:00
string value = condition.Value;
2014-04-01 22:23:07 +00:00
2014-05-09 19:43:06 +00:00
if (string.IsNullOrEmpty(value))
{
continue;
}
2014-12-14 20:01:26 +00:00
// No way to express this
if (condition.Condition == ProfileConditionType.GreaterThanEqual)
{
continue;
}
2014-04-01 22:23:07 +00:00
switch (condition.Property)
{
case ProfileConditionValue.AudioBitrate:
{
if (qualifiedOnly)
{
continue;
}
2014-04-01 22:23:07 +00:00
int num;
2016-10-22 02:08:34 +00:00
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
2014-04-01 22:23:07 +00:00
{
2017-09-07 18:17:18 +00: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);
}
2014-04-01 22:23:07 +00:00
}
break;
}
case ProfileConditionValue.AudioChannels:
{
if (qualifiedOnly)
{
continue;
}
2014-04-01 22:23:07 +00:00
int num;
2016-10-22 02:08:34 +00:00
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
2014-04-01 22:23:07 +00:00
{
2017-09-07 18:17:18 +00:00
if (condition.Condition == ProfileConditionType.Equals)
{
item.MaxAudioChannels = num;
}
else if (condition.Condition == ProfileConditionType.LessThanEqual)
{
item.MaxAudioChannels = Math.Min(num, item.MaxAudioChannels ?? num);
}
else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
{
item.MaxAudioChannels = Math.Max(num, item.MaxAudioChannels ?? num);
}
2014-04-01 22:23:07 +00:00
}
break;
}
2016-11-14 07:28:20 +00:00
case ProfileConditionValue.IsAvc:
{
if (qualifiedOnly)
{
continue;
}
2016-11-14 07:28:20 +00:00
bool isAvc;
if (bool.TryParse(value, out isAvc))
{
if (isAvc && condition.Condition == ProfileConditionType.Equals)
{
item.RequireAvc = true;
}
else if (!isAvc && condition.Condition == ProfileConditionType.NotEquals)
{
item.RequireAvc = true;
}
}
break;
}
2014-06-22 16:25:47 +00:00
case ProfileConditionValue.IsAnamorphic:
2017-03-15 19:57:18 +00:00
{
if (qualifiedOnly)
{
continue;
}
2017-03-15 19:57:18 +00:00
bool isAnamorphic;
if (bool.TryParse(value, out isAnamorphic))
{
if (isAnamorphic && condition.Condition == ProfileConditionType.Equals)
{
item.RequireNonAnamorphic = true;
}
else if (!isAnamorphic && condition.Condition == ProfileConditionType.NotEquals)
{
item.RequireNonAnamorphic = true;
}
}
break;
}
case ProfileConditionValue.IsInterlaced:
{
if (string.IsNullOrWhiteSpace(qualifier))
{
continue;
}
2017-03-15 19:57:18 +00:00
bool isInterlaced;
if (bool.TryParse(value, out isInterlaced))
{
2017-06-06 06:13:27 +00:00
if (!isInterlaced && condition.Condition == ProfileConditionType.Equals)
2017-03-15 19:57:18 +00:00
{
item.SetOption(qualifier, "deinterlace", "true");
2017-03-15 19:57:18 +00:00
}
2017-06-06 06:13:27 +00:00
else if (isInterlaced && condition.Condition == ProfileConditionType.NotEquals)
2017-03-15 19:57:18 +00:00
{
item.SetOption(qualifier, "deinterlace", "true");
2017-03-15 19:57:18 +00:00
}
}
break;
}
2014-12-14 20:01:26 +00:00
case ProfileConditionValue.AudioProfile:
2014-04-01 22:23:07 +00:00
case ProfileConditionValue.Has64BitOffsets:
2014-04-24 05:08:10 +00:00
case ProfileConditionValue.PacketLength:
case ProfileConditionValue.NumAudioStreams:
case ProfileConditionValue.NumVideoStreams:
case ProfileConditionValue.IsSecondaryAudio:
2014-04-24 05:08:10 +00:00
case ProfileConditionValue.VideoTimestamp:
2014-04-01 22:23:07 +00:00
{
// Not supported yet
break;
}
2014-09-23 04:05:29 +00:00
case ProfileConditionValue.RefFrames:
{
2017-09-25 05:06:15 +00:00
if (string.IsNullOrWhiteSpace(qualifier))
{
continue;
}
2014-09-23 04:05:29 +00:00
int num;
2016-10-22 02:08:34 +00:00
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
2014-09-23 04:05:29 +00:00
{
2017-09-07 18:17:18 +00:00
if (condition.Condition == ProfileConditionType.Equals)
{
2017-09-25 05:06:15 +00:00
item.SetOption(qualifier, "maxrefframes", StringHelper.ToStringCultureInvariant(num));
2017-09-07 18:17:18 +00:00
}
else if (condition.Condition == ProfileConditionType.LessThanEqual)
{
2017-09-25 05:06:15 +00:00
item.SetOption(qualifier, "maxrefframes", StringHelper.ToStringCultureInvariant(Math.Min(num, item.GetTargetRefFrames(qualifier) ?? num)));
2017-09-07 18:17:18 +00:00
}
else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
{
2017-09-25 05:06:15 +00:00
item.SetOption(qualifier, "maxrefframes", StringHelper.ToStringCultureInvariant(Math.Max(num, item.GetTargetRefFrames(qualifier) ?? num)));
2017-09-07 18:17:18 +00:00
}
2014-09-23 04:05:29 +00:00
}
break;
}
case ProfileConditionValue.VideoBitDepth:
{
if (qualifiedOnly)
{
continue;
}
2014-09-23 04:05:29 +00:00
int num;
2016-10-22 02:08:34 +00:00
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
2014-09-23 04:05:29 +00:00
{
2017-09-07 18:17:18 +00:00
if (condition.Condition == ProfileConditionType.Equals)
{
item.MaxVideoBitDepth = num;
}
else if (condition.Condition == ProfileConditionType.LessThanEqual)
{
item.MaxVideoBitDepth = Math.Min(num, item.MaxVideoBitDepth ?? num);
}
else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
{
item.MaxVideoBitDepth = Math.Max(num, item.MaxVideoBitDepth ?? num);
}
2014-09-23 04:05:29 +00:00
}
break;
}
2014-04-24 05:08:10 +00:00
case ProfileConditionValue.VideoProfile:
{
2017-09-25 05:06:15 +00:00
if (string.IsNullOrWhiteSpace(qualifier))
{
continue;
}
2017-09-25 05:06:15 +00:00
if (!string.IsNullOrWhiteSpace(value))
{
// change from split by | to comma
2017-09-29 20:10:13 +00:00
// strip spaces to avoid having to encode
var values = value
.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
item.SetOption(qualifier, "profile", string.Join(",", values));
2017-09-25 05:06:15 +00:00
}
2014-04-24 05:08:10 +00:00
break;
}
2014-04-01 22:23:07 +00:00
case ProfileConditionValue.Height:
{
if (qualifiedOnly)
{
continue;
}
2014-04-01 22:23:07 +00:00
int num;
2016-10-22 02:08:34 +00:00
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
2014-04-01 22:23:07 +00:00
{
2017-09-07 18:17:18 +00: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);
}
2014-04-01 22:23:07 +00:00
}
break;
}
case ProfileConditionValue.VideoBitrate:
{
if (qualifiedOnly)
{
continue;
}
2014-04-01 22:23:07 +00:00
int num;
2016-10-22 02:08:34 +00:00
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
2014-04-01 22:23:07 +00:00
{
2017-09-07 18:17:18 +00: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);
}
2014-04-01 22:23:07 +00:00
}
break;
}
case ProfileConditionValue.VideoFramerate:
{
if (qualifiedOnly)
{
continue;
}
2014-06-23 16:05:19 +00:00
float num;
2016-10-23 19:47:34 +00:00
if (float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
2014-04-01 22:23:07 +00:00
{
2017-09-07 18:17:18 +00: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);
}
2014-04-01 22:23:07 +00:00
}
break;
}
case ProfileConditionValue.VideoLevel:
{
2017-09-25 05:06:15 +00:00
if (string.IsNullOrWhiteSpace(qualifier))
{
continue;
}
2014-04-01 22:23:07 +00:00
int num;
2016-10-22 02:08:34 +00:00
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
2014-04-01 22:23:07 +00:00
{
2017-09-07 18:17:18 +00:00
if (condition.Condition == ProfileConditionType.Equals)
{
2017-09-25 05:06:15 +00:00
item.SetOption(qualifier, "level", StringHelper.ToStringCultureInvariant(num));
2017-09-07 18:17:18 +00:00
}
else if (condition.Condition == ProfileConditionType.LessThanEqual)
{
2017-09-25 05:06:15 +00:00
item.SetOption(qualifier, "level", StringHelper.ToStringCultureInvariant(Math.Min(num, item.GetTargetVideoLevel(qualifier) ?? num)));
2017-09-07 18:17:18 +00:00
}
else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
{
2017-09-25 05:06:15 +00:00
item.SetOption(qualifier, "level", StringHelper.ToStringCultureInvariant(Math.Max(num, item.GetTargetVideoLevel(qualifier) ?? num)));
2017-09-07 18:17:18 +00:00
}
2014-04-01 22:23:07 +00:00
}
break;
}
case ProfileConditionValue.Width:
{
if (qualifiedOnly)
{
continue;
}
2014-04-01 22:23:07 +00:00
int num;
2016-10-22 02:08:34 +00:00
if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
2014-04-01 22:23:07 +00:00
{
2017-09-07 18:17:18 +00: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);
}
2014-04-01 22:23:07 +00:00
}
break;
}
2016-11-14 07:28:20 +00:00
default:
break;
2014-04-01 22:23:07 +00:00
}
}
}
2014-04-06 17:53:23 +00:00
private bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audioStream)
2014-04-01 22:23:07 +00:00
{
// Check container type
if (!profile.SupportsContainer(item.Container))
2014-04-01 22:23:07 +00:00
{
return false;
2014-04-01 22:23:07 +00:00
}
// Check audio codec
2017-08-19 19:43:35 +00:00
var audioCodecs = profile.GetAudioCodecs();
if (audioCodecs.Length > 0)
{
// Check audio codecs
string audioCodec = audioStream == null ? null : audioStream.Codec;
if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
{
return false;
}
}
2014-04-01 22:23:07 +00:00
return true;
}
2014-04-06 17:53:23 +00:00
private bool IsVideoDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream)
2014-04-01 22:23:07 +00:00
{
// Check container type
if (!profile.SupportsContainer(item.Container))
2014-04-01 22:23:07 +00:00
{
return false;
2014-04-01 22:23:07 +00:00
}
// Check video codec
2017-08-19 19:43:35 +00:00
var videoCodecs = profile.GetVideoCodecs();
if (videoCodecs.Length > 0)
2014-04-01 22:23:07 +00:00
{
2014-05-08 20:09:53 +00:00
string videoCodec = videoStream == null ? null : videoStream.Codec;
2014-06-05 02:32:40 +00:00
if (string.IsNullOrEmpty(videoCodec) || !ListHelper.ContainsIgnoreCase(videoCodecs, videoCodec))
2014-04-01 22:23:07 +00:00
{
return false;
}
}
// Check audio codec
2017-08-04 14:49:46 +00:00
if (audioStream != null)
2014-04-01 22:23:07 +00:00
{
2017-08-19 19:43:35 +00:00
var audioCodecs = profile.GetAudioCodecs();
if (audioCodecs.Length > 0)
2014-04-01 22:23:07 +00:00
{
2017-08-04 14:49:46 +00:00
// Check audio codecs
string audioCodec = audioStream == null ? null : audioStream.Codec;
if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
{
return false;
}
2014-04-01 22:23:07 +00:00
}
}
return true;
}
}
2016-10-01 03:33:26 +00:00
}