DefaultIntroProvider.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Security;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Movies;
  5. using MediaBrowser.Controller.Entities.TV;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Model.Configuration;
  8. using MediaBrowser.Model.Entities;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Threading.Tasks;
  14. using MediaBrowser.Model.IO;
  15. using MediaBrowser.Model.Extensions;
  16. using MediaBrowser.Model.Globalization;
  17. namespace Emby.Server.Implementations.Intros
  18. {
  19. public class DefaultIntroProvider : IIntroProvider
  20. {
  21. private readonly ISecurityManager _security;
  22. private readonly ILocalizationManager _localization;
  23. private readonly IConfigurationManager _serverConfig;
  24. private readonly ILibraryManager _libraryManager;
  25. private readonly IFileSystem _fileSystem;
  26. private readonly IMediaSourceManager _mediaSourceManager;
  27. public DefaultIntroProvider(ISecurityManager security, ILocalizationManager localization, IConfigurationManager serverConfig, ILibraryManager libraryManager, IFileSystem fileSystem, IMediaSourceManager mediaSourceManager)
  28. {
  29. _security = security;
  30. _localization = localization;
  31. _serverConfig = serverConfig;
  32. _libraryManager = libraryManager;
  33. _fileSystem = fileSystem;
  34. _mediaSourceManager = mediaSourceManager;
  35. }
  36. public async Task<IEnumerable<IntroInfo>> GetIntros(BaseItem item, User user)
  37. {
  38. var config = GetOptions();
  39. if (item is Movie)
  40. {
  41. if (!config.EnableIntrosForMovies)
  42. {
  43. return new List<IntroInfo>();
  44. }
  45. }
  46. else if (item is Episode)
  47. {
  48. if (!config.EnableIntrosForEpisodes)
  49. {
  50. return new List<IntroInfo>();
  51. }
  52. }
  53. else
  54. {
  55. return new List<IntroInfo>();
  56. }
  57. var ratingLevel = string.IsNullOrWhiteSpace(item.OfficialRating)
  58. ? null
  59. : _localization.GetRatingLevel(item.OfficialRating);
  60. var candidates = new List<ItemWithTrailer>();
  61. var trailerTypes = new List<TrailerType>();
  62. var sourceTypes = new List<SourceType>();
  63. if (config.EnableIntrosFromMoviesInLibrary)
  64. {
  65. trailerTypes.Add(TrailerType.LocalTrailer);
  66. sourceTypes.Add(SourceType.Library);
  67. }
  68. if (IsSupporter)
  69. {
  70. if (config.EnableIntrosFromUpcomingTrailers)
  71. {
  72. trailerTypes.Add(TrailerType.ComingSoonToTheaters);
  73. sourceTypes.Clear();
  74. }
  75. if (config.EnableIntrosFromUpcomingDvdMovies)
  76. {
  77. trailerTypes.Add(TrailerType.ComingSoonToDvd);
  78. sourceTypes.Clear();
  79. }
  80. if (config.EnableIntrosFromUpcomingStreamingMovies)
  81. {
  82. trailerTypes.Add(TrailerType.ComingSoonToStreaming);
  83. sourceTypes.Clear();
  84. }
  85. if (config.EnableIntrosFromSimilarMovies)
  86. {
  87. trailerTypes.Add(TrailerType.Archive);
  88. sourceTypes.Clear();
  89. }
  90. }
  91. if (trailerTypes.Count > 0)
  92. {
  93. if (trailerTypes.Count >= 5)
  94. {
  95. trailerTypes.Clear();
  96. }
  97. // hack - can't filter by user library because local trailers get TopParentId =null in the db.
  98. // for now we have to use a post-query filter afterwards to solve that
  99. var trailerResult = _libraryManager.GetItemList(new InternalItemsQuery
  100. {
  101. IncludeItemTypes = new[] { typeof(Trailer).Name },
  102. TrailerTypes = trailerTypes.ToArray(),
  103. SimilarTo = item,
  104. //IsPlayed = config.EnableIntrosForWatchedContent ? (bool?)null : false,
  105. MaxParentalRating = config.EnableIntrosParentalControl ? ratingLevel : null,
  106. BlockUnratedItems = config.EnableIntrosParentalControl ? new[] { UnratedItem.Trailer } : new UnratedItem[] { },
  107. // Account for duplicates by imdb id, since the database doesn't support this yet
  108. Limit = config.TrailerLimit * 4,
  109. SourceTypes = sourceTypes.ToArray(),
  110. MinSimilarityScore = 0
  111. })
  112. .Where(i => string.IsNullOrWhiteSpace(i.GetProviderId(MetadataProviders.Imdb)) || !string.Equals(i.GetProviderId(MetadataProviders.Imdb), item.GetProviderId(MetadataProviders.Imdb), StringComparison.OrdinalIgnoreCase))
  113. .Where(i => i.IsVisibleStandalone(user))
  114. .Where(i => config.EnableIntrosForWatchedContent || !i.IsPlayed(user))
  115. .Take(config.TrailerLimit);
  116. candidates.AddRange(trailerResult.Select(i => new ItemWithTrailer
  117. {
  118. Item = i,
  119. Type = i.SourceType == SourceType.Channel ? ItemWithTrailerType.ChannelTrailer : ItemWithTrailerType.ItemWithTrailer,
  120. LibraryManager = _libraryManager
  121. }));
  122. }
  123. return GetResult(item, candidates, config);
  124. }
  125. private IEnumerable<IntroInfo> GetResult(BaseItem item, IEnumerable<ItemWithTrailer> candidates, CinemaModeConfiguration config)
  126. {
  127. var customIntros = !string.IsNullOrWhiteSpace(config.CustomIntroPath) ?
  128. GetCustomIntros(config) :
  129. new List<IntroInfo>();
  130. var mediaInfoIntros = !string.IsNullOrWhiteSpace(config.MediaInfoIntroPath) ?
  131. GetMediaInfoIntros(config, item) :
  132. new List<IntroInfo>();
  133. // Avoid implicitly captured closure
  134. return candidates.Select(i => i.IntroInfo)
  135. .Concat(customIntros.Take(1))
  136. .Concat(mediaInfoIntros);
  137. }
  138. private CinemaModeConfiguration GetOptions()
  139. {
  140. return _serverConfig.GetConfiguration<CinemaModeConfiguration>("cinemamode");
  141. }
  142. private List<IntroInfo> GetCustomIntros(CinemaModeConfiguration options)
  143. {
  144. try
  145. {
  146. return GetCustomIntroFiles(options, true, false)
  147. .OrderBy(i => Guid.NewGuid())
  148. .Select(i => new IntroInfo
  149. {
  150. Path = i
  151. }).ToList();
  152. }
  153. catch (IOException)
  154. {
  155. return new List<IntroInfo>();
  156. }
  157. }
  158. private IEnumerable<IntroInfo> GetMediaInfoIntros(CinemaModeConfiguration options, BaseItem item)
  159. {
  160. try
  161. {
  162. var hasMediaSources = item as IHasMediaSources;
  163. if (hasMediaSources == null)
  164. {
  165. return new List<IntroInfo>();
  166. }
  167. var mediaSource = _mediaSourceManager.GetStaticMediaSources(hasMediaSources, false)
  168. .FirstOrDefault();
  169. if (mediaSource == null)
  170. {
  171. return new List<IntroInfo>();
  172. }
  173. var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
  174. var audioStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);
  175. var allIntros = GetCustomIntroFiles(options, false, true)
  176. .OrderBy(i => Guid.NewGuid())
  177. .Select(i => new IntroInfo
  178. {
  179. Path = i
  180. }).ToList();
  181. var returnResult = new List<IntroInfo>();
  182. if (videoStream != null)
  183. {
  184. returnResult.AddRange(GetMediaInfoIntrosByVideoStream(allIntros, videoStream).Take(1));
  185. }
  186. if (audioStream != null)
  187. {
  188. returnResult.AddRange(GetMediaInfoIntrosByAudioStream(allIntros, audioStream).Take(1));
  189. }
  190. returnResult.AddRange(GetMediaInfoIntrosByTags(allIntros, item.Tags).Take(1));
  191. return returnResult.DistinctBy(i => i.Path, StringComparer.OrdinalIgnoreCase);
  192. }
  193. catch (IOException)
  194. {
  195. return new List<IntroInfo>();
  196. }
  197. }
  198. private IEnumerable<IntroInfo> GetMediaInfoIntrosByVideoStream(List<IntroInfo> allIntros, MediaStream stream)
  199. {
  200. var codec = stream.Codec;
  201. if (string.IsNullOrWhiteSpace(codec))
  202. {
  203. return new List<IntroInfo>();
  204. }
  205. return allIntros
  206. .Where(i => IsMatch(i.Path, codec))
  207. .OrderBy(i => Guid.NewGuid());
  208. }
  209. private IEnumerable<IntroInfo> GetMediaInfoIntrosByAudioStream(List<IntroInfo> allIntros, MediaStream stream)
  210. {
  211. var codec = stream.Codec;
  212. if (string.IsNullOrWhiteSpace(codec))
  213. {
  214. return new List<IntroInfo>();
  215. }
  216. return allIntros
  217. .Where(i => IsAudioMatch(i.Path, stream))
  218. .OrderBy(i => Guid.NewGuid());
  219. }
  220. private IEnumerable<IntroInfo> GetMediaInfoIntrosByTags(List<IntroInfo> allIntros, List<string> tags)
  221. {
  222. return allIntros
  223. .Where(i => tags.Any(t => IsMatch(i.Path, t)))
  224. .OrderBy(i => Guid.NewGuid());
  225. }
  226. private bool IsMatch(string file, string attribute)
  227. {
  228. var filename = Path.GetFileNameWithoutExtension(file) ?? string.Empty;
  229. filename = Normalize(filename);
  230. if (string.IsNullOrWhiteSpace(filename))
  231. {
  232. return false;
  233. }
  234. attribute = Normalize(attribute);
  235. if (string.IsNullOrWhiteSpace(attribute))
  236. {
  237. return false;
  238. }
  239. return string.Equals(filename, attribute, StringComparison.OrdinalIgnoreCase);
  240. }
  241. private string Normalize(string value)
  242. {
  243. return value;
  244. }
  245. private bool IsAudioMatch(string path, MediaStream stream)
  246. {
  247. if (!string.IsNullOrWhiteSpace(stream.Codec))
  248. {
  249. if (IsMatch(path, stream.Codec))
  250. {
  251. return true;
  252. }
  253. }
  254. if (!string.IsNullOrWhiteSpace(stream.Profile))
  255. {
  256. if (IsMatch(path, stream.Profile))
  257. {
  258. return true;
  259. }
  260. }
  261. return false;
  262. }
  263. private IEnumerable<string> GetCustomIntroFiles(CinemaModeConfiguration options, bool enableCustomIntros, bool enableMediaInfoIntros)
  264. {
  265. var list = new List<string>();
  266. if (enableCustomIntros && !string.IsNullOrWhiteSpace(options.CustomIntroPath))
  267. {
  268. list.AddRange(_fileSystem.GetFilePaths(options.CustomIntroPath, true)
  269. .Where(_libraryManager.IsVideoFile));
  270. }
  271. if (enableMediaInfoIntros && !string.IsNullOrWhiteSpace(options.MediaInfoIntroPath))
  272. {
  273. list.AddRange(_fileSystem.GetFilePaths(options.MediaInfoIntroPath, true)
  274. .Where(_libraryManager.IsVideoFile));
  275. }
  276. return list.Distinct(StringComparer.OrdinalIgnoreCase);
  277. }
  278. public IEnumerable<string> GetAllIntroFiles()
  279. {
  280. return GetCustomIntroFiles(GetOptions(), true, true);
  281. }
  282. private bool IsSupporter
  283. {
  284. get { return _security.IsMBSupporter; }
  285. }
  286. public string Name
  287. {
  288. get { return "Default"; }
  289. }
  290. internal class ItemWithTrailer
  291. {
  292. internal BaseItem Item;
  293. internal ItemWithTrailerType Type;
  294. internal ILibraryManager LibraryManager;
  295. public IntroInfo IntroInfo
  296. {
  297. get
  298. {
  299. var id = Item.Id;
  300. if (Type == ItemWithTrailerType.ItemWithTrailer)
  301. {
  302. var hasTrailers = Item as IHasTrailers;
  303. if (hasTrailers != null)
  304. {
  305. id = hasTrailers.LocalTrailerIds.FirstOrDefault();
  306. }
  307. }
  308. return new IntroInfo
  309. {
  310. ItemId = id
  311. };
  312. }
  313. }
  314. }
  315. internal enum ItemWithTrailerType
  316. {
  317. ChannelTrailer,
  318. ItemWithTrailer
  319. }
  320. }
  321. public class CinemaModeConfigurationFactory : IConfigurationFactory
  322. {
  323. public IEnumerable<ConfigurationStore> GetConfigurations()
  324. {
  325. return new[]
  326. {
  327. new ConfigurationStore
  328. {
  329. ConfigurationType = typeof(CinemaModeConfiguration),
  330. Key = "cinemamode"
  331. }
  332. };
  333. }
  334. }
  335. }