| | 1 | | #pragma warning disable CS1591 |
| | 2 | |
|
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using Jellyfin.Database.Implementations.Enums; |
| | 7 | | using Jellyfin.Extensions; |
| | 8 | | using MediaBrowser.Model.Entities; |
| | 9 | |
|
| | 10 | | namespace Emby.Server.Implementations.Library |
| | 11 | | { |
| | 12 | | public static class MediaStreamSelector |
| | 13 | | { |
| | 14 | | public static int? GetDefaultAudioStreamIndex(IReadOnlyList<MediaStream> streams, IReadOnlyList<string> preferre |
| | 15 | | { |
| 9 | 16 | | var sortedStreams = GetSortedStreams(streams, MediaStreamType.Audio, preferredLanguages).ToList(); |
| | 17 | |
|
| 9 | 18 | | if (preferDefaultTrack) |
| | 19 | | { |
| 4 | 20 | | var defaultStream = sortedStreams.FirstOrDefault(i => i.IsDefault); |
| | 21 | |
|
| 4 | 22 | | if (defaultStream is not null) |
| | 23 | | { |
| 3 | 24 | | return defaultStream.Index; |
| | 25 | | } |
| | 26 | | } |
| | 27 | |
|
| 6 | 28 | | return sortedStreams.FirstOrDefault()?.Index; |
| | 29 | | } |
| | 30 | |
|
| | 31 | | public static int? GetDefaultSubtitleStreamIndex( |
| | 32 | | IEnumerable<MediaStream> streams, |
| | 33 | | IReadOnlyList<string> preferredLanguages, |
| | 34 | | SubtitlePlaybackMode mode, |
| | 35 | | string audioTrackLanguage) |
| | 36 | | { |
| 0 | 37 | | if (mode == SubtitlePlaybackMode.None) |
| | 38 | | { |
| 0 | 39 | | return null; |
| | 40 | | } |
| | 41 | |
|
| | 42 | | // Sort in the following order: Default > No tag > Forced |
| 0 | 43 | | var sortedStreams = streams |
| 0 | 44 | | .Where(i => i.Type == MediaStreamType.Subtitle) |
| 0 | 45 | | .OrderByDescending(x => x.IsExternal) |
| 0 | 46 | | .ThenByDescending(x => x.IsDefault) |
| 0 | 47 | | .ThenByDescending(x => !x.IsForced && MatchesPreferredLanguage(x.Language, preferredLanguages)) |
| 0 | 48 | | .ThenByDescending(x => x.IsForced && MatchesPreferredLanguage(x.Language, preferredLanguages)) |
| 0 | 49 | | .ThenByDescending(x => x.IsForced && IsLanguageUndefined(x.Language)) |
| 0 | 50 | | .ThenByDescending(x => x.IsForced) |
| 0 | 51 | | .ToList(); |
| | 52 | |
|
| 0 | 53 | | MediaStream? stream = null; |
| | 54 | |
|
| 0 | 55 | | if (mode == SubtitlePlaybackMode.Default) |
| | 56 | | { |
| | 57 | | // Load subtitles according to external, default and forced flags. |
| 0 | 58 | | stream = sortedStreams.FirstOrDefault(x => x.IsExternal || x.IsDefault || x.IsForced); |
| | 59 | | } |
| 0 | 60 | | else if (mode == SubtitlePlaybackMode.Smart) |
| | 61 | | { |
| | 62 | | // Only attempt to load subtitles if the audio language is not one of the user's preferred subtitle lang |
| | 63 | | // If no subtitles of preferred language available, use none. |
| | 64 | | // If the audio language is one of the user's preferred subtitle languages behave like OnlyForced. |
| 0 | 65 | | if (!preferredLanguages.Contains(audioTrackLanguage, StringComparison.OrdinalIgnoreCase)) |
| | 66 | | { |
| 0 | 67 | | stream = sortedStreams.FirstOrDefault(x => MatchesPreferredLanguage(x.Language, preferredLanguages)) |
| | 68 | | } |
| | 69 | | else |
| | 70 | | { |
| 0 | 71 | | stream = BehaviorOnlyForced(sortedStreams, preferredLanguages).FirstOrDefault(); |
| | 72 | | } |
| | 73 | | } |
| 0 | 74 | | else if (mode == SubtitlePlaybackMode.Always) |
| | 75 | | { |
| | 76 | | // Always load (full/non-forced) subtitles of the user's preferred subtitle language if possible, otherw |
| 0 | 77 | | stream = sortedStreams.FirstOrDefault(x => !x.IsForced && MatchesPreferredLanguage(x.Language, preferred |
| 0 | 78 | | BehaviorOnlyForced(sortedStreams, preferredLanguages).FirstOrDefault(); |
| | 79 | | } |
| 0 | 80 | | else if (mode == SubtitlePlaybackMode.OnlyForced) |
| | 81 | | { |
| | 82 | | // Load subtitles that are flagged forced of the user's preferred subtitle language or with an undefined |
| 0 | 83 | | stream = BehaviorOnlyForced(sortedStreams, preferredLanguages).FirstOrDefault(); |
| | 84 | | } |
| | 85 | |
|
| 0 | 86 | | return stream?.Index; |
| | 87 | | } |
| | 88 | |
|
| | 89 | | private static IEnumerable<MediaStream> GetSortedStreams(IEnumerable<MediaStream> streams, MediaStreamType type, |
| | 90 | | { |
| | 91 | | // Give some preference to external text subs for better performance |
| 9 | 92 | | return streams |
| 9 | 93 | | .Where(i => i.Type == type) |
| 9 | 94 | | .OrderByDescending(i => GetStreamScore(i, languagePreferences)); |
| | 95 | | } |
| | 96 | |
|
| | 97 | | public static void SetSubtitleStreamScores( |
| | 98 | | IReadOnlyList<MediaStream> streams, |
| | 99 | | IReadOnlyList<string> preferredLanguages, |
| | 100 | | SubtitlePlaybackMode mode, |
| | 101 | | string audioTrackLanguage) |
| | 102 | | { |
| 0 | 103 | | if (mode == SubtitlePlaybackMode.None) |
| | 104 | | { |
| 0 | 105 | | return; |
| | 106 | | } |
| | 107 | |
|
| 0 | 108 | | var sortedStreams = GetSortedStreams(streams, MediaStreamType.Subtitle, preferredLanguages).ToList(); |
| | 109 | |
|
| 0 | 110 | | List<MediaStream>? filteredStreams = null; |
| | 111 | |
|
| 0 | 112 | | if (mode == SubtitlePlaybackMode.Default) |
| | 113 | | { |
| | 114 | | // Prefer embedded metadata over smart logic |
| | 115 | | // Load subtitles according to external, default, and forced flags. |
| 0 | 116 | | filteredStreams = sortedStreams.Where(s => s.IsExternal || s.IsDefault || s.IsForced) |
| 0 | 117 | | .ToList(); |
| | 118 | | } |
| 0 | 119 | | else if (mode == SubtitlePlaybackMode.Smart) |
| | 120 | | { |
| | 121 | | // Prefer smart logic over embedded metadata |
| | 122 | | // Only attempt to load subtitles if the audio language is not one of the user's preferred subtitle lang |
| 0 | 123 | | if (!preferredLanguages.Contains(audioTrackLanguage, StringComparison.OrdinalIgnoreCase)) |
| | 124 | | { |
| 0 | 125 | | filteredStreams = sortedStreams.Where(s => MatchesPreferredLanguage(s.Language, preferredLanguages)) |
| 0 | 126 | | .ToList(); |
| | 127 | | } |
| | 128 | | else |
| | 129 | | { |
| 0 | 130 | | filteredStreams = BehaviorOnlyForced(sortedStreams, preferredLanguages); |
| | 131 | | } |
| | 132 | | } |
| 0 | 133 | | else if (mode == SubtitlePlaybackMode.Always) |
| | 134 | | { |
| | 135 | | // Always load (full/non-forced) subtitles of the user's preferred subtitle language if possible, otherw |
| 0 | 136 | | filteredStreams = sortedStreams.Where(s => !s.IsForced && MatchesPreferredLanguage(s.Language, preferred |
| 0 | 137 | | .ToList() ?? BehaviorOnlyForced(sortedStreams, preferredLanguages); |
| | 138 | | } |
| 0 | 139 | | else if (mode == SubtitlePlaybackMode.OnlyForced) |
| | 140 | | { |
| | 141 | | // Load subtitles that are flagged forced of the user's preferred subtitle language or with an undefined |
| 0 | 142 | | filteredStreams = BehaviorOnlyForced(sortedStreams, preferredLanguages); |
| | 143 | | } |
| | 144 | |
|
| | 145 | | // If filteredStreams is null, initialize it as an empty list to avoid null reference errors |
| 0 | 146 | | filteredStreams ??= new List<MediaStream>(); |
| | 147 | |
|
| 0 | 148 | | foreach (var stream in filteredStreams) |
| | 149 | | { |
| 0 | 150 | | stream.Score = GetStreamScore(stream, preferredLanguages); |
| | 151 | | } |
| 0 | 152 | | } |
| | 153 | |
|
| | 154 | | private static bool MatchesPreferredLanguage(string language, IReadOnlyList<string> preferredLanguages) |
| | 155 | | { |
| | 156 | | // If preferredLanguages is empty, treat it as "any language" (wildcard) |
| 0 | 157 | | return preferredLanguages.Count == 0 || |
| 0 | 158 | | preferredLanguages.Contains(language, StringComparison.OrdinalIgnoreCase); |
| | 159 | | } |
| | 160 | |
|
| | 161 | | private static bool IsLanguageUndefined(string language) |
| | 162 | | { |
| | 163 | | // Check for null, empty, or known placeholders |
| 0 | 164 | | return string.IsNullOrEmpty(language) || |
| 0 | 165 | | language.Equals("und", StringComparison.OrdinalIgnoreCase) || |
| 0 | 166 | | language.Equals("unknown", StringComparison.OrdinalIgnoreCase) || |
| 0 | 167 | | language.Equals("undetermined", StringComparison.OrdinalIgnoreCase) || |
| 0 | 168 | | language.Equals("mul", StringComparison.OrdinalIgnoreCase) || |
| 0 | 169 | | language.Equals("zxx", StringComparison.OrdinalIgnoreCase); |
| | 170 | | } |
| | 171 | |
|
| | 172 | | private static List<MediaStream> BehaviorOnlyForced(IEnumerable<MediaStream> sortedStreams, IReadOnlyList<string |
| | 173 | | { |
| 0 | 174 | | return sortedStreams |
| 0 | 175 | | .Where(s => s.IsForced && (MatchesPreferredLanguage(s.Language, preferredLanguages) || IsLanguageUndefin |
| 0 | 176 | | .OrderByDescending(s => MatchesPreferredLanguage(s.Language, preferredLanguages)) |
| 0 | 177 | | .ThenByDescending(s => IsLanguageUndefined(s.Language)) |
| 0 | 178 | | .ToList(); |
| | 179 | | } |
| | 180 | |
|
| | 181 | | internal static int GetStreamScore(MediaStream stream, IReadOnlyList<string> languagePreferences) |
| | 182 | | { |
| 22 | 183 | | var index = languagePreferences.FindIndex(x => string.Equals(x, stream.Language, StringComparison.OrdinalIgn |
| 22 | 184 | | var score = index == -1 ? 1 : 101 - index; |
| 22 | 185 | | score = (score * 10) + (stream.IsForced ? 2 : 1); |
| 22 | 186 | | score = (score * 10) + (stream.IsDefault ? 2 : 1); |
| 22 | 187 | | score = (score * 10) + (stream.SupportsExternalStream ? 2 : 1); |
| 22 | 188 | | score = (score * 10) + (stream.IsTextSubtitleStream ? 2 : 1); |
| 22 | 189 | | score = (score * 10) + (stream.IsExternal ? 2 : 1); |
| 22 | 190 | | return score; |
| | 191 | | } |
| | 192 | | } |
| | 193 | | } |