DlnaServerController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.IO;
  5. using System.Net.Mime;
  6. using System.Threading.Tasks;
  7. using Emby.Dlna;
  8. using Emby.Dlna.Main;
  9. using Jellyfin.Api.Attributes;
  10. using Jellyfin.Api.Constants;
  11. using MediaBrowser.Controller.Dlna;
  12. using MediaBrowser.Model.Net;
  13. using Microsoft.AspNetCore.Authorization;
  14. using Microsoft.AspNetCore.Http;
  15. using Microsoft.AspNetCore.Mvc;
  16. namespace Jellyfin.Api.Controllers
  17. {
  18. /// <summary>
  19. /// Dlna Server Controller.
  20. /// </summary>
  21. [Route("Dlna")]
  22. [DlnaEnabled]
  23. [Authorize(Policy = Policies.AnonymousLanAccessPolicy)]
  24. public class DlnaServerController : BaseJellyfinApiController
  25. {
  26. private readonly IDlnaManager _dlnaManager;
  27. private readonly IContentDirectory _contentDirectory;
  28. private readonly IConnectionManager _connectionManager;
  29. private readonly IMediaReceiverRegistrar _mediaReceiverRegistrar;
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="DlnaServerController"/> class.
  32. /// </summary>
  33. /// <param name="dlnaManager">Instance of the <see cref="IDlnaManager"/> interface.</param>
  34. public DlnaServerController(IDlnaManager dlnaManager)
  35. {
  36. _dlnaManager = dlnaManager;
  37. _contentDirectory = DlnaEntryPoint.Current.ContentDirectory;
  38. _connectionManager = DlnaEntryPoint.Current.ConnectionManager;
  39. _mediaReceiverRegistrar = DlnaEntryPoint.Current.MediaReceiverRegistrar;
  40. }
  41. /// <summary>
  42. /// Get Description Xml.
  43. /// </summary>
  44. /// <param name="serverId">Server UUID.</param>
  45. /// <response code="200">Description xml returned.</response>
  46. /// <response code="503">DLNA is disabled.</response>
  47. /// <returns>An <see cref="OkResult"/> containing the description xml.</returns>
  48. [HttpGet("{serverId}/description")]
  49. [HttpGet("{serverId}/description.xml", Name = "GetDescriptionXml_2")]
  50. [ProducesResponseType(StatusCodes.Status200OK)]
  51. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  52. [Produces(MediaTypeNames.Text.Xml)]
  53. [ProducesFile(MediaTypeNames.Text.Xml)]
  54. public ActionResult GetDescriptionXml([FromRoute, Required] string serverId)
  55. {
  56. var url = GetAbsoluteUri();
  57. var serverAddress = url.Substring(0, url.IndexOf("/dlna/", StringComparison.OrdinalIgnoreCase));
  58. var xml = _dlnaManager.GetServerDescriptionXml(Request.Headers, serverId, serverAddress);
  59. return Ok(xml);
  60. }
  61. /// <summary>
  62. /// Gets Dlna content directory xml.
  63. /// </summary>
  64. /// <param name="serverId">Server UUID.</param>
  65. /// <response code="200">Dlna content directory returned.</response>
  66. /// <response code="503">DLNA is disabled.</response>
  67. /// <returns>An <see cref="OkResult"/> containing the dlna content directory xml.</returns>
  68. [HttpGet("{serverId}/ContentDirectory")]
  69. [HttpGet("{serverId}/ContentDirectory/ContentDirectory", Name = "GetContentDirectory_2")]
  70. [HttpGet("{serverId}/ContentDirectory/ContentDirectory.xml", Name = "GetContentDirectory_3")]
  71. [ProducesResponseType(StatusCodes.Status200OK)]
  72. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  73. [Produces(MediaTypeNames.Text.Xml)]
  74. [ProducesFile(MediaTypeNames.Text.Xml)]
  75. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
  76. public ActionResult GetContentDirectory([FromRoute, Required] string serverId)
  77. {
  78. return Ok(_contentDirectory.GetServiceXml());
  79. }
  80. /// <summary>
  81. /// Gets Dlna media receiver registrar xml.
  82. /// </summary>
  83. /// <param name="serverId">Server UUID.</param>
  84. /// <response code="200">Dlna media receiver registrar xml returned.</response>
  85. /// <response code="503">DLNA is disabled.</response>
  86. /// <returns>Dlna media receiver registrar xml.</returns>
  87. [HttpGet("{serverId}/MediaReceiverRegistrar")]
  88. [HttpGet("{serverId}/MediaReceiverRegistrar/MediaReceiverRegistrar", Name = "GetMediaReceiverRegistrar_2")]
  89. [HttpGet("{serverId}/MediaReceiverRegistrar/MediaReceiverRegistrar.xml", Name = "GetMediaReceiverRegistrar_3")]
  90. [ProducesResponseType(StatusCodes.Status200OK)]
  91. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  92. [Produces(MediaTypeNames.Text.Xml)]
  93. [ProducesFile(MediaTypeNames.Text.Xml)]
  94. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
  95. public ActionResult GetMediaReceiverRegistrar([FromRoute, Required] string serverId)
  96. {
  97. return Ok(_mediaReceiverRegistrar.GetServiceXml());
  98. }
  99. /// <summary>
  100. /// Gets Dlna media receiver registrar xml.
  101. /// </summary>
  102. /// <param name="serverId">Server UUID.</param>
  103. /// <response code="200">Dlna media receiver registrar xml returned.</response>
  104. /// <response code="503">DLNA is disabled.</response>
  105. /// <returns>Dlna media receiver registrar xml.</returns>
  106. [HttpGet("{serverId}/ConnectionManager")]
  107. [HttpGet("{serverId}/ConnectionManager/ConnectionManager", Name = "GetConnectionManager_2")]
  108. [HttpGet("{serverId}/ConnectionManager/ConnectionManager.xml", Name = "GetConnectionManager_3")]
  109. [ProducesResponseType(StatusCodes.Status200OK)]
  110. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  111. [Produces(MediaTypeNames.Text.Xml)]
  112. [ProducesFile(MediaTypeNames.Text.Xml)]
  113. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
  114. public ActionResult GetConnectionManager([FromRoute, Required] string serverId)
  115. {
  116. return Ok(_connectionManager.GetServiceXml());
  117. }
  118. /// <summary>
  119. /// Process a content directory control request.
  120. /// </summary>
  121. /// <param name="serverId">Server UUID.</param>
  122. /// <response code="200">Request processed.</response>
  123. /// <response code="503">DLNA is disabled.</response>
  124. /// <returns>Control response.</returns>
  125. [HttpPost("{serverId}/ContentDirectory/Control")]
  126. [ProducesResponseType(StatusCodes.Status200OK)]
  127. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  128. [Produces(MediaTypeNames.Text.Xml)]
  129. [ProducesFile(MediaTypeNames.Text.Xml)]
  130. public async Task<ActionResult<ControlResponse>> ProcessContentDirectoryControlRequest([FromRoute, Required] string serverId)
  131. {
  132. return await ProcessControlRequestInternalAsync(serverId, Request.Body, _contentDirectory).ConfigureAwait(false);
  133. }
  134. /// <summary>
  135. /// Process a connection manager control request.
  136. /// </summary>
  137. /// <param name="serverId">Server UUID.</param>
  138. /// <response code="200">Request processed.</response>
  139. /// <response code="503">DLNA is disabled.</response>
  140. /// <returns>Control response.</returns>
  141. [HttpPost("{serverId}/ConnectionManager/Control")]
  142. [ProducesResponseType(StatusCodes.Status200OK)]
  143. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  144. [Produces(MediaTypeNames.Text.Xml)]
  145. [ProducesFile(MediaTypeNames.Text.Xml)]
  146. public async Task<ActionResult<ControlResponse>> ProcessConnectionManagerControlRequest([FromRoute, Required] string serverId)
  147. {
  148. return await ProcessControlRequestInternalAsync(serverId, Request.Body, _connectionManager).ConfigureAwait(false);
  149. }
  150. /// <summary>
  151. /// Process a media receiver registrar control request.
  152. /// </summary>
  153. /// <param name="serverId">Server UUID.</param>
  154. /// <response code="200">Request processed.</response>
  155. /// <response code="503">DLNA is disabled.</response>
  156. /// <returns>Control response.</returns>
  157. [HttpPost("{serverId}/MediaReceiverRegistrar/Control")]
  158. [ProducesResponseType(StatusCodes.Status200OK)]
  159. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  160. [Produces(MediaTypeNames.Text.Xml)]
  161. [ProducesFile(MediaTypeNames.Text.Xml)]
  162. public async Task<ActionResult<ControlResponse>> ProcessMediaReceiverRegistrarControlRequest([FromRoute, Required] string serverId)
  163. {
  164. return await ProcessControlRequestInternalAsync(serverId, Request.Body, _mediaReceiverRegistrar).ConfigureAwait(false);
  165. }
  166. /// <summary>
  167. /// Processes an event subscription request.
  168. /// </summary>
  169. /// <param name="serverId">Server UUID.</param>
  170. /// <response code="200">Request processed.</response>
  171. /// <response code="503">DLNA is disabled.</response>
  172. /// <returns>Event subscription response.</returns>
  173. [HttpSubscribe("{serverId}/MediaReceiverRegistrar/Events")]
  174. [HttpUnsubscribe("{serverId}/MediaReceiverRegistrar/Events")]
  175. [ApiExplorerSettings(IgnoreApi = true)] // Ignore in openapi docs
  176. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
  177. [ProducesResponseType(StatusCodes.Status200OK)]
  178. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  179. [Produces(MediaTypeNames.Text.Xml)]
  180. [ProducesFile(MediaTypeNames.Text.Xml)]
  181. public ActionResult<EventSubscriptionResponse> ProcessMediaReceiverRegistrarEventRequest(string serverId)
  182. {
  183. return ProcessEventRequest(_mediaReceiverRegistrar);
  184. }
  185. /// <summary>
  186. /// Processes an event subscription request.
  187. /// </summary>
  188. /// <param name="serverId">Server UUID.</param>
  189. /// <response code="200">Request processed.</response>
  190. /// <response code="503">DLNA is disabled.</response>
  191. /// <returns>Event subscription response.</returns>
  192. [HttpSubscribe("{serverId}/ContentDirectory/Events")]
  193. [HttpUnsubscribe("{serverId}/ContentDirectory/Events")]
  194. [ApiExplorerSettings(IgnoreApi = true)] // Ignore in openapi docs
  195. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
  196. [ProducesResponseType(StatusCodes.Status200OK)]
  197. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  198. [Produces(MediaTypeNames.Text.Xml)]
  199. [ProducesFile(MediaTypeNames.Text.Xml)]
  200. public ActionResult<EventSubscriptionResponse> ProcessContentDirectoryEventRequest(string serverId)
  201. {
  202. return ProcessEventRequest(_contentDirectory);
  203. }
  204. /// <summary>
  205. /// Processes an event subscription request.
  206. /// </summary>
  207. /// <param name="serverId">Server UUID.</param>
  208. /// <response code="200">Request processed.</response>
  209. /// <response code="503">DLNA is disabled.</response>
  210. /// <returns>Event subscription response.</returns>
  211. [HttpSubscribe("{serverId}/ConnectionManager/Events")]
  212. [HttpUnsubscribe("{serverId}/ConnectionManager/Events")]
  213. [ApiExplorerSettings(IgnoreApi = true)] // Ignore in openapi docs
  214. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
  215. [ProducesResponseType(StatusCodes.Status200OK)]
  216. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  217. [Produces(MediaTypeNames.Text.Xml)]
  218. [ProducesFile(MediaTypeNames.Text.Xml)]
  219. public ActionResult<EventSubscriptionResponse> ProcessConnectionManagerEventRequest(string serverId)
  220. {
  221. return ProcessEventRequest(_connectionManager);
  222. }
  223. /// <summary>
  224. /// Gets a server icon.
  225. /// </summary>
  226. /// <param name="serverId">Server UUID.</param>
  227. /// <param name="fileName">The icon filename.</param>
  228. /// <response code="200">Request processed.</response>
  229. /// <response code="404">Not Found.</response>
  230. /// <response code="503">DLNA is disabled.</response>
  231. /// <returns>Icon stream.</returns>
  232. [HttpGet("{serverId}/icons/{fileName}")]
  233. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
  234. [ProducesResponseType(StatusCodes.Status200OK)]
  235. [ProducesResponseType(StatusCodes.Status404NotFound)]
  236. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  237. [ProducesImageFile]
  238. public ActionResult GetIconId([FromRoute, Required] string serverId, [FromRoute, Required] string fileName)
  239. {
  240. return GetIconInternal(fileName);
  241. }
  242. /// <summary>
  243. /// Gets a server icon.
  244. /// </summary>
  245. /// <param name="fileName">The icon filename.</param>
  246. /// <returns>Icon stream.</returns>
  247. /// <response code="200">Request processed.</response>
  248. /// <response code="404">Not Found.</response>
  249. /// <response code="503">DLNA is disabled.</response>
  250. [HttpGet("icons/{fileName}")]
  251. [ProducesResponseType(StatusCodes.Status200OK)]
  252. [ProducesResponseType(StatusCodes.Status404NotFound)]
  253. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  254. [ProducesImageFile]
  255. public ActionResult GetIcon([FromRoute, Required] string fileName)
  256. {
  257. return GetIconInternal(fileName);
  258. }
  259. private ActionResult GetIconInternal(string fileName)
  260. {
  261. var icon = _dlnaManager.GetIcon(fileName);
  262. if (icon == null)
  263. {
  264. return NotFound();
  265. }
  266. return File(icon.Stream, MimeTypes.GetMimeType(fileName));
  267. }
  268. private string GetAbsoluteUri()
  269. {
  270. return $"{Request.Scheme}://{Request.Host}{Request.PathBase}{Request.Path}";
  271. }
  272. private Task<ControlResponse> ProcessControlRequestInternalAsync(string id, Stream requestStream, IUpnpService service)
  273. {
  274. return service.ProcessControlRequestAsync(new ControlRequest(Request.Headers)
  275. {
  276. InputXml = requestStream,
  277. TargetServerUuId = id,
  278. RequestedUrl = GetAbsoluteUri()
  279. });
  280. }
  281. private EventSubscriptionResponse ProcessEventRequest(IDlnaEventManager dlnaEventManager)
  282. {
  283. var subscriptionId = Request.Headers["SID"];
  284. if (string.Equals(Request.Method, "subscribe", StringComparison.OrdinalIgnoreCase))
  285. {
  286. var notificationType = Request.Headers["NT"];
  287. var callback = Request.Headers["CALLBACK"];
  288. var timeoutString = Request.Headers["TIMEOUT"];
  289. if (string.IsNullOrEmpty(notificationType))
  290. {
  291. return dlnaEventManager.RenewEventSubscription(
  292. subscriptionId,
  293. notificationType,
  294. timeoutString,
  295. callback);
  296. }
  297. return dlnaEventManager.CreateEventSubscription(notificationType, timeoutString, callback);
  298. }
  299. return dlnaEventManager.CancelEventSubscription(subscriptionId);
  300. }
  301. }
  302. }