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

222 lines
8.1 KiB
C#
Raw Normal View History

2019-11-01 10:38:54 -07:00
#pragma warning disable CS1591
using System;
2014-05-12 11:04:42 -07:00
using System.Collections.Generic;
using System.Linq;
2020-05-12 19:10:35 -07:00
using Jellyfin.Data.Enums;
using MediaBrowser.Model.Entities;
2014-05-12 11:04:42 -07:00
2018-09-12 10:26:21 -07:00
namespace Emby.Server.Implementations.Library
2014-05-12 11:04:42 -07:00
{
public static class MediaStreamSelector
{
2018-09-12 10:26:21 -07:00
public static int? GetDefaultAudioStreamIndex(List<MediaStream> streams, string[] preferredLanguages, bool preferDefaultTrack)
2014-05-12 11:04:42 -07:00
{
2018-09-12 10:26:21 -07:00
streams = GetSortedStreams(streams, MediaStreamType.Audio, preferredLanguages)
2014-05-12 11:08:07 -07:00
.ToList();
2014-05-12 11:04:42 -07:00
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 null;
}
2020-04-19 06:18:28 -07:00
public static int? GetDefaultSubtitleStreamIndex(
List<MediaStream> streams,
2018-09-12 10:26:21 -07:00
string[] preferredLanguages,
2014-05-12 11:04:42 -07:00
SubtitlePlaybackMode mode,
string audioTrackLanguage)
{
streams = GetSortedStreams(streams, MediaStreamType.Subtitle, preferredLanguages)
.ToList();
2014-05-12 11:04:42 -07:00
MediaStream stream = null;
if (mode == SubtitlePlaybackMode.None)
2014-05-12 11:04:42 -07:00
{
return null;
2014-05-12 11:04:42 -07:00
}
if (mode == SubtitlePlaybackMode.Default)
2014-05-12 11:04:42 -07:00
{
2015-12-26 01:07:24 -07:00
// Prefer embedded metadata over smart logic
stream = streams.FirstOrDefault(s => s.IsForced && string.Equals(s.Language, audioTrackLanguage, StringComparison.OrdinalIgnoreCase)) ??
streams.FirstOrDefault(s => s.IsForced) ??
streams.FirstOrDefault(s => s.IsDefault);
// if the audio language is not understood by the user, load their preferred subs, if there are any
2018-09-12 10:26:21 -07:00
if (stream == null && !preferredLanguages.Contains(audioTrackLanguage, StringComparer.OrdinalIgnoreCase))
2015-12-26 01:07:24 -07:00
{
2018-09-12 10:26:21 -07:00
stream = streams.Where(s => !s.IsForced).FirstOrDefault(s => preferredLanguages.Contains(s.Language, StringComparer.OrdinalIgnoreCase));
2015-12-26 01:07:24 -07:00
}
}
else if (mode == SubtitlePlaybackMode.Smart)
{
// Prefer smart logic over embedded metadata
// if the audio language is not understood by the user, load their preferred subs, if there are any
2018-09-12 10:26:21 -07:00
if (!preferredLanguages.Contains(audioTrackLanguage, StringComparer.OrdinalIgnoreCase))
{
2018-09-12 10:26:21 -07:00
stream = streams.Where(s => !s.IsForced).FirstOrDefault(s => preferredLanguages.Contains(s.Language, StringComparer.OrdinalIgnoreCase)) ??
streams.FirstOrDefault(s => preferredLanguages.Contains(s.Language, StringComparer.OrdinalIgnoreCase));
}
2014-05-12 11:04:42 -07:00
}
else if (mode == SubtitlePlaybackMode.Always)
2014-05-12 11:04:42 -07:00
{
// always load the most suitable full subtitles
2015-03-31 09:24:16 -07:00
stream = streams.FirstOrDefault(s => !s.IsForced);
2014-05-12 11:04:42 -07:00
}
2015-12-26 01:07:24 -07:00
else if (mode == SubtitlePlaybackMode.OnlyForced)
{
// always load the most suitable full subtitles
stream = streams.FirstOrDefault(s => s.IsForced && string.Equals(s.Language, audioTrackLanguage, StringComparison.OrdinalIgnoreCase)) ??
streams.FirstOrDefault(s => s.IsForced);
}
// load forced subs if we have found no suitable full subtitles
stream = stream ?? streams.FirstOrDefault(s => s.IsForced && string.Equals(s.Language, audioTrackLanguage, StringComparison.OrdinalIgnoreCase));
2014-05-12 11:04:42 -07:00
if (stream != null)
{
return stream.Index;
}
return null;
}
2014-05-12 11:08:07 -07:00
2018-09-12 10:26:21 -07:00
private static IEnumerable<MediaStream> GetSortedStreams(IEnumerable<MediaStream> streams, MediaStreamType type, string[] languagePreferences)
2014-05-12 11:04:42 -07:00
{
// Give some preferance to external text subs for better performance
return streams.Where(i => i.Type == type)
.OrderBy(i =>
2014-06-10 10:36:06 -07:00
{
2018-09-12 10:26:21 -07:00
var index = FindIndex(languagePreferences, i.Language);
2014-06-10 10:36:06 -07:00
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 11:04:42 -07:00
}
2020-04-19 06:18:28 -07:00
public static void SetSubtitleStreamScores(
List<MediaStream> streams,
2018-09-12 10:26:21 -07:00
string[] preferredLanguages,
2015-03-31 09:24:16 -07:00
SubtitlePlaybackMode mode,
string audioTrackLanguage)
{
if (mode == SubtitlePlaybackMode.None)
{
return;
}
streams = GetSortedStreams(streams, MediaStreamType.Subtitle, preferredLanguages)
.ToList();
var filteredStreams = new List<MediaStream>();
if (mode == SubtitlePlaybackMode.Default)
{
2015-12-26 01:07:24 -07:00
// Prefer embedded metadata over smart logic
filteredStreams = streams.Where(s => s.IsForced || s.IsDefault)
.ToList();
}
else if (mode == SubtitlePlaybackMode.Smart)
{
// Prefer smart logic over embedded metadata
2018-09-12 10:26:21 -07:00
if (!preferredLanguages.Contains(audioTrackLanguage, StringComparer.OrdinalIgnoreCase))
2015-03-31 09:24:16 -07:00
{
2018-09-12 10:26:21 -07:00
filteredStreams = streams.Where(s => !s.IsForced && preferredLanguages.Contains(s.Language, StringComparer.OrdinalIgnoreCase))
2015-03-31 09:24:16 -07:00
.ToList();
}
}
else if (mode == SubtitlePlaybackMode.Always)
{
// always load the most suitable full subtitles
filteredStreams = streams.Where(s => !s.IsForced)
.ToList();
}
2015-12-26 01:07:24 -07:00
else if (mode == SubtitlePlaybackMode.OnlyForced)
{
// always load the most suitable full subtitles
filteredStreams = streams.Where(s => s.IsForced).ToList();
}
2015-03-31 09:24:16 -07:00
// 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();
}
foreach (var stream in filteredStreams)
{
stream.Score = GetSubtitleScore(stream, preferredLanguages);
}
}
2018-09-12 10:26:21 -07:00
private static int FindIndex(string[] list, string value)
{
for (var i = 0; i < list.Length; i++)
2018-09-12 10:26:21 -07:00
{
if (string.Equals(list[i], value, StringComparison.OrdinalIgnoreCase))
{
return i;
}
}
return -1;
}
private static int GetSubtitleScore(MediaStream stream, string[] languagePreferences)
2015-03-31 09:24:16 -07:00
{
var values = new List<int>();
2018-09-12 10:26:21 -07:00
var index = FindIndex(languagePreferences, stream.Language);
2015-03-31 09:24:16 -07:00
values.Add(index == -1 ? 0 : 100 - index);
2015-12-26 01:07:24 -07:00
values.Add(stream.IsForced ? 1 : 0);
2015-03-31 09:24:16 -07: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;
}
private static int GetBooleanOrderBy(bool value)
{
return value ? 0 : 1;
}
2014-05-12 11:04:42 -07:00
}
}