|
|
|
@ -1,5 +1,3 @@
|
|
|
|
|
#nullable disable
|
|
|
|
|
|
|
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
@ -13,39 +11,24 @@ namespace Emby.Server.Implementations.Library
|
|
|
|
|
{
|
|
|
|
|
public static class MediaStreamSelector
|
|
|
|
|
{
|
|
|
|
|
public static int? GetDefaultAudioStreamIndex(List<MediaStream> streams, string[] preferredLanguages, bool preferDefaultTrack)
|
|
|
|
|
public static int? GetDefaultAudioStreamIndex(IReadOnlyList<MediaStream> streams, IReadOnlyList<string> preferredLanguages, bool preferDefaultTrack)
|
|
|
|
|
{
|
|
|
|
|
streams = GetSortedStreams(streams, MediaStreamType.Audio, preferredLanguages)
|
|
|
|
|
.ToList();
|
|
|
|
|
var sortedStreams = GetSortedStreams(streams, MediaStreamType.Audio, preferredLanguages);
|
|
|
|
|
|
|
|
|
|
if (preferDefaultTrack)
|
|
|
|
|
{
|
|
|
|
|
var defaultStream = streams.FirstOrDefault(i => i.IsDefault);
|
|
|
|
|
|
|
|
|
|
if (defaultStream != null)
|
|
|
|
|
{
|
|
|
|
|
return defaultStream.Index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var stream = streams.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (stream != null)
|
|
|
|
|
{
|
|
|
|
|
return stream.Index;
|
|
|
|
|
return sortedStreams.FirstOrDefault(i => i.IsDefault)?.Index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
return sortedStreams.FirstOrDefault()?.Index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int? GetDefaultSubtitleStreamIndex(
|
|
|
|
|
IEnumerable<MediaStream> streams,
|
|
|
|
|
string[] preferredLanguages,
|
|
|
|
|
IReadOnlyList<string> preferredLanguages,
|
|
|
|
|
SubtitlePlaybackMode mode,
|
|
|
|
|
string audioTrackLanguage)
|
|
|
|
|
{
|
|
|
|
|
MediaStream stream = null;
|
|
|
|
|
|
|
|
|
|
if (mode == SubtitlePlaybackMode.None)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
@ -59,6 +42,7 @@ namespace Emby.Server.Implementations.Library
|
|
|
|
|
.ThenByDescending(x => x.IsDefault)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
MediaStream? stream = null;
|
|
|
|
|
if (mode == SubtitlePlaybackMode.Default)
|
|
|
|
|
{
|
|
|
|
|
// Prefer embedded metadata over smart logic
|
|
|
|
@ -95,13 +79,14 @@ namespace Emby.Server.Implementations.Library
|
|
|
|
|
return stream?.Index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<MediaStream> GetSortedStreams(IEnumerable<MediaStream> streams, MediaStreamType type, string[] languagePreferences)
|
|
|
|
|
private static IEnumerable<MediaStream> GetSortedStreams(IEnumerable<MediaStream> streams, MediaStreamType type, IReadOnlyList<string> languagePreferences)
|
|
|
|
|
{
|
|
|
|
|
// Give some preference to external text subs for better performance
|
|
|
|
|
return streams.Where(i => i.Type == type)
|
|
|
|
|
return streams
|
|
|
|
|
.Where(i => i.Type == type)
|
|
|
|
|
.OrderBy(i =>
|
|
|
|
|
{
|
|
|
|
|
var index = FindIndex(languagePreferences, i.Language);
|
|
|
|
|
var index = languagePreferences.FindIndex(x => string.Equals(x, i.Language, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
return index == -1 ? 100 : index;
|
|
|
|
|
})
|
|
|
|
@ -113,8 +98,8 @@ namespace Emby.Server.Implementations.Library
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetSubtitleStreamScores(
|
|
|
|
|
List<MediaStream> streams,
|
|
|
|
|
string[] preferredLanguages,
|
|
|
|
|
IReadOnlyList<MediaStream> streams,
|
|
|
|
|
IReadOnlyList<string> preferredLanguages,
|
|
|
|
|
SubtitlePlaybackMode mode,
|
|
|
|
|
string audioTrackLanguage)
|
|
|
|
|
{
|
|
|
|
@ -123,15 +108,14 @@ namespace Emby.Server.Implementations.Library
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
streams = GetSortedStreams(streams, MediaStreamType.Subtitle, preferredLanguages)
|
|
|
|
|
.ToList();
|
|
|
|
|
var sortedStreams = GetSortedStreams(streams, MediaStreamType.Subtitle, preferredLanguages);
|
|
|
|
|
|
|
|
|
|
var filteredStreams = new List<MediaStream>();
|
|
|
|
|
|
|
|
|
|
if (mode == SubtitlePlaybackMode.Default)
|
|
|
|
|
{
|
|
|
|
|
// Prefer embedded metadata over smart logic
|
|
|
|
|
filteredStreams = streams.Where(s => s.IsForced || s.IsDefault)
|
|
|
|
|
filteredStreams = sortedStreams.Where(s => s.IsForced || s.IsDefault)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else if (mode == SubtitlePlaybackMode.Smart)
|
|
|
|
@ -139,54 +123,37 @@ namespace Emby.Server.Implementations.Library
|
|
|
|
|
// Prefer smart logic over embedded metadata
|
|
|
|
|
if (!preferredLanguages.Contains(audioTrackLanguage, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
filteredStreams = streams.Where(s => !s.IsForced && preferredLanguages.Contains(s.Language, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
filteredStreams = sortedStreams.Where(s => !s.IsForced && preferredLanguages.Contains(s.Language, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (mode == SubtitlePlaybackMode.Always)
|
|
|
|
|
{
|
|
|
|
|
// always load the most suitable full subtitles
|
|
|
|
|
filteredStreams = streams.Where(s => !s.IsForced)
|
|
|
|
|
.ToList();
|
|
|
|
|
filteredStreams = sortedStreams.Where(s => !s.IsForced).ToList();
|
|
|
|
|
}
|
|
|
|
|
else if (mode == SubtitlePlaybackMode.OnlyForced)
|
|
|
|
|
{
|
|
|
|
|
// always load the most suitable full subtitles
|
|
|
|
|
filteredStreams = streams.Where(s => s.IsForced).ToList();
|
|
|
|
|
filteredStreams = sortedStreams.Where(s => s.IsForced).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// load forced subs if we have found no suitable full subtitles
|
|
|
|
|
if (filteredStreams.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
filteredStreams = streams
|
|
|
|
|
.Where(s => s.IsForced && string.Equals(s.Language, audioTrackLanguage, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
var iterStreams = filteredStreams.Count == 0
|
|
|
|
|
? sortedStreams.Where(s => s.IsForced && string.Equals(s.Language, audioTrackLanguage, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
: filteredStreams;
|
|
|
|
|
|
|
|
|
|
foreach (var stream in filteredStreams)
|
|
|
|
|
foreach (var stream in iterStreams)
|
|
|
|
|
{
|
|
|
|
|
stream.Score = GetSubtitleScore(stream, preferredLanguages);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int FindIndex(string[] list, string value)
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < list.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (string.Equals(list[i], value, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int GetSubtitleScore(MediaStream stream, string[] languagePreferences)
|
|
|
|
|
private static int GetSubtitleScore(MediaStream stream, IReadOnlyList<string> languagePreferences)
|
|
|
|
|
{
|
|
|
|
|
var values = new List<int>();
|
|
|
|
|
|
|
|
|
|
var index = FindIndex(languagePreferences, stream.Language);
|
|
|
|
|
var index = languagePreferences.FindIndex(x => string.Equals(x, stream.Language, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
values.Add(index == -1 ? 0 : 100 - index);
|
|
|
|
|
|
|
|
|
|