2019-11-01 13:38:54 -04:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 14:54:44 -05:00
|
|
|
using System;
|
2014-05-12 14:04:42 -04:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-05-12 22:10:35 -04:00
|
|
|
using Jellyfin.Data.Enums;
|
2021-12-20 07:31:07 -05:00
|
|
|
using Jellyfin.Extensions;
|
2019-01-13 14:21:32 -05:00
|
|
|
using MediaBrowser.Model.Entities;
|
2014-05-12 14:04:42 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
namespace Emby.Server.Implementations.Library
|
2014-05-12 14:04:42 -04:00
|
|
|
{
|
|
|
|
public static class MediaStreamSelector
|
|
|
|
{
|
2022-01-22 09:40:05 -05:00
|
|
|
public static int? GetDefaultAudioStreamIndex(IReadOnlyList<MediaStream> streams, IReadOnlyList<string> preferredLanguages, bool preferDefaultTrack)
|
2014-05-12 14:04:42 -04:00
|
|
|
{
|
2022-01-22 09:40:05 -05:00
|
|
|
var sortedStreams = GetSortedStreams(streams, MediaStreamType.Audio, preferredLanguages);
|
2014-05-12 14:04:42 -04:00
|
|
|
|
|
|
|
if (preferDefaultTrack)
|
|
|
|
{
|
2022-02-14 09:03:08 -05:00
|
|
|
var defaultStream = streams.FirstOrDefault(i => i.IsDefault);
|
|
|
|
|
|
|
|
if (defaultStream != null)
|
|
|
|
{
|
|
|
|
return defaultStream.Index;
|
|
|
|
}
|
2014-05-12 14:04:42 -04:00
|
|
|
}
|
|
|
|
|
2022-01-22 09:40:05 -05:00
|
|
|
return sortedStreams.FirstOrDefault()?.Index;
|
2014-05-12 14:04:42 -04:00
|
|
|
}
|
|
|
|
|
2020-04-19 09:18:28 -04:00
|
|
|
public static int? GetDefaultSubtitleStreamIndex(
|
2021-10-26 06:59:11 -04:00
|
|
|
IEnumerable<MediaStream> streams,
|
2022-01-22 09:40:05 -05:00
|
|
|
IReadOnlyList<string> preferredLanguages,
|
2014-05-12 14:04:42 -04:00
|
|
|
SubtitlePlaybackMode mode,
|
|
|
|
string audioTrackLanguage)
|
|
|
|
{
|
2014-05-12 15:09:43 -04:00
|
|
|
if (mode == SubtitlePlaybackMode.None)
|
2014-05-12 14:04:42 -04:00
|
|
|
{
|
2014-05-12 15:09:43 -04:00
|
|
|
return null;
|
2014-05-12 14:04:42 -04:00
|
|
|
}
|
2014-05-12 15:09:43 -04:00
|
|
|
|
2021-10-26 14:51:35 -04:00
|
|
|
var sortedStreams = streams
|
|
|
|
.Where(i => i.Type == MediaStreamType.Subtitle)
|
2021-10-26 06:59:11 -04:00
|
|
|
.OrderByDescending(x => x.IsExternal)
|
|
|
|
.ThenByDescending(x => x.IsForced && string.Equals(x.Language, audioTrackLanguage, StringComparison.OrdinalIgnoreCase))
|
|
|
|
.ThenByDescending(x => x.IsForced)
|
|
|
|
.ThenByDescending(x => x.IsDefault)
|
|
|
|
.ToList();
|
|
|
|
|
2022-01-22 09:40:05 -05:00
|
|
|
MediaStream? stream = null;
|
2014-05-12 15:09:43 -04:00
|
|
|
if (mode == SubtitlePlaybackMode.Default)
|
2014-05-12 14:04:42 -04:00
|
|
|
{
|
2015-12-26 03:07:24 -05:00
|
|
|
// Prefer embedded metadata over smart logic
|
2021-10-26 06:59:11 -04:00
|
|
|
stream = sortedStreams.FirstOrDefault(s => s.IsExternal || s.IsForced || s.IsDefault);
|
2015-12-26 03:07:24 -05:00
|
|
|
|
|
|
|
// if the audio language is not understood by the user, load their preferred subs, if there are any
|
2021-12-20 07:31:07 -05:00
|
|
|
if (stream == null && !preferredLanguages.Contains(audioTrackLanguage, StringComparison.OrdinalIgnoreCase))
|
2015-12-26 03:07:24 -05:00
|
|
|
{
|
2021-12-20 07:31:07 -05:00
|
|
|
stream = sortedStreams.FirstOrDefault(s => !s.IsForced && preferredLanguages.Contains(s.Language, StringComparison.OrdinalIgnoreCase));
|
2015-12-26 03:07:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (mode == SubtitlePlaybackMode.Smart)
|
|
|
|
{
|
2014-05-12 15:09:43 -04:00
|
|
|
// if the audio language is not understood by the user, load their preferred subs, if there are any
|
2021-12-20 07:31:07 -05:00
|
|
|
if (!preferredLanguages.Contains(audioTrackLanguage, StringComparison.OrdinalIgnoreCase))
|
2014-05-12 15:09:43 -04:00
|
|
|
{
|
2021-12-20 07:31:07 -05:00
|
|
|
stream = streams.FirstOrDefault(s => !s.IsForced && preferredLanguages.Contains(s.Language, StringComparison.OrdinalIgnoreCase)) ??
|
|
|
|
streams.FirstOrDefault(s => preferredLanguages.Contains(s.Language, StringComparison.OrdinalIgnoreCase));
|
2014-05-12 15:09:43 -04:00
|
|
|
}
|
2014-05-12 14:04:42 -04:00
|
|
|
}
|
2014-05-12 15:09:43 -04:00
|
|
|
else if (mode == SubtitlePlaybackMode.Always)
|
2014-05-12 14:04:42 -04:00
|
|
|
{
|
2014-05-12 15:09:43 -04:00
|
|
|
// always load the most suitable full subtitles
|
2021-10-26 06:59:11 -04:00
|
|
|
stream = sortedStreams.FirstOrDefault(s => !s.IsForced);
|
2014-05-12 14:04:42 -04:00
|
|
|
}
|
2015-12-26 03:07:24 -05:00
|
|
|
else if (mode == SubtitlePlaybackMode.OnlyForced)
|
|
|
|
{
|
|
|
|
// always load the most suitable full subtitles
|
2021-10-26 06:59:11 -04:00
|
|
|
stream = sortedStreams.FirstOrDefault(x => x.IsForced);
|
2015-12-26 03:07:24 -05:00
|
|
|
}
|
2014-05-23 19:58:28 -04:00
|
|
|
|
2014-05-12 15:09:43 -04:00
|
|
|
// load forced subs if we have found no suitable full subtitles
|
2021-10-26 06:59:11 -04:00
|
|
|
stream ??= sortedStreams.FirstOrDefault(s => s.IsForced && string.Equals(s.Language, audioTrackLanguage, StringComparison.OrdinalIgnoreCase));
|
|
|
|
return stream?.Index;
|
2014-05-12 14:04:42 -04:00
|
|
|
}
|
2014-05-12 14:08:07 -04:00
|
|
|
|
2022-01-22 09:40:05 -05:00
|
|
|
private static IEnumerable<MediaStream> GetSortedStreams(IEnumerable<MediaStream> streams, MediaStreamType type, IReadOnlyList<string> languagePreferences)
|
2014-05-12 14:04:42 -04:00
|
|
|
{
|
2020-11-18 08:23:45 -05:00
|
|
|
// Give some preference to external text subs for better performance
|
2022-01-22 09:40:05 -05:00
|
|
|
return streams
|
|
|
|
.Where(i => i.Type == type)
|
2015-01-12 22:46:44 -05:00
|
|
|
.OrderBy(i =>
|
2022-01-22 09:40:05 -05:00
|
|
|
{
|
|
|
|
var index = languagePreferences.FindIndex(x => string.Equals(x, i.Language, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
return index == -1 ? 100 : index;
|
|
|
|
})
|
|
|
|
.ThenBy(i => GetBooleanOrderBy(i.IsDefault))
|
|
|
|
.ThenBy(i => GetBooleanOrderBy(i.SupportsExternalStream))
|
|
|
|
.ThenBy(i => GetBooleanOrderBy(i.IsTextSubtitleStream))
|
|
|
|
.ThenBy(i => GetBooleanOrderBy(i.IsExternal))
|
|
|
|
.ThenBy(i => i.Index);
|
2014-05-12 14:04:42 -04:00
|
|
|
}
|
2015-02-07 16:03:09 -05:00
|
|
|
|
2020-04-19 09:18:28 -04:00
|
|
|
public static void SetSubtitleStreamScores(
|
2022-01-22 09:40:05 -05:00
|
|
|
IReadOnlyList<MediaStream> streams,
|
|
|
|
IReadOnlyList<string> preferredLanguages,
|
2015-03-31 12:24:16 -04:00
|
|
|
SubtitlePlaybackMode mode,
|
|
|
|
string audioTrackLanguage)
|
|
|
|
{
|
|
|
|
if (mode == SubtitlePlaybackMode.None)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-22 09:40:05 -05:00
|
|
|
var sortedStreams = GetSortedStreams(streams, MediaStreamType.Subtitle, preferredLanguages);
|
2015-03-31 12:24:16 -04:00
|
|
|
|
|
|
|
var filteredStreams = new List<MediaStream>();
|
|
|
|
|
|
|
|
if (mode == SubtitlePlaybackMode.Default)
|
|
|
|
{
|
2015-12-26 03:07:24 -05:00
|
|
|
// Prefer embedded metadata over smart logic
|
2022-01-22 09:40:05 -05:00
|
|
|
filteredStreams = sortedStreams.Where(s => s.IsForced || s.IsDefault)
|
2015-12-26 03:07:24 -05:00
|
|
|
.ToList();
|
|
|
|
}
|
|
|
|
else if (mode == SubtitlePlaybackMode.Smart)
|
|
|
|
{
|
|
|
|
// Prefer smart logic over embedded metadata
|
2021-12-20 07:31:07 -05:00
|
|
|
if (!preferredLanguages.Contains(audioTrackLanguage, StringComparison.OrdinalIgnoreCase))
|
2015-03-31 12:24:16 -04:00
|
|
|
{
|
2022-01-22 09:40:05 -05:00
|
|
|
filteredStreams = sortedStreams.Where(s => !s.IsForced && preferredLanguages.Contains(s.Language, StringComparison.OrdinalIgnoreCase))
|
2015-03-31 12:24:16 -04:00
|
|
|
.ToList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (mode == SubtitlePlaybackMode.Always)
|
|
|
|
{
|
|
|
|
// always load the most suitable full subtitles
|
2022-01-22 09:40:05 -05:00
|
|
|
filteredStreams = sortedStreams.Where(s => !s.IsForced).ToList();
|
2015-03-31 12:24:16 -04:00
|
|
|
}
|
2015-12-26 03:07:24 -05:00
|
|
|
else if (mode == SubtitlePlaybackMode.OnlyForced)
|
|
|
|
{
|
|
|
|
// always load the most suitable full subtitles
|
2022-01-22 09:40:05 -05:00
|
|
|
filteredStreams = sortedStreams.Where(s => s.IsForced).ToList();
|
2015-12-26 03:07:24 -05:00
|
|
|
}
|
2015-03-31 12:24:16 -04:00
|
|
|
|
|
|
|
// load forced subs if we have found no suitable full subtitles
|
2022-01-22 09:40:05 -05:00
|
|
|
var iterStreams = filteredStreams.Count == 0
|
|
|
|
? sortedStreams.Where(s => s.IsForced && string.Equals(s.Language, audioTrackLanguage, StringComparison.OrdinalIgnoreCase))
|
|
|
|
: filteredStreams;
|
2015-03-31 12:24:16 -04:00
|
|
|
|
2022-01-22 09:40:05 -05:00
|
|
|
foreach (var stream in iterStreams)
|
2015-03-31 12:24:16 -04:00
|
|
|
{
|
|
|
|
stream.Score = GetSubtitleScore(stream, preferredLanguages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 09:40:05 -05:00
|
|
|
private static int GetSubtitleScore(MediaStream stream, IReadOnlyList<string> languagePreferences)
|
2015-03-31 12:24:16 -04:00
|
|
|
{
|
|
|
|
var values = new List<int>();
|
|
|
|
|
2022-01-22 09:40:05 -05:00
|
|
|
var index = languagePreferences.FindIndex(x => string.Equals(x, stream.Language, StringComparison.OrdinalIgnoreCase));
|
2015-03-31 12:24:16 -04:00
|
|
|
|
|
|
|
values.Add(index == -1 ? 0 : 100 - index);
|
|
|
|
|
2015-12-26 03:07:24 -05:00
|
|
|
values.Add(stream.IsForced ? 1 : 0);
|
2015-03-31 12:24:16 -04:00
|
|
|
values.Add(stream.IsDefault ? 1 : 0);
|
|
|
|
values.Add(stream.SupportsExternalStream ? 1 : 0);
|
|
|
|
values.Add(stream.IsTextSubtitleStream ? 1 : 0);
|
|
|
|
values.Add(stream.IsExternal ? 1 : 0);
|
|
|
|
|
|
|
|
values.Reverse();
|
|
|
|
var scale = 1;
|
|
|
|
var score = 0;
|
|
|
|
|
|
|
|
foreach (var value in values)
|
|
|
|
{
|
|
|
|
score += scale * (value + 1);
|
|
|
|
scale *= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
2015-02-07 16:03:09 -05:00
|
|
|
private static int GetBooleanOrderBy(bool value)
|
|
|
|
{
|
|
|
|
return value ? 0 : 1;
|
|
|
|
}
|
2014-05-12 14:04:42 -04:00
|
|
|
}
|
|
|
|
}
|