< Summary - Jellyfin

Information
Class: MediaBrowser.Model.Dlna.StreamBuilder
Assembly: MediaBrowser.Model
File(s): /srv/git/jellyfin/MediaBrowser.Model/Dlna/StreamBuilder.cs
Line coverage
69%
Covered lines: 722
Uncovered lines: 324
Coverable lines: 1046
Total lines: 2376
Line coverage: 69%
Branch coverage
58%
Covered branches: 544
Total branches: 928
Branch coverage: 58.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%11100%
GetOptimalAudioStream(...)0%110100%
GetOptimalAudioStream(...)0%4692680%
GetOptimalVideoStream(...)80%1010100%
GetOptimalStream(...)100%11100%
SortMediaSources(...)100%11100%
GetTranscodeReasonForFailedCondition(...)29.62%2432733.33%
NormalizeMediaSourceFormatIntoSingleContainer(...)100%1616100%
GetAudioDirectPlayProfile(...)0%600240%
GetTranscodeReasonsFromDirectPlayProfile(...)0%420200%
GetDefaultSubtitleStreamIndex(...)96.15%272688.23%
SetStreamInfoOptionsFromTranscodingProfile(...)87.5%8895.23%
SetStreamInfoOptionsFromDirectPlayProfile(...)0%620%
BuildVideoItem(...)67.34%1029892.78%
GetVideoTranscodeProfile(...)70%101096.96%
BuildStreamVideoItem(...)92.06%12612697.24%
GetDefaultAudioBitrate(...)68.18%332271.42%
GetAudioBitrate(...)87.5%323295.83%
GetMaxAudioBitrateForTotalBitrate(...)68.75%231670.58%
GetVideoDirectPlayProfile(...)63.63%222296.11%
AggregateFailureConditions(...)100%11100%
LogConditionFailure(...)66.66%66100%
GetSubtitleProfile(...)14.28%4974236.36%
IsSubtitleEmbedSupported(...)0%4260%
GetExternalSubtitleProfile(...)91.66%3636100%
IsBitrateLimitExceeded(...)66.66%6690.9%
ValidateMediaOptions(...)68.75%351658.33%
GetProfileConditionsForVideoAudio(...)100%11100%
GetProfileConditionsForAudio(...)0%620%
ApplyTranscodingConditions(...)63.8%714822147.85%
IsAudioContainerSupported(...)0%2040%
IsAudioDirectPlaySupported(...)0%4260%
IsAudioDirectStreamSupported(...)0%4260%
GetRank(...)100%44100%
CheckVideoConditions(...)50%3030100%
GetCompatibilityContainer(...)100%11100%
GetCompatibilityVideoCodec(...)100%11100%
GetCompatibilityAudioCodec(...)75%44100%
GetCompatibilityAudioCodecDirect(...)100%22100%

File(s)

/srv/git/jellyfin/MediaBrowser.Model/Dlna/StreamBuilder.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Globalization;
 4using System.Linq;
 5using Jellyfin.Data.Enums;
 6using Jellyfin.Extensions;
 7using MediaBrowser.Model.Dto;
 8using MediaBrowser.Model.Entities;
 9using MediaBrowser.Model.Extensions;
 10using MediaBrowser.Model.MediaInfo;
 11using MediaBrowser.Model.Session;
 12using Microsoft.Extensions.Logging;
 13
 14namespace 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;
 131        private static readonly string[] _supportedHlsVideoCodecs = ["h264", "hevc", "vp9", "av1"];
 132        private static readonly string[] _supportedHlsAudioCodecsTs = ["aac", "ac3", "eac3", "mp3"];
 133        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        {
 27842            _transcoderSupport = transcoderSupport;
 27843            _logger = logger;
 27844        }
 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        {
 053            ValidateMediaOptions(options, false);
 54
 055            List<StreamInfo> streams = [];
 056            foreach (var mediaSource in options.MediaSources)
 57            {
 058                if (!(string.IsNullOrEmpty(options.MediaSourceId)
 059                    || string.Equals(mediaSource.Id, options.MediaSourceId, StringComparison.OrdinalIgnoreCase)))
 60                {
 61                    continue;
 62                }
 63
 064                StreamInfo? streamInfo = GetOptimalAudioStream(mediaSource, options);
 065                if (streamInfo is not null)
 66                {
 067                    streamInfo.DeviceId = options.DeviceId;
 068                    streamInfo.DeviceProfileId = options.Profile.Id?.ToString("N", CultureInfo.InvariantCulture);
 069                    streams.Add(streamInfo);
 70                }
 71            }
 72
 073            return GetOptimalStream(streams, options.GetMaxBitrate(true) ?? 0);
 74        }
 75
 76        private StreamInfo? GetOptimalAudioStream(MediaSourceInfo item, MediaOptions options)
 77        {
 078            var playlistItem = new StreamInfo
 079            {
 080                ItemId = options.ItemId,
 081                MediaType = DlnaProfileType.Audio,
 082                MediaSource = item,
 083                RunTimeTicks = item.RunTimeTicks,
 084                Context = options.Context,
 085                DeviceProfile = options.Profile
 086            };
 87
 088            if (options.ForceDirectPlay)
 89            {
 090                playlistItem.PlayMethod = PlayMethod.DirectPlay;
 091                playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, 
 092                return playlistItem;
 93            }
 94
 095            if (options.ForceDirectStream)
 96            {
 097                playlistItem.PlayMethod = PlayMethod.DirectStream;
 098                playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, 
 099                return playlistItem;
 100            }
 101
 0102            MediaStream audioStream = item.GetDefaultAudioStream(null);
 103
 0104            ArgumentNullException.ThrowIfNull(audioStream);
 105
 0106            var directPlayInfo = GetAudioDirectPlayProfile(item, audioStream, options);
 107
 0108            var directPlayMethod = directPlayInfo.PlayMethod;
 0109            var transcodeReasons = directPlayInfo.TranscodeReasons;
 110
 0111            if (directPlayMethod is PlayMethod.DirectPlay)
 112            {
 0113                var audioFailureReasons = GetCompatibilityAudioCodec(options, item, item.Container, audioStream, null, f
 0114                transcodeReasons |= audioFailureReasons;
 115
 0116                if (audioFailureReasons == 0)
 117                {
 0118                    playlistItem.PlayMethod = directPlayMethod.Value;
 0119                    playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profi
 120
 0121                    return playlistItem;
 122                }
 123            }
 124
 0125            if (directPlayMethod is PlayMethod.DirectStream)
 126            {
 0127                var remuxContainer = item.TranscodingContainer ?? "ts";
 0128                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
 0131                remuxContainer = Array.Exists(supportedHlsContainers, element => string.Equals(element, directPlayInfo.P
 132                bool codeIsSupported;
 0133                if (item.TranscodingSubProtocol == MediaStreamProtocol.hls)
 134                {
 135                    // Enforce HLS audio codec restrictions
 0136                    if (string.Equals(remuxContainer, "mp4", StringComparison.OrdinalIgnoreCase))
 137                    {
 0138                        codeIsSupported = _supportedHlsAudioCodecsMp4.Contains(directPlayInfo.Profile?.AudioCodec ?? dir
 139                    }
 140                    else
 141                    {
 0142                        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
 0148                    codeIsSupported = true;
 149                }
 150
 0151                if (codeIsSupported)
 152                {
 0153                    playlistItem.PlayMethod = directPlayMethod.Value;
 0154                    playlistItem.Container = remuxContainer;
 0155                    playlistItem.TranscodeReasons = transcodeReasons;
 0156                    playlistItem.SubProtocol = item.TranscodingSubProtocol;
 0157                    item.TranscodingContainer = remuxContainer;
 0158                    return playlistItem;
 159                }
 160
 0161                transcodeReasons |= TranscodeReason.AudioCodecNotSupported;
 0162                playlistItem.TranscodeReasons = transcodeReasons;
 163            }
 164
 0165            TranscodingProfile? transcodingProfile = null;
 0166            foreach (var tcProfile in options.Profile.TranscodingProfiles)
 167            {
 0168                if (tcProfile.Type == playlistItem.MediaType
 0169                    && tcProfile.Context == options.Context
 0170                    && _transcoderSupport.CanEncodeToAudioCodec(tcProfile.AudioCodec ?? tcProfile.Container))
 171                {
 0172                    transcodingProfile = tcProfile;
 0173                    break;
 174                }
 175            }
 176
 0177            if (transcodingProfile is not null)
 178            {
 0179                if (!item.SupportsTranscoding)
 180                {
 0181                    return null;
 182                }
 183
 0184                SetStreamInfoOptionsFromTranscodingProfile(item, playlistItem, transcodingProfile);
 185
 0186                var inputAudioChannels = audioStream.Channels;
 0187                var inputAudioBitrate = audioStream.BitRate;
 0188                var inputAudioSampleRate = audioStream.SampleRate;
 0189                var inputAudioBitDepth = audioStream.BitDepth;
 190
 0191                var audioTranscodingConditions = GetProfileConditionsForAudio(options.Profile.CodecProfiles, transcoding
 0192                ApplyTranscodingConditions(playlistItem, audioTranscodingConditions, null, true, true);
 193
 194                // Honor requested max channels
 0195                playlistItem.GlobalMaxAudioChannels = options.MaxAudioChannels;
 196
 0197                var configuredBitrate = options.GetMaxBitrate(true);
 198
 0199                long transcodingBitrate = options.AudioTranscodingBitrate
 0200                    ?? (options.Context == EncodingContext.Streaming ? options.Profile.MusicStreamingTranscodingBitrate 
 0201                    ?? configuredBitrate
 0202                    ?? 128000;
 203
 0204                if (configuredBitrate.HasValue)
 205                {
 0206                    transcodingBitrate = Math.Min(configuredBitrate.Value, transcodingBitrate);
 207                }
 208
 0209                var longBitrate = Math.Min(transcodingBitrate, playlistItem.AudioBitrate ?? transcodingBitrate);
 0210                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.
 0215                if (playlistItem.AudioCodecs.Count == 0 && !string.IsNullOrWhiteSpace(transcodingProfile.AudioCodec))
 216                {
 0217                    playlistItem.AudioCodecs = [transcodingProfile.AudioCodec];
 218                }
 219            }
 220
 0221            playlistItem.TranscodeReasons = transcodeReasons;
 0222            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        {
 278232            ValidateMediaOptions(options, true);
 233
 278234            var mediaSources = string.IsNullOrEmpty(options.MediaSourceId)
 278235                ? options.MediaSources
 278236                : options.MediaSources.Where(x => string.Equals(x.Id, options.MediaSourceId, StringComparison.OrdinalIgn
 237
 278238            List<StreamInfo> streams = [];
 1112239            foreach (var mediaSourceInfo in mediaSources)
 240            {
 278241                var streamInfo = BuildVideoItem(mediaSourceInfo, options);
 278242                if (streamInfo is not null)
 243                {
 278244                    streams.Add(streamInfo);
 245                }
 246            }
 247
 1112248            foreach (var stream in streams)
 249            {
 278250                stream.DeviceId = options.DeviceId;
 278251                stream.DeviceProfileId = options.Profile.Id?.ToString("N", CultureInfo.InvariantCulture);
 252            }
 253
 278254            return GetOptimalStream(streams, options.GetMaxBitrate(false) ?? 0);
 255        }
 256
 257        private static StreamInfo? GetOptimalStream(List<StreamInfo> streams, long maxBitrate)
 278258            => SortMediaSources(streams, maxBitrate).FirstOrDefault();
 259
 260        private static IOrderedEnumerable<StreamInfo> SortMediaSources(List<StreamInfo> streams, long maxBitrate)
 261        {
 278262            return streams.OrderBy(i =>
 278263            {
 278264                // Nothing beats direct playing a file
 278265                if (i.PlayMethod == PlayMethod.DirectPlay && i.MediaSource?.Protocol == MediaProtocol.File)
 278266                {
 278267                    return 0;
 278268                }
 278269
 278270                return 1;
 278271            }).ThenBy(i =>
 278272            {
 278273                switch (i.PlayMethod)
 278274                {
 278275                    // Let's assume direct streaming a file is just as desirable as direct playing a remote url
 278276                    case PlayMethod.DirectStream:
 278277                    case PlayMethod.DirectPlay:
 278278                        return 0;
 278279                    default:
 278280                        return 1;
 278281                }
 278282            }).ThenBy(i =>
 278283            {
 278284                switch (i.MediaSource?.Protocol)
 278285                {
 278286                    case MediaProtocol.File:
 278287                        return 0;
 278288                    default:
 278289                        return 1;
 278290                }
 278291            }).ThenBy(i =>
 278292            {
 278293                if (maxBitrate > 0)
 278294                {
 278295                    if (i.MediaSource?.Bitrate is not null)
 278296                    {
 278297                        return Math.Abs(i.MediaSource.Bitrate.Value - maxBitrate);
 278298                    }
 278299                }
 278300
 278301                return 0;
 278302            }).ThenBy(streams.IndexOf);
 303        }
 304
 305        private static TranscodeReason GetTranscodeReasonForFailedCondition(ProfileCondition condition)
 306        {
 247307            switch (condition.Property)
 308            {
 309                case ProfileConditionValue.AudioBitrate:
 0310                    return TranscodeReason.AudioBitrateNotSupported;
 311
 312                case ProfileConditionValue.AudioChannels:
 122313                    return TranscodeReason.AudioChannelsNotSupported;
 314
 315                case ProfileConditionValue.AudioProfile:
 0316                    return TranscodeReason.AudioProfileNotSupported;
 317
 318                case ProfileConditionValue.AudioSampleRate:
 0319                    return TranscodeReason.AudioSampleRateNotSupported;
 320
 321                case ProfileConditionValue.Has64BitOffsets:
 322                    // TODO
 0323                    return 0;
 324
 325                case ProfileConditionValue.Height:
 0326                    return TranscodeReason.VideoResolutionNotSupported;
 327
 328                case ProfileConditionValue.IsAnamorphic:
 0329                    return TranscodeReason.AnamorphicVideoNotSupported;
 330
 331                case ProfileConditionValue.IsAvc:
 332                    // TODO
 0333                    return 0;
 334
 335                case ProfileConditionValue.IsInterlaced:
 0336                    return TranscodeReason.InterlacedVideoNotSupported;
 337
 338                case ProfileConditionValue.IsSecondaryAudio:
 35339                    return TranscodeReason.SecondaryAudioNotSupported;
 340
 341                case ProfileConditionValue.NumStreams:
 2342                    return TranscodeReason.StreamCountExceedsLimit;
 343
 344                case ProfileConditionValue.NumAudioStreams:
 345                    // TODO
 0346                    return 0;
 347
 348                case ProfileConditionValue.NumVideoStreams:
 349                    // TODO
 0350                    return 0;
 351
 352                case ProfileConditionValue.PacketLength:
 353                    // TODO
 0354                    return 0;
 355
 356                case ProfileConditionValue.RefFrames:
 0357                    return TranscodeReason.RefFramesNotSupported;
 358
 359                case ProfileConditionValue.VideoBitDepth:
 0360                    return TranscodeReason.VideoBitDepthNotSupported;
 361
 362                case ProfileConditionValue.AudioBitDepth:
 0363                    return TranscodeReason.AudioBitDepthNotSupported;
 364
 365                case ProfileConditionValue.VideoBitrate:
 4366                    return TranscodeReason.VideoBitrateNotSupported;
 367
 368                case ProfileConditionValue.VideoCodecTag:
 18369                    return TranscodeReason.VideoCodecTagNotSupported;
 370
 371                case ProfileConditionValue.VideoFramerate:
 0372                    return TranscodeReason.VideoFramerateNotSupported;
 373
 374                case ProfileConditionValue.VideoLevel:
 13375                    return TranscodeReason.VideoLevelNotSupported;
 376
 377                case ProfileConditionValue.VideoProfile:
 25378                    return TranscodeReason.VideoProfileNotSupported;
 379
 380                case ProfileConditionValue.VideoRangeType:
 28381                    return TranscodeReason.VideoRangeTypeNotSupported;
 382
 383                case ProfileConditionValue.VideoTimestamp:
 384                    // TODO
 0385                    return 0;
 386
 387                case ProfileConditionValue.Width:
 0388                    return TranscodeReason.VideoResolutionNotSupported;
 389
 390                default:
 0391                    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
 402406            if (profile is null || string.IsNullOrEmpty(inputContainer) || !inputContainer.Contains(',', StringCompariso
 407            {
 94408                return inputContainer;
 409            }
 410
 308411            var formats = ContainerHelper.Split(inputContainer);
 308412            var playProfiles = playProfile is null ? profile.DirectPlayProfiles : [playProfile];
 1361413            foreach (var format in formats)
 414            {
 2593415                foreach (var directPlayProfile in playProfiles)
 416                {
 924417                    if (directPlayProfile.Type != type)
 418                    {
 419                        continue;
 420                    }
 421
 696422                    if (directPlayProfile.SupportsContainer(format))
 423                    {
 291424                        return format;
 425                    }
 426                }
 427            }
 428
 17429            return inputContainer;
 430        }
 431
 432        private (DirectPlayProfile? Profile, PlayMethod? PlayMethod, TranscodeReason TranscodeReasons) GetAudioDirectPla
 433        {
 0434            var directPlayProfile = options.Profile.DirectPlayProfiles
 0435                .FirstOrDefault(x => x.Type == DlnaProfileType.Audio && IsAudioDirectPlaySupported(x, item, audioStream)
 436
 0437            TranscodeReason transcodeReasons = 0;
 0438            if (directPlayProfile is null)
 439            {
 0440                _logger.LogDebug(
 0441                    "Profile: {0}, No audio direct play profiles found for {1} with codec {2}",
 0442                    options.Profile.Name ?? "Unknown Profile",
 0443                    item.Path ?? "Unknown path",
 0444                    audioStream.Codec ?? "Unknown codec");
 445
 0446                var directStreamProfile = options.Profile.DirectPlayProfiles
 0447                    .FirstOrDefault(x => x.Type == DlnaProfileType.Audio && IsAudioDirectStreamSupported(x, item, audioS
 448
 0449                if (directStreamProfile is not null)
 450                {
 0451                    directPlayProfile = directStreamProfile;
 0452                    transcodeReasons |= TranscodeReason.ContainerNotSupported;
 453                }
 454                else
 455                {
 0456                    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
 0464            if (item.SupportsDirectPlay && transcodeReasons == 0)
 465            {
 0466                if (!IsBitrateLimitExceeded(item, options.GetMaxBitrate(true) ?? 0))
 467                {
 0468                    if (options.EnableDirectPlay)
 469                    {
 0470                        return (directPlayProfile, PlayMethod.DirectPlay, 0);
 471                    }
 472                }
 473                else
 474                {
 0475                    transcodeReasons |= TranscodeReason.ContainerBitrateExceedsLimit;
 476                }
 477            }
 478
 479            // While options takes the network and other factors into account. Only applies to direct stream
 0480            if (item.SupportsDirectStream)
 481            {
 0482                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
 0487                    if (transcodeReasons == TranscodeReason.ContainerNotSupported)
 488                    {
 0489                        return (directPlayProfile, PlayMethod.DirectStream, transcodeReasons);
 490                    }
 491                }
 492                else
 493                {
 0494                    transcodeReasons |= TranscodeReason.ContainerBitrateExceedsLimit;
 495                }
 496            }
 497
 0498            return (directPlayProfile, null, transcodeReasons);
 499        }
 500
 501        private static TranscodeReason GetTranscodeReasonsFromDirectPlayProfile(MediaSourceInfo item, MediaStream? video
 502        {
 0503            var mediaType = videoStream is null ? DlnaProfileType.Audio : DlnaProfileType.Video;
 504
 0505            var containerSupported = false;
 0506            var audioSupported = false;
 0507            var videoSupported = false;
 508
 0509            foreach (var profile in directPlayProfiles)
 510            {
 511                // Check container type
 0512                if (profile.Type == mediaType && profile.SupportsContainer(item.Container))
 513                {
 0514                    containerSupported = true;
 515
 0516                    videoSupported = videoStream is null || profile.SupportsVideoCodec(videoStream.Codec);
 517
 0518                    audioSupported = audioStream is null || profile.SupportsAudioCodec(audioStream.Codec);
 519
 0520                    if (videoSupported && audioSupported)
 521                    {
 0522                        break;
 523                    }
 524                }
 525            }
 526
 0527            TranscodeReason reasons = 0;
 0528            if (!containerSupported)
 529            {
 0530                reasons |= TranscodeReason.ContainerNotSupported;
 531            }
 532
 0533            if (!videoSupported)
 534            {
 0535                reasons |= TranscodeReason.VideoCodecNotSupported;
 536            }
 537
 0538            if (!audioSupported)
 539            {
 0540                reasons |= TranscodeReason.AudioCodecNotSupported;
 541            }
 542
 0543            return reasons;
 544        }
 545
 546        private static int? GetDefaultSubtitleStreamIndex(MediaSourceInfo item, SubtitleProfile[] subtitleProfiles)
 547        {
 175548            int highestScore = -1;
 1788549            foreach (var stream in item.MediaStreams)
 550            {
 719551                if (stream.Type == MediaStreamType.Subtitle
 719552                    && stream.Score.HasValue
 719553                    && stream.Score.Value > highestScore)
 554                {
 144555                    highestScore = stream.Score.Value;
 556                }
 557            }
 558
 175559            List<MediaStream> topStreams = [];
 1788560            foreach (var stream in item.MediaStreams)
 561            {
 719562                if (stream.Type == MediaStreamType.Subtitle && stream.Score.HasValue && stream.Score.Value == highestSco
 563                {
 321564                    topStreams.Add(stream);
 565                }
 566            }
 567
 568            // If multiple streams have an equal score, try to pick the most efficient one
 175569            if (topStreams.Count > 1)
 570            {
 378571                foreach (var stream in topStreams)
 572                {
 1464573                    foreach (var profile in subtitleProfiles)
 574                    {
 549575                        if (profile.Method == SubtitleDeliveryMethod.External && string.Equals(profile.Format, stream.Co
 576                        {
 0577                            return stream.Index;
 578                        }
 579                    }
 580                }
 581            }
 582
 583            // If no optimization panned out, just use the original default
 175584            return item.DefaultSubtitleStreamIndex;
 0585        }
 586
 587        private static void SetStreamInfoOptionsFromTranscodingProfile(MediaSourceInfo item, StreamInfo playlistItem, Tr
 588        {
 146589            var container = transcodingProfile.Container;
 146590            var protocol = transcodingProfile.Protocol;
 591
 146592            item.TranscodingContainer = container;
 146593            item.TranscodingSubProtocol = protocol;
 594
 146595            if (playlistItem.PlayMethod == PlayMethod.Transcode)
 596            {
 146597                playlistItem.Container = container;
 146598                playlistItem.SubProtocol = protocol;
 599            }
 600
 146601            playlistItem.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo;
 146602            if (int.TryParse(transcodingProfile.MaxAudioChannels, CultureInfo.InvariantCulture, out int transcodingMaxAu
 603            {
 133604                playlistItem.TranscodingMaxAudioChannels = transcodingMaxAudioChannels;
 605            }
 606
 146607            playlistItem.EstimateContentLength = transcodingProfile.EstimateContentLength;
 608
 146609            playlistItem.CopyTimestamps = transcodingProfile.CopyTimestamps;
 146610            playlistItem.EnableSubtitlesInManifest = transcodingProfile.EnableSubtitlesInManifest;
 146611            playlistItem.EnableMpegtsM2TsMode = transcodingProfile.EnableMpegtsM2TsMode;
 612
 146613            playlistItem.BreakOnNonKeyFrames = transcodingProfile.BreakOnNonKeyFrames;
 146614            playlistItem.EnableAudioVbrEncoding = transcodingProfile.EnableAudioVbrEncoding;
 615
 146616            if (transcodingProfile.MinSegments > 0)
 617            {
 102618                playlistItem.MinSegments = transcodingProfile.MinSegments;
 619            }
 620
 146621            if (transcodingProfile.SegmentLength > 0)
 622            {
 0623                playlistItem.SegmentLength = transcodingProfile.SegmentLength;
 624            }
 146625        }
 626
 627        private static void SetStreamInfoOptionsFromDirectPlayProfile(MediaOptions options, MediaSourceInfo item, Stream
 628        {
 0629            var container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileTy
 0630            var protocol = MediaStreamProtocol.http;
 631
 0632            item.TranscodingContainer = container;
 0633            item.TranscodingSubProtocol = protocol;
 634
 0635            playlistItem.Container = container;
 0636            playlistItem.SubProtocol = protocol;
 637
 0638            playlistItem.VideoCodecs = [item.VideoStream.Codec];
 0639            playlistItem.AudioCodecs = ContainerHelper.Split(directPlayProfile?.AudioCodec);
 0640        }
 641
 642        private StreamInfo BuildVideoItem(MediaSourceInfo item, MediaOptions options)
 643        {
 278644            ArgumentNullException.ThrowIfNull(item);
 645
 278646            StreamInfo playlistItem = new StreamInfo
 278647            {
 278648                ItemId = options.ItemId,
 278649                MediaType = DlnaProfileType.Video,
 278650                MediaSource = item,
 278651                RunTimeTicks = item.RunTimeTicks,
 278652                Context = options.Context,
 278653                DeviceProfile = options.Profile,
 278654                SubtitleStreamIndex = options.SubtitleStreamIndex ?? GetDefaultSubtitleStreamIndex(item, options.Profile
 278655                AlwaysBurnInSubtitleWhenTranscoding = options.AlwaysBurnInSubtitleWhenTranscoding
 278656            };
 657
 278658            var subtitleStream = playlistItem.SubtitleStreamIndex.HasValue ? item.GetMediaStream(MediaStreamType.Subtitl
 659
 278660            var audioStream = item.GetDefaultAudioStream(options.AudioStreamIndex ?? item.DefaultAudioStreamIndex);
 278661            if (audioStream is not null)
 662            {
 277663                playlistItem.AudioStreamIndex = audioStream.Index;
 664            }
 665
 666            // Collect candidate audio streams
 278667            ICollection<MediaStream> candidateAudioStreams = audioStream is null ? [] : [audioStream];
 278668            if (!options.AudioStreamIndex.HasValue || options.AudioStreamIndex < 0)
 669            {
 175670                if (audioStream?.IsDefault == true)
 671                {
 174672                    candidateAudioStreams = item.MediaStreams.Where(stream => stream.Type == MediaStreamType.Audio && st
 673                }
 674                else
 675                {
 1676                    candidateAudioStreams = item.MediaStreams.Where(stream => stream.Type == MediaStreamType.Audio && st
 677                }
 678            }
 679
 278680            var videoStream = item.VideoStream;
 681
 278682            var bitrateLimitExceeded = IsBitrateLimitExceeded(item, options.GetMaxBitrate(false) ?? 0);
 278683            var isEligibleForDirectPlay = options.EnableDirectPlay && (options.ForceDirectPlay || !bitrateLimitExceeded)
 278684            var isEligibleForDirectStream = options.EnableDirectStream && (options.ForceDirectStream || !bitrateLimitExc
 278685            TranscodeReason transcodeReasons = 0;
 686
 687            // Force transcode or remux for BD/DVD folders
 278688            if (item.VideoType == VideoType.Dvd || item.VideoType == VideoType.BluRay)
 689            {
 0690                isEligibleForDirectPlay = false;
 691            }
 692
 278693            if (bitrateLimitExceeded)
 694            {
 24695                transcodeReasons = TranscodeReason.ContainerBitrateExceedsLimit;
 696            }
 697
 278698            _logger.LogDebug(
 278699                "Profile: {0}, Path: {1}, isEligibleForDirectPlay: {2}, isEligibleForDirectStream: {3}",
 278700                options.Profile.Name ?? "Unknown Profile",
 278701                item.Path ?? "Unknown path",
 278702                isEligibleForDirectPlay,
 278703                isEligibleForDirectStream);
 704
 278705            DirectPlayProfile? directPlayProfile = null;
 278706            if (isEligibleForDirectPlay || isEligibleForDirectStream)
 707            {
 708                // See if it can be direct played
 254709                var directPlayInfo = GetVideoDirectPlayProfile(options, item, videoStream, audioStream, candidateAudioSt
 254710                var directPlay = directPlayInfo.PlayMethod;
 254711                transcodeReasons |= directPlayInfo.TranscodeReasons;
 712
 254713                if (directPlay.HasValue)
 714                {
 124715                    directPlayProfile = directPlayInfo.Profile;
 124716                    playlistItem.PlayMethod = directPlay.Value;
 124717                    playlistItem.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profi
 124718                    var videoCodec = videoStream?.Codec;
 124719                    playlistItem.VideoCodecs = videoCodec is null ? [] : [videoCodec];
 720
 124721                    if (directPlay == PlayMethod.DirectPlay)
 722                    {
 124723                        playlistItem.SubProtocol = MediaStreamProtocol.http;
 724
 124725                        var audioStreamIndex = directPlayInfo.AudioStreamIndex ?? audioStream?.Index;
 124726                        if (audioStreamIndex.HasValue)
 727                        {
 124728                            playlistItem.AudioStreamIndex = audioStreamIndex;
 124729                            var audioCodec = item.GetMediaStream(MediaStreamType.Audio, audioStreamIndex.Value)?.Codec;
 124730                            playlistItem.AudioCodecs = audioCodec is null ? [] : [audioCodec];
 731                        }
 732                    }
 0733                    else if (directPlay == PlayMethod.DirectStream)
 734                    {
 0735                        playlistItem.AudioStreamIndex = audioStream?.Index;
 0736                        if (audioStream is not null)
 737                        {
 0738                            playlistItem.AudioCodecs = ContainerHelper.Split(directPlayProfile?.AudioCodec);
 739                        }
 740
 0741                        SetStreamInfoOptionsFromDirectPlayProfile(options, item, playlistItem, directPlayProfile);
 0742                        BuildStreamVideoItem(playlistItem, options, item, videoStream, audioStream, candidateAudioStream
 743                    }
 744
 124745                    if (subtitleStream is not null)
 746                    {
 116747                        var subtitleProfile = GetSubtitleProfile(item, subtitleStream, options.Profile.SubtitleProfiles,
 748
 116749                        playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
 116750                        playlistItem.SubtitleFormat = subtitleProfile.Format;
 751                    }
 752                }
 753
 254754                _logger.LogDebug(
 254755                    "DirectPlay Result for Profile: {0}, Path: {1}, PlayMethod: {2}, AudioStreamIndex: {3}, SubtitleStre
 254756                    options.Profile.Name ?? "Anonymous Profile",
 254757                    item.Path ?? "Unknown path",
 254758                    directPlayInfo.PlayMethod,
 254759                    directPlayInfo.AudioStreamIndex ?? audioStream?.Index,
 254760                    playlistItem.SubtitleStreamIndex,
 254761                    directPlayInfo.TranscodeReasons);
 762            }
 763
 278764            playlistItem.TranscodeReasons = transcodeReasons;
 765
 278766            if (playlistItem.PlayMethod != PlayMethod.DirectStream && playlistItem.PlayMethod != PlayMethod.DirectPlay)
 767            {
 768                // Can't direct play, find the transcoding profile
 769                // If we do this for direct-stream we will overwrite the info
 154770                var (transcodingProfile, playMethod) = GetVideoTranscodeProfile(item, options, videoStream, audioStream,
 771
 154772                if (transcodingProfile is not null && playMethod.HasValue)
 773                {
 146774                    SetStreamInfoOptionsFromTranscodingProfile(item, playlistItem, transcodingProfile);
 775
 146776                    BuildStreamVideoItem(playlistItem, options, item, videoStream, audioStream, candidateAudioStreams, t
 777
 146778                    playlistItem.PlayMethod = PlayMethod.Transcode;
 779
 146780                    if (subtitleStream is not null)
 781                    {
 123782                        var subtitleProfile = GetSubtitleProfile(item, subtitleStream, options.Profile.SubtitleProfiles,
 123783                        playlistItem.SubtitleDeliveryMethod = subtitleProfile.Method;
 123784                        playlistItem.SubtitleFormat = subtitleProfile.Format;
 123785                        playlistItem.SubtitleCodecs = [subtitleProfile.Format];
 786                    }
 787
 146788                    if ((playlistItem.TranscodeReasons & (VideoReasons | TranscodeReason.ContainerBitrateExceedsLimit)) 
 789                    {
 44790                        ApplyTranscodingConditions(playlistItem, transcodingProfile.Conditions, null, true, true);
 791                    }
 792                }
 793            }
 794
 278795            _logger.LogDebug(
 278796                "StreamBuilder.BuildVideoItem( Profile={0}, Path={1}, AudioStreamIndex={2}, SubtitleStreamIndex={3} ) =>
 278797                options.Profile.Name ?? "Anonymous Profile",
 278798                item.Path ?? "Unknown path",
 278799                options.AudioStreamIndex,
 278800                options.SubtitleStreamIndex,
 278801                playlistItem.PlayMethod,
 278802                playlistItem.TranscodeReasons,
 278803                playlistItem.ToUrl("media:", "<token>", null));
 804
 278805            item.Container = NormalizeMediaSourceFormatIntoSingleContainer(item.Container, options.Profile, DlnaProfileT
 278806            return playlistItem;
 807        }
 808
 809        private (TranscodingProfile? Profile, PlayMethod? PlayMethod) GetVideoTranscodeProfile(
 810            MediaSourceInfo item,
 811            MediaOptions options,
 812            MediaStream? videoStream,
 813            MediaStream? audioStream,
 814            StreamInfo playlistItem)
 815        {
 154816            var mediaSource = playlistItem.MediaSource;
 817
 154818            ArgumentNullException.ThrowIfNull(mediaSource);
 819
 154820            if (!(item.SupportsTranscoding || item.SupportsDirectStream))
 821            {
 0822                return (null, null);
 823            }
 824
 154825            var transcodingProfiles = options.Profile.TranscodingProfiles
 154826                .Where(i => i.Type == playlistItem.MediaType && i.Context == options.Context);
 827
 154828            if (item.UseMostCompatibleTranscodingProfile)
 829            {
 0830                transcodingProfiles = transcodingProfiles.Where(i => string.Equals(i.Container, "ts", StringComparison.O
 831            }
 832
 154833            var videoCodec = videoStream?.Codec;
 154834            var audioCodec = audioStream?.Codec;
 835
 154836            var analyzedProfiles = transcodingProfiles
 154837                .Select(transcodingProfile =>
 154838                {
 154839                    var rank = (Video: 3, Audio: 3);
 154840
 154841                    var container = transcodingProfile.Container;
 154842
 154843                    if (videoStream is not null
 154844                        && options.AllowVideoStreamCopy
 154845                        && ContainerHelper.ContainsContainer(transcodingProfile.VideoCodec, videoCodec))
 154846                    {
 154847                        var failures = GetCompatibilityVideoCodec(options, mediaSource, container, videoStream);
 154848                        rank.Video = failures == 0 ? 1 : 2;
 154849                    }
 154850
 154851                    if (audioStream is not null
 154852                        && options.AllowAudioStreamCopy)
 154853                    {
 154854                        // For Audio stream, we prefer the audio codec that can be directly copied, then the codec that 
 154855                        // the transcoding conditions, then the one does not satisfy the transcoding conditions.
 154856                        // For example: A client can support both aac and flac, but flac only supports 2 channels while 
 154857                        // When the source audio is 6 channel flac, we should transcode to 6 channel aac, instead of dow
 154858                        var transcodingAudioCodecs = ContainerHelper.Split(transcodingProfile.AudioCodec);
 154859
 154860                        foreach (var transcodingAudioCodec in transcodingAudioCodecs)
 154861                        {
 154862                            var failures = GetCompatibilityAudioCodec(options, mediaSource, container, audioStream, tran
 154863
 154864                            var rankAudio = 3;
 154865
 154866                            if (failures == 0)
 154867                            {
 154868                                rankAudio = string.Equals(transcodingAudioCodec, audioCodec, StringComparison.OrdinalIgn
 154869                            }
 154870
 154871                            rank.Audio = Math.Min(rank.Audio, rankAudio);
 154872
 154873                            if (rank.Audio == 1)
 154874                            {
 154875                                break;
 154876                            }
 154877                        }
 154878                    }
 154879
 154880                    PlayMethod playMethod = PlayMethod.Transcode;
 154881
 154882                    if (rank.Video == 1)
 154883                    {
 154884                        playMethod = PlayMethod.DirectStream;
 154885                    }
 154886
 154887                    return (Profile: transcodingProfile, PlayMethod: playMethod, Rank: rank);
 154888                })
 154889                .OrderBy(analysis => analysis.Rank);
 890
 154891            var profileMatch = analyzedProfiles.FirstOrDefault();
 892
 154893            return (profileMatch.Profile, profileMatch.PlayMethod);
 894        }
 895
 896        private void BuildStreamVideoItem(
 897            StreamInfo playlistItem,
 898            MediaOptions options,
 899            MediaSourceInfo item,
 900            MediaStream? videoStream,
 901            MediaStream? audioStream,
 902            IEnumerable<MediaStream> candidateAudioStreams,
 903            string? container,
 904            string? videoCodec,
 905            string? audioCodec)
 906        {
 907            // Prefer matching video codecs
 146908            var videoCodecs = ContainerHelper.Split(videoCodec).ToList();
 909
 146910            if (videoCodecs.Count == 0 && videoStream is not null)
 911            {
 912                // Add the original codec if no codec is specified
 0913                videoCodecs.Add(videoStream.Codec);
 914            }
 915
 916            // Enforce HLS video codec restrictions
 146917            if (playlistItem.SubProtocol == MediaStreamProtocol.hls)
 918            {
 132919                videoCodecs = videoCodecs.Where(codec => _supportedHlsVideoCodecs.Contains(codec)).ToList();
 920            }
 921
 146922            playlistItem.VideoCodecs = videoCodecs;
 923
 924            // Copy video codec options as a starting point, this applies to transcode and direct-stream
 146925            playlistItem.MaxFramerate = videoStream?.ReferenceFrameRate;
 146926            var qualifier = videoStream?.Codec;
 146927            if (videoStream?.Level is not null)
 928            {
 145929                playlistItem.SetOption(qualifier, "level", videoStream.Level.Value.ToString(CultureInfo.InvariantCulture
 930            }
 931
 146932            if (videoStream?.BitDepth is not null)
 933            {
 145934                playlistItem.SetOption(qualifier, "videobitdepth", videoStream.BitDepth.Value.ToString(CultureInfo.Invar
 935            }
 936
 146937            if (!string.IsNullOrEmpty(videoStream?.Profile))
 938            {
 145939                playlistItem.SetOption(qualifier, "profile", videoStream.Profile.ToLowerInvariant());
 940            }
 941
 942            // Prefer matching audio codecs, could do better here
 146943            var audioCodecs = ContainerHelper.Split(audioCodec).ToList();
 944
 146945            if (audioCodecs.Count == 0 && audioStream is not null)
 946            {
 947                // Add the original codec if no codec is specified
 0948                audioCodecs.Add(audioStream.Codec);
 949            }
 950
 951            // Enforce HLS audio codec restrictions
 146952            if (playlistItem.SubProtocol == MediaStreamProtocol.hls)
 953            {
 132954                if (string.Equals(playlistItem.Container, "mp4", StringComparison.OrdinalIgnoreCase))
 955                {
 72956                    audioCodecs = audioCodecs.Where(codec => _supportedHlsAudioCodecsMp4.Contains(codec)).ToList();
 957                }
 958                else
 959                {
 60960                    audioCodecs = audioCodecs.Where(codec => _supportedHlsAudioCodecsTs.Contains(codec)).ToList();
 961                }
 962            }
 963
 146964            var audioStreamWithSupportedCodec = candidateAudioStreams.Where(stream => ContainerHelper.ContainsContainer(
 965
 146966            var channelsExceedsLimit = audioStreamWithSupportedCodec is not null && audioStreamWithSupportedCodec.Channe
 967
 146968            var directAudioFailures = audioStreamWithSupportedCodec is null ? default : GetCompatibilityAudioCodec(optio
 969
 146970            playlistItem.TranscodeReasons |= directAudioFailures;
 971
 146972            var directAudioStreamSatisfied = audioStreamWithSupportedCodec is not null && !channelsExceedsLimit
 146973                && directAudioFailures == 0;
 974
 146975            directAudioStreamSatisfied = directAudioStreamSatisfied && !playlistItem.TranscodeReasons.HasFlag(TranscodeR
 976
 146977            var directAudioStream = directAudioStreamSatisfied ? audioStreamWithSupportedCodec : null;
 978
 146979            if (channelsExceedsLimit && playlistItem.TargetAudioStream is not null)
 980            {
 10981                playlistItem.TranscodeReasons |= TranscodeReason.AudioChannelsNotSupported;
 10982                playlistItem.TargetAudioStream.Channels = playlistItem.TranscodingMaxAudioChannels;
 983            }
 984
 146985            playlistItem.AudioCodecs = audioCodecs;
 146986            if (directAudioStream is not null)
 987            {
 55988                audioStream = directAudioStream;
 55989                playlistItem.AudioStreamIndex = audioStream.Index;
 55990                audioCodecs = [audioStream.Codec];
 55991                playlistItem.AudioCodecs = audioCodecs;
 992
 993                // Copy matching audio codec options
 55994                playlistItem.AudioSampleRate = audioStream.SampleRate;
 55995                playlistItem.SetOption(qualifier, "audiochannels", audioStream.Channels?.ToString(CultureInfo.InvariantC
 996
 55997                if (!string.IsNullOrEmpty(audioStream.Profile))
 998                {
 53999                    playlistItem.SetOption(audioStream.Codec, "profile", audioStream.Profile.ToLowerInvariant());
 1000                }
 1001
 551002                if (audioStream.Level.HasValue && audioStream.Level.Value != 0)
 1003                {
 01004                    playlistItem.SetOption(audioStream.Codec, "level", audioStream.Level.Value.ToString(CultureInfo.Inva
 1005                }
 1006            }
 1007
 1461008            int? width = videoStream?.Width;
 1461009            int? height = videoStream?.Height;
 1461010            int? bitDepth = videoStream?.BitDepth;
 1461011            int? videoBitrate = videoStream?.BitRate;
 1461012            double? videoLevel = videoStream?.Level;
 1461013            string? videoProfile = videoStream?.Profile;
 1461014            VideoRangeType? videoRangeType = videoStream?.VideoRangeType;
 1461015            float videoFramerate = videoStream is null ? 0 : videoStream.ReferenceFrameRate ?? 0;
 1461016            bool? isAnamorphic = videoStream?.IsAnamorphic;
 1461017            bool? isInterlaced = videoStream?.IsInterlaced;
 1461018            string? videoCodecTag = videoStream?.CodecTag;
 1461019            bool? isAvc = videoStream?.IsAVC;
 1020
 1461021            TransportStreamTimestamp? timestamp = videoStream is null ? TransportStreamTimestamp.None : item.Timestamp;
 1461022            int? packetLength = videoStream?.PacketLength;
 1461023            int? refFrames = videoStream?.RefFrames;
 1024
 1461025            int numStreams = item.MediaStreams.Count;
 1461026            int? numAudioStreams = item.GetStreamCount(MediaStreamType.Audio);
 1461027            int? numVideoStreams = item.GetStreamCount(MediaStreamType.Video);
 1028
 1461029            var useSubContainer = playlistItem.SubProtocol == MediaStreamProtocol.hls;
 1030
 1461031            var appliedVideoConditions = options.Profile.CodecProfiles
 1461032                .Where(i => i.Type == CodecType.Video &&
 1461033                    i.ContainsAnyCodec(playlistItem.VideoCodecs, container, useSubContainer) &&
 1461034                    i.ApplyConditions.All(applyCondition => ConditionProcessor.IsVideoConditionSatisfied(applyCondition,
 1461035                // Reverse codec profiles for backward compatibility - first codec profile has higher priority
 1461036                .Reverse();
 8881037            foreach (var condition in appliedVideoConditions)
 1038            {
 24161039                foreach (var transcodingVideoCodec in playlistItem.VideoCodecs)
 1040                {
 9101041                    if (condition.ContainsAnyCodec(transcodingVideoCodec, container, useSubContainer))
 1042                    {
 3061043                        ApplyTranscodingConditions(playlistItem, condition.Conditions, transcodingVideoCodec, true, true
 1044                        continue;
 1045                    }
 1046                }
 1047            }
 1048
 1049            // Honor requested max channels
 1461050            playlistItem.GlobalMaxAudioChannels = channelsExceedsLimit ? playlistItem.TranscodingMaxAudioChannels : opti
 1051
 1461052            int audioBitrate = GetAudioBitrate(options.GetMaxBitrate(true) ?? 0, playlistItem.TargetAudioCodec, audioStr
 1461053            playlistItem.AudioBitrate = Math.Min(playlistItem.AudioBitrate ?? audioBitrate, audioBitrate);
 1054
 1461055            bool? isSecondaryAudio = audioStream is null ? null : item.IsSecondaryAudio(audioStream);
 1461056            int? inputAudioBitrate = audioStream?.BitRate;
 1461057            int? audioChannels = audioStream?.Channels;
 1461058            string? audioProfile = audioStream?.Profile;
 1461059            int? inputAudioSampleRate = audioStream?.SampleRate;
 1461060            int? inputAudioBitDepth = audioStream?.BitDepth;
 1061
 1461062            var appliedAudioConditions = options.Profile.CodecProfiles
 1461063                .Where(i => i.Type == CodecType.VideoAudio &&
 1461064                    i.ContainsAnyCodec(playlistItem.AudioCodecs, container) &&
 1461065                    i.ApplyConditions.All(applyCondition => ConditionProcessor.IsVideoAudioConditionSatisfied(applyCondi
 1461066                // Reverse codec profiles for backward compatibility - first codec profile has higher priority
 1461067                .Reverse();
 1068
 5961069            foreach (var codecProfile in appliedAudioConditions)
 1070            {
 4561071                foreach (var transcodingAudioCodec in playlistItem.AudioCodecs)
 1072                {
 1521073                    if (codecProfile.ContainsAnyCodec(transcodingAudioCodec, container))
 1074                    {
 1521075                        ApplyTranscodingConditions(playlistItem, codecProfile.Conditions, transcodingAudioCodec, true, t
 1521076                        break;
 1077                    }
 1078                }
 1079            }
 1080
 1461081            var maxBitrateSetting = options.GetMaxBitrate(false);
 1082            // Honor max rate
 1461083            if (maxBitrateSetting.HasValue)
 1084            {
 1461085                var availableBitrateForVideo = maxBitrateSetting.Value;
 1086
 1461087                if (playlistItem.AudioBitrate.HasValue)
 1088                {
 1461089                    availableBitrateForVideo -= playlistItem.AudioBitrate.Value;
 1090                }
 1091
 1092                // Make sure the video bitrate is lower than bitrate settings but at least 64k
 1093                // Don't use Math.Clamp as availableBitrateForVideo can be lower then 64k.
 1461094                var currentValue = playlistItem.VideoBitrate ?? availableBitrateForVideo;
 1461095                playlistItem.VideoBitrate = Math.Max(Math.Min(availableBitrateForVideo, currentValue), 64_000);
 1096            }
 1097
 1461098            _logger.LogDebug(
 1461099                "Transcode Result for Profile: {Profile}, Path: {Path}, PlayMethod: {PlayMethod}, AudioStreamIndex: {Aud
 1461100                options.Profile.Name ?? "Anonymous Profile",
 1461101                item.Path ?? "Unknown path",
 1461102                playlistItem.PlayMethod,
 1461103                audioStream?.Index,
 1461104                playlistItem.SubtitleStreamIndex,
 1461105                playlistItem.TranscodeReasons);
 1461106        }
 1107
 1108        private static int GetDefaultAudioBitrate(string? audioCodec, int? audioChannels)
 1109        {
 601110            if (!string.IsNullOrEmpty(audioCodec))
 1111            {
 1112                // Default to a higher bitrate for stream copy
 601113                if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase)
 601114                    || string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase)
 601115                    || string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase)
 601116                    || string.Equals(audioCodec, "eac3", StringComparison.OrdinalIgnoreCase))
 1117                {
 551118                    if ((audioChannels ?? 0) < 2)
 1119                    {
 01120                        return 128000;
 1121                    }
 1122
 551123                    return (audioChannels ?? 0) >= 6 ? 640000 : 384000;
 1124                }
 1125
 51126                if (string.Equals(audioCodec, "flac", StringComparison.OrdinalIgnoreCase)
 51127                    || string.Equals(audioCodec, "alac", StringComparison.OrdinalIgnoreCase))
 1128                {
 01129                    if ((audioChannels ?? 0) < 2)
 1130                    {
 01131                        return 768000;
 1132                    }
 1133
 01134                    return (audioChannels ?? 0) >= 6 ? 3584000 : 1536000;
 1135                }
 1136            }
 1137
 51138            return 192000;
 1139        }
 1140
 1141        private static int GetAudioBitrate(long maxTotalBitrate, IReadOnlyList<string> targetAudioCodecs, MediaStream? a
 1142        {
 1461143            string? targetAudioCodec = targetAudioCodecs.Count == 0 ? null : targetAudioCodecs[0];
 1144
 1461145            int? targetAudioChannels = item.GetTargetAudioChannels(targetAudioCodec);
 1146
 1147            int defaultBitrate;
 1461148            int encoderAudioBitrateLimit = int.MaxValue;
 1149
 1461150            if (audioStream is null)
 1151            {
 11152                defaultBitrate = 192000;
 1153            }
 1154            else
 1155            {
 1451156                if (targetAudioChannels.HasValue
 1451157                    && audioStream.Channels.HasValue
 1451158                    && audioStream.Channels.Value > targetAudioChannels.Value)
 1159                {
 1160                    // Reduce the bitrate if we're down mixing.
 461161                    defaultBitrate = GetDefaultAudioBitrate(targetAudioCodec, targetAudioChannels);
 1162                }
 991163                else if (targetAudioChannels.HasValue
 991164                         && audioStream.Channels.HasValue
 991165                         && audioStream.Channels.Value <= targetAudioChannels.Value
 991166                         && !string.IsNullOrEmpty(audioStream.Codec)
 991167                         && targetAudioCodecs is not null
 991168                         && targetAudioCodecs.Count > 0
 991169                         && !targetAudioCodecs.Any(elem => string.Equals(audioStream.Codec, elem, StringComparison.Ordin
 1170                {
 1171                    // Shift the bitrate if we're transcoding to a different audio codec.
 141172                    defaultBitrate = GetDefaultAudioBitrate(targetAudioCodec, audioStream.Channels.Value);
 1173                }
 1174                else
 1175                {
 851176                    defaultBitrate = audioStream.BitRate ?? GetDefaultAudioBitrate(targetAudioCodec, targetAudioChannels
 1177                }
 1178
 1179                // Seeing webm encoding failures when source has 1 audio channel and 22k bitrate.
 1180                // Any attempts to transcode over 64k will fail
 1451181                if (audioStream.Channels == 1
 1451182                    && (audioStream.BitRate ?? 0) < 64000)
 1183                {
 01184                    encoderAudioBitrateLimit = 64000;
 1185                }
 1186            }
 1187
 1461188            if (maxTotalBitrate > 0)
 1189            {
 1461190                defaultBitrate = Math.Min(GetMaxAudioBitrateForTotalBitrate(maxTotalBitrate), defaultBitrate);
 1191            }
 1192
 1461193            return Math.Min(defaultBitrate, encoderAudioBitrateLimit);
 1194        }
 1195
 1196        private static int GetMaxAudioBitrateForTotalBitrate(long totalBitrate)
 1197        {
 1461198            if (totalBitrate <= 640000)
 1199            {
 81200                return 128000;
 1201            }
 1202
 1381203            if (totalBitrate <= 2000000)
 1204            {
 01205                return 384000;
 1206            }
 1207
 1381208            if (totalBitrate <= 3000000)
 1209            {
 01210                return 448000;
 1211            }
 1212
 1381213            if (totalBitrate <= 4000000)
 1214            {
 01215                return 640000;
 1216            }
 1217
 1381218            if (totalBitrate <= 5000000)
 1219            {
 01220                return 768000;
 1221            }
 1222
 1381223            if (totalBitrate <= 10000000)
 1224            {
 81225                return 1536000;
 1226            }
 1227
 1301228            if (totalBitrate <= 15000000)
 1229            {
 01230                return 2304000;
 1231            }
 1232
 1301233            if (totalBitrate <= 20000000)
 1234            {
 141235                return 3584000;
 1236            }
 1237
 1161238            return 7168000;
 1239        }
 1240
 1241        private (DirectPlayProfile? Profile, PlayMethod? PlayMethod, int? AudioStreamIndex, TranscodeReason TranscodeRea
 1242            MediaOptions options,
 1243            MediaSourceInfo mediaSource,
 1244            MediaStream? videoStream,
 1245            MediaStream? audioStream,
 1246            ICollection<MediaStream> candidateAudioStreams,
 1247            MediaStream? subtitleStream,
 1248            bool isEligibleForDirectPlay,
 1249            bool isEligibleForDirectStream)
 1250        {
 2541251            if (options.ForceDirectPlay)
 1252            {
 01253                return (null, PlayMethod.DirectPlay, audioStream?.Index, 0);
 1254            }
 1255
 2541256            if (options.ForceDirectStream)
 1257            {
 01258                return (null, PlayMethod.DirectStream, audioStream?.Index, 0);
 1259            }
 1260
 2541261            DeviceProfile profile = options.Profile;
 2541262            string container = mediaSource.Container;
 1263
 1264            // Check container conditions
 2541265            var containerProfileReasons = GetCompatibilityContainer(options, mediaSource, container, videoStream);
 1266
 1267            // Check video conditions
 2541268            var videoCodecProfileReasons = videoStream is null ? default : GetCompatibilityVideoCodec(options, mediaSour
 1269
 1270            // Check audio candidates profile conditions
 2541271            var audioStreamMatches = candidateAudioStreams.ToDictionary(s => s, audioStream => GetCompatibilityAudioCode
 1272
 2541273            TranscodeReason subtitleProfileReasons = 0;
 2541274            if (subtitleStream is not null)
 1275            {
 2231276                var subtitleProfile = GetSubtitleProfile(mediaSource, subtitleStream, options.Profile.SubtitleProfiles, 
 1277
 2231278                if (subtitleProfile.Method != SubtitleDeliveryMethod.Drop
 2231279                    && subtitleProfile.Method != SubtitleDeliveryMethod.External
 2231280                    && subtitleProfile.Method != SubtitleDeliveryMethod.Embed)
 1281                {
 01282                    _logger.LogDebug("Not eligible for {0} due to unsupported subtitles", PlayMethod.DirectPlay);
 01283                    subtitleProfileReasons |= TranscodeReason.SubtitleCodecNotSupported;
 1284                }
 1285            }
 1286
 2541287            var containerSupported = false;
 2541288            TranscodeReason[] rankings = [TranscodeReason.VideoCodecNotSupported, VideoCodecReasons, TranscodeReason.Aud
 1289
 1290            // Check DirectPlay profiles to see if it can be direct played
 2541291            var analyzedProfiles = profile.DirectPlayProfiles
 2541292                .Where(directPlayProfile => directPlayProfile.Type == DlnaProfileType.Video)
 2541293                .Select((directPlayProfile, order) =>
 2541294                {
 2541295                    TranscodeReason directPlayProfileReasons = 0;
 2541296                    TranscodeReason audioCodecProfileReasons = 0;
 2541297
 2541298                    // Check container type
 2541299                    if (!directPlayProfile.SupportsContainer(container))
 2541300                    {
 2541301                        directPlayProfileReasons |= TranscodeReason.ContainerNotSupported;
 2541302                    }
 2541303                    else
 2541304                    {
 2541305                        containerSupported = true;
 2541306                    }
 2541307
 2541308                    // Check video codec
 2541309                    string? videoCodec = videoStream?.Codec;
 2541310                    if (!directPlayProfile.SupportsVideoCodec(videoCodec))
 2541311                    {
 2541312                        directPlayProfileReasons |= TranscodeReason.VideoCodecNotSupported;
 2541313                    }
 2541314
 2541315                    // Check audio codec
 2541316                    MediaStream? selectedAudioStream = null;
 2541317                    if (candidateAudioStreams.Count != 0)
 2541318                    {
 2541319                        selectedAudioStream = candidateAudioStreams.FirstOrDefault(audioStream => directPlayProfile.Supp
 2541320                        if (selectedAudioStream is null)
 2541321                        {
 2541322                            directPlayProfileReasons |= TranscodeReason.AudioCodecNotSupported;
 2541323                        }
 2541324                        else
 2541325                        {
 2541326                            audioCodecProfileReasons = audioStreamMatches.GetValueOrDefault(selectedAudioStream);
 2541327                        }
 2541328                    }
 2541329
 2541330                    var failureReasons = directPlayProfileReasons | containerProfileReasons | subtitleProfileReasons;
 2541331
 2541332                    if ((failureReasons & TranscodeReason.VideoCodecNotSupported) == 0)
 2541333                    {
 2541334                        failureReasons |= videoCodecProfileReasons;
 2541335                    }
 2541336
 2541337                    if ((failureReasons & TranscodeReason.AudioCodecNotSupported) == 0)
 2541338                    {
 2541339                        failureReasons |= audioCodecProfileReasons;
 2541340                    }
 2541341
 2541342                    var directStreamFailureReasons = failureReasons & (~DirectStreamReasons);
 2541343
 2541344                    PlayMethod? playMethod = null;
 2541345                    if (failureReasons == 0 && isEligibleForDirectPlay && mediaSource.SupportsDirectPlay)
 2541346                    {
 2541347                        playMethod = PlayMethod.DirectPlay;
 2541348                    }
 2541349                    else if (directStreamFailureReasons == 0 && isEligibleForDirectStream && mediaSource.SupportsDirectS
 2541350                    {
 2541351                        playMethod = PlayMethod.DirectStream;
 2541352                    }
 2541353
 2541354                    var ranked = GetRank(ref failureReasons, rankings);
 2541355
 2541356                    return (Result: (Profile: directPlayProfile, PlayMethod: playMethod, AudioStreamIndex: selectedAudio
 2541357                })
 2541358                .OrderByDescending(analysis => analysis.Result.PlayMethod)
 2541359                .ThenByDescending(analysis => analysis.Rank)
 2541360                .ThenBy(analysis => analysis.Order)
 2541361                .ToArray()
 2541362                .ToLookup(analysis => analysis.Result.PlayMethod is not null);
 1363
 2541364            var profileMatch = analyzedProfiles[true]
 2541365                .Select(analysis => analysis.Result)
 2541366                .FirstOrDefault();
 2541367            if (profileMatch.Profile is not null)
 1368            {
 1241369                return profileMatch;
 1370            }
 1371
 1301372            var failureReasons = analyzedProfiles[false]
 1301373                .Select(analysis => analysis.Result)
 1301374                .Where(result => !containerSupported || !result.TranscodeReason.HasFlag(TranscodeReason.ContainerNotSupp
 1301375                .FirstOrDefault().TranscodeReason;
 1301376            if (failureReasons == 0)
 1377            {
 141378                failureReasons = TranscodeReason.DirectPlayError;
 1379            }
 1380
 1301381            return (Profile: null, PlayMethod: null, AudioStreamIndex: null, TranscodeReasons: failureReasons);
 1382        }
 1383
 1384        private TranscodeReason AggregateFailureConditions(MediaSourceInfo mediaSource, DeviceProfile profile, string ty
 1385        {
 16611386            return conditions.Aggregate<ProfileCondition, TranscodeReason>(0, (reasons, i) =>
 16611387            {
 16611388                LogConditionFailure(profile, type, i, mediaSource);
 16611389                var transcodeReasons = GetTranscodeReasonForFailedCondition(i);
 16611390                return reasons | transcodeReasons;
 16611391            });
 1392        }
 1393
 1394        private void LogConditionFailure(DeviceProfile profile, string type, ProfileCondition condition, MediaSourceInfo
 1395        {
 2471396            _logger.LogDebug(
 2471397                "Profile: {0}, DirectPlay=false. Reason={1}.{2} Condition: {3}. ConditionValue: {4}. IsRequired: {5}. Pa
 2471398                type,
 2471399                profile.Name ?? "Unknown Profile",
 2471400                condition.Property,
 2471401                condition.Condition,
 2471402                condition.Value ?? string.Empty,
 2471403                condition.IsRequired,
 2471404                mediaSource.Path ?? "Unknown path");
 2471405        }
 1406
 1407        /// <summary>
 1408        /// Normalizes input container.
 1409        /// </summary>
 1410        /// <param name="mediaSource">The <see cref="MediaSourceInfo"/>.</param>
 1411        /// <param name="subtitleStream">The <see cref="MediaStream"/> of the subtitle stream.</param>
 1412        /// <param name="subtitleProfiles">The list of supported <see cref="SubtitleProfile"/>s.</param>
 1413        /// <param name="playMethod">The <see cref="PlayMethod"/>.</param>
 1414        /// <param name="transcoderSupport">The <see cref="ITranscoderSupport"/>.</param>
 1415        /// <param name="outputContainer">The output container.</param>
 1416        /// <param name="transcodingSubProtocol">The subtitle transcoding protocol.</param>
 1417        /// <returns>The normalized input container.</returns>
 1418        public static SubtitleProfile GetSubtitleProfile(
 1419            MediaSourceInfo mediaSource,
 1420            MediaStream subtitleStream,
 1421            SubtitleProfile[] subtitleProfiles,
 1422            PlayMethod playMethod,
 1423            ITranscoderSupport transcoderSupport,
 1424            string? outputContainer,
 1425            MediaStreamProtocol? transcodingSubProtocol)
 1426        {
 4621427            if (!subtitleStream.IsExternal && (playMethod != PlayMethod.Transcode || transcodingSubProtocol != MediaStre
 1428            {
 1429                // Look for supported embedded subs of the same format
 01430                foreach (var profile in subtitleProfiles)
 1431                {
 01432                    if (!profile.SupportsLanguage(subtitleStream.Language))
 1433                    {
 1434                        continue;
 1435                    }
 1436
 01437                    if (profile.Method != SubtitleDeliveryMethod.Embed)
 1438                    {
 1439                        continue;
 1440                    }
 1441
 01442                    if (!ContainerHelper.ContainsContainer(profile.Container, outputContainer))
 1443                    {
 1444                        continue;
 1445                    }
 1446
 01447                    if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(outputContainer))
 1448                    {
 1449                        continue;
 1450                    }
 1451
 01452                    if (subtitleStream.IsTextSubtitleStream == MediaStream.IsTextFormat(profile.Format) && string.Equals
 1453                    {
 01454                        return profile;
 1455                    }
 1456                }
 1457
 1458                // Look for supported embedded subs of a convertible format
 01459                foreach (var profile in subtitleProfiles)
 1460                {
 01461                    if (!profile.SupportsLanguage(subtitleStream.Language))
 1462                    {
 1463                        continue;
 1464                    }
 1465
 01466                    if (profile.Method != SubtitleDeliveryMethod.Embed)
 1467                    {
 1468                        continue;
 1469                    }
 1470
 01471                    if (!ContainerHelper.ContainsContainer(profile.Container, outputContainer))
 1472                    {
 1473                        continue;
 1474                    }
 1475
 01476                    if (playMethod == PlayMethod.Transcode && !IsSubtitleEmbedSupported(outputContainer))
 1477                    {
 1478                        continue;
 1479                    }
 1480
 01481                    if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsSubtitleConversionTo(profile.Forma
 1482                    {
 01483                        return profile;
 1484                    }
 1485                }
 1486            }
 1487
 1488            // Look for an external or hls profile that matches the stream type (text/graphical) and doesn't require con
 4621489            return GetExternalSubtitleProfile(mediaSource, subtitleStream, subtitleProfiles, playMethod, transcoderSuppo
 4621490                GetExternalSubtitleProfile(mediaSource, subtitleStream, subtitleProfiles, playMethod, transcoderSupport,
 4621491                new SubtitleProfile
 4621492                {
 4621493                    Method = SubtitleDeliveryMethod.Encode,
 4621494                    Format = subtitleStream.Codec
 4621495                };
 1496        }
 1497
 1498        private static bool IsSubtitleEmbedSupported(string? transcodingContainer)
 1499        {
 01500            if (!string.IsNullOrEmpty(transcodingContainer))
 1501            {
 01502                if (ContainerHelper.ContainsContainer(transcodingContainer, "ts,mpegts,mp4"))
 1503                {
 01504                    return false;
 1505                }
 1506
 01507                if (ContainerHelper.ContainsContainer(transcodingContainer, "mkv,matroska"))
 1508                {
 01509                    return true;
 1510                }
 1511            }
 1512
 01513            return false;
 1514        }
 1515
 1516        private static SubtitleProfile? GetExternalSubtitleProfile(MediaSourceInfo mediaSource, MediaStream subtitleStre
 1517        {
 50661518            foreach (var profile in subtitleProfiles)
 1519            {
 20161520                if (profile.Method != SubtitleDeliveryMethod.External && profile.Method != SubtitleDeliveryMethod.Hls)
 1521                {
 1522                    continue;
 1523                }
 1524
 13821525                if (profile.Method == SubtitleDeliveryMethod.Hls && playMethod != PlayMethod.Transcode)
 1526                {
 1527                    continue;
 1528                }
 1529
 13821530                if (!profile.SupportsLanguage(subtitleStream.Language))
 1531                {
 1532                    continue;
 1533                }
 1534
 13821535                if (!subtitleStream.IsExternal && !transcoderSupport.CanExtractSubtitles(subtitleStream.Codec))
 1536                {
 1537                    continue;
 1538                }
 1539
 13821540                if ((profile.Method == SubtitleDeliveryMethod.External && subtitleStream.IsTextSubtitleStream == MediaSt
 13821541                    (profile.Method == SubtitleDeliveryMethod.Hls && subtitleStream.IsTextSubtitleStream))
 1542                {
 13641543                    bool requiresConversion = !string.Equals(subtitleStream.Codec, profile.Format, StringComparison.Ordi
 1544
 13641545                    if (!requiresConversion)
 1546                    {
 1761547                        return profile;
 1548                    }
 1549
 11881550                    if (!allowConversion)
 1551                    {
 1552                        continue;
 1553                    }
 1554
 1555                    // TODO: Build this into subtitleStream.SupportsExternalStream
 2861556                    if (mediaSource.IsInfiniteStream)
 1557                    {
 1558                        continue;
 1559                    }
 1560
 2861561                    if (subtitleStream.IsTextSubtitleStream && subtitleStream.SupportsExternalStream && subtitleStream.S
 1562                    {
 2861563                        return profile;
 1564                    }
 1565                }
 1566            }
 1567
 2861568            return null;
 1569        }
 1570
 1571        private bool IsBitrateLimitExceeded(MediaSourceInfo item, long maxBitrate)
 1572        {
 1573            // Don't restrict bitrate if item is remote.
 2781574            if (item.IsRemote)
 1575            {
 01576                return false;
 1577            }
 1578
 1579            // If no maximum bitrate is set, default to no maximum bitrate.
 2781580            long requestedMaxBitrate = maxBitrate > 0 ? maxBitrate : int.MaxValue;
 1581
 1582            // If we don't know the item bitrate, then force a transcode if requested max bitrate is under 40 mbps
 2781583            int itemBitrate = item.Bitrate ?? 40000000;
 1584
 2781585            if (itemBitrate > requestedMaxBitrate)
 1586            {
 241587                _logger.LogDebug(
 241588                    "Bitrate exceeds limit: media bitrate: {MediaBitrate}, max bitrate: {MaxBitrate}",
 241589                    itemBitrate,
 241590                    requestedMaxBitrate);
 241591                return true;
 1592            }
 1593
 2541594            return false;
 1595        }
 1596
 1597        private static void ValidateMediaOptions(MediaOptions options, bool isMediaSource)
 1598        {
 2781599            if (options.ItemId.IsEmpty())
 1600            {
 01601                ArgumentException.ThrowIfNullOrEmpty(options.DeviceId);
 1602            }
 1603
 2781604            if (options.Profile is null)
 1605            {
 01606                throw new ArgumentException("Profile is required");
 1607            }
 1608
 2781609            if (options.MediaSources is null)
 1610            {
 01611                throw new ArgumentException("MediaSources is required");
 1612            }
 1613
 2781614            if (isMediaSource)
 1615            {
 2781616                if (options.AudioStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
 1617                {
 01618                    throw new ArgumentException("MediaSourceId is required when a specific audio stream is requested");
 1619                }
 1620
 2781621                if (options.SubtitleStreamIndex.HasValue && string.IsNullOrEmpty(options.MediaSourceId))
 1622                {
 01623                    throw new ArgumentException("MediaSourceId is required when a specific subtitle stream is requested"
 1624                }
 1625            }
 2781626        }
 1627
 1628        private static IEnumerable<ProfileCondition> GetProfileConditionsForVideoAudio(
 1629            IEnumerable<CodecProfile> codecProfiles,
 1630            string container,
 1631            string codec,
 1632            int? audioChannels,
 1633            int? audioBitrate,
 1634            int? audioSampleRate,
 1635            int? audioBitDepth,
 1636            string audioProfile,
 1637            bool? isSecondaryAudio)
 1638        {
 10051639            return codecProfiles
 10051640                .Where(profile => profile.Type == CodecType.VideoAudio &&
 10051641                    profile.ContainsAnyCodec(codec, container) &&
 10051642                    profile.ApplyConditions.All(applyCondition => ConditionProcessor.IsVideoAudioConditionSatisfied(appl
 10051643                .SelectMany(profile => profile.Conditions)
 10051644                .Where(condition => !ConditionProcessor.IsVideoAudioConditionSatisfied(condition, audioChannels, audioBi
 1645        }
 1646
 1647        private static IEnumerable<ProfileCondition> GetProfileConditionsForAudio(
 1648            IEnumerable<CodecProfile> codecProfiles,
 1649            string container,
 1650            string? codec,
 1651            int? audioChannels,
 1652            int? audioBitrate,
 1653            int? audioSampleRate,
 1654            int? audioBitDepth,
 1655            bool checkConditions)
 1656        {
 01657            var conditions = codecProfiles
 01658                .Where(profile => profile.Type == CodecType.Audio &&
 01659                    profile.ContainsAnyCodec(codec, container) &&
 01660                    profile.ApplyConditions.All(applyCondition => ConditionProcessor.IsAudioConditionSatisfied(applyCond
 01661                .SelectMany(profile => profile.Conditions);
 1662
 01663            if (!checkConditions)
 1664            {
 01665                return conditions;
 1666            }
 1667
 01668            return conditions.Where(condition => !ConditionProcessor.IsAudioConditionSatisfied(condition, audioChannels,
 1669        }
 1670
 1671        private void ApplyTranscodingConditions(StreamInfo item, IEnumerable<ProfileCondition> conditions, string? quali
 1672        {
 34921673            foreach (ProfileCondition condition in conditions)
 1674            {
 12441675                string value = condition.Value;
 1676
 12441677                if (string.IsNullOrEmpty(value))
 1678                {
 1679                    continue;
 1680                }
 1681
 1682                // No way to express this
 12441683                if (condition.Condition == ProfileConditionType.GreaterThanEqual)
 1684                {
 1685                    continue;
 1686                }
 1687
 12441688                switch (condition.Property)
 1689                {
 1690                    case ProfileConditionValue.AudioBitrate:
 1691                        {
 01692                            if (!enableNonQualifiedConditions)
 1693                            {
 1694                                continue;
 1695                            }
 1696
 01697                            if (int.TryParse(value, CultureInfo.InvariantCulture, out var num))
 1698                            {
 01699                                if (condition.Condition == ProfileConditionType.Equals)
 1700                                {
 01701                                    item.AudioBitrate = num;
 1702                                }
 01703                                else if (condition.Condition == ProfileConditionType.LessThanEqual)
 1704                                {
 01705                                    item.AudioBitrate = Math.Min(num, item.AudioBitrate ?? num);
 1706                                }
 01707                                else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
 1708                                {
 01709                                    item.AudioBitrate = Math.Max(num, item.AudioBitrate ?? num);
 1710                                }
 1711                            }
 1712
 01713                            break;
 1714                        }
 1715
 1716                    case ProfileConditionValue.AudioSampleRate:
 1717                        {
 01718                            if (!enableNonQualifiedConditions)
 1719                            {
 1720                                continue;
 1721                            }
 1722
 01723                            if (int.TryParse(value, CultureInfo.InvariantCulture, out var num))
 1724                            {
 01725                                if (condition.Condition == ProfileConditionType.Equals)
 1726                                {
 01727                                    item.AudioSampleRate = num;
 1728                                }
 01729                                else if (condition.Condition == ProfileConditionType.LessThanEqual)
 1730                                {
 01731                                    item.AudioSampleRate = Math.Min(num, item.AudioSampleRate ?? num);
 1732                                }
 01733                                else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
 1734                                {
 01735                                    item.AudioSampleRate = Math.Max(num, item.AudioSampleRate ?? num);
 1736                                }
 1737                            }
 1738
 01739                            break;
 1740                        }
 1741
 1742                    case ProfileConditionValue.AudioChannels:
 1743                        {
 281744                            if (string.IsNullOrEmpty(qualifier))
 1745                            {
 01746                                if (!enableNonQualifiedConditions)
 1747                                {
 01748                                    continue;
 1749                                }
 1750                            }
 1751                            else
 1752                            {
 281753                                if (!enableQualifiedConditions)
 1754                                {
 1755                                    continue;
 1756                                }
 1757                            }
 1758
 281759                            if (int.TryParse(value, CultureInfo.InvariantCulture, out var num))
 1760                            {
 281761                                if (condition.Condition == ProfileConditionType.Equals)
 1762                                {
 01763                                    item.SetOption(qualifier, "audiochannels", num.ToString(CultureInfo.InvariantCulture
 1764                                }
 281765                                else if (condition.Condition == ProfileConditionType.LessThanEqual)
 1766                                {
 281767                                    item.SetOption(qualifier, "audiochannels", Math.Min(num, item.GetTargetAudioChannels
 1768                                }
 01769                                else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
 1770                                {
 01771                                    item.SetOption(qualifier, "audiochannels", Math.Max(num, item.GetTargetAudioChannels
 1772                                }
 1773                            }
 1774
 01775                            break;
 1776                        }
 1777
 1778                    case ProfileConditionValue.IsAvc:
 1779                        {
 01780                            if (!enableNonQualifiedConditions)
 1781                            {
 1782                                continue;
 1783                            }
 1784
 01785                            if (bool.TryParse(value, out var isAvc))
 1786                            {
 01787                                if (isAvc && condition.Condition == ProfileConditionType.Equals)
 1788                                {
 01789                                    item.RequireAvc = true;
 1790                                }
 01791                                else if (!isAvc && condition.Condition == ProfileConditionType.NotEquals)
 1792                                {
 01793                                    item.RequireAvc = true;
 1794                                }
 1795                            }
 1796
 01797                            break;
 1798                        }
 1799
 1800                    case ProfileConditionValue.IsAnamorphic:
 1801                        {
 2011802                            if (!enableNonQualifiedConditions)
 1803                            {
 1804                                continue;
 1805                            }
 1806
 2011807                            if (bool.TryParse(value, out var isAnamorphic))
 1808                            {
 2011809                                if (isAnamorphic && condition.Condition == ProfileConditionType.Equals)
 1810                                {
 01811                                    item.RequireNonAnamorphic = true;
 1812                                }
 2011813                                else if (!isAnamorphic && condition.Condition == ProfileConditionType.NotEquals)
 1814                                {
 01815                                    item.RequireNonAnamorphic = true;
 1816                                }
 1817                            }
 1818
 01819                            break;
 1820                        }
 1821
 1822                    case ProfileConditionValue.IsInterlaced:
 1823                        {
 1091824                            if (string.IsNullOrEmpty(qualifier))
 1825                            {
 01826                                if (!enableNonQualifiedConditions)
 1827                                {
 01828                                    continue;
 1829                                }
 1830                            }
 1831                            else
 1832                            {
 1091833                                if (!enableQualifiedConditions)
 1834                                {
 1835                                    continue;
 1836                                }
 1837                            }
 1838
 1091839                            if (bool.TryParse(value, out var isInterlaced))
 1840                            {
 1091841                                if (!isInterlaced && condition.Condition == ProfileConditionType.Equals)
 1842                                {
 01843                                    item.SetOption(qualifier, "deinterlace", "true");
 1844                                }
 1091845                                else if (isInterlaced && condition.Condition == ProfileConditionType.NotEquals)
 1846                                {
 1091847                                    item.SetOption(qualifier, "deinterlace", "true");
 1848                                }
 1849                            }
 1850
 1091851                            break;
 1852                        }
 1853
 1854                    case ProfileConditionValue.AudioProfile:
 1855                    case ProfileConditionValue.Has64BitOffsets:
 1856                    case ProfileConditionValue.PacketLength:
 1857                    case ProfileConditionValue.NumStreams:
 1858                    case ProfileConditionValue.NumAudioStreams:
 1859                    case ProfileConditionValue.NumVideoStreams:
 1860                    case ProfileConditionValue.IsSecondaryAudio:
 1861                    case ProfileConditionValue.VideoTimestamp:
 1862                        {
 1863                            // Not supported yet
 1864                            break;
 1865                        }
 1866
 1867                    case ProfileConditionValue.RefFrames:
 1868                        {
 21869                            if (string.IsNullOrEmpty(qualifier))
 1870                            {
 01871                                if (!enableNonQualifiedConditions)
 1872                                {
 01873                                    continue;
 1874                                }
 1875                            }
 1876                            else
 1877                            {
 21878                                if (!enableQualifiedConditions)
 1879                                {
 1880                                    continue;
 1881                                }
 1882                            }
 1883
 21884                            if (int.TryParse(value, CultureInfo.InvariantCulture, out var num))
 1885                            {
 21886                                if (condition.Condition == ProfileConditionType.Equals)
 1887                                {
 01888                                    item.SetOption(qualifier, "maxrefframes", num.ToString(CultureInfo.InvariantCulture)
 1889                                }
 21890                                else if (condition.Condition == ProfileConditionType.LessThanEqual)
 1891                                {
 21892                                    item.SetOption(qualifier, "maxrefframes", Math.Min(num, item.GetTargetRefFrames(qual
 1893                                }
 01894                                else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
 1895                                {
 01896                                    item.SetOption(qualifier, "maxrefframes", Math.Max(num, item.GetTargetRefFrames(qual
 1897                                }
 1898                            }
 1899
 01900                            break;
 1901                        }
 1902
 1903                    case ProfileConditionValue.VideoBitDepth:
 1904                        {
 01905                            if (string.IsNullOrEmpty(qualifier))
 1906                            {
 01907                                if (!enableNonQualifiedConditions)
 1908                                {
 01909                                    continue;
 1910                                }
 1911                            }
 1912                            else
 1913                            {
 01914                                if (!enableQualifiedConditions)
 1915                                {
 1916                                    continue;
 1917                                }
 1918                            }
 1919
 01920                            if (int.TryParse(value, CultureInfo.InvariantCulture, out var num))
 1921                            {
 01922                                if (condition.Condition == ProfileConditionType.Equals)
 1923                                {
 01924                                    item.SetOption(qualifier, "videobitdepth", num.ToString(CultureInfo.InvariantCulture
 1925                                }
 01926                                else if (condition.Condition == ProfileConditionType.LessThanEqual)
 1927                                {
 01928                                    item.SetOption(qualifier, "videobitdepth", Math.Min(num, item.GetTargetVideoBitDepth
 1929                                }
 01930                                else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
 1931                                {
 01932                                    item.SetOption(qualifier, "videobitdepth", Math.Max(num, item.GetTargetVideoBitDepth
 1933                                }
 1934                            }
 1935
 01936                            break;
 1937                        }
 1938
 1939                    case ProfileConditionValue.VideoProfile:
 1940                        {
 2241941                            if (string.IsNullOrEmpty(qualifier))
 1942                            {
 1943                                continue;
 1944                            }
 1945
 1946                            // Change from split by | to comma
 1947                            // Strip spaces to avoid having to encode
 2241948                            var values = value
 2241949                                .Split('|', StringSplitOptions.RemoveEmptyEntries);
 1950
 2241951                            if (condition.Condition == ProfileConditionType.Equals)
 1952                            {
 01953                                item.SetOption(qualifier, "profile", string.Join(',', values));
 1954                            }
 2241955                            else if (condition.Condition == ProfileConditionType.EqualsAny)
 1956                            {
 2241957                                var currentValue = item.GetOption(qualifier, "profile");
 2241958                                if (!string.IsNullOrEmpty(currentValue) && values.Any(value => value == currentValue))
 1959                                {
 711960                                    item.SetOption(qualifier, "profile", currentValue);
 1961                                }
 1962                                else
 1963                                {
 1531964                                    item.SetOption(qualifier, "profile", string.Join(',', values));
 1965                                }
 1966                            }
 1967
 1531968                            break;
 1969                        }
 1970
 1971                    case ProfileConditionValue.VideoRangeType:
 1972                        {
 2581973                            if (string.IsNullOrEmpty(qualifier))
 1974                            {
 1975                                continue;
 1976                            }
 1977
 1978                            // change from split by | to comma
 1979                            // strip spaces to avoid having to encode
 2581980                            var values = value
 2581981                                .Split('|', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
 1982
 2581983                            if (condition.Condition == ProfileConditionType.Equals)
 1984                            {
 01985                                item.SetOption(qualifier, "rangetype", string.Join(',', values));
 1986                            }
 2581987                            else if (condition.Condition == ProfileConditionType.NotEquals)
 1988                            {
 01989                                item.SetOption(qualifier, "rangetype", string.Join(',', Enum.GetNames(typeof(VideoRangeT
 1990                            }
 2581991                            else if (condition.Condition == ProfileConditionType.EqualsAny)
 1992                            {
 2581993                                var currentValue = item.GetOption(qualifier, "rangetype");
 2581994                                if (!string.IsNullOrEmpty(currentValue) && values.Any(v => string.Equals(v, currentValue
 1995                                {
 01996                                    item.SetOption(qualifier, "rangetype", currentValue);
 1997                                }
 1998                                else
 1999                                {
 2582000                                    item.SetOption(qualifier, "rangetype", string.Join(',', values));
 2001                                }
 2002                            }
 2003
 2582004                            break;
 2005                        }
 2006
 2007                    case ProfileConditionValue.VideoCodecTag:
 2008                        {
 122009                            if (string.IsNullOrEmpty(qualifier))
 2010                            {
 2011                                continue;
 2012                            }
 2013
 2014                            // change from split by | to comma
 2015                            // strip spaces to avoid having to encode
 122016                            var values = value
 122017                                .Split('|', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
 2018
 122019                            if (condition.Condition == ProfileConditionType.Equals)
 2020                            {
 02021                                item.SetOption(qualifier, "codectag", string.Join(',', values));
 2022                            }
 122023                            else if (condition.Condition == ProfileConditionType.EqualsAny)
 2024                            {
 122025                                var currentValue = item.GetOption(qualifier, "codectag");
 122026                                if (!string.IsNullOrEmpty(currentValue) && values.Any(v => string.Equals(v, currentValue
 2027                                {
 02028                                    item.SetOption(qualifier, "codectag", currentValue);
 2029                                }
 2030                                else
 2031                                {
 122032                                    item.SetOption(qualifier, "codectag", string.Join(',', values));
 2033                                }
 2034                            }
 2035
 122036                            break;
 2037                        }
 2038
 2039                    case ProfileConditionValue.Height:
 2040                        {
 02041                            if (!enableNonQualifiedConditions)
 2042                            {
 2043                                continue;
 2044                            }
 2045
 02046                            if (int.TryParse(value, CultureInfo.InvariantCulture, out var num))
 2047                            {
 02048                                if (condition.Condition == ProfileConditionType.Equals)
 2049                                {
 02050                                    item.MaxHeight = num;
 2051                                }
 02052                                else if (condition.Condition == ProfileConditionType.LessThanEqual)
 2053                                {
 02054                                    item.MaxHeight = Math.Min(num, item.MaxHeight ?? num);
 2055                                }
 02056                                else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
 2057                                {
 02058                                    item.MaxHeight = Math.Max(num, item.MaxHeight ?? num);
 2059                                }
 2060                            }
 2061
 02062                            break;
 2063                        }
 2064
 2065                    case ProfileConditionValue.VideoBitrate:
 2066                        {
 322067                            if (!enableNonQualifiedConditions)
 2068                            {
 2069                                continue;
 2070                            }
 2071
 322072                            if (int.TryParse(value, CultureInfo.InvariantCulture, out var num))
 2073                            {
 322074                                if (condition.Condition == ProfileConditionType.Equals)
 2075                                {
 02076                                    item.VideoBitrate = num;
 2077                                }
 322078                                else if (condition.Condition == ProfileConditionType.LessThanEqual)
 2079                                {
 322080                                    item.VideoBitrate = Math.Min(num, item.VideoBitrate ?? num);
 2081                                }
 02082                                else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
 2083                                {
 02084                                    item.VideoBitrate = Math.Max(num, item.VideoBitrate ?? num);
 2085                                }
 2086                            }
 2087
 02088                            break;
 2089                        }
 2090
 2091                    case ProfileConditionValue.VideoFramerate:
 2092                        {
 122093                            if (!enableNonQualifiedConditions)
 2094                            {
 2095                                continue;
 2096                            }
 2097
 122098                            if (float.TryParse(value, CultureInfo.InvariantCulture, out var num))
 2099                            {
 122100                                if (condition.Condition == ProfileConditionType.Equals)
 2101                                {
 02102                                    item.MaxFramerate = num;
 2103                                }
 122104                                else if (condition.Condition == ProfileConditionType.LessThanEqual)
 2105                                {
 122106                                    item.MaxFramerate = Math.Min(num, item.MaxFramerate ?? num);
 2107                                }
 02108                                else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
 2109                                {
 02110                                    item.MaxFramerate = Math.Max(num, item.MaxFramerate ?? num);
 2111                                }
 2112                            }
 2113
 02114                            break;
 2115                        }
 2116
 2117                    case ProfileConditionValue.VideoLevel:
 2118                        {
 2122119                            if (string.IsNullOrEmpty(qualifier))
 2120                            {
 2121                                continue;
 2122                            }
 2123
 2122124                            if (int.TryParse(value, CultureInfo.InvariantCulture, out var num))
 2125                            {
 2122126                                if (condition.Condition == ProfileConditionType.Equals)
 2127                                {
 02128                                    item.SetOption(qualifier, "level", num.ToString(CultureInfo.InvariantCulture));
 2129                                }
 2122130                                else if (condition.Condition == ProfileConditionType.LessThanEqual)
 2131                                {
 2122132                                    item.SetOption(qualifier, "level", Math.Min(num, item.GetTargetVideoLevel(qualifier)
 2133                                }
 02134                                else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
 2135                                {
 02136                                    item.SetOption(qualifier, "level", Math.Max(num, item.GetTargetVideoLevel(qualifier)
 2137                                }
 2138                            }
 2139
 02140                            break;
 2141                        }
 2142
 2143                    case ProfileConditionValue.Width:
 2144                        {
 42145                            if (!enableNonQualifiedConditions)
 2146                            {
 2147                                continue;
 2148                            }
 2149
 42150                            if (int.TryParse(value, CultureInfo.InvariantCulture, out var num))
 2151                            {
 42152                                if (condition.Condition == ProfileConditionType.Equals)
 2153                                {
 02154                                    item.MaxWidth = num;
 2155                                }
 42156                                else if (condition.Condition == ProfileConditionType.LessThanEqual)
 2157                                {
 42158                                    item.MaxWidth = Math.Min(num, item.MaxWidth ?? num);
 2159                                }
 02160                                else if (condition.Condition == ProfileConditionType.GreaterThanEqual)
 2161                                {
 02162                                    item.MaxWidth = Math.Max(num, item.MaxWidth ?? num);
 2163                                }
 2164                            }
 2165
 2166                            break;
 2167                        }
 2168
 2169                    default:
 2170                        break;
 2171                }
 2172            }
 5022173        }
 2174
 2175        private static bool IsAudioContainerSupported(DirectPlayProfile profile, MediaSourceInfo item)
 2176        {
 2177            // Check container type
 02178            if (!profile.SupportsContainer(item.Container))
 2179            {
 02180                return false;
 2181            }
 2182
 2183            // Never direct play audio in matroska when the device only declare support for webm.
 2184            // The first check is not enough because mkv is assumed can be webm.
 2185            // See https://github.com/jellyfin/jellyfin/issues/13344
 02186            return !ContainerHelper.ContainsContainer("mkv", item.Container)
 02187                   || profile.SupportsContainer("mkv");
 2188        }
 2189
 2190        private static bool IsAudioDirectPlaySupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream audi
 2191        {
 02192            if (!IsAudioContainerSupported(profile, item))
 2193            {
 02194                return false;
 2195            }
 2196
 2197            // Check audio codec
 02198            string? audioCodec = audioStream?.Codec;
 02199            if (!profile.SupportsAudioCodec(audioCodec))
 2200            {
 02201                return false;
 2202            }
 2203
 02204            return true;
 2205        }
 2206
 2207        private static bool IsAudioDirectStreamSupported(DirectPlayProfile profile, MediaSourceInfo item, MediaStream au
 2208        {
 2209            // Check container type, this should NOT be supported
 2210            // If the container is supported, the file should be directly played
 02211            if (IsAudioContainerSupported(profile, item))
 2212            {
 02213                return false;
 2214            }
 2215
 2216            // Check audio codec, we cannot use the SupportsAudioCodec here
 2217            // Because that one assumes empty container supports all codec, which is just useless
 02218            string? audioCodec = audioStream?.Codec;
 02219            return string.Equals(profile.AudioCodec, audioCodec, StringComparison.OrdinalIgnoreCase)
 02220                   || string.Equals(profile.Container, audioCodec, StringComparison.OrdinalIgnoreCase);
 2221        }
 2222
 2223        private int GetRank(ref TranscodeReason a, TranscodeReason[] rankings)
 2224        {
 12092225            var index = 1;
 87402226            foreach (var flag in rankings)
 2227            {
 36662228                var reason = a & flag;
 36662229                if (reason != 0)
 2230                {
 10102231                    return index;
 2232                }
 2233
 26562234                index++;
 2235            }
 2236
 1992237            return index;
 2238        }
 2239
 2240        /// <summary>
 2241        /// Check the profile conditions.
 2242        /// </summary>
 2243        /// <param name="conditions">Profile conditions.</param>
 2244        /// <param name="mediaSource">Media source.</param>
 2245        /// <param name="videoStream">Video stream.</param>
 2246        /// <returns>Failed profile conditions.</returns>
 2247        private IEnumerable<ProfileCondition> CheckVideoConditions(ProfileCondition[] conditions, MediaSourceInfo mediaS
 2248        {
 7292249            int? width = videoStream?.Width;
 7292250            int? height = videoStream?.Height;
 7292251            int? bitDepth = videoStream?.BitDepth;
 7292252            int? videoBitrate = videoStream?.BitRate;
 7292253            double? videoLevel = videoStream?.Level;
 7292254            string? videoProfile = videoStream?.Profile;
 7292255            VideoRangeType? videoRangeType = videoStream?.VideoRangeType;
 7292256            float videoFramerate = videoStream is null ? 0 : videoStream.ReferenceFrameRate ?? 0;
 7292257            bool? isAnamorphic = videoStream?.IsAnamorphic;
 7292258            bool? isInterlaced = videoStream?.IsInterlaced;
 7292259            string? videoCodecTag = videoStream?.CodecTag;
 7292260            bool? isAvc = videoStream?.IsAVC;
 2261
 7292262            TransportStreamTimestamp? timestamp = videoStream is null ? TransportStreamTimestamp.None : mediaSource.Time
 7292263            int? packetLength = videoStream?.PacketLength;
 7292264            int? refFrames = videoStream?.RefFrames;
 2265
 7292266            int numStreams = mediaSource.MediaStreams.Count;
 7292267            int? numAudioStreams = mediaSource.GetStreamCount(MediaStreamType.Audio);
 7292268            int? numVideoStreams = mediaSource.GetStreamCount(MediaStreamType.Video);
 2269
 7292270            return conditions.Where(applyCondition => !ConditionProcessor.IsVideoConditionSatisfied(applyCondition, widt
 2271        }
 2272
 2273        /// <summary>
 2274        /// Check the compatibility of the container.
 2275        /// </summary>
 2276        /// <param name="options">Media options.</param>
 2277        /// <param name="mediaSource">Media source.</param>
 2278        /// <param name="container">Container.</param>
 2279        /// <param name="videoStream">Video stream.</param>
 2280        /// <returns>Transcode reasons if the container is not fully compatible.</returns>
 2281        private TranscodeReason GetCompatibilityContainer(MediaOptions options, MediaSourceInfo mediaSource, string cont
 2282        {
 2542283            var profile = options.Profile;
 2284
 2542285            var failures = AggregateFailureConditions(
 2542286                mediaSource,
 2542287                profile,
 2542288                "VideoCodecProfile",
 2542289                profile.ContainerProfiles
 2542290                    .Where(containerProfile => containerProfile.Type == DlnaProfileType.Video && containerProfile.Contai
 2542291                    .SelectMany(containerProfile => CheckVideoConditions(containerProfile.Conditions, mediaSource, video
 2292
 2542293            return failures;
 2294        }
 2295
 2296        /// <summary>
 2297        /// Check the compatibility of the video codec.
 2298        /// </summary>
 2299        /// <param name="options">Media options.</param>
 2300        /// <param name="mediaSource">Media source.</param>
 2301        /// <param name="container">Container.</param>
 2302        /// <param name="videoStream">Video stream.</param>
 2303        /// <returns>Transcode reasons if the video stream is not fully compatible.</returns>
 2304        private TranscodeReason GetCompatibilityVideoCodec(MediaOptions options, MediaSourceInfo mediaSource, string con
 2305        {
 4022306            var profile = options.Profile;
 2307
 4022308            string videoCodec = videoStream.Codec;
 2309
 4022310            var failures = AggregateFailureConditions(
 4022311                mediaSource,
 4022312                profile,
 4022313                "VideoCodecProfile",
 4022314                profile.CodecProfiles
 4022315                    .Where(codecProfile => codecProfile.Type == CodecType.Video &&
 4022316                        codecProfile.ContainsAnyCodec(videoCodec, container) &&
 4022317                        !CheckVideoConditions(codecProfile.ApplyConditions, mediaSource, videoStream).Any())
 4022318                    .SelectMany(codecProfile => CheckVideoConditions(codecProfile.Conditions, mediaSource, videoStream))
 2319
 4022320            return failures;
 2321        }
 2322
 2323        /// <summary>
 2324        /// Check the compatibility of the audio codec.
 2325        /// </summary>
 2326        /// <param name="options">Media options.</param>
 2327        /// <param name="mediaSource">Media source.</param>
 2328        /// <param name="container">Container.</param>
 2329        /// <param name="audioStream">Audio stream.</param>
 2330        /// <param name="transcodingAudioCodec">Override audio codec.</param>
 2331        /// <param name="isVideo">The media source is video.</param>
 2332        /// <param name="isSecondaryAudio">The audio stream is secondary.</param>
 2333        /// <returns>Transcode reasons if the audio stream is not fully compatible.</returns>
 2334        private TranscodeReason GetCompatibilityAudioCodec(MediaOptions options, MediaSourceInfo mediaSource, string con
 2335        {
 10052336            var profile = options.Profile;
 2337
 10052338            var audioCodec = transcodingAudioCodec ?? audioStream.Codec;
 10052339            var audioProfile = audioStream.Profile;
 10052340            var audioChannels = audioStream.Channels;
 10052341            var audioBitrate = audioStream.BitRate;
 10052342            var audioSampleRate = audioStream.SampleRate;
 10052343            var audioBitDepth = audioStream.BitDepth;
 2344
 10052345            var audioFailureConditions = isVideo
 10052346                ? GetProfileConditionsForVideoAudio(profile.CodecProfiles, container, audioCodec, audioChannels, audioBi
 10052347                : GetProfileConditionsForAudio(profile.CodecProfiles, container, audioCodec, audioChannels, audioBitrate
 2348
 10052349            var failures = AggregateFailureConditions(mediaSource, profile, "AudioCodecProfile", audioFailureConditions)
 2350
 10052351            return failures;
 2352        }
 2353
 2354        /// <summary>
 2355        /// Check the compatibility of the audio codec for direct playback.
 2356        /// </summary>
 2357        /// <param name="options">Media options.</param>
 2358        /// <param name="mediaSource">Media source.</param>
 2359        /// <param name="container">Container.</param>
 2360        /// <param name="audioStream">Audio stream.</param>
 2361        /// <param name="isVideo">The media source is video.</param>
 2362        /// <param name="isSecondaryAudio">The audio stream is secondary.</param>
 2363        /// <returns>Transcode reasons if the audio stream is not fully compatible for direct playback.</returns>
 2364        private TranscodeReason GetCompatibilityAudioCodecDirect(MediaOptions options, MediaSourceInfo mediaSource, stri
 2365        {
 2732366            var failures = GetCompatibilityAudioCodec(options, mediaSource, container, audioStream, null, isVideo, isSec
 2367
 2732368            if (audioStream.IsExternal)
 2369            {
 62370                failures |= TranscodeReason.AudioIsExternal;
 2371            }
 2372
 2732373            return failures;
 2374        }
 2375    }
 2376}

Methods/Properties

.cctor()
.ctor(MediaBrowser.Model.Dlna.ITranscoderSupport,Microsoft.Extensions.Logging.ILogger)
GetOptimalAudioStream(MediaBrowser.Model.Dlna.MediaOptions)
GetOptimalAudioStream(MediaBrowser.Model.Dto.MediaSourceInfo,MediaBrowser.Model.Dlna.MediaOptions)
GetOptimalVideoStream(MediaBrowser.Model.Dlna.MediaOptions)
GetOptimalStream(System.Collections.Generic.List`1<MediaBrowser.Model.Dlna.StreamInfo>,System.Int64)
SortMediaSources(System.Collections.Generic.List`1<MediaBrowser.Model.Dlna.StreamInfo>,System.Int64)
GetTranscodeReasonForFailedCondition(MediaBrowser.Model.Dlna.ProfileCondition)
NormalizeMediaSourceFormatIntoSingleContainer(System.String,MediaBrowser.Model.Dlna.DeviceProfile,MediaBrowser.Model.Dlna.DlnaProfileType,MediaBrowser.Model.Dlna.DirectPlayProfile)
GetAudioDirectPlayProfile(MediaBrowser.Model.Dto.MediaSourceInfo,MediaBrowser.Model.Entities.MediaStream,MediaBrowser.Model.Dlna.MediaOptions)
GetTranscodeReasonsFromDirectPlayProfile(MediaBrowser.Model.Dto.MediaSourceInfo,MediaBrowser.Model.Entities.MediaStream,MediaBrowser.Model.Entities.MediaStream,System.Collections.Generic.IEnumerable`1<MediaBrowser.Model.Dlna.DirectPlayProfile>)
GetDefaultSubtitleStreamIndex(MediaBrowser.Model.Dto.MediaSourceInfo,MediaBrowser.Model.Dlna.SubtitleProfile[])
SetStreamInfoOptionsFromTranscodingProfile(MediaBrowser.Model.Dto.MediaSourceInfo,MediaBrowser.Model.Dlna.StreamInfo,MediaBrowser.Model.Dlna.TranscodingProfile)
SetStreamInfoOptionsFromDirectPlayProfile(MediaBrowser.Model.Dlna.MediaOptions,MediaBrowser.Model.Dto.MediaSourceInfo,MediaBrowser.Model.Dlna.StreamInfo,MediaBrowser.Model.Dlna.DirectPlayProfile)
BuildVideoItem(MediaBrowser.Model.Dto.MediaSourceInfo,MediaBrowser.Model.Dlna.MediaOptions)
GetVideoTranscodeProfile(MediaBrowser.Model.Dto.MediaSourceInfo,MediaBrowser.Model.Dlna.MediaOptions,MediaBrowser.Model.Entities.MediaStream,MediaBrowser.Model.Entities.MediaStream,MediaBrowser.Model.Dlna.StreamInfo)
BuildStreamVideoItem(MediaBrowser.Model.Dlna.StreamInfo,MediaBrowser.Model.Dlna.MediaOptions,MediaBrowser.Model.Dto.MediaSourceInfo,MediaBrowser.Model.Entities.MediaStream,MediaBrowser.Model.Entities.MediaStream,System.Collections.Generic.IEnumerable`1<MediaBrowser.Model.Entities.MediaStream>,System.String,System.String,System.String)
GetDefaultAudioBitrate(System.String,System.Nullable`1<System.Int32>)
GetAudioBitrate(System.Int64,System.Collections.Generic.IReadOnlyList`1<System.String>,MediaBrowser.Model.Entities.MediaStream,MediaBrowser.Model.Dlna.StreamInfo)
GetMaxAudioBitrateForTotalBitrate(System.Int64)
GetVideoDirectPlayProfile(MediaBrowser.Model.Dlna.MediaOptions,MediaBrowser.Model.Dto.MediaSourceInfo,MediaBrowser.Model.Entities.MediaStream,MediaBrowser.Model.Entities.MediaStream,System.Collections.Generic.ICollection`1<MediaBrowser.Model.Entities.MediaStream>,MediaBrowser.Model.Entities.MediaStream,System.Boolean,System.Boolean)
AggregateFailureConditions(MediaBrowser.Model.Dto.MediaSourceInfo,MediaBrowser.Model.Dlna.DeviceProfile,System.String,System.Collections.Generic.IEnumerable`1<MediaBrowser.Model.Dlna.ProfileCondition>)
LogConditionFailure(MediaBrowser.Model.Dlna.DeviceProfile,System.String,MediaBrowser.Model.Dlna.ProfileCondition,MediaBrowser.Model.Dto.MediaSourceInfo)
GetSubtitleProfile(MediaBrowser.Model.Dto.MediaSourceInfo,MediaBrowser.Model.Entities.MediaStream,MediaBrowser.Model.Dlna.SubtitleProfile[],MediaBrowser.Model.Session.PlayMethod,MediaBrowser.Model.Dlna.ITranscoderSupport,System.String,System.Nullable`1<Jellyfin.Data.Enums.MediaStreamProtocol>)
IsSubtitleEmbedSupported(System.String)
GetExternalSubtitleProfile(MediaBrowser.Model.Dto.MediaSourceInfo,MediaBrowser.Model.Entities.MediaStream,MediaBrowser.Model.Dlna.SubtitleProfile[],MediaBrowser.Model.Session.PlayMethod,MediaBrowser.Model.Dlna.ITranscoderSupport,System.Boolean)
IsBitrateLimitExceeded(MediaBrowser.Model.Dto.MediaSourceInfo,System.Int64)
ValidateMediaOptions(MediaBrowser.Model.Dlna.MediaOptions,System.Boolean)
GetProfileConditionsForVideoAudio(System.Collections.Generic.IEnumerable`1<MediaBrowser.Model.Dlna.CodecProfile>,System.String,System.String,System.Nullable`1<System.Int32>,System.Nullable`1<System.Int32>,System.Nullable`1<System.Int32>,System.Nullable`1<System.Int32>,System.String,System.Nullable`1<System.Boolean>)
GetProfileConditionsForAudio(System.Collections.Generic.IEnumerable`1<MediaBrowser.Model.Dlna.CodecProfile>,System.String,System.String,System.Nullable`1<System.Int32>,System.Nullable`1<System.Int32>,System.Nullable`1<System.Int32>,System.Nullable`1<System.Int32>,System.Boolean)
ApplyTranscodingConditions(MediaBrowser.Model.Dlna.StreamInfo,System.Collections.Generic.IEnumerable`1<MediaBrowser.Model.Dlna.ProfileCondition>,System.String,System.Boolean,System.Boolean)
IsAudioContainerSupported(MediaBrowser.Model.Dlna.DirectPlayProfile,MediaBrowser.Model.Dto.MediaSourceInfo)
IsAudioDirectPlaySupported(MediaBrowser.Model.Dlna.DirectPlayProfile,MediaBrowser.Model.Dto.MediaSourceInfo,MediaBrowser.Model.Entities.MediaStream)
IsAudioDirectStreamSupported(MediaBrowser.Model.Dlna.DirectPlayProfile,MediaBrowser.Model.Dto.MediaSourceInfo,MediaBrowser.Model.Entities.MediaStream)
GetRank(MediaBrowser.Model.Session.TranscodeReason&,MediaBrowser.Model.Session.TranscodeReason[])
CheckVideoConditions(MediaBrowser.Model.Dlna.ProfileCondition[],MediaBrowser.Model.Dto.MediaSourceInfo,MediaBrowser.Model.Entities.MediaStream)
GetCompatibilityContainer(MediaBrowser.Model.Dlna.MediaOptions,MediaBrowser.Model.Dto.MediaSourceInfo,System.String,MediaBrowser.Model.Entities.MediaStream)
GetCompatibilityVideoCodec(MediaBrowser.Model.Dlna.MediaOptions,MediaBrowser.Model.Dto.MediaSourceInfo,System.String,MediaBrowser.Model.Entities.MediaStream)
GetCompatibilityAudioCodec(MediaBrowser.Model.Dlna.MediaOptions,MediaBrowser.Model.Dto.MediaSourceInfo,System.String,MediaBrowser.Model.Entities.MediaStream,System.String,System.Boolean,System.Boolean)
GetCompatibilityAudioCodecDirect(MediaBrowser.Model.Dlna.MediaOptions,MediaBrowser.Model.Dto.MediaSourceInfo,System.String,MediaBrowser.Model.Entities.MediaStream,System.Boolean,System.Boolean)