DescriptionXmlBuilder.cs 12 KB

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