| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Globalization; |
| | 4 | | using System.Linq; |
| | 5 | | using Jellyfin.Data.Enums; |
| | 6 | | using Jellyfin.Extensions; |
| | 7 | | using MediaBrowser.Model.Dto; |
| | 8 | | using MediaBrowser.Model.Entities; |
| | 9 | | using MediaBrowser.Model.Extensions; |
| | 10 | | using MediaBrowser.Model.MediaInfo; |
| | 11 | | using MediaBrowser.Model.Session; |
| | 12 | | using Microsoft.Extensions.Logging; |
| | 13 | |
|
| | 14 | | namespace MediaBrowser.Model.Dlna |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Class StreamBuilder. |
| | 18 | | /// </summary> |
| | 19 | | public class StreamBuilder |
| | 20 | | { |
| | 21 | | // Aliases |
| | 22 | | internal const TranscodeReason ContainerReasons = TranscodeReason.ContainerNotSupported | TranscodeReason.Contai |
| | 23 | | internal const TranscodeReason AudioCodecReasons = TranscodeReason.AudioBitrateNotSupported | TranscodeReason.Au |
| | 24 | | internal const TranscodeReason AudioReasons = TranscodeReason.AudioCodecNotSupported | AudioCodecReasons; |
| | 25 | | internal const TranscodeReason VideoCodecReasons = TranscodeReason.VideoResolutionNotSupported | TranscodeReason |
| | 26 | | internal const TranscodeReason VideoReasons = TranscodeReason.VideoCodecNotSupported | VideoCodecReasons; |
| | 27 | | internal const TranscodeReason DirectStreamReasons = AudioReasons | TranscodeReason.ContainerNotSupported | Tran |
| | 28 | |
|
| | 29 | | private readonly ILogger _logger; |
| | 30 | | private readonly ITranscoderSupport _transcoderSupport; |
| 1 | 31 | | private static readonly string[] _supportedHlsVideoCodecs = ["h264", "hevc", "vp9", "av1"]; |
| 1 | 32 | | private static readonly string[] _supportedHlsAudioCodecsTs = ["aac", "ac3", "eac3", "mp3"]; |
| 1 | 33 | | private static readonly string[] _supportedHlsAudioCodecsMp4 = ["aac", "ac3", "eac3", "mp3", "alac", "flac", "op |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Initializes a new instance of the <see cref="StreamBuilder"/> class. |
| | 37 | | /// </summary> |
| | 38 | | /// <param name="transcoderSupport">The <see cref="ITranscoderSupport"/> object.</param> |
| | 39 | | /// <param name="logger">The <see cref="ILogger"/> object.</param> |
| | 40 | | public StreamBuilder(ITranscoderSupport transcoderSupport, ILogger logger) |
| | 41 | | { |
| 278 | 42 | | _transcoderSupport = transcoderSupport; |
| 278 | 43 | | _logger = logger; |
| 278 | 44 | | } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Gets the optimal audio stream. |
| | 48 | | /// </summary> |
| | 49 | | /// <param name="options">The <see cref="MediaOptions"/> object to get the audio stream from.</param> |
| | 50 | | /// <returns>The <see cref="StreamInfo"/> of the optimal audio stream.</returns> |
| | 51 | | public StreamInfo? GetOptimalAudioStream(MediaOptions options) |
| | 52 | | { |
| 0 | 53 | | ValidateMediaOptions(options, false); |
| | 54 | |
|
| 0 | 55 | | List<StreamInfo> streams = []; |
| 0 | 56 | | foreach (var mediaSource in options.MediaSources) |
| | 57 | | { |
| 0 | 58 | | if (!(string.IsNullOrEmpty(options.MediaSourceId) |
| 0 | 59 | | || string.Equals(mediaSource.Id, options.MediaSourceId, StringComparison.OrdinalIgnoreCase))) |
| | 60 | | { |
| | 61 | | continue; |
| | 62 | | } |
| | 63 | |
|
| 0 | 64 | | StreamInfo? streamInfo = GetOptimalAudioStream(mediaSource, options); |
| 0 | 65 | | if (streamInfo is not null) |
| | 66 | | { |
| 0 | 67 | | streamInfo.DeviceId = options.DeviceId; |
| 0 | 68 | | streamInfo.DeviceProfileId = options.Profile.Id?.ToString("N", CultureInfo.InvariantCulture); |
| 0 | 69 | | streams.Add(streamInfo); |
| | 70 | | } |
| | 71 | | } |
| | 72 | |
|
| 0 | 73 | | return GetOptimalStream(streams, options.GetMaxBitrate(true) ?? 0); |
| | 74 | | } |
| | 75 | |
|
| | 76 | | private StreamInfo? GetOptimalAudioStream(MediaSourceInfo item, MediaOptions options) |
| | 77 | | { |
| 0 | 78 | | var playlistItem = new StreamInfo |
| 0 | 79 | | { |
| 0 | 80 | | ItemId = options.ItemId, |
| 0 | 81 | | MediaType = DlnaProfileType.Audio, |
| 0 | 82 | | MediaSource = item, |
| 0 | 83 | | RunTimeTicks = item.RunTimeTicks, |
| 0 | 84 | | Context = options.Context, |
| 0 | 85 | | DeviceProfile = options.Profile |
| 0 | 86 | | }; |
| | 87 | |
|
| 0 | 88 | | if (options.ForceDirectPlay) |
| | 89 | | { |
| 0 | 90 | | playlistItem.PlayMethod = PlayMethod.DirectPlay; |
| 0 | 91 | | playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, |
| 0 | 92 | | return playlistItem; |
| | 93 | | } |
| | 94 | |
|
| 0 | 95 | | if (options.ForceDirectStream) |
| | 96 | | { |
| 0 | 97 | | playlistItem.PlayMethod = PlayMethod.DirectStream; |
| 0 | 98 | | playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, |
| 0 | 99 | | return playlistItem; |
| | 100 | | } |
| | 101 | |
|
| 0 | 102 | | MediaStream audioStream = item.GetDefaultAudioStream(null); |
| | 103 | |
|
| 0 | 104 | | ArgumentNullException.ThrowIfNull(audioStream); |
| | 105 | |
|
| 0 | 106 | | var directPlayInfo = GetAudioDirectPlayProfile(item, audioStream, options); |
| | 107 | |
|
| 0 | 108 | | var directPlayMethod = directPlayInfo.PlayMethod; |
| 0 | 109 | | var transcodeReasons = directPlayInfo.TranscodeReasons; |
| | 110 | |
|
| 0 | 111 | | if (directPlayMethod is PlayMethod.DirectPlay) |
| | 112 | | { |
| 0 | 113 | | var audioFailureReasons = GetCompatibilityAudioCodec(options, item, item.Container, audioStream, null, f |
| 0 | 114 | | transcodeReasons |= audioFailureReasons; |
| | 115 | |
|
| 0 | 116 | | if (audioFailureReasons == 0) |
| | 117 | | { |
| 0 | 118 | | playlistItem.PlayMethod = directPlayMethod.Value; |
| 0 | 119 | | playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profi |
| | 120 | |
|
| 0 | 121 | | return playlistItem; |
| | 122 | | } |
| | 123 | | } |
| | 124 | |
|
| 0 | 125 | | if (directPlayMethod is PlayMethod.DirectStream) |
| | 126 | | { |
| 0 | 127 | | var remuxContainer = item.TranscodingContainer ?? "ts"; |
| 0 | 128 | | string[] supportedHlsContainers = ["ts", "mp4"]; |
| | 129 | | // If the container specified for the profile is an HLS supported container, use that container instead, |
| | 130 | | // The client should be responsible to ensure this container is compatible |
| 0 | 131 | | remuxContainer = Array.Exists(supportedHlsContainers, element => string.Equals(element, directPlayInfo.P |
| | 132 | | bool codeIsSupported; |
| 0 | 133 | | if (item.TranscodingSubProtocol == MediaStreamProtocol.hls) |
| | 134 | | { |
| | 135 | | // Enforce HLS audio codec restrictions |
| 0 | 136 | | if (string.Equals(remuxContainer, "mp4", StringComparison.OrdinalIgnoreCase)) |
| | 137 | | { |
| 0 | 138 | | codeIsSupported = _supportedHlsAudioCodecsMp4.Contains(directPlayInfo.Profile?.AudioCodec ?? dir |
| | 139 | | } |
| | 140 | | else |
| | 141 | | { |
| 0 | 142 | | codeIsSupported = _supportedHlsAudioCodecsTs.Contains(directPlayInfo.Profile?.AudioCodec ?? dire |
| | 143 | | } |
| | 144 | | } |
| | 145 | | else |
| | 146 | | { |
| | 147 | | // Let's assume the client has given a correct container for http |
| 0 | 148 | | codeIsSupported = true; |
| | 149 | | } |
| | 150 | |
|
| 0 | 151 | | if (codeIsSupported) |
| | 152 | | { |
| 0 | 153 | | playlistItem.PlayMethod = directPlayMethod.Value; |
| 0 | 154 | | playlistItem.Container = remuxContainer; |
| 0 | 155 | | playlistItem.TranscodeReasons = transcodeReasons; |
| 0 | 156 | | playlistItem.SubProtocol = item.TranscodingSubProtocol; |
| 0 | 157 | | item.TranscodingContainer = remuxContainer; |
| 0 | 158 | | return playlistItem; |
| | 159 | | } |
| | 160 | |
|
| 0 | 161 | | transcodeReasons |= TranscodeReason.AudioCodecNotSupported; |
| 0 | 162 | | playlistItem.TranscodeReasons = transcodeReasons; |
| | 163 | | } |
| | 164 | |
|
| 0 | 165 | | TranscodingProfile? transcodingProfile = null; |
| 0 | 166 | | foreach (var tcProfile in options.Profile.TranscodingProfiles) |
| | 167 | | { |
| 0 | 168 | | if (tcProfile.Type == playlistItem.MediaType |
| 0 | 169 | | && tcProfile.Context == options.Context |
| 0 | 170 | | && _transcoderSupport.CanEncodeToAudioCodec(tcProfile.AudioCodec ?? tcProfile.Container)) |
| | 171 | | { |
| 0 | 172 | | transcodingProfile = tcProfile; |
| 0 | 173 | | break; |
| | 174 | | } |
| | 175 | | } |
| | 176 | |
|
| 0 | 177 | | if (transcodingProfile is not null) |
| | 178 | | { |
| 0 | 179 | | if (!item.SupportsTranscoding) |
| | 180 | | { |
| 0 | 181 | | return null; |
| | 182 | | } |
| | 183 | |
|
| 0 | 184 | | SetStreamInfoOptionsFromTranscodingProfile(item, playlistItem, transcodingProfile); |
| | 185 | |
|
| 0 | 186 | | var inputAudioChannels = audioStream.Channels; |
| 0 | 187 | | var inputAudioBitrate = audioStream.BitRate; |
| 0 | 188 | | var inputAudioSampleRate = audioStream.SampleRate; |
| 0 | 189 | | var inputAudioBitDepth = audioStream.BitDepth; |
| | 190 | |
|
| 0 | 191 | | var audioTranscodingConditions = GetProfileConditionsForAudio(options.Profile.CodecProfiles, transcoding |
| 0 | 192 | | ApplyTranscodingConditions(playlistItem, audioTranscodingConditions, null, true, true); |
| | 193 | |
|
| | 194 | | // Honor requested max channels |
| 0 | 195 | | playlistItem.GlobalMaxAudioChannels = options.MaxAudioChannels; |
| | 196 | |
|
| 0 | 197 | | var configuredBitrate = options.GetMaxBitrate(true); |
| | 198 | |
|
| 0 | 199 | | long transcodingBitrate = options.AudioTranscodingBitrate |
| 0 | 200 | | ?? (options.Context == EncodingContext.Streaming ? options.Profile.MusicStreamingTranscodingBitrate |
| 0 | 201 | | ?? configuredBitrate |
| 0 | 202 | | ?? 128000; |
| | 203 | |
|
| 0 | 204 | | if (configuredBitrate.HasValue) |
| | 205 | | { |
| 0 | 206 | | transcodingBitrate = Math.Min(configuredBitrate.Value, transcodingBitrate); |
| | 207 | | } |
| | 208 | |
|
| 0 | 209 | | var longBitrate = Math.Min(transcodingBitrate, playlistItem.AudioBitrate ?? transcodingBitrate); |
| 0 | 210 | | playlistItem.AudioBitrate = longBitrate > int.MaxValue ? int.MaxValue : Convert.ToInt32(longBitrate); |
| | 211 | |
|
| | 212 | | // Pure audio transcoding does not support comma separated list of transcoding codec at the moment. |
| | 213 | | // So just use the AudioCodec as is would be safe enough as the _transcoderSupport.CanEncodeToAudioCodec |
| | 214 | | // would fail so this profile will not even be picked up. |
| 0 | 215 | | if (playlistItem.AudioCodecs.Count == 0 && !string.IsNullOrWhiteSpace(transcodingProfile.AudioCodec)) |
| | 216 | | { |
| 0 | 217 | | playlistItem.AudioCodecs = [transcodingProfile.AudioCodec]; |
| | 218 | | } |
| | 219 | | } |
| | 220 | |
|
| 0 | 221 | | playlistItem.TranscodeReasons = transcodeReasons; |
| 0 | 222 | | return playlistItem; |
| | 223 | | } |
| | 224 | |
|
| | 225 | | /// <summary> |
| | 226 | | /// Gets the optimal video stream. |
| | 227 | | /// </summary> |
| | 228 | | /// <param name="options">The <see cref="MediaOptions"/> object to get the video stream from.</param> |
| | 229 | | /// <returns>The <see cref="StreamInfo"/> of the optimal video stream.</returns> |
| | 230 | | public StreamInfo? GetOptimalVideoStream(MediaOptions options) |
| | 231 | | { |
| 278 | 232 | | ValidateMediaOptions(options, true); |
| | 233 | |
|
| 278 | 234 | | var mediaSources = string.IsNullOrEmpty(options.MediaSourceId) |
| 278 | 235 | | ? options.MediaSources |
| 278 | 236 | | : options.MediaSources.Where(x => string.Equals(x.Id, options.MediaSourceId, StringComparison.OrdinalIgn |
| | 237 | |
|
| 278 | 238 | | List<StreamInfo> streams = []; |
| 1112 | 239 | | foreach (var mediaSourceInfo in mediaSources) |
| | 240 | | { |
| 278 | 241 | | var streamInfo = BuildVideoItem(mediaSourceInfo, options); |
| 278 | 242 | | if (streamInfo is not null) |
| | 243 | | { |
| 278 | 244 | | streams.Add(streamInfo); |
| | 245 | | } |
| | 246 | | } |
| | 247 | |
|
| 1112 | 248 | | foreach (var stream in streams) |
| | 249 | | { |
| 278 | 250 | | stream.DeviceId = options.DeviceId; |
| 278 | 251 | | stream.DeviceProfileId = options.Profile.Id?.ToString("N", CultureInfo.InvariantCulture); |
| | 252 | | } |
| | 253 | |
|
| 278 | 254 | | return GetOptimalStream(streams, options.GetMaxBitrate(false) ?? 0); |
| | 255 | | } |
| | 256 | |
|
| | 257 | | private static StreamInfo? GetOptimalStream(List<StreamInfo> streams, long maxBitrate) |
| 278 | 258 | | => SortMediaSources(streams, maxBitrate).FirstOrDefault(); |
| | 259 | |
|
| | 260 | | private static IOrderedEnumerable<StreamInfo> SortMediaSources(List<StreamInfo> streams, long maxBitrate) |
| | 261 | | { |
| 278 | 262 | | return streams.OrderBy(i => |
| 278 | 263 | | { |
| 278 | 264 | | // Nothing beats direct playing a file |
| 278 | 265 | | if (i.PlayMethod == PlayMethod.DirectPlay && i.MediaSource?.Protocol == MediaProtocol.File) |
| 278 | 266 | | { |
| 278 | 267 | | return 0; |
| 278 | 268 | | } |
| 278 | 269 | |
|
| 278 | 270 | | return 1; |
| 278 | 271 | | }).ThenBy(i => |
| 278 | 272 | | { |
| 278 | 273 | | switch (i.PlayMethod) |
| 278 | 274 | | { |
| 278 | 275 | | // Let's assume direct streaming a file is just as desirable as direct playing a remote url |
| 278 | 276 | | case PlayMethod.DirectStream: |
| 278 | 277 | | case PlayMethod.DirectPlay: |
| 278 | 278 | | return 0; |
| 278 | 279 | | default: |
| 278 | 280 | | return 1; |
| 278 | 281 | | } |
| 278 | 282 | | }).ThenBy(i => |
| 278 | 283 | | { |
| 278 | 284 | | switch (i.MediaSource?.Protocol) |
| 278 | 285 | | { |
| 278 | 286 | | case MediaProtocol.File: |
| 278 | 287 | | return 0; |
| 278 | 288 | | default: |
| 278 | 289 | | return 1; |
| 278 | 290 | | } |
| 278 | 291 | | }).ThenBy(i => |
| 278 | 292 | | { |
| 278 | 293 | | if (maxBitrate > 0) |
| 278 | 294 | | { |
| 278 | 295 | | if (i.MediaSource?.Bitrate is not null) |
| 278 | 296 | | { |
| 278 | 297 | | return Math.Abs(i.MediaSource.Bitrate.Value - maxBitrate); |
| 278 | 298 | | } |
| 278 | 299 | | } |
| 278 | 300 | |
|
| 278 | 301 | | return 0; |
| 278 | 302 | | }).ThenBy(streams.IndexOf); |
| | 303 | | } |
| | 304 | |
|
| | 305 | | private static TranscodeReason GetTranscodeReasonForFailedCondition(ProfileCondition condition) |
| | 306 | | { |
| 247 | 307 | | switch (condition.Property) |
| | 308 | | { |
| | 309 | | case ProfileConditionValue.AudioBitrate: |
| 0 | 310 | | return TranscodeReason.AudioBitrateNotSupported; |
| | 311 | |
|
| | 312 | | case ProfileConditionValue.AudioChannels: |
| 122 | 313 | | return TranscodeReason.AudioChannelsNotSupported; |
| | 314 | |
|
| | 315 | | case ProfileConditionValue.AudioProfile: |
| 0 | 316 | | return TranscodeReason.AudioProfileNotSupported; |
| | 317 | |
|
| | 318 | | case ProfileConditionValue.AudioSampleRate: |
| 0 | 319 | | return TranscodeReason.AudioSampleRateNotSupported; |
| | 320 | |
|
| | 321 | | case ProfileConditionValue.Has64BitOffsets: |
| | 322 | | // TODO |
| 0 | 323 | | return 0; |
| | 324 | |
|
| | 325 | | case ProfileConditionValue.Height: |
| 0 | 326 | | return TranscodeReason.VideoResolutionNotSupported; |
| | 327 | |
|
| | 328 | | case ProfileConditionValue.IsAnamorphic: |
| 0 | 329 | | return TranscodeReason.AnamorphicVideoNotSupported; |
| | 330 | |
|
| | 331 | | case ProfileConditionValue.IsAvc: |
| | 332 | | // TODO |
| 0 | 333 | | return 0; |
| | 334 | |
|
| | 335 | | case ProfileConditionValue.IsInterlaced: |
| 0 | 336 | | return TranscodeReason.InterlacedVideoNotSupported; |
| | 337 | |
|
| | 338 | | case ProfileConditionValue.IsSecondaryAudio: |
| 35 | 339 | | return TranscodeReason.SecondaryAudioNotSupported; |
| | 340 | |
|
| | 341 | | case ProfileConditionValue.NumStreams: |
| 2 | 342 | | return TranscodeReason.StreamCountExceedsLimit; |
| | 343 | |
|
| | 344 | | case ProfileConditionValue.NumAudioStreams: |
| | 345 | | // TODO |
| 0 | 346 | | return 0; |
| | 347 | |
|
| | 348 | | case ProfileConditionValue.NumVideoStreams: |
| | 349 | | // TODO |
| 0 | 350 | | return 0; |
| | 351 | |
|
| | 352 | | case ProfileConditionValue.PacketLength: |
| | 353 | | // TODO |
| 0 | 354 | | return 0; |
| | 355 | |
|
| | 356 | | case ProfileConditionValue.RefFrames: |
| 0 | 357 | | return TranscodeReason.RefFramesNotSupported; |
| | 358 | |
|
| | 359 | | case ProfileConditionValue.VideoBitDepth: |
| 0 | 360 | | return TranscodeReason.VideoBitDepthNotSupported; |
| | 361 | |
|
| | 362 | | case ProfileConditionValue.AudioBitDepth: |
| 0 | 363 | | return TranscodeReason.AudioBitDepthNotSupported; |
| | 364 | |
|
| | 365 | | case ProfileConditionValue.VideoBitrate: |
| 4 | 366 | | return TranscodeReason.VideoBitrateNotSupported; |
| | 367 | |
|
| | 368 | | case ProfileConditionValue.VideoCodecTag: |
| 18 | 369 | | return TranscodeReason.VideoCodecTagNotSupported; |
| | 370 | |
|
| | 371 | | case ProfileConditionValue.VideoFramerate: |
| 0 | 372 | | return TranscodeReason.VideoFramerateNotSupported; |
| | 373 | |
|
| | 374 | | case ProfileConditionValue.VideoLevel: |
| 13 | 375 | | return TranscodeReason.VideoLevelNotSupported; |
| | 376 | |
|
| | 377 | | case ProfileConditionValue.VideoProfile: |
| 25 | 378 | | return TranscodeReason.VideoProfileNotSupported; |
| | 379 | |
|
| | 380 | | case ProfileConditionValue.VideoRangeType: |
| 28 | 381 | | return TranscodeReason.VideoRangeTypeNotSupported; |
| | 382 | |
|
| | 383 | | case ProfileConditionValue.VideoTimestamp: |
| | 384 | | // TODO |
| 0 | 385 | | return 0; |
| | 386 | |
|
| | 387 | | case ProfileConditionValue.Width: |
| 0 | 388 | | return TranscodeReason.VideoResolutionNotSupported; |
| | 389 | |
|
| | 390 | | default: |
| 0 | 391 | | return 0; |
| | 392 | | } |
| | 393 | | } |
| | 394 | |
|
| | 395 | | /// <summary> |
| | 396 | | /// Normalizes input container. |
| | 397 | | /// </summary> |
| | 398 | | /// <param name="inputContainer">The input container.</param> |
| | 399 | | /// <param name="profile">The <see cref="DeviceProfile"/>.</param> |
| | 400 | | /// <param name="type">The <see cref="DlnaProfileType"/>.</param> |
| | 401 | | /// <param name="playProfile">The <see cref="DirectPlayProfile"/> object to get the video stream from.</param> |
| | 402 | | /// <returns>The normalized input container.</returns> |
| | 403 | | public static string? NormalizeMediaSourceFormatIntoSingleContainer(string inputContainer, DeviceProfile? profil |
| | 404 | | { |
| | 405 | | // If the source is Live TV the inputContainer will be null until the mediasource is probed on first access |
| 402 | 406 | | if (profile is null || string.IsNullOrEmpty(inputContainer) || !inputContainer.Contains(',', StringCompariso |
| | 407 | | { |
| 94 | 408 | | return inputContainer; |
| | 409 | | } |
| | 410 | |
|
| 308 | 411 | | var formats = ContainerHelper.Split(inputContainer); |
| 308 | 412 | | var playProfiles = playProfile is null ? profile.DirectPlayProfiles : [playProfile]; |
| 1361 | 413 | | foreach (var format in formats) |
| | 414 | | { |
| 2593 | 415 | | foreach (var directPlayProfile in playProfiles) |
| | 416 | | { |
| 924 | 417 | | if (directPlayProfile.Type != type) |
| | 418 | | { |
| | 419 | | continue; |
| | 420 | | } |
| | 421 | |
|
| 696 | 422 | | if (directPlayProfile.SupportsContainer(format)) |
| | 423 | | { |
| 291 | 424 | | return format; |
| | 425 | | } |
| | 426 | | } |
| | 427 | | } |
| | 428 | |
|
| 17 | 429 | | return inputContainer; |
| | 430 | | } |
| | 431 | |
|
| | 432 | | private (DirectPlayProfile? Profile, PlayMethod? PlayMethod, TranscodeReason TranscodeReasons) GetAudioDirectPla |
| | 433 | | { |
| 0 | 434 | | var directPlayProfile = options.Profile.DirectPlayProfiles |
| 0 | 435 | | .FirstOrDefault(x => x.Type == DlnaProfileType.Audio && IsAudioDirectPlaySupported(x, item, audioStream) |
| | 436 | |
|
| 0 | 437 | | TranscodeReason transcodeReasons = 0; |
| 0 | 438 | | if (directPlayProfile is null) |
| | 439 | | { |
| 0 | 440 | | _logger.LogDebug( |
| 0 | 441 | | "Profile: {0}, No audio direct play profiles found for {1} with codec {2}", |
| 0 | 442 | | options.Profile.Name ?? "Unknown Profile", |
| 0 | 443 | | item.Path ?? "Unknown path", |
| 0 | 444 | | audioStream.Codec ?? "Unknown codec"); |
| | 445 | |
|
| 0 | 446 | | var directStreamProfile = options.Profile.DirectPlayProfiles |
| 0 | 447 | | .FirstOrDefault(x => x.Type == DlnaProfileType.Audio && IsAudioDirectStreamSupported(x, item, audioS |
| | 448 | |
|
| 0 | 449 | | if (directStreamProfile is not null) |
| | 450 | | { |
| 0 | 451 | | directPlayProfile = directStreamProfile; |
| 0 | 452 | | transcodeReasons |= TranscodeReason.ContainerNotSupported; |
| | 453 | | } |
| | 454 | | else |
| | 455 | | { |
| 0 | 456 | | return (null, null, GetTranscodeReasonsFromDirectPlayProfile(item, null, audioStream, options.Profil |
| | 457 | | } |
| | 458 | | } |
| | 459 | |
|
| | 460 | | // The profile describes what the device supports |
| | 461 | | // If device requirements are satisfied then allow both direct stream and direct play |
| | 462 | | // Note: As of 10.10 codebase, SupportsDirectPlay is always true because the MediaSourceInfo initializes thi |
| | 463 | | // Need to check additionally for current transcode reasons |
| 0 | 464 | | if (item.SupportsDirectPlay && transcodeReasons == 0) |
| | 465 | | { |
| 0 | 466 | | if (!IsBitrateLimitExceeded(item, options.GetMaxBitrate(true) ?? 0)) |
| | 467 | | { |
| 0 | 468 | | if (options.EnableDirectPlay) |
| | 469 | | { |
| 0 | 470 | | return (directPlayProfile, PlayMethod.DirectPlay, 0); |
| | 471 | | } |
| | 472 | | } |
| | 473 | | else |
| | 474 | | { |
| 0 | 475 | | transcodeReasons |= TranscodeReason.ContainerBitrateExceedsLimit; |
| | 476 | | } |
| | 477 | | } |
| | 478 | |
|
| | 479 | | // While options takes the network and other factors into account. Only applies to direct stream |
| 0 | 480 | | if (item.SupportsDirectStream) |
| | 481 | | { |
| 0 | 482 | | if (!IsBitrateLimitExceeded(item, options.GetMaxBitrate(true) ?? 0)) |
| | 483 | | { |
| | 484 | | // Note: as of 10.10 codebase, the options.EnableDirectStream is always false due to |
| | 485 | | // "direct-stream http streaming is currently broken" |
| | 486 | | // Don't check that option for audio as we always assume that is supported |
| 0 | 487 | | if (transcodeReasons == TranscodeReason.ContainerNotSupported) |
| | 488 | | { |
| 0 | 489 | | return (directPlayProfile, PlayMethod.DirectStream, transcodeReasons); |
| | 490 | | } |
| | 491 | | } |
| | 492 | | else |
| | 493 | | { |
| 0 | 494 | | transcodeReasons |= TranscodeReason.ContainerBitrateExceedsLimit; |
| | 495 | | } |
| | 496 | | } |
| | 497 | |
|
| 0 | 498 | | return (directPlayProfile, null, transcodeReasons); |
| | 499 | | } |
| | 500 | |
|
| | 501 | | private static TranscodeReason GetTranscodeReasonsFromDirectPlayProfile(MediaSourceInfo item, MediaStream? video |
| | 502 | | { |
| 0 | 503 | | var mediaType = videoStream is null ? DlnaProfileType.Audio : DlnaProfileType.Video; |
| | 504 | |
|
| 0 | 505 | | var containerSupported = false; |
| 0 | 506 | | var audioSupported = false; |
| 0 | 507 | | var videoSupported = false; |
| | 508 | |
|
| 0 | 509 | | foreach (var profile in directPlayProfiles) |
| | 510 | | { |
| | 511 | | // Check container type |
| 0 | 512 | | if (profile.Type == mediaType && profile.SupportsContainer(item.Container)) |
| | 513 | | { |
| 0 | 514 | | containerSupported = true; |
| | 515 | |
|
| 0 | 516 | | videoSupported = videoStream is null || profile.SupportsVideoCodec(videoStream.Codec); |
| | 517 | |
|
| 0 | 518 | | audioSupported = audioStream is null || profile.SupportsAudioCodec(audioStream.Codec); |
| | 519 | |
|
| 0 | 520 | | if (videoSupported && audioSupported) |
| | 521 | | { |
| 0 | 522 | | break; |
| | 523 | | } |
| | 524 | | } |
| | 525 | | } |
| | 526 | |
|
| 0 | 527 | | TranscodeReason reasons = 0; |
| 0 | 528 | | if (!containerSupported) |
| | 529 | | { |
| 0 | 530 | | reasons |= TranscodeReason.ContainerNotSupported; |
| | 531 | | } |
| | 532 | |
|
| 0 | 533 | | if (!videoSupported) |
| | 534 | | { |
| 0 | 535 | | reasons |= TranscodeReason.VideoCodecNotSupported; |
| | 536 | | } |
| | 537 | |
|
| 0 | 538 | | if (!audioSupported) |
| | 539 | | { |
| 0 | 540 | | reasons |= TranscodeReason.AudioCodecNotSupported; |
| | 541 | | } |
| | 542 | |
|
| 0 | 543 | | return reasons; |
| | 544 | | } |
| | 545 | |
|
| | 546 | | private static int? GetDefaultSubtitleStreamIndex(MediaSourceInfo item, SubtitleProfile[] subtitleProfiles) |
| | 547 | | { |
| 175 | 548 | | int highestScore = -1; |
| 1788 | 549 | | foreach (var stream in item.MediaStreams) |
| | 550 | | { |
| 719 | 551 | | if (stream.Type == MediaStreamType.Subtitle |
| 719 | 552 | | && stream.Score.HasValue |
| 719 | 553 | | && stream.Score.Value > highestScore) |
| | 554 | | { |
| 144 | 555 | | highestScore = stream.Score.Value; |
| | 556 | | } |
| | 557 | | } |
| | 558 | |
|
| 175 | 559 | | List<MediaStream> topStreams = []; |
| 1788 | 560 | | foreach (var stream in item.MediaStreams) |
| | 561 | | { |
| 719 | 562 | | if (stream.Type == MediaStreamType.Subtitle && stream.Score.HasValue && stream.Score.Value == highestSco |
| | 563 | | { |
| 321 | 564 | | topStreams.Add(stream); |
| | 565 | | } |
| | 566 | | } |
| | 567 | |
|
| | 568 | | // If multiple streams have an equal score, try to pick the most efficient one |
| 175 | 569 | | if (topStreams.Count > 1) |
| | 570 | | { |
| 378 | 571 | | foreach (var stream in topStreams) |
| | 572 | | { |
| 1464 | 573 | | foreach (var profile in subtitleProfiles) |
| | 574 | | { |
| 549 | 575 | | if (profile.Method == SubtitleDeliveryMethod.External && string.Equals(profile.Format, stream.Co |
| | 576 | | { |
| 0 | 577 | | return stream.Index; |
| | 578 | | } |
| | 579 | | } |
| | 580 | | } |
| | 581 | | } |
| | 582 | |
|
| | 583 | | // If no optimization panned out, just use the original default |
| 175 | 584 | | return item.DefaultSubtitleStreamIndex; |
| 0 | 585 | | } |
| | 586 | |
|
| | 587 | | private static void SetStreamInfoOptionsFromTranscodingProfile(MediaSourceInfo item, StreamInfo playlistItem, Tr |
| | 588 | | { |
| 146 | 589 | | var container = transcodingProfile.Container; |
| 146 | 590 | | var protocol = transcodingProfile.Protocol; |
| | 591 | |
|
| 146 | 592 | | item.TranscodingContainer = container; |
| 146 | 593 | | item.TranscodingSubProtocol = protocol; |
| | 594 | |
|
| 146 | 595 | | if (playlistItem.PlayMethod == PlayMethod.Transcode) |
| | 596 | | { |
| 146 | 597 | | playlistItem.Container = container; |
| 146 | 598 | | playlistItem.SubProtocol = protocol; |
| | 599 | | } |
| | 600 | |
|
| 146 | 601 | | playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo; |
| 146 | 602 | | if (int.TryParse(transcodingProfile.MaxAudioChannels, CultureInfo.InvariantCulture, out int transcodingMaxAu |
| | 603 | | { |
| 133 | 604 | | playlistItem.TranscodingMaxAudioChannels = transcodingMaxAudioChannels; |
| | 605 | | } |
| | 606 | |
|
| 146 | 607 | | playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength; |
| | 608 | |
|
| 146 | 609 | | playlistItem.CopyTimestamps = transcodingProfile.CopyTimestamps; |
| 146 | 610 | | playlistItem.EnableSubtitlesInManifest = transcodingProfile.EnableSubtitlesInManifest; |
| 146 | 611 | | playlistItem.EnableMpegtsM2TsMode = transcodingProfile.EnableMpegtsM2TsMode; |
| | 612 | |
|
| 146 | 613 | | playlistItem.BreakOnNonKeyFrames = transcodingProfile.BreakOnNonKeyFrames; |
| 146 | 614 | | playlistItem.EnableAudioVbrEncoding = transcodingProfile.EnableAudioVbrEncoding; |
| | 615 | |
|
| 146 | 616 | | if (transcodingProfile.MinSegments > 0) |
| | 617 | | { |
| 102 | 618 | | playlistItem.MinSegments = transcodingProfile.MinSegments; |
| | 619 | | } |
| | 620 | |
|
| 146 | 621 | | if (transcodingProfile.SegmentLength > 0) |
| | 622 | | { |
| 0 | 623 | | playlistItem.SegmentLength = transcodingProfile.SegmentLength; |
| | 624 | | } |
| 146 | 625 | | } |
| | 626 | |
|
| | 627 | | private static void SetStreamInfoOptionsFromDirectPlayProfile(MediaOptions options, MediaSourceInfo item, Stream |
| | 628 | | { |
| 0 | 629 | | var container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileTy |
| 0 | 630 | | var protocol = MediaStreamProtocol.http; |
| | 631 | |
|
| 0 | 632 | | item.TranscodingContainer = container; |
| 0 | 633 | | item.TranscodingSubProtocol = protocol; |
| | 634 | |
|
| 0 | 635 | | playlistItem.Container = container; |
| 0 | 636 | | playlistItem.SubProtocol = protocol; |
| | 637 | |
|
| 0 | 638 | | playlistItem.VideoCodecs = [item.VideoStream.Codec]; |
| 0 | 639 | | playlistItem.AudioCodecs = ContainerHelper.Split(directPlayProfile?.AudioCodec); |
| 0 | 640 | | } |
| | 641 | |
|
| | 642 | | private StreamInfo BuildVideoItem(MediaSourceInfo item, MediaOptions options) |
| | 643 | | { |
| 278 | 644 | | ArgumentNullException.ThrowIfNull(item); |
| | 645 | |
|
| 278 | 646 | | StreamInfo playlistItem = new StreamInfo |
| 278 | 647 | | { |
| 278 | 648 | | ItemId = options.ItemId, |
| 278 | 649 | | MediaType = DlnaProfileType.Video, |
| 278 | 650 | | MediaSource = item, |
| 278 | 651 | | RunTimeTicks = item.RunTimeTicks, |
| 278 | 652 | | Context = options.Context, |
| 278 | 653 | | DeviceProfile = options.Profile, |
| 278 | 654 | | SubtitleStreamIndex = options.SubtitleStreamIndex ?? GetDefaultSubtitleStreamIndex(item, options.Profile |
| 278 | 655 | | AlwaysBurnInSubtitleWhenTranscoding = options.AlwaysBurnInSubtitleWhenTranscoding |
| 278 | 656 | | }; |
| | 657 | |
|
| 278 | 658 | | var subtitleStream = playlistItem.SubtitleStreamIndex.HasValue ? item.GetMediaStream(MediaStreamType.Subtitl |
| | 659 | |
|
| 278 | 660 | | var audioStream = item.GetDefaultAudioStream(options.AudioStreamIndex ?? item.DefaultAudioStreamIndex); |
| 278 | 661 | | if (audioStream is not null) |
| | 662 | | { |
| 277 | 663 | | playlistItem.AudioStreamIndex = audioStream.Index; |
| | 664 | | } |
| | 665 | |
|
| | 666 | | // Collect candidate audio streams |
| 278 | 667 | | ICollection<MediaStream> candidateAudioStreams = audioStream is null ? [] : [audioStream]; |
| | 668 | | // When the index is explicitly required by client or the default is specified by user, don't do any stream |
| 278 | 669 | | if (!item.DefaultAudioIndexSource.HasFlag(AudioIndexSource.User) && (options.AudioStreamIndex is null or < 0 |
| | 670 | | { |
| | 671 | | // When user has no preferences allow stream selection on all streams. |
| 175 | 672 | | if (item.DefaultAudioIndexSource == AudioIndexSource.None && audioStream is not null) |
| | 673 | | { |
| 174 | 674 | | candidateAudioStreams = item.MediaStreams.Where(stream => stream.Type == MediaStreamType.Audio).ToAr |
| 174 | 675 | | if (audioStream.IsDefault) |
| | 676 | | { |
| | 677 | | // If default is picked, only allow selection within default streams. |
| 174 | 678 | | candidateAudioStreams = candidateAudioStreams.Where(stream => stream.IsDefault).ToArray(); |
| | 679 | | } |
| | 680 | | } |
| | 681 | |
|
| 175 | 682 | | if (item.DefaultAudioIndexSource.HasFlag(AudioIndexSource.Language)) |
| | 683 | | { |
| | 684 | | // If user has language preference, only allow stream selection within the same language. |
| 0 | 685 | | candidateAudioStreams = item.MediaStreams.Where(stream => stream.Type == MediaStreamType.Audio && st |
| 0 | 686 | | if (item.DefaultAudioIndexSource.HasFlag(AudioIndexSource.Default)) |
| | 687 | | { |
| 0 | 688 | | var defaultStreamsInPreferredLanguage = candidateAudioStreams.Where(stream => stream.IsDefault). |
| | 689 | |
|
| | 690 | | // If the user also prefers default streams, try limit selection within default tracks in the sa |
| | 691 | | // If there is no default stream in the preferred language, allow selection on all default strea |
| 0 | 692 | | candidateAudioStreams = defaultStreamsInPreferredLanguage.Length > 0 |
| 0 | 693 | | ? defaultStreamsInPreferredLanguage |
| 0 | 694 | | : item.MediaStreams.Where(stream => stream.Type == MediaStreamType.Audio && stream.IsDefault |
| | 695 | | } |
| | 696 | | } |
| 175 | 697 | | else if (item.DefaultAudioIndexSource.HasFlag(AudioIndexSource.Default)) |
| | 698 | | { |
| | 699 | | // If user prefers default streams, only allow stream selection on default streams. |
| 0 | 700 | | candidateAudioStreams = item.MediaStreams.Where(stream => stream.Type == MediaStreamType.Audio && st |
| | 701 | | } |
| | 702 | | } |
| | 703 | |
|
| 278 | 704 | | var videoStream = item.VideoStream; |
| | 705 | |
|
| 278 | 706 | | var bitrateLimitExceeded = IsBitrateLimitExceeded(item, options.GetMaxBitrate(false) ?? 0); |
| 278 | 707 | | var isEligibleForDirectPlay = options.EnableDirectPlay && (options.ForceDirectPlay || !bitrateLimitExceeded) |
| 278 | 708 | | var isEligibleForDirectStream = options.EnableDirectStream && (options.ForceDirectStream || !bitrateLimitExc |
| 278 | 709 | | TranscodeReason transcodeReasons = 0; |
| | 710 | |
|
| | 711 | | // Force transcode or remux for BD/DVD folders |
| 278 | 712 | | if (item.VideoType == VideoType.Dvd || item.VideoType == VideoType.BluRay) |
| | 713 | | { |
| 0 | 714 | | isEligibleForDirectPlay = false; |
| | 715 | | } |
| | 716 | |
|
| 278 | 717 | | if (bitrateLimitExceeded) |
| | 718 | | { |
| 24 | 719 | | transcodeReasons = TranscodeReason.ContainerBitrateExceedsLimit; |
| | 720 | | } |
| | 721 | |
|
| 278 | 722 | | _logger.LogDebug( |
| 278 | 723 | | "Profile: {0}, Path: {1}, isEligibleForDirectPlay: {2}, isEligibleForDirectStream: {3}", |
| 278 | 724 | | options.Profile.Name ?? "Unknown Profile", |
| 278 | 725 | | item.Path ?? "Unknown path", |
| 278 | 726 | | isEligibleForDirectPlay, |
| 278 | 727 | | isEligibleForDirectStream); |
| | 728 | |
|
| 278 | 729 | | DirectPlayProfile? directPlayProfile = null; |
| 278 | 730 | | if (isEligibleForDirectPlay || isEligibleForDirectStream) |
| | 731 | | { |
| | 732 | | // See if it can be direct played |
| 254 | 733 | | var directPlayInfo = GetVideoDirectPlayProfile(options, item, videoStream, audioStream, candidateAudioSt |
| 254 | 734 | | var directPlay = directPlayInfo.PlayMethod; |
| 254 | 735 | | transcodeReasons |= directPlayInfo.TranscodeReasons; |
| | 736 | |
|
| 254 | 737 | | if (directPlay.HasValue) |
| | 738 | | { |
| 124 | 739 | | directPlayProfile = directPlayInfo.Profile; |
| 124 | 740 | | playlistItem.PlayMethod = directPlay.Value; |
| 124 | 741 | | playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profi |
| 124 | 742 | | var videoCodec = videoStream?.Codec; |
| 124 | 743 | | playlistItem.VideoCodecs = videoCodec is null ? [] : [videoCodec]; |
| | 744 | |
|
| 124 | 745 | | if (directPlay == PlayMethod.DirectPlay) |
| | 746 | | { |
| 124 | 747 | | playlistItem.SubProtocol = MediaStreamProtocol.http; |
| | 748 | |
|
| 124 | 749 | | var audioStreamIndex = directPlayInfo.AudioStreamIndex ?? audioStream?.Index; |
| 124 | 750 | | if (audioStreamIndex.HasValue) |
| | 751 | | { |
| 124 | 752 | | playlistItem.AudioStreamIndex = audioStreamIndex; |
| 124 | 753 | | var audioCodec = item.GetMediaStream(MediaStreamType.Audio, audioStreamIndex.Value)?.Codec; |
| 124 | 754 | | playlistItem.AudioCodecs = audioCodec is null ? [] : [audioCodec]; |
| | 755 | | } |
| | 756 | | } |
| 0 | 757 | | else if (directPlay == PlayMethod.DirectStream) |
| | 758 | | { |
| 0 | 759 | | playlistItem.AudioStreamIndex = audioStream?.Index; |
| 0 | 760 | | if (audioStream is not null) |
| | 761 | | { |
| 0 | 762 | | playlistItem.AudioCodecs = ContainerHelper.Split(directPlayProfile?.AudioCodec); |
| | 763 | | } |
| | 764 | |
|
| 0 | 765 | | SetStreamInfoOptionsFromDirectPlayProfile(options, item, playlistItem, directPlayProfile); |
| 0 | 766 | | BuildStreamVideoItem(playlistItem, options, item, videoStream, audioStream, candidateAudioStream |
| | 767 | | } |
| | 768 | |
|
| 124 | 769 | | if (subtitleStream is not null) |
| | 770 | | { |
| 116 | 771 | | var subtitleProfile = GetSubtitleProfile(item, subtitleStream, options.Profile.SubtitleProfiles, |
| | 772 | |
|
| 116 | 773 | | playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method; |
| 116 | 774 | | playlistItem.SubtitleFormat = subtitleProfile.Format; |
| | 775 | | } |
| | 776 | | } |
| | 777 | |
|
| 254 | 778 | | _logger.LogDebug( |
| 254 | 779 | | "DirectPlay Result for Profile: {0}, Path: {1}, PlayMethod: {2}, AudioStreamIndex: {3}, SubtitleStre |
| 254 | 780 | | options.Profile.Name ?? "Anonymous Profile", |
| 254 | 781 | | item.Path ?? "Unknown path", |
| 254 | 782 | | directPlayInfo.PlayMethod, |
| 254 | 783 | | directPlayInfo.AudioStreamIndex ?? audioStream?.Index, |
| 254 | 784 | | playlistItem.SubtitleStreamIndex, |
| 254 | 785 | | directPlayInfo.TranscodeReasons); |
| | 786 | | } |
| | 787 | |
|
| 278 | 788 | | playlistItem.TranscodeReasons = transcodeReasons; |
| | 789 | |
|
| 278 | 790 | | if (playlistItem.PlayMethod != PlayMethod.DirectStream && playlistItem.PlayMethod != PlayMethod.DirectPlay) |
| | 791 | | { |
| | 792 | | // Can't direct play, find the transcoding profile |
| | 793 | | // If we do this for direct-stream we will overwrite the info |
| 154 | 794 | | var (transcodingProfile, playMethod) = GetVideoTranscodeProfile(item, options, videoStream, audioStream, |
| | 795 | |
|
| 154 | 796 | | if (transcodingProfile is not null && playMethod.HasValue) |
| | 797 | | { |
| 146 | 798 | | SetStreamInfoOptionsFromTranscodingProfile(item, playlistItem, transcodingProfile); |
| | 799 | |
|
| 146 | 800 | | BuildStreamVideoItem(playlistItem, options, item, videoStream, audioStream, candidateAudioStreams, t |
| | 801 | |
|
| 146 | 802 | | playlistItem.PlayMethod = PlayMethod.Transcode; |
| | 803 | |
|
| 146 | 804 | | if (subtitleStream is not null) |
| | 805 | | { |
| 123 | 806 | | var subtitleProfile = GetSubtitleProfile(item, subtitleStream, options.Profile.SubtitleProfiles, |
| 123 | 807 | | playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method; |
| 123 | 808 | | playlistItem.SubtitleFormat = subtitleProfile.Format; |
| 123 | 809 | | playlistItem.SubtitleCodecs = [subtitleProfile.Format]; |
| | 810 | | } |
| | 811 | |
|
| 146 | 812 | | if ((playlistItem.TranscodeReasons & (VideoReasons | TranscodeReason.ContainerBitrateExceedsLimit)) |
| | 813 | | { |
| 44 | 814 | | ApplyTranscodingConditions(playlistItem, transcodingProfile.Conditions, null, true, true); |
| | 815 | | } |
| | 816 | | } |
| | 817 | | } |
| | 818 | |
|
| 278 | 819 | | _logger.LogDebug( |
| 278 | 820 | | "StreamBuilder.BuildVideoItem( Profile={0}, Path={1}, AudioStreamIndex={2}, SubtitleStreamIndex={3} ) => |
| 278 | 821 | | options.Profile.Name ?? "Anonymous Profile", |
| 278 | 822 | | item.Path ?? "Unknown path", |
| 278 | 823 | | options.AudioStreamIndex, |
| 278 | 824 | | options.SubtitleStreamIndex, |
| 278 | 825 | | playlistItem.PlayMethod, |
| 278 | 826 | | playlistItem.TranscodeReasons, |
| 278 | 827 | | playlistItem.ToUrl("media:", "<token>", null)); |
| | 828 | |
|
| 278 | 829 | | item.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileT |
| 278 | 830 | | return playlistItem; |
| | 831 | | } |
| | 832 | |
|
| | 833 | | private (TranscodingProfile? Profile, PlayMethod? PlayMethod) GetVideoTranscodeProfile( |
| | 834 | | MediaSourceInfo item, |
| | 835 | | MediaOptions options, |
| | 836 | | MediaStream? videoStream, |
| | 837 | | MediaStream? audioStream, |
| | 838 | | StreamInfo playlistItem) |
| | 839 | | { |
| 154 | 840 | | var mediaSource = playlistItem.MediaSource; |
| | 841 | |
|
| 154 | 842 | | ArgumentNullException.ThrowIfNull(mediaSource); |
| | 843 | |
|
| 154 | 844 | | if (!(item.SupportsTranscoding || item.SupportsDirectStream)) |
| | 845 | | { |
| 0 | 846 | | return (null, null); |
| | 847 | | } |
| | 848 | |
|
| 154 | 849 | | var transcodingProfiles = options.Profile.TranscodingProfiles |
| 154 | 850 | | .Where(i => i.Type == playlistItem.MediaType && i.Context == options.Context); |
| | 851 | |
|
| 154 | 852 | | if (item.UseMostCompatibleTranscodingProfile) |
| | 853 | | { |
| 0 | 854 | | transcodingProfiles = transcodingProfiles.Where(i => string.Equals(i.Container, "ts", StringComparison.O |
| | 855 | | } |
| | 856 | |
|
| 154 | 857 | | var videoCodec = videoStream?.Codec; |
| 154 | 858 | | var audioCodec = audioStream?.Codec; |
| | 859 | |
|
| 154 | 860 | | var analyzedProfiles = transcodingProfiles |
| 154 | 861 | | .Select(transcodingProfile => |
| 154 | 862 | | { |
| 154 | 863 | | var rank = (Video: 3, Audio: 3); |
| 154 | 864 | |
|
| 154 | 865 | | var container = transcodingProfile.Container; |
| 154 | 866 | |
|
| 154 | 867 | | if (videoStream is not null |
| 154 | 868 | | && options.AllowVideoStreamCopy |
| 154 | 869 | | && ContainerHelper.ContainsContainer(transcodingProfile.VideoCodec, videoCodec)) |
| 154 | 870 | | { |
| 154 | 871 | | var failures = GetCompatibilityVideoCodec(options, mediaSource, container, videoStream); |
| 154 | 872 | | rank.Video = failures == 0 ? 1 : 2; |
| 154 | 873 | | } |
| 154 | 874 | |
|
| 154 | 875 | | if (audioStream is not null |
| 154 | 876 | | && options.AllowAudioStreamCopy) |
| 154 | 877 | | { |
| 154 | 878 | | // For Audio stream, we prefer the audio codec that can be directly copied, then the codec that |
| 154 | 879 | | // the transcoding conditions, then the one does not satisfy the transcoding conditions. |
| 154 | 880 | | // For example: A client can support both aac and flac, but flac only supports 2 channels while |
| 154 | 881 | | // When the source audio is 6 channel flac, we should transcode to 6 channel aac, instead of dow |
| 154 | 882 | | var transcodingAudioCodecs = ContainerHelper.Split(transcodingProfile.AudioCodec); |
| 154 | 883 | |
|
| 154 | 884 | | foreach (var transcodingAudioCodec in transcodingAudioCodecs) |
| 154 | 885 | | { |
| 154 | 886 | | var failures = GetCompatibilityAudioCodec(options, mediaSource, container, audioStream, tran |
| 154 | 887 | |
|
| 154 | 888 | | var rankAudio = 3; |
| 154 | 889 | |
|
| 154 | 890 | | if (failures == 0) |
| 154 | 891 | | { |
| 154 | 892 | | rankAudio = string.Equals(transcodingAudioCodec, audioCodec, StringComparison.OrdinalIgn |
| 154 | 893 | | } |
| 154 | 894 | |
|
| 154 | 895 | | rank.Audio = Math.Min(rank.Audio, rankAudio); |
| 154 | 896 | |
|
| 154 | 897 | | if (rank.Audio == 1) |
| 154 | 898 | | { |
| 154 | 899 | | break; |
| 154 | 900 | | } |
| 154 | 901 | | } |
| 154 | 902 | | } |
| 154 | 903 | |
|
| 154 | 904 | | PlayMethod playMethod = PlayMethod.Transcode; |
| 154 | 905 | |
|
| 154 | 906 | | if (rank.Video == 1) |
| 154 | 907 | | { |
| 154 | 908 | | playMethod = PlayMethod.DirectStream; |
| 154 | 909 | | } |
| 154 | 910 | |
|
| 154 | 911 | | return (Profile: transcodingProfile, PlayMethod: playMethod, Rank: rank); |
| 154 | 912 | | }) |
| 154 | 913 | | .OrderBy(analysis => analysis.Rank); |
| | 914 | |
|
| 154 | 915 | | var profileMatch = analyzedProfiles.FirstOrDefault(); |
| | 916 | |
|
| 154 | 917 | | return (profileMatch.Profile, profileMatch.PlayMethod); |
| | 918 | | } |
| | 919 | |
|
| | 920 | | private void BuildStreamVideoItem( |
| | 921 | | StreamInfo playlistItem, |
| | 922 | | MediaOptions options, |
| | 923 | | MediaSourceInfo item, |
| | 924 | | MediaStream? videoStream, |
| | 925 | | MediaStream? audioStream, |
| | 926 | | IEnumerable<MediaStream> candidateAudioStreams, |
| | 927 | | string? container, |
| | 928 | | string? videoCodec, |
| | 929 | | string? audioCodec) |
| | 930 | | { |
| | 931 | | // Prefer matching video codecs |
| 146 | 932 | | var videoCodecs = ContainerHelper.Split(videoCodec).ToList(); |
| | 933 | |
|
| 146 | 934 | | if (videoCodecs.Count == 0 && videoStream is not null) |
| | 935 | | { |
| | 936 | | // Add the original codec if no codec is specified |
| 0 | 937 | | videoCodecs.Add(videoStream.Codec); |
| | 938 | | } |
| | 939 | |
|
| | 940 | | // Enforce HLS video codec restrictions |
| 146 | 941 | | if (playlistItem.SubProtocol == MediaStreamProtocol.hls) |
| | 942 | | { |
| 132 | 943 | | videoCodecs = videoCodecs.Where(codec => _supportedHlsVideoCodecs.Contains(codec)).ToList(); |
| | 944 | | } |
| | 945 | |
|
| 146 | 946 | | playlistItem.VideoCodecs = videoCodecs; |
| | 947 | |
|
| | 948 | | // Copy video codec options as a starting point, this applies to transcode and direct-stream |
| 146 | 949 | | playlistItem.MaxFramerate = videoStream?.ReferenceFrameRate; |
| 146 | 950 | | var qualifier = videoStream?.Codec; |
| 146 | 951 | | if (videoStream?.Level is not null) |
| | 952 | | { |
| 145 | 953 | | playlistItem.SetOption(qualifier, "level", videoStream.Level.Value.ToString(CultureInfo.InvariantCulture |
| | 954 | | } |
| | 955 | |
|
| 146 | 956 | | if (videoStream?.BitDepth is not null) |
| | 957 | | { |
| 145 | 958 | | playlistItem.SetOption(qualifier, "videobitdepth", videoStream.BitDepth.Value.ToString(CultureInfo.Invar |
| | 959 | | } |
| | 960 | |
|
| 146 | 961 | | if (!string.IsNullOrEmpty(videoStream?.Profile)) |
| | 962 | | { |
| 145 | 963 | | playlistItem.SetOption(qualifier, "profile", videoStream.Profile.ToLowerInvariant()); |
| | 964 | | } |
| | 965 | |
|
| | 966 | | // Prefer matching audio codecs, could do better here |
| 146 | 967 | | var audioCodecs = ContainerHelper.Split(audioCodec).ToList(); |
| | 968 | |
|
| 146 | 969 | | if (audioCodecs.Count == 0 && audioStream is not null) |
| | 970 | | { |
| | 971 | | // Add the original codec if no codec is specified |
| 0 | 972 | | audioCodecs.Add(audioStream.Codec); |
| | 973 | | } |
| | 974 | |
|
| | 975 | | // Enforce HLS audio codec restrictions |
| 146 | 976 | | if (playlistItem.SubProtocol == MediaStreamProtocol.hls) |
| | 977 | | { |
| 132 | 978 | | if (string.Equals(playlistItem.Container, "mp4", StringComparison.OrdinalIgnoreCase)) |
| | 979 | | { |
| 72 | 980 | | audioCodecs = audioCodecs.Where(codec => _supportedHlsAudioCodecsMp4.Contains(codec)).ToList(); |
| | 981 | | } |
| | 982 | | else |
| | 983 | | { |
| 60 | 984 | | audioCodecs = audioCodecs.Where(codec => _supportedHlsAudioCodecsTs.Contains(codec)).ToList(); |
| | 985 | | } |
| | 986 | | } |
| | 987 | |
|
| 146 | 988 | | var audioStreamWithSupportedCodec = candidateAudioStreams.Where(stream => ContainerHelper.ContainsContainer( |
| | 989 | |
|
| 146 | 990 | | var channelsExceedsLimit = audioStreamWithSupportedCodec is not null && audioStreamWithSupportedCodec.Channe |
| | 991 | |
|
| 146 | 992 | | var directAudioFailures = audioStreamWithSupportedCodec is null ? default : GetCompatibilityAudioCodec(optio |
| | 993 | |
|
| 146 | 994 | | playlistItem.TranscodeReasons |= directAudioFailures; |
| | 995 | |
|
| 146 | 996 | | var directAudioStreamSatisfied = audioStreamWithSupportedCodec is not null && !channelsExceedsLimit |
| 146 | 997 | | && directAudioFailures == 0; |
| | 998 | |
|
| 146 | 999 | | directAudioStreamSatisfied = directAudioStreamSatisfied && !playlistItem.TranscodeReasons.HasFlag(TranscodeR |
| | 1000 | |
|
| 146 | 1001 | | var directAudioStream = directAudioStreamSatisfied ? audioStreamWithSupportedCodec : null; |
| | 1002 | |
|
| 146 | 1003 | | if (channelsExceedsLimit && playlistItem.TargetAudioStream is not null) |
| | 1004 | | { |
| 10 | 1005 | | playlistItem.TranscodeReasons |= TranscodeReason.AudioChannelsNotSupported; |
| 10 | 1006 | | playlistItem.TargetAudioStream.Channels = playlistItem.TranscodingMaxAudioChannels; |
| | 1007 | | } |
| | 1008 | |
|
| 146 | 1009 | | playlistItem.AudioCodecs = audioCodecs; |
| 146 | 1010 | | if (directAudioStream is not null) |
| | 1011 | | { |
| 55 | 1012 | | audioStream = directAudioStream; |
| 55 | 1013 | | playlistItem.AudioStreamIndex = audioStream.Index; |
| 55 | 1014 | | audioCodecs = [audioStream.Codec]; |
| 55 | 1015 | | playlistItem.AudioCodecs = audioCodecs; |
| | 1016 | |
|
| | 1017 | | // Copy matching audio codec options |
| 55 | 1018 | | playlistItem.AudioSampleRate = audioStream.SampleRate; |
| 55 | 1019 | | playlistItem.SetOption(qualifier, "audiochannels", audioStream.Channels?.ToString(CultureInfo.InvariantC |
| | 1020 | |
|
| 55 | 1021 | | if (!string.IsNullOrEmpty(audioStream.Profile)) |
| | 1022 | | { |
| 53 | 1023 | | playlistItem.SetOption(audioStream.Codec, "profile", audioStream.Profile.ToLowerInvariant()); |
| | 1024 | | } |
| | 1025 | |
|
| 55 | 1026 | | if (audioStream.Level.HasValue && audioStream.Level.Value != 0) |
| | 1027 | | { |
| 0 | 1028 | | playlistItem.SetOption(audioStream.Codec, "level", audioStream.Level.Value.ToString(CultureInfo.Inva |
| | 1029 | | } |
| | 1030 | | } |
| | 1031 | |
|
| 146 | 1032 | | int? width = videoStream?.Width; |
| 146 | 1033 | | int? height = videoStream?.Height; |
| 146 | 1034 | | int? bitDepth = videoStream?.BitDepth; |
| 146 | 1035 | | int? videoBitrate = videoStream?.BitRate; |
| 146 | 1036 | | double? videoLevel = videoStream?.Level; |
| 146 | 1037 | | string? videoProfile = videoStream?.Profile; |
| 146 | 1038 | | VideoRangeType? videoRangeType = videoStream?.VideoRangeType; |
| 146 | 1039 | | float videoFramerate = videoStream is null ? 0 : videoStream.ReferenceFrameRate ?? 0; |
| 146 | 1040 | | bool? isAnamorphic = videoStream?.IsAnamorphic; |
| 146 | 1041 | | bool? isInterlaced = videoStream?.IsInterlaced; |
| 146 | 1042 | | string? videoCodecTag = videoStream?.CodecTag; |
| 146 | 1043 | | bool? isAvc = videoStream?.IsAVC; |
| | 1044 | |
|
| 146 | 1045 | | TransportStreamTimestamp? timestamp = videoStream is null ? TransportStreamTimestamp.None : item.Timestamp; |
| 146 | 1046 | | int? packetLength = videoStream?.PacketLength; |
| 146 | 1047 | | int? refFrames = videoStream?.RefFrames; |
| | 1048 | |
|
| 146 | 1049 | | int numStreams = item.MediaStreams.Count; |
| 146 | 1050 | | int? numAudioStreams = item.GetStreamCount(MediaStreamType.Audio); |
| 146 | 1051 | | int? numVideoStreams = item.GetStreamCount(MediaStreamType.Video); |
| | 1052 | |
|
| 146 | 1053 | | var useSubContainer = playlistItem.SubProtocol == MediaStreamProtocol.hls; |
| | 1054 | |
|
| 146 | 1055 | | var appliedVideoConditions = options.Profile.CodecProfiles |
| 146 | 1056 | | .Where(i => i.Type == CodecType.Video && |
| 146 | 1057 | | i.ContainsAnyCodec(playlistItem.VideoCodecs, container, useSubContainer) && |
| 146 | 1058 | | i.ApplyConditions.All(applyCondition => ConditionProcessor.IsVideoConditionSatisfied(applyCondition, |
| 146 | 1059 | | // Reverse codec profiles for backward compatibility - first codec profile has higher priority |
| 146 | 1060 | | .Reverse(); |
| 888 | 1061 | | foreach (var condition in appliedVideoConditions) |
| | 1062 | | { |
| 2416 | 1063 | | foreach (var transcodingVideoCodec in playlistItem.VideoCodecs) |
| | 1064 | | { |
| 910 | 1065 | | if (condition.ContainsAnyCodec(transcodingVideoCodec, container, useSubContainer)) |
| | 1066 | | { |
| 306 | 1067 | | ApplyTranscodingConditions(playlistItem, condition.Conditions, transcodingVideoCodec, true, true |
| | 1068 | | continue; |
| | 1069 | | } |
| | 1070 | | } |
| | 1071 | | } |
| | 1072 | |
|
| | 1073 | | // Honor requested max channels |
| 146 | 1074 | | playlistItem.GlobalMaxAudioChannels = channelsExceedsLimit ? playlistItem.TranscodingMaxAudioChannels : opti |
| | 1075 | |
|
| 146 | 1076 | | int audioBitrate = GetAudioBitrate(options.GetMaxBitrate(true) ?? 0, playlistItem.TargetAudioCodec, audioStr |
| 146 | 1077 | | playlistItem.AudioBitrate = Math.Min(playlistItem.AudioBitrate ?? audioBitrate, audioBitrate); |
| | 1078 | |
|
| 146 | 1079 | | bool? isSecondaryAudio = audioStream is null ? null : item.IsSecondaryAudio(audioStream); |
| 146 | 1080 | | int? inputAudioBitrate = audioStream?.BitRate; |
| 146 | 1081 | | int? audioChannels = audioStream?.Channels; |
| 146 | 1082 | | string? audioProfile = audioStream?.Profile; |
| 146 | 1083 | | int? inputAudioSampleRate = audioStream?.SampleRate; |
| 146 | 1084 | | int? inputAudioBitDepth = audioStream?.BitDepth; |
| | 1085 | |
|
| 146 | 1086 | | var appliedAudioConditions = options.Profile.CodecProfiles |
| 146 | 1087 | | .Where(i => i.Type == CodecType.VideoAudio && |
| 146 | 1088 | | i.ContainsAnyCodec(playlistItem.AudioCodecs, container) && |
| 146 | 1089 | | i.ApplyConditions.All(applyCondition => ConditionProcessor.IsVideoAudioConditionSatisfied(applyCondi |
| 146 | 1090 | | // Reverse codec profiles for backward compatibility - first codec profile has higher priority |
| 146 | 1091 | | .Reverse(); |
| | 1092 | |
|
| 596 | 1093 | | foreach (var codecProfile in appliedAudioConditions) |
| | 1094 | | { |
| 456 | 1095 | | foreach (var transcodingAudioCodec in playlistItem.AudioCodecs) |
| | 1096 | | { |
| 152 | 1097 | | if (codecProfile.ContainsAnyCodec(transcodingAudioCodec, container)) |
| | 1098 | | { |
| 152 | 1099 | | ApplyTranscodingConditions(playlistItem, codecProfile.Conditions, transcodingAudioCodec, true, t |
| 152 | 1100 | | break; |
| | 1101 | | } |
| | 1102 | | } |
| | 1103 | | } |
| | 1104 | |
|
| 146 | 1105 | | var maxBitrateSetting = options.GetMaxBitrate(false); |
| | 1106 | | // Honor max rate |
| 146 | 1107 | | if (maxBitrateSetting.HasValue) |
| | 1108 | | { |
| 146 | 1109 | | var availableBitrateForVideo = maxBitrateSetting.Value; |
| | 1110 | |
|
| 146 | 1111 | | if (playlistItem.AudioBitrate.HasValue) |
| | 1112 | | { |
| 146 | 1113 | | availableBitrateForVideo -= playlistItem.AudioBitrate.Value; |
| | 1114 | | } |
| | 1115 | |
|
| | 1116 | | // Make sure the video bitrate is lower than bitrate settings but at least 64k |
| | 1117 | | // Don't use Math.Clamp as availableBitrateForVideo can be lower then 64k. |
| 146 | 1118 | | var currentValue = playlistItem.VideoBitrate ?? availableBitrateForVideo; |
| 146 | 1119 | | playlistItem.VideoBitrate = Math.Max(Math.Min(availableBitrateForVideo, currentValue), 64_000); |
| | 1120 | | } |
| | 1121 | |
|
| 146 | 1122 | | _logger.LogDebug( |
| 146 | 1123 | | "Transcode Result for Profile: {Profile}, Path: {Path}, PlayMethod: {PlayMethod}, AudioStreamIndex: {Aud |
| 146 | 1124 | | options.Profile.Name ?? "Anonymous Profile", |
| 146 | 1125 | | item.Path ?? "Unknown path", |
| 146 | 1126 | | playlistItem.PlayMethod, |
| 146 | 1127 | | audioStream?.Index, |
| 146 | 1128 | | playlistItem.SubtitleStreamIndex, |
| 146 | 1129 | | playlistItem.TranscodeReasons); |
| 146 | 1130 | | } |
| | 1131 | |
|
| | 1132 | | private static int GetDefaultAudioBitrate(string? audioCodec, int? audioChannels) |
| | 1133 | | { |
| 60 | 1134 | | if (!string.IsNullOrEmpty(audioCodec)) |
| | 1135 | | { |
| | 1136 | | // Default to a higher bitrate for stream copy |
| 60 | 1137 | | if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase) |
| 60 | 1138 | | || string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase) |
| 60 | 1139 | | || string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase) |
| 60 | 1140 | | || string.Equals(audioCodec, "eac3", StringComparison.OrdinalIgnoreCase)) |
| | 1141 | | { |
| 55 | 1142 | | if ((audioChannels ?? 0) < 2) |
| | 1143 | | { |
| 0 | 1144 | | return 128000; |
| | 1145 | | } |
| | 1146 | |
|
| 55 | 1147 | | return (audioChannels ?? 0) >= 6 ? 640000 : 384000; |
| | 1148 | | } |
| | 1149 | |
|
| 5 | 1150 | | if (string.Equals(audioCodec, "flac", StringComparison.OrdinalIgnoreCase) |
| 5 | 1151 | | || string.Equals(audioCodec, "alac", StringComparison.OrdinalIgnoreCase)) |
| | 1152 | | { |
| 0 | 1153 | | if ((audioChannels ?? 0) < 2) |
| | 1154 | | { |
| 0 | 1155 | | return 768000; |
| | 1156 | | } |
| | 1157 | |
|
| 0 | 1158 | | return (audioChannels ?? 0) >= 6 ? 3584000 : 1536000; |
| | 1159 | | } |
| | 1160 | | } |
| | 1161 | |
|
| 5 | 1162 | | return 192000; |
| | 1163 | | } |
| | 1164 | |
|
| | 1165 | | private static int GetAudioBitrate(long maxTotalBitrate, IReadOnlyList<string> targetAudioCodecs, MediaStream? a |
| | 1166 | | { |
| 146 | 1167 | | string? targetAudioCodec = targetAudioCodecs.Count == 0 ? null : targetAudioCodecs[0]; |
| | 1168 | |
|
| 146 | 1169 | | int? targetAudioChannels = item.GetTargetAudioChannels(targetAudioCodec); |
| | 1170 | |
|
| | 1171 | | int defaultBitrate; |
| 146 | 1172 | | int encoderAudioBitrateLimit = int.MaxValue; |
| | 1173 | |
|
| 146 | 1174 | | if (audioStream is null) |
| | 1175 | | { |
| 1 | 1176 | | defaultBitrate = 192000; |
| | 1177 | | } |
| | 1178 | | else |
| | 1179 | | { |
| 145 | 1180 | | if (targetAudioChannels.HasValue |
| 145 | 1181 | | && audioStream.Channels.HasValue |
| 145 | 1182 | | && audioStream.Channels.Value > targetAudioChannels.Value) |
| | 1183 | | { |
| | 1184 | | // Reduce the bitrate if we're down mixing. |
| 46 | 1185 | | defaultBitrate = GetDefaultAudioBitrate(targetAudioCodec, targetAudioChannels); |
| | 1186 | | } |
| 99 | 1187 | | else if (targetAudioChannels.HasValue |
| 99 | 1188 | | && audioStream.Channels.HasValue |
| 99 | 1189 | | && audioStream.Channels.Value <= targetAudioChannels.Value |
| 99 | 1190 | | && !string.IsNullOrEmpty(audioStream.Codec) |
| 99 | 1191 | | && targetAudioCodecs is not null |
| 99 | 1192 | | && targetAudioCodecs.Count > 0 |
| 99 | 1193 | | && !targetAudioCodecs.Any(elem => string.Equals(audioStream.Codec, elem, StringComparison.Ordin |
| | 1194 | | { |
| | 1195 | | // Shift the bitrate if we're transcoding to a different audio codec. |
| 14 | 1196 | | defaultBitrate = GetDefaultAudioBitrate(targetAudioCodec, audioStream.Channels.Value); |
| | 1197 | | } |
| | 1198 | | else |
| | 1199 | | { |
| 85 | 1200 | | defaultBitrate = audioStream.BitRate ?? GetDefaultAudioBitrate(targetAudioCodec, targetAudioChannels |
| | 1201 | | } |
| | 1202 | |
|
| | 1203 | | // Seeing webm encoding failures when source has 1 audio channel and 22k bitrate. |
| | 1204 | | // Any attempts to transcode over 64k will fail |
| 145 | 1205 | | if (audioStream.Channels == 1 |
| 145 | 1206 | | && (audioStream.BitRate ?? 0) < 64000) |
| | 1207 | | { |
| 0 | 1208 | | encoderAudioBitrateLimit = 64000; |
| | 1209 | | } |
| | 1210 | | } |
| | 1211 | |
|
| 146 | 1212 | | if (maxTotalBitrate > 0) |
| | 1213 | | { |
| 146 | 1214 | | defaultBitrate = Math.Min(GetMaxAudioBitrateForTotalBitrate(maxTotalBitrate), defaultBitrate); |
| | 1215 | | } |
| | 1216 | |
|
| 146 | 1217 | | return Math.Min(defaultBitrate, encoderAudioBitrateLimit); |
| | 1218 | | } |
| | 1219 | |
|
| | 1220 | | private static int GetMaxAudioBitrateForTotalBitrate(long totalBitrate) |
| | 1221 | | { |
| 146 | 1222 | | if (totalBitrate <= 640000) |
| | 1223 | | { |
| 8 | 1224 | | return 128000; |
| | 1225 | | } |
| | 1226 | |
|
| 138 | 1227 | | if (totalBitrate <= 2000000) |
| | 1228 | | { |
| 0 | 1229 | | return 384000; |
| | 1230 | | } |
| | 1231 | |
|
| 138 | 1232 | | if (totalBitrate <= 3000000) |
| | 1233 | | { |
| 0 | 1234 | | return 448000; |
| | 1235 | | } |
| | 1236 | |
|
| 138 | 1237 | | if (totalBitrate <= 4000000) |
| | 1238 | | { |
| 0 | 1239 | | return 640000; |
| | 1240 | | } |
| | 1241 | |
|
| 138 | 1242 | | if (totalBitrate <= 5000000) |
| | 1243 | | { |
| 0 | 1244 | | return 768000; |
| | 1245 | | } |
| | 1246 | |
|
| 138 | 1247 | | if (totalBitrate <= 10000000) |
| | 1248 | | { |
| 8 | 1249 | | return 1536000; |
| | 1250 | | } |
| | 1251 | |
|
| 130 | 1252 | | if (totalBitrate <= 15000000) |
| | 1253 | | { |
| 0 | 1254 | | return 2304000; |
| | 1255 | | } |
| | 1256 | |
|
| 130 | 1257 | | if (totalBitrate <= 20000000) |
| | 1258 | | { |
| 14 | 1259 | | return 3584000; |
| | 1260 | | } |
| | 1261 | |
|
| 116 | 1262 | | return 7168000; |
| | 1263 | | } |
| | 1264 | |
|
| | 1265 | | private (DirectPlayProfile? Profile, PlayMethod? PlayMethod, int? AudioStreamIndex, TranscodeReason TranscodeRea |
| | 1266 | | MediaOptions options, |
| | 1267 | | MediaSourceInfo mediaSource, |
| | 1268 | | MediaStream? videoStream, |
| | 1269 | | MediaStream? audioStream, |
| | 1270 | | ICollection<MediaStream> candidateAudioStreams, |
| | 1271 | | MediaStream? subtitleStream, |
| | 1272 | | bool isEligibleForDirectPlay, |
| | 1273 | | bool isEligibleForDirectStream) |
| | 1274 | | { |
| 254 | 1275 | | if (options.ForceDirectPlay) |
| | 1276 | | { |
| 0 | 1277 | | return (null, PlayMethod.DirectPlay, audioStream?.Index, 0); |
| | 1278 | | } |
| | 1279 | |
|
| 254 | 1280 | | if (options.ForceDirectStream) |
| | 1281 | | { |
| 0 | 1282 | | return (null, PlayMethod.DirectStream, audioStream?.Index, 0); |
| | 1283 | | } |
| | 1284 | |
|
| 254 | 1285 | | DeviceProfile profile = options.Profile; |
| 254 | 1286 | | string container = mediaSource.Container; |
| | 1287 | |
|
| | 1288 | | // Check container conditions |
| 254 | 1289 | | var containerProfileReasons = GetCompatibilityContainer(options, mediaSource, container, videoStream); |
| | 1290 | |
|
| | 1291 | | // Check video conditions |
| 254 | 1292 | | var videoCodecProfileReasons = videoStream is null ? default : GetCompatibilityVideoCodec(options, mediaSour |
| | 1293 | |
|
| | 1294 | | // Check audio candidates profile conditions |
| 254 | 1295 | | var audioStreamMatches = candidateAudioStreams.ToDictionary(s => s, audioStream => GetCompatibilityAudioCode |
| | 1296 | |
|
| 254 | 1297 | | TranscodeReason subtitleProfileReasons = 0; |
| 254 | 1298 | | if (subtitleStream is not null) |
| | 1299 | | { |
| 223 | 1300 | | var subtitleProfile = GetSubtitleProfile(mediaSource, subtitleStream, options.Profile.SubtitleProfiles, |
| | 1301 | |
|
| 223 | 1302 | | if (subtitleProfile.Method != SubtitleDeliveryMethod.Drop |
| 223 | 1303 | | && subtitleProfile.Method != SubtitleDeliveryMethod.External |
| 223 | 1304 | | && subtitleProfile.Method != SubtitleDeliveryMethod.Embed) |
| | 1305 | | { |
| 0 | 1306 | | _logger.LogDebug("Not eligible for {0} due to unsupported subtitles", PlayMethod.DirectPlay); |
| 0 | 1307 | | subtitleProfileReasons |= TranscodeReason.SubtitleCodecNotSupported; |
| | 1308 | | } |
| | 1309 | | } |
| | 1310 | |
|
| 254 | 1311 | | var containerSupported = false; |
| 254 | 1312 | | TranscodeReason[] rankings = [TranscodeReason.VideoCodecNotSupported, VideoCodecReasons, TranscodeReason.Aud |
| | 1313 | |
|
| | 1314 | | // Check DirectPlay profiles to see if it can be direct played |
| 254 | 1315 | | var analyzedProfiles = profile.DirectPlayProfiles |
| 254 | 1316 | | .Where(directPlayProfile => directPlayProfile.Type == DlnaProfileType.Video) |
| 254 | 1317 | | .Select((directPlayProfile, order) => |
| 254 | 1318 | | { |
| 254 | 1319 | | TranscodeReason directPlayProfileReasons = 0; |
| 254 | 1320 | | TranscodeReason audioCodecProfileReasons = 0; |
| 254 | 1321 | |
|
| 254 | 1322 | | // Check container type |
| 254 | 1323 | | if (!directPlayProfile.SupportsContainer(container)) |
| 254 | 1324 | | { |
| 254 | 1325 | | directPlayProfileReasons |= TranscodeReason.ContainerNotSupported; |
| 254 | 1326 | | } |
| 254 | 1327 | | else |
| 254 | 1328 | | { |
| 254 | 1329 | | containerSupported = true; |
| 254 | 1330 | | } |
| 254 | 1331 | |
|
| 254 | 1332 | | // Check video codec |
| 254 | 1333 | | string? videoCodec = videoStream?.Codec; |
| 254 | 1334 | | if (!directPlayProfile.SupportsVideoCodec(videoCodec)) |
| 254 | 1335 | | { |
| 254 | 1336 | | directPlayProfileReasons |= TranscodeReason.VideoCodecNotSupported; |
| 254 | 1337 | | } |
| 254 | 1338 | |
|
| 254 | 1339 | | // Check audio codec |
| 254 | 1340 | | MediaStream? selectedAudioStream = null; |
| 254 | 1341 | | if (candidateAudioStreams.Count != 0) |
| 254 | 1342 | | { |
| 254 | 1343 | | selectedAudioStream = candidateAudioStreams.FirstOrDefault(audioStream => directPlayProfile.Supp |
| 254 | 1344 | | if (selectedAudioStream is null) |
| 254 | 1345 | | { |
| 254 | 1346 | | directPlayProfileReasons |= TranscodeReason.AudioCodecNotSupported; |
| 254 | 1347 | | } |
| 254 | 1348 | | else |
| 254 | 1349 | | { |
| 254 | 1350 | | audioCodecProfileReasons = audioStreamMatches.GetValueOrDefault(selectedAudioStream); |
| 254 | 1351 | | } |
| 254 | 1352 | | } |
| 254 | 1353 | |
|
| 254 | 1354 | | var failureReasons = directPlayProfileReasons | containerProfileReasons | subtitleProfileReasons; |
| 254 | 1355 | |
|
| 254 | 1356 | | if ((failureReasons & TranscodeReason.VideoCodecNotSupported) == 0) |
| 254 | 1357 | | { |
| 254 | 1358 | | failureReasons |= videoCodecProfileReasons; |
| 254 | 1359 | | } |
| 254 | 1360 | |
|
| 254 | 1361 | | if ((failureReasons & TranscodeReason.AudioCodecNotSupported) == 0) |
| 254 | 1362 | | { |
| 254 | 1363 | | failureReasons |= audioCodecProfileReasons; |
| 254 | 1364 | | } |
| 254 | 1365 | |
|
| 254 | 1366 | | var directStreamFailureReasons = failureReasons & (~DirectStreamReasons); |
| 254 | 1367 | |
|
| 254 | 1368 | | PlayMethod? playMethod = null; |
| 254 | 1369 | | if (failureReasons == 0 && isEligibleForDirectPlay && mediaSource.SupportsDirectPlay) |
| 254 | 1370 | | { |
| 254 | 1371 | | playMethod = PlayMethod.DirectPlay; |
| 254 | 1372 | | } |
| 254 | 1373 | | else if (directStreamFailureReasons == 0 && isEligibleForDirectStream && mediaSource.SupportsDirectS |
| 254 | 1374 | | { |
| 254 | 1375 | | playMethod = PlayMethod.DirectStream; |
| 254 | 1376 | | } |
| 254 | 1377 | |
|
| 254 | 1378 | | var ranked = GetRank(ref failureReasons, rankings); |
| 254 | 1379 | |
|
| 254 | 1380 | | return (Result: (Profile: directPlayProfile, PlayMethod: playMethod, AudioStreamIndex: selectedAudio |
| 254 | 1381 | | }) |
| 254 | 1382 | | .OrderByDescending(analysis => analysis.Result.PlayMethod) |
| 254 | 1383 | | .ThenByDescending(analysis => analysis.Rank) |
| 254 | 1384 | | .ThenBy(analysis => analysis.Order) |
| 254 | 1385 | | .ToArray() |
| 254 | 1386 | | .ToLookup(analysis => analysis.Result.PlayMethod is not null); |
| | 1387 | |
|
| 254 | 1388 | | var profileMatch = analyzedProfiles[true] |
| 254 | 1389 | | .Select(analysis => analysis.Result) |
| 254 | 1390 | | .FirstOrDefault(); |
| 254 | 1391 | | if (profileMatch.Profile is not null) |
| | 1392 | | { |
| 124 | 1393 | | return profileMatch; |
| | 1394 | | } |
| | 1395 | |
|
| 130 | 1396 | | var failureReasons = analyzedProfiles[false] |
| 130 | 1397 | | .Select(analysis => analysis.Result) |
| 130 | 1398 | | .Where(result => !containerSupported || !result.TranscodeReason.HasFlag(TranscodeReason.ContainerNotSupp |
| 130 | 1399 | | .FirstOrDefault().TranscodeReason; |
| 130 | 1400 | | if (failureReasons == 0) |
| | 1401 | | { |
| 14 | 1402 | | failureReasons = TranscodeReason.DirectPlayError; |
| | 1403 | | } |
| | 1404 | |
|
| 130 | 1405 | | return (Profile: null, PlayMethod: null, AudioStreamIndex: null, TranscodeReasons: failureReasons); |
| | 1406 | | } |
| | 1407 | |
|
| | 1408 | | private TranscodeReason AggregateFailureConditions(MediaSourceInfo mediaSource, DeviceProfile profile, string ty |
| | 1409 | | { |
| 1661 | 1410 | | return conditions.Aggregate<ProfileCondition, TranscodeReason>(0, (reasons, i) => |
| 1661 | 1411 | | { |
| 1661 | 1412 | | LogConditionFailure(profile, type, i, mediaSource); |
| 1661 | 1413 | | var transcodeReasons = GetTranscodeReasonForFailedCondition(i); |
| 1661 | 1414 | | return reasons | transcodeReasons; |
| 1661 | 1415 | | }); |
| | 1416 | | } |
| | 1417 | |
|
| | 1418 | | private void LogConditionFailure(DeviceProfile profile, string type, ProfileCondition condition, MediaSourceInfo |
| | 1419 | | { |
| 247 | 1420 | | _logger.LogDebug( |
| 247 | 1421 | | "Profile: {0}, DirectPlay=false. Reason={1}.{2} Condition: {3}. ConditionValue: {4}. IsRequired: {5}. Pa |
| 247 | 1422 | | type, |
| 247 | 1423 | | profile.Name ?? "Unknown Profile", |
| 247 | 1424 | | condition.Property, |
| 247 | 1425 | | condition.Condition, |
| 247 | 1426 | | condition.Value ?? string.Empty, |
| 247 | 1427 | | condition.IsRequired, |
| 247 | 1428 | | mediaSource.Path ?? "Unknown path"); |
| 247 | 1429 | | } |
| | 1430 | |
|
| | 1431 | | /// <summary> |
| | 1432 | | /// Normalizes input container. |
| | 1433 | | /// </summary> |
| | 1434 | | /// <param name="mediaSource">The <see cref="MediaSourceInfo"/>.</param> |
| | 1435 | | /// <param name="subtitleStream">The <see cref="MediaStream"/> of the subtitle stream.</param> |
| | 1436 | | /// <param name="subtitleProfiles">The list of supported <see cref="SubtitleProfile"/>s.</param> |
| | 1437 | | /// <param name="playMethod">The <see cref="PlayMethod"/>.</param> |
| | 1438 | | /// <param name="transcoderSupport">The <see cref="ITranscoderSupport"/>.</param> |
| | 1439 | | /// <param name="outputContainer">The output container.</param> |
| | 1440 | | /// <param name="transcodingSubProtocol">The subtitle transcoding protocol.</param> |
| | 1441 | | /// <returns>The normalized input container.</returns> |
| | 1442 | | public static SubtitleProfile GetSubtitleProfile( |
| | 1443 | | MediaSourceInfo mediaSource, |
| | 1444 | | MediaStream subtitleStream, |
| | 1445 | | SubtitleProfile[] subtitleProfiles, |
| | 1446 | | PlayMethod playMethod, |
| | 1447 | | ITranscoderSupport transcoderSupport, |
| | 1448 | | string? outputContainer, |
| | 1449 | | MediaStreamProtocol? transcodingSubProtocol) |
| | 1450 | | { |
| 462 | 1451 | | if (!subtitleStream.IsExternal && (playMethod != PlayMethod.Transcode || transcodingSubProtocol != MediaStre |
| | 1452 | | { |
| | 1453 | | // Look for supported embedded subs of the same format |
| 0 | 1454 | | foreach (var profile in subtitleProfiles) |
| | 1455 | | { |
| 0 | 1456 | | if (!profile.SupportsLanguage(subtitleStream.Language)) |
| | 1457 | | { |
| | 1458 | | continue; |
| | 1459 | | } |
| | 1460 | |
|
| 0 | 1461 | | if (profile.Method != SubtitleDeliveryMethod.Embed) |
| | 1462 | | { |
| | 1463 | | continue; |
| | 1464 | | } |
| | 1465 | |
|
| 0 | 1466 | | if (!ContainerHelper.ContainsContainer(profile.Container, outputContainer)) |
| | 1467 | | { |
| | 1468 | | continue; |
| | 1469 | | } |
| | 1470 | |
|
| 0 | 1471 | | if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(outputContainer)) |
| | 1472 | | { |
| | 1473 | | continue; |
| | 1474 | | } |
| | 1475 | |
|
| 0 | 1476 | | if (subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format) && string.Equals |
| | 1477 | | { |
| 0 | 1478 | | return profile; |
| | 1479 | | } |
| | 1480 | | } |
| | 1481 | |
|
| | 1482 | | // Look for supported embedded subs of a convertible format |
| 0 | 1483 | | foreach (var profile in subtitleProfiles) |
| | 1484 | | { |
| 0 | 1485 | | if (!profile.SupportsLanguage(subtitleStream.Language)) |
| | 1486 | | { |
| | 1487 | | continue; |
| | 1488 | | } |
| | 1489 | |
|
| 0 | 1490 | | if (profile.Method != SubtitleDeliveryMethod.Embed) |
| | 1491 | | { |
| | 1492 | | continue; |
| | 1493 | | } |
| | 1494 | |
|
| 0 | 1495 | | if (!ContainerHelper.ContainsContainer(profile.Container, outputContainer)) |
| | 1496 | | { |
| | 1497 | | continue; |
| | 1498 | | } |
| | 1499 | |
|
| 0 | 1500 | | if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(outputContainer)) |
| | 1501 | | { |
| | 1502 | | continue; |
| | 1503 | | } |
| | 1504 | |
|
| 0 | 1505 | | if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsSubtitleConversionTo(profile.Forma |
| | 1506 | | { |
| 0 | 1507 | | return profile; |
| | 1508 | | } |
| | 1509 | | } |
| | 1510 | | } |
| | 1511 | |
|
| | 1512 | | // Look for an external or hls profile that matches the stream type (text/graphical) and doesn't require con |
| 462 | 1513 | | return GetExternalSubtitleProfile(mediaSource, subtitleStream, subtitleProfiles, playMethod, transcoderSuppo |
| 462 | 1514 | | GetExternalSubtitleProfile(mediaSource, subtitleStream, subtitleProfiles, playMethod, transcoderSupport, |
| 462 | 1515 | | new SubtitleProfile |
| 462 | 1516 | | { |
| 462 | 1517 | | Method = SubtitleDeliveryMethod.Encode, |
| 462 | 1518 | | Format = subtitleStream.Codec |
| 462 | 1519 | | }; |
| | 1520 | | } |
| | 1521 | |
|
| | 1522 | | private static bool IsSubtitleEmbedSupported(string? transcodingContainer) |
| | 1523 | | { |
| 0 | 1524 | | if (!string.IsNullOrEmpty(transcodingContainer)) |
| | 1525 | | { |
| 0 | 1526 | | if (ContainerHelper.ContainsContainer(transcodingContainer, "ts,mpegts,mp4")) |
| | 1527 | | { |
| 0 | 1528 | | return false; |
| | 1529 | | } |
| | 1530 | |
|
| 0 | 1531 | | if (ContainerHelper.ContainsContainer(transcodingContainer, "mkv,matroska")) |
| | 1532 | | { |
| 0 | 1533 | | return true; |
| | 1534 | | } |
| | 1535 | | } |
| | 1536 | |
|
| 0 | 1537 | | return false; |
| | 1538 | | } |
| | 1539 | |
|
| | 1540 | | private static SubtitleProfile? GetExternalSubtitleProfile(MediaSourceInfo mediaSource, MediaStream subtitleStre |
| | 1541 | | { |
| 5066 | 1542 | | foreach (var profile in subtitleProfiles) |
| | 1543 | | { |
| 2016 | 1544 | | if (profile.Method != SubtitleDeliveryMethod.External && profile.Method != SubtitleDeliveryMethod.Hls) |
| | 1545 | | { |
| | 1546 | | continue; |
| | 1547 | | } |
| | 1548 | |
|
| 1382 | 1549 | | if (profile.Method == SubtitleDeliveryMethod.Hls && playMethod != PlayMethod.Transcode) |
| | 1550 | | { |
| | 1551 | | continue; |
| | 1552 | | } |
| | 1553 | |
|
| 1382 | 1554 | | if (!profile.SupportsLanguage(subtitleStream.Language)) |
| | 1555 | | { |
| | 1556 | | continue; |
| | 1557 | | } |
| | 1558 | |
|
| 1382 | 1559 | | if (!subtitleStream.IsExternal && !transcoderSupport.CanExtractSubtitles(subtitleStream.Codec)) |
| | 1560 | | { |
| | 1561 | | continue; |
| | 1562 | | } |
| | 1563 | |
|
| 1382 | 1564 | | if ((profile.Method == SubtitleDeliveryMethod.External && subtitleStream.IsTextSubtitleStream == MediaSt |
| 1382 | 1565 | | (profile.Method == SubtitleDeliveryMethod.Hls && subtitleStream.IsTextSubtitleStream)) |
| | 1566 | | { |
| 1364 | 1567 | | bool requiresConversion = !string.Equals(subtitleStream.Codec, profile.Format, StringComparison.Ordi |
| | 1568 | |
|
| 1364 | 1569 | | if (!requiresConversion) |
| | 1570 | | { |
| 176 | 1571 | | return profile; |
| | 1572 | | } |
| | 1573 | |
|
| 1188 | 1574 | | if (!allowConversion) |
| | 1575 | | { |
| | 1576 | | continue; |
| | 1577 | | } |
| | 1578 | |
|
| | 1579 | | // TODO: Build this into subtitleStream.SupportsExternalStream |
| 286 | 1580 | | if (mediaSource.IsInfiniteStream) |
| | 1581 | | { |
| | 1582 | | continue; |
| | 1583 | | } |
| | 1584 | |
|
| 286 | 1585 | | if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsExternalStream && subtitleStream.S |
| | 1586 | | { |
| 286 | 1587 | | return profile; |
| | 1588 | | } |
| | 1589 | | } |
| | 1590 | | } |
| | 1591 | |
|
| 286 | 1592 | | return null; |
| | 1593 | | } |
| | 1594 | |
|
| | 1595 | | private bool IsBitrateLimitExceeded(MediaSourceInfo item, long maxBitrate) |
| | 1596 | | { |
| | 1597 | | // Don't restrict bitrate if item is remote. |
| 278 | 1598 | | if (item.IsRemote) |
| | 1599 | | { |
| 0 | 1600 | | return false; |
| | 1601 | | } |
| | 1602 | |
|
| | 1603 | | // If no maximum bitrate is set, default to no maximum bitrate. |
| 278 | 1604 | | long requestedMaxBitrate = maxBitrate > 0 ? maxBitrate : int.MaxValue; |
| | 1605 | |
|
| | 1606 | | // If we don't know the item bitrate, then force a transcode if requested max bitrate is under 40 mbps |
| 278 | 1607 | | int itemBitrate = item.Bitrate ?? 40000000; |
| | 1608 | |
|
| 278 | 1609 | | if (itemBitrate > requestedMaxBitrate) |
| | 1610 | | { |
| 24 | 1611 | | _logger.LogDebug( |
| 24 | 1612 | | "Bitrate exceeds limit: media bitrate: {MediaBitrate}, max bitrate: {MaxBitrate}", |
| 24 | 1613 | | itemBitrate, |
| 24 | 1614 | | requestedMaxBitrate); |
| 24 | 1615 | | return true; |
| | 1616 | | } |
| | 1617 | |
|
| 254 | 1618 | | return false; |
| | 1619 | | } |
| | 1620 | |
|
| | 1621 | | private static void ValidateMediaOptions(MediaOptions options, bool isMediaSource) |
| | 1622 | | { |
| 278 | 1623 | | if (options.ItemId.IsEmpty()) |
| | 1624 | | { |
| 0 | 1625 | | ArgumentException.ThrowIfNullOrEmpty(options.DeviceId); |
| | 1626 | | } |
| | 1627 | |
|
| 278 | 1628 | | if (options.Profile is null) |
| | 1629 | | { |
| 0 | 1630 | | throw new ArgumentException("Profile is required"); |
| | 1631 | | } |
| | 1632 | |
|
| 278 | 1633 | | if (options.MediaSources is null) |
| | 1634 | | { |
| 0 | 1635 | | throw new ArgumentException("MediaSources is required"); |
| | 1636 | | } |
| | 1637 | |
|
| 278 | 1638 | | if (isMediaSource) |
| | 1639 | | { |
| 278 | 1640 | | if (options.AudioStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId)) |
| | 1641 | | { |
| 0 | 1642 | | throw new ArgumentException("MediaSourceId is required when a specific audio stream is requested"); |
| | 1643 | | } |
| | 1644 | |
|
| 278 | 1645 | | if (options.SubtitleStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId)) |
| | 1646 | | { |
| 0 | 1647 | | throw new ArgumentException("MediaSourceId is required when a specific subtitle stream is requested" |
| | 1648 | | } |
| | 1649 | | } |
| 278 | 1650 | | } |
| | 1651 | |
|
| | 1652 | | private static IEnumerable<ProfileCondition> GetProfileConditionsForVideoAudio( |
| | 1653 | | IEnumerable<CodecProfile> codecProfiles, |
| | 1654 | | string container, |
| | 1655 | | string codec, |
| | 1656 | | int? audioChannels, |
| | 1657 | | int? audioBitrate, |
| | 1658 | | int? audioSampleRate, |
| | 1659 | | int? audioBitDepth, |
| | 1660 | | string audioProfile, |
| | 1661 | | bool? isSecondaryAudio) |
| | 1662 | | { |
| 1005 | 1663 | | return codecProfiles |
| 1005 | 1664 | | .Where(profile => profile.Type == CodecType.VideoAudio && |
| 1005 | 1665 | | profile.ContainsAnyCodec(codec, container) && |
| 1005 | 1666 | | profile.ApplyConditions.All(applyCondition => ConditionProcessor.IsVideoAudioConditionSatisfied(appl |
| 1005 | 1667 | | .SelectMany(profile => profile.Conditions) |
| 1005 | 1668 | | .Where(condition => !ConditionProcessor.IsVideoAudioConditionSatisfied(condition, audioChannels, audioBi |
| | 1669 | | } |
| | 1670 | |
|
| | 1671 | | private static IEnumerable<ProfileCondition> GetProfileConditionsForAudio( |
| | 1672 | | IEnumerable<CodecProfile> codecProfiles, |
| | 1673 | | string container, |
| | 1674 | | string? codec, |
| | 1675 | | int? audioChannels, |
| | 1676 | | int? audioBitrate, |
| | 1677 | | int? audioSampleRate, |
| | 1678 | | int? audioBitDepth, |
| | 1679 | | bool checkConditions) |
| | 1680 | | { |
| 0 | 1681 | | var conditions = codecProfiles |
| 0 | 1682 | | .Where(profile => profile.Type == CodecType.Audio && |
| 0 | 1683 | | profile.ContainsAnyCodec(codec, container) && |
| 0 | 1684 | | profile.ApplyConditions.All(applyCondition => ConditionProcessor.IsAudioConditionSatisfied(applyCond |
| 0 | 1685 | | .SelectMany(profile => profile.Conditions); |
| | 1686 | |
|
| 0 | 1687 | | if (!checkConditions) |
| | 1688 | | { |
| 0 | 1689 | | return conditions; |
| | 1690 | | } |
| | 1691 | |
|
| 0 | 1692 | | return conditions.Where(condition => !ConditionProcessor.IsAudioConditionSatisfied(condition, audioChannels, |
| | 1693 | | } |
| | 1694 | |
|
| | 1695 | | private void ApplyTranscodingConditions(StreamInfo item, IEnumerable<ProfileCondition> conditions, string? quali |
| | 1696 | | { |
| 3492 | 1697 | | foreach (ProfileCondition condition in conditions) |
| | 1698 | | { |
| 1244 | 1699 | | string value = condition.Value; |
| | 1700 | |
|
| 1244 | 1701 | | if (string.IsNullOrEmpty(value)) |
| | 1702 | | { |
| | 1703 | | continue; |
| | 1704 | | } |
| | 1705 | |
|
| | 1706 | | // No way to express this |
| 1244 | 1707 | | if (condition.Condition == ProfileConditionType.GreaterThanEqual) |
| | 1708 | | { |
| | 1709 | | continue; |
| | 1710 | | } |
| | 1711 | |
|
| 1244 | 1712 | | switch (condition.Property) |
| | 1713 | | { |
| | 1714 | | case ProfileConditionValue.AudioBitrate: |
| | 1715 | | { |
| 0 | 1716 | | if (!enableNonQualifiedConditions) |
| | 1717 | | { |
| | 1718 | | continue; |
| | 1719 | | } |
| | 1720 | |
|
| 0 | 1721 | | if (int.TryParse(value, CultureInfo.InvariantCulture, out var num)) |
| | 1722 | | { |
| 0 | 1723 | | if (condition.Condition == ProfileConditionType.Equals) |
| | 1724 | | { |
| 0 | 1725 | | item.AudioBitrate = num; |
| | 1726 | | } |
| 0 | 1727 | | else if (condition.Condition == ProfileConditionType.LessThanEqual) |
| | 1728 | | { |
| 0 | 1729 | | item.AudioBitrate = Math.Min(num, item.AudioBitrate ?? num); |
| | 1730 | | } |
| 0 | 1731 | | else if (condition.Condition == ProfileConditionType.GreaterThanEqual) |
| | 1732 | | { |
| 0 | 1733 | | item.AudioBitrate = Math.Max(num, item.AudioBitrate ?? num); |
| | 1734 | | } |
| | 1735 | | } |
| | 1736 | |
|
| 0 | 1737 | | break; |
| | 1738 | | } |
| | 1739 | |
|
| | 1740 | | case ProfileConditionValue.AudioSampleRate: |
| | 1741 | | { |
| 0 | 1742 | | if (!enableNonQualifiedConditions) |
| | 1743 | | { |
| | 1744 | | continue; |
| | 1745 | | } |
| | 1746 | |
|
| 0 | 1747 | | if (int.TryParse(value, CultureInfo.InvariantCulture, out var num)) |
| | 1748 | | { |
| 0 | 1749 | | if (condition.Condition == ProfileConditionType.Equals) |
| | 1750 | | { |
| 0 | 1751 | | item.AudioSampleRate = num; |
| | 1752 | | } |
| 0 | 1753 | | else if (condition.Condition == ProfileConditionType.LessThanEqual) |
| | 1754 | | { |
| 0 | 1755 | | item.AudioSampleRate = Math.Min(num, item.AudioSampleRate ?? num); |
| | 1756 | | } |
| 0 | 1757 | | else if (condition.Condition == ProfileConditionType.GreaterThanEqual) |
| | 1758 | | { |
| 0 | 1759 | | item.AudioSampleRate = Math.Max(num, item.AudioSampleRate ?? num); |
| | 1760 | | } |
| | 1761 | | } |
| | 1762 | |
|
| 0 | 1763 | | break; |
| | 1764 | | } |
| | 1765 | |
|
| | 1766 | | case ProfileConditionValue.AudioChannels: |
| | 1767 | | { |
| 28 | 1768 | | if (string.IsNullOrEmpty(qualifier)) |
| | 1769 | | { |
| 0 | 1770 | | if (!enableNonQualifiedConditions) |
| | 1771 | | { |
| 0 | 1772 | | continue; |
| | 1773 | | } |
| | 1774 | | } |
| | 1775 | | else |
| | 1776 | | { |
| 28 | 1777 | | if (!enableQualifiedConditions) |
| | 1778 | | { |
| | 1779 | | continue; |
| | 1780 | | } |
| | 1781 | | } |
| | 1782 | |
|
| 28 | 1783 | | if (int.TryParse(value, CultureInfo.InvariantCulture, out var num)) |
| | 1784 | | { |
| 28 | 1785 | | if (condition.Condition == ProfileConditionType.Equals) |
| | 1786 | | { |
| 0 | 1787 | | item.SetOption(qualifier, "audiochannels", num.ToString(CultureInfo.InvariantCulture |
| | 1788 | | } |
| 28 | 1789 | | else if (condition.Condition == ProfileConditionType.LessThanEqual) |
| | 1790 | | { |
| 28 | 1791 | | item.SetOption(qualifier, "audiochannels", Math.Min(num, item.GetTargetAudioChannels |
| | 1792 | | } |
| 0 | 1793 | | else if (condition.Condition == ProfileConditionType.GreaterThanEqual) |
| | 1794 | | { |
| 0 | 1795 | | item.SetOption(qualifier, "audiochannels", Math.Max(num, item.GetTargetAudioChannels |
| | 1796 | | } |
| | 1797 | | } |
| | 1798 | |
|
| 0 | 1799 | | break; |
| | 1800 | | } |
| | 1801 | |
|
| | 1802 | | case ProfileConditionValue.IsAvc: |
| | 1803 | | { |
| 0 | 1804 | | if (!enableNonQualifiedConditions) |
| | 1805 | | { |
| | 1806 | | continue; |
| | 1807 | | } |
| | 1808 | |
|
| 0 | 1809 | | if (bool.TryParse(value, out var isAvc)) |
| | 1810 | | { |
| 0 | 1811 | | if (isAvc && condition.Condition == ProfileConditionType.Equals) |
| | 1812 | | { |
| 0 | 1813 | | item.RequireAvc = true; |
| | 1814 | | } |
| 0 | 1815 | | else if (!isAvc && condition.Condition == ProfileConditionType.NotEquals) |
| | 1816 | | { |
| 0 | 1817 | | item.RequireAvc = true; |
| | 1818 | | } |
| | 1819 | | } |
| | 1820 | |
|
| 0 | 1821 | | break; |
| | 1822 | | } |
| | 1823 | |
|
| | 1824 | | case ProfileConditionValue.IsAnamorphic: |
| | 1825 | | { |
| 201 | 1826 | | if (!enableNonQualifiedConditions) |
| | 1827 | | { |
| | 1828 | | continue; |
| | 1829 | | } |
| | 1830 | |
|
| 201 | 1831 | | if (bool.TryParse(value, out var isAnamorphic)) |
| | 1832 | | { |
| 201 | 1833 | | if (isAnamorphic && condition.Condition == ProfileConditionType.Equals) |
| | 1834 | | { |
| 0 | 1835 | | item.RequireNonAnamorphic = true; |
| | 1836 | | } |
| 201 | 1837 | | else if (!isAnamorphic && condition.Condition == ProfileConditionType.NotEquals) |
| | 1838 | | { |
| 0 | 1839 | | item.RequireNonAnamorphic = true; |
| | 1840 | | } |
| | 1841 | | } |
| | 1842 | |
|
| 0 | 1843 | | break; |
| | 1844 | | } |
| | 1845 | |
|
| | 1846 | | case ProfileConditionValue.IsInterlaced: |
| | 1847 | | { |
| 109 | 1848 | | if (string.IsNullOrEmpty(qualifier)) |
| | 1849 | | { |
| 0 | 1850 | | if (!enableNonQualifiedConditions) |
| | 1851 | | { |
| 0 | 1852 | | continue; |
| | 1853 | | } |
| | 1854 | | } |
| | 1855 | | else |
| | 1856 | | { |
| 109 | 1857 | | if (!enableQualifiedConditions) |
| | 1858 | | { |
| | 1859 | | continue; |
| | 1860 | | } |
| | 1861 | | } |
| | 1862 | |
|
| 109 | 1863 | | if (bool.TryParse(value, out var isInterlaced)) |
| | 1864 | | { |
| 109 | 1865 | | if (!isInterlaced && condition.Condition == ProfileConditionType.Equals) |
| | 1866 | | { |
| 0 | 1867 | | item.SetOption(qualifier, "deinterlace", "true"); |
| | 1868 | | } |
| 109 | 1869 | | else if (isInterlaced && condition.Condition == ProfileConditionType.NotEquals) |
| | 1870 | | { |
| 109 | 1871 | | item.SetOption(qualifier, "deinterlace", "true"); |
| | 1872 | | } |
| | 1873 | | } |
| | 1874 | |
|
| 109 | 1875 | | break; |
| | 1876 | | } |
| | 1877 | |
|
| | 1878 | | case ProfileConditionValue.AudioProfile: |
| | 1879 | | case ProfileConditionValue.Has64BitOffsets: |
| | 1880 | | case ProfileConditionValue.PacketLength: |
| | 1881 | | case ProfileConditionValue.NumStreams: |
| | 1882 | | case ProfileConditionValue.NumAudioStreams: |
| | 1883 | | case ProfileConditionValue.NumVideoStreams: |
| | 1884 | | case ProfileConditionValue.IsSecondaryAudio: |
| | 1885 | | case ProfileConditionValue.VideoTimestamp: |
| | 1886 | | { |
| | 1887 | | // Not supported yet |
| | 1888 | | break; |
| | 1889 | | } |
| | 1890 | |
|
| | 1891 | | case ProfileConditionValue.RefFrames: |
| | 1892 | | { |
| 2 | 1893 | | if (string.IsNullOrEmpty(qualifier)) |
| | 1894 | | { |
| 0 | 1895 | | if (!enableNonQualifiedConditions) |
| | 1896 | | { |
| 0 | 1897 | | continue; |
| | 1898 | | } |
| | 1899 | | } |
| | 1900 | | else |
| | 1901 | | { |
| 2 | 1902 | | if (!enableQualifiedConditions) |
| | 1903 | | { |
| | 1904 | | continue; |
| | 1905 | | } |
| | 1906 | | } |
| | 1907 | |
|
| 2 | 1908 | | if (int.TryParse(value, CultureInfo.InvariantCulture, out var num)) |
| | 1909 | | { |
| 2 | 1910 | | if (condition.Condition == ProfileConditionType.Equals) |
| | 1911 | | { |
| 0 | 1912 | | item.SetOption(qualifier, "maxrefframes", num.ToString(CultureInfo.InvariantCulture) |
| | 1913 | | } |
| 2 | 1914 | | else if (condition.Condition == ProfileConditionType.LessThanEqual) |
| | 1915 | | { |
| 2 | 1916 | | item.SetOption(qualifier, "maxrefframes", Math.Min(num, item.GetTargetRefFrames(qual |
| | 1917 | | } |
| 0 | 1918 | | else if (condition.Condition == ProfileConditionType.GreaterThanEqual) |
| | 1919 | | { |
| 0 | 1920 | | item.SetOption(qualifier, "maxrefframes", Math.Max(num, item.GetTargetRefFrames(qual |
| | 1921 | | } |
| | 1922 | | } |
| | 1923 | |
|
| 0 | 1924 | | break; |
| | 1925 | | } |
| | 1926 | |
|
| | 1927 | | case ProfileConditionValue.VideoBitDepth: |
| | 1928 | | { |
| 0 | 1929 | | if (string.IsNullOrEmpty(qualifier)) |
| | 1930 | | { |
| 0 | 1931 | | if (!enableNonQualifiedConditions) |
| | 1932 | | { |
| 0 | 1933 | | continue; |
| | 1934 | | } |
| | 1935 | | } |
| | 1936 | | else |
| | 1937 | | { |
| 0 | 1938 | | if (!enableQualifiedConditions) |
| | 1939 | | { |
| | 1940 | | continue; |
| | 1941 | | } |
| | 1942 | | } |
| | 1943 | |
|
| 0 | 1944 | | if (int.TryParse(value, CultureInfo.InvariantCulture, out var num)) |
| | 1945 | | { |
| 0 | 1946 | | if (condition.Condition == ProfileConditionType.Equals) |
| | 1947 | | { |
| 0 | 1948 | | item.SetOption(qualifier, "videobitdepth", num.ToString(CultureInfo.InvariantCulture |
| | 1949 | | } |
| 0 | 1950 | | else if (condition.Condition == ProfileConditionType.LessThanEqual) |
| | 1951 | | { |
| 0 | 1952 | | item.SetOption(qualifier, "videobitdepth", Math.Min(num, item.GetTargetVideoBitDepth |
| | 1953 | | } |
| 0 | 1954 | | else if (condition.Condition == ProfileConditionType.GreaterThanEqual) |
| | 1955 | | { |
| 0 | 1956 | | item.SetOption(qualifier, "videobitdepth", Math.Max(num, item.GetTargetVideoBitDepth |
| | 1957 | | } |
| | 1958 | | } |
| | 1959 | |
|
| 0 | 1960 | | break; |
| | 1961 | | } |
| | 1962 | |
|
| | 1963 | | case ProfileConditionValue.VideoProfile: |
| | 1964 | | { |
| 224 | 1965 | | if (string.IsNullOrEmpty(qualifier)) |
| | 1966 | | { |
| | 1967 | | continue; |
| | 1968 | | } |
| | 1969 | |
|
| | 1970 | | // Change from split by | to comma |
| | 1971 | | // Strip spaces to avoid having to encode |
| 224 | 1972 | | var values = value |
| 224 | 1973 | | .Split('|', StringSplitOptions.RemoveEmptyEntries); |
| | 1974 | |
|
| 224 | 1975 | | if (condition.Condition == ProfileConditionType.Equals) |
| | 1976 | | { |
| 0 | 1977 | | item.SetOption(qualifier, "profile", string.Join(',', values)); |
| | 1978 | | } |
| 224 | 1979 | | else if (condition.Condition == ProfileConditionType.EqualsAny) |
| | 1980 | | { |
| 224 | 1981 | | var currentValue = item.GetOption(qualifier, "profile"); |
| 224 | 1982 | | if (!string.IsNullOrEmpty(currentValue) && values.Any(value => value == currentValue)) |
| | 1983 | | { |
| 71 | 1984 | | item.SetOption(qualifier, "profile", currentValue); |
| | 1985 | | } |
| | 1986 | | else |
| | 1987 | | { |
| 153 | 1988 | | item.SetOption(qualifier, "profile", string.Join(',', values)); |
| | 1989 | | } |
| | 1990 | | } |
| | 1991 | |
|
| 153 | 1992 | | break; |
| | 1993 | | } |
| | 1994 | |
|
| | 1995 | | case ProfileConditionValue.VideoRangeType: |
| | 1996 | | { |
| 258 | 1997 | | if (string.IsNullOrEmpty(qualifier)) |
| | 1998 | | { |
| | 1999 | | continue; |
| | 2000 | | } |
| | 2001 | |
|
| | 2002 | | // change from split by | to comma |
| | 2003 | | // strip spaces to avoid having to encode |
| 258 | 2004 | | var values = value |
| 258 | 2005 | | .Split('|', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); |
| | 2006 | |
|
| 258 | 2007 | | if (condition.Condition == ProfileConditionType.Equals) |
| | 2008 | | { |
| 0 | 2009 | | item.SetOption(qualifier, "rangetype", string.Join(',', values)); |
| | 2010 | | } |
| 258 | 2011 | | else if (condition.Condition == ProfileConditionType.NotEquals) |
| | 2012 | | { |
| 0 | 2013 | | item.SetOption(qualifier, "rangetype", string.Join(',', Enum.GetNames(typeof(VideoRangeT |
| | 2014 | | } |
| 258 | 2015 | | else if (condition.Condition == ProfileConditionType.EqualsAny) |
| | 2016 | | { |
| 258 | 2017 | | var currentValue = item.GetOption(qualifier, "rangetype"); |
| 258 | 2018 | | if (!string.IsNullOrEmpty(currentValue) && values.Any(v => string.Equals(v, currentValue |
| | 2019 | | { |
| 0 | 2020 | | item.SetOption(qualifier, "rangetype", currentValue); |
| | 2021 | | } |
| | 2022 | | else |
| | 2023 | | { |
| 258 | 2024 | | item.SetOption(qualifier, "rangetype", string.Join(',', values)); |
| | 2025 | | } |
| | 2026 | | } |
| | 2027 | |
|
| 258 | 2028 | | break; |
| | 2029 | | } |
| | 2030 | |
|
| | 2031 | | case ProfileConditionValue.VideoCodecTag: |
| | 2032 | | { |
| 12 | 2033 | | if (string.IsNullOrEmpty(qualifier)) |
| | 2034 | | { |
| | 2035 | | continue; |
| | 2036 | | } |
| | 2037 | |
|
| | 2038 | | // change from split by | to comma |
| | 2039 | | // strip spaces to avoid having to encode |
| 12 | 2040 | | var values = value |
| 12 | 2041 | | .Split('|', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); |
| | 2042 | |
|
| 12 | 2043 | | if (condition.Condition == ProfileConditionType.Equals) |
| | 2044 | | { |
| 0 | 2045 | | item.SetOption(qualifier, "codectag", string.Join(',', values)); |
| | 2046 | | } |
| 12 | 2047 | | else if (condition.Condition == ProfileConditionType.EqualsAny) |
| | 2048 | | { |
| 12 | 2049 | | var currentValue = item.GetOption(qualifier, "codectag"); |
| 12 | 2050 | | if (!string.IsNullOrEmpty(currentValue) && values.Any(v => string.Equals(v, currentValue |
| | 2051 | | { |
| 0 | 2052 | | item.SetOption(qualifier, "codectag", currentValue); |
| | 2053 | | } |
| | 2054 | | else |
| | 2055 | | { |
| 12 | 2056 | | item.SetOption(qualifier, "codectag", string.Join(',', values)); |
| | 2057 | | } |
| | 2058 | | } |
| | 2059 | |
|
| 12 | 2060 | | break; |
| | 2061 | | } |
| | 2062 | |
|
| | 2063 | | case ProfileConditionValue.Height: |
| | 2064 | | { |
| 0 | 2065 | | if (!enableNonQualifiedConditions) |
| | 2066 | | { |
| | 2067 | | continue; |
| | 2068 | | } |
| | 2069 | |
|
| 0 | 2070 | | if (int.TryParse(value, CultureInfo.InvariantCulture, out var num)) |
| | 2071 | | { |
| 0 | 2072 | | if (condition.Condition == ProfileConditionType.Equals) |
| | 2073 | | { |
| 0 | 2074 | | item.MaxHeight = num; |
| | 2075 | | } |
| 0 | 2076 | | else if (condition.Condition == ProfileConditionType.LessThanEqual) |
| | 2077 | | { |
| 0 | 2078 | | item.MaxHeight = Math.Min(num, item.MaxHeight ?? num); |
| | 2079 | | } |
| 0 | 2080 | | else if (condition.Condition == ProfileConditionType.GreaterThanEqual) |
| | 2081 | | { |
| 0 | 2082 | | item.MaxHeight = Math.Max(num, item.MaxHeight ?? num); |
| | 2083 | | } |
| | 2084 | | } |
| | 2085 | |
|
| 0 | 2086 | | break; |
| | 2087 | | } |
| | 2088 | |
|
| | 2089 | | case ProfileConditionValue.VideoBitrate: |
| | 2090 | | { |
| 32 | 2091 | | if (!enableNonQualifiedConditions) |
| | 2092 | | { |
| | 2093 | | continue; |
| | 2094 | | } |
| | 2095 | |
|
| 32 | 2096 | | if (int.TryParse(value, CultureInfo.InvariantCulture, out var num)) |
| | 2097 | | { |
| 32 | 2098 | | if (condition.Condition == ProfileConditionType.Equals) |
| | 2099 | | { |
| 0 | 2100 | | item.VideoBitrate = num; |
| | 2101 | | } |
| 32 | 2102 | | else if (condition.Condition == ProfileConditionType.LessThanEqual) |
| | 2103 | | { |
| 32 | 2104 | | item.VideoBitrate = Math.Min(num, item.VideoBitrate ?? num); |
| | 2105 | | } |
| 0 | 2106 | | else if (condition.Condition == ProfileConditionType.GreaterThanEqual) |
| | 2107 | | { |
| 0 | 2108 | | item.VideoBitrate = Math.Max(num, item.VideoBitrate ?? num); |
| | 2109 | | } |
| | 2110 | | } |
| | 2111 | |
|
| 0 | 2112 | | break; |
| | 2113 | | } |
| | 2114 | |
|
| | 2115 | | case ProfileConditionValue.VideoFramerate: |
| | 2116 | | { |
| 12 | 2117 | | if (!enableNonQualifiedConditions) |
| | 2118 | | { |
| | 2119 | | continue; |
| | 2120 | | } |
| | 2121 | |
|
| 12 | 2122 | | if (float.TryParse(value, CultureInfo.InvariantCulture, out var num)) |
| | 2123 | | { |
| 12 | 2124 | | if (condition.Condition == ProfileConditionType.Equals) |
| | 2125 | | { |
| 0 | 2126 | | item.MaxFramerate = num; |
| | 2127 | | } |
| 12 | 2128 | | else if (condition.Condition == ProfileConditionType.LessThanEqual) |
| | 2129 | | { |
| 12 | 2130 | | item.MaxFramerate = Math.Min(num, item.MaxFramerate ?? num); |
| | 2131 | | } |
| 0 | 2132 | | else if (condition.Condition == ProfileConditionType.GreaterThanEqual) |
| | 2133 | | { |
| 0 | 2134 | | item.MaxFramerate = Math.Max(num, item.MaxFramerate ?? num); |
| | 2135 | | } |
| | 2136 | | } |
| | 2137 | |
|
| 0 | 2138 | | break; |
| | 2139 | | } |
| | 2140 | |
|
| | 2141 | | case ProfileConditionValue.VideoLevel: |
| | 2142 | | { |
| 212 | 2143 | | if (string.IsNullOrEmpty(qualifier)) |
| | 2144 | | { |
| | 2145 | | continue; |
| | 2146 | | } |
| | 2147 | |
|
| 212 | 2148 | | if (int.TryParse(value, CultureInfo.InvariantCulture, out var num)) |
| | 2149 | | { |
| 212 | 2150 | | if (condition.Condition == ProfileConditionType.Equals) |
| | 2151 | | { |
| 0 | 2152 | | item.SetOption(qualifier, "level", num.ToString(CultureInfo.InvariantCulture)); |
| | 2153 | | } |
| 212 | 2154 | | else if (condition.Condition == ProfileConditionType.LessThanEqual) |
| | 2155 | | { |
| 212 | 2156 | | item.SetOption(qualifier, "level", Math.Min(num, item.GetTargetVideoLevel(qualifier) |
| | 2157 | | } |
| 0 | 2158 | | else if (condition.Condition == ProfileConditionType.GreaterThanEqual) |
| | 2159 | | { |
| 0 | 2160 | | item.SetOption(qualifier, "level", Math.Max(num, item.GetTargetVideoLevel(qualifier) |
| | 2161 | | } |
| | 2162 | | } |
| | 2163 | |
|
| 0 | 2164 | | break; |
| | 2165 | | } |
| | 2166 | |
|
| | 2167 | | case ProfileConditionValue.Width: |
| | 2168 | | { |
| 4 | 2169 | | if (!enableNonQualifiedConditions) |
| | 2170 | | { |
| | 2171 | | continue; |
| | 2172 | | } |
| | 2173 | |
|
| 4 | 2174 | | if (int.TryParse(value, CultureInfo.InvariantCulture, out var num)) |
| | 2175 | | { |
| 4 | 2176 | | if (condition.Condition == ProfileConditionType.Equals) |
| | 2177 | | { |
| 0 | 2178 | | item.MaxWidth = num; |
| | 2179 | | } |
| 4 | 2180 | | else if (condition.Condition == ProfileConditionType.LessThanEqual) |
| | 2181 | | { |
| 4 | 2182 | | item.MaxWidth = Math.Min(num, item.MaxWidth ?? num); |
| | 2183 | | } |
| 0 | 2184 | | else if (condition.Condition == ProfileConditionType.GreaterThanEqual) |
| | 2185 | | { |
| 0 | 2186 | | item.MaxWidth = Math.Max(num, item.MaxWidth ?? num); |
| | 2187 | | } |
| | 2188 | | } |
| | 2189 | |
|
| | 2190 | | break; |
| | 2191 | | } |
| | 2192 | |
|
| | 2193 | | default: |
| | 2194 | | break; |
| | 2195 | | } |
| | 2196 | | } |
| 502 | 2197 | | } |
| | 2198 | |
|
| | 2199 | | private static bool IsAudioContainerSupported(DirectPlayProfile profile, MediaSourceInfo item) |
| | 2200 | | { |
| | 2201 | | // Check container type |
| 0 | 2202 | | if (!profile.SupportsContainer(item.Container)) |
| | 2203 | | { |
| 0 | 2204 | | return false; |
| | 2205 | | } |
| | 2206 | |
|
| | 2207 | | // Never direct play audio in matroska when the device only declare support for webm. |
| | 2208 | | // The first check is not enough because mkv is assumed can be webm. |
| | 2209 | | // See https://github.com/jellyfin/jellyfin/issues/13344 |
| 0 | 2210 | | return !ContainerHelper.ContainsContainer("mkv", item.Container) |
| 0 | 2211 | | || profile.SupportsContainer("mkv"); |
| | 2212 | | } |
| | 2213 | |
|
| | 2214 | | private static bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audi |
| | 2215 | | { |
| 0 | 2216 | | if (!IsAudioContainerSupported(profile, item)) |
| | 2217 | | { |
| 0 | 2218 | | return false; |
| | 2219 | | } |
| | 2220 | |
|
| | 2221 | | // Check audio codec |
| 0 | 2222 | | string? audioCodec = audioStream?.Codec; |
| 0 | 2223 | | if (!profile.SupportsAudioCodec(audioCodec)) |
| | 2224 | | { |
| 0 | 2225 | | return false; |
| | 2226 | | } |
| | 2227 | |
|
| 0 | 2228 | | return true; |
| | 2229 | | } |
| | 2230 | |
|
| | 2231 | | private static bool IsAudioDirectStreamSupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream au |
| | 2232 | | { |
| | 2233 | | // Check container type, this should NOT be supported |
| | 2234 | | // If the container is supported, the file should be directly played |
| 0 | 2235 | | if (IsAudioContainerSupported(profile, item)) |
| | 2236 | | { |
| 0 | 2237 | | return false; |
| | 2238 | | } |
| | 2239 | |
|
| | 2240 | | // Check audio codec, we cannot use the SupportsAudioCodec here |
| | 2241 | | // Because that one assumes empty container supports all codec, which is just useless |
| 0 | 2242 | | string? audioCodec = audioStream?.Codec; |
| 0 | 2243 | | return string.Equals(profile.AudioCodec, audioCodec, StringComparison.OrdinalIgnoreCase) |
| 0 | 2244 | | || string.Equals(profile.Container, audioCodec, StringComparison.OrdinalIgnoreCase); |
| | 2245 | | } |
| | 2246 | |
|
| | 2247 | | private int GetRank(ref TranscodeReason a, TranscodeReason[] rankings) |
| | 2248 | | { |
| 1209 | 2249 | | var index = 1; |
| 8740 | 2250 | | foreach (var flag in rankings) |
| | 2251 | | { |
| 3666 | 2252 | | var reason = a & flag; |
| 3666 | 2253 | | if (reason != 0) |
| | 2254 | | { |
| 1010 | 2255 | | return index; |
| | 2256 | | } |
| | 2257 | |
|
| 2656 | 2258 | | index++; |
| | 2259 | | } |
| | 2260 | |
|
| 199 | 2261 | | return index; |
| | 2262 | | } |
| | 2263 | |
|
| | 2264 | | /// <summary> |
| | 2265 | | /// Check the profile conditions. |
| | 2266 | | /// </summary> |
| | 2267 | | /// <param name="conditions">Profile conditions.</param> |
| | 2268 | | /// <param name="mediaSource">Media source.</param> |
| | 2269 | | /// <param name="videoStream">Video stream.</param> |
| | 2270 | | /// <returns>Failed profile conditions.</returns> |
| | 2271 | | private IEnumerable<ProfileCondition> CheckVideoConditions(ProfileCondition[] conditions, MediaSourceInfo mediaS |
| | 2272 | | { |
| 729 | 2273 | | int? width = videoStream?.Width; |
| 729 | 2274 | | int? height = videoStream?.Height; |
| 729 | 2275 | | int? bitDepth = videoStream?.BitDepth; |
| 729 | 2276 | | int? videoBitrate = videoStream?.BitRate; |
| 729 | 2277 | | double? videoLevel = videoStream?.Level; |
| 729 | 2278 | | string? videoProfile = videoStream?.Profile; |
| 729 | 2279 | | VideoRangeType? videoRangeType = videoStream?.VideoRangeType; |
| 729 | 2280 | | float videoFramerate = videoStream is null ? 0 : videoStream.ReferenceFrameRate ?? 0; |
| 729 | 2281 | | bool? isAnamorphic = videoStream?.IsAnamorphic; |
| 729 | 2282 | | bool? isInterlaced = videoStream?.IsInterlaced; |
| 729 | 2283 | | string? videoCodecTag = videoStream?.CodecTag; |
| 729 | 2284 | | bool? isAvc = videoStream?.IsAVC; |
| | 2285 | |
|
| 729 | 2286 | | TransportStreamTimestamp? timestamp = videoStream is null ? TransportStreamTimestamp.None : mediaSource.Time |
| 729 | 2287 | | int? packetLength = videoStream?.PacketLength; |
| 729 | 2288 | | int? refFrames = videoStream?.RefFrames; |
| | 2289 | |
|
| 729 | 2290 | | int numStreams = mediaSource.MediaStreams.Count; |
| 729 | 2291 | | int? numAudioStreams = mediaSource.GetStreamCount(MediaStreamType.Audio); |
| 729 | 2292 | | int? numVideoStreams = mediaSource.GetStreamCount(MediaStreamType.Video); |
| | 2293 | |
|
| 729 | 2294 | | return conditions.Where(applyCondition => !ConditionProcessor.IsVideoConditionSatisfied(applyCondition, widt |
| | 2295 | | } |
| | 2296 | |
|
| | 2297 | | /// <summary> |
| | 2298 | | /// Check the compatibility of the container. |
| | 2299 | | /// </summary> |
| | 2300 | | /// <param name="options">Media options.</param> |
| | 2301 | | /// <param name="mediaSource">Media source.</param> |
| | 2302 | | /// <param name="container">Container.</param> |
| | 2303 | | /// <param name="videoStream">Video stream.</param> |
| | 2304 | | /// <returns>Transcode reasons if the container is not fully compatible.</returns> |
| | 2305 | | private TranscodeReason GetCompatibilityContainer(MediaOptions options, MediaSourceInfo mediaSource, string cont |
| | 2306 | | { |
| 254 | 2307 | | var profile = options.Profile; |
| | 2308 | |
|
| 254 | 2309 | | var failures = AggregateFailureConditions( |
| 254 | 2310 | | mediaSource, |
| 254 | 2311 | | profile, |
| 254 | 2312 | | "VideoCodecProfile", |
| 254 | 2313 | | profile.ContainerProfiles |
| 254 | 2314 | | .Where(containerProfile => containerProfile.Type == DlnaProfileType.Video && containerProfile.Contai |
| 254 | 2315 | | .SelectMany(containerProfile => CheckVideoConditions(containerProfile.Conditions, mediaSource, video |
| | 2316 | |
|
| 254 | 2317 | | return failures; |
| | 2318 | | } |
| | 2319 | |
|
| | 2320 | | /// <summary> |
| | 2321 | | /// Check the compatibility of the video codec. |
| | 2322 | | /// </summary> |
| | 2323 | | /// <param name="options">Media options.</param> |
| | 2324 | | /// <param name="mediaSource">Media source.</param> |
| | 2325 | | /// <param name="container">Container.</param> |
| | 2326 | | /// <param name="videoStream">Video stream.</param> |
| | 2327 | | /// <returns>Transcode reasons if the video stream is not fully compatible.</returns> |
| | 2328 | | private TranscodeReason GetCompatibilityVideoCodec(MediaOptions options, MediaSourceInfo mediaSource, string con |
| | 2329 | | { |
| 402 | 2330 | | var profile = options.Profile; |
| | 2331 | |
|
| 402 | 2332 | | string videoCodec = videoStream.Codec; |
| | 2333 | |
|
| 402 | 2334 | | var failures = AggregateFailureConditions( |
| 402 | 2335 | | mediaSource, |
| 402 | 2336 | | profile, |
| 402 | 2337 | | "VideoCodecProfile", |
| 402 | 2338 | | profile.CodecProfiles |
| 402 | 2339 | | .Where(codecProfile => codecProfile.Type == CodecType.Video && |
| 402 | 2340 | | codecProfile.ContainsAnyCodec(videoCodec, container) && |
| 402 | 2341 | | !CheckVideoConditions(codecProfile.ApplyConditions, mediaSource, videoStream).Any()) |
| 402 | 2342 | | .SelectMany(codecProfile => CheckVideoConditions(codecProfile.Conditions, mediaSource, videoStream)) |
| | 2343 | |
|
| 402 | 2344 | | return failures; |
| | 2345 | | } |
| | 2346 | |
|
| | 2347 | | /// <summary> |
| | 2348 | | /// Check the compatibility of the audio codec. |
| | 2349 | | /// </summary> |
| | 2350 | | /// <param name="options">Media options.</param> |
| | 2351 | | /// <param name="mediaSource">Media source.</param> |
| | 2352 | | /// <param name="container">Container.</param> |
| | 2353 | | /// <param name="audioStream">Audio stream.</param> |
| | 2354 | | /// <param name="transcodingAudioCodec">Override audio codec.</param> |
| | 2355 | | /// <param name="isVideo">The media source is video.</param> |
| | 2356 | | /// <param name="isSecondaryAudio">The audio stream is secondary.</param> |
| | 2357 | | /// <returns>Transcode reasons if the audio stream is not fully compatible.</returns> |
| | 2358 | | private TranscodeReason GetCompatibilityAudioCodec(MediaOptions options, MediaSourceInfo mediaSource, string con |
| | 2359 | | { |
| 1005 | 2360 | | var profile = options.Profile; |
| | 2361 | |
|
| 1005 | 2362 | | var audioCodec = transcodingAudioCodec ?? audioStream.Codec; |
| 1005 | 2363 | | var audioProfile = audioStream.Profile; |
| 1005 | 2364 | | var audioChannels = audioStream.Channels; |
| 1005 | 2365 | | var audioBitrate = audioStream.BitRate; |
| 1005 | 2366 | | var audioSampleRate = audioStream.SampleRate; |
| 1005 | 2367 | | var audioBitDepth = audioStream.BitDepth; |
| | 2368 | |
|
| 1005 | 2369 | | var audioFailureConditions = isVideo |
| 1005 | 2370 | | ? GetProfileConditionsForVideoAudio(profile.CodecProfiles, container, audioCodec, audioChannels, audioBi |
| 1005 | 2371 | | : GetProfileConditionsForAudio(profile.CodecProfiles, container, audioCodec, audioChannels, audioBitrate |
| | 2372 | |
|
| 1005 | 2373 | | var failures = AggregateFailureConditions(mediaSource, profile, "AudioCodecProfile", audioFailureConditions) |
| | 2374 | |
|
| 1005 | 2375 | | return failures; |
| | 2376 | | } |
| | 2377 | |
|
| | 2378 | | /// <summary> |
| | 2379 | | /// Check the compatibility of the audio codec for direct playback. |
| | 2380 | | /// </summary> |
| | 2381 | | /// <param name="options">Media options.</param> |
| | 2382 | | /// <param name="mediaSource">Media source.</param> |
| | 2383 | | /// <param name="container">Container.</param> |
| | 2384 | | /// <param name="audioStream">Audio stream.</param> |
| | 2385 | | /// <param name="isVideo">The media source is video.</param> |
| | 2386 | | /// <param name="isSecondaryAudio">The audio stream is secondary.</param> |
| | 2387 | | /// <returns>Transcode reasons if the audio stream is not fully compatible for direct playback.</returns> |
| | 2388 | | private TranscodeReason GetCompatibilityAudioCodecDirect(MediaOptions options, MediaSourceInfo mediaSource, stri |
| | 2389 | | { |
| 273 | 2390 | | var failures = GetCompatibilityAudioCodec(options, mediaSource, container, audioStream, null, isVideo, isSec |
| | 2391 | |
|
| 273 | 2392 | | if (audioStream.IsExternal) |
| | 2393 | | { |
| 6 | 2394 | | failures |= TranscodeReason.AudioIsExternal; |
| | 2395 | | } |
| | 2396 | |
|
| 273 | 2397 | | return failures; |
| | 2398 | | } |
| | 2399 | | } |
| | 2400 | | } |