123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146 |
- using MediaBrowser.Common.Extensions;
- using MediaBrowser.Common.Net;
- using MediaBrowser.Controller.Dlna;
- using MediaBrowser.Controller.Drawing;
- using MediaBrowser.Controller.Dto;
- 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.Model.Dlna;
- using MediaBrowser.Model.Drawing;
- using MediaBrowser.Model.Entities;
- using MediaBrowser.Model.Logging;
- using MediaBrowser.Model.Querying;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Xml;
- namespace MediaBrowser.Dlna.Server
- {
- public class ControlHandler
- {
- private readonly ILogger _logger;
- private readonly ILibraryManager _libraryManager;
- private readonly DeviceProfile _profile;
- private readonly IDtoService _dtoService;
- private readonly IImageProcessor _imageProcessor;
- private readonly IUserDataManager _userDataManager;
- private readonly User _user;
- private readonly string _serverAddress;
- private const string NS_DC = "http://purl.org/dc/elements/1.1/";
- private const string NS_DIDL = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/";
- private const string NS_DLNA = "urn:schemas-dlna-org:metadata-1-0/";
- private const string NS_SEC = "http://www.sec.co.kr/";
- private const string NS_SOAPENV = "http://schemas.xmlsoap.org/soap/envelope/";
- private const string NS_UPNP = "urn:schemas-upnp-org:metadata-1-0/upnp/";
- private readonly int _systemUpdateId;
- private readonly CultureInfo _usCulture = new CultureInfo("en-US");
- public ControlHandler(ILogger logger, ILibraryManager libraryManager, DeviceProfile profile, string serverAddress, IDtoService dtoService, IImageProcessor imageProcessor, IUserDataManager userDataManager, User user, int systemUpdateId)
- {
- _logger = logger;
- _libraryManager = libraryManager;
- _profile = profile;
- _serverAddress = serverAddress;
- _dtoService = dtoService;
- _imageProcessor = imageProcessor;
- _userDataManager = userDataManager;
- _user = user;
- _systemUpdateId = systemUpdateId;
- }
- public ControlResponse ProcessControlRequest(ControlRequest request)
- {
- try
- {
- return ProcessControlRequestInternal(request);
- }
- catch (Exception ex)
- {
- _logger.ErrorException("Error processing control request", ex);
- return GetErrorResponse(ex);
- }
- }
- private ControlResponse ProcessControlRequestInternal(ControlRequest request)
- {
- var soap = new XmlDocument();
- soap.LoadXml(request.InputXml);
- var sparams = new Headers();
- var body = soap.GetElementsByTagName("Body", NS_SOAPENV).Item(0);
- var method = body.FirstChild;
- foreach (var p in method.ChildNodes)
- {
- var e = p as XmlElement;
- if (e == null)
- {
- continue;
- }
- sparams.Add(e.LocalName, e.InnerText.Trim());
- }
- var deviceId = "fgd";
- var env = new XmlDocument();
- env.AppendChild(env.CreateXmlDeclaration("1.0", "utf-8", "yes"));
- var envelope = env.CreateElement("SOAP-ENV", "Envelope", NS_SOAPENV);
- env.AppendChild(envelope);
- envelope.SetAttribute("encodingStyle", NS_SOAPENV, "http://schemas.xmlsoap.org/soap/encoding/");
- var rbody = env.CreateElement("SOAP-ENV:Body", NS_SOAPENV);
- env.DocumentElement.AppendChild(rbody);
- IEnumerable<KeyValuePair<string, string>> result;
- _logger.Debug("Received control request {0}", method.Name);
- var user = _user;
- if (string.Equals(method.LocalName, "GetSearchCapabilities", StringComparison.OrdinalIgnoreCase))
- result = HandleGetSearchCapabilities();
- else if (string.Equals(method.LocalName, "GetSortCapabilities", StringComparison.OrdinalIgnoreCase))
- result = HandleGetSortCapabilities();
- else if (string.Equals(method.LocalName, "GetSystemUpdateID", StringComparison.OrdinalIgnoreCase))
- result = HandleGetSystemUpdateID();
- else if (string.Equals(method.LocalName, "Browse", StringComparison.OrdinalIgnoreCase))
- result = HandleBrowse(sparams, user, deviceId);
- else if (string.Equals(method.LocalName, "X_GetFeatureList", StringComparison.OrdinalIgnoreCase))
- result = HandleXGetFeatureList();
- else if (string.Equals(method.LocalName, "X_SetBookmark", StringComparison.OrdinalIgnoreCase))
- result = HandleXSetBookmark(sparams, user);
- else if (string.Equals(method.LocalName, "Search", StringComparison.OrdinalIgnoreCase))
- result = HandleSearch(sparams, user, deviceId);
- else
- throw new ResourceNotFoundException("Unexpected control request name: " + method.LocalName);
- var response = env.CreateElement(String.Format("u:{0}Response", method.LocalName), method.NamespaceURI);
- rbody.AppendChild(response);
- foreach (var i in result)
- {
- var ri = env.CreateElement(i.Key);
- ri.InnerText = i.Value;
- response.AppendChild(ri);
- }
- var controlResponse = new ControlResponse
- {
- Xml = env.OuterXml,
- IsSuccessful = true
- };
- controlResponse.Headers.Add("EXT", string.Empty);
- return controlResponse;
- }
- private ControlResponse GetErrorResponse(Exception ex)
- {
- var env = new XmlDocument();
- env.AppendChild(env.CreateXmlDeclaration("1.0", "utf-8", "yes"));
- var envelope = env.CreateElement("SOAP-ENV", "Envelope", NS_SOAPENV);
- env.AppendChild(envelope);
- envelope.SetAttribute("encodingStyle", NS_SOAPENV, "http://schemas.xmlsoap.org/soap/encoding/");
- var rbody = env.CreateElement("SOAP-ENV:Body", NS_SOAPENV);
- env.DocumentElement.AppendChild(rbody);
- var fault = env.CreateElement("SOAP-ENV", "Fault", NS_SOAPENV);
- var faultCode = env.CreateElement("faultcode");
- faultCode.InnerText = "500";
- fault.AppendChild(faultCode);
- var faultString = env.CreateElement("faultstring");
- faultString.InnerText = ex.ToString();
- fault.AppendChild(faultString);
- var detail = env.CreateDocumentFragment();
- detail.InnerXml = "<detail><UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\"><errorCode>401</errorCode><errorDescription>Invalid Action</errorDescription></UPnPError></detail>";
- fault.AppendChild(detail);
- rbody.AppendChild(fault);
- return new ControlResponse
- {
- Xml = env.OuterXml,
- IsSuccessful = false
- };
- }
- private IEnumerable<KeyValuePair<string, string>> HandleXSetBookmark(IDictionary<string, string> sparams, User user)
- {
- var id = sparams["ObjectID"];
- var item = GetItemFromObjectId(id, user);
- var newbookmark = int.Parse(sparams["PosSecond"], _usCulture);
- var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
- userdata.PlaybackPositionTicks = TimeSpan.FromSeconds(newbookmark).Ticks;
- _userDataManager.SaveUserData(user.Id, item, userdata, UserDataSaveReason.TogglePlayed,
- CancellationToken.None);
- return new Headers();
- }
- private IEnumerable<KeyValuePair<string, string>> HandleGetSearchCapabilities()
- {
- return new Headers { { "SearchCaps", string.Empty } };
- }
- private IEnumerable<KeyValuePair<string, string>> HandleGetSortCapabilities()
- {
- return new Headers { { "SortCaps", string.Empty } };
- }
- private IEnumerable<KeyValuePair<string, string>> HandleGetSystemUpdateID()
- {
- return new Headers { { "Id", _systemUpdateId.ToString(_usCulture) } };
- }
- private IEnumerable<KeyValuePair<string, string>> HandleXGetFeatureList()
- {
- return new Headers { { "FeatureList", GetFeatureListXml() } };
- }
- private string GetFeatureListXml()
- {
- var builder = new StringBuilder();
- builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
- builder.Append("<Features xmlns=\"urn:schemas-upnp-org:av:avs\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:schemas-upnp-org:av:avs http://www.upnp.org/schemas/av/avs.xsd\">");
- builder.Append("<Feature name=\"samsung.com_BASICVIEW\" version=\"1\">");
- builder.Append("<container id=\"I\" type=\"object.item.imageItem\"/>");
- builder.Append("<container id=\"A\" type=\"object.item.audioItem\"/>");
- builder.Append("<container id=\"V\" type=\"object.item.videoItem\"/>");
- builder.Append("</Feature>");
- builder.Append("</Features>");
- return builder.ToString();
- }
- private IEnumerable<KeyValuePair<string, string>> HandleBrowse(Headers sparams, User user, string deviceId)
- {
- var id = sparams["ObjectID"];
- var flag = sparams["BrowseFlag"];
- var filter = new Filter(sparams.GetValueOrDefault("Filter", "*"));
- var sortCriteria = new SortCriteria(sparams.GetValueOrDefault("SortCriteria", ""));
- var provided = 0;
- var requested = 0;
- var start = 0;
- if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out requested) && requested <= 0)
- {
- requested = 0;
- }
- if (sparams.ContainsKey("StartingIndex") && int.TryParse(sparams["StartingIndex"], out start) && start <= 0)
- {
- start = 0;
- }
- //var root = GetItem(id) as IMediaFolder;
- var result = new XmlDocument();
- var didl = result.CreateElement(string.Empty, "DIDL-Lite", NS_DIDL);
- didl.SetAttribute("xmlns:dc", NS_DC);
- didl.SetAttribute("xmlns:dlna", NS_DLNA);
- didl.SetAttribute("xmlns:upnp", NS_UPNP);
- //didl.SetAttribute("xmlns:sec", NS_SEC);
- result.AppendChild(didl);
- var folder = (Folder)GetItemFromObjectId(id, user);
- var children = GetChildrenSorted(folder, user, sortCriteria).ToList();
- var totalCount = children.Count;
- if (string.Equals(flag, "BrowseMetadata"))
- {
- Browse_AddFolder(result, folder, children.Count, filter);
- provided++;
- }
- else
- {
- if (start > 0)
- {
- children = children.Skip(start).ToList();
- }
- if (requested > 0)
- {
- children = children.Take(requested).ToList();
- }
- provided = children.Count;
- foreach (var i in children)
- {
- if (i.IsFolder)
- {
- var f = (Folder)i;
- var childCount = GetChildrenSorted(f, user, sortCriteria).Count();
- Browse_AddFolder(result, f, childCount, filter);
- }
- else
- {
- Browse_AddItem(result, i, user, deviceId, filter);
- }
- }
- }
- var resXML = result.OuterXml;
- return new List<KeyValuePair<string, string>>
- {
- new KeyValuePair<string,string>("Result", resXML),
- new KeyValuePair<string,string>("NumberReturned", provided.ToString(_usCulture)),
- new KeyValuePair<string,string>("TotalMatches", totalCount.ToString(_usCulture)),
- new KeyValuePair<string,string>("UpdateID", _systemUpdateId.ToString(_usCulture))
- };
- }
- private IEnumerable<KeyValuePair<string, string>> HandleSearch(Headers sparams, User user, string deviceId)
- {
- var searchCriteria = new SearchCriteria(sparams.GetValueOrDefault("SearchCriteria", ""));
- var sortCriteria = new SortCriteria(sparams.GetValueOrDefault("SortCriteria", ""));
- var filter = new Filter(sparams.GetValueOrDefault("Filter", "*"));
- // sort example: dc:title, dc:date
- var provided = 0;
- var requested = 0;
- var start = 0;
- if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out requested) && requested <= 0)
- {
- requested = 0;
- }
- if (sparams.ContainsKey("StartingIndex") && int.TryParse(sparams["StartingIndex"], out start) && start <= 0)
- {
- start = 0;
- }
- //var root = GetItem(id) as IMediaFolder;
- var result = new XmlDocument();
- var didl = result.CreateElement(string.Empty, "DIDL-Lite", NS_DIDL);
- didl.SetAttribute("xmlns:dc", NS_DC);
- didl.SetAttribute("xmlns:dlna", NS_DLNA);
- didl.SetAttribute("xmlns:upnp", NS_UPNP);
- //didl.SetAttribute("xmlns:sec", NS_SEC);
- result.AppendChild(didl);
- var folder = (Folder)GetItemFromObjectId(sparams["ContainerID"], user);
- var children = GetChildrenSorted(folder, user, searchCriteria, sortCriteria).ToList();
- var totalCount = children.Count;
- if (start > 0)
- {
- children = children.Skip(start).ToList();
- }
- if (requested > 0)
- {
- children = children.Take(requested).ToList();
- }
- provided = children.Count;
- foreach (var i in children)
- {
- if (i.IsFolder)
- {
- var f = (Folder)i;
- var childCount = GetChildrenSorted(f, user, searchCriteria, sortCriteria).Count();
- Browse_AddFolder(result, f, childCount, filter);
- }
- else
- {
- Browse_AddItem(result, i, user, deviceId, filter);
- }
- }
- var resXML = result.OuterXml;
- return new List<KeyValuePair<string, string>>
- {
- new KeyValuePair<string,string>("Result", resXML),
- new KeyValuePair<string,string>("NumberReturned", provided.ToString(_usCulture)),
- new KeyValuePair<string,string>("TotalMatches", totalCount.ToString(_usCulture)),
- new KeyValuePair<string,string>("UpdateID", _systemUpdateId.ToString(_usCulture))
- };
- }
- private IEnumerable<BaseItem> GetChildrenSorted(Folder folder, User user, SearchCriteria search, SortCriteria sort)
- {
- if (search.SearchType == SearchType.Unknown)
- {
- return GetChildrenSorted(folder, user, sort);
- }
- var items = folder.GetRecursiveChildren(user);
- items = FilterUnsupportedContent(items);
- if (search.SearchType == SearchType.Audio)
- {
- items = items.OfType<Audio>();
- }
- else if (search.SearchType == SearchType.Video)
- {
- items = items.OfType<Video>();
- }
- else if (search.SearchType == SearchType.Image)
- {
- items = items.OfType<Photo>();
- }
- else if (search.SearchType == SearchType.Playlist)
- {
- }
- return SortItems(items, user, sort);
- }
- private IEnumerable<BaseItem> GetChildrenSorted(Folder folder, User user, SortCriteria sort)
- {
- var items = folder.GetChildren(user, true);
- items = FilterUnsupportedContent(items);
- if (folder is Series || folder is Season || folder is BoxSet)
- {
- return items;
- }
- return SortItems(items, user, sort);
- }
- private IEnumerable<BaseItem> SortItems(IEnumerable<BaseItem> items, User user, SortCriteria sort)
- {
- return _libraryManager.Sort(items, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending);
- }
- private IEnumerable<BaseItem> FilterUnsupportedContent(IEnumerable<BaseItem> items)
- {
- return items.Where(i =>
- {
- // Unplayable
- // TODO: Display and prevent playback with restricted flag?
- if (i.LocationType == LocationType.Virtual)
- {
- return false;
- }
- // Unplayable
- // TODO: Display and prevent playback with restricted flag?
- var supportsPlaceHolder = i as ISupportsPlaceHolders;
- if (supportsPlaceHolder != null && supportsPlaceHolder.IsPlaceHolder)
- {
- return false;
- }
- // Upnp renderers won't understand these
- // TODO: Display and prevent playback with restricted flag?
- if (i is Game || i is Book)
- {
- return false;
- }
- return true;
- });
- }
- private BaseItem GetItemFromObjectId(string id, User user)
- {
- return string.IsNullOrWhiteSpace(id) || string.Equals(id, "0", StringComparison.OrdinalIgnoreCase)
- // Samsung sometimes uses 1 as root
- || string.Equals(id, "1", StringComparison.OrdinalIgnoreCase)
- ? user.RootFolder
- : _libraryManager.GetItemById(new Guid(id));
- }
- private void Browse_AddFolder(XmlDocument result, Folder f, int childCount, Filter filter)
- {
- var container = result.CreateElement(string.Empty, "container", NS_DIDL);
- container.SetAttribute("restricted", "0");
- container.SetAttribute("searchable", "1");
- container.SetAttribute("childCount", childCount.ToString(_usCulture));
- container.SetAttribute("id", f.Id.ToString("N"));
- var parent = f.Parent;
- if (parent == null)
- {
- container.SetAttribute("parentID", "0");
- }
- else
- {
- container.SetAttribute("parentID", parent.Id.ToString("N"));
- }
- AddCommonFields(f, container, filter);
- AddCover(f, container);
- result.DocumentElement.AppendChild(container);
- }
- private void AddValue(XmlElement elem, string prefix, string name, string value, string namespaceUri)
- {
- try
- {
- var date = elem.OwnerDocument.CreateElement(prefix, name, namespaceUri);
- date.InnerText = value;
- elem.AppendChild(date);
- }
- catch (XmlException)
- {
- //_logger.Error("Error adding xml value: " + value);
- }
- }
- private void Browse_AddItem(XmlDocument result, BaseItem item, User user, string deviceId, Filter filter)
- {
- var element = result.CreateElement(string.Empty, "item", NS_DIDL);
- element.SetAttribute("restricted", "1");
- element.SetAttribute("id", item.Id.ToString("N"));
- if (item.Parent != null)
- {
- element.SetAttribute("parentID", item.Parent.Id.ToString("N"));
- }
- //AddBookmarkInfo(item, user, element);
- AddGeneralProperties(item, element, filter);
- // refID?
- // storeAttribute(itemNode, object, ClassProperties.REF_ID, false);
- var audio = item as Audio;
- if (audio != null)
- {
- AddAudioResource(element, audio, deviceId, filter);
- }
- var video = item as Video;
- if (video != null)
- {
- AddVideoResource(element, video, deviceId, filter);
- }
- AddCover(item, element);
- result.DocumentElement.AppendChild(element);
- }
- private void AddVideoResource(XmlElement container, Video video, string deviceId, Filter filter)
- {
- var res = container.OwnerDocument.CreateElement(string.Empty, "res", NS_DIDL);
- var sources = _dtoService.GetMediaSources(video);
- int? maxBitrateSetting = null;
- var streamInfo = new StreamBuilder().BuildVideoItem(new VideoOptions
- {
- ItemId = video.Id.ToString("N"),
- MediaSources = sources,
- Profile = _profile,
- DeviceId = deviceId,
- MaxBitrate = maxBitrateSetting
- });
- var url = streamInfo.ToDlnaUrl(_serverAddress);
- res.InnerText = url;
- var mediaSource = sources.First(i => string.Equals(i.Id, streamInfo.MediaSourceId));
- if (mediaSource.RunTimeTicks.HasValue)
- {
- res.SetAttribute("duration", TimeSpan.FromTicks(mediaSource.RunTimeTicks.Value).ToString("c", _usCulture));
- }
- if (filter.Contains("res@size"))
- {
- if (streamInfo.IsDirectStream || streamInfo.EstimateContentLength)
- {
- var size = streamInfo.TargetSize;
- if (size.HasValue)
- {
- res.SetAttribute("size", size.Value.ToString(_usCulture));
- }
- }
- }
- var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video && !string.Equals(i.Codec, "mjpeg", StringComparison.OrdinalIgnoreCase));
- var targetAudioBitrate = streamInfo.TargetAudioBitrate;
- var targetSampleRate = streamInfo.TargetAudioSampleRate;
- var targetChannels = streamInfo.TargetAudioChannels;
- var targetWidth = streamInfo.MaxWidth ?? (videoStream == null ? null : videoStream.Width);
- var targetHeight = streamInfo.MaxHeight ?? (videoStream == null ? null : videoStream.Height);
- var targetVideoCodec = streamInfo.IsDirectStream
- ? (videoStream == null ? null : videoStream.Codec)
- : streamInfo.VideoCodec;
- var targetAudioCodec = streamInfo.TargetAudioCodec;
- var targetBitrate = maxBitrateSetting ?? mediaSource.Bitrate;
- if (targetChannels.HasValue)
- {
- res.SetAttribute("nrAudioChannels", targetChannels.Value.ToString(_usCulture));
- }
- if (filter.Contains("res@resolution"))
- {
- if (targetWidth.HasValue && targetHeight.HasValue)
- {
- res.SetAttribute("resolution", string.Format("{0}x{1}", targetWidth.Value, targetHeight.Value));
- }
- }
- if (targetSampleRate.HasValue)
- {
- res.SetAttribute("sampleFrequency", targetSampleRate.Value.ToString(_usCulture));
- }
- if (targetAudioBitrate.HasValue)
- {
- res.SetAttribute("bitrate", targetAudioBitrate.Value.ToString(_usCulture));
- }
- var mediaProfile = _profile.GetVideoMediaProfile(streamInfo.Container,
- streamInfo.AudioCodec,
- streamInfo.VideoCodec,
- streamInfo.TargetAudioStream,
- streamInfo.TargetVideoStream);
- var formatProfile = mediaProfile == null ? null : mediaProfile.OrgPn;
- if (string.IsNullOrEmpty(formatProfile))
- {
- var format = new MediaFormatProfileResolver().ResolveVideoFormat(streamInfo.Container,
- targetVideoCodec,
- targetAudioCodec,
- targetWidth,
- targetHeight,
- targetBitrate,
- TransportStreamTimestamp.VALID);
- formatProfile = format.HasValue ? format.Value.ToString() : null;
- }
- var filename = url.Substring(0, url.IndexOf('?'));
- var mimeType = mediaProfile == null || string.IsNullOrEmpty(mediaProfile.MimeType)
- ? MimeTypes.GetMimeType(filename)
- : mediaProfile.MimeType;
- var orgOpValue = DlnaMaps.GetOrgOpValue(mediaSource.RunTimeTicks.HasValue, streamInfo.IsDirectStream, streamInfo.TranscodeSeekInfo);
- var orgCi = streamInfo.IsDirectStream ? ";DLNA.ORG_CI=0" : ";DLNA.ORG_CI=1";
- var orgPn = !string.IsNullOrEmpty(formatProfile) ? "DLNA.ORG_PN=:" + formatProfile + ";" : string.Empty;
- res.SetAttribute("protocolInfo", String.Format(
- "http-get:*:{0}:{1}DLNA.ORG_OP={2};DLNA.ORG_CI={3};DLNA.ORG_FLAGS={4}",
- mimeType,
- orgPn,
- orgOpValue,
- orgCi,
- DlnaMaps.DefaultStreaming
- ));
- container.AppendChild(res);
- }
- private void AddAudioResource(XmlElement container, Audio audio, string deviceId, Filter filter)
- {
- var res = container.OwnerDocument.CreateElement(string.Empty, "res", NS_DIDL);
- var sources = _dtoService.GetMediaSources(audio);
- var streamInfo = new StreamBuilder().BuildAudioItem(new AudioOptions
- {
- ItemId = audio.Id.ToString("N"),
- MediaSources = sources,
- Profile = _profile,
- DeviceId = deviceId
- });
- var url = streamInfo.ToDlnaUrl(_serverAddress);
- res.InnerText = url;
- var mediaSource = sources.First(i => string.Equals(i.Id, streamInfo.MediaSourceId));
- if (mediaSource.RunTimeTicks.HasValue)
- {
- res.SetAttribute("duration", TimeSpan.FromTicks(mediaSource.RunTimeTicks.Value).ToString("c", _usCulture));
- }
- if (filter.Contains("res@size"))
- {
- if (streamInfo.IsDirectStream || streamInfo.EstimateContentLength)
- {
- var size = streamInfo.TargetSize;
- if (size.HasValue)
- {
- res.SetAttribute("size", size.Value.ToString(_usCulture));
- }
- }
- }
- var targetAudioBitrate = streamInfo.TargetAudioBitrate;
- var targetSampleRate = streamInfo.TargetAudioSampleRate;
- var targetChannels = streamInfo.TargetAudioChannels;
- if (targetChannels.HasValue)
- {
- res.SetAttribute("nrAudioChannels", targetChannels.Value.ToString(_usCulture));
- }
- if (targetSampleRate.HasValue)
- {
- res.SetAttribute("sampleFrequency", targetSampleRate.Value.ToString(_usCulture));
- }
- if (targetAudioBitrate.HasValue)
- {
- res.SetAttribute("bitrate", targetAudioBitrate.Value.ToString(_usCulture));
- }
- var mediaProfile = _profile.GetAudioMediaProfile(streamInfo.Container,
- streamInfo.AudioCodec,
- streamInfo.TargetAudioStream);
- var formatProfile = mediaProfile == null ? null : mediaProfile.OrgPn;
- if (string.IsNullOrEmpty(formatProfile))
- {
- var format = new MediaFormatProfileResolver().ResolveAudioFormat(streamInfo.Container,
- targetAudioBitrate, targetSampleRate, targetChannels);
- formatProfile = format.HasValue ? format.Value.ToString() : null;
- }
- var filename = url.Substring(0, url.IndexOf('?'));
- var mimeType = mediaProfile == null || string.IsNullOrEmpty(mediaProfile.MimeType)
- ? MimeTypes.GetMimeType(filename)
- : mediaProfile.MimeType;
- var orgOpValue = DlnaMaps.GetOrgOpValue(mediaSource.RunTimeTicks.HasValue, streamInfo.IsDirectStream, streamInfo.TranscodeSeekInfo);
- var orgCi = streamInfo.IsDirectStream ? ";DLNA.ORG_CI=0" : ";DLNA.ORG_CI=1";
- var orgPn = !string.IsNullOrEmpty(formatProfile) ? "DLNA.ORG_PN=:" + formatProfile + ";" : string.Empty;
-
- res.SetAttribute("protocolInfo", String.Format(
- "http-get:*:{0}:{1}DLNA.ORG_OP={2};DLNA.ORG_CI={3};DLNA.ORG_FLAGS={4}",
- mimeType,
- orgPn,
- orgOpValue,
- orgCi,
- DlnaMaps.DefaultStreaming
- ));
- container.AppendChild(res);
- }
- private XmlElement CreateObjectClass(XmlDocument result, BaseItem item)
- {
- var objectClass = result.CreateElement("upnp", "class", NS_UPNP);
- if (item.IsFolder)
- {
- string classType = null;
- if (!_profile.RequiresPlainFolders)
- {
- if (item is MusicAlbum)
- {
- classType = "object.container.musicAlbum";
- }
- if (item is MusicArtist)
- {
- classType = "object.container.musicArtist";
- }
- }
- objectClass.InnerText = classType ?? "object.container.storageFolder";
- }
- else if (string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))
- {
- objectClass.InnerText = "object.item.audioItem.musicTrack";
- }
- else if (string.Equals(item.MediaType, MediaType.Photo, StringComparison.OrdinalIgnoreCase))
- {
- objectClass.InnerText = "object.item.imageItem.photo";
- }
- else if (string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
- {
- if (!_profile.RequiresPlainVideoItems && item is Movie)
- {
- objectClass.InnerText = "object.item.videoItem.movie";
- }
- else
- {
- objectClass.InnerText = "object.item.videoItem";
- }
- }
- else
- {
- throw new NotSupportedException();
- }
- return objectClass;
- }
- private void AddPeople(BaseItem item, XmlElement element)
- {
- foreach (var actor in item.People)
- {
- AddValue(element, "upnp", (actor.Type ?? PersonType.Actor).ToLower(), actor.Name, NS_UPNP);
- }
- }
- private void AddBookmarkInfo(BaseItem item, User user, XmlElement element)
- {
- var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
- if (userdata.PlaybackPositionTicks > 0)
- {
- var dcmInfo = element.OwnerDocument.CreateElement("sec", "dcmInfo", NS_SEC);
- dcmInfo.InnerText = string.Format("BM={0}", Convert.ToInt32(TimeSpan.FromTicks(userdata.PlaybackPositionTicks).TotalSeconds).ToString(_usCulture));
- element.AppendChild(dcmInfo);
- }
- }
- /// <summary>
- /// Adds fields used by both items and folders
- /// </summary>
- /// <param name="item">The item.</param>
- /// <param name="element">The element.</param>
- /// <param name="filter">The filter.</param>
- private void AddCommonFields(BaseItem item, XmlElement element, Filter filter)
- {
- if (filter.Contains("dc:title"))
- {
- AddValue(element, "dc", "title", item.Name, NS_DC);
- }
- element.AppendChild(CreateObjectClass(element.OwnerDocument, item));
- if (filter.Contains("dc:date"))
- {
- if (item.PremiereDate.HasValue)
- {
- AddValue(element, "dc", "date", item.PremiereDate.Value.ToString("o"), NS_DC);
- }
- }
- foreach (var genre in item.Genres)
- {
- AddValue(element, "upnp", "genre", genre, NS_UPNP);
- }
- foreach (var studio in item.Studios)
- {
- AddValue(element, "upnp", "publisher", studio, NS_UPNP);
- }
- if (filter.Contains("dc:description"))
- {
- if (!string.IsNullOrWhiteSpace(item.Overview))
- {
- AddValue(element, "dc", "description", item.Overview, NS_DC);
- }
- }
- if (filter.Contains("upnp:longDescription"))
- {
- if (!string.IsNullOrWhiteSpace(item.Overview))
- {
- AddValue(element, "upnp", "longDescription", item.Overview, NS_UPNP);
- }
- }
- if (!string.IsNullOrEmpty(item.OfficialRating))
- {
- if (filter.Contains("dc:rating"))
- {
- AddValue(element, "dc", "rating", item.OfficialRating, NS_DC);
- }
- if (filter.Contains("upnp:rating"))
- {
- AddValue(element, "upnp", "rating", item.OfficialRating, NS_UPNP);
- }
- }
- AddPeople(item, element);
- }
- private void AddGeneralProperties(BaseItem item, XmlElement element, Filter filter)
- {
- AddCommonFields(item, element, filter);
- var audio = item as Audio;
- if (audio != null)
- {
- foreach (var artist in audio.Artists)
- {
- AddValue(element, "upnp", "artist", artist, NS_UPNP);
- }
- if (!string.IsNullOrEmpty(audio.Album))
- {
- AddValue(element, "upnp", "album", audio.Album, NS_UPNP);
- }
- if (!string.IsNullOrEmpty(audio.AlbumArtist))
- {
- AddValue(element, "upnp", "albumArtist", audio.AlbumArtist, NS_UPNP);
- }
- }
- var album = item as MusicAlbum;
- if (album != null)
- {
- if (!string.IsNullOrEmpty(album.AlbumArtist))
- {
- AddValue(element, "upnp", "artist", album.AlbumArtist, NS_UPNP);
- AddValue(element, "upnp", "albumArtist", album.AlbumArtist, NS_UPNP);
- }
- }
- var musicVideo = item as MusicVideo;
- if (musicVideo != null)
- {
- if (!string.IsNullOrEmpty(musicVideo.Artist))
- {
- AddValue(element, "upnp", "artist", musicVideo.Artist, NS_UPNP);
- }
- if (!string.IsNullOrEmpty(musicVideo.Album))
- {
- AddValue(element, "upnp", "album", musicVideo.Album, NS_UPNP);
- }
- }
- if (item.IndexNumber.HasValue)
- {
- AddValue(element, "upnp", "originalTrackNumber", item.IndexNumber.Value.ToString(_usCulture), NS_UPNP);
- }
- }
- private void AddCover(BaseItem item, XmlElement element)
- {
- var imageInfo = GetImageInfo(item);
- if (imageInfo == null)
- {
- return;
- }
- var result = element.OwnerDocument;
- var albumartUrlInfo = GetImageUrl(imageInfo, _profile.MaxAlbumArtWidth, _profile.MaxAlbumArtHeight);
- var icon = result.CreateElement("upnp", "albumArtURI", NS_UPNP);
- var profile = result.CreateAttribute("dlna", "profileID", NS_DLNA);
- profile.InnerText = _profile.AlbumArtPn;
- icon.SetAttributeNode(profile);
- icon.InnerText = albumartUrlInfo.Url;
- element.AppendChild(icon);
- var iconUrlInfo = GetImageUrl(imageInfo, _profile.MaxIconWidth, _profile.MaxIconHeight);
- icon = result.CreateElement("upnp", "icon", NS_UPNP);
- profile = result.CreateAttribute("dlna", "profileID", NS_DLNA);
- profile.InnerText = _profile.AlbumArtPn;
- icon.SetAttributeNode(profile);
- icon.InnerText = iconUrlInfo.Url;
- element.AppendChild(icon);
- if (!_profile.EnableAlbumArtInDidl)
- {
- return;
- }
- var res = result.CreateElement(string.Empty, "res", NS_DIDL);
- res.InnerText = albumartUrlInfo.Url;
- var width = albumartUrlInfo.Width;
- var height = albumartUrlInfo.Height;
- var mediaProfile = new MediaFormatProfileResolver().ResolveImageFormat("jpg", width, height);
- var orgPn = mediaProfile.HasValue ? "DLNA.ORG_PN=:" + mediaProfile.Value + ";" : string.Empty;
- res.SetAttribute("protocolInfo", string.Format(
- "http-get:*:{1}:{0}DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS={2}",
- orgPn,
- "image/jpeg",
- DlnaMaps.DefaultStreaming
- ));
- if (width.HasValue && height.HasValue)
- {
- res.SetAttribute("resolution", string.Format("{0}x{1}", width.Value, height.Value));
- }
- element.AppendChild(res);
- }
- 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 is Audio || item is Episode)
- {
- item = item.Parents.FirstOrDefault(i => i.HasImage(ImageType.Primary));
- if (item != null)
- {
- return GetImageInfo(item, ImageType.Primary);
- }
- }
- return null;
- }
- private ImageDownloadInfo GetImageInfo(BaseItem item, ImageType type)
- {
- var imageInfo = item.GetImageInfo(type, 0);
- string tag = null;
- try
- {
- var guid = _imageProcessor.GetImageCacheTag(item, ImageType.Primary);
- tag = guid.HasValue ? guid.Value.ToString("N") : null;
- }
- catch
- {
- }
- int? width = null;
- int? height = null;
- try
- {
- var size = _imageProcessor.GetImageSize(imageInfo.Path, imageInfo.DateModified);
- width = Convert.ToInt32(size.Width);
- height = Convert.ToInt32(size.Height);
- }
- catch
- {
- }
- return new ImageDownloadInfo
- {
- ItemId = item.Id.ToString("N"),
- Type = ImageType.Primary,
- ImageTag = tag,
- Width = width,
- Height = height
- };
- }
- class ImageDownloadInfo
- {
- internal string ItemId;
- internal string ImageTag;
- internal ImageType Type;
- internal int? Width;
- internal int? Height;
- }
- class ImageUrlInfo
- {
- internal string Url;
- internal int? Width;
- internal int? Height;
- }
- private ImageUrlInfo GetImageUrl(ImageDownloadInfo info, int? maxWidth, int? maxHeight)
- {
- var url = string.Format("{0}/Items/{1}/Images/{2}?tag={3}&format=jpg",
- _serverAddress,
- info.ItemId,
- info.Type,
- info.ImageTag);
- if (maxWidth.HasValue)
- {
- url += "&maxWidth=" + maxWidth.Value.ToString(_usCulture);
- }
- if (maxHeight.HasValue)
- {
- url += "&maxHeight=" + maxHeight.Value.ToString(_usCulture);
- }
- var width = info.Width;
- var height = info.Height;
- if (width.HasValue && height.HasValue)
- {
- if (maxWidth.HasValue || maxHeight.HasValue)
- {
- var newSize = DrawingUtils.Resize(new ImageSize
- {
- Height = height.Value,
- Width = width.Value
- }, maxWidth: maxWidth, maxHeight: maxHeight);
- width = Convert.ToInt32(newSize.Width);
- height = Convert.ToInt32(newSize.Height);
- }
- }
- return new ImageUrlInfo
- {
- Url = url,
- Width = width,
- Height = height
- };
- }
- }
- }
|