HlsSegmentController.cs 7.3 KB

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