jellyfin-server/MediaBrowser.Server.Implementations/Dto/MediaStreamSelector.cs

104 lines
3.5 KiB
C#
Raw Normal View History

2014-05-12 18:04:42 +00:00
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
2014-05-12 22:30:32 +00:00
namespace MediaBrowser.Server.Implementations.Dto
2014-05-12 18:04:42 +00:00
{
public static class MediaStreamSelector
{
2014-05-12 18:08:07 +00:00
public static int? GetDefaultAudioStreamIndex(List<MediaStream> streams, IEnumerable<string> preferredLanguages, bool preferDefaultTrack)
2014-05-12 18:04:42 +00:00
{
streams = GetSortedStreams(streams, MediaStreamType.Audio, preferredLanguages.ToList())
2014-05-12 18:08:07 +00:00
.ToList();
2014-05-12 18:04:42 +00: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;
}
2014-05-12 18:08:07 +00:00
public static int? GetDefaultSubtitleStreamIndex(List<MediaStream> streams,
IEnumerable<string> preferredLanguages,
2014-05-12 18:04:42 +00:00
SubtitlePlaybackMode mode,
string audioTrackLanguage)
{
2014-06-10 17:36:06 +00:00
var languages = preferredLanguages.ToList();
streams = GetSortedStreams(streams, MediaStreamType.Subtitle, languages).ToList();
var full = streams.Where(s => !s.IsForced);
var forced = streams.Where(s => s.IsForced && string.Equals(s.Language, audioTrackLanguage, StringComparison.OrdinalIgnoreCase));
2014-05-12 18:04:42 +00:00
MediaStream stream = null;
if (mode == SubtitlePlaybackMode.None)
2014-05-12 18:04:42 +00:00
{
return null;
2014-05-12 18:04:42 +00:00
}
if (mode == SubtitlePlaybackMode.Default)
2014-05-12 18:04:42 +00:00
{
// if the audio language is not understood by the user, load their preferred subs, if there are any
if (!ContainsOrdinal(languages, audioTrackLanguage))
{
stream = full.FirstOrDefault(s => ContainsOrdinal(languages, s.Language));
}
2014-05-12 18:04:42 +00:00
}
else if (mode == SubtitlePlaybackMode.Always)
2014-05-12 18:04:42 +00:00
{
// always load the most suitable full subtitles
stream = full.FirstOrDefault();
2014-05-12 18:04:42 +00:00
}
// load forced subs if we have found no suitable full subtitles
stream = stream ?? forced.FirstOrDefault();
2014-05-12 18:04:42 +00:00
if (stream != null)
{
return stream.Index;
}
return null;
}
2014-05-12 18:08:07 +00:00
private static bool ContainsOrdinal(IEnumerable<string> list, string item)
{
return list.Any(i => string.Equals(i, item, StringComparison.OrdinalIgnoreCase));
}
private static IEnumerable<MediaStream> GetSortedStreams(IEnumerable<MediaStream> streams, MediaStreamType type, List<string> languagePreferences)
2014-05-12 18:04:42 +00:00
{
var orderStreams = streams
.Where(i => i.Type == type);
// Give some preferance to external text subs for better performance
2014-06-10 17:36:06 +00:00
return orderStreams.OrderBy(i =>
{
var index = languagePreferences.FindIndex(l => string.Equals(i.Language, l, StringComparison.OrdinalIgnoreCase));
return index == -1 ? 100 : index;
})
.ThenBy(i => i.IsDefault)
2014-06-11 20:57:18 +00:00
.ThenBy(i => i.IsTextSubtitleStream)
.ThenBy(i => i.IsExternal)
.ThenBy(i => i.Index)
.ToList();
2014-05-12 18:04:42 +00:00
}
}
}