DidlBuilder.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Xml;
  7. using Emby.Dlna.Configuration;
  8. using Emby.Dlna.ContentDirectory;
  9. using MediaBrowser.Controller.Channels;
  10. using MediaBrowser.Controller.Drawing;
  11. using MediaBrowser.Controller.Entities;
  12. using MediaBrowser.Controller.Entities.Audio;
  13. using MediaBrowser.Controller.Entities.Movies;
  14. using MediaBrowser.Controller.Entities.TV;
  15. using MediaBrowser.Controller.Library;
  16. using MediaBrowser.Controller.MediaEncoding;
  17. using MediaBrowser.Controller.Playlists;
  18. using MediaBrowser.Model.Dlna;
  19. using MediaBrowser.Model.Drawing;
  20. using MediaBrowser.Model.Entities;
  21. using MediaBrowser.Model.Extensions;
  22. using MediaBrowser.Model.Globalization;
  23. using MediaBrowser.Model.Net;
  24. using Microsoft.Extensions.Logging;
  25. namespace Emby.Dlna.Didl
  26. {
  27. public class DidlBuilder
  28. {
  29. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  30. private const string NS_DIDL = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/";
  31. private const string NS_DC = "http://purl.org/dc/elements/1.1/";
  32. private const string NS_UPNP = "urn:schemas-upnp-org:metadata-1-0/upnp/";
  33. private const string NS_DLNA = "urn:schemas-dlna-org:metadata-1-0/";
  34. private readonly DeviceProfile _profile;
  35. private readonly IImageProcessor _imageProcessor;
  36. private readonly string _serverAddress;
  37. private readonly string _accessToken;
  38. private readonly User _user;
  39. private readonly IUserDataManager _userDataManager;
  40. private readonly ILocalizationManager _localization;
  41. private readonly IMediaSourceManager _mediaSourceManager;
  42. private readonly ILogger _logger;
  43. private readonly ILibraryManager _libraryManager;
  44. private readonly IMediaEncoder _mediaEncoder;
  45. public DidlBuilder(DeviceProfile profile, User user, IImageProcessor imageProcessor, string serverAddress, string accessToken, IUserDataManager userDataManager, ILocalizationManager localization, IMediaSourceManager mediaSourceManager, ILogger logger, ILibraryManager libraryManager, IMediaEncoder mediaEncoder)
  46. {
  47. _profile = profile;
  48. _imageProcessor = imageProcessor;
  49. _serverAddress = serverAddress;
  50. _userDataManager = userDataManager;
  51. _localization = localization;
  52. _mediaSourceManager = mediaSourceManager;
  53. _logger = logger;
  54. _libraryManager = libraryManager;
  55. _mediaEncoder = mediaEncoder;
  56. _accessToken = accessToken;
  57. _user = user;
  58. }
  59. public static string NormalizeDlnaMediaUrl(string url)
  60. {
  61. return url + "&dlnaheaders=true";
  62. }
  63. public string GetItemDidl(DlnaOptions options, BaseItem item, User user, BaseItem context, string deviceId, Filter filter, StreamInfo streamInfo)
  64. {
  65. var settings = new XmlWriterSettings
  66. {
  67. Encoding = Encoding.UTF8,
  68. CloseOutput = false,
  69. OmitXmlDeclaration = true,
  70. ConformanceLevel = ConformanceLevel.Fragment
  71. };
  72. using (StringWriter builder = new StringWriterWithEncoding(Encoding.UTF8))
  73. {
  74. using (var writer = XmlWriter.Create(builder, settings))
  75. {
  76. //writer.WriteStartDocument();
  77. writer.WriteStartElement(string.Empty, "DIDL-Lite", NS_DIDL);
  78. writer.WriteAttributeString("xmlns", "dc", null, NS_DC);
  79. writer.WriteAttributeString("xmlns", "dlna", null, NS_DLNA);
  80. writer.WriteAttributeString("xmlns", "upnp", null, NS_UPNP);
  81. //didl.SetAttribute("xmlns:sec", NS_SEC);
  82. WriteXmlRootAttributes(_profile, writer);
  83. WriteItemElement(options, writer, item, user, context, null, deviceId, filter, streamInfo);
  84. writer.WriteFullEndElement();
  85. //writer.WriteEndDocument();
  86. }
  87. return builder.ToString();
  88. }
  89. }
  90. public static void WriteXmlRootAttributes(DeviceProfile profile, XmlWriter writer)
  91. {
  92. foreach (var att in profile.XmlRootAttributes)
  93. {
  94. var parts = att.Name.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
  95. if (parts.Length == 2)
  96. {
  97. writer.WriteAttributeString(parts[0], parts[1], null, att.Value);
  98. }
  99. else
  100. {
  101. writer.WriteAttributeString(att.Name, att.Value);
  102. }
  103. }
  104. }
  105. public void WriteItemElement(DlnaOptions options,
  106. XmlWriter writer,
  107. BaseItem item,
  108. User user,
  109. BaseItem context,
  110. StubType? contextStubType,
  111. string deviceId,
  112. Filter filter,
  113. StreamInfo streamInfo = null)
  114. {
  115. var clientId = GetClientId(item, null);
  116. writer.WriteStartElement(string.Empty, "item", NS_DIDL);
  117. writer.WriteAttributeString("restricted", "1");
  118. writer.WriteAttributeString("id", clientId);
  119. if (context != null)
  120. {
  121. writer.WriteAttributeString("parentID", GetClientId(context, contextStubType));
  122. }
  123. else
  124. {
  125. var parent = item.DisplayParentId;
  126. if (!parent.Equals(Guid.Empty))
  127. {
  128. writer.WriteAttributeString("parentID", GetClientId(parent, null));
  129. }
  130. }
  131. AddGeneralProperties(item, null, context, writer, filter);
  132. AddSamsungBookmarkInfo(item, user, writer);
  133. // refID?
  134. // storeAttribute(itemNode, object, ClassProperties.REF_ID, false);
  135. var hasMediaSources = item as IHasMediaSources;
  136. if (hasMediaSources != null)
  137. {
  138. if (string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))
  139. {
  140. AddAudioResource(options, writer, item, deviceId, filter, streamInfo);
  141. }
  142. else if (string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
  143. {
  144. AddVideoResource(options, writer, item, deviceId, filter, streamInfo);
  145. }
  146. }
  147. AddCover(item, context, null, writer);
  148. writer.WriteFullEndElement();
  149. }
  150. private string GetMimeType(string input)
  151. {
  152. var mime = MimeTypes.GetMimeType(input);
  153. // TODO: Instead of being hard-coded here, this should probably be moved into all of the existing profiles
  154. if (string.Equals(mime, "video/mp2t", StringComparison.OrdinalIgnoreCase))
  155. {
  156. mime = "video/mpeg";
  157. }
  158. return mime;
  159. }
  160. private void AddVideoResource(DlnaOptions options, XmlWriter writer, BaseItem video, string deviceId, Filter filter, StreamInfo streamInfo = null)
  161. {
  162. if (streamInfo == null)
  163. {
  164. var sources = _mediaSourceManager.GetStaticMediaSources(video, true, _user);
  165. streamInfo = new StreamBuilder(_mediaEncoder, _logger).BuildVideoItem(new VideoOptions
  166. {
  167. ItemId = video.Id,
  168. MediaSources = sources.ToArray(),
  169. Profile = _profile,
  170. DeviceId = deviceId,
  171. MaxBitrate = _profile.MaxStreamingBitrate
  172. });
  173. }
  174. var targetWidth = streamInfo.TargetWidth;
  175. var targetHeight = streamInfo.TargetHeight;
  176. var contentFeatureList = new ContentFeatureBuilder(_profile).BuildVideoHeader(streamInfo.Container,
  177. streamInfo.TargetVideoCodec.FirstOrDefault(),
  178. streamInfo.TargetAudioCodec.FirstOrDefault(),
  179. targetWidth,
  180. targetHeight,
  181. streamInfo.TargetVideoBitDepth,
  182. streamInfo.TargetVideoBitrate,
  183. streamInfo.TargetTimestamp,
  184. streamInfo.IsDirectStream,
  185. streamInfo.RunTimeTicks ?? 0,
  186. streamInfo.TargetVideoProfile,
  187. streamInfo.TargetVideoLevel,
  188. streamInfo.TargetFramerate ?? 0,
  189. streamInfo.TargetPacketLength,
  190. streamInfo.TranscodeSeekInfo,
  191. streamInfo.IsTargetAnamorphic,
  192. streamInfo.IsTargetInterlaced,
  193. streamInfo.TargetRefFrames,
  194. streamInfo.TargetVideoStreamCount,
  195. streamInfo.TargetAudioStreamCount,
  196. streamInfo.TargetVideoCodecTag,
  197. streamInfo.IsTargetAVC);
  198. foreach (var contentFeature in contentFeatureList)
  199. {
  200. AddVideoResource(writer, video, deviceId, filter, contentFeature, streamInfo);
  201. }
  202. var subtitleProfiles = streamInfo.GetSubtitleProfiles(_mediaEncoder, false, _serverAddress, _accessToken)
  203. .Where(subtitle => subtitle.DeliveryMethod == SubtitleDeliveryMethod.External)
  204. .ToList();
  205. foreach (var subtitle in subtitleProfiles)
  206. {
  207. var subtitleAdded = AddSubtitleElement(writer, subtitle);
  208. if (subtitleAdded && _profile.EnableSingleSubtitleLimit)
  209. {
  210. break;
  211. }
  212. }
  213. }
  214. private bool AddSubtitleElement(XmlWriter writer, SubtitleStreamInfo info)
  215. {
  216. var subtitleProfile = _profile.SubtitleProfiles
  217. .FirstOrDefault(i => string.Equals(info.Format, i.Format, StringComparison.OrdinalIgnoreCase) && i.Method == SubtitleDeliveryMethod.External);
  218. if (subtitleProfile == null)
  219. {
  220. return false;
  221. }
  222. var subtitleMode = subtitleProfile.DidlMode;
  223. if (string.Equals(subtitleMode, "CaptionInfoEx", StringComparison.OrdinalIgnoreCase))
  224. {
  225. // <sec:CaptionInfoEx sec:type="srt">http://192.168.1.3:9999/video.srt</sec:CaptionInfoEx>
  226. // <sec:CaptionInfo sec:type="srt">http://192.168.1.3:9999/video.srt</sec:CaptionInfo>
  227. writer.WriteStartElement("sec", "CaptionInfoEx", null);
  228. writer.WriteAttributeString("sec", "type", null, info.Format.ToLower());
  229. writer.WriteString(info.Url);
  230. writer.WriteFullEndElement();
  231. }
  232. else if (string.Equals(subtitleMode, "smi", StringComparison.OrdinalIgnoreCase))
  233. {
  234. writer.WriteStartElement(string.Empty, "res", NS_DIDL);
  235. writer.WriteAttributeString("protocolInfo", "http-get:*:smi/caption:*");
  236. writer.WriteString(info.Url);
  237. writer.WriteFullEndElement();
  238. }
  239. else
  240. {
  241. writer.WriteStartElement(string.Empty, "res", NS_DIDL);
  242. var protocolInfo = string.Format("http-get:*:text/{0}:*", info.Format.ToLower());
  243. writer.WriteAttributeString("protocolInfo", protocolInfo);
  244. writer.WriteString(info.Url);
  245. writer.WriteFullEndElement();
  246. }
  247. return true;
  248. }
  249. private void AddVideoResource(XmlWriter writer, BaseItem video, string deviceId, Filter filter, string contentFeatures, StreamInfo streamInfo)
  250. {
  251. writer.WriteStartElement(string.Empty, "res", NS_DIDL);
  252. var url = NormalizeDlnaMediaUrl(streamInfo.ToUrl(_serverAddress, _accessToken));
  253. var mediaSource = streamInfo.MediaSource;
  254. if (mediaSource.RunTimeTicks.HasValue)
  255. {
  256. writer.WriteAttributeString("duration", TimeSpan.FromTicks(mediaSource.RunTimeTicks.Value).ToString("c", _usCulture));
  257. }
  258. if (filter.Contains("res@size"))
  259. {
  260. if (streamInfo.IsDirectStream || streamInfo.EstimateContentLength)
  261. {
  262. var size = streamInfo.TargetSize;
  263. if (size.HasValue)
  264. {
  265. writer.WriteAttributeString("size", size.Value.ToString(_usCulture));
  266. }
  267. }
  268. }
  269. var totalBitrate = streamInfo.TargetTotalBitrate;
  270. var targetSampleRate = streamInfo.TargetAudioSampleRate;
  271. var targetChannels = streamInfo.TargetAudioChannels;
  272. var targetWidth = streamInfo.TargetWidth;
  273. var targetHeight = streamInfo.TargetHeight;
  274. if (targetChannels.HasValue)
  275. {
  276. writer.WriteAttributeString("nrAudioChannels", targetChannels.Value.ToString(_usCulture));
  277. }
  278. if (filter.Contains("res@resolution"))
  279. {
  280. if (targetWidth.HasValue && targetHeight.HasValue)
  281. {
  282. writer.WriteAttributeString("resolution", string.Format("{0}x{1}", targetWidth.Value, targetHeight.Value));
  283. }
  284. }
  285. if (targetSampleRate.HasValue)
  286. {
  287. writer.WriteAttributeString("sampleFrequency", targetSampleRate.Value.ToString(_usCulture));
  288. }
  289. if (totalBitrate.HasValue)
  290. {
  291. writer.WriteAttributeString("bitrate", totalBitrate.Value.ToString(_usCulture));
  292. }
  293. var mediaProfile = _profile.GetVideoMediaProfile(streamInfo.Container,
  294. streamInfo.TargetAudioCodec.FirstOrDefault(),
  295. streamInfo.TargetVideoCodec.FirstOrDefault(),
  296. streamInfo.TargetAudioBitrate,
  297. targetWidth,
  298. targetHeight,
  299. streamInfo.TargetVideoBitDepth,
  300. streamInfo.TargetVideoProfile,
  301. streamInfo.TargetVideoLevel,
  302. streamInfo.TargetFramerate ?? 0,
  303. streamInfo.TargetPacketLength,
  304. streamInfo.TargetTimestamp,
  305. streamInfo.IsTargetAnamorphic,
  306. streamInfo.IsTargetInterlaced,
  307. streamInfo.TargetRefFrames,
  308. streamInfo.TargetVideoStreamCount,
  309. streamInfo.TargetAudioStreamCount,
  310. streamInfo.TargetVideoCodecTag,
  311. streamInfo.IsTargetAVC);
  312. var filename = url.Substring(0, url.IndexOf('?'));
  313. var mimeType = mediaProfile == null || string.IsNullOrEmpty(mediaProfile.MimeType)
  314. ? GetMimeType(filename)
  315. : mediaProfile.MimeType;
  316. writer.WriteAttributeString("protocolInfo", string.Format(
  317. "http-get:*:{0}:{1}",
  318. mimeType,
  319. contentFeatures
  320. ));
  321. writer.WriteString(url);
  322. writer.WriteFullEndElement();
  323. }
  324. private string GetDisplayName(BaseItem item, StubType? itemStubType, BaseItem context)
  325. {
  326. if (itemStubType.HasValue && itemStubType.Value == StubType.Latest)
  327. {
  328. return _localization.GetLocalizedString("Latest");
  329. }
  330. if (itemStubType.HasValue && itemStubType.Value == StubType.Playlists)
  331. {
  332. return _localization.GetLocalizedString("Playlists");
  333. }
  334. if (itemStubType.HasValue && itemStubType.Value == StubType.AlbumArtists)
  335. {
  336. return _localization.GetLocalizedString("HeaderAlbumArtists");
  337. }
  338. if (itemStubType.HasValue && itemStubType.Value == StubType.Albums)
  339. {
  340. return _localization.GetLocalizedString("Albums");
  341. }
  342. if (itemStubType.HasValue && itemStubType.Value == StubType.Artists)
  343. {
  344. return _localization.GetLocalizedString("Artists");
  345. }
  346. if (itemStubType.HasValue && itemStubType.Value == StubType.Songs)
  347. {
  348. return _localization.GetLocalizedString("Songs");
  349. }
  350. if (itemStubType.HasValue && itemStubType.Value == StubType.Genres)
  351. {
  352. return _localization.GetLocalizedString("Genres");
  353. }
  354. if (itemStubType.HasValue && itemStubType.Value == StubType.FavoriteAlbums)
  355. {
  356. return _localization.GetLocalizedString("HeaderFavoriteAlbums");
  357. }
  358. if (itemStubType.HasValue && itemStubType.Value == StubType.FavoriteArtists)
  359. {
  360. return _localization.GetLocalizedString("HeaderFavoriteArtists");
  361. }
  362. if (itemStubType.HasValue && itemStubType.Value == StubType.FavoriteSongs)
  363. {
  364. return _localization.GetLocalizedString("HeaderFavoriteSongs");
  365. }
  366. if (itemStubType.HasValue && itemStubType.Value == StubType.ContinueWatching)
  367. {
  368. return _localization.GetLocalizedString("HeaderContinueWatching");
  369. }
  370. if (itemStubType.HasValue && itemStubType.Value == StubType.Movies)
  371. {
  372. return _localization.GetLocalizedString("Movies");
  373. }
  374. if (itemStubType.HasValue && itemStubType.Value == StubType.Collections)
  375. {
  376. return _localization.GetLocalizedString("Collections");
  377. }
  378. if (itemStubType.HasValue && itemStubType.Value == StubType.Favorites)
  379. {
  380. return _localization.GetLocalizedString("Favorites");
  381. }
  382. if (itemStubType.HasValue && itemStubType.Value == StubType.NextUp)
  383. {
  384. return _localization.GetLocalizedString("HeaderNextUp");
  385. }
  386. if (itemStubType.HasValue && itemStubType.Value == StubType.FavoriteSeries)
  387. {
  388. return _localization.GetLocalizedString("HeaderFavoriteShows");
  389. }
  390. if (itemStubType.HasValue && itemStubType.Value == StubType.FavoriteEpisodes)
  391. {
  392. return _localization.GetLocalizedString("HeaderFavoriteEpisodes");
  393. }
  394. if (itemStubType.HasValue && itemStubType.Value == StubType.Series)
  395. {
  396. return _localization.GetLocalizedString("Shows");
  397. }
  398. var episode = item as Episode;
  399. var season = context as Season;
  400. if (episode != null && season != null)
  401. {
  402. // This is a special embedded within a season
  403. if (item.ParentIndexNumber.HasValue && item.ParentIndexNumber.Value == 0)
  404. {
  405. if (season.IndexNumber.HasValue && season.IndexNumber.Value != 0)
  406. {
  407. return string.Format(_localization.GetLocalizedString("ValueSpecialEpisodeName"), item.Name);
  408. }
  409. }
  410. if (item.IndexNumber.HasValue)
  411. {
  412. var number = item.IndexNumber.Value.ToString("00", CultureInfo.InvariantCulture);
  413. if (episode.IndexNumberEnd.HasValue)
  414. {
  415. number += "-" + episode.IndexNumberEnd.Value.ToString("00", CultureInfo.InvariantCulture);
  416. }
  417. return number + " - " + item.Name;
  418. }
  419. }
  420. return item.Name;
  421. }
  422. private void AddAudioResource(DlnaOptions options, XmlWriter writer, BaseItem audio, string deviceId, Filter filter, StreamInfo streamInfo = null)
  423. {
  424. writer.WriteStartElement(string.Empty, "res", NS_DIDL);
  425. if (streamInfo == null)
  426. {
  427. var sources = _mediaSourceManager.GetStaticMediaSources(audio, true, _user);
  428. streamInfo = new StreamBuilder(_mediaEncoder, _logger).BuildAudioItem(new AudioOptions
  429. {
  430. ItemId = audio.Id,
  431. MediaSources = sources.ToArray(),
  432. Profile = _profile,
  433. DeviceId = deviceId
  434. });
  435. }
  436. var url = NormalizeDlnaMediaUrl(streamInfo.ToUrl(_serverAddress, _accessToken));
  437. var mediaSource = streamInfo.MediaSource;
  438. if (mediaSource.RunTimeTicks.HasValue)
  439. {
  440. writer.WriteAttributeString("duration", TimeSpan.FromTicks(mediaSource.RunTimeTicks.Value).ToString("c", _usCulture));
  441. }
  442. if (filter.Contains("res@size"))
  443. {
  444. if (streamInfo.IsDirectStream || streamInfo.EstimateContentLength)
  445. {
  446. var size = streamInfo.TargetSize;
  447. if (size.HasValue)
  448. {
  449. writer.WriteAttributeString("size", size.Value.ToString(_usCulture));
  450. }
  451. }
  452. }
  453. var targetAudioBitrate = streamInfo.TargetAudioBitrate;
  454. var targetSampleRate = streamInfo.TargetAudioSampleRate;
  455. var targetChannels = streamInfo.TargetAudioChannels;
  456. var targetAudioBitDepth = streamInfo.TargetAudioBitDepth;
  457. if (targetChannels.HasValue)
  458. {
  459. writer.WriteAttributeString("nrAudioChannels", targetChannels.Value.ToString(_usCulture));
  460. }
  461. if (targetSampleRate.HasValue)
  462. {
  463. writer.WriteAttributeString("sampleFrequency", targetSampleRate.Value.ToString(_usCulture));
  464. }
  465. if (targetAudioBitrate.HasValue)
  466. {
  467. writer.WriteAttributeString("bitrate", targetAudioBitrate.Value.ToString(_usCulture));
  468. }
  469. var mediaProfile = _profile.GetAudioMediaProfile(streamInfo.Container,
  470. streamInfo.TargetAudioCodec.FirstOrDefault(),
  471. targetChannels,
  472. targetAudioBitrate,
  473. targetSampleRate,
  474. targetAudioBitDepth);
  475. var filename = url.Substring(0, url.IndexOf('?'));
  476. var mimeType = mediaProfile == null || string.IsNullOrEmpty(mediaProfile.MimeType)
  477. ? GetMimeType(filename)
  478. : mediaProfile.MimeType;
  479. var contentFeatures = new ContentFeatureBuilder(_profile).BuildAudioHeader(streamInfo.Container,
  480. streamInfo.TargetAudioCodec.FirstOrDefault(),
  481. targetAudioBitrate,
  482. targetSampleRate,
  483. targetChannels,
  484. targetAudioBitDepth,
  485. streamInfo.IsDirectStream,
  486. streamInfo.RunTimeTicks ?? 0,
  487. streamInfo.TranscodeSeekInfo);
  488. writer.WriteAttributeString("protocolInfo", string.Format(
  489. "http-get:*:{0}:{1}",
  490. mimeType,
  491. contentFeatures
  492. ));
  493. writer.WriteString(url);
  494. writer.WriteFullEndElement();
  495. }
  496. public static bool IsIdRoot(string id)
  497. {
  498. if (string.IsNullOrWhiteSpace(id) ||
  499. string.Equals(id, "0", StringComparison.OrdinalIgnoreCase)
  500. // Samsung sometimes uses 1 as root
  501. || string.Equals(id, "1", StringComparison.OrdinalIgnoreCase))
  502. {
  503. return true;
  504. }
  505. return false;
  506. }
  507. public void WriteFolderElement(XmlWriter writer, BaseItem folder, StubType? stubType, BaseItem context, int childCount, Filter filter, string requestedId = null)
  508. {
  509. writer.WriteStartElement(string.Empty, "container", NS_DIDL);
  510. writer.WriteAttributeString("restricted", "1");
  511. writer.WriteAttributeString("searchable", "1");
  512. writer.WriteAttributeString("childCount", childCount.ToString(_usCulture));
  513. var clientId = GetClientId(folder, stubType);
  514. if (string.Equals(requestedId, "0"))
  515. {
  516. writer.WriteAttributeString("id", "0");
  517. writer.WriteAttributeString("parentID", "-1");
  518. }
  519. else
  520. {
  521. writer.WriteAttributeString("id", clientId);
  522. if (context != null)
  523. {
  524. writer.WriteAttributeString("parentID", GetClientId(context, null));
  525. }
  526. else
  527. {
  528. var parent = folder.DisplayParentId;
  529. if (parent.Equals(Guid.Empty))
  530. {
  531. writer.WriteAttributeString("parentID", "0");
  532. }
  533. else
  534. {
  535. writer.WriteAttributeString("parentID", GetClientId(parent, null));
  536. }
  537. }
  538. }
  539. AddGeneralProperties(folder, stubType, context, writer, filter);
  540. AddCover(folder, context, stubType, writer);
  541. writer.WriteFullEndElement();
  542. }
  543. private void AddSamsungBookmarkInfo(BaseItem item, User user, XmlWriter writer)
  544. {
  545. if (!item.SupportsPositionTicksResume || item is Folder)
  546. {
  547. return;
  548. }
  549. MediaBrowser.Model.Dlna.XmlAttribute secAttribute = null;
  550. foreach (var attribute in _profile.XmlRootAttributes)
  551. {
  552. if (string.Equals(attribute.Name, "xmlns:sec", StringComparison.OrdinalIgnoreCase))
  553. {
  554. secAttribute = attribute;
  555. break;
  556. }
  557. }
  558. // Not a samsung device
  559. if (secAttribute == null)
  560. {
  561. return;
  562. }
  563. var userdata = _userDataManager.GetUserData(user, item);
  564. if (userdata.PlaybackPositionTicks > 0)
  565. {
  566. var elementValue = string.Format("BM={0}", Convert.ToInt32(TimeSpan.FromTicks(userdata.PlaybackPositionTicks).TotalSeconds).ToString(_usCulture));
  567. AddValue(writer, "sec", "dcmInfo", elementValue, secAttribute.Value);
  568. }
  569. }
  570. /// <summary>
  571. /// Adds fields used by both items and folders
  572. /// </summary>
  573. private void AddCommonFields(BaseItem item, StubType? itemStubType, BaseItem context, XmlWriter writer, Filter filter)
  574. {
  575. // Don't filter on dc:title because not all devices will include it in the filter
  576. // MediaMonkey for example won't display content without a title
  577. //if (filter.Contains("dc:title"))
  578. {
  579. AddValue(writer, "dc", "title", GetDisplayName(item, itemStubType, context), NS_DC);
  580. }
  581. WriteObjectClass(writer, item, itemStubType);
  582. if (filter.Contains("dc:date"))
  583. {
  584. if (item.PremiereDate.HasValue)
  585. {
  586. AddValue(writer, "dc", "date", item.PremiereDate.Value.ToString("o"), NS_DC);
  587. }
  588. }
  589. if (filter.Contains("upnp:genre"))
  590. {
  591. foreach (var genre in item.Genres)
  592. {
  593. AddValue(writer, "upnp", "genre", genre, NS_UPNP);
  594. }
  595. }
  596. foreach (var studio in item.Studios)
  597. {
  598. AddValue(writer, "upnp", "publisher", studio, NS_UPNP);
  599. }
  600. if (!(item is Folder))
  601. {
  602. if (filter.Contains("dc:description"))
  603. {
  604. var desc = item.Overview;
  605. if (!string.IsNullOrWhiteSpace(desc))
  606. {
  607. AddValue(writer, "dc", "description", desc, NS_DC);
  608. }
  609. }
  610. //if (filter.Contains("upnp:longDescription"))
  611. //{
  612. // if (!string.IsNullOrWhiteSpace(item.Overview))
  613. // {
  614. // AddValue(writer, "upnp", "longDescription", item.Overview, NS_UPNP);
  615. // }
  616. //}
  617. }
  618. if (!string.IsNullOrEmpty(item.OfficialRating))
  619. {
  620. if (filter.Contains("dc:rating"))
  621. {
  622. AddValue(writer, "dc", "rating", item.OfficialRating, NS_DC);
  623. }
  624. if (filter.Contains("upnp:rating"))
  625. {
  626. AddValue(writer, "upnp", "rating", item.OfficialRating, NS_UPNP);
  627. }
  628. }
  629. AddPeople(item, writer);
  630. }
  631. private void WriteObjectClass(XmlWriter writer, BaseItem item, StubType? stubType)
  632. {
  633. // More types here
  634. // http://oss.linn.co.uk/repos/Public/LibUpnpCil/DidlLite/UpnpAv/Test/TestDidlLite.cs
  635. writer.WriteStartElement("upnp", "class", NS_UPNP);
  636. if (item.IsDisplayedAsFolder || stubType.HasValue)
  637. {
  638. string classType = null;
  639. if (!_profile.RequiresPlainFolders)
  640. {
  641. if (item is MusicAlbum)
  642. {
  643. classType = "object.container.album.musicAlbum";
  644. }
  645. else if (item is MusicArtist)
  646. {
  647. classType = "object.container.person.musicArtist";
  648. }
  649. else if (item is Series || item is Season || item is BoxSet || item is Video)
  650. {
  651. classType = "object.container.album.videoAlbum";
  652. }
  653. else if (item is Playlist)
  654. {
  655. classType = "object.container.playlistContainer";
  656. }
  657. else if (item is PhotoAlbum)
  658. {
  659. classType = "object.container.album.photoAlbum";
  660. }
  661. }
  662. writer.WriteString(classType ?? "object.container.storageFolder");
  663. }
  664. else if (string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))
  665. {
  666. writer.WriteString("object.item.audioItem.musicTrack");
  667. }
  668. else if (string.Equals(item.MediaType, MediaType.Photo, StringComparison.OrdinalIgnoreCase))
  669. {
  670. writer.WriteString("object.item.imageItem.photo");
  671. }
  672. else if (string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
  673. {
  674. if (!_profile.RequiresPlainVideoItems && item is Movie)
  675. {
  676. writer.WriteString("object.item.videoItem.movie");
  677. }
  678. else if (!_profile.RequiresPlainVideoItems && item is MusicVideo)
  679. {
  680. writer.WriteString("object.item.videoItem.musicVideoClip");
  681. }
  682. else
  683. {
  684. writer.WriteString("object.item.videoItem");
  685. }
  686. }
  687. else if (item is MusicGenre)
  688. {
  689. writer.WriteString(_profile.RequiresPlainFolders ? "object.container.storageFolder" : "object.container.genre.musicGenre");
  690. }
  691. else if (item is Genre || item is GameGenre)
  692. {
  693. writer.WriteString(_profile.RequiresPlainFolders ? "object.container.storageFolder" : "object.container.genre");
  694. }
  695. else
  696. {
  697. writer.WriteString("object.item");
  698. }
  699. writer.WriteFullEndElement();
  700. }
  701. private void AddPeople(BaseItem item, XmlWriter writer)
  702. {
  703. //var types = new[]
  704. //{
  705. // PersonType.Director,
  706. // PersonType.Writer,
  707. // PersonType.Producer,
  708. // PersonType.Composer,
  709. // "Creator"
  710. //};
  711. //var people = _libraryManager.GetPeople(item);
  712. //var index = 0;
  713. //// Seeing some LG models locking up due content with large lists of people
  714. //// The actual issue might just be due to processing a more metadata than it can handle
  715. //var limit = 6;
  716. //foreach (var actor in people)
  717. //{
  718. // var type = types.FirstOrDefault(i => string.Equals(i, actor.Type, StringComparison.OrdinalIgnoreCase) || string.Equals(i, actor.Role, StringComparison.OrdinalIgnoreCase))
  719. // ?? PersonType.Actor;
  720. // AddValue(writer, "upnp", type.ToLower(), actor.Name, NS_UPNP);
  721. // index++;
  722. // if (index >= limit)
  723. // {
  724. // break;
  725. // }
  726. //}
  727. }
  728. private void AddGeneralProperties(BaseItem item, StubType? itemStubType, BaseItem context, XmlWriter writer, Filter filter)
  729. {
  730. AddCommonFields(item, itemStubType, context, writer, filter);
  731. var hasArtists = item as IHasArtist;
  732. var hasAlbumArtists = item as IHasAlbumArtist;
  733. if (hasArtists != null)
  734. {
  735. foreach (var artist in hasArtists.Artists)
  736. {
  737. AddValue(writer, "upnp", "artist", artist, NS_UPNP);
  738. AddValue(writer, "dc", "creator", artist, NS_DC);
  739. // If it doesn't support album artists (musicvideo), then tag as both
  740. if (hasAlbumArtists == null)
  741. {
  742. AddAlbumArtist(writer, artist);
  743. }
  744. }
  745. }
  746. if (hasAlbumArtists != null)
  747. {
  748. foreach (var albumArtist in hasAlbumArtists.AlbumArtists)
  749. {
  750. AddAlbumArtist(writer, albumArtist);
  751. }
  752. }
  753. if (!string.IsNullOrWhiteSpace(item.Album))
  754. {
  755. AddValue(writer, "upnp", "album", item.Album, NS_UPNP);
  756. }
  757. if (item.IndexNumber.HasValue)
  758. {
  759. AddValue(writer, "upnp", "originalTrackNumber", item.IndexNumber.Value.ToString(_usCulture), NS_UPNP);
  760. if (item is Episode)
  761. {
  762. AddValue(writer, "upnp", "episodeNumber", item.IndexNumber.Value.ToString(_usCulture), NS_UPNP);
  763. }
  764. }
  765. }
  766. private void AddAlbumArtist(XmlWriter writer, string name)
  767. {
  768. try
  769. {
  770. writer.WriteStartElement("upnp", "artist", NS_UPNP);
  771. writer.WriteAttributeString("role", "AlbumArtist");
  772. writer.WriteString(name);
  773. writer.WriteFullEndElement();
  774. }
  775. catch (XmlException ex)
  776. {
  777. _logger.LogError(ex, "Error adding xml value: {value}", name);
  778. }
  779. }
  780. private void AddValue(XmlWriter writer, string prefix, string name, string value, string namespaceUri)
  781. {
  782. try
  783. {
  784. writer.WriteElementString(prefix, name, namespaceUri, value);
  785. }
  786. catch (XmlException ex)
  787. {
  788. _logger.LogError(ex, "Error adding xml value: {value}", value);
  789. }
  790. }
  791. private void AddCover(BaseItem item, BaseItem context, StubType? stubType, XmlWriter writer)
  792. {
  793. ImageDownloadInfo imageInfo = GetImageInfo(item);;
  794. if (imageInfo == null)
  795. {
  796. return;
  797. }
  798. var albumartUrlInfo = GetImageUrl(imageInfo, _profile.MaxAlbumArtWidth, _profile.MaxAlbumArtHeight, "jpg");
  799. writer.WriteStartElement("upnp", "albumArtURI", NS_UPNP);
  800. writer.WriteAttributeString("dlna", "profileID", NS_DLNA, _profile.AlbumArtPn);
  801. writer.WriteString(albumartUrlInfo.Url);
  802. writer.WriteFullEndElement();
  803. // TOOD: Remove these default values
  804. var iconUrlInfo = GetImageUrl(imageInfo, _profile.MaxIconWidth ?? 48, _profile.MaxIconHeight ?? 48, "jpg");
  805. writer.WriteElementString("upnp", "icon", NS_UPNP, iconUrlInfo.Url);
  806. if (!_profile.EnableAlbumArtInDidl)
  807. {
  808. if (string.Equals(item.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase)
  809. || string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
  810. {
  811. if (!stubType.HasValue)
  812. {
  813. return;
  814. }
  815. }
  816. }
  817. AddImageResElement(item, writer, 160, 160, "jpg", "JPEG_TN");
  818. if (!_profile.EnableSingleAlbumArtLimit || string.Equals(item.MediaType, MediaType.Photo, StringComparison.OrdinalIgnoreCase))
  819. {
  820. AddImageResElement(item, writer, 4096, 4096, "jpg", "JPEG_LRG");
  821. AddImageResElement(item, writer, 1024, 768, "jpg", "JPEG_MED");
  822. AddImageResElement(item, writer, 640, 480, "jpg", "JPEG_SM");
  823. AddImageResElement(item, writer, 4096, 4096, "png", "PNG_LRG");
  824. AddImageResElement(item, writer, 160, 160, "png", "PNG_TN");
  825. }
  826. }
  827. private void AddEmbeddedImageAsCover(string name, XmlWriter writer)
  828. {
  829. writer.WriteStartElement("upnp", "albumArtURI", NS_UPNP);
  830. writer.WriteAttributeString("dlna", "profileID", NS_DLNA, _profile.AlbumArtPn);
  831. writer.WriteString(_serverAddress + "/Dlna/icons/people480.jpg");
  832. writer.WriteFullEndElement();
  833. writer.WriteElementString("upnp", "icon", NS_UPNP, _serverAddress + "/Dlna/icons/people48.jpg");
  834. }
  835. private void AddImageResElement(BaseItem item,
  836. XmlWriter writer,
  837. int maxWidth,
  838. int maxHeight,
  839. string format,
  840. string org_Pn)
  841. {
  842. var imageInfo = GetImageInfo(item);
  843. if (imageInfo == null)
  844. {
  845. return;
  846. }
  847. var albumartUrlInfo = GetImageUrl(imageInfo, maxWidth, maxHeight, format);
  848. writer.WriteStartElement(string.Empty, "res", NS_DIDL);
  849. // Images must have a reported size or many clients (Bubble upnp), will only use the first thumbnail
  850. // rather than using a larger one when available
  851. var width = albumartUrlInfo.Width ?? maxWidth;
  852. var height = albumartUrlInfo.Height ?? maxHeight;
  853. var contentFeatures = new ContentFeatureBuilder(_profile)
  854. .BuildImageHeader(format, width, height, imageInfo.IsDirectStream, org_Pn);
  855. writer.WriteAttributeString("protocolInfo", string.Format(
  856. "http-get:*:{0}:{1}",
  857. GetMimeType("file." + format),
  858. contentFeatures
  859. ));
  860. writer.WriteAttributeString("resolution", string.Format("{0}x{1}", width, height));
  861. writer.WriteString(albumartUrlInfo.Url);
  862. writer.WriteFullEndElement();
  863. }
  864. private ImageDownloadInfo GetImageInfo(BaseItem item)
  865. {
  866. if (item.HasImage(ImageType.Primary))
  867. {
  868. return GetImageInfo(item, ImageType.Primary);
  869. }
  870. if (item.HasImage(ImageType.Thumb))
  871. {
  872. return GetImageInfo(item, ImageType.Thumb);
  873. }
  874. if (item.HasImage(ImageType.Backdrop))
  875. {
  876. if (item is Channel)
  877. {
  878. return GetImageInfo(item, ImageType.Backdrop);
  879. }
  880. }
  881. item = item.GetParents().FirstOrDefault(i => i.HasImage(ImageType.Primary));
  882. if (item != null)
  883. {
  884. if (item.HasImage(ImageType.Primary))
  885. {
  886. return GetImageInfo(item, ImageType.Primary);
  887. }
  888. }
  889. return null;
  890. }
  891. private ImageDownloadInfo GetImageInfo(BaseItem item, ImageType type)
  892. {
  893. var imageInfo = item.GetImageInfo(type, 0);
  894. string tag = null;
  895. try
  896. {
  897. tag = _imageProcessor.GetImageCacheTag(item, type);
  898. }
  899. catch (Exception ex)
  900. {
  901. _logger.LogError(ex, "Error getting image cache tag");
  902. }
  903. int? width = imageInfo.Width;
  904. int? height = imageInfo.Height;
  905. if (width == 0 || height == 0)
  906. {
  907. //_imageProcessor.GetImageSize(item, imageInfo);
  908. width = null;
  909. height = null;
  910. }
  911. else if (width == -1 || height == -1)
  912. {
  913. width = null;
  914. height = null;
  915. }
  916. //try
  917. //{
  918. // var size = _imageProcessor.GetImageSize(imageInfo);
  919. // width = size.Width;
  920. // height = size.Height;
  921. //}
  922. //catch
  923. //{
  924. //}
  925. var inputFormat = (Path.GetExtension(imageInfo.Path) ?? string.Empty)
  926. .TrimStart('.')
  927. .Replace("jpeg", "jpg", StringComparison.OrdinalIgnoreCase);
  928. return new ImageDownloadInfo
  929. {
  930. ItemId = item.Id,
  931. Type = type,
  932. ImageTag = tag,
  933. Width = width,
  934. Height = height,
  935. Format = inputFormat,
  936. ItemImageInfo = imageInfo
  937. };
  938. }
  939. class ImageDownloadInfo
  940. {
  941. internal Guid ItemId;
  942. internal string ImageTag;
  943. internal ImageType Type;
  944. internal int? Width;
  945. internal int? Height;
  946. internal bool IsDirectStream;
  947. internal string Format;
  948. internal ItemImageInfo ItemImageInfo;
  949. }
  950. class ImageUrlInfo
  951. {
  952. internal string Url;
  953. internal int? Width;
  954. internal int? Height;
  955. }
  956. public static string GetClientId(BaseItem item, StubType? stubType)
  957. {
  958. return GetClientId(item.Id, stubType);
  959. }
  960. public static string GetClientId(Guid idValue, StubType? stubType)
  961. {
  962. var id = idValue.ToString("N");
  963. if (stubType.HasValue)
  964. {
  965. id = stubType.Value.ToString().ToLower() + "_" + id;
  966. }
  967. return id;
  968. }
  969. private ImageUrlInfo GetImageUrl(ImageDownloadInfo info, int maxWidth, int maxHeight, string format)
  970. {
  971. var url = string.Format("{0}/Items/{1}/Images/{2}/0/{3}/{4}/{5}/{6}/0/0",
  972. _serverAddress,
  973. info.ItemId.ToString("N"),
  974. info.Type,
  975. info.ImageTag,
  976. format,
  977. maxWidth.ToString(CultureInfo.InvariantCulture),
  978. maxHeight.ToString(CultureInfo.InvariantCulture));
  979. var width = info.Width;
  980. var height = info.Height;
  981. info.IsDirectStream = false;
  982. if (width.HasValue && height.HasValue)
  983. {
  984. var newSize = DrawingUtils.Resize(
  985. new ImageDimensions(width.Value, height.Value), 0, 0, maxWidth, maxHeight);
  986. width = newSize.Width;
  987. height = newSize.Height;
  988. var normalizedFormat = format
  989. .Replace("jpeg", "jpg", StringComparison.OrdinalIgnoreCase);
  990. if (string.Equals(info.Format, normalizedFormat, StringComparison.OrdinalIgnoreCase))
  991. {
  992. info.IsDirectStream = maxWidth >= width.Value && maxHeight >= height.Value;
  993. }
  994. }
  995. // just lie
  996. info.IsDirectStream = true;
  997. return new ImageUrlInfo
  998. {
  999. Url = url,
  1000. Width = width,
  1001. Height = height
  1002. };
  1003. }
  1004. }
  1005. }