2020-04-22 01:15:27 +00:00
|
|
|
using System;
|
2020-07-31 17:01:30 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2020-04-22 01:15:27 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Emby.Dlna;
|
|
|
|
using Emby.Dlna.Main;
|
|
|
|
using Jellyfin.Api.Attributes;
|
|
|
|
using MediaBrowser.Controller.Dlna;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace Jellyfin.Api.Controllers
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Dlna Server Controller.
|
|
|
|
/// </summary>
|
|
|
|
[Route("Dlna")]
|
|
|
|
public class DlnaServerController : BaseJellyfinApiController
|
|
|
|
{
|
|
|
|
private const string XMLContentType = "text/xml; charset=UTF-8";
|
|
|
|
|
|
|
|
private readonly IDlnaManager _dlnaManager;
|
|
|
|
private readonly IContentDirectory _contentDirectory;
|
|
|
|
private readonly IConnectionManager _connectionManager;
|
|
|
|
private readonly IMediaReceiverRegistrar _mediaReceiverRegistrar;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="DlnaServerController"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dlnaManager">Instance of the <see cref="IDlnaManager"/> interface.</param>
|
|
|
|
public DlnaServerController(IDlnaManager dlnaManager)
|
|
|
|
{
|
|
|
|
_dlnaManager = dlnaManager;
|
|
|
|
_contentDirectory = DlnaEntryPoint.Current.ContentDirectory;
|
|
|
|
_connectionManager = DlnaEntryPoint.Current.ConnectionManager;
|
|
|
|
_mediaReceiverRegistrar = DlnaEntryPoint.Current.MediaReceiverRegistrar;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Get Description Xml.
|
|
|
|
/// </summary>
|
2020-07-31 17:01:30 +00:00
|
|
|
/// <param name="serverId">Server UUID.</param>
|
|
|
|
/// <response code="200">Description xml returned.</response>
|
|
|
|
/// <returns>An <see cref="OkResult"/> containing the description xml.</returns>
|
|
|
|
[HttpGet("{serverId}/description.xml")]
|
|
|
|
[HttpGet("{serverId}/description")]
|
2020-04-22 01:15:27 +00:00
|
|
|
[Produces(XMLContentType)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-07-31 17:01:30 +00:00
|
|
|
public ActionResult GetDescriptionXml([FromRoute] string serverId)
|
2020-04-22 01:15:27 +00:00
|
|
|
{
|
|
|
|
var url = GetAbsoluteUri();
|
|
|
|
var serverAddress = url.Substring(0, url.IndexOf("/dlna/", StringComparison.OrdinalIgnoreCase));
|
2020-07-31 17:01:30 +00:00
|
|
|
var xml = _dlnaManager.GetServerDescriptionXml(Request.Headers, serverId, serverAddress);
|
2020-04-22 01:15:27 +00:00
|
|
|
return Ok(xml);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets Dlna content directory xml.
|
|
|
|
/// </summary>
|
2020-07-31 17:01:30 +00:00
|
|
|
/// <param name="serverId">Server UUID.</param>
|
|
|
|
/// <response code="200">Dlna content directory returned.</response>
|
|
|
|
/// <returns>An <see cref="OkResult"/> containing the dlna content directory xml.</returns>
|
|
|
|
[HttpGet("{serverId}/ContentDirectory/ContentDirectory.xml")]
|
|
|
|
[HttpGet("{serverId}/ContentDirectory/ContentDirectory")]
|
2020-04-22 01:15:27 +00:00
|
|
|
[Produces(XMLContentType)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-07-31 17:01:30 +00:00
|
|
|
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
|
|
|
|
public ActionResult GetContentDirectory([FromRoute] string serverId)
|
2020-04-22 01:15:27 +00:00
|
|
|
{
|
|
|
|
return Ok(_contentDirectory.GetServiceXml());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets Dlna media receiver registrar xml.
|
|
|
|
/// </summary>
|
2020-07-31 17:01:30 +00:00
|
|
|
/// <param name="serverId">Server UUID.</param>
|
2020-04-22 01:15:27 +00:00
|
|
|
/// <returns>Dlna media receiver registrar xml.</returns>
|
2020-07-31 17:01:30 +00:00
|
|
|
[HttpGet("{serverId}/MediaReceiverRegistrar/MediaReceiverRegistrar.xml")]
|
|
|
|
[HttpGet("{serverId}/MediaReceiverRegistrar/MediaReceiverRegistrar")]
|
2020-04-22 01:15:27 +00:00
|
|
|
[Produces(XMLContentType)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-07-31 17:01:30 +00:00
|
|
|
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
|
|
|
|
public ActionResult GetMediaReceiverRegistrar([FromRoute] string serverId)
|
2020-04-22 01:15:27 +00:00
|
|
|
{
|
|
|
|
return Ok(_mediaReceiverRegistrar.GetServiceXml());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets Dlna media receiver registrar xml.
|
|
|
|
/// </summary>
|
2020-07-31 17:01:30 +00:00
|
|
|
/// <param name="serverId">Server UUID.</param>
|
2020-04-22 01:15:27 +00:00
|
|
|
/// <returns>Dlna media receiver registrar xml.</returns>
|
2020-07-31 17:01:30 +00:00
|
|
|
[HttpGet("{serverId}/ConnectionManager/ConnectionManager.xml")]
|
|
|
|
[HttpGet("{serverId}/ConnectionManager/ConnectionManager")]
|
2020-04-22 01:15:27 +00:00
|
|
|
[Produces(XMLContentType)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-07-31 17:01:30 +00:00
|
|
|
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
|
|
|
|
public ActionResult GetConnectionManager([FromRoute] string serverId)
|
2020-04-22 01:15:27 +00:00
|
|
|
{
|
|
|
|
return Ok(_connectionManager.GetServiceXml());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Process a content directory control request.
|
|
|
|
/// </summary>
|
2020-07-31 17:01:30 +00:00
|
|
|
/// <param name="serverId">Server UUID.</param>
|
2020-04-22 01:15:27 +00:00
|
|
|
/// <returns>Control response.</returns>
|
2020-07-31 17:01:30 +00:00
|
|
|
[HttpPost("{serverId}/ContentDirectory/Control")]
|
|
|
|
public async Task<ActionResult<ControlResponse>> ProcessContentDirectoryControlRequest([FromRoute] string serverId)
|
2020-04-22 01:15:27 +00:00
|
|
|
{
|
2020-07-31 17:01:30 +00:00
|
|
|
return await ProcessControlRequestInternalAsync(serverId, Request.Body, _contentDirectory).ConfigureAwait(false);
|
2020-04-22 01:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Process a connection manager control request.
|
|
|
|
/// </summary>
|
2020-07-31 17:01:30 +00:00
|
|
|
/// <param name="serverId">Server UUID.</param>
|
2020-04-22 01:15:27 +00:00
|
|
|
/// <returns>Control response.</returns>
|
2020-07-31 17:01:30 +00:00
|
|
|
[HttpPost("{serverId}/ConnectionManager/Control")]
|
|
|
|
public async Task<ActionResult<ControlResponse>> ProcessConnectionManagerControlRequest([FromRoute] string serverId)
|
2020-04-22 01:15:27 +00:00
|
|
|
{
|
2020-07-31 17:01:30 +00:00
|
|
|
return await ProcessControlRequestInternalAsync(serverId, Request.Body, _connectionManager).ConfigureAwait(false);
|
2020-04-22 01:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Process a media receiver registrar control request.
|
|
|
|
/// </summary>
|
2020-07-31 17:01:30 +00:00
|
|
|
/// <param name="serverId">Server UUID.</param>
|
2020-04-22 01:15:27 +00:00
|
|
|
/// <returns>Control response.</returns>
|
2020-07-31 17:01:30 +00:00
|
|
|
[HttpPost("{serverId}/MediaReceiverRegistrar/Control")]
|
|
|
|
public async Task<ActionResult<ControlResponse>> ProcessMediaReceiverRegistrarControlRequest([FromRoute] string serverId)
|
2020-04-22 01:15:27 +00:00
|
|
|
{
|
2020-07-31 17:01:30 +00:00
|
|
|
return await ProcessControlRequestInternalAsync(serverId, Request.Body, _mediaReceiverRegistrar).ConfigureAwait(false);
|
2020-04-22 01:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Processes an event subscription request.
|
|
|
|
/// </summary>
|
2020-07-31 17:01:30 +00:00
|
|
|
/// <param name="serverId">Server UUID.</param>
|
2020-04-22 01:15:27 +00:00
|
|
|
/// <returns>Event subscription response.</returns>
|
2020-07-31 17:01:30 +00:00
|
|
|
[HttpSubscribe("{serverId}/MediaReceiverRegistrar/Events")]
|
|
|
|
[HttpUnsubscribe("{serverId}/MediaReceiverRegistrar/Events")]
|
|
|
|
[ApiExplorerSettings(IgnoreApi = true)] // Ignore in openapi docs
|
|
|
|
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
|
|
|
|
public ActionResult<EventSubscriptionResponse> ProcessMediaReceiverRegistrarEventRequest(string serverId)
|
2020-04-22 01:15:27 +00:00
|
|
|
{
|
2020-07-31 17:01:30 +00:00
|
|
|
return ProcessEventRequest(_mediaReceiverRegistrar);
|
2020-04-22 01:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Processes an event subscription request.
|
|
|
|
/// </summary>
|
2020-07-31 17:01:30 +00:00
|
|
|
/// <param name="serverId">Server UUID.</param>
|
2020-04-22 01:15:27 +00:00
|
|
|
/// <returns>Event subscription response.</returns>
|
2020-07-31 17:01:30 +00:00
|
|
|
[HttpSubscribe("{serverId}/ContentDirectory/Events")]
|
|
|
|
[HttpUnsubscribe("{serverId}/ContentDirectory/Events")]
|
|
|
|
[ApiExplorerSettings(IgnoreApi = true)] // Ignore in openapi docs
|
|
|
|
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
|
|
|
|
public ActionResult<EventSubscriptionResponse> ProcessContentDirectoryEventRequest(string serverId)
|
2020-04-22 01:15:27 +00:00
|
|
|
{
|
2020-07-31 17:01:30 +00:00
|
|
|
return ProcessEventRequest(_contentDirectory);
|
2020-04-22 01:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Processes an event subscription request.
|
|
|
|
/// </summary>
|
2020-07-31 17:01:30 +00:00
|
|
|
/// <param name="serverId">Server UUID.</param>
|
2020-04-22 01:15:27 +00:00
|
|
|
/// <returns>Event subscription response.</returns>
|
2020-07-31 17:01:30 +00:00
|
|
|
[HttpSubscribe("{serverId}/ConnectionManager/Events")]
|
|
|
|
[HttpUnsubscribe("{serverId}/ConnectionManager/Events")]
|
|
|
|
[ApiExplorerSettings(IgnoreApi = true)] // Ignore in openapi docs
|
|
|
|
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
|
|
|
|
public ActionResult<EventSubscriptionResponse> ProcessConnectionManagerEventRequest(string serverId)
|
2020-04-22 01:15:27 +00:00
|
|
|
{
|
2020-07-31 17:01:30 +00:00
|
|
|
return ProcessEventRequest(_connectionManager);
|
2020-04-22 01:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a server icon.
|
|
|
|
/// </summary>
|
2020-07-31 17:01:30 +00:00
|
|
|
/// <param name="serverId">Server UUID.</param>
|
2020-04-22 01:15:27 +00:00
|
|
|
/// <param name="fileName">The icon filename.</param>
|
|
|
|
/// <returns>Icon stream.</returns>
|
2020-07-31 17:01:30 +00:00
|
|
|
[HttpGet("{serverId}/icons/{filename}")]
|
|
|
|
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
|
|
|
|
public ActionResult GetIconId([FromRoute] string serverId, [FromRoute] string fileName)
|
2020-04-22 01:15:27 +00:00
|
|
|
{
|
2020-07-31 17:01:30 +00:00
|
|
|
return GetIconInternal(fileName);
|
2020-04-22 01:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a server icon.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="fileName">The icon filename.</param>
|
|
|
|
/// <returns>Icon stream.</returns>
|
2020-07-31 17:01:30 +00:00
|
|
|
[HttpGet("icons/{filename}")]
|
|
|
|
public ActionResult GetIcon([FromRoute] string fileName)
|
2020-04-22 01:15:27 +00:00
|
|
|
{
|
2020-07-31 17:01:30 +00:00
|
|
|
return GetIconInternal(fileName);
|
2020-04-22 01:15:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 17:01:30 +00:00
|
|
|
private ActionResult GetIconInternal(string fileName)
|
2020-04-22 01:15:27 +00:00
|
|
|
{
|
|
|
|
var icon = _dlnaManager.GetIcon(fileName);
|
|
|
|
if (icon == null)
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
var contentType = "image/" + Path.GetExtension(fileName)
|
|
|
|
.TrimStart('.')
|
|
|
|
.ToLowerInvariant();
|
|
|
|
|
2020-07-31 17:01:30 +00:00
|
|
|
return File(icon.Stream, contentType);
|
2020-04-22 01:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private string GetAbsoluteUri()
|
|
|
|
{
|
|
|
|
return $"{Request.Scheme}://{Request.Host}{Request.Path}";
|
|
|
|
}
|
|
|
|
|
2020-07-31 17:01:30 +00:00
|
|
|
private Task<ControlResponse> ProcessControlRequestInternalAsync(string id, Stream requestStream, IUpnpService service)
|
2020-04-22 01:15:27 +00:00
|
|
|
{
|
|
|
|
return service.ProcessControlRequestAsync(new ControlRequest
|
|
|
|
{
|
|
|
|
Headers = Request.Headers,
|
|
|
|
InputXml = requestStream,
|
|
|
|
TargetServerUuId = id,
|
|
|
|
RequestedUrl = GetAbsoluteUri()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private EventSubscriptionResponse ProcessEventRequest(IEventManager eventManager)
|
|
|
|
{
|
|
|
|
var subscriptionId = Request.Headers["SID"];
|
|
|
|
if (string.Equals(Request.Method, "subscribe", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
var notificationType = Request.Headers["NT"];
|
|
|
|
var callback = Request.Headers["CALLBACK"];
|
|
|
|
var timeoutString = Request.Headers["TIMEOUT"];
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(notificationType))
|
|
|
|
{
|
|
|
|
return eventManager.RenewEventSubscription(
|
|
|
|
subscriptionId,
|
|
|
|
notificationType,
|
|
|
|
timeoutString,
|
|
|
|
callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
return eventManager.CreateEventSubscription(notificationType, timeoutString, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
return eventManager.CancelEventSubscription(subscriptionId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|