1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072 |
- 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;
- namespace MediaBrowser.Model.Dlna
- {
- public class StreamBuilder
- {
- private readonly ILocalPlayer _localPlayer;
- private readonly ILogger _logger;
- public StreamBuilder(ILocalPlayer localPlayer, ILogger logger)
- {
- _localPlayer = localPlayer;
- _logger = logger;
- }
- public StreamBuilder(ILogger logger)
- : this(new NullLocalPlayer(), 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);
- }
- 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);
- }
- private StreamInfo GetOptimalStream(List<StreamInfo> streams)
- {
- streams = StreamInfoSorter.SortMediaSources(streams);
- foreach (StreamInfo stream in streams)
- {
- return stream;
- }
- return null;
- }
- private StreamInfo BuildAudioItem(MediaSourceInfo item, AudioOptions options)
- {
- StreamInfo playlistItem = new StreamInfo
- {
- ItemId = options.ItemId,
- MediaType = DlnaProfileType.Audio,
- MediaSource = item,
- RunTimeTicks = item.RunTimeTicks,
- Context = options.Context,
- DeviceProfile = options.Profile
- };
- MediaStream audioStream = item.GetDefaultAudioStream(null);
- List<PlayMethod> directPlayMethods = GetAudioDirectPlayMethods(item, audioStream, options);
- if (directPlayMethods.Count > 0)
- {
- string audioCodec = audioStream == null ? null : audioStream.Codec;
- // Make sure audio codec profiles are satisfied
- if (!string.IsNullOrEmpty(audioCodec))
- {
- ConditionProcessor conditionProcessor = new ConditionProcessor();
- List<ProfileCondition> conditions = new List<ProfileCondition>();
- foreach (CodecProfile i in options.Profile.CodecProfiles)
- {
- if (i.Type == CodecType.Audio && i.ContainsCodec(audioCodec, item.Container))
- {
- foreach (ProfileCondition c in i.Conditions)
- {
- conditions.Add(c);
- }
- }
- }
- int? audioChannels = audioStream.Channels;
- int? audioBitrate = audioStream.BitRate;
- bool all = true;
- foreach (ProfileCondition c in conditions)
- {
- if (!conditionProcessor.IsAudioConditionSatisfied(c, audioChannels, audioBitrate))
- {
- LogConditionFailure(options.Profile, "AudioCodecProfile", c, item);
- all = false;
- break;
- }
- }
- if (all)
- {
- if (item.Protocol == MediaProtocol.File &&
- directPlayMethods.Contains(PlayMethod.DirectPlay) &&
- _localPlayer.CanAccessFile(item.Path))
- {
- playlistItem.PlayMethod = PlayMethod.DirectPlay;
- }
- else if (item.Protocol == MediaProtocol.Http &&
- directPlayMethods.Contains(PlayMethod.DirectPlay) &&
- _localPlayer.CanAccessUrl(item.Path, item.RequiredHttpHeaders.Count > 0))
- {
- playlistItem.PlayMethod = PlayMethod.DirectPlay;
- }
- else 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)
- {
- 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;
- playlistItem.AudioCodec = transcodingProfile.AudioCodec;
- 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)
- {
- 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);
- }
- int configuredBitrate = options.AudioTranscodingBitrate ??
- (options.Context == EncodingContext.Static ? options.Profile.MusicSyncBitrate : options.Profile.MusicStreamingTranscodingBitrate) ??
- 128000;
- playlistItem.AudioBitrate = Math.Min(configuredBitrate, playlistItem.AudioBitrate ?? configuredBitrate);
- }
- return playlistItem;
- }
- private int? GetBitrateForDirectPlayCheck(MediaSourceInfo item, AudioOptions options)
- {
- if (item.Protocol == MediaProtocol.File)
- {
- return options.Profile.MaxStaticBitrate;
- }
- return options.GetMaxBitrate();
- }
- private List<PlayMethod> GetAudioDirectPlayMethods(MediaSourceInfo item, MediaStream audioStream, AudioOptions options)
- {
- 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 && IsAudioEligibleForDirectPlay(item, options.GetMaxBitrate()))
- {
- playMethods.Add(PlayMethod.DirectStream);
- }
- // The profile describes what the device supports
- // If device requirements are satisfied then allow both direct stream and direct play
- if (item.SupportsDirectPlay &&
- IsAudioEligibleForDirectPlay(item, GetBitrateForDirectPlayCheck(item, options)))
- {
- playMethods.Add(PlayMethod.DirectPlay);
- }
- }
- else
- {
- _logger.Info("Profile: {0}, No direct play profiles found for Path: {1}",
- options.Profile.Name ?? "Unknown Profile",
- item.Path ?? "Unknown path");
- }
- return playMethods;
- }
- 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)
- {
- 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
- bool isEligibleForDirectPlay = IsEligibleForDirectPlay(item, GetBitrateForDirectPlayCheck(item, options), subtitleStream, options, PlayMethod.DirectPlay);
- bool isEligibleForDirectStream = IsEligibleForDirectPlay(item, options.GetMaxBitrate(), subtitleStream, options, PlayMethod.DirectStream);
- _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
- PlayMethod? directPlay = GetVideoDirectPlayProfile(options.Profile, item, videoStream, audioStream, isEligibleForDirectPlay, isEligibleForDirectStream);
- if (directPlay != null)
- {
- playlistItem.PlayMethod = directPlay.Value;
- playlistItem.Container = item.Container;
- if (subtitleStream != null)
- {
- SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options.Profile.SubtitleProfiles, options.Context, directPlay.Value);
- playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
- playlistItem.SubtitleFormat = subtitleProfile.Format;
- }
- return playlistItem;
- }
- }
- // 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, options.Context, PlayMethod.Transcode);
- playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
- playlistItem.SubtitleFormat = subtitleProfile.Format;
- }
- playlistItem.PlayMethod = PlayMethod.Transcode;
- playlistItem.Container = transcodingProfile.Container;
- playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
- playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
- // TODO: We should probably preserve the full list and sent it tp the server that way
- string[] supportedAudioCodecs = transcodingProfile.AudioCodec.Split(',');
- string inputAudioCodec = audioStream == null ? null : audioStream.Codec;
- foreach (string supportedAudioCodec in supportedAudioCodecs)
- {
- if (StringHelper.EqualsIgnoreCase(supportedAudioCodec, inputAudioCodec))
- {
- playlistItem.AudioCodec = supportedAudioCodec;
- break;
- }
- }
- if (string.IsNullOrEmpty(playlistItem.AudioCodec))
- {
- playlistItem.AudioCodec = supportedAudioCodecs[0];
- }
- playlistItem.VideoCodec = transcodingProfile.VideoCodec;
- playlistItem.CopyTimestamps = transcodingProfile.CopyTimestamps;
- playlistItem.SubProtocol = transcodingProfile.Protocol;
- playlistItem.AudioStreamIndex = audioStreamIndex;
- List<ProfileCondition> videoTranscodingConditions = new List<ProfileCondition>();
- foreach (CodecProfile i in options.Profile.CodecProfiles)
- {
- if (i.Type == CodecType.Video && i.ContainsCodec(transcodingProfile.VideoCodec, transcodingProfile.Container))
- {
- 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.AudioCodec, transcodingProfile.Container))
- {
- 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(options.GetMaxBitrate(), playlistItem.TargetAudioChannels, playlistItem.TargetAudioCodec, audioStream);
- playlistItem.AudioBitrate = Math.Min(playlistItem.AudioBitrate ?? audioBitrate, audioBitrate);
- int? maxBitrateSetting = options.GetMaxBitrate();
- // Honor max rate
- if (maxBitrateSetting.HasValue)
- {
- int 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
- int currentValue = playlistItem.VideoBitrate ?? videoBitrate;
- playlistItem.VideoBitrate = Math.Max(Math.Min(videoBitrate, currentValue), 64000);
- }
- }
- return playlistItem;
- }
- private int GetAudioBitrate(int? maxTotalBitrate, int? targetAudioChannels, string targetAudioCodec, MediaStream audioStream)
- {
- var defaultBitrate = 128000;
- if (targetAudioChannels.HasValue)
- {
- if (targetAudioChannels.Value >= 5 && (maxTotalBitrate ?? 0) >= 2000000)
- {
- defaultBitrate = 320000;
- }
- }
- 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;
- }
- }
- }
- return Math.Min(defaultBitrate, encoderAudioBitrateLimit);
- }
- private PlayMethod? GetVideoDirectPlayProfile(DeviceProfile profile,
- MediaSourceInfo mediaSource,
- MediaStream videoStream,
- MediaStream audioStream,
- bool isEligibleForDirectPlay,
- bool isEligibleForDirectStream)
- {
- 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 null;
- }
- // 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 null;
- }
- string container = mediaSource.Container;
- List<ProfileCondition> conditions = new List<ProfileCondition>();
- foreach (ContainerProfile i in profile.ContainerProfiles)
- {
- if (i.Type == DlnaProfileType.Video &&
- ListHelper.ContainsIgnoreCase(i.GetContainers(), 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? isCabac = videoStream == null ? null : videoStream.IsCabac;
- string videoCodecTag = videoStream == null ? null : videoStream.CodecTag;
- int? audioBitrate = audioStream == null ? null : audioStream.BitRate;
- int? audioChannels = audioStream == null ? null : audioStream.Channels;
- string audioProfile = audioStream == null ? null : audioStream.Profile;
- 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, isCabac, refFrames, numVideoStreams, numAudioStreams, videoCodecTag))
- {
- LogConditionFailure(profile, "VideoContainerProfile", i, mediaSource);
- return null;
- }
- }
- 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 null;
- }
- conditions = new List<ProfileCondition>();
- foreach (CodecProfile i in profile.CodecProfiles)
- {
- if (i.Type == CodecType.Video && i.ContainsCodec(videoCodec, container))
- {
- 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, isCabac, refFrames, numVideoStreams, numAudioStreams, videoCodecTag))
- {
- LogConditionFailure(profile, "VideoCodecProfile", i, mediaSource);
- return null;
- }
- }
- 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 null;
- }
- conditions = new List<ProfileCondition>();
- foreach (CodecProfile i in profile.CodecProfiles)
- {
- if (i.Type == CodecType.VideoAudio && i.ContainsCodec(audioCodec, container))
- {
- foreach (ProfileCondition c in i.Conditions)
- {
- conditions.Add(c);
- }
- }
- }
- foreach (ProfileCondition i in conditions)
- {
- bool? isSecondaryAudio = audioStream == null ? null : mediaSource.IsSecondaryAudio(audioStream);
- if (!conditionProcessor.IsVideoAudioConditionSatisfied(i, audioChannels, audioBitrate, audioProfile, isSecondaryAudio))
- {
- LogConditionFailure(profile, "VideoAudioCodecProfile", i, mediaSource);
- return null;
- }
- }
- }
- if (isEligibleForDirectPlay && mediaSource.SupportsDirectPlay)
- {
- if (mediaSource.Protocol == MediaProtocol.Http)
- {
- if (_localPlayer.CanAccessUrl(mediaSource.Path, mediaSource.RequiredHttpHeaders.Count > 0))
- {
- return PlayMethod.DirectPlay;
- }
- }
- else if (mediaSource.Protocol == MediaProtocol.File)
- {
- if (_localPlayer.CanAccessFile(mediaSource.Path))
- {
- return PlayMethod.DirectPlay;
- }
- }
- }
- if (isEligibleForDirectStream && mediaSource.SupportsDirectStream)
- {
- return PlayMethod.DirectStream;
- }
- return null;
- }
- 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 bool IsEligibleForDirectPlay(MediaSourceInfo item,
- int? maxBitrate,
- MediaStream subtitleStream,
- VideoOptions options,
- PlayMethod playMethod)
- {
- if (subtitleStream != null)
- {
- SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options.Profile.SubtitleProfiles, options.Context, playMethod);
- if (subtitleProfile.Method != SubtitleDeliveryMethod.External && subtitleProfile.Method != SubtitleDeliveryMethod.Embed)
- {
- _logger.Info("Not eligible for {0} due to unsupported subtitles", playMethod);
- return false;
- }
- }
- return IsAudioEligibleForDirectPlay(item, maxBitrate);
- }
- public static SubtitleProfile GetSubtitleProfile(MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, EncodingContext context, PlayMethod playMethod)
- {
- if (playMethod != PlayMethod.Transcode && !subtitleStream.IsExternal)
- {
- // Look for supported embedded subs
- foreach (SubtitleProfile profile in subtitleProfiles)
- {
- if (!profile.SupportsLanguage(subtitleStream.Language))
- {
- continue;
- }
- if (profile.Method != SubtitleDeliveryMethod.Embed)
- {
- continue;
- }
- if (subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format) && StringHelper.EqualsIgnoreCase(profile.Format, subtitleStream.Codec))
- {
- return profile;
- }
- }
- }
- // Look for an external profile that matches the stream type (text/graphical)
- foreach (SubtitleProfile profile in subtitleProfiles)
- {
- if (profile.Method != SubtitleDeliveryMethod.External && profile.Method != SubtitleDeliveryMethod.Hls)
- {
- 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 (subtitleStream.IsTextSubtitleStream || !requiresConversion)
- {
- if (subtitleStream.SupportsExternalStream)
- {
- return profile;
- }
- // For sync we can handle the longer extraction times
- if (context == EncodingContext.Static && subtitleStream.IsTextSubtitleStream)
- {
- return profile;
- }
- }
- }
- }
- return new SubtitleProfile
- {
- Method = SubtitleDeliveryMethod.Encode,
- Format = subtitleStream.Codec
- };
- }
- private bool IsAudioEligibleForDirectPlay(MediaSourceInfo item, int? maxBitrate)
- {
- if (!maxBitrate.HasValue || (item.Bitrate.HasValue && item.Bitrate.Value <= maxBitrate.Value))
- {
- return true;
- }
- _logger.Info("Bitrate exceeds DirectPlay limit");
- return false;
- }
- 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 (IntHelper.TryParseCultureInvariant(value, out num))
- {
- item.AudioBitrate = num;
- }
- break;
- }
- case ProfileConditionValue.AudioChannels:
- {
- int num;
- if (IntHelper.TryParseCultureInvariant(value, out num))
- {
- item.MaxAudioChannels = num;
- }
- break;
- }
- case ProfileConditionValue.IsCabac:
- {
- bool val;
- if (BoolHelper.TryParseCultureInvariant(value, out val))
- {
- if (condition.Condition == ProfileConditionType.Equals)
- {
- item.Cabac = val;
- }
- else if (condition.Condition == ProfileConditionType.NotEquals)
- {
- item.Cabac = !val;
- }
- }
- break;
- }
- case ProfileConditionValue.IsAnamorphic:
- 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 (IntHelper.TryParseCultureInvariant(value, out num))
- {
- item.MaxRefFrames = num;
- }
- break;
- }
- case ProfileConditionValue.VideoBitDepth:
- {
- int num;
- if (IntHelper.TryParseCultureInvariant(value, out num))
- {
- item.MaxVideoBitDepth = num;
- }
- break;
- }
- case ProfileConditionValue.VideoProfile:
- {
- item.VideoProfile = (value ?? string.Empty).Split('|')[0];
- break;
- }
- case ProfileConditionValue.Height:
- {
- int num;
- if (IntHelper.TryParseCultureInvariant(value, out num))
- {
- item.MaxHeight = num;
- }
- break;
- }
- case ProfileConditionValue.VideoBitrate:
- {
- int num;
- if (IntHelper.TryParseCultureInvariant(value, out num))
- {
- item.VideoBitrate = num;
- }
- break;
- }
- case ProfileConditionValue.VideoFramerate:
- {
- float num;
- if (FloatHelper.TryParseCultureInvariant(value, out num))
- {
- item.MaxFramerate = num;
- }
- break;
- }
- case ProfileConditionValue.VideoLevel:
- {
- int num;
- if (IntHelper.TryParseCultureInvariant(value, out num))
- {
- item.VideoLevel = num;
- }
- break;
- }
- case ProfileConditionValue.Width:
- {
- int num;
- if (IntHelper.TryParseCultureInvariant(value, out num))
- {
- item.MaxWidth = num;
- }
- break;
- }
- }
- }
- }
- private bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audioStream)
- {
- if (profile.Container.Length > 0)
- {
- // Check container type
- string mediaContainer = item.Container ?? string.Empty;
- bool any = false;
- foreach (string i in profile.GetContainers())
- {
- if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
- {
- any = true;
- break;
- }
- }
- if (!any)
- {
- return false;
- }
- }
- return true;
- }
- private bool IsVideoDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream videoStream, MediaStream audioStream)
- {
- if (profile.Container.Length > 0)
- {
- // Check container type
- string mediaContainer = item.Container ?? string.Empty;
- bool any = false;
- foreach (string i in profile.GetContainers())
- {
- if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
- {
- any = true;
- break;
- }
- }
- if (!any)
- {
- 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;
- }
- }
- 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;
- }
- }
- }
|