jellyfin/MediaBrowser.Dlna/PlayTo/PlaylistItemFactory.cs

483 lines
20 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Dlna;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
2014-04-01 22:23:07 +00:00
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Entities;
using System;
2014-03-23 05:10:33 +00:00
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
namespace MediaBrowser.Dlna.PlayTo
{
public class PlaylistItemFactory
{
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
2014-03-23 05:10:33 +00:00
public PlaylistItem Create(Audio item, List<MediaStream> mediaStreams, DeviceProfile profile)
{
var playlistItem = new PlaylistItem
{
ItemId = item.Id.ToString("N"),
MediaType = DlnaProfileType.Audio
};
var audioStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);
2014-03-24 17:54:45 +00:00
var directPlay = profile.DirectPlayProfiles
.FirstOrDefault(i => i.Type == playlistItem.MediaType && IsSupported(i, item, audioStream));
if (directPlay != null)
{
2014-03-24 17:54:45 +00:00
var audioCodec = audioStream == null ? null : audioStream.Codec;
2014-03-24 17:54:45 +00:00
// Make sure audio codec profiles are satisfied
if (!string.IsNullOrEmpty(audioCodec) && profile.CodecProfiles.Where(i => i.Type == CodecType.Audio && i.ContainsCodec(audioCodec))
2014-03-24 17:54:45 +00:00
.All(i => AreConditionsSatisfied(i.Conditions, item.Path, null, audioStream)))
2014-03-23 00:48:34 +00:00
{
playlistItem.Transcode = false;
playlistItem.Container = Path.GetExtension(item.Path);
return playlistItem;
}
}
var transcodingProfile = profile.TranscodingProfiles
.FirstOrDefault(i => i.Type == playlistItem.MediaType && IsSupported(profile, i, item));
if (transcodingProfile != null)
{
playlistItem.Transcode = true;
2014-03-22 19:37:15 +00:00
playlistItem.Container = "." + transcodingProfile.Container.TrimStart('.');
2014-03-23 16:42:02 +00:00
playlistItem.AudioCodec = transcodingProfile.AudioCodec;
2014-03-24 17:54:45 +00:00
var audioTranscodingConditions = profile.CodecProfiles
.Where(i => i.Type == CodecType.Audio && i.ContainsCodec(transcodingProfile.AudioCodec))
2014-03-24 17:54:45 +00:00
.Take(1)
.SelectMany(i => i.Conditions);
ApplyTranscodingConditions(playlistItem, audioTranscodingConditions);
}
2014-03-24 17:54:45 +00:00
return playlistItem;
}
public PlaylistItem Create(Photo item, DeviceProfile profile)
{
var playlistItem = new PlaylistItem
{
ItemId = item.Id.ToString("N"),
MediaType = DlnaProfileType.Photo
};
var directPlay = profile.DirectPlayProfiles
.FirstOrDefault(i => i.Type == playlistItem.MediaType && IsSupported(i, item));
if (directPlay != null)
{
playlistItem.Transcode = false;
2014-03-22 19:37:15 +00:00
playlistItem.Container = Path.GetExtension(item.Path);
return playlistItem;
}
var transcodingProfile = profile.TranscodingProfiles
.FirstOrDefault(i => i.Type == playlistItem.MediaType && IsSupported(profile, i, item));
if (transcodingProfile != null)
{
playlistItem.Transcode = true;
2014-03-22 19:37:15 +00:00
playlistItem.Container = "." + transcodingProfile.Container.TrimStart('.');
}
2014-03-24 17:54:45 +00:00
return playlistItem;
}
2014-03-23 05:10:33 +00:00
public PlaylistItem Create(Video item, List<MediaStream> mediaStreams, DeviceProfile profile)
{
var playlistItem = new PlaylistItem
{
ItemId = item.Id.ToString("N"),
MediaType = DlnaProfileType.Video
};
var audioStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);
var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
2014-03-24 17:54:45 +00:00
var directPlay = profile.DirectPlayProfiles
.FirstOrDefault(i => i.Type == playlistItem.MediaType && IsSupported(i, item, videoStream, audioStream));
if (directPlay != null)
{
2014-03-24 17:54:45 +00:00
var videoCodec = videoStream == null ? null : videoStream.Codec;
2014-03-24 17:54:45 +00:00
// Make sure video codec profiles are satisfied
if (!string.IsNullOrEmpty(videoCodec) && profile.CodecProfiles.Where(i => i.Type == CodecType.Video && i.ContainsCodec(videoCodec))
2014-03-24 17:54:45 +00:00
.All(i => AreConditionsSatisfied(i.Conditions, item.Path, videoStream, audioStream)))
2014-03-23 00:48:34 +00:00
{
2014-03-24 17:54:45 +00:00
var audioCodec = audioStream == null ? null : audioStream.Codec;
2014-03-23 00:48:34 +00:00
2014-03-24 17:54:45 +00:00
// Make sure audio codec profiles are satisfied
if (string.IsNullOrEmpty(audioCodec) || profile.CodecProfiles.Where(i => i.Type == CodecType.VideoAudio && i.ContainsCodec(audioCodec))
2014-03-24 17:54:45 +00:00
.All(i => AreConditionsSatisfied(i.Conditions, item.Path, videoStream, audioStream)))
{
playlistItem.Transcode = false;
playlistItem.Container = Path.GetExtension(item.Path);
return playlistItem;
}
2014-03-23 00:48:34 +00:00
}
}
var transcodingProfile = profile.TranscodingProfiles
.FirstOrDefault(i => i.Type == playlistItem.MediaType && IsSupported(profile, i, item));
if (transcodingProfile != null)
{
playlistItem.Transcode = true;
2014-03-22 19:37:15 +00:00
playlistItem.Container = "." + transcodingProfile.Container.TrimStart('.');
2014-03-23 16:42:02 +00:00
playlistItem.AudioCodec = transcodingProfile.AudioCodec.Split(',').FirstOrDefault();
playlistItem.VideoCodec = transcodingProfile.VideoCodec;
2014-03-24 17:54:45 +00:00
var videoTranscodingConditions = profile.CodecProfiles
.Where(i => i.Type == CodecType.Video && i.ContainsCodec(transcodingProfile.VideoCodec))
2014-03-24 17:54:45 +00:00
.Take(1)
.SelectMany(i => i.Conditions);
ApplyTranscodingConditions(playlistItem, videoTranscodingConditions);
var audioTranscodingConditions = profile.CodecProfiles
.Where(i => i.Type == CodecType.VideoAudio && i.ContainsCodec(transcodingProfile.AudioCodec))
2014-03-24 17:54:45 +00:00
.Take(1)
.SelectMany(i => i.Conditions);
ApplyTranscodingConditions(playlistItem, audioTranscodingConditions);
}
return playlistItem;
}
2014-03-24 17:54:45 +00:00
private void ApplyTranscodingConditions(PlaylistItem item, IEnumerable<ProfileCondition> conditions)
{
2014-03-25 05:25:03 +00:00
foreach (var condition in conditions
.Where(i => !string.IsNullOrEmpty(i.Value)))
2014-03-24 17:54:45 +00:00
{
var value = condition.Value;
switch (condition.Property)
{
case ProfileConditionValue.AudioBitrate:
{
2014-03-25 05:25:03 +00:00
int num;
2014-03-24 17:54:45 +00:00
if (int.TryParse(value, NumberStyles.Any, _usCulture, out num))
{
item.AudioBitrate = num;
}
break;
}
case ProfileConditionValue.AudioChannels:
{
2014-03-25 05:25:03 +00:00
int num;
2014-03-24 17:54:45 +00:00
if (int.TryParse(value, NumberStyles.Any, _usCulture, out num))
{
item.MaxAudioChannels = num;
}
break;
}
case ProfileConditionValue.AudioProfile:
case ProfileConditionValue.Has64BitOffsets:
case ProfileConditionValue.VideoBitDepth:
case ProfileConditionValue.VideoProfile:
{
// Not supported yet
break;
}
case ProfileConditionValue.Height:
{
2014-03-25 05:25:03 +00:00
int num;
2014-03-24 17:54:45 +00:00
if (int.TryParse(value, NumberStyles.Any, _usCulture, out num))
{
item.MaxHeight = num;
}
break;
}
case ProfileConditionValue.VideoBitrate:
{
2014-03-25 05:25:03 +00:00
int num;
2014-03-24 17:54:45 +00:00
if (int.TryParse(value, NumberStyles.Any, _usCulture, out num))
{
item.VideoBitrate = num;
}
break;
}
case ProfileConditionValue.VideoFramerate:
{
2014-03-25 05:25:03 +00:00
int num;
2014-03-24 17:54:45 +00:00
if (int.TryParse(value, NumberStyles.Any, _usCulture, out num))
{
item.MaxFramerate = num;
}
break;
}
case ProfileConditionValue.VideoLevel:
{
2014-03-25 05:25:03 +00:00
int num;
2014-03-24 17:54:45 +00:00
if (int.TryParse(value, NumberStyles.Any, _usCulture, out num))
{
item.VideoLevel = num;
}
break;
}
case ProfileConditionValue.Width:
{
2014-03-25 05:25:03 +00:00
int num;
2014-03-24 17:54:45 +00:00
if (int.TryParse(value, NumberStyles.Any, _usCulture, out num))
{
item.MaxWidth = num;
}
break;
}
default:
throw new ArgumentException("Unrecognized ProfileConditionValue");
}
}
}
private bool IsSupported(DirectPlayProfile profile, Photo item)
{
var mediaPath = item.Path;
2014-03-23 06:07:43 +00:00
if (profile.Container.Length > 0)
{
2014-03-22 20:02:10 +00:00
// Check container type
var mediaContainer = Path.GetExtension(mediaPath);
2014-03-23 06:07:43 +00:00
if (!profile.GetContainers().Any(i => string.Equals("." + i.TrimStart('.'), mediaContainer, StringComparison.OrdinalIgnoreCase)))
2014-03-22 20:02:10 +00:00
{
return false;
}
}
return true;
}
2014-03-24 17:54:45 +00:00
private bool IsSupported(DirectPlayProfile profile, Audio item, MediaStream audioStream)
{
var mediaPath = item.Path;
2014-03-23 06:07:43 +00:00
if (profile.Container.Length > 0)
{
2014-03-22 20:02:10 +00:00
// Check container type
var mediaContainer = Path.GetExtension(mediaPath);
2014-03-23 06:07:43 +00:00
if (!profile.GetContainers().Any(i => string.Equals("." + i.TrimStart('.'), mediaContainer, StringComparison.OrdinalIgnoreCase)))
2014-03-22 20:02:10 +00:00
{
return false;
}
}
return true;
}
private bool IsSupported(DirectPlayProfile profile, Video item, MediaStream videoStream, MediaStream audioStream)
{
if (item.VideoType != VideoType.VideoFile)
{
return false;
}
var mediaPath = item.Path;
2014-03-23 06:07:43 +00:00
if (profile.Container.Length > 0)
{
2014-03-22 20:02:10 +00:00
// Check container type
var mediaContainer = Path.GetExtension(mediaPath);
2014-03-23 06:07:43 +00:00
if (!profile.GetContainers().Any(i => string.Equals("." + i.TrimStart('.'), mediaContainer, StringComparison.OrdinalIgnoreCase)))
2014-03-22 20:02:10 +00:00
{
return false;
}
}
2014-03-22 19:37:15 +00:00
// Check video codec
var videoCodecs = profile.GetVideoCodecs();
if (videoCodecs.Count > 0)
2014-03-22 19:37:15 +00:00
{
var videoCodec = videoStream == null ? null : videoStream.Codec;
if (string.IsNullOrWhiteSpace(videoCodec) || !videoCodecs.Contains(videoCodec, StringComparer.OrdinalIgnoreCase))
2014-03-22 19:37:15 +00:00
{
return false;
}
}
var audioCodecs = profile.GetAudioCodecs();
if (audioCodecs.Count > 0)
2014-03-22 19:37:15 +00:00
{
// Check audio codecs
var audioCodec = audioStream == null ? null : audioStream.Codec;
if (string.IsNullOrWhiteSpace(audioCodec) || !audioCodecs.Contains(audioCodec, StringComparer.OrdinalIgnoreCase))
2014-03-22 19:37:15 +00:00
{
return false;
}
}
return true;
}
private bool IsSupported(DeviceProfile profile, TranscodingProfile transcodingProfile, Audio item)
{
// Placeholder for future conditions
return true;
}
private bool IsSupported(DeviceProfile profile, TranscodingProfile transcodingProfile, Photo item)
{
// Placeholder for future conditions
return true;
}
private bool IsSupported(DeviceProfile profile, TranscodingProfile transcodingProfile, Video item)
{
// Placeholder for future conditions
return true;
}
2014-03-24 17:54:45 +00:00
private bool AreConditionsSatisfied(IEnumerable<ProfileCondition> conditions, string mediaPath, MediaStream videoStream, MediaStream audioStream)
2014-03-23 00:48:34 +00:00
{
2014-03-24 17:54:45 +00:00
return conditions.All(i => IsConditionSatisfied(i, mediaPath, videoStream, audioStream));
2014-03-23 00:48:34 +00:00
}
2014-03-22 18:29:36 +00:00
/// <summary>
/// Determines whether [is condition satisfied] [the specified condition].
/// </summary>
/// <param name="condition">The condition.</param>
/// <param name="mediaPath">The media path.</param>
/// <param name="videoStream">The video stream.</param>
/// <param name="audioStream">The audio stream.</param>
/// <returns><c>true</c> if [is condition satisfied] [the specified condition]; otherwise, <c>false</c>.</returns>
/// <exception cref="System.InvalidOperationException">Unexpected ProfileConditionType</exception>
private bool IsConditionSatisfied(ProfileCondition condition, string mediaPath, MediaStream videoStream, MediaStream audioStream)
{
2014-03-23 19:36:25 +00:00
if (condition.Property == ProfileConditionValue.Has64BitOffsets)
{
// TODO: Determine how to evaluate this
}
2014-03-23 00:48:34 +00:00
if (condition.Property == ProfileConditionValue.VideoProfile)
{
var profile = videoStream == null ? null : videoStream.Profile;
2014-03-23 00:48:34 +00:00
if (!string.IsNullOrWhiteSpace(profile))
{
switch (condition.Condition)
{
case ProfileConditionType.Equals:
return string.Equals(profile, condition.Value, StringComparison.OrdinalIgnoreCase);
case ProfileConditionType.NotEquals:
return !string.Equals(profile, condition.Value, StringComparison.OrdinalIgnoreCase);
default:
throw new InvalidOperationException("Unexpected ProfileConditionType");
}
}
}
else if (condition.Property == ProfileConditionValue.AudioProfile)
{
2014-03-23 00:48:34 +00:00
var profile = audioStream == null ? null : audioStream.Profile;
if (!string.IsNullOrWhiteSpace(profile))
{
switch (condition.Condition)
{
case ProfileConditionType.Equals:
2014-03-23 00:48:34 +00:00
return string.Equals(profile, condition.Value, StringComparison.OrdinalIgnoreCase);
case ProfileConditionType.NotEquals:
2014-03-23 00:48:34 +00:00
return !string.Equals(profile, condition.Value, StringComparison.OrdinalIgnoreCase);
default:
throw new InvalidOperationException("Unexpected ProfileConditionType");
}
}
}
2014-03-23 00:48:34 +00:00
else
{
var actualValue = GetConditionValue(condition, mediaPath, videoStream, audioStream);
if (actualValue.HasValue)
{
long expected;
if (long.TryParse(condition.Value, NumberStyles.Any, _usCulture, out expected))
{
switch (condition.Condition)
{
case ProfileConditionType.Equals:
return actualValue.Value == expected;
case ProfileConditionType.GreaterThanEqual:
return actualValue.Value >= expected;
case ProfileConditionType.LessThanEqual:
return actualValue.Value <= expected;
case ProfileConditionType.NotEquals:
return actualValue.Value != expected;
default:
throw new InvalidOperationException("Unexpected ProfileConditionType");
}
}
}
}
// Value doesn't exist in metadata. Fail it if required.
return !condition.IsRequired;
}
2014-03-22 18:29:36 +00:00
/// <summary>
/// Gets the condition value.
/// </summary>
/// <param name="condition">The condition.</param>
/// <param name="mediaPath">The media path.</param>
/// <param name="videoStream">The video stream.</param>
/// <param name="audioStream">The audio stream.</param>
/// <returns>System.Nullable{System.Int64}.</returns>
/// <exception cref="System.InvalidOperationException">Unexpected Property</exception>
private long? GetConditionValue(ProfileCondition condition, string mediaPath, MediaStream videoStream, MediaStream audioStream)
{
switch (condition.Property)
{
case ProfileConditionValue.AudioBitrate:
return audioStream == null ? null : audioStream.BitRate;
case ProfileConditionValue.AudioChannels:
return audioStream == null ? null : audioStream.Channels;
case ProfileConditionValue.VideoBitrate:
return videoStream == null ? null : videoStream.BitRate;
case ProfileConditionValue.VideoFramerate:
return videoStream == null ? null : (ConvertToLong(videoStream.AverageFrameRate ?? videoStream.RealFrameRate));
2014-03-22 20:02:10 +00:00
case ProfileConditionValue.Height:
return videoStream == null ? null : videoStream.Height;
2014-03-22 20:02:10 +00:00
case ProfileConditionValue.Width:
return videoStream == null ? null : videoStream.Width;
2014-03-22 19:37:15 +00:00
case ProfileConditionValue.VideoLevel:
return videoStream == null ? null : ConvertToLong(videoStream.Level);
default:
throw new InvalidOperationException("Unexpected Property");
}
}
2014-03-22 18:29:36 +00:00
/// <summary>
/// Converts to long.
/// </summary>
/// <param name="val">The value.</param>
/// <returns>System.Nullable{System.Int64}.</returns>
private long? ConvertToLong(float? val)
{
return val.HasValue ? Convert.ToInt64(val.Value) : (long?)null;
}
2014-03-22 19:37:15 +00:00
/// <summary>
/// Converts to long.
/// </summary>
/// <param name="val">The value.</param>
/// <returns>System.Nullable{System.Int64}.</returns>
private long? ConvertToLong(double? val)
{
return val.HasValue ? Convert.ToInt64(val.Value) : (long?)null;
}
}
}