DescriptionXmlBuilder.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using MediaBrowser.Dlna.Common;
  2. using MediaBrowser.Model.Dlna;
  3. using MediaBrowser.Model.Extensions;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Security;
  9. using System.Text;
  10. namespace MediaBrowser.Dlna.Server
  11. {
  12. public class DescriptionXmlBuilder
  13. {
  14. private readonly DeviceProfile _profile;
  15. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  16. private readonly string _serverUdn;
  17. private readonly string _serverAddress;
  18. private readonly string _serverName;
  19. private readonly string _serverId;
  20. public DescriptionXmlBuilder(DeviceProfile profile, string serverUdn, string serverAddress, string serverName, string serverId)
  21. {
  22. if (string.IsNullOrWhiteSpace(serverUdn))
  23. {
  24. throw new ArgumentNullException("serverUdn");
  25. }
  26. if (string.IsNullOrWhiteSpace(serverAddress))
  27. {
  28. throw new ArgumentNullException("serverAddress");
  29. }
  30. _profile = profile;
  31. _serverUdn = serverUdn;
  32. _serverAddress = serverAddress;
  33. _serverName = serverName;
  34. _serverId = serverId;
  35. }
  36. private bool EnableAbsoluteUrls
  37. {
  38. get { return false; }
  39. }
  40. public string GetXml()
  41. {
  42. var builder = new StringBuilder();
  43. builder.Append("<?xml version=\"1.0\"?>");
  44. builder.Append("<root");
  45. var attributes = _profile.XmlRootAttributes.ToList();
  46. attributes.Insert(0, new XmlAttribute
  47. {
  48. Name = "xmlns:dlna",
  49. Value = "urn:schemas-dlna-org:device-1-0"
  50. });
  51. attributes.Insert(0, new XmlAttribute
  52. {
  53. Name = "xmlns",
  54. Value = "urn:schemas-upnp-org:device-1-0"
  55. });
  56. foreach (var att in attributes)
  57. {
  58. builder.AppendFormat(" {0}=\"{1}\"", att.Name, att.Value);
  59. }
  60. builder.Append(">");
  61. builder.Append("<specVersion>");
  62. builder.Append("<major>1</major>");
  63. builder.Append("<minor>0</minor>");
  64. builder.Append("</specVersion>");
  65. AppendDeviceInfo(builder);
  66. builder.Append("</root>");
  67. return builder.ToString();
  68. }
  69. private void AppendDeviceInfo(StringBuilder builder)
  70. {
  71. builder.Append("<device>");
  72. AppendDeviceProperties(builder);
  73. AppendIconList(builder);
  74. AppendServiceList(builder);
  75. builder.Append("</device>");
  76. }
  77. private void AppendDeviceProperties(StringBuilder builder)
  78. {
  79. builder.Append("<deviceType>urn:schemas-upnp-org:device:MediaServer:1</deviceType>");
  80. builder.Append("<dlna:X_DLNACAP>" + SecurityElement.Escape(_profile.XDlnaCap ?? string.Empty) + "</dlna:X_DLNACAP>");
  81. builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">M-DMS-1.50</dlna:X_DLNADOC>");
  82. builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">" + SecurityElement.Escape(_profile.XDlnaDoc ?? string.Empty) + "</dlna:X_DLNADOC>");
  83. builder.Append("<friendlyName>" + SecurityElement.Escape(GetFriendlyName()) + "</friendlyName>");
  84. builder.Append("<manufacturer>" + SecurityElement.Escape(_profile.Manufacturer ?? string.Empty) + "</manufacturer>");
  85. builder.Append("<manufacturerURL>" + SecurityElement.Escape(_profile.ManufacturerUrl ?? string.Empty) + "</manufacturerURL>");
  86. builder.Append("<modelDescription>" + SecurityElement.Escape(_profile.ModelDescription ?? string.Empty) + "</modelDescription>");
  87. builder.Append("<modelName>" + SecurityElement.Escape(_profile.ModelName ?? string.Empty) + "</modelName>");
  88. builder.Append("<modelNumber>" + SecurityElement.Escape(_profile.ModelNumber ?? string.Empty) + "</modelNumber>");
  89. builder.Append("<modelURL>" + SecurityElement.Escape(_profile.ModelUrl ?? string.Empty) + "</modelURL>");
  90. if (string.IsNullOrWhiteSpace(_profile.SerialNumber))
  91. {
  92. builder.Append("<serialNumber>" + SecurityElement.Escape(_serverId) + "</serialNumber>");
  93. }
  94. else
  95. {
  96. builder.Append("<serialNumber>" + SecurityElement.Escape(_profile.SerialNumber) + "</serialNumber>");
  97. }
  98. builder.Append("<UDN>uuid:" + SecurityElement.Escape(_serverUdn) + "</UDN>");
  99. builder.Append("<presentationURL>" + SecurityElement.Escape(_serverAddress) + "</presentationURL>");
  100. if (!EnableAbsoluteUrls)
  101. {
  102. //builder.Append("<URLBase>" + SecurityElement.Escape(_serverAddress) + "</URLBase>");
  103. }
  104. if (!string.IsNullOrWhiteSpace(_profile.SonyAggregationFlags))
  105. {
  106. builder.Append("<av:aggregationFlags xmlns:av=\"urn:schemas-sony-com:av\">" + SecurityElement.Escape(_profile.SonyAggregationFlags) + "</av:aggregationFlags>");
  107. }
  108. }
  109. private string GetFriendlyName()
  110. {
  111. if (string.IsNullOrWhiteSpace(_profile.FriendlyName))
  112. {
  113. return "Emby - " + _serverName;
  114. }
  115. var characters = _serverName.Where(c => (char.IsLetterOrDigit(c) || c == '-')).ToArray();
  116. var serverName = new string(characters);
  117. var name = (_profile.FriendlyName ?? string.Empty).Replace("${HostName}", serverName, StringComparison.OrdinalIgnoreCase);
  118. return name;
  119. }
  120. private void AppendIconList(StringBuilder builder)
  121. {
  122. builder.Append("<iconList>");
  123. foreach (var icon in GetIcons())
  124. {
  125. builder.Append("<icon>");
  126. builder.Append("<mimetype>" + SecurityElement.Escape(icon.MimeType ?? string.Empty) + "</mimetype>");
  127. builder.Append("<width>" + SecurityElement.Escape(icon.Width.ToString(_usCulture)) + "</width>");
  128. builder.Append("<height>" + SecurityElement.Escape(icon.Height.ToString(_usCulture)) + "</height>");
  129. builder.Append("<depth>" + SecurityElement.Escape(icon.Depth ?? string.Empty) + "</depth>");
  130. builder.Append("<url>" + BuildUrl(icon.Url) + "</url>");
  131. builder.Append("</icon>");
  132. }
  133. builder.Append("</iconList>");
  134. }
  135. private void AppendServiceList(StringBuilder builder)
  136. {
  137. builder.Append("<serviceList>");
  138. foreach (var service in GetServices())
  139. {
  140. builder.Append("<service>");
  141. builder.Append("<serviceType>" + SecurityElement.Escape(service.ServiceType ?? string.Empty) + "</serviceType>");
  142. builder.Append("<serviceId>" + SecurityElement.Escape(service.ServiceId ?? string.Empty) + "</serviceId>");
  143. builder.Append("<SCPDURL>" + BuildUrl(service.ScpdUrl) + "</SCPDURL>");
  144. builder.Append("<controlURL>" + BuildUrl(service.ControlUrl) + "</controlURL>");
  145. builder.Append("<eventSubURL>" + BuildUrl(service.EventSubUrl) + "</eventSubURL>");
  146. builder.Append("</service>");
  147. }
  148. builder.Append("</serviceList>");
  149. }
  150. private string BuildUrl(string url)
  151. {
  152. if (string.IsNullOrWhiteSpace(url))
  153. {
  154. return string.Empty;
  155. }
  156. url = url.TrimStart('/');
  157. url = "/dlna/" + _serverUdn + "/" + url;
  158. if (EnableAbsoluteUrls)
  159. {
  160. url = _serverAddress.TrimEnd('/') + url;
  161. }
  162. return SecurityElement.Escape(url);
  163. }
  164. private IEnumerable<DeviceIcon> GetIcons()
  165. {
  166. var list = new List<DeviceIcon>();
  167. list.Add(new DeviceIcon
  168. {
  169. MimeType = "image/png",
  170. Depth = "24",
  171. Width = 240,
  172. Height = 240,
  173. Url = "icons/logo240.png"
  174. });
  175. list.Add(new DeviceIcon
  176. {
  177. MimeType = "image/jpeg",
  178. Depth = "24",
  179. Width = 240,
  180. Height = 240,
  181. Url = "icons/logo240.jpg"
  182. });
  183. list.Add(new DeviceIcon
  184. {
  185. MimeType = "image/png",
  186. Depth = "24",
  187. Width = 120,
  188. Height = 120,
  189. Url = "icons/logo120.png"
  190. });
  191. list.Add(new DeviceIcon
  192. {
  193. MimeType = "image/jpeg",
  194. Depth = "24",
  195. Width = 120,
  196. Height = 120,
  197. Url = "icons/logo120.jpg"
  198. });
  199. list.Add(new DeviceIcon
  200. {
  201. MimeType = "image/png",
  202. Depth = "24",
  203. Width = 48,
  204. Height = 48,
  205. Url = "icons/logo48.png"
  206. });
  207. list.Add(new DeviceIcon
  208. {
  209. MimeType = "image/jpeg",
  210. Depth = "24",
  211. Width = 48,
  212. Height = 48,
  213. Url = "icons/logo48.jpg"
  214. });
  215. return list;
  216. }
  217. private IEnumerable<DeviceService> GetServices()
  218. {
  219. var list = new List<DeviceService>();
  220. list.Add(new DeviceService
  221. {
  222. ServiceType = "urn:schemas-upnp-org:service:ContentDirectory:1",
  223. ServiceId = "urn:upnp-org:serviceId:ContentDirectory",
  224. ScpdUrl = "contentdirectory/contentdirectory.xml",
  225. ControlUrl = "contentdirectory/control",
  226. EventSubUrl = "contentdirectory/events"
  227. });
  228. list.Add(new DeviceService
  229. {
  230. ServiceType = "urn:schemas-upnp-org:service:ConnectionManager:1",
  231. ServiceId = "urn:upnp-org:serviceId:ConnectionManager",
  232. ScpdUrl = "connectionmanager/connectionmanager.xml",
  233. ControlUrl = "connectionmanager/control",
  234. EventSubUrl = "connectionmanager/events"
  235. });
  236. if (_profile.EnableMSMediaReceiverRegistrar)
  237. {
  238. list.Add(new DeviceService
  239. {
  240. ServiceType = "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1",
  241. ServiceId = "urn:microsoft.com:serviceId:X_MS_MediaReceiverRegistrar",
  242. ScpdUrl = "mediareceiverregistrar/mediareceiverregistrar.xml",
  243. ControlUrl = "mediareceiverregistrar/control",
  244. EventSubUrl = "mediareceiverregistrar/events"
  245. });
  246. }
  247. return list;
  248. }
  249. public override string ToString()
  250. {
  251. return GetXml();
  252. }
  253. }
  254. }