| | 1 | | using System; |
| | 2 | | using System.ComponentModel.DataAnnotations; |
| | 3 | | using System.Diagnostics.CodeAnalysis; |
| | 4 | | using System.IO; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using Jellyfin.Api.Attributes; |
| | 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 | |
|
| | 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 ITranscodeManager _transcodeManager; |
| | 28 | |
|
| | 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.</p |
| | 34 | | /// <param name="transcodeManager">Instance of the <see cref="ITranscodeManager"/> interface.</param> |
| 0 | 35 | | public HlsSegmentController( |
| 0 | 36 | | IFileSystem fileSystem, |
| 0 | 37 | | IServerConfigurationManager serverConfigurationManager, |
| 0 | 38 | | ITranscodeManager transcodeManager) |
| | 39 | | { |
| 0 | 40 | | _fileSystem = fileSystem; |
| 0 | 41 | | _serverConfigurationManager = serverConfigurationManager; |
| 0 | 42 | | _transcodeManager = transcodeManager; |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Gets the specified audio segment for an audio item. |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="itemId">The item id.</param> |
| | 49 | | /// <param name="segmentId">The segment id.</param> |
| | 50 | | /// <response code="200">Hls audio segment returned.</response> |
| | 51 | | /// <returns>A <see cref="FileStreamResult"/> containing the audio stream.</returns> |
| | 52 | | // Can't require authentication just yet due to seeing some requests come from Chrome without full query string |
| | 53 | | // [Authenticated] |
| | 54 | | [HttpGet("Audio/{itemId}/hls/{segmentId}/stream.mp3", Name = "GetHlsAudioSegmentLegacyMp3")] |
| | 55 | | [HttpGet("Audio/{itemId}/hls/{segmentId}/stream.aac", Name = "GetHlsAudioSegmentLegacyAac")] |
| | 56 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | 57 | | [ProducesAudioFile] |
| | 58 | | [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "itemId", Justification = "Re |
| | 59 | | public ActionResult GetHlsAudioSegmentLegacy([FromRoute, Required] string itemId, [FromRoute, Required] string segme |
| | 60 | | { |
| | 61 | | // TODO: Deprecate with new iOS app |
| 0 | 62 | | var file = string.Concat(segmentId, Path.GetExtension(Request.Path.Value.AsSpan())); |
| 0 | 63 | | var transcodePath = _serverConfigurationManager.GetTranscodePath(); |
| 0 | 64 | | file = Path.GetFullPath(Path.Combine(transcodePath, file)); |
| 0 | 65 | | var fileDir = Path.GetDirectoryName(file); |
| 0 | 66 | | if (string.IsNullOrEmpty(fileDir) || !fileDir.StartsWith(transcodePath, StringComparison.InvariantCulture)) |
| | 67 | | { |
| 0 | 68 | | return BadRequest("Invalid segment."); |
| | 69 | | } |
| | 70 | |
|
| 0 | 71 | | return FileStreamResponseHelpers.GetStaticFileResult(file, MimeTypes.GetMimeType(file)); |
| | 72 | | } |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Gets a hls video playlist. |
| | 76 | | /// </summary> |
| | 77 | | /// <param name="itemId">The video id.</param> |
| | 78 | | /// <param name="playlistId">The playlist id.</param> |
| | 79 | | /// <response code="200">Hls video playlist returned.</response> |
| | 80 | | /// <returns>A <see cref="FileStreamResult"/> containing the playlist.</returns> |
| | 81 | | [HttpGet("Videos/{itemId}/hls/{playlistId}/stream.m3u8")] |
| | 82 | | [Authorize] |
| | 83 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | 84 | | [ProducesPlaylistFile] |
| | 85 | | [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "itemId", Justification = "Re |
| | 86 | | public ActionResult GetHlsPlaylistLegacy([FromRoute, Required] string itemId, [FromRoute, Required] string playlistI |
| | 87 | | { |
| 0 | 88 | | var file = string.Concat(playlistId, Path.GetExtension(Request.Path.Value.AsSpan())); |
| 0 | 89 | | var transcodePath = _serverConfigurationManager.GetTranscodePath(); |
| 0 | 90 | | file = Path.GetFullPath(Path.Combine(transcodePath, file)); |
| 0 | 91 | | var fileDir = Path.GetDirectoryName(file); |
| 0 | 92 | | if (string.IsNullOrEmpty(fileDir) || !fileDir.StartsWith(transcodePath, StringComparison.InvariantCulture) |
| 0 | 93 | | || Path.GetExtension(file.AsSpan()).Equals(".m3u8", StringComparison.OrdinalIgnoreCase)) |
| | 94 | | { |
| 0 | 95 | | return BadRequest("Invalid segment."); |
| | 96 | | } |
| | 97 | |
|
| 0 | 98 | | return GetFileResult(file, file); |
| | 99 | | } |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Stops an active encoding. |
| | 103 | | /// </summary> |
| | 104 | | /// <param name="deviceId">The device id of the client requesting. Used to stop encoding processes when needed.</par |
| | 105 | | /// <param name="playSessionId">The play session id.</param> |
| | 106 | | /// <response code="204">Encoding stopped successfully.</response> |
| | 107 | | /// <returns>A <see cref="NoContentResult"/> indicating success.</returns> |
| | 108 | | [HttpDelete("Videos/ActiveEncodings")] |
| | 109 | | [Authorize] |
| | 110 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | 111 | | public ActionResult StopEncodingProcess( |
| | 112 | | [FromQuery, Required] string deviceId, |
| | 113 | | [FromQuery, Required] string playSessionId) |
| | 114 | | { |
| 0 | 115 | | _transcodeManager.KillTranscodingJobs(deviceId, playSessionId, _ => true); |
| 0 | 116 | | return NoContent(); |
| | 117 | | } |
| | 118 | |
|
| | 119 | | /// <summary> |
| | 120 | | /// Gets a hls video segment. |
| | 121 | | /// </summary> |
| | 122 | | /// <param name="itemId">The item id.</param> |
| | 123 | | /// <param name="playlistId">The playlist id.</param> |
| | 124 | | /// <param name="segmentId">The segment id.</param> |
| | 125 | | /// <param name="segmentContainer">The segment container.</param> |
| | 126 | | /// <response code="200">Hls video segment returned.</response> |
| | 127 | | /// <response code="404">Hls segment not found.</response> |
| | 128 | | /// <returns>A <see cref="FileStreamResult"/> containing the video segment.</returns> |
| | 129 | | // Can't require authentication just yet due to seeing some requests come from Chrome without full query string |
| | 130 | | // [Authenticated] |
| | 131 | | [HttpGet("Videos/{itemId}/hls/{playlistId}/{segmentId}.{segmentContainer}")] |
| | 132 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | 133 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 134 | | [ProducesVideoFile] |
| | 135 | | [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "itemId", Justification = "Re |
| | 136 | | public ActionResult GetHlsVideoSegmentLegacy( |
| | 137 | | [FromRoute, Required] string itemId, |
| | 138 | | [FromRoute, Required] string playlistId, |
| | 139 | | [FromRoute, Required] string segmentId, |
| | 140 | | [FromRoute, Required] string segmentContainer) |
| | 141 | | { |
| 0 | 142 | | var file = string.Concat(segmentId, Path.GetExtension(Request.Path.Value.AsSpan())); |
| 0 | 143 | | var transcodeFolderPath = _serverConfigurationManager.GetTranscodePath(); |
| | 144 | |
|
| 0 | 145 | | file = Path.GetFullPath(Path.Combine(transcodeFolderPath, file)); |
| 0 | 146 | | var fileDir = Path.GetDirectoryName(file); |
| 0 | 147 | | if (string.IsNullOrEmpty(fileDir) || !fileDir.StartsWith(transcodeFolderPath, StringComparison.InvariantCulture) |
| | 148 | | { |
| 0 | 149 | | return BadRequest("Invalid segment."); |
| | 150 | | } |
| | 151 | |
|
| 0 | 152 | | var normalizedPlaylistId = playlistId; |
| | 153 | |
|
| 0 | 154 | | var filePaths = _fileSystem.GetFilePaths(transcodeFolderPath); |
| | 155 | | // Add . to start of segment container for future use. |
| 0 | 156 | | segmentContainer = segmentContainer.Insert(0, "."); |
| 0 | 157 | | string? playlistPath = null; |
| 0 | 158 | | foreach (var path in filePaths) |
| | 159 | | { |
| 0 | 160 | | var pathExtension = Path.GetExtension(path); |
| 0 | 161 | | if ((string.Equals(pathExtension, segmentContainer, StringComparison.OrdinalIgnoreCase) |
| 0 | 162 | | || string.Equals(pathExtension, ".m3u8", StringComparison.OrdinalIgnoreCase)) |
| 0 | 163 | | && path.Contains(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase)) |
| | 164 | | { |
| 0 | 165 | | playlistPath = path; |
| 0 | 166 | | break; |
| | 167 | | } |
| | 168 | | } |
| | 169 | |
|
| 0 | 170 | | return playlistPath is null |
| 0 | 171 | | ? NotFound("Hls segment not found.") |
| 0 | 172 | | : GetFileResult(file, playlistPath); |
| | 173 | | } |
| | 174 | |
|
| | 175 | | private ActionResult GetFileResult(string path, string playlistPath) |
| | 176 | | { |
| 0 | 177 | | var transcodingJob = _transcodeManager.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls); |
| | 178 | |
|
| 0 | 179 | | Response.OnCompleted(() => |
| 0 | 180 | | { |
| 0 | 181 | | if (transcodingJob is not null) |
| 0 | 182 | | { |
| 0 | 183 | | _transcodeManager.OnTranscodeEndRequest(transcodingJob); |
| 0 | 184 | | } |
| 0 | 185 | |
|
| 0 | 186 | | return Task.CompletedTask; |
| 0 | 187 | | }); |
| | 188 | |
|
| 0 | 189 | | return FileStreamResponseHelpers.GetStaticFileResult(path, MimeTypes.GetMimeType(path)); |
| | 190 | | } |
| | 191 | | } |