HlsSegmentController.cs 7.5 KB

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