HlsSegmentController.cs 7.2 KB

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