diff options
Diffstat (limited to 'src/Jellyfin.MediaEncoding.Hls')
5 files changed, 342 insertions, 0 deletions
diff --git a/src/Jellyfin.MediaEncoding.Hls/Extensions/MediaEncodingHlsServiceCollectionExtensions.cs b/src/Jellyfin.MediaEncoding.Hls/Extensions/MediaEncodingHlsServiceCollectionExtensions.cs new file mode 100644 index 000000000..327898366 --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Hls/Extensions/MediaEncodingHlsServiceCollectionExtensions.cs @@ -0,0 +1,21 @@ +using Jellyfin.MediaEncoding.Hls.Playlist; +using Microsoft.Extensions.DependencyInjection; + +namespace Jellyfin.MediaEncoding.Hls.Extensions +{ + /// <summary> + /// Extensions for the <see cref="IServiceCollection"/> interface. + /// </summary> + public static class MediaEncodingHlsServiceCollectionExtensions + { + /// <summary> + /// Adds the hls playlist generators to the <see cref="IServiceCollection"/>. + /// </summary> + /// <param name="serviceCollection">An instance of the <see cref="IServiceCollection"/> interface.</param> + /// <returns>The updated service collection.</returns> + public static IServiceCollection AddHlsPlaylistGenerator(this IServiceCollection serviceCollection) + { + return serviceCollection.AddSingleton<IDynamicHlsPlaylistGenerator, DynamicHlsPlaylistGenerator>(); + } + } +} diff --git a/src/Jellyfin.MediaEncoding.Hls/Jellyfin.MediaEncoding.Hls.csproj b/src/Jellyfin.MediaEncoding.Hls/Jellyfin.MediaEncoding.Hls.csproj new file mode 100644 index 000000000..89cd1378b --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Hls/Jellyfin.MediaEncoding.Hls.csproj @@ -0,0 +1,22 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>net5.0</TargetFramework> + </PropertyGroup> + + <!-- Code Analyzers--> + <ItemGroup Condition=" '$(Configuration)' == 'Debug' "> + <PackageReference Include="SerilogAnalyzer" Version="0.15.0" PrivateAssets="All" /> + <PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" /> + <PackageReference Include="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31" PrivateAssets="All" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\Jellyfin.MediaEncoding.Keyframes\Jellyfin.MediaEncoding.Keyframes.csproj" /> + </ItemGroup> + <ItemGroup> + <Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60"> + <HintPath>..\..\..\..\..\..\Program Files\dotnet\packs\Microsoft.AspNetCore.App.Ref\5.0.0\ref\net5.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll</HintPath> + </Reference> + </ItemGroup> + +</Project> diff --git a/src/Jellyfin.MediaEncoding.Hls/Playlist/CreateMainPlaylistRequest.cs b/src/Jellyfin.MediaEncoding.Hls/Playlist/CreateMainPlaylistRequest.cs new file mode 100644 index 000000000..d6db1ca6e --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Hls/Playlist/CreateMainPlaylistRequest.cs @@ -0,0 +1,57 @@ +namespace Jellyfin.MediaEncoding.Hls.Playlist +{ + /// <summary> + /// Request class for the <see cref="IDynamicHlsPlaylistGenerator.CreateMainPlaylist(CreateMainPlaylistRequest)"/> method. + /// </summary> + public class CreateMainPlaylistRequest + { + /// <summary> + /// Initializes a new instance of the <see cref="CreateMainPlaylistRequest"/> class. + /// </summary> + /// <param name="filePath">The absolute file path to the file.</param> + /// <param name="desiredSegmentLengthMs">The desired segment length in milliseconds.</param> + /// <param name="totalRuntimeTicks">The total duration of the file in ticks.</param> + /// <param name="segmentContainer">The desired segment container eg. "ts".</param> + /// <param name="endpointPrefix">The URI prefix for the relative URL in the playlist.</param> + /// <param name="queryString">The desired query string to append (must start with ?).</param> + public CreateMainPlaylistRequest(string filePath, int desiredSegmentLengthMs, long totalRuntimeTicks, string segmentContainer, string endpointPrefix, string queryString) + { + FilePath = filePath; + DesiredSegmentLengthMs = desiredSegmentLengthMs; + TotalRuntimeTicks = totalRuntimeTicks; + SegmentContainer = segmentContainer; + EndpointPrefix = endpointPrefix; + QueryString = queryString; + } + + /// <summary> + /// Gets the file path. + /// </summary> + public string FilePath { get; } + + /// <summary> + /// Gets the desired segment length in milliseconds. + /// </summary> + public int DesiredSegmentLengthMs { get; } + + /// <summary> + /// Gets the total runtime in ticks. + /// </summary> + public long TotalRuntimeTicks { get; } + + /// <summary> + /// Gets the segment container. + /// </summary> + public string SegmentContainer { get; } + + /// <summary> + /// Gets the endpoint prefix for the URL. + /// </summary> + public string EndpointPrefix { get; } + + /// <summary> + /// Gets the query string. + /// </summary> + public string QueryString { get; } + } +} diff --git a/src/Jellyfin.MediaEncoding.Hls/Playlist/DynamicHlsPlaylistGenerator.cs b/src/Jellyfin.MediaEncoding.Hls/Playlist/DynamicHlsPlaylistGenerator.cs new file mode 100644 index 000000000..8c16545df --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Hls/Playlist/DynamicHlsPlaylistGenerator.cs @@ -0,0 +1,227 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.Json; +using Jellyfin.Extensions.Json; +using Jellyfin.MediaEncoding.Keyframes; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Extensions; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.MediaEncoding; +using Microsoft.Extensions.Logging; + +namespace Jellyfin.MediaEncoding.Hls.Playlist +{ + /// <inheritdoc /> + public class DynamicHlsPlaylistGenerator : IDynamicHlsPlaylistGenerator + { + private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; + private readonly IServerConfigurationManager _serverConfigurationManager; + private readonly IMediaEncoder _mediaEncoder; + private readonly IApplicationPaths _applicationPaths; + private readonly KeyframeExtractor _keyframeExtractor; + private const string DefaultContainerExtension = ".ts"; + + /// <summary> + /// Initializes a new instance of the <see cref="DynamicHlsPlaylistGenerator"/> class. + /// </summary> + /// <param name="serverConfigurationManager">An instance of the see <see cref="IServerConfigurationManager"/> interface.</param> + /// <param name="mediaEncoder">An instance of the see <see cref="IMediaEncoder"/> interface.</param> + /// <param name="applicationPaths">An instance of the <see cref="IApplicationPaths"/> interface.</param> + /// <param name="loggerFactory">An instance of the see <see cref="ILoggerFactory"/> interface.</param> + public DynamicHlsPlaylistGenerator(IServerConfigurationManager serverConfigurationManager, IMediaEncoder mediaEncoder, IApplicationPaths applicationPaths, ILoggerFactory loggerFactory) + { + _serverConfigurationManager = serverConfigurationManager; + _mediaEncoder = mediaEncoder; + _applicationPaths = applicationPaths; + _keyframeExtractor = new KeyframeExtractor(loggerFactory.CreateLogger<KeyframeExtractor>()); + } + + private string KeyframeCachePath => Path.Combine(_applicationPaths.DataPath, "keyframes"); + + /// <inheritdoc /> + public string CreateMainPlaylist(CreateMainPlaylistRequest request) + { + IReadOnlyList<double> segments; + if (IsExtractionAllowed(request.FilePath)) + { + segments = ComputeSegments(request.FilePath, request.DesiredSegmentLengthMs); + } + else + { + segments = ComputeEqualLengthSegments(request.DesiredSegmentLengthMs, request.TotalRuntimeTicks); + } + + var segmentExtension = GetSegmentFileExtension(request.SegmentContainer); + + // http://ffmpeg.org/ffmpeg-all.html#toc-hls-2 + var isHlsInFmp4 = string.Equals(segmentExtension, "mp4", StringComparison.OrdinalIgnoreCase); + var hlsVersion = isHlsInFmp4 ? "7" : "3"; + + var builder = new StringBuilder(128); + + builder.AppendLine("#EXTM3U") + .AppendLine("#EXT-X-PLAYLIST-TYPE:VOD") + .Append("#EXT-X-VERSION:") + .Append(hlsVersion) + .AppendLine() + .Append("#EXT-X-TARGETDURATION:") + .Append(Math.Ceiling(segments.Count > 0 ? segments.Max() : request.DesiredSegmentLengthMs)) + .AppendLine() + .AppendLine("#EXT-X-MEDIA-SEQUENCE:0"); + + var index = 0; + + if (isHlsInFmp4) + { + builder.Append("#EXT-X-MAP:URI=\"") + .Append(request.EndpointPrefix) + .Append("-1") + .Append(segmentExtension) + .Append(request.QueryString) + .Append('"') + .AppendLine(); + } + + double currentRuntimeInSeconds = 0; + foreach (var length in segments) + { + builder.Append("#EXTINF:") + .Append(length.ToString("0.0000", CultureInfo.InvariantCulture)) + .AppendLine(", nodesc") + .Append(request.EndpointPrefix) + .Append(index++) + .Append(segmentExtension) + .Append(request.QueryString) + .Append("&runtimeTicks=") + .Append(TimeSpan.FromSeconds(currentRuntimeInSeconds).Ticks) + .Append("&actualSegmentLengthTicks=") + .Append(TimeSpan.FromSeconds(length).Ticks) + .AppendLine(); + + currentRuntimeInSeconds += length; + } + + builder.AppendLine("#EXT-X-ENDLIST"); + + return builder.ToString(); + } + + private IReadOnlyList<double> ComputeSegments(string filePath, int desiredSegmentLengthMs) + { + KeyframeData keyframeData; + var cachePath = GetCachePath(filePath); + if (TryReadFromCache(cachePath, out var cachedResult)) + { + keyframeData = cachedResult; + } + else + { + keyframeData = _keyframeExtractor.GetKeyframeData(filePath, _mediaEncoder.ProbePath, string.Empty); + CacheResult(cachePath, keyframeData); + } + + long lastKeyframe = 0; + var result = new List<double>(); + // Scale the segment length to ticks to match the keyframes + var desiredSegmentLengthTicks = TimeSpan.FromMilliseconds(desiredSegmentLengthMs).Ticks; + var desiredCutTime = desiredSegmentLengthTicks; + for (var j = 0; j < keyframeData.KeyframeTicks.Count; j++) + { + var keyframe = keyframeData.KeyframeTicks[j]; + if (keyframe >= desiredCutTime) + { + var currentSegmentLength = keyframe - lastKeyframe; + result.Add(TimeSpan.FromTicks(currentSegmentLength).TotalSeconds); + lastKeyframe = keyframe; + desiredCutTime += desiredSegmentLengthTicks; + } + } + + result.Add(TimeSpan.FromTicks(keyframeData.TotalDuration - lastKeyframe).TotalSeconds); + return result; + } + + private void CacheResult(string cachePath, KeyframeData keyframeData) + { + var json = JsonSerializer.Serialize(keyframeData, _jsonOptions); + Directory.CreateDirectory(Path.GetDirectoryName(cachePath) ?? throw new ArgumentException($"Provided path ({cachePath}) is not valid.", nameof(cachePath))); + File.WriteAllText(cachePath, json); + } + + private string GetCachePath(string filePath) + { + var lastWriteTimeUtc = File.GetLastWriteTimeUtc(filePath); + ReadOnlySpan<char> filename = (filePath + "_" + lastWriteTimeUtc.Ticks.ToString(CultureInfo.InvariantCulture)).GetMD5() + ".json"; + var prefix = filename.Slice(0, 1); + + return Path.Join(KeyframeCachePath, prefix, filename); + } + + private bool TryReadFromCache(string cachePath, [NotNullWhen(true)] out KeyframeData? cachedResult) + { + if (File.Exists(cachePath)) + { + var bytes = File.ReadAllBytes(cachePath); + cachedResult = JsonSerializer.Deserialize<KeyframeData>(bytes, _jsonOptions); + return cachedResult != null; + } + + cachedResult = null; + return false; + } + + private bool IsExtractionAllowed(ReadOnlySpan<char> filePath) + { + // Remove the leading dot + var extension = Path.GetExtension(filePath)[1..]; + var allowedExtensions = _serverConfigurationManager.GetEncodingOptions().AllowAutomaticKeyframeExtractionForExtensions; + for (var i = 0; i < allowedExtensions.Length; i++) + { + var allowedExtension = allowedExtensions[i]; + if (extension.Equals(allowedExtension, StringComparison.OrdinalIgnoreCase)) + { + return true; + } + } + + return false; + } + + private static double[] ComputeEqualLengthSegments(long desiredSegmentLengthMs, long totalRuntimeTicks) + { + var segmentLengthTicks = TimeSpan.FromMilliseconds(desiredSegmentLengthMs).Ticks; + var wholeSegments = totalRuntimeTicks / segmentLengthTicks; + var remainingTicks = totalRuntimeTicks % segmentLengthTicks; + + var segmentsLen = wholeSegments + (remainingTicks == 0 ? 0 : 1); + var segments = new double[segmentsLen]; + for (int i = 0; i < wholeSegments; i++) + { + segments[i] = desiredSegmentLengthMs; + } + + if (remainingTicks != 0) + { + segments[^1] = TimeSpan.FromTicks(remainingTicks).TotalSeconds; + } + + return segments; + } + + // TODO copied from DynamicHlsController + private static string GetSegmentFileExtension(string segmentContainer) + { + if (!string.IsNullOrWhiteSpace(segmentContainer)) + { + return "." + segmentContainer; + } + + return DefaultContainerExtension; + } + } +} diff --git a/src/Jellyfin.MediaEncoding.Hls/Playlist/IDynamicHlsPlaylistGenerator.cs b/src/Jellyfin.MediaEncoding.Hls/Playlist/IDynamicHlsPlaylistGenerator.cs new file mode 100644 index 000000000..534f15a90 --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Hls/Playlist/IDynamicHlsPlaylistGenerator.cs @@ -0,0 +1,15 @@ +namespace Jellyfin.MediaEncoding.Hls.Playlist +{ + /// <summary> + /// Generator for dynamic HLS playlists where the segment lengths aren't known in advance. + /// </summary> + public interface IDynamicHlsPlaylistGenerator + { + /// <summary> + /// Creates the main playlist containing the main video or audio stream. + /// </summary> + /// <param name="request">An instance of the <see cref="CreateMainPlaylistRequest"/> class.</param> + /// <returns></returns> + string CreateMainPlaylist(CreateMainPlaylistRequest request); + } +} |
