jellyfin/MediaBrowser.Model/Dlna/StreamBuilder.cs

907 lines
35 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;
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;
2014-04-01 22:23:07 +00:00
namespace MediaBrowser.Model.Dlna
{
public class StreamBuilder
{
private readonly ILocalPlayer _localPlayer;
public StreamBuilder(ILocalPlayer localPlayer)
{
_localPlayer = localPlayer;
}
public StreamBuilder()
: this(new NullLocalPlayer())
{
}
2014-04-01 22:23:07 +00:00
public StreamInfo BuildAudioItem(AudioOptions options)
{
ValidateAudioInput(options);
2014-04-03 22:50:04 +00:00
2015-03-16 01:48:25 +00:00
List<MediaSourceInfo> mediaSources = new List<MediaSourceInfo>();
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
}
2014-05-08 21:23:24 +00:00
List<StreamInfo> streams = new List<StreamInfo>();
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;
}
return GetOptimalStream(streams);
}
public StreamInfo BuildVideoItem(VideoOptions options)
{
ValidateInput(options);
2015-03-16 01:48:25 +00:00
List<MediaSourceInfo> mediaSources = new List<MediaSourceInfo>();
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
}
2014-05-08 21:23:24 +00:00
List<StreamInfo> streams = new List<StreamInfo>();
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;
}
return GetOptimalStream(streams);
}
private StreamInfo GetOptimalStream(List<StreamInfo> streams)
{
// Grab the first one that can be direct streamed
// If that doesn't produce anything, just take the first
2014-05-08 21:23:24 +00:00
foreach (StreamInfo i in streams)
2015-03-23 17:19:21 +00:00
{
if (i.PlayMethod == PlayMethod.DirectPlay && i.MediaSource.Protocol == MediaProtocol.File)
{
return i;
}
}
foreach (StreamInfo i in streams)
2014-05-08 21:23:24 +00:00
{
2014-10-12 01:46:02 +00:00
if (i.PlayMethod == PlayMethod.DirectPlay)
{
return i;
}
}
foreach (StreamInfo i in streams)
{
if (i.PlayMethod == PlayMethod.DirectStream)
2014-05-08 21:23:24 +00:00
{
return i;
}
}
foreach (StreamInfo stream in streams)
{
return stream;
}
2015-03-02 18:48:21 +00:00
return null;
2014-04-01 22:23:07 +00:00
}
2014-04-06 17:53:23 +00:00
private StreamInfo BuildAudioItem(MediaSourceInfo item, AudioOptions options)
2014-04-01 22:23:07 +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
};
2015-03-23 17:19:21 +00:00
MediaStream audioStream = item.GetDefaultAudioStream(null);
List<PlayMethod> directPlayMethods = GetAudioDirectPlayMethods(item, audioStream, options);
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
{
2015-03-13 01:55:22 +00:00
ConditionProcessor conditionProcessor = new ConditionProcessor();
2014-04-06 17:53:23 +00:00
2015-03-13 01:55:22 +00:00
List<ProfileCondition> conditions = new List<ProfileCondition>();
foreach (CodecProfile i in options.Profile.CodecProfiles)
2014-04-06 17:53:23 +00:00
{
2015-03-13 01:55:22 +00:00
if (i.Type == CodecType.Audio && i.ContainsCodec(audioCodec))
2014-05-09 19:43:06 +00:00
{
2015-03-13 01:55:22 +00:00
foreach (ProfileCondition c in i.Conditions)
2014-07-01 04:06:28 +00:00
{
2015-03-13 01:55:22 +00:00
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
int? audioChannels = audioStream.Channels;
int? audioBitrate = audioStream.BitRate;
2014-04-24 05:08:10 +00:00
2015-03-13 01:55:22 +00:00
bool all = true;
foreach (ProfileCondition c in conditions)
{
if (!conditionProcessor.IsAudioConditionSatisfied(c, audioChannels, audioBitrate))
2014-05-09 19:43:06 +00:00
{
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 (item.Protocol == MediaProtocol.File &&
directPlayMethods.Contains(PlayMethod.DirectPlay) &&
_localPlayer.CanAccessFile(item.Path))
2014-04-24 05:08:10 +00:00
{
2015-03-13 01:55:22 +00:00
playlistItem.PlayMethod = PlayMethod.DirectPlay;
}
2015-03-14 18:54:53 +00:00
else if (item.Protocol == MediaProtocol.Http &&
directPlayMethods.Contains(PlayMethod.DirectPlay) &&
_localPlayer.CanAccessUrl(item.Path, item.RequiredHttpHeaders.Count > 0))
{
playlistItem.PlayMethod = PlayMethod.DirectPlay;
}
2015-03-13 01:55:22 +00:00
else if (directPlayMethods.Contains(PlayMethod.DirectStream))
{
playlistItem.PlayMethod = PlayMethod.DirectStream;
}
2015-03-13 01:55:22 +00:00
playlistItem.Container = item.Container;
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
{
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;
}
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;
playlistItem.AudioCodec = transcodingProfile.AudioCodec;
playlistItem.SubProtocol = transcodingProfile.Protocol;
2014-04-01 22:23:07 +00:00
2014-05-09 23:08:08 +00:00
List<CodecProfile> audioCodecProfiles = new List<CodecProfile>();
foreach (CodecProfile i in options.Profile.CodecProfiles)
{
if (i.Type == CodecType.Audio && i.ContainsCodec(transcodingProfile.AudioCodec))
{
audioCodecProfiles.Add(i);
}
if (audioCodecProfiles.Count >= 1) break;
}
List<ProfileCondition> audioTranscodingConditions = new List<ProfileCondition>();
foreach (CodecProfile i in audioCodecProfiles)
2014-07-01 04:06:28 +00:00
{
2014-09-07 15:52:25 +00:00
foreach (ProfileCondition c in i.Conditions)
2014-07-01 04:06:28 +00:00
{
audioTranscodingConditions.Add(c);
}
}
2014-04-01 22:23:07 +00:00
ApplyTranscodingConditions(playlistItem, audioTranscodingConditions);
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);
}
2014-09-07 15:52:25 +00:00
int configuredBitrate = options.AudioTranscodingBitrate ??
2014-08-14 13:24:30 +00:00
(options.Context == EncodingContext.Static ? options.Profile.MusicSyncBitrate : options.Profile.MusicStreamingTranscodingBitrate) ??
128000;
playlistItem.AudioBitrate = Math.Min(configuredBitrate, playlistItem.AudioBitrate ?? configuredBitrate);
2014-04-01 22:23:07 +00:00
}
return playlistItem;
}
2015-03-23 17:19:21 +00:00
private List<PlayMethod> GetAudioDirectPlayMethods(MediaSourceInfo item, MediaStream audioStream, AudioOptions options)
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;
}
}
List<PlayMethod> playMethods = new List<PlayMethod>();
if (directPlayProfile != null)
{
// While options takes the network and other factors into account. Only applies to direct stream
2015-03-17 03:51:35 +00:00
if (item.SupportsDirectStream && IsAudioEligibleForDirectPlay(item, options.GetMaxBitrate()))
2015-03-13 01:55:22 +00:00
{
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 (IsAudioEligibleForDirectPlay(item, options.Profile.MaxStaticBitrate))
{
playMethods.Add(PlayMethod.DirectPlay);
}
}
return playMethods;
}
2014-04-01 22:23:07 +00:00
private StreamInfo BuildVideoItem(MediaSourceInfo item, VideoOptions options)
{
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
};
playlistItem.SubtitleStreamIndex = options.SubtitleStreamIndex ?? item.DefaultSubtitleStreamIndex;
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);
int? audioStreamIndex = audioStream == null ? (int?)null : audioStream.Index;
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
bool isEligibleForDirectPlay = IsEligibleForDirectPlay(item, options.Profile.MaxStaticBitrate, subtitleStream, options);
bool isEligibleForDirectStream = IsEligibleForDirectPlay(item, options.GetMaxBitrate(), subtitleStream, options);
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
2015-03-23 20:06:06 +00:00
PlayMethod? directPlay = GetVideoDirectPlayProfile(options.Profile, item, videoStream, audioStream, isEligibleForDirectPlay, isEligibleForDirectStream);
2014-04-01 22:23:07 +00:00
if (directPlay != null)
{
2014-10-12 01:46:02 +00:00
playlistItem.PlayMethod = directPlay.Value;
2014-04-24 05:08:10 +00:00
playlistItem.Container = item.Container;
2014-04-01 22:23:07 +00:00
if (subtitleStream != null)
{
2015-03-23 17:19:21 +00:00
SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options.Profile.SubtitleProfiles, options.Context);
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
}
}
// 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)
{
2015-03-23 17:19:21 +00:00
SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options.Profile.SubtitleProfiles, options.Context);
2015-01-15 05:54:42 +00:00
playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
playlistItem.SubtitleFormat = 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;
2014-05-08 21:23:24 +00:00
playlistItem.AudioCodec = transcodingProfile.AudioCodec.Split(',')[0];
2014-04-01 22:23:07 +00:00
playlistItem.VideoCodec = transcodingProfile.VideoCodec;
playlistItem.SubProtocol = transcodingProfile.Protocol;
playlistItem.AudioStreamIndex = audioStreamIndex;
2014-04-01 22:23:07 +00:00
2014-05-09 23:08:08 +00:00
List<ProfileCondition> videoTranscodingConditions = new List<ProfileCondition>();
foreach (CodecProfile i in options.Profile.CodecProfiles)
{
if (i.Type == CodecType.Video && i.ContainsCodec(transcodingProfile.VideoCodec))
{
2014-09-07 15:52:25 +00:00
foreach (ProfileCondition c in i.Conditions)
2014-07-01 04:06:28 +00:00
{
videoTranscodingConditions.Add(c);
}
2014-05-09 23:08:08 +00:00
break;
}
}
2014-04-01 22:23:07 +00:00
ApplyTranscodingConditions(playlistItem, videoTranscodingConditions);
2014-05-09 23:08:08 +00:00
List<ProfileCondition> audioTranscodingConditions = new List<ProfileCondition>();
foreach (CodecProfile i in options.Profile.CodecProfiles)
{
if (i.Type == CodecType.VideoAudio && i.ContainsCodec(transcodingProfile.AudioCodec))
{
2014-09-07 15:52:25 +00:00
foreach (ProfileCondition c in i.Conditions)
2014-07-01 04:06:28 +00:00
{
audioTranscodingConditions.Add(c);
}
2014-05-09 23:08:08 +00:00
break;
}
}
2014-04-01 22:23:07 +00:00
ApplyTranscodingConditions(playlistItem, audioTranscodingConditions);
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);
}
if (!playlistItem.AudioBitrate.HasValue)
2014-04-06 17:53:23 +00:00
{
playlistItem.AudioBitrate = GetAudioBitrate(playlistItem.TargetAudioChannels, playlistItem.TargetAudioCodec);
2014-04-06 17:53:23 +00:00
}
2015-03-23 20:06:06 +00:00
int? maxBitrateSetting = options.GetMaxBitrate();
2014-04-06 17:53:23 +00:00
// Honor max rate
if (maxBitrateSetting.HasValue)
2014-04-06 17:53:23 +00:00
{
2014-05-08 20:09:53 +00:00
int videoBitrate = maxBitrateSetting.Value;
2014-04-06 17:53:23 +00:00
if (playlistItem.AudioBitrate.HasValue)
{
videoBitrate -= playlistItem.AudioBitrate.Value;
}
2014-05-08 20:09:53 +00:00
int currentValue = playlistItem.VideoBitrate ?? videoBitrate;
2014-04-06 17:53:23 +00:00
playlistItem.VideoBitrate = Math.Min(videoBitrate, currentValue);
}
}
2014-06-23 16:05:19 +00:00
return playlistItem;
}
private int GetAudioBitrate(int? channels, string codec)
{
if (channels.HasValue)
{
if (channels.Value >= 5)
2014-06-23 16:05:19 +00:00
{
return 320000;
2014-06-23 16:05:19 +00:00
}
2014-04-01 22:23:07 +00:00
}
return 128000;
2014-04-01 22:23:07 +00:00
}
2015-03-23 20:06:06 +00:00
private PlayMethod? GetVideoDirectPlayProfile(DeviceProfile profile,
2014-04-24 05:08:10 +00:00
MediaSourceInfo mediaSource,
MediaStream videoStream,
2015-03-23 20:06:06 +00:00
MediaStream audioStream,
bool isEligibleForDirectPlay,
bool isEligibleForDirectStream)
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)
{
return null;
}
2014-05-08 20:09:53 +00:00
string container = mediaSource.Container;
2014-04-24 05:08:10 +00:00
2014-05-09 19:43:06 +00:00
List<ProfileCondition> conditions = new List<ProfileCondition>();
foreach (ContainerProfile i in profile.ContainerProfiles)
{
if (i.Type == DlnaProfileType.Video &&
2014-06-05 02:32:40 +00:00
ListHelper.ContainsIgnoreCase(i.GetContainers(), 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;
2014-10-23 04:26:01 +00:00
bool? isCabac = videoStream == null ? null : videoStream.IsCabac;
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;
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
// Check container conditions
2014-05-09 19:43:06 +00:00
foreach (ProfileCondition i in conditions)
2014-04-24 05:08:10 +00:00
{
2014-10-23 04:26:01 +00:00
if (!conditionProcessor.IsVideoConditionSatisfied(i, audioBitrate, audioChannels, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isCabac, refFrames))
2014-05-09 19:43:06 +00:00
{
return null;
}
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
if (string.IsNullOrEmpty(videoCodec))
{
return null;
}
2014-05-09 19:43:06 +00:00
conditions = new List<ProfileCondition>();
foreach (CodecProfile i in profile.CodecProfiles)
{
if (i.Type == CodecType.Video && i.ContainsCodec(videoCodec))
2014-07-01 04:06:28 +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-09 19:43:06 +00:00
foreach (ProfileCondition i in conditions)
2014-04-24 05:08:10 +00:00
{
2014-10-23 04:26:01 +00:00
if (!conditionProcessor.IsVideoConditionSatisfied(i, audioBitrate, audioChannels, width, height, bitDepth, videoBitrate, videoProfile, videoLevel, videoFramerate, packetLength, timestamp, isAnamorphic, isCabac, refFrames))
2014-05-09 19:43:06 +00:00
{
return null;
}
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
if (string.IsNullOrEmpty(audioCodec))
{
return null;
}
2014-05-09 19:43:06 +00:00
conditions = new List<ProfileCondition>();
foreach (CodecProfile i in profile.CodecProfiles)
{
if (i.Type == CodecType.VideoAudio && i.ContainsCodec(audioCodec))
2014-07-01 04:06:28 +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-09 19:43:06 +00:00
foreach (ProfileCondition i in conditions)
2014-04-24 05:08:10 +00:00
{
2014-05-09 19:43:06 +00:00
if (!conditionProcessor.IsVideoAudioConditionSatisfied(i, audioChannels, audioBitrate, audioProfile))
{
return null;
}
2014-04-24 05:08:10 +00:00
}
}
2015-03-23 20:06:06 +00:00
if (isEligibleForDirectPlay)
2014-10-12 01:46:02 +00:00
{
2015-03-23 20:06:06 +00:00
if (mediaSource.Protocol == MediaProtocol.Http)
2014-10-12 01:46:02 +00:00
{
2015-03-23 20:06:06 +00:00
if (_localPlayer.CanAccessUrl(mediaSource.Path, mediaSource.RequiredHttpHeaders.Count > 0))
{
return PlayMethod.DirectPlay;
}
2014-10-12 01:46:02 +00:00
}
2015-03-23 20:06:06 +00:00
else if (mediaSource.Protocol == MediaProtocol.File)
2015-03-14 18:54:53 +00:00
{
2015-03-23 20:06:06 +00:00
if (_localPlayer.CanAccessFile(mediaSource.Path))
{
return PlayMethod.DirectPlay;
}
2015-03-14 18:54:53 +00:00
}
2014-10-12 01:46:02 +00:00
}
2015-03-17 03:51:35 +00:00
2015-03-23 20:06:06 +00:00
if (isEligibleForDirectStream)
2015-03-17 03:51:35 +00:00
{
2015-03-23 20:06:06 +00:00
if (mediaSource.SupportsDirectStream)
{
return PlayMethod.DirectStream;
}
2015-03-17 03:51:35 +00:00
}
2015-03-23 20:06:06 +00:00
return null;
2014-04-24 05:08:10 +00:00
}
private bool IsEligibleForDirectPlay(MediaSourceInfo item,
int? maxBitrate,
MediaStream subtitleStream,
VideoOptions options)
2014-04-01 22:23:07 +00:00
{
if (subtitleStream != null)
2014-04-01 22:23:07 +00:00
{
2015-03-23 17:19:21 +00:00
SubtitleProfile subtitleProfile = GetSubtitleProfile(subtitleStream, options.Profile.SubtitleProfiles, options.Context);
2015-01-15 05:54:42 +00:00
if (subtitleProfile.Method != SubtitleDeliveryMethod.External && subtitleProfile.Method != SubtitleDeliveryMethod.Embed)
{
return false;
}
2014-04-01 22:23:07 +00:00
}
return IsAudioEligibleForDirectPlay(item, maxBitrate);
2014-04-06 17:53:23 +00:00
}
2015-03-23 17:19:21 +00:00
public static SubtitleProfile GetSubtitleProfile(MediaStream subtitleStream, SubtitleProfile[] subtitleProfiles, EncodingContext context)
{
2015-02-02 18:14:02 +00:00
// Look for an external profile that matches the stream type (text/graphical)
2015-03-23 17:19:21 +00:00
foreach (SubtitleProfile profile in subtitleProfiles)
{
2015-02-27 17:43:06 +00:00
if (profile.Method == SubtitleDeliveryMethod.External && subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format))
{
2015-02-27 17:43:06 +00:00
if (subtitleStream.SupportsExternalStream)
{
return profile;
}
2015-02-27 17:43:06 +00:00
// For sync we can handle the longer extraction times
if (context == EncodingContext.Static && subtitleStream.IsTextSubtitleStream)
{
return profile;
}
}
2015-02-02 18:14:02 +00:00
}
2015-03-23 17:19:21 +00:00
foreach (SubtitleProfile profile in subtitleProfiles)
2015-02-02 18:14:02 +00:00
{
2015-02-05 05:29:37 +00:00
if (profile.Method == SubtitleDeliveryMethod.Embed && subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format))
{
2015-02-05 05:29:37 +00:00
return profile;
}
}
2015-01-15 05:54:42 +00:00
return new SubtitleProfile
{
2015-01-16 20:54:37 +00:00
Method = SubtitleDeliveryMethod.Encode,
2015-01-15 05:54:42 +00:00
Format = subtitleStream.Codec
};
}
private bool IsAudioEligibleForDirectPlay(MediaSourceInfo item, int? maxBitrate)
2014-04-06 17:53:23 +00:00
{
// Honor the max bitrate setting
return !maxBitrate.HasValue || (item.Bitrate.HasValue && item.Bitrate.Value <= maxBitrate.Value);
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)
{
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:
{
int num;
if (IntHelper.TryParseCultureInvariant(value, out num))
2014-04-01 22:23:07 +00:00
{
item.AudioBitrate = num;
}
break;
}
case ProfileConditionValue.AudioChannels:
{
int num;
if (IntHelper.TryParseCultureInvariant(value, out num))
2014-04-01 22:23:07 +00:00
{
item.MaxAudioChannels = num;
}
break;
}
2014-11-12 04:51:40 +00:00
case ProfileConditionValue.IsCabac:
{
bool val;
if (BoolHelper.TryParseCultureInvariant(value, out val))
{
2014-12-14 20:01:26 +00:00
if (condition.Condition == ProfileConditionType.Equals)
{
item.Cabac = val;
}
else if (condition.Condition == ProfileConditionType.NotEquals)
{
item.Cabac = !val;
}
2014-11-12 04:51:40 +00:00
}
break;
}
2014-06-22 16:25:47 +00:00
case ProfileConditionValue.IsAnamorphic:
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.VideoTimestamp:
2014-04-01 22:23:07 +00:00
{
// Not supported yet
break;
}
2014-09-23 04:05:29 +00:00
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;
}
2014-04-24 05:08:10 +00:00
case ProfileConditionValue.VideoProfile:
{
2014-10-20 03:04:45 +00:00
item.VideoProfile = (value ?? string.Empty).Split('|')[0];
2014-04-24 05:08:10 +00:00
break;
}
2014-04-01 22:23:07 +00:00
case ProfileConditionValue.Height:
{
int num;
if (IntHelper.TryParseCultureInvariant(value, out num))
2014-04-01 22:23:07 +00:00
{
item.MaxHeight = num;
}
break;
}
case ProfileConditionValue.VideoBitrate:
{
int num;
if (IntHelper.TryParseCultureInvariant(value, out num))
2014-04-01 22:23:07 +00:00
{
item.VideoBitrate = num;
}
break;
}
case ProfileConditionValue.VideoFramerate:
{
2014-06-23 16:05:19 +00:00
float num;
if (FloatHelper.TryParseCultureInvariant(value, out num))
2014-04-01 22:23:07 +00:00
{
item.MaxFramerate = num;
}
break;
}
case ProfileConditionValue.VideoLevel:
{
int num;
if (IntHelper.TryParseCultureInvariant(value, out num))
2014-04-01 22:23:07 +00:00
{
item.VideoLevel = num;
}
break;
}
case ProfileConditionValue.Width:
{
int num;
if (IntHelper.TryParseCultureInvariant(value, out num))
2014-04-01 22:23:07 +00:00
{
item.MaxWidth = num;
}
break;
}
default:
throw new ArgumentException("Unrecognized ProfileConditionValue");
}
}
}
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
{
if (profile.Container.Length > 0)
{
// Check container type
2014-05-08 20:09:53 +00:00
string mediaContainer = item.Container ?? string.Empty;
2014-05-09 19:43:06 +00:00
bool any = false;
foreach (string i in profile.GetContainers())
{
if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
2014-05-09 19:43:06 +00:00
{
any = true;
break;
}
}
if (!any)
2014-04-01 22:23:07 +00:00
{
return false;
}
}
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
{
if (profile.Container.Length > 0)
{
// Check container type
2014-05-08 20:09:53 +00:00
string mediaContainer = item.Container ?? string.Empty;
2014-05-09 19:43:06 +00:00
bool any = false;
foreach (string i in profile.GetContainers())
{
if (StringHelper.EqualsIgnoreCase(i, mediaContainer))
2014-05-09 19:43:06 +00:00
{
any = true;
break;
}
}
if (!any)
2014-04-01 22:23:07 +00:00
{
return false;
}
}
// Check video codec
2014-05-08 20:09:53 +00:00
List<string> videoCodecs = profile.GetVideoCodecs();
2014-04-01 22:23:07 +00:00
if (videoCodecs.Count > 0)
{
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;
}
}
2014-05-08 20:09:53 +00:00
List<string> audioCodecs = profile.GetAudioCodecs();
2014-04-01 22:23:07 +00:00
if (audioCodecs.Count > 0)
{
// Check audio codecs
2014-05-08 20:09:53 +00:00
string audioCodec = audioStream == null ? null : audioStream.Codec;
2014-06-05 02:32:40 +00:00
if (string.IsNullOrEmpty(audioCodec) || !ListHelper.ContainsIgnoreCase(audioCodecs, audioCodec))
2014-04-01 22:23:07 +00:00
{
return false;
}
}
return true;
}
}
}