ControlHandler.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Controller.Dlna;
  4. using MediaBrowser.Controller.Drawing;
  5. using MediaBrowser.Controller.Dto;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.Entities.Audio;
  8. using MediaBrowser.Controller.Entities.Movies;
  9. using MediaBrowser.Controller.Entities.TV;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Model.Dlna;
  12. using MediaBrowser.Model.Drawing;
  13. using MediaBrowser.Model.Entities;
  14. using MediaBrowser.Model.Logging;
  15. using MediaBrowser.Model.Querying;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Globalization;
  19. using System.Linq;
  20. using System.Text;
  21. using System.Threading;
  22. using System.Xml;
  23. namespace MediaBrowser.Dlna.Server
  24. {
  25. public class ControlHandler
  26. {
  27. private readonly ILogger _logger;
  28. private readonly ILibraryManager _libraryManager;
  29. private readonly DeviceProfile _profile;
  30. private readonly IDtoService _dtoService;
  31. private readonly IImageProcessor _imageProcessor;
  32. private readonly IUserDataManager _userDataManager;
  33. private readonly User _user;
  34. private readonly string _serverAddress;
  35. private const string NS_DC = "http://purl.org/dc/elements/1.1/";
  36. private const string NS_DIDL = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/";
  37. private const string NS_DLNA = "urn:schemas-dlna-org:metadata-1-0/";
  38. private const string NS_SEC = "http://www.sec.co.kr/";
  39. private const string NS_SOAPENV = "http://schemas.xmlsoap.org/soap/envelope/";
  40. private const string NS_UPNP = "urn:schemas-upnp-org:metadata-1-0/upnp/";
  41. private readonly int _systemUpdateId;
  42. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  43. public ControlHandler(ILogger logger, ILibraryManager libraryManager, DeviceProfile profile, string serverAddress, IDtoService dtoService, IImageProcessor imageProcessor, IUserDataManager userDataManager, User user, int systemUpdateId)
  44. {
  45. _logger = logger;
  46. _libraryManager = libraryManager;
  47. _profile = profile;
  48. _serverAddress = serverAddress;
  49. _dtoService = dtoService;
  50. _imageProcessor = imageProcessor;
  51. _userDataManager = userDataManager;
  52. _user = user;
  53. _systemUpdateId = systemUpdateId;
  54. }
  55. public ControlResponse ProcessControlRequest(ControlRequest request)
  56. {
  57. try
  58. {
  59. return ProcessControlRequestInternal(request);
  60. }
  61. catch (Exception ex)
  62. {
  63. _logger.ErrorException("Error processing control request", ex);
  64. return GetErrorResponse(ex);
  65. }
  66. }
  67. private ControlResponse ProcessControlRequestInternal(ControlRequest request)
  68. {
  69. var soap = new XmlDocument();
  70. soap.LoadXml(request.InputXml);
  71. var sparams = new Headers();
  72. var body = soap.GetElementsByTagName("Body", NS_SOAPENV).Item(0);
  73. var method = body.FirstChild;
  74. foreach (var p in method.ChildNodes)
  75. {
  76. var e = p as XmlElement;
  77. if (e == null)
  78. {
  79. continue;
  80. }
  81. sparams.Add(e.LocalName, e.InnerText.Trim());
  82. }
  83. var deviceId = "fgd";
  84. var env = new XmlDocument();
  85. env.AppendChild(env.CreateXmlDeclaration("1.0", "utf-8", "yes"));
  86. var envelope = env.CreateElement("SOAP-ENV", "Envelope", NS_SOAPENV);
  87. env.AppendChild(envelope);
  88. envelope.SetAttribute("encodingStyle", NS_SOAPENV, "http://schemas.xmlsoap.org/soap/encoding/");
  89. var rbody = env.CreateElement("SOAP-ENV:Body", NS_SOAPENV);
  90. env.DocumentElement.AppendChild(rbody);
  91. IEnumerable<KeyValuePair<string, string>> result;
  92. _logger.Debug("Received control request {0}", method.Name);
  93. var user = _user;
  94. if (string.Equals(method.LocalName, "GetSearchCapabilities", StringComparison.OrdinalIgnoreCase))
  95. result = HandleGetSearchCapabilities();
  96. else if (string.Equals(method.LocalName, "GetSortCapabilities", StringComparison.OrdinalIgnoreCase))
  97. result = HandleGetSortCapabilities();
  98. else if (string.Equals(method.LocalName, "GetSystemUpdateID", StringComparison.OrdinalIgnoreCase))
  99. result = HandleGetSystemUpdateID();
  100. else if (string.Equals(method.LocalName, "Browse", StringComparison.OrdinalIgnoreCase))
  101. result = HandleBrowse(sparams, user, deviceId);
  102. else if (string.Equals(method.LocalName, "X_GetFeatureList", StringComparison.OrdinalIgnoreCase))
  103. result = HandleXGetFeatureList();
  104. else if (string.Equals(method.LocalName, "X_SetBookmark", StringComparison.OrdinalIgnoreCase))
  105. result = HandleXSetBookmark(sparams, user);
  106. else if (string.Equals(method.LocalName, "Search", StringComparison.OrdinalIgnoreCase))
  107. result = HandleSearch(sparams, user, deviceId);
  108. else
  109. throw new ResourceNotFoundException("Unexpected control request name: " + method.LocalName);
  110. var response = env.CreateElement(String.Format("u:{0}Response", method.LocalName), method.NamespaceURI);
  111. rbody.AppendChild(response);
  112. foreach (var i in result)
  113. {
  114. var ri = env.CreateElement(i.Key);
  115. ri.InnerText = i.Value;
  116. response.AppendChild(ri);
  117. }
  118. var controlResponse = new ControlResponse
  119. {
  120. Xml = env.OuterXml,
  121. IsSuccessful = true
  122. };
  123. controlResponse.Headers.Add("EXT", string.Empty);
  124. return controlResponse;
  125. }
  126. private ControlResponse GetErrorResponse(Exception ex)
  127. {
  128. var env = new XmlDocument();
  129. env.AppendChild(env.CreateXmlDeclaration("1.0", "utf-8", "yes"));
  130. var envelope = env.CreateElement("SOAP-ENV", "Envelope", NS_SOAPENV);
  131. env.AppendChild(envelope);
  132. envelope.SetAttribute("encodingStyle", NS_SOAPENV, "http://schemas.xmlsoap.org/soap/encoding/");
  133. var rbody = env.CreateElement("SOAP-ENV:Body", NS_SOAPENV);
  134. env.DocumentElement.AppendChild(rbody);
  135. var fault = env.CreateElement("SOAP-ENV", "Fault", NS_SOAPENV);
  136. var faultCode = env.CreateElement("faultcode");
  137. faultCode.InnerText = "500";
  138. fault.AppendChild(faultCode);
  139. var faultString = env.CreateElement("faultstring");
  140. faultString.InnerText = ex.ToString();
  141. fault.AppendChild(faultString);
  142. var detail = env.CreateDocumentFragment();
  143. detail.InnerXml = "<detail><UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\"><errorCode>401</errorCode><errorDescription>Invalid Action</errorDescription></UPnPError></detail>";
  144. fault.AppendChild(detail);
  145. rbody.AppendChild(fault);
  146. return new ControlResponse
  147. {
  148. Xml = env.OuterXml,
  149. IsSuccessful = false
  150. };
  151. }
  152. private IEnumerable<KeyValuePair<string, string>> HandleXSetBookmark(IDictionary<string, string> sparams, User user)
  153. {
  154. var id = sparams["ObjectID"];
  155. var item = GetItemFromObjectId(id, user);
  156. var newbookmark = int.Parse(sparams["PosSecond"], _usCulture);
  157. var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
  158. userdata.PlaybackPositionTicks = TimeSpan.FromSeconds(newbookmark).Ticks;
  159. _userDataManager.SaveUserData(user.Id, item, userdata, UserDataSaveReason.TogglePlayed,
  160. CancellationToken.None);
  161. return new Headers();
  162. }
  163. private IEnumerable<KeyValuePair<string, string>> HandleGetSearchCapabilities()
  164. {
  165. return new Headers { { "SearchCaps", string.Empty } };
  166. }
  167. private IEnumerable<KeyValuePair<string, string>> HandleGetSortCapabilities()
  168. {
  169. return new Headers { { "SortCaps", string.Empty } };
  170. }
  171. private IEnumerable<KeyValuePair<string, string>> HandleGetSystemUpdateID()
  172. {
  173. return new Headers { { "Id", _systemUpdateId.ToString(_usCulture) } };
  174. }
  175. private IEnumerable<KeyValuePair<string, string>> HandleXGetFeatureList()
  176. {
  177. return new Headers { { "FeatureList", GetFeatureListXml() } };
  178. }
  179. private string GetFeatureListXml()
  180. {
  181. var builder = new StringBuilder();
  182. builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  183. 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\">");
  184. builder.Append("<Feature name=\"samsung.com_BASICVIEW\" version=\"1\">");
  185. builder.Append("<container id=\"I\" type=\"object.item.imageItem\"/>");
  186. builder.Append("<container id=\"A\" type=\"object.item.audioItem\"/>");
  187. builder.Append("<container id=\"V\" type=\"object.item.videoItem\"/>");
  188. builder.Append("</Feature>");
  189. builder.Append("</Features>");
  190. return builder.ToString();
  191. }
  192. private IEnumerable<KeyValuePair<string, string>> HandleBrowse(Headers sparams, User user, string deviceId)
  193. {
  194. var id = sparams["ObjectID"];
  195. var flag = sparams["BrowseFlag"];
  196. var filter = new Filter(sparams.GetValueOrDefault("Filter", "*"));
  197. var sortCriteria = new SortCriteria(sparams.GetValueOrDefault("SortCriteria", ""));
  198. var provided = 0;
  199. var requested = 0;
  200. var start = 0;
  201. if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out requested) && requested <= 0)
  202. {
  203. requested = 0;
  204. }
  205. if (sparams.ContainsKey("StartingIndex") && int.TryParse(sparams["StartingIndex"], out start) && start <= 0)
  206. {
  207. start = 0;
  208. }
  209. //var root = GetItem(id) as IMediaFolder;
  210. var result = new XmlDocument();
  211. var didl = result.CreateElement(string.Empty, "DIDL-Lite", NS_DIDL);
  212. didl.SetAttribute("xmlns:dc", NS_DC);
  213. didl.SetAttribute("xmlns:dlna", NS_DLNA);
  214. didl.SetAttribute("xmlns:upnp", NS_UPNP);
  215. //didl.SetAttribute("xmlns:sec", NS_SEC);
  216. result.AppendChild(didl);
  217. var folder = (Folder)GetItemFromObjectId(id, user);
  218. var children = GetChildrenSorted(folder, user, sortCriteria).ToList();
  219. var totalCount = children.Count;
  220. if (string.Equals(flag, "BrowseMetadata"))
  221. {
  222. Browse_AddFolder(result, folder, children.Count, filter);
  223. provided++;
  224. }
  225. else
  226. {
  227. if (start > 0)
  228. {
  229. children = children.Skip(start).ToList();
  230. }
  231. if (requested > 0)
  232. {
  233. children = children.Take(requested).ToList();
  234. }
  235. provided = children.Count;
  236. foreach (var i in children)
  237. {
  238. if (i.IsFolder)
  239. {
  240. var f = (Folder)i;
  241. var childCount = GetChildrenSorted(f, user, sortCriteria).Count();
  242. Browse_AddFolder(result, f, childCount, filter);
  243. }
  244. else
  245. {
  246. Browse_AddItem(result, i, user, deviceId, filter);
  247. }
  248. }
  249. }
  250. var resXML = result.OuterXml;
  251. return new List<KeyValuePair<string, string>>
  252. {
  253. new KeyValuePair<string,string>("Result", resXML),
  254. new KeyValuePair<string,string>("NumberReturned", provided.ToString(_usCulture)),
  255. new KeyValuePair<string,string>("TotalMatches", totalCount.ToString(_usCulture)),
  256. new KeyValuePair<string,string>("UpdateID", _systemUpdateId.ToString(_usCulture))
  257. };
  258. }
  259. private IEnumerable<KeyValuePair<string, string>> HandleSearch(Headers sparams, User user, string deviceId)
  260. {
  261. var searchCriteria = new SearchCriteria(sparams.GetValueOrDefault("SearchCriteria", ""));
  262. var sortCriteria = new SortCriteria(sparams.GetValueOrDefault("SortCriteria", ""));
  263. var filter = new Filter(sparams.GetValueOrDefault("Filter", "*"));
  264. // sort example: dc:title, dc:date
  265. var provided = 0;
  266. var requested = 0;
  267. var start = 0;
  268. if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out requested) && requested <= 0)
  269. {
  270. requested = 0;
  271. }
  272. if (sparams.ContainsKey("StartingIndex") && int.TryParse(sparams["StartingIndex"], out start) && start <= 0)
  273. {
  274. start = 0;
  275. }
  276. //var root = GetItem(id) as IMediaFolder;
  277. var result = new XmlDocument();
  278. var didl = result.CreateElement(string.Empty, "DIDL-Lite", NS_DIDL);
  279. didl.SetAttribute("xmlns:dc", NS_DC);
  280. didl.SetAttribute("xmlns:dlna", NS_DLNA);
  281. didl.SetAttribute("xmlns:upnp", NS_UPNP);
  282. //didl.SetAttribute("xmlns:sec", NS_SEC);
  283. result.AppendChild(didl);
  284. var folder = (Folder)GetItemFromObjectId(sparams["ContainerID"], user);
  285. var children = GetChildrenSorted(folder, user, searchCriteria, sortCriteria).ToList();
  286. var totalCount = children.Count;
  287. if (start > 0)
  288. {
  289. children = children.Skip(start).ToList();
  290. }
  291. if (requested > 0)
  292. {
  293. children = children.Take(requested).ToList();
  294. }
  295. provided = children.Count;
  296. foreach (var i in children)
  297. {
  298. if (i.IsFolder)
  299. {
  300. var f = (Folder)i;
  301. var childCount = GetChildrenSorted(f, user, searchCriteria, sortCriteria).Count();
  302. Browse_AddFolder(result, f, childCount, filter);
  303. }
  304. else
  305. {
  306. Browse_AddItem(result, i, user, deviceId, filter);
  307. }
  308. }
  309. var resXML = result.OuterXml;
  310. return new List<KeyValuePair<string, string>>
  311. {
  312. new KeyValuePair<string,string>("Result", resXML),
  313. new KeyValuePair<string,string>("NumberReturned", provided.ToString(_usCulture)),
  314. new KeyValuePair<string,string>("TotalMatches", totalCount.ToString(_usCulture)),
  315. new KeyValuePair<string,string>("UpdateID", _systemUpdateId.ToString(_usCulture))
  316. };
  317. }
  318. private IEnumerable<BaseItem> GetChildrenSorted(Folder folder, User user, SearchCriteria search, SortCriteria sort)
  319. {
  320. if (search.SearchType == SearchType.Unknown)
  321. {
  322. return GetChildrenSorted(folder, user, sort);
  323. }
  324. var items = folder.GetRecursiveChildren(user);
  325. items = FilterUnsupportedContent(items);
  326. if (search.SearchType == SearchType.Audio)
  327. {
  328. items = items.OfType<Audio>();
  329. }
  330. else if (search.SearchType == SearchType.Video)
  331. {
  332. items = items.OfType<Video>();
  333. }
  334. else if (search.SearchType == SearchType.Image)
  335. {
  336. items = items.OfType<Photo>();
  337. }
  338. else if (search.SearchType == SearchType.Playlist)
  339. {
  340. }
  341. return SortItems(items, user, sort);
  342. }
  343. private IEnumerable<BaseItem> GetChildrenSorted(Folder folder, User user, SortCriteria sort)
  344. {
  345. var items = folder.GetChildren(user, true);
  346. items = FilterUnsupportedContent(items);
  347. if (folder is Series || folder is Season || folder is BoxSet)
  348. {
  349. return items;
  350. }
  351. return SortItems(items, user, sort);
  352. }
  353. private IEnumerable<BaseItem> SortItems(IEnumerable<BaseItem> items, User user, SortCriteria sort)
  354. {
  355. return _libraryManager.Sort(items, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending);
  356. }
  357. private IEnumerable<BaseItem> FilterUnsupportedContent(IEnumerable<BaseItem> items)
  358. {
  359. return items.Where(i =>
  360. {
  361. // Unplayable
  362. // TODO: Display and prevent playback with restricted flag?
  363. if (i.LocationType == LocationType.Virtual)
  364. {
  365. return false;
  366. }
  367. // Unplayable
  368. // TODO: Display and prevent playback with restricted flag?
  369. var supportsPlaceHolder = i as ISupportsPlaceHolders;
  370. if (supportsPlaceHolder != null && supportsPlaceHolder.IsPlaceHolder)
  371. {
  372. return false;
  373. }
  374. // Upnp renderers won't understand these
  375. // TODO: Display and prevent playback with restricted flag?
  376. if (i is Game || i is Book)
  377. {
  378. return false;
  379. }
  380. return true;
  381. });
  382. }
  383. private BaseItem GetItemFromObjectId(string id, User user)
  384. {
  385. return string.IsNullOrWhiteSpace(id) || string.Equals(id, "0", StringComparison.OrdinalIgnoreCase)
  386. // Samsung sometimes uses 1 as root
  387. || string.Equals(id, "1", StringComparison.OrdinalIgnoreCase)
  388. ? user.RootFolder
  389. : _libraryManager.GetItemById(new Guid(id));
  390. }
  391. private void Browse_AddFolder(XmlDocument result, Folder f, int childCount, Filter filter)
  392. {
  393. var container = result.CreateElement(string.Empty, "container", NS_DIDL);
  394. container.SetAttribute("restricted", "0");
  395. container.SetAttribute("searchable", "1");
  396. container.SetAttribute("childCount", childCount.ToString(_usCulture));
  397. container.SetAttribute("id", f.Id.ToString("N"));
  398. var parent = f.Parent;
  399. if (parent == null)
  400. {
  401. container.SetAttribute("parentID", "0");
  402. }
  403. else
  404. {
  405. container.SetAttribute("parentID", parent.Id.ToString("N"));
  406. }
  407. AddCommonFields(f, container, filter);
  408. AddCover(f, container);
  409. result.DocumentElement.AppendChild(container);
  410. }
  411. private void AddValue(XmlElement elem, string prefix, string name, string value, string namespaceUri)
  412. {
  413. try
  414. {
  415. var date = elem.OwnerDocument.CreateElement(prefix, name, namespaceUri);
  416. date.InnerText = value;
  417. elem.AppendChild(date);
  418. }
  419. catch (XmlException)
  420. {
  421. //_logger.Error("Error adding xml value: " + value);
  422. }
  423. }
  424. private void Browse_AddItem(XmlDocument result, BaseItem item, User user, string deviceId, Filter filter)
  425. {
  426. var element = result.CreateElement(string.Empty, "item", NS_DIDL);
  427. element.SetAttribute("restricted", "1");
  428. element.SetAttribute("id", item.Id.ToString("N"));
  429. if (item.Parent != null)
  430. {
  431. element.SetAttribute("parentID", item.Parent.Id.ToString("N"));
  432. }
  433. //AddBookmarkInfo(item, user, element);
  434. AddGeneralProperties(item, element, filter);
  435. // refID?
  436. // storeAttribute(itemNode, object, ClassProperties.REF_ID, false);
  437. var audio = item as Audio;
  438. if (audio != null)
  439. {
  440. AddAudioResource(element, audio, deviceId, filter);
  441. }
  442. var video = item as Video;
  443. if (video != null)
  444. {
  445. AddVideoResource(element, video, deviceId, filter);
  446. }
  447. AddCover(item, element);
  448. result.DocumentElement.AppendChild(element);
  449. }
  450. private void AddVideoResource(XmlElement container, Video video, string deviceId, Filter filter)
  451. {
  452. var res = container.OwnerDocument.CreateElement(string.Empty, "res", NS_DIDL);
  453. var sources = _dtoService.GetMediaSources(video);
  454. int? maxBitrateSetting = null;
  455. var streamInfo = new StreamBuilder().BuildVideoItem(new VideoOptions
  456. {
  457. ItemId = video.Id.ToString("N"),
  458. MediaSources = sources,
  459. Profile = _profile,
  460. DeviceId = deviceId,
  461. MaxBitrate = maxBitrateSetting
  462. });
  463. var url = streamInfo.ToDlnaUrl(_serverAddress);
  464. res.InnerText = url;
  465. var mediaSource = sources.First(i => string.Equals(i.Id, streamInfo.MediaSourceId));
  466. if (mediaSource.RunTimeTicks.HasValue)
  467. {
  468. res.SetAttribute("duration", TimeSpan.FromTicks(mediaSource.RunTimeTicks.Value).ToString("c", _usCulture));
  469. }
  470. if (filter.Contains("res@size"))
  471. {
  472. if (streamInfo.IsDirectStream || streamInfo.EstimateContentLength)
  473. {
  474. var size = streamInfo.TargetSize;
  475. if (size.HasValue)
  476. {
  477. res.SetAttribute("size", size.Value.ToString(_usCulture));
  478. }
  479. }
  480. }
  481. var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video && !string.Equals(i.Codec, "mjpeg", StringComparison.OrdinalIgnoreCase));
  482. var targetAudioBitrate = streamInfo.TargetAudioBitrate;
  483. var targetSampleRate = streamInfo.TargetAudioSampleRate;
  484. var targetChannels = streamInfo.TargetAudioChannels;
  485. var targetWidth = streamInfo.MaxWidth ?? (videoStream == null ? null : videoStream.Width);
  486. var targetHeight = streamInfo.MaxHeight ?? (videoStream == null ? null : videoStream.Height);
  487. var targetVideoCodec = streamInfo.IsDirectStream
  488. ? (videoStream == null ? null : videoStream.Codec)
  489. : streamInfo.VideoCodec;
  490. var targetAudioCodec = streamInfo.TargetAudioCodec;
  491. var targetBitrate = maxBitrateSetting ?? mediaSource.Bitrate;
  492. if (targetChannels.HasValue)
  493. {
  494. res.SetAttribute("nrAudioChannels", targetChannels.Value.ToString(_usCulture));
  495. }
  496. if (filter.Contains("res@resolution"))
  497. {
  498. if (targetWidth.HasValue && targetHeight.HasValue)
  499. {
  500. res.SetAttribute("resolution", string.Format("{0}x{1}", targetWidth.Value, targetHeight.Value));
  501. }
  502. }
  503. if (targetSampleRate.HasValue)
  504. {
  505. res.SetAttribute("sampleFrequency", targetSampleRate.Value.ToString(_usCulture));
  506. }
  507. if (targetAudioBitrate.HasValue)
  508. {
  509. res.SetAttribute("bitrate", targetAudioBitrate.Value.ToString(_usCulture));
  510. }
  511. var mediaProfile = _profile.GetVideoMediaProfile(streamInfo.Container,
  512. streamInfo.AudioCodec,
  513. streamInfo.VideoCodec,
  514. streamInfo.TargetAudioStream,
  515. streamInfo.TargetVideoStream);
  516. var formatProfile = mediaProfile == null ? null : mediaProfile.OrgPn;
  517. if (string.IsNullOrEmpty(formatProfile))
  518. {
  519. var format = new MediaFormatProfileResolver().ResolveVideoFormat(streamInfo.Container,
  520. targetVideoCodec,
  521. targetAudioCodec,
  522. targetWidth,
  523. targetHeight,
  524. targetBitrate,
  525. TransportStreamTimestamp.VALID);
  526. formatProfile = format.HasValue ? format.Value.ToString() : null;
  527. }
  528. var filename = url.Substring(0, url.IndexOf('?'));
  529. var mimeType = mediaProfile == null || string.IsNullOrEmpty(mediaProfile.MimeType)
  530. ? MimeTypes.GetMimeType(filename)
  531. : mediaProfile.MimeType;
  532. var orgOpValue = DlnaMaps.GetOrgOpValue(mediaSource.RunTimeTicks.HasValue, streamInfo.IsDirectStream, streamInfo.TranscodeSeekInfo);
  533. var orgCi = streamInfo.IsDirectStream ? ";DLNA.ORG_CI=0" : ";DLNA.ORG_CI=1";
  534. var orgPn = !string.IsNullOrEmpty(formatProfile) ? "DLNA.ORG_PN=:" + formatProfile + ";" : string.Empty;
  535. res.SetAttribute("protocolInfo", String.Format(
  536. "http-get:*:{0}:{1}DLNA.ORG_OP={2};DLNA.ORG_CI={3};DLNA.ORG_FLAGS={4}",
  537. mimeType,
  538. orgPn,
  539. orgOpValue,
  540. orgCi,
  541. DlnaMaps.DefaultStreaming
  542. ));
  543. container.AppendChild(res);
  544. }
  545. private void AddAudioResource(XmlElement container, Audio audio, string deviceId, Filter filter)
  546. {
  547. var res = container.OwnerDocument.CreateElement(string.Empty, "res", NS_DIDL);
  548. var sources = _dtoService.GetMediaSources(audio);
  549. var streamInfo = new StreamBuilder().BuildAudioItem(new AudioOptions
  550. {
  551. ItemId = audio.Id.ToString("N"),
  552. MediaSources = sources,
  553. Profile = _profile,
  554. DeviceId = deviceId
  555. });
  556. var url = streamInfo.ToDlnaUrl(_serverAddress);
  557. res.InnerText = url;
  558. var mediaSource = sources.First(i => string.Equals(i.Id, streamInfo.MediaSourceId));
  559. if (mediaSource.RunTimeTicks.HasValue)
  560. {
  561. res.SetAttribute("duration", TimeSpan.FromTicks(mediaSource.RunTimeTicks.Value).ToString("c", _usCulture));
  562. }
  563. if (filter.Contains("res@size"))
  564. {
  565. if (streamInfo.IsDirectStream || streamInfo.EstimateContentLength)
  566. {
  567. var size = streamInfo.TargetSize;
  568. if (size.HasValue)
  569. {
  570. res.SetAttribute("size", size.Value.ToString(_usCulture));
  571. }
  572. }
  573. }
  574. var targetAudioBitrate = streamInfo.TargetAudioBitrate;
  575. var targetSampleRate = streamInfo.TargetAudioSampleRate;
  576. var targetChannels = streamInfo.TargetAudioChannels;
  577. if (targetChannels.HasValue)
  578. {
  579. res.SetAttribute("nrAudioChannels", targetChannels.Value.ToString(_usCulture));
  580. }
  581. if (targetSampleRate.HasValue)
  582. {
  583. res.SetAttribute("sampleFrequency", targetSampleRate.Value.ToString(_usCulture));
  584. }
  585. if (targetAudioBitrate.HasValue)
  586. {
  587. res.SetAttribute("bitrate", targetAudioBitrate.Value.ToString(_usCulture));
  588. }
  589. var mediaProfile = _profile.GetAudioMediaProfile(streamInfo.Container,
  590. streamInfo.AudioCodec,
  591. streamInfo.TargetAudioStream);
  592. var formatProfile = mediaProfile == null ? null : mediaProfile.OrgPn;
  593. if (string.IsNullOrEmpty(formatProfile))
  594. {
  595. var format = new MediaFormatProfileResolver().ResolveAudioFormat(streamInfo.Container,
  596. targetAudioBitrate, targetSampleRate, targetChannels);
  597. formatProfile = format.HasValue ? format.Value.ToString() : null;
  598. }
  599. var filename = url.Substring(0, url.IndexOf('?'));
  600. var mimeType = mediaProfile == null || string.IsNullOrEmpty(mediaProfile.MimeType)
  601. ? MimeTypes.GetMimeType(filename)
  602. : mediaProfile.MimeType;
  603. var orgOpValue = DlnaMaps.GetOrgOpValue(mediaSource.RunTimeTicks.HasValue, streamInfo.IsDirectStream, streamInfo.TranscodeSeekInfo);
  604. var orgCi = streamInfo.IsDirectStream ? ";DLNA.ORG_CI=0" : ";DLNA.ORG_CI=1";
  605. var orgPn = !string.IsNullOrEmpty(formatProfile) ? "DLNA.ORG_PN=:" + formatProfile + ";" : string.Empty;
  606. res.SetAttribute("protocolInfo", String.Format(
  607. "http-get:*:{0}:{1}DLNA.ORG_OP={2};DLNA.ORG_CI={3};DLNA.ORG_FLAGS={4}",
  608. mimeType,
  609. orgPn,
  610. orgOpValue,
  611. orgCi,
  612. DlnaMaps.DefaultStreaming
  613. ));
  614. container.AppendChild(res);
  615. }
  616. private XmlElement CreateObjectClass(XmlDocument result, BaseItem item)
  617. {
  618. var objectClass = result.CreateElement("upnp", "class", NS_UPNP);
  619. if (item.IsFolder)
  620. {
  621. string classType = null;
  622. if (!_profile.RequiresPlainFolders)
  623. {
  624. if (item is MusicAlbum)
  625. {
  626. classType = "object.container.musicAlbum";
  627. }
  628. if (item is MusicArtist)
  629. {
  630. classType = "object.container.musicArtist";
  631. }
  632. }
  633. objectClass.InnerText = classType ?? "object.container.storageFolder";
  634. }
  635. else if (string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))
  636. {
  637. objectClass.InnerText = "object.item.audioItem.musicTrack";
  638. }
  639. else if (string.Equals(item.MediaType, MediaType.Photo, StringComparison.OrdinalIgnoreCase))
  640. {
  641. objectClass.InnerText = "object.item.imageItem.photo";
  642. }
  643. else if (string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
  644. {
  645. if (!_profile.RequiresPlainVideoItems && item is Movie)
  646. {
  647. objectClass.InnerText = "object.item.videoItem.movie";
  648. }
  649. else
  650. {
  651. objectClass.InnerText = "object.item.videoItem";
  652. }
  653. }
  654. else
  655. {
  656. throw new NotSupportedException();
  657. }
  658. return objectClass;
  659. }
  660. private void AddPeople(BaseItem item, XmlElement element)
  661. {
  662. foreach (var actor in item.People)
  663. {
  664. AddValue(element, "upnp", (actor.Type ?? PersonType.Actor).ToLower(), actor.Name, NS_UPNP);
  665. }
  666. }
  667. private void AddBookmarkInfo(BaseItem item, User user, XmlElement element)
  668. {
  669. var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
  670. if (userdata.PlaybackPositionTicks > 0)
  671. {
  672. var dcmInfo = element.OwnerDocument.CreateElement("sec", "dcmInfo", NS_SEC);
  673. dcmInfo.InnerText = string.Format("BM={0}", Convert.ToInt32(TimeSpan.FromTicks(userdata.PlaybackPositionTicks).TotalSeconds).ToString(_usCulture));
  674. element.AppendChild(dcmInfo);
  675. }
  676. }
  677. /// <summary>
  678. /// Adds fields used by both items and folders
  679. /// </summary>
  680. /// <param name="item">The item.</param>
  681. /// <param name="element">The element.</param>
  682. /// <param name="filter">The filter.</param>
  683. private void AddCommonFields(BaseItem item, XmlElement element, Filter filter)
  684. {
  685. if (filter.Contains("dc:title"))
  686. {
  687. AddValue(element, "dc", "title", item.Name, NS_DC);
  688. }
  689. element.AppendChild(CreateObjectClass(element.OwnerDocument, item));
  690. if (filter.Contains("dc:date"))
  691. {
  692. if (item.PremiereDate.HasValue)
  693. {
  694. AddValue(element, "dc", "date", item.PremiereDate.Value.ToString("o"), NS_DC);
  695. }
  696. }
  697. foreach (var genre in item.Genres)
  698. {
  699. AddValue(element, "upnp", "genre", genre, NS_UPNP);
  700. }
  701. foreach (var studio in item.Studios)
  702. {
  703. AddValue(element, "upnp", "publisher", studio, NS_UPNP);
  704. }
  705. if (filter.Contains("dc:description"))
  706. {
  707. if (!string.IsNullOrWhiteSpace(item.Overview))
  708. {
  709. AddValue(element, "dc", "description", item.Overview, NS_DC);
  710. }
  711. }
  712. if (filter.Contains("upnp:longDescription"))
  713. {
  714. if (!string.IsNullOrWhiteSpace(item.Overview))
  715. {
  716. AddValue(element, "upnp", "longDescription", item.Overview, NS_UPNP);
  717. }
  718. }
  719. if (!string.IsNullOrEmpty(item.OfficialRating))
  720. {
  721. if (filter.Contains("dc:rating"))
  722. {
  723. AddValue(element, "dc", "rating", item.OfficialRating, NS_DC);
  724. }
  725. if (filter.Contains("upnp:rating"))
  726. {
  727. AddValue(element, "upnp", "rating", item.OfficialRating, NS_UPNP);
  728. }
  729. }
  730. AddPeople(item, element);
  731. }
  732. private void AddGeneralProperties(BaseItem item, XmlElement element, Filter filter)
  733. {
  734. AddCommonFields(item, element, filter);
  735. var audio = item as Audio;
  736. if (audio != null)
  737. {
  738. foreach (var artist in audio.Artists)
  739. {
  740. AddValue(element, "upnp", "artist", artist, NS_UPNP);
  741. }
  742. if (!string.IsNullOrEmpty(audio.Album))
  743. {
  744. AddValue(element, "upnp", "album", audio.Album, NS_UPNP);
  745. }
  746. if (!string.IsNullOrEmpty(audio.AlbumArtist))
  747. {
  748. AddValue(element, "upnp", "albumArtist", audio.AlbumArtist, NS_UPNP);
  749. }
  750. }
  751. var album = item as MusicAlbum;
  752. if (album != null)
  753. {
  754. if (!string.IsNullOrEmpty(album.AlbumArtist))
  755. {
  756. AddValue(element, "upnp", "artist", album.AlbumArtist, NS_UPNP);
  757. AddValue(element, "upnp", "albumArtist", album.AlbumArtist, NS_UPNP);
  758. }
  759. }
  760. var musicVideo = item as MusicVideo;
  761. if (musicVideo != null)
  762. {
  763. if (!string.IsNullOrEmpty(musicVideo.Artist))
  764. {
  765. AddValue(element, "upnp", "artist", musicVideo.Artist, NS_UPNP);
  766. }
  767. if (!string.IsNullOrEmpty(musicVideo.Album))
  768. {
  769. AddValue(element, "upnp", "album", musicVideo.Album, NS_UPNP);
  770. }
  771. }
  772. if (item.IndexNumber.HasValue)
  773. {
  774. AddValue(element, "upnp", "originalTrackNumber", item.IndexNumber.Value.ToString(_usCulture), NS_UPNP);
  775. }
  776. }
  777. private void AddCover(BaseItem item, XmlElement element)
  778. {
  779. var imageInfo = GetImageInfo(item);
  780. if (imageInfo == null)
  781. {
  782. return;
  783. }
  784. var result = element.OwnerDocument;
  785. var albumartUrlInfo = GetImageUrl(imageInfo, _profile.MaxAlbumArtWidth, _profile.MaxAlbumArtHeight);
  786. var icon = result.CreateElement("upnp", "albumArtURI", NS_UPNP);
  787. var profile = result.CreateAttribute("dlna", "profileID", NS_DLNA);
  788. profile.InnerText = _profile.AlbumArtPn;
  789. icon.SetAttributeNode(profile);
  790. icon.InnerText = albumartUrlInfo.Url;
  791. element.AppendChild(icon);
  792. var iconUrlInfo = GetImageUrl(imageInfo, _profile.MaxIconWidth, _profile.MaxIconHeight);
  793. icon = result.CreateElement("upnp", "icon", NS_UPNP);
  794. profile = result.CreateAttribute("dlna", "profileID", NS_DLNA);
  795. profile.InnerText = _profile.AlbumArtPn;
  796. icon.SetAttributeNode(profile);
  797. icon.InnerText = iconUrlInfo.Url;
  798. element.AppendChild(icon);
  799. if (!_profile.EnableAlbumArtInDidl)
  800. {
  801. return;
  802. }
  803. var res = result.CreateElement(string.Empty, "res", NS_DIDL);
  804. res.InnerText = albumartUrlInfo.Url;
  805. var width = albumartUrlInfo.Width;
  806. var height = albumartUrlInfo.Height;
  807. var mediaProfile = new MediaFormatProfileResolver().ResolveImageFormat("jpg", width, height);
  808. var orgPn = mediaProfile.HasValue ? "DLNA.ORG_PN=:" + mediaProfile.Value + ";" : string.Empty;
  809. res.SetAttribute("protocolInfo", string.Format(
  810. "http-get:*:{1}:{0}DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS={2}",
  811. orgPn,
  812. "image/jpeg",
  813. DlnaMaps.DefaultStreaming
  814. ));
  815. if (width.HasValue && height.HasValue)
  816. {
  817. res.SetAttribute("resolution", string.Format("{0}x{1}", width.Value, height.Value));
  818. }
  819. element.AppendChild(res);
  820. }
  821. private ImageDownloadInfo GetImageInfo(BaseItem item)
  822. {
  823. if (item.HasImage(ImageType.Primary))
  824. {
  825. return GetImageInfo(item, ImageType.Primary);
  826. }
  827. if (item.HasImage(ImageType.Thumb))
  828. {
  829. return GetImageInfo(item, ImageType.Thumb);
  830. }
  831. if (item is Audio || item is Episode)
  832. {
  833. item = item.Parents.FirstOrDefault(i => i.HasImage(ImageType.Primary));
  834. if (item != null)
  835. {
  836. return GetImageInfo(item, ImageType.Primary);
  837. }
  838. }
  839. return null;
  840. }
  841. private ImageDownloadInfo GetImageInfo(BaseItem item, ImageType type)
  842. {
  843. var imageInfo = item.GetImageInfo(type, 0);
  844. string tag = null;
  845. try
  846. {
  847. var guid = _imageProcessor.GetImageCacheTag(item, ImageType.Primary);
  848. tag = guid.HasValue ? guid.Value.ToString("N") : null;
  849. }
  850. catch
  851. {
  852. }
  853. int? width = null;
  854. int? height = null;
  855. try
  856. {
  857. var size = _imageProcessor.GetImageSize(imageInfo.Path, imageInfo.DateModified);
  858. width = Convert.ToInt32(size.Width);
  859. height = Convert.ToInt32(size.Height);
  860. }
  861. catch
  862. {
  863. }
  864. return new ImageDownloadInfo
  865. {
  866. ItemId = item.Id.ToString("N"),
  867. Type = ImageType.Primary,
  868. ImageTag = tag,
  869. Width = width,
  870. Height = height
  871. };
  872. }
  873. class ImageDownloadInfo
  874. {
  875. internal string ItemId;
  876. internal string ImageTag;
  877. internal ImageType Type;
  878. internal int? Width;
  879. internal int? Height;
  880. }
  881. class ImageUrlInfo
  882. {
  883. internal string Url;
  884. internal int? Width;
  885. internal int? Height;
  886. }
  887. private ImageUrlInfo GetImageUrl(ImageDownloadInfo info, int? maxWidth, int? maxHeight)
  888. {
  889. var url = string.Format("{0}/Items/{1}/Images/{2}?tag={3}&format=jpg",
  890. _serverAddress,
  891. info.ItemId,
  892. info.Type,
  893. info.ImageTag);
  894. if (maxWidth.HasValue)
  895. {
  896. url += "&maxWidth=" + maxWidth.Value.ToString(_usCulture);
  897. }
  898. if (maxHeight.HasValue)
  899. {
  900. url += "&maxHeight=" + maxHeight.Value.ToString(_usCulture);
  901. }
  902. var width = info.Width;
  903. var height = info.Height;
  904. if (width.HasValue && height.HasValue)
  905. {
  906. if (maxWidth.HasValue || maxHeight.HasValue)
  907. {
  908. var newSize = DrawingUtils.Resize(new ImageSize
  909. {
  910. Height = height.Value,
  911. Width = width.Value
  912. }, maxWidth: maxWidth, maxHeight: maxHeight);
  913. width = Convert.ToInt32(newSize.Width);
  914. height = Convert.ToInt32(newSize.Height);
  915. }
  916. }
  917. return new ImageUrlInfo
  918. {
  919. Url = url,
  920. Width = width,
  921. Height = height
  922. };
  923. }
  924. }
  925. }