jellyfin/Emby.Server.Implementations/Intros/DefaultIntroProvider.cs

385 lines
13 KiB
C#
Raw Normal View History

2014-09-22 23:56:54 +02:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Security;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
2014-09-23 02:04:50 +02:00
using System.IO;
2014-09-22 23:56:54 +02:00
using System.Linq;
using System.Threading.Tasks;
2016-10-25 21:02:04 +02:00
using MediaBrowser.Model.IO;
2016-10-22 04:08:34 +02:00
using MediaBrowser.Model.Extensions;
2016-10-24 04:45:23 +02:00
using MediaBrowser.Model.Globalization;
2014-09-22 23:56:54 +02:00
namespace Emby.Server.Implementations.Intros
2014-09-22 23:56:54 +02:00
{
public class DefaultIntroProvider : IIntroProvider
{
private readonly ISecurityManager _security;
private readonly ILocalizationManager _localization;
private readonly IConfigurationManager _serverConfig;
2014-11-16 21:44:08 +01:00
private readonly ILibraryManager _libraryManager;
2015-09-24 19:50:49 +02:00
private readonly IFileSystem _fileSystem;
2016-02-05 06:59:09 +01:00
private readonly IMediaSourceManager _mediaSourceManager;
2014-09-22 23:56:54 +02:00
2016-03-19 20:32:37 +01:00
public DefaultIntroProvider(ISecurityManager security, ILocalizationManager localization, IConfigurationManager serverConfig, ILibraryManager libraryManager, IFileSystem fileSystem, IMediaSourceManager mediaSourceManager)
2014-09-22 23:56:54 +02:00
{
_security = security;
_localization = localization;
_serverConfig = serverConfig;
2014-11-16 21:44:08 +01:00
_libraryManager = libraryManager;
2015-09-24 19:50:49 +02:00
_fileSystem = fileSystem;
2016-02-05 06:59:09 +01:00
_mediaSourceManager = mediaSourceManager;
2014-09-22 23:56:54 +02:00
}
public async Task<IEnumerable<IntroInfo>> GetIntros(BaseItem item, User user)
{
var config = GetOptions();
if (item is Movie)
{
if (!config.EnableIntrosForMovies)
{
return new List<IntroInfo>();
}
}
else if (item is Episode)
{
if (!config.EnableIntrosForEpisodes)
{
return new List<IntroInfo>();
}
}
else
{
return new List<IntroInfo>();
}
var ratingLevel = string.IsNullOrWhiteSpace(item.OfficialRating)
2014-12-03 04:13:03 +01:00
? null
2014-09-22 23:56:54 +02:00
: _localization.GetRatingLevel(item.OfficialRating);
var candidates = new List<ItemWithTrailer>();
2014-09-30 06:47:30 +02:00
var trailerTypes = new List<TrailerType>();
2016-10-16 19:11:32 +02:00
var sourceTypes = new List<SourceType>();
2014-09-30 06:47:30 +02:00
2016-03-19 20:32:37 +01:00
if (config.EnableIntrosFromMoviesInLibrary)
2014-09-30 06:47:30 +02:00
{
2016-03-19 20:32:37 +01:00
trailerTypes.Add(TrailerType.LocalTrailer);
2016-10-16 19:11:32 +02:00
sourceTypes.Add(SourceType.Library);
2014-09-30 06:47:30 +02:00
}
2016-03-19 20:32:37 +01:00
if (IsSupporter)
2014-10-09 01:31:44 +02:00
{
2016-03-19 20:32:37 +01:00
if (config.EnableIntrosFromUpcomingTrailers)
{
trailerTypes.Add(TrailerType.ComingSoonToTheaters);
2016-10-16 19:11:32 +02:00
sourceTypes.Clear();
2016-03-19 20:32:37 +01:00
}
if (config.EnableIntrosFromUpcomingDvdMovies)
{
trailerTypes.Add(TrailerType.ComingSoonToDvd);
2016-10-16 19:11:32 +02:00
sourceTypes.Clear();
2016-03-19 20:32:37 +01:00
}
if (config.EnableIntrosFromUpcomingStreamingMovies)
{
trailerTypes.Add(TrailerType.ComingSoonToStreaming);
2016-10-16 19:11:32 +02:00
sourceTypes.Clear();
2016-03-19 20:32:37 +01:00
}
if (config.EnableIntrosFromSimilarMovies)
{
trailerTypes.Add(TrailerType.Archive);
2016-10-16 19:11:32 +02:00
sourceTypes.Clear();
2016-03-19 20:32:37 +01:00
}
2014-10-09 01:31:44 +02:00
}
2014-09-30 06:47:30 +02:00
2016-03-19 20:32:37 +01:00
if (trailerTypes.Count > 0)
2014-09-22 23:56:54 +02:00
{
2016-03-20 07:46:51 +01:00
var trailerResult = _libraryManager.GetItemList(new InternalItemsQuery
2014-09-22 23:56:54 +02:00
{
2016-03-19 20:32:37 +01:00
IncludeItemTypes = new[] { typeof(Trailer).Name },
2016-06-01 08:20:21 +02:00
TrailerTypes = trailerTypes.ToArray(),
SimilarTo = item,
2016-09-17 08:09:02 +02:00
IsPlayed = config.EnableIntrosForWatchedContent ? (bool?)null : false,
2016-06-01 08:20:21 +02:00
MaxParentalRating = config.EnableIntrosParentalControl ? ratingLevel : null,
2016-09-17 08:09:02 +02:00
BlockUnratedItems = config.EnableIntrosParentalControl ? new[] { UnratedItem.Trailer } : new UnratedItem[] { },
2016-10-21 20:41:26 +02:00
// Account for duplicates by imdb id, since the database doesn't support this yet
Limit = config.TrailerLimit * 2,
2016-10-16 19:11:32 +02:00
SourceTypes = sourceTypes.ToArray()
2016-10-21 20:41:26 +02:00
}).Where(i => string.IsNullOrWhiteSpace(i.GetProviderId(MetadataProviders.Imdb)) || !string.Equals(i.GetProviderId(MetadataProviders.Imdb), item.GetProviderId(MetadataProviders.Imdb), StringComparison.OrdinalIgnoreCase)).Take(config.TrailerLimit);
2014-09-22 23:56:54 +02:00
2016-03-20 07:46:51 +01:00
candidates.AddRange(trailerResult.Select(i => new ItemWithTrailer
2014-09-22 23:56:54 +02:00
{
Item = i,
2016-03-19 20:32:37 +01:00
Type = i.SourceType == SourceType.Channel ? ItemWithTrailerType.ChannelTrailer : ItemWithTrailerType.ItemWithTrailer,
2015-06-21 05:35:22 +02:00
LibraryManager = _libraryManager
2014-09-22 23:56:54 +02:00
}));
2016-09-17 08:09:02 +02:00
}
2014-09-22 23:56:54 +02:00
2016-06-01 08:20:21 +02:00
return GetResult(item, candidates, config);
2015-01-25 07:34:50 +01:00
}
2016-06-01 08:20:21 +02:00
private IEnumerable<IntroInfo> GetResult(BaseItem item, IEnumerable<ItemWithTrailer> candidates, CinemaModeConfiguration config)
2015-01-25 07:34:50 +01:00
{
2014-10-02 02:28:16 +02:00
var customIntros = !string.IsNullOrWhiteSpace(config.CustomIntroPath) ?
2015-11-17 18:46:15 +01:00
GetCustomIntros(config) :
2014-09-22 23:56:54 +02:00
new List<IntroInfo>();
2016-02-05 06:59:09 +01:00
var mediaInfoIntros = !string.IsNullOrWhiteSpace(config.MediaInfoIntroPath) ?
GetMediaInfoIntros(config, item) :
new List<IntroInfo>();
2014-09-22 23:56:54 +02:00
// Avoid implicitly captured closure
2016-06-01 08:20:21 +02:00
return candidates.Select(i => i.IntroInfo)
2016-02-05 06:59:09 +01:00
.Concat(customIntros.Take(1))
.Concat(mediaInfoIntros);
2014-09-22 23:56:54 +02:00
}
private CinemaModeConfiguration GetOptions()
{
return _serverConfig.GetConfiguration<CinemaModeConfiguration>("cinemamode");
}
2015-11-17 18:46:15 +01:00
private List<IntroInfo> GetCustomIntros(CinemaModeConfiguration options)
2014-09-22 23:56:54 +02:00
{
2014-09-23 02:04:50 +02:00
try
{
2015-11-17 18:46:15 +01:00
return GetCustomIntroFiles(options, true, false)
2014-09-23 02:04:50 +02:00
.OrderBy(i => Guid.NewGuid())
.Select(i => new IntroInfo
{
Path = i
}).ToList();
}
catch (IOException)
{
return new List<IntroInfo>();
}
}
2016-02-05 06:59:09 +01:00
private IEnumerable<IntroInfo> GetMediaInfoIntros(CinemaModeConfiguration options, BaseItem item)
{
try
{
var hasMediaSources = item as IHasMediaSources;
if (hasMediaSources == null)
{
return new List<IntroInfo>();
}
var mediaSource = _mediaSourceManager.GetStaticMediaSources(hasMediaSources, false)
.FirstOrDefault();
if (mediaSource == null)
{
return new List<IntroInfo>();
}
var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
var audioStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);
var allIntros = GetCustomIntroFiles(options, false, true)
.OrderBy(i => Guid.NewGuid())
.Select(i => new IntroInfo
{
Path = i
}).ToList();
var returnResult = new List<IntroInfo>();
if (videoStream != null)
{
returnResult.AddRange(GetMediaInfoIntrosByVideoStream(allIntros, videoStream).Take(1));
}
if (audioStream != null)
{
returnResult.AddRange(GetMediaInfoIntrosByAudioStream(allIntros, audioStream).Take(1));
}
returnResult.AddRange(GetMediaInfoIntrosByTags(allIntros, item.Tags).Take(1));
2016-09-17 08:09:02 +02:00
2016-02-05 06:59:09 +01:00
return returnResult.DistinctBy(i => i.Path, StringComparer.OrdinalIgnoreCase);
}
catch (IOException)
{
return new List<IntroInfo>();
}
}
private IEnumerable<IntroInfo> GetMediaInfoIntrosByVideoStream(List<IntroInfo> allIntros, MediaStream stream)
{
var codec = stream.Codec;
if (string.IsNullOrWhiteSpace(codec))
{
return new List<IntroInfo>();
}
return allIntros
2016-08-29 09:12:34 +02:00
.Where(i => IsMatch(i.Path, codec))
.OrderBy(i => Guid.NewGuid());
2016-02-05 06:59:09 +01:00
}
private IEnumerable<IntroInfo> GetMediaInfoIntrosByAudioStream(List<IntroInfo> allIntros, MediaStream stream)
{
var codec = stream.Codec;
if (string.IsNullOrWhiteSpace(codec))
{
return new List<IntroInfo>();
}
return allIntros
2016-08-29 09:12:34 +02:00
.Where(i => IsAudioMatch(i.Path, stream))
.OrderBy(i => Guid.NewGuid());
2016-02-05 06:59:09 +01:00
}
private IEnumerable<IntroInfo> GetMediaInfoIntrosByTags(List<IntroInfo> allIntros, List<string> tags)
{
return allIntros
2016-08-29 09:12:34 +02:00
.Where(i => tags.Any(t => IsMatch(i.Path, t)))
.OrderBy(i => Guid.NewGuid());
2016-02-05 06:59:09 +01:00
}
private bool IsMatch(string file, string attribute)
{
var filename = Path.GetFileNameWithoutExtension(file) ?? string.Empty;
filename = Normalize(filename);
if (string.IsNullOrWhiteSpace(filename))
{
return false;
}
attribute = Normalize(attribute);
if (string.IsNullOrWhiteSpace(attribute))
{
return false;
}
return string.Equals(filename, attribute, StringComparison.OrdinalIgnoreCase);
}
private string Normalize(string value)
{
return value;
}
private bool IsAudioMatch(string path, MediaStream stream)
{
if (!string.IsNullOrWhiteSpace(stream.Codec))
{
if (IsMatch(path, stream.Codec))
{
return true;
}
}
if (!string.IsNullOrWhiteSpace(stream.Profile))
{
if (IsMatch(path, stream.Profile))
{
return true;
}
}
return false;
}
2015-11-17 18:46:15 +01:00
private IEnumerable<string> GetCustomIntroFiles(CinemaModeConfiguration options, bool enableCustomIntros, bool enableMediaInfoIntros)
2014-09-23 02:04:50 +02:00
{
2015-11-17 17:37:16 +01:00
var list = new List<string>();
2015-11-17 18:46:15 +01:00
if (enableCustomIntros && !string.IsNullOrWhiteSpace(options.CustomIntroPath))
2014-09-23 02:04:50 +02:00
{
2015-11-17 17:37:16 +01:00
list.AddRange(_fileSystem.GetFilePaths(options.CustomIntroPath, true)
.Where(_libraryManager.IsVideoFile));
2014-09-23 02:04:50 +02:00
}
2015-11-17 18:46:15 +01:00
if (enableMediaInfoIntros && !string.IsNullOrWhiteSpace(options.MediaInfoIntroPath))
2015-11-17 17:37:16 +01:00
{
2015-11-17 18:41:58 +01:00
list.AddRange(_fileSystem.GetFilePaths(options.MediaInfoIntroPath, true)
2015-11-17 17:37:16 +01:00
.Where(_libraryManager.IsVideoFile));
}
2015-11-17 18:46:15 +01:00
2015-11-17 17:37:16 +01:00
return list.Distinct(StringComparer.OrdinalIgnoreCase);
2014-09-22 23:56:54 +02:00
}
public IEnumerable<string> GetAllIntroFiles()
{
2015-11-17 18:46:15 +01:00
return GetCustomIntroFiles(GetOptions(), true, true);
2014-09-22 23:56:54 +02:00
}
private bool IsSupporter
{
get { return _security.IsMBSupporter; }
}
public string Name
{
get { return "Default"; }
}
internal class ItemWithTrailer
{
internal BaseItem Item;
internal ItemWithTrailerType Type;
2015-06-21 05:35:22 +02:00
internal ILibraryManager LibraryManager;
2014-09-22 23:56:54 +02:00
public IntroInfo IntroInfo
{
get
{
var id = Item.Id;
if (Type == ItemWithTrailerType.ItemWithTrailer)
{
var hasTrailers = Item as IHasTrailers;
if (hasTrailers != null)
{
id = hasTrailers.LocalTrailerIds.FirstOrDefault();
}
}
return new IntroInfo
{
ItemId = id
};
}
}
}
internal enum ItemWithTrailerType
{
ChannelTrailer,
ItemWithTrailer
}
}
public class CinemaModeConfigurationFactory : IConfigurationFactory
{
public IEnumerable<ConfigurationStore> GetConfigurations()
{
return new[]
{
new ConfigurationStore
{
ConfigurationType = typeof(CinemaModeConfiguration),
Key = "cinemamode"
}
};
}
}
}