DescriptionXmlBuilder.cs 12 KB

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