DidlBuilder.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Entities.Audio;
  3. using MediaBrowser.Model.Entities;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. namespace MediaBrowser.Dlna.PlayTo
  8. {
  9. internal class DidlBuilder
  10. {
  11. const string CRLF = "\r\n";
  12. const string UNKNOWN = "Unknown";
  13. const string DIDL_START = @"<item id=""{0}"" parentID=""{1}"" restricted=""1"" xmlns=""urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"">" + CRLF;
  14. const string DIDL_TITLE = @" <dc:title xmlns:dc=""http://purl.org/dc/elements/1.1/"">{0}</dc:title>" + CRLF;
  15. const string DIDL_ARTIST = @"<upnp:artist xmlns:upnp=""urn:schemas-upnp-org:metadata-1-0/upnp/"">{0}</upnp:artist>" + CRLF;
  16. const string DIDL_ALBUM = @"<upnp:album xmlns:upnp=""urn:schemas-upnp-org:metadata-1-0/upnp/"">{0}</upnp:album>" + CRLF;
  17. const string DIDL_TRACKNUM = @"<upnp:originalTrackNumber xmlns:upnp=""urn:schemas-upnp-org:metadata-1-0/upnp/"">{0}</upnp:originalTrackNumber>" + CRLF;
  18. const string DIDL_VIDEOCLASS = @" <upnp:class xmlns:upnp=""urn:schemas-upnp-org:metadata-1-0/upnp/"">object.item.videoItem</upnp:class>" + CRLF;
  19. const string DIDL_AUDIOCLASS = @" <upnp:class xmlns:upnp=""urn:schemas-upnp-org:metadata-1-0/upnp/"">object.item.audioItem.musicTrack</upnp:class>" + CRLF;
  20. const string DIDL_IMAGE = @" <upnp:albumArtURI dlna:profileID=""JPEG_TN"" xmlns:dlna=""urn:schemas-dlna-org:metadata-1-0/"" xmlns:upnp=""urn:schemas-upnp-org:metadata-1-0/upnp/"">{0}</upnp:albumArtURI>" + CRLF +
  21. @" <upnp:icon xmlns:upnp=""urn:schemas-upnp-org:metadata-1-0/upnp/"">{0}</upnp:icon>" + CRLF;
  22. const string DIDL_RELEASEDATE = @" <dc:date xmlns:dc=""http://purl.org/dc/elements/1.1/"">{0}</dc:date>" + CRLF;
  23. const string DIDL_GENRE = @" <upnp:genre xmlns:upnp=""urn:schemas-upnp-org:metadata-1-0/upnp/"">{0}</upnp:genre>" + CRLF;
  24. const string DESCRIPTION = @" <dc:description xmlns:dc=""http://purl.org/dc/elements/1.1/"">{0}</dc:description>" + CRLF;
  25. const string DIDL_VIDEO_RES = @" <res bitrate=""{0}"" duration=""{1}"" protocolInfo=""http-get:*:video/x-msvideo:DLNA.ORG_PN=AVI;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000"" resolution=""{2}x{3}"">{4}</res>" + CRLF;
  26. const string DIDL_AUDIO_RES = @" <res bitrate=""{0}"" duration=""{1}"" nrAudioChannels=""2"" protocolInfo=""http-get:*:audio/mp3:DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000"" sampleFrequency=""{2}"">{3}</res>" + CRLF;
  27. const string DIDL_IMAGE_RES = @" <res protocolInfo=""http-get:*:image/jpeg:DLNA.ORG_PN=JPEG_TN;DLNA.ORG_OP=00;DLNA.ORG_CI=1;DLNA.ORG_FLAGS=00D00000000000000000000000000000"" resolution=""212x320"">{0}</res>" + CRLF;
  28. const string DIDL_ALBUMIMAGE_RES = @" <res protocolInfo=""http-get:*:image/jpeg:DLNA.ORG_PN=JPEG_TN;DLNA.ORG_OP=00;DLNA.ORG_CI=1;DLNA.ORG_FLAGS=00D00000000000000000000000000000"" resolution=""320x320"">{0}</res>" + CRLF;
  29. const string DIDL_RATING = @" <upnp:rating xmlns:upnp=""urn:schemas-upnp-org:metadata-1-0/upnp/"">{0}</upnp:rating>" + CRLF;
  30. const string DIDL_END = "</item>";
  31. /// <summary>
  32. /// Builds a Didl MetaData object for the specified dto.
  33. /// </summary>
  34. /// <param name="dto">The dto.</param>
  35. /// <param name="userId">The user identifier.</param>
  36. /// <param name="serverAddress">The server address.</param>
  37. /// <param name="streamUrl">The stream URL.</param>
  38. /// <param name="streams">The streams.</param>
  39. /// <returns>System.String.</returns>
  40. public static string Build(BaseItem dto, string userId, string serverAddress, string streamUrl, IEnumerable<MediaStream> streams, bool includeImageRes)
  41. {
  42. string response = string.Format(DIDL_START, dto.Id, userId);
  43. response += string.Format(DIDL_TITLE, dto.Name.Replace("&", "and"));
  44. if (IsVideo(dto))
  45. response += DIDL_VIDEOCLASS;
  46. else
  47. response += DIDL_AUDIOCLASS;
  48. var imageUrl = GetImageUrl(dto, serverAddress);
  49. if (!string.IsNullOrWhiteSpace(imageUrl))
  50. {
  51. response += string.Format(DIDL_IMAGE, imageUrl);
  52. }
  53. response += string.Format(DIDL_RELEASEDATE, GetDateString(dto.PremiereDate));
  54. //TODO Add genres to didl;
  55. response += string.Format(DIDL_GENRE, UNKNOWN);
  56. if (IsVideo(dto))
  57. {
  58. response += string.Format(DESCRIPTION, UNKNOWN);
  59. response += GetVideoDIDL(dto, streamUrl, streams);
  60. if (includeImageRes && !string.IsNullOrWhiteSpace(imageUrl))
  61. {
  62. response += string.Format(DIDL_IMAGE_RES, imageUrl);
  63. }
  64. }
  65. else
  66. {
  67. var audio = dto as Audio;
  68. if (audio != null)
  69. {
  70. response += string.Format(DIDL_ARTIST, audio.Artists.FirstOrDefault() ?? UNKNOWN);
  71. response += string.Format(DIDL_ALBUM, audio.Album);
  72. response += string.Format(DIDL_TRACKNUM, audio.IndexNumber ?? 0);
  73. }
  74. response += GetAudioDIDL(dto, streamUrl, streams);
  75. if (includeImageRes && !string.IsNullOrWhiteSpace(imageUrl))
  76. {
  77. response += string.Format(DIDL_ALBUMIMAGE_RES, imageUrl);
  78. }
  79. }
  80. response += DIDL_END;
  81. return response;
  82. }
  83. private static string GetVideoDIDL(BaseItem dto, string streamUrl, IEnumerable<MediaStream> streams)
  84. {
  85. var videostream = streams.Where(stream => stream.Type == MediaStreamType.Video).OrderBy(s => s.IsDefault ? 0 : 1).FirstOrDefault();
  86. if (videostream == null)
  87. {
  88. // TOOD: ???
  89. return string.Empty;
  90. }
  91. return string.Format(DIDL_VIDEO_RES, videostream.BitRate.HasValue ? videostream.BitRate.Value / 10 : 0, GetDurationString(dto), videostream.Width ?? 0, videostream.Height ?? 0, streamUrl);
  92. }
  93. private static string GetAudioDIDL(BaseItem dto, string streamUrl, IEnumerable<MediaStream> streams)
  94. {
  95. var audiostream = streams.Where(stream => stream.Type == MediaStreamType.Audio).OrderBy(s => s.IsDefault ? 0 : 1).FirstOrDefault();
  96. if (audiostream == null)
  97. {
  98. // TOOD: ???
  99. return string.Empty;
  100. }
  101. return string.Format(DIDL_AUDIO_RES, audiostream.BitRate.HasValue ? audiostream.BitRate.Value / 10 : 16000, GetDurationString(dto), audiostream.SampleRate ?? 0, streamUrl);
  102. }
  103. private static string GetImageUrl(BaseItem dto, string serverAddress)
  104. {
  105. const ImageType imageType = ImageType.Primary;
  106. if (!dto.HasImage(imageType))
  107. {
  108. dto = dto.Parents.FirstOrDefault(i => i.HasImage(imageType));
  109. }
  110. return dto == null ? null : string.Format("{0}/Items/{1}/Images/{2}", serverAddress, dto.Id, imageType);
  111. }
  112. private static string GetDurationString(BaseItem dto)
  113. {
  114. var duration = TimeSpan.FromTicks(dto.RunTimeTicks.HasValue ? dto.RunTimeTicks.Value : 0);
  115. // TODO: Bad format string?
  116. return string.Format("{0}:{1:00}:2{00}.000", duration.Hours, duration.Minutes, duration.Seconds);
  117. }
  118. private static string GetDateString(DateTime? date)
  119. {
  120. if (!date.HasValue)
  121. return UNKNOWN;
  122. return string.Format("{0}-{1:00}-{2:00}", date.Value.Year, date.Value.Month, date.Value.Day);
  123. }
  124. private static bool IsVideo(BaseItem item)
  125. {
  126. return string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase);
  127. }
  128. }
  129. }