< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.HlsSegmentController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/HlsSegmentController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 56
Coverable lines: 56
Total lines: 192
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 24
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 1/23/2026 - 12:11:06 AM Line coverage: 0% (0/56) Branch coverage: 0% (0/24) Total lines: 1914/30/2026 - 12:14:58 AM Line coverage: 0% (0/56) Branch coverage: 0% (0/24) Total lines: 192 1/23/2026 - 12:11:06 AM Line coverage: 0% (0/56) Branch coverage: 0% (0/24) Total lines: 1914/30/2026 - 12:14:58 AM Line coverage: 0% (0/56) Branch coverage: 0% (0/24) Total lines: 192

Coverage delta

Coverage delta 1 -1

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetHlsAudioSegmentLegacy(...)0%2040%
GetHlsPlaylistLegacy(...)0%4260%
StopEncodingProcess(...)100%210%
GetHlsVideoSegmentLegacy(...)0%210140%
GetFileResult(...)100%210%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Controllers/HlsSegmentController.cs

#LineLine coverage
 1using System;
 2using System.ComponentModel.DataAnnotations;
 3using System.Diagnostics.CodeAnalysis;
 4using System.IO;
 5using System.Threading.Tasks;
 6using Jellyfin.Api.Attributes;
 7using Jellyfin.Api.Helpers;
 8using MediaBrowser.Common.Configuration;
 9using MediaBrowser.Controller.Configuration;
 10using MediaBrowser.Controller.MediaEncoding;
 11using MediaBrowser.Model.IO;
 12using MediaBrowser.Model.Net;
 13using Microsoft.AspNetCore.Authorization;
 14using Microsoft.AspNetCore.Http;
 15using Microsoft.AspNetCore.Mvc;
 16
 17namespace Jellyfin.Api.Controllers;
 18
 19/// <summary>
 20/// The hls segment controller.
 21/// </summary>
 22[Route("")]
 23[ApiExplorerSettings(IgnoreApi = true)]
 24public class HlsSegmentController : BaseJellyfinApiController
 25{
 26    private readonly IFileSystem _fileSystem;
 27    private readonly IServerConfigurationManager _serverConfigurationManager;
 28    private readonly ITranscodeManager _transcodeManager;
 29
 30    /// <summary>
 31    /// Initializes a new instance of the <see cref="HlsSegmentController"/> class.
 32    /// </summary>
 33    /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
 34    /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</p
 35    /// <param name="transcodeManager">Instance of the <see cref="ITranscodeManager"/> interface.</param>
 036    public HlsSegmentController(
 037        IFileSystem fileSystem,
 038        IServerConfigurationManager serverConfigurationManager,
 039        ITranscodeManager transcodeManager)
 40    {
 041        _fileSystem = fileSystem;
 042        _serverConfigurationManager = serverConfigurationManager;
 043        _transcodeManager = transcodeManager;
 044    }
 45
 46    /// <summary>
 47    /// Gets the specified audio segment for an audio item.
 48    /// </summary>
 49    /// <param name="itemId">The item id.</param>
 50    /// <param name="segmentId">The segment id.</param>
 51    /// <response code="200">Hls audio segment returned.</response>
 52    /// <returns>A <see cref="FileStreamResult"/> containing the audio stream.</returns>
 53    // Can't require authentication just yet due to seeing some requests come from Chrome without full query string
 54    // [Authenticated]
 55    [HttpGet("Audio/{itemId}/hls/{segmentId}/stream.mp3", Name = "GetHlsAudioSegmentLegacyMp3")]
 56    [HttpGet("Audio/{itemId}/hls/{segmentId}/stream.aac", Name = "GetHlsAudioSegmentLegacyAac")]
 57    [ProducesResponseType(StatusCodes.Status200OK)]
 58    [ProducesAudioFile]
 59    [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "itemId", Justification = "Re
 60    public ActionResult GetHlsAudioSegmentLegacy([FromRoute, Required] string itemId, [FromRoute, Required] string segme
 61    {
 62        // TODO: Deprecate with new iOS app
 063        var file = string.Concat(segmentId, Path.GetExtension(Request.Path.Value.AsSpan()));
 064        var transcodePath = _serverConfigurationManager.GetTranscodePath();
 065        file = Path.GetFullPath(Path.Combine(transcodePath, file));
 066        var fileDir = Path.GetDirectoryName(file);
 067        if (string.IsNullOrEmpty(fileDir) || !fileDir.StartsWith(transcodePath, StringComparison.InvariantCulture))
 68        {
 069            return BadRequest("Invalid segment.");
 70        }
 71
 072        return FileStreamResponseHelpers.GetStaticFileResult(file, MimeTypes.GetMimeType(file));
 73    }
 74
 75    /// <summary>
 76    /// Gets a hls video playlist.
 77    /// </summary>
 78    /// <param name="itemId">The video id.</param>
 79    /// <param name="playlistId">The playlist id.</param>
 80    /// <response code="200">Hls video playlist returned.</response>
 81    /// <returns>A <see cref="FileStreamResult"/> containing the playlist.</returns>
 82    [HttpGet("Videos/{itemId}/hls/{playlistId}/stream.m3u8")]
 83    [Authorize]
 84    [ProducesResponseType(StatusCodes.Status200OK)]
 85    [ProducesPlaylistFile]
 86    [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "itemId", Justification = "Re
 87    public ActionResult GetHlsPlaylistLegacy([FromRoute, Required] string itemId, [FromRoute, Required] string playlistI
 88    {
 089        var file = string.Concat(playlistId, Path.GetExtension(Request.Path.Value.AsSpan()));
 090        var transcodePath = _serverConfigurationManager.GetTranscodePath();
 091        file = Path.GetFullPath(Path.Combine(transcodePath, file));
 092        var fileDir = Path.GetDirectoryName(file);
 093        if (string.IsNullOrEmpty(fileDir) || !fileDir.StartsWith(transcodePath, StringComparison.InvariantCulture)
 094            || Path.GetExtension(file.AsSpan()).Equals(".m3u8", StringComparison.OrdinalIgnoreCase))
 95        {
 096            return BadRequest("Invalid segment.");
 97        }
 98
 099        return GetFileResult(file, file);
 100    }
 101
 102    /// <summary>
 103    /// Stops an active encoding.
 104    /// </summary>
 105    /// <param name="deviceId">The device id of the client requesting. Used to stop encoding processes when needed.</par
 106    /// <param name="playSessionId">The play session id.</param>
 107    /// <response code="204">Encoding stopped successfully.</response>
 108    /// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
 109    [HttpDelete("Videos/ActiveEncodings")]
 110    [Authorize]
 111    [ProducesResponseType(StatusCodes.Status204NoContent)]
 112    public ActionResult StopEncodingProcess(
 113        [FromQuery, Required] string deviceId,
 114        [FromQuery, Required] string playSessionId)
 115    {
 0116        _transcodeManager.KillTranscodingJobs(deviceId, playSessionId, _ => true);
 0117        return NoContent();
 118    }
 119
 120    /// <summary>
 121    /// Gets a hls video segment.
 122    /// </summary>
 123    /// <param name="itemId">The item id.</param>
 124    /// <param name="playlistId">The playlist id.</param>
 125    /// <param name="segmentId">The segment id.</param>
 126    /// <param name="segmentContainer">The segment container.</param>
 127    /// <response code="200">Hls video segment returned.</response>
 128    /// <response code="404">Hls segment not found.</response>
 129    /// <returns>A <see cref="FileStreamResult"/> containing the video segment.</returns>
 130    // Can't require authentication just yet due to seeing some requests come from Chrome without full query string
 131    // [Authenticated]
 132    [HttpGet("Videos/{itemId}/hls/{playlistId}/{segmentId}.{segmentContainer}")]
 133    [ProducesResponseType(StatusCodes.Status200OK)]
 134    [ProducesResponseType(StatusCodes.Status404NotFound)]
 135    [ProducesVideoFile]
 136    [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "itemId", Justification = "Re
 137    public ActionResult GetHlsVideoSegmentLegacy(
 138        [FromRoute, Required] string itemId,
 139        [FromRoute, Required] string playlistId,
 140        [FromRoute, Required] string segmentId,
 141        [FromRoute, Required] string segmentContainer)
 142    {
 0143        var file = string.Concat(segmentId, Path.GetExtension(Request.Path.Value.AsSpan()));
 0144        var transcodeFolderPath = _serverConfigurationManager.GetTranscodePath();
 145
 0146        file = Path.GetFullPath(Path.Combine(transcodeFolderPath, file));
 0147        var fileDir = Path.GetDirectoryName(file);
 0148        if (string.IsNullOrEmpty(fileDir) || !fileDir.StartsWith(transcodeFolderPath, StringComparison.InvariantCulture)
 149        {
 0150            return BadRequest("Invalid segment.");
 151        }
 152
 0153        var normalizedPlaylistId = playlistId;
 154
 0155        var filePaths = _fileSystem.GetFilePaths(transcodeFolderPath);
 156        // Add . to start of segment container for future use.
 0157        segmentContainer = segmentContainer.Insert(0, ".");
 0158        string? playlistPath = null;
 0159        foreach (var path in filePaths)
 160        {
 0161            var pathExtension = Path.GetExtension(path);
 0162            if ((string.Equals(pathExtension, segmentContainer, StringComparison.OrdinalIgnoreCase)
 0163                 || string.Equals(pathExtension, ".m3u8", StringComparison.OrdinalIgnoreCase))
 0164                && path.Contains(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase))
 165            {
 0166                playlistPath = path;
 0167                break;
 168            }
 169        }
 170
 0171        return playlistPath is null
 0172            ? NotFound("Hls segment not found.")
 0173            : GetFileResult(file, playlistPath);
 174    }
 175
 176    private ActionResult GetFileResult(string path, string playlistPath)
 177    {
 0178        var transcodingJob = _transcodeManager.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
 179
 0180        Response.OnCompleted(() =>
 0181        {
 0182            if (transcodingJob is not null)
 0183            {
 0184                _transcodeManager.OnTranscodeEndRequest(transcodingJob);
 0185            }
 0186
 0187            return Task.CompletedTask;
 0188        });
 189
 0190        return FileStreamResponseHelpers.GetStaticFileResult(path, MimeTypes.GetMimeType(path));
 191    }
 192}