jellyfin-server/MediaBrowser.Dlna/Server/DescriptionXmlBuilder.cs

318 lines
11 KiB
C#
Raw Normal View History

2014-04-10 15:06:54 +00:00
using MediaBrowser.Dlna.Common;
using MediaBrowser.Model.Dlna;
2015-02-01 21:32:01 +00:00
using MediaBrowser.Model.Extensions;
2014-04-10 15:06:54 +00:00
using System;
using System.Collections.Generic;
using System.Globalization;
2015-02-01 21:32:01 +00:00
using System.Linq;
2014-04-10 15:06:54 +00:00
using System.Security;
using System.Text;
namespace MediaBrowser.Dlna.Server
{
public class DescriptionXmlBuilder
{
private readonly DeviceProfile _profile;
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly string _serverUdn;
2014-05-16 17:11:07 +00:00
private readonly string _serverAddress;
2015-02-01 21:32:01 +00:00
private readonly string _serverName;
2015-05-08 16:28:06 +00:00
private readonly string _serverId;
2014-04-10 15:06:54 +00:00
2015-05-08 16:28:06 +00:00
public DescriptionXmlBuilder(DeviceProfile profile, string serverUdn, string serverAddress, string serverName, string serverId)
2014-04-10 15:06:54 +00:00
{
if (string.IsNullOrWhiteSpace(serverUdn))
{
throw new ArgumentNullException("serverUdn");
}
2015-01-30 21:14:08 +00:00
if (string.IsNullOrWhiteSpace(serverAddress))
{
throw new ArgumentNullException("serverAddress");
}
2014-04-10 15:06:54 +00:00
_profile = profile;
_serverUdn = serverUdn;
2014-05-16 17:11:07 +00:00
_serverAddress = serverAddress;
2015-02-01 21:32:01 +00:00
_serverName = serverName;
2015-05-08 16:28:06 +00:00
_serverId = serverId;
2014-04-10 15:06:54 +00:00
}
2015-01-31 23:12:22 +00:00
private bool EnableAbsoluteUrls
{
2015-01-31 23:12:22 +00:00
get { return false; }
}
2014-04-10 15:06:54 +00:00
public string GetXml()
{
var builder = new StringBuilder();
builder.Append("<?xml version=\"1.0\"?>");
2014-04-24 14:11:05 +00:00
2015-05-08 16:28:06 +00:00
builder.Append("<root");
2015-05-10 21:56:13 +00:00
var attributes = _profile.XmlRootAttributes.ToList();
2016-01-15 18:45:48 +00:00
attributes.Insert(0, new XmlAttribute
2015-05-10 21:56:13 +00:00
{
2016-01-15 18:45:48 +00:00
Name = "xmlns:dlna",
Value = "urn:schemas-dlna-org:device-1-0"
});
2015-05-10 21:56:13 +00:00
attributes.Insert(0, new XmlAttribute
{
Name = "xmlns",
Value = "urn:schemas-upnp-org:device-1-0"
});
foreach (var att in attributes)
2014-04-24 14:11:05 +00:00
{
builder.AppendFormat(" {0}=\"{1}\"", att.Name, att.Value);
}
2015-05-10 21:56:13 +00:00
2014-04-24 14:11:05 +00:00
builder.Append(">");
2014-04-10 15:06:54 +00:00
builder.Append("<specVersion>");
builder.Append("<major>1</major>");
builder.Append("<minor>0</minor>");
builder.Append("</specVersion>");
AppendDeviceInfo(builder);
builder.Append("</root>");
return builder.ToString();
}
private void AppendDeviceInfo(StringBuilder builder)
{
2014-04-15 07:27:20 +00:00
builder.Append("<device>");
2014-04-10 15:06:54 +00:00
AppendDeviceProperties(builder);
2016-01-14 21:41:23 +00:00
AppendIconList(builder);
2014-04-10 15:06:54 +00:00
AppendServiceList(builder);
2014-04-15 07:27:20 +00:00
builder.Append("</device>");
2014-04-10 15:06:54 +00:00
}
private void AppendDeviceProperties(StringBuilder builder)
{
2015-05-12 02:32:37 +00:00
builder.Append("<deviceType>urn:schemas-upnp-org:device:MediaServer:1</deviceType>");
2015-05-08 16:28:06 +00:00
2016-01-15 18:45:48 +00:00
builder.Append("<dlna:X_DLNACAP>" + SecurityElement.Escape(_profile.XDlnaCap ?? string.Empty) + "</dlna:X_DLNACAP>");
2016-01-14 21:41:23 +00:00
2016-01-15 18:45:48 +00:00
builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">M-DMS-1.50</dlna:X_DLNADOC>");
builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">" + SecurityElement.Escape(_profile.XDlnaDoc ?? string.Empty) + "</dlna:X_DLNADOC>");
2015-05-10 21:56:13 +00:00
2015-02-01 21:32:01 +00:00
builder.Append("<friendlyName>" + SecurityElement.Escape(GetFriendlyName()) + "</friendlyName>");
2014-04-10 15:06:54 +00:00
builder.Append("<manufacturer>" + SecurityElement.Escape(_profile.Manufacturer ?? string.Empty) + "</manufacturer>");
builder.Append("<manufacturerURL>" + SecurityElement.Escape(_profile.ManufacturerUrl ?? string.Empty) + "</manufacturerURL>");
2015-05-08 16:28:06 +00:00
2015-05-10 21:56:13 +00:00
builder.Append("<modelDescription>" + SecurityElement.Escape(_profile.ModelDescription ?? string.Empty) + "</modelDescription>");
2015-05-12 02:32:37 +00:00
builder.Append("<modelName>" + SecurityElement.Escape(_profile.ModelName ?? string.Empty) + "</modelName>");
2015-05-10 21:56:13 +00:00
2014-04-10 15:06:54 +00:00
builder.Append("<modelNumber>" + SecurityElement.Escape(_profile.ModelNumber ?? string.Empty) + "</modelNumber>");
builder.Append("<modelURL>" + SecurityElement.Escape(_profile.ModelUrl ?? string.Empty) + "</modelURL>");
2015-05-08 16:28:06 +00:00
if (string.IsNullOrWhiteSpace(_profile.SerialNumber))
{
builder.Append("<serialNumber>" + SecurityElement.Escape(_serverId) + "</serialNumber>");
}
else
{
builder.Append("<serialNumber>" + SecurityElement.Escape(_profile.SerialNumber) + "</serialNumber>");
}
2014-04-10 15:06:54 +00:00
builder.Append("<UDN>uuid:" + SecurityElement.Escape(_serverUdn) + "</UDN>");
2015-01-31 23:12:22 +00:00
builder.Append("<presentationURL>" + SecurityElement.Escape(_serverAddress) + "</presentationURL>");
if (!EnableAbsoluteUrls)
{
2015-02-01 21:32:01 +00:00
//builder.Append("<URLBase>" + SecurityElement.Escape(_serverAddress) + "</URLBase>");
}
2015-01-30 21:14:08 +00:00
if (!string.IsNullOrWhiteSpace(_profile.SonyAggregationFlags))
{
builder.Append("<av:aggregationFlags xmlns:av=\"urn:schemas-sony-com:av\">" + SecurityElement.Escape(_profile.SonyAggregationFlags) + "</av:aggregationFlags>");
}
2014-04-10 15:06:54 +00:00
}
2015-02-01 21:32:01 +00:00
private string GetFriendlyName()
{
2016-03-30 01:33:18 +00:00
if (string.IsNullOrWhiteSpace(_profile.FriendlyName))
{
2016-03-30 03:12:20 +00:00
return "Emby - " + _serverName;
2016-03-30 01:33:18 +00:00
}
2015-02-01 21:32:01 +00:00
var characters = _serverName.Where(c => (char.IsLetterOrDigit(c) || c == '-')).ToArray();
var serverName = new string(characters);
2015-02-02 16:49:33 +00:00
var name = (_profile.FriendlyName ?? string.Empty).Replace("${HostName}", serverName, StringComparison.OrdinalIgnoreCase);
2015-02-01 21:32:01 +00:00
return name;
}
2014-04-10 15:06:54 +00:00
private void AppendIconList(StringBuilder builder)
{
builder.Append("<iconList>");
foreach (var icon in GetIcons())
{
builder.Append("<icon>");
builder.Append("<mimetype>" + SecurityElement.Escape(icon.MimeType ?? string.Empty) + "</mimetype>");
builder.Append("<width>" + SecurityElement.Escape(icon.Width.ToString(_usCulture)) + "</width>");
builder.Append("<height>" + SecurityElement.Escape(icon.Height.ToString(_usCulture)) + "</height>");
builder.Append("<depth>" + SecurityElement.Escape(icon.Depth ?? string.Empty) + "</depth>");
2015-01-30 21:14:08 +00:00
builder.Append("<url>" + BuildUrl(icon.Url) + "</url>");
2014-04-10 15:06:54 +00:00
builder.Append("</icon>");
}
builder.Append("</iconList>");
}
private void AppendServiceList(StringBuilder builder)
{
builder.Append("<serviceList>");
foreach (var service in GetServices())
{
2014-04-15 03:54:52 +00:00
builder.Append("<service>");
2014-04-10 15:06:54 +00:00
builder.Append("<serviceType>" + SecurityElement.Escape(service.ServiceType ?? string.Empty) + "</serviceType>");
builder.Append("<serviceId>" + SecurityElement.Escape(service.ServiceId ?? string.Empty) + "</serviceId>");
2015-01-30 21:14:08 +00:00
builder.Append("<SCPDURL>" + BuildUrl(service.ScpdUrl) + "</SCPDURL>");
builder.Append("<controlURL>" + BuildUrl(service.ControlUrl) + "</controlURL>");
builder.Append("<eventSubURL>" + BuildUrl(service.EventSubUrl) + "</eventSubURL>");
2014-04-10 15:06:54 +00:00
2014-04-15 03:54:52 +00:00
builder.Append("</service>");
2014-04-10 15:06:54 +00:00
}
builder.Append("</serviceList>");
}
2015-01-30 21:14:08 +00:00
private string BuildUrl(string url)
{
if (string.IsNullOrWhiteSpace(url))
{
return string.Empty;
}
url = url.TrimStart('/');
url = "/dlna/" + _serverUdn + "/" + url;
2015-01-31 23:12:22 +00:00
if (EnableAbsoluteUrls)
{
url = _serverAddress.TrimEnd('/') + url;
}
2015-01-30 21:14:08 +00:00
return SecurityElement.Escape(url);
}
2014-04-10 15:06:54 +00:00
private IEnumerable<DeviceIcon> GetIcons()
{
var list = new List<DeviceIcon>();
2014-07-30 03:31:35 +00:00
list.Add(new DeviceIcon
{
MimeType = "image/png",
Depth = "24",
Width = 240,
Height = 240,
2015-01-30 21:14:08 +00:00
Url = "icons/logo240.png"
2014-07-30 03:31:35 +00:00
});
list.Add(new DeviceIcon
{
MimeType = "image/jpeg",
Depth = "24",
Width = 240,
Height = 240,
2015-01-30 21:14:08 +00:00
Url = "icons/logo240.jpg"
2014-07-30 03:31:35 +00:00
});
2014-04-10 15:06:54 +00:00
list.Add(new DeviceIcon
{
2014-04-16 05:08:12 +00:00
MimeType = "image/png",
2014-04-10 15:06:54 +00:00
Depth = "24",
2014-04-16 05:08:12 +00:00
Width = 120,
Height = 120,
2015-01-30 21:14:08 +00:00
Url = "icons/logo120.png"
2014-04-10 15:06:54 +00:00
});
list.Add(new DeviceIcon
{
MimeType = "image/jpeg",
Depth = "24",
Width = 120,
Height = 120,
2015-01-30 21:14:08 +00:00
Url = "icons/logo120.jpg"
2014-04-10 15:06:54 +00:00
});
list.Add(new DeviceIcon
{
MimeType = "image/png",
Depth = "24",
Width = 48,
Height = 48,
2015-01-30 21:14:08 +00:00
Url = "icons/logo48.png"
2014-04-10 15:06:54 +00:00
});
list.Add(new DeviceIcon
{
2014-04-16 05:08:12 +00:00
MimeType = "image/jpeg",
2014-04-10 15:06:54 +00:00
Depth = "24",
2014-04-16 05:08:12 +00:00
Width = 48,
Height = 48,
2015-01-30 21:14:08 +00:00
Url = "icons/logo48.jpg"
2014-04-10 15:06:54 +00:00
});
2014-04-20 05:21:08 +00:00
2014-04-10 15:06:54 +00:00
return list;
}
private IEnumerable<DeviceService> GetServices()
{
var list = new List<DeviceService>();
list.Add(new DeviceService
{
ServiceType = "urn:schemas-upnp-org:service:ContentDirectory:1",
ServiceId = "urn:upnp-org:serviceId:ContentDirectory",
2015-01-30 21:14:08 +00:00
ScpdUrl = "contentdirectory/contentdirectory.xml",
ControlUrl = "contentdirectory/control",
EventSubUrl = "contentdirectory/events"
2014-04-10 15:06:54 +00:00
});
2014-05-21 00:56:24 +00:00
list.Add(new DeviceService
{
ServiceType = "urn:schemas-upnp-org:service:ConnectionManager:1",
ServiceId = "urn:upnp-org:serviceId:ConnectionManager",
2015-01-30 21:14:08 +00:00
ScpdUrl = "connectionmanager/connectionmanager.xml",
ControlUrl = "connectionmanager/control",
EventSubUrl = "connectionmanager/events"
2014-05-21 00:56:24 +00:00
});
2015-02-04 11:56:48 +00:00
if (_profile.EnableMSMediaReceiverRegistrar)
2015-01-30 17:58:38 +00:00
{
2015-02-04 11:56:48 +00:00
list.Add(new DeviceService
{
ServiceType = "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1",
ServiceId = "urn:microsoft.com:serviceId:X_MS_MediaReceiverRegistrar",
ScpdUrl = "mediareceiverregistrar/mediareceiverregistrar.xml",
ControlUrl = "mediareceiverregistrar/control",
EventSubUrl = "mediareceiverregistrar/events"
});
}
2015-01-30 17:58:38 +00:00
2014-04-10 15:06:54 +00:00
return list;
}
public override string ToString()
{
return GetXml();
}
}
}