< 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: 191
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

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("")]
 23public 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>
 035    public HlsSegmentController(
 036        IFileSystem fileSystem,
 037        IServerConfigurationManager serverConfigurationManager,
 038        ITranscodeManager transcodeManager)
 39    {
 040        _fileSystem = fileSystem;
 041        _serverConfigurationManager = serverConfigurationManager;
 042        _transcodeManager = transcodeManager;
 043    }
 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
 062        var file = string.Concat(segmentId, Path.GetExtension(Request.Path.Value.AsSpan()));
 063        var transcodePath = _serverConfigurationManager.GetTranscodePath();
 064        file = Path.GetFullPath(Path.Combine(transcodePath, file));
 065        var fileDir = Path.GetDirectoryName(file);
 066        if (string.IsNullOrEmpty(fileDir) || !fileDir.StartsWith(transcodePath, StringComparison.InvariantCulture))
 67        {
 068            return BadRequest("Invalid segment.");
 69        }
 70
 071        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    {
 088        var file = string.Concat(playlistId, Path.GetExtension(Request.Path.Value.AsSpan()));
 089        var transcodePath = _serverConfigurationManager.GetTranscodePath();
 090        file = Path.GetFullPath(Path.Combine(transcodePath, file));
 091        var fileDir = Path.GetDirectoryName(file);
 092        if (string.IsNullOrEmpty(fileDir) || !fileDir.StartsWith(transcodePath, StringComparison.InvariantCulture)
 093            || Path.GetExtension(file.AsSpan()).Equals(".m3u8", StringComparison.OrdinalIgnoreCase))
 94        {
 095            return BadRequest("Invalid segment.");
 96        }
 97
 098        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    {
 0115        _transcodeManager.KillTranscodingJobs(deviceId, playSessionId, _ => true);
 0116        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    {
 0142        var file = string.Concat(segmentId, Path.GetExtension(Request.Path.Value.AsSpan()));
 0143        var transcodeFolderPath = _serverConfigurationManager.GetTranscodePath();
 144
 0145        file = Path.GetFullPath(Path.Combine(transcodeFolderPath, file));
 0146        var fileDir = Path.GetDirectoryName(file);
 0147        if (string.IsNullOrEmpty(fileDir) || !fileDir.StartsWith(transcodeFolderPath, StringComparison.InvariantCulture)
 148        {
 0149            return BadRequest("Invalid segment.");
 150        }
 151
 0152        var normalizedPlaylistId = playlistId;
 153
 0154        var filePaths = _fileSystem.GetFilePaths(transcodeFolderPath);
 155        // Add . to start of segment container for future use.
 0156        segmentContainer = segmentContainer.Insert(0, ".");
 0157        string? playlistPath = null;
 0158        foreach (var path in filePaths)
 159        {
 0160            var pathExtension = Path.GetExtension(path);
 0161            if ((string.Equals(pathExtension, segmentContainer, StringComparison.OrdinalIgnoreCase)
 0162                 || string.Equals(pathExtension, ".m3u8", StringComparison.OrdinalIgnoreCase))
 0163                && path.Contains(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase))
 164            {
 0165                playlistPath = path;
 0166                break;
 167            }
 168        }
 169
 0170        return playlistPath is null
 0171            ? NotFound("Hls segment not found.")
 0172            : GetFileResult(file, playlistPath);
 173    }
 174
 175    private ActionResult GetFileResult(string path, string playlistPath)
 176    {
 0177        var transcodingJob = _transcodeManager.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
 178
 0179        Response.OnCompleted(() =>
 0180        {
 0181            if (transcodingJob is not null)
 0182            {
 0183                _transcodeManager.OnTranscodeEndRequest(transcodingJob);
 0184            }
 0185
 0186            return Task.CompletedTask;
 0187        });
 188
 0189        return FileStreamResponseHelpers.GetStaticFileResult(path, MimeTypes.GetMimeType(path));
 190    }
 191}