2020-11-21 13:26:03 +00:00
|
|
|
using System;
|
2020-09-05 23:11:44 +00:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
2020-07-27 07:47:19 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
using System.IO;
|
|
|
|
using System.Threading.Tasks;
|
2020-09-01 23:26:49 +00:00
|
|
|
using Jellyfin.Api.Attributes;
|
2020-07-27 07:47:19 +00:00
|
|
|
using Jellyfin.Api.Constants;
|
|
|
|
using Jellyfin.Api.Helpers;
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
using MediaBrowser.Model.Net;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace Jellyfin.Api.Controllers
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The hls segment controller.
|
|
|
|
/// </summary>
|
2020-08-04 18:48:53 +00:00
|
|
|
[Route("")]
|
2020-07-27 07:47:19 +00:00
|
|
|
public class HlsSegmentController : BaseJellyfinApiController
|
|
|
|
{
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
private readonly IServerConfigurationManager _serverConfigurationManager;
|
|
|
|
private readonly TranscodingJobHelper _transcodingJobHelper;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="HlsSegmentController"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
|
|
|
|
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
|
|
|
|
/// <param name="transcodingJobHelper">Initialized instance of the <see cref="TranscodingJobHelper"/>.</param>
|
|
|
|
public HlsSegmentController(
|
|
|
|
IFileSystem fileSystem,
|
|
|
|
IServerConfigurationManager serverConfigurationManager,
|
|
|
|
TranscodingJobHelper transcodingJobHelper)
|
|
|
|
{
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
_serverConfigurationManager = serverConfigurationManager;
|
|
|
|
_transcodingJobHelper = transcodingJobHelper;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the specified audio segment for an audio item.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="itemId">The item id.</param>
|
|
|
|
/// <param name="segmentId">The segment id.</param>
|
|
|
|
/// <response code="200">Hls audio segment returned.</response>
|
|
|
|
/// <returns>A <see cref="FileStreamResult"/> containing the audio stream.</returns>
|
|
|
|
// Can't require authentication just yet due to seeing some requests come from Chrome without full query string
|
|
|
|
// [Authenticated]
|
2020-08-04 18:48:53 +00:00
|
|
|
[HttpGet("Audio/{itemId}/hls/{segmentId}/stream.mp3", Name = "GetHlsAudioSegmentLegacyMp3")]
|
|
|
|
[HttpGet("Audio/{itemId}/hls/{segmentId}/stream.aac", Name = "GetHlsAudioSegmentLegacyAac")]
|
2020-07-27 07:47:19 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-09-01 23:26:49 +00:00
|
|
|
[ProducesAudioFile]
|
2020-07-27 07:47:19 +00:00
|
|
|
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "itemId", Justification = "Required for ServiceStack")]
|
2020-09-06 15:07:27 +00:00
|
|
|
public ActionResult GetHlsAudioSegmentLegacy([FromRoute, Required] string itemId, [FromRoute, Required] string segmentId)
|
2020-07-27 07:47:19 +00:00
|
|
|
{
|
|
|
|
// TODO: Deprecate with new iOS app
|
|
|
|
var file = segmentId + Path.GetExtension(Request.Path);
|
2021-03-19 23:38:58 +00:00
|
|
|
var transcodePath = _serverConfigurationManager.GetTranscodePath();
|
|
|
|
file = Path.GetFullPath(Path.Combine(transcodePath, file));
|
|
|
|
var fileDir = Path.GetDirectoryName(file);
|
2021-11-08 19:39:02 +00:00
|
|
|
if (string.IsNullOrEmpty(fileDir) || !fileDir.StartsWith(transcodePath, StringComparison.InvariantCulture))
|
2021-03-19 23:38:58 +00:00
|
|
|
{
|
|
|
|
return BadRequest("Invalid segment.");
|
|
|
|
}
|
2020-07-27 07:47:19 +00:00
|
|
|
|
2022-01-09 01:53:53 +00:00
|
|
|
return FileStreamResponseHelpers.GetStaticFileResult(file, MimeTypes.GetMimeType(file));
|
2020-07-27 07:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a hls video playlist.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="itemId">The video id.</param>
|
|
|
|
/// <param name="playlistId">The playlist id.</param>
|
|
|
|
/// <response code="200">Hls video playlist returned.</response>
|
|
|
|
/// <returns>A <see cref="FileStreamResult"/> containing the playlist.</returns>
|
2020-08-04 18:48:53 +00:00
|
|
|
[HttpGet("Videos/{itemId}/hls/{playlistId}/stream.m3u8")]
|
2020-07-27 07:47:19 +00:00
|
|
|
[Authorize(Policy = Policies.DefaultAuthorization)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-09-01 23:26:49 +00:00
|
|
|
[ProducesPlaylistFile]
|
2020-07-27 07:47:19 +00:00
|
|
|
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "itemId", Justification = "Required for ServiceStack")]
|
2020-09-06 15:07:27 +00:00
|
|
|
public ActionResult GetHlsPlaylistLegacy([FromRoute, Required] string itemId, [FromRoute, Required] string playlistId)
|
2020-07-27 07:47:19 +00:00
|
|
|
{
|
|
|
|
var file = playlistId + Path.GetExtension(Request.Path);
|
2021-03-19 23:38:58 +00:00
|
|
|
var transcodePath = _serverConfigurationManager.GetTranscodePath();
|
|
|
|
file = Path.GetFullPath(Path.Combine(transcodePath, file));
|
|
|
|
var fileDir = Path.GetDirectoryName(file);
|
2021-11-08 19:39:02 +00:00
|
|
|
if (string.IsNullOrEmpty(fileDir) || !fileDir.StartsWith(transcodePath, StringComparison.InvariantCulture) || Path.GetExtension(file) != ".m3u8")
|
2021-03-19 23:38:58 +00:00
|
|
|
{
|
|
|
|
return BadRequest("Invalid segment.");
|
|
|
|
}
|
2020-07-27 07:47:19 +00:00
|
|
|
|
|
|
|
return GetFileResult(file, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Stops an active encoding.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="deviceId">The device id of the client requesting. Used to stop encoding processes when needed.</param>
|
|
|
|
/// <param name="playSessionId">The play session id.</param>
|
|
|
|
/// <response code="204">Encoding stopped successfully.</response>
|
|
|
|
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
|
2020-08-04 18:48:53 +00:00
|
|
|
[HttpDelete("Videos/ActiveEncodings")]
|
2020-07-27 07:47:19 +00:00
|
|
|
[Authorize(Policy = Policies.DefaultAuthorization)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2021-03-05 01:25:52 +00:00
|
|
|
public ActionResult StopEncodingProcess(
|
|
|
|
[FromQuery, Required] string deviceId,
|
|
|
|
[FromQuery, Required] string playSessionId)
|
2020-07-27 07:47:19 +00:00
|
|
|
{
|
|
|
|
_transcodingJobHelper.KillTranscodingJobs(deviceId, playSessionId, path => true);
|
|
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a hls video segment.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="itemId">The item id.</param>
|
|
|
|
/// <param name="playlistId">The playlist id.</param>
|
|
|
|
/// <param name="segmentId">The segment id.</param>
|
|
|
|
/// <param name="segmentContainer">The segment container.</param>
|
|
|
|
/// <response code="200">Hls video segment returned.</response>
|
2020-11-19 23:16:23 +00:00
|
|
|
/// <response code="404">Hls segment not found.</response>
|
2020-07-27 07:47:19 +00:00
|
|
|
/// <returns>A <see cref="FileStreamResult"/> containing the video segment.</returns>
|
|
|
|
// Can't require authentication just yet due to seeing some requests come from Chrome without full query string
|
|
|
|
// [Authenticated]
|
2020-08-04 18:48:53 +00:00
|
|
|
[HttpGet("Videos/{itemId}/hls/{playlistId}/{segmentId}.{segmentContainer}")]
|
2020-07-27 07:47:19 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-11-19 23:16:23 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
2020-09-01 23:26:49 +00:00
|
|
|
[ProducesVideoFile]
|
2020-07-27 07:47:19 +00:00
|
|
|
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "itemId", Justification = "Required for ServiceStack")]
|
|
|
|
public ActionResult GetHlsVideoSegmentLegacy(
|
2020-09-06 15:07:27 +00:00
|
|
|
[FromRoute, Required] string itemId,
|
|
|
|
[FromRoute, Required] string playlistId,
|
|
|
|
[FromRoute, Required] string segmentId,
|
|
|
|
[FromRoute, Required] string segmentContainer)
|
2020-07-27 07:47:19 +00:00
|
|
|
{
|
|
|
|
var file = segmentId + Path.GetExtension(Request.Path);
|
|
|
|
var transcodeFolderPath = _serverConfigurationManager.GetTranscodePath();
|
|
|
|
|
2021-03-19 23:38:58 +00:00
|
|
|
file = Path.GetFullPath(Path.Combine(transcodeFolderPath, file));
|
|
|
|
var fileDir = Path.GetDirectoryName(file);
|
2021-11-08 19:39:02 +00:00
|
|
|
if (string.IsNullOrEmpty(fileDir) || !fileDir.StartsWith(transcodeFolderPath, StringComparison.InvariantCulture))
|
2021-03-19 23:38:58 +00:00
|
|
|
{
|
|
|
|
return BadRequest("Invalid segment.");
|
|
|
|
}
|
2020-07-27 07:47:19 +00:00
|
|
|
|
|
|
|
var normalizedPlaylistId = playlistId;
|
|
|
|
|
2020-11-19 23:15:20 +00:00
|
|
|
var filePaths = _fileSystem.GetFilePaths(transcodeFolderPath);
|
|
|
|
// Add . to start of segment container for future use.
|
|
|
|
segmentContainer = segmentContainer.Insert(0, ".");
|
|
|
|
string? playlistPath = null;
|
|
|
|
foreach (var path in filePaths)
|
|
|
|
{
|
|
|
|
var pathExtension = Path.GetExtension(path);
|
|
|
|
if ((string.Equals(pathExtension, segmentContainer, StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| string.Equals(pathExtension, ".m3u8", StringComparison.OrdinalIgnoreCase))
|
|
|
|
&& path.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1)
|
|
|
|
{
|
|
|
|
playlistPath = path;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-11-13 16:04:31 +00:00
|
|
|
|
2020-11-19 23:15:20 +00:00
|
|
|
return playlistPath == null
|
|
|
|
? NotFound("Hls segment not found.")
|
|
|
|
: GetFileResult(file, playlistPath);
|
2020-07-27 07:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private ActionResult GetFileResult(string path, string playlistPath)
|
|
|
|
{
|
|
|
|
var transcodingJob = _transcodingJobHelper.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
|
|
|
|
|
|
|
|
Response.OnCompleted(() =>
|
|
|
|
{
|
|
|
|
if (transcodingJob != null)
|
|
|
|
{
|
|
|
|
_transcodingJobHelper.OnTranscodeEndRequest(transcodingJob);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
});
|
|
|
|
|
2022-01-09 01:53:53 +00:00
|
|
|
return FileStreamResponseHelpers.GetStaticFileResult(path, MimeTypes.GetMimeType(path));
|
2020-07-27 07:47:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|