using MediaBrowser.Common.Extensions; using MediaBrowser.Controller.Configuration; 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.Dlna.Didl; using MediaBrowser.Dlna.Server; using MediaBrowser.Dlna.Service; using MediaBrowser.Model.Dlna; 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.ContentDirectory { public class ControlHandler : BaseControlHandler { private readonly ILibraryManager _libraryManager; private readonly IUserDataManager _userDataManager; private readonly User _user; 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_UPNP = "urn:schemas-upnp-org:metadata-1-0/upnp/"; private readonly int _systemUpdateId; private readonly CultureInfo _usCulture = new CultureInfo("en-US"); private readonly DidlBuilder _didlBuilder; private readonly DeviceProfile _profile; public ControlHandler(ILogger logger, ILibraryManager libraryManager, DeviceProfile profile, string serverAddress, IImageProcessor imageProcessor, IUserDataManager userDataManager, User user, int systemUpdateId, IServerConfigurationManager config) : base(config, logger) { _libraryManager = libraryManager; _userDataManager = userDataManager; _user = user; _systemUpdateId = systemUpdateId; _profile = profile; _didlBuilder = new DidlBuilder(profile, user, imageProcessor, serverAddress); } protected override IEnumerable> GetResult(string methodName, Headers methodParams) { var deviceId = "test"; var user = _user; if (string.Equals(methodName, "GetSearchCapabilities", StringComparison.OrdinalIgnoreCase)) return HandleGetSearchCapabilities(); if (string.Equals(methodName, "GetSortCapabilities", StringComparison.OrdinalIgnoreCase)) return HandleGetSortCapabilities(); if (string.Equals(methodName, "GetSystemUpdateID", StringComparison.OrdinalIgnoreCase)) return HandleGetSystemUpdateID(); if (string.Equals(methodName, "Browse", StringComparison.OrdinalIgnoreCase)) return HandleBrowse(methodParams, user, deviceId); if (string.Equals(methodName, "X_GetFeatureList", StringComparison.OrdinalIgnoreCase)) return HandleXGetFeatureList(); if (string.Equals(methodName, "X_SetBookmark", StringComparison.OrdinalIgnoreCase)) return HandleXSetBookmark(methodParams, user); if (string.Equals(methodName, "Search", StringComparison.OrdinalIgnoreCase)) return HandleSearch(methodParams, user, deviceId); throw new ResourceNotFoundException("Unexpected control request name: " + methodName); } private IEnumerable> HandleXSetBookmark(IDictionary 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> HandleGetSearchCapabilities() { return new Headers { { "SearchCaps", string.Empty } }; } private IEnumerable> HandleGetSortCapabilities() { return new Headers { { "SortCaps", string.Empty } }; } private IEnumerable> HandleGetSystemUpdateID() { var headers = new Headers(true); headers.Add("Id", _systemUpdateId.ToString(_usCulture)); return headers; } private IEnumerable> HandleXGetFeatureList() { return new Headers { { "FeatureList", GetFeatureListXml() } }; } private string GetFeatureListXml() { var builder = new StringBuilder(); builder.Append(""); builder.Append(""); builder.Append(""); builder.Append(""); builder.Append(""); builder.Append(""); builder.Append(""); builder.Append(""); return builder.ToString(); } private IEnumerable> 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")) { result.DocumentElement.AppendChild(_didlBuilder.GetFolderElement(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(); result.DocumentElement.AppendChild(_didlBuilder.GetFolderElement(result, f, childCount, filter)); } else { result.DocumentElement.AppendChild(_didlBuilder.GetItemElement(result, i, deviceId, filter)); } } } var resXML = result.OuterXml; return new List> { new KeyValuePair("Result", resXML), new KeyValuePair("NumberReturned", provided.ToString(_usCulture)), new KeyValuePair("TotalMatches", totalCount.ToString(_usCulture)), new KeyValuePair("UpdateID", _systemUpdateId.ToString(_usCulture)) }; } private IEnumerable> 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 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); foreach (var att in _profile.XmlRootAttributes) { didl.SetAttribute(att.Name, att.Value); } 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(); } var provided = children.Count; foreach (var i in children) { if (i.IsFolder) { var f = (Folder)i; var childCount = GetChildrenSorted(f, user, searchCriteria, sortCriteria).Count(); result.DocumentElement.AppendChild(_didlBuilder.GetFolderElement(result, f, childCount, filter)); } else { result.DocumentElement.AppendChild(_didlBuilder.GetItemElement(result, i, deviceId, filter)); } } var resXML = result.OuterXml; return new List> { new KeyValuePair("Result", resXML), new KeyValuePair("NumberReturned", provided.ToString(_usCulture)), new KeyValuePair("TotalMatches", totalCount.ToString(_usCulture)), new KeyValuePair("UpdateID", _systemUpdateId.ToString(_usCulture)) }; } private IEnumerable 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