12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589 |
- using MediaBrowser.Model.Dto;
- using MediaBrowser.Model.Entities;
- using MediaBrowser.Model.Extensions;
- using MediaBrowser.Model.Logging;
- using MediaBrowser.Model.MediaInfo;
- using MediaBrowser.Model.Session;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- namespace MediaBrowser.Model.Dlna
- {
- public class StreamBuilder
- {
- private readonly ILogger _logger;
- private readonly ITranscoderSupport _transcoderSupport;
- public StreamBuilder(ITranscoderSupport transcoderSupport, ILogger logger)
- {
- _transcoderSupport = transcoderSupport;
- _logger = logger;
- }
- public StreamBuilder(ILogger logger)
- : this(new FullTranscoderSupport(), logger)
- {
- }
- public StreamInfo BuildAudioItem(AudioOptions options)
- {
- ValidateAudioInput(options);
- List<MediaSourceInfo> mediaSources = new List<MediaSourceInfo>();
- foreach (MediaSourceInfo i in options.MediaSources)
- {
- if (string.IsNullOrEmpty(options.MediaSourceId) ||
- StringHelper.EqualsIgnoreCase(i.Id, options.MediaSourceId))
- {
- mediaSources.Add(i);
- }
- }
- List<StreamInfo> streams = new List<StreamInfo>();
- foreach (MediaSourceInfo i in mediaSources)
- {
- StreamInfo streamInfo = BuildAudioItem(i, options);
- if (streamInfo != null)
- {
- streams.Add(streamInfo);
- }
- }
- foreach (StreamInfo stream in streams)
- {
- stream.DeviceId = options.DeviceId;
- stream.DeviceProfileId = options.Profile.Id;
- }
- return GetOptimalStream(streams, options.GetMaxBitrate(true));
- }
- public StreamInfo BuildVideoItem(VideoOptions options)
- {
- ValidateInput(options);
- List<MediaSourceInfo> mediaSources = new List<MediaSourceInfo>();
- foreach (MediaSourceInfo i in options.MediaSources)
- {
- if (string.IsNullOrEmpty(options.MediaSourceId) ||
- StringHelper.EqualsIgnoreCase(i.Id, options.MediaSourceId))
- {
- mediaSources.Add(i);
- }
- }
- List<StreamInfo> streams = new List<StreamInfo>();
- foreach (MediaSourceInfo i in mediaSources)
- {
- StreamInfo streamInfo = BuildVideoItem(i, options);
- if (streamInfo != null)
- {
- streams.Add(streamInfo);
- }
- }
- foreach (StreamInfo stream in streams)
- {
- stream.DeviceId = options.DeviceId;
- stream.DeviceProfileId = options.Profile.Id;
- }
- return GetOptimalStream(streams, options.GetMaxBitrate(false));
- }
- private StreamInfo GetOptimalStream(List<StreamInfo> streams, long? maxBitrate)
- {
- streams = StreamInfoSorter.SortMediaSources(streams, maxBitrate);
- foreach (StreamInfo stream in streams)
- {
- return stream;
- }
- return null;
- }
- 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;
- case ProfileConditionValue.AudioBitDepth:
- return TranscodeReason.AudioBitDepthNotSupported;
- case ProfileConditionValue.VideoBitrate:
- return TranscodeReason.VideoBitrateNotSupported;
- case ProfileConditionValue.VideoCodecTag:
- return TranscodeReason.VideoCodecNotSupported;
- case ProfileConditionValue.VideoFramerate:
- return TranscodeReason.VideoFramerateNotSupported;
- case ProfileConditionValue.VideoLevel:
- return TranscodeReason.VideoLevelNotSupported;
- case ProfileConditionValue.VideoProfile:
- return TranscodeReason.VideoProfileNotSupported;
- case ProfileConditionValue.VideoTimestamp:
- // TODO
- return null;
- case ProfileConditionValue.Width:
- return TranscodeReason.VideoResolutionNotSupported;
- default:
- return null;
- }
- }
- private StreamInfo BuildAudioItem(MediaSourceInfo item, AudioOptions options)
- {
- List<TranscodeReason> transcodeReasons = new List<TranscodeReason>();
- StreamInfo playlistItem = new StreamInfo
- {
- 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 = item.Container;
- return playlistItem;
- }
- if (options.ForceDirectStream)
- {
- playlistItem.PlayMethod = PlayMethod.DirectStream;
- playlistItem.Container = item.Container;
- return playlistItem;
- }
- MediaStream audioStream = item.GetDefaultAudioStream(null);
- var directPlayInfo = GetAudioDirectPlayMethods(item, audioStream, options);
- List<PlayMethod> directPlayMethods = directPlayInfo.Item1;
- transcodeReasons.AddRange(directPlayInfo.Item2);
- ConditionProcessor conditionProcessor = new ConditionProcessor();
- int? inputAudioChannels = audioStream == null ? null : audioStream.Channels;
- int? inputAudioBitrate = audioStream == null ? null : audioStream.BitDepth;
- int? inputAudioSampleRate = audioStream == null ? null : audioStream.SampleRate;
- int? inputAudioBitDepth = audioStream == null ? null : audioStream.BitDepth;
- if (directPlayMethods.Count > 0)
- {
- string audioCodec = audioStream == null ? null : audioStream.Codec;
- // Make sure audio codec profiles are satisfied
- if (!string.IsNullOrEmpty(audioCodec))
- {
- List<ProfileCondition> conditions = new List<ProfileCondition>();
- foreach (CodecProfile i in options.Profile.CodecProfiles)
- {
- if (i.Type == CodecType.Audio && i.ContainsCodec(audioCodec, item.Container))
- {
- bool applyConditions = true;
- foreach (ProfileCondition applyCondition in i.ApplyConditions)
- {
- if (!conditionProcessor.IsAudioConditionSatisfied(applyCondition, inputAudioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth))
- {
- LogConditionFailure(options.Profile, "AudioCodecProfile", applyCondition, item);
- applyConditions = false;
- break;
- }
- }
- if (applyConditions)
- {
- foreach (ProfileCondition c in i.Conditions)
- {
- conditions.Add(c);
- }
- }
- }
- }
- bool all = true;
- foreach (ProfileCondition c in conditions)
- {
- if (!conditionProcessor.IsAudioConditionSatisfied(c, inputAudioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth))
- {
- LogConditionFailure(options.Profile, "AudioCodecProfile", c, item);
- var transcodeReason = GetTranscodeReasonForFailedCondition(c);
- if (transcodeReason.HasValue)
- {
- transcodeReasons.Add(transcodeReason.Value);
- }
- all = false;
- break;
- }
- }
- if (all)
- {
- if (directPlayMethods.Contains(PlayMethod.DirectStream))
- {
- playlistItem.PlayMethod = PlayMethod.DirectStream;
- }
- playlistItem.Container = item.Container;
- return playlistItem;
- }
- }
- }
- TranscodingProfile transcodingProfile = null;
- foreach (TranscodingProfile i in options.Profile.TranscodingProfiles)
- {
- if (i.Type == playlistItem.MediaType && i.Context == options.Context)
- {
- if (_transcoderSupport.CanEncodeToAudioCodec(i.AudioCodec ?? i.Container))
- {
- transcodingProfile = i;
- break;
- }
- }
- }
- if (transcodingProfile != null)
- {
- if (!item.SupportsTranscoding)
- {
- return null;
- }
- playlistItem.PlayMethod = PlayMethod.Transcode;
- playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
- playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
- playlistItem.Container = transcodingProfile.Container;
- if (string.IsNullOrEmpty(transcodingProfile.AudioCodec))
- {
- playlistItem.AudioCodecs = new string[] { };
- }
- else
- {
- playlistItem.AudioCodecs = transcodingProfile.AudioCodec.Split(',');
- }
- playlistItem.SubProtocol = transcodingProfile.Protocol;
- List<CodecProfile> audioCodecProfiles = new List<CodecProfile>();
- foreach (CodecProfile i in options.Profile.CodecProfiles)
- {
- if (i.Type == CodecType.Audio && i.ContainsCodec(transcodingProfile.AudioCodec, transcodingProfile.Container))
- {
- audioCodecProfiles.Add(i);
- }
- if (audioCodecProfiles.Count >= 1) break;
- }
- List<ProfileCondition> audioTranscodingConditions = new List<ProfileCondition>();
- foreach (CodecProfile i in audioCodecProfiles)
- {
- bool applyConditions = true;
- foreach (ProfileCondition applyCondition in i.ApplyConditions)
- {
- if (!conditionProcessor.IsAudioConditionSatisfied(applyCondition, inputAudioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth))
- {
- LogConditionFailure(options.Profile, "AudioCodecProfile", applyCondition, item);
- applyConditions = false;
- break;
- }
- }
- if (applyConditions)
- {
- foreach (ProfileCondition c in i.Conditions)
- {
- audioTranscodingConditions.Add(c);
- }
- }
- }
- ApplyTranscodingConditions(playlistItem, audioTranscodingConditions);
- // Honor requested max channels
- if (options.MaxAudioChannels.HasValue)
- {
- int currentValue = playlistItem.MaxAudioChannels ?? options.MaxAudioChannels.Value;
- playlistItem.MaxAudioChannels = Math.Min(options.MaxAudioChannels.Value, currentValue);
- }
- long transcodingBitrate = options.AudioTranscodingBitrate ??
- options.Profile.MusicStreamingTranscodingBitrate ??
- 128000;
- var configuredBitrate = options.GetMaxBitrate(true);
- 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;
- return playlistItem;
- }
- private long? GetBitrateForDirectPlayCheck(MediaSourceInfo item, AudioOptions options, bool isAudio)
- {
- if (item.Protocol == MediaProtocol.File)
- {
- return options.Profile.MaxStaticBitrate;
- }
- return options.GetMaxBitrate(isAudio);
- }
- private Tuple<List<PlayMethod>, List<TranscodeReason>> GetAudioDirectPlayMethods(MediaSourceInfo item, MediaStream audioStream, AudioOptions options)
- {
- List<TranscodeReason> transcodeReasons = new List<TranscodeReason>();
- DirectPlayProfile directPlayProfile = null;
- foreach (DirectPlayProfile i in options.Profile.DirectPlayProfiles)
- {
- if (i.Type == DlnaProfileType.Audio && IsAudioDirectPlaySupported(i, item, audioStream))
- {
- directPlayProfile = i;
- break;
- }
- }
- List<PlayMethod> playMethods = new List<PlayMethod>();
- if (directPlayProfile != null)
- {
- // While options takes the network and other factors into account. Only applies to direct stream
- if (item.SupportsDirectStream)
- {
- if (IsAudioEligibleForDirectPlay(item, options.GetMaxBitrate(true), PlayMethod.DirectStream))
- {
- if (options.EnableDirectStream)
- {
- playMethods.Add(PlayMethod.DirectStream);
- }
- }
- else
- {
- transcodeReasons.Add(TranscodeReason.ContainerBitrateExceedsLimit);
- }
- }
- // The profile describes what the device supports
- // If device requirements are satisfied then allow both direct stream and direct play
- if (item.SupportsDirectPlay)
- {
- if (IsAudioEligibleForDirectPlay(item, GetBitrateForDirectPlayCheck(item, options, true), PlayMethod.DirectPlay))
- {
- if (options.EnableDirectPlay)
- {
- playMethods.Add(PlayMethod.DirectPlay);
- }
- }
- else
- {
- transcodeReasons.Add(TranscodeReason.ContainerBitrateExceedsLimit);
- }
- }
- }
- else
- {
- transcodeReasons.InsertRange(0, GetTranscodeReasonsFromDirectPlayProfile(item, null, audioStream, options.Profile.DirectPlayProfiles));
- _logger.Info("Profile: {0}, No direct play profiles found for Path: {1}",
- options.Profile.Name ?? "Unknown Profile",
- item.Path ?? "Unknown path");
- }
- 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))
- {
- containerSupported = true;
- if (videoStream != null)
- {
- // Check video codec
- List<string> videoCodecs = profile.GetVideoCodecs();
- if (videoCodecs.Count > 0)
- {
- string videoCodec = videoStream.Codec;
- if (!string.IsNullOrEmpty(videoCodec) && ListHelper.ContainsIgnoreCase(videoCodecs, videoCodec))
- {
- videoSupported = true;
- }
- }
- else
- {
- videoSupported = true;
- }
- }
- if (audioStream != null)
- {
- // Check audio codec
- List<string> audioCodecs = profile.GetAudioCodecs();
- if (audioCodecs.Count > 0)
- {
- string audioCodec = audioStream.Codec;
- if (!string.IsNullOrEmpty(audioCodec) && ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
- {
- audioSupported = true;
- }
- }
- else
- {
- audioSupported = true;
- }
- }
- }
- }
- if (!containerSupported)
- {
- list.Add(TranscodeReason.ContainerNotSupported);
- }
- if (videoStream != null && !videoSupported)
- {
- list.Add(TranscodeReason.VideoCodecNotSupported);
- }
- if (audioStream != null && !audioSupported)
- {
- list.Add(TranscodeReason.AudioCodecNotSupported);
- }
- return list;
- }
- 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;
- }
- }
- }
- List<MediaStream> topStreams = new List<MediaStream>();
- 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;
- }
- private StreamInfo BuildVideoItem(MediaSourceInfo item, VideoOptions options)
- {
- if (item == null)
- {
- throw new ArgumentNullException("item");
- }
- List<TranscodeReason> transcodeReasons = new List<TranscodeReason>();
- StreamInfo playlistItem = new StreamInfo
- {
- 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);
- MediaStream subtitleStream = playlistItem.SubtitleStreamIndex.HasValue ? item.GetMediaStream(MediaStreamType.Subtitle, playlistItem.SubtitleStreamIndex.Value) : null;
- MediaStream audioStream = item.GetDefaultAudioStream(options.AudioStreamIndex ?? item.DefaultAudioStreamIndex);
- int? audioStreamIndex = null;
- if (audioStream != null)
- {
- audioStreamIndex = audioStream.Index;
- }
- MediaStream videoStream = item.VideoStream;
- // TODO: This doesn't accout for situation of device being able to handle media bitrate, but wifi connection not fast enough
- 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);
- _logger.Info("Profile: {0}, Path: {1}, isEligibleForDirectPlay: {2}, isEligibleForDirectStream: {3}",
- options.Profile.Name ?? "Unknown Profile",
- item.Path ?? "Unknown path",
- isEligibleForDirectPlay,
- isEligibleForDirectStream);
- if (isEligibleForDirectPlay || isEligibleForDirectStream)
- {
- // See if it can be direct played
- var directPlayInfo = GetVideoDirectPlayProfile(options, item, videoStream, audioStream, isEligibleForDirectPlay, isEligibleForDirectStream);
- var directPlay = directPlayInfo.Item1;
- if (directPlay != null)
- {
- playlistItem.PlayMethod = directPlay.Value;
- playlistItem.Container = item.Container;
- if (subtitleStream != null)
- {
- SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options.Profile.SubtitleProfiles, directPlay.Value, null, null);
- playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
- playlistItem.SubtitleFormat = subtitleProfile.Format;
- }
- return playlistItem;
- }
- transcodeReasons.AddRange(directPlayInfo.Item2);
- }
- if (directPlayEligibilityResult.Item2.HasValue)
- {
- transcodeReasons.Add(directPlayEligibilityResult.Item2.Value);
- }
- if (directStreamEligibilityResult.Item2.HasValue)
- {
- transcodeReasons.Add(directStreamEligibilityResult.Item2.Value);
- }
- // Can't direct play, find the transcoding profile
- TranscodingProfile transcodingProfile = null;
- foreach (TranscodingProfile i in options.Profile.TranscodingProfiles)
- {
- if (i.Type == playlistItem.MediaType && i.Context == options.Context)
- {
- transcodingProfile = i;
- break;
- }
- }
- if (transcodingProfile != null)
- {
- if (!item.SupportsTranscoding)
- {
- return null;
- }
- if (subtitleStream != null)
- {
- SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options.Profile.SubtitleProfiles, PlayMethod.Transcode, transcodingProfile.Protocol, transcodingProfile.Container);
- playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
- playlistItem.SubtitleFormat = subtitleProfile.Format;
- playlistItem.SubtitleCodecs = new[] { subtitleProfile.Format };
- }
- playlistItem.PlayMethod = PlayMethod.Transcode;
- playlistItem.Container = transcodingProfile.Container;
- playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
- playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
- playlistItem.AudioCodecs = transcodingProfile.AudioCodec.Split(',');
- playlistItem.VideoCodecs = transcodingProfile.VideoCodec.Split(',');
- playlistItem.CopyTimestamps = transcodingProfile.CopyTimestamps;
- playlistItem.EnableSubtitlesInManifest = transcodingProfile.EnableSubtitlesInManifest;
- playlistItem.BreakOnNonKeyFrames = transcodingProfile.BreakOnNonKeyFrames;
- if (transcodingProfile.MinSegments > 0)
- {
- playlistItem.MinSegments = transcodingProfile.MinSegments;
- }
- if (transcodingProfile.SegmentLength > 0)
- {
- playlistItem.SegmentLength = transcodingProfile.SegmentLength;
- }
- if (!string.IsNullOrEmpty(transcodingProfile.MaxAudioChannels))
- {
- int transcodingMaxAudioChannels;
- if (int.TryParse(transcodingProfile.MaxAudioChannels, NumberStyles.Any, CultureInfo.InvariantCulture, out transcodingMaxAudioChannels))
- {
- playlistItem.TranscodingMaxAudioChannels = transcodingMaxAudioChannels;
- }
- }
- playlistItem.SubProtocol = transcodingProfile.Protocol;
- playlistItem.AudioStreamIndex = audioStreamIndex;
- ConditionProcessor conditionProcessor = new ConditionProcessor();
- List<ProfileCondition> videoTranscodingConditions = new List<ProfileCondition>();
- foreach (CodecProfile i in options.Profile.CodecProfiles)
- {
- if (i.Type == CodecType.Video && i.ContainsCodec(transcodingProfile.VideoCodec, transcodingProfile.Container))
- {
- bool applyConditions = true;
- foreach (ProfileCondition applyCondition in i.ApplyConditions)
- {
- 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;
- if (!conditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, inputAudioBitrate, inputAudioSampleRate, inputAudioBitDepth, audioProfile, isSecondaryAudio))
- {
- LogConditionFailure(options.Profile, "AudioCodecProfile", applyCondition, item);
- applyConditions = false;
- break;
- }
- }
- if (applyConditions)
- {
- foreach (ProfileCondition c in i.Conditions)
- {
- videoTranscodingConditions.Add(c);
- }
- break;
- }
- }
- }
- ApplyTranscodingConditions(playlistItem, videoTranscodingConditions);
- List<ProfileCondition> audioTranscodingConditions = new List<ProfileCondition>();
- foreach (CodecProfile i in options.Profile.CodecProfiles)
- {
- if (i.Type == CodecType.VideoAudio && i.ContainsCodec(playlistItem.TargetAudioCodec, transcodingProfile.Container))
- {
- bool applyConditions = true;
- foreach (ProfileCondition applyCondition in i.ApplyConditions)
- {
- 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;
- 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))
- {
- LogConditionFailure(options.Profile, "VideoCodecProfile", applyCondition, item);
- applyConditions = false;
- break;
- }
- }
- if (applyConditions)
- {
- foreach (ProfileCondition c in i.Conditions)
- {
- audioTranscodingConditions.Add(c);
- }
- break;
- }
- }
- }
- ApplyTranscodingConditions(playlistItem, audioTranscodingConditions);
- // Honor requested max channels
- if (options.MaxAudioChannels.HasValue)
- {
- int currentValue = playlistItem.MaxAudioChannels ?? options.MaxAudioChannels.Value;
- playlistItem.MaxAudioChannels = Math.Min(options.MaxAudioChannels.Value, currentValue);
- }
- int audioBitrate = GetAudioBitrate(playlistItem.SubProtocol, options.GetMaxBitrate(false), playlistItem.TargetAudioChannels, playlistItem.TargetAudioCodec, audioStream);
- playlistItem.AudioBitrate = Math.Min(playlistItem.AudioBitrate ?? audioBitrate, audioBitrate);
- var maxBitrateSetting = options.GetMaxBitrate(false);
- // Honor max rate
- if (maxBitrateSetting.HasValue)
- {
- var videoBitrate = maxBitrateSetting.Value;
- if (playlistItem.AudioBitrate.HasValue)
- {
- videoBitrate -= playlistItem.AudioBitrate.Value;
- }
- // Make sure the video bitrate is lower than bitrate settings but at least 64k
- long currentValue = playlistItem.VideoBitrate ?? videoBitrate;
- var longBitrate = Math.Max(Math.Min(videoBitrate, currentValue), 64000);
- playlistItem.VideoBitrate = longBitrate > int.MaxValue ? int.MaxValue : Convert.ToInt32(longBitrate);
- }
- }
- playlistItem.TranscodeReasons = transcodeReasons;
- return playlistItem;
- }
- private int GetDefaultAudioBitrateIfUnknown(MediaStream audioStream)
- {
- if ((audioStream.Channels ?? 0) >= 6)
- {
- return 384000;
- }
- return 192000;
- }
- private int GetAudioBitrate(string subProtocol, long? maxTotalBitrate, int? targetAudioChannels, string targetAudioCodec, MediaStream audioStream)
- {
- int defaultBitrate = audioStream == null ? 192000 : audioStream.BitRate ?? GetDefaultAudioBitrateIfUnknown(audioStream);
- // Reduce the bitrate if we're downmixing
- if (targetAudioChannels.HasValue && audioStream != null && audioStream.Channels.HasValue && targetAudioChannels.Value < audioStream.Channels.Value)
- {
- defaultBitrate = StringHelper.EqualsIgnoreCase(targetAudioCodec, "ac3") ? 192000 : 128000;
- }
- if (StringHelper.EqualsIgnoreCase(subProtocol, "hls"))
- {
- defaultBitrate = Math.Min(384000, defaultBitrate);
- }
- else
- {
- defaultBitrate = Math.Min(448000, defaultBitrate);
- }
- 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;
- }
- }
- }
- if (maxTotalBitrate.HasValue)
- {
- if (maxTotalBitrate.Value < 640000)
- {
- defaultBitrate = Math.Min(128000, defaultBitrate);
- }
- }
- return Math.Min(defaultBitrate, encoderAudioBitrateLimit);
- }
- private Tuple<PlayMethod?, List<TranscodeReason>> GetVideoDirectPlayProfile(VideoOptions options,
- MediaSourceInfo mediaSource,
- MediaStream videoStream,
- MediaStream audioStream,
- bool isEligibleForDirectPlay,
- bool isEligibleForDirectStream)
- {
- DeviceProfile profile = options.Profile;
- if (options.ForceDirectPlay)
- {
- return new Tuple<PlayMethod?, List<TranscodeReason>>(PlayMethod.DirectPlay, new List<TranscodeReason>());
- }
- if (options.ForceDirectStream)
- {
- return new Tuple<PlayMethod?, List<TranscodeReason>>(PlayMethod.DirectStream, new List<TranscodeReason>());
- }
- if (videoStream == null)
- {
- _logger.Info("Profile: {0}, Cannot direct stream with no known video stream. Path: {1}",
- profile.Name ?? "Unknown Profile",
- mediaSource.Path ?? "Unknown path");
- return new Tuple<PlayMethod?, List<TranscodeReason>>(null, new List<TranscodeReason> { TranscodeReason.UnknownVideoStreamInfo });
- }
- // See if it can be direct played
- DirectPlayProfile directPlay = null;
- foreach (DirectPlayProfile i in profile.DirectPlayProfiles)
- {
- if (i.Type == DlnaProfileType.Video && IsVideoDirectPlaySupported(i, mediaSource, videoStream, audioStream))
- {
- directPlay = i;
- break;
- }
- }
- if (directPlay == null)
- {
- _logger.Info("Profile: {0}, No direct play profiles found for Path: {1}",
- profile.Name ?? "Unknown Profile",
- mediaSource.Path ?? "Unknown path");
- return new Tuple<PlayMethod?, List<TranscodeReason>>(null, GetTranscodeReasonsFromDirectPlayProfile(mediaSource, videoStream, audioStream, profile.DirectPlayProfiles));
- }
- string container = mediaSource.Container;
- List<ProfileCondition> conditions = new List<ProfileCondition>();
- foreach (ContainerProfile i in profile.ContainerProfiles)
- {
- if (i.Type == DlnaProfileType.Video &&
- i.ContainsContainer(container))
- {
- foreach (ProfileCondition c in i.Conditions)
- {
- conditions.Add(c);
- }
- }
- }
- ConditionProcessor conditionProcessor = new ConditionProcessor();
- 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;
- int? audioBitrate = audioStream == null ? null : audioStream.BitRate;
- int? audioChannels = audioStream == null ? null : audioStream.Channels;
- string audioProfile = audioStream == null ? null : audioStream.Profile;
- int? audioSampleRate = audioStream == null ? null : audioStream.SampleRate;
- int? audioBitDepth = audioStream == null ? null : audioStream.BitDepth;
- TransportStreamTimestamp? timestamp = videoStream == null ? TransportStreamTimestamp.None : mediaSource.Timestamp;
- int? packetLength = videoStream == null ? null : videoStream.PacketLength;
- int? refFrames = videoStream == null ? null : videoStream.RefFrames;
- int? numAudioStreams = mediaSource.GetStreamCount(MediaStreamType.Audio);
- int? numVideoStreams = mediaSource.GetStreamCount(MediaStreamType.Video);
- // Check container conditions
- foreach (ProfileCondition i in conditions)
- {
- if (!conditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
- {
- LogConditionFailure(profile, "VideoContainerProfile", i, mediaSource);
- var transcodeReason = GetTranscodeReasonForFailedCondition(i);
- var transcodeReasons = transcodeReason.HasValue
- ? new List<TranscodeReason> { transcodeReason.Value }
- : new List<TranscodeReason> { };
- return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
- }
- }
- string videoCodec = videoStream == null ? null : videoStream.Codec;
- if (string.IsNullOrEmpty(videoCodec))
- {
- _logger.Info("Profile: {0}, DirectPlay=false. Reason=Unknown video codec. Path: {1}",
- profile.Name ?? "Unknown Profile",
- mediaSource.Path ?? "Unknown path");
- return new Tuple<PlayMethod?, List<TranscodeReason>>(null, new List<TranscodeReason> { TranscodeReason.UnknownVideoStreamInfo });
- }
- conditions = new List<ProfileCondition>();
- foreach (CodecProfile i in profile.CodecProfiles)
- {
- if (i.Type == CodecType.Video && i.ContainsCodec(videoCodec, container))
- {
- bool applyConditions = true;
- foreach (ProfileCondition applyCondition in i.ApplyConditions)
- {
- if (!conditionProcessor.IsVideoConditionSatisfied(applyCondition, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
- {
- LogConditionFailure(profile, "VideoCodecProfile", applyCondition, mediaSource);
- applyConditions = false;
- break;
- }
- }
- if (applyConditions)
- {
- foreach (ProfileCondition c in i.Conditions)
- {
- conditions.Add(c);
- }
- }
- }
- }
- foreach (ProfileCondition i in conditions)
- {
- if (!conditionProcessor.IsVideoConditionSatisfied(i, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isInterlaced, refFrames, numVideoStreams, numAudioStreams, videoCodecTag, isAvc))
- {
- LogConditionFailure(profile, "VideoCodecProfile", i, mediaSource);
- var transcodeReason = GetTranscodeReasonForFailedCondition(i);
- var transcodeReasons = transcodeReason.HasValue
- ? new List<TranscodeReason> { transcodeReason.Value }
- : new List<TranscodeReason> { };
- return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
- }
- }
- if (audioStream != null)
- {
- string audioCodec = audioStream.Codec;
- if (string.IsNullOrEmpty(audioCodec))
- {
- _logger.Info("Profile: {0}, DirectPlay=false. Reason=Unknown audio codec. Path: {1}",
- profile.Name ?? "Unknown Profile",
- mediaSource.Path ?? "Unknown path");
- return new Tuple<PlayMethod?, List<TranscodeReason>>(null, new List<TranscodeReason> { TranscodeReason.UnknownAudioStreamInfo });
- }
- conditions = new List<ProfileCondition>();
- bool? isSecondaryAudio = audioStream == null ? null : mediaSource.IsSecondaryAudio(audioStream);
- foreach (CodecProfile i in profile.CodecProfiles)
- {
- if (i.Type == CodecType.VideoAudio && i.ContainsCodec(audioCodec, container))
- {
- bool applyConditions = true;
- foreach (ProfileCondition applyCondition in i.ApplyConditions)
- {
- if (!conditionProcessor.IsVideoAudioConditionSatisfied(applyCondition, audioChannels, audioBitrate, audioSampleRate, audioBitDepth, audioProfile, isSecondaryAudio))
- {
- LogConditionFailure(profile, "VideoAudioCodecProfile", applyCondition, mediaSource);
- applyConditions = false;
- break;
- }
- }
- if (applyConditions)
- {
- foreach (ProfileCondition c in i.Conditions)
- {
- conditions.Add(c);
- }
- }
- }
- }
- foreach (ProfileCondition i in conditions)
- {
- if (!conditionProcessor.IsVideoAudioConditionSatisfied(i, audioChannels, audioBitrate, audioSampleRate, audioBitDepth, audioProfile, isSecondaryAudio))
- {
- LogConditionFailure(profile, "VideoAudioCodecProfile", i, mediaSource);
- var transcodeReason = GetTranscodeReasonForFailedCondition(i);
- var transcodeReasons = transcodeReason.HasValue
- ? new List<TranscodeReason> { transcodeReason.Value }
- : new List<TranscodeReason> { };
- return new Tuple<PlayMethod?, List<TranscodeReason>>(null, transcodeReasons);
- }
- }
- }
- if (isEligibleForDirectStream && mediaSource.SupportsDirectStream)
- {
- return new Tuple<PlayMethod?, List<TranscodeReason>>(PlayMethod.DirectStream, new List<TranscodeReason>());
- }
- return new Tuple<PlayMethod?, List<TranscodeReason>>(null, new List<TranscodeReason> { TranscodeReason.ContainerBitrateExceedsLimit });
- }
- private void LogConditionFailure(DeviceProfile profile, string type, ProfileCondition condition, MediaSourceInfo mediaSource)
- {
- _logger.Info("Profile: {0}, DirectPlay=false. Reason={1}.{2} Condition: {3}. ConditionValue: {4}. IsRequired: {5}. Path: {6}",
- type,
- profile.Name ?? "Unknown Profile",
- condition.Property,
- condition.Condition,
- condition.Value ?? string.Empty,
- condition.IsRequired,
- mediaSource.Path ?? "Unknown path");
- }
- private Tuple<bool, TranscodeReason?> IsEligibleForDirectPlay(MediaSourceInfo item,
- long? maxBitrate,
- MediaStream subtitleStream,
- VideoOptions options,
- PlayMethod playMethod)
- {
- if (subtitleStream != null)
- {
- SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options.Profile.SubtitleProfiles, playMethod, null, null);
- if (subtitleProfile.Method != SubtitleDeliveryMethod.External && subtitleProfile.Method != SubtitleDeliveryMethod.Embed)
- {
- _logger.Info("Not eligible for {0} due to unsupported subtitles", playMethod);
- return new Tuple<bool, TranscodeReason?>(false, TranscodeReason.SubtitleCodecNotSupported);
- }
- }
- var result = IsAudioEligibleForDirectPlay(item, maxBitrate, playMethod);
- if (result)
- {
- return new Tuple<bool, TranscodeReason?>(result, null);
- }
- return new Tuple<bool, TranscodeReason?>(result, TranscodeReason.ContainerBitrateExceedsLimit);
- }
- public static SubtitleProfile GetSubtitleProfile(MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod, string transcodingSubProtocol, string transcodingContainer)
- {
- if (!subtitleStream.IsExternal && (playMethod != PlayMethod.Transcode || !string.Equals(transcodingSubProtocol, "hls", StringComparison.OrdinalIgnoreCase)))
- {
- // Look for supported embedded subs of the same 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 == MediaStream.IsTextFormat(profile.Format) && StringHelper.EqualsIgnoreCase(profile.Format, subtitleStream.Codec))
- {
- return profile;
- }
- }
- // 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;
- }
- }
- }
- // Look for an external or hls profile that matches the stream type (text/graphical) and doesn't require conversion
- return GetExternalSubtitleProfile(subtitleStream, subtitleProfiles, playMethod, false) ?? GetExternalSubtitleProfile(subtitleStream, subtitleProfiles, playMethod, true) ?? new SubtitleProfile
- {
- Method = SubtitleDeliveryMethod.Encode,
- Format = subtitleStream.Codec
- };
- }
- private static bool IsSubtitleEmbedSupported(MediaStream subtitleStream, SubtitleProfile subtitleProfile, string transcodingSubProtocol, string transcodingContainer)
- {
- if (string.Equals(transcodingContainer, "ts", StringComparison.OrdinalIgnoreCase))
- {
- return false;
- }
- if (string.Equals(transcodingContainer, "mpegts", StringComparison.OrdinalIgnoreCase))
- {
- return false;
- }
- if (string.Equals(transcodingContainer, "mp4", StringComparison.OrdinalIgnoreCase))
- {
- return false;
- }
- if (string.Equals(transcodingContainer, "mkv", StringComparison.OrdinalIgnoreCase))
- {
- return true;
- }
- return false;
- }
- private static SubtitleProfile GetExternalSubtitleProfile(MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, PlayMethod playMethod, bool allowConversion)
- {
- foreach (SubtitleProfile profile in subtitleProfiles)
- {
- 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 ((profile.Method == SubtitleDeliveryMethod.External && subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format)) ||
- (profile.Method == SubtitleDeliveryMethod.Hls && subtitleStream.IsTextSubtitleStream))
- {
- bool requiresConversion = !StringHelper.EqualsIgnoreCase(subtitleStream.Codec, profile.Format);
- if (!requiresConversion)
- {
- return profile;
- }
- if (!allowConversion)
- {
- continue;
- }
- if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsExternalStream && subtitleStream.SupportsSubtitleConversionTo(profile.Format))
- {
- return profile;
- }
- }
- }
- return null;
- }
- private bool IsAudioEligibleForDirectPlay(MediaSourceInfo item, long? maxBitrate, PlayMethod playMethod)
- {
- // Don't restrict by bitrate if coming from an external domain
- if (item.IsRemote)
- {
- return true;
- }
- if (!maxBitrate.HasValue)
- {
- _logger.Info("Cannot " + playMethod + " due to unknown supported bitrate");
- return false;
- }
- if (!item.Bitrate.HasValue)
- {
- _logger.Info("Cannot " + playMethod + " due to unknown content bitrate");
- return false;
- }
- if (item.Bitrate.Value > maxBitrate.Value)
- {
- _logger.Info("Bitrate exceeds " + playMethod + " limit: media bitrate: {0}, max bitrate: {1}", item.Bitrate.Value.ToString(CultureInfo.InvariantCulture), maxBitrate.Value.ToString(CultureInfo.InvariantCulture));
- return false;
- }
- return true;
- }
- 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)
- {
- foreach (ProfileCondition condition in conditions)
- {
- 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:
- {
- int num;
- if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
- {
- item.AudioBitrate = num;
- }
- break;
- }
- case ProfileConditionValue.AudioChannels:
- {
- int num;
- if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
- {
- item.MaxAudioChannels = num;
- }
- break;
- }
- case ProfileConditionValue.IsAvc:
- {
- 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;
- }
- case ProfileConditionValue.IsAnamorphic:
- {
- 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:
- {
- bool isInterlaced;
- if (bool.TryParse(value, out isInterlaced))
- {
- if (!isInterlaced && condition.Condition == ProfileConditionType.Equals)
- {
- item.DeInterlace = true;
- }
- else if (isInterlaced && condition.Condition == ProfileConditionType.NotEquals)
- {
- item.DeInterlace = true;
- }
- }
- break;
- }
- 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;
- }
- case ProfileConditionValue.RefFrames:
- {
- int num;
- if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
- {
- item.MaxRefFrames = num;
- }
- break;
- }
- case ProfileConditionValue.VideoBitDepth:
- {
- int num;
- if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
- {
- item.MaxVideoBitDepth = num;
- }
- break;
- }
- case ProfileConditionValue.VideoProfile:
- {
- item.VideoProfile = (value ?? string.Empty).Split('|')[0];
- break;
- }
- case ProfileConditionValue.Height:
- {
- int num;
- if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
- {
- item.MaxHeight = num;
- }
- break;
- }
- case ProfileConditionValue.VideoBitrate:
- {
- int num;
- if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
- {
- item.VideoBitrate = num;
- }
- break;
- }
- case ProfileConditionValue.VideoFramerate:
- {
- float num;
- if (float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
- {
- item.MaxFramerate = num;
- }
- break;
- }
- case ProfileConditionValue.VideoLevel:
- {
- int num;
- if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
- {
- item.VideoLevel = num;
- }
- break;
- }
- case ProfileConditionValue.Width:
- {
- int num;
- if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
- {
- item.MaxWidth = num;
- }
- break;
- }
- default:
- break;
- }
- }
- }
- private bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audioStream)
- {
- // Check container type
- if (!profile.SupportsContainer(item.Container))
- {
- return false;
- }
- // Check audio codec
- List<string> audioCodecs = profile.GetAudioCodecs();
- if (audioCodecs.Count > 0)
- {
- // Check audio codecs
- string audioCodec = audioStream == null ? null : audioStream.Codec;
- if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
- {
- return false;
- }
- }
- return true;
- }
- private bool IsVideoDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream)
- {
- // Check container type
- if (!profile.SupportsContainer(item.Container))
- {
- return false;
- }
- // Check video codec
- List<string> videoCodecs = profile.GetVideoCodecs();
- if (videoCodecs.Count > 0)
- {
- string videoCodec = videoStream == null ? null : videoStream.Codec;
- if (string.IsNullOrEmpty(videoCodec) || !ListHelper.ContainsIgnoreCase(videoCodecs, videoCodec))
- {
- return false;
- }
- }
- // Check audio codec
- List<string> audioCodecs = profile.GetAudioCodecs();
- if (audioCodecs.Count > 0)
- {
- // Check audio codecs
- string audioCodec = audioStream == null ? null : audioStream.Codec;
- if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
- {
- return false;
- }
- }
- return true;
- }
- }
- }
|