jellyfin-server/Emby.Server.Implementations/Library/MediaStreamSelector.cs

160 lines
6.9 KiB
C#
Raw Normal View History

2019-11-01 17:38:54 +00:00
#pragma warning disable CS1591
using System;
2014-05-12 18:04:42 +00:00
using System.Collections.Generic;
using System.Linq;
2020-05-13 02:10:35 +00:00
using Jellyfin.Data.Enums;
2021-12-20 12:31:07 +00:00
using Jellyfin.Extensions;
using MediaBrowser.Model.Entities;
2014-05-12 18:04:42 +00:00
2018-09-12 17:26:21 +00:00
namespace Emby.Server.Implementations.Library
2014-05-12 18:04:42 +00:00
{
public static class MediaStreamSelector
{
2022-01-22 14:40:05 +00:00
public static int? GetDefaultAudioStreamIndex(IReadOnlyList<MediaStream> streams, IReadOnlyList<string> preferredLanguages, bool preferDefaultTrack)
2014-05-12 18:04:42 +00:00
{
var sortedStreams = GetSortedStreams(streams, MediaStreamType.Audio, preferredLanguages).ToList();
2014-05-12 18:04:42 +00:00
if (preferDefaultTrack)
{
var defaultStream = sortedStreams.FirstOrDefault(i => i.IsDefault);
2022-02-14 14:03:08 +00:00
2022-12-05 14:01:13 +00:00
if (defaultStream is not null)
2022-02-14 14:03:08 +00:00
{
return defaultStream.Index;
}
2014-05-12 18:04:42 +00:00
}
2022-01-22 14:40:05 +00:00
return sortedStreams.FirstOrDefault()?.Index;
2014-05-12 18:04:42 +00:00
}
2020-04-19 13:18:28 +00:00
public static int? GetDefaultSubtitleStreamIndex(
IEnumerable<MediaStream> streams,
2022-01-22 14:40:05 +00:00
IReadOnlyList<string> preferredLanguages,
2014-05-12 18:04:42 +00:00
SubtitlePlaybackMode mode,
string audioTrackLanguage)
{
if (mode == SubtitlePlaybackMode.None)
2014-05-12 18:04:42 +00:00
{
return null;
2014-05-12 18:04:42 +00:00
}
var sortedStreams = streams
.Where(i => i.Type == MediaStreamType.Subtitle)
.OrderByDescending(x => x.IsExternal)
.ThenByDescending(x => x.IsForced && string.Equals(x.Language, audioTrackLanguage, StringComparison.OrdinalIgnoreCase))
.ThenByDescending(x => x.IsForced)
.ThenByDescending(x => x.IsDefault)
2022-09-28 21:26:42 +00:00
.ThenByDescending(x => preferredLanguages.Contains(x.Language, StringComparison.OrdinalIgnoreCase))
.ToList();
2022-01-22 14:40:05 +00:00
MediaStream? stream = null;
if (mode == SubtitlePlaybackMode.Default)
2014-05-12 18:04:42 +00:00
{
2022-09-28 21:26:42 +00:00
// Load subtitles according to external, forced and default flags.
stream = sortedStreams.FirstOrDefault(x => x.IsExternal || x.IsForced || x.IsDefault);
2015-12-26 08:07:24 +00:00
}
else if (mode == SubtitlePlaybackMode.Smart)
{
2022-09-28 21:26:42 +00:00
// Only attempt to load subtitles if the audio language is not one of the user's preferred subtitle languages.
// If no subtitles of preferred language available, use default behaviour.
2021-12-20 12:31:07 +00:00
if (!preferredLanguages.Contains(audioTrackLanguage, StringComparison.OrdinalIgnoreCase))
{
2022-09-28 21:26:42 +00:00
stream = sortedStreams.FirstOrDefault(x => preferredLanguages.Contains(x.Language, StringComparison.OrdinalIgnoreCase)) ??
sortedStreams.FirstOrDefault(x => x.IsExternal || x.IsForced || x.IsDefault);
}
2022-10-07 13:17:20 +00:00
else
{
// Respect forced flag.
stream = sortedStreams.FirstOrDefault(x => x.IsForced);
}
2014-05-12 18:04:42 +00:00
}
else if (mode == SubtitlePlaybackMode.Always)
2014-05-12 18:04:42 +00:00
{
// Always load (full/non-forced) subtitles of the user's preferred subtitle language if possible, otherwise default behaviour.
stream = sortedStreams.FirstOrDefault(x => !x.IsForced && preferredLanguages.Contains(x.Language, StringComparison.OrdinalIgnoreCase)) ??
2022-09-28 21:26:42 +00:00
sortedStreams.FirstOrDefault(x => x.IsExternal || x.IsForced || x.IsDefault);
2014-05-12 18:04:42 +00:00
}
2015-12-26 08:07:24 +00:00
else if (mode == SubtitlePlaybackMode.OnlyForced)
{
2022-09-28 21:26:42 +00:00
// Only load subtitles that are flagged forced.
stream = sortedStreams.FirstOrDefault(x => x.IsForced);
2015-12-26 08:07:24 +00:00
}
return stream?.Index;
2014-05-12 18:04:42 +00:00
}
2014-05-12 18:08:07 +00:00
2022-01-22 14:40:05 +00:00
private static IEnumerable<MediaStream> GetSortedStreams(IEnumerable<MediaStream> streams, MediaStreamType type, IReadOnlyList<string> languagePreferences)
2014-05-12 18:04:42 +00:00
{
2020-11-18 13:23:45 +00:00
// Give some preference to external text subs for better performance
2022-01-22 14:40:05 +00:00
return streams
2022-02-14 15:31:07 +00:00
.Where(i => i.Type == type)
.OrderByDescending(i => GetStreamScore(i, languagePreferences));
2014-05-12 18:04:42 +00:00
}
2020-04-19 13:18:28 +00:00
public static void SetSubtitleStreamScores(
2022-01-22 14:40:05 +00:00
IReadOnlyList<MediaStream> streams,
IReadOnlyList<string> preferredLanguages,
2015-03-31 16:24:16 +00:00
SubtitlePlaybackMode mode,
string audioTrackLanguage)
{
if (mode == SubtitlePlaybackMode.None)
{
return;
}
var sortedStreams = GetSortedStreams(streams, MediaStreamType.Subtitle, preferredLanguages).ToList();
2015-03-31 16:24:16 +00:00
List<MediaStream>? filteredStreams = null;
2015-03-31 16:24:16 +00:00
if (mode == SubtitlePlaybackMode.Default)
{
2015-12-26 08:07:24 +00:00
// Prefer embedded metadata over smart logic
2022-01-22 14:40:05 +00:00
filteredStreams = sortedStreams.Where(s => s.IsForced || s.IsDefault)
2015-12-26 08:07:24 +00:00
.ToList();
}
else if (mode == SubtitlePlaybackMode.Smart)
{
// Prefer smart logic over embedded metadata
2021-12-20 12:31:07 +00:00
if (!preferredLanguages.Contains(audioTrackLanguage, StringComparison.OrdinalIgnoreCase))
2015-03-31 16:24:16 +00:00
{
2022-01-22 14:40:05 +00:00
filteredStreams = sortedStreams.Where(s => !s.IsForced && preferredLanguages.Contains(s.Language, StringComparison.OrdinalIgnoreCase))
2015-03-31 16:24:16 +00:00
.ToList();
}
}
else if (mode == SubtitlePlaybackMode.Always)
{
// always load the most suitable full subtitles
2022-01-22 14:40:05 +00:00
filteredStreams = sortedStreams.Where(s => !s.IsForced).ToList();
2015-03-31 16:24:16 +00:00
}
2015-12-26 08:07:24 +00:00
else if (mode == SubtitlePlaybackMode.OnlyForced)
{
// always load the most suitable full subtitles
2022-01-22 14:40:05 +00:00
filteredStreams = sortedStreams.Where(s => s.IsForced).ToList();
2015-12-26 08:07:24 +00:00
}
2015-03-31 16:24:16 +00:00
// load forced subs if we have found no suitable full subtitles
var iterStreams = filteredStreams is null || filteredStreams.Count == 0
2022-01-22 14:40:05 +00:00
? sortedStreams.Where(s => s.IsForced && string.Equals(s.Language, audioTrackLanguage, StringComparison.OrdinalIgnoreCase))
: filteredStreams;
2015-03-31 16:24:16 +00:00
2022-01-22 14:40:05 +00:00
foreach (var stream in iterStreams)
2015-03-31 16:24:16 +00:00
{
stream.Score = GetStreamScore(stream, preferredLanguages);
2015-03-31 16:24:16 +00:00
}
}
internal static int GetStreamScore(MediaStream stream, IReadOnlyList<string> languagePreferences)
2015-03-31 16:24:16 +00:00
{
2022-01-22 14:40:05 +00:00
var index = languagePreferences.FindIndex(x => string.Equals(x, stream.Language, StringComparison.OrdinalIgnoreCase));
var score = index == -1 ? 1 : 101 - index;
score = (score * 10) + (stream.IsForced ? 2 : 1);
score = (score * 10) + (stream.IsDefault ? 2 : 1);
score = (score * 10) + (stream.SupportsExternalStream ? 2 : 1);
score = (score * 10) + (stream.IsTextSubtitleStream ? 2 : 1);
score = (score * 10) + (stream.IsExternal ? 2 : 1);
2015-03-31 16:24:16 +00:00
return score;
}
2014-05-12 18:04:42 +00:00
}
}