| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Net; |
| | | 6 | | using System.Security.Claims; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using Jellyfin.Api.Extensions; |
| | | 11 | | using Jellyfin.Data.Enums; |
| | | 12 | | using Jellyfin.Database.Implementations.Entities; |
| | | 13 | | using Jellyfin.Extensions; |
| | | 14 | | using MediaBrowser.Common.Configuration; |
| | | 15 | | using MediaBrowser.Common.Extensions; |
| | | 16 | | using MediaBrowser.Common.Net; |
| | | 17 | | using MediaBrowser.Controller.Configuration; |
| | | 18 | | using MediaBrowser.Controller.Library; |
| | | 19 | | using MediaBrowser.Controller.MediaEncoding; |
| | | 20 | | using MediaBrowser.Controller.Streaming; |
| | | 21 | | using MediaBrowser.Controller.Trickplay; |
| | | 22 | | using MediaBrowser.Model.Dlna; |
| | | 23 | | using MediaBrowser.Model.Entities; |
| | | 24 | | using MediaBrowser.Model.Net; |
| | | 25 | | using Microsoft.AspNetCore.Http; |
| | | 26 | | using Microsoft.AspNetCore.Mvc; |
| | | 27 | | using Microsoft.Extensions.Logging; |
| | | 28 | | using Microsoft.Net.Http.Headers; |
| | | 29 | | |
| | | 30 | | namespace Jellyfin.Api.Helpers; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Dynamic hls helper. |
| | | 34 | | /// </summary> |
| | | 35 | | public class DynamicHlsHelper |
| | | 36 | | { |
| | | 37 | | private readonly ILibraryManager _libraryManager; |
| | | 38 | | private readonly IUserManager _userManager; |
| | | 39 | | private readonly IMediaSourceManager _mediaSourceManager; |
| | | 40 | | private readonly IServerConfigurationManager _serverConfigurationManager; |
| | | 41 | | private readonly IMediaEncoder _mediaEncoder; |
| | | 42 | | private readonly ITranscodeManager _transcodeManager; |
| | | 43 | | private readonly INetworkManager _networkManager; |
| | | 44 | | private readonly ILogger<DynamicHlsHelper> _logger; |
| | | 45 | | private readonly IHttpContextAccessor _httpContextAccessor; |
| | | 46 | | private readonly EncodingHelper _encodingHelper; |
| | | 47 | | private readonly ITrickplayManager _trickplayManager; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Initializes a new instance of the <see cref="DynamicHlsHelper"/> class. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param> |
| | | 53 | | /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param> |
| | | 54 | | /// <param name="mediaSourceManager">Instance of the <see cref="IMediaSourceManager"/> interface.</param> |
| | | 55 | | /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</p |
| | | 56 | | /// <param name="mediaEncoder">Instance of the <see cref="IMediaEncoder"/> interface.</param> |
| | | 57 | | /// <param name="transcodeManager">Instance of <see cref="ITranscodeManager"/>.</param> |
| | | 58 | | /// <param name="networkManager">Instance of the <see cref="INetworkManager"/> interface.</param> |
| | | 59 | | /// <param name="logger">Instance of the <see cref="ILogger{DynamicHlsHelper}"/> interface.</param> |
| | | 60 | | /// <param name="httpContextAccessor">Instance of the <see cref="IHttpContextAccessor"/> interface.</param> |
| | | 61 | | /// <param name="encodingHelper">Instance of <see cref="EncodingHelper"/>.</param> |
| | | 62 | | /// <param name="trickplayManager">Instance of <see cref="ITrickplayManager"/>.</param> |
| | | 63 | | public DynamicHlsHelper( |
| | | 64 | | ILibraryManager libraryManager, |
| | | 65 | | IUserManager userManager, |
| | | 66 | | IMediaSourceManager mediaSourceManager, |
| | | 67 | | IServerConfigurationManager serverConfigurationManager, |
| | | 68 | | IMediaEncoder mediaEncoder, |
| | | 69 | | ITranscodeManager transcodeManager, |
| | | 70 | | INetworkManager networkManager, |
| | | 71 | | ILogger<DynamicHlsHelper> logger, |
| | | 72 | | IHttpContextAccessor httpContextAccessor, |
| | | 73 | | EncodingHelper encodingHelper, |
| | | 74 | | ITrickplayManager trickplayManager) |
| | | 75 | | { |
| | 0 | 76 | | _libraryManager = libraryManager; |
| | 0 | 77 | | _userManager = userManager; |
| | 0 | 78 | | _mediaSourceManager = mediaSourceManager; |
| | 0 | 79 | | _serverConfigurationManager = serverConfigurationManager; |
| | 0 | 80 | | _mediaEncoder = mediaEncoder; |
| | 0 | 81 | | _transcodeManager = transcodeManager; |
| | 0 | 82 | | _networkManager = networkManager; |
| | 0 | 83 | | _logger = logger; |
| | 0 | 84 | | _httpContextAccessor = httpContextAccessor; |
| | 0 | 85 | | _encodingHelper = encodingHelper; |
| | 0 | 86 | | _trickplayManager = trickplayManager; |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Get master hls playlist. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="transcodingJobType">Transcoding job type.</param> |
| | | 93 | | /// <param name="streamingRequest">Streaming request dto.</param> |
| | | 94 | | /// <param name="enableAdaptiveBitrateStreaming">Enable adaptive bitrate streaming.</param> |
| | | 95 | | /// <returns>A <see cref="Task"/> containing the resulting <see cref="ActionResult"/>.</returns> |
| | | 96 | | public async Task<ActionResult> GetMasterHlsPlaylist( |
| | | 97 | | TranscodingJobType transcodingJobType, |
| | | 98 | | StreamingRequestDto streamingRequest, |
| | | 99 | | bool enableAdaptiveBitrateStreaming) |
| | | 100 | | { |
| | | 101 | | var isHeadRequest = _httpContextAccessor.HttpContext?.Request.Method == WebRequestMethods.Http.Head; |
| | | 102 | | // CTS lifecycle is managed internally. |
| | | 103 | | var cancellationTokenSource = new CancellationTokenSource(); |
| | | 104 | | return await GetMasterPlaylistInternal( |
| | | 105 | | streamingRequest, |
| | | 106 | | isHeadRequest, |
| | | 107 | | enableAdaptiveBitrateStreaming, |
| | | 108 | | transcodingJobType, |
| | | 109 | | cancellationTokenSource).ConfigureAwait(false); |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | private async Task<ActionResult> GetMasterPlaylistInternal( |
| | | 113 | | StreamingRequestDto streamingRequest, |
| | | 114 | | bool isHeadRequest, |
| | | 115 | | bool enableAdaptiveBitrateStreaming, |
| | | 116 | | TranscodingJobType transcodingJobType, |
| | | 117 | | CancellationTokenSource cancellationTokenSource) |
| | | 118 | | { |
| | | 119 | | if (_httpContextAccessor.HttpContext is null) |
| | | 120 | | { |
| | | 121 | | throw new ResourceNotFoundException(nameof(_httpContextAccessor.HttpContext)); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | using var state = await StreamingHelpers.GetStreamingState( |
| | | 125 | | streamingRequest, |
| | | 126 | | _httpContextAccessor.HttpContext, |
| | | 127 | | _mediaSourceManager, |
| | | 128 | | _userManager, |
| | | 129 | | _libraryManager, |
| | | 130 | | _serverConfigurationManager, |
| | | 131 | | _mediaEncoder, |
| | | 132 | | _encodingHelper, |
| | | 133 | | _transcodeManager, |
| | | 134 | | transcodingJobType, |
| | | 135 | | cancellationTokenSource.Token) |
| | | 136 | | .ConfigureAwait(false); |
| | | 137 | | |
| | | 138 | | _httpContextAccessor.HttpContext.Response.Headers.Append(HeaderNames.Expires, "0"); |
| | | 139 | | if (isHeadRequest) |
| | | 140 | | { |
| | | 141 | | return new FileContentResult(Array.Empty<byte>(), MimeTypes.GetMimeType("playlist.m3u8")); |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | var totalBitrate = (state.OutputAudioBitrate ?? 0) + (state.OutputVideoBitrate ?? 0); |
| | | 145 | | |
| | | 146 | | var builder = new StringBuilder(); |
| | | 147 | | |
| | | 148 | | builder.AppendLine("#EXTM3U"); |
| | | 149 | | |
| | | 150 | | var isLiveStream = state.IsSegmentedLiveStream; |
| | | 151 | | |
| | | 152 | | var queryString = _httpContextAccessor.HttpContext.Request.QueryString.ToString(); |
| | | 153 | | |
| | | 154 | | // from universal audio service, need to override the AudioCodec when the actual request differs from original q |
| | | 155 | | if (!string.Equals(state.OutputAudioCodec, _httpContextAccessor.HttpContext.Request.Query["AudioCodec"].ToString |
| | | 156 | | { |
| | | 157 | | var newQuery = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(queryString); |
| | | 158 | | newQuery["AudioCodec"] = state.OutputAudioCodec; |
| | | 159 | | queryString = Microsoft.AspNetCore.WebUtilities.QueryHelpers.AddQueryString(string.Empty, newQuery); |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | // from universal audio service |
| | | 163 | | if (!string.IsNullOrWhiteSpace(state.Request.SegmentContainer) |
| | | 164 | | && !queryString.Contains("SegmentContainer", StringComparison.OrdinalIgnoreCase)) |
| | | 165 | | { |
| | | 166 | | queryString += "&SegmentContainer=" + state.Request.SegmentContainer; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | // from universal audio service |
| | | 170 | | if (!string.IsNullOrWhiteSpace(state.Request.TranscodeReasons) |
| | | 171 | | && !queryString.Contains("TranscodeReasons=", StringComparison.OrdinalIgnoreCase)) |
| | | 172 | | { |
| | | 173 | | queryString += "&TranscodeReasons=" + state.Request.TranscodeReasons; |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | // Video rotation metadata is only supported in fMP4 remuxing |
| | | 177 | | if (state.VideoStream is not null |
| | | 178 | | && state.VideoRequest is not null |
| | | 179 | | && (state.VideoStream?.Rotation ?? 0) != 0 |
| | | 180 | | && EncodingHelper.IsCopyCodec(state.OutputVideoCodec) |
| | | 181 | | && !string.IsNullOrWhiteSpace(state.Request.SegmentContainer) |
| | | 182 | | && !string.Equals(state.Request.SegmentContainer, "mp4", StringComparison.OrdinalIgnoreCase)) |
| | | 183 | | { |
| | | 184 | | queryString += "&AllowVideoStreamCopy=false"; |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | // Main stream |
| | | 188 | | var baseUrl = isLiveStream ? "live.m3u8" : "main.m3u8"; |
| | | 189 | | var playlistUrl = baseUrl + queryString; |
| | | 190 | | var playlistQuery = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(queryString); |
| | | 191 | | |
| | | 192 | | var subtitleStreams = state.MediaSource |
| | | 193 | | .MediaStreams |
| | | 194 | | .Where(i => i.IsTextSubtitleStream) |
| | | 195 | | .ToList(); |
| | | 196 | | |
| | | 197 | | var subtitleGroup = subtitleStreams.Count > 0 && (state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Hls || |
| | | 198 | | ? "subs" |
| | | 199 | | : null; |
| | | 200 | | |
| | | 201 | | // If we're burning in subtitles then don't add additional subs to the manifest |
| | | 202 | | if (state.SubtitleStream is not null && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode) |
| | | 203 | | { |
| | | 204 | | subtitleGroup = null; |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | if (!string.IsNullOrWhiteSpace(subtitleGroup)) |
| | | 208 | | { |
| | | 209 | | AddSubtitles(state, subtitleStreams, builder, _httpContextAccessor.HttpContext.User); |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | // For DoVi profiles without a compatible base layer (P5 HEVC, P10/bl0 AV1), |
| | | 213 | | // add a spec-compliant dvh1/dav1 variant before the hvc1 hack variant. |
| | | 214 | | // SUPPLEMENTAL-CODECS cannot be used for these profiles (no compatible BL to supplement). |
| | | 215 | | // The DoVi variant is listed first so spec-compliant clients (Apple TV, webOS 24+) |
| | | 216 | | // select it over the fallback when both have identical BANDWIDTH. |
| | | 217 | | // Only emit for clients that explicitly declared DOVI support to avoid breaking |
| | | 218 | | // non-compliant players that don't recognize dvh1/dav1 CODECS strings. |
| | | 219 | | if (state.VideoStream is not null |
| | | 220 | | && state.VideoRequest is not null |
| | | 221 | | && EncodingHelper.IsCopyCodec(state.OutputVideoCodec) |
| | | 222 | | && state.VideoStream.VideoRangeType == VideoRangeType.DOVI |
| | | 223 | | && state.VideoStream.DvProfile.HasValue |
| | | 224 | | && state.VideoStream.DvLevel.HasValue |
| | | 225 | | && state.GetRequestedRangeTypes(state.VideoStream.Codec) |
| | | 226 | | .Contains(VideoRangeType.DOVI.ToString(), StringComparison.OrdinalIgnoreCase)) |
| | | 227 | | { |
| | | 228 | | AppendDoviPlaylist(builder, state, playlistUrl, totalBitrate, subtitleGroup); |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | var basicPlaylist = AppendPlaylist(builder, state, playlistUrl, totalBitrate, subtitleGroup); |
| | | 232 | | |
| | | 233 | | if (state.VideoStream is not null && state.VideoRequest is not null) |
| | | 234 | | { |
| | | 235 | | var encodingOptions = _serverConfigurationManager.GetEncodingOptions(); |
| | | 236 | | |
| | | 237 | | // Provide AV1 and HEVC SDR entrances for backward compatibility. |
| | | 238 | | foreach (var sdrVideoCodec in new[] { "av1", "hevc" }) |
| | | 239 | | { |
| | | 240 | | var isAv1EncodingAllowed = encodingOptions.AllowAv1Encoding |
| | | 241 | | && string.Equals(sdrVideoCodec, "av1", StringComparison.OrdinalIgnoreCase) |
| | | 242 | | && string.Equals(state.ActualOutputVideoCodec, "av1", StringComparison.OrdinalIgnoreCase); |
| | | 243 | | var isHevcEncodingAllowed = encodingOptions.AllowHevcEncoding |
| | | 244 | | && string.Equals(sdrVideoCodec, "hevc", StringComparison.OrdinalIgnoreCase) |
| | | 245 | | && string.Equals(state.ActualOutputVideoCodec, "hevc", StringComparison.OrdinalIgnoreCase); |
| | | 246 | | var isEncodingAllowed = isAv1EncodingAllowed || isHevcEncodingAllowed; |
| | | 247 | | |
| | | 248 | | if (isEncodingAllowed |
| | | 249 | | && EncodingHelper.IsCopyCodec(state.OutputVideoCodec) |
| | | 250 | | && state.VideoStream.VideoRange == VideoRange.HDR) |
| | | 251 | | { |
| | | 252 | | // Force AV1 and HEVC Main Profile and disable video stream copy. |
| | | 253 | | state.OutputVideoCodec = sdrVideoCodec; |
| | | 254 | | |
| | | 255 | | var sdrPlaylistQuery = playlistQuery; |
| | | 256 | | sdrPlaylistQuery["VideoCodec"] = sdrVideoCodec; |
| | | 257 | | sdrPlaylistQuery[sdrVideoCodec + "-profile"] = "main"; |
| | | 258 | | sdrPlaylistQuery["AllowVideoStreamCopy"] = "false"; |
| | | 259 | | |
| | | 260 | | var sdrVideoUrl = Microsoft.AspNetCore.WebUtilities.QueryHelpers.AddQueryString(baseUrl, sdrPlaylist |
| | | 261 | | |
| | | 262 | | // HACK: Use the same bitrate so that the client can choose by other attributes, such as color range |
| | | 263 | | AppendPlaylist(builder, state, sdrVideoUrl, totalBitrate, subtitleGroup); |
| | | 264 | | |
| | | 265 | | // Restore the video codec |
| | | 266 | | state.OutputVideoCodec = "copy"; |
| | | 267 | | } |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | // Provide H.264 SDR entrance for backward compatibility. |
| | | 271 | | if (EncodingHelper.IsCopyCodec(state.OutputVideoCodec) |
| | | 272 | | && state.VideoStream.VideoRange == VideoRange.HDR) |
| | | 273 | | { |
| | | 274 | | // Force H.264 and disable video stream copy. |
| | | 275 | | state.OutputVideoCodec = "h264"; |
| | | 276 | | |
| | | 277 | | var sdrPlaylistQuery = playlistQuery; |
| | | 278 | | sdrPlaylistQuery["VideoCodec"] = "h264"; |
| | | 279 | | sdrPlaylistQuery["AllowVideoStreamCopy"] = "false"; |
| | | 280 | | |
| | | 281 | | var sdrVideoUrl = Microsoft.AspNetCore.WebUtilities.QueryHelpers.AddQueryString(baseUrl, sdrPlaylistQuer |
| | | 282 | | |
| | | 283 | | // HACK: Use the same bitrate so that the client can choose by other attributes, such as color range. |
| | | 284 | | AppendPlaylist(builder, state, sdrVideoUrl, totalBitrate, subtitleGroup); |
| | | 285 | | |
| | | 286 | | // Restore the video codec |
| | | 287 | | state.OutputVideoCodec = "copy"; |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | // Provide Level 5.0 entrance for backward compatibility. |
| | | 291 | | // e.g. Apple A10 chips refuse the master playlist containing SDR HEVC Main Level 5.1 video, |
| | | 292 | | // but in fact it is capable of playing videos up to Level 6.1. |
| | | 293 | | if (EncodingHelper.IsCopyCodec(state.OutputVideoCodec) |
| | | 294 | | && state.VideoStream.Level.HasValue |
| | | 295 | | && state.VideoStream.Level > 150 |
| | | 296 | | && state.VideoStream.VideoRange == VideoRange.SDR |
| | | 297 | | && string.Equals(state.ActualOutputVideoCodec, "hevc", StringComparison.OrdinalIgnoreCase)) |
| | | 298 | | { |
| | | 299 | | var playlistCodecsField = new StringBuilder(); |
| | | 300 | | AppendPlaylistCodecsField(playlistCodecsField, state); |
| | | 301 | | |
| | | 302 | | // Force the video level to 5.0. |
| | | 303 | | var originalLevel = state.VideoStream.Level; |
| | | 304 | | state.VideoStream.Level = 150; |
| | | 305 | | var newPlaylistCodecsField = new StringBuilder(); |
| | | 306 | | AppendPlaylistCodecsField(newPlaylistCodecsField, state); |
| | | 307 | | |
| | | 308 | | // Restore the video level. |
| | | 309 | | state.VideoStream.Level = originalLevel; |
| | | 310 | | var newPlaylist = ReplacePlaylistCodecsField(basicPlaylist, playlistCodecsField, newPlaylistCodecsField) |
| | | 311 | | builder.Append(newPlaylist); |
| | | 312 | | } |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | if (EnableAdaptiveBitrateStreaming(state, isLiveStream, enableAdaptiveBitrateStreaming, _httpContextAccessor.Htt |
| | | 316 | | { |
| | | 317 | | var requestedVideoBitrate = state.VideoRequest?.VideoBitRate ?? 0; |
| | | 318 | | |
| | | 319 | | // By default, vary by just 200k |
| | | 320 | | var variation = GetBitrateVariation(totalBitrate); |
| | | 321 | | |
| | | 322 | | var newBitrate = totalBitrate - variation; |
| | | 323 | | var variantQuery = playlistQuery; |
| | | 324 | | variantQuery["VideoBitrate"] = (requestedVideoBitrate - variation).ToString(CultureInfo.InvariantCulture); |
| | | 325 | | var variantUrl = Microsoft.AspNetCore.WebUtilities.QueryHelpers.AddQueryString(baseUrl, variantQuery); |
| | | 326 | | AppendPlaylist(builder, state, variantUrl, newBitrate, subtitleGroup); |
| | | 327 | | |
| | | 328 | | variation *= 2; |
| | | 329 | | newBitrate = totalBitrate - variation; |
| | | 330 | | variantQuery["VideoBitrate"] = (requestedVideoBitrate - variation).ToString(CultureInfo.InvariantCulture); |
| | | 331 | | variantUrl = Microsoft.AspNetCore.WebUtilities.QueryHelpers.AddQueryString(baseUrl, variantQuery); |
| | | 332 | | AppendPlaylist(builder, state, variantUrl, newBitrate, subtitleGroup); |
| | | 333 | | } |
| | | 334 | | |
| | | 335 | | if (!isLiveStream && (state.VideoRequest?.EnableTrickplay ?? false)) |
| | | 336 | | { |
| | | 337 | | var sourceId = Guid.Parse(state.Request.MediaSourceId); |
| | | 338 | | var trickplayResolutions = await _trickplayManager.GetTrickplayResolutions(sourceId).ConfigureAwait(false); |
| | | 339 | | AddTrickplay(state, trickplayResolutions, builder, _httpContextAccessor.HttpContext.User); |
| | | 340 | | } |
| | | 341 | | |
| | | 342 | | return new FileContentResult(Encoding.UTF8.GetBytes(builder.ToString()), MimeTypes.GetMimeType("playlist.m3u8")) |
| | | 343 | | } |
| | | 344 | | |
| | | 345 | | private StringBuilder AppendPlaylist(StringBuilder builder, StreamState state, string url, int bitrate, string? subt |
| | | 346 | | { |
| | 0 | 347 | | var playlistBuilder = new StringBuilder(); |
| | 0 | 348 | | playlistBuilder.Append("#EXT-X-STREAM-INF:BANDWIDTH=") |
| | 0 | 349 | | .Append(bitrate.ToString(CultureInfo.InvariantCulture)) |
| | 0 | 350 | | .Append(",AVERAGE-BANDWIDTH=") |
| | 0 | 351 | | .Append(bitrate.ToString(CultureInfo.InvariantCulture)); |
| | | 352 | | |
| | 0 | 353 | | AppendPlaylistVideoRangeField(playlistBuilder, state); |
| | | 354 | | |
| | 0 | 355 | | AppendPlaylistCodecsField(playlistBuilder, state); |
| | | 356 | | |
| | 0 | 357 | | AppendPlaylistSupplementalCodecsField(playlistBuilder, state); |
| | | 358 | | |
| | 0 | 359 | | AppendPlaylistResolutionField(playlistBuilder, state); |
| | | 360 | | |
| | 0 | 361 | | AppendPlaylistFramerateField(playlistBuilder, state); |
| | | 362 | | |
| | 0 | 363 | | if (!string.IsNullOrWhiteSpace(subtitleGroup)) |
| | | 364 | | { |
| | 0 | 365 | | playlistBuilder.Append(",SUBTITLES=\"") |
| | 0 | 366 | | .Append(subtitleGroup) |
| | 0 | 367 | | .Append('"'); |
| | | 368 | | } |
| | | 369 | | |
| | 0 | 370 | | playlistBuilder.Append(Environment.NewLine); |
| | 0 | 371 | | playlistBuilder.AppendLine(url); |
| | 0 | 372 | | builder.Append(playlistBuilder); |
| | | 373 | | |
| | 0 | 374 | | return playlistBuilder; |
| | | 375 | | } |
| | | 376 | | |
| | | 377 | | /// <summary> |
| | | 378 | | /// Appends a Dolby Vision variant with dvh1/dav1 CODECS for profiles without a compatible |
| | | 379 | | /// base layer (P5 HEVC, P10/bl0 AV1). This enables spec-compliant HLS clients to detect |
| | | 380 | | /// DoVi from the manifest rather than relying on init segment inspection. |
| | | 381 | | /// </summary> |
| | | 382 | | /// <param name="builder">StringBuilder for the master playlist.</param> |
| | | 383 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 384 | | /// <param name="url">Playlist URL for this variant.</param> |
| | | 385 | | /// <param name="bitrate">Bitrate for the BANDWIDTH field.</param> |
| | | 386 | | /// <param name="subtitleGroup">Subtitle group identifier, or null.</param> |
| | | 387 | | private void AppendDoviPlaylist(StringBuilder builder, StreamState state, string url, int bitrate, string? subtitleG |
| | | 388 | | { |
| | 0 | 389 | | var dvProfile = state.VideoStream.DvProfile; |
| | 0 | 390 | | var dvLevel = state.VideoStream.DvLevel; |
| | 0 | 391 | | if (dvProfile is null || dvLevel is null) |
| | | 392 | | { |
| | 0 | 393 | | return; |
| | | 394 | | } |
| | | 395 | | |
| | 0 | 396 | | var playlistBuilder = new StringBuilder(); |
| | 0 | 397 | | playlistBuilder.Append("#EXT-X-STREAM-INF:BANDWIDTH=") |
| | 0 | 398 | | .Append(bitrate.ToString(CultureInfo.InvariantCulture)) |
| | 0 | 399 | | .Append(",AVERAGE-BANDWIDTH=") |
| | 0 | 400 | | .Append(bitrate.ToString(CultureInfo.InvariantCulture)); |
| | | 401 | | |
| | 0 | 402 | | playlistBuilder.Append(",VIDEO-RANGE=PQ"); |
| | | 403 | | |
| | 0 | 404 | | var dvCodec = HlsCodecStringHelpers.GetDoviString(dvProfile.Value, dvLevel.Value, state.ActualOutputVideoCodec); |
| | | 405 | | |
| | 0 | 406 | | string audioCodecs = string.Empty; |
| | 0 | 407 | | if (!string.IsNullOrEmpty(state.ActualOutputAudioCodec)) |
| | | 408 | | { |
| | 0 | 409 | | audioCodecs = GetPlaylistAudioCodecs(state); |
| | | 410 | | } |
| | | 411 | | |
| | 0 | 412 | | playlistBuilder.Append(",CODECS=\"") |
| | 0 | 413 | | .Append(dvCodec); |
| | 0 | 414 | | if (!string.IsNullOrEmpty(audioCodecs)) |
| | | 415 | | { |
| | 0 | 416 | | playlistBuilder.Append(',').Append(audioCodecs); |
| | | 417 | | } |
| | | 418 | | |
| | 0 | 419 | | playlistBuilder.Append('"'); |
| | | 420 | | |
| | 0 | 421 | | AppendPlaylistResolutionField(playlistBuilder, state); |
| | 0 | 422 | | AppendPlaylistFramerateField(playlistBuilder, state); |
| | | 423 | | |
| | 0 | 424 | | if (!string.IsNullOrWhiteSpace(subtitleGroup)) |
| | | 425 | | { |
| | 0 | 426 | | playlistBuilder.Append(",SUBTITLES=\"") |
| | 0 | 427 | | .Append(subtitleGroup) |
| | 0 | 428 | | .Append('"'); |
| | | 429 | | } |
| | | 430 | | |
| | 0 | 431 | | playlistBuilder.AppendLine(); |
| | 0 | 432 | | playlistBuilder.AppendLine(url); |
| | 0 | 433 | | builder.Append(playlistBuilder); |
| | 0 | 434 | | } |
| | | 435 | | |
| | | 436 | | /// <summary> |
| | | 437 | | /// Appends a VIDEO-RANGE field containing the range of the output video stream. |
| | | 438 | | /// </summary> |
| | | 439 | | /// <seealso cref="AppendPlaylist(StringBuilder, StreamState, string, int, string)"/> |
| | | 440 | | /// <param name="builder">StringBuilder to append the field to.</param> |
| | | 441 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 442 | | private void AppendPlaylistVideoRangeField(StringBuilder builder, StreamState state) |
| | | 443 | | { |
| | 0 | 444 | | if (state.VideoStream is not null && state.VideoStream.VideoRange != VideoRange.Unknown) |
| | | 445 | | { |
| | 0 | 446 | | var videoRange = state.VideoStream.VideoRange; |
| | 0 | 447 | | var videoRangeType = state.VideoStream.VideoRangeType; |
| | 0 | 448 | | if (EncodingHelper.IsCopyCodec(state.OutputVideoCodec)) |
| | | 449 | | { |
| | 0 | 450 | | if (videoRange == VideoRange.SDR) |
| | | 451 | | { |
| | 0 | 452 | | builder.Append(",VIDEO-RANGE=SDR"); |
| | | 453 | | } |
| | | 454 | | |
| | 0 | 455 | | if (videoRange == VideoRange.HDR) |
| | | 456 | | { |
| | | 457 | | switch (videoRangeType) |
| | | 458 | | { |
| | | 459 | | case VideoRangeType.HLG: |
| | | 460 | | case VideoRangeType.DOVIWithHLG: |
| | 0 | 461 | | builder.Append(",VIDEO-RANGE=HLG"); |
| | 0 | 462 | | break; |
| | | 463 | | default: |
| | 0 | 464 | | builder.Append(",VIDEO-RANGE=PQ"); |
| | 0 | 465 | | break; |
| | | 466 | | } |
| | | 467 | | } |
| | | 468 | | } |
| | | 469 | | else |
| | | 470 | | { |
| | | 471 | | // Currently we only encode to SDR. |
| | 0 | 472 | | builder.Append(",VIDEO-RANGE=SDR"); |
| | | 473 | | } |
| | | 474 | | } |
| | 0 | 475 | | } |
| | | 476 | | |
| | | 477 | | /// <summary> |
| | | 478 | | /// Appends a CODECS field containing formatted strings of |
| | | 479 | | /// the active streams output video and audio codecs. |
| | | 480 | | /// </summary> |
| | | 481 | | /// <seealso cref="AppendPlaylist(StringBuilder, StreamState, string, int, string)"/> |
| | | 482 | | /// <seealso cref="GetPlaylistVideoCodecs(StreamState, string, int)"/> |
| | | 483 | | /// <seealso cref="GetPlaylistAudioCodecs(StreamState)"/> |
| | | 484 | | /// <param name="builder">StringBuilder to append the field to.</param> |
| | | 485 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 486 | | private void AppendPlaylistCodecsField(StringBuilder builder, StreamState state) |
| | | 487 | | { |
| | | 488 | | // Video |
| | 0 | 489 | | string videoCodecs = string.Empty; |
| | 0 | 490 | | int? videoCodecLevel = GetOutputVideoCodecLevel(state); |
| | 0 | 491 | | if (!string.IsNullOrEmpty(state.ActualOutputVideoCodec) && videoCodecLevel.HasValue) |
| | | 492 | | { |
| | 0 | 493 | | videoCodecs = GetPlaylistVideoCodecs(state, state.ActualOutputVideoCodec, videoCodecLevel.Value); |
| | | 494 | | } |
| | | 495 | | |
| | | 496 | | // Audio |
| | 0 | 497 | | string audioCodecs = string.Empty; |
| | 0 | 498 | | if (!string.IsNullOrEmpty(state.ActualOutputAudioCodec)) |
| | | 499 | | { |
| | 0 | 500 | | audioCodecs = GetPlaylistAudioCodecs(state); |
| | | 501 | | } |
| | | 502 | | |
| | 0 | 503 | | StringBuilder codecs = new StringBuilder(); |
| | | 504 | | |
| | 0 | 505 | | codecs.Append(videoCodecs); |
| | | 506 | | |
| | 0 | 507 | | if (!string.IsNullOrEmpty(videoCodecs) && !string.IsNullOrEmpty(audioCodecs)) |
| | | 508 | | { |
| | 0 | 509 | | codecs.Append(','); |
| | | 510 | | } |
| | | 511 | | |
| | 0 | 512 | | codecs.Append(audioCodecs); |
| | | 513 | | |
| | 0 | 514 | | if (codecs.Length > 1) |
| | | 515 | | { |
| | 0 | 516 | | builder.Append(",CODECS=\"") |
| | 0 | 517 | | .Append(codecs) |
| | 0 | 518 | | .Append('"'); |
| | | 519 | | } |
| | 0 | 520 | | } |
| | | 521 | | |
| | | 522 | | /// <summary> |
| | | 523 | | /// Appends a SUPPLEMENTAL-CODECS field containing formatted strings of |
| | | 524 | | /// the active streams output Dolby Vision Videos. |
| | | 525 | | /// </summary> |
| | | 526 | | /// <seealso cref="AppendPlaylist(StringBuilder, StreamState, string, int, string)"/> |
| | | 527 | | /// <seealso cref="GetPlaylistVideoCodecs(StreamState, string, int)"/> |
| | | 528 | | /// <param name="builder">StringBuilder to append the field to.</param> |
| | | 529 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 530 | | private void AppendPlaylistSupplementalCodecsField(StringBuilder builder, StreamState state) |
| | | 531 | | { |
| | | 532 | | // HDR dynamic metadata currently cannot exist when transcoding |
| | 0 | 533 | | if (!EncodingHelper.IsCopyCodec(state.OutputVideoCodec)) |
| | | 534 | | { |
| | 0 | 535 | | return; |
| | | 536 | | } |
| | | 537 | | |
| | 0 | 538 | | if (EncodingHelper.IsDovi(state.VideoStream) && !_encodingHelper.IsDoviRemoved(state)) |
| | | 539 | | { |
| | 0 | 540 | | AppendDvString(); |
| | | 541 | | } |
| | 0 | 542 | | else if (EncodingHelper.IsHdr10Plus(state.VideoStream) && !_encodingHelper.IsHdr10PlusRemoved(state)) |
| | | 543 | | { |
| | 0 | 544 | | AppendHdr10PlusString(); |
| | | 545 | | } |
| | | 546 | | |
| | 0 | 547 | | return; |
| | | 548 | | |
| | | 549 | | void AppendDvString() |
| | | 550 | | { |
| | | 551 | | var dvProfile = state.VideoStream.DvProfile; |
| | | 552 | | var dvLevel = state.VideoStream.DvLevel; |
| | | 553 | | var dvRangeString = state.VideoStream.VideoRangeType switch |
| | | 554 | | { |
| | | 555 | | VideoRangeType.DOVIWithHDR10 => "db1p", |
| | | 556 | | VideoRangeType.DOVIWithHLG => "db4h", |
| | | 557 | | VideoRangeType.DOVIWithHDR10Plus => "db1p", // The HDR10+ metadata would be removed if Dovi metadata is |
| | | 558 | | _ => string.Empty // Don't label Dovi with EL and SDR due to compatability issues, ignore invalid config |
| | | 559 | | }; |
| | | 560 | | |
| | | 561 | | if (dvProfile is null || dvLevel is null || string.IsNullOrEmpty(dvRangeString)) |
| | | 562 | | { |
| | | 563 | | return; |
| | | 564 | | } |
| | | 565 | | |
| | | 566 | | var dvFourCc = string.Equals(state.ActualOutputVideoCodec, "av1", StringComparison.OrdinalIgnoreCase) ? "dav |
| | | 567 | | builder.Append(",SUPPLEMENTAL-CODECS=\"") |
| | | 568 | | .Append(dvFourCc) |
| | | 569 | | .Append('.') |
| | | 570 | | .Append(dvProfile.Value.ToString("D2", CultureInfo.InvariantCulture)) |
| | | 571 | | .Append('.') |
| | | 572 | | .Append(dvLevel.Value.ToString("D2", CultureInfo.InvariantCulture)) |
| | | 573 | | .Append('/') |
| | | 574 | | .Append(dvRangeString) |
| | | 575 | | .Append('"'); |
| | | 576 | | } |
| | | 577 | | |
| | | 578 | | void AppendHdr10PlusString() |
| | | 579 | | { |
| | | 580 | | var videoCodecLevel = GetOutputVideoCodecLevel(state); |
| | | 581 | | if (string.IsNullOrEmpty(state.ActualOutputVideoCodec) || videoCodecLevel is null) |
| | | 582 | | { |
| | | 583 | | return; |
| | | 584 | | } |
| | | 585 | | |
| | | 586 | | var videoCodecString = GetPlaylistVideoCodecs(state, state.ActualOutputVideoCodec, videoCodecLevel.Value); |
| | | 587 | | builder.Append(",SUPPLEMENTAL-CODECS=\"") |
| | | 588 | | .Append(videoCodecString) |
| | | 589 | | .Append('/') |
| | | 590 | | .Append("cdm4") |
| | | 591 | | .Append('"'); |
| | | 592 | | } |
| | | 593 | | } |
| | | 594 | | |
| | | 595 | | /// <summary> |
| | | 596 | | /// Appends a RESOLUTION field containing the resolution of the output stream. |
| | | 597 | | /// </summary> |
| | | 598 | | /// <seealso cref="AppendPlaylist(StringBuilder, StreamState, string, int, string)"/> |
| | | 599 | | /// <param name="builder">StringBuilder to append the field to.</param> |
| | | 600 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 601 | | private void AppendPlaylistResolutionField(StringBuilder builder, StreamState state) |
| | | 602 | | { |
| | 0 | 603 | | if (state.OutputWidth.HasValue && state.OutputHeight.HasValue) |
| | | 604 | | { |
| | 0 | 605 | | builder.Append(",RESOLUTION=") |
| | 0 | 606 | | .Append(state.OutputWidth.GetValueOrDefault()) |
| | 0 | 607 | | .Append('x') |
| | 0 | 608 | | .Append(state.OutputHeight.GetValueOrDefault()); |
| | | 609 | | } |
| | 0 | 610 | | } |
| | | 611 | | |
| | | 612 | | /// <summary> |
| | | 613 | | /// Appends a FRAME-RATE field containing the framerate of the output stream. |
| | | 614 | | /// </summary> |
| | | 615 | | /// <seealso cref="AppendPlaylist(StringBuilder, StreamState, string, int, string)"/> |
| | | 616 | | /// <param name="builder">StringBuilder to append the field to.</param> |
| | | 617 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 618 | | private void AppendPlaylistFramerateField(StringBuilder builder, StreamState state) |
| | | 619 | | { |
| | 0 | 620 | | double? framerate = null; |
| | 0 | 621 | | if (state.TargetFramerate.HasValue) |
| | | 622 | | { |
| | 0 | 623 | | framerate = Math.Round(state.TargetFramerate.GetValueOrDefault(), 3); |
| | | 624 | | } |
| | 0 | 625 | | else if (state.VideoStream?.RealFrameRate is not null) |
| | | 626 | | { |
| | 0 | 627 | | framerate = Math.Round(state.VideoStream.RealFrameRate.GetValueOrDefault(), 3); |
| | | 628 | | } |
| | | 629 | | |
| | 0 | 630 | | if (framerate.HasValue) |
| | | 631 | | { |
| | 0 | 632 | | builder.Append(",FRAME-RATE=") |
| | 0 | 633 | | .Append(framerate.Value.ToString(CultureInfo.InvariantCulture)); |
| | | 634 | | } |
| | 0 | 635 | | } |
| | | 636 | | |
| | | 637 | | private bool EnableAdaptiveBitrateStreaming(StreamState state, bool isLiveStream, bool enableAdaptiveBitrateStreamin |
| | | 638 | | { |
| | | 639 | | // Within the local network this will likely do more harm than good. |
| | 0 | 640 | | if (_networkManager.IsInLocalNetwork(ipAddress)) |
| | | 641 | | { |
| | 0 | 642 | | return false; |
| | | 643 | | } |
| | | 644 | | |
| | 0 | 645 | | if (!enableAdaptiveBitrateStreaming) |
| | | 646 | | { |
| | 0 | 647 | | return false; |
| | | 648 | | } |
| | | 649 | | |
| | 0 | 650 | | if (isLiveStream || string.IsNullOrWhiteSpace(state.MediaPath)) |
| | | 651 | | { |
| | | 652 | | // Opening live streams is so slow it's not even worth it |
| | 0 | 653 | | return false; |
| | | 654 | | } |
| | | 655 | | |
| | 0 | 656 | | if (EncodingHelper.IsCopyCodec(state.OutputVideoCodec)) |
| | | 657 | | { |
| | 0 | 658 | | return false; |
| | | 659 | | } |
| | | 660 | | |
| | 0 | 661 | | if (EncodingHelper.IsCopyCodec(state.OutputAudioCodec)) |
| | | 662 | | { |
| | 0 | 663 | | return false; |
| | | 664 | | } |
| | | 665 | | |
| | 0 | 666 | | if (!state.IsOutputVideo) |
| | | 667 | | { |
| | 0 | 668 | | return false; |
| | | 669 | | } |
| | | 670 | | |
| | 0 | 671 | | return state.VideoRequest?.VideoBitRate.HasValue ?? false; |
| | | 672 | | } |
| | | 673 | | |
| | | 674 | | private void AddSubtitles(StreamState state, IEnumerable<MediaStream> subtitles, StringBuilder builder, ClaimsPrinci |
| | | 675 | | { |
| | 0 | 676 | | if (state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Drop) |
| | | 677 | | { |
| | 0 | 678 | | return; |
| | | 679 | | } |
| | | 680 | | |
| | 0 | 681 | | var selectedIndex = state.SubtitleStream is null || state.SubtitleDeliveryMethod != SubtitleDeliveryMethod.Hls ? |
| | | 682 | | const string Format = "#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID=\"subs\",NAME=\"{0}\",DEFAULT={1},FORCED={2},AUTOSEL |
| | | 683 | | |
| | 0 | 684 | | foreach (var stream in subtitles) |
| | | 685 | | { |
| | 0 | 686 | | var name = stream.DisplayTitle; |
| | | 687 | | |
| | 0 | 688 | | var isDefault = selectedIndex.HasValue && selectedIndex.Value == stream.Index; |
| | 0 | 689 | | var isForced = stream.IsForced; |
| | | 690 | | |
| | 0 | 691 | | var url = string.Format( |
| | 0 | 692 | | CultureInfo.InvariantCulture, |
| | 0 | 693 | | "{0}/Subtitles/{1}/subtitles.m3u8?SegmentLength={2}&ApiKey={3}", |
| | 0 | 694 | | state.Request.MediaSourceId, |
| | 0 | 695 | | stream.Index.ToString(CultureInfo.InvariantCulture), |
| | 0 | 696 | | 30.ToString(CultureInfo.InvariantCulture), |
| | 0 | 697 | | user.GetToken()); |
| | | 698 | | |
| | 0 | 699 | | var line = string.Format( |
| | 0 | 700 | | CultureInfo.InvariantCulture, |
| | 0 | 701 | | Format, |
| | 0 | 702 | | name, |
| | 0 | 703 | | isDefault ? "YES" : "NO", |
| | 0 | 704 | | isForced ? "YES" : "NO", |
| | 0 | 705 | | url, |
| | 0 | 706 | | stream.Language ?? "Unknown"); |
| | | 707 | | |
| | 0 | 708 | | builder.AppendLine(line); |
| | | 709 | | } |
| | 0 | 710 | | } |
| | | 711 | | |
| | | 712 | | /// <summary> |
| | | 713 | | /// Appends EXT-X-IMAGE-STREAM-INF playlists for each available trickplay resolution. |
| | | 714 | | /// </summary> |
| | | 715 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 716 | | /// <param name="trickplayResolutions">Dictionary of widths to corresponding tiles info.</param> |
| | | 717 | | /// <param name="builder">StringBuilder to append the field to.</param> |
| | | 718 | | /// <param name="user">Http user context.</param> |
| | | 719 | | private void AddTrickplay(StreamState state, Dictionary<int, TrickplayInfo> trickplayResolutions, StringBuilder buil |
| | | 720 | | { |
| | | 721 | | const string playlistFormat = "#EXT-X-IMAGE-STREAM-INF:BANDWIDTH={0},RESOLUTION={1}x{2},CODECS=\"jpeg\",URI=\"{3 |
| | | 722 | | |
| | 0 | 723 | | foreach (var resolution in trickplayResolutions) |
| | | 724 | | { |
| | 0 | 725 | | var width = resolution.Key; |
| | 0 | 726 | | var trickplayInfo = resolution.Value; |
| | | 727 | | |
| | 0 | 728 | | var url = string.Format( |
| | 0 | 729 | | CultureInfo.InvariantCulture, |
| | 0 | 730 | | "Trickplay/{0}/tiles.m3u8?MediaSourceId={1}&ApiKey={2}", |
| | 0 | 731 | | width.ToString(CultureInfo.InvariantCulture), |
| | 0 | 732 | | state.Request.MediaSourceId, |
| | 0 | 733 | | user.GetToken()); |
| | | 734 | | |
| | 0 | 735 | | builder.AppendFormat( |
| | 0 | 736 | | CultureInfo.InvariantCulture, |
| | 0 | 737 | | playlistFormat, |
| | 0 | 738 | | trickplayInfo.Bandwidth.ToString(CultureInfo.InvariantCulture), |
| | 0 | 739 | | trickplayInfo.Width.ToString(CultureInfo.InvariantCulture), |
| | 0 | 740 | | trickplayInfo.Height.ToString(CultureInfo.InvariantCulture), |
| | 0 | 741 | | url); |
| | | 742 | | |
| | 0 | 743 | | builder.AppendLine(); |
| | | 744 | | } |
| | 0 | 745 | | } |
| | | 746 | | |
| | | 747 | | /// <summary> |
| | | 748 | | /// Get the H.26X level of the output video stream. |
| | | 749 | | /// </summary> |
| | | 750 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 751 | | /// <returns>H.26X level of the output video stream.</returns> |
| | | 752 | | private int? GetOutputVideoCodecLevel(StreamState state) |
| | | 753 | | { |
| | 0 | 754 | | string levelString = string.Empty; |
| | 0 | 755 | | if (EncodingHelper.IsCopyCodec(state.OutputVideoCodec) |
| | 0 | 756 | | && state.VideoStream is not null |
| | 0 | 757 | | && state.VideoStream.Level.HasValue) |
| | | 758 | | { |
| | 0 | 759 | | levelString = state.VideoStream.Level.Value.ToString(CultureInfo.InvariantCulture); |
| | | 760 | | } |
| | | 761 | | else |
| | | 762 | | { |
| | 0 | 763 | | if (string.Equals(state.ActualOutputVideoCodec, "h264", StringComparison.OrdinalIgnoreCase)) |
| | | 764 | | { |
| | 0 | 765 | | levelString = state.GetRequestedLevel(state.ActualOutputVideoCodec) ?? "41"; |
| | 0 | 766 | | levelString = EncodingHelper.NormalizeTranscodingLevel(state, levelString); |
| | | 767 | | } |
| | | 768 | | |
| | 0 | 769 | | if (string.Equals(state.ActualOutputVideoCodec, "h265", StringComparison.OrdinalIgnoreCase) |
| | 0 | 770 | | || string.Equals(state.ActualOutputVideoCodec, "hevc", StringComparison.OrdinalIgnoreCase)) |
| | | 771 | | { |
| | 0 | 772 | | levelString = state.GetRequestedLevel("h265") ?? state.GetRequestedLevel("hevc") ?? "120"; |
| | 0 | 773 | | levelString = EncodingHelper.NormalizeTranscodingLevel(state, levelString); |
| | | 774 | | } |
| | | 775 | | |
| | 0 | 776 | | if (string.Equals(state.ActualOutputVideoCodec, "av1", StringComparison.OrdinalIgnoreCase)) |
| | | 777 | | { |
| | 0 | 778 | | levelString = state.GetRequestedLevel("av1") ?? "19"; |
| | 0 | 779 | | levelString = EncodingHelper.NormalizeTranscodingLevel(state, levelString); |
| | | 780 | | } |
| | | 781 | | } |
| | | 782 | | |
| | 0 | 783 | | if (int.TryParse(levelString, NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsedLevel)) |
| | | 784 | | { |
| | 0 | 785 | | return parsedLevel; |
| | | 786 | | } |
| | | 787 | | |
| | 0 | 788 | | return null; |
| | | 789 | | } |
| | | 790 | | |
| | | 791 | | /// <summary> |
| | | 792 | | /// Get the profile of the output video stream. |
| | | 793 | | /// </summary> |
| | | 794 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 795 | | /// <param name="codec">Video codec.</param> |
| | | 796 | | /// <returns>Profile of the output video stream.</returns> |
| | | 797 | | private string GetOutputVideoCodecProfile(StreamState state, string codec) |
| | | 798 | | { |
| | 0 | 799 | | string profileString = string.Empty; |
| | 0 | 800 | | if (EncodingHelper.IsCopyCodec(state.OutputVideoCodec) |
| | 0 | 801 | | && !string.IsNullOrEmpty(state.VideoStream.Profile)) |
| | | 802 | | { |
| | 0 | 803 | | profileString = state.VideoStream.Profile; |
| | | 804 | | } |
| | 0 | 805 | | else if (!string.IsNullOrEmpty(codec)) |
| | | 806 | | { |
| | 0 | 807 | | profileString = state.GetRequestedProfiles(codec).FirstOrDefault() ?? string.Empty; |
| | 0 | 808 | | if (string.Equals(state.ActualOutputVideoCodec, "h264", StringComparison.OrdinalIgnoreCase)) |
| | | 809 | | { |
| | 0 | 810 | | profileString ??= "high"; |
| | | 811 | | } |
| | | 812 | | |
| | 0 | 813 | | if (string.Equals(state.ActualOutputVideoCodec, "h265", StringComparison.OrdinalIgnoreCase) |
| | 0 | 814 | | || string.Equals(state.ActualOutputVideoCodec, "hevc", StringComparison.OrdinalIgnoreCase) |
| | 0 | 815 | | || string.Equals(state.ActualOutputVideoCodec, "av1", StringComparison.OrdinalIgnoreCase)) |
| | | 816 | | { |
| | 0 | 817 | | profileString ??= "main"; |
| | | 818 | | } |
| | | 819 | | } |
| | | 820 | | |
| | 0 | 821 | | return profileString; |
| | | 822 | | } |
| | | 823 | | |
| | | 824 | | /// <summary> |
| | | 825 | | /// Gets a formatted string of the output audio codec, for use in the CODECS field. |
| | | 826 | | /// </summary> |
| | | 827 | | /// <seealso cref="AppendPlaylistCodecsField(StringBuilder, StreamState)"/> |
| | | 828 | | /// <seealso cref="GetPlaylistVideoCodecs(StreamState, string, int)"/> |
| | | 829 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 830 | | /// <returns>Formatted audio codec string.</returns> |
| | | 831 | | private string GetPlaylistAudioCodecs(StreamState state) |
| | | 832 | | { |
| | 0 | 833 | | if (string.Equals(state.ActualOutputAudioCodec, "aac", StringComparison.OrdinalIgnoreCase)) |
| | | 834 | | { |
| | 0 | 835 | | string? profile = EncodingHelper.IsCopyCodec(state.OutputAudioCodec) |
| | 0 | 836 | | ? state.AudioStream?.Profile : state.GetRequestedProfiles("aac").FirstOrDefault(); |
| | | 837 | | |
| | 0 | 838 | | return HlsCodecStringHelpers.GetAACString(profile); |
| | | 839 | | } |
| | | 840 | | |
| | 0 | 841 | | if (string.Equals(state.ActualOutputAudioCodec, "mp3", StringComparison.OrdinalIgnoreCase)) |
| | | 842 | | { |
| | 0 | 843 | | return HlsCodecStringHelpers.GetMP3String(); |
| | | 844 | | } |
| | | 845 | | |
| | 0 | 846 | | if (string.Equals(state.ActualOutputAudioCodec, "ac3", StringComparison.OrdinalIgnoreCase)) |
| | | 847 | | { |
| | 0 | 848 | | return HlsCodecStringHelpers.GetAC3String(); |
| | | 849 | | } |
| | | 850 | | |
| | 0 | 851 | | if (string.Equals(state.ActualOutputAudioCodec, "eac3", StringComparison.OrdinalIgnoreCase)) |
| | | 852 | | { |
| | 0 | 853 | | return HlsCodecStringHelpers.GetEAC3String(); |
| | | 854 | | } |
| | | 855 | | |
| | 0 | 856 | | if (string.Equals(state.ActualOutputAudioCodec, "flac", StringComparison.OrdinalIgnoreCase)) |
| | | 857 | | { |
| | 0 | 858 | | return HlsCodecStringHelpers.GetFLACString(); |
| | | 859 | | } |
| | | 860 | | |
| | 0 | 861 | | if (string.Equals(state.ActualOutputAudioCodec, "alac", StringComparison.OrdinalIgnoreCase)) |
| | | 862 | | { |
| | 0 | 863 | | return HlsCodecStringHelpers.GetALACString(); |
| | | 864 | | } |
| | | 865 | | |
| | 0 | 866 | | if (string.Equals(state.ActualOutputAudioCodec, "opus", StringComparison.OrdinalIgnoreCase)) |
| | | 867 | | { |
| | 0 | 868 | | return HlsCodecStringHelpers.GetOPUSString(); |
| | | 869 | | } |
| | | 870 | | |
| | 0 | 871 | | if (string.Equals(state.ActualOutputAudioCodec, "truehd", StringComparison.OrdinalIgnoreCase)) |
| | | 872 | | { |
| | 0 | 873 | | return HlsCodecStringHelpers.GetTRUEHDString(); |
| | | 874 | | } |
| | | 875 | | |
| | 0 | 876 | | if (string.Equals(state.ActualOutputAudioCodec, "dts", StringComparison.OrdinalIgnoreCase)) |
| | | 877 | | { |
| | | 878 | | // lavc only support encoding DTS core profile |
| | 0 | 879 | | string? profile = EncodingHelper.IsCopyCodec(state.OutputAudioCodec) ? state.AudioStream?.Profile : "DTS"; |
| | | 880 | | |
| | 0 | 881 | | return HlsCodecStringHelpers.GetDTSString(profile); |
| | | 882 | | } |
| | | 883 | | |
| | 0 | 884 | | return string.Empty; |
| | | 885 | | } |
| | | 886 | | |
| | | 887 | | /// <summary> |
| | | 888 | | /// Gets a formatted string of the output video codec, for use in the CODECS field. |
| | | 889 | | /// </summary> |
| | | 890 | | /// <seealso cref="AppendPlaylistCodecsField(StringBuilder, StreamState)"/> |
| | | 891 | | /// <seealso cref="GetPlaylistAudioCodecs(StreamState)"/> |
| | | 892 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 893 | | /// <param name="codec">Video codec.</param> |
| | | 894 | | /// <param name="level">Video level.</param> |
| | | 895 | | /// <returns>Formatted video codec string.</returns> |
| | | 896 | | private string GetPlaylistVideoCodecs(StreamState state, string codec, int level) |
| | | 897 | | { |
| | 0 | 898 | | if (level == 0) |
| | | 899 | | { |
| | | 900 | | // This is 0 when there's no requested level in the device profile |
| | | 901 | | // and the source is not encoded in H.26X or AV1 |
| | 0 | 902 | | _logger.LogError("Got invalid level when building CODECS field for HLS master playlist"); |
| | 0 | 903 | | return string.Empty; |
| | | 904 | | } |
| | | 905 | | |
| | 0 | 906 | | if (string.Equals(codec, "h264", StringComparison.OrdinalIgnoreCase)) |
| | | 907 | | { |
| | 0 | 908 | | string profile = GetOutputVideoCodecProfile(state, "h264"); |
| | 0 | 909 | | return HlsCodecStringHelpers.GetH264String(profile, level); |
| | | 910 | | } |
| | | 911 | | |
| | 0 | 912 | | if (string.Equals(codec, "h265", StringComparison.OrdinalIgnoreCase) |
| | 0 | 913 | | || string.Equals(codec, "hevc", StringComparison.OrdinalIgnoreCase)) |
| | | 914 | | { |
| | 0 | 915 | | string profile = GetOutputVideoCodecProfile(state, "hevc"); |
| | 0 | 916 | | return HlsCodecStringHelpers.GetH265String(profile, level); |
| | | 917 | | } |
| | | 918 | | |
| | 0 | 919 | | if (string.Equals(codec, "av1", StringComparison.OrdinalIgnoreCase)) |
| | | 920 | | { |
| | 0 | 921 | | string profile = GetOutputVideoCodecProfile(state, "av1"); |
| | | 922 | | |
| | | 923 | | // Currently we only transcode to 8 bits AV1 |
| | 0 | 924 | | int bitDepth = 8; |
| | 0 | 925 | | if (EncodingHelper.IsCopyCodec(state.OutputVideoCodec) |
| | 0 | 926 | | && state.VideoStream is not null |
| | 0 | 927 | | && state.VideoStream.BitDepth.HasValue) |
| | | 928 | | { |
| | 0 | 929 | | bitDepth = state.VideoStream.BitDepth.Value; |
| | | 930 | | } |
| | | 931 | | |
| | 0 | 932 | | return HlsCodecStringHelpers.GetAv1String(profile, level, false, bitDepth); |
| | | 933 | | } |
| | | 934 | | |
| | | 935 | | // VP9 HLS is for video remuxing only, everything is probed from the original video |
| | 0 | 936 | | if (string.Equals(codec, "vp9", StringComparison.OrdinalIgnoreCase)) |
| | | 937 | | { |
| | 0 | 938 | | var width = state.VideoStream.Width ?? 0; |
| | 0 | 939 | | var height = state.VideoStream.Height ?? 0; |
| | 0 | 940 | | var framerate = state.VideoStream.ReferenceFrameRate ?? 30; |
| | 0 | 941 | | var bitDepth = state.VideoStream.BitDepth ?? 8; |
| | 0 | 942 | | return HlsCodecStringHelpers.GetVp9String( |
| | 0 | 943 | | width, |
| | 0 | 944 | | height, |
| | 0 | 945 | | state.VideoStream.PixelFormat, |
| | 0 | 946 | | framerate, |
| | 0 | 947 | | bitDepth); |
| | | 948 | | } |
| | | 949 | | |
| | 0 | 950 | | return string.Empty; |
| | | 951 | | } |
| | | 952 | | |
| | | 953 | | private int GetBitrateVariation(int bitrate) |
| | | 954 | | { |
| | | 955 | | // By default, vary by just 50k |
| | 0 | 956 | | var variation = 50000; |
| | | 957 | | |
| | 0 | 958 | | if (bitrate >= 10000000) |
| | | 959 | | { |
| | 0 | 960 | | variation = 2000000; |
| | | 961 | | } |
| | 0 | 962 | | else if (bitrate >= 5000000) |
| | | 963 | | { |
| | 0 | 964 | | variation = 1500000; |
| | | 965 | | } |
| | 0 | 966 | | else if (bitrate >= 3000000) |
| | | 967 | | { |
| | 0 | 968 | | variation = 1000000; |
| | | 969 | | } |
| | 0 | 970 | | else if (bitrate >= 2000000) |
| | | 971 | | { |
| | 0 | 972 | | variation = 500000; |
| | | 973 | | } |
| | 0 | 974 | | else if (bitrate >= 1000000) |
| | | 975 | | { |
| | 0 | 976 | | variation = 300000; |
| | | 977 | | } |
| | 0 | 978 | | else if (bitrate >= 600000) |
| | | 979 | | { |
| | 0 | 980 | | variation = 200000; |
| | | 981 | | } |
| | 0 | 982 | | else if (bitrate >= 400000) |
| | | 983 | | { |
| | 0 | 984 | | variation = 100000; |
| | | 985 | | } |
| | | 986 | | |
| | 0 | 987 | | return variation; |
| | | 988 | | } |
| | | 989 | | |
| | | 990 | | private string ReplacePlaylistCodecsField(StringBuilder playlist, StringBuilder oldValue, StringBuilder newValue) |
| | | 991 | | { |
| | 0 | 992 | | var oldPlaylist = playlist.ToString(); |
| | 0 | 993 | | return oldPlaylist.Replace( |
| | 0 | 994 | | oldValue.ToString(), |
| | 0 | 995 | | newValue.ToString(), |
| | 0 | 996 | | StringComparison.Ordinal); |
| | | 997 | | } |
| | | 998 | | } |