| | | 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(_httpContextAccessor.HttpContext.Re |
| | | 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 | | // Main stream |
| | | 177 | | var playlistUrl = isLiveStream ? "live.m3u8" : "main.m3u8"; |
| | | 178 | | |
| | | 179 | | playlistUrl += queryString; |
| | | 180 | | |
| | | 181 | | var subtitleStreams = state.MediaSource |
| | | 182 | | .MediaStreams |
| | | 183 | | .Where(i => i.IsTextSubtitleStream) |
| | | 184 | | .ToList(); |
| | | 185 | | |
| | | 186 | | var subtitleGroup = subtitleStreams.Count > 0 && (state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Hls || |
| | | 187 | | ? "subs" |
| | | 188 | | : null; |
| | | 189 | | |
| | | 190 | | // If we're burning in subtitles then don't add additional subs to the manifest |
| | | 191 | | if (state.SubtitleStream is not null && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode) |
| | | 192 | | { |
| | | 193 | | subtitleGroup = null; |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | if (!string.IsNullOrWhiteSpace(subtitleGroup)) |
| | | 197 | | { |
| | | 198 | | AddSubtitles(state, subtitleStreams, builder, _httpContextAccessor.HttpContext.User); |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | // Video rotation metadata is only supported in fMP4 remuxing |
| | | 202 | | if (state.VideoStream is not null |
| | | 203 | | && state.VideoRequest is not null |
| | | 204 | | && (state.VideoStream?.Rotation ?? 0) != 0 |
| | | 205 | | && EncodingHelper.IsCopyCodec(state.OutputVideoCodec) |
| | | 206 | | && !string.IsNullOrWhiteSpace(state.Request.SegmentContainer) |
| | | 207 | | && !string.Equals(state.Request.SegmentContainer, "mp4", StringComparison.OrdinalIgnoreCase)) |
| | | 208 | | { |
| | | 209 | | playlistUrl += "&AllowVideoStreamCopy=false"; |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | var basicPlaylist = AppendPlaylist(builder, state, playlistUrl, totalBitrate, subtitleGroup); |
| | | 213 | | |
| | | 214 | | if (state.VideoStream is not null && state.VideoRequest is not null) |
| | | 215 | | { |
| | | 216 | | var encodingOptions = _serverConfigurationManager.GetEncodingOptions(); |
| | | 217 | | |
| | | 218 | | // Provide SDR HEVC entrance for backward compatibility. |
| | | 219 | | if (encodingOptions.AllowHevcEncoding |
| | | 220 | | && !encodingOptions.AllowAv1Encoding |
| | | 221 | | && EncodingHelper.IsCopyCodec(state.OutputVideoCodec) |
| | | 222 | | && state.VideoStream.VideoRange == VideoRange.HDR |
| | | 223 | | && string.Equals(state.ActualOutputVideoCodec, "hevc", StringComparison.OrdinalIgnoreCase)) |
| | | 224 | | { |
| | | 225 | | var requestedVideoProfiles = state.GetRequestedProfiles("hevc"); |
| | | 226 | | if (requestedVideoProfiles is not null && requestedVideoProfiles.Length > 0) |
| | | 227 | | { |
| | | 228 | | // Force HEVC Main Profile and disable video stream copy. |
| | | 229 | | state.OutputVideoCodec = "hevc"; |
| | | 230 | | var sdrVideoUrl = ReplaceProfile(playlistUrl, "hevc", string.Join(',', requestedVideoProfiles), "mai |
| | | 231 | | sdrVideoUrl += "&AllowVideoStreamCopy=false"; |
| | | 232 | | |
| | | 233 | | // HACK: Use the same bitrate so that the client can choose by other attributes, such as color range |
| | | 234 | | AppendPlaylist(builder, state, sdrVideoUrl, totalBitrate, subtitleGroup); |
| | | 235 | | |
| | | 236 | | // Restore the video codec |
| | | 237 | | state.OutputVideoCodec = "copy"; |
| | | 238 | | } |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | // Provide Level 5.0 entrance for backward compatibility. |
| | | 242 | | // e.g. Apple A10 chips refuse the master playlist containing SDR HEVC Main Level 5.1 video, |
| | | 243 | | // but in fact it is capable of playing videos up to Level 6.1. |
| | | 244 | | if (encodingOptions.AllowHevcEncoding |
| | | 245 | | && !encodingOptions.AllowAv1Encoding |
| | | 246 | | && EncodingHelper.IsCopyCodec(state.OutputVideoCodec) |
| | | 247 | | && state.VideoStream.Level.HasValue |
| | | 248 | | && state.VideoStream.Level > 150 |
| | | 249 | | && state.VideoStream.VideoRange == VideoRange.SDR |
| | | 250 | | && string.Equals(state.ActualOutputVideoCodec, "hevc", StringComparison.OrdinalIgnoreCase)) |
| | | 251 | | { |
| | | 252 | | var playlistCodecsField = new StringBuilder(); |
| | | 253 | | AppendPlaylistCodecsField(playlistCodecsField, state); |
| | | 254 | | |
| | | 255 | | // Force the video level to 5.0. |
| | | 256 | | var originalLevel = state.VideoStream.Level; |
| | | 257 | | state.VideoStream.Level = 150; |
| | | 258 | | var newPlaylistCodecsField = new StringBuilder(); |
| | | 259 | | AppendPlaylistCodecsField(newPlaylistCodecsField, state); |
| | | 260 | | |
| | | 261 | | // Restore the video level. |
| | | 262 | | state.VideoStream.Level = originalLevel; |
| | | 263 | | var newPlaylist = ReplacePlaylistCodecsField(basicPlaylist, playlistCodecsField, newPlaylistCodecsField) |
| | | 264 | | builder.Append(newPlaylist); |
| | | 265 | | } |
| | | 266 | | } |
| | | 267 | | |
| | | 268 | | if (EnableAdaptiveBitrateStreaming(state, isLiveStream, enableAdaptiveBitrateStreaming, _httpContextAccessor.Htt |
| | | 269 | | { |
| | | 270 | | var requestedVideoBitrate = state.VideoRequest?.VideoBitRate ?? 0; |
| | | 271 | | |
| | | 272 | | // By default, vary by just 200k |
| | | 273 | | var variation = GetBitrateVariation(totalBitrate); |
| | | 274 | | |
| | | 275 | | var newBitrate = totalBitrate - variation; |
| | | 276 | | var variantUrl = ReplaceVideoBitrate(playlistUrl, requestedVideoBitrate, requestedVideoBitrate - variation); |
| | | 277 | | AppendPlaylist(builder, state, variantUrl, newBitrate, subtitleGroup); |
| | | 278 | | |
| | | 279 | | variation *= 2; |
| | | 280 | | newBitrate = totalBitrate - variation; |
| | | 281 | | variantUrl = ReplaceVideoBitrate(playlistUrl, requestedVideoBitrate, requestedVideoBitrate - variation); |
| | | 282 | | AppendPlaylist(builder, state, variantUrl, newBitrate, subtitleGroup); |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | if (!isLiveStream && (state.VideoRequest?.EnableTrickplay ?? false)) |
| | | 286 | | { |
| | | 287 | | var sourceId = Guid.Parse(state.Request.MediaSourceId); |
| | | 288 | | var trickplayResolutions = await _trickplayManager.GetTrickplayResolutions(sourceId).ConfigureAwait(false); |
| | | 289 | | AddTrickplay(state, trickplayResolutions, builder, _httpContextAccessor.HttpContext.User); |
| | | 290 | | } |
| | | 291 | | |
| | | 292 | | return new FileContentResult(Encoding.UTF8.GetBytes(builder.ToString()), MimeTypes.GetMimeType("playlist.m3u8")) |
| | | 293 | | } |
| | | 294 | | |
| | | 295 | | private StringBuilder AppendPlaylist(StringBuilder builder, StreamState state, string url, int bitrate, string? subt |
| | | 296 | | { |
| | 0 | 297 | | var playlistBuilder = new StringBuilder(); |
| | 0 | 298 | | playlistBuilder.Append("#EXT-X-STREAM-INF:BANDWIDTH=") |
| | 0 | 299 | | .Append(bitrate.ToString(CultureInfo.InvariantCulture)) |
| | 0 | 300 | | .Append(",AVERAGE-BANDWIDTH=") |
| | 0 | 301 | | .Append(bitrate.ToString(CultureInfo.InvariantCulture)); |
| | | 302 | | |
| | 0 | 303 | | AppendPlaylistVideoRangeField(playlistBuilder, state); |
| | | 304 | | |
| | 0 | 305 | | AppendPlaylistCodecsField(playlistBuilder, state); |
| | | 306 | | |
| | 0 | 307 | | AppendPlaylistSupplementalCodecsField(playlistBuilder, state); |
| | | 308 | | |
| | 0 | 309 | | AppendPlaylistResolutionField(playlistBuilder, state); |
| | | 310 | | |
| | 0 | 311 | | AppendPlaylistFramerateField(playlistBuilder, state); |
| | | 312 | | |
| | 0 | 313 | | if (!string.IsNullOrWhiteSpace(subtitleGroup)) |
| | | 314 | | { |
| | 0 | 315 | | playlistBuilder.Append(",SUBTITLES=\"") |
| | 0 | 316 | | .Append(subtitleGroup) |
| | 0 | 317 | | .Append('"'); |
| | | 318 | | } |
| | | 319 | | |
| | 0 | 320 | | playlistBuilder.Append(Environment.NewLine); |
| | 0 | 321 | | playlistBuilder.AppendLine(url); |
| | 0 | 322 | | builder.Append(playlistBuilder); |
| | | 323 | | |
| | 0 | 324 | | return playlistBuilder; |
| | | 325 | | } |
| | | 326 | | |
| | | 327 | | /// <summary> |
| | | 328 | | /// Appends a VIDEO-RANGE field containing the range of the output video stream. |
| | | 329 | | /// </summary> |
| | | 330 | | /// <seealso cref="AppendPlaylist(StringBuilder, StreamState, string, int, string)"/> |
| | | 331 | | /// <param name="builder">StringBuilder to append the field to.</param> |
| | | 332 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 333 | | private void AppendPlaylistVideoRangeField(StringBuilder builder, StreamState state) |
| | | 334 | | { |
| | 0 | 335 | | if (state.VideoStream is not null && state.VideoStream.VideoRange != VideoRange.Unknown) |
| | | 336 | | { |
| | 0 | 337 | | var videoRange = state.VideoStream.VideoRange; |
| | 0 | 338 | | var videoRangeType = state.VideoStream.VideoRangeType; |
| | 0 | 339 | | if (EncodingHelper.IsCopyCodec(state.OutputVideoCodec)) |
| | | 340 | | { |
| | 0 | 341 | | if (videoRange == VideoRange.SDR) |
| | | 342 | | { |
| | 0 | 343 | | builder.Append(",VIDEO-RANGE=SDR"); |
| | | 344 | | } |
| | | 345 | | |
| | 0 | 346 | | if (videoRange == VideoRange.HDR) |
| | | 347 | | { |
| | | 348 | | switch (videoRangeType) |
| | | 349 | | { |
| | | 350 | | case VideoRangeType.HLG: |
| | | 351 | | case VideoRangeType.DOVIWithHLG: |
| | 0 | 352 | | builder.Append(",VIDEO-RANGE=HLG"); |
| | 0 | 353 | | break; |
| | | 354 | | default: |
| | 0 | 355 | | builder.Append(",VIDEO-RANGE=PQ"); |
| | 0 | 356 | | break; |
| | | 357 | | } |
| | | 358 | | } |
| | | 359 | | } |
| | | 360 | | else |
| | | 361 | | { |
| | | 362 | | // Currently we only encode to SDR. |
| | 0 | 363 | | builder.Append(",VIDEO-RANGE=SDR"); |
| | | 364 | | } |
| | | 365 | | } |
| | 0 | 366 | | } |
| | | 367 | | |
| | | 368 | | /// <summary> |
| | | 369 | | /// Appends a CODECS field containing formatted strings of |
| | | 370 | | /// the active streams output video and audio codecs. |
| | | 371 | | /// </summary> |
| | | 372 | | /// <seealso cref="AppendPlaylist(StringBuilder, StreamState, string, int, string)"/> |
| | | 373 | | /// <seealso cref="GetPlaylistVideoCodecs(StreamState, string, int)"/> |
| | | 374 | | /// <seealso cref="GetPlaylistAudioCodecs(StreamState)"/> |
| | | 375 | | /// <param name="builder">StringBuilder to append the field to.</param> |
| | | 376 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 377 | | private void AppendPlaylistCodecsField(StringBuilder builder, StreamState state) |
| | | 378 | | { |
| | | 379 | | // Video |
| | 0 | 380 | | string videoCodecs = string.Empty; |
| | 0 | 381 | | int? videoCodecLevel = GetOutputVideoCodecLevel(state); |
| | 0 | 382 | | if (!string.IsNullOrEmpty(state.ActualOutputVideoCodec) && videoCodecLevel.HasValue) |
| | | 383 | | { |
| | 0 | 384 | | videoCodecs = GetPlaylistVideoCodecs(state, state.ActualOutputVideoCodec, videoCodecLevel.Value); |
| | | 385 | | } |
| | | 386 | | |
| | | 387 | | // Audio |
| | 0 | 388 | | string audioCodecs = string.Empty; |
| | 0 | 389 | | if (!string.IsNullOrEmpty(state.ActualOutputAudioCodec)) |
| | | 390 | | { |
| | 0 | 391 | | audioCodecs = GetPlaylistAudioCodecs(state); |
| | | 392 | | } |
| | | 393 | | |
| | 0 | 394 | | StringBuilder codecs = new StringBuilder(); |
| | | 395 | | |
| | 0 | 396 | | codecs.Append(videoCodecs); |
| | | 397 | | |
| | 0 | 398 | | if (!string.IsNullOrEmpty(videoCodecs) && !string.IsNullOrEmpty(audioCodecs)) |
| | | 399 | | { |
| | 0 | 400 | | codecs.Append(','); |
| | | 401 | | } |
| | | 402 | | |
| | 0 | 403 | | codecs.Append(audioCodecs); |
| | | 404 | | |
| | 0 | 405 | | if (codecs.Length > 1) |
| | | 406 | | { |
| | 0 | 407 | | builder.Append(",CODECS=\"") |
| | 0 | 408 | | .Append(codecs) |
| | 0 | 409 | | .Append('"'); |
| | | 410 | | } |
| | 0 | 411 | | } |
| | | 412 | | |
| | | 413 | | /// <summary> |
| | | 414 | | /// Appends a SUPPLEMENTAL-CODECS field containing formatted strings of |
| | | 415 | | /// the active streams output Dolby Vision Videos. |
| | | 416 | | /// </summary> |
| | | 417 | | /// <seealso cref="AppendPlaylist(StringBuilder, StreamState, string, int, string)"/> |
| | | 418 | | /// <seealso cref="GetPlaylistVideoCodecs(StreamState, string, int)"/> |
| | | 419 | | /// <param name="builder">StringBuilder to append the field to.</param> |
| | | 420 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 421 | | private void AppendPlaylistSupplementalCodecsField(StringBuilder builder, StreamState state) |
| | | 422 | | { |
| | | 423 | | // HDR dynamic metadata currently cannot exist when transcoding |
| | 0 | 424 | | if (!EncodingHelper.IsCopyCodec(state.OutputVideoCodec)) |
| | | 425 | | { |
| | 0 | 426 | | return; |
| | | 427 | | } |
| | | 428 | | |
| | 0 | 429 | | if (EncodingHelper.IsDovi(state.VideoStream) && !_encodingHelper.IsDoviRemoved(state)) |
| | | 430 | | { |
| | 0 | 431 | | AppendDvString(); |
| | | 432 | | } |
| | 0 | 433 | | else if (EncodingHelper.IsHdr10Plus(state.VideoStream) && !_encodingHelper.IsHdr10PlusRemoved(state)) |
| | | 434 | | { |
| | 0 | 435 | | AppendHdr10PlusString(); |
| | | 436 | | } |
| | | 437 | | |
| | 0 | 438 | | return; |
| | | 439 | | |
| | | 440 | | void AppendDvString() |
| | | 441 | | { |
| | | 442 | | var dvProfile = state.VideoStream.DvProfile; |
| | | 443 | | var dvLevel = state.VideoStream.DvLevel; |
| | | 444 | | var dvRangeString = state.VideoStream.VideoRangeType switch |
| | | 445 | | { |
| | | 446 | | VideoRangeType.DOVIWithHDR10 => "db1p", |
| | | 447 | | VideoRangeType.DOVIWithHLG => "db4h", |
| | | 448 | | VideoRangeType.DOVIWithHDR10Plus => "db1p", // The HDR10+ metadata would be removed if Dovi metadata is |
| | | 449 | | _ => string.Empty // Don't label Dovi with EL and SDR due to compatability issues, ignore invalid config |
| | | 450 | | }; |
| | | 451 | | |
| | | 452 | | if (dvProfile is null || dvLevel is null || string.IsNullOrEmpty(dvRangeString)) |
| | | 453 | | { |
| | | 454 | | return; |
| | | 455 | | } |
| | | 456 | | |
| | | 457 | | var dvFourCc = string.Equals(state.ActualOutputVideoCodec, "av1", StringComparison.OrdinalIgnoreCase) ? "dav |
| | | 458 | | builder.Append(",SUPPLEMENTAL-CODECS=\"") |
| | | 459 | | .Append(dvFourCc) |
| | | 460 | | .Append('.') |
| | | 461 | | .Append(dvProfile.Value.ToString("D2", CultureInfo.InvariantCulture)) |
| | | 462 | | .Append('.') |
| | | 463 | | .Append(dvLevel.Value.ToString("D2", CultureInfo.InvariantCulture)) |
| | | 464 | | .Append('/') |
| | | 465 | | .Append(dvRangeString) |
| | | 466 | | .Append('"'); |
| | | 467 | | } |
| | | 468 | | |
| | | 469 | | void AppendHdr10PlusString() |
| | | 470 | | { |
| | | 471 | | var videoCodecLevel = GetOutputVideoCodecLevel(state); |
| | | 472 | | if (string.IsNullOrEmpty(state.ActualOutputVideoCodec) || videoCodecLevel is null) |
| | | 473 | | { |
| | | 474 | | return; |
| | | 475 | | } |
| | | 476 | | |
| | | 477 | | var videoCodecString = GetPlaylistVideoCodecs(state, state.ActualOutputVideoCodec, videoCodecLevel.Value); |
| | | 478 | | builder.Append(",SUPPLEMENTAL-CODECS=\"") |
| | | 479 | | .Append(videoCodecString) |
| | | 480 | | .Append('/') |
| | | 481 | | .Append("cdm4") |
| | | 482 | | .Append('"'); |
| | | 483 | | } |
| | | 484 | | } |
| | | 485 | | |
| | | 486 | | /// <summary> |
| | | 487 | | /// Appends a RESOLUTION field containing the resolution of the output stream. |
| | | 488 | | /// </summary> |
| | | 489 | | /// <seealso cref="AppendPlaylist(StringBuilder, StreamState, string, int, string)"/> |
| | | 490 | | /// <param name="builder">StringBuilder to append the field to.</param> |
| | | 491 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 492 | | private void AppendPlaylistResolutionField(StringBuilder builder, StreamState state) |
| | | 493 | | { |
| | 0 | 494 | | if (state.OutputWidth.HasValue && state.OutputHeight.HasValue) |
| | | 495 | | { |
| | 0 | 496 | | builder.Append(",RESOLUTION=") |
| | 0 | 497 | | .Append(state.OutputWidth.GetValueOrDefault()) |
| | 0 | 498 | | .Append('x') |
| | 0 | 499 | | .Append(state.OutputHeight.GetValueOrDefault()); |
| | | 500 | | } |
| | 0 | 501 | | } |
| | | 502 | | |
| | | 503 | | /// <summary> |
| | | 504 | | /// Appends a FRAME-RATE field containing the framerate of the output stream. |
| | | 505 | | /// </summary> |
| | | 506 | | /// <seealso cref="AppendPlaylist(StringBuilder, StreamState, string, int, string)"/> |
| | | 507 | | /// <param name="builder">StringBuilder to append the field to.</param> |
| | | 508 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 509 | | private void AppendPlaylistFramerateField(StringBuilder builder, StreamState state) |
| | | 510 | | { |
| | 0 | 511 | | double? framerate = null; |
| | 0 | 512 | | if (state.TargetFramerate.HasValue) |
| | | 513 | | { |
| | 0 | 514 | | framerate = Math.Round(state.TargetFramerate.GetValueOrDefault(), 3); |
| | | 515 | | } |
| | 0 | 516 | | else if (state.VideoStream?.RealFrameRate is not null) |
| | | 517 | | { |
| | 0 | 518 | | framerate = Math.Round(state.VideoStream.RealFrameRate.GetValueOrDefault(), 3); |
| | | 519 | | } |
| | | 520 | | |
| | 0 | 521 | | if (framerate.HasValue) |
| | | 522 | | { |
| | 0 | 523 | | builder.Append(",FRAME-RATE=") |
| | 0 | 524 | | .Append(framerate.Value.ToString(CultureInfo.InvariantCulture)); |
| | | 525 | | } |
| | 0 | 526 | | } |
| | | 527 | | |
| | | 528 | | private bool EnableAdaptiveBitrateStreaming(StreamState state, bool isLiveStream, bool enableAdaptiveBitrateStreamin |
| | | 529 | | { |
| | | 530 | | // Within the local network this will likely do more harm than good. |
| | 0 | 531 | | if (_networkManager.IsInLocalNetwork(ipAddress)) |
| | | 532 | | { |
| | 0 | 533 | | return false; |
| | | 534 | | } |
| | | 535 | | |
| | 0 | 536 | | if (!enableAdaptiveBitrateStreaming) |
| | | 537 | | { |
| | 0 | 538 | | return false; |
| | | 539 | | } |
| | | 540 | | |
| | 0 | 541 | | if (isLiveStream || string.IsNullOrWhiteSpace(state.MediaPath)) |
| | | 542 | | { |
| | | 543 | | // Opening live streams is so slow it's not even worth it |
| | 0 | 544 | | return false; |
| | | 545 | | } |
| | | 546 | | |
| | 0 | 547 | | if (EncodingHelper.IsCopyCodec(state.OutputVideoCodec)) |
| | | 548 | | { |
| | 0 | 549 | | return false; |
| | | 550 | | } |
| | | 551 | | |
| | 0 | 552 | | if (EncodingHelper.IsCopyCodec(state.OutputAudioCodec)) |
| | | 553 | | { |
| | 0 | 554 | | return false; |
| | | 555 | | } |
| | | 556 | | |
| | 0 | 557 | | if (!state.IsOutputVideo) |
| | | 558 | | { |
| | 0 | 559 | | return false; |
| | | 560 | | } |
| | | 561 | | |
| | 0 | 562 | | return state.VideoRequest?.VideoBitRate.HasValue ?? false; |
| | | 563 | | } |
| | | 564 | | |
| | | 565 | | private void AddSubtitles(StreamState state, IEnumerable<MediaStream> subtitles, StringBuilder builder, ClaimsPrinci |
| | | 566 | | { |
| | 0 | 567 | | if (state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Drop) |
| | | 568 | | { |
| | 0 | 569 | | return; |
| | | 570 | | } |
| | | 571 | | |
| | 0 | 572 | | var selectedIndex = state.SubtitleStream is null || state.SubtitleDeliveryMethod != SubtitleDeliveryMethod.Hls ? |
| | | 573 | | const string Format = "#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID=\"subs\",NAME=\"{0}\",DEFAULT={1},FORCED={2},AUTOSEL |
| | | 574 | | |
| | 0 | 575 | | foreach (var stream in subtitles) |
| | | 576 | | { |
| | 0 | 577 | | var name = stream.DisplayTitle; |
| | | 578 | | |
| | 0 | 579 | | var isDefault = selectedIndex.HasValue && selectedIndex.Value == stream.Index; |
| | 0 | 580 | | var isForced = stream.IsForced; |
| | | 581 | | |
| | 0 | 582 | | var url = string.Format( |
| | 0 | 583 | | CultureInfo.InvariantCulture, |
| | 0 | 584 | | "{0}/Subtitles/{1}/subtitles.m3u8?SegmentLength={2}&ApiKey={3}", |
| | 0 | 585 | | state.Request.MediaSourceId, |
| | 0 | 586 | | stream.Index.ToString(CultureInfo.InvariantCulture), |
| | 0 | 587 | | 30.ToString(CultureInfo.InvariantCulture), |
| | 0 | 588 | | user.GetToken()); |
| | | 589 | | |
| | 0 | 590 | | var line = string.Format( |
| | 0 | 591 | | CultureInfo.InvariantCulture, |
| | 0 | 592 | | Format, |
| | 0 | 593 | | name, |
| | 0 | 594 | | isDefault ? "YES" : "NO", |
| | 0 | 595 | | isForced ? "YES" : "NO", |
| | 0 | 596 | | url, |
| | 0 | 597 | | stream.Language ?? "Unknown"); |
| | | 598 | | |
| | 0 | 599 | | builder.AppendLine(line); |
| | | 600 | | } |
| | 0 | 601 | | } |
| | | 602 | | |
| | | 603 | | /// <summary> |
| | | 604 | | /// Appends EXT-X-IMAGE-STREAM-INF playlists for each available trickplay resolution. |
| | | 605 | | /// </summary> |
| | | 606 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 607 | | /// <param name="trickplayResolutions">Dictionary of widths to corresponding tiles info.</param> |
| | | 608 | | /// <param name="builder">StringBuilder to append the field to.</param> |
| | | 609 | | /// <param name="user">Http user context.</param> |
| | | 610 | | private void AddTrickplay(StreamState state, Dictionary<int, TrickplayInfo> trickplayResolutions, StringBuilder buil |
| | | 611 | | { |
| | | 612 | | const string playlistFormat = "#EXT-X-IMAGE-STREAM-INF:BANDWIDTH={0},RESOLUTION={1}x{2},CODECS=\"jpeg\",URI=\"{3 |
| | | 613 | | |
| | 0 | 614 | | foreach (var resolution in trickplayResolutions) |
| | | 615 | | { |
| | 0 | 616 | | var width = resolution.Key; |
| | 0 | 617 | | var trickplayInfo = resolution.Value; |
| | | 618 | | |
| | 0 | 619 | | var url = string.Format( |
| | 0 | 620 | | CultureInfo.InvariantCulture, |
| | 0 | 621 | | "Trickplay/{0}/tiles.m3u8?MediaSourceId={1}&ApiKey={2}", |
| | 0 | 622 | | width.ToString(CultureInfo.InvariantCulture), |
| | 0 | 623 | | state.Request.MediaSourceId, |
| | 0 | 624 | | user.GetToken()); |
| | | 625 | | |
| | 0 | 626 | | builder.AppendFormat( |
| | 0 | 627 | | CultureInfo.InvariantCulture, |
| | 0 | 628 | | playlistFormat, |
| | 0 | 629 | | trickplayInfo.Bandwidth.ToString(CultureInfo.InvariantCulture), |
| | 0 | 630 | | trickplayInfo.Width.ToString(CultureInfo.InvariantCulture), |
| | 0 | 631 | | trickplayInfo.Height.ToString(CultureInfo.InvariantCulture), |
| | 0 | 632 | | url); |
| | | 633 | | |
| | 0 | 634 | | builder.AppendLine(); |
| | | 635 | | } |
| | 0 | 636 | | } |
| | | 637 | | |
| | | 638 | | /// <summary> |
| | | 639 | | /// Get the H.26X level of the output video stream. |
| | | 640 | | /// </summary> |
| | | 641 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 642 | | /// <returns>H.26X level of the output video stream.</returns> |
| | | 643 | | private int? GetOutputVideoCodecLevel(StreamState state) |
| | | 644 | | { |
| | 0 | 645 | | string levelString = string.Empty; |
| | 0 | 646 | | if (EncodingHelper.IsCopyCodec(state.OutputVideoCodec) |
| | 0 | 647 | | && state.VideoStream is not null |
| | 0 | 648 | | && state.VideoStream.Level.HasValue) |
| | | 649 | | { |
| | 0 | 650 | | levelString = state.VideoStream.Level.Value.ToString(CultureInfo.InvariantCulture); |
| | | 651 | | } |
| | | 652 | | else |
| | | 653 | | { |
| | 0 | 654 | | if (string.Equals(state.ActualOutputVideoCodec, "h264", StringComparison.OrdinalIgnoreCase)) |
| | | 655 | | { |
| | 0 | 656 | | levelString = state.GetRequestedLevel(state.ActualOutputVideoCodec) ?? "41"; |
| | 0 | 657 | | levelString = EncodingHelper.NormalizeTranscodingLevel(state, levelString); |
| | | 658 | | } |
| | | 659 | | |
| | 0 | 660 | | if (string.Equals(state.ActualOutputVideoCodec, "h265", StringComparison.OrdinalIgnoreCase) |
| | 0 | 661 | | || string.Equals(state.ActualOutputVideoCodec, "hevc", StringComparison.OrdinalIgnoreCase)) |
| | | 662 | | { |
| | 0 | 663 | | levelString = state.GetRequestedLevel("h265") ?? state.GetRequestedLevel("hevc") ?? "120"; |
| | 0 | 664 | | levelString = EncodingHelper.NormalizeTranscodingLevel(state, levelString); |
| | | 665 | | } |
| | | 666 | | |
| | 0 | 667 | | if (string.Equals(state.ActualOutputVideoCodec, "av1", StringComparison.OrdinalIgnoreCase)) |
| | | 668 | | { |
| | 0 | 669 | | levelString = state.GetRequestedLevel("av1") ?? "19"; |
| | 0 | 670 | | levelString = EncodingHelper.NormalizeTranscodingLevel(state, levelString); |
| | | 671 | | } |
| | | 672 | | } |
| | | 673 | | |
| | 0 | 674 | | if (int.TryParse(levelString, NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsedLevel)) |
| | | 675 | | { |
| | 0 | 676 | | return parsedLevel; |
| | | 677 | | } |
| | | 678 | | |
| | 0 | 679 | | return null; |
| | | 680 | | } |
| | | 681 | | |
| | | 682 | | /// <summary> |
| | | 683 | | /// Get the profile of the output video stream. |
| | | 684 | | /// </summary> |
| | | 685 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 686 | | /// <param name="codec">Video codec.</param> |
| | | 687 | | /// <returns>Profile of the output video stream.</returns> |
| | | 688 | | private string GetOutputVideoCodecProfile(StreamState state, string codec) |
| | | 689 | | { |
| | 0 | 690 | | string profileString = string.Empty; |
| | 0 | 691 | | if (EncodingHelper.IsCopyCodec(state.OutputVideoCodec) |
| | 0 | 692 | | && !string.IsNullOrEmpty(state.VideoStream.Profile)) |
| | | 693 | | { |
| | 0 | 694 | | profileString = state.VideoStream.Profile; |
| | | 695 | | } |
| | 0 | 696 | | else if (!string.IsNullOrEmpty(codec)) |
| | | 697 | | { |
| | 0 | 698 | | profileString = state.GetRequestedProfiles(codec).FirstOrDefault() ?? string.Empty; |
| | 0 | 699 | | if (string.Equals(state.ActualOutputVideoCodec, "h264", StringComparison.OrdinalIgnoreCase)) |
| | | 700 | | { |
| | 0 | 701 | | profileString ??= "high"; |
| | | 702 | | } |
| | | 703 | | |
| | 0 | 704 | | if (string.Equals(state.ActualOutputVideoCodec, "h265", StringComparison.OrdinalIgnoreCase) |
| | 0 | 705 | | || string.Equals(state.ActualOutputVideoCodec, "hevc", StringComparison.OrdinalIgnoreCase) |
| | 0 | 706 | | || string.Equals(state.ActualOutputVideoCodec, "av1", StringComparison.OrdinalIgnoreCase)) |
| | | 707 | | { |
| | 0 | 708 | | profileString ??= "main"; |
| | | 709 | | } |
| | | 710 | | } |
| | | 711 | | |
| | 0 | 712 | | return profileString; |
| | | 713 | | } |
| | | 714 | | |
| | | 715 | | /// <summary> |
| | | 716 | | /// Gets a formatted string of the output audio codec, for use in the CODECS field. |
| | | 717 | | /// </summary> |
| | | 718 | | /// <seealso cref="AppendPlaylistCodecsField(StringBuilder, StreamState)"/> |
| | | 719 | | /// <seealso cref="GetPlaylistVideoCodecs(StreamState, string, int)"/> |
| | | 720 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 721 | | /// <returns>Formatted audio codec string.</returns> |
| | | 722 | | private string GetPlaylistAudioCodecs(StreamState state) |
| | | 723 | | { |
| | 0 | 724 | | if (string.Equals(state.ActualOutputAudioCodec, "aac", StringComparison.OrdinalIgnoreCase)) |
| | | 725 | | { |
| | 0 | 726 | | string? profile = state.GetRequestedProfiles("aac").FirstOrDefault(); |
| | 0 | 727 | | return HlsCodecStringHelpers.GetAACString(profile); |
| | | 728 | | } |
| | | 729 | | |
| | 0 | 730 | | if (string.Equals(state.ActualOutputAudioCodec, "mp3", StringComparison.OrdinalIgnoreCase)) |
| | | 731 | | { |
| | 0 | 732 | | return HlsCodecStringHelpers.GetMP3String(); |
| | | 733 | | } |
| | | 734 | | |
| | 0 | 735 | | if (string.Equals(state.ActualOutputAudioCodec, "ac3", StringComparison.OrdinalIgnoreCase)) |
| | | 736 | | { |
| | 0 | 737 | | return HlsCodecStringHelpers.GetAC3String(); |
| | | 738 | | } |
| | | 739 | | |
| | 0 | 740 | | if (string.Equals(state.ActualOutputAudioCodec, "eac3", StringComparison.OrdinalIgnoreCase)) |
| | | 741 | | { |
| | 0 | 742 | | return HlsCodecStringHelpers.GetEAC3String(); |
| | | 743 | | } |
| | | 744 | | |
| | 0 | 745 | | if (string.Equals(state.ActualOutputAudioCodec, "flac", StringComparison.OrdinalIgnoreCase)) |
| | | 746 | | { |
| | 0 | 747 | | return HlsCodecStringHelpers.GetFLACString(); |
| | | 748 | | } |
| | | 749 | | |
| | 0 | 750 | | if (string.Equals(state.ActualOutputAudioCodec, "alac", StringComparison.OrdinalIgnoreCase)) |
| | | 751 | | { |
| | 0 | 752 | | return HlsCodecStringHelpers.GetALACString(); |
| | | 753 | | } |
| | | 754 | | |
| | 0 | 755 | | if (string.Equals(state.ActualOutputAudioCodec, "opus", StringComparison.OrdinalIgnoreCase)) |
| | | 756 | | { |
| | 0 | 757 | | return HlsCodecStringHelpers.GetOPUSString(); |
| | | 758 | | } |
| | | 759 | | |
| | 0 | 760 | | return string.Empty; |
| | | 761 | | } |
| | | 762 | | |
| | | 763 | | /// <summary> |
| | | 764 | | /// Gets a formatted string of the output video codec, for use in the CODECS field. |
| | | 765 | | /// </summary> |
| | | 766 | | /// <seealso cref="AppendPlaylistCodecsField(StringBuilder, StreamState)"/> |
| | | 767 | | /// <seealso cref="GetPlaylistAudioCodecs(StreamState)"/> |
| | | 768 | | /// <param name="state">StreamState of the current stream.</param> |
| | | 769 | | /// <param name="codec">Video codec.</param> |
| | | 770 | | /// <param name="level">Video level.</param> |
| | | 771 | | /// <returns>Formatted video codec string.</returns> |
| | | 772 | | private string GetPlaylistVideoCodecs(StreamState state, string codec, int level) |
| | | 773 | | { |
| | 0 | 774 | | if (level == 0) |
| | | 775 | | { |
| | | 776 | | // This is 0 when there's no requested level in the device profile |
| | | 777 | | // and the source is not encoded in H.26X or AV1 |
| | 0 | 778 | | _logger.LogError("Got invalid level when building CODECS field for HLS master playlist"); |
| | 0 | 779 | | return string.Empty; |
| | | 780 | | } |
| | | 781 | | |
| | 0 | 782 | | if (string.Equals(codec, "h264", StringComparison.OrdinalIgnoreCase)) |
| | | 783 | | { |
| | 0 | 784 | | string profile = GetOutputVideoCodecProfile(state, "h264"); |
| | 0 | 785 | | return HlsCodecStringHelpers.GetH264String(profile, level); |
| | | 786 | | } |
| | | 787 | | |
| | 0 | 788 | | if (string.Equals(codec, "h265", StringComparison.OrdinalIgnoreCase) |
| | 0 | 789 | | || string.Equals(codec, "hevc", StringComparison.OrdinalIgnoreCase)) |
| | | 790 | | { |
| | 0 | 791 | | string profile = GetOutputVideoCodecProfile(state, "hevc"); |
| | 0 | 792 | | return HlsCodecStringHelpers.GetH265String(profile, level); |
| | | 793 | | } |
| | | 794 | | |
| | 0 | 795 | | if (string.Equals(codec, "av1", StringComparison.OrdinalIgnoreCase)) |
| | | 796 | | { |
| | 0 | 797 | | string profile = GetOutputVideoCodecProfile(state, "av1"); |
| | | 798 | | |
| | | 799 | | // Currently we only transcode to 8 bits AV1 |
| | 0 | 800 | | int bitDepth = 8; |
| | 0 | 801 | | if (EncodingHelper.IsCopyCodec(state.OutputVideoCodec) |
| | 0 | 802 | | && state.VideoStream is not null |
| | 0 | 803 | | && state.VideoStream.BitDepth.HasValue) |
| | | 804 | | { |
| | 0 | 805 | | bitDepth = state.VideoStream.BitDepth.Value; |
| | | 806 | | } |
| | | 807 | | |
| | 0 | 808 | | return HlsCodecStringHelpers.GetAv1String(profile, level, false, bitDepth); |
| | | 809 | | } |
| | | 810 | | |
| | | 811 | | // VP9 HLS is for video remuxing only, everything is probed from the original video |
| | 0 | 812 | | if (string.Equals(codec, "vp9", StringComparison.OrdinalIgnoreCase)) |
| | | 813 | | { |
| | 0 | 814 | | var width = state.VideoStream.Width ?? 0; |
| | 0 | 815 | | var height = state.VideoStream.Height ?? 0; |
| | 0 | 816 | | var framerate = state.VideoStream.ReferenceFrameRate ?? 30; |
| | 0 | 817 | | var bitDepth = state.VideoStream.BitDepth ?? 8; |
| | 0 | 818 | | return HlsCodecStringHelpers.GetVp9String( |
| | 0 | 819 | | width, |
| | 0 | 820 | | height, |
| | 0 | 821 | | state.VideoStream.PixelFormat, |
| | 0 | 822 | | framerate, |
| | 0 | 823 | | bitDepth); |
| | | 824 | | } |
| | | 825 | | |
| | 0 | 826 | | return string.Empty; |
| | | 827 | | } |
| | | 828 | | |
| | | 829 | | private int GetBitrateVariation(int bitrate) |
| | | 830 | | { |
| | | 831 | | // By default, vary by just 50k |
| | 0 | 832 | | var variation = 50000; |
| | | 833 | | |
| | 0 | 834 | | if (bitrate >= 10000000) |
| | | 835 | | { |
| | 0 | 836 | | variation = 2000000; |
| | | 837 | | } |
| | 0 | 838 | | else if (bitrate >= 5000000) |
| | | 839 | | { |
| | 0 | 840 | | variation = 1500000; |
| | | 841 | | } |
| | 0 | 842 | | else if (bitrate >= 3000000) |
| | | 843 | | { |
| | 0 | 844 | | variation = 1000000; |
| | | 845 | | } |
| | 0 | 846 | | else if (bitrate >= 2000000) |
| | | 847 | | { |
| | 0 | 848 | | variation = 500000; |
| | | 849 | | } |
| | 0 | 850 | | else if (bitrate >= 1000000) |
| | | 851 | | { |
| | 0 | 852 | | variation = 300000; |
| | | 853 | | } |
| | 0 | 854 | | else if (bitrate >= 600000) |
| | | 855 | | { |
| | 0 | 856 | | variation = 200000; |
| | | 857 | | } |
| | 0 | 858 | | else if (bitrate >= 400000) |
| | | 859 | | { |
| | 0 | 860 | | variation = 100000; |
| | | 861 | | } |
| | | 862 | | |
| | 0 | 863 | | return variation; |
| | | 864 | | } |
| | | 865 | | |
| | | 866 | | private string ReplaceVideoBitrate(string url, int oldValue, int newValue) |
| | | 867 | | { |
| | 0 | 868 | | return url.Replace( |
| | 0 | 869 | | "videobitrate=" + oldValue.ToString(CultureInfo.InvariantCulture), |
| | 0 | 870 | | "videobitrate=" + newValue.ToString(CultureInfo.InvariantCulture), |
| | 0 | 871 | | StringComparison.OrdinalIgnoreCase); |
| | | 872 | | } |
| | | 873 | | |
| | | 874 | | private string ReplaceProfile(string url, string codec, string oldValue, string newValue) |
| | | 875 | | { |
| | 0 | 876 | | string profileStr = codec + "-profile="; |
| | 0 | 877 | | return url.Replace( |
| | 0 | 878 | | profileStr + oldValue, |
| | 0 | 879 | | profileStr + newValue, |
| | 0 | 880 | | StringComparison.OrdinalIgnoreCase); |
| | | 881 | | } |
| | | 882 | | |
| | | 883 | | private string ReplacePlaylistCodecsField(StringBuilder playlist, StringBuilder oldValue, StringBuilder newValue) |
| | | 884 | | { |
| | 0 | 885 | | var oldPlaylist = playlist.ToString(); |
| | 0 | 886 | | return oldPlaylist.Replace( |
| | 0 | 887 | | oldValue.ToString(), |
| | 0 | 888 | | newValue.ToString(), |
| | 0 | 889 | | StringComparison.Ordinal); |
| | | 890 | | } |
| | | 891 | | } |