DefaultIntroProvider.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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.Controller.Localization;
  8. using MediaBrowser.Model.Configuration;
  9. using MediaBrowser.Model.Entities;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Threading.Tasks;
  15. using CommonIO;
  16. using MoreLinq;
  17. namespace MediaBrowser.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. if (config.EnableIntrosFromMoviesInLibrary)
  63. {
  64. trailerTypes.Add(TrailerType.LocalTrailer);
  65. }
  66. if (IsSupporter)
  67. {
  68. if (config.EnableIntrosFromUpcomingTrailers)
  69. {
  70. trailerTypes.Add(TrailerType.ComingSoonToTheaters);
  71. }
  72. if (config.EnableIntrosFromUpcomingDvdMovies)
  73. {
  74. trailerTypes.Add(TrailerType.ComingSoonToDvd);
  75. }
  76. if (config.EnableIntrosFromUpcomingStreamingMovies)
  77. {
  78. trailerTypes.Add(TrailerType.ComingSoonToStreaming);
  79. }
  80. if (config.EnableIntrosFromSimilarMovies)
  81. {
  82. trailerTypes.Add(TrailerType.Archive);
  83. }
  84. }
  85. if (trailerTypes.Count > 0)
  86. {
  87. var trailerResult = _libraryManager.GetItemList(new InternalItemsQuery
  88. {
  89. IncludeItemTypes = new[] { typeof(Trailer).Name },
  90. TrailerTypes = trailerTypes.ToArray(),
  91. SimilarTo = item,
  92. IsPlayed = config.EnableIntrosForWatchedContent ? (bool?)null : false,
  93. MaxParentalRating = config.EnableIntrosParentalControl ? ratingLevel : null,
  94. BlockUnratedItems = config.EnableIntrosParentalControl ? new[] { UnratedItem.Trailer } : new UnratedItem[] { },
  95. Limit = config.TrailerLimit
  96. });
  97. candidates.AddRange(trailerResult.Select(i => new ItemWithTrailer
  98. {
  99. Item = i,
  100. Type = i.SourceType == SourceType.Channel ? ItemWithTrailerType.ChannelTrailer : ItemWithTrailerType.ItemWithTrailer,
  101. LibraryManager = _libraryManager
  102. }));
  103. }
  104. return GetResult(item, candidates, config);
  105. }
  106. private IEnumerable<IntroInfo> GetResult(BaseItem item, IEnumerable<ItemWithTrailer> candidates, CinemaModeConfiguration config)
  107. {
  108. var customIntros = !string.IsNullOrWhiteSpace(config.CustomIntroPath) ?
  109. GetCustomIntros(config) :
  110. new List<IntroInfo>();
  111. var mediaInfoIntros = !string.IsNullOrWhiteSpace(config.MediaInfoIntroPath) ?
  112. GetMediaInfoIntros(config, item) :
  113. new List<IntroInfo>();
  114. // Avoid implicitly captured closure
  115. return candidates.Select(i => i.IntroInfo)
  116. .Concat(customIntros.Take(1))
  117. .Concat(mediaInfoIntros);
  118. }
  119. private CinemaModeConfiguration GetOptions()
  120. {
  121. return _serverConfig.GetConfiguration<CinemaModeConfiguration>("cinemamode");
  122. }
  123. private List<IntroInfo> GetCustomIntros(CinemaModeConfiguration options)
  124. {
  125. try
  126. {
  127. return GetCustomIntroFiles(options, true, false)
  128. .OrderBy(i => Guid.NewGuid())
  129. .Select(i => new IntroInfo
  130. {
  131. Path = i
  132. }).ToList();
  133. }
  134. catch (IOException)
  135. {
  136. return new List<IntroInfo>();
  137. }
  138. }
  139. private IEnumerable<IntroInfo> GetMediaInfoIntros(CinemaModeConfiguration options, BaseItem item)
  140. {
  141. try
  142. {
  143. var hasMediaSources = item as IHasMediaSources;
  144. if (hasMediaSources == null)
  145. {
  146. return new List<IntroInfo>();
  147. }
  148. var mediaSource = _mediaSourceManager.GetStaticMediaSources(hasMediaSources, false)
  149. .FirstOrDefault();
  150. if (mediaSource == null)
  151. {
  152. return new List<IntroInfo>();
  153. }
  154. var videoStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
  155. var audioStream = mediaSource.MediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);
  156. var allIntros = GetCustomIntroFiles(options, false, true)
  157. .OrderBy(i => Guid.NewGuid())
  158. .Select(i => new IntroInfo
  159. {
  160. Path = i
  161. }).ToList();
  162. var returnResult = new List<IntroInfo>();
  163. if (videoStream != null)
  164. {
  165. returnResult.AddRange(GetMediaInfoIntrosByVideoStream(allIntros, videoStream).Take(1));
  166. }
  167. if (audioStream != null)
  168. {
  169. returnResult.AddRange(GetMediaInfoIntrosByAudioStream(allIntros, audioStream).Take(1));
  170. }
  171. returnResult.AddRange(GetMediaInfoIntrosByTags(allIntros, item.Tags).Take(1));
  172. return returnResult.DistinctBy(i => i.Path, StringComparer.OrdinalIgnoreCase);
  173. }
  174. catch (IOException)
  175. {
  176. return new List<IntroInfo>();
  177. }
  178. }
  179. private IEnumerable<IntroInfo> GetMediaInfoIntrosByVideoStream(List<IntroInfo> allIntros, MediaStream stream)
  180. {
  181. var codec = stream.Codec;
  182. if (string.IsNullOrWhiteSpace(codec))
  183. {
  184. return new List<IntroInfo>();
  185. }
  186. return allIntros
  187. .Where(i => IsMatch(i.Path, codec))
  188. .OrderBy(i => Guid.NewGuid());
  189. }
  190. private IEnumerable<IntroInfo> GetMediaInfoIntrosByAudioStream(List<IntroInfo> allIntros, MediaStream stream)
  191. {
  192. var codec = stream.Codec;
  193. if (string.IsNullOrWhiteSpace(codec))
  194. {
  195. return new List<IntroInfo>();
  196. }
  197. return allIntros
  198. .Where(i => IsAudioMatch(i.Path, stream))
  199. .OrderBy(i => Guid.NewGuid());
  200. }
  201. private IEnumerable<IntroInfo> GetMediaInfoIntrosByTags(List<IntroInfo> allIntros, List<string> tags)
  202. {
  203. return allIntros
  204. .Where(i => tags.Any(t => IsMatch(i.Path, t)))
  205. .OrderBy(i => Guid.NewGuid());
  206. }
  207. private bool IsMatch(string file, string attribute)
  208. {
  209. var filename = Path.GetFileNameWithoutExtension(file) ?? string.Empty;
  210. filename = Normalize(filename);
  211. if (string.IsNullOrWhiteSpace(filename))
  212. {
  213. return false;
  214. }
  215. attribute = Normalize(attribute);
  216. if (string.IsNullOrWhiteSpace(attribute))
  217. {
  218. return false;
  219. }
  220. return string.Equals(filename, attribute, StringComparison.OrdinalIgnoreCase);
  221. }
  222. private string Normalize(string value)
  223. {
  224. return value;
  225. }
  226. private bool IsAudioMatch(string path, MediaStream stream)
  227. {
  228. if (!string.IsNullOrWhiteSpace(stream.Codec))
  229. {
  230. if (IsMatch(path, stream.Codec))
  231. {
  232. return true;
  233. }
  234. }
  235. if (!string.IsNullOrWhiteSpace(stream.Profile))
  236. {
  237. if (IsMatch(path, stream.Profile))
  238. {
  239. return true;
  240. }
  241. }
  242. return false;
  243. }
  244. private IEnumerable<string> GetCustomIntroFiles(CinemaModeConfiguration options, bool enableCustomIntros, bool enableMediaInfoIntros)
  245. {
  246. var list = new List<string>();
  247. if (enableCustomIntros && !string.IsNullOrWhiteSpace(options.CustomIntroPath))
  248. {
  249. list.AddRange(_fileSystem.GetFilePaths(options.CustomIntroPath, true)
  250. .Where(_libraryManager.IsVideoFile));
  251. }
  252. if (enableMediaInfoIntros && !string.IsNullOrWhiteSpace(options.MediaInfoIntroPath))
  253. {
  254. list.AddRange(_fileSystem.GetFilePaths(options.MediaInfoIntroPath, true)
  255. .Where(_libraryManager.IsVideoFile));
  256. }
  257. return list.Distinct(StringComparer.OrdinalIgnoreCase);
  258. }
  259. public IEnumerable<string> GetAllIntroFiles()
  260. {
  261. return GetCustomIntroFiles(GetOptions(), true, true);
  262. }
  263. private bool IsSupporter
  264. {
  265. get { return _security.IsMBSupporter; }
  266. }
  267. public string Name
  268. {
  269. get { return "Default"; }
  270. }
  271. internal class ItemWithTrailer
  272. {
  273. internal BaseItem Item;
  274. internal ItemWithTrailerType Type;
  275. internal ILibraryManager LibraryManager;
  276. public IntroInfo IntroInfo
  277. {
  278. get
  279. {
  280. var id = Item.Id;
  281. if (Type == ItemWithTrailerType.ItemWithTrailer)
  282. {
  283. var hasTrailers = Item as IHasTrailers;
  284. if (hasTrailers != null)
  285. {
  286. id = hasTrailers.LocalTrailerIds.FirstOrDefault();
  287. }
  288. }
  289. return new IntroInfo
  290. {
  291. ItemId = id
  292. };
  293. }
  294. }
  295. }
  296. internal enum ItemWithTrailerType
  297. {
  298. ChannelTrailer,
  299. ItemWithTrailer
  300. }
  301. }
  302. public class CinemaModeConfigurationFactory : IConfigurationFactory
  303. {
  304. public IEnumerable<ConfigurationStore> GetConfigurations()
  305. {
  306. return new[]
  307. {
  308. new ConfigurationStore
  309. {
  310. ConfigurationType = typeof(CinemaModeConfiguration),
  311. Key = "cinemamode"
  312. }
  313. };
  314. }
  315. }
  316. }