jellyfin/Emby.Dlna/Didl/DidlBuilder.cs

1223 lines
44 KiB
C#
Raw Normal View History

2016-10-29 22:22:20 +00:00
using MediaBrowser.Model.Extensions;
using MediaBrowser.Controller.Channels;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Playlists;
2016-10-29 22:34:54 +00:00
using Emby.Dlna.ContentDirectory;
2016-10-29 22:22:20 +00:00
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
2016-11-04 08:31:05 +00:00
using System.Text;
2017-05-26 06:48:54 +00:00
using System.Threading.Tasks;
2016-10-29 22:22:20 +00:00
using System.Xml;
using MediaBrowser.Controller.MediaEncoding;
2018-09-12 17:26:21 +00:00
using Emby.Dlna.Configuration;
2016-10-29 22:22:20 +00:00
using MediaBrowser.Model.Globalization;
2016-10-29 22:34:54 +00:00
namespace Emby.Dlna.Didl
2016-10-29 22:22:20 +00:00
{
public class DidlBuilder
{
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private const string NS_DIDL = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/";
private const string NS_DC = "http://purl.org/dc/elements/1.1/";
private const string NS_UPNP = "urn:schemas-upnp-org:metadata-1-0/upnp/";
private const string NS_DLNA = "urn:schemas-dlna-org:metadata-1-0/";
private readonly DeviceProfile _profile;
private readonly IImageProcessor _imageProcessor;
private readonly string _serverAddress;
private readonly string _accessToken;
private readonly User _user;
private readonly IUserDataManager _userDataManager;
private readonly ILocalizationManager _localization;
private readonly IMediaSourceManager _mediaSourceManager;
private readonly ILogger _logger;
private readonly ILibraryManager _libraryManager;
private readonly IMediaEncoder _mediaEncoder;
public DidlBuilder(DeviceProfile profile, User user, IImageProcessor imageProcessor, string serverAddress, string accessToken, IUserDataManager userDataManager, ILocalizationManager localization, IMediaSourceManager mediaSourceManager, ILogger logger, ILibraryManager libraryManager, IMediaEncoder mediaEncoder)
{
_profile = profile;
_imageProcessor = imageProcessor;
_serverAddress = serverAddress;
_userDataManager = userDataManager;
_localization = localization;
_mediaSourceManager = mediaSourceManager;
_logger = logger;
_libraryManager = libraryManager;
_mediaEncoder = mediaEncoder;
_accessToken = accessToken;
_user = user;
}
2018-09-12 17:26:21 +00:00
public static string NormalizeDlnaMediaUrl(string url)
{
return url + "&dlnaheaders=true";
}
2016-12-26 17:37:49 +00:00
public string GetItemDidl(DlnaOptions options, BaseItem item, User user, BaseItem context, string deviceId, Filter filter, StreamInfo streamInfo)
2016-10-29 22:22:20 +00:00
{
2016-11-04 08:31:05 +00:00
var settings = new XmlWriterSettings
{
Encoding = Encoding.UTF8,
CloseOutput = false,
OmitXmlDeclaration = true,
ConformanceLevel = ConformanceLevel.Fragment
};
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
using (StringWriter builder = new StringWriterWithEncoding(Encoding.UTF8))
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
using (XmlWriter writer = XmlWriter.Create(builder, settings))
{
//writer.WriteStartDocument();
2016-11-04 08:31:05 +00:00
2018-09-12 17:26:21 +00:00
writer.WriteStartElement(string.Empty, "DIDL-Lite", NS_DIDL);
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
writer.WriteAttributeString("xmlns", "dc", null, NS_DC);
writer.WriteAttributeString("xmlns", "dlna", null, NS_DLNA);
writer.WriteAttributeString("xmlns", "upnp", null, NS_UPNP);
//didl.SetAttribute("xmlns:sec", NS_SEC);
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
WriteXmlRootAttributes(_profile, writer);
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
WriteItemElement(options, writer, item, user, context, null, deviceId, filter, streamInfo);
2016-11-04 08:31:05 +00:00
2018-09-12 17:26:21 +00:00
writer.WriteFullEndElement();
//writer.WriteEndDocument();
}
2016-11-04 08:31:05 +00:00
2018-09-12 17:26:21 +00:00
return builder.ToString();
}
2016-10-29 22:22:20 +00:00
}
2016-11-06 17:30:44 +00:00
public static void WriteXmlRootAttributes(DeviceProfile profile, XmlWriter writer)
{
foreach (var att in profile.XmlRootAttributes)
{
var parts = att.Name.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 2)
{
writer.WriteAttributeString(parts[0], parts[1], null, att.Value);
}
else
{
writer.WriteAttributeString(att.Name, att.Value);
}
}
}
2017-05-26 06:48:54 +00:00
public void WriteItemElement(DlnaOptions options,
XmlWriter writer,
BaseItem item,
2016-12-26 17:37:49 +00:00
User user,
2017-05-26 06:48:54 +00:00
BaseItem context,
StubType? contextStubType,
string deviceId,
Filter filter,
2016-12-26 17:37:49 +00:00
StreamInfo streamInfo = null)
2016-10-29 22:22:20 +00:00
{
var clientId = GetClientId(item, null);
2016-11-04 08:31:05 +00:00
writer.WriteStartElement(string.Empty, "item", NS_DIDL);
writer.WriteAttributeString("restricted", "1");
writer.WriteAttributeString("id", clientId);
2016-10-29 22:22:20 +00:00
if (context != null)
{
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("parentID", GetClientId(context, contextStubType));
2016-10-29 22:22:20 +00:00
}
else
{
var parent = item.DisplayParentId;
2018-09-12 17:26:21 +00:00
if (!parent.Equals(Guid.Empty))
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
writer.WriteAttributeString("parentID", GetClientId(parent, null));
2016-10-29 22:22:20 +00:00
}
}
2016-11-06 17:30:44 +00:00
AddGeneralProperties(item, null, context, writer, filter);
2016-12-26 17:37:49 +00:00
AddSamsungBookmarkInfo(item, user, writer);
2016-10-29 22:22:20 +00:00
// refID?
// storeAttribute(itemNode, object, ClassProperties.REF_ID, false);
var hasMediaSources = item as IHasMediaSources;
if (hasMediaSources != null)
{
if (string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))
{
2018-09-12 17:26:21 +00:00
AddAudioResource(options, writer, item, deviceId, filter, streamInfo);
2016-10-29 22:22:20 +00:00
}
else if (string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
{
2018-09-12 17:26:21 +00:00
AddVideoResource(options, writer, item, deviceId, filter, streamInfo);
2016-10-29 22:22:20 +00:00
}
}
2016-11-04 08:31:05 +00:00
AddCover(item, context, null, writer);
2016-11-06 17:30:44 +00:00
writer.WriteFullEndElement();
2016-10-29 22:22:20 +00:00
}
private ILogger GetStreamBuilderLogger(DlnaOptions options)
{
if (options.EnableDebugLog)
{
return _logger;
}
return new NullLogger();
}
2017-01-02 05:32:37 +00:00
private string GetMimeType(string input)
{
var mime = MimeTypes.GetMimeType(input);
2018-09-12 17:26:21 +00:00
// TODO: Instead of being hard-coded here, this should probably be moved into all of the existing profiles
2017-01-02 05:32:37 +00:00
if (string.Equals(mime, "video/mp2t", StringComparison.OrdinalIgnoreCase))
{
mime = "video/mpeg";
}
return mime;
}
2018-09-12 17:26:21 +00:00
private void AddVideoResource(DlnaOptions options, XmlWriter writer, BaseItem video, string deviceId, Filter filter, StreamInfo streamInfo = null)
2016-10-29 22:22:20 +00:00
{
if (streamInfo == null)
{
2017-08-05 19:02:33 +00:00
var sources = _mediaSourceManager.GetStaticMediaSources(video, true, _user);
2016-10-29 22:22:20 +00:00
streamInfo = new StreamBuilder(_mediaEncoder, GetStreamBuilderLogger(options)).BuildVideoItem(new VideoOptions
{
2018-09-12 17:26:21 +00:00
ItemId = video.Id,
MediaSources = sources.ToArray(),
2016-10-29 22:22:20 +00:00
Profile = _profile,
DeviceId = deviceId,
MaxBitrate = _profile.MaxStreamingBitrate
});
}
var targetWidth = streamInfo.TargetWidth;
var targetHeight = streamInfo.TargetHeight;
var contentFeatureList = new ContentFeatureBuilder(_profile).BuildVideoHeader(streamInfo.Container,
2017-09-25 05:06:15 +00:00
streamInfo.TargetVideoCodec.FirstOrDefault(),
streamInfo.TargetAudioCodec.FirstOrDefault(),
2016-10-29 22:22:20 +00:00
targetWidth,
targetHeight,
streamInfo.TargetVideoBitDepth,
streamInfo.TargetVideoBitrate,
streamInfo.TargetTimestamp,
streamInfo.IsDirectStream,
2018-09-12 17:26:21 +00:00
streamInfo.RunTimeTicks ?? 0,
2016-10-29 22:22:20 +00:00
streamInfo.TargetVideoProfile,
streamInfo.TargetVideoLevel,
2018-09-12 17:26:21 +00:00
streamInfo.TargetFramerate ?? 0,
2016-10-29 22:22:20 +00:00
streamInfo.TargetPacketLength,
streamInfo.TranscodeSeekInfo,
streamInfo.IsTargetAnamorphic,
2017-05-29 12:35:59 +00:00
streamInfo.IsTargetInterlaced,
2016-10-29 22:22:20 +00:00
streamInfo.TargetRefFrames,
streamInfo.TargetVideoStreamCount,
streamInfo.TargetAudioStreamCount,
streamInfo.TargetVideoCodecTag,
2016-12-14 20:58:55 +00:00
streamInfo.IsTargetAVC);
2016-10-29 22:22:20 +00:00
foreach (var contentFeature in contentFeatureList)
{
2016-11-04 08:31:05 +00:00
AddVideoResource(writer, video, deviceId, filter, contentFeature, streamInfo);
2016-10-29 22:22:20 +00:00
}
var subtitleProfiles = streamInfo.GetSubtitleProfiles(_mediaEncoder, false, _serverAddress, _accessToken)
2016-12-08 15:40:20 +00:00
.Where(subtitle => subtitle.DeliveryMethod == SubtitleDeliveryMethod.External)
.ToList();
foreach (var subtitle in subtitleProfiles)
2016-10-29 22:22:20 +00:00
{
2016-12-08 15:40:20 +00:00
var subtitleAdded = AddSubtitleElement(writer, subtitle);
2016-10-29 22:22:20 +00:00
2016-12-08 15:40:20 +00:00
if (subtitleAdded && _profile.EnableSingleSubtitleLimit)
{
break;
2016-10-29 22:22:20 +00:00
}
}
}
2016-11-04 08:31:05 +00:00
private bool AddSubtitleElement(XmlWriter writer, SubtitleStreamInfo info)
2016-10-29 22:22:20 +00:00
{
var subtitleProfile = _profile.SubtitleProfiles
.FirstOrDefault(i => string.Equals(info.Format, i.Format, StringComparison.OrdinalIgnoreCase) && i.Method == SubtitleDeliveryMethod.External);
if (subtitleProfile == null)
{
return false;
}
var subtitleMode = subtitleProfile.DidlMode;
if (string.Equals(subtitleMode, "CaptionInfoEx", StringComparison.OrdinalIgnoreCase))
{
// <sec:CaptionInfoEx sec:type="srt">http://192.168.1.3:9999/video.srt</sec:CaptionInfoEx>
// <sec:CaptionInfo sec:type="srt">http://192.168.1.3:9999/video.srt</sec:CaptionInfo>
2016-11-04 08:31:05 +00:00
writer.WriteStartElement("sec", "CaptionInfoEx", null);
writer.WriteAttributeString("sec", "type", null, info.Format.ToLower());
2016-10-29 22:22:20 +00:00
2016-11-04 08:31:05 +00:00
writer.WriteString(info.Url);
2016-11-06 17:30:44 +00:00
writer.WriteFullEndElement();
2016-10-29 22:22:20 +00:00
}
else if (string.Equals(subtitleMode, "smi", StringComparison.OrdinalIgnoreCase))
{
2016-11-04 08:31:05 +00:00
writer.WriteStartElement(string.Empty, "res", NS_DIDL);
2016-10-29 22:22:20 +00:00
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("protocolInfo", "http-get:*:smi/caption:*");
2016-10-29 22:22:20 +00:00
2016-11-04 08:31:05 +00:00
writer.WriteString(info.Url);
2016-11-06 17:30:44 +00:00
writer.WriteFullEndElement();
2016-10-29 22:22:20 +00:00
}
else
{
2016-11-04 08:31:05 +00:00
writer.WriteStartElement(string.Empty, "res", NS_DIDL);
2016-10-29 22:22:20 +00:00
var protocolInfo = string.Format("http-get:*:text/{0}:*", info.Format.ToLower());
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("protocolInfo", protocolInfo);
2016-10-29 22:22:20 +00:00
2016-11-04 08:31:05 +00:00
writer.WriteString(info.Url);
2016-11-06 17:30:44 +00:00
writer.WriteFullEndElement();
2016-10-29 22:22:20 +00:00
}
return true;
}
2018-09-12 17:26:21 +00:00
private void AddVideoResource(XmlWriter writer, BaseItem video, string deviceId, Filter filter, string contentFeatures, StreamInfo streamInfo)
2016-10-29 22:22:20 +00:00
{
2016-11-04 08:31:05 +00:00
writer.WriteStartElement(string.Empty, "res", NS_DIDL);
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
var url = NormalizeDlnaMediaUrl(streamInfo.ToUrl(_serverAddress, _accessToken));
2016-10-29 22:22:20 +00:00
var mediaSource = streamInfo.MediaSource;
if (mediaSource.RunTimeTicks.HasValue)
{
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("duration", TimeSpan.FromTicks(mediaSource.RunTimeTicks.Value).ToString("c", _usCulture));
2016-10-29 22:22:20 +00:00
}
if (filter.Contains("res@size"))
{
if (streamInfo.IsDirectStream || streamInfo.EstimateContentLength)
{
var size = streamInfo.TargetSize;
if (size.HasValue)
{
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("size", size.Value.ToString(_usCulture));
2016-10-29 22:22:20 +00:00
}
}
}
var totalBitrate = streamInfo.TargetTotalBitrate;
var targetSampleRate = streamInfo.TargetAudioSampleRate;
var targetChannels = streamInfo.TargetAudioChannels;
var targetWidth = streamInfo.TargetWidth;
var targetHeight = streamInfo.TargetHeight;
if (targetChannels.HasValue)
{
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("nrAudioChannels", targetChannels.Value.ToString(_usCulture));
2016-10-29 22:22:20 +00:00
}
if (filter.Contains("res@resolution"))
{
if (targetWidth.HasValue && targetHeight.HasValue)
{
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("resolution", string.Format("{0}x{1}", targetWidth.Value, targetHeight.Value));
2016-10-29 22:22:20 +00:00
}
}
if (targetSampleRate.HasValue)
{
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("sampleFrequency", targetSampleRate.Value.ToString(_usCulture));
2016-10-29 22:22:20 +00:00
}
if (totalBitrate.HasValue)
{
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("bitrate", totalBitrate.Value.ToString(_usCulture));
2016-10-29 22:22:20 +00:00
}
var mediaProfile = _profile.GetVideoMediaProfile(streamInfo.Container,
2017-09-25 05:06:15 +00:00
streamInfo.TargetAudioCodec.FirstOrDefault(),
streamInfo.TargetVideoCodec.FirstOrDefault(),
2016-10-29 22:22:20 +00:00
streamInfo.TargetAudioBitrate,
targetWidth,
targetHeight,
streamInfo.TargetVideoBitDepth,
streamInfo.TargetVideoProfile,
streamInfo.TargetVideoLevel,
2018-09-12 17:26:21 +00:00
streamInfo.TargetFramerate ?? 0,
2016-10-29 22:22:20 +00:00
streamInfo.TargetPacketLength,
streamInfo.TargetTimestamp,
streamInfo.IsTargetAnamorphic,
2017-05-29 12:35:59 +00:00
streamInfo.IsTargetInterlaced,
2016-10-29 22:22:20 +00:00
streamInfo.TargetRefFrames,
streamInfo.TargetVideoStreamCount,
streamInfo.TargetAudioStreamCount,
streamInfo.TargetVideoCodecTag,
2016-12-14 20:58:55 +00:00
streamInfo.IsTargetAVC);
2016-10-29 22:22:20 +00:00
var filename = url.Substring(0, url.IndexOf('?'));
var mimeType = mediaProfile == null || string.IsNullOrEmpty(mediaProfile.MimeType)
2017-01-02 05:32:37 +00:00
? GetMimeType(filename)
2016-10-29 22:22:20 +00:00
: mediaProfile.MimeType;
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("protocolInfo", String.Format(
2016-10-29 22:22:20 +00:00
"http-get:*:{0}:{1}",
mimeType,
contentFeatures
));
2016-11-04 08:31:05 +00:00
writer.WriteString(url);
2016-11-06 17:30:44 +00:00
writer.WriteFullEndElement();
2016-10-29 22:22:20 +00:00
}
private string GetDisplayName(BaseItem item, StubType? itemStubType, BaseItem context)
{
2017-07-22 23:00:48 +00:00
if (itemStubType.HasValue && itemStubType.Value == StubType.Latest)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("Latest");
2017-07-22 23:00:48 +00:00
}
if (itemStubType.HasValue && itemStubType.Value == StubType.Playlists)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("Playlists");
2017-07-22 23:00:48 +00:00
}
if (itemStubType.HasValue && itemStubType.Value == StubType.AlbumArtists)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("HeaderAlbumArtists");
2017-07-22 23:00:48 +00:00
}
if (itemStubType.HasValue && itemStubType.Value == StubType.Albums)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("Albums");
2017-07-22 23:00:48 +00:00
}
if (itemStubType.HasValue && itemStubType.Value == StubType.Artists)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("Artists");
2017-07-22 23:00:48 +00:00
}
if (itemStubType.HasValue && itemStubType.Value == StubType.Songs)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("Songs");
2017-07-22 23:00:48 +00:00
}
if (itemStubType.HasValue && itemStubType.Value == StubType.Genres)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("Genres");
2017-07-22 23:00:48 +00:00
}
if (itemStubType.HasValue && itemStubType.Value == StubType.FavoriteAlbums)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("HeaderFavoriteAlbums");
2017-07-22 23:00:48 +00:00
}
if (itemStubType.HasValue && itemStubType.Value == StubType.FavoriteArtists)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("HeaderFavoriteArtists");
2017-07-22 23:00:48 +00:00
}
if (itemStubType.HasValue && itemStubType.Value == StubType.FavoriteSongs)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("HeaderFavoriteSongs");
2017-07-22 23:00:48 +00:00
}
if (itemStubType.HasValue && itemStubType.Value == StubType.ContinueWatching)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("HeaderContinueWatching");
}
if (itemStubType.HasValue && itemStubType.Value == StubType.Movies)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("Movies");
}
if (itemStubType.HasValue && itemStubType.Value == StubType.Collections)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("Collections");
}
if (itemStubType.HasValue && itemStubType.Value == StubType.Favorites)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("Favorites");
}
if (itemStubType.HasValue && itemStubType.Value == StubType.NextUp)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("HeaderNextUp");
}
if (itemStubType.HasValue && itemStubType.Value == StubType.FavoriteSeries)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("HeaderFavoriteShows");
}
if (itemStubType.HasValue && itemStubType.Value == StubType.FavoriteEpisodes)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("HeaderFavoriteEpisodes");
}
if (itemStubType.HasValue && itemStubType.Value == StubType.Series)
{
2017-10-13 05:43:11 +00:00
return _localization.GetLocalizedString("Shows");
}
2016-10-29 22:22:20 +00:00
var episode = item as Episode;
var season = context as Season;
if (episode != null && season != null)
{
// This is a special embedded within a season
if (item.ParentIndexNumber.HasValue && item.ParentIndexNumber.Value == 0)
{
if (season.IndexNumber.HasValue && season.IndexNumber.Value != 0)
{
return string.Format(_localization.GetLocalizedString("ValueSpecialEpisodeName"), item.Name);
}
}
if (item.IndexNumber.HasValue)
{
var number = item.IndexNumber.Value.ToString("00", CultureInfo.InvariantCulture);
if (episode.IndexNumberEnd.HasValue)
{
number += "-" + episode.IndexNumberEnd.Value.ToString("00", CultureInfo.InvariantCulture);
}
return number + " - " + item.Name;
}
}
return item.Name;
}
2018-09-12 17:26:21 +00:00
private void AddAudioResource(DlnaOptions options, XmlWriter writer, BaseItem audio, string deviceId, Filter filter, StreamInfo streamInfo = null)
2016-10-29 22:22:20 +00:00
{
2016-11-04 08:31:05 +00:00
writer.WriteStartElement(string.Empty, "res", NS_DIDL);
2016-10-29 22:22:20 +00:00
if (streamInfo == null)
{
2017-08-05 19:02:33 +00:00
var sources = _mediaSourceManager.GetStaticMediaSources(audio, true, _user);
2016-10-29 22:22:20 +00:00
streamInfo = new StreamBuilder(_mediaEncoder, GetStreamBuilderLogger(options)).BuildAudioItem(new AudioOptions
2016-11-04 08:31:05 +00:00
{
2018-09-12 17:26:21 +00:00
ItemId = audio.Id,
2017-08-19 19:43:35 +00:00
MediaSources = sources.ToArray(sources.Count),
2016-11-04 08:31:05 +00:00
Profile = _profile,
DeviceId = deviceId
});
2016-10-29 22:22:20 +00:00
}
2018-09-12 17:26:21 +00:00
var url = NormalizeDlnaMediaUrl(streamInfo.ToUrl(_serverAddress, _accessToken));
2016-10-29 22:22:20 +00:00
var mediaSource = streamInfo.MediaSource;
if (mediaSource.RunTimeTicks.HasValue)
{
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("duration", TimeSpan.FromTicks(mediaSource.RunTimeTicks.Value).ToString("c", _usCulture));
2016-10-29 22:22:20 +00:00
}
if (filter.Contains("res@size"))
{
if (streamInfo.IsDirectStream || streamInfo.EstimateContentLength)
{
var size = streamInfo.TargetSize;
if (size.HasValue)
{
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("size", size.Value.ToString(_usCulture));
2016-10-29 22:22:20 +00:00
}
}
}
var targetAudioBitrate = streamInfo.TargetAudioBitrate;
var targetSampleRate = streamInfo.TargetAudioSampleRate;
var targetChannels = streamInfo.TargetAudioChannels;
2017-06-26 15:10:52 +00:00
var targetAudioBitDepth = streamInfo.TargetAudioBitDepth;
2016-10-29 22:22:20 +00:00
if (targetChannels.HasValue)
{
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("nrAudioChannels", targetChannels.Value.ToString(_usCulture));
2016-10-29 22:22:20 +00:00
}
if (targetSampleRate.HasValue)
{
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("sampleFrequency", targetSampleRate.Value.ToString(_usCulture));
2016-10-29 22:22:20 +00:00
}
if (targetAudioBitrate.HasValue)
{
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("bitrate", targetAudioBitrate.Value.ToString(_usCulture));
2016-10-29 22:22:20 +00:00
}
var mediaProfile = _profile.GetAudioMediaProfile(streamInfo.Container,
2017-09-25 05:06:15 +00:00
streamInfo.TargetAudioCodec.FirstOrDefault(),
2016-10-29 22:22:20 +00:00
targetChannels,
2017-05-13 19:31:25 +00:00
targetAudioBitrate,
2017-06-26 15:10:52 +00:00
targetSampleRate,
targetAudioBitDepth);
2016-10-29 22:22:20 +00:00
var filename = url.Substring(0, url.IndexOf('?'));
var mimeType = mediaProfile == null || string.IsNullOrEmpty(mediaProfile.MimeType)
2017-01-02 05:32:37 +00:00
? GetMimeType(filename)
2016-10-29 22:22:20 +00:00
: mediaProfile.MimeType;
var contentFeatures = new ContentFeatureBuilder(_profile).BuildAudioHeader(streamInfo.Container,
2017-09-25 05:06:15 +00:00
streamInfo.TargetAudioCodec.FirstOrDefault(),
2016-10-29 22:22:20 +00:00
targetAudioBitrate,
targetSampleRate,
targetChannels,
2017-06-26 15:10:52 +00:00
targetAudioBitDepth,
2016-10-29 22:22:20 +00:00
streamInfo.IsDirectStream,
2018-09-12 17:26:21 +00:00
streamInfo.RunTimeTicks ?? 0,
2016-10-29 22:22:20 +00:00
streamInfo.TranscodeSeekInfo);
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("protocolInfo", String.Format(
2016-10-29 22:22:20 +00:00
"http-get:*:{0}:{1}",
mimeType,
contentFeatures
));
2016-11-04 08:31:05 +00:00
writer.WriteString(url);
2016-11-06 17:30:44 +00:00
writer.WriteFullEndElement();
2016-10-29 22:22:20 +00:00
}
public static bool IsIdRoot(string id)
{
if (string.IsNullOrWhiteSpace(id) ||
string.Equals(id, "0", StringComparison.OrdinalIgnoreCase)
// Samsung sometimes uses 1 as root
|| string.Equals(id, "1", StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
}
2016-11-04 08:31:05 +00:00
public void WriteFolderElement(XmlWriter writer, BaseItem folder, StubType? stubType, BaseItem context, int childCount, Filter filter, string requestedId = null)
2016-10-29 22:22:20 +00:00
{
2016-11-04 08:31:05 +00:00
writer.WriteStartElement(string.Empty, "container", NS_DIDL);
2018-09-12 17:26:21 +00:00
writer.WriteAttributeString("restricted", "1");
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("searchable", "1");
writer.WriteAttributeString("childCount", childCount.ToString(_usCulture));
2016-10-29 22:22:20 +00:00
var clientId = GetClientId(folder, stubType);
if (string.Equals(requestedId, "0"))
{
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("id", "0");
writer.WriteAttributeString("parentID", "-1");
2016-10-29 22:22:20 +00:00
}
else
{
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("id", clientId);
2016-10-29 22:22:20 +00:00
if (context != null)
{
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("parentID", GetClientId(context, null));
2016-10-29 22:22:20 +00:00
}
else
{
var parent = folder.DisplayParentId;
2018-09-12 17:26:21 +00:00
if (parent.Equals(Guid.Empty))
2016-10-29 22:22:20 +00:00
{
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("parentID", "0");
2016-10-29 22:22:20 +00:00
}
else
{
2018-09-12 17:26:21 +00:00
writer.WriteAttributeString("parentID", GetClientId(parent, null));
2016-10-29 22:22:20 +00:00
}
}
}
2016-11-06 17:30:44 +00:00
AddGeneralProperties(folder, stubType, context, writer, filter);
2016-11-04 08:31:05 +00:00
AddCover(folder, context, stubType, writer);
2016-10-29 22:22:20 +00:00
2016-11-06 17:30:44 +00:00
writer.WriteFullEndElement();
2016-10-29 22:22:20 +00:00
}
2016-12-26 17:37:49 +00:00
private void AddSamsungBookmarkInfo(BaseItem item, User user, XmlWriter writer)
{
if (!item.SupportsPositionTicksResume || item is Folder)
{
return;
}
MediaBrowser.Model.Dlna.XmlAttribute secAttribute = null;
2016-12-26 17:37:49 +00:00
foreach (var attribute in _profile.XmlRootAttributes)
{
if (string.Equals(attribute.Name, "xmlns:sec", StringComparison.OrdinalIgnoreCase))
{
secAttribute = attribute;
break;
}
}
// Not a samsung device
if (secAttribute == null)
{
return;
}
2018-09-12 17:26:21 +00:00
var userdata = _userDataManager.GetUserData(user, item);
2016-12-26 17:37:49 +00:00
if (userdata.PlaybackPositionTicks > 0)
{
var elementValue = string.Format("BM={0}", Convert.ToInt32(TimeSpan.FromTicks(userdata.PlaybackPositionTicks).TotalSeconds).ToString(_usCulture));
AddValue(writer, "sec", "dcmInfo", elementValue, secAttribute.Value);
}
}
2016-10-29 22:22:20 +00:00
/// <summary>
/// Adds fields used by both items and folders
/// </summary>
2016-11-04 08:31:05 +00:00
private void AddCommonFields(BaseItem item, StubType? itemStubType, BaseItem context, XmlWriter writer, Filter filter)
2016-10-29 22:22:20 +00:00
{
// Don't filter on dc:title because not all devices will include it in the filter
// MediaMonkey for example won't display content without a title
//if (filter.Contains("dc:title"))
{
2016-11-04 08:31:05 +00:00
AddValue(writer, "dc", "title", GetDisplayName(item, itemStubType, context), NS_DC);
2016-10-29 22:22:20 +00:00
}
2016-11-04 08:31:05 +00:00
WriteObjectClass(writer, item, itemStubType);
2016-10-29 22:22:20 +00:00
if (filter.Contains("dc:date"))
{
if (item.PremiereDate.HasValue)
{
2016-11-04 08:31:05 +00:00
AddValue(writer, "dc", "date", item.PremiereDate.Value.ToString("o"), NS_DC);
2016-10-29 22:22:20 +00:00
}
}
if (filter.Contains("upnp:genre"))
{
foreach (var genre in item.Genres)
{
2016-11-04 08:31:05 +00:00
AddValue(writer, "upnp", "genre", genre, NS_UPNP);
2016-10-29 22:22:20 +00:00
}
}
foreach (var studio in item.Studios)
{
2016-11-04 08:31:05 +00:00
AddValue(writer, "upnp", "publisher", studio, NS_UPNP);
2016-10-29 22:22:20 +00:00
}
2018-09-12 17:26:21 +00:00
if (!(item is Folder))
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
if (filter.Contains("dc:description"))
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
var desc = item.Overview;
if (!string.IsNullOrWhiteSpace(desc))
{
AddValue(writer, "dc", "description", desc, NS_DC);
}
2016-10-29 22:22:20 +00:00
}
2018-09-12 17:26:21 +00:00
//if (filter.Contains("upnp:longDescription"))
//{
// if (!string.IsNullOrWhiteSpace(item.Overview))
// {
// AddValue(writer, "upnp", "longDescription", item.Overview, NS_UPNP);
// }
//}
2016-10-29 22:22:20 +00:00
}
if (!string.IsNullOrEmpty(item.OfficialRating))
{
if (filter.Contains("dc:rating"))
{
2016-11-04 08:31:05 +00:00
AddValue(writer, "dc", "rating", item.OfficialRating, NS_DC);
2016-10-29 22:22:20 +00:00
}
if (filter.Contains("upnp:rating"))
{
2016-11-04 08:31:05 +00:00
AddValue(writer, "upnp", "rating", item.OfficialRating, NS_UPNP);
2016-10-29 22:22:20 +00:00
}
}
2016-11-04 08:31:05 +00:00
AddPeople(item, writer);
2016-10-29 22:22:20 +00:00
}
2016-11-04 08:31:05 +00:00
private void WriteObjectClass(XmlWriter writer, BaseItem item, StubType? stubType)
2016-10-29 22:22:20 +00:00
{
// More types here
// http://oss.linn.co.uk/repos/Public/LibUpnpCil/DidlLite/UpnpAv/Test/TestDidlLite.cs
2016-11-04 08:31:05 +00:00
writer.WriteStartElement("upnp", "class", NS_UPNP);
2016-10-29 22:22:20 +00:00
2017-01-06 04:38:03 +00:00
if (item.IsDisplayedAsFolder || stubType.HasValue)
2016-10-29 22:22:20 +00:00
{
string classType = null;
if (!_profile.RequiresPlainFolders)
{
if (item is MusicAlbum)
{
classType = "object.container.album.musicAlbum";
}
else if (item is MusicArtist)
{
classType = "object.container.person.musicArtist";
}
else if (item is Series || item is Season || item is BoxSet || item is Video)
{
classType = "object.container.album.videoAlbum";
}
else if (item is Playlist)
{
classType = "object.container.playlistContainer";
}
else if (item is PhotoAlbum)
{
classType = "object.container.album.photoAlbum";
}
}
2016-11-04 08:31:05 +00:00
writer.WriteString(classType ?? "object.container.storageFolder");
2016-10-29 22:22:20 +00:00
}
else if (string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))
{
2016-11-04 08:31:05 +00:00
writer.WriteString("object.item.audioItem.musicTrack");
2016-10-29 22:22:20 +00:00
}
else if (string.Equals(item.MediaType, MediaType.Photo, StringComparison.OrdinalIgnoreCase))
{
2016-11-04 08:31:05 +00:00
writer.WriteString("object.item.imageItem.photo");
2016-10-29 22:22:20 +00:00
}
else if (string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
{
if (!_profile.RequiresPlainVideoItems && item is Movie)
{
2016-11-04 08:31:05 +00:00
writer.WriteString("object.item.videoItem.movie");
2016-10-29 22:22:20 +00:00
}
else if (!_profile.RequiresPlainVideoItems && item is MusicVideo)
{
2016-11-04 08:31:05 +00:00
writer.WriteString("object.item.videoItem.musicVideoClip");
2016-10-29 22:22:20 +00:00
}
else
{
2016-11-04 08:31:05 +00:00
writer.WriteString("object.item.videoItem");
2016-10-29 22:22:20 +00:00
}
}
else if (item is MusicGenre)
{
2016-11-04 08:31:05 +00:00
writer.WriteString(_profile.RequiresPlainFolders ? "object.container.storageFolder" : "object.container.genre.musicGenre");
2016-10-29 22:22:20 +00:00
}
else if (item is Genre || item is GameGenre)
{
2016-11-04 08:31:05 +00:00
writer.WriteString(_profile.RequiresPlainFolders ? "object.container.storageFolder" : "object.container.genre");
2016-10-29 22:22:20 +00:00
}
else
{
2016-11-04 08:31:05 +00:00
writer.WriteString("object.item");
2016-10-29 22:22:20 +00:00
}
2016-11-06 17:30:44 +00:00
writer.WriteFullEndElement();
2016-10-29 22:22:20 +00:00
}
2016-11-04 08:31:05 +00:00
private void AddPeople(BaseItem item, XmlWriter writer)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
//var types = new[]
//{
// PersonType.Director,
// PersonType.Writer,
// PersonType.Producer,
// PersonType.Composer,
// "Creator"
//};
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
//var people = _libraryManager.GetPeople(item);
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
//var index = 0;
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
//// Seeing some LG models locking up due content with large lists of people
//// The actual issue might just be due to processing a more metadata than it can handle
//var limit = 6;
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
//foreach (var actor in people)
//{
// var type = types.FirstOrDefault(i => string.Equals(i, actor.Type, StringComparison.OrdinalIgnoreCase) || string.Equals(i, actor.Role, StringComparison.OrdinalIgnoreCase))
// ?? PersonType.Actor;
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
// AddValue(writer, "upnp", type.ToLower(), actor.Name, NS_UPNP);
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
// index++;
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
// if (index >= limit)
// {
// break;
// }
//}
2016-10-29 22:22:20 +00:00
}
2016-11-04 08:31:05 +00:00
private void AddGeneralProperties(BaseItem item, StubType? itemStubType, BaseItem context, XmlWriter writer, Filter filter)
2016-10-29 22:22:20 +00:00
{
2016-11-04 08:31:05 +00:00
AddCommonFields(item, itemStubType, context, writer, filter);
2016-10-29 22:22:20 +00:00
2017-10-27 03:50:17 +00:00
var hasArtists = item as IHasArtist;
var hasAlbumArtists = item as IHasAlbumArtist;
2016-10-29 22:22:20 +00:00
2017-10-27 03:50:17 +00:00
if (hasArtists != null)
2016-10-29 22:22:20 +00:00
{
2017-10-27 03:50:17 +00:00
foreach (var artist in hasArtists.Artists)
2016-10-29 22:22:20 +00:00
{
2016-11-04 08:31:05 +00:00
AddValue(writer, "upnp", "artist", artist, NS_UPNP);
2017-10-27 03:50:17 +00:00
AddValue(writer, "dc", "creator", artist, NS_DC);
2016-10-29 22:22:20 +00:00
2017-10-27 03:50:17 +00:00
// If it doesn't support album artists (musicvideo), then tag as both
if (hasAlbumArtists == null)
{
AddAlbumArtist(writer, artist);
}
2016-10-29 22:22:20 +00:00
}
}
2017-10-27 03:50:17 +00:00
if (hasAlbumArtists != null)
2016-10-29 22:22:20 +00:00
{
2017-10-27 03:50:17 +00:00
foreach (var albumArtist in hasAlbumArtists.AlbumArtists)
2016-10-29 22:22:20 +00:00
{
2017-10-27 03:50:17 +00:00
AddAlbumArtist(writer, albumArtist);
2016-10-29 22:22:20 +00:00
}
}
2017-10-27 03:50:17 +00:00
if (!string.IsNullOrWhiteSpace(item.Album))
2016-10-29 22:22:20 +00:00
{
2017-10-27 03:50:17 +00:00
AddValue(writer, "upnp", "album", item.Album, NS_UPNP);
2016-10-29 22:22:20 +00:00
}
if (item.IndexNumber.HasValue)
{
2016-11-04 08:31:05 +00:00
AddValue(writer, "upnp", "originalTrackNumber", item.IndexNumber.Value.ToString(_usCulture), NS_UPNP);
2016-10-29 22:22:20 +00:00
if (item is Episode)
{
2016-11-04 08:31:05 +00:00
AddValue(writer, "upnp", "episodeNumber", item.IndexNumber.Value.ToString(_usCulture), NS_UPNP);
2016-10-29 22:22:20 +00:00
}
}
}
2016-11-04 08:31:05 +00:00
private void AddAlbumArtist(XmlWriter writer, string name)
2016-10-29 22:22:20 +00:00
{
try
{
2016-11-04 08:31:05 +00:00
writer.WriteStartElement("upnp", "artist", NS_UPNP);
writer.WriteAttributeString("role", "AlbumArtist");
2016-10-29 22:22:20 +00:00
2016-11-04 08:31:05 +00:00
writer.WriteString(name);
2016-10-29 22:22:20 +00:00
2016-11-06 17:30:44 +00:00
writer.WriteFullEndElement();
2016-10-29 22:22:20 +00:00
}
catch (XmlException)
{
//_logger.Error("Error adding xml value: " + value);
}
}
2016-11-04 08:31:05 +00:00
private void AddValue(XmlWriter writer, string prefix, string name, string value, string namespaceUri)
2016-10-29 22:22:20 +00:00
{
try
{
2016-11-04 08:31:05 +00:00
writer.WriteElementString(prefix, name, namespaceUri, value);
2016-10-29 22:22:20 +00:00
}
catch (XmlException)
{
//_logger.Error("Error adding xml value: " + value);
}
}
2016-11-04 08:31:05 +00:00
private void AddCover(BaseItem item, BaseItem context, StubType? stubType, XmlWriter writer)
2016-10-29 22:22:20 +00:00
{
ImageDownloadInfo imageInfo = null;
// Finally, just use the image from the item
if (imageInfo == null)
{
imageInfo = GetImageInfo(item);
}
if (imageInfo == null)
{
return;
}
2018-09-12 17:26:21 +00:00
var albumartUrlInfo = GetImageUrl(imageInfo, _profile.MaxAlbumArtWidth, _profile.MaxAlbumArtHeight, "jpg");
2016-10-29 22:22:20 +00:00
2016-11-04 08:31:05 +00:00
writer.WriteStartElement("upnp", "albumArtURI", NS_UPNP);
writer.WriteAttributeString("dlna", "profileID", NS_DLNA, _profile.AlbumArtPn);
writer.WriteString(albumartUrlInfo.Url);
2016-11-06 17:30:44 +00:00
writer.WriteFullEndElement();
2016-10-29 22:22:20 +00:00
// TOOD: Remove these default values
2018-09-12 17:26:21 +00:00
var iconUrlInfo = GetImageUrl(imageInfo, _profile.MaxIconWidth ?? 48, _profile.MaxIconHeight ?? 48, "jpg");
2016-11-04 08:31:05 +00:00
writer.WriteElementString("upnp", "icon", NS_UPNP, iconUrlInfo.Url);
2016-10-29 22:22:20 +00:00
if (!_profile.EnableAlbumArtInDidl)
{
if (string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase)
|| string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
{
if (!stubType.HasValue)
{
return;
}
}
}
2018-09-12 17:26:21 +00:00
AddImageResElement(item, writer, 160, 160, "jpg", "JPEG_TN");
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
if (!_profile.EnableSingleAlbumArtLimit || string.Equals(item.MediaType, MediaType.Photo, StringComparison.OrdinalIgnoreCase))
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
AddImageResElement(item, writer, 4096, 4096, "jpg", "JPEG_LRG");
AddImageResElement(item, writer, 1024, 768, "jpg", "JPEG_MED");
AddImageResElement(item, writer, 640, 480, "jpg", "JPEG_SM");
AddImageResElement(item, writer, 4096, 4096, "png", "PNG_LRG");
AddImageResElement(item, writer, 160, 160, "png", "PNG_TN");
2016-10-29 22:22:20 +00:00
}
}
2016-11-04 08:31:05 +00:00
private void AddEmbeddedImageAsCover(string name, XmlWriter writer)
2016-10-29 22:22:20 +00:00
{
2016-11-04 08:31:05 +00:00
writer.WriteStartElement("upnp", "albumArtURI", NS_UPNP);
writer.WriteAttributeString("dlna", "profileID", NS_DLNA, _profile.AlbumArtPn);
writer.WriteString(_serverAddress + "/Dlna/icons/people480.jpg");
2016-11-06 17:30:44 +00:00
writer.WriteFullEndElement();
2016-11-04 08:31:05 +00:00
writer.WriteElementString("upnp", "icon", NS_UPNP, _serverAddress + "/Dlna/icons/people48.jpg");
2016-10-29 22:22:20 +00:00
}
private void AddImageResElement(BaseItem item,
2016-11-04 08:31:05 +00:00
XmlWriter writer,
2016-10-29 22:22:20 +00:00
int maxWidth,
int maxHeight,
string format,
string org_Pn)
{
var imageInfo = GetImageInfo(item);
if (imageInfo == null)
{
return;
}
2018-09-12 17:26:21 +00:00
var albumartUrlInfo = GetImageUrl(imageInfo, maxWidth, maxHeight, format);
2016-10-29 22:22:20 +00:00
2016-11-04 08:31:05 +00:00
writer.WriteStartElement(string.Empty, "res", NS_DIDL);
2016-10-29 22:22:20 +00:00
2017-10-17 19:49:55 +00:00
// Images must have a reported size or many clients (Bubble upnp), will only use the first thumbnail
// rather than using a larger one when available
var width = albumartUrlInfo.Width ?? maxWidth;
var height = albumartUrlInfo.Height ?? maxHeight;
2016-10-29 22:22:20 +00:00
var contentFeatures = new ContentFeatureBuilder(_profile)
.BuildImageHeader(format, width, height, imageInfo.IsDirectStream, org_Pn);
2016-11-04 08:31:05 +00:00
writer.WriteAttributeString("protocolInfo", String.Format(
2016-10-29 22:22:20 +00:00
"http-get:*:{0}:{1}",
2017-01-02 05:32:37 +00:00
GetMimeType("file." + format),
2016-10-29 22:22:20 +00:00
contentFeatures
));
2017-10-17 19:49:55 +00:00
writer.WriteAttributeString("resolution", string.Format("{0}x{1}", width, height));
2016-10-29 22:22:20 +00:00
2016-11-04 08:31:05 +00:00
writer.WriteString(albumartUrlInfo.Url);
2016-11-06 17:30:44 +00:00
writer.WriteFullEndElement();
2016-10-29 22:22:20 +00:00
}
private ImageDownloadInfo GetImageInfo(BaseItem item)
{
if (item.HasImage(ImageType.Primary))
{
return GetImageInfo(item, ImageType.Primary);
}
if (item.HasImage(ImageType.Thumb))
{
return GetImageInfo(item, ImageType.Thumb);
}
if (item.HasImage(ImageType.Backdrop))
{
if (item is Channel)
{
return GetImageInfo(item, ImageType.Backdrop);
}
}
item = item.GetParents().FirstOrDefault(i => i.HasImage(ImageType.Primary));
if (item != null)
{
if (item.HasImage(ImageType.Primary))
{
return GetImageInfo(item, ImageType.Primary);
}
}
return null;
}
private ImageDownloadInfo GetImageInfo(BaseItem item, ImageType type)
{
var imageInfo = item.GetImageInfo(type, 0);
string tag = null;
try
{
tag = _imageProcessor.GetImageCacheTag(item, type);
}
catch
{
}
2017-10-22 06:22:43 +00:00
int? width = imageInfo.Width;
int? height = imageInfo.Height;
if (width == 0 || height == 0)
{
//_imageProcessor.GetImageSize(item, imageInfo);
width = null;
height = null;
}
else if (width == -1 || height == -1)
{
width = null;
height = null;
}
2016-10-29 22:22:20 +00:00
2017-09-22 20:33:01 +00:00
//try
//{
// var size = _imageProcessor.GetImageSize(imageInfo);
2016-10-29 22:22:20 +00:00
2017-09-22 20:33:01 +00:00
// width = Convert.ToInt32(size.Width);
// height = Convert.ToInt32(size.Height);
//}
//catch
//{
2016-10-29 22:22:20 +00:00
2017-09-22 20:33:01 +00:00
//}
2016-10-29 22:22:20 +00:00
var inputFormat = (Path.GetExtension(imageInfo.Path) ?? string.Empty)
.TrimStart('.')
.Replace("jpeg", "jpg", StringComparison.OrdinalIgnoreCase);
return new ImageDownloadInfo
{
2018-09-12 17:26:21 +00:00
ItemId = item.Id,
2016-10-29 22:22:20 +00:00
Type = type,
ImageTag = tag,
Width = width,
Height = height,
Format = inputFormat,
ItemImageInfo = imageInfo
};
}
class ImageDownloadInfo
{
2018-09-12 17:26:21 +00:00
internal Guid ItemId;
2016-10-29 22:22:20 +00:00
internal string ImageTag;
internal ImageType Type;
internal int? Width;
internal int? Height;
internal bool IsDirectStream;
internal string Format;
internal ItemImageInfo ItemImageInfo;
}
class ImageUrlInfo
{
internal string Url;
internal int? Width;
internal int? Height;
}
public static string GetClientId(BaseItem item, StubType? stubType)
{
return GetClientId(item.Id, stubType);
}
public static string GetClientId(Guid idValue, StubType? stubType)
{
var id = idValue.ToString("N");
if (stubType.HasValue)
{
id = stubType.Value.ToString().ToLower() + "_" + id;
}
return id;
}
2018-09-12 17:26:21 +00:00
private ImageUrlInfo GetImageUrl(ImageDownloadInfo info, int maxWidth, int maxHeight, string format)
2016-10-29 22:22:20 +00:00
{
2018-09-12 17:26:21 +00:00
var url = string.Format("{0}/Items/{1}/Images/{2}/0/{3}/{4}/{5}/{6}/0/0",
2016-10-29 22:22:20 +00:00
_serverAddress,
2018-09-12 17:26:21 +00:00
info.ItemId.ToString("N"),
2016-10-29 22:22:20 +00:00
info.Type,
info.ImageTag,
format,
maxWidth.ToString(CultureInfo.InvariantCulture),
2018-09-12 17:26:21 +00:00
maxHeight.ToString(CultureInfo.InvariantCulture)
2016-10-29 22:22:20 +00:00
);
var width = info.Width;
var height = info.Height;
info.IsDirectStream = false;
if (width.HasValue && height.HasValue)
{
var newSize = DrawingUtils.Resize(new ImageSize
{
Height = height.Value,
Width = width.Value
2018-09-12 17:26:21 +00:00
}, 0, 0, maxWidth, maxHeight);
2016-10-29 22:22:20 +00:00
width = Convert.ToInt32(newSize.Width);
height = Convert.ToInt32(newSize.Height);
var normalizedFormat = format
.Replace("jpeg", "jpg", StringComparison.OrdinalIgnoreCase);
if (string.Equals(info.Format, normalizedFormat, StringComparison.OrdinalIgnoreCase))
{
info.IsDirectStream = maxWidth >= width.Value && maxHeight >= height.Value;
}
}
2018-09-12 17:26:21 +00:00
// just lie
info.IsDirectStream = true;
2016-10-29 22:22:20 +00:00
return new ImageUrlInfo
{
Url = url,
Width = width,
Height = height
};
}
}
}