2014-05-12 14:04:42 -04:00
|
|
|
|
using MediaBrowser.Model.Configuration;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2014-06-14 14:24:20 -04:00
|
|
|
|
namespace MediaBrowser.Controller.MediaEncoding
|
2014-05-12 14:04:42 -04:00
|
|
|
|
{
|
|
|
|
|
public static class MediaStreamSelector
|
|
|
|
|
{
|
2014-05-12 14:08:07 -04:00
|
|
|
|
public static int? GetDefaultAudioStreamIndex(List<MediaStream> streams, IEnumerable<string> preferredLanguages, bool preferDefaultTrack)
|
2014-05-12 14:04:42 -04:00
|
|
|
|
{
|
2014-05-12 15:09:43 -04:00
|
|
|
|
streams = GetSortedStreams(streams, MediaStreamType.Audio, preferredLanguages.ToList())
|
2014-05-12 14:08:07 -04:00
|
|
|
|
.ToList();
|
2014-05-12 14:04:42 -04: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 14:08:07 -04:00
|
|
|
|
public static int? GetDefaultSubtitleStreamIndex(List<MediaStream> streams,
|
2014-12-27 13:06:32 -05:00
|
|
|
|
List<string> preferredLanguages,
|
2014-05-12 14:04:42 -04:00
|
|
|
|
SubtitlePlaybackMode mode,
|
|
|
|
|
string audioTrackLanguage)
|
|
|
|
|
{
|
2015-01-12 22:46:44 -05:00
|
|
|
|
streams = GetSortedStreams(streams, MediaStreamType.Subtitle, preferredLanguages)
|
|
|
|
|
.ToList();
|
2014-05-12 15:09:43 -04:00
|
|
|
|
|
2014-05-12 14:04:42 -04:00
|
|
|
|
MediaStream stream = null;
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
if (mode == SubtitlePlaybackMode.Default)
|
2014-05-12 14:04:42 -04:00
|
|
|
|
{
|
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
|
2014-12-27 13:06:32 -05:00
|
|
|
|
if (!ContainsOrdinal(preferredLanguages, audioTrackLanguage))
|
2014-05-12 15:09:43 -04:00
|
|
|
|
{
|
2015-03-31 12:24:16 -04:00
|
|
|
|
stream = streams.Where(s => !s.IsForced).FirstOrDefault(s => ContainsOrdinal(preferredLanguages, s.Language));
|
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
|
2015-03-31 12:24:16 -04:00
|
|
|
|
stream = streams.FirstOrDefault(s => !s.IsForced);
|
2014-05-12 14:04:42 -04: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
|
2014-12-27 13:06:32 -05:00
|
|
|
|
stream = stream ?? streams.FirstOrDefault(s => s.IsForced && string.Equals(s.Language, audioTrackLanguage, StringComparison.OrdinalIgnoreCase));
|
2014-05-12 14:04:42 -04:00
|
|
|
|
|
|
|
|
|
if (stream != null)
|
|
|
|
|
{
|
|
|
|
|
return stream.Index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-05-12 14:08:07 -04:00
|
|
|
|
|
2014-05-12 15:09:43 -04: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 14:04:42 -04:00
|
|
|
|
{
|
2014-05-23 19:58:28 -04:00
|
|
|
|
// Give some preferance to external text subs for better performance
|
2015-01-12 22:46:44 -05:00
|
|
|
|
return streams.Where(i => i.Type == type)
|
|
|
|
|
.OrderBy(i =>
|
2014-06-10 13:36:06 -04:00
|
|
|
|
{
|
|
|
|
|
var index = languagePreferences.FindIndex(l => string.Equals(i.Language, l, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
return index == -1 ? 100 : index;
|
|
|
|
|
})
|
2015-02-07 16:03:09 -05:00
|
|
|
|
.ThenBy(i => GetBooleanOrderBy(i.IsDefault))
|
|
|
|
|
.ThenBy(i => GetBooleanOrderBy(i.SupportsExternalStream))
|
|
|
|
|
.ThenBy(i => GetBooleanOrderBy(i.IsTextSubtitleStream))
|
|
|
|
|
.ThenBy(i => GetBooleanOrderBy(i.IsExternal))
|
2015-01-12 22:46:44 -05:00
|
|
|
|
.ThenBy(i => i.Index);
|
2014-05-12 14:04:42 -04:00
|
|
|
|
}
|
2015-02-07 16:03:09 -05:00
|
|
|
|
|
2015-03-31 12:24:16 -04:00
|
|
|
|
public static void SetSubtitleStreamScores(List<MediaStream> streams,
|
|
|
|
|
List<string> preferredLanguages,
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
// if the audio language is not understood by the user, load their preferred subs, if there are any
|
|
|
|
|
if (!ContainsOrdinal(preferredLanguages, audioTrackLanguage))
|
|
|
|
|
{
|
|
|
|
|
filteredStreams = streams.Where(s => !s.IsForced && ContainsOrdinal(preferredLanguages, s.Language))
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (mode == SubtitlePlaybackMode.Always)
|
|
|
|
|
{
|
|
|
|
|
// always load the most suitable full subtitles
|
|
|
|
|
filteredStreams = streams.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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var stream in filteredStreams)
|
|
|
|
|
{
|
|
|
|
|
stream.Score = GetSubtitleScore(stream, preferredLanguages);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int GetSubtitleScore(MediaStream stream, List<string> languagePreferences)
|
|
|
|
|
{
|
|
|
|
|
var values = new List<int>();
|
|
|
|
|
|
|
|
|
|
var index = languagePreferences.FindIndex(l => string.Equals(stream.Language, l, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
values.Add(index == -1 ? 0 : 100 - index);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|