| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Diagnostics.CodeAnalysis; |
| | 4 | | using System.Globalization; |
| | 5 | | using System.IO; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Text; |
| | 8 | | using Jellyfin.MediaEncoding.Hls.Extractors; |
| | 9 | | using Jellyfin.MediaEncoding.Keyframes; |
| | 10 | | using MediaBrowser.Common.Configuration; |
| | 11 | | using MediaBrowser.Controller.Configuration; |
| | 12 | | using MediaBrowser.Controller.MediaEncoding; |
| | 13 | |
|
| | 14 | | namespace Jellyfin.MediaEncoding.Hls.Playlist; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class DynamicHlsPlaylistGenerator : IDynamicHlsPlaylistGenerator |
| | 18 | | { |
| | 19 | | private readonly IServerConfigurationManager _serverConfigurationManager; |
| | 20 | | private readonly IKeyframeExtractor[] _extractors; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Initializes a new instance of the <see cref="DynamicHlsPlaylistGenerator"/> class. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="serverConfigurationManager">An instance of the see <see cref="IServerConfigurationManager"/> interf |
| | 26 | | /// <param name="extractors">An instance of <see cref="IEnumerable{IKeyframeExtractor}"/>.</param> |
| | 27 | | public DynamicHlsPlaylistGenerator(IServerConfigurationManager serverConfigurationManager, IEnumerable<IKeyframeExtr |
| 0 | 28 | | { |
| 0 | 29 | | _serverConfigurationManager = serverConfigurationManager; |
| 0 | 30 | | _extractors = extractors.Where(e => e.IsMetadataBased).ToArray(); |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <inheritdoc /> |
| | 34 | | public string CreateMainPlaylist(CreateMainPlaylistRequest request) |
| 0 | 35 | | { |
| | 36 | | IReadOnlyList<double> segments; |
| | 37 | | // For video transcodes it is sufficient with equal length segments as ffmpeg will create new keyframes |
| 0 | 38 | | if (request.IsRemuxingVideo |
| 0 | 39 | | && request.MediaSourceId is not null |
| 0 | 40 | | && TryExtractKeyframes(request.MediaSourceId.Value, request.FilePath, out var keyframeData)) |
| 0 | 41 | | { |
| 0 | 42 | | segments = ComputeSegments(keyframeData, request.DesiredSegmentLengthMs); |
| 0 | 43 | | } |
| | 44 | | else |
| 0 | 45 | | { |
| 0 | 46 | | segments = ComputeEqualLengthSegments(request.DesiredSegmentLengthMs, request.TotalRuntimeTicks); |
| 0 | 47 | | } |
| | 48 | |
|
| 0 | 49 | | var segmentExtension = EncodingHelper.GetSegmentFileExtension(request.SegmentContainer); |
| | 50 | |
|
| | 51 | | // http://ffmpeg.org/ffmpeg-all.html#toc-hls-2 |
| 0 | 52 | | var isHlsInFmp4 = string.Equals(segmentExtension, ".mp4", StringComparison.OrdinalIgnoreCase); |
| 0 | 53 | | var hlsVersion = isHlsInFmp4 ? "7" : "3"; |
| | 54 | |
|
| 0 | 55 | | var builder = new StringBuilder(128); |
| | 56 | |
|
| 0 | 57 | | builder.AppendLine("#EXTM3U") |
| 0 | 58 | | .AppendLine("#EXT-X-PLAYLIST-TYPE:VOD") |
| 0 | 59 | | .Append("#EXT-X-VERSION:") |
| 0 | 60 | | .Append(hlsVersion) |
| 0 | 61 | | .AppendLine() |
| 0 | 62 | | .Append("#EXT-X-TARGETDURATION:") |
| 0 | 63 | | .Append(Math.Ceiling(segments.Count > 0 ? segments.Max() : request.DesiredSegmentLengthMs)) |
| 0 | 64 | | .AppendLine() |
| 0 | 65 | | .AppendLine("#EXT-X-MEDIA-SEQUENCE:0"); |
| | 66 | |
|
| 0 | 67 | | var index = 0; |
| | 68 | |
|
| 0 | 69 | | if (isHlsInFmp4) |
| 0 | 70 | | { |
| | 71 | | // Init file that only includes fMP4 headers |
| 0 | 72 | | builder.Append("#EXT-X-MAP:URI=\"") |
| 0 | 73 | | .Append(request.EndpointPrefix) |
| 0 | 74 | | .Append("-1") |
| 0 | 75 | | .Append(segmentExtension) |
| 0 | 76 | | .Append(request.QueryString) |
| 0 | 77 | | .Append("&runtimeTicks=0") |
| 0 | 78 | | .Append("&actualSegmentLengthTicks=0") |
| 0 | 79 | | .Append('"') |
| 0 | 80 | | .AppendLine(); |
| 0 | 81 | | } |
| | 82 | |
|
| 0 | 83 | | long currentRuntimeInSeconds = 0; |
| 0 | 84 | | foreach (var length in segments) |
| 0 | 85 | | { |
| | 86 | | // Manually convert to ticks to avoid precision loss when converting double |
| 0 | 87 | | var lengthTicks = Convert.ToInt64(length * TimeSpan.TicksPerSecond); |
| 0 | 88 | | builder.Append("#EXTINF:") |
| 0 | 89 | | .Append(length.ToString("0.000000", CultureInfo.InvariantCulture)) |
| 0 | 90 | | .AppendLine(", nodesc") |
| 0 | 91 | | .Append(request.EndpointPrefix) |
| 0 | 92 | | .Append(index++) |
| 0 | 93 | | .Append(segmentExtension) |
| 0 | 94 | | .Append(request.QueryString) |
| 0 | 95 | | .Append("&runtimeTicks=") |
| 0 | 96 | | .Append(currentRuntimeInSeconds) |
| 0 | 97 | | .Append("&actualSegmentLengthTicks=") |
| 0 | 98 | | .Append(lengthTicks) |
| 0 | 99 | | .AppendLine(); |
| | 100 | |
|
| 0 | 101 | | currentRuntimeInSeconds += lengthTicks; |
| 0 | 102 | | } |
| | 103 | |
|
| 0 | 104 | | builder.AppendLine("#EXT-X-ENDLIST"); |
| | 105 | |
|
| 0 | 106 | | return builder.ToString(); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | private bool TryExtractKeyframes(Guid itemId, string filePath, [NotNullWhen(true)] out KeyframeData? keyframeData) |
| 0 | 110 | | { |
| 0 | 111 | | keyframeData = null; |
| 0 | 112 | | if (!IsExtractionAllowedForFile(filePath, _serverConfigurationManager.GetEncodingOptions().AllowOnDemandMetadata |
| 0 | 113 | | { |
| 0 | 114 | | return false; |
| | 115 | | } |
| | 116 | |
|
| 0 | 117 | | var len = _extractors.Length; |
| 0 | 118 | | for (var i = 0; i < len; i++) |
| 0 | 119 | | { |
| 0 | 120 | | var extractor = _extractors[i]; |
| 0 | 121 | | if (!extractor.TryExtractKeyframes(itemId, filePath, out var result)) |
| 0 | 122 | | { |
| 0 | 123 | | continue; |
| | 124 | | } |
| | 125 | |
|
| 0 | 126 | | keyframeData = result; |
| 0 | 127 | | return true; |
| | 128 | | } |
| | 129 | |
|
| 0 | 130 | | return false; |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | internal static bool IsExtractionAllowedForFile(ReadOnlySpan<char> filePath, IReadOnlyList<string> allowedExtensions |
| 5 | 134 | | { |
| 5 | 135 | | var extension = Path.GetExtension(filePath); |
| 5 | 136 | | if (extension.IsEmpty) |
| 1 | 137 | | { |
| 1 | 138 | | return false; |
| | 139 | | } |
| | 140 | |
|
| | 141 | | // Remove the leading dot |
| 4 | 142 | | var extensionWithoutDot = extension[1..]; |
| 20 | 143 | | for (var i = 0; i < allowedExtensions.Count; i++) |
| 8 | 144 | | { |
| 8 | 145 | | var allowedExtension = allowedExtensions[i].AsSpan().TrimStart('.'); |
| 8 | 146 | | if (extensionWithoutDot.Equals(allowedExtension, StringComparison.OrdinalIgnoreCase)) |
| 2 | 147 | | { |
| 2 | 148 | | return true; |
| | 149 | | } |
| 6 | 150 | | } |
| | 151 | |
|
| 2 | 152 | | return false; |
| 5 | 153 | | } |
| | 154 | |
|
| | 155 | | internal static IReadOnlyList<double> ComputeSegments(KeyframeData keyframeData, int desiredSegmentLengthMs) |
| 5 | 156 | | { |
| 5 | 157 | | if (keyframeData.KeyframeTicks.Count > 0 && keyframeData.TotalDuration < keyframeData.KeyframeTicks[^1]) |
| 1 | 158 | | { |
| 1 | 159 | | throw new ArgumentException("Invalid duration in keyframe data", nameof(keyframeData)); |
| | 160 | | } |
| | 161 | |
|
| 4 | 162 | | long lastKeyframe = 0; |
| 4 | 163 | | var result = new List<double>(); |
| | 164 | | // Scale the segment length to ticks to match the keyframes |
| 4 | 165 | | var desiredSegmentLengthTicks = TimeSpan.FromMilliseconds(desiredSegmentLengthMs).Ticks; |
| 4 | 166 | | var desiredCutTime = desiredSegmentLengthTicks; |
| 30 | 167 | | for (var j = 0; j < keyframeData.KeyframeTicks.Count; j++) |
| 11 | 168 | | { |
| 11 | 169 | | var keyframe = keyframeData.KeyframeTicks[j]; |
| 11 | 170 | | if (keyframe >= desiredCutTime) |
| 5 | 171 | | { |
| 5 | 172 | | var currentSegmentLength = keyframe - lastKeyframe; |
| 5 | 173 | | result.Add(TimeSpan.FromTicks(currentSegmentLength).TotalSeconds); |
| 5 | 174 | | lastKeyframe = keyframe; |
| 5 | 175 | | desiredCutTime += desiredSegmentLengthTicks; |
| 5 | 176 | | } |
| 11 | 177 | | } |
| | 178 | |
|
| 4 | 179 | | result.Add(TimeSpan.FromTicks(keyframeData.TotalDuration - lastKeyframe).TotalSeconds); |
| 4 | 180 | | return result; |
| 4 | 181 | | } |
| | 182 | |
|
| | 183 | | internal static double[] ComputeEqualLengthSegments(int desiredSegmentLengthMs, long totalRuntimeTicks) |
| 7 | 184 | | { |
| 7 | 185 | | if (desiredSegmentLengthMs == 0 || totalRuntimeTicks == 0) |
| 2 | 186 | | { |
| 2 | 187 | | throw new InvalidOperationException($"Invalid segment length ({desiredSegmentLengthMs}) or runtime ticks ({t |
| | 188 | | } |
| | 189 | |
|
| 5 | 190 | | var desiredSegmentLength = TimeSpan.FromMilliseconds(desiredSegmentLengthMs); |
| | 191 | |
|
| 5 | 192 | | var segmentLengthTicks = desiredSegmentLength.Ticks; |
| 5 | 193 | | var wholeSegments = totalRuntimeTicks / segmentLengthTicks; |
| 5 | 194 | | var remainingTicks = totalRuntimeTicks % segmentLengthTicks; |
| | 195 | |
|
| 5 | 196 | | var segmentsLen = wholeSegments + (remainingTicks == 0 ? 0 : 1); |
| 5 | 197 | | var segments = new double[segmentsLen]; |
| 38 | 198 | | for (int i = 0; i < wholeSegments; i++) |
| 14 | 199 | | { |
| 14 | 200 | | segments[i] = desiredSegmentLength.TotalSeconds; |
| 14 | 201 | | } |
| | 202 | |
|
| 5 | 203 | | if (remainingTicks != 0) |
| 4 | 204 | | { |
| 4 | 205 | | segments[^1] = TimeSpan.FromTicks(remainingTicks).TotalSeconds; |
| 4 | 206 | | } |
| | 207 | |
|
| 5 | 208 | | return segments; |
| 5 | 209 | | } |
| | 210 | | } |