DescriptionXmlBuilder.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. private string GetFriendlyName()
  120. {
  121. if (string.IsNullOrEmpty(_profile.FriendlyName))
  122. {
  123. return "Jellyfin - " + _serverName;
  124. }
  125. var characterList = new List<char>();
  126. foreach (var c in _serverName)
  127. {
  128. if (char.IsLetterOrDigit(c) || c == '-')
  129. {
  130. characterList.Add(c);
  131. }
  132. }
  133. var characters = characterList.ToArray();
  134. var serverName = new string(characters);
  135. var name = _profile.FriendlyName?.Replace("${HostName}", serverName, StringComparison.OrdinalIgnoreCase);
  136. return name ?? string.Empty;
  137. }
  138. private void AppendIconList(StringBuilder builder)
  139. {
  140. builder.Append("<iconList>");
  141. foreach (var icon in GetIcons())
  142. {
  143. builder.Append("<icon>");
  144. builder.Append("<mimetype>")
  145. .Append(SecurityElement.Escape(icon.MimeType))
  146. .Append("</mimetype>");
  147. builder.Append("<width>")
  148. .Append(SecurityElement.Escape(icon.Width.ToString(CultureInfo.InvariantCulture)))
  149. .Append("</width>");
  150. builder.Append("<height>")
  151. .Append(SecurityElement.Escape(icon.Height.ToString(CultureInfo.InvariantCulture)))
  152. .Append("</height>");
  153. builder.Append("<depth>")
  154. .Append(SecurityElement.Escape(icon.Depth))
  155. .Append("</depth>");
  156. builder.Append("<url>")
  157. .Append(BuildUrl(icon.Url))
  158. .Append("</url>");
  159. builder.Append("</icon>");
  160. }
  161. builder.Append("</iconList>");
  162. }
  163. private void AppendServiceList(StringBuilder builder)
  164. {
  165. builder.Append("<serviceList>");
  166. foreach (var service in GetServices())
  167. {
  168. builder.Append("<service>");
  169. builder.Append("<serviceType>")
  170. .Append(SecurityElement.Escape(service.ServiceType))
  171. .Append("</serviceType>");
  172. builder.Append("<serviceId>")
  173. .Append(SecurityElement.Escape(service.ServiceId))
  174. .Append("</serviceId>");
  175. builder.Append("<SCPDURL>")
  176. .Append(BuildUrl(service.ScpdUrl))
  177. .Append("</SCPDURL>");
  178. builder.Append("<controlURL>")
  179. .Append(BuildUrl(service.ControlUrl))
  180. .Append("</controlURL>");
  181. builder.Append("<eventSubURL>")
  182. .Append(BuildUrl(service.EventSubUrl))
  183. .Append("</eventSubURL>");
  184. builder.Append("</service>");
  185. }
  186. builder.Append("</serviceList>");
  187. }
  188. private string BuildUrl(string url)
  189. {
  190. if (string.IsNullOrEmpty(url))
  191. {
  192. return string.Empty;
  193. }
  194. url = _serverAddress.TrimEnd('/') + "/dlna/" + _serverUdn + "/" + url.TrimStart('/');
  195. return SecurityElement.Escape(url);
  196. }
  197. private IEnumerable<DeviceIcon> GetIcons()
  198. => new[]
  199. {
  200. new DeviceIcon
  201. {
  202. MimeType = "image/png",
  203. Depth = "24",
  204. Width = 240,
  205. Height = 240,
  206. Url = "icons/logo240.png"
  207. },
  208. new DeviceIcon
  209. {
  210. MimeType = "image/jpeg",
  211. Depth = "24",
  212. Width = 240,
  213. Height = 240,
  214. Url = "icons/logo240.jpg"
  215. },
  216. new DeviceIcon
  217. {
  218. MimeType = "image/png",
  219. Depth = "24",
  220. Width = 120,
  221. Height = 120,
  222. Url = "icons/logo120.png"
  223. },
  224. new DeviceIcon
  225. {
  226. MimeType = "image/jpeg",
  227. Depth = "24",
  228. Width = 120,
  229. Height = 120,
  230. Url = "icons/logo120.jpg"
  231. },
  232. new DeviceIcon
  233. {
  234. MimeType = "image/png",
  235. Depth = "24",
  236. Width = 48,
  237. Height = 48,
  238. Url = "icons/logo48.png"
  239. },
  240. new DeviceIcon
  241. {
  242. MimeType = "image/jpeg",
  243. Depth = "24",
  244. Width = 48,
  245. Height = 48,
  246. Url = "icons/logo48.jpg"
  247. }
  248. };
  249. private IEnumerable<DeviceService> GetServices()
  250. {
  251. var list = new List<DeviceService>();
  252. list.Add(new DeviceService
  253. {
  254. ServiceType = "urn:schemas-upnp-org:service:ContentDirectory:1",
  255. ServiceId = "urn:upnp-org:serviceId:ContentDirectory",
  256. ScpdUrl = "contentdirectory/contentdirectory.xml",
  257. ControlUrl = "contentdirectory/control",
  258. EventSubUrl = "contentdirectory/events"
  259. });
  260. list.Add(new DeviceService
  261. {
  262. ServiceType = "urn:schemas-upnp-org:service:ConnectionManager:1",
  263. ServiceId = "urn:upnp-org:serviceId:ConnectionManager",
  264. ScpdUrl = "connectionmanager/connectionmanager.xml",
  265. ControlUrl = "connectionmanager/control",
  266. EventSubUrl = "connectionmanager/events"
  267. });
  268. if (_profile.EnableMSMediaReceiverRegistrar)
  269. {
  270. list.Add(new DeviceService
  271. {
  272. ServiceType = "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1",
  273. ServiceId = "urn:microsoft.com:serviceId:X_MS_MediaReceiverRegistrar",
  274. ScpdUrl = "mediareceiverregistrar/mediareceiverregistrar.xml",
  275. ControlUrl = "mediareceiverregistrar/control",
  276. EventSubUrl = "mediareceiverregistrar/events"
  277. });
  278. }
  279. return list;
  280. }
  281. public override string ToString()
  282. {
  283. return GetXml();
  284. }
  285. }
  286. }