HlsSegmentController.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Jellyfin.Api.Constants;
  8. using Jellyfin.Api.Helpers;
  9. using MediaBrowser.Common.Configuration;
  10. using MediaBrowser.Controller.Configuration;
  11. using MediaBrowser.Controller.MediaEncoding;
  12. using MediaBrowser.Model.IO;
  13. using MediaBrowser.Model.Net;
  14. using Microsoft.AspNetCore.Authorization;
  15. using Microsoft.AspNetCore.Http;
  16. using Microsoft.AspNetCore.Mvc;
  17. namespace Jellyfin.Api.Controllers
  18. {
  19. /// <summary>
  20. /// The hls segment controller.
  21. /// </summary>
  22. [Route("")]
  23. public class HlsSegmentController : BaseJellyfinApiController
  24. {
  25. private readonly IFileSystem _fileSystem;
  26. private readonly IServerConfigurationManager _serverConfigurationManager;
  27. private readonly TranscodingJobHelper _transcodingJobHelper;
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="HlsSegmentController"/> class.
  30. /// </summary>
  31. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  32. /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
  33. /// <param name="transcodingJobHelper">Initialized instance of the <see cref="TranscodingJobHelper"/>.</param>
  34. public HlsSegmentController(
  35. IFileSystem fileSystem,
  36. IServerConfigurationManager serverConfigurationManager,
  37. TranscodingJobHelper transcodingJobHelper)
  38. {
  39. _fileSystem = fileSystem;
  40. _serverConfigurationManager = serverConfigurationManager;
  41. _transcodingJobHelper = transcodingJobHelper;
  42. }
  43. /// <summary>
  44. /// Gets the specified audio segment for an audio item.
  45. /// </summary>
  46. /// <param name="itemId">The item id.</param>
  47. /// <param name="segmentId">The segment id.</param>
  48. /// <response code="200">Hls audio segment returned.</response>
  49. /// <returns>A <see cref="FileStreamResult"/> containing the audio stream.</returns>
  50. // Can't require authentication just yet due to seeing some requests come from Chrome without full query string
  51. // [Authenticated]
  52. [HttpGet("Audio/{itemId}/hls/{segmentId}/stream.mp3", Name = "GetHlsAudioSegmentLegacyMp3")]
  53. [HttpGet("Audio/{itemId}/hls/{segmentId}/stream.aac", Name = "GetHlsAudioSegmentLegacyAac")]
  54. [ProducesResponseType(StatusCodes.Status200OK)]
  55. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "itemId", Justification = "Required for ServiceStack")]
  56. public ActionResult GetHlsAudioSegmentLegacy([FromRoute][Required] string itemId, [FromRoute][Required] string segmentId)
  57. {
  58. // TODO: Deprecate with new iOS app
  59. var file = segmentId + Path.GetExtension(Request.Path);
  60. file = Path.Combine(_serverConfigurationManager.GetTranscodePath(), file);
  61. return FileStreamResponseHelpers.GetStaticFileResult(file, MimeTypes.GetMimeType(file)!, false, HttpContext);
  62. }
  63. /// <summary>
  64. /// Gets a hls video playlist.
  65. /// </summary>
  66. /// <param name="itemId">The video id.</param>
  67. /// <param name="playlistId">The playlist id.</param>
  68. /// <response code="200">Hls video playlist returned.</response>
  69. /// <returns>A <see cref="FileStreamResult"/> containing the playlist.</returns>
  70. [HttpGet("Videos/{itemId}/hls/{playlistId}/stream.m3u8")]
  71. [Authorize(Policy = Policies.DefaultAuthorization)]
  72. [ProducesResponseType(StatusCodes.Status200OK)]
  73. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "itemId", Justification = "Required for ServiceStack")]
  74. public ActionResult GetHlsPlaylistLegacy([FromRoute][Required] string itemId, [FromRoute][Required] string playlistId)
  75. {
  76. var file = playlistId + Path.GetExtension(Request.Path);
  77. file = Path.Combine(_serverConfigurationManager.GetTranscodePath(), file);
  78. return GetFileResult(file, file);
  79. }
  80. /// <summary>
  81. /// Stops an active encoding.
  82. /// </summary>
  83. /// <param name="deviceId">The device id of the client requesting. Used to stop encoding processes when needed.</param>
  84. /// <param name="playSessionId">The play session id.</param>
  85. /// <response code="204">Encoding stopped successfully.</response>
  86. /// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
  87. [HttpDelete("Videos/ActiveEncodings")]
  88. [Authorize(Policy = Policies.DefaultAuthorization)]
  89. [ProducesResponseType(StatusCodes.Status204NoContent)]
  90. public ActionResult StopEncodingProcess([FromQuery] string deviceId, [FromQuery] string playSessionId)
  91. {
  92. _transcodingJobHelper.KillTranscodingJobs(deviceId, playSessionId, path => true);
  93. return NoContent();
  94. }
  95. /// <summary>
  96. /// Gets a hls video segment.
  97. /// </summary>
  98. /// <param name="itemId">The item id.</param>
  99. /// <param name="playlistId">The playlist id.</param>
  100. /// <param name="segmentId">The segment id.</param>
  101. /// <param name="segmentContainer">The segment container.</param>
  102. /// <response code="200">Hls video segment returned.</response>
  103. /// <returns>A <see cref="FileStreamResult"/> containing the video segment.</returns>
  104. // Can't require authentication just yet due to seeing some requests come from Chrome without full query string
  105. // [Authenticated]
  106. [HttpGet("Videos/{itemId}/hls/{playlistId}/{segmentId}.{segmentContainer}")]
  107. [ProducesResponseType(StatusCodes.Status200OK)]
  108. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "itemId", Justification = "Required for ServiceStack")]
  109. public ActionResult GetHlsVideoSegmentLegacy(
  110. [FromRoute][Required] string itemId,
  111. [FromRoute][Required] string playlistId,
  112. [FromRoute][Required] string segmentId,
  113. [FromRoute][Required] string segmentContainer)
  114. {
  115. var file = segmentId + Path.GetExtension(Request.Path);
  116. var transcodeFolderPath = _serverConfigurationManager.GetTranscodePath();
  117. file = Path.Combine(transcodeFolderPath, file);
  118. var normalizedPlaylistId = playlistId;
  119. var playlistPath = _fileSystem.GetFilePaths(transcodeFolderPath)
  120. .FirstOrDefault(i =>
  121. string.Equals(Path.GetExtension(i), ".m3u8", StringComparison.OrdinalIgnoreCase)
  122. && i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1);
  123. return GetFileResult(file, playlistPath);
  124. }
  125. private ActionResult GetFileResult(string path, string playlistPath)
  126. {
  127. var transcodingJob = _transcodingJobHelper.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
  128. Response.OnCompleted(() =>
  129. {
  130. if (transcodingJob != null)
  131. {
  132. _transcodingJobHelper.OnTranscodeEndRequest(transcodingJob);
  133. }
  134. return Task.CompletedTask;
  135. });
  136. return FileStreamResponseHelpers.GetStaticFileResult(path, MimeTypes.GetMimeType(path)!, false, HttpContext);
  137. }
  138. }
  139. }