2015-01-03 20:38:22 +01:00
|
|
|
|
using MediaBrowser.Controller.Dlna;
|
2014-04-10 17:06:54 +02:00
|
|
|
|
using ServiceStack;
|
|
|
|
|
using ServiceStack.Web;
|
2014-04-20 07:21:08 +02:00
|
|
|
|
using System;
|
2014-04-10 17:06:54 +02:00
|
|
|
|
using System.Collections.Generic;
|
2014-04-20 07:21:08 +02:00
|
|
|
|
using System.Globalization;
|
2014-04-10 17:06:54 +02:00
|
|
|
|
using System.IO;
|
2014-04-20 07:21:08 +02:00
|
|
|
|
using System.Linq;
|
2014-04-10 17:06:54 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2016-10-06 20:55:01 +02:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Controller.IO;
|
2016-10-23 21:14:57 +02:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2014-04-10 17:06:54 +02:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.Dlna
|
|
|
|
|
{
|
|
|
|
|
[Route("/Dlna/{UuId}/description.xml", "GET", Summary = "Gets dlna server info")]
|
|
|
|
|
[Route("/Dlna/{UuId}/description", "GET", Summary = "Gets dlna server info")]
|
|
|
|
|
public class GetDescriptionXml
|
|
|
|
|
{
|
|
|
|
|
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
|
|
|
|
|
public string UuId { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 22:14:08 +01:00
|
|
|
|
[Route("/Dlna/{UuId}/contentdirectory/contentdirectory.xml", "GET", Summary = "Gets dlna content directory xml")]
|
|
|
|
|
[Route("/Dlna/{UuId}/contentdirectory/contentdirectory", "GET", Summary = "Gets dlna content directory xml")]
|
2014-04-10 17:06:54 +02:00
|
|
|
|
public class GetContentDirectory
|
|
|
|
|
{
|
2015-01-30 22:14:08 +01:00
|
|
|
|
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
|
|
|
|
|
public string UuId { get; set; }
|
2014-04-10 17:06:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 22:14:08 +01:00
|
|
|
|
[Route("/Dlna/{UuId}/connectionmanager/connectionmanager.xml", "GET", Summary = "Gets dlna connection manager xml")]
|
|
|
|
|
[Route("/Dlna/{UuId}/connectionmanager/connectionmanager", "GET", Summary = "Gets dlna connection manager xml")]
|
2014-05-21 02:56:24 +02:00
|
|
|
|
public class GetConnnectionManager
|
|
|
|
|
{
|
2015-01-30 22:14:08 +01:00
|
|
|
|
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
|
|
|
|
|
public string UuId { get; set; }
|
2014-05-21 02:56:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 22:14:08 +01:00
|
|
|
|
[Route("/Dlna/{UuId}/mediareceiverregistrar/mediareceiverregistrar.xml", "GET", Summary = "Gets dlna mediareceiverregistrar xml")]
|
|
|
|
|
[Route("/Dlna/{UuId}/mediareceiverregistrar/mediareceiverregistrar", "GET", Summary = "Gets dlna mediareceiverregistrar xml")]
|
2015-01-30 18:58:38 +01:00
|
|
|
|
public class GetMediaReceiverRegistrar
|
|
|
|
|
{
|
2015-01-30 22:14:08 +01:00
|
|
|
|
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
|
|
|
|
|
public string UuId { get; set; }
|
2015-01-30 18:58:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 22:14:08 +01:00
|
|
|
|
[Route("/Dlna/{UuId}/contentdirectory/control", "POST", Summary = "Processes a control request")]
|
2014-05-21 02:56:24 +02:00
|
|
|
|
public class ProcessContentDirectoryControlRequest : IRequiresRequestStream
|
|
|
|
|
{
|
|
|
|
|
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
|
|
|
|
|
public string UuId { get; set; }
|
|
|
|
|
|
|
|
|
|
public Stream RequestStream { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 22:14:08 +01:00
|
|
|
|
[Route("/Dlna/{UuId}/connectionmanager/control", "POST", Summary = "Processes a control request")]
|
2014-05-21 02:56:24 +02:00
|
|
|
|
public class ProcessConnectionManagerControlRequest : IRequiresRequestStream
|
2014-04-10 17:06:54 +02:00
|
|
|
|
{
|
2014-04-18 07:03:01 +02:00
|
|
|
|
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
|
|
|
|
|
public string UuId { get; set; }
|
2014-04-20 07:21:08 +02:00
|
|
|
|
|
2014-04-10 17:06:54 +02:00
|
|
|
|
public Stream RequestStream { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 22:14:08 +01:00
|
|
|
|
[Route("/Dlna/{UuId}/mediareceiverregistrar/control", "POST", Summary = "Processes a control request")]
|
2015-01-30 18:58:38 +01:00
|
|
|
|
public class ProcessMediaReceiverRegistrarControlRequest : IRequiresRequestStream
|
|
|
|
|
{
|
|
|
|
|
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
|
|
|
|
|
public string UuId { get; set; }
|
|
|
|
|
|
|
|
|
|
public Stream RequestStream { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 22:14:08 +01:00
|
|
|
|
[Route("/Dlna/{UuId}/mediareceiverregistrar/events", Summary = "Processes an event subscription request")]
|
2015-01-30 18:58:38 +01:00
|
|
|
|
public class ProcessMediaReceiverRegistrarEventRequest
|
|
|
|
|
{
|
2015-02-01 06:07:03 +01:00
|
|
|
|
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "SUBSCRIBE,POST")]
|
2015-01-30 18:58:38 +01:00
|
|
|
|
public string UuId { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 22:14:08 +01:00
|
|
|
|
[Route("/Dlna/{UuId}/contentdirectory/events", Summary = "Processes an event subscription request")]
|
2014-05-21 03:15:46 +02:00
|
|
|
|
public class ProcessContentDirectoryEventRequest
|
|
|
|
|
{
|
2015-02-01 06:07:03 +01:00
|
|
|
|
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "SUBSCRIBE,POST")]
|
2014-05-21 03:15:46 +02:00
|
|
|
|
public string UuId { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 22:14:08 +01:00
|
|
|
|
[Route("/Dlna/{UuId}/connectionmanager/events", Summary = "Processes an event subscription request")]
|
2014-05-21 03:15:46 +02:00
|
|
|
|
public class ProcessConnectionManagerEventRequest
|
2014-04-20 07:21:08 +02:00
|
|
|
|
{
|
2015-02-01 06:07:03 +01:00
|
|
|
|
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "SUBSCRIBE,POST")]
|
2014-04-20 07:21:08 +02:00
|
|
|
|
public string UuId { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 22:14:08 +01:00
|
|
|
|
[Route("/Dlna/{UuId}/icons/{Filename}", "GET", Summary = "Gets a server icon")]
|
2014-04-16 07:08:12 +02:00
|
|
|
|
[Route("/Dlna/icons/{Filename}", "GET", Summary = "Gets a server icon")]
|
|
|
|
|
public class GetIcon
|
|
|
|
|
{
|
2015-01-30 22:14:08 +01:00
|
|
|
|
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
|
|
|
|
|
public string UuId { get; set; }
|
|
|
|
|
|
2014-04-16 07:08:12 +02:00
|
|
|
|
[ApiMember(Name = "Filename", Description = "The icon filename", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
|
|
|
|
|
public string Filename { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-10 17:06:54 +02:00
|
|
|
|
public class DlnaServerService : BaseApiService
|
|
|
|
|
{
|
|
|
|
|
private readonly IDlnaManager _dlnaManager;
|
2014-04-21 18:02:30 +02:00
|
|
|
|
private readonly IContentDirectory _contentDirectory;
|
2014-05-21 02:56:24 +02:00
|
|
|
|
private readonly IConnectionManager _connectionManager;
|
2015-01-30 18:58:38 +01:00
|
|
|
|
private readonly IMediaReceiverRegistrar _mediaReceiverRegistrar;
|
2014-04-10 17:06:54 +02:00
|
|
|
|
|
2015-05-13 06:16:33 +02:00
|
|
|
|
private const string XMLContentType = "text/xml; charset=UTF-8";
|
2016-10-06 20:55:01 +02:00
|
|
|
|
private readonly IMemoryStreamProvider _memoryStreamProvider;
|
2015-05-12 01:04:16 +02:00
|
|
|
|
|
2016-10-06 20:55:01 +02:00
|
|
|
|
public DlnaServerService(IDlnaManager dlnaManager, IContentDirectory contentDirectory, IConnectionManager connectionManager, IMediaReceiverRegistrar mediaReceiverRegistrar, IMemoryStreamProvider memoryStreamProvider)
|
2014-04-10 17:06:54 +02:00
|
|
|
|
{
|
|
|
|
|
_dlnaManager = dlnaManager;
|
2014-04-21 18:02:30 +02:00
|
|
|
|
_contentDirectory = contentDirectory;
|
2014-05-21 02:56:24 +02:00
|
|
|
|
_connectionManager = connectionManager;
|
2015-01-30 18:58:38 +01:00
|
|
|
|
_mediaReceiverRegistrar = mediaReceiverRegistrar;
|
2016-10-06 20:55:01 +02:00
|
|
|
|
_memoryStreamProvider = memoryStreamProvider;
|
2014-04-10 17:06:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object Get(GetDescriptionXml request)
|
|
|
|
|
{
|
2015-01-30 22:14:08 +01:00
|
|
|
|
var url = Request.AbsoluteUri;
|
|
|
|
|
var serverAddress = url.Substring(0, url.IndexOf("/dlna/", StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
var xml = _dlnaManager.GetServerDescriptionXml(GetRequestHeaders(), request.UuId, serverAddress);
|
2014-04-10 17:06:54 +02:00
|
|
|
|
|
2015-05-12 01:04:16 +02:00
|
|
|
|
return ResultFactory.GetResult(xml, XMLContentType);
|
2014-04-10 17:06:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object Get(GetContentDirectory request)
|
|
|
|
|
{
|
2014-05-21 02:56:24 +02:00
|
|
|
|
var xml = _contentDirectory.GetServiceXml(GetRequestHeaders());
|
2014-04-10 17:06:54 +02:00
|
|
|
|
|
2015-05-12 01:04:16 +02:00
|
|
|
|
return ResultFactory.GetResult(xml, XMLContentType);
|
2014-04-10 17:06:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 18:58:38 +01:00
|
|
|
|
public object Get(GetMediaReceiverRegistrar request)
|
|
|
|
|
{
|
|
|
|
|
var xml = _mediaReceiverRegistrar.GetServiceXml(GetRequestHeaders());
|
|
|
|
|
|
2015-05-12 01:04:16 +02:00
|
|
|
|
return ResultFactory.GetResult(xml, XMLContentType);
|
2015-01-30 18:58:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 02:56:24 +02:00
|
|
|
|
public object Get(GetConnnectionManager request)
|
|
|
|
|
{
|
|
|
|
|
var xml = _connectionManager.GetServiceXml(GetRequestHeaders());
|
|
|
|
|
|
2015-05-12 01:04:16 +02:00
|
|
|
|
return ResultFactory.GetResult(xml, XMLContentType);
|
2014-05-21 02:56:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 18:58:38 +01:00
|
|
|
|
public async Task<object> Post(ProcessMediaReceiverRegistrarControlRequest request)
|
2014-05-21 02:56:24 +02:00
|
|
|
|
{
|
2015-01-30 19:48:17 +01:00
|
|
|
|
var response = await PostAsync(request.RequestStream, _mediaReceiverRegistrar).ConfigureAwait(false);
|
2014-05-21 02:56:24 +02:00
|
|
|
|
|
2015-05-12 01:04:16 +02:00
|
|
|
|
return ResultFactory.GetResult(response.Xml, XMLContentType);
|
2014-05-21 02:56:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 18:58:38 +01:00
|
|
|
|
public async Task<object> Post(ProcessContentDirectoryControlRequest request)
|
|
|
|
|
{
|
2015-01-30 19:48:17 +01:00
|
|
|
|
var response = await PostAsync(request.RequestStream, _contentDirectory).ConfigureAwait(false);
|
2015-01-30 18:58:38 +01:00
|
|
|
|
|
2015-05-12 01:04:16 +02:00
|
|
|
|
return ResultFactory.GetResult(response.Xml, XMLContentType);
|
2015-01-30 18:58:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-17 07:38:13 +02:00
|
|
|
|
public async Task<object> Post(ProcessConnectionManagerControlRequest request)
|
2014-04-10 17:06:54 +02:00
|
|
|
|
{
|
2014-08-17 07:38:13 +02:00
|
|
|
|
var response = await PostAsync(request.RequestStream, _connectionManager).ConfigureAwait(false);
|
2014-04-10 17:06:54 +02:00
|
|
|
|
|
2015-05-12 01:04:16 +02:00
|
|
|
|
return ResultFactory.GetResult(response.Xml, XMLContentType);
|
2014-04-10 17:06:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 02:56:24 +02:00
|
|
|
|
private async Task<ControlResponse> PostAsync(Stream requestStream, IUpnpService service)
|
2014-04-10 17:06:54 +02:00
|
|
|
|
{
|
2015-01-22 17:41:34 +01:00
|
|
|
|
var id = GetPathValue(2);
|
2014-04-20 07:21:08 +02:00
|
|
|
|
|
2014-05-21 02:56:24 +02:00
|
|
|
|
using (var reader = new StreamReader(requestStream))
|
2014-04-10 17:06:54 +02:00
|
|
|
|
{
|
2014-05-21 02:56:24 +02:00
|
|
|
|
return service.ProcessControlRequest(new ControlRequest
|
2014-04-10 17:06:54 +02:00
|
|
|
|
{
|
|
|
|
|
Headers = GetRequestHeaders(),
|
2014-04-18 07:03:01 +02:00
|
|
|
|
InputXml = await reader.ReadToEndAsync().ConfigureAwait(false),
|
2014-04-25 19:30:41 +02:00
|
|
|
|
TargetServerUuId = id,
|
|
|
|
|
RequestedUrl = Request.AbsoluteUri
|
2014-04-10 17:06:54 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
2014-04-16 07:08:12 +02:00
|
|
|
|
}
|
2014-04-10 17:06:54 +02:00
|
|
|
|
|
|
|
|
|
private IDictionary<string, string> GetRequestHeaders()
|
|
|
|
|
{
|
|
|
|
|
var headers = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
foreach (var key in Request.Headers.AllKeys)
|
|
|
|
|
{
|
|
|
|
|
headers[key] = Request.Headers[key];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return headers;
|
|
|
|
|
}
|
2014-04-16 07:08:12 +02:00
|
|
|
|
|
|
|
|
|
public object Get(GetIcon request)
|
|
|
|
|
{
|
|
|
|
|
using (var response = _dlnaManager.GetIcon(request.Filename))
|
|
|
|
|
{
|
2016-10-06 20:55:01 +02:00
|
|
|
|
using (var ms = _memoryStreamProvider.CreateNew())
|
2014-04-16 07:08:12 +02:00
|
|
|
|
{
|
|
|
|
|
response.Stream.CopyTo(ms);
|
|
|
|
|
|
|
|
|
|
ms.Position = 0;
|
|
|
|
|
var bytes = ms.ToArray();
|
|
|
|
|
return ResultFactory.GetResult(bytes, "image/" + response.Format.ToString().ToLower());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-20 07:21:08 +02:00
|
|
|
|
|
2014-05-21 03:15:46 +02:00
|
|
|
|
public object Any(ProcessContentDirectoryEventRequest request)
|
|
|
|
|
{
|
|
|
|
|
return ProcessEventRequest(_contentDirectory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object Any(ProcessConnectionManagerEventRequest request)
|
|
|
|
|
{
|
|
|
|
|
return ProcessEventRequest(_connectionManager);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 18:58:38 +01:00
|
|
|
|
public object Any(ProcessMediaReceiverRegistrarEventRequest request)
|
|
|
|
|
{
|
|
|
|
|
return ProcessEventRequest(_mediaReceiverRegistrar);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 03:15:46 +02:00
|
|
|
|
private object ProcessEventRequest(IEventManager eventManager)
|
2014-04-20 07:21:08 +02:00
|
|
|
|
{
|
|
|
|
|
var subscriptionId = GetHeader("SID");
|
|
|
|
|
var notificationType = GetHeader("NT");
|
|
|
|
|
var callback = GetHeader("CALLBACK");
|
|
|
|
|
var timeoutString = GetHeader("TIMEOUT");
|
|
|
|
|
|
2014-04-21 18:02:30 +02:00
|
|
|
|
var timeout = ParseTimeout(timeoutString);
|
2014-04-20 07:21:08 +02:00
|
|
|
|
|
|
|
|
|
if (string.Equals(Request.Verb, "SUBSCRIBE", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(notificationType))
|
|
|
|
|
{
|
2014-05-21 03:15:46 +02:00
|
|
|
|
return GetSubscriptionResponse(eventManager.RenewEventSubscription(subscriptionId, timeout));
|
2014-04-20 07:21:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 03:15:46 +02:00
|
|
|
|
return GetSubscriptionResponse(eventManager.CreateEventSubscription(notificationType, timeout, callback));
|
2014-04-20 07:21:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 03:15:46 +02:00
|
|
|
|
return GetSubscriptionResponse(eventManager.CancelEventSubscription(subscriptionId));
|
2014-04-20 07:21:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-21 18:02:30 +02:00
|
|
|
|
private object GetSubscriptionResponse(EventSubscriptionResponse response)
|
2014-04-20 07:21:08 +02:00
|
|
|
|
{
|
2014-04-21 18:02:30 +02:00
|
|
|
|
return ResultFactory.GetResult(response.Content, response.ContentType, response.Headers);
|
2014-04-20 07:21:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
private int? ParseTimeout(string header)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(header))
|
|
|
|
|
{
|
|
|
|
|
// Starts with SECOND-
|
|
|
|
|
header = header.Split('-').Last();
|
|
|
|
|
|
|
|
|
|
int val;
|
|
|
|
|
|
|
|
|
|
if (int.TryParse(header, NumberStyles.Any, _usCulture, out val))
|
|
|
|
|
{
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-04-10 17:06:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|