DescriptionXmlBuilder.cs 12 KB

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