DidlBuilder.cs 44 KB

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