DefaultIntroProvider.cs 13 KB

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